Skip to content

Commit

Permalink
number fixes
Browse files Browse the repository at this point in the history
- number is now a supertype of float and decimal
- decimals can now include a leading negative sign
  • Loading branch information
bollian committed Feb 1, 2024
1 parent 453786d commit ce89936
Show file tree
Hide file tree
Showing 8 changed files with 860 additions and 1,006 deletions.
3 changes: 2 additions & 1 deletion grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ module.exports = grammar({
supertypes: $ => [
$.literal,
$.expression,
$.number,
],

word: $ => $.identifier,
Expand Down Expand Up @@ -378,7 +379,7 @@ module.exports = grammar({

string: _ => token(seq('"', repeat(choice(/[^"]/, '\\"')), '"')),
number: $ => choice($.decimal, $.float),
decimal: _ => token(/\d+/),
decimal: _ => token(/-?\d+/),
float: _ => token(/-?(\d+(\.\d+)?|\.\d+)(e-?\d+)?/),
boolean: _ => choice('true', 'false'),
undef: _ => 'undef',
Expand Down
5 changes: 3 additions & 2 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -2038,7 +2038,7 @@
"type": "TOKEN",
"content": {
"type": "PATTERN",
"value": "\\d+"
"value": "-?\\d+"
}
},
"float": {
Expand Down Expand Up @@ -2120,7 +2120,8 @@
"inline": [],
"supertypes": [
"literal",
"expression"
"expression",
"number"
]
}

33 changes: 14 additions & 19 deletions src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@
}
]
},
{
"type": "number",
"named": true,
"subtypes": [
{
"type": "decimal",
"named": true
},
{
"type": "float",
"named": true
}
]
},
{
"type": "arguments",
"named": true,
Expand Down Expand Up @@ -1234,25 +1248,6 @@
}
}
},
{
"type": "number",
"named": true,
"fields": {},
"children": {
"multiple": false,
"required": true,
"types": [
{
"type": "decimal",
"named": true
},
{
"type": "float",
"named": true
}
]
}
},
{
"type": "parameter",
"named": true,
Expand Down
37 changes: 16 additions & 21 deletions src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -747,8 +747,9 @@ static const TSSymbolMetadata ts_symbol_metadata[] = {
.named = true,
},
[sym_number] = {
.visible = true,
.visible = false,
.named = true,
.supertype = true,
},
[sym_boolean] = {
.visible = true,
Expand Down Expand Up @@ -1360,11 +1361,11 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) {
END_STATE();
case 5:
if (lookahead == '*') ADVANCE(7);
if (lookahead == '/') ADVANCE(56);
if (lookahead == '/') ADVANCE(55);
END_STATE();
case 6:
if (lookahead == '*') ADVANCE(6);
if (lookahead == '/') ADVANCE(55);
if (lookahead == '/') ADVANCE(54);
if (lookahead != 0) ADVANCE(7);
END_STATE();
case 7:
Expand All @@ -1373,7 +1374,7 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) {
END_STATE();
case 8:
if (lookahead == '-') ADVANCE(13);
if (('0' <= lookahead && lookahead <= '9')) ADVANCE(54);
if (('0' <= lookahead && lookahead <= '9')) ADVANCE(53);
END_STATE();
case 9:
if (lookahead == '/') ADVANCE(5);
Expand All @@ -1391,15 +1392,15 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) {
if (lookahead == '|') ADVANCE(36);
END_STATE();
case 12:
if (('0' <= lookahead && lookahead <= '9')) ADVANCE(53);
if (('0' <= lookahead && lookahead <= '9')) ADVANCE(52);
END_STATE();
case 13:
if (('0' <= lookahead && lookahead <= '9')) ADVANCE(54);
if (('0' <= lookahead && lookahead <= '9')) ADVANCE(53);
END_STATE();
case 14:
if (lookahead != 0 &&
lookahead != '\r') ADVANCE(56);
if (lookahead == '\r') ADVANCE(57);
lookahead != '\r') ADVANCE(55);
if (lookahead == '\r') ADVANCE(56);
END_STATE();
case 15:
ACCEPT_TOKEN(ts_builtin_sym_end);
Expand Down Expand Up @@ -1456,15 +1457,15 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) {
END_STATE();
case 32:
ACCEPT_TOKEN(anon_sym_DOT);
if (('0' <= lookahead && lookahead <= '9')) ADVANCE(53);
if (('0' <= lookahead && lookahead <= '9')) ADVANCE(52);
END_STATE();
case 33:
ACCEPT_TOKEN(anon_sym_DASH);
END_STATE();
case 34:
ACCEPT_TOKEN(anon_sym_DASH);
if (lookahead == '.') ADVANCE(12);
if (('0' <= lookahead && lookahead <= '9')) ADVANCE(52);
if (('0' <= lookahead && lookahead <= '9')) ADVANCE(51);
END_STATE();
case 35:
ACCEPT_TOKEN(anon_sym_PLUS);
Expand Down Expand Up @@ -1498,7 +1499,7 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) {
case 44:
ACCEPT_TOKEN(anon_sym_SLASH);
if (lookahead == '*') ADVANCE(7);
if (lookahead == '/') ADVANCE(56);
if (lookahead == '/') ADVANCE(55);
END_STATE();
case 45:
ACCEPT_TOKEN(anon_sym_CARET);
Expand Down Expand Up @@ -1533,32 +1534,26 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) {
END_STATE();
case 52:
ACCEPT_TOKEN(sym_float);
if (lookahead == '.') ADVANCE(12);
if (lookahead == 'e') ADVANCE(8);
if (('0' <= lookahead && lookahead <= '9')) ADVANCE(52);
END_STATE();
case 53:
ACCEPT_TOKEN(sym_float);
if (lookahead == 'e') ADVANCE(8);
if (('0' <= lookahead && lookahead <= '9')) ADVANCE(53);
END_STATE();
case 54:
ACCEPT_TOKEN(sym_float);
if (('0' <= lookahead && lookahead <= '9')) ADVANCE(54);
END_STATE();
case 55:
ACCEPT_TOKEN(sym_comment);
END_STATE();
case 56:
case 55:
ACCEPT_TOKEN(sym_comment);
if (lookahead == '\\') ADVANCE(14);
if (lookahead != 0 &&
lookahead != '\n') ADVANCE(56);
lookahead != '\n') ADVANCE(55);
END_STATE();
case 57:
case 56:
ACCEPT_TOKEN(sym_comment);
if (lookahead != 0 &&
lookahead != '\\') ADVANCE(56);
lookahead != '\\') ADVANCE(55);
if (lookahead == '\\') ADVANCE(14);
END_STATE();
default:
Expand Down
Loading

0 comments on commit ce89936

Please sign in to comment.