lib/edn/parser.rb in edn-0.9.1 vs lib/edn/parser.rb in edn-0.9.2

- old
+ new

@@ -1,7 +1,32 @@ require 'parslet' +class IgnoreParslet < Parslet::Atoms::Base + def initialize(parslet) + @parslet = parslet + end + def to_s_inner(prec) + @parslet.to_s(prec) + end + def try(source, context) + success, value = result = @parslet.try(source, context) + + return succ(nil) if success + return result + end +end + +module IgnoreDSL + def ignore + IgnoreParslet.new(self) + end +end + +class Parslet::Atoms::Base + include IgnoreDSL +end + module EDN class Parser < Parslet::Parser root(:top) rule(:top) { @@ -26,17 +51,18 @@ integer | symbol } rule(:tagged_value) { - tag >> space >> base_value.as(:value) + tag >> space? >> base_value.as(:value) } # Collections rule(:vector) { str('[') >> + space? >> top.repeat.as(:vector) >> space? >> str(']') } @@ -103,11 +129,11 @@ } # Parts rule(:tag) { - str('#') >> symbol.as(:tag) + str('#') >> match['[:alpha:]'].present? >> symbol.as(:tag) } rule(:symbol_chars) { (symbol_first_char >> valid_chars.repeat) | @@ -129,11 +155,21 @@ rule(:digit) { match['0-9'] } + rule(:newline) { str("\r").maybe >> str("\n") } + + rule(:comment) { + str(';') >> (newline.absent? >> any).repeat + } + + rule(:discard) { + str('#_') >> space? >> (tagged_value | base_value).ignore + } + rule(:space) { - match('[\s,]').repeat(1) + (discard | comment | match['\s,']).repeat(1) } rule(:space?) { space.maybe } end end