regexp_parser 0.1.5 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5d3544709ae86e53530ef7cbe037dcab48690c2c
4
- data.tar.gz: fb96ce92b21303ec32e88103d708a508c80588fc
3
+ metadata.gz: 6ef4ef1e296f8e15fe5316a5603c15e96446cb45
4
+ data.tar.gz: 0430451d4d0fb874dbdcc123d017d2867856d891
5
5
  SHA512:
6
- metadata.gz: d4191f06120c4e5abe9f0fc6b7eb466aab12a61b37c4cb7c33204f2c2dba9907866d42317320cf3916146f59e12ff3c027db76300321342b1615270d7085ade9
7
- data.tar.gz: 8d0173c9d02e4e26eae291e0dbedd48fb7d9995629b761f47ab696ad9b2955efa542c6a3360fa711f27c0d7f4cc5df6f293f89685e47aacd4e9a2f8eab6d336f
6
+ metadata.gz: 05706f3dbe8f1fe9684ea63abf9a0b0e4ff354146bddd488a72fb9bd5352979123bb6d6adc75624dae33c39f1d41e34b76c33cc3706f1ee6f6a989b8e2e259f1
7
+ data.tar.gz: d427c8ec82b4f955f47b1f27043a23604f654ff25244160fa416b0331c587cca0f2c4f1e9f47ee898934b753391e03cfa1ca6a3eb88b04c34efc255adf6391b8
data/ChangeLog CHANGED
@@ -1,3 +1,40 @@
1
+ Sun Oct 5 19:58:17 2014 Ammar Ali <ammarabuali@gmail.com>
2
+
3
+ * Fixed test and gem building rake tasks and extracted the gem
4
+ specification from the Rakefile into a .gemspec file.
5
+
6
+ * Added syntax files for missing ruby 2.x versions. These do not add
7
+ extra syntax support, they just make the gem work with the newer
8
+ ruby versions.
9
+
10
+ * Added .travis.yml to project root.
11
+
12
+ * README:
13
+
14
+ * Removed note purporting runtime support for ruby 1.8.6.
15
+
16
+ * Added a section identifying the main unsupported syntax features.
17
+
18
+ * Added sections for Testing and Building
19
+
20
+ * Added badges for gem version, Travis CI, and code climate.
21
+
22
+ Sat Oct 4 13:46:24 2014 Ammar Ali <ammarabuali@gmail.com>
23
+
24
+ * Updated README, fixing broken examples, and converting it from
25
+ a rdoc file to Github's flavor of Markdown.
26
+
27
+ * Fixed a parser bug where an alternation sequence that contained
28
+ nested expressions was incorrectly being appended to the parent
29
+ expression when the nesting was exited. e.g. in /a|(b)c/, c was
30
+ appended to the root.
31
+
32
+ Wed May 7 5:52:37 2014 Ammar Ali <ammarabuali@gmail.com>
33
+
34
+ * Fixed a bug where character types were not being correctly scanned
35
+ within character sets. e.g. in [\d], two tokens were scanned; one
36
+ for the backslash '\' and one for the 'd'
37
+
1
38
  Tue Jan 14 13:14:24 2014 Ammar Ali <ammarabuali@gmail.com>
2
39
 
3
40
  * Released version 0.1.5, with a correct ChangeLog.
