Skip to content

Commit 491b49d

Browse files
committed
Use tokenizer for JSON example
1 parent c80ef2d commit 491b49d

File tree

1 file changed

+35
-43
lines changed

1 file changed

+35
-43
lines changed

examples/json.ne

Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,35 @@
11
# http://www.json.org/
22
# http://www.asciitable.com/
3+
@{%
4+
// const moo = typeof module === 'object' && module.exports ? require('moo') : moo
5+
const moo = require('../moo')
6+
7+
let lexer = moo.compile({
8+
SPACE: {match: /\s+/, lineBreaks: true},
9+
NUMBER: /-?(?:[0-9]|[1-9][0-9]+)(?:\.[0-9]+)?(?:[eE][-+]?[0-9]+)?\b/,
10+
STRING: /"(?:\\["bfnrt\/\\]|\\u[a-fA-F0-9]{4}|[^"\\])*"/,
11+
'{': '{',
12+
'}': '}',
13+
'[': '[',
14+
']': ']',
15+
',': ',',
16+
':': ':',
17+
TRUE: /true\b/,
18+
FALSE: /false\b/,
19+
NULL: /null\b/,
20+
})
21+
22+
var SPACE = {type: 'SPACE'}
23+
var STRING = {type: 'STRING'}
24+
var NUMBER = {type: 'NUMBER'}
25+
var TRUE = {type: 'TRUE'}
26+
var FALSE = {type: 'FALSE'}
27+
var NULL = {type: 'NULL'}
28+
%}
329

4-
json -> [\s]:* (object | array) [\s]:* {% function(d) { return d[1][0]; } %}
30+
@lexer lexer
31+
32+
json -> _ (object | array) _ {% function(d) { return d[1][0]; } %}
533

634
object -> "{" _ "}" {% function(d) { return {}; } %}
735
| "{" _ pair (_ "," _ pair):* _ "}" {% extractObject %}
@@ -14,39 +42,19 @@ value ->
1442
| array {% id %}
1543
| number {% id %}
1644
| string {% id %}
17-
| "true" {% function(d) { return true; } %}
18-
| "false" {% function(d) { return false; } %}
19-
| "null" {% function(d) { return null; } %}
45+
| %TRUE {% function(d) { return true; } %}
46+
| %FALSE {% function(d) { return false; } %}
47+
| %NULL {% function(d) { return null; } %}
2048

21-
number -> "-":? ("0" | intPart) fracPart:? expPart:? {% extractNumber %}
49+
number -> %NUMBER {% function(d) { return parseFloat(d[0].value) } %}
2250

23-
string -> "\"" validChar:* "\"" {% function(d) { return d[1].join("") } %}
51+
string -> %STRING {% function(d) { return JSON.parse(d[0].value) } %}
2452

2553
pair -> key _ ":" _ value {% function(d) { return [d[0], d[4]]; } %}
2654

2755
key -> string {% id %}
2856

29-
intPart -> [1-9] [0-9]:* {% function(d) { return d[0] + d[1].join(""); } %}
30-
31-
fracPart -> "." [0-9]:* {% function(d) { return d[0] + d[1].join(""); } %}
32-
33-
expPart -> [eE] [+-]:? [0-9]:* {% function(d) { return d[0] + (d[1] || '') + d[2].join(""); } %}
34-
35-
validChar ->
36-
[^"\\] {% function(d) { return d[0]; } %}
37-
| "\\\"" {% function(d) { return "\""; } %}
38-
| "\\\\" {% function(d) { return "\\"; } %}
39-
| "\\/" {% function(d) { return "/"; } %}
40-
| "\\n" {% function(d) { return "\n"; } %}
41-
| "\\b" {% function(d) { return "\b"; } %}
42-
| "\\f" {% function(d) { return "\f"; } %}
43-
| "\\r" {% function(d) { return "\r"; } %}
44-
| "\\t" {% function(d) { return "\t"; } %}
45-
| "\\u" hex hex hex hex {% unicodehex %}
46-
47-
hex -> [0-9a-fA-F] {% function(d) { return d[0]; } %}
48-
49-
_ -> null | [\s]:+ {% function(d) { return null; } %}
57+
_ -> null | %SPACE {% function(d) { return null; } %}
5058

5159
@{%
5260

@@ -76,20 +84,4 @@ function extractArray(d) {
7684
return output;
7785
}
7886

79-
function unicodehex(d) {
80-
let codePoint = parseInt(d[1]+d[2]+d[3]+d[4], 16);
81-
82-
// Handle '\\'
83-
if (codePoint == 92) {
84-
return "\\";
85-
}
86-
87-
return String.fromCodePoint(codePoint);
88-
}
89-
90-
function extractNumber(d) {
91-
let value = (d[0] || '') + d[1] + (d[2] || '') + (d[3] || '');
92-
return parseFloat(value);
93-
}
94-
9587
%}

0 commit comments

Comments
 (0)