rrevenantt / antlr4rust

ANTLR4 parser generator runtime for Rust programming laguage
Other
404 stars 70 forks source link

I try to use visitor pattern, but failed. #10

Open mrunix opened 4 years ago

mrunix commented 4 years ago

g4 file:

grammar CSV;

csvFile: hdr row+ ;
hdr : row ;

row : field (',' field)* '\r'? '\n' ;

field
    : TEXT
    | STRING
    |
    ;

TEXT   : ~[,\n\r"]+ ;
STRING : '"' ('""'|~'"')* '"' ; // quote-quote is an escaped quote

generate codes:

java -cp /opt/antlr/antlr4-4.8-2-SNAPSHOT-complete.jar org.antlr.v4.Tool -Dlanguage=Rust CSV.g4  -no-listener -visitor

When I compile the codes, got errors:

    Compiling lixcep v0.0.1 (/home/lix/works/repos/project/lix/lixcep)
error: expected one of `:`, `@`, or `|`, found `)`
  --> src/csv_visitor/csvvisitor.rs:17:47
   |
17 |     fn visitCsvFile(ctx: CSVParser.CsvFileContext) -> T;
   |                                                  ^ expected one of `:`, `@`, or `|`
   |
   = note: anonymous parameters are removed in the 2018 edition (see RFC 1685)
help: if this is a parameter name, give it a type
   |
17 |     fn visitCsvFile(ctx: CSVParser.CsvFileContext: TypeName) -> T;
   |                                    ^^^^^^^^^^^^^^^^^^^^^^^^
help: if this is a type, explicitly ignore the parameter name
   |
17 |     fn visitCsvFile(ctx: CSVParser._: CsvFileContext) -> T;
   |                                    ^^^^^^^^^^^^^^^^^

error: expected one of `!`, `(`, `)`, `+`, `,`, `::`, or `<`, found `.`
  --> src/csv_visitor/csvvisitor.rs:17:32
   |
17 |     fn visitCsvFile(ctx: CSVParser.CsvFileContext) -> T;
   |                                   ^
   |                                   |
   |                                   expected one of 7 possible tokens
   |                                   help: missing `,`

error: expected one of `:`, `@`, or `|`, found `)`
  --> src/csv_visitor/csvvisitor.rs:23:39
   |
23 |     fn visitHdr(ctx: CSVParser.HdrContext) -> T;
   |                                          ^ expected one of `:`, `@`, or `|`
   |
   = note: anonymous parameters are removed in the 2018 edition (see RFC 1685)
help: if this is a parameter name, give it a type
   |
23 |     fn visitHdr(ctx: CSVParser.HdrContext: TypeName) -> T;
   |                                ^^^^^^^^^^^^^^^^^^^^
help: if this is a type, explicitly ignore the parameter name
   |
23 |     fn visitHdr(ctx: CSVParser._: HdrContext) -> T;
   |                                ^^^^^^^^^^^^^

error: expected one of `!`, `(`, `)`, `+`, `,`, `::`, or `<`, found `.`
  --> src/csv_visitor/csvvisitor.rs:23:28
   |
23 |     fn visitHdr(ctx: CSVParser.HdrContext) -> T;
   |                               ^
   |                               |
   |                               expected one of 7 possible tokens
   |                               help: missing `,`

error: expected one of `:`, `@`, or `|`, found `)`
  --> src/csv_visitor/csvvisitor.rs:29:39
   |
29 |     fn visitRow(ctx: CSVParser.RowContext) -> T;
   |                                          ^ expected one of `:`, `@`, or `|`
   |
   = note: anonymous parameters are removed in the 2018 edition (see RFC 1685)
help: if this is a parameter name, give it a type
   |
29 |     fn visitRow(ctx: CSVParser.RowContext: TypeName) -> T;
   |                                ^^^^^^^^^^^^^^^^^^^^
help: if this is a type, explicitly ignore the parameter name
   |
29 |     fn visitRow(ctx: CSVParser._: RowContext) -> T;
   |                                ^^^^^^^^^^^^^

error: expected one of `!`, `(`, `)`, `+`, `,`, `::`, or `<`, found `.`
  --> src/csv_visitor/csvvisitor.rs:29:28
   |
29 |     fn visitRow(ctx: CSVParser.RowContext) -> T;
   |                               ^
   |                               |
   |                               expected one of 7 possible tokens
   |                               help: missing `,`

error: expected one of `:`, `@`, or `|`, found `)`
  --> src/csv_visitor/csvvisitor.rs:35:43
   |
35 |     fn visitField(ctx: CSVParser.FieldContext) -> T;
   |                                              ^ expected one of `:`, `@`, or `|`
   |
   = note: anonymous parameters are removed in the 2018 edition (see RFC 1685)
help: if this is a parameter name, give it a type
   |
35 |     fn visitField(ctx: CSVParser.FieldContext: TypeName) -> T;
   |                                  ^^^^^^^^^^^^^^^^^^^^^^
help: if this is a type, explicitly ignore the parameter name
   |
35 |     fn visitField(ctx: CSVParser._: FieldContext) -> T;
   |                                  ^^^^^^^^^^^^^^^

error: expected one of `!`, `(`, `)`, `+`, `,`, `::`, or `<`, found `.`
  --> src/csv_visitor/csvvisitor.rs:35:30
   |
35 |     fn visitField(ctx: CSVParser.FieldContext) -> T;
   |                                 ^
   |                                 |
   |                                 expected one of 7 possible tokens
   |                                 help: missing `,`

error[E0433]: failed to resolve: use of undeclared type or module `antlr_runtime`
 --> src/csv_visitor/csvvisitor.rs:2:5
  |
2 | use antlr_runtime::tree::ParseTreeVisitor;
  |     ^^^^^^^^^^^^^ use of undeclared type or module `antlr_runtime`

error[E0405]: cannot find trait `ParseTreeVisitor` in this scope
  --> src/csv_visitor/csvvisitor.rs:11:27
   |
11 | pub trait  CSVVisitor<T>: ParseTreeVisitor<T> {
   |                           ^^^^^^^^^^^^^^^^ not found in this scope

error[E0412]: cannot find type `CSVParser` in this scope
  --> src/csv_visitor/csvvisitor.rs:17:23
   |
17 |     fn visitCsvFile(ctx: CSVParser.CsvFileContext) -> T;
   |                          ^^^^^^^^^ not found in this scope
   |
help: consider importing this struct
   |
2  | use crate::csv_visitor::CSVParser;
   |

error[E0412]: cannot find type `CSVParser` in this scope
  --> src/csv_visitor/csvvisitor.rs:23:19
   |
23 |     fn visitHdr(ctx: CSVParser.HdrContext) -> T;
   |                      ^^^^^^^^^ not found in this scope
   |
help: consider importing this struct
   |
2  | use crate::csv_visitor::CSVParser;
   |

error[E0412]: cannot find type `CSVParser` in this scope
  --> src/csv_visitor/csvvisitor.rs:29:19
   |
29 |     fn visitRow(ctx: CSVParser.RowContext) -> T;
   |                      ^^^^^^^^^ not found in this scope
   |
help: consider importing this struct
   |
2  | use crate::csv_visitor::CSVParser;
   |

error[E0412]: cannot find type `CSVParser` in this scope
  --> src/csv_visitor/csvvisitor.rs:35:21
   |
35 |     fn visitField(ctx: CSVParser.FieldContext) -> T;
   |                        ^^^^^^^^^ not found in this scope
   |
help: consider importing this struct
   |
2  | use crate::csv_visitor::CSVParser;
   |

warning: unused import: `antlr_rust::common_token_stream::CommonTokenStream`
 --> src/csv_visitor/mod.rs:1:5
  |
1 | use antlr_rust::common_token_stream::CommonTokenStream;
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: unused import: `antlr_rust::input_stream::InputStream`
 --> src/csv_visitor/mod.rs:2:5
  |
2 | use antlr_rust::input_stream::InputStream;
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

warning: unused imports: `ParseTreeListener`, `ParseTree`
 --> src/csv_visitor/mod.rs:3:24
  |
3 | use antlr_rust::tree::{ParseTree, ParseTreeListener};
  |                        ^^^^^^^^^  ^^^^^^^^^^^^^^^^^

warning: unused import: `csvlexer::CSVLexer`
 --> src/csv_visitor/mod.rs:8:5
  |
8 | use csvlexer::CSVLexer;
  |     ^^^^^^^^^^^^^^^^^^

warning: unused import: `csvparser::*`
 --> src/csv_visitor/mod.rs:9:5
  |
9 | use csvparser::*;
  |     ^^^^^^^^^^^^

warning: unused import: `csvvisitor::*`
  --> src/csv_visitor/mod.rs:10:5
   |
10 | use csvvisitor::*;
   |     ^^^^^^^^^^^^^

error: aborting due to 14 previous errors; 6 warnings emitted

Some errors have detailed explanations: E0405, E0412, E0433.
For more information about an error, try `rustc --explain E0405`.
error: could not compile `lixcep`.

To learn more, run the command again with --verbose.

Any suggestions?

rrevenantt commented 4 years ago

0.1 version does not support visitors yet. You might be able to workaround by walking through generated tree with listener and ParseTreeWalker. Visitors are going to be supported in 0.2 which is mostly finished.

mrunix commented 4 years ago

Feel so sad.

I have to use the visitor pattern in my project, when can you complete it? I am looking forward. Thank you!