1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 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 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 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 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 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 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 517 518 519 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 547 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 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 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
|
[comment {-*- text -*- doctools manpage}]
[manpage_begin pt::rde n 1.0.2]
[include include/module.inc]
[titledesc {Parsing Runtime Support, PARAM based}]
[require pt::rde [opt 1.0.2]]
[require snit]
[require struct::stack 1.4]
[require pt::ast 1.1]
[description]
[include include/ref_intro.inc]
This package provides a class whose instances provide the runtime
support for recursive descent parsers with backtracking, as is needed
for the execution of, for example, parsing expression grammars. It
implements the [manpage {PackRat Machine Specification}], as such that
document is [emph required] reading to understand both this manpage,
and the package itself. The description below does make numerous
shorthand references to the PARAM's instructions and the various parts
of its architectural state.
[para]
The package resides in the Execution section of the Core Layer of
Parser Tools.
[para][image arch_core_transform][para]
[para]
Note: This package not only has the standard Tcl implementation, but
also an accelerator, i.e. a C implementation, based on Critcl.
[para]
[subsection {Class API}]
The package exports the API described here.
[list_begin definitions]
[call [cmd ::pt::rde] [arg objectName]]
The command creates a new runtime object for a recursive descent
parser with backtracking and returns the fully qualified name of the
object command as its result. The API of this object command is
described in the section [sectref {Object API}]. It may be used to
invoke various operations on the object.
[list_end]
[subsection {Object API}]
All objects created by this package provide the following 63 methods
for the manipulation and querying of their state, which is, in essence
the architectural state of a PARAM.
[para]
First some general methods and the state accessors.
[list_begin definitions]
[call [arg objectName] [method destroy]]
This method destroys the object, releasing all claimed memory, and
deleting the associated object command.
[call [arg objectName] [method reset] [arg chan]]
This method resets the state of the runtme to its defaults, preparing
it for the parsing of the character in the channel [arg chan], which
becomes IN.
[include include/channel_notes.inc]
[call [arg objectName] [method complete]]
This method completes parsing, either returning the AST made from the
elements of ARS, or throwing an error containing the current ER.
[call [arg objectName] [method chan]]
This method returns the handle of the channel which is IN.
[call [arg objectName] [method line]]
This method returns the line number for the position IN is currently
at. Note that this may not match with the line number for CL, due to
backtracking.
[call [arg objectName] [method column]]
This method returns the column for the position IN is currently
at. Note that this may not match with the column for CL, due to
backtracking.
[call [arg objectName] [method current]]
This method returns CC.
[call [arg objectName] [method location]]
This method returns CL.
[call [arg objectName] [method locations]]
This method returns the LS. The topmost entry of the stack will be the
first element of the returned list.
[call [arg objectName] [method ok]]
This method returns ST.
[call [arg objectName] [method value]]
This method returns SV.
[call [arg objectName] [method error]]
This method returns ER. This is either the empty string for an empty
ER, or a list of 2 elements, the location the error is for, and a set
of messages which specify which symbols were expected at the
location. The messages are encoded as one of the possible atomic
parsing expressions (special operators, terminal, range, and
nonterminal operator).
[call [arg objectName] [method errors]]
This method returns ES. The topmost entry of the stack will be the
first element of the returned list. Each entry is encoded as described
for [method error].
[call [arg objectName] [method tokens] [opt "[arg from] [opt [arg to]]"]]
This method returns the part of TC for the range of locations of IN
starting at [arg from] and ending at [arg to]. If [arg to] is not
specified it is taken as identical to [arg from]. If neither argument
is specified the whole of TC is returned.
[para]
Each token in the returned list is a list of three elements itself,
containing the character at the location, and the associated line and
column numbers, in this order.
[call [arg objectName] [method symbols]]
This method returns a dictionary containing NC. Keys are two-element
lists containing nonterminal symbol and location, in this order. The
values are 4-tuples containing CL, ST, ER, and SV, in this order. ER
is encoded as specified for the method [method error].
[call [arg objectName] [method known]]
This method returns a list containing the keys of SC. They are
encoded in the same manner as is done by method [method symbols].
[call [arg objectName] [method reducible]]
This method returns ARS. The topmost entry of the stack will be the
first element of the returned list
[call [arg objectName] [method asts]]
This method returns AS. The topmost entry of the stack will be the
first element of the returned list
[call [arg objectName] [method ast]]
This is a convenience method returning the topmost element of ARS.
[call [arg objectName] [method position] [arg loc]]
This method returns the line and column numbers for the specified
location of IN, assuming that this location has already been reached
during the parsing process.
[list_end]
The following methods implement all PARAM instructions. They all have
the prefix "i_".
[para]
The control flow is mainly provided by Tcl's builtin commands, like
[cmd if], [cmd while], etc., plus a few guarded variants of PARAM
instructions and Tcl commands.. That means that these instruction
variants will do nothing if their guard condition is not
fulfilled. They can be recognized by the prefix "i:ok_" and "i:fail_",
which denote the value ST has to have for the instruction to execute.
[para]
The instructions are listed in the same order they occur in the
[manpage {PackRat Machine Specification}], with the guard variants
listed after their regular implementation, if any, or in their place.
[list_begin definitions]
[vset INS input_next][vset IA0 msg][include include/rde_1ins.inc]
[vset INS test_alnum][include include/rde_0ins.inc]
[vset INS test_alpha][include include/rde_0ins.inc]
[vset INS test_ascii][include include/rde_0ins.inc]
[vset INS test_char][vset IA0 char][include include/rde_1ins.inc]
[vset INS test_ddigit][include include/rde_0ins.inc]
[vset INS test_digit][include include/rde_0ins.inc]
[vset INS test_graph][include include/rde_0ins.inc]
[vset INS test_lower][include include/rde_0ins.inc]
[vset INS test_print][include include/rde_0ins.inc]
[vset INS test_punct][include include/rde_0ins.inc]
[vset INS test_range][vset IA0 chars][vset IA1 chare][include include/rde_2ins.inc]
[vset INS test_space][include include/rde_0ins.inc]
[vset INS test_upper][include include/rde_0ins.inc]
[vset INS test_wordchar][include include/rde_0ins.inc]
[vset INS test_xdigit][include include/rde_0ins.inc]
[vset INS error_clear][include include/rde_0ins.inc]
[vset INS error_push][include include/rde_0ins.inc]
[vset INS error_pop_merge][include include/rde_0ins.inc]
[vset INS error_nonterminal][vset IA0 symbol][include include/rde_1ins.inc]
[vset INS status_ok][include include/rde_0ins.inc]
[vset INS status_fail][include include/rde_0ins.inc]
[vset INS status_negate][include include/rde_0ins.inc]
[vset INS loc_push][include include/rde_0ins.inc]
[vset INS loc_pop_discard][include include/rde_0ins.inc]
[vset INS loc_pop_rewind][include include/rde_0ins.inc]
[vset G ok][vset INS loc_pop_rewind][include include/rde_0gins.inc]
[vset IFAIL loc_pop_rewind]
[vset IOK loc_pop_discard]
[vset IOKX discard][include include/rde_0cins.inc]
[vset INS symbol_restore][vset IA0 symbol][include include/rde_1ins.inc]
[para] The boolean result of the check is returned as the result of
the method and can be used with standard Tcl control flow commands.
[vset INS symbol_save][vset IA0 symbol][include include/rde_1ins.inc]
[vset INS value_clear][include include/rde_0ins.inc]
[vset IFAIL value_clear]
[vset IOK value_leaf]
[vset IOKX leaf][include include/rde_0cins.inc]
[vset IFAIL value_clear]
[vset IOK value_reduce]
[vset IOKX reduce][include include/rde_0cins.inc]
[vset G ok][vset INS ast_value_push][include include/rde_0ginsb.inc]
[vset INS ast_push][include include/rde_0ins.inc]
[vset INS ast_pop_rewind][include include/rde_0ins.inc]
[vset G fail][vset INS ast_pop_rewind][include include/rde_0gins.inc]
[vset IFAIL ast_pop_rewind]
[vset IOK ast_pop_discard]
[vset IOKX discard][include include/rde_0cins.inc]
[vset INS ast_pop_discard][include include/rde_0ins.inc]
[vset IFAIL ast_pop_discard]
[vset IOK ast_pop_rewind]
[vset IOKX rewind][include include/rde_0cins.inc]
[call [arg objectName] [method i:ok_continue]]
This guarded method executes only for "ST == ok". Then it aborts the
current iteration of the innermost loop in the calling Tcl procedure.
[call [arg objectName] [method i:fail_continue]]
This guarded method executes only for "ST == fail". Then it aborts the
current iteration of the innermost loop in the calling Tcl procedure.
[call [arg objectName] [method i:fail_return]]
This guarded method executes only for "ST == fail". Then it aborts the
calling Tcl procedure.
[call [arg objectName] [method i:ok_return]]
This guarded method executes only for "ST == ok". Then it aborts the
calling Tcl procedure.
[list_end]
[para]
The next set of methods are [term {super instructions}], meaning that
each implements a longer sequence of instructions commonly used in
parsers. The combinated instructions of the previous set, i.e. those
with names matching the pattern "i_*/*", are actually super
instructions as well, albeit with limited scope, handling 2
instructions with their control flow. The upcoming set is much broader
in scope, folding as much as six or more PARAM instructions into a
single method call.
[para]
In this we can see the reasoning behind their use well:
[list_begin enumerated]
[enum]
By using less instructions the generated parsers become smaller, as
the common parts are now truly part of the common runtime, and not
explicitly written in the parser's code over and over again.
[enum]
Using less instructions additionally reduces the overhead associated
with calls into the runtime, i.e. the cost of method dispatch and of
setting up the variable context.
[enum]
Another effect of the super instructions is that their internals can
be optimized as well, especially regarding control flow, and stack
use, as the runtime internals are accessible to all instructions
folded into the sequence.
[list_end]
[para]
[list_begin definitions]
[call [arg objectName] [method si:void_state_push]]
This method combines [example {
i_loc_push
i_error_clear
i_error_push
}]
Parsers use it at the beginning of [term void] sequences and choices
with a [term void] initial branch.
[call [arg objectName] [method si:void2_state_push]]
This method combines [example {
i_loc_push
i_error_clear
i_error_push
}]
Parsers use it at the beginning of optional and repeated expressions.
[call [arg objectName] [method si:value_state_push]]
This method combines [example {
i_ast_push
i_loc_push
i_error_clear
i_error_push
}]
Parsers use it at the beginning of sequences generating an AST and
choices with an initial branch generating an AST.
[call [arg objectName] [method si:void_state_merge]]
This method combines [example {
i_error_pop_merge
i_loc_pop_rewind/discard
}]
Parsers use it at the end of void sequences and choices whose last
branch is void.
[call [arg objectName] [method si:void_state_merge_ok]]
This method combines [example {
i_error_pop_merge
i_loc_pop_rewind/discard
i_status_ok
}]
Parsers use it at the end of optional expressions
[call [arg objectName] [method si:value_state_merge]]
This method combines [example {
i_error_pop_merge
i_ast_pop_rewind/discard
i_loc_pop_rewind/discard
}]
Parsers use it at the end of sequences generating ASTs and choices
whose last branch generates an AST
[call [arg objectName] [method si:value_notahead_start]]
This method combines [example {
i_loc_push
i_ast_push
}]
Parsers use it at the beginning of negative lookahead predicates which
generate ASTs.
[call [arg objectName] [method si:void_notahead_exit]]
This method combines [example {
i_loc_pop_rewind
i_status_negate
}]
Parsers use it at the end of void negative lookahead predicates.
[call [arg objectName] [method si:value_notahead_exit]]
This method combines [example {
i_ast_pop_discard/rewind
i_loc_pop_rewind
i_status_negate
}]
Parsers use it at the end of negative lookahead predicates which
generate ASTs.
[call [arg objectName] [method si:kleene_abort]]
This method combines [example {
i_loc_pop_rewind/discard
i:fail_return
}]
Parsers use it to stop a positive repetition when its first, required, expression fails.
[call [arg objectName] [method si:kleene_close]]
This method combines [example {
i_error_pop_merge
i_loc_pop_rewind/discard
i:fail_status_ok
i:fail_return
}]
Parsers use it at the end of repetitions.
[call [arg objectName] [method si:voidvoid_branch]]
This method combines [example {
i_error_pop_merge
i:ok_loc_pop_discard
i:ok_return
i_loc_rewind
i_error_push
}]
Parsers use it when transiting between branches of a choice when both are void.
[call [arg objectName] [method si:voidvalue_branch]]
This method combines [example {
i_error_pop_merge
i:ok_loc_pop_discard
i:ok_return
i_ast_push
i_loc_rewind
i_error_push
}]
Parsers use it when transiting between branches of a choice when the
failing branch is void, and the next to test generates an AST.
[call [arg objectName] [method si:valuevoid_branch]]
This method combines [example {
i_error_pop_merge
i_ast_pop_rewind/discard
i:ok_loc_pop_discard
i:ok_return
i_loc_rewind
i_error_push
}]
Parsers use it when transiting between branches of a choice when the
failing branch generates an AST, and the next to test is void.
[call [arg objectName] [method si:valuevalue_branch]]
This method combines [example {
i_error_pop_merge
i_ast_pop_discard
i:ok_loc_pop_discard
i:ok_return
i_ast_rewind
i_loc_rewind
i_error_push
}]
Parsers use it when transiting between branches of a choice when both
generate ASTs.
[call [arg objectName] [method si:voidvoid_part]]
This method combines [example {
i_error_pop_merge
i:fail_loc_pop_rewind
i:fail_return
i_error_push
}]
Parsers use it when transiting between parts of a sequence and both
are void.
[call [arg objectName] [method si:voidvalue_part]]
This method combines [example {
i_error_pop_merge
i:fail_loc_pop_rewind
i:fail_return
i_ast_push
i_error_push
}]
Parsers use it when transiting between parts of a sequence and the
sucessfully matched part is void, and after it an AST is generated.
[call [arg objectName] [method si:valuevalue_part]]
This method combines [example {
i_error_pop_merge
i:fail_ast_pop_rewind
i:fail_loc_pop_rewind
i:fail_return
i_error_push
}]
Parsers use it when transiting between parts of a sequence and both
parts generate ASTs.
[call [arg objectName] [method si:value_symbol_start] [arg symbol]]
This method combines [example {
if/found? i_symbol_restore $symbol
i:found:ok_ast_value_push
i:found_return
i_loc_push
i_ast_push
}]
Parsers use it at the beginning of a nonterminal symbol generating an
AST, whose right-hand side may have generated an AST as well.
[call [arg objectName] [method si:value_void_symbol_start] [arg symbol]]
This method combines [example {
if/found? i_symbol_restore $symbol
i:found:ok_ast_value_push
i:found_return
i_loc_push
i_ast_push
}]
Parsers use it at the beginning of a void nonterminal symbol whose
right-hand side may generate an AST.
[call [arg objectName] [method si:void_symbol_start] [arg symbol]]
This method combines [example {
if/found? i_symbol_restore $symbol
i:found_return
i_loc_push
i_ast_push
}]
Parsers use it at the beginning of a nonterminal symbol generating an
AST whose right-hand side is void.
[call [arg objectName] [method si:void_void_symbol_start] [arg symbol]]
This method combines [example {
if/found? i_symbol_restore $symbol
i:found_return
i_loc_push
}]
Parsers use it at the beginning of a void nonterminal symbol whose
right-hand side is void as well.
[call [arg objectName] [method si:reduce_symbol_end] [arg symbol]]
This method combines [example {
i_value_clear/reduce $symbol
i_symbol_save $symbol
i_error_nonterminal $symbol
i_ast_pop_rewind
i_loc_pop_discard
i:ok_ast_value_push
}]
Parsers use it at the end of a non-terminal symbol generating an AST
using the AST generated by the right-hand side as child.
[call [arg objectName] [method si:void_leaf_symbol_end] [arg symbol]]
This method combines [example {
i_value_clear/leaf $symbol
i_symbol_save $symbol
i_error_nonterminal $symbol
i_loc_pop_discard
i:ok_ast_value_push
}]
Parsers use it at the end of a non-terminal symbol generating an AST
whose right-hand side is void.
[call [arg objectName] [method si:value_leaf_symbol_end] [arg symbol]]
This method combines [example {
i_value_clear/leaf $symbol
i_symbol_save $symbol
i_error_nonterminal $symbol
i_loc_pop_discard
i_ast_pop_rewind
i:ok_ast_value_push
}]
Parsers use it at the end of a non-terminal symbol generating an AST
discarding the AST generated by the right-hand side.
[call [arg objectName] [method si:value_clear_symbol_end] [arg symbol]]
This method combines [example {
i_value_clear
i_symbol_save $symbol
i_error_nonterminal $symbol
i_loc_pop_discard
i_ast_pop_rewind
}]
Parsers use it at the end of a void non-terminal symbol, discarding
the AST generated by the right-hand side.
[call [arg objectName] [method si:void_clear_symbol_end] [arg symbol]]
This method combines [example {
i_value_clear
i_symbol_save $symbol
i_error_nonterminal $symbol
i_loc_pop_discard
}]
Parsers use it at the end of a void non-terminal symbol with a void
right-hand side.
[call [arg objectName] [method si:next_char] [arg tok]]
[call [arg objectName] [method si:next_range] [arg toks] [arg toke]]
[call [arg objectName] [method si:next_alnum]]
[call [arg objectName] [method si:next_alpha]]
[call [arg objectName] [method si:next_ascii]]
[call [arg objectName] [method si:next_ddigit]]
[call [arg objectName] [method si:next_digit]]
[call [arg objectName] [method si:next_graph]]
[call [arg objectName] [method si:next_lower]]
[call [arg objectName] [method si:next_print]]
[call [arg objectName] [method si:next_punct]]
[call [arg objectName] [method si:next_space]]
[call [arg objectName] [method si:next_upper]]
[call [arg objectName] [method si:next_wordchar]]
[call [arg objectName] [method si:next_xdigit]]
These methods all combine [example {
i_input_next $msg
i:fail_return
}]
with the appropriate [cmd i_test_xxx] instruction. Parsers use them for
handling atomic expressions.
[list_end]
[para]
[include include/feedback.inc]
[manpage_end]
|