Skip to content

Commit

Permalink
Add support for regexp character classes
Browse files Browse the repository at this point in the history
Closes #21
  • Loading branch information
Beaglefoot committed Jan 14, 2024
1 parent 4b4b46c commit dcf4ac4
Show file tree
Hide file tree
Showing 6 changed files with 55,258 additions and 54,698 deletions.
3 changes: 2 additions & 1 deletion grammar.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ declare interface IRules {
regex: ($: IRules & IExternals) => any;
_regex_char: ($: IRules & IExternals) => any;
_regex_char_escaped: ($: IRules & IExternals) => any;
_regex_char_list: ($: IRules & IExternals) => any;
_regex_char_class: ($: IRules & IExternals) => any;
_regex_bracket_exp: ($: IRules & IExternals) => any;
regex_pattern: ($: IRules & IExternals) => any;
regex_flags: ($: IRules & IExternals) => any;
regex_constant: ($: IRules & IExternals) => any;
Expand Down
28 changes: 25 additions & 3 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,15 +375,37 @@ module.exports = grammar({

_regex_char_escaped: $ => token.immediate(seq('\\', /./)),

_regex_char_list: $ =>
_regex_char_class: $ =>
seq(
token.immediate('['),
repeat1(choice($._regex_char_escaped, $._regex_char)),
token.immediate(':'),
choice(
'alnum',
'alpha',
'blank',
'cntrl',
'digit',
'graph',
'lower',
'print',
'punct',
'space',
'upper',
'xdigit'
),
token.immediate(':'),
token.immediate(']')
),

_regex_bracket_exp: $ =>
seq(
token.immediate('['),
repeat1(choice($._regex_char_escaped, $._regex_char, $._regex_char_class)),
token.immediate(']')
),

regex_pattern: $ => {
return repeat1(choice($._regex_char, $._regex_char_escaped, $._regex_char_list));
return repeat1(choice($._regex_char, $._regex_char_escaped, $._regex_bracket_exp));
},

regex_flags: $ => token.immediate(/[a-z]+/),
Expand Down
94 changes: 92 additions & 2 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -2583,7 +2583,93 @@
]
}
},
"_regex_char_list": {
"_regex_char_class": {
"type": "SEQ",
"members": [
{
"type": "IMMEDIATE_TOKEN",
"content": {
"type": "STRING",
"value": "["
}
},
{
"type": "IMMEDIATE_TOKEN",
"content": {
"type": "STRING",
"value": ":"
}
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "alnum"
},
{
"type": "STRING",
"value": "alpha"
},
{
"type": "STRING",
"value": "blank"
},
{
"type": "STRING",
"value": "cntrl"
},
{
"type": "STRING",
"value": "digit"
},
{
"type": "STRING",
"value": "graph"
},
{
"type": "STRING",
"value": "lower"
},
{
"type": "STRING",
"value": "print"
},
{
"type": "STRING",
"value": "punct"
},
{
"type": "STRING",
"value": "space"
},
{
"type": "STRING",
"value": "upper"
},
{
"type": "STRING",
"value": "xdigit"
}
]
},
{
"type": "IMMEDIATE_TOKEN",
"content": {
"type": "STRING",
"value": ":"
}
},
{
"type": "IMMEDIATE_TOKEN",
"content": {
"type": "STRING",
"value": "]"
}
}
]
},
"_regex_bracket_exp": {
"type": "SEQ",
"members": [
{
Expand All @@ -2605,6 +2691,10 @@
{
"type": "SYMBOL",
"name": "_regex_char"
},
{
"type": "SYMBOL",
"name": "_regex_char_class"
}
]
}
Expand Down Expand Up @@ -2633,7 +2723,7 @@
},
{
"type": "SYMBOL",
"name": "_regex_char_list"
"name": "_regex_bracket_exp"
}
]
}
Expand Down
44 changes: 44 additions & 0 deletions src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -5165,6 +5165,18 @@
"type": "^=",
"named": false
},
{
"type": "alnum",
"named": false
},
{
"type": "alpha",
"named": false
},
{
"type": "blank",
"named": false
},
{
"type": "break_statement",
"named": true
Expand All @@ -5173,6 +5185,10 @@
"type": "case",
"named": false
},
{
"type": "cntrl",
"named": false
},
{
"type": "concatenating_space",
"named": true
Expand All @@ -5189,6 +5205,10 @@
"type": "delete",
"named": false
},
{
"type": "digit",
"named": false
},
{
"type": "do",
"named": false
Expand Down Expand Up @@ -5221,6 +5241,10 @@
"type": "getline",
"named": false
},
{
"type": "graph",
"named": false
},
{
"type": "identifier",
"named": true
Expand All @@ -5233,6 +5257,10 @@
"type": "in",
"named": false
},
{
"type": "lower",
"named": false
},
{
"type": "namespace",
"named": false
Expand All @@ -5253,6 +5281,10 @@
"type": "printf",
"named": false
},
{
"type": "punct",
"named": false
},
{
"type": "regex_flags",
"named": true
Expand All @@ -5261,14 +5293,26 @@
"type": "return",
"named": false
},
{
"type": "space",
"named": false
},
{
"type": "switch",
"named": false
},
{
"type": "upper",
"named": false
},
{
"type": "while",
"named": false
},
{
"type": "xdigit",
"named": false
},
{
"type": "{",
"named": false
Expand Down
Loading

0 comments on commit dcf4ac4

Please sign in to comment.