Class: RParsec::Parser

Class | RParsec::Parser
In: | rparsec/parser.rb
Parent: | Object

Represents a parser that parses a certain grammar rule.

### Methods

* << >> atomize bindn catchp delimited delimited1 expect followed fragment infixl infixn infixr lexeme lookahead many many_ map mapn nested new not optional parse peek postfix prefix repeat repeat_ separated separated1 seq setName some some_ to_s token | ~

### Included Modules

Functors Monad

### Constants

MyMonad | = | ParserMonad.new

### Attributes

name | [RW] |

### Public Class methods

new()

### Public Instance methods

*(min, max=min)

Alias for repeat_

<<(other)

Alias for followed

>>(other)

Similar to seq. other is auto-boxed if it is not of type Parser.

atomize()

Create a new parser that‘s atomic., meaning that when it fails, input consumption is undone.

bindn(&block)

self is first executed, the parser result is then passed as parameter to the associated block, which evaluates to another Parser object at runtime. This new Parser object is then executed to get the final parser result.

Different from bind, parser result of self will be expanded first if it is an array.

catchp(symbol)

a.catchp(:somesymbol) will catch the :somesymbol thrown by a.

delimited(delim)

To create a parser that repeats self for unlimited times, with the pattern recognized by delim as separator that separates each occurrence and also possibly ends the pattern. Return values of self are collected in an array.

delimited1(delim)

To create a parser that repeats self for unlimited times, with the pattern recognized by delim as separator that separates each occurrence and also possibly ends the pattern. self has to match for at least once. Return values of self are collected in an array.

expect(msg)

To create a parser that fails with a given error message.

followed(other)

a.followed b will sequentially run a and b; result of a is preserved as the ultimate return value.

fragment()

a.fragment will return the string matched by a.

infixl(op)

For left-associative infix binary operator. op has to return a Proc that takes two parameters, who are returned by the self parser as operands.

infixn(op)

For non-associative infix binary operator. op has to return a Proc that takes two parameters, who are returned by the self parser as operands.

infixr(op)

For right-associative infix binary operator. op has to return a Proc that takes two parameters, who are returned by the self parser as operands.

lexeme(delim = Parsers.whitespaces)

a.lexeme(delim) will parse a for 0 or more times and ignore all patterns recognized by delim. Values returned by a are collected in an array.

lookahead(n)

To create a parser that does "look ahead" for n inputs.

many(least=0)

To create a parser that repeats self for at least least times. All return values are collected in an array.

many_(least=0)

To create a parser that repeats self for at least least times. parser.many_ is equivalent to bnf notation "parser*". Only the return value of the last execution is preserved.

map(&block)

a.map{|x|x+1} will first execute parser a, when it succeeds, the associated block is executed to transform the result to a new value (increment it in this case).

mapn(&block)

a.mapn{|x,y|x+y} will first execute parser a, when it succeeds, the array result (if any) is expanded and passed as parameters to the associated block. The result of the block is then used as the parsing result.

nested(parser)

a.nested b will feed the token array returned by parser a to parser b for a nested parsing.

not(msg="

To create a new parser that succeed only if self fails.

optional(default=nil)

a.optional(default) is equivalent to a.plus(value(default))

parse(src)

parses a string.

peek()

Create a new parser that looks at inputs whthout consuming them.

postfix(op)

For postfix unary operator. a.postfix op will run parser a for once and then op for 0 or more times. op should return a Proc that accepts one parameter. Proc objects returned by op is then fed with the value returned by a from left to right. The final result is returned as return value.

prefix(op)

For prefix unary operator. a.prefix op will run parser op for 0 or more times and eventually run parser a for one time. op should return a Proc that accepts one parameter. Proc objects returned by op is then fed with the value returned by a from right to left. The final result is returned as return value.

repeat(min, max=min)

To create a parser that repeats self for a minimum min times, and maximally max times. All return values are collected in an array.

repeat_(min, max=min)

To create a parser that repeats self for a minimum min times, and maximally max times. Only the return value of the last execution is preserved.

separated(delim)

To create a parser that repeats self for unlimited times, with the pattern recognized by delim as separator that separates each occurrence. Return values of self are collected in an array.

separated1(delim)

To create a parser that repeats self for unlimited times, with the pattern recognized by delim as separator that separates each occurrence. self has to match for at least once. Return values of self are collected in an array.

seq(other, &block)

a.seq b will sequentially run a then b. The result of b is preserved as return value. If a block is associated, values returned by a and b are passed into the block and the return value of the block is used as the final result of the parser.

setName(nm)

Set name for the parser. self is returned.

some(max)

To create a parser that repeats self for at most max times. All return values are collected in an array.

some_(max)

To create a parser that repeats self for at most max times. Only the return value of the last execution is preserved.

to_s()

String representation

token(kind)

a.token(:word_token) will return a Token object when a succeeds. The matched string (or the string returned by a, if any) is encapsulated in the token, together with the :word_token symbol and the starting index of the match.

|(other)

a | b will run b when a fails. b is auto-boxed to Parser when it is not of type Parser.

~(msg="

Alias for not

[Validate]