(86 lines)
1 #!/usr/bin/env nomsu -V7.0.02 ###3 Tool to print out a parse tree of files in an easy-to-read format. Usage:4 nomsu tools/parse.nom file1 file2 directory1 ...6 use "filesystem"7 use "commandline"9 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~11 (print tree $t at indent $indent) means:12 if $t.type is:13 "Action":14 say "\($indent)Action (\($t.stub)):"15 for $arg in $t:16 if ($arg is syntax tree):17 print tree $arg at indent "\$indent "19 "MethodCall":20 say "\($indent)MethodCall on:"21 print tree $t.1 at indent "\$indent "22 print tree $t.2 at indent "\$indent "24 "Number":25 say "\$indent\($t.1)"27 "Var":28 say "\($indent)$\($t.1)"30 else:31 say "\$indent\($t.type):"32 for $arg in $t:33 when:34 ($arg is syntax tree):35 print tree $arg at indent "\$indent "37 else:38 say "\$indent \(quote $arg)"40 ($ as xml) means:41 when (type of $) is:42 "a Syntax Tree":43 $body = ([: for $bit in $: add ($bit as xml)], joined with " ")44 if ($.type == "Action"):45 return "<Action name=\"\(($, get stub) as xml)\">\($body)</Action>"46 ..else:47 return "<\($.type)>\($body)</\($.type)>"49 "Text":50 return51 (52 ($, with "&" -> "&", with "\"" -> """, with "'" -> "'"),53 with "<" -> "<"54 ), with ">" -> ">"56 else:57 return "\$"59 ($ as lisp) means:60 when (type of $) is:61 "a Syntax Tree":62 $body = ([$.type, : for $bit in $: add ($bit as lisp)], joined with " ")63 return "(\$body)"65 "Text":66 return "\"\($, with "\\" -> "\\\\", with "\"" -> "\\\"", with "\n" -> "\\n")\""68 else:69 return "\$"71 command line program with $args:72 for $filename in $args.extras:73 $file = (read file $filename)74 unless $file:75 fail "File does not exist: \$filename"76 $nomsu = (NomsuCode from (Source $filename 1 #$file) $file)77 $tree = ($nomsu parsed)78 when:79 ($args.x or $args.xml):80 say ($tree as xml)82 ($args.l or $args.lisp):83 say ($tree as lisp)85 else:86 print tree $tree at indent ""