Class: Grammar::Ruby
- Inherits:
-
Object
- Object
- Grammar::Ruby
- Defined in:
- lib/grammar/ruby.rb,
lib/grammar/ruby/code.rb
Defined Under Namespace
Class Method Summary collapse
-
.compile(gram, input_method = :[]) ⇒ Object
compile a grammar to a parser.
-
.dump(gram) ⇒ Object
dump the grammar to a Code object.
Instance Method Summary collapse
-
#<<(item) ⇒ Object
appends to the engine’s output.
-
#[](object, clone = false) ⇒ Object
convert a normal Ruby object to something that can be used in an action block.
-
#alternation(gram1) ⇒ Object
:yield: engine.
-
#always ⇒ Object
:yield: engine.
-
#and(first, &second) ⇒ Object
replacement for ruby’s builtin “and”/“&&” operators.
- #any ⇒ Object
- #assign(code) ⇒ Object
-
#backref(gram, &block) ⇒ Object
:yield: n[, engine].
-
#backtrack(gram, len = nil, &block) ⇒ Object
:yield: n[, engine].
-
#call(inner = nil, &outer) ⇒ Object
:yield: engine.
- #consume(matching, expected = nil) ⇒ Object
-
#discard(gram) ⇒ Object
:yield: engine.
- #dump(gram) ⇒ Object
- #eof ⇒ Object
- #failure(expected = nil) ⇒ Object
-
#if(condition, yes, &no) ⇒ Object
replacement for ruby’s builtin “if”/“else”/“?”/“:” constructs.
-
#initialize(input_method = :[]) ⇒ Ruby
constructor
A new instance of Ruby.
- #match(pat) ⇒ Object
-
#negative ⇒ Object
:yield: engine.
-
#not(operand) ⇒ Object
replacement for ruby’s builtin “not”/“!” operators.
-
#or(first, &second) ⇒ Object
replacement for ruby’s builtin “or”/“||” operators.
-
#output(&block) ⇒ Object
:yield: buf[, engine].
-
#pipe(lexer, parser, buf0, max_size = nil, &block) ⇒ Object
:yield: buf[, engine].
-
#positive ⇒ Object
:yield: engine.
-
#recurse(inner, expand = false, &outer) ⇒ Object
:yield: engine.
-
#redirect(gram, buf0, &block) ⇒ Object
:yield: buf[, engine].
-
#send(obj, method, *args, &block) ⇒ Object
method call.
-
#send_splat(obj, method, *args, &block) ⇒ Object
method call with the last argument splatted.
-
#sequence(gram1) ⇒ Object
:yield: engine.
-
#steps(*steps) ⇒ Object
replacement for ruby’s builtin “;” operator.
-
#supply(tokenizer, parser, buf0, &block) ⇒ Object
:yield: buf[, engine].
-
#variables(n = nil, &block) ⇒ Object
:yield: *vars.
Constructor Details
#initialize(input_method = :[]) ⇒ Ruby
Returns a new instance of Ruby.
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 |
# File 'lib/grammar/ruby.rb', line 74 def initialize(input_method=:[]) @followed = 0 @consumed = 0 @output0 = Code::Primary.new("o") @output = @output0 @output_init = nil @input0 = Code::Primary.new("i") @input = @input0 @input_method = input_method @lookahead0 = Code::Primary.new("la") @lookahead = @lookahead0 @lookahead_get = Code::Self.at(Code[0]) @lookahead_set = Code::Self[Code[0]] @fail0 = Code::Primary.new("f") @fail = @fail0 @failure_hard = lambda { |i, la, *expected| Code::Always.new(@fail[i, la, *expected]) } @failure_break = proc { Code::Always.new(Code::Implicit.break) } @failure = @failure_hard @expected = [] @assignments = [] @constants = Hash.new { |h, object| code = Code[object] if code._immediate h[object] = code else h[object] = assign(code) end } @method_definitions = [] @methods = {} @consume = true @void = nil @scope = nil end |
Class Method Details
.compile(gram, input_method = :[]) ⇒ Object
compile a grammar to a parser. The parser can be called with an input (#[] to get next element) and an output (#<< appends to it).
9 10 11 12 13 14 |
# File 'lib/grammar/ruby.rb', line 9 def self.compile(gram, input_method=:[]) code = new(input_method).dump(gram) #p(code) #code.lined eval(code.to_s).new end |
.dump(gram) ⇒ Object
dump the grammar to a Code object. #to_s get get Ruby code from it.
17 18 19 |
# File 'lib/grammar/ruby.rb', line 17 def self.dump(gram) new.dump(gram) end |
Instance Method Details
#<<(item) ⇒ Object
appends to the engine’s output
70 71 72 |
# File 'lib/grammar/ruby.rb', line 70 def <<(item) @output << item end |
#[](object, clone = false) ⇒ Object
convert a normal Ruby object to something that can be used in an action block. Set clone if a clone should be made everytime this is referenced.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/grammar/ruby.rb', line 23 def [](object, clone=false) if clone case object when String,Bignum,Float Code::Primary.new(object.inspect) when Array,Hash object.empty? ? Code::Primary.new(object.inspect) : @constants[object].clone else code = @constants[object] code._immediate ? code : code.clone end else @constants[object] end end |
#alternation(gram1) ⇒ Object
:yield: engine
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 |
# File 'lib/grammar/ruby.rb', line 272 def alternation(gram1) # :yield: engine failure = @failure @failure = nil if output_init = @output_init begin gram1[self]._and { @output_init ? @output._assign(@output_init) : Code::True } ensure failure1 = @failure output_init1 = @output_init end._or{ @failure = failure @output_init = output_init code = yield(self) if !output_init1 and @output_init code = @output._assign(@output_init)._step(code) end code } else begin gram1[self] ensure failure1 = @failure end._or{ @failure = failure yield(self) } end ensure @failure = failure1 if failure1 end |
#always ⇒ Object
:yield: engine
261 262 263 |
# File 'lib/grammar/ruby.rb', line 261 def always # :yield: engine Code::Always.new(yield(self)) end |
#and(first, &second) ⇒ Object
replacement for ruby’s builtin “and”/“&&” operators
54 55 56 |
# File 'lib/grammar/ruby.rb', line 54 def and(first, &second) # :yield: first._and(&second) end |
#any ⇒ Object
240 241 242 243 |
# File 'lib/grammar/ruby.rb', line 240 def any @expected << Code["ANY"] consume(@lookahead) end |
#assign(code) ⇒ Object
111 112 113 114 115 |
# File 'lib/grammar/ruby.rb', line 111 def assign(code) var = Code::Primary.new("@_#{@assignments.size}") ensure @assignments << var._assign(code) end |
#backref(gram, &block) ⇒ Object
:yield: n[, engine]
628 629 630 |
# File 'lib/grammar/ruby.rb', line 628 def backref(gram, &block) # :yield: n[, engine] raise("TODO") end |
#backtrack(gram, len = nil, &block) ⇒ Object
:yield: n[, engine]
632 633 634 |
# File 'lib/grammar/ruby.rb', line 632 def backtrack(gram, len=nil, &block) # :yield: n[, engine] raise("TODO") end |
#call(inner = nil, &outer) ⇒ Object
:yield: engine
386 387 388 389 390 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 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 |
# File 'lib/grammar/ruby.rb', line 386 def call(inner=nil, &outer) # :yield: engine @consume or return(inner ? recurse(inner, true, &outer) : outer[self]) @methods[outer] or begin i = @method_definitions.size @method_definitions << nil name = "m#{i}" output = @output input = @input lookahead = @lookahead fail = @fail failure = @failure expected = @expected scope = @scope consume = @consume begin @output = @output0 @input = @input0 @lookahead = @lookahead0 @scope = name @fail = @fail0 @failure = nil @expected = [] @consume = nil code = inner ? recurse(inner, true, &outer) : outer[self] @expected.clear if code._always @methods[outer] = [name,@expected] @fail = @fail0 @failure = nil @expected = [] @consume = true code = inner ? recurse(inner, true, &outer) : outer[self] @method_definitions[i] = code._and { @lookahead_set._assign(@lookahead)._step(Code::True) }._def( name, @output, @input, @lookahead, @fail ) ensure @output = output @input = input @lookahead = lookahead @consume = consume @fail = fail @failure = failure @expected = expected @scope = scope end end name,expected = @methods[outer] output = @output || (@void ||= assign( Code[Class].new { Code::Def.new(:<<, [Code::Primary.new("other")], Code::Self) }.new )) call = Code::Self.method_missing(name, output, @input, @lookahead, @fail)._and { @lookahead._assign(@lookahead_get)._step(Code::True) } if expected.empty? Code::Always.new(call) else call._or{ @expected.concat(expected); failure() } end end |
#consume(matching, expected = nil) ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/grammar/ruby.rb', line 212 def consume(matching, expected=nil) if @consume matching = matching._and { consume = @lookahead._assign( @input.method_missing(@input_method) ) if @output consume = ((@output_init ? @output._assign(@output_init) : @output) << @lookahead). _step(consume) end consume._step(Code::True) } end matching._or{failure(expected)} ensure if @consume @output_init = nil @failure = @failure_hard @consumed += 1 end end |
#discard(gram) ⇒ Object
:yield: engine
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 |
# File 'lib/grammar/ruby.rb', line 552 def discard(gram) # :yield: engine @consume or return(gram[self]) output = @output consume = @consume @followed += 1 if block_given? begin @output = nil @consume = true gram[self] ensure @followed -= 1 if block_given? @output = output @consume = consume end._and { !block_given? ? Code::True : Code::Always.new(@output << yield(self)) } end |
#dump(gram) ⇒ Object
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 |
# File 'lib/grammar/ruby.rb', line 161 def dump(gram) scope = @scope @scope = "call" top = gram[self] @scope = scope CodeProxy.new(Code[Class].new(Code[Array]) { || steps = [] @assignments.each { |assignment| steps << assignment } steps << Code::Implicit.super(Code[1]) steps = [Code::Steps.new(steps)._def(:initialize)] unless steps.empty? @method_definitions.each { |definition| steps << definition } steps << top._and { Code::Self.block_given?._and{ Code::Implicit.yield(@lookahead) }._step(Code::True) }._def(:call, @output, @input, @lookahead._assign(@input.method_missing(@input_method)), @fail._assign(Code::Self.lambda { |i, la, *exp| exp = exp[0] Code::Implicit.raise( Code["found: "].concat(la.inspect). concat(Code[", expected: "]). concat(exp.map{|e|e.inspect}.join(Code[", "])) ) }) ) #steps << Code::Self.alias_method(Code[:call], Code[name]) steps << Code::Self.alias_method(Code[:"[]"], Code[:call]) Code::Steps.new(steps) }) end |
#eof ⇒ Object
234 235 236 237 238 239 |
# File 'lib/grammar/ruby.rb', line 234 def eof @lookahead._not._or{ @expected << Code["EOF"] failure } end |
#failure(expected = nil) ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/grammar/ruby.rb', line 200 def failure(expected=nil) @expected << self[expected] if expected if @failure begin @failure[@input, @lookahead, *@expected] ensure @expected = [] end else Code::False end end |
#if(condition, yes, &no) ⇒ Object
replacement for ruby’s builtin “if”/“else”/“?”/“:” constructs
66 67 68 |
# File 'lib/grammar/ruby.rb', line 66 def if(condition, yes, &no) condition._question(yes[], no[]) end |
#match(pat) ⇒ Object
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/grammar/ruby.rb', line 244 def match(pat) if Range===pat op = pat.exclude_end? ? :> : :>= @expected << Code[pat] consume(@lookahead._and{(self[pat.begin]<=@lookahead)._and{ self[pat.end].__send__(op, @lookahead) }}) else code = @constants[pat] @expected << code consume(if code._immediate code.equal?(@lookahead) else code===@lookahead end) end end |
#negative ⇒ Object
:yield: engine
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
# File 'lib/grammar/ruby.rb', line 338 def negative # :yield: engine consume = @consume @consume = nil failure = @failure @failure = nil begin yield(self)._not ensure @failure = failure @consume = consume end._or{ @expected << Code["something else"] failure() } end |
#not(operand) ⇒ Object
replacement for ruby’s builtin “not”/“!” operators
62 63 64 |
# File 'lib/grammar/ruby.rb', line 62 def not(operand) operand._not end |
#or(first, &second) ⇒ Object
replacement for ruby’s builtin “or”/“||” operators
50 51 52 |
# File 'lib/grammar/ruby.rb', line 50 def or(first, &second) # :yield: first._or(&second) end |
#output(&block) ⇒ Object
:yield: buf[, engine]
264 265 266 267 268 269 270 |
# File 'lib/grammar/ruby.rb', line 264 def output(&block) # :yield: buf[, engine] Code::Always.new( block_given? ? @output._assign( block.arity==1 ? yield(@output) : yield(@output, self) ) : @output ) end |
#pipe(lexer, parser, buf0, max_size = nil, &block) ⇒ Object
:yield: buf[, engine]
636 637 638 |
# File 'lib/grammar/ruby.rb', line 636 def pipe(lexer, parser, buf0, max_size=nil, &block) # :yield: buf[, engine] raise("TODO") end |
#positive ⇒ Object
:yield: engine
328 329 330 331 332 333 334 335 336 |
# File 'lib/grammar/ruby.rb', line 328 def positive # :yield: engine consume = @consume @consume = nil begin yield(self) ensure @consume = consume end end |
#recurse(inner, expand = false, &outer) ⇒ Object
:yield: engine
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 507 508 509 510 511 512 513 514 515 516 517 518 519 520 |
# File 'lib/grammar/ruby.rb', line 458 def recurse(inner, =false, &outer) # :yield: engine inner0 = inner.to_proc left = false right = false right0 = false middle = false failure = @failure followed = @followed consumed = @consumed nexpected = @expected.size inner << proc { if @consumed==consumed @followed!=followed or raise("something must follow left recursion") left = true Code::False elsif !left && @followed==followed && @failure if !failure right0 = true recurse(inner, true, &outer) else right = true Code::FalseTerminator end else middle = true call(inner, &outer) end } body = yield(self) if !@consume body elsif left !right or raise("middle recursion should have happened") consumed = @consumed inner << proc { if @consumed==consumed @failure = @failure_break Code::True else call(inner, &outer) end } body._and { begin expected = @expected @expected = [] Code::While.new(Code::True, yield(self))._step(Code::True) ensure @expected = expected end } elsif right Code::Until.new(body)._step(Code::True) elsif ! && (@methods.include?(outer) || !right0) n = @expected.size-nexpected @expected.slice!(-n, n) call(inner, &outer) else body end ensure inner << inner0 end |
#redirect(gram, buf0, &block) ⇒ Object
:yield: buf[, engine]
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 547 548 549 550 |
# File 'lib/grammar/ruby.rb', line 522 def redirect(gram, buf0, &block) # :yield: buf[, engine] @consume or return(gram[self]) output = @output consume = @consume @followed += 1 if block_given? Code::Local.new { |buf| begin @output = buf @consume = true output_init = self[buf0,true] if @failure @output._assign(output_init)._step(gram[self]) else @output_init = output_init gram[self] end ensure @followed -= 1 if block_given? @output = output @consume = consume end._and { !block_given? ? Code::True : (@output_init ? (@output._assign(@output_init)) : Code::True). _step(Code::Always.new( block.arity==1 ? yield(buf) : yield(buf, self) )) } } end |
#send(obj, method, *args, &block) ⇒ Object
method call. Typically this shouldn’t be needed. obj.method(*args, &block) should be sufficient.
41 42 43 |
# File 'lib/grammar/ruby.rb', line 41 def send(obj, method, *args, &block) obj.method_missing(method, *args, &block) end |
#send_splat(obj, method, *args, &block) ⇒ Object
method call with the last argument splatted.
45 46 47 48 |
# File 'lib/grammar/ruby.rb', line 45 def send_splat(obj, method, *args, &block) args << args.pop._splat obj.method_missing(method, *args, &block) end |
#sequence(gram1) ⇒ Object
:yield: engine
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/grammar/ruby.rb', line 306 def sequence(gram1) # :yield: engine nexpected = @expected.size @followed += 1 begin gram1[self] ensure @followed -= 1 end._and{ expected = @expected begin if expected.size>nexpected @expected = [] @consume ? yield(self) : Code::True else yield(self) end ensure @expected = expected end } end |
#steps(*steps) ⇒ Object
replacement for ruby’s builtin “;” operator
58 59 60 |
# File 'lib/grammar/ruby.rb', line 58 def steps(*steps) steps.inject { |compound,step| compound._step(step) } end |
#supply(tokenizer, parser, buf0, &block) ⇒ Object
:yield: buf[, engine]
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 |
# File 'lib/grammar/ruby.rb', line 571 def supply(tokenizer, parser, buf0, &block) # :yield: buf[, engine] @consume or return(Code::True) input0 = @input lookahead0 = @lookahead output0 = @output consume0 = @consume @followed += 1 if block_given? Code::Local.new { |buf,index,input1,lookahead1| begin Code::Steps.new([ buf._assign(self[buf0,true]), index._assign(Code[-1]), input1._assign(Code::Implicit::lambda { || buf[index._assign(index+Code[1])]._or { steps( buf.slice!(Code[0], buf.size), begin @output = buf @consume = true @expected = [] tokenizer[self] ensure @output = output0 @consume = consume0 end, buf[index._assign(Code[0])] )} }), lookahead1._assign(input1[]), begin @input = input1 @lookahead = lookahead1 @consume = true @expected = [] parser[self] ensure @input = input0 @lookahead = lookahead0 @consume = consume0 end ]) ensure @followed -= 1 if block_given? end._and { !block_given? ? Code::True : steps( buf.slice!(Code[0], index+Code[1]), buf << lookahead1, Code::Always.new( block.arity==1 ? yield(buf) : yield(buf, self) ) ) } } ensure @expected = [] end |
#variables(n = nil, &block) ⇒ Object
:yield: *vars
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
# File 'lib/grammar/ruby.rb', line 370 def variables(n=nil, &block) # :yield: *vars scopes = {} visitor = lambda { scopes[@scope] = true } vars2 = nil Code::Local.new(n||block.arity) { |*vars| vars2 = vars yield(*vars.map { |var| Var.new(var, visitor) })[self] } ensure if scopes.size>1 vars2.each { |var| var._data.replace(assign(Code[nil])._data) } end end |