...
1 package chroma
2
3 import (
4 "fmt"
5 "strings"
6 )
7
8 var (
9 defaultOptions = &TokeniseOptions{
10 State: "root",
11 EnsureLF: true,
12 }
13 )
14
15
16 type Config struct {
17
18 Name string `xml:"name,omitempty"`
19
20
21 Aliases []string `xml:"alias,omitempty"`
22
23
24 Filenames []string `xml:"filename,omitempty"`
25
26
27 AliasFilenames []string `xml:"alias_filename,omitempty"`
28
29
30 MimeTypes []string `xml:"mime_type,omitempty"`
31
32
33 CaseInsensitive bool `xml:"case_insensitive,omitempty"`
34
35
36 DotAll bool `xml:"dot_all,omitempty"`
37
38
39
40
41 NotMultiline bool `xml:"not_multiline,omitempty"`
42
43
44
45
46
47
48
49
50
51 EnsureNL bool `xml:"ensure_nl,omitempty"`
52
53
54
55
56
57
58
59 Priority float32 `xml:"priority,omitempty"`
60 }
61
62
63 type Token struct {
64 Type TokenType `json:"type"`
65 Value string `json:"value"`
66 }
67
68 func (t *Token) String() string { return t.Value }
69 func (t *Token) GoString() string { return fmt.Sprintf("&Token{%s, %q}", t.Type, t.Value) }
70
71
72 func (t *Token) Clone() Token {
73 return *t
74 }
75
76
77 var EOF Token
78
79
80 type TokeniseOptions struct {
81
82 State string
83
84 Nested bool
85
86
87
88 EnsureLF bool
89 }
90
91
92 type Lexer interface {
93
94 Config() *Config
95
96 Tokenise(options *TokeniseOptions, text string) (Iterator, error)
97
98
99
100
101 SetRegistry(registry *LexerRegistry) Lexer
102
103
104
105
106
107 SetAnalyser(analyser func(text string) float32) Lexer
108
109
110 AnalyseText(text string) float32
111 }
112
113
114 type Lexers []Lexer
115
116 func (l Lexers) Len() int { return len(l) }
117 func (l Lexers) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
118 func (l Lexers) Less(i, j int) bool {
119 return strings.ToLower(l[i].Config().Name) < strings.ToLower(l[j].Config().Name)
120 }
121
122
123 type PrioritisedLexers []Lexer
124
125 func (l PrioritisedLexers) Len() int { return len(l) }
126 func (l PrioritisedLexers) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
127 func (l PrioritisedLexers) Less(i, j int) bool {
128 ip := l[i].Config().Priority
129 if ip == 0 {
130 ip = 1
131 }
132 jp := l[j].Config().Priority
133 if jp == 0 {
134 jp = 1
135 }
136 return ip > jp
137 }
138
139
140 type Analyser interface {
141 AnalyseText(text string) float32
142 }
143
View as plain text