| 1 |
package sourcemap |
| 2 |
|
| 3 |
import ( |
| 4 |
"io" |
| 5 |
"strings" |
| 6 |
|
| 7 |
"gopkg.in/sourcemap.v1/base64vlq" |
| 8 |
) |
| 9 |
|
| 10 |
type fn func(m *mappings) (fn, error) |
| 11 |
|
| 12 |
type sourceMap struct { |
| 13 |
Version int `json:"version"` |
| 14 |
File string `json:"file"` |
| 15 |
SourceRoot string `json:"sourceRoot"` |
| 16 |
Sources []string `json:"sources"` |
| 17 |
Names []interface{} `json:"names"` |
| 18 |
Mappings string `json:"mappings"` |
| 19 |
} |
| 20 |
|
| 21 |
type mapping struct { |
| 22 |
genLine int |
| 23 |
genCol int |
| 24 |
sourcesInd int |
| 25 |
sourceLine int |
| 26 |
sourceCol int |
| 27 |
namesInd int |
| 28 |
} |
| 29 |
|
| 30 |
type mappings struct { |
| 31 |
rd *strings.Reader |
| 32 |
dec *base64vlq.Decoder |
| 33 |
|
| 34 |
hasName bool |
| 35 |
value mapping |
| 36 |
|
| 37 |
values []mapping |
| 38 |
} |
| 39 |
|
| 40 |
func parseMappings(s string) ([]mapping, error) { |
| 41 |
rd := strings.NewReader(s) |
| 42 |
m := &mappings{ |
| 43 |
rd: rd, |
| 44 |
dec: base64vlq.NewDecoder(rd), |
| 45 |
} |
| 46 |
m.value.genLine = 1 |
| 47 |
m.value.sourceLine = 1 |
| 48 |
|
| 49 |
err := m.parse() |
| 50 |
if err != nil { |
| 51 |
return nil, err |
| 52 |
} |
| 53 |
return m.values, nil |
| 54 |
} |
| 55 |
|
| 56 |
func (m *mappings) parse() error { |
| 57 |
next := parseGenCol |
| 58 |
for { |
| 59 |
c, err := m.rd.ReadByte() |
| 60 |
if err == io.EOF { |
| 61 |
m.pushValue() |
| 62 |
return nil |
| 63 |
} |
| 64 |
if err != nil { |
| 65 |
return err |
| 66 |
} |
| 67 |
|
| 68 |
switch c { |
| 69 |
case ',': |
| 70 |
m.pushValue() |
| 71 |
next = parseGenCol |
| 72 |
case ';': |
| 73 |
m.pushValue() |
| 74 |
|
| 75 |
m.value.genLine++ |
| 76 |
m.value.genCol = 0 |
| 77 |
|
| 78 |
next = parseGenCol |
| 79 |
default: |
| 80 |
err := m.rd.UnreadByte() |
| 81 |
if err != nil { |
| 82 |
return err |
| 83 |
} |
| 84 |
|
| 85 |
next, err = next(m) |
| 86 |
if err != nil { |
| 87 |
return err |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
func parseGenCol(m *mappings) (fn, error) { |
| 94 |
n, err := m.dec.Decode() |
| 95 |
if err != nil { |
| 96 |
return nil, err |
| 97 |
} |
| 98 |
m.value.genCol += n |
| 99 |
return parseSourcesInd, nil |
| 100 |
} |
| 101 |
|
| 102 |
func parseSourcesInd(m *mappings) (fn, error) { |
| 103 |
n, err := m.dec.Decode() |
| 104 |
if err != nil { |
| 105 |
return nil, err |
| 106 |
} |
| 107 |
m.value.sourcesInd += n |
| 108 |
return parseSourceLine, nil |
| 109 |
} |
| 110 |
|
| 111 |
func parseSourceLine(m *mappings) (fn, error) { |
| 112 |
n, err := m.dec.Decode() |
| 113 |
if err != nil { |
| 114 |
return nil, err |
| 115 |
} |
| 116 |
m.value.sourceLine += n |
| 117 |
return parseSourceCol, nil |
| 118 |
} |
| 119 |
|
| 120 |
func parseSourceCol(m *mappings) (fn, error) { |
| 121 |
n, err := m.dec.Decode() |
| 122 |
if err != nil { |
| 123 |
return nil, err |
| 124 |
} |
| 125 |
m.value.sourceCol += n |
| 126 |
return parseNamesInd, nil |
| 127 |
} |
| 128 |
|
| 129 |
func parseNamesInd(m *mappings) (fn, error) { |
| 130 |
n, err := m.dec.Decode() |
| 131 |
if err != nil { |
| 132 |
return nil, err |
| 133 |
} |
| 134 |
m.hasName = true |
| 135 |
m.value.namesInd += n |
| 136 |
return parseGenCol, nil |
| 137 |
} |
| 138 |
|
| 139 |
func (m *mappings) pushValue() { |
| 140 |
if m.value.sourceLine == 1 && m.value.sourceCol == 0 { |
| 141 |
return |
| 142 |
} |
| 143 |
|
| 144 |
if m.hasName { |
| 145 |
m.values = append(m.values, m.value) |
| 146 |
m.hasName = false |
| 147 |
} else { |
| 148 |
m.values = append(m.values, mapping{ |
| 149 |
genLine: m.value.genLine, |
| 150 |
genCol: m.value.genCol, |
| 151 |
sourcesInd: m.value.sourcesInd, |
| 152 |
sourceLine: m.value.sourceLine, |
| 153 |
sourceCol: m.value.sourceCol, |
| 154 |
namesInd: -1, |
| 155 |
}) |
| 156 |
} |
| 157 |
} |