default.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import {
  2. Ident,
  3. String as StringToken,
  4. Number as NumberToken,
  5. Function as FunctionToken,
  6. Url,
  7. Hash,
  8. Dimension,
  9. Percentage,
  10. LeftParenthesis,
  11. LeftSquareBracket,
  12. Comma,
  13. Delim
  14. } from '../../tokenizer/index.js';
  15. const NUMBERSIGN = 0x0023; // U+0023 NUMBER SIGN (#)
  16. const ASTERISK = 0x002A; // U+002A ASTERISK (*)
  17. const PLUSSIGN = 0x002B; // U+002B PLUS SIGN (+)
  18. const HYPHENMINUS = 0x002D; // U+002D HYPHEN-MINUS (-)
  19. const SOLIDUS = 0x002F; // U+002F SOLIDUS (/)
  20. const U = 0x0075; // U+0075 LATIN SMALL LETTER U (u)
  21. export default function defaultRecognizer(context) {
  22. switch (this.tokenType) {
  23. case Hash:
  24. return this.Hash();
  25. case Comma:
  26. return this.Operator();
  27. case LeftParenthesis:
  28. return this.Parentheses(this.readSequence, context.recognizer);
  29. case LeftSquareBracket:
  30. return this.Brackets(this.readSequence, context.recognizer);
  31. case StringToken:
  32. return this.String();
  33. case Dimension:
  34. return this.Dimension();
  35. case Percentage:
  36. return this.Percentage();
  37. case NumberToken:
  38. return this.Number();
  39. case FunctionToken:
  40. return this.cmpStr(this.tokenStart, this.tokenEnd, 'url(')
  41. ? this.Url()
  42. : this.Function(this.readSequence, context.recognizer);
  43. case Url:
  44. return this.Url();
  45. case Ident:
  46. // check for unicode range, it should start with u+ or U+
  47. if (this.cmpChar(this.tokenStart, U) &&
  48. this.cmpChar(this.tokenStart + 1, PLUSSIGN)) {
  49. return this.UnicodeRange();
  50. } else {
  51. return this.Identifier();
  52. }
  53. case Delim: {
  54. const code = this.charCodeAt(this.tokenStart);
  55. if (code === SOLIDUS ||
  56. code === ASTERISK ||
  57. code === PLUSSIGN ||
  58. code === HYPHENMINUS) {
  59. return this.Operator(); // TODO: replace with Delim
  60. }
  61. // TODO: produce a node with Delim node type
  62. if (code === NUMBERSIGN) {
  63. this.error('Hex or identifier is expected', this.tokenStart + 1);
  64. }
  65. break;
  66. }
  67. }
  68. };