Class: Junoser::RuleTree::Parser
- Inherits:
-
Object
- Object
- Junoser::RuleTree::Parser
- Defined in:
- lib/junoser/rule_tree/parser.rb
Instance Method Summary collapse
-
#initialize(content) ⇒ Parser
constructor
A new instance of Parser.
- #parse ⇒ Object
- #print ⇒ Object
Constructor Details
#initialize(content) ⇒ Parser
Returns a new instance of Parser.
4 5 6 7 8 |
# File 'lib/junoser/rule_tree/parser.rb', line 4 def initialize(content) @content = content @nodes = {} @root = nil end |
Instance Method Details
#parse ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/junoser/rule_tree/parser.rb', line 15 def parse rules = content.scan(/^rule\(:([a-z\d_]+)\) do\n(.*?)^end/m) root_index = rules.index { |r| r[0] == 'configuration' } primitives = rules.map(&:first) .then { |r| r[0, root_index] } + %w[any] validate_uniq rules[root_index..].map(&:first) @nodes = Hash[rules[root_index..].map { |name, _| [name, Node.new(name)] }] used = Set.new rules[root_index..].map do |name, refs| children = refs.split.map { |i| i.strip.gsub(/,$/, '') } .reject { |i| primitives.include?(i) } .uniq used += children children.each do |child| raise %(Unknown rule: #{child}) unless @nodes[child] @nodes[name] << @nodes[child] end end validate_used @nodes.keys, used.to_a end |
#print ⇒ Object
10 11 12 13 |
# File 'lib/junoser/rule_tree/parser.rb', line 10 def print parse @nodes['configuration'].print 0 end |