module Paco::Combinators::Char

Public Instance Methods

any_char() click to toggle source

Returns a parser that consumes and returns the next character of the input. @return [Paco::Parser]

# File lib/paco/combinators/char.rb, line 86
def any_char
  memoize { satisfy("any_char") { |ch| ch.length > 0 } }
end
cr() click to toggle source

Returns a parser that checks for the “carriage return” (‘r`) character. @return [Paco::Parser]

# File lib/paco/combinators/char.rb, line 123
def cr
  memoize { string("\r") }
end
crlf() click to toggle source

Returns a parser that checks for the “carriage return” character followed by the “line feed” character (‘rn`). @return [Paco::Parser]

# File lib/paco/combinators/char.rb, line 135
def crlf
  memoize { string("\r\n") }
end
digit() click to toggle source

Alias for ‘Paco::Combinators.regexp_char(//)`. @return [Paco::Parser]

# File lib/paco/combinators/char.rb, line 171
def digit
  memoize { regexp_char(/[0-9]/) }
end
digits() click to toggle source

Alias for ‘Paco::Combinators.regexp(/+/)`. @return [Paco::Parser]

# File lib/paco/combinators/char.rb, line 177
def digits
  memoize { regexp(/[0-9]+/) }
end
end_of_line() click to toggle source

Returns a parser that will match any kind of line ending including end of file. @return [Paco::Parser]

# File lib/paco/combinators/char.rb, line 147
def end_of_line
  memoize { alt(newline, eof) }
end
eof() click to toggle source

Returns a parser that matches end of file and returns nil. @return [Paco::Parser]

# File lib/paco/combinators/char.rb, line 112
def eof
  memoize do
    Parser.new("end of file") do |ctx, parser|
      parser.failure(ctx) unless ctx.eof?
      nil
    end
  end
end
letter() click to toggle source

Alias for ‘Paco::Combinators.regexp_char(//i)`. @return [Paco::Parser]

# File lib/paco/combinators/char.rb, line 153
def letter
  memoize { regexp_char(/[a-z]/i) }
end
letters() click to toggle source

Alias for ‘Paco::Combinators.regexp(/+/i)`. @return [Paco::Parser]

# File lib/paco/combinators/char.rb, line 159
def letters
  memoize { regexp(/[a-z]+/i) }
end
lf() click to toggle source

Returns a parser that checks for the “line feed” (‘n`) character. @return [Paco::Parser]

# File lib/paco/combinators/char.rb, line 129
def lf
  memoize { string("\n") }
end
newline() click to toggle source

Returns a parser that will match any kind of line ending. @return [Paco::Parser]

# File lib/paco/combinators/char.rb, line 141
def newline
  memoize { alt(crlf, lf, cr) }
end
none_of(matcher) click to toggle source

Returns a parser that looks for exactly one character NOT from passed ‘matcher`, and returns its value on success. @param [String, Array<String>] matcher @return [Paco::Parser]

# File lib/paco/combinators/char.rb, line 80
def none_of(matcher)
  satisfy("none_of(#{matcher})") { |char| !matcher.include?(char) }
end
one_of(matcher) click to toggle source

Returns a parser that looks for exactly one character from passed ‘matcher`, and returns its value on success. @param [String, Array<String>] matcher @return [Paco::Parser]

# File lib/paco/combinators/char.rb, line 72
def one_of(matcher)
  satisfy("one_of(#{matcher})") { |char| matcher.include?(char) }
end
opt_digits() click to toggle source

Alias for ‘Paco::Combinators.regexp(/*/)`. @return [Paco::Parser]

# File lib/paco/combinators/char.rb, line 183
def opt_digits
  memoize { regexp(/[0-9]*/) }
end
opt_letters() click to toggle source

Alias for ‘Paco::Combinators.regexp(/*/i)`. @return [Paco::Parser]

# File lib/paco/combinators/char.rb, line 165
def opt_letters
  memoize { regexp(/[a-z]*/i) }
end
opt_ws() click to toggle source

Alias for ‘Paco::Combinators.regexp(/s*/)`. @return [Paco::Parser]

# File lib/paco/combinators/char.rb, line 195
def opt_ws
  memoize { regexp(/\s*/) }
end
regexp(regexp, group: 0) click to toggle source

Returns a parser that looks for a match to the regexp and returns the entire text matched. The regexp will always match starting at the current parse location. When ‘group` is specified, it returns only the text in the specific regexp match group. @param [Regexp] regexp @return [Paco::Parser] @param [Integer] group

# File lib/paco/combinators/char.rb, line 50
def regexp(regexp, group: 0)
  anchored_regexp = Regexp.new("\\A(?:#{regexp.source})", regexp.options)
  Parser.new("regexp(#{regexp.inspect})") do |ctx, parser|
    match = anchored_regexp.match(ctx.read_all)
    parser.failure(ctx) if match.nil?

    ctx.pos += match[0].length
    match[group]
  end
end
regexp_char(regexp) click to toggle source

Returns a parser that checks current character against the passed ‘regexp` @param [Regexp] regexp @return [Paco::Parser]

# File lib/paco/combinators/char.rb, line 64
def regexp_char(regexp)
  satisfy("regexp_char(#{regexp.inspect})") { |char| regexp.match?(char) }
end
remainder() click to toggle source

Returns a parser that consumes and returns the entire remainder of the input. @return [Paco::Parser]

# File lib/paco/combinators/char.rb, line 92
def remainder
  memoize do
    Parser.new("remainder") do |ctx, parser|
      result = ctx.read_all
      ctx.pos += result.length
      result
    end
  end
end
satisfy(desc = "", &block) click to toggle source

Returns a parser that returns a single character if passed block result is truthy:

@example lower = Combinators.satisfy do |char|

char == char.downcase

end

lower.parse(“a”) #=> “a” lower.parse(“P”) #=> ParseError

@param [String] desc optional description for the parser @param [Proc] block proc with one argument – a next char of the input @return [Paco::Parser]

# File lib/paco/combinators/char.rb, line 19
def satisfy(desc = "", &block)
  Parser.new(desc) do |ctx, parser|
    parser.failure(ctx) if ctx.eof?

    char = ctx.read(1)
    parser.failure(ctx) unless block.call(char)

    ctx.pos += 1
    char
  end
end
spaced(parser) click to toggle source

Alias for ‘parser.trim(Paco::Combinators.opt_ws)`. @param [Paco::Parser] parser @return [Paco::Parser]

# File lib/paco/combinators/char.rb, line 202
def spaced(parser)
  parser.trim(opt_ws)
end
string(matcher) click to toggle source

Returns a parser that looks for a passed ‘matcher` string and returns its value on success. @param [String] matcher @return [Paco::Parser]

# File lib/paco/combinators/char.rb, line 34
def string(matcher)
  Parser.new("string(#{matcher.inspect})") do |ctx, parser|
    src = ctx.read(matcher.length)
    parser.failure(ctx) if src != matcher

    ctx.pos += matcher.length
    src
  end
end
take_while(&block) click to toggle source

Returns a parser that returns a string containing all the next characters that are truthy for the passed block. @param [Proc] block proc with one argument – a next char of the input @return [Paco::Parser]

# File lib/paco/combinators/char.rb, line 106
def take_while(&block)
  satisfy(&block).many.join
end
ws() click to toggle source

Alias for ‘Paco::Combinators.regexp(/s+/)`. @return [Paco::Parser]

# File lib/paco/combinators/char.rb, line 189
def ws
  memoize { regexp(/\s+/) }
end