Class: GhostWheel::ParserBuilder::Ruby

Inherits:
Object
  • Object
show all
Defined in:
lib/ghost_wheel/parser_builder/ruby.rb

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Ruby

Returns a new instance of Ruby.


6
7
8
9
10
# File 'lib/ghost_wheel/parser_builder/ruby.rb', line 6

def initialize(&block)
  @parser = Parser.new

  build(&block) unless block.nil?
end

Instance Method Details

#alt(*expressions, &transformer) ⇒ Object


52
53
54
55
56
57
# File 'lib/ghost_wheel/parser_builder/ruby.rb', line 52

def alt(*expressions, &transformer)
  wrap_transform(
    Expression::Alternation.new(*expand_expressions(*expressions)),
    &transformer
  )
end

#build(&block) ⇒ Object


12
13
14
15
16
17
18
# File 'lib/ghost_wheel/parser_builder/ruby.rb', line 12

def build(&block)
  if block.arity == 1
    block[self]
  else
    instance_eval(&block)
  end
end

#count(exact_count, expression, &transformer) ⇒ Object


108
109
110
111
112
113
114
115
# File 'lib/ghost_wheel/parser_builder/ruby.rb', line 108

def count(exact_count, expression, &transformer)
  wrap_transform(
    Expression::Repetition.new( expand_expression(expression),
                                exact_count,
                                exact_count ),
    &transformer
  )
end

#empty(&transformer) ⇒ Object


59
60
61
# File 'lib/ghost_wheel/parser_builder/ruby.rb', line 59

def empty(&transformer)
  wrap_transform(Expression::Empty.instance, &transformer)
end

#eof(&transformer) ⇒ Object


133
134
135
# File 'lib/ghost_wheel/parser_builder/ruby.rb', line 133

def eof(&transformer)
  wrap_transform(Expression::EndOfFile.instance, &transformer)
end

#lit(*args, &transformer) ⇒ Object


36
37
38
39
# File 'lib/ghost_wheel/parser_builder/ruby.rb', line 36

def lit(*args, &transformer)
  args.reverse! if args.size == 2 and [true, false].include?(args.first)
  wrap_transform(Expression::Literal.new(*args), &transformer)
end

#look(*args, &transformer) ⇒ Object


117
118
119
120
121
122
123
124
# File 'lib/ghost_wheel/parser_builder/ruby.rb', line 117

def look(*args, &transformer)
  args.reverse! if args.size == 2 and [true, false].include?(args.first)
  wrap_transform(
    Expression::LookAhead.new( expand_expression(args.first),
                               *args[1..-1] ),
    &transformer
  )
end

#max(maximum_count, expression, &transformer) ⇒ Object


90
91
92
93
94
95
96
97
# File 'lib/ghost_wheel/parser_builder/ruby.rb', line 90

def max(maximum_count, expression, &transformer)
  wrap_transform(
    Expression::Repetition.new( expand_expression(expression),
                                0,
                                maximum_count ),
    &transformer
  )
end

#min(minimum_count, expression, &transformer) ⇒ Object


82
83
84
85
86
87
88
# File 'lib/ghost_wheel/parser_builder/ruby.rb', line 82

def min(minimum_count, expression, &transformer)
  wrap_transform(
    Expression::Repetition.new( expand_expression(expression),
                                minimum_count ),
    &transformer
  )
end

#minmax(minimum_count, maximum_count, expression, &transformer) ⇒ Object


99
100
101
102
103
104
105
106
# File 'lib/ghost_wheel/parser_builder/ruby.rb', line 99

def minmax(minimum_count, maximum_count, expression, &transformer)
  wrap_transform(
    Expression::Repetition.new( expand_expression(expression),
                                minimum_count,
                                maximum_count ),
    &transformer
  )
end

#nlook(expression, &transformer) ⇒ Object


126
127
128
129
130
131
# File 'lib/ghost_wheel/parser_builder/ruby.rb', line 126

def nlook(expression, &transformer)
  wrap_transform(
    Expression::LookAhead.new(expand_expression(expression), true),
    &transformer
  )
end

#oplus(expression, &transformer) ⇒ Object


75
76
77
78
79
80
# File 'lib/ghost_wheel/parser_builder/ruby.rb', line 75

def oplus(expression, &transformer)
  wrap_transform(
    Expression::Repetition.new(expand_expression(expression), 1),
    &transformer
  )
end

#opt(expression, &transformer) ⇒ Object


63
64
65
66
# File 'lib/ghost_wheel/parser_builder/ruby.rb', line 63

def opt(expression, &transformer)
  wrap_transform( Expression::Optional.new(expand_expression(expression)),
                  &transformer )
end

#parser(name, expression, &transformer) ⇒ Object


26
27
28
29
# File 'lib/ghost_wheel/parser_builder/ruby.rb', line 26

def parser(name, expression, &transformer)
  rule(name, expression, &transformer)
  @parser.build_parser(name)
end

#query(expression, &block) ⇒ Object


137
138
139
# File 'lib/ghost_wheel/parser_builder/ruby.rb', line 137

def query(expression, &block)
  Expression::Query.new(expand_expression(expression), &block)
end

#ref(name, &transformer) ⇒ Object


31
32
33
34
# File 'lib/ghost_wheel/parser_builder/ruby.rb', line 31

def ref(name, &transformer)
  @parser[name] ||= ::GhostWheel::Expression::Rule.new
  wrap_transform(@parser[name], &transformer)
end

#rule(name, expression, &transformer) ⇒ Object


20
21
22
23
24
# File 'lib/ghost_wheel/parser_builder/ruby.rb', line 20

def rule(name, expression, &transformer)
  @parser[name] ||= ::GhostWheel::Expression::Rule.new
  @parser[name].expression =
    wrap_transform(expand_expression(expression), &transformer)
end

#seq(*expressions, &transformer) ⇒ Object


45
46
47
48
49
50
# File 'lib/ghost_wheel/parser_builder/ruby.rb', line 45

def seq(*expressions, &transformer)
  wrap_transform(
    Expression::Sequence.new(*expand_expressions(*expressions)),
    &transformer
  )
end

#skip(literal, &transformer) ⇒ Object


41
42
43
# File 'lib/ghost_wheel/parser_builder/ruby.rb', line 41

def skip(literal, &transformer)
  wrap_transform(Expression::Literal.new(literal, true), &transformer)
end

#to_parserObject


145
146
147
# File 'lib/ghost_wheel/parser_builder/ruby.rb', line 145

def to_parser
  @parser
end

#trans(expression, &block) ⇒ Object


141
142
143
# File 'lib/ghost_wheel/parser_builder/ruby.rb', line 141

def trans(expression, &block)
  Expression::Transform.new(expand_expression(expression), &block)
end

#zplus(expression, &transformer) ⇒ Object


68
69
70
71
72
73
# File 'lib/ghost_wheel/parser_builder/ruby.rb', line 68

def zplus(expression, &transformer)
  wrap_transform(
    Expression::Repetition.new(expand_expression(expression)),
    &transformer
  )
end