diff --git a/toml/parser.ts b/toml/parser.ts index 9263f01112..d8acc608be 100644 --- a/toml/parser.ts +++ b/toml/parser.ts @@ -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}/; diff --git a/toml/parser_test.ts b/toml/parser_test.ts index cc66752960..104428b25e 100644 --- a/toml/parser_test.ts +++ b/toml/parser_test.ts @@ -149,7 +149,8 @@ test({ deno: "is", not: "[node]", regex: "", - NANI: "何?!" + NANI: "何?!", + comment: "Comment inside # the comment" }; const actual = parseFile(path.join(testFilesDir, "simple.toml")); assertEquals(actual, expected); diff --git a/toml/testdata/boolean.toml b/toml/testdata/boolean.toml index 92264888a2..242d29c969 100644 --- a/toml/testdata/boolean.toml +++ b/toml/testdata/boolean.toml @@ -1,3 +1,3 @@ -[boolean] +[boolean] # i hate comments bool1 = true bool2 = false \ No newline at end of file diff --git a/toml/testdata/cargo.toml b/toml/testdata/cargo.toml index 5402d10a4c..291aa7db6d 100644 --- a/toml/testdata/cargo.toml +++ b/toml/testdata/cargo.toml @@ -53,4 +53,4 @@ tokio-threadpool = "0.1.11" url = "1.7.2" [target.'cfg(windows)'.dependencies] -winapi = "0.3.6" \ No newline at end of file +winapi = "0.3.6" diff --git a/toml/testdata/datetime.toml b/toml/testdata/datetime.toml index b21924793c..62377a4ba2 100644 --- a/toml/testdata/datetime.toml +++ b/toml/testdata/datetime.toml @@ -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 diff --git a/toml/testdata/float.toml b/toml/testdata/float.toml index 92f0b6d17b..6a384179cf 100644 --- a/toml/testdata/float.toml +++ b/toml/testdata/float.toml @@ -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 diff --git a/toml/testdata/simple.toml b/toml/testdata/simple.toml index aac44714e9..f3f6c10365 100644 --- a/toml/testdata/simple.toml +++ b/toml/testdata/simple.toml @@ -1,4 +1,5 @@ deno = "is" not = "[node]" regex = '<\i\c*\s*>' -NANI = '何?!' \ No newline at end of file +NANI = '何?!' +comment = "Comment inside # the comment" # Comment