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

Side by Side Diff: lib/src/loader.dart

Issue 1310423004: Hoist tokenizing RegExps out. (Closed) Base URL: https://github.com/dart-lang/yaml.git@master
Patch Set: Rebase. Created 5 years, 3 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, 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 license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library yaml.loader; 5 library yaml.loader;
6 6
7 import 'package:source_span/source_span.dart'; 7 import 'package:source_span/source_span.dart';
8 8
9 import 'equality.dart'; 9 import 'equality.dart';
10 import 'event.dart'; 10 import 'event.dart';
11 import 'parser.dart'; 11 import 'parser.dart';
12 import 'yaml_document.dart'; 12 import 'yaml_document.dart';
13 import 'yaml_exception.dart'; 13 import 'yaml_exception.dart';
14 import 'yaml_node.dart'; 14 import 'yaml_node.dart';
15 15
16 final _nullRegExp = new RegExp(r"^(null|Null|NULL|~|)$");
17 final _boolRegExp = new RegExp(r"^(?:(true|True|TRUE)|(false|False|FALSE))$");
18 final _decimalIntRegExp = new RegExp(r"^[-+]?[0-9]+$");
19 final _octalIntRegExp = new RegExp(r"^0o([0-7]+)$");
20 final _hexIntRegExp = new RegExp(r"^0x[0-9a-fA-F]+$");
21 final _floatRegExp = new RegExp(
22 r"^[-+]?(\.[0-9]+|[0-9]+(\.[0-9]*)?)([eE][-+]?[0-9]+)?$");
23 final _infinityRegExp = new RegExp(r"^([+-]?)\.(inf|Inf|INF)$");
24 final _nanRegExp = new RegExp(r"^\.(nan|NaN|NAN)$");
25
16 /// A loader that reads [Event]s emitted by a [Parser] and emits 26 /// A loader that reads [Event]s emitted by a [Parser] and emits
17 /// [YamlDocument]s. 27 /// [YamlDocument]s.
18 /// 28 ///
19 /// This is based on the libyaml loader, available at 29 /// This is based on the libyaml loader, available at
20 /// https://github.com/yaml/libyaml/blob/master/src/loader.c. The license for 30 /// https://github.com/yaml/libyaml/blob/master/src/loader.c. The license for
21 /// that is available in ../../libyaml-license.txt. 31 /// that is available in ../../libyaml-license.txt.
22 class Loader { 32 class Loader {
23 /// The underlying [Parser] that generates [Event]s. 33 /// The underlying [Parser] that generates [Event]s.
24 final Parser _parser; 34 final Parser _parser;
25 35
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 case "tag:yaml.org,2002:float": return _parseFloat(scalar); 188 case "tag:yaml.org,2002:float": return _parseFloat(scalar);
179 case "tag:yaml.org,2002:str": return _parseString(scalar); 189 case "tag:yaml.org,2002:str": return _parseString(scalar);
180 } 190 }
181 throw new YamlException('Undefined tag: ${scalar.tag}.', scalar.span); 191 throw new YamlException('Undefined tag: ${scalar.tag}.', scalar.span);
182 } 192 }
183 193
184 /// Parses a null scalar. 194 /// Parses a null scalar.
185 YamlScalar _parseNull(ScalarEvent scalar) { 195 YamlScalar _parseNull(ScalarEvent scalar) {
186 // TODO(nweiz): stop using regexps. 196 // TODO(nweiz): stop using regexps.
187 // TODO(nweiz): add ScalarStyle and implicit metadata to the scalars. 197 // TODO(nweiz): add ScalarStyle and implicit metadata to the scalars.
188 if (new RegExp(r"^(null|Null|NULL|~|)$").hasMatch(scalar.value)) { 198 if (_nullRegExp.hasMatch(scalar.value)) {
189 return new YamlScalar.internal(null, scalar.span, scalar.style); 199 return new YamlScalar.internal(null, scalar.span, scalar.style);
190 } else { 200 } else {
191 return null; 201 return null;
192 } 202 }
193 } 203 }
194 204
195 /// Parses a boolean scalar. 205 /// Parses a boolean scalar.
196 YamlScalar _parseBool(ScalarEvent scalar) { 206 YamlScalar _parseBool(ScalarEvent scalar) {
197 var match = new RegExp(r"^(?:(true|True|TRUE)|(false|False|FALSE))$"). 207 var match = _boolRegExp.firstMatch(scalar.value);
198 firstMatch(scalar.value);
199 if (match == null) return null; 208 if (match == null) return null;
200 return new YamlScalar.internal( 209 return new YamlScalar.internal(
201 match.group(1) != null, scalar.span, scalar.style); 210 match.group(1) != null, scalar.span, scalar.style);
202 } 211 }
203 212
204 /// Parses an integer scalar. 213 /// Parses an integer scalar.
205 YamlScalar _parseInt(ScalarEvent scalar) { 214 YamlScalar _parseInt(ScalarEvent scalar) {
206 var match = new RegExp(r"^[-+]?[0-9]+$").firstMatch(scalar.value); 215 var match = _decimalIntRegExp.firstMatch(scalar.value);
207 if (match != null) { 216 if (match != null) {
208 return new YamlScalar.internal( 217 return new YamlScalar.internal(
209 int.parse(match.group(0)), scalar.span, scalar.style); 218 int.parse(match.group(0)), scalar.span, scalar.style);
210 } 219 }
211 220
212 match = new RegExp(r"^0o([0-7]+)$").firstMatch(scalar.value); 221 match = _octalIntRegExp.firstMatch(scalar.value);
213 if (match != null) { 222 if (match != null) {
214 var n = int.parse(match.group(1), radix: 8); 223 var n = int.parse(match.group(1), radix: 8);
215 return new YamlScalar.internal(n, scalar.span, scalar.style); 224 return new YamlScalar.internal(n, scalar.span, scalar.style);
216 } 225 }
217 226
218 match = new RegExp(r"^0x[0-9a-fA-F]+$").firstMatch(scalar.value); 227 match = _hexIntRegExp.firstMatch(scalar.value);
219 if (match != null) { 228 if (match != null) {
220 return new YamlScalar.internal( 229 return new YamlScalar.internal(
221 int.parse(match.group(0)), scalar.span, scalar.style); 230 int.parse(match.group(0)), scalar.span, scalar.style);
222 } 231 }
223 232
224 return null; 233 return null;
225 } 234 }
226 235
227 /// Parses a floating-point scalar. 236 /// Parses a floating-point scalar.
228 YamlScalar _parseFloat(ScalarEvent scalar) { 237 YamlScalar _parseFloat(ScalarEvent scalar) {
229 var match = new RegExp( 238 var match = _floatRegExp.firstMatch(scalar.value);
230 r"^[-+]?(\.[0-9]+|[0-9]+(\.[0-9]*)?)([eE][-+]?[0-9]+)?$").
231 firstMatch(scalar.value);
232 if (match != null) { 239 if (match != null) {
233 // YAML allows floats of the form "0.", but Dart does not. Fix up those 240 // YAML allows floats of the form "0.", but Dart does not. Fix up those
234 // floats by removing the trailing dot. 241 // floats by removing the trailing dot.
235 var matchStr = match.group(0).replaceAll(new RegExp(r"\.$"), ""); 242 var matchStr = match.group(0).replaceAll(new RegExp(r"\.$"), "");
236 return new YamlScalar.internal( 243 return new YamlScalar.internal(
237 double.parse(matchStr), scalar.span, scalar.style); 244 double.parse(matchStr), scalar.span, scalar.style);
238 } 245 }
239 246
240 match = new RegExp(r"^([+-]?)\.(inf|Inf|INF)$").firstMatch(scalar.value); 247 match = _infinityRegExp.firstMatch(scalar.value);
241 if (match != null) { 248 if (match != null) {
242 var value = match.group(1) == "-" ? -double.INFINITY : double.INFINITY; 249 var value = match.group(1) == "-" ? -double.INFINITY : double.INFINITY;
243 return new YamlScalar.internal(value, scalar.span, scalar.style); 250 return new YamlScalar.internal(value, scalar.span, scalar.style);
244 } 251 }
245 252
246 match = new RegExp(r"^\.(nan|NaN|NAN)$").firstMatch(scalar.value); 253 match = _nanRegExp.firstMatch(scalar.value);
247 if (match != null) { 254 if (match != null) {
248 return new YamlScalar.internal(double.NAN, scalar.span, scalar.style); 255 return new YamlScalar.internal(double.NAN, scalar.span, scalar.style);
249 } 256 }
250 257
251 return null; 258 return null;
252 } 259 }
253 260
254 /// Parses a string scalar. 261 /// Parses a string scalar.
255 YamlScalar _parseString(ScalarEvent scalar) => 262 YamlScalar _parseString(ScalarEvent scalar) =>
256 new YamlScalar.internal(scalar.value, scalar.span, scalar.style); 263 new YamlScalar.internal(scalar.value, scalar.span, scalar.style);
257 } 264 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698