/
vipl.js
167 lines (137 loc) · 3.48 KB
/
vipl.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
/**
* Created by Dzhambulat on 17.02.2015.
*/
function ViplCore(){
this.lexer=new Lexer();
this.parser=new Parser();
}
ViplCore.prototype.getTokens=function(text)
{
var tokens=[];
this.tokenIndex=0;
var v=this.lexer.scanStrings(text);
text= v.text;
delete v.text;
tokens.push.apply(tokens,v);
v=this.lexer.scanIdentificators(text);
text= v.text;
delete v.text;
tokens.push.apply(tokens,v);
v=this.lexer.scanNumbers(text);
text= v.text;
delete v.text;
tokens.push.apply(tokens,v);
v=this.lexer.scanLpAriphmeticOperators(text);
text= v.text;
delete v.text;
tokens.push.apply(tokens,v);
v=this.lexer.scanHpAriphmeticOperators(text);
delete v.text;
tokens.push.apply(tokens,v);
v=this.lexer.scanAssigmentOperators(text);
delete v.text;
tokens.push.apply(tokens,v);
tokens.sort(function(a,b)
{
return a.index- b.index;
});
this.tokens=tokens;
return tokens;
}
ViplCore.prototype.shift=function()
{
if(this.tokenIndex==this.tokens.length)
{
return null;
}
var token=this.tokens[this.tokenIndex];
this.tokenIndex+=1;
return token;
}
ViplCore.prototype.reduceProduct=function(state,token)
{
var isReduce=false;
var isShift=false;
for(var i in state)
{
var product=state[i];
var p=product.products.split(' ');
if (token!=null && p[product.pointIndex]===token.type)
{
isShift=true;
break;
}
else if (product.pointIndex=== p.length)
{
isReduce=true;
var productToReduce=product;
}
}
if (!isShift && isReduce)
{
return productToReduce;
}
else
{
return null;
}
}
ViplCore.prototype.interpret=function(text)
{
this.getTokens(text);
var genStack=[];
var states=[];
states.push([
{
pointIndex: 0,
products: 'EQ',
symbol: 'SA',
stateIndex:0
}]
);
states[0]=this.parser.closure(states[0],0); //states of LR parser
var currentState=0; //current state
var endParse=false;
var token=this.shift();
while (!endParse)
{
var newState=this.parser.closure(states[currentState],currentState);
if(newState!=null)
{
states[currentState]=newState;
continue;
}
else
{
var cstate=states[currentState];
var productToReduce=this.reduceProduct(cstate,token)
if (productToReduce==null)
{
if (token==null)
{
endParse=true;
continue
}
token.isToken=true;
genStack.push(token);
newState=this.parser.goto(states[currentState],token.type);
states.push(newState);
currentState+=1;
token=this.shift();
}
else
{
if (productToReduce.symbol==='SA')
{
endParse=true;
}
var reducedProduct=this.parser.reduce(productToReduce,genStack);
genStack.push(reducedProduct);
newState=this.parser.goto(states[productToReduce.stateIndex],productToReduce.symbol);
currentState+=1;
states.push(newState);
}
}
}
return reducedProduct;
}