I'll help you create a simple Tcl parser for this Textual Tree Notation Language (TNL) with the specific indentation rules you described. Here's an implementation that captures the core parsing logic:
package require Tcl 8.6
# Parser for Textual Tree Notation Language
namespace eval TNL {
# Parse the entire input text into a nested tree structure
proc parse {text} {
set lines [split $text "\n"]
set root [list]
set stack [list]
foreach line $lines {
# Skip empty lines
if {[string trim $line] eq ""} continue
# Determine indentation
set indent [expr {[string length [string range $line 0 [expr {[string first [string trimleft $line] $line] - 1}]]]/ 2}]
set content [string trim $line]
# Create node
set node [list type normal content $content children [list]]
# Special handling for Tcl-tagged nodes
if {[string match "tnl-tcl:*" $content]} {
set node [list type tcl content [string range $content 9 end] children [list]]
}
# Manage tree hierarchy based on indentation
while {[llength $stack] > $indent} {
set stack [lrange $stack 0 end-1]
}
# Add node to appropriate parent
if {$indent == 0} {
# Root level node
lappend root $node
} else {
# Child node
set parent [lindex $stack end]
lset parent 3 [lappend [lindex $parent 3] $node]
}
# Update stack
if {$indent == [llength $stack]} {
lappend stack $node
} elseif {$indent > [llength $stack]} {
lappend stack $node
}
}
return $root
}
# Pretty print the parsed tree (for debugging)
proc printTree {tree {indent 0}} {
foreach node $tree {
set prefix [string repeat " " $indent]
puts "$prefix[dict get $node content]"
if {[dict get $node children] ne {}} {
printTree [dict get $node children] [expr {$indent + 1}]
}
}
}
# Execute Tcl-tagged nodes
proc executeTclNodes {tree} {
foreach node $tree {
if {[dict get $node type] eq "tcl"} {
set code [dict get $node content]
try {
set result [uplevel #0 $code]
puts "Tcl node result: $result"
} on error {errMsg} {
puts "Error in Tcl node: $errMsg"
}
}
# Recursively process children
if {[dict get $node children] ne {}} {
executeTclNodes [dict get $node children]
}
}
}
}
# Example usage
set sampleText {
root node
child 1
child 2
grandchild 2.1
tnl-tcl: puts "This is a Tcl node!"
nested tcl child
}
# Parse the text
set parsedTree [TNL::parse $sampleText]
# Print the parsed tree
puts "Parsed Tree:"
TNL::printTree $parsedTree
# Execute Tcl nodes
puts "\nExecuting Tcl Nodes:"
TNL::executeTclNodes $parsedTree
This implementation has several key features:
-
Strict Indentation Parsing:
- Each level of indentation is exactly 2 spaces
- Nodes are parsed into a nested structure based on indentation
-
Tcl Node Handling:
- Nodes prefixed with
tnl-tcl: are treated as Tcl code
- These nodes can be executed using
executeTclNodes
-
Flexible Structure:
- Supports nested tree structures
- Allows mixing of normal and Tcl-tagged nodes
Example usage demonstrates:
- Parsing a text with mixed normal and Tcl nodes
- Printing the parsed tree structure
- Executing Tcl-tagged nodes
You can extend this further by:
- Adding more sophisticated error handling
- Implementing more complex Tcl node execution
- Adding metadata or additional node types
Would you like me to elaborate on any part of the implementation or discuss potential extensions?