Add field names

This commit is contained in:
Jonatan Kłosko 2021-09-28 17:26:43 +02:00
parent d229dddf66
commit bb5f90516e
8 changed files with 270366 additions and 259448 deletions

View File

@ -364,7 +364,7 @@ module.exports = grammar({
_keywords_with_trailing_separator: ($) =>
seq(sep1($.pair, ","), optional(",")),
pair: ($) => seq($._keyword, $._expression),
pair: ($) => seq(field("key", $._keyword), field("value", $._expression)),
_keyword: ($) => choice($.keyword, $.quoted_keyword),
@ -534,7 +534,11 @@ module.exports = grammar({
dot: ($) =>
prec(
PREC.DOT_OP,
seq(choice($._expression), ".", choice($.alias, $.tuple))
seq(
field("left", $._expression),
field("operator", "."),
field("right", choice($.alias, $.tuple))
)
),
call: ($) => choice($._call_without_parentheses, $._call_with_parentheses),
@ -560,7 +564,7 @@ module.exports = grammar({
_local_call_without_parentheses: ($) =>
prec.left(
seq(
$._identifier,
field("target", $._identifier),
alias($._call_arguments_without_parentheses, $.arguments),
optional(seq(optional($._newline_before_do), $.do_block))
)
@ -569,7 +573,7 @@ module.exports = grammar({
_local_call_with_parentheses: ($) =>
prec.left(
seq(
$._identifier,
field("target", $._identifier),
alias($._call_arguments_with_parentheses_immediate, $.arguments),
optional(seq(optional($._newline_before_do), $.do_block))
)
@ -577,12 +581,12 @@ module.exports = grammar({
_local_call_just_do_block: ($) =>
// Lower precedence than identifier, because `foo bar do` is `foo(bar) do end`
prec(-1, seq($._identifier, $.do_block)),
prec(-1, seq(field("target", $._identifier), $.do_block)),
_remote_call_without_parentheses: ($) =>
prec.left(
seq(
alias($._remote_dot, $.dot),
field("target", alias($._remote_dot, $.dot)),
optional(alias($._call_arguments_without_parentheses, $.arguments)),
optional(seq(optional($._newline_before_do), $.do_block))
)
@ -591,7 +595,7 @@ module.exports = grammar({
_remote_call_with_parentheses: ($) =>
prec.left(
seq(
alias($._remote_dot, $.dot),
field("target", alias($._remote_dot, $.dot)),
alias($._call_arguments_with_parentheses_immediate, $.arguments),
optional(seq(optional($._newline_before_do), $.do_block))
)
@ -601,8 +605,10 @@ module.exports = grammar({
prec(
PREC.DOT_OP,
seq(
$._expression,
".",
field("left", $._expression),
field("operator", "."),
field(
"right",
choice(
$._identifier,
alias(choice(...RESERVED_WORD_TOKENS), $.identifier),
@ -611,19 +617,26 @@ module.exports = grammar({
alias($._quoted_i_single, $.charlist)
)
)
)
),
_anonymous_call: ($) =>
seq(
alias($._anonymous_dot, $.dot),
field("target", alias($._anonymous_dot, $.dot)),
alias($._call_arguments_with_parentheses, $.arguments)
),
_anonymous_dot: ($) => prec(PREC.DOT_OP, seq($._expression, ".")),
_anonymous_dot: ($) =>
prec(
PREC.DOT_OP,
seq(field("left", $._expression), field("operator", "."))
),
_double_call: ($) =>
prec.left(
seq(
field(
"target",
alias(
choice(
$._local_call_with_parentheses,
@ -631,6 +644,7 @@ module.exports = grammar({
$._anonymous_call
),
$.call
)
),
alias($._call_arguments_with_parentheses, $.arguments),
optional(seq(optional($._newline_before_do), $.do_block))
@ -684,12 +698,23 @@ module.exports = grammar({
access_call: ($) =>
prec(
PREC.ACCESS,
seq($._expression, token.immediate("["), $._expression, "]")
seq(
field("target", $._expression),
token.immediate("["),
field("key", $._expression),
"]"
)
),
stab_clause: ($) =>
// Right precedence, because we want to consume body if any
prec.right(seq(optional($._stab_clause_left), "->", optional($.body))),
prec.right(
seq(
optional(field("left", $._stab_clause_left)),
field("operator", "->"),
optional(field("right", $.body))
)
),
_stab_clause_left: ($) =>
choice(
@ -738,9 +763,12 @@ module.exports = grammar({
_stab_clause_arguments_with_parentheses_with_guard: ($) =>
seq(
alias($._stab_clause_arguments_with_parentheses, $.arguments),
"when",
$._expression
field(
"left",
alias($._stab_clause_arguments_with_parentheses, $.arguments)
),
field("operator", "when"),
field("right", $._expression)
),
_stab_clause_arguments_without_parentheses_with_guard: ($) =>
@ -751,9 +779,12 @@ module.exports = grammar({
prec.dynamic(
1,
seq(
alias($._stab_clause_arguments_without_parentheses, $.arguments),
"when",
$._expression
field(
"left",
alias($._stab_clause_arguments_without_parentheses, $.arguments)
),
field("operator", "when"),
field("right", $._expression)
)
),
@ -798,7 +829,7 @@ function unaryOp($, assoc, precedence, operator, right = null) {
seq(
optional($._before_unary_op),
field("operator", operator),
right || $._expression
field("operand", right || $._expression)
)
)
);

View File

@ -2102,13 +2102,21 @@
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "key",
"content": {
"type": "SYMBOL",
"name": "_keyword"
}
},
{
"type": "FIELD",
"name": "value",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
}
]
},
"_keyword": {
@ -2690,9 +2698,13 @@
}
},
{
"type": "FIELD",
"name": "operand",
"content": {
"type": "SYMBOL",
"name": "_capture_expression"
}
}
]
}
}
@ -2752,9 +2764,13 @@
}
},
{
"type": "FIELD",
"name": "operand",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
}
]
}
}
@ -2789,9 +2805,13 @@
}
},
{
"type": "FIELD",
"name": "operand",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
}
]
}
}
@ -2826,9 +2846,13 @@
}
},
{
"type": "FIELD",
"name": "operand",
"content": {
"type": "SYMBOL",
"name": "integer"
}
}
]
}
}
@ -3914,19 +3938,25 @@
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "FIELD",
"name": "left",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
]
},
{
"type": "FIELD",
"name": "operator",
"content": {
"type": "STRING",
"value": "."
}
},
{
"type": "FIELD",
"name": "right",
"content": {
"type": "CHOICE",
"members": [
{
@ -3939,6 +3969,7 @@
}
]
}
}
]
}
},
@ -4000,8 +4031,12 @@
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "target",
"content": {
"type": "SYMBOL",
"name": "_identifier"
}
},
{
"type": "ALIAS",
@ -4051,8 +4086,12 @@
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "target",
"content": {
"type": "SYMBOL",
"name": "_identifier"
}
},
{
"type": "ALIAS",
@ -4102,8 +4141,12 @@
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "target",
"content": {
"type": "SYMBOL",
"name": "_identifier"
}
},
{
"type": "SYMBOL",
@ -4119,6 +4162,9 @@
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "target",
"content": {
"type": "ALIAS",
"content": {
"type": "SYMBOL",
@ -4126,6 +4172,7 @@
},
"named": true,
"value": "dot"
}
},
{
"type": "CHOICE",
@ -4183,6 +4230,9 @@
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "target",
"content": {
"type": "ALIAS",
"content": {
"type": "SYMBOL",
@ -4190,6 +4240,7 @@
},
"named": true,
"value": "dot"
}
},
{
"type": "ALIAS",
@ -4239,14 +4290,25 @@
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "left",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
},
{
"type": "FIELD",
"name": "operator",
"content": {
"type": "STRING",
"value": "."
}
},
{
"type": "FIELD",
"name": "right",
"content": {
"type": "CHOICE",
"members": [
{
@ -4347,6 +4409,7 @@
}
]
}
}
]
}
},
@ -4354,6 +4417,9 @@
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "target",
"content": {
"type": "ALIAS",
"content": {
"type": "SYMBOL",
@ -4361,6 +4427,7 @@
},
"named": true,
"value": "dot"
}
},
{
"type": "ALIAS",
@ -4380,13 +4447,21 @@
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "left",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
},
{
"type": "FIELD",
"name": "operator",
"content": {
"type": "STRING",
"value": "."
}
}
]
}
},
@ -4397,6 +4472,9 @@
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "target",
"content": {
"type": "ALIAS",
"content": {
"type": "CHOICE",
@ -4417,6 +4495,7 @@
},
"named": true,
"value": "call"
}
},
{
"type": "ALIAS",
@ -5291,8 +5370,12 @@
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "target",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
},
{
"type": "IMMEDIATE_TOKEN",
@ -5302,8 +5385,12 @@
}
},
{
"type": "FIELD",
"name": "key",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
},
{
"type": "STRING",
@ -5322,8 +5409,12 @@
"type": "CHOICE",
"members": [
{
"type": "FIELD",
"name": "left",
"content": {
"type": "SYMBOL",
"name": "_stab_clause_left"
}
},
{
"type": "BLANK"
@ -5331,15 +5422,23 @@
]
},
{
"type": "FIELD",
"name": "operator",
"content": {
"type": "STRING",
"value": "->"
}
},
{
"type": "CHOICE",
"members": [
{
"type": "FIELD",
"name": "right",
"content": {
"type": "SYMBOL",
"name": "body"
}
},
{
"type": "BLANK"
@ -5551,6 +5650,9 @@
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "left",
"content": {
"type": "ALIAS",
"content": {
"type": "SYMBOL",
@ -5558,15 +5660,24 @@
},
"named": true,
"value": "arguments"
}
},
{
"type": "FIELD",
"name": "operator",
"content": {
"type": "STRING",
"value": "when"
}
},
{
"type": "FIELD",
"name": "right",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
}
]
},
"_stab_clause_arguments_without_parentheses_with_guard": {
@ -5576,6 +5687,9 @@
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "left",
"content": {
"type": "ALIAS",
"content": {
"type": "SYMBOL",
@ -5583,15 +5697,24 @@
},
"named": true,
"value": "arguments"
}
},
{
"type": "FIELD",
"name": "operator",
"content": {
"type": "STRING",
"value": "when"
}
},
{
"type": "FIELD",
"name": "right",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
}
]
}
},

