Skip to content

Commit 982310d

Browse files
committed
refactor parser
1 parent 230f63e commit 982310d

File tree

4 files changed

+74
-53
lines changed

4 files changed

+74
-53
lines changed

‎parser/index.pegjs

Lines changed: 32 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
{
22
// final parsed output for coderoad.json file
3-
var position = {
4-
page: -1,
5-
task: -1,
6-
};
73
var output = {
84
info: {
95
title: 'Tutorial Title',
@@ -47,62 +43,53 @@ start
4743
{ return output; }
4844

4945
doc
50-
= info_title
51-
info_description*
46+
= info
5247
break?
53-
page*
48+
page*
49+
50+
info
51+
= title: info_title
52+
description: description*
53+
{
54+
output.info.title = title;
55+
output.info.description = description.join('\n');
56+
}
5457

5558
page
56-
= page_title
57-
page_description*
58-
page_task*
59+
= title: page_title
60+
description: description*
61+
tasks: page_task*
62+
{
63+
output.pages.push({
64+
title,
65+
description: description.join('\n'),
66+
tasks,
67+
});
68+
69+
}
5970

6071
page_title
6172
= '##'
6273
space?
6374
title: content
6475
break
65-
{
66-
// increment page
67-
position.page += 1;
68-
position.task = -1;
69-
// add page outline
70-
output.pages.push({
71-
title: 'Page ' + position.page,
72-
description: '',
73-
});
74-
// add page title
75-
output.pages[position.page].title = adjust(title);
76-
}
77-
78-
page_description
79-
= description: content
80-
break
81-
{
82-
const d = output.pages[position.page].description;
83-
output.pages[position.page].description += d.length > 0 ? '\n' : '';
84-
output.pages[position.page].description += adjust(description);
85-
}
76+
{ return adjust(title); }
8677

8778
page_task
8879
= '+'
8980
space?
9081
description: content
9182
break
92-
test: task_test*
93-
hint: task_hint*
83+
tests: task_test*
84+
hints: task_hint*
9485
break?
9586

9687
{
97-
position.task += 1;
98-
if (!output.pages[position.page].tasks) {
99-
output.pages[position.page].tasks = [];
100-
}
101-
output.pages[position.page].tasks.push({
102-
description: adjust(description),
103-
tests: test,
104-
hints: hint,
105-
})
88+
return {
89+
description: adjust(description),
90+
tests,
91+
hints,
92+
};
10693
}
10794

10895
page_actions
@@ -141,16 +128,12 @@ info_title
141128
= '#'
142129
space?
143130
title: content
144-
{ output.info.title = adjust(title); }
131+
{ return adjust(title); }
145132

146-
info_description
133+
description
147134
= description: content
148135
break
149-
{
150-
const d = output.info.description;
151-
output.info.description += d.length > 0 ? '\n' : '';
152-
output.info.description += adjust(description);
153-
}
136+
{ return adjust(description); }
154137

155138
content = [^#^@^+] [^\n^\r]+ [\n\r]
156139
space = [ \s]

‎test/action.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const parser = readFileSync('../parser/index.pegjs', 'utf8');
66
const parse = pegjs.buildParser(parser).parse;
77

88
test.todo('parses an action: open');
9+
10+
911
test.todo('parses multiple actions: open');
1012

1113
test.todo('parses an action: set');

‎test/info.js

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { readFileSync } from 'fs';
55
const parser = readFileSync('../parser/index.pegjs', 'utf8');
66
const parse = pegjs.buildParser(parser).parse;
77

8-
test('parses a title', t => {
8+
test('parses an info title', t => {
99
const data = `# Title\n`;
1010
const expected = {
1111
info: {
@@ -17,7 +17,19 @@ test('parses a title', t => {
1717
t.deepEqual(result.info, expected.info);
1818
});
1919

20-
test('parses a description', t => {
20+
test('parses an info title with a # in the middle', t => {
21+
const data = `# T#i#t#l#e#\n`;
22+
const expected = {
23+
info: {
24+
title: 'T#i#t#l#e#',
25+
description: ''
26+
}
27+
};
28+
const result = parse(data);
29+
t.deepEqual(result.info, expected.info);
30+
});
31+
32+
test('parses an info description', t => {
2133
const data = `# Title
2234
some description
2335
`;
@@ -31,25 +43,47 @@ some description
3143
t.deepEqual(result.info, expected.info);
3244
});
3345

34-
test('parses a multiline description', t => {
46+
test('parses an info multiline description', t => {
47+
const data = `# Title
48+
some description
49+
and more on
50+
the next line
51+
`;
52+
const expected = {
53+
info: {
54+
title: 'Title',
55+
description: `some description
56+
and more on
57+
the next line`
58+
}
59+
};
60+
const result = parse(data);
61+
t.deepEqual(result.info, expected.info);
62+
});
63+
64+
test('parses an info description seperated by breaks', t => {
3565
const data = `# Title
3666
some description
67+
3768
and more on
69+
3870
the next line
3971
`;
4072
const expected = {
4173
info: {
4274
title: 'Title',
4375
description: `some description
76+
4477
and more on
78+
4579
the next line`
4680
}
4781
};
4882
const result = parse(data);
4983
t.deepEqual(result.info, expected.info);
5084
});
5185

52-
test.skip('parses a title after empty spaces', t => {
86+
test.skip('parses an info title after empty spaces', t => {
5387
const data = `
5488
5589

‎test/task-test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,5 @@ description
8080
});
8181

8282
test.todo('warns when missing a task test');
83+
84+
test.todo('parses an array of task tests');

0 commit comments

Comments
 (0)