commit
message
[Parser] refactoring stack reduction variables
author
Ben Vogt <[email protected]>
date
2017-08-26 18:49:05
stats
1 file(s) changed,
26 insertions(+),
16 deletions(-)
files
src/Parser/Parser.ts
1diff --git a/src/Parser/Parser.ts b/src/Parser/Parser.ts
2index b8d2b89..2147165 100644
3--- a/src/Parser/Parser.ts
4+++ b/src/Parser/Parser.ts
5@@ -100,6 +100,11 @@ const enum ReduceActions {
6 REDUCE_LAST_THREE_B
7 }
8
9+/**
10+ * Productions is used to look up both the number to use when reducing the stack (productions[x][1]) and the semantic
11+ * value that will replace the tokens in the stack (productions[x][0]).
12+ * @type {Array}
13+ */
14 let productions = [];
15 productions[ReduceActions.NO_ACTION] = 0;
16 productions[ReduceActions.RETURN_LAST] = [3, 2];
17@@ -1135,8 +1140,8 @@ let Parser = (function () {
18 parse: function parse(input) {
19 let self = this,
20 stack = [0],
21- semanticValueStack = [null], // semantic value stack
22- locationStack = [], // location stack
23+ semanticValueStack = [null],
24+ locationStack = [],
25 table = this.table,
26 yytext = '',
27 yylineno = 0,
28@@ -1146,9 +1151,6 @@ let Parser = (function () {
29 EOF = 1;
30
31 let args = locationStack.slice.call(arguments, 1);
32-
33- //this.reductionCount = this.shiftCount = 0;
34-
35 let lexer = Object.create(this.lexer);
36 let sharedState = {
37 yy: {
38@@ -1210,7 +1212,6 @@ let Parser = (function () {
39 _$: undefined
40 },
41 p,
42- len,
43 newState,
44 expected;
45 while (true) {
46@@ -1343,19 +1344,21 @@ let Parser = (function () {
47 break;
48
49 case LexActions.REDUCE: // Reduce
50- len = this.productions[action[1]][1];
51+ let currentProduction = this.productions[action[1]];
52+
53+ let lengthToReduceStackBy = currentProduction[1];
54
55 // perform semantic action
56- yyval.$ = semanticValueStack[semanticValueStack.length - len]; // default to $$ = $1
57+ yyval.$ = semanticValueStack[semanticValueStack.length - lengthToReduceStackBy]; // default to $$ = $1
58 // default location, uses first token for firsts, last for lasts
59 yyval._$ = {
60- first_line: locationStack[locationStack.length - (len || 1)].first_line,
61+ first_line: locationStack[locationStack.length - (lengthToReduceStackBy || 1)].first_line,
62 last_line: locationStack[locationStack.length - 1].last_line,
63- first_column: locationStack[locationStack.length - (len || 1)].first_column,
64+ first_column: locationStack[locationStack.length - (lengthToReduceStackBy || 1)].first_column,
65 last_column: locationStack[locationStack.length - 1].last_column
66 };
67 if (ranges) {
68- yyval._$.range = [locationStack[locationStack.length - (len || 1)].range[0], locationStack[locationStack.length - 1].range[1]];
69+ yyval._$.range = [locationStack[locationStack.length - (lengthToReduceStackBy || 1)].range[0], locationStack[locationStack.length - 1].range[1]];
70 }
71 result = this.performAction.apply(yyval, [yytext, sharedState.yy, action[1], semanticValueStack].concat(args));
72
73@@ -1364,14 +1367,14 @@ let Parser = (function () {
74 }
75
76 // pop off stack
77- if (len) {
78- stack = stack.slice(0, -1 * len * 2);
79- semanticValueStack = semanticValueStack.slice(0, -1 * len);
80- locationStack = locationStack.slice(0, -1 * len);
81+ if (lengthToReduceStackBy) {
82+ stack = stack.slice(0, -1 * lengthToReduceStackBy * 2);
83+ semanticValueStack = semanticValueStack.slice(0, -1 * lengthToReduceStackBy);
84+ locationStack = locationStack.slice(0, -1 * lengthToReduceStackBy);
85 }
86
87 // push non-terminal (reduce)
88- stack.push(this.productions[action[1]][0]);
89+ stack.push(currentProduction[0]);
90 semanticValueStack.push(yyval.$);
91 locationStack.push(yyval._$);
92 newState = table[stack[stack.length - 2]][stack[stack.length - 1]];