Class: Aurum::Pattern
- Inherits:
-
Object
- Object
- Aurum::Pattern
- Defined in:
- lib/aurum/lexical_table_generator.rb
Instance Attribute Summary collapse
-
#accept ⇒ Object
readonly
Returns the value of attribute accept.
-
#automata ⇒ Object
readonly
Returns the value of attribute automata.
Class Method Summary collapse
- .concat(*patterns) ⇒ Object
- .from_char_set(set) ⇒ Object
- .from_enum(enum_literal) ⇒ Object
- .from_string(literal) ⇒ Object
Instance Method Summary collapse
- #[](least, most = least) ⇒ Object
-
#initialize(automata, accept) ⇒ Pattern
constructor
A new instance of Pattern.
- #iterate ⇒ Object (also: #one_or_more)
- #kleene ⇒ Object (also: #zero_or_more)
- #negate ⇒ Object (also: #not)
- #opt ⇒ Object (also: #zero_or_one)
- #|(other) ⇒ Object
- #~ ⇒ Object
Constructor Details
#initialize(automata, accept) ⇒ Pattern
Returns a new instance of Pattern.
122 123 124 |
# File 'lib/aurum/lexical_table_generator.rb', line 122 def initialize automata, accept @automata, @accept = automata, accept end |
Instance Attribute Details
#accept ⇒ Object (readonly)
Returns the value of attribute accept.
95 96 97 |
# File 'lib/aurum/lexical_table_generator.rb', line 95 def accept @accept end |
#automata ⇒ Object (readonly)
Returns the value of attribute automata.
95 96 97 |
# File 'lib/aurum/lexical_table_generator.rb', line 95 def automata @automata end |
Class Method Details
.concat(*patterns) ⇒ Object
114 115 116 117 118 119 120 |
# File 'lib/aurum/lexical_table_generator.rb', line 114 def self.concat *patterns automata, index = Automata.new, 0 patterns.each do |pattern| index = automata.connect(index, Epsilon, automata.merge!(pattern.automata)) + pattern.accept end new automata, index end |
.from_char_set(set) ⇒ Object
102 103 104 105 106 |
# File 'lib/aurum/lexical_table_generator.rb', line 102 def self.from_char_set set automata = Automata.new 2 automata.connect 0, set, 1 new automata, 1 end |
.from_enum(enum_literal) ⇒ Object
108 109 110 111 112 |
# File 'lib/aurum/lexical_table_generator.rb', line 108 def self.from_enum enum_literal automata = Automata.new enum_literal.length + 2 enum_literal.each_byte {|byte| automata.connect 0, CharacterSet::Interval.new(byte).to_char_set, 1} new automata, 1 end |
.from_string(literal) ⇒ Object
96 97 98 99 100 |
# File 'lib/aurum/lexical_table_generator.rb', line 96 def self.from_string literal automata, index = Automata.new(literal.length + 1), 0 literal.each_byte {|byte|automata.connect index, CharacterSet::Interval.new(byte).to_char_set, (index += 1)} new automata, index end |
Instance Method Details
#[](least, most = least) ⇒ Object
163 164 165 |
# File 'lib/aurum/lexical_table_generator.rb', line 163 def [] least, most = least Pattern.concat *([self] * least + [self.opt] * (most-least)) end |
#iterate ⇒ Object Also known as: one_or_more
134 135 136 137 138 |
# File 'lib/aurum/lexical_table_generator.rb', line 134 def iterate iterate_automata = @automata.dup iterate_automata.connect @accept, Epsilon, 0 Pattern.new iterate_automata, @accept end |
#kleene ⇒ Object Also known as: zero_or_more
126 127 128 129 130 131 |
# File 'lib/aurum/lexical_table_generator.rb', line 126 def kleene kleene_automata = @automata.dup kleene_automata.connect 0, Epsilon, @accept kleene_automata.connect @accept, Epsilon, 0 Pattern.new kleene_automata, @accept end |
#negate ⇒ Object Also known as: not
148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/aurum/lexical_table_generator.rb', line 148 def negate deterministic, accepts = automata.determinize [@accept] sink = deterministic.new_state deterministic.connect sink, CharacterSet.any, sink sink.times do |state| joint = CharacterSet.any deterministic.table[state].each {|tran| joint.delete tran.symbols} deterministic.connect state, joint, sink unless joint.empty? end accept = deterministic.new_state accept.times {|state| deterministic.connect state, Epsilon, accept unless accepts.include? state } Pattern.new deterministic, accept end |
#opt ⇒ Object Also known as: zero_or_one
141 142 143 144 145 |
# File 'lib/aurum/lexical_table_generator.rb', line 141 def opt opt_automata = @automata.dup opt_automata.connect 0, Epsilon, @accept Pattern.new opt_automata, @accept end |
#|(other) ⇒ Object
167 168 169 170 171 172 173 |
# File 'lib/aurum/lexical_table_generator.rb', line 167 def | other automata = Automata.new 2 [self, other].each do |pattern| automata.connect automata.connect(0, Epsilon, automata.merge!(pattern.automata)) + pattern.accept, Epsilon, 1 end Pattern.new automata, 1 end |
#~ ⇒ Object
175 176 177 178 |
# File 'lib/aurum/lexical_table_generator.rb', line 175 def ~ any = Pattern.from_char_set(CharacterSet.any).kleene return Pattern.concat(Pattern.concat(any, self, any).negate, self) end |