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

Side by Side Diff: pkg/front_end/lib/src/fasta/scanner/error_token.dart

Issue 2965393002: Use FastaMessage instead of String. Part 1. (Closed)
Patch Set: Add type variable to Code. Created 3 years, 5 months 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 (c) 2017, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style licenset hat can be found in the LICENSE file. 3 // BSD-style licenset hat can be found in the LICENSE file.
4 4
5 library dart_scanner.error_token; 5 library dart_scanner.error_token;
6 6
7 import '../../scanner/token.dart' show BeginToken, TokenType, TokenWithComment; 7 import '../../scanner/token.dart' show BeginToken, TokenType, TokenWithComment;
8 8
9 import '../fasta_codes.dart' 9 import '../fasta_codes.dart'
10 show 10 show
11 FastaCode, 11 Code,
12 codeAsciiControlCharacter, 12 codeAsciiControlCharacter,
13 codeEncoding, 13 codeEncoding,
14 codeExpectedHexDigit, 14 codeExpectedHexDigit,
15 codeMissingExponent, 15 codeMissingExponent,
16 codeNonAsciiIdentifier, 16 codeNonAsciiIdentifier,
17 codeNonAsciiWhitespace, 17 codeNonAsciiWhitespace,
18 codeUnexpectedDollarInString, 18 codeUnexpectedDollarInString,
19 codeUnmatchedToken, 19 codeUnmatchedToken,
20 codeUnterminatedComment, 20 codeUnterminatedComment,
21 codeUnterminatedString, 21 codeUnterminatedString,
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 70
71 /// This is a token that wraps around an error message. Return 1 71 /// This is a token that wraps around an error message. Return 1
72 /// instead of the size of the length of the error message. 72 /// instead of the size of the length of the error message.
73 @override 73 @override
74 int get length => 1; 74 int get length => 1;
75 75
76 String get lexeme => throw assertionMessage; 76 String get lexeme => throw assertionMessage;
77 77
78 String get assertionMessage; 78 String get assertionMessage;
79 79
80 FastaCode get errorCode; 80 Code get errorCode;
81 81
82 int get character => null; 82 int get character => null;
83 83
84 String get start => null; 84 String get start => null;
85 85
86 int get endOffset => null; 86 int get endOffset => null;
87 87
88 BeginToken get begin => null; 88 BeginToken get begin => null;
89 89
90 @override 90 @override
91 Token copy() { 91 Token copy() {
92 throw 'unsupported operation'; 92 throw 'unsupported operation';
93 } 93 }
94 } 94 }
95 95
96 /// Represents an encoding error. 96 /// Represents an encoding error.
97 class EncodingErrorToken extends ErrorToken { 97 class EncodingErrorToken extends ErrorToken {
98 EncodingErrorToken(int charOffset) : super(charOffset); 98 EncodingErrorToken(int charOffset) : super(charOffset);
99 99
100 String toString() => "EncodingErrorToken()"; 100 String toString() => "EncodingErrorToken()";
101 101
102 String get assertionMessage => "Unable to decode bytes as UTF-8."; 102 String get assertionMessage => "Unable to decode bytes as UTF-8.";
103 103
104 FastaCode get errorCode => codeEncoding; 104 Code get errorCode => codeEncoding;
105 } 105 }
106 106
107 /// Represents a non-ASCII character outside a string or comment. 107 /// Represents a non-ASCII character outside a string or comment.
108 class NonAsciiIdentifierToken extends ErrorToken { 108 class NonAsciiIdentifierToken extends ErrorToken {
109 final int character; 109 final int character;
110 110
111 NonAsciiIdentifierToken(this.character, int charOffset) : super(charOffset); 111 NonAsciiIdentifierToken(this.character, int charOffset) : super(charOffset);
112 112
113 String toString() => "NonAsciiIdentifierToken($character)"; 113 String toString() => "NonAsciiIdentifierToken($character)";
114 114
115 String get assertionMessage { 115 String get assertionMessage {
116 String c = new String.fromCharCodes([character]); 116 String c = new String.fromCharCodes([character]);
117 String hex = character.toRadixString(16); 117 String hex = character.toRadixString(16);
118 String padding = "0000".substring(hex.length); 118 String padding = "0000".substring(hex.length);
119 hex = "$padding$hex"; 119 hex = "$padding$hex";
120 return "The non-ASCII character '$c' (U+$hex) can't be used in identifiers," 120 return "The non-ASCII character '$c' (U+$hex) can't be used in identifiers,"
121 " only in strings and comments.\n" 121 " only in strings and comments.\n"
122 "Try using an US-ASCII letter, a digit, '_' (an underscore)," 122 "Try using an US-ASCII letter, a digit, '_' (an underscore),"
123 " or '\$' (a dollar sign)."; 123 " or '\$' (a dollar sign).";
124 } 124 }
125 125
126 FastaCode get errorCode => codeNonAsciiIdentifier; 126 Code get errorCode => codeNonAsciiIdentifier;
127 } 127 }
128 128
129 /// Represents a non-ASCII whitespace outside a string or comment. 129 /// Represents a non-ASCII whitespace outside a string or comment.
130 class NonAsciiWhitespaceToken extends ErrorToken { 130 class NonAsciiWhitespaceToken extends ErrorToken {
131 final int character; 131 final int character;
132 132
133 NonAsciiWhitespaceToken(this.character, int charOffset) : super(charOffset); 133 NonAsciiWhitespaceToken(this.character, int charOffset) : super(charOffset);
134 134
135 String toString() => "NonAsciiWhitespaceToken($character)"; 135 String toString() => "NonAsciiWhitespaceToken($character)";
136 136
137 String get assertionMessage { 137 String get assertionMessage {
138 String hex = character.toRadixString(16); 138 String hex = character.toRadixString(16);
139 return "The non-ASCII space character U+$hex can only be used in strings " 139 return "The non-ASCII space character U+$hex can only be used in strings "
140 "and comments."; 140 "and comments.";
141 } 141 }
142 142
143 FastaCode get errorCode => codeNonAsciiWhitespace; 143 Code get errorCode => codeNonAsciiWhitespace;
144 } 144 }
145 145
146 /// Represents an ASCII control character outside a string or comment. 146 /// Represents an ASCII control character outside a string or comment.
147 class AsciiControlCharacterToken extends ErrorToken { 147 class AsciiControlCharacterToken extends ErrorToken {
148 final int character; 148 final int character;
149 149
150 AsciiControlCharacterToken(this.character, int charOffset) 150 AsciiControlCharacterToken(this.character, int charOffset)
151 : super(charOffset); 151 : super(charOffset);
152 152
153 String toString() => "AsciiControlCharacterToken($character)"; 153 String toString() => "AsciiControlCharacterToken($character)";
154 154
155 String get assertionMessage { 155 String get assertionMessage {
156 String hex = character.toRadixString(16); 156 String hex = character.toRadixString(16);
157 return "The control character U+$hex can only be used in strings and " 157 return "The control character U+$hex can only be used in strings and "
158 "comments."; 158 "comments.";
159 } 159 }
160 160
161 FastaCode get errorCode => codeAsciiControlCharacter; 161 Code get errorCode => codeAsciiControlCharacter;
162 } 162 }
163 163
164 /// Represents an unterminated string. 164 /// Represents an unterminated string.
165 class UnterminatedToken extends ErrorToken { 165 class UnterminatedToken extends ErrorToken {
166 final String start; 166 final String start;
167 final int endOffset; 167 final int endOffset;
168 168
169 UnterminatedToken(this.start, int charOffset, this.endOffset) 169 UnterminatedToken(this.start, int charOffset, this.endOffset)
170 : super(charOffset); 170 : super(charOffset);
171 171
172 String toString() => "UnterminatedToken($start)"; 172 String toString() => "UnterminatedToken($start)";
173 173
174 String get assertionMessage => "'$start' isn't terminated."; 174 String get assertionMessage => "'$start' isn't terminated.";
175 175
176 int get charCount => endOffset - charOffset; 176 int get charCount => endOffset - charOffset;
177 177
178 FastaCode get errorCode { 178 Code get errorCode {
179 switch (start) { 179 switch (start) {
180 case '1e': 180 case '1e':
181 return codeMissingExponent; 181 return codeMissingExponent;
182 182
183 case '"': 183 case '"':
184 case "'": 184 case "'":
185 case '"""': 185 case '"""':
186 case "'''": 186 case "'''":
187 case 'r"': 187 case 'r"':
188 case "r'": 188 case "r'":
(...skipping 24 matching lines...) Expand all
213 final BeginToken begin; 213 final BeginToken begin;
214 214
215 UnmatchedToken(BeginToken begin) 215 UnmatchedToken(BeginToken begin)
216 : this.begin = begin, 216 : this.begin = begin,
217 super(begin.charOffset); 217 super(begin.charOffset);
218 218
219 String toString() => "UnmatchedToken(${begin.lexeme})"; 219 String toString() => "UnmatchedToken(${begin.lexeme})";
220 220
221 String get assertionMessage => "'$begin' isn't closed."; 221 String get assertionMessage => "'$begin' isn't closed.";
222 222
223 FastaCode get errorCode => codeUnmatchedToken; 223 Code get errorCode => codeUnmatchedToken;
224 } 224 }
OLDNEW
« no previous file with comments | « pkg/front_end/lib/src/fasta/parser/parser.dart ('k') | pkg/front_end/lib/src/fasta/scanner/recover.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698