GeneralEnclosed.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import {
  2. Function as FunctionToken,
  3. LeftParenthesis,
  4. RightParenthesis
  5. } from '../../tokenizer/index.js';
  6. export const name = 'GeneralEnclosed';
  7. export const structure = {
  8. kind: String,
  9. function: [String, null],
  10. children: [[]]
  11. };
  12. // <function-token> <any-value> )
  13. // ( <any-value> )
  14. export function parse(kind) {
  15. const start = this.tokenStart;
  16. let functionName = null;
  17. if (this.tokenType === FunctionToken) {
  18. functionName = this.consumeFunctionName();
  19. } else {
  20. this.eat(LeftParenthesis);
  21. }
  22. const children = this.parseWithFallback(
  23. () => {
  24. const startValueToken = this.tokenIndex;
  25. const children = this.readSequence(this.scope.Value);
  26. if (this.eof === false &&
  27. this.isBalanceEdge(startValueToken) === false) {
  28. this.error();
  29. }
  30. return children;
  31. },
  32. () => this.createSingleNodeList(
  33. this.Raw(null, false)
  34. )
  35. );
  36. if (!this.eof) {
  37. this.eat(RightParenthesis);
  38. }
  39. return {
  40. type: 'GeneralEnclosed',
  41. loc: this.getLocation(start, this.tokenStart),
  42. kind,
  43. function: functionName,
  44. children
  45. };
  46. }
  47. export function generate(node) {
  48. if (node.function) {
  49. this.token(FunctionToken, node.function + '(');
  50. } else {
  51. this.token(LeftParenthesis, '(');
  52. }
  53. this.children(node);
  54. this.token(RightParenthesis, ')');
  55. }