Skip to content

Commit b0c7823

Browse files
committed
Allow statements at the top level
This improves parsing of code snippets, as often appear in markdown readmes
1 parent c03e250 commit b0c7823

6 files changed

Lines changed: 21867 additions & 25417 deletions

File tree

‎corpus/statements.txt‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,3 +622,21 @@ func main() {
622622
(interpreted_string_literal)
623623
(index_expression (identifier) (identifier))))))))
624624
(argument_list)))))))
625+
626+
============================================
627+
Top-level statements
628+
============================================
629+
630+
foo(5)
631+
x := T { a: b }
632+
633+
---
634+
635+
(source_file
636+
(call_expression (identifier) (argument_list (int_literal)))
637+
(short_var_declaration
638+
(expression_list (identifier))
639+
(expression_list
640+
(composite_literal
641+
(type_identifier)
642+
(literal_value (keyed_element (field_identifier) (identifier)))))))

‎grammar.js‎

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ module.exports = grammar({
7373
$._type_identifier,
7474
$._field_identifier,
7575
$._package_identifier,
76+
$._top_level_declaration,
7677
$._string_literal,
7778
],
7879

@@ -95,17 +96,18 @@ module.exports = grammar({
9596
],
9697

9798
rules: {
98-
source_file: $ => repeat(seq(
99-
choice(
100-
$.package_clause,
101-
$.function_declaration,
102-
$.method_declaration,
103-
$.import_declaration,
104-
$._declaration
105-
),
106-
optional(terminator)
99+
source_file: $ => repeat(choice(
100+
seq($._statement, terminator),
101+
seq($._top_level_declaration, optional(terminator)),
107102
)),
108103

104+
_top_level_declaration: $ => choice(
105+
$.package_clause,
106+
$.function_declaration,
107+
$.method_declaration,
108+
$.import_declaration
109+
),
110+
109111
package_clause: $ => seq(
110112
'package',
111113
$._package_identifier
@@ -189,15 +191,15 @@ module.exports = grammar({
189191
)
190192
),
191193

192-
function_declaration: $ => prec.right(seq(
194+
function_declaration: $ => prec.right(1, seq(
193195
'func',
194196
field('name', $.identifier),
195197
field('parameters', $.parameter_list),
196198
field('result', optional(choice($.parameter_list, $._simple_type))),
197199
field('body', optional($.block))
198200
)),
199201

200-
method_declaration: $ => prec.right(seq(
202+
method_declaration: $ => prec.right(1, seq(
201203
'func',
202204
field('receiver', $.parameter_list),
203205
field('name', $._field_identifier),

‎script/parse-examples‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@ checkout_at "examples/moby" "moby/moby" "f57f260b49b6142366e6bc1274204ee0a120594
2020

2121
known_failures="$(cat script/known-failures.txt | egrep -v '^#')"
2222

23-
echo -q \
23+
tree-sitter parse -q \
2424
'examples/**/*.go' \
2525
'!**/testdata/**/*' \
2626
'!**/go/test/**/*' \
2727
$(for failure in $known_failures; do echo "!${failure}"; done)
2828

29-
exit
3029
example_count=$(find examples -name '*.go' | egrep -v 'go/test|testdata' | wc -l)
3130
failure_count=$(wc -w <<< "$known_failures")
3231
success_count=$(( $example_count - $failure_count ))

‎src/grammar.json‎

Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,15 @@
55
"source_file": {
66
"type": "REPEAT",
77
"content": {
8-
"type": "SEQ",
8+
"type": "CHOICE",
99
"members": [
1010
{
11-
"type": "CHOICE",
11+
"type": "SEQ",
1212
"members": [
1313
{
1414
"type": "SYMBOL",
15-
"name": "package_clause"
15+
"name": "_statement"
1616
},
17-
{
18-
"type": "SYMBOL",
19-
"name": "function_declaration"
20-
},
21-
{
22-
"type": "SYMBOL",
23-
"name": "method_declaration"
24-
},
25-
{
26-
"type": "SYMBOL",
27-
"name": "import_declaration"
28-
},
29-
{
30-
"type": "SYMBOL",
31-
"name": "_declaration"
32-
}
33-
]
34-
},
35-
{
36-
"type": "CHOICE",
37-
"members": [
3817
{
3918
"type": "CHOICE",
4019
"members": [
@@ -47,15 +26,63 @@
4726
"value": ";"
4827
}
4928
]
29+
}
30+
]
31+
},
32+
{
33+
"type": "SEQ",
34+
"members": [
35+
{
36+
"type": "SYMBOL",
37+
"name": "_top_level_declaration"
5038
},
5139
{
52-
"type": "BLANK"
40+
"type": "CHOICE",
41+
"members": [
42+
{
43+
"type": "CHOICE",
44+
"members": [
45+
{
46+
"type": "STRING",
47+
"value": "\n"
48+
},
49+
{
50+
"type": "STRING",
51+
"value": ";"
52+
}
53+
]
54+
},
55+
{
56+
"type": "BLANK"
57+
}
58+
]
5359
}
5460
]
5561
}
5662
]
5763
}
5864
},
65+
"_top_level_declaration": {
66+
"type": "CHOICE",
67+
"members": [
68+
{
69+
"type": "SYMBOL",
70+
"name": "package_clause"
71+
},
72+
{
73+
"type": "SYMBOL",
74+
"name": "function_declaration"
75+
},
76+
{
77+
"type": "SYMBOL",
78+
"name": "method_declaration"
79+
},
80+
{
81+
"type": "SYMBOL",
82+
"name": "import_declaration"
83+
}
84+
]
85+
},
5986
"package_clause": {
6087
"type": "SEQ",
6188
"members": [
@@ -483,7 +510,7 @@
483510
},
484511
"function_declaration": {
485512
"type": "PREC_RIGHT",
486-
"value": 0,
513+
"value": 1,
487514
"content": {
488515
"type": "SEQ",
489516
"members": [
@@ -553,7 +580,7 @@
553580
},
554581
"method_declaration": {
555582
"type": "PREC_RIGHT",
556-
"value": 0,
583+
"value": 1,
557584
"content": {
558585
"type": "SEQ",
559586
"members": [
@@ -6487,6 +6514,7 @@
64876514
"_type_identifier",
64886515
"_field_identifier",
64896516
"_package_identifier",
6517+
"_top_level_declaration",
64906518
"_string_literal"
64916519
],
64926520
"supertypes": [

‎src/node-types.json‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1963,7 +1963,7 @@
19631963
"required": false,
19641964
"types": [
19651965
{
1966-
"type": "const_declaration",
1966+
"type": "_statement",
19671967
"named": true
19681968
},
19691969
{
@@ -1981,14 +1981,6 @@
19811981
{
19821982
"type": "package_clause",
19831983
"named": true
1984-
},
1985-
{
1986-
"type": "type_declaration",
1987-
"named": true
1988-
},
1989-
{
1990-
"type": "var_declaration",
1991-
"named": true
19921984
}
19931985
]
19941986
}

0 commit comments

Comments
 (0)