1 | "use strict";
|
---|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
3 | exports.parseFunctionArgs = exports.nonFunctionArgSeparator = exports.nonWhiteSpace = exports.isIdentWithValue = exports.isStringToken = exports.isIdentToken = exports.isNumberToken = exports.isDimensionToken = exports.Parser = void 0;
|
---|
4 | var tokenizer_1 = require("./tokenizer");
|
---|
5 | var Parser = /** @class */ (function () {
|
---|
6 | function Parser(tokens) {
|
---|
7 | this._tokens = tokens;
|
---|
8 | }
|
---|
9 | Parser.create = function (value) {
|
---|
10 | var tokenizer = new tokenizer_1.Tokenizer();
|
---|
11 | tokenizer.write(value);
|
---|
12 | return new Parser(tokenizer.read());
|
---|
13 | };
|
---|
14 | Parser.parseValue = function (value) {
|
---|
15 | return Parser.create(value).parseComponentValue();
|
---|
16 | };
|
---|
17 | Parser.parseValues = function (value) {
|
---|
18 | return Parser.create(value).parseComponentValues();
|
---|
19 | };
|
---|
20 | Parser.prototype.parseComponentValue = function () {
|
---|
21 | var token = this.consumeToken();
|
---|
22 | while (token.type === 31 /* WHITESPACE_TOKEN */) {
|
---|
23 | token = this.consumeToken();
|
---|
24 | }
|
---|
25 | if (token.type === 32 /* EOF_TOKEN */) {
|
---|
26 | throw new SyntaxError("Error parsing CSS component value, unexpected EOF");
|
---|
27 | }
|
---|
28 | this.reconsumeToken(token);
|
---|
29 | var value = this.consumeComponentValue();
|
---|
30 | do {
|
---|
31 | token = this.consumeToken();
|
---|
32 | } while (token.type === 31 /* WHITESPACE_TOKEN */);
|
---|
33 | if (token.type === 32 /* EOF_TOKEN */) {
|
---|
34 | return value;
|
---|
35 | }
|
---|
36 | throw new SyntaxError("Error parsing CSS component value, multiple values found when expecting only one");
|
---|
37 | };
|
---|
38 | Parser.prototype.parseComponentValues = function () {
|
---|
39 | var values = [];
|
---|
40 | while (true) {
|
---|
41 | var value = this.consumeComponentValue();
|
---|
42 | if (value.type === 32 /* EOF_TOKEN */) {
|
---|
43 | return values;
|
---|
44 | }
|
---|
45 | values.push(value);
|
---|
46 | values.push();
|
---|
47 | }
|
---|
48 | };
|
---|
49 | Parser.prototype.consumeComponentValue = function () {
|
---|
50 | var token = this.consumeToken();
|
---|
51 | switch (token.type) {
|
---|
52 | case 11 /* LEFT_CURLY_BRACKET_TOKEN */:
|
---|
53 | case 28 /* LEFT_SQUARE_BRACKET_TOKEN */:
|
---|
54 | case 2 /* LEFT_PARENTHESIS_TOKEN */:
|
---|
55 | return this.consumeSimpleBlock(token.type);
|
---|
56 | case 19 /* FUNCTION_TOKEN */:
|
---|
57 | return this.consumeFunction(token);
|
---|
58 | }
|
---|
59 | return token;
|
---|
60 | };
|
---|
61 | Parser.prototype.consumeSimpleBlock = function (type) {
|
---|
62 | var block = { type: type, values: [] };
|
---|
63 | var token = this.consumeToken();
|
---|
64 | while (true) {
|
---|
65 | if (token.type === 32 /* EOF_TOKEN */ || isEndingTokenFor(token, type)) {
|
---|
66 | return block;
|
---|
67 | }
|
---|
68 | this.reconsumeToken(token);
|
---|
69 | block.values.push(this.consumeComponentValue());
|
---|
70 | token = this.consumeToken();
|
---|
71 | }
|
---|
72 | };
|
---|
73 | Parser.prototype.consumeFunction = function (functionToken) {
|
---|
74 | var cssFunction = {
|
---|
75 | name: functionToken.value,
|
---|
76 | values: [],
|
---|
77 | type: 18 /* FUNCTION */
|
---|
78 | };
|
---|
79 | while (true) {
|
---|
80 | var token = this.consumeToken();
|
---|
81 | if (token.type === 32 /* EOF_TOKEN */ || token.type === 3 /* RIGHT_PARENTHESIS_TOKEN */) {
|
---|
82 | return cssFunction;
|
---|
83 | }
|
---|
84 | this.reconsumeToken(token);
|
---|
85 | cssFunction.values.push(this.consumeComponentValue());
|
---|
86 | }
|
---|
87 | };
|
---|
88 | Parser.prototype.consumeToken = function () {
|
---|
89 | var token = this._tokens.shift();
|
---|
90 | return typeof token === 'undefined' ? tokenizer_1.EOF_TOKEN : token;
|
---|
91 | };
|
---|
92 | Parser.prototype.reconsumeToken = function (token) {
|
---|
93 | this._tokens.unshift(token);
|
---|
94 | };
|
---|
95 | return Parser;
|
---|
96 | }());
|
---|
97 | exports.Parser = Parser;
|
---|
98 | var isDimensionToken = function (token) { return token.type === 15 /* DIMENSION_TOKEN */; };
|
---|
99 | exports.isDimensionToken = isDimensionToken;
|
---|
100 | var isNumberToken = function (token) { return token.type === 17 /* NUMBER_TOKEN */; };
|
---|
101 | exports.isNumberToken = isNumberToken;
|
---|
102 | var isIdentToken = function (token) { return token.type === 20 /* IDENT_TOKEN */; };
|
---|
103 | exports.isIdentToken = isIdentToken;
|
---|
104 | var isStringToken = function (token) { return token.type === 0 /* STRING_TOKEN */; };
|
---|
105 | exports.isStringToken = isStringToken;
|
---|
106 | var isIdentWithValue = function (token, value) {
|
---|
107 | return exports.isIdentToken(token) && token.value === value;
|
---|
108 | };
|
---|
109 | exports.isIdentWithValue = isIdentWithValue;
|
---|
110 | var nonWhiteSpace = function (token) { return token.type !== 31 /* WHITESPACE_TOKEN */; };
|
---|
111 | exports.nonWhiteSpace = nonWhiteSpace;
|
---|
112 | var nonFunctionArgSeparator = function (token) {
|
---|
113 | return token.type !== 31 /* WHITESPACE_TOKEN */ && token.type !== 4 /* COMMA_TOKEN */;
|
---|
114 | };
|
---|
115 | exports.nonFunctionArgSeparator = nonFunctionArgSeparator;
|
---|
116 | var parseFunctionArgs = function (tokens) {
|
---|
117 | var args = [];
|
---|
118 | var arg = [];
|
---|
119 | tokens.forEach(function (token) {
|
---|
120 | if (token.type === 4 /* COMMA_TOKEN */) {
|
---|
121 | if (arg.length === 0) {
|
---|
122 | throw new Error("Error parsing function args, zero tokens for arg");
|
---|
123 | }
|
---|
124 | args.push(arg);
|
---|
125 | arg = [];
|
---|
126 | return;
|
---|
127 | }
|
---|
128 | if (token.type !== 31 /* WHITESPACE_TOKEN */) {
|
---|
129 | arg.push(token);
|
---|
130 | }
|
---|
131 | });
|
---|
132 | if (arg.length) {
|
---|
133 | args.push(arg);
|
---|
134 | }
|
---|
135 | return args;
|
---|
136 | };
|
---|
137 | exports.parseFunctionArgs = parseFunctionArgs;
|
---|
138 | var isEndingTokenFor = function (token, type) {
|
---|
139 | if (type === 11 /* LEFT_CURLY_BRACKET_TOKEN */ && token.type === 12 /* RIGHT_CURLY_BRACKET_TOKEN */) {
|
---|
140 | return true;
|
---|
141 | }
|
---|
142 | if (type === 28 /* LEFT_SQUARE_BRACKET_TOKEN */ && token.type === 29 /* RIGHT_SQUARE_BRACKET_TOKEN */) {
|
---|
143 | return true;
|
---|
144 | }
|
---|
145 | return type === 2 /* LEFT_PARENTHESIS_TOKEN */ && token.type === 3 /* RIGHT_PARENTHESIS_TOKEN */;
|
---|
146 | };
|
---|
147 | //# sourceMappingURL=parser.js.map |
---|