class OedipusLex::Rule

A Rule represents the main component of Oedipus Lex. These are the things that “get stuff done” at the lexical level. They consist of:

+ an optional required start state symbol or predicate method name + a regexp to match on + an optional action method or block

Attributes

group[RW]

What group this rule is in, if any.

group?[RW]

What group this rule is in, if any.

Public Class Methods

[](start, regexp, action) click to toggle source

A simple constructor

# File lib/oedipus_lex.rb, line 111
def self.[] start, regexp, action
  new start, regexp.inspect, action
end

Public Instance Methods

to_ruby(state, predicates, exclusive) click to toggle source

Generate equivalent ruby code for the rule.

# File lib/oedipus_lex.rb, line 125
def to_ruby state, predicates, exclusive
  return unless group? or
    start_state == state or
    (state.nil? and predicates.include? start_state)

  uses_text = false

  body =
    case action
    when nil, false then
      "  # do nothing"
    when /^\{/ then
      uses_text = action =~ /\btext\b/
      "  action #{action}"
    when /^:/, "nil" then
      "  [:state, #{action}]"
    else # plain method name
      uses_text = true
      "  #{action} text"
    end

  check = uses_text ? "text = ss.scan(#{regexp})" : "ss.skip(#{regexp})"

  cond = if exclusive or not start_state then
           check
         elsif /^:/.match?(start_state) then
           "(state == #{start_state}) && (#{check})"
         else # predicate method
           "#{start_state} && (#{check})"
         end

  ["when #{cond} then", body]
end