Skip to content

Commit 5b305c3

Browse files
authored
Merge pull request #220 from aibaars/parser-improvements-2
Parser improvements: `!=` operator and `key: [line_break]`
2 parents 6334d6a + 72bf3a1 commit 5b305c3

File tree

7 files changed

+319590
-307594
lines changed

7 files changed

+319590
-307594
lines changed

grammar.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ module.exports = grammar({
3636
inline: $ => [$._arg_rhs, $._call_operator],
3737
externals: $ => [
3838
$._line_break,
39+
$._no_line_break,
3940

4041
// Delimited literals
4142
$.simple_symbol,
@@ -62,6 +63,8 @@ module.exports = grammar({
6263
$._binary_star,
6364
$._singleton_class_left_angle_left_langle,
6465
$.hash_key_symbol,
66+
$._identifier_suffix,
67+
$._constant_suffix,
6568
$._hash_splat_star_star,
6669
$._binary_star_star,
6770
$._element_reference_bracket,
@@ -1054,9 +1057,9 @@ module.exports = grammar({
10541057
nil: $ => 'nil',
10551058

10561059
constant: $ => token(seq(/[A-Z]/, IDENTIFIER_CHARS)),
1057-
constant_suffix: $ => token(seq(/[A-Z]/, IDENTIFIER_CHARS, /[?!]/)),
1060+
constant_suffix: $ => choice(token(seq(/[A-Z]/, IDENTIFIER_CHARS, /[?]/)), $._constant_suffix),
10581061
identifier: $ => token(seq(LOWER_ALPHA_CHAR, IDENTIFIER_CHARS)),
1059-
identifier_suffix: $ => token(seq(LOWER_ALPHA_CHAR, IDENTIFIER_CHARS, /[?!]/)),
1062+
identifier_suffix: $ => choice(token(seq(LOWER_ALPHA_CHAR, IDENTIFIER_CHARS, /[?]/)), $._identifier_suffix),
10601063
instance_variable: $ => token(seq('@', ALPHA_CHAR, IDENTIFIER_CHARS)),
10611064
class_variable: $ => token(seq('@@', ALPHA_CHAR, IDENTIFIER_CHARS)),
10621065

@@ -1175,7 +1178,12 @@ module.exports = grammar({
11751178
alias($.constant_suffix, $.hash_key_symbol),
11761179
)),
11771180
token.immediate(':'),
1178-
field('value', optional($._arg))
1181+
choice(
1182+
field('value', optional($._arg)),
1183+
// This alternative never matches, because '_no_line_break' tokens do not exist.
1184+
// The purpose is give a hint to the scanner that it should not produce any line-break
1185+
// terminators at this point.
1186+
$._no_line_break)
11791187
)
11801188
)),
11811189

src/grammar.json

Lines changed: 86 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6893,24 +6893,33 @@
68936893
}
68946894
},
68956895
"constant_suffix": {
6896-
"type": "TOKEN",
6897-
"content": {
6898-
"type": "SEQ",
6899-
"members": [
6900-
{
6901-
"type": "PATTERN",
6902-
"value": "[A-Z]"
6903-
},
6904-
{
6905-
"type": "PATTERN",
6906-
"value": "[^\\x00-\\x1F\\s:;`\"'@$#.,|^&<=>+\\-*/\\\\%?!~()\\[\\]{}]*"
6907-
},
6908-
{
6909-
"type": "PATTERN",
6910-
"value": "[?!]"
6896+
"type": "CHOICE",
6897+
"members": [
6898+
{
6899+
"type": "TOKEN",
6900+
"content": {
6901+
"type": "SEQ",
6902+
"members": [
6903+
{
6904+
"type": "PATTERN",
6905+
"value": "[A-Z]"
6906+
},
6907+
{
6908+
"type": "PATTERN",
6909+
"value": "[^\\x00-\\x1F\\s:;`\"'@$#.,|^&<=>+\\-*/\\\\%?!~()\\[\\]{}]*"
6910+
},
6911+
{
6912+
"type": "PATTERN",
6913+
"value": "[?]"
6914+
}
6915+
]
69116916
}
6912-
]
6913-
}
6917+
},
6918+
{
6919+
"type": "SYMBOL",
6920+
"name": "_constant_suffix"
6921+
}
6922+
]
69146923
},
69156924
"identifier": {
69166925
"type": "TOKEN",
@@ -6929,24 +6938,33 @@
69296938
}
69306939
},
69316940
"identifier_suffix": {
6932-
"type": "TOKEN",
6933-
"content": {
6934-
"type": "SEQ",
6935-
"members": [
6936-
{
6937-
"type": "PATTERN",
6938-
"value": "[^\\x00-\\x1F\\sA-Z0-9:;`\"'@$#.,|^&<=>+\\-*/\\\\%?!~()\\[\\]{}]"
6939-
},
6940-
{
6941-
"type": "PATTERN",
6942-
"value": "[^\\x00-\\x1F\\s:;`\"'@$#.,|^&<=>+\\-*/\\\\%?!~()\\[\\]{}]*"
6943-
},
6944-
{
6945-
"type": "PATTERN",
6946-
"value": "[?!]"
6941+
"type": "CHOICE",
6942+
"members": [
6943+
{
6944+
"type": "TOKEN",
6945+
"content": {
6946+
"type": "SEQ",
6947+
"members": [
6948+
{
6949+
"type": "PATTERN",
6950+
"value": "[^\\x00-\\x1F\\sA-Z0-9:;`\"'@$#.,|^&<=>+\\-*/\\\\%?!~()\\[\\]{}]"
6951+
},
6952+
{
6953+
"type": "PATTERN",
6954+
"value": "[^\\x00-\\x1F\\s:;`\"'@$#.,|^&<=>+\\-*/\\\\%?!~()\\[\\]{}]*"
6955+
},
6956+
{
6957+
"type": "PATTERN",
6958+
"value": "[?]"
6959+
}
6960+
]
69476961
}
6948-
]
6949-
}
6962+
},
6963+
{
6964+
"type": "SYMBOL",
6965+
"name": "_identifier_suffix"
6966+
}
6967+
]
69506968
},
69516969
"instance_variable": {
69526970
"type": "TOKEN",
@@ -7677,20 +7695,29 @@
76777695
}
76787696
},
76797697
{
7680-
"type": "FIELD",
7681-
"name": "value",
7682-
"content": {
7683-
"type": "CHOICE",
7684-
"members": [
7685-
{
7686-
"type": "SYMBOL",
7687-
"name": "_arg"
7688-
},
7689-
{
7690-
"type": "BLANK"
7698+
"type": "CHOICE",
7699+
"members": [
7700+
{
7701+
"type": "FIELD",
7702+
"name": "value",
7703+
"content": {
7704+
"type": "CHOICE",
7705+
"members": [
7706+
{
7707+
"type": "SYMBOL",
7708+
"name": "_arg"
7709+
},
7710+
{
7711+
"type": "BLANK"
7712+
}
7713+
]
76917714
}
7692-
]
7693-
}
7715+
},
7716+
{
7717+
"type": "SYMBOL",
7718+
"name": "_no_line_break"
7719+
}
7720+
]
76947721
}
76957722
]
76967723
}
@@ -7805,6 +7832,10 @@
78057832
"type": "SYMBOL",
78067833
"name": "_line_break"
78077834
},
7835+
{
7836+
"type": "SYMBOL",
7837+
"name": "_no_line_break"
7838+
},
78087839
{
78097840
"type": "SYMBOL",
78107841
"name": "simple_symbol"
@@ -7893,6 +7924,14 @@
78937924
"type": "SYMBOL",
78947925
"name": "hash_key_symbol"
78957926
},
7927+
{
7928+
"type": "SYMBOL",
7929+
"name": "_identifier_suffix"
7930+
},
7931+
{
7932+
"type": "SYMBOL",
7933+
"name": "_constant_suffix"
7934+
},
78967935
{
78977936
"type": "SYMBOL",
78987937
"name": "_hash_splat_star_star"

src/node-types.json

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,6 +1445,11 @@
14451445
}
14461446
}
14471447
},
1448+
{
1449+
"type": "constant",
1450+
"named": true,
1451+
"fields": {}
1452+
},
14481453
{
14491454
"type": "delimited_symbol",
14501455
"named": true,
@@ -1896,6 +1901,11 @@
18961901
]
18971902
}
18981903
},
1904+
{
1905+
"type": "hash_key_symbol",
1906+
"named": true,
1907+
"fields": {}
1908+
},
18991909
{
19001910
"type": "hash_pattern",
19011911
"named": true,
@@ -1993,6 +2003,11 @@
19932003
]
19942004
}
19952005
},
2006+
{
2007+
"type": "identifier",
2008+
"named": true,
2009+
"fields": {}
2010+
},
19962011
{
19972012
"type": "if",
19982013
"named": true,
@@ -3837,10 +3852,6 @@
38373852
"type": "comment",
38383853
"named": true
38393854
},
3840-
{
3841-
"type": "constant",
3842-
"named": true
3843-
},
38443855
{
38453856
"type": "def",
38463857
"named": false
@@ -3897,10 +3908,6 @@
38973908
"type": "global_variable",
38983909
"named": true
38993910
},
3900-
{
3901-
"type": "hash_key_symbol",
3902-
"named": true
3903-
},
39043911
{
39053912
"type": "heredoc_beginning",
39063913
"named": true
@@ -3917,10 +3924,6 @@
39173924
"type": "i",
39183925
"named": false
39193926
},
3920-
{
3921-
"type": "identifier",
3922-
"named": true
3923-
},
39243927
{
39253928
"type": "if",
39263929
"named": false

0 commit comments

Comments
 (0)