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 symantic action that evaluates ruby code.
-
#disallow(arg) ⇒ Object
Create a new disallow parser.
-
#dispatch_action(arg, attrs = {}) ⇒ Object
Create a new symantic action that dispatches to a method.
-
#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.
-
#list0(term_arg, sep_arg) ⇒ Object
(also: #list)
Create a new list parser.
-
#list1(term_arg, sep_arg) ⇒ Object
Create a new list1 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, or EOF.
-
#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. -
#rule(name, &block) ⇒ Rule
Evaluate the given block to define a parse rule.
-
#rules(&block) ⇒ RuleSet
Evaluate the given block to define parse rules.
-
#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.
294 295 296 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 294 def alnum match :ALNUM end |
#alpha ⇒ Match
Returns a parser matching the POSIX alpha
character class.
299 300 301 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 299 def alpha match :ALPHA end |
#assert(parser) ⇒ Assert #assert(arg) ⇒ Assert
Create a new assert parser.
185 186 187 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 185 def assert(arg) Assert[to_parser(arg)] end |
#blank ⇒ Match
Returns a parser matching the POSIX blank
character class.
304 305 306 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 304 def blank match :BLANK end |
#cntrl ⇒ Match
Returns a parser matching the POSIX cntrl
character class.
309 310 311 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 309 def cntrl match :CNTRL end |
#digit ⇒ Match
Returns a parser matching the POSIX digit
character class.
314 315 316 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 314 def digit match :DIGIT end |
#direct_action(parser, code) ⇒ DirectAction #direct_action(arg, code) ⇒ DirectAction
Create a new symantic action that evaluates ruby code.
228 229 230 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 228 def direct_action(arg, code) DirectAction[to_parser(arg), code] end |
#disallow(parser) ⇒ Disallow #disallow(arg) ⇒ Disallow
Create a new disallow parser.
197 198 199 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 197 def disallow(arg) Disallow[to_parser(arg)] end |
#dispatch_action(parser) ⇒ DispatchAction #dispatch_action(arg) ⇒ DispatchAction
Create a new symantic action that dispatches to a method.
214 215 216 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 214 def dispatch_action(arg, attrs={}) DispatchAction[to_parser(arg), attrs] end |
#eof ⇒ Object
Returns the eof parser.
202 203 204 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 202 def eof Eof[] end |
#fail(message) ⇒ Fail
Returns a parser that always fails.
274 275 276 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 274 def fail() Fail[:expr, ] end |
#fail_parse(message) ⇒ Fail
Returns a parser that fails the entire parse.
284 285 286 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 284 def fail_parse() Fail[:parse, ] end |
#fail_rule(message) ⇒ Fail
Returns a parser that fails the entire rule.
279 280 281 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 279 def fail_rule() Fail[:rule, ] end |
#graph ⇒ Match
Returns a parser matching the POSIX graph
character class.
319 320 321 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 319 def graph match :GRAPH end |
#label(parser) ⇒ Label #label(arg) ⇒ Label
Create a new labeled parser.
269 270 271 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 269 def label(name, arg) Label[name, to_parser(arg)] end |
#list(term_parser, sep_parser) ⇒ List0 #list(term_arg, sep_arg) ⇒ List0 Also known as: list
Create a new list parser.
160 161 162 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 160 def list0(term_arg, sep_arg) List0[to_parser(term_arg), to_parser(sep_arg)] end |
#list(term_parser, sep_parser) ⇒ List1 #list(term_arg, sep_arg) ⇒ List1
Create a new list1 parser.
173 174 175 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 173 def list1(term_arg, sep_arg) List1[to_parser(term_arg), to_parser(sep_arg)] end |
#lower ⇒ Match
Returns a parser matching the POSIX lower
character class.
324 325 326 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 324 def lower match :LOWER end |
#match(pattern) ⇒ Match #match(literal) ⇒ Match #match(rule_name) ⇒ Apply #match(posix_name) ⇒ Match #match(: EOF) ⇒ Eof
Create a new parser to match a pattern, literal, referenced parse rule, posix character class, or EOF.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 91 def match(arg) case arg when Regexp then Match[arg] when :EOF then eof 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) ⇒ OneOrMore #one_or_more(arg) ⇒ OneOrMore Also known as: some
Create a new one-or-more parser.
147 148 149 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 147 def one_or_more(arg) OneOrMore[to_parser(arg)] end |
#optional(parser) ⇒ Optional #optional(arg) ⇒ Optional
Create a new optional parser.
121 122 123 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 121 def optional(arg) Optional[to_parser(arg)] end |
#print ⇒ Match
Returns a parser matching the POSIX print
character class.
329 330 331 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 329 def print match :PRINT end |
#punct ⇒ Match
Returns a parser matching the POSIX punct
character class.
334 335 336 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 334 def punct match :PUNCT 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 |
#skip(parser) ⇒ Skip #skip(arg) ⇒ Skip
Create a new skip parser.
257 258 259 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 257 def skip(arg) Skip[to_parser(arg)] end |
#space ⇒ Match
Returns a parser matching the POSIX space
character class.
339 340 341 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 339 def space match :SPACE end |
#token(rule_name, &block) ⇒ Rule #token(parser) ⇒ Token #token(arg) ⇒ Token
Create a new token parser or token rule.
241 242 243 244 245 246 247 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 241 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.
344 345 346 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 344 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.
354 355 356 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 354 def word match :WORD end |
#xdigit ⇒ Match
Returns a parser matching the POSIX xdigit
character class.
349 350 351 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 349 def xdigit match :XDIGIT end |
#zero_or_more(parser) ⇒ ZeroOrMore #zero_or_more(arg) ⇒ ZeroOrMore Also known as: any
Create a new zero-or-more parser.
133 134 135 |
# File 'lib/rattler/parsers/parser_dsl.rb', line 133 def zero_or_more(arg) ZeroOrMore[to_parser(arg)] end |