occam-parsers 23.0.53 → 23.0.55

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. package/README.md +7 -7
  2. package/example.js +579 -189
  3. package/lib/bnf/bnf.js +2 -2
  4. package/lib/bnf/parser.js +4 -4
  5. package/lib/definition/{lookAheadModifierRule.js → callAheadModifierRule.js} +11 -11
  6. package/lib/definition/partRule/nonTerminal/ruleName.js +3 -3
  7. package/lib/example/basic/bnf.js +14 -0
  8. package/lib/example/basic/entries.js +18 -0
  9. package/lib/example/basic/lexer.js +136 -0
  10. package/lib/example/basic/parser.js +134 -0
  11. package/lib/example/view/basic.js +8 -8
  12. package/lib/example/view/div/sizeable.js +2 -2
  13. package/lib/node/bnf/{lookAheadModifier.js → callAheadModifier.js} +12 -12
  14. package/lib/node/bnf/choiceOfParts.js +4 -4
  15. package/lib/node/bnf/definition.js +3 -3
  16. package/lib/node/bnf/endOfLine.js +2 -2
  17. package/lib/node/bnf/epsilon.js +2 -2
  18. package/lib/node/bnf/noWhitespacePart.js +2 -2
  19. package/lib/node/bnf/part/nonTerminal.js +6 -6
  20. package/lib/node/bnf/part/terminal.js +3 -3
  21. package/lib/node/bnf/part.js +5 -5
  22. package/lib/node/bnf/partChoice.js +4 -4
  23. package/lib/node/bnf/regularExpression.js +2 -2
  24. package/lib/node/bnf/ruleName.js +3 -3
  25. package/lib/node/bnf/sequenceOfParts.js +4 -4
  26. package/lib/node/bnf/significantTokenType.js +2 -2
  27. package/lib/node/bnf/startOfContentPart.js +2 -2
  28. package/lib/node/bnf/terminalSymbol.js +2 -2
  29. package/lib/node/bnf/wildcard.js +2 -2
  30. package/lib/node/nonTerminal.js +39 -7
  31. package/lib/node/terminal.js +40 -11
  32. package/lib/part/nonTerminal/choiceOfParts.js +4 -4
  33. package/lib/part/nonTerminal/oneOrMoreParts.js +4 -4
  34. package/lib/part/nonTerminal/optionalPart.js +4 -4
  35. package/lib/part/nonTerminal/ruleName.js +8 -8
  36. package/lib/part/nonTerminal/sequenceOfParts.js +4 -4
  37. package/lib/part/nonTerminal/zeroOrMoreParts.js +4 -4
  38. package/lib/part/nonTerminal.js +6 -6
  39. package/lib/part/terminal.js +5 -5
  40. package/lib/rule/{lookAheadModifier.js → callAheadModifier.js} +15 -15
  41. package/lib/rule.js +21 -14
  42. package/lib/ruleNames.js +5 -5
  43. package/lib/utilities/bnf.js +9 -9
  44. package/lib/utilities/parse.js +28 -28
  45. package/package.json +1 -1
  46. package/src/bnf/bnf.js +2 -2
  47. package/src/bnf/parser.js +3 -3
  48. package/src/definition/{lookAheadModifierRule.js → callAheadModifierRule.js} +3 -3
  49. package/src/definition/partRule/nonTerminal/ruleName.js +4 -4
  50. package/src/example/basic/bnf.js +27 -0
  51. package/src/example/basic/entries.js +9 -0
  52. package/src/example/basic/lexer.js +17 -0
  53. package/src/example/basic/parser.js +15 -0
  54. package/src/example/view/basic.js +13 -10
  55. package/src/example/view/div/sizeable.js +1 -1
  56. package/src/node/bnf/callAheadModifier.js +7 -0
  57. package/src/node/bnf/choiceOfParts.js +3 -3
  58. package/src/node/bnf/definition.js +2 -2
  59. package/src/node/bnf/endOfLine.js +1 -1
  60. package/src/node/bnf/epsilon.js +1 -1
  61. package/src/node/bnf/noWhitespacePart.js +1 -1
  62. package/src/node/bnf/part/nonTerminal.js +6 -6
  63. package/src/node/bnf/part/terminal.js +2 -2
  64. package/src/node/bnf/part.js +4 -4
  65. package/src/node/bnf/partChoice.js +3 -3
  66. package/src/node/bnf/regularExpression.js +1 -1
  67. package/src/node/bnf/ruleName.js +2 -2
  68. package/src/node/bnf/sequenceOfParts.js +3 -3
  69. package/src/node/bnf/significantTokenType.js +1 -1
  70. package/src/node/bnf/startOfContentPart.js +1 -1
  71. package/src/node/bnf/terminalSymbol.js +1 -1
  72. package/src/node/bnf/wildcard.js +1 -1
  73. package/src/node/nonTerminal.js +43 -10
  74. package/src/node/terminal.js +37 -9
  75. package/src/part/nonTerminal/choiceOfParts.js +4 -4
  76. package/src/part/nonTerminal/oneOrMoreParts.js +4 -4
  77. package/src/part/nonTerminal/optionalPart.js +4 -4
  78. package/src/part/nonTerminal/ruleName.js +9 -9
  79. package/src/part/nonTerminal/sequenceOfParts.js +4 -4
  80. package/src/part/nonTerminal/zeroOrMoreParts.js +4 -4
  81. package/src/part/nonTerminal.js +4 -4
  82. package/src/part/terminal.js +3 -3
  83. package/src/rule/callAheadModifier.js +22 -0
  84. package/src/rule.js +30 -18
  85. package/src/ruleNames.js +1 -1
  86. package/src/utilities/bnf.js +6 -6
  87. package/src/utilities/parse.js +38 -28
  88. package/src/node/bnf/lookAheadModifier.js +0 -7
  89. package/src/rule/lookAheadModifier.js +0 -22
