Combinator.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. var TYPE = require('../../tokenizer').TYPE;
  2. var IDENT = TYPE.Ident;
  3. var PLUSSIGN = 0x002B; // U+002B PLUS SIGN (+)
  4. var SOLIDUS = 0x002F; // U+002F SOLIDUS (/)
  5. var GREATERTHANSIGN = 0x003E; // U+003E GREATER-THAN SIGN (>)
  6. var TILDE = 0x007E; // U+007E TILDE (~)
  7. // + | > | ~ | /deep/
  8. module.exports = {
  9. name: 'Combinator',
  10. structure: {
  11. name: String
  12. },
  13. parse: function() {
  14. var start = this.scanner.tokenStart;
  15. var code = this.scanner.source.charCodeAt(this.scanner.tokenStart);
  16. switch (code) {
  17. case GREATERTHANSIGN:
  18. case PLUSSIGN:
  19. case TILDE:
  20. this.scanner.next();
  21. break;
  22. case SOLIDUS:
  23. this.scanner.next();
  24. if (this.scanner.tokenType !== IDENT || this.scanner.lookupValue(0, 'deep') === false) {
  25. this.error('Identifier `deep` is expected');
  26. }
  27. this.scanner.next();
  28. if (!this.scanner.isDelim(SOLIDUS)) {
  29. this.error('Solidus is expected');
  30. }
  31. this.scanner.next();
  32. break;
  33. default:
  34. this.error('Combinator is expected');
  35. }
  36. return {
  37. type: 'Combinator',
  38. loc: this.getLocation(start, this.scanner.tokenStart),
  39. name: this.scanner.substrToCursor(start)
  40. };
  41. },
  42. generate: function(node) {
  43. this.chunk(node.name);
  44. }
  45. };