parser.js 5.5 KB

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