Skip to content

Commit 6570716

Browse files
committed
update to page 5, also add generated javascript file for reference
1 parent 183cd45 commit 6570716

File tree

3 files changed

+652
-17
lines changed

3 files changed

+652
-17
lines changed

nathansununiversity_03.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,66 @@ assert_eq(parse("Gray dog"), undefined,
7575
"don't parse Gray dog");
7676

7777
// page 4
78+
79+
// first attempt
80+
// this solution is not a solution, it just make test pass
81+
// need to think about several layer nested situation
82+
// need to recursively parse the expression
83+
// now using:
84+
// https://github.com/dannycoates/node-inspector
85+
// for debug node/pegjs
86+
// $ node --debug-brk your/short/node/script.js
87+
// $ node-inspector &
88+
// # open chrome/safari:
89+
// http://192.168.77.250:8080/debug?port=5858
90+
91+
start =
92+
expression
93+
94+
expression =
95+
atom
96+
/ listatom
97+
/ nestedlist
98+
99+
validchar
100+
= [0-9a-zA-Z_?!+\-=@#$%^&*/.]
101+
102+
atom =
103+
chars:validchar+
104+
{ return chars.join(""); }
105+
106+
listatom =
107+
"(" space* natom:atom satom:spaceatom* space* ")"
108+
{ if (satom && satom.length) {
109+
satom.unshift(natom);
110+
return satom;
111+
} else {
112+
return [natom];
113+
}
114+
}
115+
116+
nestedlist =
117+
"(" space* natom1:atom* satom1:spaceatom* space* latom1:listatom* space* ")"
118+
{
119+
var result = [];
120+
if (natom1 && natom1.length) {
121+
result = result.concat(natom1);
122+
}
123+
124+
if (satom1 && satom1.length) {
125+
result = result.concat(satom1);
126+
}
127+
128+
if (latom1 && latom1.length) {
129+
result = result.concat(latom1);
130+
}
131+
132+
return result;
133+
}
134+
135+
space =
136+
" "
137+
138+
spaceatom =
139+
space natom:atom
140+
{ return natom; }

0 commit comments

Comments
 (0)