Skip to content

Commit 51c9a38

Browse files
committed
info title and description parsed
1 parent 5617122 commit 51c9a38

File tree

5 files changed

+87
-35
lines changed

5 files changed

+87
-35
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# CodeRoad Parser
2+
3+
WIP

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"test": "ava"
1111
},
1212
"dependencies": {
13-
"pegjs": "^0.9.0"
13+
"pegjs": "^0.9.0",
14+
"pegjs-import": "^0.2.6"
1415
},
1516
"devDependencies": {
1617
"ava": "^0.16.0"

parser/action.pegjs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
action
2+
= action_insert
3+
/ action_set
4+
/ action_write
5+
/ action_write_from_file
6+
/ action_open
7+
8+
action_insert
9+
= 'insert(' + content + ')'
10+
action_set
11+
= 'set(' + content + ')'
12+
action_write
13+
= 'write(' + content + ')'
14+
action_write_from_file
15+
= 'writeFromFile(' + content + ')'
16+
action_open
17+
= 'open(' + quote? + file_path + quote? ')'

parser/index.pegjs

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,68 +8,49 @@
88
}
99

1010
start
11-
= doc
11+
= info
1212
{ return output; }
1313

1414
doc
1515
= info
16+
page*
1617

1718
page
18-
= title: page_title
19-
description: description
20-
{
21-
// set page
22-
output.pages.push({
23-
title: title,
24-
description: description
25-
});
26-
}
19+
= page_title
20+
description
2721

2822
page_title
2923
= '##'
3024
space
3125
title: content
26+
EOL
3227
{ return title.join(''); }
3328

3429
info
3530
= title: info_title
36-
end
37-
description: description
38-
end
31+
description: description
3932
{
4033
// set info
4134
output.info.title = title;
42-
output.info.description = description;
35+
output.info.description = description
4336
}
4437

4538
info_title
4639
= '#'
4740
space
4841
title: content
49-
end
42+
EOL
5043
{ return title.join(''); }
5144

5245
description
5346
= description: content
47+
EOL
5448
{ return description.join(''); }
5549

56-
content = .+
57-
space = [ ]?
58-
end = [\n]
59-
60-
// @import
61-
62-
// page
63-
// page_title
64-
// page_description
65-
// page_test
66-
// page_action
67-
// page_hint
68-
// page_on_complete
50+
content = [0-9A-Za-z ]*
51+
space = [ ]
52+
EOL = [\n\r]?
53+
file_path = [a-z_\-\s0-9\.]+
54+
quote = [\"\'\`]
6955

70-
// @action_insert
71-
// @action_set
72-
// @action_write
73-
// @action_write_from_file
74-
// @action_open
75-
// @action_cursor_position
56+
break = space EOL

test/titles.js

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

88
test('parses a title', t => {
9+
const data = `# Title\n`;
10+
const expected = {
11+
info: {
12+
title: 'Title',
13+
description: ''
14+
}
15+
};
16+
const result = parse(data);
17+
t.deepEqual(result.info, expected.info);
18+
});
19+
20+
test('parses a description', t => {
921
const data = `# Title
1022
some description
1123
`;
24+
console.log(data);
25+
const expected = {
26+
info: {
27+
title: 'Title',
28+
description: 'some description'
29+
}
30+
};
31+
const result = parse(data);
32+
t.deepEqual(result.info, expected.info);
33+
});
34+
35+
test.skip('parses a multiline description', t => {
36+
const data = `# Title
37+
some description
38+
and more on
39+
the next line
40+
`;
41+
console.log(data);
42+
const expected = {
43+
info: {
44+
title: 'Title',
45+
description: `some description
46+
and more on
47+
the next line`
48+
}
49+
};
50+
const result = parse(data);
51+
t.deepEqual(result.info, expected.info);
52+
});
53+
54+
test.skip('parses a title after empty spaces', t => {
55+
const data = `
56+
57+
58+
# Title
59+
some description
60+
`;
61+
console.log(data);
1262
const expected = {
1363
info: {
1464
title: 'Title',

0 commit comments

Comments
 (0)