Skip to content

Commit

Permalink
toml: fix parsing comments (#311)
Browse files Browse the repository at this point in the history
  • Loading branch information
zekth authored and ry committed Mar 29, 2019
1 parent fa1664e commit a48fdd6
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 49 deletions.
73 changes: 43 additions & 30 deletions toml/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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`)) {
Expand All @@ -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}/;
Expand Down
3 changes: 2 additions & 1 deletion toml/parser_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion toml/testdata/boolean.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[boolean]
[boolean] # i hate comments
bool1 = true
bool2 = false
2 changes: 1 addition & 1 deletion toml/testdata/cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
14 changes: 7 additions & 7 deletions toml/testdata/datetime.toml
Original file line number Diff line number Diff line change
@@ -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
16 changes: 8 additions & 8 deletions toml/testdata/float.toml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 2 additions & 1 deletion toml/testdata/simple.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
deno = "is"
not = "[node]"
regex = '<\i\c*\s*>'
NANI = '何?!'
NANI = '何?!'
comment = "Comment inside # the comment" # Comment

0 comments on commit a48fdd6

Please sign in to comment.