Debian Codegraph @

internal/frontend/token/token.go

raw symbols SCIP
1 package token
2
3 import (
4 "bytes"
5 "fmt"
6 "os"
7 "regexp"
8 "strconv"
9 "strings"
10 )
11
12 type Token struct {
13 Type Type
14 Lit []byte
15 }
16
17 func NewToken(typ Type, lit []byte) *Token {
18 return &Token{typ, lit}
19 }
20
21 func (this *Token) Equals(that *Token) bool {
22 if this == nil || that == nil {
23 return this == that
24 }
25
26 if this.Type != that.Type {
27 return false
28 }
29
30 return bytes.Equal(this.Lit, that.Lit)
31 }
32
33 func (this *Token) String() string {
34 str := ""
35 if this.Type == EOF {
36 str += "\"␚\""
37 } else {
38 str += "\"" + string(this.Lit) + "\""
39 }
40 str += "(" + strconv.Itoa(int(this.Type)) + ")"
41 return str
42 }
43
44 type Type int
45
46 const (
47 ILLEGAL Type = iota - 1
48 EOF
49 )
50
51 func (T Type) String() string {
52 return strconv.Itoa(int(T))
53 }
54
55 // Position describes an arbitrary source position
56 // including the file, line, and column location.
57 // A Position is valid if the line number is > 0.
58 type Position struct {
59 Offset int // offset, starting at 0
60 Line int // line number, starting at 1
61 Column int // column number, starting at 1 (character count)
62 }
63
64 // IsValid returns true if the position is valid.
65 func (pos *Position) IsValid() bool { return pos.Line > 0 }
66
67 // String returns a string in one of several forms:
68 //
69 // file:line:column valid position with file name
70 // line:column valid position without file name
71 // file invalid position with file name
72 // - invalid position without file name
73 func (pos Position) String() string {
74 s := ""
75 if pos.IsValid() {
76 s += fmt.Sprintf("%d:%d", pos.Line, pos.Column)
77 }
78 if s == "" {
79 s = "-"
80 }
81 return s
82 }
83
84 func (T *Token) IntValue() (int64, error) {
85 return strconv.ParseInt(string(T.Lit), 10, 64)
86 }
87
88 func (T *Token) UintValue() (uint64, error) {
89 return strconv.ParseUint(string(T.Lit), 10, 64)
90 }
91
92 var sdtRex = regexp.MustCompile(`\$(?:[0-9]+|T[0-9]+|Context)`)
93
94 func (T *Token) SDTVal() string {
95 res := sdtRex.ReplaceAllStringFunc(string(T.Lit), func(match string) string {
96 switch match[1] {
97 case 'T': // user wants this as a token.
98 return "X[" + match[2:] + "].(*token.Token)"
99
100 case 'C': // user wants context.
101 return "C"
102
103 default: // just pass it as an attrib.
104 return "X[" + match[1:] + "]"
105 }
106 })
107 return strings.TrimSpace(res[2 : len(res)-2])
108 }
109
110 // Tokenmap
111
112 type TokenMap struct {
113 tokenMap []string
114 stringMap map[string]Type
115 }
116
117 func NewMap() *TokenMap {
118 tm := &TokenMap{make([]string, 0, 10), make(map[string]Type)}
119 tm.AddToken("␚")
120 // tm.AddToken("ε")
121 return tm
122 }
123
124 func (this *TokenMap) AddToken(str string) {
125 if _, exists := this.stringMap[str]; exists {
126 return
127 }
128 this.stringMap[str] = Type(len(this.tokenMap))
129 this.tokenMap = append(this.tokenMap, str)
130 }
131
132 func NewMapFromFile(file string) (*TokenMap, error) {
133 src, err := os.ReadFile(file)
134 if err != nil {
135 return nil, err
136 }
137 return NewMapFromString(string(src)), nil
138 }
139
140 func NewMapFromStrings(input []string) *TokenMap {
141 tm := NewMap()
142 for _, s := range input {
143 tm.AddToken(s)
144 }
145 return tm
146 }
147
148 func NewMapFromString(input string) *TokenMap {
149 tokens := strings.Fields(input)
150 return NewMapFromStrings(tokens)
151 }
152
153 func (this *TokenMap) Len() int {
154 return len(this.tokenMap)
155 }
156
157 func (this *TokenMap) Type(key string) Type {
158 tok, ok := this.stringMap[key]
159 if !ok {
160 return ILLEGAL
161 }
162 return tok
163 }
164
165 func (this *TokenMap) TokenString(typ Type) string {
166 tok := int(typ)
167 if tok < 0 || tok >= len(this.tokenMap) {
168 return "illegal " + strconv.Itoa(tok)
169 }
170 return this.tokenMap[tok]
171 }
172
173 func (this *TokenMap) String() string {
174 res := ""
175 for str, tok := range this.stringMap {
176 res += str + " : " + strconv.Itoa(int(tok)) + "\n"
177 }
178 return res
179 }
180
181 func (this *TokenMap) Strings() []string {
182 return this.tokenMap[1:]
183 }
184
185 func (this *TokenMap) Equals(that *TokenMap) bool {
186 if this == nil || that == nil {
187 return false
188 }
189
190 if len(this.stringMap) != len(that.stringMap) ||
191 len(this.tokenMap) != len(that.tokenMap) {
192 return false
193 }
194
195 for str, tok := range this.stringMap {
196 if tok1, ok := that.stringMap[str]; !ok || tok1 != tok {
197 return false
198 }
199 }
200
201 return true
202 }
203
204 func (this *TokenMap) Tokens() []*Token {
205 res := make([]*Token, 0, len(this.stringMap))
206 for typ, str := range this.tokenMap {
207 res = append(res, &Token{Type(typ), []byte(str)})
208 }
209 return res
210 }
211
212 func (this *TokenMap) WriteFile(file string) error {
213 out := ""
214 for i := 1; i < len(this.tokenMap); i++ {
215 out += this.TokenString(Type(i)) + "\n"
216 }
217 return os.WriteFile(file, []byte(out), 0644)
218 }