Module: RParsec::Parsers

Module | RParsec::Parsers
In: | rparsec/parser.rb

This module provides all out-of-box parser implementations.

### Methods

alt among any are arent char comment_block comment_line eof failure get_index integer is isnt lazy longer longest map mapn not_among not_char not_string number one range regexp satisfies sequence set_index shorter shortest str string string_nocase sum throwp token value watch watchn whitespace whitespaces word zero

### Constants

Whitespaces | = | " \t\r\n" |  | characters considered white space.

### Public Instance methods

alt(*alts)

A parser that calls alternative parsers until one succeeds.

among(*vals)

A parser that succeeds when the the current input is among the given values.

any()

A parser that consumes one input.

are(vals, expected="

A parser that tries to match the current inputs one by one with the given values. It succeeds only when all given values are matched, in which case all the matched inputs are consumed.

arent(vals, expected="

A parser that makes sure that the given values don‘t match the current inputs. One input is consumed if it succeeds.

char(c)

A parser that succeeds when the the current input is the given character.

comment_block(open, close)

A parser that parses a chunk of text started with open and ended by close. nil is the result.

comment_line(start)

A parser that parses a line started with start. nil is the result.

eof(expected="EOF expected")

A parser that succeeds when there‘s no input available.

failure(msg)

A parser that always fails with the given error message.

get_index()

A parser that returns the current input index (starting from 0).

integer(expected='integer expected')

A parser that parses an integer and return the matched integer as string.

is(v, expected="

A parser that succeeds when the the current input is equal to the given value. expected is the error message when pred returns false.

isnt(v, expected="

A parser that succeeds when the the current input is not equal to the given value. expected is the error message when pred returns false.

lazy(&block)

A lazy parser, when executed, calls the given block to get a parser object and delegate the call to this lazily instantiated parser.

longer(*parsers)

Alias for longest

longest(*parsers)

A parser that tries all given alternative parsers and picks the one with the longest match.

map(&block)

A parser that maps current parser result to a new result using the given block.

Different from Parser#map, this method does not need to be combined with any Parser object. It is rather an independent Parser object that maps the current parser result.

parser1.map{|x|…} is equivalent to parser1 >> map{|x|…}

mapn(&block)

A parser that maps current parser result to a new result using the given block. If the current parser result is an array, the array elements are expanded and then passed as parameters to the block.

Different from Parser#mapn, this method does not need to be combined with any Parser object. It is rather an independent Parser object that maps the current parser result.

parser1.mapn{|x,y|…} is equivalent to parser1 >> mapn{|x,y|…}

not_among(*vals)

A parser that succeeds when the the current input is not among the given values.

not_char(c)

A parser that succeeds when the the current input is not the given character.

not_string(str, msg="\"

A parser that makes sure that the current input doesn‘t match a string. One character is consumed if it succeeds.

number(expected='number expected')

A parser that parses a number (integer, or decimal number) and return the matched number as string.

one()

A parser that always succeeds.

range(from, to, msg="

A parser that succeeds if the current input is within a certain range.

regexp(ptn, expected="/

A parser that succeeds if the current inputs match the given regular expression. The matched string is consumed and returned as result.

satisfies(expected, &pred)

A parser that succeeds when the given predicate returns true (with the current input as the parameter). expected is the error message when pred returns false.

sequence(*parsers, &proc)

A parser that sequentially run the given parsers. The result of the last parser is used as return value. If a block is given, the results of the parsers are passed into the block as parameters, and the block return value is used as result instead.

set_index(ind)

A parser that moves the current input pointer to a certain index.

shorter(*parsers)

Alias for shortest

shortest(*parsers)

A parser that tries all given alternative parsers and picks the one with the shortest match.

str(str, msg = "\"

Alias for string

string(str, msg = "\"

A parser that matches the given string.

string_nocase(str, expected="'

A parser that matches the given string, case insensitively.

sum(*alts)

A parser that calls alternative parsers until one succeed, or any failure with input consumption beyond the current look-ahead.

throwp(symbol)

A parser that throws a symbol.

token(*kinds, &proc)

A parser that succeeds when the current input is a token with one of the the given token kinds. If a block is given, the token text is passed to the block as parameter, and the block return value is used as result. Otherwise, the token object is used as result.

value(v)

A parser that always succeeds with the given return value.

watch(&block)

A parser that watches the current parser result without changing it. The following assert will succeed:

char(?a) >> watch{|x|assert_equal(?a, x)}

watch can also be used as a handy tool to print trace information, for example:

some_parser >> watch {puts "some_parser succeeded."}

watchn(&block)

A parser that watches the current parser result without changing it. The following assert will succeed:

char(?a).repeat(2) >> watchn{|x,y|assert_equal([?a,?a], [x,y])}

Slightly different from watch, watchn expands the current parser result before passing it into the associated block.

whitespace(expected="whitespace expected")

A parser that parses a white space character.

whitespaces(expected="whitespace(s) expected")

A parser that parses 1 or more white space characters.

word(expected='word expected')

A parser that parses a word (starting with alpha or underscore, followed by 0 or more alpha, number or underscore). and return the matched word as string.

zero()

A parser that always fails.

[Validate]