Module: Pegarus

Defined in:
lib/pegarus/ast/if.rb,
lib/pegarus/ast/any.rb,
lib/pegarus/ast/set.rb,
lib/pegarus/machine.rb,
lib/pegarus/version.rb,
lib/pegarus/ast/never.rb,
lib/pegarus/evaluator.rb,
lib/pegarus/ast/always.rb,
lib/pegarus/ast/choice.rb,
lib/pegarus/ast/unless.rb,
lib/pegarus/ast/grammar.rb,
lib/pegarus/ast/pattern.rb,
lib/pegarus/ast/product.rb,
lib/pegarus/ast/variable.rb,
lib/pegarus/ast/any_range.rb,
lib/pegarus/ast/character.rb,
lib/pegarus/machine/state.rb,
lib/pegarus/ast/difference.rb,
lib/pegarus/machine/compiler.rb,
lib/pegarus/ast/concatenation.rb,
lib/pegarus/machine/generator.rb,
lib/pegarus/rubinius/compiler.rb,
lib/pegarus/parser/parse_error.rb,
lib/pegarus/rubinius/generator.rb,
lib/pegarus/ast/character_range.rb,
lib/pegarus/machine/interpreter.rb,
lib/pegarus/machine/instructions.rb

Defined Under Namespace

Modules: Machine, ParsingMachine, Rubinius Classes: Always, Any, AnyRange, BinaryOp, Character, CharacterRange, Choice, Concatenation, Difference, Evaluator, Grammar, If, Never, ParseError, Pattern, Product, Set, UnaryOp, Unless, Variable

Constant Summary collapse

VERSION =
"0.2.0"

Class Method Summary collapse

Class Method Details

.grammar(spec = nil, pattern = nil) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/pegarus/ast/grammar.rb', line 2

def grammar(spec=nil, pattern=nil)
  case spec
  when Symbol
    Grammar.new Variable.new(spec, pattern)
  when Variable
    Grammar.new spec
  when Pattern
    Grammar.new Variable.new(:S, spec)
  when nil
    Grammar.new
  else
    Grammar.new Variable.new(:S, pattern)
  end
end

.pattern(spec) ⇒ Object

Accepts a Ruby object representation of simple PEG “atoms” and returns an AST node.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/pegarus/ast/pattern.rb', line 4

def pattern(spec)
  case spec
  when Array
    Set.new spec.join
  when Integer
    Any.new spec
  when String
    Character.new spec
  when Range
    case spec.first
    when Integer
      AnyRange.new spec
    when String
      CharacterRange.new spec
    end
  when Symbol
    variable spec
  when Pattern
    spec
  when true
    Always.new
  when false
    Never.new
  when nil
    nil
  else
    raise ParseError, "unknown specification type for Pattern: #{spec.inspect}"
  end
end

.variable(name, pattern = nil) ⇒ Object



2
3
4
# File 'lib/pegarus/ast/variable.rb', line 2

def variable(name, pattern=nil)
  Pegarus::Variable.new name, pattern
end