CSTParser.jl icon indicating copy to clipboard operation
CSTParser.jl copied to clipboard

Problem parsing in JuliaFormatter

Open guilhermebodin opened this issue 2 years ago • 3 comments

I had an error in JuliaFormatter and it might be a problem with the parser https://github.com/domluna/JuliaFormatter.jl/issues/642#issuecomment-1271789498

julia> format_text("@constraint(Lower(model), +x[1] + x[2] <= 2)")
ERROR: Error while PARSING formatted text:

1 @constraint(Lower(model), x[1] + x[2] +  <= 2)

...

Error occurred on line 1, offset 45 of formatted text.

The error might not be precisely on this line but it should be in the region of the code block. Try commenting the region out and see if that removes the error.
Stacktrace:
 [1] error(s::String)
   @ Base .\error.jl:33
 [2] format_text(cst::CSTParser.EXPR, style::DefaultStyle, s::JuliaFormatter.State)
   @ JuliaFormatter C:\Users\guilhermebodin\.julia\packages\JuliaFormatter\xKNsr\src\JuliaFormatter.jl:699
 [3] format_text(text::String, style::DefaultStyle, opts::JuliaFormatter.Options)
   @ JuliaFormatter C:\Users\guilhermebodin\.julia\packages\JuliaFormatter\xKNsr\src\JuliaFormatter.jl:632
 [4] #format_text#238
   @ C:\Users\guilhermebodin\.julia\packages\JuliaFormatter\xKNsr\src\JuliaFormatter.jl:604 [inlined]
 [5] format_text
   @ C:\Users\guilhermebodin\.julia\packages\JuliaFormatter\xKNsr\src\JuliaFormatter.jl:602 [inlined]
 [6] #format_text#237
   @ C:\Users\guilhermebodin\.julia\packages\JuliaFormatter\xKNsr\src\JuliaFormatter.jl:598 [inlined]
 [7] format_text(text::String)
   @ JuliaFormatter C:\Users\guilhermebodin\.julia\packages\JuliaFormatter\xKNsr\src\JuliaFormatter.jl:598
 [8] top-level scope
   @ REPL[13]:1

guilhermebodin avatar Oct 07 '22 20:10 guilhermebodin

This looks like a bug in JuliaFormatter to me. As far as I can tell it seems to reformat the code to

@constraint(Lower(model), x[1] + x[2] +  <= 2)

which is not valid Julia code because of the + <= part, right? So, at that point it is expected that this isn't properly parsed?

CC @domluna

Closing for now, if I'm wrong just let me know here :)

davidanthoff avatar Oct 15 '22 14:10 davidanthoff

@davidanthoff

julia> s = """
       +x[1] + x[2] <= 2
       """
"+x[1] + x[2] <= 2\n"

julia> x = CSTParser.parse(s)
  1:18  call
  1:3    OP: <=
  4:16   call
  4:4     OP: +
  5:9     ref
  5:5      x
  6:6      INTEGER: 1
 10:14    ref
 10:10     x
 11:11     INTEGER: 2
 17:18   INTEGER: 2

julia> x[1]
  1:13  call
  1:1    OP: +
  2:6    ref
  2:2     x
  3:3     INTEGER: 1
  7:11   ref
  7:7     x
  8:8     INTEGER: 2

julia> collect(x[1])
4-element Vector{Any}:
   1:5   ref
  1:1    x
  2:2    INTEGER: 1
   1:1   OP: +
   1:5   ref
  1:1    x
  2:2    INTEGER: 2
   1:2   OP: +

julia> s = """
       +x[1] + x[2] <= 2
       """
"+x[1] + x[2] <= 2\n"

julia> x = CSTParser.parse(s)
  1:18  call
  1:3    OP: <=
  4:16   call
  4:4     OP: +
  5:9     ref
  5:5      x
  6:6      INTEGER: 1
 10:14    ref
 10:10     x
 11:11     INTEGER: 2
 17:18   INTEGER: 2

julia> collect(x[1])
4-element Vector{Any}:
   1:5   ref
  1:1    x
  2:2    INTEGER: 1
   1:1   OP: +
   1:5   ref
  1:1    x
  2:2    INTEGER: 2
   1:2   OP: +

julia>

The issue appears to be that CSTParser moves the + in front of x[1] to after x[2]. So the ordering is off when JuliaFormatter processes it.

domluna avatar Oct 16 '22 18:10 domluna

Possibly simpler reproduction case:

julia> CSTParser.parse("+a + b")
  1:6   call
  1:1    OP: +
  2:3    a
  4:4    b

which leads to

julia> collect(CSTParser.parse("+a + b"))
4-element Vector{Any}:
   1:2   a
   1:1   OP: +
   1:1   b
   1:2   OP: +

while instead, most notably

julia> CSTParser.parse("-a + b")
  1:6   call
  1:2    OP: +
  3:5    call
  3:3     OP: -
  4:5     a
  6:6    b

and

julia> collect(CSTParser.parse("-a + b"))
3-element Vector{Any}:
   1:3   call
  1:1    OP: -
  2:3    a
   1:2   OP: +
   1:1   b

mmesiti avatar Aug 09 '23 11:08 mmesiti