Skip to content

Commit

Permalink
Merge pull request #3 from amaanq/regex-i
Browse files Browse the repository at this point in the history
use regex i-flag
  • Loading branch information
MichaHoffmann authored Aug 13, 2023
2 parents ed9a12f + 420e3ad commit 77625d7
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 85 deletions.
30 changes: 9 additions & 21 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module.exports = grammar({

extras: ($) => [$.comment, /\s/],
conflicts: ($) => [
[$.metric_name, $.function_name],
[$.instant_vector_selector, $.range_vector_selector],
],

Expand Down Expand Up @@ -75,7 +74,7 @@ module.exports = grammar({

grouping: ($) =>
seq(
choice(caseInsensitive("by"), caseInsensitive("without")),
choice(/by/i, /without/i),
"(",
commaSep($.label_name),
")",
Expand All @@ -93,18 +92,18 @@ module.exports = grammar({
3,
seq(
choice("==", "!=", ">", ">=", "<", "<="),
optional(caseInsensitive("bool")),
optional(/bool/i),
),
],
[
2,
choice(
caseInsensitive("and"),
caseInsensitive("or"),
caseInsensitive("unless"),
/and/i,
/or/i,
/unless/i,
),
],
[1, choice(caseInsensitive("atan2"))],
[1, /atan2/i],
];

return choice(
Expand All @@ -120,13 +119,13 @@ module.exports = grammar({
binary_grouping: ($) =>
prec.right(
seq(
choice(caseInsensitive("on"), caseInsensitive("ignoring")),
choice(/on/i, /ignoring/i),
seq("(", commaSep($.label_name), ")"),
optional(
seq(
choice(
caseInsensitive("group_left"),
caseInsensitive("group_right"),
/group_left/i,
/group_right/i,
),
optional(seq("(", commaSep($.label_name), ")")),
),
Expand Down Expand Up @@ -169,14 +168,3 @@ function commaSep(rule) {
function commaSep1(rule) {
return seq(rule, repeat(seq(",", rule)), optional(","));
}

// taken from https://github.com/stadelmanma/tree-sitter-fortran/blob/a9c79b20a84075467d705ebe714c90f275dd5835/grammar.js#L1125C1-L1133C2
function caseInsensitive(keyword) {
let result = new RegExp(
keyword
.split("")
.map((l) => (l !== l.toUpperCase() ? `[${l}${l.toUpperCase()}]` : l))
.join(""),
);
return result;
}
71 changes: 41 additions & 30 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@
},
"float_literal": {
"type": "PATTERN",
"value": "[-+]?([0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?|0[xX][0-9a-fA-F]+|[nN][aA][nN]|[iI][nN][fF])"
"value": "[-+]?([0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?|0[xX][0-9a-fA-F]+|[nN][aA][nN]|[iI][nN][fF])",
"flags": ""
},
"string_literal": {
"type": "SYMBOL",
Expand Down Expand Up @@ -245,7 +246,8 @@
},
{
"type": "PATTERN",
"value": "[1-9][0-9]*"
"value": "[1-9][0-9]*",
"flags": ""
}
]
}
Expand Down Expand Up @@ -507,11 +509,13 @@
"members": [
{
"type": "PATTERN",
"value": "[bB][yY]"
"value": "by",
"flags": "i"
},
{
"type": "PATTERN",
"value": "[wW][iI][tT][hH][oO][uU][tT]"
"value": "without",
"flags": "i"
}
]
},
Expand Down Expand Up @@ -750,7 +754,8 @@
"members": [
{
"type": "PATTERN",
"value": "[bB][oO][oO][lL]"
"value": "bool",
"flags": "i"
},
{
"type": "BLANK"
Expand Down Expand Up @@ -793,15 +798,18 @@
"members": [
{
"type": "PATTERN",
"value": "[aA][nN][dD]"
"value": "and",
"flags": "i"
},
{
"type": "PATTERN",
"value": "[oO][rR]"
"value": "or",
"flags": "i"
},
{
"type": "PATTERN",
"value": "[uU][nN][lL][eE][sS][sS]"
"value": "unless",
"flags": "i"
}
]
},
Expand Down Expand Up @@ -835,13 +843,9 @@
"name": "_query"
},
{
"type": "CHOICE",
"members": [
{
"type": "PATTERN",
"value": "[aA][tT][aA][nN]2"
}
]
"type": "PATTERN",
"value": "atan2",
"flags": "i"
},
{
"type": "CHOICE",
Expand Down Expand Up @@ -875,11 +879,13 @@
"members": [
{
"type": "PATTERN",
"value": "[oO][nN]"
"value": "on",
"flags": "i"
},
{
"type": "PATTERN",
"value": "[iI][gG][nN][oO][rR][iI][nN][gG]"
"value": "ignoring",
"flags": "i"
}
]
},
Expand Down Expand Up @@ -952,11 +958,13 @@
"members": [
{
"type": "PATTERN",
"value": "[gG][rR][oO][uU][pP]_[lL][eE][fF][tT]"
"value": "group_left",
"flags": "i"
},
{
"type": "PATTERN",
"value": "[gG][rR][oO][uU][pP]_[rR][iI][gG][hH][tT]"
"value": "group_right",
"flags": "i"
}
]
},
Expand Down Expand Up @@ -1126,7 +1134,8 @@
},
{
"type": "PATTERN",
"value": "(\\\\\"|[^\"])*"
"value": "(\\\\\"|[^\"])*",
"flags": ""
},
{
"type": "STRING",
Expand All @@ -1146,7 +1155,8 @@
},
{
"type": "PATTERN",
"value": "(\\\\'|[^'])*"
"value": "(\\\\'|[^'])*",
"flags": ""
},
{
"type": "STRING",
Expand All @@ -1166,7 +1176,8 @@
},
{
"type": "PATTERN",
"value": "(\\\\`|[^`])*"
"value": "(\\\\`|[^`])*",
"flags": ""
},
{
"type": "STRING",
Expand All @@ -1182,7 +1193,8 @@
"members": [
{
"type": "PATTERN",
"value": "[0-9]+"
"value": "[0-9]+",
"flags": ""
},
{
"type": "CHOICE",
Expand Down Expand Up @@ -1222,7 +1234,8 @@
},
"_identifier": {
"type": "PATTERN",
"value": "[a-zA-Z_:][a-zA-Z0-9_:]*"
"value": "[a-zA-Z_:][a-zA-Z0-9_:]*",
"flags": ""
},
"comment": {
"type": "TOKEN",
Expand All @@ -1238,7 +1251,8 @@
},
{
"type": "PATTERN",
"value": ".*"
"value": ".*",
"flags": ""
}
]
}
Expand All @@ -1253,14 +1267,11 @@
},
{
"type": "PATTERN",
"value": "\\s"
"value": "\\s",
"flags": ""
}
],
"conflicts": [
[
"metric_name",
"function_name"
],
[
"instant_vector_selector",
"range_vector_selector"
Expand Down
37 changes: 9 additions & 28 deletions src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,6 @@ static const TSStateId ts_primary_state_ids[STATE_COUNT] = {

static bool ts_lex(TSLexer *lexer, TSStateId state) {
START_LEXER();
eof = lexer->eof(lexer);
switch (state) {
case 0:
if (eof) ADVANCE(85);
Expand Down Expand Up @@ -854,9 +853,7 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) {
lookahead == 'i') ADVANCE(45);
if (lookahead == 'U' ||
lookahead == 'u') ADVANCE(57);
if (lookahead == '\t' ||
lookahead == '\n' ||
lookahead == '\r' ||
if (('\t' <= lookahead && lookahead <= '\r') ||
lookahead == ' ') SKIP(0)
if (('1' <= lookahead && lookahead <= '9')) ADVANCE(102);
END_STATE();
Expand All @@ -878,9 +875,7 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) {
lookahead == 'i') ADVANCE(167);
if (lookahead == 'N' ||
lookahead == 'n') ADVANCE(155);
if (lookahead == '\t' ||
lookahead == '\n' ||
lookahead == '\r' ||
if (('\t' <= lookahead && lookahead <= '\r') ||
lookahead == ' ') SKIP(1)
if (('1' <= lookahead && lookahead <= '9')) ADVANCE(90);
if (lookahead == ':' ||
Expand All @@ -906,9 +901,7 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) {
lookahead == 'n') ADVANCE(155);
if (lookahead == 'O' ||
lookahead == 'o') ADVANCE(169);
if (lookahead == '\t' ||
lookahead == '\n' ||
lookahead == '\r' ||
if (('\t' <= lookahead && lookahead <= '\r') ||
lookahead == ' ') SKIP(2)
if (('1' <= lookahead && lookahead <= '9')) ADVANCE(90);
if (lookahead == ':' ||
Expand All @@ -932,9 +925,7 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) {
lookahead == 'i') ADVANCE(167);
if (lookahead == 'N' ||
lookahead == 'n') ADVANCE(155);
if (lookahead == '\t' ||
lookahead == '\n' ||
lookahead == '\r' ||
if (('\t' <= lookahead && lookahead <= '\r') ||
lookahead == ' ') SKIP(3)
if (('1' <= lookahead && lookahead <= '9')) ADVANCE(90);
if (lookahead == ':' ||
Expand All @@ -958,9 +949,7 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) {
lookahead == 'n') ADVANCE(155);
if (lookahead == 'O' ||
lookahead == 'o') ADVANCE(169);
if (lookahead == '\t' ||
lookahead == '\n' ||
lookahead == '\r' ||
if (('\t' <= lookahead && lookahead <= '\r') ||
lookahead == ' ') SKIP(4)
if (('1' <= lookahead && lookahead <= '9')) ADVANCE(90);
if (lookahead == ':' ||
Expand All @@ -981,9 +970,7 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) {
if (lookahead == '#') ADVANCE(183);
if (lookahead == ')') ADVANCE(87);
if (lookahead == '}') ADVANCE(105);
if (lookahead == '\t' ||
lookahead == '\n' ||
lookahead == '\r' ||
if (('\t' <= lookahead && lookahead <= '\r') ||
lookahead == ' ') SKIP(7)
if (lookahead == ':' ||
('A' <= lookahead && lookahead <= 'Z') ||
Expand All @@ -998,18 +985,14 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) {
if (lookahead == 's') ADVANCE(146);
if (lookahead == 'w') ADVANCE(151);
if (lookahead == 'y') ADVANCE(153);
if (lookahead == '\t' ||
lookahead == '\n' ||
lookahead == '\r' ||
if (('\t' <= lookahead && lookahead <= '\r') ||
lookahead == ' ') SKIP(8)
END_STATE();
case 9:
if (lookahead == '#') ADVANCE(183);
if (lookahead == 'e') ADVANCE(30);
if (lookahead == 's') ADVANCE(33);
if (lookahead == '\t' ||
lookahead == '\n' ||
lookahead == '\r' ||
if (('\t' <= lookahead && lookahead <= '\r') ||
lookahead == ' ') SKIP(9)
if (('1' <= lookahead && lookahead <= '9')) ADVANCE(102);
END_STATE();
Expand Down Expand Up @@ -1343,9 +1326,7 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) {
lookahead == 'u') ADVANCE(57);
if (lookahead == 'W' ||
lookahead == 'w') ADVANCE(49);
if (lookahead == '\t' ||
lookahead == '\n' ||
lookahead == '\r' ||
if (('\t' <= lookahead && lookahead <= '\r') ||
lookahead == ' ') SKIP(84)
if (('0' <= lookahead && lookahead <= '9')) ADVANCE(144);
END_STATE();
Expand Down
Loading

0 comments on commit 77625d7

Please sign in to comment.