-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVisualParser.js
More file actions
185 lines (185 loc) · 4.38 KB
/
VisualParser.js
File metadata and controls
185 lines (185 loc) · 4.38 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
function VisualParser(code, fx, root)
{
this.origCode = code;
this.parseTable = fx;
this.rootObject = root;
this.lines = code.split(/\r?\n/);
this.warncodes = {
0: "Unspecified warning",
1: "Unexpected %last_token%",
2: "Uknown identifier %last_token%",
3: "Duplicate name \"%last_name%\"",
};
this.WARN_UNEXPECTED_TOKEN = 1;
this.WARN_UNKNOWN_TOKEN = 2;
this.WARN_NAME_COLLISION = 3;
this.WARN_MISSING_PARAM = 4;
this.errorcodes = {
0: "Unspecified error",
1: "No root object found",
2: "Name '%last_name%' already exists in current %parent_type%."
};
this.ERR_NO_ROOT = 1;
this.ERR_NAME_COLLISION = 2;
for(var linenumber = 0;linenumber < this.lines.length; linenumber++)
{
this.lines[linenumber] = this.lines[linenumber].trim();
}
this.init = function()
{
this.index = 0;
this.offset = 0;
this.oldOffset = 0;
this.currentLine = "";
this.objectStack = [];
this.errors = [];
this.warnings = [];
this.statevars = {};
};
this.warn = function(code = 0)
{
let effectiveCode = code;
if(!this.warncodes[code])
effectiveCode = 0;
let msg = this.warncodes[effectiveCode];
let msg2 = msg;
for(let statevar in this.statevars)
{
msg2 = msg2.replaceAll("%"+statevar+"%",this.statevars[statevar]);
}
console.warn("Parser warning on line "+ Number(this.index+1)+": #" + code + ": " + msg2);
console.log(this.objectStack);
this.warnings.push({code: code, message: msg2, line: this.index, character: this.oldOffset});
};
this.error = function(code = 0)
{
let effectiveCode = code;
if(!this.errorcodes[code])
effectiveCode = 0;
let msg = this.errorcodes[effectiveCode];
let msg2 = msg;
for(let statevar in this.statevars)
{
//console.warn(statevar);
msg2 = msg2.replaceAll("%"+statevar+"%",this.statevars[statevar]);
}
console.error("Syntax error on line "+ this.index+": #" + code + ": " + msg2);
this.errors.push({code: code, message: msg2, line: this.index, character: this.oldOffset});
};
this.saveOffset = function()
{
this.oldOffset = this.offset;
};
this.backtrack = function()
{
this.offset = this.oldOffset;
};
this.getWord = function ()
{
let nextspace = this.currentLine.indexOf(" ", this.offset);
let word = "";
if(this.offset >= this.currentLine.length)
{
return "";
}
if(nextspace === -1)
{
word = this.currentLine.slice(this.offset);
this.offset = this.currentLine.length;
this.statevars['last_name'] = word;
return word;
}
else
{
word = this.currentLine.slice(this.offset, nextspace);
this.offset = nextspace+1;
this.statevars['last_name'] = word;
return word;
}
}
this.getInt = function (suppressNaN = false)
{
const word = this.getWord();
const value = parseInt(word);
if(isNaN(value) && suppressNaN)
{
return 0;
}
return value;
};
this.getRest = function()
{
let word = this.currentLine.slice(this.offset);
this.offset = this.currentLine.length;
this.statevars['last_name'] = word;
return word;
}
/// gets whatever the topmost object currently is
this.getCurrent = function()
{
return this.objectStack.length >0 && this.objectStack[0];
};
///
this.doLine = function()
{
this.currentLine = this.lines[this.index];
this.offset = 0;
this.oldOffset = 0;
const word = this.getWord();
// skip the empty line
if(!word)
return true;
this.statevars['last_token'] = word;
const op = word.toUpperCase();
const current = this.getCurrent();
if(op in this.parseTable)
{
this.oldOffset = this.offset;
return this.parseTable[op].call(this,current);
}
else
{
this.warn(this.WARN_UNKNOWN_TOKEN);
return true;
}
};
this.go = function()
{
while(this.index<this.lines.length && this.doLine())
{
this.index++;
}
this.commit("");
};
this.checkParent = function(type)
{
for(let i =0;i<this.objectStack.length;i++)
{
if(this.objectStack[i].type == type)
{
return true;
}
}
return false;
}
this.commit = function(type)
{
this.statevars['parent_type'] = type;
while(this.objectStack.length > 0 && this.objectStack[0].type != type)
{
let cur = this.objectStack[0];
//console.log("trying to commit a <", cur.type, ">...")
if(cur.commit(this))
{
//console.log("yes");
let added = cur.parent.addItem(cur);
if(!added)
{
this.warn(this.WARN_NAME_COLLISION);
}
}
this.objectStack.shift();
}
return this.objectStack.length > 0 ? this.objectStack[0] : null;
};
};