lib/dolos/parsers.rb in dolos-0.2.1 vs lib/dolos/parsers.rb in dolos-0.3.0

- old
+ new

@@ -1,9 +1,14 @@ # frozen_string_literal: true module Dolos module Parsers + + # String parser + # Matches exactly the given string + # string('hello').run('hello') => Success.new('hello', 5) + # Alias: c, for case-sensitive. Ex: c('hello').run('hello') => Success.new('hello', 5) def string(str) utf8_str = str.encode('UTF-8') Parser.new do |state| state.input.mark_offset @@ -19,13 +24,16 @@ state ) end end end - alias_method :c, :string + # Regex parser + # Accepts a regex, matches the regex against the input + # parser = regex(/\d+/) + # result = parser.run('123') # => Success.new('123', 3) def regex(pattern) Parser.new do |state| state.input.mark_offset if (matched_string = state.input.matches_regex?(pattern)) Success.new(matched_string, matched_string.bytesize) @@ -39,10 +47,12 @@ ) end end end + # Matches any character + # any_char.run('a') # => Success.new('a', 1) def any_char Parser.new do |state| state.input.mark_offset char, = state.input.peek(1) @@ -60,9 +70,10 @@ end end end # Matches any character in a string + # Passed string can be imagined as a set of characters # Example: # char_in('abc').run('b') # => Success.new('b', 1) def char_in(characters_string) characters_set = characters_string.chars