OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library glob.single_component; | |
6 | |
7 import 'package:path/path.dart' as p; | |
8 import 'package:string_scanner/string_scanner.dart'; | |
9 | |
10 import 'ast.dart'; | |
11 import 'utils.dart'; | |
12 | |
13 /// A parser for globs. | |
14 class Parser { | |
15 /// The scanner used to scan the source. | |
16 final StringScanner _scanner; | |
17 | |
18 /// The path context for the glob. | |
19 final p.Context _context; | |
20 | |
21 Parser(String component, this._context) | |
22 : _scanner = new StringScanner(component); | |
23 | |
24 /// Parses an entire glob. | |
25 SequenceNode parse() => _parseSequence(); | |
26 | |
27 /// Parses a [SequenceNode]. | |
28 /// | |
29 /// If [inOptions] is true, this is parsing within an [OptionsNode]. | |
30 SequenceNode _parseSequence({bool inOptions: false}) { | |
31 var nodes = []; | |
32 | |
33 while (!_scanner.isDone) { | |
34 if (inOptions && (_scanner.matches(',') || _scanner.matches('}'))) break; | |
35 nodes.add(_parseNode(inOptions: inOptions)); | |
36 } | |
37 | |
38 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
| |
39 } | |
40 | |
41 /// Parses an [AstNode]. | |
42 /// | |
43 /// If [inOptions] is true, this is parsing within an [OptionsNode]. | |
44 AstNode _parseNode({bool inOptions: false}) { | |
45 var star = _parseStar(); | |
46 if (star != null) return star; | |
47 | |
48 var anyChar = _parseAnyChar(); | |
49 if (anyChar != null) return anyChar; | |
50 | |
51 var range = _parseRange(); | |
52 if (range != null) return range; | |
53 | |
54 var options = _parseOptions(); | |
55 if (options != null) return options; | |
56 | |
57 return _parseLiteral(inOptions: inOptions); | |
58 } | |
59 | |
60 /// Tries to parse a [StarNode] or a [DoubleStarNode]. | |
61 /// | |
62 /// Returns `null` if there's not one to parse. | |
63 AstNode _parseStar() { | |
64 if (!_scanner.scan('*')) return null; | |
65 return _scanner.scan('*') ? new DoubleStarNode(_context) : new StarNode(); | |
66 } | |
67 | |
68 /// Tries to parse an [AnyCharNode]. | |
69 /// | |
70 /// Returns `null` if there's not one to parse. | |
71 AstNode _parseAnyChar() { | |
72 if (!_scanner.scan('?')) return null; | |
73 return new AnyCharNode(); | |
74 } | |
75 | |
76 /// Tries to parse an [RangeNode]. | |
77 /// | |
78 /// Returns `null` if there's not one to parse. | |
79 AstNode _parseRange() { | |
80 if (!_scanner.scan('[')) return null; | |
81 if (_scanner.matches(']')) _scanner.error('unexpected "]".'); | |
82 var negated = _scanner.scan('!') || _scanner.scan('^'); | |
83 | |
84 var ranges = []; | |
85 while (!_scanner.scan(']')) { | |
86 // Allow a backslash to escape a character. | |
87 _scanner.scan('\\'); | |
88 var char = _scanner.readChar(); | |
89 | |
90 if (_scanner.scan('-')) { | |
91 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.
| |
92 _scanner.error('unexpected "${_scanner.lastMatch[0]}".'); | |
93 } | |
94 | |
95 // Allow a backslash to escape a character. | |
96 _scanner.scan('\\'); | |
97 | |
98 var end = _scanner.readChar(); | |
99 if (end < char) { | |
100 _scanner.error("Range out of order.", | |
101 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.
| |
102 } | |
103 ranges.add(new Range(char, end)); | |
104 } else { | |
105 ranges.add(new Range.singleton(char)); | |
106 } | |
107 } | |
108 | |
109 return new RangeNode(ranges, negated: negated); | |
110 } | |
111 | |
112 /// Tries to parse an [OptionsNode]. | |
113 /// | |
114 /// Returns `null` if there's not one to parse. | |
115 AstNode _parseOptions() { | |
116 if (!_scanner.scan('{')) return null; | |
117 if (_scanner.matches('}')) _scanner.error('unexpected "}".'); | |
118 | |
119 var options = [_parseSequence(inOptions: true)]; | |
120 while (_scanner.scan(',')) { | |
Bob Nystrom
2014/08/27 00:42:04
Use a do/while here.
nweiz
2014/08/27 01:36:33
Done.
| |
121 options.add(_parseSequence(inOptions: true)); | |
122 } | |
123 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
| |
124 _scanner.expect('}'); | |
125 | |
126 return new OptionsNode(options); | |
127 } | |
128 | |
129 /// Parses a [LiteralNode]. | |
130 AstNode _parseLiteral({bool inOptions: false}) { | |
131 var regExp = new RegExp( | |
132 inOptions ? r'[^*{[?\\}\],()]*' : r'[^*{[?\\}\]()]*'); | |
Bob Nystrom
2014/08/27 00:42:04
Document this.
nweiz
2014/08/27 01:36:33
Done.
| |
133 | |
134 _scanner.scan(regExp); | |
135 var buffer = new StringBuffer()..write(_scanner.lastMatch[0]); | |
136 | |
137 while (_scanner.scan('\\')) { | |
138 buffer.writeCharCode(_scanner.readChar()); | |
139 _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.
| |
140 buffer.write(_scanner.lastMatch[0]); | |
141 } | |
142 | |
143 for (var char in const [']', '(', ')']) { | |
144 if (_scanner.matches(char)) _scanner.error('unexpected "$char"'); | |
145 } | |
146 if (!inOptions && _scanner.matches('}')) _scanner.error('unexpected "}"'); | |
147 | |
148 return new LiteralNode(buffer.toString(), _context); | |
149 } | |
150 } | |
OLD | NEW |