Skip to content

Commit

Permalink
Simplify composite_literal
Browse files Browse the repository at this point in the history
  • Loading branch information
adonovan committed Mar 25, 2022
1 parent 420ee79 commit 6c0eb66
Show file tree
Hide file tree
Showing 4 changed files with 17,160 additions and 17,274 deletions.
31 changes: 11 additions & 20 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -763,30 +763,21 @@ module.exports = grammar({

literal_value: $ => seq(
'{',
optional(seq(
choice($.element, $.keyed_element),
repeat(seq(',', choice($.element, $.keyed_element))),
optional(',')
)),
optional(
seq(
commaSep(choice($.literal_element, $.keyed_element)),
optional(','))),
'}'
),

keyed_element: $ => seq(
choice(
seq($._expression, ':'),
seq($.literal_value, ':'),
prec(1, seq($._field_identifier, ':'))
),
choice(
$._expression,
$.literal_value
)
),
literal_element: $ => choice($._expression, $.literal_value),

element: $ => choice(
$._expression,
$.literal_value
),
// In T{k: v}, the key k may be:
// - any expression (when T is a map, slice or array),
// - a field identifier (when T is a struct), or
// - a literal_element (when T is an array).
// The first two cases cannot be distinguished without type information.
keyed_element: $ => seq($.literal_element, ':', $.literal_element),

func_literal: $ => seq(
'func',
Expand Down
148 changes: 56 additions & 92 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -3759,40 +3759,53 @@
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "element"
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "literal_element"
},
{
"type": "SYMBOL",
"name": "keyed_element"
}
]
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "literal_element"
},
{
"type": "SYMBOL",
"name": "keyed_element"
}
]
}
]
}
}
]
},
{
"type": "SYMBOL",
"name": "keyed_element"
"type": "BLANK"
}
]
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "element"
},
{
"type": "SYMBOL",
"name": "keyed_element"
}
]
}
]
}
},
{
"type": "CHOICE",
"members": [
Expand All @@ -3818,82 +3831,33 @@
}
]
},
"keyed_element": {
"type": "SEQ",
"literal_element": {
"type": "CHOICE",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_expression"
},
{
"type": "STRING",
"value": ":"
}
]
},
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "literal_value"
},
{
"type": "STRING",
"value": ":"
}
]
},
{
"type": "PREC",
"value": 1,
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_field_identifier"
},
{
"type": "STRING",
"value": ":"
}
]
}
}
]
"type": "SYMBOL",
"name": "_expression"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_expression"
},
{
"type": "SYMBOL",
"name": "literal_value"
}
]
"type": "SYMBOL",
"name": "literal_value"
}
]
},
"element": {
"type": "CHOICE",
"keyed_element": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_expression"
"name": "literal_element"
},
{
"type": "STRING",
"value": ":"
},
{
"type": "SYMBOL",
"name": "literal_value"
"name": "literal_element"
}
]
},
Expand Down
52 changes: 22 additions & 30 deletions src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -822,25 +822,6 @@
"named": true,
"fields": {}
},
{
"type": "element",
"named": true,
"fields": {},
"children": {
"multiple": false,
"required": true,
"types": [
{
"type": "_expression",
"named": true
},
{
"type": "literal_value",
"named": true
}
]
}
},
{
"type": "empty_statement",
"named": true,
Expand Down Expand Up @@ -1496,15 +1477,7 @@
"required": true,
"types": [
{
"type": "_expression",
"named": true
},
{
"type": "field_identifier",
"named": true
},
{
"type": "literal_value",
"type": "literal_element",
"named": true
}
]
Expand Down Expand Up @@ -1536,6 +1509,25 @@
]
}
},
{
"type": "literal_element",
"named": true,
"fields": {},
"children": {
"multiple": false,
"required": true,
"types": [
{
"type": "_expression",
"named": true
},
{
"type": "literal_value",
"named": true
}
]
}
},
{
"type": "literal_value",
"named": true,
Expand All @@ -1545,11 +1537,11 @@
"required": false,
"types": [
{
"type": "element",
"type": "keyed_element",
"named": true
},
{
"type": "keyed_element",
"type": "literal_element",
"named": true
}
]
Expand Down
Loading

0 comments on commit 6c0eb66

Please sign in to comment.