According to the grammar defined here , the reason the version with the unconverted version is invalid but enclosed in brackets is because the syntax tree takes a different path and expects closing brackets ) because there should not be another character in this context.
In the first case:
functioncall ::= prefixexp args | prefixexp `:ยด Name args prefixexp => prefixexp ::= var | functioncall | `(ยด exp `)ยด var -> print THEREFORE prefixexp -> print args => args ::= `(ยด [explist] `)ยด | tableconstructor | String match '(' explist => explist ::= {exp `,ยด} exp exp => exp ::= nil | false | true | Number | String | `...ยด | function | prefixexp | tableconstructor | exp binop exp | unop exp tableconstructor => explist-> {'foo','bar'} THEREFORE explist = {'foo','bar'} match ')' ERROR!!! found '[' expected ')'
On the other hand, with parentheses:
functioncall ::= prefixexp args | prefixexp `:ยด Name args prefixexp => prefixexp ::= var | functioncall | `(ยด exp `)ยด var -> print THEREFORE prefixexp -> print args => args ::= `(ยด [explist] `)ยด | tableconstructor | String match '(' explist => explist ::= {exp `,ยด} exp exp => exp ::= nil | false | true | Number | String | `...ยด | function | prefixexp | tableconstructor | exp binop exp | unop exp prefixexp => prefixexp ::= var | functioncall | `(ยด exp `)ยด var => var ::= Name | prefixexp `[ยด exp `]ยด | prefixexp `.ยด Name prefixexp => prefixexp ::= var | functioncall | `(ยด exp `)ยด match '(' exp => exp ::= nil | false | true | Number | String | `...ยด | function | prefixexp | tableconstructor | exp binop exp | unop exp tableconstructor => THEREFORE exp = {'foo','bar'} match ')' THEREFORE prefixexp = ({'foo','bar'}) match '[' exp => Number = 1 match ']' THEREFORE VAR = ({'foo','bar'})[1] THEREFORE prefixexp = VAR THEREFOER exp = VAR THEREFORE explist = VAR match ')' THEREFORE args = (VAR) => print(VAR)
source share