Class: JPT
Constant Summary collapse
- DATA_DIR =
Pathname.new(__FILE__) + "../../data/"
- SAFE_FN =
/\A[-._a-zA-Z0-9]+\z/
- COMPARE_SWAP =
{">" => "<", ">=" => "<="}
- @@parser =
JPTGRAMMARParser.new
Constants included from JPTType
JPTType::FUNCSIG_CHARS, JPTType::FUNCTABLE
Instance Attribute Summary collapse
-
#ast ⇒ Object
Returns the value of attribute ast.
-
#directives ⇒ Object
Returns the value of attribute directives.
-
#tree ⇒ Object
Returns the value of attribute tree.
Class Method Summary collapse
Instance Method Summary collapse
- #apply(arg) ⇒ Object
- #containers(n) ⇒ Object
- #deep_clone ⇒ Object
- #enumerate(n) ⇒ Object
- #filt_apply(logexp, root_node, curr_node) ⇒ Object
- #filt_to_logical(val) ⇒ Object
- #filt_to_value(val) ⇒ Object
-
#initialize(ast_) ⇒ JPT
constructor
A new instance of JPT.
- #select_query(tree, nodes, root_node, curr_node) ⇒ Object
- #select_segment(seg, nodes, root_node, curr_node) ⇒ Object
Methods included from JPTType
add_funcsig, #declared_as, #declared_type, #query_singular?
Constructor Details
#initialize(ast_) ⇒ JPT
Returns a new instance of JPT.
100 101 102 103 |
# File 'lib/jpt.rb', line 100 def initialize(ast_) @ast = ast_ @tree = ast.ast end |
Instance Attribute Details
#ast ⇒ Object
Returns the value of attribute ast.
99 100 101 |
# File 'lib/jpt.rb', line 99 def ast @ast end |
#directives ⇒ Object
Returns the value of attribute directives.
99 100 101 |
# File 'lib/jpt.rb', line 99 def directives @directives end |
#tree ⇒ Object
Returns the value of attribute tree.
99 100 101 |
# File 'lib/jpt.rb', line 99 def tree @tree end |
Class Method Details
.from_jp(s) ⇒ Object
89 90 91 92 93 94 95 96 97 |
# File 'lib/jpt.rb', line 89 def self.from_jp(s) ast = @@parser.parse s if !ast fail self.reason(@@parser, s) end ret = JPT.new(ast) ret end |
.reason(parser, s) ⇒ Object
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/jpt.rb', line 76 def self.reason(parser, s) reason = [parser.failure_reason] parser.failure_reason =~ /^(Expected .+) after/m reason << "#{$1.gsub("\n", '<<<NEWLINE>>>')}:" if $1 if line = s.lines.to_a[parser.failure_line - 1] reason << line reason << "#{'~' * (parser.failure_column - 1)}^" end reason.join("\n") end |
Instance Method Details
#apply(arg) ⇒ Object
109 110 111 112 |
# File 'lib/jpt.rb', line 109 def apply(arg) nodes = [arg] select_query(tree, nodes, nodes, :error) end |
#containers(n) ⇒ Object
223 224 225 226 227 228 229 230 231 232 |
# File 'lib/jpt.rb', line 223 def containers(n) case n in Array [n, *n.flat_map{containers(_1)}] in Hash [n, *n.flat_map{|k, v| containers(v)}] else [] end end |
#deep_clone ⇒ Object
105 106 107 |
# File 'lib/jpt.rb', line 105 def deep_clone Marshal.load(Marshal.dump(self)) end |
#enumerate(n) ⇒ Object
212 213 214 215 216 217 218 219 220 221 |
# File 'lib/jpt.rb', line 212 def enumerate(n) case n in Array n in Hash n.map{|k, v| v} else [] end end |
#filt_apply(logexp, root_node, curr_node) ⇒ Object
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
# File 'lib/jpt.rb', line 259 def filt_apply(logexp, root_node, curr_node) # warn "***B #{logexp.inspect} #{root_node.inspect} #{curr_node.inspect}" case logexp in ["@", *] rt = query_singular?(logexp) ? :onenode : :nodes [rt, select_query(logexp, curr_node, root_node, curr_node)] in ["$", *] rt = query_singular?(logexp) ? :onenode : :nodes [rt, select_query(logexp, root_node, root_node, curr_node)] in [("==" | "!=" | "<" | ">" | "<=" | ">="), a, b] lhs = filt_to_value(filt_apply(a, root_node, curr_node)) rescue :nothing rhs = filt_to_value(filt_apply(b, root_node, curr_node)) rescue :nothing op = logexp[0] # warn "***C #{op} #{lhs.inspect}, #{rhs.inspect}" if sop = COMPARE_SWAP[op] lhs, rhs = rhs, lhs op = sop end # warn "***C1 #{op} #{lhs.inspect}, #{rhs.inspect}" res = if op != "<" && (lhs == rhs rescue false) op == "!=" ? false : true else if op[0] == "<" # "<" or "<=" case [lhs, rhs] in Numeric, Numeric lhs < rhs in String, String lhs < rhs else false end else op == "!=" end end # warn "***C9 #{res}" [:logical, res] in ["and" | "or", a, b] lhs = filt_to_logical(filt_apply(a, root_node, curr_node)) rhs = filt_to_logical(filt_apply(b, root_node, curr_node)) op = logexp[0] # warn "***C #{op} #{lhs.inspect}, #{rhs.inspect}" [:logical, case op in "or" lhs || rhs in "and" lhs && rhs end] in ["not", a] lhs = filt_to_logical(filt_apply(a, root_node, curr_node)) [:logical, !lhs] in ["func", "length", value] value = filt_to_value(filt_apply(value, root_node, curr_node)) [:value, case value in Hash | Array | String value.length else :nothing end ] in ["func", "count", nodes] ty, nodes = filt_apply(nodes, root_node, curr_node) [:value, if ty != :nodes && ty != :onenode warn "*** func count ty #{ty.inspect}" 0 else nodes.length end] in ["func", "match", str, re] str = filt_to_value(filt_apply(str, root_node, curr_node)) re = filt_to_value(filt_apply(re, root_node, curr_node)) [:logical, begin /\A(?:#{re})\z/ === str rescue => e warn "*** #{e.} #{e.backtrace}" false end ] in ["func", "search", str, re] str = filt_to_value(filt_apply(str, root_node, curr_node)) re = filt_to_value(filt_apply(re, root_node, curr_node)) [:logical, begin /#{re}/ === str rescue => e warn "*** #{e.} #{e.backtrace}" false end ] in ["func", name, *args] warn "*** Unknown function extension #{name} with args #{args.inspect}" [:logical, false] in String | Numeric | false | true | nil [:value, logexp] end end |
#filt_to_logical(val) ⇒ Object
234 235 236 237 238 239 240 241 |
# File 'lib/jpt.rb', line 234 def filt_to_logical(val) case val in [(:nodes|:onenode), v] v != [] in [:logical, v] v end end |
#filt_to_value(val) ⇒ Object
243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/jpt.rb', line 243 def filt_to_value(val) case val in [:onenode, v] case v.length in 1 v[0] in 0 :nothing end in [:value, v] v end end |
#select_query(tree, nodes, root_node, curr_node) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/jpt.rb', line 114 def select_query(tree, nodes, root_node, curr_node) case tree in ["$", *segments] nodes = root_node segments.each do |seg| nodes = select_segment(seg, nodes, root_node, curr_node) end in ["@", *segments] nodes = curr_node segments.each do |seg| nodes = select_segment(seg, nodes, root_node, curr_node) end end nodes end |
#select_segment(seg, nodes, root_node, curr_node) ⇒ Object
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 163 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 |
# File 'lib/jpt.rb', line 130 def select_segment(seg, nodes, root_node, curr_node) case seg in Integer => ix nodes = nodes.flat_map do |n| if Array === n [n.fetch(ix, :nothing)] else [] end end in String => ky nodes = nodes.flat_map do |n| if Hash === n [n.fetch(ky, :nothing)] else [] end end in ["u", *sel] # nodes = sel.flat_map{ |sel1| select_segment(sel1, nodes, root_node, curr_node)} nodes = nodes.flat_map do |n| sel.flat_map{ |sel1| select_segment(sel1, [n], root_node, curr_node)} end in ["wild"] nodes = nodes.flat_map do |n| enumerate(n) end in ["desc", sel] nodes = nodes.flat_map do |n| containers(n) end nodes = select_segment(sel, nodes, root_node, curr_node) in ["slice", st, en, sp] nodes = nodes.flat_map do |n| if (Array === n) && sp != 0 len = n.length ret = [] sp ||= 1 if sp > 0 # weird formulae copied from spec st ||= 0 en ||= len else # warn "ARR #{st} #{en} #{sp}" st ||= len - 1 en ||= -len - 1 # warn "ARR2 #{st} #{en} #{sp}" end st, en = [st, en].map{|i| i >= 0 ? i : len + i} if sp > 0 lo = [[st, 0].max, len].min up = [[en, 0].max, len].min while lo < up ret << n[lo]; lo += sp end else up = [[st, -1].max, len-1].min lo = [[en, -1].max, len-1].min # warn "ARR3 #{st} #{en} #{sp} #{lo} #{up}" while lo < up ret << n[up]; up += sp end end ret else [] end end in ["filt", logexp] nodes = nodes.flat_map do |n| enumerate(n).flat_map do |cand| a = filt_apply(logexp, root_node, [cand]) # warn "***A #{a.inspect}" if filt_to_logical(a) [cand] else [] end end end end nodes.delete(:nothing) nodes end |