data/README.md ADDED
@@ -0,0 +1,443 @@
1
+ # Regexp::Parser [![Gem Version](https://badge.fury.io/rb/regexp_parser.svg)](http://badge.fury.io/rb/regexp_parser) [![Build Status](https://secure.travis-ci.org/ammar/regexp_parser.png?branch=master)](http://travis-ci.org/ammar/regexp_parser) [![Code Climate](https://codeclimate.com/github/ammar/regexp_parser.png)](https://codeclimate.com/github/ammar/regexp_parser/badges)
2
+
3
+ A ruby library to help with lexing, parsing, and transforming regular expressions.
4
+
5
+ * Multilayered
6
+ * A scanner based on [ragel](http://www.complang.org/ragel/)
7
+ * A lexer that produces a "stream" of tokens
8
+ * A parser that produces a "tree" of Regexp::Expression objects (OO API)
9
+ * Supports ruby 1.8, 1.9, and all but one of the 2.x expressions [See Scanner Syntax](#scanner-syntax)
10
+ * Supports ruby 1.8, 1.9, 2.0, and 2.1 runtimes.
11
+
12
+ _For an example of regexp_parser in use, see the [meta_re project](https://github.com/ammar/meta_re)_
13
+
14
+ ---
15
+ ## Requirements
16
+
17
+ * ruby '1.8.7'..'2.1.3'
18
+ * ragel, but only if you want to build the gem or work on the scanner
19
+
20
+
21
+ _Note: See the .travis.yml file for covered versions._
22
+
23
+ ---
24
+ ## Install
25
+
26
+ `gem install regexp_parser`
27
+
28
+ ---
29
+ ## Usage
30
+
31
+ ```ruby
32
+ # require the gem, then call one of:
33
+ require 'regexp_parser'
34
+
35
+ # The Scanner
36
+ Regexp::Scanner.scan regexp
37
+
38
+ # The Lexer
39
+ Regexp::Lexer.scan regexp
40
+
41
+ # Or the Parser
42
+ Regexp::Parser.parse regexp
43
+ ```
44
+
45
+ _All three can either return their results or take a block to perform further handling._
46
+
47
+ ---
48
+ ## Components
49
+
50
+ ### Scanner
51
+ A ragel generated scanner that recognizes the cumulative syntax of both
52
+ supported flavors. Breaks the expression's text into tokens, including
53
+ their type, token, text, and start/end offsets within the original
54
+ pattern.
55
+
56
+ #### Example
57
+ The following scans the given pattern and prints out the type, token, text and
58
+ start/end offsets for each token found.
59
+
60
+ ```ruby
61
+ require 'regexp_parser'
62
+
63
+ Regexp::Scanner.scan /(ab?(cd)*[e-h]+)/ do |type, token, text, ts, te|
64
+ puts "type: #{type}, token: #{token}, text: '#{text}' [#{ts}..#{te}]"
65
+ end
66
+
67
+ # output
68
+ # type: group, token: capture, text: '(' [0..1]
69
+ # type: literal, token: literal, text: 'ab' [1..3]
70
+ # type: quantifier, token: zero_or_one, text: '?' [3..4]
71
+ # type: group, token: capture, text: '(' [4..5]
72
+ # type: literal, token: literal, text: 'cd' [5..7]
73
+ # type: group, token: close, text: ')' [7..8]
74
+ # type: quantifier, token: zero_or_more, text: '*' [8..9]
75
+ # type: set, token: open, text: '[' [9..10]
76
+ # type: set, token: range, text: 'e-h' [10..13]
77
+ # type: set, token: close, text: ']' [13..14]
78
+ # type: quantifier, token: one_or_more, text: '+' [14..15]
79
+ # type: group, token: close, text: ')' [15..16]
80
+ ```
81
+
82
+ A one-liner that returns an array of the textual parts of the given pattern:
83
+
84
+ ```ruby
85
+ Regexp::Scanner.scan( /(cat?([bhm]at)){3,5}/ ).map {|token| token[2]}
86
+ #=> ["(", "cat", "?", "(", "[", "b", "h", "m", "]", "at", ")", ")", "{3,5}"]
87
+ ```
88
+
89
+
90
+ #### Notes
91
+ * The scanner performs basic syntax error checking, like detecting missing
92
+ balancing punctuation and premature end of pattern. Flavor validity checks
93
+ are performed in the lexer.
94
+
95
+ * If the input is a ruby Regexp object, the scanner calls #source on it to
96
+ get its string representation. #source does not include the options of
97
+ expression (m, i, and x) To include the options the scan, #to_s should
98
+ be called on the Regexp before passing it to the scanner, or any of the
99
+ higher layers.
100
+
101
+ * To keep the scanner simple(r) and fairly reusable for other purposes, it
102
+ does not perform lexical analysis on the tokens, sticking to the task
103
+ of tokenizing and leaving lexical analysis upto to the lexer.
104
+
105
+
106
+ ---
107
+ ### Syntax
108
+ Defines the supported tokens for a specific engine implementation (aka a
109
+ flavor). Syntax classes act as lookup tables, and are layered to create
110
+ flavor variations. Syntax only comes into play in the lexer.
111
+
112
+ #### Example
113
+ The following instantiates the syntax for Ruby 1.9 and checks a couple of its
114
+ implementations features, and then does the same for Ruby 1.8:
115
+
116
+ ```ruby
117
+ require 'regexp_parser'
118
+
119
+ ruby_19 = Regexp::Syntax.new 'ruby/1.9'
120
+ ruby_19.implements? :quantifier, :zero_or_one # => true
121
+ ruby_19.implements? :quantifier, :zero_or_one_reluctant # => true
122
+ ruby_19.implements? :quantifier, :zero_or_one_possessive # => true
123
+
124
+ ruby_18 = Regexp::Syntax.new 'ruby/1.8'
125
+ ruby_18.implements? :quantifier, :zero_or_one # => true
126
+ ruby_18.implements? :quantifier, :zero_or_one_reluctant # => true
127
+ ruby_18.implements? :quantifier, :zero_or_one_possessive # => false
128
+ ```
129
+
130
+
131
+ #### Notes
132
+ * Variatiions on a token, for example a named group with < and > vs one with a
133
+ pair of single quotes, are specified with an underscore followed by two
134
+ characters appended to the base token. In the previous named group example,
135
+ the tokens would be :named_ab (angle brackets) and :named_sq (single quotes).
136
+ These variations are normalized by the syntax to :named.
137
+
138
+
139
+ ---
140
+ ### Lexer
141
+ Sits on top of the scanner and performs lexical analysis on the tokens that
142
+ it emits. Among its tasks are breaking quantified literal runs, collecting the
143
+ emitted token structures into an array of Token objects, calculating their
144
+ nesting depth, normalizing tokens for the parser, and checkng if the tokens
145
+ are implemented by the given syntax flavor.
146
+
147
+ Tokens are Struct objects, with a few helper methods; #next, #previous, #offsets
148
+ and #length.
149
+
150
+ #### Example
151
+ The following example scans the given pattern, checks it against the ruby 1.8
152
+ syntax, and prints the token objects' text.
153
+
154
+ ```ruby
155
+ require 'regexp_parser'
156
+
157
+ Regexp::Lexer.scan /a?(b(c))*[d]+/ do |token|
158
+ puts "#{' ' * token.level}#{token.text}"
159
+ end
160
+
161
+ # output
162
+ # a
163
+ # ?
164
+ # (
165
+ # b
166
+ # (
167
+ # c
168
+ # )
169
+ # )
170
+ # *
171
+ # [
172
+ # d
173
+ # ]
174
+ # +
175
+ ```
176
+
177
+ A one-liner that returns an array of the textual parts of the given pattern.
178
+ Compare the output with that of the one-liner example of the Scanner; notably
179
+ how the sequence 'cat' is treated.
180
+
181
+ ```ruby
182
+ Regexp::Lexer.scan( /(cat?([b]at)){3,5}/ ).map {|token| token.text}
183
+ #=> ["(", "ca", "t", "?", "(", "[", "b", "]", "at", ")", ")", "{3,5}"]
184
+ ```
185
+
186
+ #### Notes
187
+ * The default syntax is that of the latest released version of ruby.
188
+
189
+ * The lexer performs some basic parsing to determine the depth of the
190
+ emitted tokens. This responsibility might be relegated to the scanner
191
+ in a future release.
192
+
193
+
194
+ ---
195
+ ### Parser
196
+ Sits on top of the lexer and transforms the "stream" of Token objects emitted
197
+ by it into a tree of Expression objects represented by an instance of the
198
+ Expression::Root class. See Expression below for more information.
199
+
200
+ #### Example
201
+
202
+ ```ruby
203
+ require 'regexp_parser'
204
+
205
+ regex = /a?(b)*[c]+/m
206
+
207
+ # using #to_s on the Regexp object to include options. Note that this turns the
208
+ # expression into '(?m-ix:a?(b)*[c]+)', thus the Group::Options in the output
209
+ root = Regexp::Parser.parse( regex.to_s, 'ruby/2.1')
210
+
211
+ root.multiline? # => true (aliased as m?)
212
+ root.case_insensitive? # => false (aliased as i?)
213
+
214
+ # simple tree walking method (depth-first, pre-order)
215
+ def walk(e, depth = 0)
216
+ puts "#{' ' * depth}> #{e.class}"
217
+
218
+ if e.respond_to?(:expressions)
219
+ e.each {|s| walk(s, depth+1) }
220
+ end
221
+ end
222
+
223
+ walk(root)
224
+
225
+ # output
226
+ # > Regexp::Expression::Root
227
+ # > Regexp::Expression::Group::Options
228
+ # > Regexp::Expression::Literal
229
+ # > Regexp::Expression::Group::Capture
230
+ # > Regexp::Expression::Literal
231
+ # > Regexp::Expression::CharacterSet
232
+ ```
233
+
234
+ _Note: quantifiers do not appear in the output because they are members of the
235
+ Expression class. See the next section for details._
236
+
237
+
238
+ ---
239
+ ### Expression
240
+ The base class of all objects returned by the parser, implements most of the
241
+ functions that are common to all expression classes.
242
+
243
+ Each Expression object contains the following members:
244
+
245
+ * **quantifier**: an instance of Expression::Quantifier that holds the details
246
+ of repetition for the Expression. Has a nil value if the expression is not
247
+ quantified.
248
+ * **expressions**: an array, holds the sub-expressions for the expression if it
249
+ is a group or alternation expression. Empty if the expression doesn't have
250
+ sub-expressions.
251
+ * **options**: a hash, holds the keys :i, :m, and :x with a boolean value that
252
+ indicates if the expression has a given option.
253
+
254
+
255
+ Expressions also contain the following members from the scanner/lexer:
256
+
257
+ * **type**: a symbol, denoting the expression type, such as :group, :quantifier
258
+ * **token**: a symbol, for the object's token, or opening token (in the case of
259
+ groups and sets)
260
+ * **text**: a string, the text of the expression (same as token for nesting expressions)
261
+
262
+
263
+ Every expression also has the following methods:
264
+
265
+ * **to_s**: returns the string representation of the expression.
266
+ * **<<**: adds sub-expresions to the expression.
267
+ * **each**: iterates over the expressions sub-expressions, if any.
268
+ * **[]**: access sub-expressions by index.
269
+ * **quantified?**: return true if the expression was followed by a quantifier.
270
+ * **quantity**: returns an array of the expression's min and max repetitions.
271
+ * **greedy?**: returns true if the expression's quantifier is greedy.
272
+ * **reluctant?** or **lazy?**: returns true if the expression's quantifier is
273
+ reluctant.
274
+ * **possessive?**: returns true if the expression's quantifier is possessive.
275
+ * **multiline?** or **m?**: returns true if the expression has the m option
276
+ * **case_insensitive?** or **ignore_case?** or **i?**: returns true if the expression
277
+ has the i option
278
+ * **free_spacing?** or **extended?** or **x?**: returns true if the expression has the x
279
+ option
280
+
281
+
282
+ A special expression class **Expression::Sequence** is used to hold the
283
+ expressions of a branch within an **Expression::Alternation** expression. For
284
+ example, the expression 'bat|cat|hat' would result in an alternation with 3
285
+ sequences, one for each possible alternative.
286
+
287
+
288
+ ## Scanner Syntax
289
+ The following syntax elements are supported by the scanner.
290
+
291
+ - Alternation: a|b|c, etc.
292
+ - Anchors: ^, $, \b, etc.
293
+ - Character Classes _(aka Sets)_: [abc], [^\]]
294
+ - Character Types: \d, \H, \s, etc.
295
+ - Escape Sequences: \t, \+, \?, etc.
296
+ - Grouped Expressions
297
+ - Assertions
298
+ - Lookahead: (?=abc)
299
+ - Negative Lookahead: (?!abc)
300
+ - Lookabehind: (?<=abc)
301
+ - Negative Lookbehind: (?<\!abc)
302
+ - Atomic: (?>abc)
303
+ - Back-references:
304
+ - Named: \k<name>
305
+ - Nest Level: \k<n-1>
306
+ - Numbered: \k<1>
307
+ - Relative: \k<-2>
308
+ - Capturing: (abc)
309
+ - Comment: (?# comment)
310
+ - Named: (?<name>abc)
311
+ - Options: (?mi-x:abc)
312
+ - Passive: (?:abc)
313
+ - Sub-expression Calls: \g<name>, \g<1>
314
+ - Literals: abc, def?, etc.
315
+ - POSIX classes: [:alpha:], [:print:], etc.
316
+ - Quantifiers
317
+ - Greedy: ?, *, +, {m,M}
318
+ - Reluctant: ??, *?, +?, {m,M}?
319
+ - Possessive: ?+, *+, ++, {m,M}+
320
+ - String Escapes
321
+ - Control: \C-C, \cD, etc.
322
+ - Hex: \x20, \x{701230}, etc.
323
+ - Meta: \M-c, \M-\C-C etc.
324
+ - Octal: \0, \01, \012
325
+ - Unicode: \uHHHH, \u{H+ H+}
326
+ - Traditional Back-references: \1 thru \9
327
+ - Unicode Properties:
328
+ - Age: \p{Age=2.1}, \P{age=5.2}, etc.
329
+ - Classes: \p{Alpha}, \P{Space}, etc.
330
+ - Derived Properties: \p{Math}, \P{Lowercase}, etc.
331
+ - General Categories: \p{Lu}, \P{Cs}, etc.
332
+ - Scripts: \p{Arabic}, \P{Hiragana}, etc.
333
+ - Simple Properties: \p{Dash}, \p{Extender}, etc.
334
+
335
+
336
+ ### Missing Features
337
+
338
+ The following were added by the Onigmo regular expression library used by
339
+ ruby 2.x and are not currently recognized by the scanner:
340
+
341
+ - Planned for support
342
+ - Conditional Expressions: (?(cond)yes-subexp), (?(cond)yes-subexp|no-subexp)
343
+ - Negative POSIX Brackets: [:^alpha:], [:^digit:]
344
+ - New Character Set Options: d, a, and u _[see](https://github.com/k-takata/Onigmo/blob/master/doc/RE#L234)_
345
+ - Not planned for support
346
+ - Keep: \K _(not enabled for ruby syntax)_
347
+ - Quotes: \Q...\E _(perl and java syntax only) [see](https://github.com/k-takata/Onigmo/blob/master/doc/RE#L452)_
348
+ - Capture History: (?@...), (?@<name>...) _(not enabled for ruby syntax) [see](https://github.com/k-takata/Onigmo/blob/master/doc/RE#L499)_
349
+
350
+
351
+ See something else missing? Please submit an [issue](https://github.com/ammar/regexp_parser/issues)
352
+
353
+ _**Note**: Attempting to process expressions with any of the missing syntax features will
354
+ cause an error._
355
+
356
+
357
+ ## Testing
358
+ To run the tests simply run rake from the root directory, as 'test' is the default task.
359
+
360
+ In addition to the main test task, which runs all tests, there are also component specific test
361
+ tasks, which only run the tests for one component at a time. These are:
362
+
363
+ * test:scanner
364
+ * test:lexer
365
+ * test:parser
366
+ * test:expression
367
+ * test:syntax
368
+
369
+ _A special task 'test:full' generatees the scanner's code from the ragel source files and
370
+ runs all the tests. This requires ragel to be installed._
371
+
372
+
373
+ The tests use ruby's test_unit, so they can also be run with:
374
+
375
+ ```
376
+ ruby test/test_all.rb
377
+ ```
378
+
379
+ This is useful when there is a need to focus on specific test files, for example:
380
+
381
+ ```
382
+ ruby test/scanner/test_properties.rb
383
+ ```
384
+
385
+
386
+ ## Building
387
+ Building the scanner and the gem requires [ragel](http://www.complang.org/ragel/) to be
388
+ installed. The build tasks will automatically invoke the 'ragel:rb' task to generate the
389
+ ruby scanner code.
390
+
391
+
392
+ The project uses the standard rubygems package tasks:
393
+
394
+
395
+ To build, run:
396
+ ```
397
+ rake build
398
+ ```
399
+
400
+ To install, run:
401
+ ```
402
+ rake install
403
+ ```
404
+
405
+
406
+ ## References
407
+ Documentation and books used while working on this project.
408
+
409
+
410
+ #### Ruby Flavors
411
+ * Oniguruma Regular Expressions [link](http://www.geocities.jp/kosako3/oniguruma/doc/RE.txt)
412
+ * Read Ruby > Regexps [link](https://github.com/runpaint/read-ruby/blob/master/src/regexps.xml)
413
+
414
+
415
+ #### Regular Expressions
416
+ * Mastering Regular Expressions, By Jeffrey E.F. Friedl (2nd Edition) [book](http://oreilly.com/catalog/9781565922570/)
417
+ * Regular Expression Flavor Comparison [link](http://www.regular-expressions.info/refflavors.html)
418
+ * Enumerating the strings of regular languages [link](http://www.cs.dartmouth.edu/~doug/nfa.ps.gz)
419
+
420
+
421
+ #### Unicode
422
+ * Unicode Explained, By Jukka K. Korpela. [book](http://oreilly.com/catalog/9780596101213)
423
+ * Unicode Derived Properties [link](http://www.unicode.org/Public/UNIDATA/DerivedCoreProperties.txt)
424
+ * Unicode Property Aliases [link](http://www.unicode.org/Public/UNIDATA/PropertyAliases.txt)
425
+ * Unicode Regular Expressions [link](http://www.unicode.org/reports/tr18/)
426
+ * Unicode Standard Annex #44 [link](http://www.unicode.org/reports/tr44/)
427
+
428
+ ## Thanks
429
+ This work is based on and inspired by the hard work and ideas of many people,
430
+ directly or indirectly. The following are only a few of those that should be
431
+ thanked.
432
+
433
+ * Adrian Thurston, for developing [ragel](http://www.complang.org/ragel/).
434
+ * Caleb Clausen, for feedback, which inspired this, valuable insights on structuring the parser,
435
+ and lots of [cool code](http://github.com/coatl).
436
+ * Jan Goyvaerts, for his [excellent resource](http://www.regular-expressions.info) on regular expressions.
437
+ * Run Paint Run Run, for his work on [Read Ruby](https://github.com/runpaint/read-ruby)
438
+ * Yukihiro Matsumoto, of course! For "The Ruby", of course!
439
+
440
+
441
+ ---
442
+ ##### Copyright
443
+ _Copyright (c) 2010-2014 Ammar Ali. See LICENSE file for details._