From 553f68fa7144179dbbb9391daff092fe6fd75da5 Mon Sep 17 00:00:00 2001 From: Riceball LEE Date: Mon, 21 Jun 2021 21:48:11 +0800 Subject: [PATCH 1/8] feat: add yaml.dump func --- spec/test_spec.lua | 22 ++++++++++++++++++++++ yaml.lua | 46 ++++++++++++++++++++++++++++++---------------- 2 files changed, 52 insertions(+), 16 deletions(-) diff --git a/spec/test_spec.lua b/spec/test_spec.lua index 16a5dd6..6c5cfcc 100644 --- a/spec/test_spec.lua +++ b/spec/test_spec.lua @@ -33,3 +33,25 @@ describe('Parsing in', function() end) end end) + +describe('dump', function() + local expected = { + total = 3, + title = "very good!", + list = { + {item = "good", id = 3}, + {item = "item2", id = 4, sub = { + no = 34, + te = "haha", + it = { + "this is false", + "aother", + {some=1, at=4} + } + }} + } + } + local str = yaml.dump(expected) + local data = yaml.eval(str) + assert.are.same(expected, data) +end) diff --git a/yaml.lua b/yaml.lua index 55211dc..6d4e1e8 100644 --- a/yaml.lua +++ b/yaml.lua @@ -10,33 +10,32 @@ table_print_value = function(value, indent, done) list[#list + 1] = key end table.sort(list, function(a, b) return tostring(a) < tostring(b) end) - local last = list[#list] + -- local last = list[#list] - local rep = "{\n" - local comma + local rep = "\n" + -- local comma for _, key in ipairs (list) do - if key == last then - comma = '' - else - comma = ',' - end + -- if key == last then + -- comma = '' + -- else + -- comma = ',' + -- end local keyRep if type(key) == "number" then - keyRep = key + keyRep = "- " else - keyRep = string.format("%q", tostring(key)) + keyRep = tostring(key)..": " -- string.format("%q", tostring(key)) end rep = rep .. string.format( - "%s[%s] = %s%s\n", - string.rep(" ", indent + 2), + "%s%s%s\n", + string.rep(" ", indent), keyRep, - table_print_value(value[key], indent + 2, done), - comma + table_print_value(value[key], indent + 2, done) ) end rep = rep .. string.rep(" ", indent) -- indent it - rep = rep .. "}" + -- rep = rep .. "}" done[value] = false return rep @@ -47,8 +46,23 @@ table_print_value = function(value, indent, done) end end +-- local dump = require("pl.pretty").dump +local function trimBlankLine(str) + local lines = {} + for s in str:gmatch("[^\r\n]+") do + s = string.gsub(s, "%s+$", "") + print(#s.."|"..s) + if #s > 0 then + table.insert(lines, s) + end + end + return table.concat(lines, "\n") +end + local table_print = function(tt) - print('return '..table_print_value(tt)) + local result = table_print_value(tt) + result = trimBlankLine(result) + return result end local table_clone = function(t) From 5b480a1030f831bf3f58e463d75bdf1226b85965 Mon Sep 17 00:00:00 2001 From: Riceball LEE Date: Sun, 18 Jul 2021 10:45:12 +0800 Subject: [PATCH 2/8] fix: remove debug print info --- yaml.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/yaml.lua b/yaml.lua index 6d4e1e8..b1ae006 100644 --- a/yaml.lua +++ b/yaml.lua @@ -51,7 +51,7 @@ local function trimBlankLine(str) local lines = {} for s in str:gmatch("[^\r\n]+") do s = string.gsub(s, "%s+$", "") - print(#s.."|"..s) + -- print(#s.."|"..s) if #s > 0 then table.insert(lines, s) end From 70df17e3f3ffd2817a81fb022319b725e3ae8e1d Mon Sep 17 00:00:00 2001 From: Riceball LEE Date: Thu, 28 Jul 2022 09:46:47 +0800 Subject: [PATCH 3/8] fix: the empty yaml file parse error attempt to index field 'token' (a nil value) --- samples/empty.lua | 2 ++ samples/empty.yaml | 0 spec/test_spec.lua | 7 ++++++- yaml.lua | 48 ++++++++++++++++++++++++---------------------- 4 files changed, 33 insertions(+), 24 deletions(-) create mode 100644 samples/empty.lua create mode 100644 samples/empty.yaml diff --git a/samples/empty.lua b/samples/empty.lua new file mode 100644 index 0000000..6ad1635 --- /dev/null +++ b/samples/empty.lua @@ -0,0 +1,2 @@ +-- can not return nil, so use 0 instead. +return 0 diff --git a/samples/empty.yaml b/samples/empty.yaml new file mode 100644 index 0000000..e69de29 diff --git a/spec/test_spec.lua b/spec/test_spec.lua index 6c5cfcc..32653e9 100644 --- a/spec/test_spec.lua +++ b/spec/test_spec.lua @@ -29,7 +29,12 @@ describe('Parsing in', function() it(file, function() local data = yaml.eval(readAll("samples/"..file..".yaml")) local answer = require("samples."..file) - assert.are.same(answer, data) + print(file, answer) + if answer == 0 then + assert.is.Nil(data) + else + assert.are.same(answer, data) + end end) end end) diff --git a/yaml.lua b/yaml.lua index b1ae006..55a8e30 100644 --- a/yaml.lua +++ b/yaml.lua @@ -329,29 +329,31 @@ Parser.parse = function (self) } push(self.parse_stack, c) - if c.token[1] == "doc" then - result = self:parseDoc() - elseif c.token[1] == "-" then - result = self:parseList() - elseif c.token[1] == "{" then - result = self:parseInlineHash() - elseif c.token[1] == "[" then - result = self:parseInlineList() - elseif c.token[1] == "id" then - result = self:parseHash() - elseif c.token[1] == "string" then - result = self:parseString("\n") - elseif c.token[1] == "timestamp" then - result = self:parseTimestamp() - elseif c.token[1] == "number" then - result = tonumber(self:advanceValue()) - elseif c.token[1] == "pipe" then - result = self:parsePipe() - elseif c.token.const == true then - self:advanceValue(); - result = c.token.value - else - error("ParseError: unexpected token '" .. c.token[1] .. "'" .. context(c.token.input)) + if c.token then + if c.token[1] == "doc" then + result = self:parseDoc() + elseif c.token[1] == "-" then + result = self:parseList() + elseif c.token[1] == "{" then + result = self:parseInlineHash() + elseif c.token[1] == "[" then + result = self:parseInlineList() + elseif c.token[1] == "id" then + result = self:parseHash() + elseif c.token[1] == "string" then + result = self:parseString("\n") + elseif c.token[1] == "timestamp" then + result = self:parseTimestamp() + elseif c.token[1] == "number" then + result = tonumber(self:advanceValue()) + elseif c.token[1] == "pipe" then + result = self:parsePipe() + elseif c.token.const == true then + self:advanceValue(); + result = c.token.value + else + error("ParseError: unexpected token '" .. c.token[1] .. "'" .. context(c.token.input)) + end end pop(self.parse_stack) From e4bee338cb0589e2a9365f7129a9cc580f977132 Mon Sep 17 00:00:00 2001 From: Riceball LEE Date: Thu, 28 Jul 2022 09:57:33 +0800 Subject: [PATCH 4/8] chore: remove debug print --- spec/test_spec.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/spec/test_spec.lua b/spec/test_spec.lua index 32653e9..954475d 100644 --- a/spec/test_spec.lua +++ b/spec/test_spec.lua @@ -29,7 +29,6 @@ describe('Parsing in', function() it(file, function() local data = yaml.eval(readAll("samples/"..file..".yaml")) local answer = require("samples."..file) - print(file, answer) if answer == 0 then assert.is.Nil(data) else From ea19fe8b3765719030928eaafb689c1511e01dc5 Mon Sep 17 00:00:00 2001 From: PavelK <2me@pavelk.ru> Date: Mon, 30 Jan 2023 05:48:30 +0300 Subject: [PATCH 5/8] feat(yaml.lua): Add support comments feat(yaml.lua): Add support check YAML version tag feat(yaml.lua): dump to yaml (author @snowyu, PR #12) fix(yaml.lua): negative numbers as properties should not parse as a list (author @FreeMasen, PR #11) fix(yaml.lua): timestamp timezone fix(yaml.lua): only parsing first block hash with indent==0 fix(yaml.lua): parsing block after empty row feat(samples): Check comments fix(samples): timezone feat(samples): Check negative numbers as properties --- samples/array-member.lua | 23 +++++++ samples/array-member.yaml | 11 +++ samples/comments.lua | 12 ++++ samples/comments.yaml | 17 +++++ samples/dates.lua | 12 ++-- samples/hash.yaml | 47 ++++++++----- samples/invoice.lua | 2 +- samples/log.lua | 2 +- samples/log2.lua | 2 +- samples/log3.lua | 2 +- samples/log4.lua | 2 +- samples/negative-prop.lua | 4 ++ samples/negative-prop.yaml | 3 + yaml.lua | 137 +++++++++++++++++++++---------------- 14 files changed, 187 insertions(+), 89 deletions(-) create mode 100644 samples/array-member.lua create mode 100644 samples/array-member.yaml create mode 100644 samples/comments.lua create mode 100644 samples/comments.yaml create mode 100644 samples/negative-prop.lua create mode 100644 samples/negative-prop.yaml diff --git a/samples/array-member.lua b/samples/array-member.lua new file mode 100644 index 0000000..48304a4 --- /dev/null +++ b/samples/array-member.lua @@ -0,0 +1,23 @@ +return { + ["arr"] = { + [1] = { + ["prop1"] = { + ["subprop1"] = "test", + ["subprop2"] = { + ["test2"] = "test", + ["test3"] = {} + } + } + }, + [2] = { + ["prop2"] = { + ["subprop3"] = "test.test" + } + }, + [3] = { + ["prop3"] = { + ["subprop4"] = 5 + } + } + } +} diff --git a/samples/array-member.yaml b/samples/array-member.yaml new file mode 100644 index 0000000..fdb6533 --- /dev/null +++ b/samples/array-member.yaml @@ -0,0 +1,11 @@ +--- +arr: + - prop1: + subprop1: test + subprop2: + test2: test + test3: {} + - prop2: + subprop3: test.test + - prop3: + subprop4: 5 \ No newline at end of file diff --git a/samples/comments.lua b/samples/comments.lua new file mode 100644 index 0000000..421d5da --- /dev/null +++ b/samples/comments.lua @@ -0,0 +1,12 @@ +return { + ["key"] = {"value line 1", "value line 2", "value line 3"}, + ["key2"] = 30, + ["key3"] = "String value", + ["key4"] = "#not a comment", + ["key5"] = false, + ["key6"] = 123, + ["key7"] = { + ["key8"] = "value", + ["key9"] = "not#comments" + } +} diff --git a/samples/comments.yaml b/samples/comments.yaml new file mode 100644 index 0000000..74600a2 --- /dev/null +++ b/samples/comments.yaml @@ -0,0 +1,17 @@ +--- +key: #comment after id + - value line 1 + #comment before list item + - value line 2 +#comment + #multi + #line + - value line 3 #Comment after list item +key2: 30 # comment 5 +key3: "String value" # comment after string with quotes +key4: "#not a comment" +key5: false # Comment boolean +key6: 123 # Comment number +key7: + key8: value # Comment simple string + key9: not#comments #Comment simple string with # inside and multiple # \ No newline at end of file diff --git a/samples/dates.lua b/samples/dates.lua index 5a0a790..00b23c9 100644 --- a/samples/dates.lua +++ b/samples/dates.lua @@ -1,8 +1,8 @@ return { - ["date0"] = 1305907200, - ["date1"] = 1305907200, - ["date2"] = 1293897600, - ["date3"] = 1298282160, - ["date4"] = 1303335300, - ["date5"] = 1307018730 + ["date0"] = os.time{year=2011, month=5, day=21, hour=0, min=0, sec=0, isdst=false}, + ["date1"] = os.time{year=2011, month=5, day=21, hour=0, min=0, sec=0, isdst=false}, + ["date2"] = os.time{year=2011, month=1, day=2, hour=0, min=0, sec=0, isdst=false}, + ["date3"] = os.time{year=2011, month=2, day=21, hour=17, min=56, sec=0, isdst=false}, + ["date4"] = os.time{year=2011, month=4, day=21, hour=5, min=35, sec=0, isdst=false}, + ["date5"] = os.time{year=2011, month=6, day=2, hour=20, min=45, sec=30, isdst=false} } diff --git a/samples/hash.yaml b/samples/hash.yaml index c45447c..5c74e62 100644 --- a/samples/hash.yaml +++ b/samples/hash.yaml @@ -1,20 +1,29 @@ --- - users: - tj: - name: tj - age: 23 - email: 'tj@vision-media.ca' - bob: - name: 'bob' - age: 27 - ted: { name: ted, age: 32, email: ted@tedtalks.com } - country: - name: Österreich - website: http://en.wikipedia.org/wiki/Austria - space: - description: space, the final frontier - brackets: - square: Square [brackets] can go in the middle of strings - squiggle: Squiggle {brackets} can also go in the middle of strings! - extrasquare: [Scratch that] brackets can go at the beginning as long as they close and have text after. - extrasquiggle: {Scratch that} squigs can go at the beginning also! +users: + tj: + name: tj + age: 23 + email: 'tj@vision-media.ca' + bob: + name: 'bob' + age: 27 + ted: { name: ted, age: 32, email: ted@tedtalks.com } + + + +country: + name: Österreich + website: http://en.wikipedia.org/wiki/Austria + +space: + description: space, the final frontier + +brackets: + square: Square [brackets] can go in the middle of strings + + + squiggle: Squiggle {brackets} can also go in the middle of strings! + + extrasquare: [Scratch that] brackets can go at the beginning as long as they close and have text after. + + extrasquiggle: {Scratch that} squigs can go at the beginning also! diff --git a/samples/invoice.lua b/samples/invoice.lua index 99c8c5a..09e6d12 100644 --- a/samples/invoice.lua +++ b/samples/invoice.lua @@ -10,7 +10,7 @@ Suite #292", ["family"] = "Dumars", ["given"] = "Chris" }, - ["date"] = 980179200, + ["date"] = os.time{year=2001, month=1, day=23, hour=0, min=0, sec=0, isdst=false}, ["invoice"] = 34843, ["ship-to"] = { ["address"] = { diff --git a/samples/log.lua b/samples/log.lua index 6ea27a6..51603ed 100644 --- a/samples/log.lua +++ b/samples/log.lua @@ -1,5 +1,5 @@ return { - ["Time"] = 1006498902, + ["Time"] = os.time{year=2001, month=11, day=23, hour=10, min=1, sec=42, isdst=false} , ["User"] = "ed", ["Warning"] = "This is an error message\ for the log file" diff --git a/samples/log2.lua b/samples/log2.lua index b5fbe08..576462f 100644 --- a/samples/log2.lua +++ b/samples/log2.lua @@ -1,5 +1,5 @@ return { - ["Time"] = 1006498951, + ["Time"] = os.time{year=2001, month=11, day=23, hour=10, min=2, sec=31, isdst=false}, ["User"] = "ed", ["Warning"] = "A slightly different error\ message." diff --git a/samples/log3.lua b/samples/log3.lua index d2d88a1..2e6b37c 100644 --- a/samples/log3.lua +++ b/samples/log3.lua @@ -1,5 +1,5 @@ return { - ["Date"] = 1006498997, + ["Date"] = os.time{year=2001, month=11, day=23, hour=10, min=3, sec=17, isdst=false}, ["Fatal"] = "Unknown variable \"bar\"", ["Stack"] = { [1] = { diff --git a/samples/log4.lua b/samples/log4.lua index f5f0022..79ff713 100644 --- a/samples/log4.lua +++ b/samples/log4.lua @@ -1,5 +1,5 @@ return { - ["Date"] = 1006498997, + ["Date"] = os.time{year=2001, month=11, day=23, hour=10, min=3, sec=17, isdst=false}, ["Fatal"] = "Unknown variable \"bar\"", ["Stack"] = { [1] = { diff --git a/samples/negative-prop.lua b/samples/negative-prop.lua new file mode 100644 index 0000000..cfa03de --- /dev/null +++ b/samples/negative-prop.lua @@ -0,0 +1,4 @@ +return { + prop1 = -1, + prop2 = -1.5, +} \ No newline at end of file diff --git a/samples/negative-prop.yaml b/samples/negative-prop.yaml new file mode 100644 index 0000000..f9922f5 --- /dev/null +++ b/samples/negative-prop.yaml @@ -0,0 +1,3 @@ +--- +prop1: -1 +prop2: -1.5 \ No newline at end of file diff --git a/yaml.lua b/yaml.lua index 55a8e30..2f9bc07 100644 --- a/yaml.lua +++ b/yaml.lua @@ -1,43 +1,45 @@ -local table_print_value -table_print_value = function(value, indent, done) - indent = indent or 0 - done = done or {} - if type(value) == "table" and not done [value] then - done [value] = true - - local list = {} +---yaml.lua +--LUA YAML parser, based on js-lua, fast and tiny. +--Although this implementation does not currently support the entire YAML specification, +--feel free to fork the project and submit a patch :) +-- https://github.com/exosite/lua-yaml + +local Generator = {} +function Generator.new (self) + return self +end + +Generator.tablePrintValue =function (self, value, indent, done) + indent =indent or 0 + done =done or {} + if type(value)=="table" and not done [value] then + done [value] =true + + local list ={} for key in pairs (value) do - list[#list + 1] = key + list[#list+1] =key end table.sort(list, function(a, b) return tostring(a) < tostring(b) end) - -- local last = list[#list] - local rep = "\n" - -- local comma + local rep ="\n" for _, key in ipairs (list) do - -- if key == last then - -- comma = '' - -- else - -- comma = ',' - -- end local keyRep - if type(key) == "number" then - keyRep = "- " + if type(key)=="number" then + keyRep ="- " else - keyRep = tostring(key)..": " -- string.format("%q", tostring(key)) + keyRep =tostring(key)..": " end - rep = rep .. string.format( + rep = rep..string.format( "%s%s%s\n", string.rep(" ", indent), keyRep, - table_print_value(value[key], indent + 2, done) + self:tablePrintValue(value[key], indent+2, done) ) end - rep = rep .. string.rep(" ", indent) -- indent it - -- rep = rep .. "}" + rep = rep..string.rep(" ", indent) - done[value] = false + done[value] =false return rep elseif type(value) == "string" then return string.format("%q", value) @@ -46,12 +48,10 @@ table_print_value = function(value, indent, done) end end --- local dump = require("pl.pretty").dump local function trimBlankLine(str) local lines = {} for s in str:gmatch("[^\r\n]+") do s = string.gsub(s, "%s+$", "") - -- print(#s.."|"..s) if #s > 0 then table.insert(lines, s) end @@ -59,11 +59,6 @@ local function trimBlankLine(str) return table.concat(lines, "\n") end -local table_print = function(tt) - local result = table_print_value(tt) - result = trimBlankLine(result) - return result -end local table_clone = function(t) local clone = {} @@ -106,13 +101,14 @@ function Parser.new (self, tokens) return self end -local exports = {version = "1.2"} +local exports = {version = "1.3"} local word = function(w) return "^("..w..")([%s$%c])" end local tokens = { - {"comment", "^#[^\n]*"}, - {"indent", "^\n( *)"}, + {"yaml-version", "^%%YAML ([0-9%.]*)"}, + {"comment", "^[\n ]+#[^\n]*"}, + {"indent", "^[\n]+( *)"}, {"space", "^ +"}, {"true", word("enabled"), const = true, value = true}, {"true", word("true"), const = true, value = true}, @@ -141,17 +137,22 @@ local tokens = { {"string", "^%b{} *[^,%c]+", noinline = true}, {"{", "^{"}, {"}", "^}"}, - {"string", "^%b[] *[^,%c]+", noinline = true}, + -- Any letter/punctuation after '[...]', before '#' is string (after '#' comment) + {"string", "^(%b[] *[^%c #]+[^%c]-) #[^\n]*", noinline = true}, + {"string", "^(%b[] *[^%c #]+[^%c]+)", noinline = true}, -- Any letter/punctuation after '[...]' is string {"[", "^%["}, {"]", "^%]"}, + {"string", "^-[^%s]+", noinline = true}, {"-", "^%-", noinline = true}, {":", "^:"}, {"pipe", "^(|)(%d*[+%-]?)", sep = "\n"}, {"pipe", "^(>)(%d*[+%-]?)", sep = " "}, {"id", "^([%w][%w %-_]*)(:[%s%c])"}, - {"string", "^[^%c]+", noinline = true}, + {"string", "^([^%c]-)( #)[^\n]+", noinline = true}, --String with " #comment" + {"string", "^([^%c]+)", noinline = true}, {"string", "^[^,%]}%c ]+"} }; + exports.tokenize = function (str) local token local row = 0 @@ -171,13 +172,12 @@ exports.tokenize = function (str) end if #captures > 0 then - captures.input = str:sub(0, 25) + captures.input = str:sub(0, 50) token = table_clone(tokens[i]) token[2] = captures local str2 = str:gsub(tokens[i][2], "", 1) token.raw = str:sub(1, #str - #str2) str = str2 - if token[1] == "{" or token[1] == "[" then inline = true elseif token.const then @@ -191,6 +191,7 @@ exports.tokenize = function (str) -- Trim token[2][1] = string_trim(token[2][1]) elseif token[1] == "string" then + token[2][1] =string_trim(token[2][1]) -- Finding numbers local snip = token[2][1] if not token.force_text then @@ -198,9 +199,8 @@ exports.tokenize = function (str) token[1] = "number" end end - elseif token[1] == "comment" then - ignore = true; + ignore = true elseif token[1] == "indent" then row = row + 1 inline = false @@ -208,7 +208,6 @@ exports.tokenize = function (str) if indentAmount == 0 then indentAmount = #token[2][1] end - if indentAmount ~= 0 then indents = (#token[2][1] / indentAmount); else @@ -330,7 +329,9 @@ Parser.parse = function (self) push(self.parse_stack, c) if c.token then - if c.token[1] == "doc" then + if c.token[1] == "yaml-version" then + result = self:parseYamlVersion() + elseif c.token[1] == "doc" then result = self:parseDoc() elseif c.token[1] == "-" then result = self:parseList() @@ -366,9 +367,19 @@ Parser.parse = function (self) if ref then self.refs[ref] = result end + return result end +Parser.parseYamlVersion = function (self) + local version = tonumber(self:advanceValue()) + if version~=1.1 then + error("Unsupported YAML protocol version, has: "..version..", need: 1.1") + end + self:accept("yaml-version") + return self:parse() +end + Parser.parseDoc = function (self) self:accept("doc") return self:parse() @@ -383,10 +394,14 @@ Parser.inline = function (self) local inline = {} local i = 0 - while self:peek(i) and not self:peekType("indent", i) and current.row == self:peek(i).row do + while + self:peek(i) and not self:peekType("indent", i) and not self:peekType("doc", i) + and current.row == self:peek(i).row + do inline[self:peek(i)[1]] = true i = i - 1 end + return inline, -i end @@ -484,7 +499,6 @@ end Parser.parseHash = function (self, hash) hash = hash or {} local indents = 0 - if self:isInline() then local id = self:advanceValue() self:expect(":", "expected semi-colon after id") @@ -503,7 +517,7 @@ Parser.parseHash = function (self, hash) while self:peekType("id") do local id = self:advanceValue() - self:expect(":","expected semi-colon after id") + self:expect(":", "expected semi-colon after id") self:ignoreSpace() hash[id] = self:parse() self:ignoreSpace(); @@ -576,23 +590,28 @@ Parser.parseInlineList = function (self) end Parser.parseTimestamp = function (self) - local capture = self:advance()[2] - + local capture =self:advance()[2] return os.time{ - year = capture[1], - month = capture[2], - day = capture[3], - hour = capture[4] or 0, - min = capture[5] or 0, - sec = capture[6] or 0, - isdst = false, - } - os.time{year=1970, month=1, day=1, hour=8} + year =capture[1], + month =capture[2], + day =capture[3], + hour =(capture[4] or 0) + (capture[7] or 0), + min =(capture[5] or 0) + (capture[8] or 0), + sec =capture[6] or 0, + isdst =false, + } end -exports.eval = function (str) - return Parser:new(exports.tokenize(str)):parse() +exports.eval = function(str) + local parser = Parser:new(exports.tokenize(str)) + return parser:parse() end -exports.dump = table_print +exports.dump = function(table) + local generator =Generator:new() + local result =generator:tablePrintValue(table) + result =trimBlankLine(result) + return result +end return exports From c3bf357e98e25593761ab799da87dfc3b2e7e2d9 Mon Sep 17 00:00:00 2001 From: PavelK <2me@pavelk.ru> Date: Wed, 1 Feb 2023 22:05:52 +0300 Subject: [PATCH 6/8] fix(liblua): Numbers 123mb fix(liblua): Inline arrah/hash comments --- samples/comments.lua | 10 +++++++++- samples/comments.yaml | 11 ++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/samples/comments.lua b/samples/comments.lua index 421d5da..901a3f8 100644 --- a/samples/comments.lua +++ b/samples/comments.lua @@ -8,5 +8,13 @@ return { ["key7"] = { ["key8"] = "value", ["key9"] = "not#comments" - } + }, + ["key10"] = {1, 2}, + ["key11"] = "[3, 4] it is another string", + ["key12"] = "[5, 6] StrinG", + ["key13"] = {7, 8}, + ["key14"] = "[9, 10] StrinG", + ["key15"] = "[11, 12] StrinG# notcomment", + ["key16"] = {13, 14}, + ["key17"] = "[15, 16] string" } diff --git a/samples/comments.yaml b/samples/comments.yaml index 74600a2..4566861 100644 --- a/samples/comments.yaml +++ b/samples/comments.yaml @@ -14,4 +14,13 @@ key5: false # Comment boolean key6: 123 # Comment number key7: key8: value # Comment simple string - key9: not#comments #Comment simple string with # inside and multiple # \ No newline at end of file + key9: not#comments #Comment simple string with # inside and multiple # + +key10: [1, 2] +key11: [3, 4] it is another string # and after comment +key12: [5, 6] StrinG # Another language comment: кирилица ёпрст +key13: [7, 8] +key14: [9, 10] StrinG # Comment with spaces and #more #more # comment +key15: [11, 12] StrinG# notcomment # comment +key16: [13, 14] #comment +key17: [15, 16] string \ No newline at end of file From 1d549aee1d7018ab9abe850ad084e27a7f775b2d Mon Sep 17 00:00:00 2001 From: Riceball LEE Date: Sat, 24 Jun 2023 10:41:11 +0800 Subject: [PATCH 7/8] fix: can not parse if the first line is comments --- samples/comments-first.lua | 3 +++ samples/comments-first.yaml | 2 ++ yaml.lua | 2 +- 3 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 samples/comments-first.lua create mode 100644 samples/comments-first.yaml diff --git a/samples/comments-first.lua b/samples/comments-first.lua new file mode 100644 index 0000000..201375a --- /dev/null +++ b/samples/comments-first.lua @@ -0,0 +1,3 @@ +return { + ["number"] = 123, +} diff --git a/samples/comments-first.yaml b/samples/comments-first.yaml new file mode 100644 index 0000000..9ab2619 --- /dev/null +++ b/samples/comments-first.yaml @@ -0,0 +1,2 @@ +#first line is comment +number: 123 diff --git a/yaml.lua b/yaml.lua index 2f9bc07..bc85f89 100644 --- a/yaml.lua +++ b/yaml.lua @@ -107,7 +107,7 @@ local word = function(w) return "^("..w..")([%s$%c])" end local tokens = { {"yaml-version", "^%%YAML ([0-9%.]*)"}, - {"comment", "^[\n ]+#[^\n]*"}, + {"comment", "^[\n ]*#[^\n]*"}, {"indent", "^[\n]+( *)"}, {"space", "^ +"}, {"true", word("enabled"), const = true, value = true}, From e9f2a45b02913147d43c8625c8255457fc3927ce Mon Sep 17 00:00:00 2001 From: Riceball LEE Date: Tue, 27 Jun 2023 09:27:04 +0800 Subject: [PATCH 8/8] fix(dump): dump empty table value error --- spec/test_spec.lua | 3 ++- yaml.lua | 27 ++++++++++++++------------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/spec/test_spec.lua b/spec/test_spec.lua index 954475d..a27fe89 100644 --- a/spec/test_spec.lua +++ b/spec/test_spec.lua @@ -52,7 +52,8 @@ describe('dump', function() "aother", {some=1, at=4} } - }} + }}, + {b = {}}, } } local str = yaml.dump(expected) diff --git a/yaml.lua b/yaml.lua index bc85f89..2871a6b 100644 --- a/yaml.lua +++ b/yaml.lua @@ -10,24 +10,25 @@ function Generator.new (self) end Generator.tablePrintValue =function (self, value, indent, done) - indent =indent or 0 - done =done or {} - if type(value)=="table" and not done [value] then - done [value] =true + indent = indent or 0 + done = done or {} + if type(value) =="table" and not done[value] then + done[value] = true + if next(value) == nil then return '{}' end - local list ={} + local list = {} for key in pairs (value) do - list[#list+1] =key + list[#list+1] = key end table.sort(list, function(a, b) return tostring(a) < tostring(b) end) local rep ="\n" - for _, key in ipairs (list) do + for _, key in ipairs(list) do local keyRep - if type(key)=="number" then - keyRep ="- " + if type(key) =="number" then + keyRep = "- " else - keyRep =tostring(key)..": " + keyRep = tostring(key)..": " end rep = rep..string.format( "%s%s%s\n", @@ -608,9 +609,9 @@ exports.eval = function(str) end exports.dump = function(table) - local generator =Generator:new() - local result =generator:tablePrintValue(table) - result =trimBlankLine(result) + local generator = Generator:new() + local result = generator:tablePrintValue(table) + result = trimBlankLine(result) return result end