Allow percentage as a unit

This commit is contained in:
Max Brunsfeld 2018-10-26 09:37:10 -07:00
parent eab22469f9
commit 3e106ca94e
4 changed files with 129 additions and 95 deletions

View File

@ -38,6 +38,22 @@ a {
(declaration (property_name) (color_value)) (declaration (property_name) (color_value))
(declaration (property_name) (color_value))))) (declaration (property_name) (color_value)))))
============================
Numbers
============================
a {
b: 0.5%;
c: 5em;
}
---
(stylesheet
(rule_set (selectors (tag_name)) (block
(declaration (property_name) (float_value (unit)))
(declaration (property_name) (integer_value (unit))))))
============================ ============================
Strings Strings
============================ ============================

View File

@ -216,7 +216,7 @@ module.exports = grammar({
optional($.unit) optional($.unit)
), ),
unit: $ => token.immediate(/[a-z]+/), unit: $ => token.immediate(/[a-zA-Z%]+/),
call_expression: $ => seq( call_expression: $ => seq(
alias($.identifier, $.function_name), alias($.identifier, $.function_name),

2
src/grammar.json vendored
View File

@ -1018,7 +1018,7 @@
"type": "IMMEDIATE_TOKEN", "type": "IMMEDIATE_TOKEN",
"content": { "content": {
"type": "PATTERN", "type": "PATTERN",
"value": "[a-z]+" "value": "[a-zA-Z%]+"
} }
}, },
"call_expression": { "call_expression": {

204
src/parser.c vendored
View File

@ -1455,6 +1455,8 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) {
SKIP(101); SKIP(101);
END_STATE(); END_STATE();
case 102: case 102:
if (lookahead == '%')
ADVANCE(103);
if (lookahead == '(') if (lookahead == '(')
ADVANCE(75); ADVANCE(75);
if (lookahead == '/') if (lookahead == '/')
@ -1462,64 +1464,79 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) {
if (lookahead == ';') if (lookahead == ';')
ADVANCE(33); ADVANCE(33);
if (lookahead == 'n') if (lookahead == 'n')
ADVANCE(103); ADVANCE(104);
if (lookahead == '-' ||
lookahead == '_')
ADVANCE(79);
if (lookahead == '\t' || if (lookahead == '\t' ||
lookahead == '\n' || lookahead == '\n' ||
lookahead == '\r' || lookahead == '\r' ||
lookahead == ' ') lookahead == ' ')
SKIP(107); SKIP(108);
if (('a' <= lookahead && lookahead <= 'z')) if (('A' <= lookahead && lookahead <= 'Z') ||
ADVANCE(106); ('a' <= lookahead && lookahead <= 'z'))
if (lookahead == '-' || ADVANCE(107);
('A' <= lookahead && lookahead <= 'Z') ||
lookahead == '_')
ADVANCE(79);
END_STATE(); END_STATE();
case 103: case 103:
ACCEPT_TOKEN(sym_unit); ACCEPT_TOKEN(sym_unit);
if (lookahead == 'o') if (lookahead == '%' ||
ADVANCE(104);
if (('a' <= lookahead && lookahead <= 'z'))
ADVANCE(106);
if (lookahead == '-' ||
('0' <= lookahead && lookahead <= '9') ||
('A' <= lookahead && lookahead <= 'Z') || ('A' <= lookahead && lookahead <= 'Z') ||
lookahead == '_') ('a' <= lookahead && lookahead <= 'z'))
ADVANCE(79); ADVANCE(103);
END_STATE(); END_STATE();
case 104: case 104:
ACCEPT_TOKEN(sym_unit); ACCEPT_TOKEN(sym_unit);
if (lookahead == 't') if (lookahead == '%')
ADVANCE(103);
if (lookahead == 'o')
ADVANCE(105); ADVANCE(105);
if (('a' <= lookahead && lookahead <= 'z'))
ADVANCE(106);
if (lookahead == '-' || if (lookahead == '-' ||
('0' <= lookahead && lookahead <= '9') || ('0' <= lookahead && lookahead <= '9') ||
('A' <= lookahead && lookahead <= 'Z') ||
lookahead == '_') lookahead == '_')
ADVANCE(79); ADVANCE(79);
if (('A' <= lookahead && lookahead <= 'Z') ||
('a' <= lookahead && lookahead <= 'z'))
ADVANCE(107);
END_STATE(); END_STATE();
case 105: case 105:
ACCEPT_TOKEN(anon_sym_not); ACCEPT_TOKEN(sym_unit);
if (('a' <= lookahead && lookahead <= 'z')) if (lookahead == '%')
ADVANCE(103);
if (lookahead == 't')
ADVANCE(106); ADVANCE(106);
if (lookahead == '-' || if (lookahead == '-' ||
('0' <= lookahead && lookahead <= '9') || ('0' <= lookahead && lookahead <= '9') ||
('A' <= lookahead && lookahead <= 'Z') ||
lookahead == '_') lookahead == '_')
ADVANCE(79); ADVANCE(79);
if (('A' <= lookahead && lookahead <= 'Z') ||
('a' <= lookahead && lookahead <= 'z'))
ADVANCE(107);
END_STATE(); END_STATE();
case 106: case 106:
ACCEPT_TOKEN(sym_unit); ACCEPT_TOKEN(anon_sym_not);
if (('a' <= lookahead && lookahead <= 'z')) if (lookahead == '%')
ADVANCE(106); ADVANCE(103);
if (lookahead == '-' || if (lookahead == '-' ||
('0' <= lookahead && lookahead <= '9') || ('0' <= lookahead && lookahead <= '9') ||
('A' <= lookahead && lookahead <= 'Z') ||
lookahead == '_') lookahead == '_')
ADVANCE(79); ADVANCE(79);
if (('A' <= lookahead && lookahead <= 'Z') ||
('a' <= lookahead && lookahead <= 'z'))
ADVANCE(107);
END_STATE(); END_STATE();
case 107: case 107:
ACCEPT_TOKEN(sym_unit);
if (lookahead == '%')
ADVANCE(103);
if (lookahead == '-' ||
('0' <= lookahead && lookahead <= '9') ||
lookahead == '_')
ADVANCE(79);
if (('A' <= lookahead && lookahead <= 'Z') ||
('a' <= lookahead && lookahead <= 'z'))
ADVANCE(107);
END_STATE();
case 108:
if (lookahead == '(') if (lookahead == '(')
ADVANCE(75); ADVANCE(75);
if (lookahead == '/') if (lookahead == '/')
@ -1532,14 +1549,14 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) {
lookahead == '\n' || lookahead == '\n' ||
lookahead == '\r' || lookahead == '\r' ||
lookahead == ' ') lookahead == ' ')
SKIP(107); SKIP(108);
if (lookahead == '-' || if (lookahead == '-' ||
('A' <= lookahead && lookahead <= 'Z') || ('A' <= lookahead && lookahead <= 'Z') ||
lookahead == '_' || lookahead == '_' ||
('a' <= lookahead && lookahead <= 'z')) ('a' <= lookahead && lookahead <= 'z'))
ADVANCE(79); ADVANCE(79);
END_STATE(); END_STATE();
case 108: case 109:
if (lookahead == '(') if (lookahead == '(')
ADVANCE(13); ADVANCE(13);
if (lookahead == ')') if (lookahead == ')')
@ -1558,14 +1575,14 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) {
lookahead == '\n' || lookahead == '\n' ||
lookahead == '\r' || lookahead == '\r' ||
lookahead == ' ') lookahead == ' ')
SKIP(109); SKIP(110);
if (lookahead == '-' || if (lookahead == '-' ||
('A' <= lookahead && lookahead <= 'Z') || ('A' <= lookahead && lookahead <= 'Z') ||
lookahead == '_' || lookahead == '_' ||
('a' <= lookahead && lookahead <= 'z')) ('a' <= lookahead && lookahead <= 'z'))
ADVANCE(79); ADVANCE(79);
END_STATE(); END_STATE();
case 109: case 110:
if (lookahead == '(') if (lookahead == '(')
ADVANCE(75); ADVANCE(75);
if (lookahead == ')') if (lookahead == ')')
@ -1584,35 +1601,16 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) {
lookahead == '\n' || lookahead == '\n' ||
lookahead == '\r' || lookahead == '\r' ||
lookahead == ' ') lookahead == ' ')
SKIP(109); SKIP(110);
if (lookahead == '-' || if (lookahead == '-' ||
('A' <= lookahead && lookahead <= 'Z') || ('A' <= lookahead && lookahead <= 'Z') ||
lookahead == '_' || lookahead == '_' ||
('a' <= lookahead && lookahead <= 'z')) ('a' <= lookahead && lookahead <= 'z'))
ADVANCE(79); ADVANCE(79);
END_STATE(); END_STATE();
case 110: case 111:
if (lookahead == ')') if (lookahead == ')')
ADVANCE(14); ADVANCE(14);
if (lookahead == ',')
ADVANCE(24);
if (lookahead == '/')
ADVANCE(26);
if (lookahead == ';')
ADVANCE(33);
if (lookahead == 'a')
ADVANCE(60);
if (lookahead == 'o')
ADVANCE(66);
if (lookahead == '{')
ADVANCE(68);
if (lookahead == '\t' ||
lookahead == '\n' ||
lookahead == '\r' ||
lookahead == ' ')
SKIP(110);
END_STATE();
case 111:
if (lookahead == ',') if (lookahead == ',')
ADVANCE(24); ADVANCE(24);
if (lookahead == '/') if (lookahead == '/')
@ -1632,23 +1630,23 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) {
SKIP(111); SKIP(111);
END_STATE(); END_STATE();
case 112: case 112:
if (lookahead == ')')
ADVANCE(14);
if (lookahead == ',') if (lookahead == ',')
ADVANCE(24); ADVANCE(24);
if (lookahead == '/') if (lookahead == '/')
ADVANCE(26); ADVANCE(26);
if (lookahead == ';') if (lookahead == ';')
ADVANCE(33); ADVANCE(33);
if (lookahead == ']') if (lookahead == 'a')
ADVANCE(57); ADVANCE(60);
if (lookahead == 'o')
ADVANCE(66);
if (lookahead == '{')
ADVANCE(68);
if (lookahead == '\t' || if (lookahead == '\t' ||
lookahead == '\n' || lookahead == '\n' ||
lookahead == '\r' || lookahead == '\r' ||
lookahead == ' ') lookahead == ' ')
SKIP(113); SKIP(112);
if (('a' <= lookahead && lookahead <= 'z'))
ADVANCE(114);
END_STATE(); END_STATE();
case 113: case 113:
if (lookahead == ')') if (lookahead == ')')
@ -1665,12 +1663,28 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) {
lookahead == '\n' || lookahead == '\n' ||
lookahead == '\r' || lookahead == '\r' ||
lookahead == ' ') lookahead == ' ')
SKIP(113); SKIP(114);
if (lookahead == '%' ||
('A' <= lookahead && lookahead <= 'Z') ||
('a' <= lookahead && lookahead <= 'z'))
ADVANCE(103);
END_STATE(); END_STATE();
case 114: case 114:
ACCEPT_TOKEN(sym_unit); if (lookahead == ')')
if (('a' <= lookahead && lookahead <= 'z')) ADVANCE(14);
ADVANCE(114); if (lookahead == ',')
ADVANCE(24);
if (lookahead == '/')
ADVANCE(26);
if (lookahead == ';')
ADVANCE(33);
if (lookahead == ']')
ADVANCE(57);
if (lookahead == '\t' ||
lookahead == '\n' ||
lookahead == '\r' ||
lookahead == ' ')
SKIP(114);
END_STATE(); END_STATE();
case 115: case 115:
if (lookahead == '#') if (lookahead == '#')
@ -1818,6 +1832,8 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) {
ADVANCE(2); ADVANCE(2);
if (lookahead == '#') if (lookahead == '#')
ADVANCE(81); ADVANCE(81);
if (lookahead == '%')
ADVANCE(103);
if (lookahead == '\'') if (lookahead == '\'')
ADVANCE(10); ADVANCE(10);
if (lookahead == '+') if (lookahead == '+')
@ -1832,6 +1848,8 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) {
ADVANCE(26); ADVANCE(26);
if (lookahead == ';') if (lookahead == ';')
ADVANCE(33); ADVANCE(33);
if (lookahead == '_')
ADVANCE(79);
if (lookahead == 'e') if (lookahead == 'e')
ADVANCE(123); ADVANCE(123);
if (lookahead == '\t' || if (lookahead == '\t' ||
@ -1841,23 +1859,23 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) {
SKIP(95); SKIP(95);
if (('0' <= lookahead && lookahead <= '9')) if (('0' <= lookahead && lookahead <= '9'))
ADVANCE(23); ADVANCE(23);
if (('a' <= lookahead && lookahead <= 'z'))
ADVANCE(106);
if (('A' <= lookahead && lookahead <= 'Z') || if (('A' <= lookahead && lookahead <= 'Z') ||
lookahead == '_') ('a' <= lookahead && lookahead <= 'z'))
ADVANCE(79); ADVANCE(107);
END_STATE(); END_STATE();
case 123: case 123:
ACCEPT_TOKEN(sym_unit); ACCEPT_TOKEN(sym_unit);
if (lookahead == '%')
ADVANCE(103);
if (lookahead == '-') if (lookahead == '-')
ADVANCE(92); ADVANCE(92);
if (lookahead == '_')
ADVANCE(79);
if (('0' <= lookahead && lookahead <= '9')) if (('0' <= lookahead && lookahead <= '9'))
ADVANCE(93); ADVANCE(93);
if (('a' <= lookahead && lookahead <= 'z'))
ADVANCE(106);
if (('A' <= lookahead && lookahead <= 'Z') || if (('A' <= lookahead && lookahead <= 'Z') ||
lookahead == '_') ('a' <= lookahead && lookahead <= 'z'))
ADVANCE(79); ADVANCE(107);
END_STATE(); END_STATE();
default: default:
return false; return false;
@ -1883,14 +1901,14 @@ static TSLexMode ts_lex_modes[STATE_COUNT] = {
[15] = {.lex_state = 76}, [15] = {.lex_state = 76},
[16] = {.lex_state = 102}, [16] = {.lex_state = 102},
[17] = {.lex_state = 102}, [17] = {.lex_state = 102},
[18] = {.lex_state = 108}, [18] = {.lex_state = 109},
[19] = {.lex_state = 96}, [19] = {.lex_state = 96},
[20] = {.lex_state = 96}, [20] = {.lex_state = 96},
[21] = {.lex_state = 96}, [21] = {.lex_state = 96},
[22] = {.lex_state = 110}, [22] = {.lex_state = 111},
[23] = {.lex_state = 111}, [23] = {.lex_state = 112},
[24] = {.lex_state = 112}, [24] = {.lex_state = 113},
[25] = {.lex_state = 112}, [25] = {.lex_state = 113},
[26] = {.lex_state = 80}, [26] = {.lex_state = 80},
[27] = {.lex_state = 100, .external_lex_state = 1}, [27] = {.lex_state = 100, .external_lex_state = 1},
[28] = {.lex_state = 115, .external_lex_state = 1}, [28] = {.lex_state = 115, .external_lex_state = 1},
@ -1900,7 +1918,7 @@ static TSLexMode ts_lex_modes[STATE_COUNT] = {
[32] = {.lex_state = 118}, [32] = {.lex_state = 118},
[33] = {.lex_state = 118}, [33] = {.lex_state = 118},
[34] = {.lex_state = 96}, [34] = {.lex_state = 96},
[35] = {.lex_state = 111}, [35] = {.lex_state = 112},
[36] = {.lex_state = 118}, [36] = {.lex_state = 118},
[37] = {.lex_state = 76}, [37] = {.lex_state = 76},
[38] = {.lex_state = 76}, [38] = {.lex_state = 76},
@ -1912,17 +1930,17 @@ static TSLexMode ts_lex_modes[STATE_COUNT] = {
[44] = {.lex_state = 76}, [44] = {.lex_state = 76},
[45] = {.lex_state = 100}, [45] = {.lex_state = 100},
[46] = {.lex_state = 76}, [46] = {.lex_state = 76},
[47] = {.lex_state = 109}, [47] = {.lex_state = 110},
[48] = {.lex_state = 109}, [48] = {.lex_state = 110},
[49] = {.lex_state = 119}, [49] = {.lex_state = 119},
[50] = {.lex_state = 109}, [50] = {.lex_state = 110},
[51] = {.lex_state = 118}, [51] = {.lex_state = 118},
[52] = {.lex_state = 96}, [52] = {.lex_state = 96},
[53] = {.lex_state = 111}, [53] = {.lex_state = 112},
[54] = {.lex_state = 96}, [54] = {.lex_state = 96},
[55] = {.lex_state = 120}, [55] = {.lex_state = 120},
[56] = {.lex_state = 120}, [56] = {.lex_state = 120},
[57] = {.lex_state = 110}, [57] = {.lex_state = 111},
[58] = {.lex_state = 96}, [58] = {.lex_state = 96},
[59] = {.lex_state = 96}, [59] = {.lex_state = 96},
[60] = {.lex_state = 118}, [60] = {.lex_state = 118},
@ -1938,7 +1956,7 @@ static TSLexMode ts_lex_modes[STATE_COUNT] = {
[70] = {.lex_state = 96}, [70] = {.lex_state = 96},
[71] = {.lex_state = 118}, [71] = {.lex_state = 118},
[72] = {.lex_state = 96}, [72] = {.lex_state = 96},
[73] = {.lex_state = 111}, [73] = {.lex_state = 112},
[74] = {.lex_state = 100, .external_lex_state = 1}, [74] = {.lex_state = 100, .external_lex_state = 1},
[75] = {.lex_state = 100, .external_lex_state = 1}, [75] = {.lex_state = 100, .external_lex_state = 1},
[76] = {.lex_state = 100, .external_lex_state = 1}, [76] = {.lex_state = 100, .external_lex_state = 1},
@ -1948,17 +1966,17 @@ static TSLexMode ts_lex_modes[STATE_COUNT] = {
[80] = {.lex_state = 116}, [80] = {.lex_state = 116},
[81] = {.lex_state = 100, .external_lex_state = 1}, [81] = {.lex_state = 100, .external_lex_state = 1},
[82] = {.lex_state = 100}, [82] = {.lex_state = 100},
[83] = {.lex_state = 109}, [83] = {.lex_state = 110},
[84] = {.lex_state = 119}, [84] = {.lex_state = 119},
[85] = {.lex_state = 96}, [85] = {.lex_state = 96},
[86] = {.lex_state = 118}, [86] = {.lex_state = 118},
[87] = {.lex_state = 96}, [87] = {.lex_state = 96},
[88] = {.lex_state = 80}, [88] = {.lex_state = 80},
[89] = {.lex_state = 80}, [89] = {.lex_state = 80},
[90] = {.lex_state = 110}, [90] = {.lex_state = 111},
[91] = {.lex_state = 96}, [91] = {.lex_state = 96},
[92] = {.lex_state = 111}, [92] = {.lex_state = 112},
[93] = {.lex_state = 110}, [93] = {.lex_state = 111},
[94] = {.lex_state = 118}, [94] = {.lex_state = 118},
[95] = {.lex_state = 100}, [95] = {.lex_state = 100},
[96] = {.lex_state = 100, .external_lex_state = 1}, [96] = {.lex_state = 100, .external_lex_state = 1},
@ -1967,16 +1985,16 @@ static TSLexMode ts_lex_modes[STATE_COUNT] = {
[99] = {.lex_state = 80}, [99] = {.lex_state = 80},
[100] = {.lex_state = 118}, [100] = {.lex_state = 118},
[101] = {.lex_state = 118}, [101] = {.lex_state = 118},
[102] = {.lex_state = 111}, [102] = {.lex_state = 112},
[103] = {.lex_state = 118}, [103] = {.lex_state = 118},
[104] = {.lex_state = 111}, [104] = {.lex_state = 112},
[105] = {.lex_state = 100, .external_lex_state = 1}, [105] = {.lex_state = 100, .external_lex_state = 1},
[106] = {.lex_state = 80}, [106] = {.lex_state = 80},
[107] = {.lex_state = 100, .external_lex_state = 1}, [107] = {.lex_state = 100, .external_lex_state = 1},
[108] = {.lex_state = 80}, [108] = {.lex_state = 80},
[109] = {.lex_state = 109}, [109] = {.lex_state = 110},
[110] = {.lex_state = 119}, [110] = {.lex_state = 119},
[111] = {.lex_state = 111}, [111] = {.lex_state = 112},
[112] = {.lex_state = 118}, [112] = {.lex_state = 118},
[113] = {.lex_state = 80}, [113] = {.lex_state = 80},
[114] = {.lex_state = 119}, [114] = {.lex_state = 119},
@ -1989,9 +2007,9 @@ static TSLexMode ts_lex_modes[STATE_COUNT] = {
[121] = {.lex_state = 80}, [121] = {.lex_state = 80},
[122] = {.lex_state = 116}, [122] = {.lex_state = 116},
[123] = {.lex_state = 119}, [123] = {.lex_state = 119},
[124] = {.lex_state = 109}, [124] = {.lex_state = 110},
[125] = {.lex_state = 119}, [125] = {.lex_state = 119},
[126] = {.lex_state = 110}, [126] = {.lex_state = 111},
[127] = {.lex_state = 100, .external_lex_state = 1}, [127] = {.lex_state = 100, .external_lex_state = 1},
[128] = {.lex_state = 80}, [128] = {.lex_state = 80},
[129] = {.lex_state = 80}, [129] = {.lex_state = 80},