Skip to content

Commit 211bb72

Browse files
authored
Various improvements (tree-sitter#13)
* Add supertypes * Support multi-line case clauses * Allow definitions in case clauses, expressions in class/object bodies * Add annotations, infix patterns, implicit parameters * Add tree-sitter CLI declaration to package.json * Add annotations on type parameters * Add type annotations, type projections; fix compound types * Fix operator_identifier regexp * Add tuple types and repeated parameter types * Give infix_pattern higher precedence than capture_pattern * Make infix_type nestable * Allow body-less trait_definition * Allow omitting parens when function_type's input is a single type * Put all alternatives function_type's parameters under parameter_types * Give alternative_pattern lower precedence than typed_pattern * Remove parenthesized_pattern * Allow empty body in case_clause * Regenerate parser
1 parent 55c9803 commit 211bb72

File tree

10 files changed

+39885
-41050
lines changed

10 files changed

+39885
-41050
lines changed

corpus/annotations.txt

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
================================
2+
Classes
3+
================================
4+
5+
@deprecated("Use D", "1.0") class C {}
6+
7+
---
8+
9+
(compilation_unit
10+
(class_definition
11+
(annotation (type_identifier) (arguments (string) (string)))
12+
(identifier)
13+
(template_body)))
14+
15+
================================
16+
Declarations and definitions
17+
================================
18+
19+
class A(x: String) {
20+
@transient @volatile var y: Int
21+
@transient @volatile val z = x
22+
23+
@throws(Error)
24+
@deprecated(message = "Don't use this", since = "1.0")
25+
def foo() {}
26+
}
27+
28+
---
29+
30+
(compilation_unit
31+
(class_definition (identifier)
32+
(class_parameters (class_parameter (identifier) (type_identifier)))
33+
(template_body
34+
(var_declaration
35+
(annotation (type_identifier))
36+
(annotation (type_identifier))
37+
(identifier) (type_identifier))
38+
(val_definition
39+
(annotation (type_identifier))
40+
(annotation (type_identifier))
41+
(identifier) (identifier))
42+
(function_definition
43+
(annotation (type_identifier) (arguments (identifier)))
44+
(annotation (type_identifier)
45+
(arguments
46+
(assignment_expression (identifier) (string))
47+
(assignment_expression (identifier) (string))))
48+
(identifier) (parameters) (block)))))
49+
50+
================================
51+
Parameters
52+
================================
53+
54+
class A(@one x: String) {
55+
def foo(@another x: Int) {}
56+
}
57+
58+
---
59+
60+
(compilation_unit
61+
(class_definition (identifier)
62+
(class_parameters
63+
(class_parameter
64+
(annotation (type_identifier)) (identifier) (type_identifier)))
65+
(template_body
66+
(function_definition
67+
(identifier)
68+
(parameters (parameter
69+
(annotation (type_identifier))
70+
(identifier) (type_identifier)))
71+
(block)))))
72+
73+
================================
74+
Types
75+
================================
76+
77+
trait Function0[@specialized(Unit, Int, Double) T] {
78+
def apply: T
79+
}
80+
81+
---
82+
83+
(compilation_unit
84+
(trait_definition (identifier)
85+
(type_parameters
86+
(annotation (type_identifier) (arguments (identifier) (identifier) (identifier))) (identifier))
87+
(template_body (function_declaration (identifier) (type_identifier)))))

corpus/definitions.txt

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ class A(b: B) extends C(b || ok) {
151151
Class definitions with parameters
152152
=======================================
153153

154-
class Point(val x: Int, val y: Int)
154+
class Point(val x: Int, val y: Int)(implicit coord: Coord)
155155

156156
---
157157

@@ -160,6 +160,8 @@ class Point(val x: Int, val y: Int)
160160
(identifier)
161161
(class_parameters
162162
(class_parameter (identifier) (type_identifier))
163+
(class_parameter (identifier) (type_identifier)))
164+
(class_parameters
163165
(class_parameter (identifier) (type_identifier)))))
164166

165167
=======================================
@@ -187,6 +189,10 @@ implicit final sealed class Point {
187189
Trait definitions
188190
=======================================
189191

192+
trait A extends B
193+
194+
trait A extends B with C
195+
190196
trait T[U] {
191197
}
192198

@@ -196,6 +202,12 @@ trait T[U] extends V.W[U] {
196202
---
197203

198204
(compilation_unit
205+
(trait_definition
206+
(identifier)
207+
(extends_clause (type_identifier)))
208+
(trait_definition
209+
(identifier)
210+
(extends_clause (compound_type (type_identifier) (type_identifier))))
199211
(trait_definition
200212
(identifier)
201213
(type_parameters (identifier))
@@ -347,6 +359,7 @@ class A {
347359
def g[H](i) {
348360
j
349361
}
362+
def h(x: T)(implicit ev: Reads[T])
350363
}
351364

352365
---
@@ -365,7 +378,31 @@ class A {
365378
(identifier)
366379
(type_parameters (identifier))
367380
(parameters (parameter (identifier)))
368-
(block (identifier))))))
381+
(block (identifier)))
382+
(function_declaration
383+
(identifier)
384+
(parameters (parameter (identifier) (type_identifier)))
385+
(parameters (parameter (identifier) (generic_type (type_identifier) (type_arguments (type_identifier)))))))))
386+
387+
=======================================
388+
Initialization expressions
389+
=======================================
390+
391+
class A(val x: Int, val y: Int) {
392+
assert(x != 0)
393+
}
394+
395+
---
396+
397+
(compilation_unit
398+
(class_definition
399+
(identifier)
400+
(class_parameters
401+
(class_parameter (identifier) (type_identifier))
402+
(class_parameter (identifier) (type_identifier)))
403+
(template_body
404+
(call_expression (identifier)
405+
(arguments (infix_expression (identifier) (operator_identifier) (number)))))))
369406

