-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathttb-parser.js
More file actions
157 lines (138 loc) · 4.64 KB
/
Copy pathttb-parser.js
File metadata and controls
157 lines (138 loc) · 4.64 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* Copyright (C) 2014 Dockbite
* Author: Kester Everts
*
* This is free software, placed under the terms of the
* GNU Lesser General Public License, as published by the Free Software
* Foundation; either version 2.1 of the License, or (at your option) any
* later version. Please see the file LICENSE-LGPL for details.
*/
var fs = require("fs");
var path = require("path");
var tablePath = process.argv[2] || "./Tables";
var time = process.hrtime();
console.log(JSON.stringify({
braillelanguages: fs.readdirSync(tablePath).filter(function(filename) {
return path.extname(filename) == ".ttb";
}).map(function(filename) {
console.error("Producing " + filename + "...");
return {
"language": path.basename(filename, ".ttb"),
"charset": parseFile(filename)
};
})}, null, " "));
var diff = process.hrtime(time);
var perf = diff[0] + diff[1] / 1000000000;
console.error("\r\nDone in %d seconds", perf);
function parseFile(filename, includedFrom) {
//console.log("Parsing " + filename + (includedFrom ? (" included from " + includedFrom) : ""));
var table = [];
var file = fs.readFileSync(path.join(tablePath, filename), {encoding: "utf-8"});
var lines = file.split(/\n|\r|\r\n/);
// console.log(lines);
for(var i = 0; i < lines.length; i++) {
var line = lines[i];
table = table.concat(parseLine(line, filename, i + 1));
}
return table;
}
function parseLine(line, filename, lineNumber) {
line = line.trim();
// console.log("Line: " + line);
// empty line, produces nothing
if(line.length == 0) {
//console.log("-> Empty line.");
return [];
}
// comment, produces nothing
if(line[0] == "#") {
//console.log("-> Comment.")
return [];
}
var directiveMatch = line.match(/^.+?\b/);
if(directiveMatch == null) {
throw new Error("Parse error: no directive on line " + lineNumber + " in " + filename);
}
var directive = directiveMatch[0];
// char, produces a single entry in the char table
if(directive == "char") {
//console.log("-> Char.")
var parsedCharacter = "";
var parsedDots = [0, 0, 0, 0, 0, 0, 0, 0];
var match = line.match(/^char\s+([^\s]+)\s+([^#]+)/);
if(match == null) {
throw new Error("Parse error: unrecognized char directive syntax on line " + lineNumber + " in " + filename);
}
var character = match[1];
var dots = match[2];
// A backslash-prefixed special character
if(character[0] == "\\") {
var c = character[1];
if(c == "b") {
// backspace
parsedCharacter = "\b";
} else if(c == "f") {
// form feed
parsedCharacter = "\f";
} else if(c == "n") {
// line feed
parsedCharacter = "\n";
} else if(c == "o") {
// octal
parsedCharacter = String.fromCharCode(parseInt(character.substr(2), 8));
} else if(c == "r") {
// carriage return
parsedCharacter = "\r";
} else if(c == "s") {
// space
parsedCharacter = " ";
} else if(c == "t") {
parsedCharacter = "\t";
} else if(c == "u" || c == "U" || c == "x" || c == "X") {
// hex
parsedCharacter = String.fromCharCode(parseInt(character.substr(2), 16));
} else if(c == "v") {
parsedCharacter = "\v";
} else if(c == "#") {
parsedCharacter = "#";
} else if(c == "<") {
throw new Error("Parse error: Unicode names are not supported on line " + lineNumber
+ " in " + filename);
} else if(c == "\\") {
parsedCharacter = "\\";
} else {
throw new Error("Parse error: Unknown special character on line " + lineNumber + " in " + filename);
}
}
// Any single character other than a backslash or a white-space character
else {
parsedCharacter = character[0];
}
parsedDots[0] = Number(dots.indexOf("7") != -1);
parsedDots[1] = Number(dots.indexOf("3") != -1);
parsedDots[2] = Number(dots.indexOf("2") != -1);
parsedDots[3] = Number(dots.indexOf("1") != -1);
parsedDots[4] = Number(dots.indexOf("4") != -1);
parsedDots[5] = Number(dots.indexOf("5") != -1);
parsedDots[6] = Number(dots.indexOf("6") != -1);
parsedDots[7] = Number(dots.indexOf("8") != -1);
return [{character: parsedCharacter, code: parsedDots}];
}
// byte, produces nothing because no sensible char information can be extracted
if(directive == "byte") {
console.warn("Warning: byte directive ignored");
return []
}
// glyph, produces nothing as it may be used for mapping characters to braille only
if(directive == "glyph") {
return [];
}
// include, may produce another table
if(directive == "include") {
var includingFile = line.match(/^include ([^#]+)/)[1];
//console.log("Including file: " + includingFile);
return parseFile(includingFile, filename);
}
// unknown directive
throw new Error("Parse error: unknown directive on line " + lineNumber + " in " + filename);
}