grammar InvoiceDSL {
token TOP {
^ <invoice>+ % \n* $
}
token invoice {
<header>
\n
<line>+
}
token header {
'invoice' \h+ <id=string> \h+ 'for' \h+ <client=string>
}
token line {
\h**4 <entry> \n?
}
token entry {
| <item>
| <tax>
| <discount>
}
token item {
'item' \h+ <desc=string> \h+ <price=num> \h+ 'x' \h+ <qty=int>
}
token tax {
'tax' \h+ <percent=num> '%'
}
token discount {
'discount' \h+ <percent=num> '%'
}
token string { \" <( <-["]>* )> \" }
token num { \d+ [ '.' \d+ ]? }
token int { \d+ }
}As an occasional Tcl coder, the example would actually be a valid Tcl script - after adding invoice, item, tax and discount procedures, the example could be run as a script. The procedures would perform actions as needed for the arguments.
It's a shame that there isn't a common library that can be used for these types of tasks. Tcl evolved into something quite complex - compiling to bytecode, object oriented features, etc, etc. Although Tcl was originally intended to be embedded in apps, that boat sailed a long time ago (except for FPGA tools, which is where I use it).