rrevenantt / antlr4rust

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

How to use listeners and visitors in 0.2? #44

Closed DoctorBracewell closed 2 years ago

DoctorBracewell commented 2 years ago

The code in my_test.rs indicates that a parser should have an add_parse_listener function that a listener can be paused to to run during parsing?

However, my parser in the code below has no such function:

let tf = CommonTokenFactory::default();
let input_stream = InputStream::new("test");
let lexer = OCRLexer::new_with_token_factory(input_stream, &tf);
let token_stream = CommonTokenStream::new(lexer);
let mut parser = OCRParser::new(token_stream);

// parser has no add_parse_listener

How can I add a listener to the parser, similar to these lines from my_test.rs?

let mut parser = CSVParser::new(token_source);
parser.add_parse_listener(Box::new(Listener {}));
println!("\nstart parsing parser_test_csv");
let result = parser.csvFile();
assert!(result.is_ok());
assert_eq!(
    result.unwrap().to_string_tree(&*parser),
    "(csvFile (hdr (row (field V123) , (field V2) \\n)) (row (field d1) , (field d2) \\n))"
);
rrevenantt commented 2 years ago

add_parse_listener is available via DerefMut<Target=BaseParser<_>> that is implemented on every parser, so i am not sure what exactly do you mean. Is there a particular error happening when you try to call it?

DoctorBracewell commented 2 years ago

Ah, for some reason it was the intellisense in my IDE not picking up that function, even though it still compiles and runs. Do you know why that might be the case?

rrevenantt commented 2 years ago

¯_(ツ)_/¯ it works in intellij for me

DoctorBracewell commented 2 years ago

Weird, VSCode isn't too happy about it. Oh well!