Skip to content

Commit 9991017

Browse files
committed
refactor: remove lexer serialization
XML lexer definitions are only consumed at runtime; the writer was kept solely for a hidden migration command. Remove the unused public round-trip API and CLI exporter while preserving XML loading.
1 parent 2e40619 commit 9991017

7 files changed

Lines changed: 147 additions & 433 deletions

File tree

cmd/chroma/main.go

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"os/signal"
1111
"path"
1212
"path/filepath"
13-
"regexp"
1413
"runtime"
1514
"runtime/pprof"
1615
"sort"
@@ -48,7 +47,6 @@ command, for Go.
4847
Check bool `help:"Do not format, check for tokenisation errors instead."`
4948
Filename string `help:"Filename to use for selecting a lexer when reading from stdin."`
5049
Fail bool `help:"Exit silently with status 1 if no specific lexer was found."`
51-
XML string `hidden:"" help:"Generate XML lexer definitions." type:"existingdir" placeholder:"DIR"`
5250

5351
Lexer string `group:"select" help:"Lexer to use when formatting or path to an XML file to load." default:"autodetect" short:"l"`
5452
Style string `group:"select" help:"Style to use for formatting or path to an XML file to load." default:"swapoff" short:"s"`
@@ -153,11 +151,6 @@ func main() {
153151
"select": "Select lexer and style:",
154152
"html": "HTML formatter options:",
155153
})
156-
if cli.XML != "" {
157-
err := dumpXMLLexerDefinitions(cli.XML)
158-
ctx.FatalIfErrorf(err)
159-
return
160-
}
161154
if cli.List {
162155
listAll()
163156
return
@@ -432,34 +425,3 @@ func check(filename string, it iter.Seq[chroma.Token]) bool {
432425
}
433426
return failed
434427
}
435-
436-
var nameCleanRe = regexp.MustCompile(`[^A-Za-z0-9_#+-]`)
437-
438-
func dumpXMLLexerDefinitions(dir string) error {
439-
for _, name := range lexers.Names(false) {
440-
lex := lexers.Get(name)
441-
if rlex, ok := lex.(*chroma.RegexLexer); ok {
442-
data, err := chroma.Marshal(rlex)
443-
if err != nil {
444-
if errors.Is(err, chroma.ErrNotSerialisable) {
445-
fmt.Fprintf(os.Stderr, "warning: %q: %s\n", name, err)
446-
continue
447-
}
448-
return err
449-
}
450-
name := strings.ToLower(nameCleanRe.ReplaceAllString(lex.Config().Name, "_"))
451-
filename := filepath.Join(dir, name) + ".xml"
452-
// fmt.Println(name)
453-
_, err = os.Stat(filename)
454-
if err == nil {
455-
fmt.Fprintf(os.Stderr, "warning: %s already exists\n", filename)
456-
continue
457-
}
458-
err = os.WriteFile(filename, data, 0600)
459-
if err != nil {
460-
return err
461-
}
462-
}
463-
}
464-
return nil
465-
}

emitters.go

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@ type ValidatingEmitter interface {
1717
ValidateEmitter(rule *CompiledRule) error
1818
}
1919

20-
// SerialisableEmitter is an Emitter that can be serialised and deserialised to/from JSON.
21-
type SerialisableEmitter interface {
22-
Emitter
23-
EmitterKind() string
24-
}
25-
2620
// EmitterFunc is a function that is an Emitter.
2721
type EmitterFunc func(groups []string, state *LexerState) iter.Seq[Token]
2822

@@ -44,8 +38,6 @@ func ByGroups(emitters ...Emitter) Emitter {
4438
return &byGroupsEmitter{Emitters: emitters}
4539
}
4640

