Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parser.setTrace(true) does not have the same semantics as with other targets #33

Open
kaby76 opened this issue Dec 17, 2022 · 2 comments
Labels
bug Something isn't working

Comments

@kaby76
Copy link

kaby76 commented Dec 17, 2022

I mistakenly was trying to test "tracing" in a parser cross targets, and misunderstood what the "trace" levers in the API actually were, and ran into this bug.

See this.

I wrote a parser driver for the PHP target that performs a $parser.setTrace(true);, then ran it. The output is unusable because the trace output is all on one line.

CSharp:

$ ./bin/Debug/net6.0/Test.exe ../examples/access.aql
enter   arangodb_query, LT(1)=RETURN
enter   data_query, LT(1)=RETURN
enter   data_access_query, LT(1)=RETURN
enter   return_expr, LT(1)=RETURN
consume [@0,0:5='RETURN',<9>,1:0] rule return_expr
enter   expr, LT(1)=1
enter   literal, LT(1)=1
enter   numeric_literal, LT(1)=1
consume [@2,7:7='1',<86>,1:7] rule numeric_literal
exit    numeric_literal, LT(1)=<EOF>
exit    literal, LT(1)=<EOF>
exit    expr, LT(1)=<EOF>
exit    return_expr, LT(1)=<EOF>
exit    data_access_query, LT(1)=<EOF>
exit    data_query, LT(1)=<EOF>
consume [@4,9:8='<EOF>',<-1>,2:0] rule arangodb_query
exit    arangodb_query, LT(1)=<EOF>
CSharp 0 ../examples/access.aql success 0.0505942
Total Time: 0.0921106

PHP:

$ php -d memory_limit=2G Test.php -file ../examples/access.aql
enter   arangodb_query, LT(1)=RETURNenter   data_query, LT(1)=RETURNenter   data_access_query, LT(1)=RETURNenter   return_expr, LT(1)=RETURNconsume [@0,0:5='RETURN',<9>,1:0] rule return_exprenter   expr, LT(1)=1enter   literal, LT(1)=1enter   numeric_literal, LT(1)=1consume [@2,7:7='1',<86>,1:7] rule numeric_literalexit    numeric_literal, LT(1)=<EOF>exit    literal, LT(1)=<EOF>exit    expr, LT(1)=<EOF>exit    return_expr, LT(1)=<EOF>exit    data_access_query, LT(1)=<EOF>exit    data_query, LT(1)=<EOF>consume [@4,9:8='<EOF>',<-1>,2:0] rule arangodb_queryexit    arangodb_query, LT(1)=<EOF>

The problem is this code in the PHP runtime: https://github.com/antlr/antlr-php-runtime/blob/dev/src/ParserTraceListener.php#L25

Compare that to CSharp: https://github.com/antlr/antlr4/blob/76fa05c21b12b96a6c12a0a82e611ed9d87d5af4/runtime/CSharp/src/Parser.cs#L32

WriteLine() in C# outputs a newline character, whereas echo in PHP does not.

The easy fix is to just add print("\n");. I don't know enough about PHP to know what IO function outputs an OS-dependent newline ("\n" Linux vs "\r\n" Windows vs "\r" Mac).

@kaby76 kaby76 added the bug Something isn't working label Dec 17, 2022
@kaby76 kaby76 changed the title Parser.setTrace(true) does not have the same semantics as with all other targets Parser.setTrace(true) does not have the same semantics as with other targets Dec 17, 2022
@parrt
Copy link
Member

parrt commented Dec 17, 2022

I think that shouldn't be trace but ParserATNSimulator.trace_atn_sim = true;

@parrt
Copy link
Member

parrt commented Dec 17, 2022

trace is definitely for parser tracing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants