-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.js
More file actions
204 lines (168 loc) · 4.42 KB
/
Copy pathParser.js
File metadata and controls
204 lines (168 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// @ts-check
const FileLoader = require("./FileLoader");
const Op = require("./Operators");
class Parser
{
/**
* @param {string} strFileName
*/
constructor(strFileName)
{
this._strFileName = strFileName;
this._arrRawFormulas = [];
this._arrParsedFormulas = [];
}
async processFile()
{
const strFileContents = await FileLoader.readFile(this._strFileName);
this._arrFormulas = this._separateFormulas(strFileContents);
// Concatanate all formulas using operator AND
this._arrFormulas = [this._arrFormulas.reduce((acc, red) => {
if(acc.length === 0)
{
return `(${red})`;
}
return acc += `&(${red})`;
}, "")];
for(let strFormula of this._arrFormulas)
{
this._arrRawFormulas.push(strFormula);
this._arrParsedFormulas.push(this._parseFormula(strFormula));
}
}
printAllFormulas()
{
for(let objFormula of this._arrParsedFormulas)
{
console.log("----------------------------------");
console.log(JSON.stringify(objFormula, null, 4));
console.log("----------------------------------\n");
}
}
_parseFormula(strFormula)
{
// First make sure to eliminate all white spaces and line endings
strFormula = strFormula.replace(/ |\r?\n|\r/g,'');
return this._addExpressionBlock(strFormula, true);
}
_addExpressionBlock(strFormula, bFirstCall = false)
{
console.log("Parsing: " + strFormula);
const objCurrentBlock = {};
if(strFormula.length === 0)
{
throw new Error("Empty string representation of formula given.");
}
// Make sure the raw formula is not bounded by redundant parentheses
if(
strFormula.charAt(0) === "("
&& strFormula.charAt(strFormula.length - 1) === ")"
&& bFirstCall === false
)
{
strFormula = strFormula.substring(1, strFormula.length - 1);
}
// // Check if there are parentheses
// if(strFormula.indexOf("(") !== -1)
let nOpenParenthesesCount = 0;
let nPrincipalOpOffset = null;
let principalOp = null;
for(let i = 0; i < strFormula.length; i++)
{
let strCurrentChar = strFormula.charAt(i);
// Only use the operators that are exterior to any parentheses
if(strCurrentChar === "(")
{
nOpenParenthesesCount++;
}
if(strCurrentChar === ")")
{
nOpenParenthesesCount--;
}
if(nOpenParenthesesCount != 0)
{
continue;
}
if(strCurrentChar === Op.OR.symbol)
{
nPrincipalOpOffset = i;
principalOp = Op.OR;
break;
}
if(strCurrentChar === Op.AND.symbol && principalOp !== Op.AND)
{
nPrincipalOpOffset = i;
principalOp = Op.AND;
}
if(strCurrentChar === Op.NOT.symbol && principalOp === null)
{
nPrincipalOpOffset = i;
principalOp = Op.NOT;
}
if(strCurrentChar === Op.IMP.symbol.charAt(0) && principalOp === null)
{
nPrincipalOpOffset = i;
principalOp = Op.IMP;
}
if(strCurrentChar === Op.EQI.symbol.charAt(0) && principalOp === null)
{
nPrincipalOpOffset = i;
principalOp = Op.EQI;
}
}
if(principalOp === null || nPrincipalOpOffset === null)
{
throw new Error("No valid operator found in expression. Check syntax.");
}
if(principalOp.type === "unary")
{
if(nPrincipalOpOffset === (strFormula.length - 1))
{
throw new Error("No characters left.");
}
objCurrentBlock.op = principalOp.name;
if(strFormula.charAt(nPrincipalOpOffset + 1) === "(")
{
// If is not primitive then it must be a parantheses block
objCurrentBlock.expR = this._addExpressionBlock(strFormula.substring(nPrincipalOpOffset + 1, strFormula.length));
}
else
{
objCurrentBlock.expR = strFormula.charAt(nPrincipalOpOffset + 1);
}
}
if(principalOp.type === "binary")
{
objCurrentBlock.op = principalOp.name;
// Checks for the left expression
if(nPrincipalOpOffset > 1)
{
objCurrentBlock.expL = this._addExpressionBlock(strFormula.substring(0, nPrincipalOpOffset));
}
else
{
objCurrentBlock.expL = strFormula.charAt(0);
}
// Checks for the right expression
if(strFormula.length - nPrincipalOpOffset > 2)
{
objCurrentBlock.expR = this._addExpressionBlock(strFormula.substring(nPrincipalOpOffset + 1, strFormula.length));
}
else
{
objCurrentBlock.expR = strFormula.charAt(nPrincipalOpOffset + 1);
}
}
return objCurrentBlock;
}
_separateFormulas(strFileContents)
{
const arrFormulas = strFileContents.split("\n");
return arrFormulas;
}
get parsedFormulas()
{
return this._arrParsedFormulas;
}
}
module.exports = Parser;