I’m writing a grammar for a language where a variable can be declared with the structure of: ‘let variable_name value’, where ‘variable_name’ is the name of the variable and ‘value’ is the value, and the value can be any literal, constant, etc., but it cannot be a built-in command/primitive. Parentheses are allowed around the value, it shouldn’t make a difference. However, if I try to test something like ‘let variable_name (command)’ (where ‘command’ is a built-in command/primitive), then ‘(’ is parsed as part of the value, and ‘command)’ is parsed as the next command in the program, and the apparent error is the lack of matching parentheses.
I would like the parentheses to be matched as much as possible, so ‘let variable_name (command)’ would be parsed as two command statements: ‘let variable_name’ and ‘(command)’, and then I can lint that the first is missing an argument.
Is there a way to prioritize grammar rules that only sometimes overlap, such as giving matching parentheses priority over ensuring command statements have enough arguments? I’ve tried precedence, but I can’t get it to work, so the relevant rules from my grammar file at this point are:
@precedence {
paren @left,
lastArgs @left
}
NewVariableDeclaration {
Let newVariableName !lastArgs Value
}
Value {
Parenthetical<Value> |
List |
CodeBlock |
VariableName |
Literal
}
Parenthetical<content> {
OpenParen !paren content !paren CloseParen
}