Use the same AST node for all identifiers

This commit is contained in:
Jonatan Kłosko 2021-09-29 12:44:04 +02:00
parent 3cf73a2529
commit 18f6c6cc5d
7 changed files with 300008 additions and 312301 deletions

View File

@ -75,14 +75,6 @@ const RESERVED_WORD_TOKENS = [
["after", "catch", "do", "else", "end", "fn", "rescue"], ["after", "catch", "do", "else", "end", "fn", "rescue"],
].flat(); ].flat();
const SPECIAL_IDENTIFIERS = [
"__MODULE__",
"__DIR__",
"__ENV__",
"__CALLER__",
"__STACKTRACE__",
];
const DIGITS = /[0-9]+/; const DIGITS = /[0-9]+/;
const BIN_DIGITS = /[0-1]+/; const BIN_DIGITS = /[0-1]+/;
const OCT_DIGITS = /[0-7]+/; const OCT_DIGITS = /[0-7]+/;
@ -208,7 +200,7 @@ module.exports = grammar({
_expression: ($) => _expression: ($) =>
choice( choice(
$.block, $.block,
$._identifier, $.identifier,
$.alias, $.alias,
$.integer, $.integer,
$.float, $.float,
@ -247,20 +239,13 @@ module.exports = grammar({
")" ")"
), ),
_identifier: ($) =>
choice($.identifier, $.unused_identifier, $.special_identifier),
identifier: ($) => identifier: ($) =>
choice( choice(
// See Ref 6. in the docs // See Ref 6. in the docs
/[\p{Ll}\p{Lm}\p{Lo}\p{Nl}\u1885\u1886\u2118\u212E\u309B\u309C][\p{ID_Continue}]*[?!]?/u, /[_\p{Ll}\p{Lm}\p{Lo}\p{Nl}\u1885\u1886\u2118\u212E\u309B\u309C][\p{ID_Continue}]*[?!]?/u,
"..." "..."
), ),
unused_identifier: ($) => /_[\p{ID_Continue}]*[?!]?/u,
special_identifier: ($) => choice(...SPECIAL_IDENTIFIERS),
alias: ($) => token(sep1(/[A-Z][_a-zA-Z0-9]*/, /\s*\.\s*/)), alias: ($) => token(sep1(/[A-Z][_a-zA-Z0-9]*/, /\s*\.\s*/)),
integer: ($) => token(INTEGER), integer: ($) => token(INTEGER),
@ -422,7 +407,7 @@ module.exports = grammar({
choice( choice(
$.alias, $.alias,
$._atom, $._atom,
$._identifier, $.identifier,
$.unary_operator, $.unary_operator,
$.dot, $.dot,
alias($._call_with_parentheses, $.call) alias($._call_with_parentheses, $.call)
@ -572,7 +557,7 @@ module.exports = grammar({
_local_call_without_parentheses: ($) => _local_call_without_parentheses: ($) =>
prec.left( prec.left(
seq( seq(
field("target", $._identifier), field("target", $.identifier),
alias($._call_arguments_without_parentheses, $.arguments), alias($._call_arguments_without_parentheses, $.arguments),
optional(seq(optional($._newline_before_do), $.do_block)) optional(seq(optional($._newline_before_do), $.do_block))
) )
@ -581,7 +566,7 @@ module.exports = grammar({
_local_call_with_parentheses: ($) => _local_call_with_parentheses: ($) =>
prec.left( prec.left(
seq( seq(
field("target", $._identifier), field("target", $.identifier),
alias($._call_arguments_with_parentheses_immediate, $.arguments), alias($._call_arguments_with_parentheses_immediate, $.arguments),
optional(seq(optional($._newline_before_do), $.do_block)) optional(seq(optional($._newline_before_do), $.do_block))
) )
@ -589,7 +574,7 @@ module.exports = grammar({
_local_call_just_do_block: ($) => _local_call_just_do_block: ($) =>
// Lower precedence than identifier, because `foo bar do` is `foo(bar) do end` // Lower precedence than identifier, because `foo bar do` is `foo(bar) do end`
prec(-1, seq(field("target", $._identifier), $.do_block)), prec(-1, seq(field("target", $.identifier), $.do_block)),
_remote_call_without_parentheses: ($) => _remote_call_without_parentheses: ($) =>
prec.left( prec.left(
@ -618,7 +603,7 @@ module.exports = grammar({
field( field(
"right", "right",
choice( choice(
$._identifier, $.identifier,
alias(choice(...RESERVED_WORD_TOKENS), $.identifier), alias(choice(...RESERVED_WORD_TOKENS), $.identifier),
$.operator_identifier, $.operator_identifier,
alias($._quoted_i_double, $.string), alias($._quoted_i_double, $.string),

View File

@ -109,7 +109,7 @@
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_identifier" "name": "identifier"
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
@ -318,29 +318,12 @@
} }
] ]
}, },
"_identifier": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "SYMBOL",
"name": "unused_identifier"
},
{
"type": "SYMBOL",
"name": "special_identifier"
}
]
},
"identifier": { "identifier": {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{ {
"type": "PATTERN", "type": "PATTERN",
"value": "[\\p{Ll}\\p{Lm}\\p{Lo}\\p{Nl}\\u1885\\u1886\\u2118\\u212E\\u309B\\u309C][\\p{ID_Continue}]*[?!]?" "value": "[_\\p{Ll}\\p{Lm}\\p{Lo}\\p{Nl}\\u1885\\u1886\\u2118\\u212E\\u309B\\u309C][\\p{ID_Continue}]*[?!]?"
}, },
{ {
"type": "STRING", "type": "STRING",
@ -348,35 +331,6 @@
} }
] ]
}, },
"unused_identifier": {
"type": "PATTERN",
"value": "_[\\p{ID_Continue}]*[?!]?"
},
"special_identifier": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "__MODULE__"
},
{
"type": "STRING",
"value": "__DIR__"
},
{
"type": "STRING",
"value": "__ENV__"
},
{
"type": "STRING",
"value": "__CALLER__"
},
{
"type": "STRING",
"value": "__STACKTRACE__"
}
]
},
"alias": { "alias": {
"type": "TOKEN", "type": "TOKEN",
"content": { "content": {
@ -2695,7 +2649,7 @@
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_identifier" "name": "identifier"
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
@ -4195,7 +4149,7 @@
"name": "target", "name": "target",
"content": { "content": {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_identifier" "name": "identifier"
} }
}, },
{ {
@ -4250,7 +4204,7 @@
"name": "target", "name": "target",
"content": { "content": {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_identifier" "name": "identifier"
} }
}, },
{ {
@ -4305,7 +4259,7 @@
"name": "target", "name": "target",
"content": { "content": {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_identifier" "name": "identifier"
} }
}, },
{ {
@ -4473,7 +4427,7 @@
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_identifier" "name": "identifier"
}, },
{ {
"type": "ALIAS", "type": "ALIAS",

View File

@ -87,10 +87,6 @@
"type": "sigil", "type": "sigil",
"named": true "named": true
}, },
{
"type": "special_identifier",
"named": true
},
{ {
"type": "string", "type": "string",
"named": true "named": true
@ -102,10 +98,6 @@
{ {
"type": "unary_operator", "type": "unary_operator",
"named": true "named": true
},
{
"type": "unused_identifier",
"named": true
} }
] ]
}, },
@ -193,10 +185,6 @@
"type": "sigil", "type": "sigil",
"named": true "named": true
}, },
{
"type": "special_identifier",
"named": true
},
{ {
"type": "string", "type": "string",
"named": true "named": true
@ -208,10 +196,6 @@
{ {
"type": "unary_operator", "type": "unary_operator",
"named": true "named": true
},
{
"type": "unused_identifier",
"named": true
} }
] ]
} }
@ -305,10 +289,6 @@
"type": "sigil", "type": "sigil",
"named": true "named": true
}, },
{
"type": "special_identifier",
"named": true
},
{ {
"type": "stab_clause", "type": "stab_clause",
"named": true "named": true
@ -324,10 +304,6 @@
{ {
"type": "unary_operator", "type": "unary_operator",
"named": true "named": true
},
{
"type": "unused_identifier",
"named": true
} }
] ]
} }
@ -439,10 +415,6 @@
"type": "sigil", "type": "sigil",
"named": true "named": true
}, },
{
"type": "special_identifier",
"named": true
},
{ {
"type": "string", "type": "string",
"named": true "named": true
@ -454,10 +426,6 @@
{ {
"type": "unary_operator", "type": "unary_operator",
"named": true "named": true
},
{
"type": "unused_identifier",
"named": true
} }
] ]
} }
@ -558,10 +526,6 @@
"type": "sigil", "type": "sigil",
"named": true "named": true
}, },
{
"type": "special_identifier",
"named": true
},
{ {
"type": "string", "type": "string",
"named": true "named": true
@ -573,10 +537,6 @@
{ {
"type": "unary_operator", "type": "unary_operator",
"named": true "named": true
},
{
"type": "unused_identifier",
"named": true
} }
] ]
}, },
@ -854,10 +814,6 @@
"type": "sigil", "type": "sigil",
"named": true "named": true
}, },
{
"type": "special_identifier",
"named": true
},
{ {
"type": "string", "type": "string",
"named": true "named": true
@ -869,10 +825,6 @@
{ {
"type": "unary_operator", "type": "unary_operator",
"named": true "named": true
},
{
"type": "unused_identifier",
"named": true
} }
] ]
} }
@ -970,10 +922,6 @@
"type": "sigil", "type": "sigil",
"named": true "named": true
}, },
{
"type": "special_identifier",
"named": true
},
{ {
"type": "string", "type": "string",
"named": true "named": true
@ -985,10 +933,6 @@
{ {
"type": "unary_operator", "type": "unary_operator",
"named": true "named": true
},
{
"type": "unused_identifier",
"named": true
} }
] ]
} }
@ -1081,10 +1025,6 @@
"type": "sigil", "type": "sigil",
"named": true "named": true
}, },
{
"type": "special_identifier",
"named": true
},
{ {
"type": "stab_clause", "type": "stab_clause",
"named": true "named": true
@ -1100,10 +1040,6 @@
{ {
"type": "unary_operator", "type": "unary_operator",
"named": true "named": true
},
{
"type": "unused_identifier",
"named": true
} }
] ]
} }
@ -1196,10 +1132,6 @@
"type": "sigil", "type": "sigil",
"named": true "named": true
}, },
{
"type": "special_identifier",
"named": true
},
{ {
"type": "string", "type": "string",
"named": true "named": true
@ -1211,10 +1143,6 @@
{ {
"type": "unary_operator", "type": "unary_operator",
"named": true "named": true
},
{
"type": "unused_identifier",
"named": true
} }
] ]
} }
@ -1243,14 +1171,6 @@
{ {
"type": "identifier", "type": "identifier",
"named": true "named": true
},
{
"type": "special_identifier",
"named": true
},
{
"type": "unused_identifier",
"named": true
} }
] ]
} }
@ -1358,10 +1278,6 @@
"type": "sigil", "type": "sigil",
"named": true "named": true
}, },
{
"type": "special_identifier",
"named": true
},
{ {
"type": "stab_clause", "type": "stab_clause",
"named": true "named": true
@ -1377,10 +1293,6 @@
{ {
"type": "unary_operator", "type": "unary_operator",
"named": true "named": true
},
{
"type": "unused_identifier",
"named": true
} }
] ]
} }
@ -1541,10 +1453,6 @@
"type": "sigil", "type": "sigil",
"named": true "named": true
}, },
{
"type": "special_identifier",
"named": true
},
{ {
"type": "stab_clause", "type": "stab_clause",
"named": true "named": true
@ -1560,10 +1468,6 @@
{ {
"type": "unary_operator", "type": "unary_operator",
"named": true "named": true
},
{
"type": "unused_identifier",
"named": true
} }
] ]
} }
@ -1656,10 +1560,6 @@
"type": "sigil", "type": "sigil",
"named": true "named": true
}, },
{
"type": "special_identifier",
"named": true
},
{ {
"type": "string", "type": "string",
"named": true "named": true
@ -1671,10 +1571,6 @@
{ {
"type": "unary_operator", "type": "unary_operator",
"named": true "named": true
},
{
"type": "unused_identifier",
"named": true
} }
] ]
}, },
@ -1708,10 +1604,6 @@
"type": "operator_identifier", "type": "operator_identifier",
"named": true "named": true
}, },
{
"type": "special_identifier",
"named": true
},
{ {
"type": "string", "type": "string",
"named": true "named": true
@ -1719,10 +1611,6 @@
{ {
"type": "tuple", "type": "tuple",
"named": true "named": true
},
{
"type": "unused_identifier",
"named": true
} }
] ]
} }
@ -1816,10 +1704,6 @@
"type": "sigil", "type": "sigil",
"named": true "named": true
}, },
{
"type": "special_identifier",
"named": true
},
{ {
"type": "stab_clause", "type": "stab_clause",
"named": true "named": true
@ -1835,10 +1719,6 @@
{ {
"type": "unary_operator", "type": "unary_operator",
"named": true "named": true
},
{
"type": "unused_identifier",
"named": true
} }
] ]
} }
@ -1936,10 +1816,6 @@
"type": "sigil", "type": "sigil",
"named": true "named": true
}, },
{
"type": "special_identifier",
"named": true
},
{ {
"type": "string", "type": "string",
"named": true "named": true
@ -1951,10 +1827,6 @@
{ {
"type": "unary_operator", "type": "unary_operator",
"named": true "named": true
},
{
"type": "unused_identifier",
"named": true
} }
] ]
} }
@ -2066,10 +1938,6 @@
"type": "sigil", "type": "sigil",
"named": true "named": true
}, },
{
"type": "special_identifier",
"named": true
},
{ {
"type": "string", "type": "string",
"named": true "named": true
@ -2081,10 +1949,6 @@
{ {
"type": "unary_operator", "type": "unary_operator",
"named": true "named": true
},
{
"type": "unused_identifier",
"named": true
} }
] ]
} }
@ -2200,10 +2064,6 @@
"type": "sigil", "type": "sigil",
"named": true "named": true
}, },
{
"type": "special_identifier",
"named": true
},
{ {
"type": "string", "type": "string",
"named": true "named": true
@ -2215,10 +2075,6 @@
{ {
"type": "unary_operator", "type": "unary_operator",
"named": true "named": true
},
{
"type": "unused_identifier",
"named": true
} }
] ]
} }
@ -2335,10 +2191,6 @@
"type": "sigil", "type": "sigil",
"named": true "named": true
}, },
{
"type": "special_identifier",
"named": true
},
{ {
"type": "string", "type": "string",
"named": true "named": true
@ -2350,10 +2202,6 @@
{ {
"type": "unary_operator", "type": "unary_operator",
"named": true "named": true
},
{
"type": "unused_identifier",
"named": true
} }
] ]
} }
@ -2551,10 +2399,6 @@
"type": "sigil", "type": "sigil",
"named": true "named": true
}, },
{
"type": "special_identifier",
"named": true
},
{ {
"type": "stab_clause", "type": "stab_clause",
"named": true "named": true
@ -2570,10 +2414,6 @@
{ {
"type": "unary_operator", "type": "unary_operator",
"named": true "named": true
},
{
"type": "unused_identifier",
"named": true
} }
] ]
} }
@ -2790,10 +2630,6 @@
"type": "sigil", "type": "sigil",
"named": true "named": true
}, },
{
"type": "special_identifier",
"named": true
},
{ {
"type": "string", "type": "string",
"named": true "named": true
@ -2805,19 +2641,10 @@
{ {
"type": "unary_operator", "type": "unary_operator",
"named": true "named": true
},
{
"type": "unused_identifier",
"named": true
} }
] ]
} }
}, },
{
"type": "special_identifier",
"named": true,
"fields": {}
},
{ {
"type": "stab_clause", "type": "stab_clause",
"named": true, "named": true,
@ -2942,17 +2769,9 @@
"type": "quoted_atom", "type": "quoted_atom",
"named": true "named": true
}, },
{
"type": "special_identifier",
"named": true
},
{ {
"type": "unary_operator", "type": "unary_operator",
"named": true "named": true
},
{
"type": "unused_identifier",
"named": true
} }
] ]
} }
@ -3049,10 +2868,6 @@
"type": "sigil", "type": "sigil",
"named": true "named": true
}, },
{
"type": "special_identifier",
"named": true
},
{ {
"type": "string", "type": "string",
"named": true "named": true
@ -3064,10 +2879,6 @@
{ {
"type": "unary_operator", "type": "unary_operator",
"named": true "named": true
},
{
"type": "unused_identifier",
"named": true
} }
] ]
} }
@ -3168,10 +2979,6 @@
"type": "sigil", "type": "sigil",
"named": true "named": true
}, },
{
"type": "special_identifier",
"named": true
},
{ {
"type": "string", "type": "string",
"named": true "named": true
@ -3183,10 +2990,6 @@
{ {
"type": "unary_operator", "type": "unary_operator",
"named": true "named": true
},
{
"type": "unused_identifier",
"named": true
} }
] ]
}, },
@ -3458,26 +3261,6 @@
"type": "^^^", "type": "^^^",
"named": false "named": false
}, },
{
"type": "__CALLER__",
"named": false
},
{
"type": "__DIR__",
"named": false
},
{
"type": "__ENV__",
"named": false
},
{
"type": "__MODULE__",
"named": false
},
{
"type": "__STACKTRACE__",
"named": false
},
{ {
"type": "after", "type": "after",
"named": false "named": false
@ -3578,10 +3361,6 @@
"type": "true", "type": "true",
"named": false "named": false
}, },
{
"type": "unused_identifier",
"named": true
},
{ {
"type": "when", "type": "when",
"named": false "named": false

611981
src/parser.c

File diff suppressed because it is too large Load Diff

View File

@ -80,5 +80,5 @@ __MODULE__.Child
(source (source
(dot (dot
(special_identifier) (identifier)
(alias))) (alias)))

View File

@ -146,7 +146,7 @@ unused struct identifier
(source (source
(map (map
(struct (struct
(unused_identifier)))) (identifier))))
===================================== =====================================
matching struct identifier matching struct identifier
@ -187,11 +187,11 @@ with special identifier
(source (source
(map (map
(struct (struct
(special_identifier))) (identifier)))
(map (map
(struct (struct
(dot (dot
(special_identifier) (identifier)
(alias))))) (alias)))))
===================================== =====================================

View File

@ -34,9 +34,9 @@ __TEST__
--- ---
(source (source
(unused_identifier) (identifier)
(unused_identifier) (identifier)
(unused_identifier)) (identifier))
===================================== =====================================
three dots identifier three dots identifier
@ -59,5 +59,5 @@ __DIR__
--- ---
(source (source
(special_identifier) (identifier)
(special_identifier)) (identifier))