@@ -52,67 +52,224 @@ window.VerbalExpression = (function(){
52
52
// Define the class methods.
53
53
VerbalExpression . prototype = {
54
54
55
+ // Variables to hold the whole
56
+ // expression construction in order
57
+ _prefixes : "" ,
55
58
_source : "" ,
59
+ _suffixes : "" ,
60
+ _modifiers : "gm" , // default to global multiline matching
56
61
57
- lol : function ( value ) {
58
- console . log ( "loL" ) ;
59
- this . compile ( value ) ;
60
- return ( this ) ;
61
- } ,
62
62
63
+ // Sanitation function for adding
64
+ // anything safely to the expression
63
65
sanitize : function ( value ) {
64
66
return value . replace ( / [ ^ \w ] / g, function ( character ) { return "\\" + character ; } ) ;
65
67
} ,
66
68
67
- startOfLine : function ( ) {
68
- if ( this . source . indexOf ( "^" ) !== 0 ) this . _source = "^" + this . _source ;
69
+ // Function to add stuff to the
70
+ // expression. Also compiles the
71
+ // new expression so it's ready to
72
+ // be used.
73
+ add : function ( value ) {
74
+ this . _source += value || "" ;
75
+ this . compile ( this . _prefixes + this . _source + this . _suffixes , this . _modifiers ) ;
76
+ return ( this ) ;
77
+ } ,
78
+
79
+ // Start and end of line functions
80
+ startOfLine : function ( enable ) {
81
+ enable = ( enable != false ) ;
82
+ this . _prefixes = enable ? "^" : "" ;
69
83
this . add ( "" ) ;
70
84
return ( this ) ;
71
85
} ,
72
86
73
- add : function ( value ) {
74
- this . _source += value || "" ;
75
- this . compile ( this . _source ) ;
87
+ endOfLine : function ( enable ) {
88
+ enable = ( enable != false ) ;
89
+ this . _suffixes = enable ? "$" : "" ;
90
+ this . add ( "" ) ;
76
91
return ( this ) ;
77
92
} ,
78
93
94
+ // We try to keep the syntax ass
95
+ // user-friendly as possible.
96
+ // So we can use the "normal"
97
+ // behaviour to split the "scentences"
98
+ // naturally.
79
99
then : function ( value ) {
80
100
value = this . sanitize ( value ) ;
81
101
this . add ( "(" + value + ")" ) ;
82
102
return ( this ) ;
83
103
} ,
84
104
105
+ // And because we can't start with
106
+ // "then" function, we create an alias
107
+ // to be used as the first function
108
+ // of the chain.
85
109
find : function ( value ) {
86
110
return ( this . then ( value ) ) ;
87
111
} ,
88
112
113
+ // Maybe is used to add values with ?
89
114
maybe : function ( value ) {
90
115
value = this . sanitize ( value ) ;
91
116
this . add ( "(" + value + ")?" ) ;
92
117
return ( this ) ;
93
118
} ,
94
119
120
+ // Any character any times
95
121
anything : function ( ) {
96
122
this . add ( "(.*)" ) ;
97
123
return ( this ) ;
98
124
} ,
99
125
100
- endOfLine : function ( ) {
101
- this . add ( "$" ) ;
102
- return ( this ) ;
103
- } ,
104
-
126
+ // Anything but these characters
105
127
anythingBut : function ( value ) {
106
128
value = this . sanitize ( value ) ;
107
129
this . add ( "([^" + value + "]*)" ) ;
108
130
return ( this ) ;
109
131
} ,
110
132
133
+ // Shorthand function for the
134
+ // String.replace function to
135
+ // give more logical flow if, for
136
+ // example, we're doing multiple
137
+ // replacements on one regexp.
111
138
replace : function ( source , value ) {
112
139
source = source . toString ( ) ;
113
140
return source . replace ( this , value ) ;
141
+ } ,
142
+
143
+
144
+ /// Add regular expression special ///
145
+ /// characters ///
146
+
147
+ // Line break
148
+ lineBreak : function ( ) {
149
+ this . add ( "(\\n|(\\r\\n))" ) ; // Unix + windows CLRF
150
+ return ( this ) ;
151
+ } ,
152
+ // And a shorthand for html-minded
153
+ br : function ( ) {
154
+ return this . lineBreak ( ) ;
155
+ } ,
156
+
157
+ // Tab (duh?)
158
+ tab : function ( ) {
159
+ this . add ( "\\t" ) ;
160
+ return ( this ) ;
161
+ } ,
162
+
163
+ // Any alphanumeric
164
+ word : function ( ) {
165
+ this . add ( "\\w+" ) ;
166
+ return ( this ) ;
167
+ } ,
168
+
169
+ // Any given character
170
+ anyOf : function ( value ) {
171
+ value = this . sanitize ( value ) ;
172
+ this . add ( "[" + value + "]" ) ;
173
+ return ( this ) ;
174
+ } ,
175
+
176
+ // Shorthand
177
+ any : function ( value ) {
178
+ return ( this . anyOf ( value ) ) ;
179
+ } ,
180
+
181
+ // Usage: .range( from, to [, from, to ...] )
182
+ range : function ( ) {
183
+
184
+ var value = "[" ;
185
+ console . log ( arguments ) ;
186
+
187
+ for ( var _from = 0 ; _from < arguments . length ; _from += 2 ) {
188
+ var _to = _from + 1 ;
189
+ if ( arguments . length <= to ) break ;
190
+
191
+ var from = this . sanitize ( arguments [ _from ] ) ;
192
+ var to = this . sanitize ( arguments [ _to ] ) ;
193
+
194
+ value += from + "-" + to ;
195
+ }
196
+
197
+ value += "]" ;
198
+
199
+ this . add ( value ) ;
200
+ return ( this ) ;
201
+ } ,
202
+
203
+
204
+ /// Modifiers ///
205
+
206
+ // Modifier abstraction
207
+ addModifier : function ( modifier ) {
208
+ if ( this . _modifiers . indesxOf ( modifier ) == - 1 ) {
209
+ this . _modifiers += modifier ;
210
+ }
211
+ this . add ( "" ) ;
212
+ return ( this ) ;
213
+ } ,
214
+ removeModifier : function ( modifier ) {
215
+ this . _modifiers = this . _modifiers . replace ( modifier , "" ) ;
216
+ this . add ( "" ) ;
217
+ return ( this ) ;
218
+ } ,
219
+
220
+ // Case-insensitivity modifier
221
+ withAnyCase : function ( enable ) {
222
+
223
+ if ( enable != false ) this . addModifier ( "i" ) ;
224
+ else this . removeModifier ( "i" ) ;
225
+
226
+ this . add ( "" ) ;
227
+ return ( this ) ;
228
+
229
+ } ,
230
+
231
+ // Default behaviour is with "g" modifier,
232
+ // so we can turn this another way around
233
+ // than other modifiers
234
+ stopAtFirst : function ( enable ) {
235
+
236
+ if ( enable != false ) this . removeModifier ( "g" ) ;
237
+ else this . addModifier ( "g" ) ;
238
+
239
+ this . add ( "" ) ;
240
+ return ( this ) ;
241
+
242
+ } ,
243
+
244
+ // Multiline, also reversed
245
+ searhOneLine : function ( enable ) {
246
+
247
+ if ( enable != false ) this . removeModifier ( "m" ) ;
248
+ else this . addModifier ( "m" ) ;
249
+
250
+ this . add ( "" ) ;
251
+ return ( this ) ;
252
+
253
+ } ,
254
+
255
+
256
+
257
+ /// Loops ///
258
+
259
+ multiple : function ( value ) {
260
+ // Use expression or string
261
+ value = value . source ? value . source : this . sanitize ( value ) ;
262
+ switch ( value . substr ( - 1 ) ) {
263
+ case "*" :
264
+ case "+" :
265
+ break ;
266
+ default :
267
+ value += "+" ;
268
+ }
269
+ this . add ( value ) ;
270
+ return ( this ) ;
114
271
}
115
-
272
+
116
273
} ;
117
274
118
275
0 commit comments