Module: Paco::Combinators::Char
- Included in:
- Paco::Combinators, Paco::Combinators
- Defined in:
- lib/paco/combinators/char.rb
Instance Method Summary collapse
-
#any_char ⇒ Paco::Parser
Returns a parser that consumes and returns the next character of the input.
-
#cr ⇒ Paco::Parser
Returns a parser that checks for the “carriage return” (
\r) character. -
#crlf ⇒ Paco::Parser
Returns a parser that checks for the “carriage return” character followed by the “line feed” character (‘rn`).
-
#digit ⇒ Paco::Parser
Alias for ‘Paco::Combinators.regexp_char(//)`.
-
#digits ⇒ Paco::Parser
Alias for ‘Paco::Combinators.regexp(/+/)`.
-
#end_of_line ⇒ Paco::Parser
Returns a parser that will match any kind of line ending including end of file.
-
#eof ⇒ Paco::Parser
Returns a parser that matches end of file and returns nil.
-
#letter ⇒ Paco::Parser
Alias for ‘Paco::Combinators.regexp_char(//i)`.
-
#letters ⇒ Paco::Parser
Alias for ‘Paco::Combinators.regexp(/+/i)`.
-
#lf ⇒ Paco::Parser
Returns a parser that checks for the “line feed” (
\n) character. -
#newline ⇒ Paco::Parser
Returns a parser that will match any kind of line ending.
-
#none_of(matcher) ⇒ Paco::Parser
Returns a parser that looks for exactly one character NOT from passed
matcher, and returns its value on success. -
#one_of(matcher) ⇒ Paco::Parser
Returns a parser that looks for exactly one character from passed
matcher, and returns its value on success. -
#opt_digits ⇒ Paco::Parser
Alias for ‘Paco::Combinators.regexp(/*/)`.
-
#opt_letters ⇒ Paco::Parser
Alias for ‘Paco::Combinators.regexp(/*/i)`.
-
#opt_ws ⇒ Paco::Parser
Alias for ‘Paco::Combinators.regexp(/s*/)`.
-
#regexp(regexp, group: 0) ⇒ Paco::Parser
Returns a parser that looks for a match to the regexp and returns the entire text matched.
-
#regexp_char(regexp) ⇒ Paco::Parser
Returns a parser that checks current character against the passed
regexp. -
#remainder ⇒ Paco::Parser
Returns a parser that consumes and returns the entire remainder of the input.
-
#satisfy(desc = "", &block) ⇒ Paco::Parser
Returns a parser that returns a single character if passed block result is truthy:.
-
#spaced(parser) ⇒ Paco::Parser
Alias for ‘parser.trim(Paco::Combinators.opt_ws)`.
-
#string(matcher) ⇒ Paco::Parser
Returns a parser that looks for a passed
matcherstring and returns its value on success. -
#take_while(&block) ⇒ Paco::Parser
Returns a parser that returns a string containing all the next characters that are truthy for the passed block.
-
#ws ⇒ Paco::Parser
Alias for ‘Paco::Combinators.regexp(/s+/)`.
Instance Method Details
#any_char ⇒ Paco::Parser
Returns a parser that consumes and returns the next character of the input.
86 87 88 |
# File 'lib/paco/combinators/char.rb', line 86 def any_char memoize { satisfy("any_char") { |ch| ch.length > 0 } } end |
#cr ⇒ Paco::Parser
Returns a parser that checks for the “carriage return” (\r) character.
123 124 125 |
# File 'lib/paco/combinators/char.rb', line 123 def cr memoize { string("\r") } end |
#crlf ⇒ Paco::Parser
Returns a parser that checks for the “carriage return” character followed by the “line feed” character (‘rn`).
135 136 137 |
# File 'lib/paco/combinators/char.rb', line 135 def crlf memoize { string("\r\n") } end |
#digit ⇒ Paco::Parser
Alias for ‘Paco::Combinators.regexp_char(//)`.
171 172 173 |
# File 'lib/paco/combinators/char.rb', line 171 def digit memoize { regexp_char(/[0-9]/) } end |
#digits ⇒ Paco::Parser
Alias for ‘Paco::Combinators.regexp(/+/)`.
177 178 179 |
# File 'lib/paco/combinators/char.rb', line 177 def digits memoize { regexp(/[0-9]+/) } end |
#end_of_line ⇒ Paco::Parser
Returns a parser that will match any kind of line ending including end of file.
147 148 149 |
# File 'lib/paco/combinators/char.rb', line 147 def end_of_line memoize { alt(newline, eof) } end |
#eof ⇒ Paco::Parser
Returns a parser that matches end of file and returns nil.
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 |
#letter ⇒ Paco::Parser
Alias for ‘Paco::Combinators.regexp_char(//i)`.
153 154 155 |
# File 'lib/paco/combinators/char.rb', line 153 def letter memoize { regexp_char(/[a-z]/i) } end |
#letters ⇒ Paco::Parser
Alias for ‘Paco::Combinators.regexp(/+/i)`.
159 160 161 |
# File 'lib/paco/combinators/char.rb', line 159 def letters memoize { regexp(/[a-z]+/i) } end |
#lf ⇒ Paco::Parser
Returns a parser that checks for the “line feed” (\n) character.
129 130 131 |
# File 'lib/paco/combinators/char.rb', line 129 def lf memoize { string("\n") } end |
#newline ⇒ Paco::Parser
Returns a parser that will match any kind of line ending.
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.
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.
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_digits ⇒ Paco::Parser
Alias for ‘Paco::Combinators.regexp(/*/)`.
183 184 185 |
# File 'lib/paco/combinators/char.rb', line 183 def opt_digits memoize { regexp(/[0-9]*/) } end |
#opt_letters ⇒ Paco::Parser
Alias for ‘Paco::Combinators.regexp(/*/i)`.
165 166 167 |
# File 'lib/paco/combinators/char.rb', line 165 def opt_letters memoize { regexp(/[a-z]*/i) } end |
#opt_ws ⇒ Paco::Parser
Alias for ‘Paco::Combinators.regexp(/s*/)`.
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.
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.) 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
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 |
#remainder ⇒ Paco::Parser
Returns a parser that consumes and returns the entire remainder of the input.
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
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)`.
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.
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.
106 107 108 |
# File 'lib/paco/combinators/char.rb', line 106 def take_while(&block) satisfy(&block).many.join end |
#ws ⇒ Paco::Parser
Alias for ‘Paco::Combinators.regexp(/s+/)`.
189 190 191 |
# File 'lib/paco/combinators/char.rb', line 189 def ws memoize { regexp(/\s+/) } end |