Class: RaddDjur::Grammar

Inherits:
Object
  • Object
show all
Includes:
Parsers
Defined in:
lib/radd_djur/grammar.rb

Defined Under Namespace

Modules: Parsers Classes: Derivs, ParseError, Parsed, Parser, Result

Constant Summary collapse

NO_PARSE =
Result.new

Instance Method Summary collapse

Methods included from Parsers

any_char, char, fail, ret, string

Constructor Details

#initialize(start_symbol, &block) ⇒ Grammar

Returns a new instance of Grammar.



165
166
167
168
169
# File 'lib/radd_djur/grammar.rb', line 165

def initialize(start_symbol, &block)
  @parsers = {}
  @start_symbol = start_symbol
  instance_exec(&block) if block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(mid, *args) ⇒ Object



187
188
189
# File 'lib/radd_djur/grammar.rb', line 187

def method_missing(mid, *args)
  Parser.new(&mid)
end

Instance Method Details

#define(sym, parser = yield)) ⇒ Object



179
180
181
# File 'lib/radd_djur/grammar.rb', line 179

def define(sym, parser = yield)
  @parsers[sym] = parser.to_parser
end

#parse(str) ⇒ Object



171
172
173
174
175
176
177
# File 'lib/radd_djur/grammar.rb', line 171

def parse(str)
  result = Derivs.new(self, str).send(@start_symbol).force
  if !result.succeeded?
    raise ParseError, "parse error"
  end
  result.value
end

#parser(sym) ⇒ Object



183
184
185
# File 'lib/radd_djur/grammar.rb', line 183

def parser(sym)
  @parsers[sym]
end