Module: Paco::Combinators::Char

Included in:
Paco::Combinators, Paco::Combinators
Defined in:
lib/paco/combinators/char.rb

Instance Method Summary collapse

Instance Method Details

#any_charPaco::Parser

Returns a parser that consumes and returns the next character of the input.

Returns:



86
87
88
# File 'lib/paco/combinators/char.rb', line 86

def any_char
  memoize { satisfy("any_char") { |ch| ch.length > 0 } }
end

#crPaco::Parser

Returns a parser that checks for the “carriage return” (‘r`) character.

Returns:



123
124
125
# File 'lib/paco/combinators/char.rb', line 123

def cr
  memoize { string("\r") }
end

#crlfPaco::Parser

Returns a parser that checks for the “carriage return” character followed by the “line feed” character (‘rn`).

Returns:



135
136
137
# File 'lib/paco/combinators/char.rb', line 135

def crlf
  memoize { string("\r\n") }
end

#digitPaco::Parser

Returns:



171
172
173
# File 'lib/paco/combinators/char.rb', line 171

def digit
  memoize { regexp_char(/[0-9]/) }
end

#digitsPaco::Parser

Alias for ‘Paco::Combinators.regexp(/+/)`.

Returns:



177
178
179
# File 'lib/paco/combinators/char.rb', line 177

def digits
  memoize { regexp(/[0-9]+/) }
end

#end_of_linePaco::Parser

Returns a parser that will match any kind of line ending including end of file.

Returns:



147
148
149
# File 'lib/paco/combinators/char.rb', line 147

def end_of_line
  memoize { alt(newline, eof) }
end

#eofPaco::Parser

Returns a parser that matches end of file and returns nil.

Returns:



112
113
114
115
116
117
118
119
# 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

#letterPaco::Parser

Returns:



153
154
155
# File 'lib/paco/combinators/char.rb', line 153

def letter
  memoize { regexp_char(/[a-z]/i) }
end

#lettersPaco::Parser

Alias for ‘Paco::Combinators.regexp(/+/i)`.

Returns:



159
160
161
# File 'lib/paco/combinators/char.rb', line 159

def letters
  memoize { regexp(/[a-z]+/i) }
end

#lfPaco::Parser

Returns a parser that checks for the “line feed” (‘n`) character.

Returns:



129
130
131
# File 'lib/paco/combinators/char.rb', line 129

def lf
  memoize { string("\n") }
end

#newlinePaco::Parser

Returns a parser that will match any kind of line ending.

Returns:



141
142
143
# File 'lib/paco/combinators/char.rb', line 141

def newline
  memoize { alt(crlf, lf, cr) }
end

#none_of(matcher) ⇒ Paco::Parser

Returns a parser that looks for exactly one character NOT from passed ‘matcher`, and returns its value on success.

Parameters:

  • matcher (String, Array<String>)

Returns:



80
81
82
# File 'lib/paco/combinators/char.rb', line 80

def none_of(matcher)
  satisfy("none_of(#{matcher})") { |char| !matcher.include?(char) }
end

#one_of(matcher) ⇒ Paco::Parser

Returns a parser that looks for exactly one character from passed ‘matcher`, and returns its value on success.

Parameters:

  • matcher (String, Array<String>)

Returns:



72
73
74
# File 'lib/paco/combinators/char.rb', line 72

def one_of(matcher)
  satisfy("one_of(#{matcher})") { |char| matcher.include?(char) }
end

#opt_digitsPaco::Parser

Alias for ‘Paco::Combinators.regexp(/*/)`.

Returns:



183
184
185
# File 'lib/paco/combinators/char.rb', line 183

def opt_digits
  memoize { regexp(/[0-9]*/) }
end

#opt_lettersPaco::Parser

Alias for ‘Paco::Combinators.regexp(/*/i)`.

Returns:



165
166
167
# File 'lib/paco/combinators/char.rb', line 165

def opt_letters
  memoize { regexp(/[a-z]*/i) }
end

#opt_wsPaco::Parser

Alias for ‘Paco::Combinators.regexp(/s*/)`.

Returns:



195
196
197
# File 'lib/paco/combinators/char.rb', line 195

def opt_ws
  memoize { regexp(/\s*/) }
end

#regexp(regexp, group: 0) ⇒ Paco::Parser

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.

Parameters:

  • regexp (Regexp)
  • group (Integer) (defaults to: 0)

Returns:



50
51
52
53
54
55
56
57
58
59
# 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) ⇒ Paco::Parser

Returns a parser that checks current character against the passed ‘regexp`

Parameters:

  • regexp (Regexp)

Returns:



64
65
66
# File 'lib/paco/combinators/char.rb', line 64

def regexp_char(regexp)
  satisfy("regexp_char(#{regexp.inspect})") { |char| regexp.match?(char) }
end

#remainderPaco::Parser

Returns a parser that consumes and returns the entire remainder of the input.

Returns:



92
93
94
95
96
97
98
99
100
# 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) ⇒ Paco::Parser

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

lower = Combinators.satisfy do |char|

char == char.downcase

end

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

Parameters:

  • desc (String) (defaults to: "")

    optional description for the parser

  • block (Proc)

    proc with one argument – a next char of the input

Returns:



19
20
21
22
23
24
25
26
27
28
29
# 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) ⇒ Paco::Parser

Alias for ‘parser.trim(Paco::Combinators.opt_ws)`.

Parameters:

Returns:



202
203
204
# File 'lib/paco/combinators/char.rb', line 202

def spaced(parser)
  parser.trim(opt_ws)
end

#string(matcher) ⇒ Paco::Parser

Returns a parser that looks for a passed ‘matcher` string and returns its value on success.

Parameters:

  • matcher (String)

Returns:



34
35
36
37
38
39
40
41
42
# 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) ⇒ Paco::Parser

Returns a parser that returns a string containing all the next characters that are truthy for the passed block.

Parameters:

  • block (Proc)

    proc with one argument – a next char of the input

Returns:



106
107
108
# File 'lib/paco/combinators/char.rb', line 106

def take_while(&block)
  satisfy(&block).many.join
end

#wsPaco::Parser

Alias for ‘Paco::Combinators.regexp(/s+/)`.

Returns:



189
190
191
# File 'lib/paco/combinators/char.rb', line 189

def ws
  memoize { regexp(/\s+/) }
end