Chromium Code Reviews| 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 ERROR_UNKNOWN TokenKind = iota | |
|
mattr
2015/10/12 18:04:13
The convention in go is to use camel case even for
azani
2015/10/13 00:23:46
Done.
| |
| 20 // A character was found which is not part of a valid token. | |
| 21 ERROR_ILLEGAL_CHAR | |
| 22 // A quoted string was opened but not closed. | |
| 23 ERROR_UNTERMINATED_STRING_LITERAL | |
| 24 // A multiline comment was opened but not closed. | |
| 25 ERROR_UNTERMINATED_COMMENT | |
| 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 INT_CONST_DEC | |
| 65 INT_CONST_HEX | |
| 66 FLOAT_CONST | |
| 67 ORDINAL | |
| 68 STRING_LITERAL | |
| 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 ERROR_UNKNOWN: | |
| 80 return "unknown token" | |
| 81 case ERROR_ILLEGAL_CHAR: | |
| 82 return "illegal token" | |
| 83 case ERROR_UNTERMINATED_STRING_LITERAL: | |
| 84 return "unterminated string literal" | |
| 85 case ERROR_UNTERMINATED_COMMENT: | |
| 86 return "unterminated comment" | |
| 87 | |
| 88 // Punctuators and Separators | |
| 89 case LPAREN: | |
| 90 return "'('" | |
| 91 case RPAREN: | |
| 92 return "')'" | |
| 93 case LBRACKET: | |
| 94 return "'['" | |
| 95 case RBRACKET: | |
| 96 return "']'" | |
| 97 case LBRACE: | |
| 98 return "'{'" | |
| 99 case RBRACE: | |
| 100 return "'}'" | |
| 101 case LANGLE: | |
| 102 return "'<'" | |
| 103 case RANGLE: | |
| 104 return "'>'" | |
| 105 case SEMI: | |
| 106 return "';'" | |
| 107 case COMMA: | |
| 108 return "','" | |
| 109 case DOT: | |
| 110 return "'.'" | |
| 111 case MINUS: | |
| 112 return "'-'" | |
| 113 case PLUS: | |
| 114 return "'+'" | |
| 115 case AMP: | |
| 116 return "'&'" | |
| 117 case QSTN: | |
| 118 return "'?'" | |
| 119 case EQUALS: | |
| 120 return "'='" | |
| 121 case RESPONSE: | |
| 122 return "'=>'" | |
| 123 | |
| 124 // Names | |
| 125 case NAME: | |
| 126 return "a name" | |
| 127 | |
| 128 // Keywords | |
| 129 case IMPORT: | |
| 130 return "'import'" | |
| 131 case MODULE: | |
| 132 return "'module'" | |
| 133 case STRUCT: | |
| 134 return "'struct'" | |
| 135 case UNION: | |
| 136 return "'union'" | |
| 137 case INTERFACE: | |
| 138 return "'interface'" | |
| 139 case ENUM: | |
| 140 return "'enum'" | |
| 141 case CONST: | |
| 142 return "'const'" | |
| 143 case TRUE: | |
| 144 return "'true'" | |
| 145 case FALSE: | |
| 146 return "'false'" | |
| 147 case DEFAULT: | |
| 148 return "'default'" | |
| 149 | |
| 150 // Constants | |
| 151 case INT_CONST_DEC: | |
| 152 return "decimal integer literal" | |
| 153 case INT_CONST_HEX: | |
| 154 return "hex integer literal" | |
| 155 case FLOAT_CONST: | |
| 156 return "float literal" | |
| 157 case ORDINAL: | |
| 158 return "an ordinal" | |
| 159 case STRING_LITERAL: | |
| 160 return "a string literal" | |
| 161 | |
| 162 default: | |
| 163 // Note(rudominer) It is important to use %d below so as to avoi d | |
| 164 // re-invoking this method and causing an infinite recursion. | |
| 165 return fmt.Sprintf("%d", tokenKind) | |
| 166 } | |
| 167 } | |
| 168 | |
| 169 type Token struct { | |
| 170 Kind TokenKind | |
| 171 Text string | |
| 172 CharPos int // Position in the source string. | |
| 173 LineNo int // First line is 0. | |
| 174 LinePos int // Position in the line. First char in line is 0. | |
| 175 } | |
| 176 | |
| 177 // This method is used to generate user-facing strings in compilation error | |
|
mattr
2015/10/12 18:04:13
The usual Go convention is to start all comments w
azani
2015/10/13 00:23:46
Done.
| |
| 178 // messages. This will be used for example in an error message that looks | |
| 179 // like the following: | |
| 180 // Unexpected token at line 5, column 6: '###'. Expecting '{'. | |
| 181 func (t Token) ShortLocationString() string { | |
| 182 return fmt.Sprintf("%d,%d", t.LineNo+1, t.LinePos+1) | |
| 183 } | |
| 184 | |
| 185 func (t Token) LongLocationString() string { | |
| 186 return fmt.Sprintf("line %d, column %d", t.LineNo+1, t.LinePos+1) | |
| 187 } | |
| 188 | |
| 189 // Is this the EOF token that represent the end of the token stream? | |
| 190 func (t Token) EOF() bool { | |
| 191 return t.Kind == EOF | |
| 192 } | |
| 193 | |
| 194 // This method is used to generate user-facing strings in compilation error | |
| 195 // messages. For many token kinds the TokenKind.String() method will produce | |
| 196 // good results for representing the token. But for other TokenKinds we will | |
| 197 // want to include some information besides a representation of the kind. | |
| 198 // For example for an ERROR_UNKNOWN kind we wnat to show the text. | |
| 199 // This will be used for example in an error message that looks | |
| 200 // like the following: | |
| 201 // Unexpected token at line 5, column 6: '###'. Expecting '{'. | |
| 202 func (token Token) String() string { | |
| 203 switch token.Kind { | |
| 204 case ERROR_UNKNOWN, NAME, STRING_LITERAL, INT_CONST_DEC, INT_CONST_HEX, FLOAT_CONST, ORDINAL: | |
| 205 return fmt.Sprintf("'%s'", token.Text) | |
| 206 | |
| 207 default: | |
| 208 return token.Kind.String() | |
| 209 } | |
| 210 } | |
| OLD | NEW |