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 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
|
# -*- tcl -*-
# Copyright (c) 2009 Andreas Kupries <andreas_kupries@sourceforge.net>
# Recursive descent parser for Tcl commands embedded in a string. (=>
# subst -novariables, without actual evaluation of the embedded
# commands). Useful for processing templates, etc. The result is an
# abstract syntax tree of strings and commands, which in turn have
# strings and commands as arguments.
# The tree can be processed further. The nodes of the tree are
# annotated with line/column/offset information to allow later stages
# the reporting of higher-level syntax and semantic errors with exact
# locations in the input.
# TODO :: Add ability to report progress through the
# TODO :: input. Callback. Invoked in 'Initialize', 'Step', and
# TODO :: 'Finalize'.
# TODO :: Investigate possibility of using tclparser package
# TODO :: ('parser') to handle the command pieces embedded in the
# TODO :: text.
# # ## ### ##### ######## ############# #####################
## Requirements
package require Tcl 8.5 9 ; # Required runtime.
package require snit ; # OO system.
package require fileutil ; # File utilities.
package require logger ; # User feedback.
package require struct::list ; # Higher-order list operations.
package require struct::stack ; # Stacks
package require struct::set ; # Finite sets
package require treeql ; # Tree queries and transformation.
# # ## ### ##### ######## ############# #####################
##
logger::initNamespace ::doctools::tcl::parse
snit::type ::doctools::tcl::parse {
# # ## ### ##### ######## #############
## Public API
typemethod file {t path {root {}}} {
$type text $t [fileutil::cat -translation binary -encoding binary $path] $root
}
typemethod text {t text {root {}}} {
# --- --- --- --------- --------- ---------
# Phase 1. Lexical processing.
# The resulting tree contains the raw tokens. See
# below for the specification of the resulting tree
# structure.
#
# This part is a recursive descent parser using Tcl's 12 rules
# for processing the input. Note: Variable references are not
# recognized, they are processed like regular text.
Initialize $t $text $root
String
Finalize
# Tree structure
# - All nodes but the root have the attributes 'type', 'range', 'line', and 'col'.
#
# * 'type' in { Command, Text, Backslash, Word, Quote, Continuation, QBrace }
# * 'range' is 2-element list (offset start, offset end)
# * 'line' is integer number > 0
# * 'col' is integer number >= 0
#
# 'type' specifies what sort of token the node contains.
#
# 'range' is the location of the token as offsets in
# characters from the beginning of the string, for
# first and last character in the token. EOL markers
# count as one character. This can be empty.
#
# 'line', 'col' are the location of the first character
# AFTER the token, as the line and column the character is
# on and at.
#
# Meaning of the various node types
#
# Command .... : A command begins here, the text in the range
# .............. is the opening bracket.
# Text ....... : A text segment in a word, anything up to the
# .............. beginning of a backslash sequence or of an
# .............. embedded command.
# Backslash .. : A backslash sequence. The text under the
# .............. range is the whole sequence.
# Word ....... : The beginning of an unquoted, quoted or
# .............. braced word. The text under the range is the
# .............. opening quote or brace, if any. The range is
# .............. empty for an unquoted word.
# Quote ...... : An embedded double-quote character which is
# .............. not the end of a quoted string (a special
# .............. type of backslash sequence). The range is the
# .............. whole sequence.
# Continuation : A continuation line in an unquoted, quoted,
# .............. or braced string. The range covers the whole
# .............. sequence, including the whitespace trailing
# .............. it.
# QBrace ..... : A quoted brace in a braced string. A special
# .............. kind of backslash sequence. The range covers
# .............. the whole sequence.
# --- --- --- --------- --------- ---------
# Phase 2. Convert the token tree into a syntax tree.
# This phase simplifies the tree by converting and
# eliminating special tokens, and further decouples
# it from the input by storing the relevant string
# ranges of the input in the tree. For the the
# specification of the resulting structure see method
# 'Verify'.
#
# The sub-phases are and do
#
# (a) Extract the string information from the input and store
# them in their Text tokens.
# (b) Convert the special tokens (QBrace, Backslash, Quote,
# Continuation) into equivalent 'Text' tokens, with proper
# string information.
# (c) Merge adjacent 'Text' tokens.
# (d) Remove irrelevant 'Word' tokens. These are tokens with a
# single Text token as child. Word tokens without children
# however represent empty strings. They are converted into
# an equivalent Text node instead.
# (e) Pull the first word of commands into the command token,
# and ensure that it is not dynamic, i.e not an embedded
# command.
ShowTree $t "Raw tree"
set q [treeql %AUTO% -tree $t]
# (a)
foreach n [$q query tree withatt type Text] {
struct::list assign [$t get $n range] a e
#$t unset $n range
$t set $n text [string range $mydata $a $e]
}
ShowTree $t "Text annotation"
# (b1)
foreach n [$q query tree withatt type QBrace] {
struct::list assign [$t get $n range] a e
incr a ; # Skip backslash
#$t unset $n range
$t set $n text [string range $mydata $a $e]
$t set $n type Text
}
ShowTree $t "Special conversion 1, quoted braces"
# (b2)
foreach n [$q query tree withatt type Backslash] {
struct::list assign [$t get $n range] a e
#$t unset $n range
$t set $n text [subst -nocommands -novariables [string range $mydata $a $e]]
#puts <'[string range $mydata $a $e]'>
#puts _'[subst -nocommands -novariables [string range $mydata $a $e]]'_
$t set $n type Text
}
ShowTree $t "Special conversion 2, backslash sequences"
# (b3)
foreach n [$q query tree withatt type Quote] {
#$t unset $n range
$t set $n text "\""
$t set $n type Text
}
ShowTree $t "Special conversion 3, quoted double quotes"
# (b4)
foreach n [$q query tree withatt type Continuation] {
#$t unset $n range
$t set $n text { }
$t set $n type Text
}
ShowTree $t "Special conversion 4, continuation lines"
# (c)
foreach n [$q query tree withatt type Text right withatt type Text] {
set left [$t previous $n]
$t append $left text [$t get $n text]
# Extend covered range. Copy location.
struct::list assign [$t get $left range] a _
struct::list assign [$t get $n range] _ e
$t set $left range [list $a $e]
$t set $left line [$t get $n line]
$t set $left col [$t get $n col]
$t delete $n
}
ShowTree $t "Merged adjacent texts"
# (d)
foreach n [$q query tree withatt type Word] {
if {![$t numchildren $n]} {
$t set $n type Text
$t set $n text {}
} elseif {[$t numchildren $n] == 1} {
$t cut $n
}
}
ShowTree $t "Dropped simple words"
# (e)
foreach n [$q query tree withatt type Command] {
set first [lindex [$t children $n] 0]
if {[$t get $first type] eq "Word"} {
error {Dynamic command name}
}
$t set $n text [$t get $first text]
$t set $n range [$t get $first range]
$t set $n line [$t get $first line]
$t set $n col [$t get $first col]
$t delete $first
}
ShowTree $t "Command lifting"
$q destroy
Verify $t
return
}
proc Verify {t} {
# Tree structure ...
# Attributes Values
# - type string in {'Command','Text','Word'} (phase 2)
# - range 2-tuple (integer, integer), can be empty. start and end offset of the word in the input string.
# - line integer, line the node starts on. First line is 1
# - col integer, column the node starts on (#char since start of line, first char is 0)
# Constraints
# .(i) The root node has no attributes at all.
# .(ii) The children of the root are Command and Text nodes in semi-alternation.
# I.e.: After a Text node a Command has to follow.
# After a Command node either Text or Command can follow.
# .(iii) The children of a Command node are Text, Word, and Command nodes, the command arguments. If any.
# .(iv) The children of a Word node are Command and Text nodes in semi-alternation.
# .(v) All Text nodes are leafs.
# .(vi) Any Command node can be a leaf.
# .(vii) Word nodes cannot be leafs.
# .(viii) All non-root nodes have the attributes 'type', 'range', 'col', and 'line'.
foreach n [$t nodes] {
if {[$t parent $n] eq ""} {
# (ii)
set last {}
foreach c [$t children $n] {
set type [$t get $c type]
if {![struct::set contains {Command Text} $type]} {
return -code error "$c :: Bad node type $type in child of root node"
} elseif {($type eq $last) && ($last eq "Text")} {
return -code error "$c :: Bad node $type, not semi-alternating"
}
set last $type
}
# (i)
if {[llength [$t getall $n]]} {
return -code error "$n :: Bad root node, has attributes, should not"
}
continue
} else {
# (viii)
foreach k {range line col} {
if {![$t keyexists $n $k]} {
return -code error "$n :: Bad node, attribute '$k' missing"
}
}
}
set type [$t get $n type]
switch -exact -- $type {
Command {
# (vi)
# No need to check children. May have some or not,
# and no specific sequence is required.
}
Word {
# (vii)
if {![llength [$t children $n]]} {
return -code error "$n :: Bad word node is leaf"
}
# (iv)
set last {}
foreach c [$t children $n] {
set type [$t get $c type]
if {![struct::set contains {Command Text} $type]} {
return -code error "$n :: Bad node type $type in word node"
} elseif {($type eq $last) && ($last eq "Text")} {
return -code error "$c :: Bad node $type, not semi-alternating"
}
set last $type
}
}
Text {
# (v)
if {[llength [$t children $n]]} {
return -code error "$n :: Bad text node is not leaf"
}
}
default {
# (iii)
return -code error "$n :: Bad node type $type"
}
}
}
return
}
# # ## ### ##### ######## #############
## Internal methods, lexical processing
proc String {} {
while 1 {
Note @String
if {[EOF]} break
if {[Command]} continue
if {[TextSegment]} continue
if {[Backslash]} continue
Stop ;# Unexpected character
}
Note @EOF
return
}
proc Command {} {
# A command starts with an opening bracket.
Note ?Command
if {![Match "\\A(\\\[)" range]} {
Note \t%No-Command
return 0
}
Note !Command
PushRoot [Node Command $range]
while {[Word]} {
# Step over any whitespace after the last word
Whitespace
# Command ends at the closing bracket
if {[Match "\\A(\\])" range]} break
if {![EOF]} continue
Stop ;# Unexpected end of input
}
Note !CommandStop
PopRoot
return 1
}
proc TextSegment {} {
# A text segment is anything up to a command start or start of
# a back slash sequence.
Note ?TextSegment
if {![Match "\\A(\[^\\\[\]+)" range]} {
Note \t%No-TextSegment
return 0
}
Note !TextSegment
Node Text $range
return 1
}
proc TextSegmentWithoutQuote {} {
Note ?TextSegmentWithoutQuote
# A text segment without quote is anything up to a command
# start or start of a back slash sequence, or a double-quote
# character.
if {![Match "\\A(\[^\"\\\\\[\]+)" range]} {
Note \t%No-TextSegmentWithoutQuote
return 0
}
Note !TextSegment
Node Text $range
return 1
}
proc Backslash {} {
Note ?Backslash
if {
![Match "\\A(\\\\x\[a-fA-F0-9\]+)" range] &&
![Match "\\A(\\\\u\[a-fA-F0-9\]{1,4})" range] &&
![Match "\\A(\\\\\[0-2\]\[0-7\]{2})" range] &&
![Match "\\A(\\\\\[0-7\]{1,2})" range] &&
![Match {\A(\\[abfnrtv])} range]
} {
Note \t%No-Backslash
return 0
}
Note !Backslash
Node Backslash $range
return 1
}
proc Word {} {
Note ?Word
if {[QuotedWord]} {return 1}
if {[BracedWord 0]} {return 1}
return [UnquotedWord]
}
proc Whitespace {} {
Note ?Whitespace
if {![Match {\A([ \t]|(\\\n[ \t]*))+} range]} {
Note \t%No-Whitespace
return 0
}
Note !Whitespace
return 1
}
proc QuotedWord {} {
# A quoted word starts with a double quote.
Note ?QuotedWord
if {![Match "\\A(\")" range]} {
Note \t%No-QuotedWord
return 0
}
Note !QuotedWord
PushRoot [Node Word $range]
QuotedString
PopRoot
return 1
}
proc BracedWord {keepclose} {
# A braced word starts with an opening brace.
Note ?BracedWord/$keepclose
if {![Match "\\A(\{)" range]} {
Note \t%No-BracedWord/$keepclose
return 0
}
Note !BracedWord/$keepclose
PushRoot [Node Word $range]
BracedString $keepclose
PopRoot
return 1
}
proc UnquotedWord {} {
Note !UnquotedWord
PushRoot [Node Word {}]
UnquotedString
PopRoot
return 1
}
proc QuotedString {} {
Note !QuotedString
while 1 {
Note !QuotedStringPart
# A quoted word (and thus the embedded string) ends with
# double quote.
if {[Match "\\A(\")" range]} {
return
}
# Now try to match possible pieces of the string. This is
# a repetition of the code in 'String', except for the
# different end condition above, and the possible embedded
# double quotes and continuation lines the outer string
# can ignore.
if {[Command]} continue
if {[Quote]} continue
if {[QuotedBraces]} continue
if {[Continuation]} continue
if {[Backslash]} continue
# Check after backslash recognition and processing
if {[TextSegmentWithoutQuote]} continue
Stop ;# Unexpected character or end of input
}
return
}
proc BracedString {keepclose} {
while 1 {
Note !BracedStringPart
# Closing brace encountered. keepclose is set if we are in
# a nested braced string. Only then do we have to put the
# brace as a regular text piece into the string
if {[Match "\\A(\})" range]} {
if {$keepclose} {
Node Text $range
}
return
}
# Special sequences.
if {[QuotedBraces]} continue
if {[Continuation]} continue
if {[BracedWord 1]} continue
# A backslash without a brace coming after is regular a
# character.
if {[Match {\A(\\)} range]} {
Node Text $range
continue
}
# Gooble sequence of regular characters. Stops at
# backslash and braces. Backslash stop is needed to handle
# the case of them starting a quoted brace.
if {[Match {\A([^\\\{\}]*)} range]} {
Node Text $range
continue
}
Stop ;# Unexpected character or end of input.
}
}
proc UnquotedString {} {
while 1 {
Note !UnquotedStringPart
# Stop conditions
# - end of string
# - whitespace
# - Closing bracket (end of command the word is in)
if {[EOF]} return
if {[Whitespace]} return
if {[Peek "\\A(\\\])" range]} return
# Match each possible type of part
if {[Command]} continue
if {[Quote]} continue
if {[Continuation]} continue
if {[Backslash]} continue
# Last, capture backslash sequences first.
if {[UnquotedTextSegment]} continue
Stop ;# Unexpected character or end of input.
}
return
}
proc UnquotedTextSegment {} {
# All chars but whitespace and brackets (start or end of
# command).
Note ?UnquotedTextSegment
if {![Match {\A([^\]\[\t\n ]+)} range]} {
Note \t%No-UnquotedTextSegment
return 0
}
Note !UnquotedTextSegment
Node Text $range
return 1
}
proc Quote {} {
Note ?EmdeddedQuote
if {![Match "\\A(\\\")" range]} {
Note \t%No-EmdeddedQuote
return 0
}
# Embedded double quote, not the end of the quoted string.
Note !EmdeddedQuote
Node Quote $range
return 1
}
proc Continuation {} {
Note ?ContinuationLine
if {![Match "\\A(\\\\\n\[ \t\]*)" range]} {
Note \t%No-ContinuationLine
return 0
}
Note !ContinuationLine
Node Continuation $range
return 1
}
proc QuotedBraces {} {
Note ?QuotedBrace
if {
![Match "\\A(\\\\\{)" range] &&
![Match "\\A(\\\\\})" range]
} {
Note \t%No-QuotedBrace
return 0
}
Note !QuotedBrace
Node QBrace $range
return 1
}
# # ## ### ##### ######## #############
## Tree construction helper commands.
proc Node {what range} {
set n [lindex [$mytree insert $myroot end] 0]
Note "+\tNode $n @ $myroot $what"
$mytree set $n type $what
$mytree set $n range $range
$mytree set $n line $myline
$mytree set $n col $mycol
return $n
}
proc PushRoot {x} {
Note "Push Root = $x"
$myrootstack push $myroot
set myroot $x
return
}
proc PopRoot {} {
set myroot [$myrootstack pop]
Note "Pop Root = $myroot"
return
}
# # ## ### ##### ######## #############
## Error reporting
proc Stop {} {
::variable myerr
set ahead [string range $mydata $mypos [expr {$mypos + 30}]]
set err [expr {![string length $ahead] ? "eof" : "char"}]
set ahead [string map [list \n \\n \t \\t \r \\r] [string range $ahead 0 0]]
set caller [lindex [info level -1] 0]
set msg "[format $myerr($err) $ahead $caller] at line ${myline}.$mycol"
set err [list doctools::tcl::parse $err $mypos $myline $mycol]
return -code error -errorcode $err $msg
}
# # ## ### ##### ######## #############
## Input processing. Match/peek lexemes, update location after
## stepping over a range. Match = Peek + Step.
proc EOF {} {
Note "?EOF($mypos >= $mysize) = [expr {$mypos >= $mysize}]"
return [expr {$mypos >= $mysize}]
}
proc Match {pattern rv} {
upvar 1 $rv range
set ok [Peek $pattern range]
if {$ok} {Step $range}
return $ok
}
proc Peek {pattern rv} {
upvar 1 $rv range
Note Peek($pattern)----|[string map [list "\n" "\\n" "\t" "\\t"] [string range $mydata $mypos [expr {$mypos + 30}]]]|
if {[regexp -start $mypos -indices -- $pattern $mydata -> range]} {
Note \tOK
return 1
} else {
Note \tFAIL
return 0
}
}
proc Step {range} {
struct::list assign $range a e
set mylastpos $mypos
set mypos $e
incr mypos
set pieces [split [string range $mydata $a $e] \n]
set delta [string length [lindex $pieces end]]
set nlines [expr {[llength $pieces] - 1}]
if {$nlines} {
incr myline $nlines
set mycol $delta
} else {
incr mycol $delta
}
return
}
# # ## ### ##### ######## #############
## Setup / Shutdown of parser/lexer
proc Initialize {t text root} {
set mytree $t
if {$root eq {}} {
set myroot [$t rootname]
} else {
set myroot $root
}
if {$myrootstack ne {}} Finalize
set myrootstack [struct::stack %AUTO%]
$myrootstack clear
set mydata $text
set mysize [string length $mydata]
set mypos 0
set myline 1
set mycol 0
return
}
proc Finalize {} {
$myrootstack destroy
set myrootstack {}
return
}
# # ## ### ##### ######## #############
## Debugging helper commands
## Add ability to disable these.
## For the tree maybe add ability to dump through a callback ?
proc Note {text} {
upvar 1 range range
set m {}
append m "$text "
if {[info exists range]} {
append m "($range) "
if {$range != {}} {
foreach {a e} $range break
append m " = \"[string map [list "\n" "\\n" "\t" "\\t"] \
[string range $mydata $a $e]]\""
}
} else {
append m "@$mypos ($myline/$mycol)"
}
#log::debug $m
puts $m
return
}
#proc ShowTreeX {args} {}
proc ShowTreeX {t x} {
puts "=== \[ $x \] [string repeat = [expr {72 - [string length $x] - 9}]]"
$t walk root -order pre -type dfs n {
set prefix [string repeat .... [$t depth $n]]
puts "$prefix$n <[DictSort [$t getall $n]]>"
}
return
}
proc Note {args} {}
proc ShowTree {args} {}
# # ## ### ##### ######## #############
proc DictSort {dict} {
array set tmp $dict
set res {}
foreach k [lsort -dict [array names tmp]] {
lappend res $k $tmp($k)
}
return $res
}
# # ## ### ##### ######## #############
## Parser state
typevariable mytree {} ; # Tree we are working on
typevariable myroot {} ; # Current root to add nodes to.
typevariable myrootstack {}
typevariable mydata {} ; # String to parse.
typevariable mysize 0 ; # Length of string to parse, cache
typevariable mylastpos ; # Last current position.
typevariable mypos 0 ; # Current parse location, offset from
typevariable myline 1 ; # the beginning of the string, line
typevariable mycol 0 ; # we are on, and the column within the
# line.
typevariable myerr -array {
char {Unexpected character '%1$s' in %2$s}
eof {Unexpected end of input in %2$s}
}
# # ## ### ##### ######## #############
## Configuration
pragma -hasinstances no ; # singleton
pragma -hastypeinfo no ; # no introspection
pragma -hastypedestroy no ; # immortal
##
# # ## ### ##### ######## #############
}
namespace eval ::doctools::tcl {
namespace export parse
}
# # ## ### ##### ######## ############# #####################
## Ready
package provide doctools::tcl::parse 0.2
return
|