Skip to content

Commit f017d20

Browse files
committed
Refinement
Problem ------- Compound type with refinement (structural type with a parent) doesn't parse. Solution -------- This adds support for it. It collides with context bounds `A: B: C`, so the precedence is set to -1.
1 parent 4072a37 commit f017d20

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

corpus/types.txt

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,10 @@ def cloneAndReset(obj: Cloneable with Resetable): Cloneable = {
194194

195195
class F extends Cloneable with Resetable with Serializable {}
196196

197+
type G = H[A1] {
198+
def foo[A2 <: Stepper[_]]: A2
199+
}
200+
197201
--------------------------------------------------------------------------------
198202

199203
(compilation_unit
@@ -213,7 +217,25 @@ class F extends Cloneable with Resetable with Serializable {}
213217
(type_identifier)
214218
(type_identifier)
215219
(type_identifier))
216-
(template_body)))
220+
(template_body))
221+
(type_definition
222+
(type_identifier)
223+
(compound_type
224+
(generic_type
225+
(type_identifier)
226+
(type_arguments
227+
(type_identifier)))
228+
(refinement
229+
(function_declaration
230+
(identifier)
231+
(type_parameters
232+
(identifier)
233+
(upper_bound
234+
(generic_type
235+
(type_identifier)
236+
(type_arguments
237+
(wildcard)))))
238+
(type_identifier))))))
217239

218240
================================================================================
219241
Infix types

grammar.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -674,14 +674,14 @@ module.exports = grammar({
674674
* DefParam ::= {Annotation} [‘inline’] Param
675675
* Param ::= id ‘:’ ParamType [‘=’ Expr]
676676
*/
677-
parameter: $ => seq(
677+
parameter: $ => prec.left(PREC.control, seq(
678678
repeat($.annotation),
679679
optional($.inline_modifier),
680680
field('name', $._identifier),
681681
':',
682682
field('type', $._param_type),
683683
optional(seq('=', field('default_value', $.expression)))
684-
),
684+
)),
685685

686686
_block: $ => prec.left(seq(
687687
sep1($._semicolon, choice(
@@ -752,11 +752,25 @@ module.exports = grammar({
752752
$.wildcard,
753753
),
754754

755-
compound_type: $ => prec(PREC.compound, seq(
756-
field('base', $._annotated_type),
757-
repeat1(seq('with', field('extra', $._annotated_type))),
758-
// TODO: Refinement.
759-
)),
755+
compound_type: $ => choice(
756+
prec.left(PREC.compound, seq(
757+
field('base', $._annotated_type),
758+
repeat1(seq('with', field('extra', $._annotated_type))),
759+
)),
760+
prec.left(-1, seq(
761+
field('base', $._annotated_type),
762+
$._refinement,
763+
)),
764+
prec.left(-1, seq(
765+
prec.left(PREC.compound, seq(
766+
field('base', $._annotated_type),
767+
repeat1(seq('with', field('extra', $._annotated_type))),
768+
)),
769+
$._refinement,
770+
)),
771+
),
772+
773+
_refinement: $ => alias($.template_body, $.refinement),
760774

761775
// This does not include _simple_type since _annotated_type covers it.
762776
_infix_type_choice: $ => prec.left(PREC.infix, choice(

0 commit comments

Comments
 (0)