Chromium Code Reviews| Index: pkg/glob/lib/src/parser.dart |
| diff --git a/pkg/glob/lib/src/parser.dart b/pkg/glob/lib/src/parser.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8bb36499816a8df019b85a1a9417571dadcb7ff5 |
| --- /dev/null |
| +++ b/pkg/glob/lib/src/parser.dart |
| @@ -0,0 +1,150 @@ |
| +// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +library glob.single_component; |
| + |
| +import 'package:path/path.dart' as p; |
| +import 'package:string_scanner/string_scanner.dart'; |
| + |
| +import 'ast.dart'; |
| +import 'utils.dart'; |
| + |
| +/// A parser for globs. |
| +class Parser { |
| + /// The scanner used to scan the source. |
| + final StringScanner _scanner; |
| + |
| + /// The path context for the glob. |
| + final p.Context _context; |
| + |
| + Parser(String component, this._context) |
| + : _scanner = new StringScanner(component); |
| + |
| + /// Parses an entire glob. |
| + SequenceNode parse() => _parseSequence(); |
| + |
| + /// Parses a [SequenceNode]. |
| + /// |
| + /// If [inOptions] is true, this is parsing within an [OptionsNode]. |
| + SequenceNode _parseSequence({bool inOptions: false}) { |
| + var nodes = []; |
| + |
| + while (!_scanner.isDone) { |
| + if (inOptions && (_scanner.matches(',') || _scanner.matches('}'))) break; |
| + nodes.add(_parseNode(inOptions: inOptions)); |
| + } |
| + |
| + return new SequenceNode(nodes); |
|
Bob Nystrom
2014/08/27 00:42:04
This allows an empty sequence intentionally, right
nweiz
2014/08/27 01:36:33
Eh, it should probably error for an empty sequence
|
| + } |
| + |
| + /// Parses an [AstNode]. |
| + /// |
| + /// If [inOptions] is true, this is parsing within an [OptionsNode]. |
| + AstNode _parseNode({bool inOptions: false}) { |
| + var star = _parseStar(); |
| + if (star != null) return star; |
| + |
| + var anyChar = _parseAnyChar(); |
| + if (anyChar != null) return anyChar; |
| + |
| + var range = _parseRange(); |
| + if (range != null) return range; |
| + |
| + var options = _parseOptions(); |
| + if (options != null) return options; |
| + |
| + return _parseLiteral(inOptions: inOptions); |
| + } |
| + |
| + /// Tries to parse a [StarNode] or a [DoubleStarNode]. |
| + /// |
| + /// Returns `null` if there's not one to parse. |
| + AstNode _parseStar() { |
| + if (!_scanner.scan('*')) return null; |
| + return _scanner.scan('*') ? new DoubleStarNode(_context) : new StarNode(); |
| + } |
| + |
| + /// Tries to parse an [AnyCharNode]. |
| + /// |
| + /// Returns `null` if there's not one to parse. |
| + AstNode _parseAnyChar() { |
| + if (!_scanner.scan('?')) return null; |
| + return new AnyCharNode(); |
| + } |
| + |
| + /// Tries to parse an [RangeNode]. |
| + /// |
| + /// Returns `null` if there's not one to parse. |
| + AstNode _parseRange() { |
| + if (!_scanner.scan('[')) return null; |
| + if (_scanner.matches(']')) _scanner.error('unexpected "]".'); |
| + var negated = _scanner.scan('!') || _scanner.scan('^'); |
| + |
| + var ranges = []; |
| + while (!_scanner.scan(']')) { |
| + // Allow a backslash to escape a character. |
| + _scanner.scan('\\'); |
| + var char = _scanner.readChar(); |
| + |
| + if (_scanner.scan('-')) { |
| + if (_scanner.matches('-') || _scanner.matches(']')) { |
|
Bob Nystrom
2014/08/27 00:42:04
'?' or '^' too?
nweiz
2014/08/27 01:36:33
No, something like [#-?] is fine.
|
| + _scanner.error('unexpected "${_scanner.lastMatch[0]}".'); |
| + } |
| + |
| + // Allow a backslash to escape a character. |
| + _scanner.scan('\\'); |
| + |
| + var end = _scanner.readChar(); |
| + if (end < char) { |
| + _scanner.error("Range out of order.", |
| + position: _scanner.position - 3, length: 3); |
|
Bob Nystrom
2014/08/27 00:42:04
This doesn't take into account '\'.
nweiz
2014/08/27 01:36:33
Done.
|
| + } |
| + ranges.add(new Range(char, end)); |
| + } else { |
| + ranges.add(new Range.singleton(char)); |
| + } |
| + } |
| + |
| + return new RangeNode(ranges, negated: negated); |
| + } |
| + |
| + /// Tries to parse an [OptionsNode]. |
| + /// |
| + /// Returns `null` if there's not one to parse. |
| + AstNode _parseOptions() { |
| + if (!_scanner.scan('{')) return null; |
| + if (_scanner.matches('}')) _scanner.error('unexpected "}".'); |
| + |
| + var options = [_parseSequence(inOptions: true)]; |
| + while (_scanner.scan(',')) { |
|
Bob Nystrom
2014/08/27 00:42:04
Use a do/while here.
nweiz
2014/08/27 01:36:33
Done.
|
| + options.add(_parseSequence(inOptions: true)); |
| + } |
| + if (options.length == 1) _scanner.expect(','); |
|
Bob Nystrom
2014/08/27 00:42:04
What's this for?
nweiz
2014/08/27 01:36:33
It disallows single-option groups. Added a comment
|
| + _scanner.expect('}'); |
| + |
| + return new OptionsNode(options); |
| + } |
| + |
| + /// Parses a [LiteralNode]. |
| + AstNode _parseLiteral({bool inOptions: false}) { |
| + var regExp = new RegExp( |
| + inOptions ? r'[^*{[?\\}\],()]*' : r'[^*{[?\\}\]()]*'); |
|
Bob Nystrom
2014/08/27 00:42:04
Document this.
nweiz
2014/08/27 01:36:33
Done.
|
| + |
| + _scanner.scan(regExp); |
| + var buffer = new StringBuffer()..write(_scanner.lastMatch[0]); |
| + |
| + while (_scanner.scan('\\')) { |
| + buffer.writeCharCode(_scanner.readChar()); |
| + _scanner.scan(regExp); |
|
Bob Nystrom
2014/08/27 00:42:04
What about multiple sequential escaped characters?
nweiz
2014/08/27 01:36:33
[regExp] just matches zero characters here.
|
| + buffer.write(_scanner.lastMatch[0]); |
| + } |
| + |
| + for (var char in const [']', '(', ')']) { |
| + if (_scanner.matches(char)) _scanner.error('unexpected "$char"'); |
| + } |
| + if (!inOptions && _scanner.matches('}')) _scanner.error('unexpected "}"'); |
| + |
| + return new LiteralNode(buffer.toString(), _context); |
| + } |
| +} |