Class: Rattler::Parsers::ParserDSL

Inherits:
Object
  • Object
show all
Defined in:
lib/rattler/parsers/parser_dsl.rb

Overview

ParserDSL defines a simple DSL for defining parsers.

Author:

  • Jason Arhart

Class Method Summary collapse

Instance Method Summary collapse

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(options = {}) #:nodoc:
  @rules = options[:rules] || []
  @options = options
  @ws = options[:ws]
end

Class Method Details

.rules(options = {}, &block) ⇒ Rattler::Parsers::RuleSet

Define parse rules with the given block

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • ws (Parser) — default: nil

    a parser to be used to skip whitespace

Returns:



25
26
27
# File 'lib/rattler/parsers/parser_dsl.rb', line 25

def self.rules(options = {}, &block)
  self.new(options).rules(&block)
end

Instance Method Details

#alnumMatch

Returns a parser matching the POSIX alnum character class.

Returns:

  • (Match)

    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

#alphaMatch

Returns a parser matching the POSIX alpha character class.

Returns:

  • (Match)

    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.

Overloads:

  • #assert(parser) ⇒ Assert

    Returns a new assert parser.

    Returns:

    • (Assert)

      a new assert parser

  • #assert(arg) ⇒ Assert

    Returns a new assert parser using arg to define a match parser.

    Returns:

    • (Assert)

      a new assert parser using arg to define a match parser

    See Also:



185
186
187
# File 'lib/rattler/parsers/parser_dsl.rb', line 185

def assert(arg)
  Assert[to_parser(arg)]
end

#blankMatch

Returns a parser matching the POSIX blank character class.

Returns:

  • (Match)

    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

#cntrlMatch

Returns a parser matching the POSIX cntrl character class.

Returns:

  • (Match)

    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

#digitMatch

Returns a parser matching the POSIX digit character class.

Returns:

  • (Match)

    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.

Overloads:

  • #direct_action(parser, code) ⇒ DirectAction

    Returns a new symantic action.

    Returns:

  • #direct_action(arg, code) ⇒ DirectAction

    Returns a new symantic action using arg to define a match parser.

    Returns:

    • (DirectAction)

      a new symantic action using arg to define a match parser

    See Also:



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.

Overloads:

  • #disallow(parser) ⇒ Disallow

    Returns a new disallow parser.

    Returns:

  • #disallow(arg) ⇒ Disallow

    Returns a new disallow parser using arg to define a match parser.

    Returns:

    • (Disallow)

      a new disallow parser using arg to define a match parser

    See Also:



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.

Overloads:

  • #dispatch_action(parser) ⇒ DispatchAction

    Returns a new symantic action.

    Returns:

  • #dispatch_action(arg) ⇒ DispatchAction

    Returns a new symantic action using arg to define a match parser.

    Returns:

    • (DispatchAction)

      a new symantic action using arg to define a match parser

    See Also:



214
215
216
# File 'lib/rattler/parsers/parser_dsl.rb', line 214

def dispatch_action(arg, attrs={})
  DispatchAction[to_parser(arg), attrs]
end

#eofObject

Returns the eof parser.

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.

Returns:

  • (Fail)

    a parser that always fails



274
275
276
# File 'lib/rattler/parsers/parser_dsl.rb', line 274

def fail(message)
  Fail[:expr, message]
end

#fail_parse(message) ⇒ Fail

Returns a parser that fails the entire parse.

Returns:

  • (Fail)

    a parser that fails the entire parse



284
285
286
# File 'lib/rattler/parsers/parser_dsl.rb', line 284

def fail_parse(message)
  Fail[:parse, message]
end

#fail_rule(message) ⇒ Fail

Returns a parser that fails the entire rule.

Returns:

  • (Fail)

    a parser that fails the entire rule



279
280
281
# File 'lib/rattler/parsers/parser_dsl.rb', line 279

def fail_rule(message)
  Fail[:rule, message]
end

#graphMatch

Returns a parser matching the POSIX graph character class.

Returns:

  • (Match)

    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.

Overloads:

  • #label(parser) ⇒ Label

    Returns a new labeled parser.

    Returns:

    • (Label)

      a new labeled parser

  • #label(arg) ⇒ Label

    Returns a new labeled parser using arg to define a match parser.

    Returns:

    • (Label)

      a new labeled parser using arg to define a match parser

    See Also:



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.

Overloads:

  • #list(term_parser, sep_parser) ⇒ List0

    Returns a new list parser.

    Returns:

    • (List0)

      a new list parser

  • #list(term_arg, sep_arg) ⇒ List0

    Returns a new list parser using args to define a match parsers.

    Returns:

    • (List0)

      a new list parser using args to define a match parsers

    See Also:



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.

Overloads:

  • #list(term_parser, sep_parser) ⇒ List1

    Returns a new list1 parser.

    Returns:

    • (List1)

      a new list1 parser

  • #list(term_arg, sep_arg) ⇒ List1

    Returns a new list1 parser using args to define match parsers.

    Returns:

    • (List1)

      a new list1 parser using args to define match parsers

    See Also:



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

