-
Notifications
You must be signed in to change notification settings - Fork 2
/
parser.js
214 lines (191 loc) · 4.75 KB
/
parser.js
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
205
206
207
208
209
210
211
212
213
//console.log(data.split('\n')[0]);
parser = (function(){
var spaceChar = ' ';
var quoteChar = '"';
var commentChar = '#';
var nifixList = ['=','<','>','<=','>='];
var mode = {
ready : 0,
section : 1,
quote : 2
}
function cutLine(line){
var wordList = [],
state = mode.ready,
word = [],
i;
//line = line + ' '; // similation \n removed by gloabal split
line = line.replace(/\r/g,' ');
line = line.replace(/\t/g,' ');
line = line + ' ';
for(i = 0; i < line.length; i++){
if(state === mode.ready){
if(line[i] === spaceChar){
state = mode.ready;
}
else if(line[i] === quoteChar){
state = mode.quote;
}
else if(line[i] === commentChar){
break;
}
else{
word.push(line[i]);
state = mode.section;
}
}
else if(state === mode.section){
if(line[i] === spaceChar){
state = mode.ready;
wordList.push(word.join(''));
word = [];
}
else if(line[i] === quoteChar){
//throw new Error('section mode shoult not meet quote char');
wordList.push(word.join(''));
word = [];
state = mode.quote;
}
else if(line[i] === commentChar){
wordList.push(word.join(''));
word = [];
break;
}
else{
word.push(line[i]);
}
}
else if(state === mode.quote){
if(line[i] === spaceChar){
word.push(line[i]);
}
else if(line[i] === quoteChar){
wordList.push(word.join(''));
word = [];
state = mode.ready;
}
else if(line[i] === commentChar){
word.push(line[i]);
}
else{
word.push(line[i]);
}
}
}
return wordList;
}
function parseBlock(wordList){
var rStack = [],
i,j = 0,
stackLeftBrace = [];
for(i = 0; i < wordList.length; i++){
if(wordList[i] === '{'){
rStack.push(wordList[i]);
stackLeftBrace.push(j);
j++;
}
else if(wordList[i] === '}'){
var lastLeftBrace = stackLeftBrace.pop();
rStack.push(wordList[i]);
rStack.push(rStack.splice(lastLeftBrace, rStack.length));
j = lastLeftBrace + 1;
}
else{
rStack.push(wordList[i]);
j++;
}
}
return rStack;
}
function isNumber(obj){
if(obj !== null && !isNaN(obj)){
return true;
}
return false;
}
function isArray(obj) {
return Object.prototype.toString.call(obj) === '[object Array]';
}
function parseNifix(wordBlock){
var i = 0,
stack = [];
if(!isArray(wordBlock)){
return wordBlock;
}
while(i < wordBlock.length){
if(nifixList.indexOf(wordBlock[i]) === -1){
stack.push(parseNifix(wordBlock[i]));
i += 1;
}
else{
stack.push([wordBlock[i], stack.pop(), parseNifix(wordBlock[i+1])]);
i += 2;
}
}
return stack;
}
function eqFind(tree, key){
var i;
for(i = 0; i < tree.length; i++){
if(tree[i][0] === '=' && tree[i][1] === key){
return tree[i][2];
}
}
}
function eqFindAll(tree, key){
var rl = [],
i;
for(i = 0; i < tree.length; i++){
if(tree[i][0] === '=' && tree[i][1] === key){
rl.push(tree[i][2]);
}
}
return rl;
}
function AST(tree){
this.tree = tree;
}
AST.prototype.find = function(key){
return new AST(eqFind(this.tree, key));
}
AST.prototype.findall = function(key){
return eqFindAll(this.tree).map(function(subtree){
return new AST(subtree);
});
}
AST.prototype.value = function(){
if(isNumber(this.tree)){
return Number(this.tree);
}
else if(isArray(this.tree) && this.tree[0] === '{'){
var value = this.tree.map(function(obj){
var obj2 = new AST(obj);
return obj2.value();
});
return value.slice(1, value.length - 1);
}
return this.tree;
}
function parse(data){
//var data = fs.readFileSync('00_eng_tech.txt', 'utf-8');
var lines = data.split('\n');
var wordLines = lines.map(cutLine);
var wordChain = Array.prototype.concat.apply([],wordLines);
var wordBlock = parseBlock(wordChain);
var wordTree = parseNifix(wordBlock);
return wordTree
}
function test(){
var fs = require('fs');
var data = fs.readFileSync('00_eng_tech.txt', 'utf-8');
var tree = new AST(parse(data));
const repl = require('repl');
repl.start('> ');
}
return {parse : parse,
cutLine : cutLine,
AST : AST
};
}());
//global.eqFind = eqFind;
//global.eqFindAll = eqFindAll;