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

Problem parsing in JuliaFormatter #365

Open
guilhermebodin opened this issue Oct 7, 2022 · 3 comments
Open

Problem parsing in JuliaFormatter #365

guilhermebodin opened this issue Oct 7, 2022 · 3 comments
Labels

Comments

@guilhermebodin
Copy link

I had an error in JuliaFormatter and it might be a problem with the parser domluna/JuliaFormatter.jl#642 (comment)

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
@davidanthoff
Copy link
Member

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

@domluna
Copy link

domluna commented Oct 16, 2022

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

@mmesiti
Copy link

mmesiti commented Aug 9, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants