lib/figtree/parser.rb in figtree-1.0.1 vs lib/figtree/parser.rb in figtree-1.2.0

- old
+ new

@@ -1,39 +1,52 @@ require 'parslet' +require 'figtree/ip_rules' module Figtree # ConFIG into a Tree :) class Parser < Parslet::Parser + include IPv4 + include IPv6 + rule(:eof) { any.absent? } rule(:group_title) { match('[a-zA-Z]').repeat(1) } - rule(:newline) { match("\\n").repeat(1) >> match("\\r").maybe } - rule(:space) { match('\s').repeat(0) } + rule(:space) { match("\s").repeat(0) } + rule(:newline) { match("\n") >> match("\r").maybe } + rule(:grouper) do + newline.maybe >> str('[') >> group_title.as(:group_title) >> str(']') end rule(:comment) do - str(';') >> - (newline.absent? >> any).repeat + # comments go uncaptured + (str(';') >> + (newline.absent? >> any).repeat) >> + newline.maybe end rule(:string) do str('"') >> ((str('\\') >> any) | (str('"').absent? >> any)).repeat.as(:string) >> str('"') end rule(:boolean) do + # expand this check (str('no') | str('yes')).as(:boolean) end rule(:number) do match('[0-9]').repeat(1).as(:number) end + rule(:ip_address) do + (ipv4 | ipv6).as(:ip_address) + end + rule(:array) do (match('[a-zA-Z]').repeat(1) >> (str(',') >> match('[a-zA-Z]').repeat(1)).repeat.maybe).maybe.as(:array) >> (str(',') | newline | eof) @@ -57,11 +70,20 @@ rule(:assignment) do snake_case_key >> space >> str("=") >> space >> - (number | boolean | array | snake_case_key | file_path | string) + # this ordering matters + # we are roughly moving from more + # to less specific + (ip_address | + number | + boolean | + array | + snake_case_key | + file_path | + string) end rule(:override_assignment) do snakey_option_key >> space >> @@ -84,8 +106,14 @@ (grouper >> group_member.repeat.maybe).as(:group). repeat.maybe end - root(:group) + rule(:comment_or_group) do + comment.maybe >> + newline.maybe >> + group.maybe + end + + root(:comment_or_group) end end