47-
func (b *byGroupsEmitter) EmitterKind() string { return "bygroups" }
48-
4941
func (b *byGroupsEmitter) ValidateEmitter(rule *CompiledRule) error {
5042
if len(rule.Regexp.GetGroupNumbers())-1 != len(b.Emitters) {
5143
return fmt.Errorf("number of groups %d does not match number of emitters %d", len(rule.Regexp.GetGroupNumbers())-1, len(b.Emitters))
@@ -146,7 +138,6 @@ type usingByGroup struct {
146138
Emitters Emitters `xml:"emitters"`
147139
}
148140

149-
func (u *usingByGroup) EmitterKind() string { return "usingbygroup" }
150141
func (u *usingByGroup) Emit(groups []string, state *LexerState) iter.Seq[Token] {
151142
// bounds check
152143
if len(u.Emitters) != len(groups)-1 {
@@ -173,7 +164,7 @@ func (u *usingByGroup) Emit(groups []string, state *LexerState) iter.Seq[Token]
173164

174165
// UsingLexer returns an Emitter that uses a given Lexer for parsing and emitting.
175166
//
176-
// This Emitter is not serialisable.
167+
// This Emitter cannot be represented in an XML lexer definition.
177168
func UsingLexer(lexer Lexer) Emitter {
178169
return EmitterFunc(func(groups []string, _ *LexerState) iter.Seq[Token] {
179170
it, err := lexer.Tokenise(&TokeniseOptions{State: "root", Nested: true}, groups[0])
@@ -188,8 +179,6 @@ type usingEmitter struct {
188179
Lexer string `xml:"lexer,attr"`
189180
}
190181

191-
func (u *usingEmitter) EmitterKind() string { return "using" }
192-
193182
func (u *usingEmitter) Emit(groups []string, state *LexerState) iter.Seq[Token] {
194183
if state.Registry == nil {
195184
panic(fmt.Sprintf("no LexerRegistry available for Using(%q)", u.Lexer))
@@ -216,8 +205,6 @@ type usingSelfEmitter struct {
216205
State string `xml:"state,attr"`
217206
}
218207

219-
func (u *usingSelfEmitter) EmitterKind() string { return "usingself" }
220-
221208
func (u *usingSelfEmitter) Emit(groups []string, state *LexerState) iter.Seq[Token] {
222209
it, err := state.Lexer.Tokenise(&TokeniseOptions{State: u.State, Nested: true}, groups[0])
223210
if err != nil {

mutators.go

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@ type Mutator interface {
1212
Mutate(state *LexerState) error
1313
}
1414

15-
// SerialisableMutator is a Mutator that can be serialised and deserialised.
16-
type SerialisableMutator interface {
17-
Mutator
18-
MutatorKind() string
19-
}
20-
2115
// ValidatingMutator is a Mutator that can validate itself against the compiled rules.
2216
//
2317
// Validation occurs once, after all LexerMutators have been applied.
@@ -64,21 +58,6 @@ func (m *multiMutator) UnmarshalXML(d *xml.Decoder, start xml.StartElement) erro
6458
}
6559
}
6660

67-
func (m *multiMutator) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
68-
name := xml.Name{Local: "mutators"}
69-
if err := e.EncodeToken(xml.StartElement{Name: name}); err != nil {
70-
return err
71-
}
72-
for _, m := range m.Mutators {
73-
if err := marshalMutator(e, m); err != nil {
74-
return err
75-
}
76-
}
77-
return e.EncodeToken(xml.EndElement{Name: name})
78-
}
79-
80-
func (m *multiMutator) MutatorKind() string { return "mutators" }
81-
8261
func (m *multiMutator) ValidateMutator(rules CompiledRules) error {
8362
for _, mutator := range m.Mutators {
8463
if validate, ok := mutator.(ValidatingMutator); ok {
@@ -113,8 +92,6 @@ func Include(state string) Rule {
11392
return Rule{Mutator: &includeMutator{state}}
11493
}
11594

116-
func (i *includeMutator) MutatorKind() string { return "include" }
117-
11895
func (i *includeMutator) Mutate(s *LexerState) error {
11996
return fmt.Errorf("should never reach here Include(%q)", i.State)
12097
}
@@ -132,8 +109,6 @@ type combinedMutator struct {
132109
States []string `xml:"state,attr"`
133110
}
134111

135-
func (c *combinedMutator) MutatorKind() string { return "combined" }
136-
137112
// Combined creates a new anonymous state from the given states, and pushes that state.
138113
func Combined(states ...string) Mutator {
139114
return &combinedMutator{states}
@@ -169,8 +144,6 @@ var (
169144
_ ValidatingMutator = (*multiMutator)(nil)
170145
)
171146

172-
func (p *pushMutator) MutatorKind() string { return "push" }
173-
174147
func (p *pushMutator) ValidateMutator(rules CompiledRules) error {
175148
for _, state := range p.States {
176149
if state == "#pop" {
@@ -209,8 +182,6 @@ type popMutator struct {
209182
Depth int `xml:"depth,attr"`
210183
}
211184

212-
func (p *popMutator) MutatorKind() string { return "pop" }
213-
214185
func (p *popMutator) Mutate(state *LexerState) error {
215186
if len(state.Stack) == 0 {
216187
return fmt.Errorf("nothing to pop")

serialise_test.go

Lines changed: 0 additions & 164 deletions
This file was deleted.

types.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,5 +371,3 @@ func (t TokenType) InSubCategory(other TokenType) bool {
371371
func (t TokenType) Emit(groups []string, _ *LexerState) iter.Seq[Token] {
372372
return Literator(Token{Type: t, Value: groups[0]})
373373
}
374-
375-
func (t TokenType) EmitterKind() string { return "token" }

0 commit comments

Comments
 (0)