antlr4rust icon indicating copy to clipboard operation
antlr4rust copied to clipboard

Splitting lexer and parser grammar into separate files causes error

Open StuartReavley opened this issue 4 years ago • 4 comments

To repro:

  • use grammars here: https://github.com/sepp2k/antlr4-string-interpolation-examples/tree/master/with-duplication

  • run commands

    • java -jar ./antlr4-4.8-2-SNAPSHOT-complete.jar -Dlanguage=Rust StringLexer.g4
    • java -jar ./antlr4-4.8-2-SNAPSHOT-complete.jar -Dlanguage=Rust StringParser.g4
  • Observe errors, e.g: error[E0412]: cannot find type `StringParserParserContextType` in this scope --> src/parsing/jasm/gen/stringparser.rs:532:49 | 156 | pub struct StringParserContextType; | ----------------------------------- similarly named struct `StringParserContextType` defined here ... 532 | fn RPAR(&self) -> Option<Rc<TerminalNode<'input,StringParserParserContextType>>> where Self:Sized{ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |

As I understand, there is no way to use lexer modes (e.g. in that example) without separate files. Any way to get this to work?

Thank you! Stuart

StuartReavley avatar Dec 02 '20 21:12 StuartReavley

@StuartReavley - did you try adding both files to the command line ?

eg:

cd grammars && java -jar /home/rbuckland/.m2/repository/org/antlr/antlr4/4.8-2-SNAPSHOT/antlr4-4.8-2-SNAPSHOT-complete.jar -Dlanguage=Rust -o ../src/gen PhpParser.g4 PhpLexer.g4 

image

image

rbuckland avatar Jan 04 '21 04:01 rbuckland

I hit the same problem with the generated code of a PL/SQL grammar even if I used the lexer and parser grammar together.

antlr4 -Dlanguage=Rust -o src -Xexact-output-dir grammars/PlSqlLexer.g4 grammars/PlSqlParser.g4

Compiling the generated code

        error[E0405]: cannot find trait `PlSqlParserParserContext` in this scope
              --> src/plsqlparser.rs:106025:14
               |
        4098   | / pub trait PlSqlParserContext<'input>:
        4099   | |     for<'x> Listenable<dyn PlSqlParserListener<'input> + 'x > + 
        4100   | |     ParserRuleContext<'input, TF=LocalTokenFactory<'input>, Ctx=PlSqlParserContextType>
        4101   | | {}
               | |__- similarly named trait `PlSqlParserContext` defined here
        ...
        106025 |   impl<'input> PlSqlParserParserContext<'input> for Table_ref_aux_internalContextAll<'input>{}
               |                ^^^^^^^^^^^^^^^^^^^^^^^^ help: a trait with a similar name exists: `PlSqlParserContext`

        Some errors have detailed explanations: E0405, E0424.
        For more information about an error, try `rustc --explain E0405`.

Renaming the generated trait to PLSqlParserContext fixed it

sed -i s/PlSqlParserParserContext/PlSqlParserContext/g src/plsqlparser.rs

mh182 avatar Mar 28 '22 09:03 mh182