Class: Fabulator::Grammar::Actions::Grammar
- Inherits:
-
Structural
- Object
- Structural
- Fabulator::Grammar::Actions::Grammar
- Defined in:
- lib/fabulator/grammar/actions/grammar.rb
Instance Method Summary collapse
- #add_rule(r, m = :default) ⇒ Object
- #compile_xml(xml, context = nil) ⇒ Object
- #get_rule(m, nom) ⇒ Object
- #match(ctx, nom, s) ⇒ Object
- #parse(ctx, nom, s) ⇒ Object
- #run_constraint(ctx, nom) ⇒ Object
- #run_filter(ctx, nom) ⇒ Object
- #run_function(context, nom, args) ⇒ Object
Instance Method Details
#add_rule(r, m = :default) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/fabulator/grammar/actions/grammar.rb', line 42 def add_rule(r, m = :default) return if r.nil? mode = ((r.mode.nil? || r.mode.to_sym == :default) ? m : r.mode).to_sym @modes[mode] ||= { } @modes[mode][r.name.to_sym] = r end |
#compile_xml(xml, context = nil) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/fabulator/grammar/actions/grammar.rb', line 18 def compile_xml(xml, context = nil) super @modes = { :default => { } } @contexts.each do |c| c.tokens.each do |token| self.add_rule(token, c.mode) end c.rules.each do |rule| self.add_rule(rule, c.mode) end end @tokens.each do |token| self.add_rule(token) end @rules.each do |rule| self.add_rule(rule) end @tokens = nil @rules = nil end |
#get_rule(m, nom) ⇒ Object
49 50 51 |
# File 'lib/fabulator/grammar/actions/grammar.rb', line 49 def get_rule(m, nom) @modes[m.to_sym].nil? ? nil : @modes[m.to_sym][nom.to_sym] end |
#match(ctx, nom, s) ⇒ Object
61 62 63 64 |
# File 'lib/fabulator/grammar/actions/grammar.rb', line 61 def match(ctx, nom, s) cursor = Fabulator::Grammar::Cursor.new(self, ctx, s) !do_parse(nom, cursor).nil? end |
#parse(ctx, nom, s) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/fabulator/grammar/actions/grammar.rb', line 53 def parse(ctx, nom, s) cursor = Fabulator::Grammar::Cursor.new(self, ctx, s) cursor.anchored = true ret = do_parse(nom, cursor) cursor.do_skip cursor.eof? ? ret : nil end |
#run_constraint(ctx, nom) ⇒ Object
109 110 111 112 |
# File 'lib/fabulator/grammar/actions/grammar.rb', line 109 def run_constraint(ctx, nom) # runs as a match and requires full anchored matching !self.parse(ctx, nom, ctx.root.to_s).nil? end |
#run_filter(ctx, nom) ⇒ Object
100 101 102 103 104 105 106 107 |
# File 'lib/fabulator/grammar/actions/grammar.rb', line 100 def run_filter(ctx, nom) # runs as a parser and replaces the string with the resulting # content that matched source = ctx.root.to_s cursor = Fabulator::Grammar::Cursor.new(self, ctx, source) ret = do_parse(nom, cursor) ctx.root.value = ret.nil? ? '' : source[cursor.start .. cursor.pos-1] end |
#run_function(context, nom, args) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/fabulator/grammar/actions/grammar.rb', line 66 def run_function(context, nom, args) # treat these as mappings strings = args.collect{ |a| a.to_s } matching = false if nom =~ /\?$/ matching = true end nom.gsub!(/\?$/, '') ret = matching ? strings.collect{ |s| self.match(context, nom, s) } : strings.collect{ |s| self.parse(context, nom, s) } ret -= [ nil ] if matching ret = ret.collect{ |r| context.root.anon_node(!!r, [FAB_NS, 'boolean']) } else while ret.select{ |r| r.name.nil? && r.value.nil? }.size > 0 ret = ret.collect{ |r| if r.name.nil? && r.value.nil? r.children else r end }.flatten - [ nil ] end if !ret.empty? new_ret = ret.first.roots['data'].anon_node(nil) ret.each do |r| new_ret.add_child(r) end end ret = [ new_ret ] end ret end |