-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jparser.js
118 lines (113 loc) · 3.34 KB
/
Jparser.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
const nullParser = input => input.startsWith('null') ? [null, input.slice(4)] : null
const boolParser = input => input.startsWith('true') ? ['true', input.slice(4)] : (input.startsWith('false') ? ['false', input.slice(5)] : null)
const commaParser = input => input.startsWith(',') ? [',', input.slice(1)] : null
const colonParser = input => input.startsWith(':') ? [':', input.slice(1)] : null
const spaceParser = input => input.match(/^[\s\n]/) ? [null, input.slice(input.match(/\S/).index)] : null
function stringParser (input) {
let i = 1
if (input.startsWith('"')) {
let s = ''
while (input[i] !== '"') {
if (input[i] === '\\') {
s = s + input.substr(i, 2)
i += 2
} else {
s = s + input[i]
i++
}
}
return [s, input.slice(i + 1)]
}
return null
}
function numParser (input) {
let regexp = String(input).match(/^[-+]?(\d+(\.\d*)?|\.\d+)([e][+-]?\d+)?/)
if (!String(input).match(/^[-+]?(\d+(\.\d*)?|\.\d+)([e][+-]?\d+)?/)) return null
return [parseInt(regexp[0]), input.slice(regexp[0].length)]
}
function arrayParser (input) {
let result
let s = []
if (!input.startsWith('[')) {
return null
}
input = input.slice(1)
while (true) {
result = spaceParser(input)
if (result !== null) input = result[1]
result = valueParser(input)
if (result === null) break
s.push(result[0])
input = result[1]
result = spaceParser(input)
if (result !== null) input = result[1]
result = commaParser(input)
if (result === null) break
input = result[1]
if (input.startsWith(']')) {
return null
}
}
result = spaceParser(input)
if (result !== null) input = result[1]
if (input.startsWith(']')) {
return [s, input.slice(1)]
}
return null
}
function objectParser (input) {
let obj = {}
let key = ''
let value
let result
if (!input.startsWith('{')) return null
input = input.slice(1)
while (true) {
result = spaceParser(input)
if (result !== null) input = result[1]
result = stringParser(input)
if (result === null) break
else {
key = result[0]
input = result[1]
result = spaceParser(input)
if (result !== null) input = result[1]
result = colonParser(input)
if (result === null) return null
input = result[1]
result = spaceParser(input)
if (result !== null) input = result[1]
result = valueParser(input)
if (result === null) return null
value = result[0]
obj[key] = value
input = result[1]
result = spaceParser(input)
if (result !== null) input = result[1]
result = commaParser(input)
if (result === null) break
input = result[1]
if (input.startsWith('}')) {
return null
}
}
}
result = spaceParser(input)
if (result !== null) input = result[1]
if (!input.startsWith('}')) return null
return [obj, input.slice(1)]
}
function valueParser (input) {
let result
result = objectParser(input) || arrayParser(input) || stringParser(input) || numParser(input) || nullParser(input) || boolParser(input)
return result
}
let input = require('fs').readFileSync('example.txt').toString()
let output = valueParser(input)
if (output === null) output = 'Invalid JSON'
else {
let result = output[1].trim()
if (result.length !== 0) output = 'Invalid JSON'
else output = output[0]
}
console.log(output)