-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathrepeat.rb
32 lines (26 loc) · 873 Bytes
/
repeat.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
require_relative '../finite_automata/fa_rule'
require_relative '../finite_automata/nfa_design'
require_relative '../finite_automata/nfa_rulebook'
require_relative 'pattern'
class Repeat < Struct.new(:pattern)
include Pattern
def to_s
pattern.bracket(precedence) + '*'
end
def precedence
2
end
def to_nfa_design
pattern_nfa_design = pattern.to_nfa_design
start_state = Object.new
accept_states = pattern_nfa_design.accept_states + [start_state]
rules = pattern_nfa_design.rulebook.rules
extra_rules =
pattern_nfa_design.accept_states.map { |accept_state|
FARule.new(accept_state, nil, pattern_nfa_design.start_state)
} +
[FARule.new(start_state, nil, pattern_nfa_design.start_state)]
rulebook = NFARulebook.new(rules + extra_rules)
NFADesign.new(start_state, accept_states, rulebook)
end
end