Debian Codegraph @

internal/frontend/token/token.go

raw symbols SCIP
1 package tokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/
2
3 import (
4 "bytesscip-go gomod github.com/golang/go/src go1.24 bytes/"
5 "fmtscip-go gomod github.com/golang/go/src go1.24 fmt/"
6 "osscip-go gomod github.com/golang/go/src go1.24 os/"
7 "regexpscip-go gomod github.com/golang/go/src go1.24 regexp/"
8 "strconvscip-go gomod github.com/golang/go/src go1.24 strconv/"
9 "stringsscip-go gomod github.com/golang/go/src go1.24 strings/"
10 )
11
12 type Tokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Token# struct {
13 Typescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Token#Type. Typescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Type#
14 Litscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Token#Lit. []byte
15 }
16
17 func NewTokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/NewToken().(typ Typescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Type#, lit []byte) *Tokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Token# {
18 return &Tokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Token#{typ, lit}
19 }
20
21 func (this *Tokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Token#) Equalsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Token#Equals().(that *Tokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Token#) bool {
22 if this == nil || that == nil {
23 return this == that
24 }
25
26 if this.Typescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Token#Type. != that.Typescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Token#Type. {
27 return false
28 }
29
30 return bytesscip-go gomod github.com/golang/go/src go1.24 bytes/.Equalscip-go gomod github.com/golang/go/src go1.24 bytes/Equal().(this.Litscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Token#Lit., that.Litscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Token#Lit.)
31 }
32
33 func (this *Tokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Token#) Stringscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Token#String().() string {
34 str := ""
35 if this.Typescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Token#Type. == EOFscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/EOF. {
36 str += "\"␚\""
37 } else {
38 str += "\"" + string(this.Litscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Token#Lit.) + "\""
39 }
40 str += "(" + strconvscip-go gomod github.com/golang/go/src go1.24 strconv/.Itoascip-go gomod github.com/golang/go/src go1.24 strconv/Itoa().(int(this.Typescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Token#Type.)) + ")"
41 return str
42 }
43
44 type Typescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Type# int
45
46 const (
47 ILLEGALscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/ILLEGAL. Typescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Type# = iota - 1
48 EOFscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/EOF.
49 )
50
51 func (T Typescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Type#) Stringscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Type#String().() string {
52 return strconvscip-go gomod github.com/golang/go/src go1.24 strconv/.Itoascip-go gomod github.com/golang/go/src go1.24 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 Positionscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Position# struct {
59 Offsetscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Position#Offset. int // offset, starting at 0
60 Linescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Position#Line. int // line number, starting at 1
61 Columnscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Position#Column. int // column number, starting at 1 (character count)
62 }
63
64 // IsValid returns true if the position is valid.
65 func (pos *Positionscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Position#) IsValidscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Position#IsValid().() bool { return pos.Linescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Position#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 Positionscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Position#) Stringscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Position#String().() string {
74 s := ""
75 if pos.IsValidscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Position#IsValid().() {
76 s += fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24 fmt/Sprintf().("%d:%d", pos.Linescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Position#Line., pos.Columnscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Position#Column.)
77 }
78 if s == "" {
79 s = "-"
80 }
81 return s
82 }
83
84 func (T *Tokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Token#) IntValuescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Token#IntValue().() (int64, error) {
85 return strconvscip-go gomod github.com/golang/go/src go1.24 strconv/.ParseIntscip-go gomod github.com/golang/go/src go1.24 strconv/ParseInt().(string(T.Litscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Token#Lit.), 10, 64)
86 }
87
88 func (T *Tokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Token#) UintValuescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Token#UintValue().() (uint64, error) {
89 return strconvscip-go gomod github.com/golang/go/src go1.24 strconv/.ParseUintscip-go gomod github.com/golang/go/src go1.24 strconv/ParseUint().(string(T.Litscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Token#Lit.), 10, 64)
90 }
91
92 var sdtRexscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/sdtRex. = regexpscip-go gomod github.com/golang/go/src go1.24 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.24 regexp/MustCompile().(`\$(?:[0-9]+|T[0-9]+|Context)`)
93
94 func (T *Tokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Token#) SDTValscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Token#SDTVal().() string {
95 res := sdtRexscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/sdtRex..ReplaceAllStringFuncscip-go gomod github.com/golang/go/src go1.24 regexp/Regexp#ReplaceAllStringFunc().(string(T.Litscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Token#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 stringsscip-go gomod github.com/golang/go/src go1.24 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.24 strings/TrimSpace().(res[2 : len(res)-2])
108 }
109
110 // Tokenmap
111
112 type TokenMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap# struct {
113 tokenMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap#tokenMap. []string
114 stringMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap#stringMap. map[string]Typescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Type#
115 }
116
117 func NewMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/NewMap().() *TokenMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap# {
118 tm := &TokenMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap#{make([]string, 0, 10), make(map[string]Typescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Type#)}
119 tm.AddTokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap#AddToken().("␚")
120 // tm.AddToken("ε")
121 return tm
122 }
123
124 func (this *TokenMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap#) AddTokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap#AddToken().(str string) {
125 if _, exists := this.stringMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap#stringMap.[str]; exists {
126 return
127 }
128 this.stringMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap#stringMap.[str] = Typescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Type#(len(this.tokenMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap#tokenMap.))
129 this.tokenMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap#tokenMap. = append(this.tokenMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap#tokenMap., str)
130 }
131
132 func NewMapFromFilescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/NewMapFromFile().(file string) (*TokenMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap#, error) {
133 src, err := osscip-go gomod github.com/golang/go/src go1.24 os/.ReadFilescip-go gomod github.com/golang/go/src go1.24 os/ReadFile().(file)
134 if err != nil {
135 return nil, err
136 }
137 return NewMapFromStringscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/NewMapFromString().(string(src)), nil
138 }
139
140 func NewMapFromStringsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/NewMapFromStrings().(input []string) *TokenMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap# {
141 tm := NewMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/NewMap().()
142 for _, s := range input {
143 tm.AddTokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap#AddToken().(s)
144 }
145 return tm
146 }
147
148 func NewMapFromStringscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/NewMapFromString().(input string) *TokenMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap# {
149 tokens := stringsscip-go gomod github.com/golang/go/src go1.24 strings/.Fieldsscip-go gomod github.com/golang/go/src go1.24 strings/Fields().(input)
150 return NewMapFromStringsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/NewMapFromStrings().(tokens)
151 }
152
153 func (this *TokenMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap#) Lenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap#Len().() int {
154 return len(this.tokenMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap#tokenMap.)
155 }
156
157 func (this *TokenMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap#) Typescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap#Type().(key string) Typescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Type# {
158 tok, ok := this.stringMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap#stringMap.[key]
159 if !ok {
160 return ILLEGALscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/ILLEGAL.
161 }
162 return tok
163 }
164
165 func (this *TokenMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap#) TokenStringscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap#TokenString().(typ Typescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Type#) string {
166 tok := int(typ)
167 if tok < 0 || tok >= len(this.tokenMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap#tokenMap.) {
168 return "illegal " + strconvscip-go gomod github.com/golang/go/src go1.24 strconv/.Itoascip-go gomod github.com/golang/go/src go1.24 strconv/Itoa().(tok)
169 }
170 return this.tokenMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap#tokenMap.[tok]
171 }
172
173 func (this *TokenMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap#) Stringscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap#String().() string {
174 res := ""
175 for str, tok := range this.stringMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap#stringMap. {
176 res += str + " : " + strconvscip-go gomod github.com/golang/go/src go1.24 strconv/.Itoascip-go gomod github.com/golang/go/src go1.24 strconv/Itoa().(int(tok)) + "\n"
177 }
178 return res
179 }
180
181 func (this *TokenMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap#) Stringsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap#Strings().() []string {
182 return this.tokenMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap#tokenMap.[1:]
183 }
184
185 func (this *TokenMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap#) Equalsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap#Equals().(that *TokenMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap#) bool {
186 if this == nil || that == nil {
187 return false
188 }
189
190 if len(this.stringMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap#stringMap.) != len(that.stringMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap#stringMap.) ||
191 len(this.tokenMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap#tokenMap.) != len(that.tokenMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap#tokenMap.) {
192 return false
193 }
194
195 for str, tok := range this.stringMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap#stringMap. {
196 if tok1, ok := that.stringMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap#stringMap.[str]; !ok || tok1 != tok {
197 return false
198 }
199 }
200
201 return true
202 }
203
204 func (this *TokenMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap#) Tokensscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap#Tokens().() []*Tokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Token# {
205 res := make([]*Tokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Token#, 0, len(this.stringMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap#stringMap.))
206 for typ, str := range this.tokenMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap#tokenMap. {
207 res = append(res, &Tokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Token#{Typescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Type#(typ), []byte(str)})
208 }
209 return res
210 }
211
212 func (this *TokenMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap#) WriteFilescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap#WriteFile().(file string) error {
213 out := ""
214 for i := 1; i < len(this.tokenMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap#tokenMap.); i++ {
215 out += this.TokenStringscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/TokenMap#TokenString().(Typescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/frontend/token`/Type#(i)) + "\n"
216 }
217 return osscip-go gomod github.com/golang/go/src go1.24 os/.WriteFilescip-go gomod github.com/golang/go/src go1.24 os/WriteFile().(file, []byte(out), 0644)
218 }