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,36 +605,46 @@ module.exports = grammar({
prec(
PREC.DOT_OP,
seq(
$._expression,
".",
choice(
$._identifier,
alias(choice(...RESERVED_WORD_TOKENS), $.identifier),
$.operator_identifier,
alias($._quoted_i_double, $.string),
alias($._quoted_i_single, $.charlist)
field("left", $._expression),
field("operator", "."),
field(
"right",
choice(
$._identifier,
alias(choice(...RESERVED_WORD_TOKENS), $.identifier),
$.operator_identifier,
alias($._quoted_i_double, $.string),
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(
alias(
choice(
$._local_call_with_parentheses,
$._remote_call_with_parentheses,
$._anonymous_call
),
$.call
field(
"target",
alias(
choice(
$._local_call_with_parentheses,
$._remote_call_with_parentheses,
$._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,12 +2102,20 @@
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_keyword"
"type": "FIELD",
"name": "key",
"content": {
"type": "SYMBOL",
"name": "_keyword"
}
},
{
"type": "SYMBOL",
"name": "_expression"
"type": "FIELD",
"name": "value",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
}
]
},
@ -2690,8 +2698,12 @@
}
},
{
"type": "SYMBOL",
"name": "_capture_expression"
"type": "FIELD",
"name": "operand",
"content": {
"type": "SYMBOL",
"name": "_capture_expression"
}
}
]
}
@ -2752,8 +2764,12 @@
}
},
{
"type": "SYMBOL",
"name": "_expression"
"type": "FIELD",
"name": "operand",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
}
]
}
@ -2789,8 +2805,12 @@
}
},
{
"type": "SYMBOL",
"name": "_expression"
"type": "FIELD",
"name": "operand",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
}
]
}
@ -2826,8 +2846,12 @@
}
},
{
"type": "SYMBOL",
"name": "integer"
"type": "FIELD",
"name": "operand",
"content": {
"type": "SYMBOL",
"name": "integer"
}
}
]
}
@ -3914,30 +3938,37 @@
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_expression"
}
]
"type": "FIELD",
"name": "left",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
},
{
"type": "STRING",
"value": "."
"type": "FIELD",
"name": "operator",
"content": {
"type": "STRING",
"value": "."
}
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "alias"
},
{
"type": "SYMBOL",
"name": "tuple"
}
]
"type": "FIELD",
"name": "right",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "alias"
},
{
"type": "SYMBOL",
"name": "tuple"
}
]
}
}
]
}
@ -4000,8 +4031,12 @@
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_identifier"
"type": "FIELD",
"name": "target",
"content": {
"type": "SYMBOL",
"name": "_identifier"
}
},
{
"type": "ALIAS",
@ -4051,8 +4086,12 @@
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_identifier"
"type": "FIELD",
"name": "target",
"content": {
"type": "SYMBOL",
"name": "_identifier"
}
},
{
"type": "ALIAS",
@ -4102,8 +4141,12 @@
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_identifier"
"type": "FIELD",
"name": "target",
"content": {
"type": "SYMBOL",
"name": "_identifier"
}
},
{
"type": "SYMBOL",
@ -4119,13 +4162,17 @@
"type": "SEQ",
"members": [
{
"type": "ALIAS",
"type": "FIELD",
"name": "target",
"content": {
"type": "SYMBOL",
"name": "_remote_dot"
},
"named": true,
"value": "dot"
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_remote_dot"
},
"named": true,
"value": "dot"
}
},
{
"type": "CHOICE",
@ -4183,13 +4230,17 @@
"type": "SEQ",
"members": [
{
"type": "ALIAS",
"type": "FIELD",
"name": "target",
"content": {
"type": "SYMBOL",
"name": "_remote_dot"
},
"named": true,
"value": "dot"
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_remote_dot"
},
"named": true,
"value": "dot"
}
},
{
"type": "ALIAS",
@ -4239,113 +4290,125 @@
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_expression"
"type": "FIELD",
"name": "left",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
},
{
"type": "STRING",
"value": "."
"type": "FIELD",
"name": "operator",
"content": {
"type": "STRING",
"value": "."
}
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_identifier"
},
{
"type": "ALIAS",
"content": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "and"
},
{
"type": "STRING",
"value": "in"
},
{
"type": "STRING",
"value": "not"
},
{
"type": "STRING",
"value": "or"
},
{
"type": "STRING",
"value": "when"
},
{
"type": "STRING",
"value": "true"
},
{
"type": "STRING",
"value": "false"
},
{
"type": "STRING",
"value": "nil"
},
{
"type": "STRING",
"value": "after"
},
{
"type": "STRING",
"value": "catch"
},
{
"type": "STRING",
"value": "do"
},
{
"type": "STRING",
"value": "else"
},
{
"type": "STRING",
"value": "end"
},
{
"type": "STRING",
"value": "fn"
},
{
"type": "STRING",
"value": "rescue"
}
]
},
"named": true,
"value": "identifier"
},
{
"type": "SYMBOL",
"name": "operator_identifier"
},
{
"type": "ALIAS",
"content": {
"type": "FIELD",
"name": "right",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_quoted_i_double"
"name": "_identifier"
},
"named": true,
"value": "string"
},
{
"type": "ALIAS",
"content": {
{
"type": "ALIAS",
"content": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "and"
},
{
"type": "STRING",
"value": "in"
},
{
"type": "STRING",
"value": "not"
},
{
"type": "STRING",
"value": "or"
},
{
"type": "STRING",
"value": "when"
},
{
"type": "STRING",
"value": "true"
},
{
"type": "STRING",
"value": "false"
},
{
"type": "STRING",
"value": "nil"
},
{
"type": "STRING",
"value": "after"
},
{
"type": "STRING",
"value": "catch"
},
{
"type": "STRING",
"value": "do"
},
{
"type": "STRING",
"value": "else"
},
{
"type": "STRING",
"value": "end"
},
{
"type": "STRING",
"value": "fn"
},
{
"type": "STRING",
"value": "rescue"
}
]
},
"named": true,
"value": "identifier"
},
{
"type": "SYMBOL",
"name": "_quoted_i_single"
"name": "operator_identifier"
},
"named": true,
"value": "charlist"
}
]
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_quoted_i_double"
},
"named": true,
"value": "string"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_quoted_i_single"
},
"named": true,
"value": "charlist"
}
]
}
}
]
}
@ -4354,13 +4417,17 @@
"type": "SEQ",
"members": [
{
"type": "ALIAS",
"type": "FIELD",
"name": "target",
"content": {
"type": "SYMBOL",
"name": "_anonymous_dot"
},
"named": true,
"value": "dot"
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_anonymous_dot"
},
"named": true,
"value": "dot"
}
},
{
"type": "ALIAS",
@ -4380,12 +4447,20 @@
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_expression"
"type": "FIELD",
"name": "left",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
},
{
"type": "STRING",
"value": "."
"type": "FIELD",
"name": "operator",
"content": {
"type": "STRING",
"value": "."
}
}
]
}
@ -4397,26 +4472,30 @@
"type": "SEQ",
"members": [
{
"type": "ALIAS",
"type": "FIELD",
"name": "target",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_local_call_with_parentheses"
},
{
"type": "SYMBOL",
"name": "_remote_call_with_parentheses"
},
{
"type": "SYMBOL",
"name": "_anonymous_call"
}
]
},
"named": true,
"value": "call"
"type": "ALIAS",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_local_call_with_parentheses"
},
{
"type": "SYMBOL",
"name": "_remote_call_with_parentheses"
},
{
"type": "SYMBOL",
"name": "_anonymous_call"
}
]
},
"named": true,
"value": "call"
}
},
{
"type": "ALIAS",
@ -5291,8 +5370,12 @@
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_expression"
"type": "FIELD",
"name": "target",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
},
{
"type": "IMMEDIATE_TOKEN",
@ -5302,8 +5385,12 @@
}
},
{
"type": "SYMBOL",
"name": "_expression"
"type": "FIELD",
"name": "key",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
},
{
"type": "STRING",
@ -5322,8 +5409,12 @@
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_stab_clause_left"
"type": "FIELD",
"name": "left",
"content": {
"type": "SYMBOL",
"name": "_stab_clause_left"
}
},
{
"type": "BLANK"
@ -5331,15 +5422,23 @@
]
},
{
"type": "STRING",
"value": "->"
"type": "FIELD",
"name": "operator",
"content": {
"type": "STRING",
"value": "->"
}
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "body"
"type": "FIELD",
"name": "right",
"content": {
"type": "SYMBOL",
"name": "body"
}
},
{
"type": "BLANK"
@ -5551,21 +5650,33 @@
"type": "SEQ",
"members": [
{
"type": "ALIAS",
"type": "FIELD",
"name": "left",
"content": {
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_stab_clause_arguments_with_parentheses"
},
"named": true,
"value": "arguments"
}
},
{
"type": "FIELD",
"name": "operator",
"content": {
"type": "STRING",
"value": "when"
}
},
{
"type": "FIELD",
"name": "right",
"content": {
"type": "SYMBOL",
"name": "_stab_clause_arguments_with_parentheses"
},
"named": true,
"value": "arguments"
},
{
"type": "STRING",
"value": "when"
},
{
"type": "SYMBOL",
"name": "_expression"
"name": "_expression"
}
}
]
},
@ -5576,21 +5687,33 @@
"type": "SEQ",
"members": [
{
"type": "ALIAS",
"type": "FIELD",
"name": "left",
"content": {
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_stab_clause_arguments_without_parentheses"
},
"named": true,
"value": "arguments"
}
},
{
"type": "FIELD",
"name": "operator",
"content": {
"type": "STRING",
"value": "when"
}
},
{
"type": "FIELD",
"name": "right",
"content": {
"type": "SYMBOL",
"name": "_stab_clause_arguments_without_parentheses"
},
"named": true,
"value": "arguments"
},
{
"type": "STRING",
"value": "when"
},
{
"type": "SYMBOL",
"name": "_expression"
"name": "_expression"
}
}
]
}

File diff suppressed because it is too large Load Diff

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
=====================================