Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(311)

Side by Side Diff: mojom/mojom_parser/lexer/tokens.go

Issue 1506023005: New Mojom parser: Fix up error messages for lexing errors. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Adds new sha1. Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 // 4 //
5 // TokenKinds is a type which describes the kinds of tokens which can be 5 // TokenKinds is a type which describes the kinds of tokens which can be
6 // encountered in a mojom file. 6 // encountered in a mojom file.
7 7
8 package lexer 8 package lexer
9 9
10 import ( 10 import (
11 "bytes" 11 "bytes"
12 "fmt" 12 "fmt"
13 "strings" 13 "strings"
14 "unicode/utf8" 14 "unicode/utf8"
15 ) 15 )
16 16
17 type TokenKind int 17 type TokenKind int
18 18
19 // TokenKinds 19 // TokenKinds
20 const ( 20 const (
21 // An error of an unknown nature has occured.
22 ErrorUnknown TokenKind = iota
23 // A character was found which is not part of a valid token. 21 // A character was found which is not part of a valid token.
24 » ErrorIllegalChar 22 » ErrorIllegalChar TokenKind = iota
25 // A quoted string was opened but not closed. 23 // A quoted string was opened but not closed.
26 ErrorUnterminatedStringLiteral 24 ErrorUnterminatedStringLiteral
27 // A multiline comment was opened but not closed. 25 // A multiline comment was opened but not closed.
28 ErrorUnterminatedComment 26 ErrorUnterminatedComment
29 // Indicates the end of a stream of tokens. 27 // Indicates the end of a stream of tokens.
30 EOF 28 EOF
31 29
32 // Punctuators and Separators 30 // Punctuators and Separators
33 LParen 31 LParen
34 RParen 32 RParen
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 ) 70 )
73 71
74 // This method is used to generate user-facing strings in compilation error 72 // This method is used to generate user-facing strings in compilation error
75 // messages. For example for LBrace we produce the string "'{'". Notice the 73 // messages. For example for LBrace we produce the string "'{'". Notice the
76 // single-quotes. This will be used for example in an error message that looks 74 // single-quotes. This will be used for example in an error message that looks
77 // like the following: 75 // like the following:
78 // Unexpected token at line 5, column 6: '###'. Expecting '{'. 76 // Unexpected token at line 5, column 6: '###'. Expecting '{'.
79 func (tokenKind TokenKind) String() string { 77 func (tokenKind TokenKind) String() string {
80 switch tokenKind { 78 switch tokenKind {
81 // Errors 79 // Errors
82 case ErrorUnknown:
83 return "unknown token"
84 case ErrorIllegalChar: 80 case ErrorIllegalChar:
85 return "illegal token" 81 return "illegal token"
86 case ErrorUnterminatedStringLiteral: 82 case ErrorUnterminatedStringLiteral:
87 return "unterminated string literal" 83 return "unterminated string literal"
88 case ErrorUnterminatedComment: 84 case ErrorUnterminatedComment:
89 return "unterminated comment" 85 return "unterminated comment"
90 86
91 // End of file 87 // End of file
92 case EOF: 88 case EOF:
93 return "eof" 89 return "eof"
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 // EOF returns true if the token on which it is called represents the end of the 199 // EOF returns true if the token on which it is called represents the end of the
204 // token string. 200 // token string.
205 func (t Token) EOF() bool { 201 func (t Token) EOF() bool {
206 return t.Kind == EOF 202 return t.Kind == EOF
207 } 203 }
208 204
209 // String is used to generate user-facing strings in compilation error 205 // String is used to generate user-facing strings in compilation error
210 // messages. For many token kinds the TokenKind.String() method will produce 206 // messages. For many token kinds the TokenKind.String() method will produce
211 // good results for representing the token. But for other TokenKinds we will 207 // good results for representing the token. But for other TokenKinds we will
212 // want to include some information besides a representation of the kind. 208 // want to include some information besides a representation of the kind.
213 // For example for an ErrorUnknown kind we wnat to show the text. 209 // For example for an ErrorIllegalChar kind we wnat to show the text.
214 // This will be used for example in an error message that looks 210 // This will be used for example in an error message that looks
215 // like the following: 211 // like the following:
216 // Unexpected token at line 5, column 6: '###'. Expecting '{'. 212 // Unexpected token at line 5, column 6: '###'. Expecting '{'.
217 func (token Token) String() string { 213 func (token Token) String() string {
218 switch token.Kind { 214 switch token.Kind {
219 » case ErrorUnknown, Name, StringLiteral, IntConstDec, IntConstHex, FloatC onst, Ordinal: 215 » case StringLiteral:
220 » » return fmt.Sprintf("'%s'", token.Text) 216 » » return token.Text
217 » case Name, IntConstDec, IntConstHex, FloatConst, Ordinal, ErrorIllegalCh ar:
218 » » return fmt.Sprintf("%q", token.Text)
221 219
222 default: 220 default:
223 return token.Kind.String() 221 return token.Kind.String()
224 } 222 }
225 } 223 }
226 224
227 // Snippet is used to generate a user-facing string in compilation error 225 // Snippet is used to generate a user-facing string in compilation error
228 // messages. It displays the token's text as well as the surrounding line for 226 // messages. It displays the token's text as well as the surrounding line for
229 // context. It also includes a line with some carets to highlight the token. 227 // context. It also includes a line with some carets to highlight the token.
230 // source is the source code where token was found. 228 // source is the source code where token was found.
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
270 if rune == '\t' { 268 if rune == '\t' {
271 snippetBuffer.WriteRune('\t') 269 snippetBuffer.WriteRune('\t')
272 } else { 270 } else {
273 snippetBuffer.WriteRune(' ') 271 snippetBuffer.WriteRune(' ')
274 } 272 }
275 } 273 }
276 274
277 // We don't want too big of a caret line as that may be distracting. So we 275 // We don't want too big of a caret line as that may be distracting. So we
278 // limit it to 20 runes at most. 276 // limit it to 20 runes at most.
279 tokenSize := utf8.RuneCountInString(token.Text) 277 tokenSize := utf8.RuneCountInString(token.Text)
280 » if tokenSize > 20 { 278 » if token.Kind == ErrorUnterminatedComment {
279 » » // In the case of an unterminated comment, the token text will c ontain
280 » » // the entire rest of the source after the opening comment. We r eally
281 » » // only want to highlight the "/*" characters marking the beginn ing of
282 » » // the comment.
283 » » tokenSize = 2
284 » } else if tokenSize > 20 {
281 tokenSize = 20 285 tokenSize = 20
282 } 286 }
283 if color { 287 if color {
284 // Set the caret characters to green. 288 // Set the caret characters to green.
285 snippetBuffer.WriteString("\x1b[32;1m") 289 snippetBuffer.WriteString("\x1b[32;1m")
286 } 290 }
287 snippetBuffer.WriteString(strings.Repeat("^", tokenSize)) 291 snippetBuffer.WriteString(strings.Repeat("^", tokenSize))
288 if color { 292 if color {
289 // Reset all printing attributes. 293 // Reset all printing attributes.
290 snippetBuffer.WriteString("\x1b[0m") 294 snippetBuffer.WriteString("\x1b[0m")
291 } 295 }
292 snippet = snippetBuffer.String() 296 snippet = snippetBuffer.String()
293 return 297 return
294 } 298 }
OLDNEW
« no previous file with comments | « mojo/public/tools/bindings/mojom_parser/bin/linux64/mojom_parser.sha1 ('k') | mojom/mojom_parser/parser/parser_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698