Skip to content

A // comment that ends the input is a parse error, because the lexer rule requires the newline #1039

Description

@Rafael-SOWNet

COMMENT in Sources/AngouriMath/Core/Antlr/AngouriMath.g is

COMMENT: ('//' ~[\r\n]* '\r'? '\n' | '/*' .*? '*/') -> skip ;

The // alternative only matches when a newline follows, so a line comment that ends the input never matches at all and its / reaches the parser as an operator.

Measured on a build of master (9b6fb5b):

MathS.FromString("x + 1 // done");     // UnhandledParseException: line 1:7 extraneous input '/'
MathS.FromString("x + 1 // done\n");   // x + 1
MathS.FromString("x + 1 /* done */");  // x + 1

The end of the input is where a comment is most likely to be written — the last line of a file, or a single-line string handed to FromString — and the block form carries no such condition, so this reads as a missing optional rather than a decision. Making the newline optional (('\r'? '\n')?) is what I would expect to fix it.

Not fixed in the documentation PR that found it, since a grammar change means regenerating the ANTLR parser under Docs/Contributing/ImproveParser.md and is its own change with its own risk. Written down as behaviour in Docs/Usage/Syntax.md meanwhile.

Found while deriving what the grammar accepts for #746 item 8.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions