Class: Packcr::Node::RuleNode
- Inherits:
-
Packcr::Node
- Object
- Packcr::Node
- Packcr::Node::RuleNode
- Defined in:
- lib/packcr/node/rule_node.rb,
lib/packcr/generated/node/rule_node.rb
Instance Attribute Summary collapse
-
#capts ⇒ Object
Returns the value of attribute capts.
-
#codes ⇒ Object
Returns the value of attribute codes.
-
#col ⇒ Object
Returns the value of attribute col.
-
#expr ⇒ Object
Returns the value of attribute expr.
-
#line ⇒ Object
Returns the value of attribute line.
-
#name ⇒ Object
Returns the value of attribute name.
-
#ref ⇒ Object
Returns the value of attribute ref.
-
#top ⇒ Object
Returns the value of attribute top.
-
#vars ⇒ Object
Returns the value of attribute vars.
Instance Method Summary collapse
- #add_ref ⇒ Object
- #debug_dump(indent = 0) ⇒ Object
- #generate_code(gen, onfail, indent, bare, oncut: nil) ⇒ Object
- #get_code(gen, onfail, indent, bare, oncut) ⇒ Object
-
#initialize(expr = nil, name = nil, line = nil, col = nil) ⇒ RuleNode
constructor
A new instance of RuleNode.
- #nodes ⇒ Object
- #reachability ⇒ Object
- #setup(ctx) ⇒ Object
- #to_h ⇒ Object
- #verify(ctx) ⇒ Object
- #verify_rule_reference(ctx) ⇒ Object
Methods inherited from Packcr::Node
#alt, #link_references, #reversible?, #seq, #sequence?, #setup_rule, #verify_captures, #verify_variables
Constructor Details
#initialize(expr = nil, name = nil, line = nil, col = nil) ⇒ RuleNode
Returns a new instance of RuleNode.
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/packcr/node/rule_node.rb', line 6 def initialize(expr = nil, name = nil, line = nil, col = nil) super() self.ref = 0 self.vars = [] self.capts = [] self.codes = [] @expr = expr @name = name @line = line @col = col end |
Instance Attribute Details
#capts ⇒ Object
Returns the value of attribute capts.
4 5 6 |
# File 'lib/packcr/node/rule_node.rb', line 4 def capts @capts end |
#codes ⇒ Object
Returns the value of attribute codes.
4 5 6 |
# File 'lib/packcr/node/rule_node.rb', line 4 def codes @codes end |
#col ⇒ Object
Returns the value of attribute col.
4 5 6 |
# File 'lib/packcr/node/rule_node.rb', line 4 def col @col end |
#expr ⇒ Object
Returns the value of attribute expr.
4 5 6 |
# File 'lib/packcr/node/rule_node.rb', line 4 def expr @expr end |
#line ⇒ Object
Returns the value of attribute line.
4 5 6 |
# File 'lib/packcr/node/rule_node.rb', line 4 def line @line end |
#name ⇒ Object
Returns the value of attribute name.
4 5 6 |
# File 'lib/packcr/node/rule_node.rb', line 4 def name @name end |
#ref ⇒ Object
Returns the value of attribute ref.
4 5 6 |
# File 'lib/packcr/node/rule_node.rb', line 4 def ref @ref end |
#top ⇒ Object
Returns the value of attribute top.
4 5 6 |
# File 'lib/packcr/node/rule_node.rb', line 4 def top @top end |
#vars ⇒ Object
Returns the value of attribute vars.
4 5 6 |
# File 'lib/packcr/node/rule_node.rb', line 4 def vars @vars end |
Instance Method Details
#add_ref ⇒ Object
46 47 48 |
# File 'lib/packcr/node/rule_node.rb', line 46 def add_ref @ref += 1 end |
#debug_dump(indent = 0) ⇒ Object
18 19 20 21 22 |
# File 'lib/packcr/node/rule_node.rb', line 18 def debug_dump(indent = 0) $stdout.print "#{" " * indent}Rule(name:'#{name}', ref:#{ref}, vars.len:#{vars.length}, capts.len:#{capts.length}, codes.len:#{codes.length}) {\n" expr.debug_dump(indent + 2) $stdout.print "#{" " * indent}}\n" end |
#generate_code(gen, onfail, indent, bare, oncut: nil) ⇒ Object
24 25 26 |
# File 'lib/packcr/node/rule_node.rb', line 24 def generate_code(gen, onfail, indent, , oncut: nil) gen.write Packcr.format_code(get_code(gen, onfail, indent, , oncut), indent: indent, unwrap: ) end |
#get_code(gen, onfail, indent, bare, oncut) ⇒ Object
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/packcr/generated/node/rule_node.rb', line 4 def get_code(gen, onfail, indent, , oncut) case gen.lang when :c erbout = +"" erbout << "static packcr_thunk_chunk_t *packcr_evaluate_rule_#{name}(packcr_context_t *ctx, size_t offset".freeze if gen.location erbout << ", packcr_location_t offset_loc".freeze end erbout << ", packcr_rule_set_t *limits) {\n packcr_thunk_chunk_t *const chunk = packcr_thunk_chunk__create(ctx);\n chunk->pos = ctx->position_offset;\n".freeze if gen.location erbout << " chunk->pos_loc = ctx->position_offset_loc;\n".freeze end erbout << " PACKCR_DEBUG(ctx->auxil, PACKCR_DBG_EVALUATE, \"#{name}\", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->buffer.len - chunk->pos));\n ctx->level++;\n packcr_value_table__resize(ctx->auxil, &chunk->values, #{vars.length});\n packcr_capture_table__resize(ctx->auxil, &chunk->capts, #{capts.length});\n".freeze if !vars.empty? erbout << " packcr_value_table__clear(ctx->auxil, &chunk->values);\n".freeze end r = expr.reachability erbout << "#{gen.generate_code(expr, 0, 4, false)} ctx->level--;\n PACKCR_DEBUG(ctx->auxil, PACKCR_DBG_MATCH, \"#{name}\", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->position_offset - chunk->pos));\n return chunk;\n".freeze if r != Packcr::CODE_REACH__ALWAYS_SUCCEED erbout << "L0000:;\n ctx->level--;\n PACKCR_DEBUG(ctx->auxil, PACKCR_DBG_NOMATCH, \"#{name}\", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->position_offset - chunk->pos));\n packcr_thunk_chunk__destroy(ctx, chunk);\n return NULL;\n".freeze end erbout << "}\n".freeze erbout when :rb erbout = +"" erbout << "def evaluate_rule_#{name}(offset".freeze if gen.location erbout << ", offset_loc".freeze end erbout << ", limits: nil)\n answer = ThunkChunk.new\n answer.pos = @position_offset\n".freeze if gen.location erbout << " answer.pos_loc = @position_offset_loc\n".freeze end erbout << " debug { warn \"\#{ \" \" * @level}EVAL #{name} \#{answer.pos} \#{@buffer[answer.pos..-1].inspect}\" }\n @level += 1\n answer.resize_captures(#{capts.length})\n".freeze if !vars.empty? erbout << " answer.values = {}\n".freeze end r = expr.reachability if r == Packcr::CODE_REACH__ALWAYS_SUCCEED erbout << "#{gen.generate_code(expr, 0, 2, false)} @level -= 1\n debug { warn \"\#{ \" \" * @level}MATCH #{name} \#{answer.pos} \#{@buffer[answer.pos...@position_offset].inspect}\" }\n return answer\n".freeze else erbout << " catch(0) do\n#{gen.generate_code(expr, 0, 4, false)} @level -= 1\n debug { warn \"\#{ \" \" * @level}MATCH #{name} \#{answer.pos} \#{@buffer[answer.pos...@position_offset].inspect}\" }\n return answer\n end\n @level -= 1\n debug { warn \"\#{ \" \" * @level}NOMATCH #{name} \#{answer.pos} \#{@buffer[answer.pos...@position_offset].inspect}\" }\n return nil\n".freeze end erbout << "end\n".freeze erbout end end |
#nodes ⇒ Object
54 55 56 |
# File 'lib/packcr/node/rule_node.rb', line 54 def nodes [expr] end |
#reachability ⇒ Object
28 29 30 |
# File 'lib/packcr/node/rule_node.rb', line 28 def reachability expr.reachability end |
#setup(ctx) ⇒ Object
50 51 52 |
# File 'lib/packcr/node/rule_node.rb', line 50 def setup(ctx) super(ctx, self) end |
#to_h ⇒ Object
58 59 60 61 62 63 |
# File 'lib/packcr/node/rule_node.rb', line 58 def to_h { type: :rule, expr: expr&.to_h, } end |
#verify(ctx) ⇒ Object
32 33 34 35 36 |
# File 'lib/packcr/node/rule_node.rb', line 32 def verify(ctx) expr.verify_variables([]) expr.verify_captures(ctx, []) verify_rule_reference(ctx) end |
#verify_rule_reference(ctx) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/packcr/node/rule_node.rb', line 38 def verify_rule_reference(ctx) return if top return unless ref == 0 ctx.error line + 1, col + 1, "Never used rule '#{name}'" end |