Rubberduck: Parser Error: comma when Erasing two Arrays

Created on 24 Mar 2016  路  8Comments  路  Source: rubberduck-vba/Rubberduck

A new module with the following code:

Public Sub EraseTwoArrays()

Dim someArray() As String
Dim someOtherArray() as String

Erase someArray(), someOtherArray()

End Sub

yeilds the following error:
msg: "extraneous input ',' expecting {, '!', '.', ACCESS, ADDRESSOF, ALIAS, AND, ATTRIBUTE, APPACTIVATE, APPEND, AS, BEGIN, BEEP, BINARY, BOOLEAN, BYVAL, BYREF, BYTE, CALL, CASE, CHDIR, CHDRIVE, CLASS, CLOSE, COLLECTION, CONST, DATABASE, DATE, DECLARE, DEFBOOL, DEFBYTE, DEFDATE, DEFDBL, DEFDEC, DEFCUR, DEFINT, DEFLNG, DEFOBJ, DEFSNG, DEFSTR, DEFVAR, DELETESETTING, DIM, DO, DOUBLE, EACH, ELSE, ELSEIF, END_ENUM, END_FUNCTION, END_IF, END_PROPERTY, END_SELECT, END_SUB, END_TYPE, END_WITH, END, ENUM, EQV, ERASE, ERROR, EVENT, EXIT_DO, EXIT_FOR, EXIT_FUNCTION, EXIT_PROPERTY, EXIT_SUB, FALSE, FILECOPY, FRIEND, FOR, FUNCTION, GET, GLOBAL, GOSUB, GOTO, IF, IMP, IMPLEMENTS, IN, INPUT, IS, INTEGER, KILL, LOAD, LOCK, LONG, LOOP, LEN, LET, LIB, LIKE, LINE_INPUT, LSET, ME, MID, MKDIR, MOD, NAME, NEXT, NEW, NOT, NOTHING, NULL, ON, ON_ERROR, ON_LOCAL_ERROR, OPEN, OPTIONAL, OPTION_BASE, OPTION_EXPLICIT, OPTION_COMPARE, OPTION_PRIVATE_MODULE, OR, OUTPUT, PARAMARRAY, PRESERVE, PRINT, PRIVATE, PROPERTY_GET, PROPERTY_LET, PROPERTY_SET, PUBLIC, PUT, RANDOM, RANDOMIZE, RAISEEVENT, READ, REDIM, REM, RESET, RESUME, RETURN, RMDIR, RSET, SAVEPICTURE, SAVESETTING, SEEK, SELECT, SENDKEYS, SET, SETATTR, SHARED, SINGLE, SPC, STATIC, STEP, STOP, STRING, SUB, TAB, TEXT, THEN, TIME, TO, TRUE, TYPE, TYPEOF, UNLOAD, UNLOCK, UNTIL, VARIANT, VERSION, WEND, WHILE, WIDTH, WITH, WITHEVENTS, WRITE, XOR, '&', '/', '\', '=', '>=', '>', '<=', '(', '<', '-', '*', '<>', '+', '^', NEWLINE, REMCOMMENT, COMMENT, ':', WS, IDENTIFIER}"

offending symbol: {[@33,109:109=',',<7>,6:17]}

Line 6, character 17 + 1 (character 18)


A workaround to

Public Sub EraseTwoArrays()

Dim someArray() As String
Dim someOtherArray() As String

Erase someArray()
Erase someOtherArray()

End Sub

fixes the issue temporarily

antlr bug critical

Most helpful comment

The grammar distinguishes statements and functions. For each statement, there is a specific parser rule in the grammar according to the official Visual Basic language reference
https://msdn.microsoft.com/en-us/library/aa338153%28v=vs.60%29.aspx
In contrast, functions are not defined as parts of the language and thus not part of the grammar.

E. g. the Erase Statement is defined in
https://msdn.microsoft.com/en-us/library/aa243360%28v=vs.60%29.aspx
just next to typical control structures and in contrast to functions such as the Format function
https://msdn.microsoft.com/en-us/library/aa262745%28v=vs.60%29.aspx
which therefore is not part of the grammar.

All 8 comments

The parser rule that defines the Erase statement syntax simply doesn't handle it - congratulations, you've found one of the [very] few bugs left in the grammar!

eraseStmt : ERASE WS valueStmt;

The fix is pretty simple: we need to allow multiple valueStmt's.

soo fix would be:

eraseStmt : ERASE WS valueStmtList
valueStmtList : valueStmt (WS? "," WS? valueStmtList)

Or is that basic recursion again a problem for ANTLR?

Why are built-in functions like these treated specially? Couldn't we just treat them as normal function calls ? That is, get rid of the eraseStmt and let it be parsed as a normal procedure call. What are the reasons against such an approach?

@autoboosh very good question. might need to ask the original author for that, @uwol - I believe Rubberduck _is_ using _some_ of these "special-cased" parser rules to implement inspections, but I can't think of one off the top of my head ATM.

@autoboosh well they _could_ be treated as function calls, but they're not part of the VBA standard library (AFAIK), so they wouldn't resolve to a known function anyway. It doesn't hurt either way, but having it in the grammar gives us a handle if we ever want to inspect things.

The grammar distinguishes statements and functions. For each statement, there is a specific parser rule in the grammar according to the official Visual Basic language reference
https://msdn.microsoft.com/en-us/library/aa338153%28v=vs.60%29.aspx
In contrast, functions are not defined as parts of the language and thus not part of the grammar.

E. g. the Erase Statement is defined in
https://msdn.microsoft.com/en-us/library/aa243360%28v=vs.60%29.aspx
just next to typical control structures and in contrast to functions such as the Format function
https://msdn.microsoft.com/en-us/library/aa262745%28v=vs.60%29.aspx
which therefore is not part of the grammar.

A non-recursive fix would be

eraseStmt : ERASE WS valueStmtList 
valueStmtList : valueStmt (WS? "," WS? valueStmt)*
Was this page helpful?
0 / 5 - 0 ratings

Related issues

philippetev picture philippetev  路  3Comments

ghost picture ghost  路  3Comments

SteGriff picture SteGriff  路  3Comments

Gener4tor picture Gener4tor  路  3Comments

ThunderFrame picture ThunderFrame  路  3Comments