OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011, 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 class JSSyntaxRegExp implements RegExp { | |
6 const JSSyntaxRegExp( | |
7 String this.pattern, | |
8 [bool this.multiLine = false, | |
9 bool this.ignoreCase = false]); | |
10 | |
11 final String pattern; | |
12 final bool multiLine; | |
13 final bool ignoreCase; | |
14 | |
15 Iterable<Match> allMatches(String str) { | |
16 return new _LazyAllMatches(this, str); | |
17 } | |
18 | |
19 Match firstMatch(String str) native; | |
20 bool hasMatch(String str) native; | |
21 String stringMatch(String str) native; | |
22 | |
23 static String _pattern(JSSyntaxRegExp regexp) native { | |
24 return regexp.pattern; | |
25 } | |
26 static bool _multiLine(JSSyntaxRegExp regexp) native { | |
27 return regexp.multiLine; | |
28 } | |
29 static bool _ignoreCase(JSSyntaxRegExp regexp) native { | |
30 return regexp.ignoreCase; | |
31 } | |
32 } | |
33 | |
34 class JSSyntaxMatch implements Match { | |
35 const JSSyntaxMatch(RegExp regexp, String str) | |
36 : this.pattern = regexp, this.str = str; | |
37 | |
38 final String str; | |
39 final Pattern pattern; | |
40 | |
41 String operator[](int group_) { | |
42 return this.group(group_); | |
43 } | |
44 | |
45 List<String> groups(List<int> groups_) { | |
46 List<String> strings = new List<String>(); | |
47 groups_.forEach((int group_) { | |
48 strings.add(this.group(group_)); | |
49 }); | |
50 return strings; | |
51 } | |
52 | |
53 String group(int nb) native; | |
54 | |
55 int start() native; | |
56 | |
57 int end() native; | |
58 | |
59 groupCount() native; | |
60 | |
61 static _new(RegExp regexp, String str) native { | |
62 return new JSSyntaxMatch(regexp, str); | |
63 } | |
64 } | |
65 | |
66 class _LazyAllMatches implements Collection<Match> { | |
67 final JSSyntaxRegExp _regexp; | |
68 final String _str; | |
69 | |
70 const _LazyAllMatches(this._regexp, this._str); | |
71 | |
72 void forEach(void f(Match match)) { | |
73 for (Match match in this) { | |
74 f(match); | |
75 } | |
76 } | |
77 | |
78 Collection map(f(Match match)) { | |
79 List result = new List(); | |
80 for (Match match in this) { | |
81 result.add(f(match)); | |
82 } | |
83 return result; | |
84 } | |
85 | |
86 Collection<Match> filter(bool f(Match match)) { | |
87 List<Match> result = new List<Match>(); | |
88 for (Match match in this) { | |
89 if (f(match)) result.add(match); | |
90 } | |
91 return result; | |
92 } | |
93 | |
94 bool every(bool f(Match match)) { | |
95 for (Match match in this) { | |
96 if (!f(match)) return false; | |
97 } | |
98 return true; | |
99 } | |
100 | |
101 bool some(bool f(Match match)) { | |
102 for (Match match in this) { | |
103 if (f(match)) return true; | |
104 } | |
105 return false; | |
106 } | |
107 | |
108 bool isEmpty() { | |
109 return _regexp.firstMatch(_str) == null; | |
110 } | |
111 | |
112 int get length() { | |
113 int result = 0; | |
114 for (Match match in this) { | |
115 result++; | |
116 } | |
117 return result; | |
118 } | |
119 | |
120 Iterator<Match> iterator() { | |
121 return new _LazyAllMatchesIterator(_regexp, _str); | |
122 } | |
123 } | |
124 | |
125 class _LazyAllMatchesIterator implements Iterator<Match> { | |
126 JSSyntaxRegExp _regexp; | |
127 String _str; | |
128 Match _nextMatch; | |
129 | |
130 _LazyAllMatchesIterator(this._regexp, this._str) { | |
131 _jsInit(_regexp); | |
132 } | |
133 | |
134 Match next() { | |
135 if (!hasNext()) throw const NoMoreElementsException(); | |
136 Match result = _nextMatch; | |
137 _nextMatch = null; | |
138 return result; | |
139 } | |
140 | |
141 bool hasNext() { | |
142 if (_nextMatch != null) return true; | |
143 _nextMatch = _computeNextMatch(_regexp, _str); | |
144 return (_nextMatch != null); | |
145 } | |
146 | |
147 void _jsInit(JSSyntaxRegExp regexp) native; | |
148 Match _computeNextMatch(JSSyntaxRegExp regexp, String str) native; | |
149 } | |
OLD | NEW |