Chromium Code Reviews| Index: lib/src/loader.dart |
| diff --git a/lib/src/loader.dart b/lib/src/loader.dart |
| index d80578fc95cf9c3fb9780b577cfdbdbcdecfa261..3584e2ab015c8709dd610b1687af21b2644b62df 100644 |
| --- a/lib/src/loader.dart |
| +++ b/lib/src/loader.dart |
| @@ -13,6 +13,16 @@ import 'yaml_document.dart'; |
| import 'yaml_exception.dart'; |
| import 'yaml_node.dart'; |
| +final _nullRegExp = new RegExp(r"^(null|Null|NULL|~|)$"); |
| +final _boolRegExp = new RegExp(r"^(?:(true|True|TRUE)|(false|False|FALSE))$"); |
| +final _decimalIntRegExp = new RegExp(r"^[-+]?[0-9]+$"); |
| +final _octalIntRegExp = new RegExp(r"^0o([0-7]+)$"); |
| +final _hexIntRegExp = new RegExp(r"^0x[0-9a-fA-F]+$"); |
| +final _floatRegExp = new RegExp( |
| + r"^[-+]?(\.[0-9]+|[0-9]+(\.[0-9]*)?)([eE][-+]?[0-9]+)?$"); |
| +final _infinityRegExp = new RegExp(r"^([+-]?)\.(inf|Inf|INF)$"); |
| +final _nanRegExp = new RegExp(r"^\.(nan|NaN|NAN)$"); |
|
nweiz
2015/09/02 00:07:57
All these symbols are blurring together. You don't
Bob Nystrom
2015/09/02 00:13:32
Commented.
|
| + |
| /// A loader that reads [Event]s emitted by a [Parser] and emits |
| /// [YamlDocument]s. |
| /// |
| @@ -185,7 +195,7 @@ class Loader { |
| YamlScalar _parseNull(ScalarEvent scalar) { |
| // TODO(nweiz): stop using regexps. |
|
nweiz
2015/09/02 00:07:56
You can remove this.
Bob Nystrom
2015/09/02 00:13:32
Done.
|
| // TODO(nweiz): add ScalarStyle and implicit metadata to the scalars. |
| - if (new RegExp(r"^(null|Null|NULL|~|)$").hasMatch(scalar.value)) { |
| + if (_nullRegExp.hasMatch(scalar.value)) { |
| return new YamlScalar.internal(null, scalar.span, scalar.style); |
| } else { |
| return null; |
| @@ -194,8 +204,7 @@ class Loader { |
| /// Parses a boolean scalar. |
| YamlScalar _parseBool(ScalarEvent scalar) { |
| - var match = new RegExp(r"^(?:(true|True|TRUE)|(false|False|FALSE))$"). |
| - firstMatch(scalar.value); |
| + var match = _boolRegExp.firstMatch(scalar.value); |
| if (match == null) return null; |
| return new YamlScalar.internal( |
| match.group(1) != null, scalar.span, scalar.style); |
| @@ -203,19 +212,19 @@ class Loader { |
| /// Parses an integer scalar. |
| YamlScalar _parseInt(ScalarEvent scalar) { |
| - var match = new RegExp(r"^[-+]?[0-9]+$").firstMatch(scalar.value); |
| + var match = _decimalIntRegExp.firstMatch(scalar.value); |
| if (match != null) { |
| return new YamlScalar.internal( |
| int.parse(match.group(0)), scalar.span, scalar.style); |
| } |
| - match = new RegExp(r"^0o([0-7]+)$").firstMatch(scalar.value); |
| + match = _octalIntRegExp.firstMatch(scalar.value); |
| if (match != null) { |
| var n = int.parse(match.group(1), radix: 8); |
| return new YamlScalar.internal(n, scalar.span, scalar.style); |
| } |
| - match = new RegExp(r"^0x[0-9a-fA-F]+$").firstMatch(scalar.value); |
| + match = _hexIntRegExp.firstMatch(scalar.value); |
| if (match != null) { |
| return new YamlScalar.internal( |
| int.parse(match.group(0)), scalar.span, scalar.style); |
| @@ -226,9 +235,7 @@ class Loader { |
| /// Parses a floating-point scalar. |
| YamlScalar _parseFloat(ScalarEvent scalar) { |
| - var match = new RegExp( |
| - r"^[-+]?(\.[0-9]+|[0-9]+(\.[0-9]*)?)([eE][-+]?[0-9]+)?$"). |
| - firstMatch(scalar.value); |
| + var match = _floatRegExp.firstMatch(scalar.value); |
| if (match != null) { |
| // YAML allows floats of the form "0.", but Dart does not. Fix up those |
| // floats by removing the trailing dot. |
| @@ -237,13 +244,13 @@ class Loader { |
| double.parse(matchStr), scalar.span, scalar.style); |
| } |
| - match = new RegExp(r"^([+-]?)\.(inf|Inf|INF)$").firstMatch(scalar.value); |
| + match = _infinityRegExp.firstMatch(scalar.value); |
| if (match != null) { |
| var value = match.group(1) == "-" ? -double.INFINITY : double.INFINITY; |
| return new YamlScalar.internal(value, scalar.span, scalar.style); |
| } |
| - match = new RegExp(r"^\.(nan|NaN|NAN)$").firstMatch(scalar.value); |
| + match = _nanRegExp.firstMatch(scalar.value); |
| if (match != null) { |
| return new YamlScalar.internal(double.NAN, scalar.span, scalar.style); |
| } |