Class: Fabulator::Grammar::Cursor
- Inherits:
-
Object
- Object
- Fabulator::Grammar::Cursor
- Defined in:
- lib/fabulator/grammar/cursor.rb
Instance Attribute Summary collapse
-
#mode ⇒ Object
Returns the value of attribute mode.
-
#skip ⇒ Object
Returns the value of attribute skip.
-
#start ⇒ Object
Returns the value of attribute start.
Instance Method Summary collapse
- #advance_position(i) ⇒ Object
- #anchored ⇒ Object
- #anchored=(t) ⇒ Object
- #attempt(&block) ⇒ Object
- #context ⇒ Object
- #data ⇒ Object
- #do_skip ⇒ Object
- #eof? ⇒ Boolean
- #find_rule(nom) ⇒ Object
- #grammar ⇒ Object
-
#initialize(g, ctx, s) ⇒ Cursor
constructor
A new instance of Cursor.
- #match_token(regex) ⇒ Object
- #name_result(nom = nil) ⇒ Object
- #point ⇒ Object
- #point=(p) ⇒ Object
- #pos ⇒ Object
- #rename_result(nom = nil) ⇒ Object
- #resync(pat) ⇒ Object
- #set_result(r) ⇒ Object
Constructor Details
#initialize(g, ctx, s) ⇒ Cursor
Returns a new instance of Cursor.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/fabulator/grammar/cursor.rb', line 6 def initialize(g,ctx,s) @source = s @grammar = g @start = 0 @curpos = 0 @end = @source.length-1 @line = 0 @col = 0 @anchored = false @mode = :default @skip = nil @context = ctx.with_root(ctx.root.anon_node(nil)) @context.root.roots['result'] = @context.root @context.root.axis = 'result' end |
Instance Attribute Details
#mode ⇒ Object
Returns the value of attribute mode.
4 5 6 |
# File 'lib/fabulator/grammar/cursor.rb', line 4 def mode @mode end |
#skip ⇒ Object
Returns the value of attribute skip.
4 5 6 |
# File 'lib/fabulator/grammar/cursor.rb', line 4 def skip @skip end |
#start ⇒ Object
Returns the value of attribute start.
4 5 6 |
# File 'lib/fabulator/grammar/cursor.rb', line 4 def start @start end |
Instance Method Details
#advance_position(i) ⇒ Object
40 41 42 |
# File 'lib/fabulator/grammar/cursor.rb', line 40 def advance_position(i) @curpos += i if i > 0 end |
#anchored ⇒ Object
44 45 46 |
# File 'lib/fabulator/grammar/cursor.rb', line 44 def anchored @anchored end |
#anchored=(t) ⇒ Object
48 49 50 |
# File 'lib/fabulator/grammar/cursor.rb', line 48 def anchored=(t) @anchored = t end |
#attempt(&block) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/fabulator/grammar/cursor.rb', line 74 def attempt(&block) saved = self.point begin yield self rescue Fabulator::Grammar::RejectParse self.point = saved return nil end ret = @context.root.roots['result'] @context.root = saved[:root] @context.root.roots['result'] = saved[:result] return ret end |
#context ⇒ Object
22 23 24 |
# File 'lib/fabulator/grammar/cursor.rb', line 22 def context @context end |
#data ⇒ Object
136 137 138 |
# File 'lib/fabulator/grammar/cursor.rb', line 136 def data @context end |
#do_skip ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/fabulator/grammar/cursor.rb', line 140 def do_skip if !@skip.nil? my_skip = @skip new_pos = @curpos self.attempt do |cursor| cursor.skip = nil cursor.anchored r = my_skip.parse(cursor) while !r.nil? r = my_skip.parse(cursor) end new_pos = cursor.pos end @curpos = new_pos end end |
#eof? ⇒ Boolean
36 37 38 |
# File 'lib/fabulator/grammar/cursor.rb', line 36 def eof? @curpos > @end end |
#find_rule(nom) ⇒ Object
128 129 130 131 132 133 134 |
# File 'lib/fabulator/grammar/cursor.rb', line 128 def find_rule(nom) r = @grammar.get_rule(@mode, nom) if r.nil? && @mode.to_s != 'default' r = @grammar.get_rule('default', nom) end r end |
#grammar ⇒ Object
52 53 54 |
# File 'lib/fabulator/grammar/cursor.rb', line 52 def grammar @grammar end |
#match_token(regex) ⇒ Object
157 158 159 160 161 162 163 164 165 |
# File 'lib/fabulator/grammar/cursor.rb', line 157 def match_token(regex) do_skip if @source[@curpos .. @end] =~ %r{^(#{regex})} @context.root.value = $1.to_s @curpos += @context.root.value.length else raise Fabulator::Grammar::RejectParse end end |
#name_result(nom = nil) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/fabulator/grammar/cursor.rb', line 100 def name_result(nom = nil) return if nom.nil? return if @context.root.value.nil? && @context.root.children.empty? if @context.root.value.nil? @context.root.name = nom else if @context.root.name.nil? @context.root.name = nom else n = @context.root.anon_node(nil) n.name = nom n.add_child(@context.root) @context.root = n @context.root.roots['result'] = @context.root end end end |
#point ⇒ Object
56 57 58 59 60 61 |
# File 'lib/fabulator/grammar/cursor.rb', line 56 def point r = { :curpos => @curpos, :line => @line, :col => @col, :root => @context.root, :mode => @mode, :anchored => @anchored, :skip => @skip, :result => @context.root.roots['result'] } @context.root.roots['result'] = r[:result].anon_node(nil) @context.root = @context.root.roots['result'] r end |
#point=(p) ⇒ Object
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/fabulator/grammar/cursor.rb', line 63 def point=(p) @curpos = p[:curpos] @line = p[:line] @col = p[:col] @mode = p[:mode] @anchored = p[:anchored] @skip = p[:skip] @context.root = p[:root] @context.root.roots['result'] = p[:result] end |
#pos ⇒ Object
26 27 28 |
# File 'lib/fabulator/grammar/cursor.rb', line 26 def pos @curpos end |
#rename_result(nom = nil) ⇒ Object
118 119 120 121 122 123 124 125 126 |
# File 'lib/fabulator/grammar/cursor.rb', line 118 def rename_result(nom = nil) return if nom.nil? return if @context.root.value.nil? && @context.root.children.empty? # if @context.root.children.select{ |c| !c.name.nil? }.empty? # @context.root.children.each { |c| c.name = nom } # else @context.root.name = nom # end end |
#resync(pat) ⇒ Object
30 31 32 33 34 |
# File 'lib/fabulator/grammar/cursor.rb', line 30 def resync(pat) until self.eof || @source[@curpos..@source.length-1] =~ %r{^#{pat}} @curpos += 1 end end |
#set_result(r) ⇒ Object
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/fabulator/grammar/cursor.rb', line 89 def set_result(r) r = [ r ] unless r.is_a?(Array) if r.size > 1 @context.root = @context.root.anon_node(nil) r.each { |rr| @context.root.add_child(rr) } elsif !r.empty? @context.root = r.first end @context.root.roots['result'] = @context.root end |