OLD | NEW |
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 // TODO(ahe): ErrorKind doesn't belong in dart_parser. Move to compiler_util or | 7 import '../fasta_codes.dart' |
8 // this package? | 8 show |
9 import '../parser/error_kind.dart' show ErrorKind; | 9 FastaCode, |
| 10 codeAsciiControlCharacter, |
| 11 codeEncoding, |
| 12 codeExpectedHexDigit, |
| 13 codeMissingExponent, |
| 14 codeNonAsciiIdentifier, |
| 15 codeNonAsciiWhitespace, |
| 16 codeUnexpectedDollarInString, |
| 17 codeUnmatchedToken, |
| 18 codeUnterminatedComment, |
| 19 codeUnterminatedString, |
| 20 codeUnterminatedToken; |
10 | 21 |
11 import '../scanner.dart' | 22 import '../scanner.dart' |
12 show BeginGroupToken, Token, unicodeReplacementCharacter; | 23 show BeginGroupToken, Token, unicodeReplacementCharacter; |
13 | 24 |
14 import 'precedence.dart' show BAD_INPUT_INFO, PrecedenceInfo; | 25 import 'precedence.dart' show BAD_INPUT_INFO, PrecedenceInfo; |
15 | 26 |
16 export '../parser/error_kind.dart' show ErrorKind; | |
17 | |
18 ErrorToken buildUnexpectedCharacterToken(int character, int charOffset) { | 27 ErrorToken buildUnexpectedCharacterToken(int character, int charOffset) { |
19 if (character < 0x1f) { | 28 if (character < 0x1f) { |
20 return new AsciiControlCharacterToken(character, charOffset); | 29 return new AsciiControlCharacterToken(character, charOffset); |
21 } | 30 } |
22 switch (character) { | 31 switch (character) { |
23 case unicodeReplacementCharacter: | 32 case unicodeReplacementCharacter: |
24 return new EncodingErrorToken(charOffset); | 33 return new EncodingErrorToken(charOffset); |
25 | 34 |
26 /// See [General Punctuation] | 35 /// See [General Punctuation] |
27 /// (http://www.unicode.org/charts/PDF/U2000.pdf). | 36 /// (http://www.unicode.org/charts/PDF/U2000.pdf). |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 PrecedenceInfo get info => BAD_INPUT_INFO; | 72 PrecedenceInfo get info => BAD_INPUT_INFO; |
64 | 73 |
65 String get lexeme => throw assertionMessage; | 74 String get lexeme => throw assertionMessage; |
66 | 75 |
67 String get stringValue => null; | 76 String get stringValue => null; |
68 | 77 |
69 bool isIdentifier() => false; | 78 bool isIdentifier() => false; |
70 | 79 |
71 String get assertionMessage; | 80 String get assertionMessage; |
72 | 81 |
73 ErrorKind get errorCode; | 82 FastaCode get errorCode; |
74 | 83 |
75 int get character => null; | 84 int get character => null; |
76 | 85 |
77 String get start => null; | 86 String get start => null; |
78 | 87 |
79 int get endOffset => null; | 88 int get endOffset => null; |
80 | 89 |
81 BeginGroupToken get begin => null; | 90 BeginGroupToken get begin => null; |
82 | 91 |
83 @override | 92 @override |
84 Token copyWithoutComments() { | 93 Token copyWithoutComments() { |
85 throw 'unsupported operation'; | 94 throw 'unsupported operation'; |
86 } | 95 } |
87 } | 96 } |
88 | 97 |
89 /// Represents an encoding error. | 98 /// Represents an encoding error. |
90 class EncodingErrorToken extends ErrorToken { | 99 class EncodingErrorToken extends ErrorToken { |
91 EncodingErrorToken(int charOffset) : super(charOffset); | 100 EncodingErrorToken(int charOffset) : super(charOffset); |
92 | 101 |
93 String toString() => "EncodingErrorToken()"; | 102 String toString() => "EncodingErrorToken()"; |
94 | 103 |
95 String get assertionMessage => "Unable to decode bytes as UTF-8."; | 104 String get assertionMessage => "Unable to decode bytes as UTF-8."; |
96 | 105 |
97 ErrorKind get errorCode => ErrorKind.Encoding; | 106 FastaCode get errorCode => codeEncoding; |
98 } | 107 } |
99 | 108 |
100 /// Represents a non-ASCII character outside a string or comment. | 109 /// Represents a non-ASCII character outside a string or comment. |
101 class NonAsciiIdentifierToken extends ErrorToken { | 110 class NonAsciiIdentifierToken extends ErrorToken { |
102 final int character; | 111 final int character; |
103 | 112 |
104 NonAsciiIdentifierToken(this.character, int charOffset) : super(charOffset); | 113 NonAsciiIdentifierToken(this.character, int charOffset) : super(charOffset); |
105 | 114 |
106 String toString() => "NonAsciiIdentifierToken($character)"; | 115 String toString() => "NonAsciiIdentifierToken($character)"; |
107 | 116 |
108 String get assertionMessage { | 117 String get assertionMessage { |
109 String c = new String.fromCharCodes([character]); | 118 String c = new String.fromCharCodes([character]); |
110 String hex = character.toRadixString(16); | 119 String hex = character.toRadixString(16); |
111 String padding = "0000".substring(hex.length); | 120 String padding = "0000".substring(hex.length); |
112 hex = "$padding$hex"; | 121 hex = "$padding$hex"; |
113 return "The non-ASCII character '$c' (U+$hex) can't be used in identifiers," | 122 return "The non-ASCII character '$c' (U+$hex) can't be used in identifiers," |
114 " only in strings and comments.\n" | 123 " only in strings and comments.\n" |
115 "Try using an US-ASCII letter, a digit, '_' (an underscore)," | 124 "Try using an US-ASCII letter, a digit, '_' (an underscore)," |
116 " or '\$' (a dollar sign)."; | 125 " or '\$' (a dollar sign)."; |
117 } | 126 } |
118 | 127 |
119 ErrorKind get errorCode => ErrorKind.NonAsciiIdentifier; | 128 FastaCode get errorCode => codeNonAsciiIdentifier; |
120 } | 129 } |
121 | 130 |
122 /// Represents a non-ASCII whitespace outside a string or comment. | 131 /// Represents a non-ASCII whitespace outside a string or comment. |
123 class NonAsciiWhitespaceToken extends ErrorToken { | 132 class NonAsciiWhitespaceToken extends ErrorToken { |
124 final int character; | 133 final int character; |
125 | 134 |
126 NonAsciiWhitespaceToken(this.character, int charOffset) : super(charOffset); | 135 NonAsciiWhitespaceToken(this.character, int charOffset) : super(charOffset); |
127 | 136 |
128 String toString() => "NonAsciiWhitespaceToken($character)"; | 137 String toString() => "NonAsciiWhitespaceToken($character)"; |
129 | 138 |
130 String get assertionMessage { | 139 String get assertionMessage { |
131 String hex = character.toRadixString(16); | 140 String hex = character.toRadixString(16); |
132 return "The non-ASCII space character U+$hex can only be used in strings " | 141 return "The non-ASCII space character U+$hex can only be used in strings " |
133 "and comments."; | 142 "and comments."; |
134 } | 143 } |
135 | 144 |
136 ErrorKind get errorCode => ErrorKind.NonAsciiWhitespace; | 145 FastaCode get errorCode => codeNonAsciiWhitespace; |
137 } | 146 } |
138 | 147 |
139 /// Represents an ASCII control character outside a string or comment. | 148 /// Represents an ASCII control character outside a string or comment. |
140 class AsciiControlCharacterToken extends ErrorToken { | 149 class AsciiControlCharacterToken extends ErrorToken { |
141 final int character; | 150 final int character; |
142 | 151 |
143 AsciiControlCharacterToken(this.character, int charOffset) | 152 AsciiControlCharacterToken(this.character, int charOffset) |
144 : super(charOffset); | 153 : super(charOffset); |
145 | 154 |
146 String toString() => "AsciiControlCharacterToken($character)"; | 155 String toString() => "AsciiControlCharacterToken($character)"; |
147 | 156 |
148 String get assertionMessage { | 157 String get assertionMessage { |
149 String hex = character.toRadixString(16); | 158 String hex = character.toRadixString(16); |
150 return "The control character U+$hex can only be used in strings and " | 159 return "The control character U+$hex can only be used in strings and " |
151 "comments."; | 160 "comments."; |
152 } | 161 } |
153 | 162 |
154 ErrorKind get errorCode => ErrorKind.AsciiControlCharacter; | 163 FastaCode get errorCode => codeAsciiControlCharacter; |
155 } | 164 } |
156 | 165 |
157 /// Represents an unterminated string. | 166 /// Represents an unterminated string. |
158 class UnterminatedToken extends ErrorToken { | 167 class UnterminatedToken extends ErrorToken { |
159 final String start; | 168 final String start; |
160 final int endOffset; | 169 final int endOffset; |
161 | 170 |
162 UnterminatedToken(this.start, int charOffset, this.endOffset) | 171 UnterminatedToken(this.start, int charOffset, this.endOffset) |
163 : super(charOffset); | 172 : super(charOffset); |
164 | 173 |
165 String toString() => "UnterminatedToken($start)"; | 174 String toString() => "UnterminatedToken($start)"; |
166 | 175 |
167 String get assertionMessage => "'$start' isn't terminated."; | 176 String get assertionMessage => "'$start' isn't terminated."; |
168 | 177 |
169 int get charCount => endOffset - charOffset; | 178 int get charCount => endOffset - charOffset; |
170 | 179 |
171 ErrorKind get errorCode { | 180 FastaCode get errorCode { |
172 switch (start) { | 181 switch (start) { |
173 case '1e': | 182 case '1e': |
174 return ErrorKind.MissingExponent; | 183 return codeMissingExponent; |
175 | 184 |
176 case '"': | 185 case '"': |
177 case "'": | 186 case "'": |
178 case '"""': | 187 case '"""': |
179 case "'''": | 188 case "'''": |
180 case 'r"': | 189 case 'r"': |
181 case "r'": | 190 case "r'": |
182 case 'r"""': | 191 case 'r"""': |
183 case "r'''": | 192 case "r'''": |
184 return ErrorKind.UnterminatedString; | 193 return codeUnterminatedString; |
185 | 194 |
186 case '0x': | 195 case '0x': |
187 return ErrorKind.ExpectedHexDigit; | 196 return codeExpectedHexDigit; |
188 | 197 |
189 case r'$': | 198 case r'$': |
190 return ErrorKind.UnexpectedDollarInString; | 199 return codeUnexpectedDollarInString; |
191 | 200 |
192 case '/*': | 201 case '/*': |
193 return ErrorKind.UnterminatedComment; | 202 return codeUnterminatedComment; |
194 | 203 |
195 default: | 204 default: |
196 return ErrorKind.UnterminatedToken; | 205 return codeUnterminatedToken; |
197 } | 206 } |
198 } | 207 } |
199 } | 208 } |
200 | 209 |
201 /// Represents an open brace without a matching close brace. | 210 /// Represents an open brace without a matching close brace. |
202 /// | 211 /// |
203 /// In this case, brace means any of `(`, `{`, `[`, and `<`, parenthesis, curly | 212 /// In this case, brace means any of `(`, `{`, `[`, and `<`, parenthesis, curly |
204 /// brace, square brace, and angle brace, respectively. | 213 /// brace, square brace, and angle brace, respectively. |
205 class UnmatchedToken extends ErrorToken { | 214 class UnmatchedToken extends ErrorToken { |
206 final BeginGroupToken begin; | 215 final BeginGroupToken begin; |
207 | 216 |
208 UnmatchedToken(BeginGroupToken begin) | 217 UnmatchedToken(BeginGroupToken begin) |
209 : this.begin = begin, | 218 : this.begin = begin, |
210 super(begin.charOffset); | 219 super(begin.charOffset); |
211 | 220 |
212 String toString() => "UnmatchedToken(${begin.lexeme})"; | 221 String toString() => "UnmatchedToken(${begin.lexeme})"; |
213 | 222 |
214 String get assertionMessage => "'$begin' isn't closed."; | 223 String get assertionMessage => "'$begin' isn't closed."; |
215 | 224 |
216 ErrorKind get errorCode => ErrorKind.UnmatchedToken; | 225 FastaCode get errorCode => codeUnmatchedToken; |
217 } | 226 } |
OLD | NEW |