OK, I swear I actually finished constants this time

pull/204/head
Jonathan Arnett 2021-12-13 23:40:39 +07:00
parent 25c5af2aa7
commit 3cb2569f12
5 changed files with 4599 additions and 1830 deletions

@ -7,6 +7,15 @@ const a:Int = 1234
const a:Float = -1_234.53__23
const a:#(Int, String) = #(1, "Hello!")
const a:List(Int) = [1, 2]
const a = <<3>>
const a = <<0:4, 1:3, 1:1>>
const a = <<3:size(8)>>
const a = <<52:size(4)-unit(4)>>
const a = <<"Hello Gleam 💫":utf8>>
const a = Node
const a = Node()
const a = Node(name: "Billy", age: 52)
const a = uri.Uri(host: "github.com")
---
@ -36,7 +45,64 @@ const a:List(Int) = [1, 2]
(type_constructor))
value: (list
(integer)
(integer))))
(integer)))
(constant
name: (identifier)
value: (bit_string
(bit_string_segment
value: (integer))))
(constant
name: (identifier)
value: (bit_string
(bit_string_segment
value: (integer)
options: (bit_string_segment_options
(constant_int)))
(bit_string_segment
value: (integer)
options: (bit_string_segment_options
(constant_int)))
(bit_string_segment
value: (integer)
options: (bit_string_segment_options
(constant_int)))))
(constant
name: (identifier)
value: (bit_string
(bit_string_segment
value: (integer)
options: (bit_string_segment_options
(bit_string_segment_option_size)))))
(constant
name: (identifier)
value: (bit_string
(bit_string_segment
value: (integer)
options: (bit_string_segment_options
(bit_string_segment_option_size)
(bit_string_segment_option_unit)))))
(constant
name: (identifier)
value: (bit_string
(bit_string_segment
value: (string)
options: (bit_string_segment_options
(bit_string_segment_option_utf8)))))
(constant
name: (identifier)
value: (record))
(constant
name: (identifier)
value: (record))
(constant
name: (identifier)
value: (record
(record_arg (string))
(record_arg (integer))))
(constant
name: (identifier)
value: (remote_record
(record_arg (string)))))
=================
Public constants
@ -47,6 +113,15 @@ pub const a:Int = 1234
pub const a:Float = -1_234.53__23
pub const a:#(Int, String) = #(1, "Hello!")
pub const a:List(Int) = [1, 2]
pub const a = <<3>>
pub const a = <<0:4, 1:3, 1:1>>
pub const a = <<3:size(8)>>
pub const a = <<52:size(4)-unit(4)>>
pub const a = <<"Hello Gleam 💫":utf8>>
pub const a = Node
pub const a = Node()
pub const a = Node(name: "Billy", age: 52)
pub const a = uri.Uri(host: "github.com")
---
@ -76,4 +151,61 @@ pub const a:List(Int) = [1, 2]
(type_constructor))
value: (list
(integer)
(integer))))
(integer)))
(public_constant
name: (identifier)
value: (bit_string
(bit_string_segment
value: (integer))))
(public_constant
name: (identifier)
value: (bit_string
(bit_string_segment
value: (integer)
options: (bit_string_segment_options
(constant_int)))
(bit_string_segment
value: (integer)
options: (bit_string_segment_options
(constant_int)))
(bit_string_segment
value: (integer)
options: (bit_string_segment_options
(constant_int)))))
(public_constant
name: (identifier)
value: (bit_string
(bit_string_segment
value: (integer)
options: (bit_string_segment_options
(bit_string_segment_option_size)))))
(public_constant
name: (identifier)
value: (bit_string
(bit_string_segment
value: (integer)
options: (bit_string_segment_options
(bit_string_segment_option_size)
(bit_string_segment_option_unit)))))
(public_constant
name: (identifier)
value: (bit_string
(bit_string_segment
value: (string)
options: (bit_string_segment_options
(bit_string_segment_option_utf8)))))
(public_constant
name: (identifier)
value: (record))
(public_constant
name: (identifier)
value: (record))
(public_constant
name: (identifier)
value: (record
(record_arg (string))
(record_arg (integer))))
(public_constant
name: (identifier)
value: (remote_record
(record_arg (string)))))