@@ -4,16 +4,16 @@ import Definition from "../../../definition";
4
4
  import RuleNamePart from "../../../part/nonTerminal/ruleName";
5
5
  import OptionalPartPart from "../../../part/nonTerminal/optionalPart";
6
6
 
7
- import { ruleNameRuleName, lookAheadModifierRuleName } from "../../../ruleNames";
7
+ import { ruleNameRuleName, callAheadModifierRuleName } from "../../../ruleNames";
8
8
 
9
9
  export default class RuleNameNonTerminalPartRuleDefinition extends Definition {
10
10
  static fromNothing() {
11
11
  const ruleNameRuleNamePart = RuleNamePart.fromRuleName(ruleNameRuleName),
12
- lookAheadModifierRuleNamePart = RuleNamePart.fromRuleName(lookAheadModifierRuleName),
13
- optionalLookAheadRuleNamePartPart = OptionalPartPart.fromPart(lookAheadModifierRuleNamePart),
12
+ callAheadModifierRuleNamePart = RuleNamePart.fromRuleName(callAheadModifierRuleName),
13
+ optionalCallAheadRuleNamePartPart = OptionalPartPart.fromPart(callAheadModifierRuleNamePart),
14
14
  parts = [
15
15
  ruleNameRuleNamePart,
16
- optionalLookAheadRuleNamePartPart
16
+ optionalCallAheadRuleNamePartPart
17
17
  ],
18
18
  precedence = null,
19
19
  ruleNameNonTerminalPartRuleDefinition = new RuleNameNonTerminalPartRuleDefinition(parts, precedence);
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+
3
+ const bnf = `
4
+
5
+ S ::= A... <END_OF_LINE> ;
6
+
7
+ A ::= T ":" . ;
8
+
9
+ T ::= T_ T~* ;
10
+
11
+ B ::= T_ T~* B~T ;
12
+
13
+ V ::= . ;
14
+
15
+ T_ ::= V ;
16
+
17
+ T~B ::= ":" B ;
18
+
19
+ B~T ::= ε ;
20
+
21
+ T~ ::= B~T B~* T~B ;
22
+
23
+ B~ ::= T~B T~* B~T ;
24
+
25
+ `;
26
+
27
+ export default bnf;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+
3
+ const entries = [
4
+ {
5
+ "unassigned": "."
6
+ }
7
+ ];
8
+
9
+ export default entries;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+
3
+ import { CommonLexer, BasicLexer as BasicLexerBase, EndOfLineSignificantToken } from "occam-lexers";
4
+
5
+ import entries from "./entries";
6
+
7
+ export default class BasicLexer extends BasicLexerBase {
8
+ static entries = entries;
9
+
10
+ static EndOfLineToken = EndOfLineSignificantToken; ///
11
+
12
+ static fromNothing() { return CommonLexer.fromNothing(BasicLexer); }
13
+
14
+ static fromRules(rules) { return CommonLexer.fromRules(BasicLexer, rules); }
15
+
16
+ static fromEntries(entries) { return CommonLexer.fromEntries(BasicLexer, entries); }
17
+ }
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+
3
+ import { CommonParser, BasicParser as BasicParserBase } from "../../index"; ///
4
+
5
+ import bnf from "./bnf";
6
+
7
+ export default class BasicParser extends BasicParserBase {
8
+ static bnf = bnf;
9
+
10
+ static fromNothing() { return CommonParser.fromNothing(BasicParser); }
11
+
12
+ static fromBNF(bnf) { return CommonParser.fromBNF(BasicParser, bnf); }
13
+
14
+ static fromRules(rules) { return CommonParser.fromRules(BasicParser, rules); }
15
+ }
@@ -1,24 +1,26 @@
1
1
  "use strict";
2
2
 
3
- import { BasicLexer } from "occam-lexers";
4
- import { BasicParser } from "../../index"; ///
5
-
6
3
  import View from "../view";
4
+ import BasicLexer from "../basic/lexer";
5
+ import BasicParser from "../basic/parser";
7
6
 
8
7
  const { bnf } = BasicParser,
9
8
  { entries } = BasicLexer;
10
9
 
11
- const basicLexer = BasicLexer.fromEntries(entries),
12
- basicParser = BasicParser.fromBNF(bnf);
13
-
14
10
  export default class BasicView extends View {
15
11
  getTokens(content) {
16
- const tokens = basicLexer.tokenise(content);
12
+ const lexicalEntries = this.getLexicalEntries(),
13
+ entries = lexicalEntries, ///
14
+ basicLexer = BasicLexer.fromEntries(entries),
15
+ tokens = basicLexer.tokenise(content);
17
16
 
18
17
  return tokens;
19
18
  }
20
19
 
21
20
  getNode(tokens) {
21
+ const bnf = this.getBNF(),
22
+ basicParser = BasicParser.fromBNF(bnf);
23
+
22
24
  const node = basicParser.parse(tokens);
23
25
 
24
26
  return node;
@@ -28,8 +30,8 @@ export default class BasicView extends View {
28
30
  this.assignContext();
29
31
 
30
32
  const { initialContent } = this.constructor,
31
- content = initialContent, ///
32
- lexicalEntries = entries; ///
33
+ lexicalEntries = entries, ///
34
+ content = initialContent; ///
33
35
 
34
36
  this.setBNF(bnf);
35
37
 
@@ -40,7 +42,8 @@ export default class BasicView extends View {
40
42
  this.keyUpHandler();
41
43
  }
42
44
 
43
- static initialContent = "1+2*3 . 1+2*3 .";
45
+ static initialContent = `f:x:y
46
+ `;
44
47
 
45
48
  static defaultProperties = {
46
49
  className: "basic"
@@ -6,7 +6,7 @@ import { SizeableDiv } from "easy-layout";
6
6
 
7
7
  export default withStyle(SizeableDiv)`
8
8
 
9
- width: 48rem;
9
+ width: 36rem;
10
10
  min-width: 24rem;
11
11
 
12
12
  `;
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+
3
+ import NonTerminalNode from "../../node/nonTerminal";
4
+
5
+ export default class CallAheadModifierBNFNode extends NonTerminalNode {
6
+ static fromRuleNameChildNodesAndCallAhead(ruleName, childNode, opacity) { return NonTerminalNode.fromRuleNameChildNodesAndCallAhead(CallAheadModifierBNFNode, ruleName, childNode, opacity); }
7
+ }
@@ -7,14 +7,14 @@ import { partChoiceRuleName } from "../../ruleNames";
7
7
  import { nodesFromChildNodesAndRuleName } from "../../utilities/node";
8
8
 
9
9
  export default class ChoiceOfPartsBNFNode extends NonTerminalNode {
10
- generatePart(lookAhead) {
10
+ generatePart(callAhead) {
11
11
  const childNodes = this.getChildNodes(),
12
12
  partChoiceBNFNodes = nodesFromChildNodesAndRuleName(childNodes, partChoiceRuleName);
13
13
 
14
- lookAhead = false; ///
14
+ callAhead = false; ///
15
15
 
16
16
  const partChoices = partChoiceBNFNodes.map((partChoiceBNFNode) => {
17
- const partChoice = partChoiceBNFNode.generatePartChoice(lookAhead);
17
+ const partChoice = partChoiceBNFNode.generatePartChoice(callAhead);
18
18
 
19
19
  return partChoice;
20
20
  }),
@@ -11,9 +11,9 @@ export default class DefinitionBNFNode extends NonTerminalNode {
11
11
  const childNodes = this.getChildNodes(),
12
12
  partBNFNodes = nodesFromChildNodesAndRuleName(childNodes, partRuleName),
13
13
  precedenceBNFNode = nodeFromChildNodesAndRuleName(childNodes, precedenceRuleName),
14
- lookAhead = false, ///
14
+ callAhead = false, ///
15
15
  parts = partBNFNodes.map((partBNFNode) => {
16
- const part = partBNFNode.generatePart(lookAhead);
16
+ const part = partBNFNode.generatePart(callAhead);
17
17
 
18
18
  return part;
19
19
  }),
@@ -4,7 +4,7 @@ import EndOfLinePart from "../../part/terminal/endOfLine";
4
4
  import NonTerminalNode from "../../node/nonTerminal";
5
5
 
6
6
  export default class EndOfLineBNFNode extends NonTerminalNode {
7
- generatePart(lookAhead) {
7
+ generatePart(callAhead) {
8
8
  const endOfLinePart = EndOfLinePart.fromNothing();
9
9
 
10
10
  return endOfLinePart;
@@ -4,7 +4,7 @@ import EpsilonPart from "../../part/terminal/epsilon";
4
4
  import NonTerminalNode from "../../node/nonTerminal";
5
5
 
6
6
  export default class EpsilonBNFNode extends NonTerminalNode {
7
- generatePart(lookAhead) {
7
+ generatePart(callAhead) {
8
8
  const epsilonPart = EpsilonPart.fromNothing();
9
9
 
10
10
  return epsilonPart;
@@ -4,7 +4,7 @@ import NonTerminalNode from "../../node/nonTerminal";
4
4
  import NoWhitespacePart from "../../part/terminal/noWhitespace";
5
5
 
6
6
  export default class NoWhitespacePartBNFNode extends NonTerminalNode {
7
- generatePart(lookAhead) {
7
+ generatePart(callAhead) {
8
8
  const noWhitespacePart = NoWhitespacePart.fromNothing();
9
9
 
10
10
  return noWhitespacePart;
@@ -4,24 +4,24 @@ import { arrayUtilities } from "necessary";
4
4
 
5
5
  import NonTerminalNode from "../../../node/nonTerminal";
6
6
 
7
- import { lookAheadModifierRuleName } from "../../../ruleNames";
7
+ import { callAheadModifierRuleName } from "../../../ruleNames";
8
8
  import { nodeFromChildNodesAndRuleName } from "../../../utilities/node";
9
9
 
10
10
  const { first } = arrayUtilities;
11
11
 
12
12
  export default class NonTerminalPartBNFNode extends NonTerminalNode {
13
- generatePart(lookAhead) {
13
+ generatePart(callAhead) {
14
14
  const childNodes = this.getChildNodes();
15
15
 
16
- if (!lookAhead) {
17
- const lookAheadModifierBNFNode = nodeFromChildNodesAndRuleName(childNodes, lookAheadModifierRuleName);
16
+ if (!callAhead) {
17
+ const callAheadModifierBNFNode = nodeFromChildNodesAndRuleName(childNodes, callAheadModifierRuleName);
18
18
 
19
- lookAhead = (lookAheadModifierBNFNode !== null);
19
+ callAhead = (callAheadModifierBNFNode !== null);
20
20
  }
21
21
 
22
22
  const firstChildNode = first(childNodes),
23
23
  node = firstChildNode, ///
24
- part = node.generatePart(lookAhead);
24
+ part = node.generatePart(callAhead);
25
25
 
26
26
  return part;
27
27
  }
@@ -7,11 +7,11 @@ import NonTerminalNode from "../../../node/nonTerminal";
7
7
  const { first } = arrayUtilities;
8
8
 
9
9
  export default class TerminalPartBNFNode extends NonTerminalNode {
10
- generatePart(lookAhead) {
10
+ generatePart(callAhead) {
11
11
  const childNodes = this.getChildNodes(),
12
12
  firstChildNode = first(childNodes),
13
13
  node = firstChildNode, ///
14
- part = node.generatePart(lookAhead);
14
+ part = node.generatePart(callAhead);
15
15
 
16
16
  return part;
17
17
  }
@@ -13,7 +13,7 @@ import { optionalQuantifierRuleName, oneOrMoreQuantifierRuleName, zeroOrMoreQuan
13
13
  const { last } = arrayUtilities;
14
14
 
15
15
  export default class PartBNFNode extends NonTerminalNode {
16
- generatePart(lookAhead) {
16
+ generatePart(callAhead) {
17
17
  const childNodes = this.getChildNodes(),
18
18
  nodes = childNodes.slice(),
19
19
  part = partFromNodes(nodes);
@@ -25,15 +25,15 @@ export default class PartBNFNode extends NonTerminalNode {
25
25
  }
26
26
 
27
27
  function partFromNodes(nodes) {
28
- let part = null;
28
+ let part;
29
29
 
30
30
  const nodesLength = nodes.length;
31
31
 
32
32
  if (nodesLength === 1) {
33
33
  const node = nodes.pop(),
34
- lookAhead = false;
34
+ callAhead = false;
35
35
 
36
- part = node.generatePart(lookAhead);
36
+ part = node.generatePart(callAhead);
37
37
  } else {
38
38
  const lastNodeQuantifierNode = isLastNodeQuantifierNode(nodes);
39
39
 
@@ -7,14 +7,14 @@ import { nodeFromChildNodesAndRuleName } from "../../utilities/node";
7
7
  import { partRuleName, precedenceRuleName } from "../../ruleNames";
8
8
 
9
9
  export default class PartChoiceBNFNode extends NonTerminalNode {
10
- generatePartChoice(lookAhead) {
10
+ generatePartChoice(callAhead) {
11
11
  const childNodes = this.getChildNodes(),
12
12
  partBNFNode = nodeFromChildNodesAndRuleName(childNodes, partRuleName),
13
13
  precedenceBNFNode = nodeFromChildNodesAndRuleName(childNodes, precedenceRuleName);
14
14
 
15
- lookAhead = false; ///
15
+ callAhead = false; ///
16
16
 
17
- const part = partBNFNode.generatePart(lookAhead),
17
+ const part = partBNFNode.generatePart(callAhead),
18
18
  precedence = (precedenceBNFNode === null) ?
19
19
  null :
20
20
  precedenceBNFNode.getPrecedence(),
@@ -10,7 +10,7 @@ const { first, second } = arrayUtilities;
10
10
  export default class RegularExpressionBNFNode extends NonTerminalNode {
11
11
  regularExpression = /^\/((?:\\.|[^\/])*)\/$/;
12
12
 
13
- generatePart(lookAhead) {
13
+ generatePart(callAhead) {
14
14
  const regularExpression = this.getRegularExpression(),
15
15
  regularExpressionPart = RegularExpressionPart.fromRegularExpression(regularExpression);
16
16
 
@@ -8,13 +8,13 @@ import NonTerminalNode from "../../node/nonTerminal";
8
8
  const { first } = arrayUtilities;
9
9
 
10
10
  export default class RuleNameBNFNode extends NonTerminalNode {
11
- generatePart(lookAhead) {
11
+ generatePart(callAhead) {
12
12
  const childNodes = this.getChildNodes(),
13
13
  firstChildNode = first(childNodes),
14
14
  terminalNode = firstChildNode, ///
15
15
  terminalNodeContent = terminalNode.getContent(),
16
16
  ruleName = terminalNodeContent, ///
17
- ruleNamePart = RuleNamePart.fromLookAheadAndRuleName(lookAhead, ruleName);
17
+ ruleNamePart = RuleNamePart.fromCallAheadAndRuleName(callAhead, ruleName);
18
18
 
19
19
  return ruleNamePart;
20
20
  }
@@ -7,14 +7,14 @@ import { partRuleName } from "../../ruleNames";
7
7
  import { nodesFromChildNodesAndRuleName } from "../../utilities/node";
8
8
 
9
9
  export default class SequenceOfPartsBNFNode extends NonTerminalNode {
10
- generatePart(lookAhead) {
10
+ generatePart(callAhead) {
11
11
  const childNodes = this.getChildNodes(),
12
12
  partBNFNodes = nodesFromChildNodesAndRuleName(childNodes, partRuleName);
13
13
 
14
- lookAhead = false; ///
14
+ callAhead = false; ///
15
15
 
16
16
  const parts = partBNFNodes.map((partBNFNode) => {
17
- const part = partBNFNode.generatePart(lookAhead);
17
+ const part = partBNFNode.generatePart(callAhead);
18
18
 
19
19
  return part;
20
20
  }),
@@ -10,7 +10,7 @@ const { first, second } = arrayUtilities;
10
10
  export default class SignificantTokenTypeBNFNode extends NonTerminalNode {
11
11
  regularExpression = /^\[([^\]]+)]$/;
12
12
 
13
- generatePart(lookAhead) {
13
+ generatePart(callAhead) {
14
14
  const significantTokenType = this.getSignificantTokenType(),
15
15
  significantTokenTypePart = SignificantTokenTypePart.fromSignificantTokenType(significantTokenType);
16
16
 
@@ -4,7 +4,7 @@ import NonTerminalNode from "../../node/nonTerminal";
4
4
  import StartOfContentPart from "../../part/terminal/startOfContent";
5
5
 
6
6
  export default class StartOfContentPartBNFNode extends NonTerminalNode {
7
- generatePart(lookAhead) {
7
+ generatePart(callAhead) {
8
8
  const startOfContentPart = StartOfContentPart.fromNothing();
9
9
 
10
10
  return startOfContentPart;
@@ -12,7 +12,7 @@ const { first, second } = arrayUtilities;
12
12
  export default class TerminalSymbolBNFNode extends NonTerminalNode {
13
13
  regularExpression = /^"((?:\\.|[^"\\])*)"$/;
14
14
 
15
- generatePart(lookAhead) {
15
+ generatePart(callAhead) {
16
16
  const content = this.getContent(),
17
17
  terminalSymbolPart = TerminalSymbolPart.fromContent(content);
18
18
 
@@ -4,7 +4,7 @@ import WildcardPart from "../../part/terminal/wildcard";
4
4
  import NonTerminalNode from "../../node/nonTerminal";
5
5
 
6
6
  export default class WildcardBNFNode extends NonTerminalNode {
7
- generatePart(lookAhead) {
7
+ generatePart(callAhead) {
8
8
  const wildcardPart = WildcardPart.fromNothing();
9
9
 
10
10
  return wildcardPart;
@@ -6,11 +6,12 @@ import { specialSymbols } from "occam-lexers";
6
6
  import NonTerminalNodeParseTree from "../parseTree/nonTerminalNode";
7
7
 
8
8
  const { first, match, forwardsSome, backwardsSome } = arrayUtilities,
9
- { opaque : opaqueSpecialSymbol , semiOpaque: semiOpaqueSpecialSymbol } = specialSymbols;
9
+ { opaque: opaqueSpecialSymbol , semiOpaque: semiOpaqueSpecialSymbol } = specialSymbols;
10
10
 
11
11
  export default class NonTerminalNode {
12
- constructor(ruleName, childNodes, opacity, precedence) {
12
+ constructor(ruleName, parentNode, childNodes, opacity, precedence) {
13
13
  this.ruleName = ruleName;
14
+ this.parentNode = parentNode;
14
15
  this.childNodes = childNodes;
15
16
  this.opacity = opacity;
16
17
  this.precedence = precedence;
@@ -20,6 +21,10 @@ export default class NonTerminalNode {
20
21
  return this.ruleName;
21
22
  }
22
23
 
24
+ getParentNode() {
25
+ return this.parentNode;
26
+ }
27
+
23
28
  getChildNodes() {
24
29
  return this.childNodes;
25
30
  }
@@ -40,6 +45,10 @@ export default class NonTerminalNode {
40
45
  this.childNodes = childNodes;
41
46
  }
42
47
 
48
+ setParentNode(parentNode) {
49
+ this.parentNode = parentNode;
50
+ }
51
+
43
52
  setPrecedence(precedence) {
44
53
  this.precedence = precedence;
45
54
  }
@@ -130,6 +139,22 @@ export default class NonTerminalNode {
130
139
  return significantTokens;
131
140
  }
132
141
 
142
+ getAncestorNodes() {
143
+ const ancestorNodes = [];
144
+
145
+ let parentNode = this.parentNode;
146
+
147
+ while (parentNode !== null) {
148
+ const ancestorNode = parentNode; ///
149
+
150
+ ancestorNodes.push(ancestorNode);
151
+
152
+ parentNode = parentNode.getParentNode();
153
+ }
154
+
155
+ return ancestorNodes;
156
+ }
157
+
133
158
  isUnprecedented() {
134
159
  let unprecedented = false;
135
160
 
@@ -235,14 +260,11 @@ export default class NonTerminalNode {
235
260
  clone(...remainingArguments) {
236
261
  const Class = this.constructor,
237
262
  ruleName = this.ruleName,
238
- childNodes = this.childNodes.map((childNode) => {
239
- childNode = childNode.clone(); ///
240
-
241
- return childNode;
242
- }),
263
+ parentNode = null,
264
+ childNodes = cloneChildNodes(this.childNodes),
243
265
  opacity = this.opacity,
244
266
  precedence = this.precedence,
245
- nonTerminalNode = new Class(ruleName, childNodes, opacity, precedence, ...remainingArguments);
267
+ nonTerminalNode = new Class(ruleName, parentNode, childNodes, opacity, precedence, ...remainingArguments);
246
268
 
247
269
  return nonTerminalNode;
248
270
  }
@@ -258,9 +280,20 @@ export default class NonTerminalNode {
258
280
  Class = NonTerminalNode; ///
259
281
  }
260
282
 
261
- const precedence = null,
262
- nonTerminalNode = new Class(ruleName, childNodes, opacity, precedence, ...remainingArguments);
283
+ const parentNode = null,
284
+ precedence = null,
285
+ nonTerminalNode = new Class(ruleName, parentNode, childNodes, opacity, precedence, ...remainingArguments);
263
286
 
264
287
  return nonTerminalNode;
265
288
  }
266
289
  }
290
+
291
+ function cloneChildNodes(childNodes) {
292
+ childNodes = childNodes.map((childNode) => { ///
293
+ childNode = childNode.clone(); ///
294
+
295
+ return childNode;
296
+ });
297
+
298
+ return childNodes;
299
+ }
@@ -3,14 +3,27 @@
3
3
  import TerminalNodeParseTree from "../parseTree/terminalNode";
4
4
 
5
5
  export default class TerminalNode {
6
- constructor(significantToken) {
6
+ constructor(parentNode, significantToken) {
7
+ this.parentNode = parentNode;
7
8
  this.significantToken = significantToken;
8
9
  }
9
10
 
11
+ getParentNode() {
12
+ return this.parentNode;
13
+ }
14
+
10
15
  getSignificantToken() {
11
16
  return this.significantToken;
12
17
  }
13
18
 
19
+ setParentNode(parentNode) {
20
+ this.parentNode = parentNode;
21
+ }
22
+
23
+ setSignificantToken(significantToken) {
24
+ this.significantToken = significantToken;
25
+ }
26
+
14
27
  isStartOfContentNode() {
15
28
  const startOfContentNode = false;
16
29
 
@@ -89,6 +102,22 @@ export default class TerminalNode {
89
102
  return significantTokens;
90
103
  }
91
104
 
105
+ getAncestorNodes() {
106
+ const ancestorNodes = [];
107
+
108
+ let parentNode = this.parentNode;
109
+
110
+ while (parentNode !== null) {
111
+ const ancestorNode = parentNode; ///
112
+
113
+ ancestorNodes.push(ancestorNode);
114
+
115
+ parentNode = parentNode.getParentNode();
116
+ }
117
+
118
+ return ancestorNodes;
119
+ }
120
+
92
121
  isIncludedIn(node) {
93
122
  let includedIn = false;
94
123
 
@@ -131,10 +160,6 @@ export default class TerminalNode {
131
160
  return matches;
132
161
  }
133
162
 
134
- setSignificantToken(significantToken) {
135
- this.significantToken = significantToken;
136
- }
137
-
138
163
  asParseTree(tokens) {
139
164
  const terminalNode = this, ///
140
165
  terminalNodeParseTree = TerminalNodeParseTree.fromTerminalNodeAndTokens(terminalNode, tokens),
@@ -145,8 +170,9 @@ export default class TerminalNode {
145
170
 
146
171
  clone(...remainingArguments) {
147
172
  const Class = this.constructor,
173
+ parentNode = null,
148
174
  significantToken = this.significantToken,
149
- terminalNode = new Class(significantToken, ...remainingArguments);
175
+ terminalNode = new Class(parentNode, significantToken, ...remainingArguments);
150
176
 
151
177
  return terminalNode;
152
178
  }
@@ -156,8 +182,9 @@ export default class TerminalNode {
156
182
  Class = TerminalNode; ///
157
183
  }
158
184
 
159
- const significantToken = null,
160
- terminalNode = new Class(significantToken, ...remainingArguments);
185
+ const parentNode = null,
186
+ significantToken = null,
187
+ terminalNode = new Class(parentNode, significantToken, ...remainingArguments);
161
188
 
162
189
  return terminalNode;
163
190
  }
@@ -169,7 +196,8 @@ export default class TerminalNode {
169
196
  Class = TerminalNode; ///
170
197
  }
171
198
 
172
- const terminalNode = new Class(significantToken, ...remainingArguments);
199
+ const parentNode = null,
200
+ terminalNode = new Class(parentNode, significantToken, ...remainingArguments);
173
201
 
174
202
  return terminalNode;
175
203
  }
@@ -6,8 +6,8 @@ import { parsePart } from "../../utilities/parse";
6
6
  import { ChoiceOfPartsPartType } from "../../partTypes";
7
7
 
8
8
  export default class ChoiceOfPartsPart extends NonTerminalPart {
9
- constructor(type, lookAhead, partChoices) {
10
- super(type, lookAhead);
9
+ constructor(type, callAhead, partChoices) {
10
+ super(type, callAhead);
11
11
 
12
12
  this.partChoices = partChoices;
13
13
  }
@@ -92,8 +92,8 @@ export default class ChoiceOfPartsPart extends NonTerminalPart {
92
92
 
93
93
  static fromPartChoices(partChoices) {
94
94
  const type = ChoiceOfPartsPartType,
95
- lookAhead = false,
96
- choiceOfPartsPart = new ChoiceOfPartsPart(type, lookAhead, partChoices);
95
+ callAhead = false,
96
+ choiceOfPartsPart = new ChoiceOfPartsPart(type, callAhead, partChoices);
97
97
 
98
98
  return choiceOfPartsPart;
99
99
  }
@@ -10,8 +10,8 @@ import { parseZeroOrMorePartsPart } from "./zeroOrMoreParts";
10
10
  const { plus } = specialSymbols;
11
11
 
12
12
  export default class OneOrMorePartsPart extends NonTerminalPart {
13
- constructor(type, lookAhead, part) {
14
- super(type, lookAhead);
13
+ constructor(type, callAhead, part) {
14
+ super(type, callAhead);
15
15
 
16
16
  this.part = part;
17
17
  }
@@ -48,8 +48,8 @@ export default class OneOrMorePartsPart extends NonTerminalPart {
48
48
 
49
49
  static fromPart(part) {
50
50
  const type = OneOrMorePartsPartType,
51
- lookAhead = false,
52
- oneOrMorePartsPart = new OneOrMorePartsPart(type, lookAhead, part);
51
+ callAhead = false,
52
+ oneOrMorePartsPart = new OneOrMorePartsPart(type, callAhead, part);
53
53
 
54
54
  return oneOrMorePartsPart;
55
55
  }