Skip to content

Commit abc04b4

Browse files
committed
Literal types [grammar, tests]
1 parent b21705e commit abc04b4

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

corpus/types.txt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,41 @@ type A = B with B1 with B2 ! C with C1
250250
(compound_type (type_identifier) (type_identifier) (type_identifier))
251251
(operator_identifier)
252252
(compound_type (type_identifier) (type_identifier)))))
253+
254+
==================================
255+
Literal type aliases (Scala 2.13+)
256+
==================================
257+
258+
type A = "hello"
259+
type B = 25
260+
type C = false
261+
type D = 0.5f
262+
263+
class Test(d: "hello", b: 25)
264+
265+
---
266+
267+
(compilation_unit
268+
(type_definition
269+
(type_identifier) (literal_type (string)))
270+
271+
(type_definition
272+
(type_identifier) (literal_type (integer_literal)))
273+
274+
(type_definition
275+
(type_identifier) (literal_type (boolean_literal)))
276+
277+
(type_definition
278+
(type_identifier) (literal_type (floating_point_literal)))
279+
280+
(class_definition
281+
(identifier)
282+
(class_parameters
283+
(class_parameter
284+
(identifier)
285+
(literal_type
286+
(string)))
287+
(class_parameter
288+
(identifier)
289+
(literal_type
290+
(integer_literal))))))

grammar.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ module.exports = grammar({
450450
$.compound_type,
451451
$.infix_type,
452452
$._annotated_type,
453+
$.literal_type
453454
),
454455

455456
// TODO: Make this a visible type, so that _type can be a supertype.
@@ -791,13 +792,20 @@ module.exports = grammar({
791792

792793
operator_identifier: $ => /[^\s\w\(\)\[\]\{\}'"`\.;,]+/,
793794

795+
_non_null_literal: $ =>
796+
choice(
797+
$.integer_literal,
798+
$.floating_point_literal,
799+
$.boolean_literal,
800+
$.character_literal,
801+
$.symbol_literal,
802+
$.string
803+
),
804+
805+
literal_type: $ => $._non_null_literal,
806+
794807
literal: $ => choice(
795-
$.integer_literal,
796-
$.floating_point_literal,
797-
$.boolean_literal,
798-
$.character_literal,
799-
$.symbol_literal,
800-
$.string,
808+
$._non_null_literal,
801809
$.null_literal
802810
),
803811

0 commit comments

Comments
 (0)