Class: OedipusLex::Group

Inherits:
Struct
  • Object
show all
Defined in:
lib/oedipus_lex.rb

Overview

A group allows you to group up multiple rules under a single regular prefix expression, allowing optimized code to be generated that skips over all actions if the prefix isn’t matched.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start) ⇒ Group

:nodoc:



189
190
191
# File 'lib/oedipus_lex.rb', line 189

def initialize start # :nodoc:
  super(start, [])
end

Instance Attribute Details

#regexObject Also known as: start_state

Returns the value of attribute regex

Returns:

  • (Object)

    the current value of regex



176
177
178
# File 'lib/oedipus_lex.rb', line 176

def regex
  @regex
end

#rulesObject

Returns the value of attribute rules

Returns:

  • (Object)

    the current value of rules



176
177
178
# File 'lib/oedipus_lex.rb', line 176

def rules
  @rules
end

Class Method Details

.[](start, *subrules) ⇒ Object

A convenience method to create a new group with a start and given subrules.



183
184
185
186
187
# File 'lib/oedipus_lex.rb', line 183

def self.[] start, *subrules
  r = new start.inspect
  r.rules.concat subrules
  r
end

Instance Method Details

#<<(rule) ⇒ Object

Add a rule to this group.



196
197
198
199
# File 'lib/oedipus_lex.rb', line 196

def << rule
  rules << rule
  nil
end

#pretty_print(pp) ⇒ Object

:nodoc:



213
214
215
216
217
218
219
220
# File 'lib/oedipus_lex.rb', line 213

def pretty_print pp # :nodoc:
  pp.text "Group"
  pp.group 2, "[", "]" do
    pp.seplist([regex] + rules, lambda { pp.comma_breakable }, :each) { |v|
      pp.send(String === v ? :text : :pp, v)
    }
  end
end

#to_ruby(state, predicates, exclusive) ⇒ Object

:nodoc:



201
202
203
204
205
206
207
208
209
210
211
# File 'lib/oedipus_lex.rb', line 201

def to_ruby state, predicates, exclusive # :nodoc:
  [
   "when ss.match?(#{regex}) then",
   "  case",
   rules.map { |subrule|
     s = subrule.to_ruby(state, predicates, exclusive)
     s && s.join("\n").gsub(/^/, "  ")
   }.compact,
   "  end # group #{regex}"
  ]
end