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, str_rex_or_block) ⇒ 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
- #blk(name, input, &block) ⇒ 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.
537 538 539 |
# File 'lib/raabro.rb', line 537 def last @last end |
Class Method Details
.included(target) ⇒ Object
604 605 606 607 608 609 610 |
# File 'lib/raabro.rb', line 604 def self.included(target) target.instance_eval do extend ::Raabro::ModuleMethods extend self end end |
Instance Method Details
#_match(name, input, parter, str_rex_or_block) ⇒ Object
245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/raabro.rb', line 245 def _match(name, input, parter, str_rex_or_block) r = Raabro::Tree.new(name, parter, input) if l = input.match(str_rex_or_block) r.result = 1 r.length = l input.offset += l end r end |
#_narrow(parser) ⇒ Object
287 288 289 290 291 292 |
# File 'lib/raabro.rb', line 287 def _narrow(parser) fail ArgumentError.new("lone quantifier #{parser}") if _quantify(parser) method(parser.to_sym) end |
#_parse(parser, input) ⇒ Object
294 295 296 297 298 299 300 301 |
# File 'lib/raabro.rb', line 294 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
273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/raabro.rb', line 273 def _quantify(parser) return nil if parser.is_a?(Symbol) && respond_to?(parser) # so that :plus and co can be overridden 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
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 |
# File 'lib/raabro.rb', line 446 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
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 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
# File 'lib/raabro.rb', line 341 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 && 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
386 387 388 389 |
# File 'lib/raabro.rb', line 386 def altg(name, input, *parsers) alt(name, input, *parsers, true) end |
#blk(name, input, &block) ⇒ Object
268 269 270 271 |
# File 'lib/raabro.rb', line 268 def blk(name, input, &block) _match(name, input, :blk, block) end |
#eseq(name, input, startpa, eltpa, seppa = nil, endpa = nil) ⇒ Object Also known as: jseq
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 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 |
# File 'lib/raabro.rb', line 465 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 on_elt = false count = 0 empty_stack = 0 loop do on_elt = ! on_elt cr = _parse(on_elt ? eltpa : seppa, input) empty_stack = cr.empty? ? empty_stack + 1 : 0 cr.result = 0 if empty_stack > 1 # # prevent "no progress" r.children.push(cr) if cr.result != 1 if on_elt && count > 0 lsep = r.children[-2] lsep.result = 0 input.offset = lsep.offset end break end count += 1 end r.result = 0 if jseq && count < 1 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
602 603 604 605 606 607 608 609 610 611 |
# File 'lib/raabro.rb', line 602 def make_includable def self.included(target) target.instance_eval do extend ::Raabro::ModuleMethods extend self end end end |
#method_added(name) ⇒ Object
539 540 541 542 543 544 545 546 |
# File 'lib/raabro.rb', line 539 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
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 |
# File 'lib/raabro.rb', line 430 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
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 |
# File 'lib/raabro.rb', line 548 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
421 422 423 424 425 426 427 |
# File 'lib/raabro.rb', line 421 def ren(name, input, parser) r = _parse(parser, input) r.name = name r end |
#rep(name, input, parser, min, max = 0) ⇒ Object
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 |
# File 'lib/raabro.rb', line 391 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
576 577 578 579 580 581 582 583 584 585 |
# File 'lib/raabro.rb', line 576 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
594 595 596 597 598 599 600 |
# File 'lib/raabro.rb', line 594 def rewrite(tree) return !! methods.find { |m| m.to_s.start_with?('rewrite_') } if tree == 0 # return true when "rewrite_xxx" methods seem to have been provided send("rewrite_#{tree.name}", tree) end |
#rewrite_(tree) ⇒ Object
587 588 589 590 591 592 |
# File 'lib/raabro.rb', line 587 def rewrite_(tree) t = tree.lookup(nil) t ? rewrite(t) : nil end |
#rex(name, input, regex_or_string) ⇒ Object
263 264 265 266 |
# File 'lib/raabro.rb', line 263 def rex(name, input, regex_or_string) _match(name, input, :rex, Regexp.new(regex_or_string)) end |
#seq(name, input, *parsers) ⇒ Object
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 |
# File 'lib/raabro.rb', line 303 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 && 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
258 259 260 261 |
# File 'lib/raabro.rb', line 258 def str(name, input, string) _match(name, input, :str, string) end |