Class: Rattler::Parsers::ParserDSL
- Inherits:
-
Object
- Object
- Rattler::Parsers::ParserDSL
- Defined in:
- lib/rattler/parsers/parser_dsl.rb
Overview
ParserDSL
defines a simple DSL for defining parsers.
Class Method Summary collapse
-
.rules(options = {}, &block) ⇒ Rattler::Parsers::RuleSet
Define parse rules with the given block.
Instance Method Summary collapse
-
#alnum ⇒ Match
A parser matching the POSIX
alnum
character class. -
#alpha ⇒ Match
A parser matching the POSIX
alpha
character class. -
#assert(arg) ⇒ Object
Create a new assert parser.
-
#blank ⇒ Match
A parser matching the POSIX
blank
character class. -
#cntrl ⇒ Match
A parser matching the POSIX
cntrl
character class. -
#digit ⇒ Match
A parser matching the POSIX
digit
character class. -
#direct_action(arg, code) ⇒ Object
Create a new semantic action that evaluates ruby code.
-
#disallow(arg) ⇒ Object
Create a new disallow parser.
-
#dispatch_action(arg, attrs = {}) ⇒ Object
Create a new semantic action that dispatches to a method.
- #e ⇒ Object
-
#eof ⇒ Object
The eof parser.
-
#fail(message) ⇒ Fail
A parser that always fails.
-
#fail_parse(message) ⇒ Fail
A parser that fails the entire parse.
-
#fail_rule(message) ⇒ Fail
A parser that fails the entire rule.
-
#graph ⇒ Match
A parser matching the POSIX
graph
character class. -
#initialize(options = {}) ⇒ ParserDSL
constructor
A new instance of ParserDSL.
-
#label(name, arg) ⇒ Object
Create a new labeled parser.
-
#list(term_arg, sep_arg, min, max) ⇒ Object
Create a new list parser.
-
#lower ⇒ Match
A parser matching the POSIX
lower
character class. -
#match(arg) ⇒ Object
Create a new parser to match a pattern, literal, referenced parse rule, posix character class, EOF, or E.
-
#one_or_more(arg) ⇒ Object
(also: #some)
Create a new one-or-more parser.
-
#optional(arg) ⇒ Object
Create a new optional parser.
-
#print ⇒ Match
A parser matching the POSIX
print
character class. -
#punct ⇒ Match
A parser matching the POSIX
punct
character class. -
#repeat(arg, min, max) ⇒ Object
Create a generalized repeat parser.
-
#rule(name, &block) ⇒ Rule
Evaluate the given block to define a parse rule.
-
#rules(&block) ⇒ RuleSet
Evaluate the given block to define parse rules.
-
#semantic_assert(arg, code) ⇒ Object
Create a new positive semantic predicate.
-
#semantic_disallow(arg, code) ⇒ Object
Create a new negative semantic predicate.
-
#side_effect(arg, code) ⇒ Object
Create a new semantic action that evaluates ruby code for effect.
-
#skip(arg) ⇒ Object
Create a new skip parser.
-
#space ⇒ Match
A parser matching the POSIX
space
character class. -
#token(arg, &block) ⇒ Object
Create a new token parser or token rule.
-
#upper ⇒ Match
A parser matching the POSIX
upper
character class. - #with_options(options, &block) ⇒ Object
-
#with_ws(ws, &block) ⇒ Object
Evaluate the given block using
ws
to skip whitespace. -
#word ⇒ Match
A parser matching the
word
character class. -
#xdigit ⇒ Match
A parser matching the POSIX
xdigit
character class. -
#zero_or_more(arg) ⇒ Object
(also: #any)
Create a new zero-or-more parser.
Constructor Details
#initialize(options = {}) ⇒ ParserDSL
Returns a new instance of ParserDSL.
30 31 32 33 34 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 30 def initialize( = {}) #:nodoc: @rules = [:rules] || [] @options = @ws = [:ws] end |
Class Method Details
.rules(options = {}, &block) ⇒ Rattler::Parsers::RuleSet
Define parse rules with the given block
25 26 27 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 25 def self.rules( = {}, &block) self.new().rules(&block) end |
Instance Method Details
#alnum ⇒ Match
Returns a parser matching the POSIX alnum
character class.
337 338 339 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 337 def alnum match :ALNUM end |
#alpha ⇒ Match
Returns a parser matching the POSIX alpha
character class.
342 343 344 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 342 def alpha match :ALPHA end |
#assert(parser) ⇒ Assert #assert(arg) ⇒ Assert
Create a new assert parser.
189 190 191 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 189 def assert(arg) Assert[to_parser(arg)] end |
#blank ⇒ Match
Returns a parser matching the POSIX blank
character class.
347 348 349 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 347 def blank match :BLANK end |
#cntrl ⇒ Match
Returns a parser matching the POSIX cntrl
character class.
352 353 354 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 352 def cntrl match :CNTRL end |
#digit ⇒ Match
Returns a parser matching the POSIX digit
character class.
357 358 359 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 357 def digit match :DIGIT end |
#direct_action(parser, code) ⇒ DirectAction #direct_action(arg, code) ⇒ DirectAction
Create a new semantic action that evaluates ruby code.
235 236 237 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 235 def direct_action(arg, code) DirectAction[to_parser(arg), code] end |
#disallow(parser) ⇒ Disallow #disallow(arg) ⇒ Disallow
Create a new disallow parser.
201 202 203 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 201 def disallow(arg) Disallow[to_parser(arg)] end |
#dispatch_action(parser) ⇒ DispatchAction #dispatch_action(arg) ⇒ DispatchAction
Create a new semantic action that dispatches to a method.
223 224 225 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 223 def dispatch_action(arg, attrs={}) DispatchAction[to_parser(arg), attrs] end |
#e ⇒ Object
211 212 213 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 211 def e ESymbol[] end |
#eof ⇒ Object
Returns the eof parser.
206 207 208 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 206 def eof Eof[] end |
#fail(message) ⇒ Fail
Returns a parser that always fails.
317 318 319 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 317 def fail() Fail[:expr, ] end |
#fail_parse(message) ⇒ Fail
Returns a parser that fails the entire parse.
327 328 329 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 327 def fail_parse() Fail[:parse, ] end |
#fail_rule(message) ⇒ Fail
Returns a parser that fails the entire rule.
322 323 324 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 322 def fail_rule() Fail[:rule, ] end |
#graph ⇒ Match
Returns a parser matching the POSIX graph
character class.
362 363 364 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 362 def graph match :GRAPH end |
#label(parser) ⇒ Label #label(arg) ⇒ Label
Create a new labeled parser.
312 313 314 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 312 def label(name, arg) Label[name, to_parser(arg)] end |
#list(term_parser, sep_parser, min, max) ⇒ ListParser #list(term_arg, sep_arg, min, max) ⇒ ListParser
Create a new list parser.
177 178 179 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 177 def list(term_arg, sep_arg, min, max) ListParser[to_parser(term_arg), to_parser(sep_arg), min, max] end |
#lower ⇒ Match
Returns a parser matching the POSIX lower
character class.
367 368 369 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 367 def lower match :LOWER end |
#match(pattern) ⇒ Match #match(literal) ⇒ Match #match(rule_name) ⇒ Apply #match(posix_name) ⇒ Match #match(: EOF) ⇒ Eof #match(: E) ⇒ ESymbol
Create a new parser to match a pattern, literal, referenced parse rule, posix character class, EOF, or E.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 95 def match(arg) case arg when Regexp then Match[arg] when :EOF then eof when :E then e when :ALNUM then match /[[:alnum:]]/ when :ALPHA then match /[[:alpha:]]/ when :BLANK then match /[[:blank:]]/ when :CNTRL then match /[[:cntrl:]]/ when :DIGIT then match /[[:digit:]]/ when :GRAPH then match /[[:graph:]]/ when :LOWER then match /[[:lower:]]/ when :PRINT then match /[[:print:]]/ when :PUNCT then match /[[:punct:]]/ when :SPACE then match /[[:space:]]/ when :UPPER then match /[[:upper:]]/ when :XDIGIT then match /[[:xdigit:]]/ when :WORD then match /[[:alnum:]_]/ when Symbol then Apply[arg] else match Regexp.new(Regexp.escape(arg.to_s)) end end |
#one_or_more(parser) ⇒ Repeat #one_or_more(arg) ⇒ Repeat Also known as: some
Create a new one-or-more parser.
152 153 154 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 152 def one_or_more(arg) Repeat[to_parser(arg), 1, nil] end |
#optional(parser) ⇒ Repeat #optional(arg) ⇒ Repeat
Create a new optional parser.
126 127 128 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 126 def optional(arg) Repeat[to_parser(arg), 0, 1] end |
#print ⇒ Match
Returns a parser matching the POSIX print
character class.
372 373 374 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 372 def print match :PRINT end |
#punct ⇒ Match
Returns a parser matching the POSIX punct
character class.
377 378 379 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 377 def punct match :PUNCT end |
#one_or_more(parser, min, max) ⇒ Repeat #one_or_more(arg, min, max) ⇒ Repeat
Create a generalized repeat parser.
166 167 168 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 166 def repeat(arg, min, max) Repeat[to_parser(arg), min, max] end |
#rule(name, &block) ⇒ Rule
Evaluate the given block to define a parse rule
62 63 64 65 66 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 62 def rule(name, &block) parser = instance_exec(self, &block) @rules << Rule[name, (@ws ? parser.with_ws(@ws) : parser)] @rules.last end |
#rules(&block) ⇒ RuleSet
Evaluate the given block to define parse rules
52 53 54 55 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 52 def rules(&block) instance_exec(self, &block) RuleSet[@rules] end |
#semantic_assert(parser, code) ⇒ SemanticAssert #semantic_assert(arg, code) ⇒ SemanticAssert
Create a new positive semantic predicate.
259 260 261 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 259 def semantic_assert(arg, code) SemanticAssert[to_parser(arg), code] end |
#semantic_disallow(parser, code) ⇒ SemanticDisallow #semantic_disallow(arg, code) ⇒ SemanticDisallow
Create a new negative semantic predicate.
271 272 273 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 271 def semantic_disallow(arg, code) SemanticDisallow[to_parser(arg), code] end |
#side_effect(parser, code) ⇒ SideEffect #side_effect(arg, code) ⇒ SideEffect
Create a new semantic action that evaluates ruby code for effect.
247 248 249 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 247 def side_effect(arg, code) SideEffect[to_parser(arg), code] end |
#skip(parser) ⇒ Skip #skip(arg) ⇒ Skip
Create a new skip parser.
300 301 302 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 300 def skip(arg) Skip[to_parser(arg)] end |
#space ⇒ Match
Returns a parser matching the POSIX space
character class.
382 383 384 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 382 def space match :SPACE end |
#token(rule_name, &block) ⇒ Rule #token(parser) ⇒ Token #token(arg) ⇒ Token
Create a new token parser or token rule.
284 285 286 287 288 289 290 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 284 def token(arg, &block) if block_given? rule(arg) { token(instance_exec(self, &block)) } else Token[to_parser(arg)] end end |
#upper ⇒ Match
Returns a parser matching the POSIX upper
character class.
387 388 389 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 387 def upper match :UPPER end |
#with_options(options, &block) ⇒ Object
37 38 39 40 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 37 def (, &block) #:nodoc: dsl = self.class.new(@options.merge(:rules => @rules).merge()) dsl.instance_exec(dsl, &block) end |
#with_ws(ws, &block) ⇒ Object
Evaluate the given block using ws
to skip whitespace
45 46 47 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 45 def with_ws(ws, &block) (:ws => to_parser(ws), &block) end |
#word ⇒ Match
Returns a parser matching the word
character class.
397 398 399 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 397 def word match :WORD end |
#xdigit ⇒ Match
Returns a parser matching the POSIX xdigit
character class.
392 393 394 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 392 def xdigit match :XDIGIT end |