File: mappings.go

package info (click to toggle)
golang-github-go-sourcemap-sourcemap 2.1.3%2Bgit20201028.eed1c20-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 96 kB
  • sloc: makefile: 7
file content (162 lines) | stat: -rw-r--r-- 2,732 bytes parent folder | download
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
158
159
160
161
162
package sourcemap

import (
	"errors"
	"io"
	"strings"

	"github.com/go-sourcemap/sourcemap/internal/base64vlq"
)

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

type mapping struct {
	genLine      int32
	genColumn    int32
	sourcesInd   int32
	sourceLine   int32
	sourceColumn int32
	namesInd     int32
}

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

	hasName bool
	value   mapping

	values []mapping
}

func parseMappings(s string) ([]mapping, error) {
	if s == "" {
		return nil, errors.New("sourcemap: mappings are empty")
	}

	rd := strings.NewReader(s)
	m := &mappings{
		rd:  rd,
		dec: base64vlq.NewDecoder(rd),

		values: make([]mapping, 0, mappingsNumber(s)),
	}
	m.value.genLine = 1
	m.value.sourceLine = 1

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

	values := m.values
	m.values = nil
	return values, nil
}

func mappingsNumber(s string) int {
	return strings.Count(s, ",") + strings.Count(s, ";")
}

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.genColumn = 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.genColumn += 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.sourceColumn += 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.sourceColumn == 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,
			genColumn:    m.value.genColumn,
			sourcesInd:   m.value.sourcesInd,
			sourceLine:   m.value.sourceLine,
			sourceColumn: m.value.sourceColumn,
			namesInd:     -1,
		})
	}
}