fop_lang 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +49 -18
- data/lib/fop/nodes.rb +3 -1
- data/lib/fop/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 711af4fad2907616e057049dcb84bce16ffdb381b3601a387dc7260cc16057d3
|
4
|
+
data.tar.gz: 40da554ca0cb21f275748593921bdc616dcf8cdfe5d4fa31494f588f2c25d66c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0253a6446b88b6de112f00a95c81f1d5f710a859998e5b3d8df21d64caecc8e013ab1edec2b68047558f8053ce135b60082078f85db1f7aa16a93b86aa487093
|
7
|
+
data.tar.gz: c7f0cb0387df52a3ea121e3e91a35e0444afe8862aabe054ec433d889955ee9221129fd0c07f7e36d6c71e6b9ab33f30cd16264447dfda24d29babb671112140
|
data/README.md
CHANGED
@@ -1,44 +1,75 @@
|
|
1
1
|
# fop_lang
|
2
2
|
|
3
|
-
Fop is
|
3
|
+
Fop (Filter and OPperations language) is an experimental, tiny expression language in the vein of awk and sed. This is a Ruby implementation. It is useful for simultaneously matching and transforming text input.
|
4
4
|
|
5
|
-
##
|
5
|
+
## Release Number Example
|
6
|
+
|
7
|
+
This example takes in GitHub branch names, decides if they're release branches, and if so, increments the version number.
|
6
8
|
|
7
9
|
```ruby
|
8
|
-
f = Fop(
|
10
|
+
f = Fop('release-{N}.{N+1}.{N=0}')
|
9
11
|
|
10
|
-
puts f.apply(
|
11
|
-
=>
|
12
|
+
puts f.apply('release-5.99.1')
|
13
|
+
=> 'release-5.100.0'
|
12
14
|
|
13
|
-
puts f.apply(
|
15
|
+
puts f.apply('release-5')
|
14
16
|
=> nil
|
15
17
|
# doesn't match the pattern
|
16
18
|
```
|
17
19
|
|
20
|
+
## Anatomy of a Fop expression
|
21
|
+
|
22
|
+
`Text Literal {Operation}`
|
23
|
+
|
24
|
+
The above expression contains the only two parts of Fop (except for the wildcard and escape characters).
|
25
|
+
|
26
|
+
**Text Literals**
|
27
|
+
|
28
|
+
A text literal works how it sounds: the input must match it exactly. The only exception is the `*` (wildcard) character, which matches 0 or more of anything. Wildcards can be used anywhere except inside `{...}` (operations).
|
29
|
+
|
30
|
+
If `\` (escape) is used before the special characters `*`, `{` or `}`, then that character is treated like a text literal. It's recommended to use single-quoted Ruby strings with Fop expressions that so you don't need to double-escape.
|
31
|
+
|
32
|
+
**Operations**
|
33
|
+
|
34
|
+
Operations are the interesting part of Fop, and are specified between `{` and `}`. An Operation can consist of one to three parts:
|
35
|
+
|
36
|
+
1. Matching character class (required): Defines what characters the operation will match and operate on.
|
37
|
+
* `N` is the numeric class and will match one or more digits.
|
38
|
+
* `A` is the alpha class and will match one or more letters (lower or upper case).
|
39
|
+
* `W` is the word class and matches alphanumeric chars and underscores.
|
40
|
+
* `*` is the wildcard class and greedily matches everything after it.
|
41
|
+
3. Operator (optional): What to do to the matching characters.
|
42
|
+
* `=` Replace the matching character(s) with the given argument. If no argument is given, drop the matching chars.
|
43
|
+
* `+` Perform addition on the matching number and the argument (`N` only).
|
44
|
+
* `-` Subtract the argument from the matching number (`N` only).
|
45
|
+
5. Operator argument (required for some operators): meaning varies by operator.
|
46
|
+
|
47
|
+
## More Examples
|
48
|
+
|
18
49
|
```ruby
|
19
|
-
f = Fop(
|
50
|
+
f = Fop('release-{N=5}.{N+1}.{N=0}')
|
20
51
|
|
21
|
-
puts f.apply(
|
22
|
-
=>
|
52
|
+
puts f.apply('release-4.99.1')
|
53
|
+
=> 'release-5.100.0'
|
23
54
|
```
|
24
55
|
|
25
56
|
```ruby
|
26
|
-
f = Fop(
|
57
|
+
f = Fop('release-*{N=5}.{N+100}.{N=0}')
|
27
58
|
|
28
|
-
puts f.apply(
|
29
|
-
=>
|
59
|
+
puts f.apply('release-foo-4.100.1')
|
60
|
+
=> 'release-foo-5.200.0'
|
30
61
|
```
|
31
62
|
|
32
63
|
```ruby
|
33
|
-
f = Fop(
|
64
|
+
f = Fop('release-{N=5}.{N+1}.{N=0}{*=}')
|
34
65
|
|
35
|
-
puts f.apply(
|
36
|
-
=>
|
66
|
+
puts f.apply('release-4.100.1.foo.bar')
|
67
|
+
=> 'release-5.101.0'
|
37
68
|
```
|
38
69
|
|
39
70
|
```ruby
|
40
|
-
f = Fop(
|
71
|
+
f = Fop('{W=version}-{N=5}.{N+1}.{N=0}')
|
41
72
|
|
42
|
-
puts f.apply(
|
43
|
-
=>
|
73
|
+
puts f.apply('release-4.100.1')
|
74
|
+
=> 'version-5.101.0'
|
44
75
|
```
|
data/lib/fop/nodes.rb
CHANGED
@@ -15,6 +15,7 @@ module Fop
|
|
15
15
|
Match = Struct.new(:wildcard, :tokens) do
|
16
16
|
NUM = "N".freeze
|
17
17
|
WORD = "W".freeze
|
18
|
+
ALPHA = "A".freeze
|
18
19
|
WILD = "*".freeze
|
19
20
|
BLANK = "".freeze
|
20
21
|
|
@@ -37,7 +38,8 @@ module Fop
|
|
37
38
|
@regex =
|
38
39
|
case @match
|
39
40
|
when NUM then Regexp.new((wildcard ? ".*?" : "^") + "[0-9]+")
|
40
|
-
when WORD then Regexp.new((wildcard ? ".*?" : "^") + "
|
41
|
+
when WORD then Regexp.new((wildcard ? ".*?" : "^") + "\\w+")
|
42
|
+
when ALPHA then Regexp.new((wildcard ? ".*?" : "^") + "[a-zA-Z]+")
|
41
43
|
when WILD then /.*/
|
42
44
|
else raise ParserError, "Unknown match type '#{@match}'"
|
43
45
|
end
|
data/lib/fop/version.rb
CHANGED