Class: Grammar::Ruby0
- Inherits:
-
Object
- Object
- Grammar::Ruby0
- Defined in:
- lib/grammar/ruby0.rb
Defined Under Namespace
Classes: Error, PipeBuffer, Var, Void
Instance Attribute Summary collapse
-
#error ⇒ Object
readonly
these are mainly for internal usage.
-
#input ⇒ Object
readonly
these are mainly for internal usage.
-
#lookahead ⇒ Object
readonly
these are mainly for internal usage.
Class Method Summary collapse
Instance Method Summary collapse
- #<<(item) ⇒ Object
- #[](object, clone = false) ⇒ Object
-
#alternation(gram1) ⇒ Object
:yield: engine.
-
#always {|_self| ... } ⇒ Object
:yield: engine.
-
#and(first) ⇒ Object
:yield:.
- #any ⇒ Object
-
#backref(gram, &block) ⇒ Object
:yield: n[, engine].
-
#backtrack(gram, len = nil) ⇒ Object
:yield: n[, engine].
- #consume ⇒ Object
-
#discard(gram) ⇒ Object
:yield: engine.
- #eof ⇒ Object
-
#failure(expected = nil) ⇒ Object
atomic parsing.
- #fork(buf) ⇒ Object
- #if(condition, yes, &no) ⇒ Object
-
#initialize(output = nil, lookahead = nil, input_buffer = [], error = self.class::Error.new, followed = 0, hold = false, &input) ⇒ Ruby0
constructor
A new instance of Ruby0.
- #join(engine) ⇒ Object
- #match(pattern) ⇒ Object
-
#negative ⇒ Object
:yield: engine.
- #not(operand) ⇒ Object
-
#or(first) ⇒ Object
:yield:.
-
#output ⇒ Object
:yield: buf.
-
#pipe(lexer, parser, buf0, max_size = nil) ⇒ Object
:yield: buf[, engine].
-
#positive ⇒ Object
:yield: engine.
-
#recurse(inner, &outer) ⇒ Object
:yield: engine.
-
#redirect(gram, buf0, &block) ⇒ Object
:yield: buf[, engine].
- #send(obj, method, *args, &block) ⇒ Object
- #send_splat(obj, method, *args, &block) ⇒ Object
-
#sequence(gram1) ⇒ Object
:yield: engine.
- #steps(*steps) ⇒ Object
-
#supply(tokenizer, parser, buf0, &block) ⇒ Object
:yield: buf[, engine].
-
#variables(n = nil, &block) ⇒ Object
:yield: *vars.
Constructor Details
#initialize(output = nil, lookahead = nil, input_buffer = [], error = self.class::Error.new, followed = 0, hold = false, &input) ⇒ Ruby0
Returns a new instance of Ruby0.
112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/grammar/ruby0.rb', line 112 def initialize(output=nil, lookahead=nil, input_buffer=[], error=self.class::Error.new, followed = 0, hold = false, &input) @output = output @input = input @lookahead = lookahead || (input && input[]) @input_buffer = input_buffer @hold = hold @followed = followed @error = error end |
Instance Attribute Details
#error ⇒ Object (readonly)
these are mainly for internal usage
45 46 47 |
# File 'lib/grammar/ruby0.rb', line 45 def error @error end |
#input ⇒ Object (readonly)
these are mainly for internal usage
45 46 47 |
# File 'lib/grammar/ruby0.rb', line 45 def input @input end |
#lookahead ⇒ Object (readonly)
these are mainly for internal usage
45 46 47 |
# File 'lib/grammar/ruby0.rb', line 45 def lookahead @lookahead end |
Class Method Details
.compile(gram) ⇒ Object
6 7 8 |
# File 'lib/grammar/ruby0.rb', line 6 def self.compile(gram) lambda { |output, input| gram[new(output, &input)] } end |
Instance Method Details
#<<(item) ⇒ Object
37 38 39 |
# File 'lib/grammar/ruby0.rb', line 37 def <<(item) @output << item end |
#[](object, clone = false) ⇒ Object
10 11 12 13 14 |
# File 'lib/grammar/ruby0.rb', line 10 def [](object, clone=false) clone ? object.clone : object rescue object end |
#alternation(gram1) ⇒ Object
:yield: engine
135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/grammar/ruby0.rb', line 135 def alternation(gram1) # :yield: engine raise = @error.noraise @followed += 1 begin gram1[self] ensure @followed -= 1 raise1 = @error.raise @error.raise = raise end or yield(self) ensure @error.raise ||= raise1 end |
#always {|_self| ... } ⇒ Object
:yield: engine
205 206 207 |
# File 'lib/grammar/ruby0.rb', line 205 def always # :yield: engine yield(self) end |
#and(first) ⇒ Object
:yield:
21 22 23 |
# File 'lib/grammar/ruby0.rb', line 21 def and(first) # :yield: first && yield end |
#any ⇒ Object
199 200 201 |
# File 'lib/grammar/ruby0.rb', line 199 def any @lookahead ? consume : failure("ANY") end |
#backref(gram, &block) ⇒ Object
:yield: n[, engine]
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'lib/grammar/ruby0.rb', line 308 def backref(gram, &block) # :yield: n[, engine] tainted = @output.tainted? size0 = @output.taint.size @followed += 1 if block_given? begin ret = gram[self] ensure @followed -= 1 if block_given? n = @output.size-size0 if @hold || !ret @output.slice!(size0, n) end end or return if !@hold && block_given? block.arity==1 ? yield(n) : yield(n, self) end true ensure @output.untaint unless tainted end |
#backtrack(gram, len = nil) ⇒ Object
:yield: n[, engine]
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 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
# File 'lib/grammar/ruby0.rb', line 329 def backtrack(gram, len=nil) # :yield: n[, engine] tainted = @output.tainted? return gram[self] if @error.raise len ||= -1 hold = @hold lookahead = @lookahead error = @error.clone buffer = @input_buffer.clone size0 = @output.taint.size @followed += 1 if block_given? begin len = -1 if hold @hold = false input = @input len += 1 @input = lambda { begin c = input[] ensure if (len-=1).zero? @input = input buffer = nil else buffer << c end end } ret = gram[self] rescue self.class::Error => err @error = err raise(err) if !buffer ensure @followed -= 1 if block_given? @hold = hold n = @output.size-size0 if hold || !ret if buffer error.concat(@error) if !ret @lookahead = lookahead @error = error index = -1 @input = lambda { buffer[index+=1] || (@input=input)[] } end @output.slice!(size0, n) return ret end end or return if !@hold && block_given? block.arity==1 ? yield(n) : yield(n, self) end true ensure @output.untaint unless tainted end |
#consume ⇒ Object
188 189 190 191 192 193 194 195 |
# File 'lib/grammar/ruby0.rb', line 188 def consume @hold or ( @error.clear @output << @lookahead @lookahead = @input[] true ) end |
#discard(gram) ⇒ Object
:yield: engine
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/grammar/ruby0.rb', line 291 def discard(gram) # :yield: engine buf = Void.new output = @output @followed += 1 if block_given? begin @output = buf gram[self] ensure @followed -= 1 if block_given? @output = output end or return if !@hold && block_given? @output << yield(self) end true end |
#eof ⇒ Object
196 197 198 |
# File 'lib/grammar/ruby0.rb', line 196 def eof !@lookahead or failure("EOF") end |
#failure(expected = nil) ⇒ Object
atomic parsing
185 186 187 |
# File 'lib/grammar/ruby0.rb', line 185 def failure(expected=nil) @error[expected, @lookahead] end |
#fork(buf) ⇒ Object
124 125 126 127 |
# File 'lib/grammar/ruby0.rb', line 124 def fork(buf) self.class.new(buf, @lookahead, @input_buffer, @error.clone, @followed, @hold, &@input) end |
#if(condition, yes, &no) ⇒ Object
27 28 29 |
# File 'lib/grammar/ruby0.rb', line 27 def if(condition, yes, &no) condition ? yes[] : no[] end |
#join(engine) ⇒ Object
129 130 131 132 133 |
# File 'lib/grammar/ruby0.rb', line 129 def join(engine) @input = engine.input @lookahead = engine.lookahead @error = engine.error end |
#match(pattern) ⇒ Object
202 203 204 |
# File 'lib/grammar/ruby0.rb', line 202 def match(pattern) (pattern===@lookahead) ? consume : failure(pattern) end |
#negative ⇒ Object
:yield: engine
171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/grammar/ruby0.rb', line 171 def negative # :yield: engine hold = @hold raise = @error.noraise begin @hold = true not yield(self) ensure @error.raise = raise @hold = hold end or failure("something else") end |
#not(operand) ⇒ Object
24 25 26 |
# File 'lib/grammar/ruby0.rb', line 24 def not(operand) !operand end |
#or(first) ⇒ Object
:yield:
18 19 20 |
# File 'lib/grammar/ruby0.rb', line 18 def or(first) # :yield: first || yield end |
#output ⇒ Object
:yield: buf
40 41 42 |
# File 'lib/grammar/ruby0.rb', line 40 def output # :yield: buf block_given? ? (@output = yield(@output)) : @output end |
#pipe(lexer, parser, buf0, max_size = nil) ⇒ Object
:yield: buf[, engine]
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 507 508 509 510 511 512 513 514 515 516 |
# File 'lib/grammar/ruby0.rb', line 454 def pipe(lexer, parser, buf0, max_size=nil) # :yield: buf[, engine] !@hold or raise("can't hold pipe") buf = buf0.clone mutex = Mutex.new cvar = ConditionVariable.new pbuf = PipeBuffer.new(buf, mutex, cvar, max_size) engine = fork(pbuf) producer = Thread.new { begin lexer[engine] ensure mutex.lock cvar.signal cvar = nil mutex.unlock end } @followed += 1 if block_given? begin index = 0 @input = lambda { mutex.lock begin buf[index] ensure begin if 2*(index+=1)>buf.size buf.slice!(0, index) index = 0 end if cvar cvar.signal cvar.wait(mutex) if buf.empty? end ensure mutex.unlock end end } mutex.lock #mutex.sleep if buf.empty? cvar.wait(mutex) if cvar && buf.empty? mutex.unlock @lookahead = input[] parser[self] ensure @followed -= 1 if block_given? mutex.lock if cvar pbuf.max_size = 1 cvar.wait(mutex) unless producer.stop? producer.kill end mutex.unlock lookahead = @lookahead join(engine) end or return if block_given? buf[0, index] = (buf.class.new << lookahead) block.arity==1 ? yield(buf) : yield(buf, self) end true end |
#positive ⇒ Object
:yield: engine
161 162 163 164 165 166 167 168 169 |
# File 'lib/grammar/ruby0.rb', line 161 def positive # :yield: engine hold = @hold begin @hold = true yield(self) ensure @hold = hold end end |
#recurse(inner, &outer) ⇒ Object
:yield: engine
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/grammar/ruby0.rb', line 227 def recurse(inner, &outer) # :yield: engine inner0 = inner.to_proc left = false right = false lloop = false followed = @followed consumed = @error.consumed inner << Proc.new { if @error.consumed==consumed @followed!=followed or raise("something must follow left recursion") left = true lloop ? (@error.raise = true) : failure(nil) elsif @followed==followed right = true true else recurse(inner, &outer) end } yield(self) or return @hold and return(true) while true while right consumed = @error.consumed right = false @error.raise = true yield(self) or raise("should have raised") end left or begin !lloop or raise("left recursion not first") return(true) end lloop = true consumed = @error.consumed begin yield(self) or raise("should have raised") rescue self.class::Error => err err.equal?(@error) && err.consumed==consumed or raise(err) return(true) end @error.consumed!=consumed or raise("infinite loop detected") end ensure inner << inner0 end |
#redirect(gram, buf0, &block) ⇒ Object
:yield: buf[, engine]
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/grammar/ruby0.rb', line 273 def redirect(gram, buf0, &block) # :yield: buf[, engine] buf = buf0.clone output = @output @followed += 1 if block_given? begin @output = buf gram[self] ensure buf = @output @followed -= 1 if block_given? @output = output end or return if !@hold && block_given? block.arity==1 ? yield(buf) : yield(buf, self) end true end |
#send(obj, method, *args, &block) ⇒ Object
30 31 32 |
# File 'lib/grammar/ruby0.rb', line 30 def send(obj, method, *args, &block) obj.__send__(method, *args, &block) end |
#send_splat(obj, method, *args, &block) ⇒ Object
33 34 35 36 |
# File 'lib/grammar/ruby0.rb', line 33 def send_splat(obj, method, *args, &block) args.concat(args.pop) obj.__send__(method, *args, &block) end |
#sequence(gram1) ⇒ Object
:yield: engine
149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/grammar/ruby0.rb', line 149 def sequence(gram1) # :yield: engine @followed += 1 begin gram1[self] ensure @followed -= 1 end and begin # TODO: keep going with hold if nothing expected yet @hold ? true : yield(self) end end |
#steps(*steps) ⇒ Object
15 16 17 |
# File 'lib/grammar/ruby0.rb', line 15 def steps(*steps) steps[-1] end |
#supply(tokenizer, parser, buf0, &block) ⇒ Object
:yield: buf[, engine]
384 385 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 |
# File 'lib/grammar/ruby0.rb', line 384 def supply(tokenizer, parser, buf0, &block) # :yield: buf[, engine] !@hold or raise("can't hold supply") buf = buf0.clone engine = fork(buf) @followed += 1 if block_given? begin index = -1 @input = lambda { buf[index+=1] or begin buf.slice!(0, buf.size) tokenizer[engine] buf[index = 0] end } @lookahead = input[] parser[self] ensure @followed -= 1 if block_given? lookahead = @lookahead join(engine) end or return if block_given? buf.slice!(0, index+1) buf << lookahead block.arity==1 ? yield(buf) : yield(buf, self) end true end |