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



337
338
339
# File 'lib/rattler/parsers/parser_dsl.rb', line 337

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



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.

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:



189
190
191
# File 'lib/rattler/parsers/parser_dsl.rb', line 189

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



347
348
349
# File 'lib/rattler/parsers/parser_dsl.rb', line 347

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



352
353
354
# File 'lib/rattler/parsers/parser_dsl.rb', line 352

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



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.

Overloads:

  • #direct_action(parser, code) ⇒ DirectAction

    Returns a new semantic action.

    Returns:

  • #direct_action(arg, code) ⇒ DirectAction

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

    Returns:

    • (DirectAction)

      a new semantic action using arg to define a match parser

    See Also:



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.

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:



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.

Overloads:

  • #dispatch_action(parser) ⇒ DispatchAction

    Returns a new semantic action.

    Returns:

  • #dispatch_action(arg) ⇒ DispatchAction

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

    Returns:

    • (DispatchAction)

      a new semantic action using arg to define a match parser

    See Also:



223
224
225
# File 'lib/rattler/parsers/parser_dsl.rb', line 223

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

#eObject



211
212
213
# File 'lib/rattler/parsers/parser_dsl.rb', line 211

def e
  ESymbol[]
end

#eofObject

Returns the eof parser.

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.

Returns:

  • (Fail)

    a parser that always fails



317
318
319
# File 'lib/rattler/parsers/parser_dsl.rb', line 317

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



327
328
329
# File 'lib/rattler/parsers/parser_dsl.rb', line 327

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



322
323
324
# File 'lib/rattler/parsers/parser_dsl.rb', line 322

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



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.

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:



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.

Overloads:

  • #list(term_parser, sep_parser, min, max) ⇒ ListParser

    Returns a new list parser.

    Returns:

  • #list(term_arg, sep_arg, min, max) ⇒ ListParser

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

    Returns:

    • (ListParser)

      a new list parser using args to define a match parsers

    See Also:



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

#lowerMatch

Returns a parser matching the POSIX lower character class.

Returns:

  • (Match)

    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.

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:

  • #match(: E) ⇒ ESymbol

    Returns the ESymbol singleton.

    Parameters:

    • :E

    Returns:



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.

Overloads:

  • #one_or_more(parser) ⇒ Repeat

    Returns a new one-or-more parser.

    Returns:

    • (Repeat)

      a new one-or-more parser

  • #one_or_more(arg) ⇒ Repeat

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

    Returns:

    • (Repeat)

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

    See Also:



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.

Overloads:

  • #optional(parser) ⇒ Repeat

    Returns a new optional parser.

    Returns:

    • (Repeat)

      a new optional parser

  • #optional(arg) ⇒ Repeat

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

    Returns:

    • (Repeat)

      a new optional parser using arg to define a match parser

    See Also:



126
127
128
# File 'lib/rattler/parsers/parser_dsl.rb', line 126

def optional(arg)
  Repeat[to_parser(arg), 0, 1]
end

Returns a parser matching the POSIX print character class.

Returns:

  • (Match)

    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

#punctMatch

Returns a parser matching the POSIX punct character class.

Returns:

  • (Match)

    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.

Overloads:

  • #one_or_more(parser, min, max) ⇒ Repeat

    Returns a new repeat parser with the given bounds.

    Returns:

    • (Repeat)

      a new repeat parser with the given bounds

  • #one_or_more(arg, min, max) ⇒ Repeat

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

    Returns:

    • (Repeat)

      a new repeat parser using arg to define a match parser

    See Also:



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

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

#semantic_assert(parser, code) ⇒ SemanticAssert #semantic_assert(arg, code) ⇒ SemanticAssert

Create a new positive semantic predicate.

Overloads:

  • #semantic_assert(parser, code) ⇒ SemanticAssert

    Returns a new positive Semantic predicate.

    Returns:

  • #semantic_assert(arg, code) ⇒ SemanticAssert

    Returns a new positive Semantic predicate using arg to define a match parser.

    Returns:

    • (SemanticAssert)

      a new positive Semantic predicate using arg to define a match parser

    See Also:



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.

Overloads:

  • #semantic_disallow(parser, code) ⇒ SemanticDisallow

    Returns a new negative Semantic predicate.

    Returns:

  • #semantic_disallow(arg, code) ⇒ SemanticDisallow

    Returns a new negative Semantic predicate using arg to define a match parser.

    Returns:

    • (SemanticDisallow)

      a new negative Semantic predicate using arg to define a match parser

    See Also:



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.

Overloads:

  • #side_effect(parser, code) ⇒ SideEffect

    Returns a new semantic action.

    Returns:

  • #side_effect(arg, code) ⇒ SideEffect

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

    Returns:

    • (SideEffect)

      a new semantic action using arg to define a match parser

    See Also:



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.

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:



300
301
302
# File 'lib/rattler/parsers/parser_dsl.rb', line 300

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



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.

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:



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

#upperMatch

Returns a parser matching the POSIX upper character class.

Returns:

  • (Match)

    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 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



397
398
399
# File 'lib/rattler/parsers/parser_dsl.rb', line 397

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



392
393
394
# File 'lib/rattler/parsers/parser_dsl.rb', line 392

def xdigit
  match :XDIGIT
end

#zero_or_more(parser) ⇒ Repeat #zero_or_more(arg) ⇒ Repeat Also known as: any

Create a new zero-or-more parser.

Overloads:

  • #zero_or_more(parser) ⇒ Repeat

    Returns a new zero-or-more parser.

    Returns:

    • (Repeat)

      a new zero-or-more parser

  • #zero_or_more(arg) ⇒ Repeat

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

    Returns:

    • (Repeat)

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

    See Also:



138
139
140
# File 'lib/rattler/parsers/parser_dsl.rb', line 138

def zero_or_more(arg)
  Repeat[to_parser(arg), 0, nil]
end