-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.js
60 lines (51 loc) · 1.68 KB
/
parse.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
// states of parser
const IDLE = 0
const IN_WORD = 1
const IN_NUMBER = 2
const whitespace_r = /^\s$/
const wordFirstChar_r = /^[A-Za-z_]$/
const wordNextChars_r = /^[A-Za-z_0-9]$/
const numberFirstChar_r = /^[\-0-9]$/
const numberNextChars_r = /^[.0-9]$/
const operatorChar_r = /^[()+\-*/]$/
function parse(expression) {
const tokens = []
let state = IDLE
let currentToken = ''
for (let i = 0; i < expression.length; i += 1) {
const currentChar = expression[i]
if(state === IDLE) {
if(wordFirstChar_r.test(currentChar)) {
currentToken = currentChar
state = IN_WORD
} else if(numberFirstChar_r.test(currentChar)) {
currentToken = currentChar
state = IN_NUMBER
} else if(operatorChar_r.test(currentChar)) {
tokens.push(currentChar)
} else if(!whitespace_r.test(currentChar)) {
throw new Error(`Неизвестный символ '${currentChar}'`)
}
} else if(state === IN_WORD) {
if(wordNextChars_r.test(currentChar)) {
currentToken += currentChar
} else {
tokens.push(currentToken)
// re-iterate with idle
state = IDLE
i -= 1
}
} else if(state === IN_NUMBER) {
if(numberNextChars_r.test(currentChar)) {
currentToken += currentChar
} else {
tokens.push(currentToken)
// re-iterate with idle
state = IDLE
i -= 1
}
}
}
return tokens
}
module.exports = parse