Debian Codegraph @

token.go

raw symbols SCIP
1 // Copyright (c) 2017 Ernest Micklei
2 //
3 // MIT License
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining
6 // a copy of this software and associated documentation files (the
7 // "Software"), to deal in the Software without restriction, including
8 // without limitation the rights to use, copy, modify, merge, publish,
9 // distribute, sublicense, and/or sell copies of the Software, and to
10 // permit persons to whom the Software is furnished to do so, subject to
11 // the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
24 package proto
25
26 import (
27 "strconv"
28 "strings"
29 )
30
31 // token represents a lexical token.
32 type token int
33
34 const (
35 // Special tokens
36 tILLEGAL token = iota
37 tEOF
38 tWS
39
40 // Literals
41 tIDENT
42
43 // Misc characters
44 tSEMICOLON // ;
45 tCOLON // :
46 tEQUALS // =
47 tQUOTE // "
48 tSINGLEQUOTE // '
49 tLEFTPAREN // (
50 tRIGHTPAREN // )
51 tLEFTCURLY // {
52 tRIGHTCURLY // }
53 tLEFTSQUARE // [
54 tRIGHTSQUARE // ]
55 tCOMMENT // /
56 tLESS // <
57 tGREATER // >
58 tCOMMA // ,
59 tDOT // .
60
61 // Keywords
62 keywordsStart
63 tEDITION
64 tSYNTAX
65 tSERVICE
66 tRPC
67 tRETURNS
68 tMESSAGE
69 tIMPORT
70 tPACKAGE
71 tOPTION
72 tREPEATED
73 tWEAK
74 tPUBLIC
75
76 // special fields
77 tONEOF
78 tMAP
79 tRESERVED
80 tENUM
81 tSTREAM
82
83 // numbers (pos or neg, float)
84 tNUMBER
85
86 // BEGIN proto2
87 tOPTIONAL
88 tGROUP
89 tEXTENSIONS
90 tEXTEND
91 tREQUIRED
92 // END proto2
93 keywordsEnd
94 )
95
96 // typeTokens exists for future validation
97 const typeTokens = "double float int32 int64 uint32 uint64 sint32 sint64 fixed32 sfixed32 sfixed64 bool string bytes"
98
99 // isKeyword returns if tok is in the keywords range
100 func isKeyword(tok token) bool {
101 return keywordsStart < tok && tok < keywordsEnd
102 }
103
104 // isWhitespace checks for space,tab and newline
105 func isWhitespace(r rune) bool {
106 return r == ' ' || r == '\t' || r == '\n'
107 }
108
109 // isDigit returns true if the rune is a digit.
110 func isDigit(ch rune) bool { return (ch >= '0' && ch <= '9') }
111
112 // isString checks if the literal is quoted (single or double).
113 func isString(lit string) bool {
114 if lit == "'" {
115 return false
116 }
117 return (strings.HasPrefix(lit, "\"") &&
118 strings.HasSuffix(lit, "\"")) ||
119 (strings.HasPrefix(lit, "'") &&
120 strings.HasSuffix(lit, "'"))
121 }
122
123 func isComment(lit string) bool {
124 return strings.HasPrefix(lit, "//") || strings.HasPrefix(lit, "/*")
125 }
126
127 func isNumber(lit string) bool {
128 if lit == "NaN" || lit == "nan" || lit == "Inf" || lit == "Infinity" || lit == "inf" || lit == "infinity" {
129 return false
130 }
131 if strings.HasPrefix(lit, "0x") || strings.HasPrefix(lit, "0X") {
132 _, err := strconv.ParseInt(lit, 0, 64)
133 return err == nil
134 }
135 _, err := strconv.ParseFloat(lit, 64)
136 return err == nil
137 }
138
139 const doubleQuoteRune = rune('"')
140
141 // unQuote removes one matching leading and trailing single or double quote.
142 //
143 // https://github.com/emicklei/proto/issues/103
144 // cannot use strconv.Unquote as this unescapes quotes.
145 func unQuote(lit string) (string, rune) {
146 if len(lit) < 2 {
147 return lit, doubleQuoteRune
148 }
149 chars := []rune(lit)
150 first, last := chars[0], chars[len(chars)-1]
151 if first != last {
152 return lit, doubleQuoteRune
153 }
154 if s := string(chars[0]); s == "\"" || s == stringWithSingleQuote {
155 return string(chars[1 : len(chars)-1]), chars[0]
156 }
157 return lit, doubleQuoteRune
158 }
159
160 func asToken(literal string) token {
161 switch literal {
162 // delimiters
163 case ";":
164 return tSEMICOLON
165 case ":":
166 return tCOLON
167 case "=":
168 return tEQUALS
169 case "\"":
170 return tQUOTE
171 case "'":
172 return tSINGLEQUOTE
173 case "(":
174 return tLEFTPAREN
175 case ")":
176 return tRIGHTPAREN
177 case "{":
178 return tLEFTCURLY
179 case "}":
180 return tRIGHTCURLY
181 case "[":
182 return tLEFTSQUARE
183 case "]":
184 return tRIGHTSQUARE
185 case "<":
186 return tLESS
187 case ">":
188 return tGREATER
189 case ",":
190 return tCOMMA
191 case ".":
192 return tDOT
193 // words
194 case "syntax":
195 return tSYNTAX
196 case "edition":
197 return tEDITION
198 case "service":
199 return tSERVICE
200 case "rpc":
201 return tRPC
202 case "returns":
203 return tRETURNS
204 case "option":
205 return tOPTION
206 case "message":
207 return tMESSAGE
208 case "import":
209 return tIMPORT
210 case "package":
211 return tPACKAGE
212 case "oneof":
213 return tONEOF
214 // special fields
215 case "map":
216 return tMAP
217 case "reserved":
218 return tRESERVED
219 case "enum":
220 return tENUM
221 case "repeated":
222 return tREPEATED
223 case "weak":
224 return tWEAK
225 case "public":
226 return tPUBLIC
227 case "stream":
228 return tSTREAM
229 // proto2
230 case "optional":
231 return tOPTIONAL
232 case "group":
233 return tGROUP
234 case "extensions":
235 return tEXTENSIONS
236 case "extend":
237 return tEXTEND
238 case "required":
239 return tREQUIRED
240 default:
241 // special cases
242 if isNumber(literal) {
243 return tNUMBER
244 }
245 if isComment(literal) {
246 return tCOMMENT
247 }
248 return tIDENT
249 }
250 }