GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — trackparse/go/parser.go at go/v0.8.1 · 4matic/trackparse

Expand file tree

/

parser.go

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
235 lines (216 loc) · 6.63 KB
/

parser.go

Copy path

File metadata and controls

235 lines (216 loc) · 6.63 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package trackparse
import (
"cmp"
"slices"
"strings"
)
// The pipeline: R1, R2, R4, R3/R6, R7, R8, R9, plus R10 runArtists (js/src/parser.ts,
// rust/src/parser.rs). The engine itself (options, tables, known keys) is in context.go.
// uploaderBase is the offset used for uploader text, so it sorts before everything from the
// input.
const uploaderBase = -1_000_000_000
// skelOf scans a piece and trims it, warning on unbalanced brackets (R3.3).
func skelOf(p piece, c *ctx) skel {
sk, unbalanced := scan(p.text, p.base)
if unbalanced {
c.warn(WarningUnbalancedBrackets)
}
return trimSkel(&sk)
}
// uploaderSkel is R6.8: the uploader minus a trailing " - Topic" and "VEVO", or nil.
func uploaderSkel(uploader string) *skel {
if uploader == "" {
return nil
}
u := normalize(uploader)
if rest, ok := strings.CutSuffix(u, " - Topic"); ok {
u = rest
}
if rest, ok := strings.CutSuffix(u, "VEVO"); ok {
u = jsTrimEnd(rest)
}
if u == "" {
return nil
}
sk, _ := scan([]rune(u), uploaderBase)
return &sk
}
// emptyTrack is the R9.5 result for an input that is empty after R1, and the start of every
// other result.
func emptyTrack(input string, mode Mode) Track {
return Track{
Input: input,
Mode: mode,
Artists: []Artist{},
Versions: []Version{},
Junk: []Junk{},
Warnings: []Warning{},
}
}
// staged is the R1, R2 and R4 stage of run, kept so the tracklist probe's work is not redone.
type staged struct {
e *engine
c *ctx
pre preludeResult
track Track
// working is the skeleton after R4; nil when the input is empty after R1 (R9.5: track is
// final).
working *skel
}
// stage runs R1, R2 and R4.
func (e *engine) stage(input string) staged {
c := e.newCtx()
norm := normalize(input)
pre := prelude(norm, e.opts.mode, c)
track := emptyTrack(input, pre.mode)
if norm == "" {
return staged{e: e, c: c, pre: pre, track: track} // R9.5
}
// R4
prefix := stripPrefixes(pre.working.text, pre.working.base, pre.mode, c)
track.Timestamp = prefix.timestamp
track.Position = prefix.position
working := skelOf(piece{text: prefix.rest, base: prefix.base}, c)
return staged{e: e, c: c, pre: pre, track: track, working: &working}
}
// run is R1 to R9 on one (already sanitized) input; listContext is the R13.4.2 flag that only
// matters in tracklist mode (R6.10).
func (e *engine) run(input string, listContext bool) Track {
return finish(e.stage(input), listContext)
}
// runArtists is R10 on one (already sanitized) input.
func (e *engine) runArtists(input string) []Artist {
c := e.newCtx()
norm := normalize(input)
if norm == "" {
return []Artist{}
}
sk, _ := scan([]rune(norm), 0)
side := parseArtistSide(&sk, c)
artists := dedupArtists(side.credits)
for i := range artists {
artists[i].Source = SourceArtist
}
if artists == nil {
artists = []Artist{}
}
return artists
}
// lineProbe is facts about a line's prefix and separator search, for the tracklist module
// (R13.4.2, R13.5).
type lineProbe struct {
// position: R4.2 took a position.
position bool
// spacedDash: the skeleton R6 searches (after R2 and R4) has a spaced dash (R6.1).
spacedDash bool
// empty: the input is empty after R1.
empty bool
staged staged
}
// probe is R1, R2 and R4 only, as run does them: no artist or title parsing. finish completes
// the parse from the same stage, so a probed line is not normalized twice.
func (e *engine) probe(input string) lineProbe {
st := e.stage(input)
return lineProbe{
position: st.track.Position != nil,
spacedDash: st.pre.pipeTitle == nil && st.working != nil &&
len(splitAtSpacedDashes(st.working, true)) >= 2,
empty: st.working == nil,
staged: st,
}
}
// finish is the parse of the probed input (R6 to R9 only). Call it once per probe: the probe's
// per-call state (warnings) is consumed.
func (p *lineProbe) finish(listContext bool) Track {
return finish(p.staged, listContext)
}
// byPos sorts items by offset, stably (like the JS sort).
func byPos[T any](items []T, pos func(*T) int) []T {
slices.SortStableFunc(items, func(a, b T) int { return cmp.Compare(pos(&a), pos(&b)) })
return items
}
// finish is R6 to R9 on a staged input.
func finish(st staged, listContext bool) Track {
c, pre, track := st.c, st.pre, st.track
if st.working == nil {
return track
}
working := *st.working
var out extracted
var artistSkel *skel
var titleSkel skel
var relaxed bool
if pre.pipeTitle != nil {
// R2.4 step 3: "artist | title", R6 skipped.
titleSkel = skelOf(*pre.pipeTitle, c)
if !working.isEmpty() {
artistSkel = &working
}
relaxed = artistSkel != nil
} else {
// R8.7: a bullet suffix is cut before R6, which then sees only the head.
head := working
bullet, cut := cutBulletSuffix(&working, c)
if cut {
head = bullet.head
track.Label = bullet.label
out.junk = append(out.junk, bullet.junk...)
}
sep := findSeparator(separatorInput{
skel: head,
mode: pre.mode,
uploader: uploaderSkel(st.e.opts.uploader),
listContext: listContext,
bulletHead: cut,
}, c)
addPeeled(&out, sep.peeled)
artistSkel = sep.artist
titleSkel = sep.title
relaxed = sep.split
}
var artistSide artistSideResult
if artistSkel != nil {
artistSide = parseArtistSide(artistSkel, c)
}
titleSide := parseTitleSide(titleSkel, relaxed, pre.mode == ModeYouTube, &out, c)
// R9
versions := byPos(out.versions, func(v *versionDraft) int { return v.pos })
for _, v := range versions {
if v.ambiguousMix {
c.warn(WarningAmbiguousMixCredit)
break
}
}
credits := slices.Concat(artistSide.credits, byPos(out.credits, func(x *credit) int { return x.pos }))
track.Artists = dedupArtists(credits)
if track.Artists == nil {
track.Artists = []Artist{}
}
track.Title = titleSide.title
track.Versions = make([]Version, 0, len(versions))
for _, v := range versions {
track.Versions = append(track.Versions, toVersion(v))
}
track.FullTitle = fullTitleOf(track.Title, track.Versions)
// R9.6: the first year in text order.
years := slices.Concat(artistSide.years, out.years)
years = byPos(years, func(y *positionedYear) int { return y.pos })
if len(years) > 0 {
y := years[0].year
track.Year = &y
}
track.Flags = Flags{
Explicit: out.explicit || artistSide.explicit,
Clean: out.clean || artistSide.clean,
UnknownArtist: artistSide.unknownArtist || out.unknownArtist,
UnknownTitle: titleSide.unknownTitle,
}
junk := slices.Concat(pre.junk, artistSide.junk, out.junk)
junk = byPos(junk, func(j *positionedJunk) int { return j.pos })
track.Junk = make([]Junk, 0, len(junk))
for _, j := range junk {
track.Junk = append(track.Junk, Junk{Raw: j.raw, Kind: j.kind})
}
track.Warnings = append([]Warning{}, c.warnings...)
return track
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.