~psmay/+junk/graut.dev

« back to all changes in this revision

Viewing changes to src/lib/graut/walk.coffee

  • Committer: Peter S. May
  • Date: 2012-11-21 03:01:39 UTC
  • Revision ID: peter_s._may_httppsmay.com-20121121030139-71h0dbelgrcr9aye
model.coffee : Made the nodes visitors.
walk.coffee : Began writing as visitable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
###
23
23
 
24
24
Model = require './model'
25
 
 
26
 
exports.walk = (top) ->
27
 
        # TODO
28
 
        throw Error "TODO"
 
25
{ InterpolatedValue, StringValue, ListValue, InlineOp, ExpandOp, CallOp, NonceOp, semanticError } = Model
 
26
 
 
27
callUnhandled = (obj) -> @unhandled obj
 
28
 
 
29
class Visitable
 
30
        constructor : (overrides) ->
 
31
                overrides ?= {}
 
32
                for own k, v of overrides
 
33
                        @[k] = v
 
34
        
 
35
        unhandled : (obj) -> semanticError obj, "Unexpected item"
 
36
        interpolatedValue : callUnhandled
 
37
        stringValue : callUnhandled
 
38
        listValue : callUnhandled
 
39
        inlineOp : callUnhandled
 
40
        expandOp : callUnhandled
 
41
        callOp : callUnhandled
 
42
        nonceOp : callUnhandled
 
43
        
 
44
walkVisitable = new Visitable
 
45
        interpolatedValue : (node) -> walkInterpolated node
 
46
 
 
47
exports.walk = (top) -> top.visit walkVisitable
 
48
 
 
49
 
 
50
interpElementVisitable = new Visitable
 
51
        stringValue : (node) -> textOfStringValue node
 
52
        callOp : (node) -> walkCallOp node
 
53
 
 
54
walkInterpolated = (node) -> node.visit interpElementVisitable
 
55
 
 
56
 
 
57
codepointToString = (cp) ->
 
58
        adj = (cp - 0x10000) & 0xFFFFF
 
59
        throw Error("Invalid codepoint " + cp) if cp isnt (adj + 0x10000)
 
60
        hi = adj >> 10
 
61
        lo = adj & 0x3FF
 
62
        String.fromCharCode(0xD800 + hi) + String.fromCharCode(0xDC00 + lo)
 
63
 
 
64
 
 
65
stringTextVisitable = new Visitable
 
66
        stringValue : (node) ->
 
67
                text = node.textToken.text
 
68
                if node.doBslashEscapes
 
69
                        text = text.replace ///
 
70
                                \\(?:
 
71
                                        U([0-9A-Fa-f]{8}) |
 
72
                                        u(?:
 
73
                                                ([0-9A-Fa-f]{4})
 
74
                                                \{ ([0-9A-Fa-f]+) \}
 
75
                                        ) |
 
76
                                        x([0-9A-Fa-f]{2}) |
 
77
                                        ([bfnrt0]) |
 
78
                                        ([\s\S])
 
79
                                ) ///g,
 
80
                                (fullMatch, x8, x4, xarb, x2, letter, any) ->
 
81
                                        hex = x8 ? x4 ? xarb ? x2
 
82
                                        if hex?
 
83
                                                codepointToString parseInt hex, 16
 
84
                                        else if letter?
 
85
                                                switch letter
 
86
                                                        when "b" then "\b"
 
87
                                                        when "f" then "\f"
 
88
                                                        when "n" then "\n"
 
89
                                                        when "r" then "\r"
 
90
                                                        when "t" then "\t"
 
91
                                                        when "0" then "\0"
 
92
                                        else
 
93
                                                any
 
94
                text
 
95
 
 
96
 
 
97
textOfStringValue = (node) -> node.visit stringTextVisitable
 
98
        
 
99