mirror of
https://github.com/denoland/deno.git
synced 2024-12-27 01:29:14 -05:00
toml: fix parsing comments (#311)
This commit is contained in:
parent
fa1664e6cc
commit
a48fdd6a29
7 changed files with 64 additions and 49 deletions
|
@ -29,8 +29,9 @@ class Parser {
|
||||||
_sanitize(): void {
|
_sanitize(): void {
|
||||||
const out = [];
|
const out = [];
|
||||||
for (let i = 0; i < this.tomlLines.length; i++) {
|
for (let i = 0; i < this.tomlLines.length; i++) {
|
||||||
const s = this.tomlLines[i].split("#")[0];
|
const s = this.tomlLines[i];
|
||||||
if (s !== "") {
|
const trimmed = s.trim();
|
||||||
|
if (trimmed !== "" && trimmed[0] !== "#") {
|
||||||
out.push(s);
|
out.push(s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -156,7 +157,7 @@ class Parser {
|
||||||
}
|
}
|
||||||
_isGroup(line: string): boolean {
|
_isGroup(line: string): boolean {
|
||||||
const t = line.trim();
|
const t = line.trim();
|
||||||
return t[0] === "[" && t[t.length - 1] === "]";
|
return t[0] === "[" && /\[(.*)\]/.exec(t) ? true : false;
|
||||||
}
|
}
|
||||||
_isDeclaration(line: string): boolean {
|
_isDeclaration(line: string): boolean {
|
||||||
return line.split("=").length > 1;
|
return line.split("=").length > 1;
|
||||||
|
@ -183,28 +184,50 @@ class Parser {
|
||||||
kv.value = this._parseData(line.slice(idx + 1));
|
kv.value = this._parseData(line.slice(idx + 1));
|
||||||
return kv;
|
return kv;
|
||||||
}
|
}
|
||||||
|
// TODO (zekth) Need refactor using ACC
|
||||||
_parseData(dataString: string): unknown {
|
_parseData(dataString: string): unknown {
|
||||||
dataString = dataString.trim();
|
dataString = dataString.trim();
|
||||||
|
|
||||||
if (this._isDate(dataString)) {
|
if (this._isDate(dataString)) {
|
||||||
return new Date(dataString);
|
return new Date(dataString.split("#")[0].trim());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this._isLocalTime(dataString)) {
|
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;
|
return Infinity;
|
||||||
}
|
}
|
||||||
if (dataString === "-inf") {
|
if (cut4 === "-inf") {
|
||||||
return -Infinity;
|
return -Infinity;
|
||||||
}
|
}
|
||||||
if (
|
|
||||||
dataString === "nan" ||
|
if (cut3 === "nan" || cut4 === "+nan" || cut4 === "-nan") {
|
||||||
dataString === "+nan" ||
|
|
||||||
dataString === "-nan"
|
|
||||||
) {
|
|
||||||
return 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] === "}") {
|
if (dataString[0] === "{" && dataString[dataString.length - 1] === "}") {
|
||||||
const reg = /([a-zA-Z0-9-_\.]*) (=)/gi;
|
const reg = /([a-zA-Z0-9-_\.]*) (=)/gi;
|
||||||
let result;
|
let result;
|
||||||
|
@ -218,17 +241,6 @@ class Parser {
|
||||||
// TODO : unflat if necessary
|
// TODO : unflat if necessary
|
||||||
return JSON.parse(dataString);
|
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
|
// Handle First and last EOL for multiline strings
|
||||||
if (dataString.startsWith(`"\\n`)) {
|
if (dataString.startsWith(`"\\n`)) {
|
||||||
|
@ -241,18 +253,19 @@ class Parser {
|
||||||
} else if (dataString.endsWith(`\\n'`)) {
|
} else if (dataString.endsWith(`\\n'`)) {
|
||||||
dataString = dataString.replace(`\\n'`, `'`);
|
dataString = dataString.replace(`\\n'`, `'`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// dataString = dataString.replace(/\\/, "\\\\");
|
|
||||||
|
|
||||||
return eval(dataString);
|
return eval(dataString);
|
||||||
}
|
}
|
||||||
_isLocalTime(str: string): boolean {
|
_isLocalTime(str: string): boolean {
|
||||||
const reg = /(\d{2}):(\d{2}):(\d{2})/;
|
const reg = /(\d{2}):(\d{2}):(\d{2})/;
|
||||||
return reg.test(str);
|
return reg.test(str);
|
||||||
}
|
}
|
||||||
_isParsableNumber(dataString: string): boolean {
|
_isParsableNumber(dataString: string): number | boolean {
|
||||||
let d = dataString.replace(/_/g, "");
|
const m = /((?:\+|-|)[0-9_\.e+\-]*)[^#]/i.exec(dataString.trim());
|
||||||
return !isNaN(parseFloat(d));
|
if (!m) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
return parseFloat(m[0].replace(/_/g, ""));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
_isDate(dateStr: string): boolean {
|
_isDate(dateStr: string): boolean {
|
||||||
const reg = /\d{4}-\d{2}-\d{2}/;
|
const reg = /\d{4}-\d{2}-\d{2}/;
|
||||||
|
|
|
@ -149,7 +149,8 @@ test({
|
||||||
deno: "is",
|
deno: "is",
|
||||||
not: "[node]",
|
not: "[node]",
|
||||||
regex: "<ic*s*>",
|
regex: "<ic*s*>",
|
||||||
NANI: "何?!"
|
NANI: "何?!",
|
||||||
|
comment: "Comment inside # the comment"
|
||||||
};
|
};
|
||||||
const actual = parseFile(path.join(testFilesDir, "simple.toml"));
|
const actual = parseFile(path.join(testFilesDir, "simple.toml"));
|
||||||
assertEquals(actual, expected);
|
assertEquals(actual, expected);
|
||||||
|
|
2
toml/testdata/boolean.toml
vendored
2
toml/testdata/boolean.toml
vendored
|
@ -1,3 +1,3 @@
|
||||||
[boolean]
|
[boolean] # i hate comments
|
||||||
bool1 = true
|
bool1 = true
|
||||||
bool2 = false
|
bool2 = false
|
2
toml/testdata/cargo.toml
vendored
2
toml/testdata/cargo.toml
vendored
|
@ -53,4 +53,4 @@ tokio-threadpool = "0.1.11"
|
||||||
url = "1.7.2"
|
url = "1.7.2"
|
||||||
|
|
||||||
[target.'cfg(windows)'.dependencies]
|
[target.'cfg(windows)'.dependencies]
|
||||||
winapi = "0.3.6"
|
winapi = "0.3.6"
|
||||||
|
|
14
toml/testdata/datetime.toml
vendored
14
toml/testdata/datetime.toml
vendored
|
@ -1,8 +1,8 @@
|
||||||
[datetime]
|
[datetime]
|
||||||
odt1 = 1979-05-27T07:32:00Z
|
odt1 = 1979-05-27T07:32:00Z # Comment
|
||||||
odt2 = 1979-05-27T00:32:00-07:00
|
odt2 = 1979-05-27T00:32:00-07:00 # Comment
|
||||||
odt3 = 1979-05-27T00:32:00.999999-07:00
|
odt3 = 1979-05-27T00:32:00.999999-07:00 # Comment
|
||||||
odt4 = 1979-05-27 07:32:00Z
|
odt4 = 1979-05-27 07:32:00Z # Comment
|
||||||
ld1 = 1979-05-27
|
ld1 = 1979-05-27 # Comment
|
||||||
lt1 = 07:32:00
|
lt1 = 07:32:00 # Comment
|
||||||
lt2 = 00:32:00.999999
|
lt2 = 00:32:00.999999 # Comment
|
||||||
|
|
16
toml/testdata/float.toml
vendored
16
toml/testdata/float.toml
vendored
|
@ -1,17 +1,17 @@
|
||||||
[float]
|
[float]
|
||||||
# fractional
|
# fractional
|
||||||
flt1 = +1.0
|
flt1 = +1.0 # Comment
|
||||||
flt2 = 3.1415
|
flt2 = 3.1415 # Comment
|
||||||
flt3 = -0.01
|
flt3 = -0.01 # Comment
|
||||||
|
|
||||||
# exponent
|
# exponent
|
||||||
flt4 = 5e+22
|
flt4 = 5e+22 # Comment
|
||||||
flt5 = 1e6
|
flt5 = 1e6 # Comment
|
||||||
flt6 = -2E-2
|
flt6 = -2E-2 # Comment
|
||||||
|
|
||||||
# both
|
# both
|
||||||
flt7 = 6.626e-34
|
flt7 = 6.626e-34 # Comment
|
||||||
flt8 = 224_617.445_991_228
|
flt8 = 224_617.445_991_228 # Comment
|
||||||
# infinity
|
# infinity
|
||||||
sf1 = inf # positive infinity
|
sf1 = inf # positive infinity
|
||||||
sf2 = +inf # positive infinity
|
sf2 = +inf # positive infinity
|
||||||
|
|
3
toml/testdata/simple.toml
vendored
3
toml/testdata/simple.toml
vendored
|
@ -1,4 +1,5 @@
|
||||||
deno = "is"
|
deno = "is"
|
||||||
not = "[node]"
|
not = "[node]"
|
||||||
regex = '<\i\c*\s*>'
|
regex = '<\i\c*\s*>'
|
||||||
NANI = '何?!'
|
NANI = '何?!'
|
||||||
|
comment = "Comment inside # the comment" # Comment
|
||||||
|
|
Loading…
Reference in a new issue