Module: Raabro::ModuleMethods
- Included in:
- Raabro
- Defined in:
- lib/raabro.rb
Instance Attribute Summary collapse
-
#last ⇒ Object
Returns the value of attribute last.
Class Method Summary collapse
Instance Method Summary collapse
- #_match(name, input, parter, regex_or_string) ⇒ Object
- #_narrow(parser) ⇒ Object
- #_parse(parser, input) ⇒ Object
- #_quantify(parser) ⇒ Object
- #all(name, input, parser) ⇒ Object
- #alt(name, input, *parsers) ⇒ Object
- #altg(name, input, *parsers) ⇒ Object
- #eseq(name, input, startpa, eltpa, seppa = nil, endpa = nil) ⇒ Object (also: #jseq)
- #make_includable ⇒ Object
- #method_added(name) ⇒ Object
- #nott(name, input, parser) ⇒ Object
- #parse(input, opts = {}) ⇒ Object
- #ren(name, input, parser) ⇒ Object (also: #rename)
- #rep(name, input, parser, min, max = 0) ⇒ Object
- #reparse_for_error(input, opts, t) ⇒ Object
- #rewrite(tree) ⇒ Object
- #rewrite_(tree) ⇒ Object
- #rex(name, input, regex_or_string) ⇒ Object
- #seq(name, input, *parsers) ⇒ Object
- #str(name, input, string) ⇒ Object
Instance Attribute Details
#last ⇒ Object
Returns the value of attribute last.
509 510 511 |
# File 'lib/raabro.rb', line 509 def last @last end |
Class Method Details
.included(target) ⇒ Object
576 577 578 579 580 581 582 |
# File 'lib/raabro.rb', line 576 def self.included(target) target.instance_eval do extend ::Raabro::ModuleMethods extend self end end |
Instance Method Details
#_match(name, input, parter, regex_or_string) ⇒ Object
232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/raabro.rb', line 232 def _match(name, input, parter, regex_or_string) r = Raabro::Tree.new(name, parter, input) if l = input.match(regex_or_string) r.result = 1 r.length = l input.offset += l end r end |
#_narrow(parser) ⇒ Object
269 270 271 272 273 274 |
# File 'lib/raabro.rb', line 269 def _narrow(parser) fail ArgumentError.new("lone quantifier #{parser}") if _quantify(parser) method(parser.to_sym) end |
#_parse(parser, input) ⇒ Object
276 277 278 279 280 281 282 283 |
# File 'lib/raabro.rb', line 276 def _parse(parser, input) #p [ caller.length, parser, input.tring ] #r = _narrow(parser).call(input) #p [ caller.length, parser, input.tring, r.to_a(children: false) ] #r _narrow(parser).call(input) end |
#_quantify(parser) ⇒ Object
255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/raabro.rb', line 255 def _quantify(parser) return nil if parser.is_a?(Symbol) && respond_to?(parser) # so that :plus and co can be overriden case parser when '?', :q, :qmark then [ 0, 1 ] when '*', :s, :star then [ 0, 0 ] when '+', :p, :plus then [ 1, 0 ] when '!' then :bang else nil end end |
#all(name, input, parser) ⇒ Object
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 |
# File 'lib/raabro.rb', line 428 def all(name, input, parser) start = input.offset length = input.string.length - input.offset r = ::Raabro::Tree.new(name, :all, input) c = _parse(parser, input) r.children << c if c.length < length input.offset = start else r.result = 1 r.length = c.length end r end |
#alt(name, input, *parsers) ⇒ Object
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 357 358 359 360 361 362 363 364 365 366 |
# File 'lib/raabro.rb', line 323 def alt(name, input, *parsers) greedy = if parsers.last == true || parsers.last == false parsers.pop else false end r = ::Raabro::Tree.new(name, greedy ? :altg : :alt, input) start = input.offset c = nil parsers.each do |pa| cc = _parse(pa, input) r.children << cc input.offset = start if greedy if cc.result == 1 && cc.length >= (c ? c.length : -1) c.result = 0 if c c = cc else cc.result = 0 end else c = cc break if c.result == 1 end end if c && c.result == 1 r.result = 1 r.length = c.length input.offset = start + r.length end r.prune! if input.[:prune] r end |
#altg(name, input, *parsers) ⇒ Object
368 369 370 371 |
# File 'lib/raabro.rb', line 368 def altg(name, input, *parsers) alt(name, input, *parsers, true) end |
#eseq(name, input, startpa, eltpa, seppa = nil, endpa = nil) ⇒ Object Also known as: jseq
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 |
# File 'lib/raabro.rb', line 447 def eseq(name, input, startpa, eltpa, seppa=nil, endpa=nil) jseq = false if seppa.nil? && endpa.nil? jseq = true seppa = eltpa; eltpa = startpa; startpa = nil end start = input.offset r = ::Raabro::Tree.new(name, jseq ? :jseq : :eseq, input) r.result = 1 c = nil if startpa c = _parse(startpa, input) r.children << c r.result = 0 if c.result != 1 end if r.result == 1 i = 0 loop do st = i > 0 ? _parse(seppa, input) : nil et = st == nil || st.result == 1 ? _parse(eltpa, input) : nil break if st && et && st.empty? && et.result == 0 break if st && et && st.empty? && et.empty? r.children << st if st r.children << et if et break if et == nil break if et.result != 1 i = i + 1 end r.result = 0 if jseq && i == 0 end if r.result == 1 && endpa c = _parse(endpa, input) r.children << c r.result = 0 if c.result != 1 end if r.result == 1 r.length = input.offset - start else input.offset = start end r.prune! if input.[:prune] r end |
#make_includable ⇒ Object
574 575 576 577 578 579 580 581 582 583 |
# File 'lib/raabro.rb', line 574 def make_includable def self.included(target) target.instance_eval do extend ::Raabro::ModuleMethods extend self end end end |
#method_added(name) ⇒ Object
511 512 513 514 515 516 517 518 |
# File 'lib/raabro.rb', line 511 def method_added(name) m = method(name) return unless m.arity == 1 return unless m.parameters[0][1] == :i || m.parameters[0][1] == :input @last = name.to_sym end |
#nott(name, input, parser) ⇒ Object
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 |
# File 'lib/raabro.rb', line 412 def nott(name, input, parser) start = input.offset r = ::Raabro::Tree.new(name, :nott, input) c = _parse(parser, input) r.children << c r.length = 0 r.result = c.result == 1 ? 0 : 1 input.offset = start r end |
#parse(input, opts = {}) ⇒ Object
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 |
# File 'lib/raabro.rb', line 520 def parse(input, opts={}) d = opts[:debug].to_i opts[:rewrite] = false if d > 0 opts[:all] = false if d > 1 opts[:prune] = false if d > 2 opts[:prune] = true unless opts.has_key?(:prune) root = self.respond_to?(:root) ? :root : @last t = if opts[:all] == false _parse(root, Raabro::Input.new(input, opts)) else all(nil, Raabro::Input.new(input, opts), root) end return reparse_for_error(input, opts, t) if opts[:error] && t.result != 1 return nil if opts[:prune] != false && t.result != 1 t = t.children.first if t.parter == :all return rewrite(t) if opts[:rewrite] != false t end |
#ren(name, input, parser) ⇒ Object Also known as: rename
403 404 405 406 407 408 409 |
# File 'lib/raabro.rb', line 403 def ren(name, input, parser) r = _parse(parser, input) r.name = name r end |
#rep(name, input, parser, min, max = 0) ⇒ Object
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 |
# File 'lib/raabro.rb', line 373 def rep(name, input, parser, min, max=0) min = 0 if min == nil || min < 0 max = nil if max.nil? || max < 1 r = ::Raabro::Tree.new(name, :rep, input) start = input.offset count = 0 loop do c = _parse(parser, input) r.children << c break if c.result != 1 count += 1 break if c.length < 1 break if max && count == max end if count >= min && (max == nil || count <= max) r.result = 1 r.length = input.offset - start else input.offset = start end r.prune! if input.[:prune] r end |
#reparse_for_error(input, opts, t) ⇒ Object
548 549 550 551 552 553 554 555 556 557 |
# File 'lib/raabro.rb', line 548 def reparse_for_error(input, opts, t) t = opts[:prune] == false ? t : parse(input, opts.merge(error: false, rewrite: false, prune: false)) #Raabro.pp(t, colours: true) t.extract_error end |
#rewrite(tree) ⇒ Object
566 567 568 569 570 571 572 |
# File 'lib/raabro.rb', line 566 def rewrite(tree) return !! methods.find { |m| m.to_s.match(/^rewrite_/) } if tree == 0 # return true when "rewrite_xxx" methods seem to have been provided send("rewrite_#{tree.name}", tree) end |
#rewrite_(tree) ⇒ Object
559 560 561 562 563 564 |
# File 'lib/raabro.rb', line 559 def rewrite_(tree) t = tree.lookup(nil) t ? rewrite(t) : nil end |
#rex(name, input, regex_or_string) ⇒ Object
250 251 252 253 |
# File 'lib/raabro.rb', line 250 def rex(name, input, regex_or_string) _match(name, input, :rex, Regexp.new(regex_or_string)) end |
#seq(name, input, *parsers) ⇒ Object
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 |
# File 'lib/raabro.rb', line 285 def seq(name, input, *parsers) r = ::Raabro::Tree.new(name, :seq, input) start = input.offset c = nil loop do pa = parsers.shift break unless pa if parsers.first == '!' parsers.shift c = nott(nil, input, pa) r.children << c elsif q = _quantify(parsers.first) parsers.shift c = rep(nil, input, pa, *q) r.children.concat(c.children) else c = _parse(pa, input) r.children << c end break if c.result != 1 end if c && c.result == 1 r.result = 1 r.length = input.offset - start else input.offset = start end r end |
#str(name, input, string) ⇒ Object
245 246 247 248 |
# File 'lib/raabro.rb', line 245 def str(name, input, string) _match(name, input, :str, string) end |