ionide / tree-sitter-fsharp

F# grammar for treesitter
MIT License
81 stars 19 forks source link

Plus sign without space in expression is parsed as constant #80

Closed cannero closed 3 months ago

cannero commented 3 months ago

Description If there is no space between the plus and a number in an expression, the plus sign is seen as belonging to the number.

Here an example, with spaces it parses correctly:

let j = 3 + 1

The grammer shows: (sorry, I copied this from emacs, the names are likely off)

(infix_expression
     (const (int))
     (infix_op +)
     (const (int)))

Without spaces, the operator is missing as +1 is seen as constant, therefore the right-hand side is shown as application expression:

let k = 3+1
(application_expression
     (const (int))
     (const (int)))

Or am I missing something and +1 has some special meaning in this context?

Environment

OS: windows Grammar version: commit 01c19eddaa7ae1968f23dd2fde8e36fbba901318 Editor: emacs

KaranAhlawat commented 3 months ago

Looking at dotnet fsi, it does evaluate to 4 in both cases, so I don't believe it has any special meaning in that context.

Nsidorenco commented 3 months ago

Thank you for the detailed bug report!

You're right, 1+1 should parse as an infix expression in this case. +1 (and -1) do have a special meaning as they are interpreted as the sign of an integer. You can see the AST of how it is interpreted.

The tricky thing, in the case of 1-1, is that 1- binds stronger than -1, which means that the sign should be interpreted as an infix operator