antlr / antlr-php-runtime

PHP Runtime for ANTLR4
BSD 3-Clause "New" or "Revised" License
81 stars 19 forks source link

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

Open kaby76 opened 1 year ago

kaby76 commented 1 year ago

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).

parrt commented 1 year ago

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

parrt commented 1 year ago

trace is definitely for parser tracing.