struct type construction

This commit is contained in:
2026-07-19 21:35:48 +02:00
parent 944648fce6
commit 59bbb197e0
15 changed files with 331415 additions and 253804 deletions
+40 -10
View File
@@ -33,9 +33,8 @@ module.exports = grammar({
[$.array_type, $.expression],
[$.array_type, $.array_literal],
[$.expression_statement, $.parenthesized_expression],
[$.block, $.tuple_literal],
[$.block, $.tuple_literal, $.anonymous_record_literal],
[$.field_initializer, $.expression],
[$.tuple_literal],
[$.capture_list, $.expression],
[$.match_capture, $.expression],
],
@@ -188,6 +187,7 @@ module.exports = grammar({
_type_atom: $ => choice(
$.builtin_type,
$.intrinsic_type,
$.named_type,
$.optional_type,
$.pointer_type,
@@ -198,9 +198,11 @@ module.exports = grammar({
parenthesized_type: $ => seq('(', repeat($._newline), $.type, repeat($._newline), ')'),
named_type: $ => prec.right(seq(
$.qualified_identifier,
optional($.argument_list),
named_type: $ => prec.right(seq($.qualified_identifier, optional($.argument_list))),
intrinsic_type: $ => prec(PREC.POSTFIX, seq(
field('function', alias('struct_type!', $.identifier)),
field('arguments', $.argument_list),
)),
optional_type: $ => seq('?', $.type),
@@ -441,6 +443,7 @@ module.exports = grammar({
$.slice_expression,
$.postfix_expression,
$.struct_literal,
$.anonymous_record_literal,
$.tuple_literal,
$.comptime_block,
$.function_literal,
@@ -556,17 +559,27 @@ module.exports = grammar({
),
field_initializer: $ => seq(
field('name', $.identifier),
field('name', $._field_name),
optional(seq('=', repeat($._newline), field('value', $.expression))),
),
anonymous_record_literal: $ => prec(2, seq(
'{',
repeat($._newline),
$.keyed_field_initializer,
repeat(seq(repeat($._newline), ',', repeat($._newline), $.keyed_field_initializer)),
optional(seq(repeat($._newline), ',')),
repeat($._newline),
'}',
)),
tuple_literal: $ => prec(1, choice(
seq('{', repeat($._newline), '}'),
seq(
'{', repeat($._newline), $.expression,
',', repeat($._newline),
repeat(seq($.expression, ',', repeat($._newline))),
optional($.expression), repeat($._newline), '}',
repeat(seq(repeat($._newline), ',', repeat($._newline), $.expression)),
optional(seq(repeat($._newline), ',')),
repeat($._newline), '}',
),
)),
@@ -597,7 +610,7 @@ module.exports = grammar({
),
keyed_field_initializer: $ => seq(
field('name', $.identifier),
field('name', $._field_name),
'=',
repeat($._newline),
field('value', $.expression),
@@ -635,6 +648,23 @@ module.exports = grammar({
undefined: _ => 'undefined',
sink: _ => '_',
_field_name: $ => prec(2, choice(
$.identifier,
alias(choice(
'test', 'func', 'c_func', 'struct', 'c_struct', 'opaque', 'union', 'enum',
'distinct', 'alias', 'import', 'hide', 'return', 'try', 'catch', 'mut',
'none', 'undefined', 'orelse', 'and', 'or', 'xor', 'if', 'while', 'for',
'expand', 'break', 'continue', 'defer', 'errdefer', 'yield', 'match', 'else',
'true', 'false',
'void', 'type', 'anyopaque', 'bool', 'int', 'uint', 'float', 'range',
'i8', 'i16', 'i32', 'i64', 'u8', 'u16', 'u32', 'u64',
'isize', 'usize', 'f32', 'f64',
'c_char', 'c_schar', 'c_uchar', 'c_short', 'c_ushort',
'c_int', 'c_uint', 'c_long', 'c_ulong', 'c_longlong',
'c_ulonglong', 'c_float', 'c_double', 'c_longdouble',
), $.identifier),
)),
identifier: _ => /[A-Za-z_][A-Za-z0-9_]*/,
integer: _ => /[0-9]+/,
float: _ => token(prec(1, /[0-9]+\.[0-9]+/)),
@@ -39,6 +39,7 @@
(parameter name: (identifier) @variable.parameter)
(intrinsic_call_expression function: (identifier) @function.builtin)
(intrinsic_type function: (identifier) @function.builtin)
(call_expression function: (expression (identifier) @function))
(call_expression function: (expression (field_expression field: (identifier) @function)))
(field_expression field: (identifier) @property)
+453 -21
View File
@@ -1149,6 +1149,10 @@
"type": "SYMBOL",
"name": "builtin_type"
},
{
"type": "SYMBOL",
"name": "intrinsic_type"
},
{
"type": "SYMBOL",
"name": "named_type"
@@ -1231,6 +1235,36 @@
]
}
},
"intrinsic_type": {
"type": "PREC",
"value": 11,
"content": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "function",
"content": {
"type": "ALIAS",
"content": {
"type": "STRING",
"value": "struct_type!"
},
"named": true,
"value": "identifier"
}
},
{
"type": "FIELD",
"name": "arguments",
"content": {
"type": "SYMBOL",
"name": "argument_list"
}
}
]
}
},
"optional_type": {
"type": "SEQ",
"members": [
@@ -3105,6 +3139,10 @@
"type": "SYMBOL",
"name": "struct_literal"
},
{
"type": "SYMBOL",
"name": "anonymous_record_literal"
},
{
"type": "SYMBOL",
"name": "tuple_literal"
@@ -4268,7 +4306,7 @@
"name": "name",
"content": {
"type": "SYMBOL",
"name": "identifier"
"name": "_field_name"
}
},
{
@@ -4305,6 +4343,95 @@
}
]
},
"anonymous_record_literal": {
"type": "PREC",
"value": 2,
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "{"
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_newline"
}
},
{
"type": "SYMBOL",
"name": "keyed_field_initializer"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_newline"
}
},
{
"type": "STRING",
"value": ","
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_newline"
}
},
{
"type": "SYMBOL",
"name": "keyed_field_initializer"
}
]
}
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_newline"
}
},
{
"type": "STRING",
"value": ","
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_newline"
}
},
{
"type": "STRING",
"value": "}"
}
]
}
},
"tuple_literal": {
"type": "PREC",
"value": 1,
@@ -4349,25 +4476,17 @@
"type": "SYMBOL",
"name": "expression"
},
{
"type": "STRING",
"value": ","
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_newline"
}
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "expression"
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_newline"
}
},
{
"type": "STRING",
@@ -4379,6 +4498,10 @@
"type": "SYMBOL",
"name": "_newline"
}
},
{
"type": "SYMBOL",
"name": "expression"
}
]
}
@@ -4387,8 +4510,20 @@
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "expression"
"type": "SEQ",
"members": [
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_newline"
}
},
{
"type": "STRING",
"value": ","
}
]
},
{
"type": "BLANK"
@@ -4564,7 +4699,7 @@
"name": "name",
"content": {
"type": "SYMBOL",
"name": "identifier"
"name": "_field_name"
}
},
{
@@ -4876,6 +5011,305 @@
"type": "STRING",
"value": "_"
},
"_field_name": {
"type": "PREC",
"value": 2,
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "ALIAS",
"content": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "test"
},
{
"type": "STRING",
"value": "func"
},
{
"type": "STRING",
"value": "c_func"
},
{
"type": "STRING",
"value": "struct"
},
{
"type": "STRING",
"value": "c_struct"
},
{
"type": "STRING",
"value": "opaque"
},
{
"type": "STRING",
"value": "union"
},
{
"type": "STRING",
"value": "enum"
},
{
"type": "STRING",
"value": "distinct"
},
{
"type": "STRING",
"value": "alias"
},
{
"type": "STRING",
"value": "import"
},
{
"type": "STRING",
"value": "hide"
},
{
"type": "STRING",
"value": "return"
},
{
"type": "STRING",
"value": "try"
},
{
"type": "STRING",
"value": "catch"
},
{
"type": "STRING",
"value": "mut"
},
{
"type": "STRING",
"value": "none"
},
{
"type": "STRING",
"value": "undefined"
},
{
"type": "STRING",
"value": "orelse"
},
{
"type": "STRING",
"value": "and"
},
{
"type": "STRING",
"value": "or"
},
{
"type": "STRING",
"value": "xor"
},
{
"type": "STRING",
"value": "if"
},
{
"type": "STRING",
"value": "while"
},
{
"type": "STRING",
"value": "for"
},
{
"type": "STRING",
"value": "expand"
},
{
"type": "STRING",
"value": "break"
},
{
"type": "STRING",
"value": "continue"
},
{
"type": "STRING",
"value": "defer"
},
{
"type": "STRING",
"value": "errdefer"
},
{
"type": "STRING",
"value": "yield"
},
{
"type": "STRING",
"value": "match"
},
{
"type": "STRING",
"value": "else"
},
{
"type": "STRING",
"value": "true"
},
{
"type": "STRING",
"value": "false"
},
{
"type": "STRING",
"value": "void"
},
{
"type": "STRING",
"value": "type"
},
{
"type": "STRING",
"value": "anyopaque"
},
{
"type": "STRING",
"value": "bool"
},
{
"type": "STRING",
"value": "int"
},
{
"type": "STRING",
"value": "uint"
},
{
"type": "STRING",
"value": "float"
},
{
"type": "STRING",
"value": "range"
},
{
"type": "STRING",
"value": "i8"
},
{
"type": "STRING",
"value": "i16"
},
{
"type": "STRING",
"value": "i32"
},
{
"type": "STRING",
"value": "i64"
},
{
"type": "STRING",
"value": "u8"
},
{
"type": "STRING",
"value": "u16"
},
{
"type": "STRING",
"value": "u32"
},
{
"type": "STRING",
"value": "u64"
},
{
"type": "STRING",
"value": "isize"
},
{
"type": "STRING",
"value": "usize"
},
{
"type": "STRING",
"value": "f32"
},
{
"type": "STRING",
"value": "f64"
},
{
"type": "STRING",
"value": "c_char"
},
{
"type": "STRING",
"value": "c_schar"
},
{
"type": "STRING",
"value": "c_uchar"
},
{
"type": "STRING",
"value": "c_short"
},
{
"type": "STRING",
"value": "c_ushort"
},
{
"type": "STRING",
"value": "c_int"
},
{
"type": "STRING",
"value": "c_uint"
},
{
"type": "STRING",
"value": "c_long"
},
{
"type": "STRING",
"value": "c_ulong"
},
{
"type": "STRING",
"value": "c_longlong"
},
{
"type": "STRING",
"value": "c_ulonglong"
},
{
"type": "STRING",
"value": "c_float"
},
{
"type": "STRING",
"value": "c_double"
},
{
"type": "STRING",
"value": "c_longdouble"
}
]
},
"named": true,
"value": "identifier"
}
]
}
},
"identifier": {
"type": "PATTERN",
"value": "[A-Za-z_][A-Za-z0-9_]*"
@@ -5029,15 +5463,13 @@
],
[
"block",
"tuple_literal"
"tuple_literal",
"anonymous_record_literal"
],
[
"field_initializer",
"expression"
],
[
"tuple_literal"
],
[
"capture_list",
"expression"
+68 -4
View File
@@ -15,6 +15,21 @@
}
}
},
{
"type": "anonymous_record_literal",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "keyed_field_initializer",
"named": true
}
]
}
},
{
"type": "argument_list",
"named": true,
@@ -748,6 +763,10 @@
"multiple": false,
"required": true,
"types": [
{
"type": "anonymous_record_literal",
"named": true
},
{
"type": "array_literal",
"named": true
@@ -1443,6 +1462,32 @@
}
}
},
{
"type": "intrinsic_type",
"named": true,
"fields": {
"arguments": {
"multiple": false,
"required": true,
"types": [
{
"type": "argument_list",
"named": true
}
]
},
"function": {
"multiple": false,
"required": true,
"types": [
{
"type": "identifier",
"named": true
}
]
}
}
},
{
"type": "keyed_field_initializer",
"named": true,
@@ -1608,6 +1653,16 @@
]
}
},
{
"type": "none",
"named": true,
"fields": {}
},
{
"type": "opaque_type",
"named": true,
"fields": {}
},
{
"type": "optional_type",
"named": true,
@@ -2158,6 +2213,10 @@
"type": "function_type",
"named": true
},
{
"type": "intrinsic_type",
"named": true
},
{
"type": "named_type",
"named": true
@@ -2273,6 +2332,11 @@
}
}
},
{
"type": "undefined",
"named": true,
"fields": {}
},
{
"type": "union_type",
"named": true,
@@ -2862,11 +2926,11 @@
},
{
"type": "none",
"named": true
"named": false
},
{
"type": "opaque_type",
"named": true
"type": "opaque",
"named": false
},
{
"type": "or",
@@ -2934,7 +2998,7 @@
},
{
"type": "undefined",
"named": true
"named": false
},
{
"type": "union",
+329846 -253739
View File
File diff suppressed because it is too large Load Diff
@@ -138,6 +138,47 @@ sum func(a, b i32) i32 ! Status {
(enum_literal
(identifier))))))))))))
==================
Anonymous keyed records
==================
inferred :: {
x = 1,
int = "bro",
}
empty :: {}
pair :: {1, 2}
---
(source_file
(global_constant_declaration
(identifier)
(expression
(anonymous_record_literal
(keyed_field_initializer
(identifier)
(expression
(integer)))
(keyed_field_initializer
(identifier)
(expression
(string
(string_content)))))))
(global_constant_declaration
(identifier)
(expression
(tuple_literal)))
(global_constant_declaration
(identifier)
(expression
(tuple_literal
(expression
(integer))
(expression
(integer))))))
==================
Intrinsic calls
==================
@@ -166,6 +207,39 @@ main func() void {
(expression
(builtin_type))))))))))
==================
Intrinsic type construction
==================
Generated :: alias struct_type!(.auto, {"value"}, {i32}, {none})
---
(source_file
(type_declaration
(identifier)
(alias_type
(type
(intrinsic_type
(identifier)
(argument_list
(expression
(enum_literal
(identifier)))
(expression
(tuple_literal
(expression
(string
(string_content)))))
(expression
(tuple_literal
(expression
(builtin_type))))
(expression
(tuple_literal
(expression
(none))))))))))
==================
Errdefer
==================
@@ -19,3 +19,6 @@ clear func($K, $V type) void {
# ^^^ keyword
# ^ operator
}
Generated :: alias struct_type!(.auto, {"value"}, {i32}, {none})
# ^^^^^^^^^^^^ function.builtin