aboutsummaryrefslogtreecommitdiff
path: root/lib/tools/parse.nom
blob: f0c35a35b47e68b9f40d1f7afb2368d65a3cabc0 (plain)
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
#!/usr/bin/env nomsu -V6.15.13.8
#
    Tool to print out a parse tree of files in an easy-to-read format. Usage:
    nomsu tools/parse.nom file1 file2 directory1 ...        
    
use "filesystem"
use "commandline"

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

(print tree $t at indent $indent) means:
    if $t.type is:
        "Action":
            say "\($indent)Action (\($t.stub)):"
            for $arg in $t:
                if ($arg is syntax tree):
                    print tree $arg at indent "\$indent  "
        
        "MethodCall":
            say "\($indent)MethodCall on:"
            print tree $t.1 at indent "\$indent    "
            print tree $t.2 at indent "\$indent    "
        
        "Number":
            say "\$indent\($t.1)"
        
        "Var":
            say "\($indent)$\($t.1)"
        
        else:
            say "\$indent\($t.type):"
            for $arg in $t:
                when:
                    ($arg is syntax tree):
                        print tree $arg at indent "\$indent  "
                    
                    else:
                        say "\$indent  \(quote $arg)"

($ as xml) means:
    when (type of $) is:
        "a Syntax Tree":
            $body = ([: for $bit in $: add ($bit as xml)], joined with " ")
            if ($.type == "Action"):
                return ("
                    <Action name="\(($, get stub) as xml)">\$body</Action>
                ")
            ..else:
                return ("
                    <\($.type)>\$body</\($.type)>
                ")
        "Text":
            return ($, with "&" -> "&amp;", with "\"" -> "&quot;", with "'" -> "&apos;", with "<" -> "&lt;", with ">" -> "&gt;")
        else:
            return "\$"

command line program with $args:
    for $filename in $args.extras:
        $file = (read file $filename)
        unless $file:
            fail "File does not exist: \$filename"
        $nomsu = (NomsuCode from (Source $filename 1 (size of $file)) $file)
        $tree = ($nomsu parsed)
        if ($args.x or $args.xml):
            say ($tree as xml)
        ..else:
            print tree $tree at indent ""