-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.js
More file actions
199 lines (184 loc) · 6.55 KB
/
Parser.js
File metadata and controls
199 lines (184 loc) · 6.55 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
/* ************************************************************************
License: MIT
Authors: Lukas Michalski, Benjamin Zeller, Thorsten Schwalb
************************************************************************ */
qx.Class.define("pms.Parser",{
statics:
{
/******************************************************************************
* FUNCTION: Parser
******************************************************************************/
Parser : function () {
this.error = null;
this.buffer = null;
this.offset = 0;
},
/******************************************************************************
* FUNCTION: parseMessage
******************************************************************************/
parseMessage : function(message,TokenAndTail) {
this.error = null;
this.offset = 0;
this.buffer = message;
if (this.buffer[0] != "/") {
this.error = "First element of message has to be a /";
return undefined;
}
this.offset++;
//parse the Command Token
var name = this.parseToken();
if (name === undefined) return undefined;
var arguments = new Array();
while (1) {
this.consumeWhitespace();
if (this.remainingLength() <= 0) break;
var arg = undefined;
var onechar = this.currentChar();
if (onechar == "\"" || onechar == "'") arg = this.parseString();
else if (onechar.match(/^[0-9|\+|\-|\.]$/)) arg = this.parseNumber();
else if (TokenAndTail == true) arg = this.parseTail();
else {
this.error = "Unexpected Elements";
return undefined;
}
if (arg === undefined) {
this.error = "Unknown Error";
return undefined;
}
arguments.push(arg);
this.nextChar();
}
return {
'name': name,
'arguments': arguments
};
},
/******************************************************************************
* FUNCTION: consumeWhitespace
******************************************************************************/
consumeWhitespace : function() {
while (this.remainingLength() > 0 && this.currentChar().match(/\s/))
this.nextChar();
},
/******************************************************************************
* FUNCTION: parseToken
******************************************************************************/
parseToken : function() {
var token = null;
var firstChar = true;
while (this.remainingLength() > 0) {
var onechar = this.currentChar();
if (onechar.match(/\s/)) return token;
if (firstChar && onechar.match(/[A-Za-z_]/)) {
firstChar = false;
token = onechar;
this.nextChar();
continue;
} else if (onechar.match(/[A-Za-z0-9_]/)) {
token += onechar;
this.nextChar();
continue;
}
this.error = "A token can only contain the following chars: [A-Za-z0-9_] and can NOT start with a number";
return undefined;
}
if (token.length) return token;
this.error = "Empty token";
return undefined;
},
/******************************************************************************
* FUNCTION: parseNumber
******************************************************************************/
parseNumber : function() {
var number;
var point = false;
var onechar = this.currentChar();
if (onechar.match(/^[0-9|\+|\-|\.]$/)) {
number = onechar;
if (onechar == '.') point = true;
} else {
this.error = "Invalid Begin of Number";
return undefined;
}
while (this.hasNext()) {
onechar = this.nextChar();
if (onechar.match(/\s/)) break;
else if (!onechar.match(/^[0-9|.]$/)) {
this.error = "Invalid Part of Number";
return undefined;
}
if (onechar == '.') {
if (point) {
this.error = "Numbers can only have one Point";
return undefined;
} else {
point = true;
}
}
number += onechar;
}
var num = NaN;
if (!point) num = parseInt(number);
else num = parseFloat(number);
if (num == NaN) {
this.error = "Javascript Number Parse Error";
return undefined;
}
return num;
},
/******************************************************************************
* FUNCTION: parseString
******************************************************************************/
parseString : function() {
var message = null;
var quotes = this.currentChar();
while (this.hasNext()) {
var onechar = this.nextChar();
if (onechar == "\\"){
onechar = this.nextChar();
}else{
if (onechar == quotes) return message;
}
if (message == null) message = onechar;
else message += onechar;
}
this.error = "String is missing its end quotes";
return undefined;
},
/******************************************************************************
* FUNCTION: parseTail
******************************************************************************/
parseTail : function() {
var message = this.currentChar();
while(this.hasNext()) {
message += this.nextChar();
}
return "\""+message+"\"";
},
/******************************************************************************
* FUNCTION: currentChar
******************************************************************************/
currentChar : function() {
return this.buffer[this.offset];
},
/******************************************************************************
* FUNCTION: nextChar
******************************************************************************/
nextChar : function() {
this.offset++;
return this.buffer[this.offset];
},
/******************************************************************************
* FUNCTION: hasNext
******************************************************************************/
hasNext : function() {
return (this.offset + 1 < this.buffer.length);
},
/******************************************************************************
* FUNCTION: remainingLength
******************************************************************************/
remainingLength : function() {
return (this.buffer.length - this.offset);
}
}
});