-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
176 lines (133 loc) · 4.35 KB
/
app.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
//check if valid JSON object
//tokenize the input returning an array of tokens
//go though the array of token and return the correct thing
const tokenType = {
NUMBER : '0123456789',
}
function lexerJSON(input) {
let tokens = [];
let currentString = ''
let insideString = false
//console.log(input)
for(let i = 0; i < input.length; i++){
//console.log(input[i])
//handel strings
if( input[i] === '"' && !insideString ){
insideString = true
currentString = ''
}else if( input[i] === '"' && insideString){
insideString = false
tokens.push(currentString)
}else if(insideString){
currentString += input[i]
}else if(input[i] === ' ' ||
input[i] === '\t' ||
input[i] === '\n' ||
input[i] === '\r'){
continue
}else if(input[i] === 't' &&
input[i + 1] === 'r' &&
input[i + 2] === 'u' &&
input[i + 3] === 'e' ){
tokens.push(true)
i += 3
}else if(input[i] === 'f' &&
input[i + 1] === 'a' &&
input[i + 2] === 'l' &&
input[i + 3] === 's' &&
input[i + 4] === 'e'){
tokens.push(false)
i += 4
}else if(input[i] === 'n' &&
input[i + 1] === 'u' &&
input[i + 2] === 'l' &&
input[i + 3] === 'l' ){
tokens.push(null)
i += 3
}else if( tokenType.NUMBER.includes(input[i]) ){
//need to handle floats
let number = ''
while( tokenType.NUMBER.includes(input[i]) ){
number += input[i]
i++
}
if( isNaN(number) || number[0] == '0'){
throw new Error('Error in parsing the number')
}
tokens.push(parseFloat(number))
if(input[i] === ','){
tokens.push(',')
}
}else{
tokens.push(input[i])
}
}
//console.log(tokens)
return tokens
}
//fucntions are designed to return a value and an array of the remaining of the array not yet parsed
function parseValue(input){
//console.log(input)
let current = input[0]
if(current === '{'){
return parseObject(input.slice(1))
}else if(current === '['){
return parseArray(input.slice(1))
}else if(typeof current === 'string' ||
typeof current === 'number' ||
typeof current === 'boolean' ||
current === null )
{
return [current, input.slice(1)]
}else{
throw new Error(`unexpected token: ${current}`)
}
}
function parseArray(input){
let arr = []
let current = input[0]
if(input.length > 1 && current === ']'){
return [arr, input.slice(1)]
}
while(current !== ']'){
let [arrVal, remainingInput] = parseValue(input);
arr.push(arrVal);
current = remainingInput[0];
if (current === ']') {
return [arr, remainingInput.slice(1)];
} else if (current !== ',') {
throw new Error('Expected comma after object in array');
}
input = remainingInput.slice(1);
}
}
function parseObject(input){
let obj = {}
let current = input[0]
if(current === '}'){
return [obj, input.slice(1)]
}
while( input.length > 1 && current !== '}'){
let jsonKey = input[0];
if (typeof jsonKey === 'string') {
input = input.slice(1);
} else {
throw new Error(`Expected string key, got: ${jsonKey}`);
}
if (input[0] !== ':') {
throw new Error(`Expected colon after key in object, got: ${current}`);
}
let [jsonValue, remainingInput] = parseValue(input.slice(1));
obj[jsonKey] = jsonValue;
current = remainingInput[0];
if (current === '}') {
return [obj, remainingInput.slice(1)];
} else if (current !== ',') {
throw new Error(`Expected comma after pair in object, got: ${current}`);
}
input = remainingInput.slice(1);
}
return obj
}
module.exports.lexerJSON = lexerJSON
module.exports.parseValue = parseValue