Class: Pegarus::Pattern

Inherits:
Object
  • Object
show all
Defined in:
lib/pegarus/ast/pattern.rb

Overview

Class Pattern is the base class for all nodes used to represent the AST for a PEG. Following LPEG, the base is a pattern and not a grammar.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.engineObject



60
61
62
63
64
65
66
67
# File 'lib/pegarus/ast/pattern.rb', line 60

def self.engine
  unless @engine
    require 'pegarus/evaluator'
    @engine = Evaluator
  end

  @engine
end

.select_engine(klass) ⇒ Object



69
70
71
# File 'lib/pegarus/ast/pattern.rb', line 69

def self.select_engine(klass)
  @engine = klass
end

Instance Method Details

#*(other) ⇒ Object



98
99
100
# File 'lib/pegarus/ast/pattern.rb', line 98

def *(other)
  Product.new self, other
end

#+(other) ⇒ Object



90
91
92
# File 'lib/pegarus/ast/pattern.rb', line 90

def +(other)
  Concatenation.new self, other
end

#+@Object



102
103
104
# File 'lib/pegarus/ast/pattern.rb', line 102

def +@
  If.new self
end

#-(other) ⇒ Object



94
95
96
# File 'lib/pegarus/ast/pattern.rb', line 94

def -(other)
  Difference.new self, other
end

#-@Object



106
107
108
# File 'lib/pegarus/ast/pattern.rb', line 106

def -@
  Unless.new self
end

#/(other) ⇒ Object

Pattern operators



86
87
88
# File 'lib/pegarus/ast/pattern.rb', line 86

def /(other)
  Choice.new self, other
end

#graphObject



73
74
75
76
77
78
79
# File 'lib/pegarus/ast/pattern.rb', line 73

def graph
  if defined? Rubinius
    Rubinius::AST::AsciiGrapher.new(self, Pattern).print
  else
    inspect
  end
end

#match(subject) ⇒ Object

Executes the PEG pattern on subject. If the pattern matches a prefix of subject, returns the index one character past the last matching character. Otherwise, returns nil.

The pattern AST is purely a representation. By default, it includes no machinery to execute the pattern over the subject. All execution is provided by other facilities, for example, the AST evaluator and the parsing machine compiler and interpreter. These facilities are called “engines”.

The Pattern class defaults to the AST Evaluator engine. The protocol requires the engine class to respond to .new_exector taking the AST root node and the subject as arguments. The default Pattern#match method is essentially a trampoline. It calls new_executor and expects that method to replace #match on the root node and then tail call to the new #match method installed by the engine.



56
57
58
# File 'lib/pegarus/ast/pattern.rb', line 56

def match(subject)
  Pattern.engine.new_executor self, subject
end

#visit(visitor) ⇒ Object



81
82
# File 'lib/pegarus/ast/pattern.rb', line 81

def visit(visitor)
end