Scope.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import {
  2. Ident,
  3. LeftParenthesis,
  4. RightParenthesis
  5. } from '../../tokenizer/index.js';
  6. export const name = 'Scope';
  7. export const structure = {
  8. root: ['SelectorList', 'Raw', null],
  9. limit: ['SelectorList', 'Raw', null]
  10. };
  11. export function parse() {
  12. let root = null;
  13. let limit = null;
  14. this.skipSC();
  15. const startOffset = this.tokenStart;
  16. if (this.tokenType === LeftParenthesis) {
  17. this.next();
  18. this.skipSC();
  19. root = this.parseWithFallback(
  20. this.SelectorList,
  21. () => this.Raw(false, true)
  22. );
  23. this.skipSC();
  24. this.eat(RightParenthesis);
  25. }
  26. if (this.lookupNonWSType(0) === Ident) {
  27. this.skipSC();
  28. this.eatIdent('to');
  29. this.skipSC();
  30. this.eat(LeftParenthesis);
  31. this.skipSC();
  32. limit = this.parseWithFallback(
  33. this.SelectorList,
  34. () => this.Raw(false, true)
  35. );
  36. this.skipSC();
  37. this.eat(RightParenthesis);
  38. }
  39. return {
  40. type: 'Scope',
  41. loc: this.getLocation(startOffset, this.tokenStart),
  42. root,
  43. limit
  44. };
  45. }
  46. export function generate(node) {
  47. if (node.root) {
  48. this.token(LeftParenthesis, '(');
  49. this.node(node.root);
  50. this.token(RightParenthesis, ')');
  51. }
  52. if (node.limit) {
  53. this.token(Ident, 'to');
  54. this.token(LeftParenthesis, '(');
  55. this.node(node.limit);
  56. this.token(RightParenthesis, ')');
  57. }
  58. }