aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/gopkg.in/sourcemap.v1/sourcemap.go
blob: 0e9af1a256367e08c47c9ca82ada0e82590c8500 (plain) (blame)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package sourcemap // import "gopkg.in/sourcemap.v1"

import (
    "io"
    "strings"

    "gopkg.in/sourcemap.v1/base64vlq"
)

type fn func(m *mappings) (fn, error)

type sourceMap struct {
    Version    int           `json:"version"`
    File       string        `json:"file"`
    SourceRoot string        `json:"sourceRoot"`
    Sources    []string      `json:"sources"`
    Names      []interface{} `json:"names"`
    Mappings   string        `json:"mappings"`
}

type mapping struct {
    genLine    int
    genCol     int
    sourcesInd int
    sourceLine int
    sourceCol  int
    namesInd   int
}

type mappings struct {
    rd  *strings.Reader
    dec *base64vlq.Decoder

    hasName bool
    value   mapping

    values []mapping
}

func parseMappings(s string) ([]mapping, error) {
    rd := strings.NewReader(s)
    m := &mappings{
        rd:  rd,
        dec: base64vlq.NewDecoder(rd),
    }
    m.value.genLine = 1
    m.value.sourceLine = 1

    err := m.parse()
    if err != nil {
        return nil, err
    }
    return m.values, nil
}

func (m *mappings) parse() error {
    next := parseGenCol
    for {
        c, err := m.rd.ReadByte()
        if err == io.EOF {
            m.pushValue()
            return nil
        }
        if err != nil {
            return err
        }

        switch c {
        case ',':
            m.pushValue()
            next = parseGenCol
        case ';':
            m.pushValue()

            m.value.genLine++
            m.value.genCol = 0

            next = parseGenCol
        default:
            err := m.rd.UnreadByte()
            if err != nil {
                return err
            }

            next, err = next(m)
            if err != nil {
                return err
            }
        }
    }
}

func parseGenCol(m *mappings) (fn, error) {
    n, err := m.dec.Decode()
    if err != nil {
        return nil, err
    }
    m.value.genCol += n
    return parseSourcesInd, nil
}

func parseSourcesInd(m *mappings) (fn, error) {
    n, err := m.dec.Decode()
    if err != nil {
        return nil, err
    }
    m.value.sourcesInd += n
    return parseSourceLine, nil
}

func parseSourceLine(m *mappings) (fn, error) {
    n, err := m.dec.Decode()
    if err != nil {
        return nil, err
    }
    m.value.sourceLine += n
    return parseSourceCol, nil
}

func parseSourceCol(m *mappings) (fn, error) {
    n, err := m.dec.Decode()
    if err != nil {
        return nil, err
    }
    m.value.sourceCol += n
    return parseNamesInd, nil
}

func parseNamesInd(m *mappings) (fn, error) {
    n, err := m.dec.Decode()
    if err != nil {
        return nil, err
    }
    m.hasName = true
    m.value.namesInd += n
    return parseGenCol, nil
}

func (m *mappings) pushValue() {
    if m.value.sourceLine == 1 && m.value.sourceCol == 0 {
        return
    }

    if m.hasName {
        m.values = append(m.values, m.value)
        m.hasName = false
    } else {
        m.values = append(m.values, mapping{
            genLine:    m.value.genLine,
            genCol:     m.value.genCol,
            sourcesInd: m.value.sourcesInd,
            sourceLine: m.value.sourceLine,
            sourceCol:  m.value.sourceCol,
            namesInd:   -1,
        })
    }
}