Class: OedipusLex::Rule
- Inherits:
-
Struct
- Object
- Struct
- OedipusLex::Rule
- Defined in:
- lib/oedipus_lex.rb
Overview
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
Instance Attribute Summary collapse
-
#action ⇒ Object
Returns the value of attribute action.
-
#group ⇒ Object
(also: #group?)
What group this rule is in, if any.
-
#regexp ⇒ Object
Returns the value of attribute regexp.
-
#start_state ⇒ Object
Returns the value of attribute start_state.
Class Method Summary collapse
-
.[](start, regexp, action) ⇒ Object
A simple constructor.
Instance Method Summary collapse
-
#initialize(start_state, regexp, action) ⇒ Rule
constructor
:nodoc:.
-
#pretty_print(pp) ⇒ Object
:nodoc:.
-
#to_ruby(state, predicates, exclusive) ⇒ Object
Generate equivalent ruby code for the rule.
Constructor Details
#initialize(start_state, regexp, action) ⇒ Rule
:nodoc:
115 116 117 118 |
# File 'lib/oedipus_lex.rb', line 115 def initialize start_state, regexp, action # :nodoc: super self.group = nil end |
Instance Attribute Details
#action ⇒ Object
Returns the value of attribute action
100 101 102 |
# File 'lib/oedipus_lex.rb', line 100 def action @action end |
#group ⇒ Object Also known as: group?
What group this rule is in, if any.
104 105 106 |
# File 'lib/oedipus_lex.rb', line 104 def group @group end |
#regexp ⇒ Object
Returns the value of attribute regexp
100 101 102 |
# File 'lib/oedipus_lex.rb', line 100 def regexp @regexp end |
#start_state ⇒ Object
Returns the value of attribute start_state
100 101 102 |
# File 'lib/oedipus_lex.rb', line 100 def start_state @start_state end |
Class Method Details
.[](start, regexp, action) ⇒ Object
A simple constructor
111 112 113 |
# File 'lib/oedipus_lex.rb', line 111 def self.[] start, regexp, action new start, regexp.inspect, action end |
Instance Method Details
#pretty_print(pp) ⇒ Object
:nodoc:
159 160 161 162 163 164 165 166 167 168 |
# File 'lib/oedipus_lex.rb', line 159 def pretty_print pp # :nodoc: pp.text "Rule" pp.group 2, "[", "]" do pp.pp start_state pp.text ", " pp.text regexp pp.text ", " pp.send(action ? :text : :pp, action) end end |
#to_ruby(state, predicates, exclusive) ⇒ Object
Generate equivalent ruby code for the rule.
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# 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 |