@ -17,8 +17,14 @@ module.exports = grammar({
$.import,
$.public_constant,
$.constant
/* $.external_type, */
/* $.external_function, */
/* $._public_extenal_type_or_function, */
/* $.function, */
/* $.public_function, */
/* $.type, */
/* $.function */
/* $.public_opaque_type, */
/* $.public_type */
),
/* Target groups */
@ -69,37 +75,101 @@ module.exports = grammar({
seq(
"const",
field("name", alias($._name, $.identifier)),
optional($._const_type_annotation),
optional($._constant_type_annotation),
"=",
field("value", $._literal)
field("value", $._constant_value)
),
_constant_value: ($) =>
choice(
$.string,
$.float,
$.integer,
alias($._constant_tuple, $.tuple),
alias($._constant_list, $.list),
alias($._constant_bit_string, $.bit_string),
alias($._constant_record, $.record),
alias($._constant_remote_record, $.remote_record)
),
_constant_tuple: ($) =>
seq("#", "(", series_of($._constant_value, ","), ")"),
_constant_list: ($) => seq("[", series_of($._constant_value, ","), "]"),
_constant_bit_string: ($) =>
seq(
"<<",
optional(
series_of(
alias($._constant_bit_string_segment, $.bit_string_segment),
","
)
),
">>"
),
_constant_bit_string_segment: ($) =>
seq(
field("value", $._constant_value),
optional(
field(
"options",
seq(
":",
alias(
$._constant_bit_string_segment_options,
$.bit_string_segment_options
)
)
)
)
),
// This is not an actual node in the Gleam AST. It only exists to allow me
// to group the segment options together into their own list.
_constant_bit_string_segment_options: ($) =>
series_of($._constant_bit_string_segment_option, "-"),
_constant_bit_string_segment_option: ($) =>
choice(
$._constant_bit_string_named_segment_option,
alias($.integer, $.constant_int)
),
_constant_bit_string_named_segment_option: ($) =>
choice(
$._bit_string_segment_option_unit,
$._constant_bit_string_segment_option_size,
$._bit_string_segment_option_literal
),
_constant_bit_string_segment_option_size: ($) =>
seq("size", "(", alias($.integer, $.bit_string_segment_option_size), ")"),
_constant_record: ($) =>
seq(
$._upname,
optional(
seq(
"(",
optional(
series_of(alias($._constant_record_arg, $.record_arg), ",")
),
")"
)
)
),
_constant_record_arg: ($) =>
seq($._name, optional(seq(":", $._constant_value))),
_constant_remote_record: ($) => seq($._name, ".", $._constant_record),
/* Special constant types */
_const_type_annotation: ($) => seq(":", field("type", $._const_type)),
_const_type: ($) =>
_constant_type_annotation: ($) => seq(":", field("type", $._constant_type)),
_constant_type: ($) =>
choice(
$.type_hole,
alias($.const_tuple_type, $.tuple_type),
alias($.const_fn_type, $.fn_type),
alias($.const_type_constructor, $.type_constructor),
alias($.const_remote_type_constructor, $.remote_type_constructor)
),
const_tuple_type: ($) =>
seq("#", "(", optional(series_of($._const_type, ",")), ")"),
const_fn_type: ($) =>
seq(
"fn",
"(",
series_of(alias($._const_type, $.argument_type), ","),
")",
"->",
alias($._const_type, $.return_type)
),
const_type_constructor: ($) => $._const_type_constructor,
const_remote_type_constructor: ($) =>
seq($._name, ".", $._const_type_constructor),
_const_type_constructor: ($) =>
seq($._upname, optional(seq("(", series_of($._const_type, ","), ")"))),
alias($.constant_tuple_type, $.tuple_type),
alias($.constant_type_constructor, $.type_constructor),
alias($.constant_remote_type_constructor, $.remote_type_constructor)
),
constant_tuple_type: ($) =>
seq("#", "(", optional(series_of($._constant_type, ",")), ")"),
constant_type_constructor: ($) => $._constant_type_constructor,
constant_remote_type_constructor: ($) =>
seq($._name, ".", $._constant_type_constructor),
_constant_type_constructor: ($) =>
seq($._upname, optional(seq("(", series_of($._constant_type, ","), ")"))),
/* Literals */
_literal: ($) =>
@ -109,7 +179,7 @@ module.exports = grammar({
$.integer,
$.tuple,
$.list
// $.bitstring,
// $.bit_string,
// $.record,
// $.remote_record
),
@ -118,6 +188,44 @@ module.exports = grammar({
integer: ($) => /-?[0-9_]+/,
tuple: ($) => seq("#", "(", series_of($._literal, ","), ")"),
list: ($) => seq("[", series_of($._literal, ","), "]"),
bit_string: ($) =>
seq("<<", optional(series_of($.bit_string_segment, ",")), ">>"),
bit_string_segment: ($) =>
seq(
$._literal,
optional(seq(":", series_of($._bit_string_segment_option, "-")))
),
_bit_string_segment_option: ($) =>
choice($.bit_string_named_segment_option, alias($.integer, $.size)),
bit_string_named_segment_option: ($) =>
choice(
$._bit_string_segment_option_unit,
$._bit_string_segment_option_size,
$._bit_string_segment_option_literal
),
_bit_string_segment_option_unit: ($) =>
seq("unit", "(", alias($.integer, $.bit_string_segment_option_unit), ")"),
_bit_string_segment_option_size: ($) => seq("size", "(", $.integer, ")"),
_bit_string_segment_option_literal: ($) =>
choice(
alias("binary", $.bit_string_segment_option_binary),
alias("bytes", $.bit_string_segment_option_binary),
alias("int", $.bit_string_segment_option_int),
alias("float", $.bit_string_segment_option_float),
alias("bit_string", $.bit_string_segment_option_bit_string),
alias("bits", $.bit_string_segment_option_bit_string),
alias("utf8", $.bit_string_segment_option_utf8),
alias("utf16", $.bit_string_segment_option_utf16),
alias("utf32", $.bit_string_segment_option_utf32),
alias("utf8_codepoint", $.bit_string_segment_option_utf8_codepoint),
alias("utf16_codepoint", $.bit_string_segment_option_utf16_codepoint),
alias("utf32_codepoint", $.bit_string_segment_option_utf32_codepoint),
alias("signed", $.bit_string_segment_option_signed),
alias("unsigned", $.bit_string_segment_option_unsigned),
alias("big", $.bit_string_segment_option_big),
alias("little", $.bit_string_segment_option_little),
alias("native", $.bit_string_segment_option_native)
),
/* Types */
type_var: ($) => $._name,

File diff suppressed because it is too large Load Diff

@ -5,7 +5,22 @@
"fields": {}
},
{
"type": "argument_type",
"type": "bit_string",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": false,
"types": [
{
"type": "bit_string_segment",
"named": true
}
]
}
},
{
"type": "bit_string_named_segment_option",
"named": true,
"fields": {},
"children": {
@ -13,23 +28,246 @@
"required": true,
"types": [
{
"type": "fn_type",
"type": "bit_string_segment_option_big",
"named": true
},
{
"type": "remote_type_constructor",
"type": "bit_string_segment_option_binary",
"named": true
},
{
"type": "tuple_type",
"type": "bit_string_segment_option_bit_string",
"named": true
},
{
"type": "type_constructor",
"type": "bit_string_segment_option_float",
"named": true
},
{
"type": "type_hole",
"type": "bit_string_segment_option_int",
"named": true
},
{
"type": "bit_string_segment_option_little",
"named": true
},
{
"type": "bit_string_segment_option_native",
"named": true
},
{
"type": "bit_string_segment_option_signed",
"named": true
},
{
"type": "bit_string_segment_option_unit",
"named": true
},
{
"type": "bit_string_segment_option_unsigned",
"named": true
},
{
"type": "bit_string_segment_option_utf16",
"named": true
},
{
"type": "bit_string_segment_option_utf16_codepoint",
"named": true
},
{
"type": "bit_string_segment_option_utf32",
"named": true
},
{
"type": "bit_string_segment_option_utf32_codepoint",
"named": true
},
{
"type": "bit_string_segment_option_utf8",
"named": true
},
{
"type": "bit_string_segment_option_utf8_codepoint",
"named": true
},
{
"type": "integer",
"named": true
}
]
}
},
{
"type": "bit_string_segment",
"named": true,
"fields": {
"options": {
"multiple": true,
"required": false,
"types": [
{
"type": ":",
"named": false
},
{
"type": "bit_string_segment_options",
"named": true
}
]
},
"value": {
"multiple": false,
"required": false,
"types": [
{
"type": "bit_string",
"named": true
},
{
"type": "float",
"named": true
},
{
"type": "integer",
"named": true
},
{
"type": "list",
"named": true
},
{
"type": "record",
"named": true
},
{
"type": "remote_record",
"named": true
},
{
"type": "string",
"named": true
},
{
"type": "tuple",
"named": true
}
]
}
},
"children": {
"multiple": true,
"required": false,
"types": [
{
"type": "bit_string_named_segment_option",
"named": true
},
{
"type": "float",
"named": true
},
{
"type": "integer",
"named": true
},
{
"type": "list",
"named": true
},
{
"type": "size",
"named": true
},
{
"type": "string",
"named": true
},
{
"type": "tuple",
"named": true
}
]
}
},
{
"type": "bit_string_segment_options",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": false,
"types": [
{
"type": "bit_string_segment_option_big",
"named": true
},
{
"type": "bit_string_segment_option_binary",
"named": true
},
{
"type": "bit_string_segment_option_bit_string",
"named": true
},
{
"type": "bit_string_segment_option_float",
"named": true
},
{
"type": "bit_string_segment_option_int",
"named": true
},
{
"type": "bit_string_segment_option_little",
"named": true
},
{
"type": "bit_string_segment_option_native",
"named": true
},
{
"type": "bit_string_segment_option_signed",
"named": true
},
{
"type": "bit_string_segment_option_size",
"named": true
},
{
"type": "bit_string_segment_option_unit",
"named": true
},
{
"type": "bit_string_segment_option_unsigned",
"named": true
},
{
"type": "bit_string_segment_option_utf16",
"named": true
},
{
"type": "bit_string_segment_option_utf16_codepoint",
"named": true
},
{
"type": "bit_string_segment_option_utf32",
"named": true
},
{
"type": "bit_string_segment_option_utf32_codepoint",
"named": true
},
{
"type": "bit_string_segment_option_utf8",
"named": true
},
{
"type": "bit_string_segment_option_utf8_codepoint",
"named": true
},
{
"type": "constant_int",
"named": true
}
]
@ -53,10 +291,6 @@
"multiple": false,
"required": false,
"types": [
{
"type": "fn_type",
"named": true
},
{
"type": "remote_type_constructor",
"named": true
@ -79,6 +313,10 @@
"multiple": false,
"required": true,
"types": [
{
"type": "bit_string",
"named": true
},
{
"type": "float",
"named": true
@ -91,6 +329,14 @@
"type": "list",
"named": true
},
{
"type": "record",
"named": true
},
{
"type": "remote_record",
"named": true
},
{
"type": "string",
"named": true
@ -103,25 +349,6 @@
}
}
},
{
"type": "fn_type",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "argument_type",
"named": true
},
{
"type": "return_type",
"named": true
}
]
}
},
{
"type": "import",
"named": true,
@ -166,6 +393,10 @@
"multiple": true,
"required": true,
"types": [
{
"type": "bit_string",
"named": true
},
{
"type": "float",
"named": true
@ -178,6 +409,14 @@
"type": "list",
"named": true
},
{
"type": "record",
"named": true
},
{
"type": "remote_record",
"named": true
},
{
"type": "string",
"named": true
@ -212,10 +451,6 @@
"multiple": false,
"required": false,
"types": [
{
"type": "fn_type",
"named": true
},
{
"type": "remote_type_constructor",
"named": true
@ -238,6 +473,10 @@
"multiple": false,
"required": true,
"types": [
{
"type": "bit_string",
"named": true
},
{
"type": "float",
"named": true
@ -250,6 +489,14 @@
"type": "list",
"named": true
},
{
"type": "record",
"named": true
},
{
"type": "remote_record",
"named": true
},
{
"type": "string",
"named": true
@ -263,7 +510,7 @@
}
},
{
"type": "remote_type_constructor",
"type": "record",
"named": true,
"fields": {},
"children": {
@ -271,40 +518,78 @@
"required": false,
"types": [
{
"type": "fn_type",
"type": "record_arg",
"named": true
}
]
}
},
{
"type": "record_arg",
"named": true,
"fields": {},
"children": {
"multiple": false,
"required": false,
"types": [
{
"type": "bit_string",
"named": true
},
{
"type": "remote_type_constructor",
"type": "float",
"named": true
},
{
"type": "tuple_type",
"type": "integer",
"named": true
},
{
"type": "type_constructor",
"type": "list",
"named": true
},
{
"type": "type_hole",
"type": "record",
"named": true
},
{
"type": "remote_record",
"named": true
},
{
"type": "string",
"named": true
},
{
"type": "tuple",
"named": true
}
]
}
},
{
"type": "return_type",
"type": "remote_record",
"named": true,
"fields": {},
"children": {
"multiple": false,
"required": true,
"multiple": true,
"required": false,
"types": [
{
"type": "fn_type",
"type": "record_arg",
"named": true
},
}
]
}
},
{
"type": "remote_type_constructor",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": false,
"types": [
{
"type": "remote_type_constructor",
"named": true
@ -398,6 +683,10 @@
"multiple": true,
"required": true,
"types": [
{
"type": "bit_string",
"named": true
},
{
"type": "float",
"named": true
@ -410,6 +699,14 @@
"type": "list",
"named": true
},
{
"type": "record",
"named": true
},
{
"type": "remote_record",
"named": true
},
{
"type": "string",
"named": true
@ -429,10 +726,6 @@
"multiple": true,
"required": false,
"types": [
{
"type": "fn_type",
"named": true
},
{
"type": "remote_type_constructor",
"named": true
@ -460,10 +753,6 @@
"multiple": true,
"required": false,
"types": [
{
"type": "fn_type",
"named": true
},
{
"type": "remote_type_constructor",
"named": true
@ -521,7 +810,7 @@
"named": false
},
{
"type": "->",
"type": "-",
"named": false
},
{
@ -536,10 +825,18 @@
"type": ":",
"named": false
},
{
"type": "<<",
"named": false
},
{
"type": "=",
"named": false
},
{
"type": ">>",
"named": false
},
{
"type": "[",
"named": false
@ -552,10 +849,82 @@
"type": "as",
"named": false
},
{
"type": "bit_string_segment_option_big",
"named": true
},
{
"type": "bit_string_segment_option_binary",
"named": true
},
{
"type": "bit_string_segment_option_bit_string",
"named": true
},
{
"type": "bit_string_segment_option_float",
"named": true
},
{
"type": "bit_string_segment_option_int",
"named": true
},
{
"type": "bit_string_segment_option_little",
"named": true
},
{
"type": "bit_string_segment_option_native",
"named": true
},
{
"type": "bit_string_segment_option_signed",
"named": true
},
{
"type": "bit_string_segment_option_size",
"named": true
},
{
"type": "bit_string_segment_option_unit",
"named": true
},
{
"type": "bit_string_segment_option_unsigned",
"named": true
},
{
"type": "bit_string_segment_option_utf16",
"named": true
},
{
"type": "bit_string_segment_option_utf16_codepoint",
"named": true
},
{
"type": "bit_string_segment_option_utf32",
"named": true
},
{
"type": "bit_string_segment_option_utf32_codepoint",
"named": true
},
{
"type": "bit_string_segment_option_utf8",
"named": true
},
{
"type": "bit_string_segment_option_utf8_codepoint",
"named": true
},
{
"type": "const",
"named": false
},
{
"type": "constant_int",
"named": true
},
{
"type": "erlang",
"named": false
@ -564,10 +933,6 @@
"type": "float",
"named": true
},
{
"type": "fn",
"named": false
},
{
"type": "identifier",
"named": true
@ -592,10 +957,22 @@
"type": "pub",
"named": false
},
{
"type": "size",
"named": true
},
{
"type": "size",
"named": false
},
{
"type": "string",
"named": true
},
{
"type": "unit",
"named": false
},
{
"type": "{",
"named": false

File diff suppressed because it is too large Load Diff