Skip to content

Commit 9e5e2bf

Browse files
authored
Merge pull request #48 from NathanLovato/nathan/line-continuation-fix
Fix line continuation parsing, follow tree-sitter-python and make it a visible token
2 parents e7393a9 + f4e841b commit 9e5e2bf

3 files changed

Lines changed: 123 additions & 23 deletions

File tree

grammar.js

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ module.exports = grammar({
2828

2929
word: ($) => $._identifier,
3030

31-
extras: ($) => [$.comment, /[\s\uFEFF\u2060\u200B]/],
31+
extras: ($) => [$.comment, /[\s\uFEFF\u2060\u200B]/, $.line_continuation],
3232

3333
externals: ($) => [
3434
$._newline,
@@ -176,25 +176,15 @@ module.exports = grammar({
176176
seq(
177177
optional("/"),
178178
$._identifier,
179-
repeat(
180-
// It's valid syntax in GDScript to wrap get node paths with
181-
// $ or % over multiple lines with line continuation marks.
182-
// But the continuation mark can only come after a trailing /
183-
seq("/", optional($._line_continuation), $._identifier),
184-
),
179+
repeat(seq("/", $._identifier)),
185180
),
186181
),
187182
),
188183
seq(
189184
"%",
190185
choice(
191186
alias($.string, "value"),
192-
seq(
193-
$._identifier,
194-
repeat(
195-
seq("/", optional($._line_continuation), $._identifier),
196-
),
197-
),
187+
seq($._identifier, repeat(seq("/", $._identifier))),
198188
),
199189
),
200190
),
@@ -839,8 +829,7 @@ module.exports = grammar({
839829
// This rule is for trailing backslashes to indicate line continuation. We
840830
// capture those as anonymous '\' tokens to be able to preserve them in code
841831
// formatters.
842-
line_continuation: ($) => token(prec(1, seq("\\", /\r?\n/))),
843-
_line_continuation: ($) => alias($.line_continuation, ""),
832+
line_continuation: ($) => token(seq("\\", /\r?\n/)),
844833
}, // end rules
845834
});
846835

test/corpus/linecontinuations.txt

Lines changed: 115 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ world"
1515

1616
(source
1717
(expression_statement
18-
(get_node))
18+
(get_node
19+
(line_continuation)))
1920
(expression_statement
20-
(get_node))
21+
(get_node
22+
(line_continuation)))
2123
(expression_statement
2224
(string_name
2325
(escape_sequence))))
@@ -26,12 +28,121 @@ world"
2628
Invalid syntax sugar line continuations
2729
===
2830

31+
# This is invalid GDScript but I'm making the parser accept it for the sake of simplicity with line continuations
2932
$get\
3033
/node
3134

3235
---
3336

3437
(source
38+
(comment)
3539
(expression_statement
36-
(get_node
37-
(ERROR))))
40+
(get_node
41+
(line_continuation))))
42+
43+
===
44+
Line continuation in binary expressions
45+
===
46+
47+
func _handles(resource):
48+
return resource is NoiseTexture2D \
49+
or resource is GradientTexture1D
50+
51+
---
52+
53+
(source
54+
(function_definition
55+
name: (name)
56+
parameters: (parameters
57+
(identifier))
58+
body: (body
59+
(return_statement
60+
(binary_operator
61+
left: (binary_operator
62+
left: (identifier)
63+
right: (identifier))
64+
(line_continuation)
65+
right: (binary_operator
66+
left: (identifier)
67+
right: (identifier)))))))
68+
69+
===
70+
Line continuation in function calls
71+
===
72+
73+
func _process(delta):
74+
move_and_slide(velocity * \
75+
delta)
76+
77+
---
78+
79+
(source
80+
(function_definition
81+
name: (name)
82+
parameters: (parameters
83+
(identifier))
84+
body: (body
85+
(expression_statement
86+
(call
87+
(identifier)
88+
arguments: (arguments
89+
(binary_operator
90+
left: (identifier)
91+
(line_continuation)
92+
right: (identifier))))))))
93+
94+
===
95+
Line continuation in array literals
96+
===
97+
98+
func test():
99+
var array = [
100+
1, \
101+
2, \
102+
3
103+
]
104+
105+
---
106+
107+
(source
108+
(function_definition
109+
name: (name)
110+
parameters: (parameters)
111+
body: (body
112+
(variable_statement
113+
name: (name)
114+
value: (array
115+
(integer)
116+
(line_continuation)
117+
(integer)
118+
(line_continuation)
119+
(integer))))))
120+
121+
===
122+
Line continuation in dictionary
123+
===
124+
125+
func test():
126+
var dict = {
127+
"key": \
128+
"value",
129+
"key2": \
130+
"value2"
131+
}
132+
133+
---
134+
135+
(source
136+
(function_definition
137+
name: (name)
138+
parameters: (parameters)
139+
body: (body
140+
(variable_statement
141+
name: (name)
142+
value: (dictionary
143+
(pair
144+
left: (string)
145+
value: (string))
146+
(pair
147+
left: (string)
148+
value: (string)))))))

test/corpus/strings.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Double-quoted regular string
66

77
---
88

9-
(source
9+
(source
1010
(expression_statement
1111
(string)))
1212

@@ -56,7 +56,7 @@ Single-quoted regular string
5656

5757
---
5858

59-
(source
59+
(source
6060
(expression_statement
6161
(string)))
6262

@@ -621,7 +621,8 @@ var v = ^ ''''''
621621
(expression_statement
622622
(get_node))
623623
(expression_statement
624-
(get_node))
624+
(get_node
625+
(line_continuation)))
625626
(comment)
626627
(comment)
627628
(expression_statement
@@ -668,4 +669,3 @@ var v = ^ ''''''
668669
(variable_statement (name) (ERROR) (string))
669670
(variable_statement (name) (ERROR) (string))
670671
(variable_statement (name) (ERROR) (string)))
671-

0 commit comments

Comments
 (0)