Skip to content

Commit

Permalink
feat: improve parser error regarding to using parentheses in deflayer
Browse files Browse the repository at this point in the history
  • Loading branch information
rszyma committed Nov 23, 2023
1 parent 5dc46c2 commit e088e0b
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions parser/src/cfg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -807,8 +807,10 @@ fn parse_defsrc(expr: &[SExpr], defcfg: &CfgOptions) -> Result<(MappedKeys, Vec<
type LayerIndexes = HashMap<String, usize>;
type Aliases = HashMap<String, &'static KanataAction>;

/// Returns layer names and their indexes into the keyberon layout. This also checks that all
/// layers have the same number of items as the defsrc. Also ensures that there are no duplicate layer names.
/// Returns layer names and their indexes into the keyberon layout. This also checks that:
/// - All layers have the same number of items as the defsrc,
/// - There are no duplicate layer names
/// - Parentheses weren't used directly or kmonad-style escapes for parentheses weren't used.
fn parse_layer_indexes(exprs: &[Spanned<Vec<SExpr>>], expected_len: usize) -> Result<LayerIndexes> {
let mut layer_indexes = HashMap::default();
for (i, expr) in exprs.iter().enumerate() {
Expand All @@ -823,7 +825,19 @@ fn parse_layer_indexes(exprs: &[Spanned<Vec<SExpr>>], expected_len: usize) -> Re
if layer_indexes.get(&layer_name).is_some() {
bail_expr!(layer_expr, "duplicate layer name: {}", layer_name);
}
let num_actions = subexprs.count();
// Check if user tried to map parentheses by using them directly - `(` and `)`
// or escaped them like in kmonad - `\(` and `\)`.
for subexpr in subexprs {
if subexpr.list(None).is_some_and(|l| {
l.is_empty()
|| l.len() == 1
&& l.first()
.is_some_and(|s| s.atom(None).is_some_and(|atom| atom == "\\"))
}) {
bail_expr!(subexpr, "If you meant to map keys to `(` and `)`, you have to use `S-0` and `S-9` instead (for US layout).\nSee https://github.com/jtroo/kanata/issues/163 for context.",)
}
}
let num_actions = expr.t.len() - 1;
if num_actions != expected_len {
bail_span!(
expr,
Expand Down

0 comments on commit e088e0b

Please sign in to comment.