grammars-v4 icon indicating copy to clipboard operation
grammars-v4 copied to clipboard

Lua's Lexer handles block comment issues

Open Dongyang0810 opened this issue 1 year ago • 2 comments

--[[some comment ]] local A = 10 unrecognized assignment expression 'local A = 10' image

Dongyang0810 avatar Aug 08 '23 13:08 Dongyang0810

It seems the LINE_COMMENT:

LINE_COMMENT
    : '--' SingleLineInputCharacter* -> channel(HIDDEN)
    ;

fragment
SingleLineInputCharacter
    : ~[\r\n\u0085\u2028\u2029]
    ;

gobbles up the entire input --[[some comment ]] local A = 10.

It should not be able to match --[[. Something like this ought to do the trick, I guess:

LINE_COMMENT
    : ( '--' ( ~[\r\n[] SingleLineInputCharacter* )?
      | '--[' ( ~[\r\n[] SingleLineInputCharacter* )?
      ) -> channel(HIDDEN)
    ;

A quick test with the rule above:

String source =
        "--[[some comment ]] local A = 10\n" +
        "-- normal line comment\n" +
        "--[[aaa]]AA=1\n" +
        "--[a]=11\n" +
        "--[\n" +
        "--\n" +
        "a=1";
LuaLexer lexer = new LuaLexer(CharStreams.fromString(source));
CommonTokenStream stream = new CommonTokenStream(lexer);
stream.fill();

System.out.println(source + "\n");

for (Token t : stream.getTokens()) {
    System.out.printf("%-20s '%s'%n",
            LuaLexer.VOCABULARY.getSymbolicName(t.getType()),
            t.getText().replace("\n", "\\n"));
}

suggests it is correct:

--[[some comment ]] local A = 10
-- normal line comment
--[[aaa]]AA=1
--[a]=11
--[
--
a=1

COMMENT              '--[[some comment ]]'
null                 'local'
NAME                 'A'
null                 '='
INT                  '10'
LINE_COMMENT         '-- normal line comment'
COMMENT              '--[[aaa]]'
NAME                 'AA'
null                 '='
INT                  '1'
LINE_COMMENT         '--[a]=11'
LINE_COMMENT         '--['
LINE_COMMENT         '--'
NAME                 'a'
null                 '='
INT                  '1'
EOF                  '<EOF>'

However, my Lua is rather rusty, so I'll leave the PR/fix to someone who knows more Lua than me.

bkiers avatar Aug 24 '23 14:08 bkiers

Oh,Thank you! I previously used

LINE_COMMENT
: '--' ('[')? (~'[') SingleLineInputCharacter* -> channel(HIDDEN)
;

But there are still problems, the LINE_COMMENT you provided looks correct.

Dongyang0810 avatar Sep 16 '23 03:09 Dongyang0810