1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-26 09:10:40 -05:00

toml: fix parsing comments (#311)

This commit is contained in:
Vincent LE GOFF 2019-03-29 15:51:23 +01:00 committed by Ryan Dahl
parent fa1664e6cc
commit a48fdd6a29
7 changed files with 64 additions and 49 deletions

View file

@ -29,8 +29,9 @@ class Parser {
_sanitize(): void {
const out = [];
for (let i = 0; i < this.tomlLines.length; i++) {
const s = this.tomlLines[i].split("#")[0];
if (s !== "") {
const s = this.tomlLines[i];
const trimmed = s.trim();
if (trimmed !== "" && trimmed[0] !== "#") {
out.push(s);
}
}
@ -156,7 +157,7 @@ class Parser {
}
_isGroup(line: string): boolean {
const t = line.trim();
return t[0] === "[" && t[t.length - 1] === "]";
return t[0] === "[" && /\[(.*)\]/.exec(t) ? true : false;
}
_isDeclaration(line: string): boolean {
return line.split("=").length > 1;
@ -183,28 +184,50 @@ class Parser {
kv.value = this._parseData(line.slice(idx + 1));
return kv;
}
// TODO (zekth) Need refactor using ACC
_parseData(dataString: string): unknown {
dataString = dataString.trim();
if (this._isDate(dataString)) {
return new Date(dataString);
return new Date(dataString.split("#")[0].trim());
}
if (this._isLocalTime(dataString)) {
return eval(`"${dataString}"`);
return eval(`"${dataString.split("#")[0].trim()}"`);
}
if (dataString === "inf" || dataString === "+inf") {
const cut3 = dataString.substring(0, 3).toLowerCase();
const cut4 = dataString.substring(0, 4).toLowerCase();
if (cut3 === "inf" || cut4 === "+inf") {
return Infinity;
}
if (dataString === "-inf") {
if (cut4 === "-inf") {
return -Infinity;
}
if (
dataString === "nan" ||
dataString === "+nan" ||
dataString === "-nan"
) {
if (cut3 === "nan" || cut4 === "+nan" || cut4 === "-nan") {
return NaN;
}
// inline table
// If binary / octal / hex
const hex = /(0(?:x|o|b)[0-9a-f_]*)[^#]/gi.exec(dataString);
if (hex && hex[0]) {
return hex[0].trim();
}
const testNumber = this._isParsableNumber(dataString);
if (testNumber && !isNaN(testNumber as number)) {
return testNumber;
}
const invalidArr = /,\]/g.exec(dataString);
if (invalidArr) {
dataString = dataString.replace(/,]/g, "]");
}
const m = /(?:\'|\[|{|\").*(?:\'|\]|\"|})\s*[^#]/g.exec(dataString);
if (m) {
dataString = m[0].trim();
}
if (dataString[0] === "{" && dataString[dataString.length - 1] === "}") {
const reg = /([a-zA-Z0-9-_\.]*) (=)/gi;
let result;
@ -218,17 +241,6 @@ class Parser {
// TODO : unflat if necessary
return JSON.parse(dataString);
}
// If binary / octal / hex
if (
dataString[0] === "0" &&
(dataString[1] === "b" || dataString[1] === "o" || dataString[1] === "x")
) {
return dataString;
}
if (this._isParsableNumber(dataString)) {
return eval(dataString.replace(/_/g, ""));
}
// Handle First and last EOL for multiline strings
if (dataString.startsWith(`"\\n`)) {
@ -241,18 +253,19 @@ class Parser {
} else if (dataString.endsWith(`\\n'`)) {
dataString = dataString.replace(`\\n'`, `'`);
}
// dataString = dataString.replace(/\\/, "\\\\");
return eval(dataString);
}
_isLocalTime(str: string): boolean {
const reg = /(\d{2}):(\d{2}):(\d{2})/;
return reg.test(str);
}
_isParsableNumber(dataString: string): boolean {
let d = dataString.replace(/_/g, "");
return !isNaN(parseFloat(d));
_isParsableNumber(dataString: string): number | boolean {
const m = /((?:\+|-|)[0-9_\.e+\-]*)[^#]/i.exec(dataString.trim());
if (!m) {
return false;
} else {
return parseFloat(m[0].replace(/_/g, ""));
}
}
_isDate(dateStr: string): boolean {
const reg = /\d{4}-\d{2}-\d{2}/;

View file

@ -149,7 +149,8 @@ test({
deno: "is",
not: "[node]",
regex: "<ic*s*>",
NANI: "何?!"
NANI: "何?!",
comment: "Comment inside # the comment"
};
const actual = parseFile(path.join(testFilesDir, "simple.toml"));
assertEquals(actual, expected);

View file

@ -1,3 +1,3 @@
[boolean]
[boolean] # i hate comments
bool1 = true
bool2 = false

View file

@ -53,4 +53,4 @@ tokio-threadpool = "0.1.11"
url = "1.7.2"
[target.'cfg(windows)'.dependencies]
winapi = "0.3.6"
winapi = "0.3.6"

View file

@ -1,8 +1,8 @@
[datetime]
odt1 = 1979-05-27T07:32:00Z
odt2 = 1979-05-27T00:32:00-07:00
odt3 = 1979-05-27T00:32:00.999999-07:00
odt4 = 1979-05-27 07:32:00Z
ld1 = 1979-05-27
lt1 = 07:32:00
lt2 = 00:32:00.999999
odt1 = 1979-05-27T07:32:00Z # Comment
odt2 = 1979-05-27T00:32:00-07:00 # Comment
odt3 = 1979-05-27T00:32:00.999999-07:00 # Comment
odt4 = 1979-05-27 07:32:00Z # Comment
ld1 = 1979-05-27 # Comment
lt1 = 07:32:00 # Comment
lt2 = 00:32:00.999999 # Comment

View file

@ -1,17 +1,17 @@
[float]
# fractional
flt1 = +1.0
flt2 = 3.1415
flt3 = -0.01
flt1 = +1.0 # Comment
flt2 = 3.1415 # Comment
flt3 = -0.01 # Comment
# exponent
flt4 = 5e+22
flt5 = 1e6
flt6 = -2E-2
flt4 = 5e+22 # Comment
flt5 = 1e6 # Comment
flt6 = -2E-2 # Comment
# both
flt7 = 6.626e-34
flt8 = 224_617.445_991_228
flt7 = 6.626e-34 # Comment
flt8 = 224_617.445_991_228 # Comment
# infinity
sf1 = inf # positive infinity
sf2 = +inf # positive infinity

View file

@ -1,4 +1,5 @@
deno = "is"
not = "[node]"
regex = '<\i\c*\s*>'
NANI = '何?!'
NANI = '何?!'
comment = "Comment inside # the comment" # Comment