Combinator.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { WhiteSpace, Delim } from '../../tokenizer/index.js';
  2. const PLUSSIGN = 0x002B; // U+002B PLUS SIGN (+)
  3. const SOLIDUS = 0x002F; // U+002F SOLIDUS (/)
  4. const GREATERTHANSIGN = 0x003E; // U+003E GREATER-THAN SIGN (>)
  5. const TILDE = 0x007E; // U+007E TILDE (~)
  6. export const name = 'Combinator';
  7. export const structure = {
  8. name: String
  9. };
  10. // + | > | ~ | /deep/
  11. export function parse() {
  12. const start = this.tokenStart;
  13. let name;
  14. switch (this.tokenType) {
  15. case WhiteSpace:
  16. name = ' ';
  17. break;
  18. case Delim:
  19. switch (this.charCodeAt(this.tokenStart)) {
  20. case GREATERTHANSIGN:
  21. case PLUSSIGN:
  22. case TILDE:
  23. this.next();
  24. break;
  25. case SOLIDUS:
  26. this.next();
  27. this.eatIdent('deep');
  28. this.eatDelim(SOLIDUS);
  29. break;
  30. default:
  31. this.error('Combinator is expected');
  32. }
  33. name = this.substrToCursor(start);
  34. break;
  35. }
  36. return {
  37. type: 'Combinator',
  38. loc: this.getLocation(start, this.tokenStart),
  39. name
  40. };
  41. }
  42. export function generate(node) {
  43. this.tokenize(node.name);
  44. }