syntax_walker.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.SyntaxWalker = void 0;
  4. const base_util_js_1 = require("../common/base_util.js");
  5. const semantic_meaning_js_1 = require("../semantic_tree/semantic_meaning.js");
  6. const abstract_walker_js_1 = require("./abstract_walker.js");
  7. const levels_js_1 = require("./levels.js");
  8. class SyntaxWalker extends abstract_walker_js_1.AbstractWalker {
  9. constructor(node, generator, highlighter, xml) {
  10. super(node, generator, highlighter, xml);
  11. this.node = node;
  12. this.generator = generator;
  13. this.highlighter = highlighter;
  14. this.levels = null;
  15. this.restoreState();
  16. }
  17. initLevels() {
  18. const levels = new levels_js_1.Levels();
  19. levels.push([this.primaryId()]);
  20. return levels;
  21. }
  22. up() {
  23. super.up();
  24. const parent = this.previousLevel();
  25. if (!parent) {
  26. return null;
  27. }
  28. this.levels.pop();
  29. return this.singletonFocus(parent);
  30. }
  31. down() {
  32. super.down();
  33. const children = this.nextLevel();
  34. if (children.length === 0) {
  35. return null;
  36. }
  37. const focus = this.singletonFocus(children[0]);
  38. if (focus) {
  39. this.levels.push(children);
  40. }
  41. return focus;
  42. }
  43. combineContentChildren(type, role, content, children) {
  44. switch (type) {
  45. case semantic_meaning_js_1.SemanticType.RELSEQ:
  46. case semantic_meaning_js_1.SemanticType.INFIXOP:
  47. case semantic_meaning_js_1.SemanticType.MULTIREL:
  48. return (0, base_util_js_1.interleaveLists)(children, content);
  49. case semantic_meaning_js_1.SemanticType.PREFIXOP:
  50. return content.concat(children);
  51. case semantic_meaning_js_1.SemanticType.POSTFIXOP:
  52. return children.concat(content);
  53. case semantic_meaning_js_1.SemanticType.MATRIX:
  54. case semantic_meaning_js_1.SemanticType.VECTOR:
  55. case semantic_meaning_js_1.SemanticType.FENCED:
  56. children.unshift(content[0]);
  57. children.push(content[1]);
  58. return children;
  59. case semantic_meaning_js_1.SemanticType.CASES:
  60. children.unshift(content[0]);
  61. return children;
  62. case semantic_meaning_js_1.SemanticType.PUNCTUATED:
  63. if (role === semantic_meaning_js_1.SemanticRole.TEXT) {
  64. return (0, base_util_js_1.interleaveLists)(children, content);
  65. }
  66. return children;
  67. case semantic_meaning_js_1.SemanticType.APPL:
  68. return [children[0], content[0], children[1]];
  69. case semantic_meaning_js_1.SemanticType.ROOT:
  70. return [children[0], children[1]];
  71. default:
  72. return children;
  73. }
  74. }
  75. left() {
  76. super.left();
  77. const index = this.levels.indexOf(this.primaryId());
  78. if (index === null) {
  79. return null;
  80. }
  81. const id = this.levels.get(index - 1);
  82. return id ? this.singletonFocus(id) : null;
  83. }
  84. right() {
  85. super.right();
  86. const index = this.levels.indexOf(this.primaryId());
  87. if (index === null) {
  88. return null;
  89. }
  90. const id = this.levels.get(index + 1);
  91. return id ? this.singletonFocus(id) : null;
  92. }
  93. findFocusOnLevel(id) {
  94. return this.singletonFocus(id.toString());
  95. }
  96. focusDomNodes() {
  97. return [this.getFocus().getDomPrimary()];
  98. }
  99. focusSemanticNodes() {
  100. return [this.getFocus().getSemanticPrimary()];
  101. }
  102. }
  103. exports.SyntaxWalker = SyntaxWalker;