-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathparse.js
139 lines (134 loc) · 3.89 KB
/
parse.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* eslint-disable no-console */
/* eslint-disable no-else-return */
/* eslint-disable dot-notation */
/* eslint-disable linebreak-style */
// Remove the space before and after the pseudocode
function removeLineContinuation(input) {
const lines = input.split('\n');
const output = [];
let builtLine = '';
for (const line of lines) {
builtLine = `${line.trim()}`;
if (builtLine !== '') {
output.push(builtLine);
}
}
return output;
}
// Extract the /Code {} section from pseudocode
function extractCode(lines) {
const jsons = [];
let json = {};
let explanation = '';
let explFlag = false;
let ind = 0;
for (const line of lines) {
if (line.localeCompare('\\In{') === 0) {
ind += 1;
} else if (line.localeCompare('\\In}') === 0) {
ind -= 1;
} else if (line.indexOf('\\Expl{') >= 0) {
explFlag = true;
explanation = '';
explanation += line.substring(7, line.length);
} else if (line.localeCompare('\\Expl}') === 0) {
explFlag = false;
if (Object.keys(json).length !== 0) {
json['explanation'] = explanation;
}
explanation = '';
} else if (explFlag) {
explanation += ' ';
explanation += line;
} else {
if (Object.keys(json).length !== 0) {
jsons.push(json);
json = {};
}
if (line.localeCompare('\\NewLine') === 0) {
jsons.push({ code: '\n', explanation: '', indentation: ind });
} else {
if (line.indexOf(' \\Ref ') >= 0) {
json['code'] = line.substring(0, line.indexOf(' \\Ref '));
json['ref'] = line.substring(
line.indexOf(' \\Ref ') + 6,
line.length
);
} else if (line.indexOf(' \\B ') >= 0) {
json['code'] = line.substring(0, line.indexOf(' \\B '));
json['bookmark'] = line.substring(
line.indexOf(' \\B ') + 4,
line.length
);
// json['ref'] = '';
} else {
json['code'] = line;
}
json['explanation'] = '';
json['indentation'] = ind;
}
}
}
if (Object.keys(json).length !== 0) {
jsons.push(json);
}
return jsons;
}
// For each code block, in other words /Code {} section,
// extract the code, explanation, indentation and reference information.
function extractCodeBlock(lines) {
let codeBlock = 'Default';
const json = {};
let value = [];
let codeFlag = false;
let blockFlag = false;
for (const line of lines) {
if (line.localeCompare('\\Code}') === 0) {
json[codeBlock] = extractCode(value);
value = [];
codeFlag = false;
} else if (line.localeCompare('\\Code{') === 0) {
codeFlag = true;
blockFlag = true;
} else if (codeFlag === true) {
if (blockFlag) {
codeBlock = line;
blockFlag = false;
} else {
value.push(line);
}
}
}
return json;
}
function addIndentation(originalBlocks, blockName, baseIndent, outputBlocks) {
let indentedLine;
// eslint-disable-next-line no-param-reassign
outputBlocks[blockName] = [];
originalBlocks[blockName].forEach((line) => {
indentedLine =
'\xa0\xa0\xa0\xa0'.repeat(baseIndent + line['indentation']) +
line['code'];
outputBlocks[blockName].push({ ...line, code: indentedLine });
if (line['ref']) {
addIndentation(
originalBlocks,
line['ref'],
baseIndent + line['indentation'],
outputBlocks
);
}
});
}
export default function parse(input) {
const inputRemovedNotes = input.replace(/\\Note\{[^}]*\}/gs, ''); // removing all flat notes
const rawCode = removeLineContinuation(inputRemovedNotes);
const rawCodeBlocks = extractCodeBlock(rawCode);
if (Object.keys(rawCodeBlocks).length > 0) {
const indentedCodeBlocks = {};
addIndentation(rawCodeBlocks, 'Main', 0, indentedCodeBlocks);
return indentedCodeBlocks;
} else {
return rawCodeBlocks;
}
}