370407
=======================================
371408
Optional parameters

corpus/expressions.txt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,15 @@ Match expressions
161161
===============================
162162

163163
def matchTest(x: Int): String = x match {
164+
case 0 =>
164165
case 1 => "one"; "uno"
165166
case 2 => "two"
166-
case _ => "many"
167+
case 3 => {
168+
"3"
169+
}
170+
case _ =>
171+
val x = "many"
172+
"more"
167173
}
168174

169175
---
@@ -174,9 +180,11 @@ def matchTest(x: Int): String = x match {
174180
(parameters (parameter (identifier) (type_identifier)))
175181
(type_identifier)
176182
(match_expression (identifier) (case_block
183+
(case_clause (number))
177184
(case_clause (number) (string) (string))
178185
(case_clause (number) (string))
179-
(case_clause (wildcard) (string))))))
186+
(case_clause (number) (block (string)))
187+
(case_clause (wildcard) (val_definition (identifier) (string)) (string))))))
180188

181189
===============================
182190
Field expressions

corpus/patterns.txt

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Typed patterns
2929
val x = y match {
3030
case 1 : Int => 2
3131
case a : B with C => d
32+
case _: B | _: C => 3
3233
}
3334

3435
---
@@ -41,14 +42,23 @@ val x = y match {
4142
(typed_pattern (number) (type_identifier)) (number))
4243
(case_clause
4344
(typed_pattern (identifier) (compound_type (type_identifier) (type_identifier)))
44-
(identifier))))))
45+
(identifier))
46+
(case_clause
47+
(alternative_pattern
48+
(typed_pattern (wildcard) (type_identifier))
49+
(typed_pattern (wildcard) (type_identifier)))
50+
(number))))))
4551

4652
============================
4753
Tuple patterns
4854
============================
4955

5056
val (a, b) = if (c) (d, e) else (f, g)
5157

58+
val x = y match {
59+
case (A, B) => X
60+
}
61+
5262
---
5363

5464
(compilation_unit
@@ -57,7 +67,12 @@ val (a, b) = if (c) (d, e) else (f, g)
5767
(if_expression
5868
(parenthesized_expression (identifier))
5969
(tuple_expression (identifier) (identifier))
60-
(tuple_expression (identifier) (identifier)))))
70+
(tuple_expression (identifier) (identifier))))
71+
(val_definition (identifier)
72+
(match_expression (identifier)
73+
(case_block
74+
(case_clause
75+
(tuple_pattern (identifier) (identifier)) (identifier))))))
6176

6277
============================
6378
Case class patterns
@@ -93,12 +108,34 @@ def showNotification(notification: Notification): String = {
93108
(case_class_pattern (type_identifier) (identifier) (identifier))
94109
(string_transform_expression (identifier) (string (interpolation (identifier)) (interpolation (identifier))))))))))
95110

111+
============================
112+
Infix patterns
113+
============================
114+
115+
def first(x: Seq[Int]) = x match {
116+
case e :+ _ => Some(e)
117+
case _ => None
118+
}
119+
120+
---
121+
122+
(compilation_unit
123+
(function_definition (identifier)
124+
(parameters (parameter (identifier) (generic_type (type_identifier) (type_arguments (type_identifier)))))
125+
(match_expression (identifier)
126+
(case_block
127+
(case_clause (infix_pattern (identifier) (operator_identifier) (wildcard))
128+
(call_expression (identifier) (arguments (identifier))))
129+
(case_clause (wildcard)
130+
(identifier))))))
131+
96132
============================
97133
Capture patterns
98134
============================
99135

100136
val x = y match {
101-
case a @ B(1) => c
137+
case a @ B(1) => a
138+
case b @ C(d @ (e @ X, _: Y)) => e
102139
}
103140

104141
---
@@ -111,4 +148,12 @@ val x = y match {
111148
(case_block
112149
(case_clause
113150
(capture_pattern (identifier) (case_class_pattern (type_identifier) (number)))
114-
(identifier))))))
151+
(identifier))
152+
(case_clause
153+
(capture_pattern (identifier)
154+
(case_class_pattern (type_identifier)
155+
(capture_pattern (identifier)
156+
(tuple_pattern
157+
(capture_pattern (identifier) (identifier))
158+
(typed_pattern (wildcard) (type_identifier))))))
159+
(identifier))))))

0 commit comments

Comments
 (0)