Skip to content

Commit 8a8692c

Browse files
committed
refactor parser into switch blocks
1 parent bf4eea9 commit 8a8692c

File tree

19 files changed

+352
-374
lines changed

19 files changed

+352
-374
lines changed

lib/build/parser/chapter.js

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,37 +15,29 @@ function chapter(result, lines, index) {
1515
while (i < lines.length - 1) {
1616
i += 1;
1717
var line = lines[i];
18-
var importFile = Match.isImport(line);
19-
if (!!importFile) {
20-
lines = import_1.loadImport(lines, importFile);
21-
continue;
22-
}
23-
if (!!Match.codeBlock(line)) {
24-
if (line.length > 3) {
25-
result = addToDescription(i, result, line, index);
18+
switch (true) {
19+
case !!Match.isImport(line):
20+
lines = import_1.loadImport(lines, Match.isImport(line));
2621
continue;
27-
}
28-
inCodeBlock = !inCodeBlock;
29-
}
30-
if (!inCodeBlock) {
31-
if (Match.page(line)) {
22+
case !!Match.codeBlock(line):
23+
if (line.length > 3) {
24+
result.chapters[index.chapter].description += line;
25+
continue;
26+
}
27+
inCodeBlock = !inCodeBlock;
28+
case inCodeBlock:
29+
continue;
30+
case !!Match.page(line):
3231
return page_1.page(result, lines.slice(i), index);
33-
}
34-
else if (Match.chapter(line) && i > 1) {
32+
case Match.chapter(line) && i > 1:
3533
return chapter(result, lines.slice(i), index);
36-
}
37-
else {
38-
result = addToDescription(i, result, line, index);
39-
}
34+
default:
35+
if (i > 1) {
36+
result.chapters[index.chapter].description += '\n';
37+
}
38+
result.chapters[index.chapter].description += line;
4039
}
4140
}
4241
return result;
4342
}
4443
exports.chapter = chapter;
45-
function addToDescription(i, result, line, index) {
46-
if (i > 1) {
47-
result.chapters[index.chapter].description += '\n';
48-
}
49-
result.chapters[index.chapter].description += line;
50-
return result;
51-
}

lib/build/parser/cleanup.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
"use strict";
2+
function bracketTracker(line) {
3+
var l = (line.match(/\(/g) || []).length;
4+
var r = (line.match(/\)/g) || []).length;
5+
return l - r;
6+
}
7+
exports.bracketTracker = bracketTracker;
28
function trimLineBreaks(text) {
39
if (text.match(/^\s+|\s+$/)) {
410
text = text.replace(/^[\s\r\n]+|[\s\r\n]+$/g, '');
@@ -59,9 +65,6 @@ function cleanup(result) {
5965
if (page.description) {
6066
page.description = trimLineBreaks(page.description);
6167
}
62-
if (page.explanation) {
63-
page.explanation = trimLineBreaks(page.explanation);
64-
}
6568
if (page.tasks) {
6669
page.tasks.map(function (task) {
6770
if (task.description) {

lib/build/parser/match.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ var regex = {
77
'##': match('#', 2),
88
'###': match('#', 3),
99
'+': match('\\+', 1),
10-
'```': match('`', 3)
10+
'```': match('`', 3),
11+
'action': /^@(action|test|hint|continue)/,
12+
'import': /^@import\((.+)\)$/,
13+
'continue': /^@continue/
1114
};
1215
function parseWithCode(code) {
1316
return function (line) {
@@ -22,24 +25,19 @@ function parseWithCode(code) {
2225
}
2326
};
2427
}
25-
function isEmpty(line) {
26-
return !line.length || !!line.match(/^\s+?[\n\r]/);
27-
}
28-
exports.isEmpty = isEmpty;
2928
exports.project = parseWithCode('#');
3029
exports.chapter = parseWithCode('##');
3130
exports.page = parseWithCode('###');
3231
exports.task = parseWithCode('+');
3332
exports.codeBlock = parseWithCode('```');
33+
exports.isAction = parseWithCode('action');
34+
exports.isImport = parseWithCode('import');
35+
exports.isContinue = parseWithCode('continue');
3436
exports.isArray = function (line) {
3537
var isMatch = line.match(/^\[.+\]$/);
3638
return isMatch ? isMatch[0] : null;
3739
};
38-
exports.isAction = function (line) {
39-
var match = line.match(/^@(action|test|hint|continue)/);
40-
return !!match ? match[1] : null;
41-
};
42-
exports.isImport = function (line) {
43-
var isMatch = line.match(/^@import\((.+)\)$/);
44-
return isMatch ? isMatch[1] : null;
45-
};
40+
function isEmpty(line) {
41+
return !line.length || !!line.match(/^\s+?[\n\r]/);
42+
}
43+
exports.isEmpty = isEmpty;

lib/build/parser/page.js

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var Match = require('./match');
33
var chapter_1 = require('./chapter');
44
var task_1 = require('./task');
55
var import_1 = require('./import');
6+
var cleanup_1 = require('./cleanup');
67
function page(result, lines, index) {
78
var hasBreak = null;
89
index.page += 1;
@@ -12,61 +13,50 @@ function page(result, lines, index) {
1213
description: ''
1314
});
1415
var inCodeBlock = false;
16+
var currentContinue = null;
17+
var bracketCount = 0;
1518
var i = 0;
1619
while (i < lines.length - 1) {
1720
i += 1;
1821
var line = lines[i];
19-
var importFile = Match.isImport(line);
20-
if (!!importFile) {
21-
lines = import_1.loadImport(lines, importFile);
22-
continue;
23-
}
24-
if (!!Match.codeBlock(line)) {
25-
if (line.length > 3) {
26-
result = addToDescriptionOrExplanation(hasBreak, i, result, line, index);
22+
switch (true) {
23+
case !!Match.isImport(line):
24+
lines = import_1.loadImport(lines, Match.isImport(line));
2725
continue;
28-
}
29-
inCodeBlock = !inCodeBlock;
30-
}
31-
if (!inCodeBlock) {
32-
if (!hasBreak && Match.isEmpty(line)) {
33-
hasBreak = i;
34-
}
35-
else if (!!Match.chapter(line)) {
26+
case !!Match.codeBlock(line):
27+
if (line.length > 3) {
28+
result.chapters[index.chapter].pages[index.page].description += '\n' + line;
29+
continue;
30+
}
31+
inCodeBlock = !inCodeBlock;
32+
case inCodeBlock:
33+
continue;
34+
case !!Match.isContinue(line) || !!currentContinue:
35+
currentContinue = currentContinue ? currentContinue += line : line;
36+
bracketCount = cleanup_1.bracketTracker(line);
37+
if (bracketCount === 0) {
38+
result.chapters[index.chapter].pages[index.page].continue = currentContinue;
39+
currentContinue = null;
40+
}
41+
console.log(line);
42+
continue;
43+
case !!Match.chapter(line):
3644
return chapter_1.chapter(result, lines.slice(i), index);
37-
}
38-
else if (!!Match.page(line)) {
45+
case !!Match.page(line):
3946
return page(result, lines.slice(i), index);
40-
}
41-
else if (!!Match.task(line)) {
47+
case !!Match.task(line):
4248
if (result.chapters[index.chapter].pages[index.page].tasks === undefined) {
4349
result.chapters[index.chapter].pages[index.page].tasks = [];
4450
}
4551
return task_1.task(result, lines.slice(i), index);
46-
}
47-
else {
48-
result = addToDescriptionOrExplanation(hasBreak, i, result, line, index);
49-
}
52+
default:
53+
if (i > 1) {
54+
result.chapters[index.chapter].pages[index.page].description += '\n';
55+
}
56+
result.chapters[index.chapter].pages[index.page].description += line;
57+
continue;
5058
}
5159
}
5260
return result;
5361
}
5462
exports.page = page;
55-
function addToDescriptionOrExplanation(hasBreak, i, result, line, index) {
56-
if (!hasBreak) {
57-
if (i > 1) {
58-
result.chapters[index.chapter].pages[index.page].description += '\n';
59-
}
60-
result.chapters[index.chapter].pages[index.page].description += line;
61-
}
62-
else {
63-
if (result.chapters[index.chapter].pages[index.page].explanation === undefined) {
64-
result.chapters[index.chapter].pages[index.page].explanation = '';
65-
}
66-
if (i > 3) {
67-
result.chapters[index.chapter].pages[index.page].explanation += '\n';
68-
}
69-
result.chapters[index.chapter].pages[index.page].explanation += line;
70-
}
71-
return result;
72-
}

lib/build/parser/project.js

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,30 @@ function project(result, lines, index) {
88
while (i < lines.length - 1) {
99
i += 1;
1010
var line = lines[i];
11-
var importFile = Match.isImport(line);
12-
if (!!importFile) {
13-
lines = import_1.loadImport(lines, importFile);
14-
continue;
15-
}
16-
if (!!Match.codeBlock(line)) {
17-
if (line.length > 3) {
18-
result = addToDescription(i, result, line);
11+
switch (true) {
12+
case !!Match.isImport(line):
13+
lines = import_1.loadImport(lines, Match.isImport(line));
1914
continue;
20-
}
21-
inCodeBlock = !inCodeBlock;
22-
}
23-
if (!inCodeBlock) {
24-
var projectTitleMatch = Match.project(line);
25-
if (!!projectTitleMatch) {
26-
result.project.title = projectTitleMatch.trim();
27-
}
28-
else if (!!Match.chapter(line)) {
15+
case !!Match.codeBlock(line):
16+
if (line.length > 3) {
17+
result.project.description += line;
18+
continue;
19+
}
20+
inCodeBlock = !inCodeBlock;
21+
case inCodeBlock:
22+
continue;
23+
case !!Match.project(line):
24+
result.project.title = Match.project(line).trim();
25+
continue;
26+
case !!Match.chapter(line):
2927
return chapter_1.chapter(result, lines.slice(i), index);
30-
}
31-
else {
32-
result = addToDescription(i, result, line);
33-
}
28+
default:
29+
if (i > 1) {
30+
result.project.description += '\n';
31+
}
32+
result.project.description += line;
3433
}
3534
}
3635
return result;
3736
}
3837
exports.project = project;
39-
function addToDescription(i, result, line) {
40-
if (i > 1) {
41-
result.project.description += '\n';
42-
}
43-
result.project.description += line;
44-
return result;
45-
}

lib/build/parser/task.js

Lines changed: 32 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,29 @@ var page_1 = require('./page');
55
var actions_1 = require('./actions');
66
var cleanup_1 = require('./cleanup');
77
var import_1 = require('./import');
8-
function bracketTracker(line) {
9-
var l = (line.match(/\(/g) || []).length;
10-
var r = (line.match(/\)/g) || []).length;
11-
return l - r;
12-
}
138
function task(result, lines, index) {
149
result.chapters[index.chapter].pages[index.page].tasks.push({
1510
description: cleanup_1.trimLeadingSpaces(Match.task(lines[0]))
1611
});
1712
index.task += 1;
18-
var inExpCodeBlock = false;
13+
var inCodeBlock = false;
1914
var currentAction = null;
2015
var bracketCount = 0;
2116
var i = 0;
2217
while (i < lines.length - 1) {
2318
i += 1;
2419
var line = lines[i];
25-
var importFile = Match.isImport(line);
26-
if (!!importFile) {
27-
lines = import_1.loadImport(lines, importFile);
28-
}
29-
else {
30-
if (!!currentAction) {
20+
switch (true) {
21+
case !!Match.isImport(line):
22+
lines = import_1.loadImport(lines, Match.isImport(line));
23+
continue;
24+
case !!currentAction:
3125
if (line.length === 0) {
3226
currentAction += '\n';
3327
}
34-
else if ((bracketTracker(line) + bracketCount) !== 0) {
28+
else if ((cleanup_1.bracketTracker(line) + bracketCount) !== 0) {
3529
currentAction += line + '\n';
36-
bracketCount += bracketTracker(line);
30+
bracketCount += cleanup_1.bracketTracker(line);
3731
}
3832
else {
3933
currentAction += line;
@@ -42,46 +36,38 @@ function task(result, lines, index) {
4236
bracketCount = 0;
4337
}
4438
continue;
45-
}
46-
var isAction = Match.isAction(line);
47-
if (!isAction && !!Match.codeBlock(line)) {
39+
case !Match.isAction(line) && !!Match.codeBlock(line):
4840
if (line.length > 3) {
49-
result = addToDescription(i, result, line, index);
50-
continue;
51-
}
52-
inExpCodeBlock = !inExpCodeBlock;
53-
}
54-
if (!inExpCodeBlock) {
55-
if (!!isAction) {
56-
currentAction = line;
57-
bracketCount = bracketTracker(line);
58-
if (bracketCount === 0) {
59-
result = actions_1.addToTasks(result, currentAction, index);
60-
currentAction = null;
41+
if (i > 0) {
42+
result.chapters[index.chapter].pages[index.page].tasks[index.task].description += '\n';
6143
}
44+
result.chapters[index.chapter].pages[index.page].tasks[index.task].description += line;
6245
}
63-
else if (!!Match.task(line)) {
64-
return task(result, lines.slice(i), index);
65-
}
66-
else if (!!Match.page(line)) {
67-
return page_1.page(result, lines.slice(i), index);
46+
else {
47+
inCodeBlock = !inCodeBlock;
6848
}
69-
else if (!!Match.chapter(line)) {
70-
return chapter_1.chapter(result, lines.slice(i), index);
49+
continue;
50+
case !!Match.isAction(line):
51+
currentAction = line;
52+
bracketCount = cleanup_1.bracketTracker(line);
53+
if (bracketCount === 0) {
54+
result = actions_1.addToTasks(result, currentAction, index);
55+
currentAction = null;
7156
}
72-
else {
73-
result = addToDescription(i, result, line, index);
57+
continue;
58+
case !!Match.task(line):
59+
return task(result, lines.slice(i), index);
60+
case !!Match.page(line):
61+
return page_1.page(result, lines.slice(i), index);
62+
case !!Match.chapter(line):
63+
return chapter_1.chapter(result, lines.slice(i), index);
64+
default:
65+
if (i > 0) {
66+
result.chapters[index.chapter].pages[index.page].tasks[index.task].description += '\n';
7467
}
75-
}
68+
result.chapters[index.chapter].pages[index.page].tasks[index.task].description += line;
7669
}
7770
}
7871
return result;
7972
}
8073
exports.task = task;
81-
function addToDescription(i, result, line, index) {
82-
if (i > 0) {
83-
result.chapters[index.chapter].pages[index.page].tasks[index.task].description += '\n';
84-
}
85-
result.chapters[index.chapter].pages[index.page].tasks[index.task].description += line;
86-
return result;
87-
}

0 commit comments

Comments
 (0)