OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 // |
| 5 // TokenKinds is a type which describes the kinds of tokens which can be |
| 6 // encountered in a mojom file. |
| 7 |
| 8 package lexer |
| 9 |
| 10 import ( |
| 11 "fmt" |
| 12 ) |
| 13 |
| 14 type TokenKind int |
| 15 |
| 16 // TokenKinds |
| 17 const ( |
| 18 // An error of an unknown nature has occured. |
| 19 ErrorUnknown TokenKind = iota |
| 20 // A character was found which is not part of a valid token. |
| 21 ErrorIllegalChar |
| 22 // A quoted string was opened but not closed. |
| 23 ErrorUnterminatedStringLiteral |
| 24 // A multiline comment was opened but not closed. |
| 25 ErrorUnterminatedComment |
| 26 // Indicates the end of a stream of tokens. |
| 27 EOF |
| 28 |
| 29 // Punctuators and Separators |
| 30 LParen |
| 31 RParen |
| 32 LBracket |
| 33 RBracket |
| 34 LBrace |
| 35 RBrace |
| 36 LAngle |
| 37 RAngle |
| 38 Semi |
| 39 Comma |
| 40 Dot |
| 41 Minus |
| 42 Plus |
| 43 Amp |
| 44 Qstn |
| 45 Equals |
| 46 Response |
| 47 |
| 48 // Names |
| 49 Name |
| 50 |
| 51 // Keywords |
| 52 Import |
| 53 Module |
| 54 Struct |
| 55 Union |
| 56 Interface |
| 57 Enum |
| 58 Const |
| 59 True |
| 60 False |
| 61 Default |
| 62 |
| 63 // Constants |
| 64 IntConstDec |
| 65 IntConstHex |
| 66 FloatConst |
| 67 Ordinal |
| 68 StringLiteral |
| 69 ) |
| 70 |
| 71 // This method is used to generate user-facing strings in compilation error |
| 72 // messages. For example for LBrace we produce the string "'{'". Notice the |
| 73 // single-quotes. This will be used for example in an error message that looks |
| 74 // like the following: |
| 75 // Unexpected token at line 5, column 6: '###'. Expecting '{'. |
| 76 func (tokenKind TokenKind) String() string { |
| 77 switch tokenKind { |
| 78 // Errors |
| 79 case ErrorUnknown: |
| 80 return "unknown token" |
| 81 case ErrorIllegalChar: |
| 82 return "illegal token" |
| 83 case ErrorUnterminatedStringLiteral: |
| 84 return "unterminated string literal" |
| 85 case ErrorUnterminatedComment: |
| 86 return "unterminated comment" |
| 87 |
| 88 // End of file |
| 89 case EOF: |
| 90 return "eof" |
| 91 |
| 92 // Punctuators and Separators |
| 93 case LParen: |
| 94 return "'('" |
| 95 case RParen: |
| 96 return "')'" |
| 97 case LBracket: |
| 98 return "'['" |
| 99 case RBracket: |
| 100 return "']'" |
| 101 case LBrace: |
| 102 return "'{'" |
| 103 case RBrace: |
| 104 return "'}'" |
| 105 case LAngle: |
| 106 return "'<'" |
| 107 case RAngle: |
| 108 return "'>'" |
| 109 case Semi: |
| 110 return "';'" |
| 111 case Comma: |
| 112 return "','" |
| 113 case Dot: |
| 114 return "'.'" |
| 115 case Minus: |
| 116 return "'-'" |
| 117 case Plus: |
| 118 return "'+'" |
| 119 case Amp: |
| 120 return "'&'" |
| 121 case Qstn: |
| 122 return "'?'" |
| 123 case Equals: |
| 124 return "'='" |
| 125 case Response: |
| 126 return "'=>'" |
| 127 |
| 128 // Names |
| 129 case Name: |
| 130 return "a name" |
| 131 |
| 132 // Keywords |
| 133 case Import: |
| 134 return "'import'" |
| 135 case Module: |
| 136 return "'module'" |
| 137 case Struct: |
| 138 return "'struct'" |
| 139 case Union: |
| 140 return "'union'" |
| 141 case Interface: |
| 142 return "'interface'" |
| 143 case Enum: |
| 144 return "'enum'" |
| 145 case Const: |
| 146 return "'const'" |
| 147 case True: |
| 148 return "'true'" |
| 149 case False: |
| 150 return "'false'" |
| 151 case Default: |
| 152 return "'default'" |
| 153 |
| 154 // Constants |
| 155 case IntConstDec: |
| 156 return "decimal integer literal" |
| 157 case IntConstHex: |
| 158 return "hex integer literal" |
| 159 case FloatConst: |
| 160 return "float literal" |
| 161 case Ordinal: |
| 162 return "an ordinal" |
| 163 case StringLiteral: |
| 164 return "a string literal" |
| 165 |
| 166 default: |
| 167 // Note(rudominer) It is important to use %d below so as to avoi
d |
| 168 // re-invoking this method and causing an infinite recursion. |
| 169 return fmt.Sprintf("%d", tokenKind) |
| 170 } |
| 171 } |
| 172 |
| 173 type Token struct { |
| 174 Kind TokenKind |
| 175 Text string |
| 176 // CharPos is the number of runes preceeding the token. |
| 177 CharPos int |
| 178 // LineNo is the line on which the token is found. (First line is 0.) |
| 179 LineNo int |
| 180 // LinePost is the number of runes preceeding the token on its line. |
| 181 LinePos int |
| 182 } |
| 183 |
| 184 // ShortLocationString is used to generate user-facing strings in compilation |
| 185 // error messages. This will be used for example in an error message that looks |
| 186 // like the following: |
| 187 // Unexpected token at line 5, column 6: '###'. Expecting '{'. |
| 188 func (t Token) ShortLocationString() string { |
| 189 return fmt.Sprintf("%d,%d", t.LineNo+1, t.LinePos+1) |
| 190 } |
| 191 |
| 192 func (t Token) LongLocationString() string { |
| 193 return fmt.Sprintf("line %d, column %d", t.LineNo+1, t.LinePos+1) |
| 194 } |
| 195 |
| 196 // EOF returns true if the token on which it is called represents the end of the |
| 197 // token string. |
| 198 func (t Token) EOF() bool { |
| 199 return t.Kind == EOF |
| 200 } |
| 201 |
| 202 // String is used to generate user-facing strings in compilation error |
| 203 // messages. For many token kinds the TokenKind.String() method will produce |
| 204 // good results for representing the token. But for other TokenKinds we will |
| 205 // want to include some information besides a representation of the kind. |
| 206 // For example for an ErrorUnknown kind we wnat to show the text. |
| 207 // This will be used for example in an error message that looks |
| 208 // like the following: |
| 209 // Unexpected token at line 5, column 6: '###'. Expecting '{'. |
| 210 func (token Token) String() string { |
| 211 switch token.Kind { |
| 212 case ErrorUnknown, Name, StringLiteral, IntConstDec, IntConstHex, FloatC
onst, Ordinal: |
| 213 return fmt.Sprintf("'%s'", token.Text) |
| 214 |
| 215 default: |
| 216 return token.Kind.String() |
| 217 } |
| 218 } |
OLD | NEW |