Class: Packcr::Node::QuantityNode
- Inherits:
-
Packcr::Node
- Object
- Packcr::Node
- Packcr::Node::QuantityNode
- Defined in:
- lib/packcr/node/quantity_node.rb,
lib/packcr/generated/node/quantity_node.rb
Instance Attribute Summary collapse
-
#expr ⇒ Object
Returns the value of attribute expr.
-
#max ⇒ Object
Returns the value of attribute max.
-
#min ⇒ Object
Returns the value of attribute min.
Instance Method Summary collapse
- #debug_dump(indent = 0) ⇒ Object
- #generate_code(gen, onfail, indent, unwrap, oncut: nil) ⇒ Object
- #get_many_code(gen, onfail, indent, unwrap, oncut) ⇒ Object
- #get_one_code(gen, onfail, indent, unwrap, oncut) ⇒ Object
-
#initialize(expr = nil, min = 0, max = 0) ⇒ QuantityNode
constructor
A new instance of QuantityNode.
- #nodes ⇒ Object
- #reachability ⇒ Object
- #to_h ⇒ Object
Methods inherited from Packcr::Node
#alt, #link_references, #reversible?, #seq, #sequence?, #setup, #setup_rule, #verify_captures, #verify_variables
Constructor Details
#initialize(expr = nil, min = 0, max = 0) ⇒ QuantityNode
Returns a new instance of QuantityNode.
6 7 8 9 10 11 |
# File 'lib/packcr/node/quantity_node.rb', line 6 def initialize(expr = nil, min = 0, max = 0) super() @expr = expr @min = min @max = max end |
Instance Attribute Details
#expr ⇒ Object
Returns the value of attribute expr.
4 5 6 |
# File 'lib/packcr/node/quantity_node.rb', line 4 def expr @expr end |
#max ⇒ Object
Returns the value of attribute max.
4 5 6 |
# File 'lib/packcr/node/quantity_node.rb', line 4 def max @max end |
#min ⇒ Object
Returns the value of attribute min.
4 5 6 |
# File 'lib/packcr/node/quantity_node.rb', line 4 def min @min end |
Instance Method Details
#debug_dump(indent = 0) ⇒ Object
13 14 15 16 17 |
# File 'lib/packcr/node/quantity_node.rb', line 13 def debug_dump(indent = 0) $stdout.print "#{" " * indent}Quantity(min:#{min}, max:#{max}) {\n" expr.debug_dump(indent + 2) $stdout.print "#{" " * indent}}\n" end |
#generate_code(gen, onfail, indent, unwrap, oncut: nil) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/packcr/node/quantity_node.rb', line 19 def generate_code(gen, onfail, indent, unwrap, oncut: nil) if max > 1 || max < 0 get_many_code(gen, onfail, indent, unwrap, oncut) elsif max == 1 if min > 0 gen.generate_code(expr, onfail, indent, unwrap, oncut: oncut) else get_one_code(gen, onfail, indent, unwrap, oncut) end end end |
#get_many_code(gen, onfail, indent, unwrap, 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 61 62 63 64 65 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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/packcr/generated/node/quantity_node.rb', line 4 def get_many_code(gen, onfail, indent, unwrap, oncut) case gen.lang when :c erbout = +"" erbout << "{\n".freeze r = expr.reachability if min > 0 erbout << " const size_t p0 = ctx->position_offset;\n".freeze if gen.location erbout << " const packcr_location_t p0_loc = ctx->position_offset_loc;\n".freeze end erbout << " const size_t n0 = chunk->thunks.len;\n".freeze end erbout << " int i;\n".freeze if max < 0 erbout << " for (i = 0;; i++) {\n".freeze else erbout << " for (i = 0; i < #{max}; i++) {\n".freeze end erbout << " const size_t p = ctx->position_offset;\n".freeze if r != Packcr::CODE_REACH__ALWAYS_SUCCEED if gen.location erbout << " const packcr_location_t p_loc = ctx->position_offset_loc;\n".freeze end erbout << " const size_t n = chunk->thunks.len;\n".freeze end l = gen.next_label erbout << "#{gen.generate_code(expr, l, 8, false)} if (ctx->position_offset == p) break;\n".freeze if r != Packcr::CODE_REACH__ALWAYS_SUCCEED erbout << " continue;\n L#{format("%04d", l)}:;\n ctx->position_offset = p;\n".freeze if gen.location erbout << " ctx->position_offset_loc = p_loc;\n".freeze end erbout << " packcr_thunk_array__revert(ctx->auxil, &chunk->thunks, n);\n break;\n".freeze end erbout << " }\n".freeze if min > 0 erbout << " if (i < #{min}) {\n ctx->position_offset = p0;\n".freeze if gen.location erbout << " ctx->position_offset_loc = p0_loc;\n".freeze end erbout << " packcr_thunk_array__revert(ctx->auxil, &chunk->thunks, n0);\n goto L#{format("%04d", onfail)};\n }\n".freeze end erbout << "}\n".freeze erbout when :rb erbout = +"" if min > 0 erbout << "q#{gen.level} = @position_offset\n".freeze if gen.location erbout << "q_loc#{gen.level} = @position_offset_loc\n".freeze end erbout << "m#{gen.level} = answer.thunks.length\n".freeze end erbout << "i#{gen.level} = 0\npos#{gen.level} = nil\n".freeze if gen.location erbout << "p_loc#{gen.level} = nil\n".freeze end erbout << "n#{gen.level} = nil\n".freeze l = gen.next_label erbout << "catch(#{l}) do\n pos#{gen.level} = @position_offset\n".freeze if gen.location erbout << " p_loc#{gen.level} = @position_offset_loc\n".freeze end erbout << " n#{gen.level} = answer.thunks.length\n".freeze r = expr.reachability erbout << "#{gen.generate_code(expr, l, 2, false)} i#{gen.level} += 1\n if @position_offset != pos#{gen.level}".freeze if max >= 0 erbout << " || i#{gen.level} < #{max}".freeze end erbout << "\n redo\n end\n pos#{gen.level} = nil\nend\n".freeze if r != Packcr::CODE_REACH__ALWAYS_SUCCEED erbout << "if pos#{gen.level}\n @position_offset = pos#{gen.level}\n".freeze if gen.location erbout << " @position_offset_loc = p_loc#{gen.level}\n".freeze end erbout << " answer.thunks[n#{gen.level}..-1] = []\nend\n".freeze end if min > 0 erbout << "if i#{gen.level} < #{min}\n @position_offset = q#{gen.level}\n".freeze if gen.location erbout << " @position_offset_loc = q_loc#{gen.level}\n".freeze end erbout << " answer.thunks[m#{gen.level}..-1] = []\n throw(#{onfail})\nend\n".freeze end erbout when :rs erbout = +"" if min > 0 erbout << "let p0 = self.input.position_offset;\n".freeze if gen.location erbout << "TODO\n".freeze end end use_count = max >= 0 || min > 0 if use_count erbout << "let mut i = -1;\n".freeze end m = gen.next_label erbout << "catch(#{m}, || {\n loop {\n".freeze if use_count erbout << " i += 1;\n".freeze end if max >= 0 erbout << " if i >= #{max} {\n return NOP;\n }\n".freeze end erbout << " let p = self.input.position_offset;\n".freeze if (r != Packcr::CODE_REACH__ALWAYS_SUCCEED) && gen.location erbout << " TODO\n".freeze end l = gen.next_label r = expr.reachability if r == Packcr::CODE_REACH__ALWAYS_SUCCEED erbout << " TODO\n match (|| {\n#{gen.generate_code(expr, l, 12, false)} if self.input.position_offset == p {\n return throw(#{m});\n }\n })() {\n NOP => continue,\n Err(label) if label != #{l} => return throw(label),\n _ => {\n self.input.position_offset = p;\n".freeze else erbout << " match (|| {\n#{gen.generate_code(expr, l, 12, false)} if self.input.position_offset == p {\n return throw(#{m});\n }\n NOP\n })() {\n NOP => continue,\n Err(label) if label != #{l} => return throw(label),\n _ => {\n self.input.position_offset = p;\n".freeze end if gen.location erbout << " TODO\n".freeze end erbout << " return throw(#{m});\n }\n }\n }\n})?;\n".freeze if min > 0 erbout << "if i < #{min} {\n self.input.position_offset = p0;\n".freeze if gen.location erbout << " TODO\n".freeze end erbout << " return throw(#{onfail});\n}\n".freeze end erbout else raise "unknown lang #{gen.lang}" end end |
#get_one_code(gen, onfail, indent, unwrap, oncut) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/packcr/generated/node/quantity_node.rb', line 164 def get_one_code(gen, onfail, indent, unwrap, oncut) case gen.lang when :c erbout = +"" r = expr.reachability if r == Packcr::CODE_REACH__ALWAYS_SUCCEED erbout << "#{gen.generate_code(expr, nil, 0, true)}".freeze else erbout << "{\n".freeze l = gen.next_label erbout << " const size_t p = ctx->position_offset;\n".freeze if gen.location erbout << " const packcr_location_t p_loc = ctx->position_offset_loc;\n".freeze end erbout << " const size_t n = chunk->thunks.len;\n#{gen.generate_code(expr, l, 4, false)}".freeze m = gen.next_label erbout << " goto L#{format("%04d", m)};\nL#{format("%04d", l)}:;\n".freeze if gen.location erbout << " ctx->position_offset_loc = p_loc;\n".freeze end erbout << " ctx->position_offset = p;\n packcr_thunk_array__revert(ctx->auxil, &chunk->thunks, n);\nL#{format("%04d", m)}:;\n}\n".freeze end erbout when :rb erbout = +"" r = expr.reachability if r == Packcr::CODE_REACH__ALWAYS_SUCCEED erbout << "#{gen.generate_code(expr, nil, 0, true)}".freeze else l = gen.next_label erbout << "pos#{gen.level} = @position_offset\n".freeze if gen.location erbout << "p_loc#{gen.level} = @position_offset_loc\n".freeze end erbout << "n#{gen.level} = answer.thunks.length\n".freeze m = gen.next_label erbout << "catch(#{m}) do\n catch(#{l}) do\n#{gen.generate_code(expr, l, 4, false)} throw(#{m})\n end\n".freeze if gen.location erbout << " @position_offset_loc = p_loc#{gen.level}\n".freeze end erbout << " @position_offset = pos#{gen.level}\n answer.thunks[n#{gen.level}..-1] = []\nend\n".freeze end erbout else raise "unknown lang #{gen.lang}" end end |
#nodes ⇒ Object
51 52 53 |
# File 'lib/packcr/node/quantity_node.rb', line 51 def nodes [expr] end |
#reachability ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/packcr/node/quantity_node.rb', line 31 def reachability if max > 1 || max < 0 if min <= 0 return Packcr::CODE_REACH__ALWAYS_SUCCEED end if expr.reachability == Packcr::CODE_REACH__ALWAYS_FAIL return Packcr::CODE_REACH__ALWAYS_FAIL end Packcr::CODE_REACH__BOTH elsif max == 1 return expr.reachability if min > 0 Packcr::CODE_REACH__ALWAYS_SUCCEED else Packcr::CODE_REACH__ALWAYS_SUCCEED end end |
#to_h ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/packcr/node/quantity_node.rb', line 55 def to_h { type: :predicate, expr: expr&.to_h, min: min, max: max, } end |