#lowerMatch

Returns a parser matching the POSIX lower character class.

Returns:

  • (Match)

    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.

Overloads:

  • #match(pattern) ⇒ Match

    Returns a new match parser.

    Parameters:

    • pattern (Regexp)

      the pattern to match

    Returns:

    • (Match)

      a new match parser

  • #match(literal) ⇒ Match

    Returns a new match parser that matches exactly literal.

    Parameters:

    • literal (String)

      the literal to match

    Returns:

    • (Match)

      a new match parser that matches exactly literal

  • #match(rule_name) ⇒ Apply

    Returns a new apply-rule parser.

    Parameters:

    • rule_name (Symbol)

      the rule name to match

    Returns:

    • (Apply)

      a new apply-rule parser

  • #match(posix_name) ⇒ Match

    Returns a new match parser that matches the character class.

    Parameters:

    • posix_name (Symbol)

      upper-case name of a posix character class

    Returns:

    • (Match)

      a new match parser that matches the character class

  • #match(: EOF) ⇒ Eof

    Returns the Eof singleton.

    Parameters:

    • :EOF

    Returns:



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.

Overloads:

  • #one_or_more(parser) ⇒ OneOrMore

    Returns a new one-or-more parser.

    Returns:

  • #one_or_more(arg) ⇒ OneOrMore

    Returns a new one-or-more parser using arg to define a match parser.

    Returns:

    • (OneOrMore)

      a new one-or-more parser using arg to define a match parser

    See Also:



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.

Overloads:

  • #optional(parser) ⇒ Optional

    Returns a new optional parser.

    Returns:

  • #optional(arg) ⇒ Optional

    Returns a new optional parser using arg to define a match parser.

    Returns:

    • (Optional)

      a new optional parser using arg to define a match parser

    See Also:



121
122
123
# File 'lib/rattler/parsers/parser_dsl.rb', line 121

def optional(arg)
  Optional[to_parser(arg)]
end

Returns a parser matching the POSIX print character class.

Returns:

  • (Match)

    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

#punctMatch

Returns a parser matching the POSIX punct character class.

Returns:

  • (Match)

    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

Parameters:

  • name (Symbol)

    the name for the rule

Returns:

  • (Rule)

    the rule defined in the block



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

Returns:

  • (RuleSet)

    the rules defined in the block



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.

Overloads:

  • #skip(parser) ⇒ Skip

    Returns a new skip parser.

    Returns:

    • (Skip)

      a new skip parser

  • #skip(arg) ⇒ Skip

    Returns a new skip parser using arg to define a match parser.

    Returns:

    • (Skip)

      a new skip parser using arg to define a match parser

    See Also:



257
258
259
# File 'lib/rattler/parsers/parser_dsl.rb', line 257

def skip(arg)
  Skip[to_parser(arg)]
end

#spaceMatch

Returns a parser matching the POSIX space character class.

Returns:

  • (Match)

    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.

Overloads:

  • #token(rule_name, &block) ⇒ Rule

    Returns a new token rule.

    Returns:

    • (Rule)

      a new token rule

  • #token(parser) ⇒ Token

    Returns a new token parser.

    Returns:

    • (Token)

      a new token parser

  • #token(arg) ⇒ Token

    Returns a new token parser using arg to define a match parser.

    Returns:

    • (Token)

      a new token parser using arg to define a match parser

    See Also:



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

#upperMatch

Returns a parser matching the POSIX upper character class.

Returns:

  • (Match)

    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 with_options(options, &block) #:nodoc:
  dsl = self.class.new(@options.merge(:rules => @rules).merge(options))
  dsl.instance_exec(dsl, &block)
end

#with_ws(ws, &block) ⇒ Object

Evaluate the given block using ws to skip whitespace

Parameters:

  • ws (Parser)

    the parser to be used to skip whitespace



45
46
47
# File 'lib/rattler/parsers/parser_dsl.rb', line 45

def with_ws(ws, &block)
  with_options(:ws => to_parser(ws), &block)
end

#wordMatch

Returns a parser matching the word character class.

Returns:

  • (Match)

    a parser matching the word character class



354
355
356
# File 'lib/rattler/parsers/parser_dsl.rb', line 354

def word
  match :WORD
end

#xdigitMatch

Returns a parser matching the POSIX xdigit character class.

Returns:

  • (Match)

    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.

Overloads:

  • #zero_or_more(parser) ⇒ ZeroOrMore

    Returns a new zero-or-more parser.

    Returns:

  • #zero_or_more(arg) ⇒ ZeroOrMore

    Returns a new zero-or-more parser using arg to define a match parser.

    Returns:

    • (ZeroOrMore)

      a new zero-or-more parser using arg to define a match parser

    See Also:



133
134
135
# File 'lib/rattler/parsers/parser_dsl.rb', line 133

def zero_or_more(arg)
  ZeroOrMore[to_parser(arg)]
end