View File

@ -2,9 +2,9 @@
{
"type": "access_call",
"named": true,
"fields": {},
"children": {
"multiple": true,
"fields": {
"key": {
"multiple": false,
"required": true,
"types": [
{
@ -108,6 +108,113 @@
"named": true
}
]
},
"target": {
"multiple": false,
"required": true,
"types": [
{
"type": "access_call",
"named": true
},
{
"type": "alias",
"named": true
},
{
"type": "anonymous_function",
"named": true
},
{
"type": "atom",
"named": true
},
{
"type": "binary_operator",
"named": true
},
{
"type": "bitstring",
"named": true
},
{
"type": "block",
"named": true
},
{
"type": "boolean",
"named": true
},
{
"type": "call",
"named": true
},
{
"type": "char",
"named": true
},
{
"type": "charlist",
"named": true
},
{
"type": "dot",
"named": true
},
{
"type": "float",
"named": true
},
{
"type": "identifier",
"named": true
},
{
"type": "integer",
"named": true
},
{
"type": "list",
"named": true
},
{
"type": "map",
"named": true
},
{
"type": "nil",
"named": true
},
{
"type": "quoted_atom",
"named": true
},
{
"type": "sigil",
"named": true
},
{
"type": "special_identifier",
"named": true
},
{
"type": "string",
"named": true
},
{
"type": "tuple",
"named": true
},
{
"type": "unary_operator",
"named": true
},
{
"type": "unused_identifier",
"named": true
}
]
}
}
},
{
@ -361,7 +468,7 @@
"fields": {
"left": {
"multiple": false,
"required": false,
"required": true,
"types": [
{
"type": "access_call",
@ -375,6 +482,10 @@
"type": "anonymous_function",
"named": true
},
{
"type": "arguments",
"named": true
},
{
"type": "atom",
"named": true
@ -471,7 +582,7 @@
},
"operator": {
"multiple": false,
"required": false,
"required": true,
"types": [
{
"type": "!=",
@ -657,7 +768,7 @@
},
"right": {
"multiple": false,
"required": false,
"required": true,
"types": [
{
"type": "access_call",
@ -765,116 +876,6 @@
}
]
}
},
"children": {
"multiple": true,
"required": false,
"types": [
{
"type": "access_call",
"named": true
},
{
"type": "alias",
"named": true
},
{
"type": "anonymous_function",
"named": true
},
{
"type": "arguments",
"named": true
},
{
"type": "atom",
"named": true
},
{
"type": "binary_operator",
"named": true
},
{
"type": "bitstring",
"named": true
},
{
"type": "block",
"named": true
},
{
"type": "boolean",
"named": true
},
{
"type": "call",
"named": true
},
{
"type": "char",
"named": true
},
{
"type": "charlist",
"named": true
},
{
"type": "dot",
"named": true
},
{
"type": "float",
"named": true
},
{
"type": "identifier",
"named": true
},
{
"type": "integer",
"named": true
},
{
"type": "list",
"named": true
},
{
"type": "map",
"named": true
},
{
"type": "nil",
"named": true
},
{
"type": "quoted_atom",
"named": true
},
{
"type": "sigil",
"named": true
},
{
"type": "special_identifier",
"named": true
},
{
"type": "string",
"named": true
},
{
"type": "tuple",
"named": true
},
{
"type": "unary_operator",
"named": true
},
{
"type": "unused_identifier",
"named": true
}
]
}
},
{
@ -1226,23 +1227,15 @@
{
"type": "call",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": false,
"fields": {
"target": {
"multiple": false,
"required": true,
"types": [
{
"type": "arguments",
"named": true
},
{
"type": "call",
"named": true
},
{
"type": "do_block",
"named": true
},
{
"type": "dot",
"named": true
@ -1262,6 +1255,21 @@
]
}
},
"children": {
"multiple": true,
"required": false,
"types": [
{
"type": "arguments",
"named": true
},
{
"type": "do_block",
"named": true
}
]
}
},
{
"type": "catch_block",
"named": true,
@ -1534,9 +1542,9 @@
{
"type": "dot",
"named": true,
"fields": {},
"children": {
"multiple": true,
"fields": {
"left": {
"multiple": false,
"required": true,
"types": [
{
@ -1611,10 +1619,6 @@
"type": "nil",
"named": true
},
{
"type": "operator_identifier",
"named": true
},
{
"type": "quoted_atom",
"named": true
@ -1644,6 +1648,55 @@
"named": true
}
]
},
"operator": {
"multiple": false,
"required": true,
"types": [
{
"type": ".",
"named": false
}
]
},
"right": {
"multiple": false,
"required": false,
"types": [
{
"type": "alias",
"named": true
},
{
"type": "charlist",
"named": true
},
{
"type": "identifier",
"named": true
},
{
"type": "operator_identifier",
"named": true
},
{
"type": "special_identifier",
"named": true
},
{
"type": "string",
"named": true
},
{
"type": "tuple",
"named": true
},
{
"type": "unused_identifier",
"named": true
}
]
}
}
},
{
@ -2154,9 +2207,23 @@
{
"type": "pair",
"named": true,
"fields": {},
"children": {
"multiple": true,
"fields": {
"key": {
"multiple": false,
"required": true,
"types": [
{
"type": "keyword",
"named": true
},
{
"type": "quoted_keyword",
"named": true
}
]
},
"value": {
"multiple": false,
"required": true,
"types": [
{
@ -2219,10 +2286,6 @@
"type": "integer",
"named": true
},
{
"type": "keyword",
"named": true
},
{
"type": "list",
"named": true
@ -2239,10 +2302,6 @@
"type": "quoted_atom",
"named": true
},
{
"type": "quoted_keyword",
"named": true
},
{
"type": "sigil",
"named": true
@ -2269,6 +2328,7 @@
}
]
}
}
},
{
"type": "quoted_atom",
@ -2581,9 +2641,9 @@
{
"type": "stab_clause",
"named": true,
"fields": {},
"children": {
"multiple": true,
"fields": {
"left": {
"multiple": false,
"required": false,
"types": [
{
@ -2593,13 +2653,30 @@
{
"type": "binary_operator",
"named": true
}
]
},
"operator": {
"multiple": false,
"required": true,
"types": [
{
"type": "->",
"named": false
}
]
},
"right": {
"multiple": false,
"required": false,
"types": [
{
"type": "body",
"named": true
}
]
}
}
},
{
"type": "string",
@ -2790,49 +2867,18 @@
"type": "unary_operator",
"named": true,
"fields": {
"operator": {
"multiple": false,
"operand": {
"multiple": true,
"required": true,
"types": [
{
"type": "!",
"type": "(",
"named": false
},
{
"type": "&",
"type": ")",
"named": false
},
{
"type": "+",
"named": false
},
{
"type": "-",
"named": false
},
{
"type": "@",
"named": false
},
{
"type": "^",
"named": false
},
{
"type": "not",
"named": false
},
{
"type": "~~~",
"named": false
}
]
}
},
"children": {
"multiple": false,
"required": true,
"types": [
{
"type": "access_call",
"named": true
@ -2934,6 +2980,45 @@
"named": true
}
]
},
"operator": {
"multiple": false,
"required": true,
"types": [
{
"type": "!",
"named": false
},
{
"type": "&",
"named": false
},
{
"type": "+",
"named": false
},
{
"type": "-",
"named": false
},
{
"type": "@",
"named": false
},
{
"type": "^",
"named": false
},
{
"type": "not",
"named": false
},
{
"type": "~~~",
"named": false
}
]
}
}
},
{

527802
src/parser.c

File diff suppressed because it is too large Load Diff

View File

@ -771,3 +771,33 @@ end
(integer))))
(body
(identifier)))))))
=====================================
[field names]
=====================================
fun do
x -> x
x when x == [] -> x
end
---
(source
(call
target: (identifier)
(do_block
(stab_clause
left: (arguments
(identifier))
right: (body
(identifier)))
(stab_clause
left: (binary_operator
left: (arguments
(identifier))
right: (binary_operator
left: (identifier)
right: (list)))
right: (body
(identifier))))))

View File

@ -916,6 +916,46 @@ unquote(name)()
(identifier)))
(arguments)))
=====================================
[field names]
=====================================
fun()
fun a
Mod.fun a
fun()()
fun.()
map[key]
---
(source
(call
target: (identifier)
(arguments))
(call
target: (identifier)
(arguments
(identifier)))
(call
target: (dot
left: (alias)
right: (identifier))
(arguments
(identifier)))
(call
target: (call
target: (identifier)
(arguments))
(arguments))
(call
target: (dot
left: (identifier))
(arguments))
(access_call
target: (identifier)
key: (identifier)))
=====================================
[error] leading argument separator
=====================================

View File

@ -742,3 +742,19 @@ foo do end..bar do end//baz do end
(integer)))
(block
(integer))))
=====================================
[field names]
=====================================
a + b
@a
---
(source
(binary_operator
left: (identifier)
right: (identifier))
(unary_operator
operand: (identifier)))

View File

@ -194,6 +194,23 @@ key interpolation
(quoted_content))
(integer)))))
=====================================
[field names]
=====================================
[a: 1, b: 2]
---
(source
(list
(keywords
(pair
key: (keyword)
value: (integer))
(pair
key: (keyword)
value: (integer)))))
=====================================
[error] with trailing items
=====================================