hexpress 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/hexpress.rb +97 -0
- data/lib/hexpress/character.rb +26 -0
- data/lib/hexpress/main.rb +5 -0
- data/lib/hexpress/modifier.rb +26 -0
- data/lib/hexpress/modifier/many.rb +25 -0
- data/lib/hexpress/modifier/one.rb +19 -0
- data/lib/hexpress/nested.rb +38 -0
- data/lib/hexpress/nested/find.rb +17 -0
- data/lib/hexpress/nested/matching.rb +31 -0
- data/lib/hexpress/value.rb +28 -0
- data/lib/hexpress/value/ending.rb +22 -0
- data/lib/hexpress/value/except.rb +22 -0
- data/lib/hexpress/value/starting.rb +22 -0
- data/lib/hexpress/value/with.rb +21 -0
- data/lib/hexpress/values.rb +20 -0
- data/lib/hexpress/values/either.rb +24 -0
- data/lib/hexpress/values/range.rb +28 -0
- data/lib/hexpress/verbal_expressions.rb +47 -0
- data/lib/hexpress/version.rb +3 -0
- data/lib/hexpress/web.rb +45 -0
- data/lib/hexpress/wrapped.rb +9 -0
- data/spec/lib/hexpress/main_spec.rb +14 -0
- data/spec/lib/hexpress/modifier/many_spec.rb +19 -0
- data/spec/lib/hexpress/modifier/one_spec.rb +21 -0
- data/spec/lib/hexpress/modifier_spec.rb +49 -0
- data/spec/lib/hexpress/nested/find_spec.rb +13 -0
- data/spec/lib/hexpress/nested/matching_spec.rb +15 -0
- data/spec/lib/hexpress/nested_spec.rb +42 -0
- data/spec/lib/hexpress/value/ending_spec.rb +13 -0
- data/spec/lib/hexpress/value/except_spec.rb +9 -0
- data/spec/lib/hexpress/value/starting_spec.rb +13 -0
- data/spec/lib/hexpress/value/with_spec.rb +9 -0
- data/spec/lib/hexpress/value_spec.rb +25 -0
- data/spec/lib/hexpress/values/either_spec.rb +9 -0
- data/spec/lib/hexpress/values/range_spec.rb +33 -0
- data/spec/lib/hexpress/values_spec.rb +22 -0
- data/spec/lib/hexpress/verbal_expressions_standard.rb +112 -0
- data/spec/lib/hexpress/version_spec.rb +7 -0
- data/spec/lib/hexpress_spec.rb +73 -0
- data/spec/spec_helper.rb +9 -0
- metadata +216 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 59cd6698ba5d1e610f469986e66e765b4c50583b
|
4
|
+
data.tar.gz: 00e04fcce40cc9d0eecc323d0aa02c352b48f586
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9e8d9bd6b4098c41657c3484457213684bbea32c7f19e78e0175e10fa31422f9334775b6ecc0ce6ea9d07432c243ee4a81ada3c92a7779a59b739df9e5e56600
|
7
|
+
data.tar.gz: 40e2fcb76837dd970bbf0492bf333d881d18ad5876f628fa04b012b7eddd6a6c73cfc4bf9e890ba40f36ac42cb47e56edb8318613172ebe8adec3df2b6ac5929
|
data/lib/hexpress.rb
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
class Hexpress
|
2
|
+
CHARACTERS = [:word, :digit, :space]
|
3
|
+
def initialize(&block)
|
4
|
+
@hexpressions = []
|
5
|
+
instance_exec(&block) if block_given?
|
6
|
+
end
|
7
|
+
|
8
|
+
# Matches \w
|
9
|
+
def word
|
10
|
+
add(Character.new(:word))
|
11
|
+
end
|
12
|
+
|
13
|
+
# Matches \d
|
14
|
+
def digit
|
15
|
+
add(Character.new(:digit))
|
16
|
+
end
|
17
|
+
|
18
|
+
# Matches \s
|
19
|
+
def space
|
20
|
+
add(Character.new(:space))
|
21
|
+
end
|
22
|
+
|
23
|
+
# Matches .
|
24
|
+
def any
|
25
|
+
add(Character.new(:any))
|
26
|
+
end
|
27
|
+
|
28
|
+
# Matches (?:.)*
|
29
|
+
def anything
|
30
|
+
many(Character.new(:any), 0)
|
31
|
+
end
|
32
|
+
|
33
|
+
# Matches (?:.)+
|
34
|
+
def something
|
35
|
+
many(Character.new(:any), 1)
|
36
|
+
end
|
37
|
+
|
38
|
+
# Matches \t
|
39
|
+
def tab
|
40
|
+
add(Character.new(:tab))
|
41
|
+
end
|
42
|
+
|
43
|
+
# Matches (?:(?:\n)|(?:\r\n))
|
44
|
+
def line
|
45
|
+
either(Character.new('(?:\n)'), Character.new('(?:\r\n)'))
|
46
|
+
end
|
47
|
+
|
48
|
+
CHARACTERS.each do |character|
|
49
|
+
define_method(character) do
|
50
|
+
add(Character.new(character))
|
51
|
+
end
|
52
|
+
|
53
|
+
define_method("non#{character}") do
|
54
|
+
add(Character.new(character, true))
|
55
|
+
end
|
56
|
+
|
57
|
+
define_method("#{character}s") do
|
58
|
+
many(Character.new(character))
|
59
|
+
end
|
60
|
+
|
61
|
+
define_method("non#{character}s") do
|
62
|
+
many(Character.new(character, true))
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
# This method returns the regular hexpression form of this object.
|
67
|
+
def to_r
|
68
|
+
Regexp.new(to_s)
|
69
|
+
end
|
70
|
+
|
71
|
+
# This method returns the string version of the regexp.
|
72
|
+
def to_s
|
73
|
+
@hexpressions.map(&:to_s).join
|
74
|
+
end
|
75
|
+
|
76
|
+
private
|
77
|
+
|
78
|
+
# This method takes an hexpression and adds it to the hexpression queue
|
79
|
+
# while returning the main object.
|
80
|
+
def add(hexpression)
|
81
|
+
tap do
|
82
|
+
@hexpressions << hexpression
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
require_relative "hexpress/character"
|
88
|
+
require_relative "hexpress/version"
|
89
|
+
require_relative "hexpress/wrapped"
|
90
|
+
require_relative "hexpress/value"
|
91
|
+
require_relative "hexpress/values"
|
92
|
+
require_relative "hexpress/nested"
|
93
|
+
require_relative "hexpress/modifier"
|
94
|
+
|
95
|
+
if defined?(Rails)
|
96
|
+
require_relative "hexpress/main"
|
97
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
class Hexpress
|
2
|
+
class Character
|
3
|
+
attr_reader :upcase
|
4
|
+
def initialize(name, upcase = false)
|
5
|
+
@value = case name
|
6
|
+
when :word then '\w'
|
7
|
+
when :digit then '\d'
|
8
|
+
when :space then '\s'
|
9
|
+
when :any then '.'
|
10
|
+
when :tab then '\t'
|
11
|
+
when :newline then '\n'
|
12
|
+
when :return then '\r'
|
13
|
+
else name
|
14
|
+
end
|
15
|
+
@upcase = upcase
|
16
|
+
end
|
17
|
+
|
18
|
+
def value
|
19
|
+
if upcase then @value.upcase else @value end
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_s
|
23
|
+
value
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
class Hexpress
|
2
|
+
module Modifier
|
3
|
+
OPEN = "(?:"
|
4
|
+
CLOSE = ")"
|
5
|
+
attr_reader :operator
|
6
|
+
|
7
|
+
def open
|
8
|
+
OPEN
|
9
|
+
end
|
10
|
+
|
11
|
+
def close
|
12
|
+
CLOSE
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_s
|
16
|
+
if @value
|
17
|
+
"#{open}#{value}#{close}#{operator}"
|
18
|
+
else
|
19
|
+
operator
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
require_relative "modifier/many"
|
26
|
+
require_relative "modifier/one"
|
@@ -0,0 +1,25 @@
|
|
1
|
+
class Hexpress
|
2
|
+
def many(value = nil, minimum = 1)
|
3
|
+
add_values(Modifier::Many, value, minimum)
|
4
|
+
end
|
5
|
+
alias_method :multiple, :many
|
6
|
+
|
7
|
+
module Modifier
|
8
|
+
class Many
|
9
|
+
include Value
|
10
|
+
include Modifier
|
11
|
+
|
12
|
+
def initialize(value, minimum = 1)
|
13
|
+
@value = value
|
14
|
+
@minimum = minimum
|
15
|
+
end
|
16
|
+
|
17
|
+
def operator
|
18
|
+
case @minimum
|
19
|
+
when 0 then "*"
|
20
|
+
when 1 then "+"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class Hexpress
|
2
|
+
def one(value = nil, &block)
|
3
|
+
add_value(Modifier::One, value, &block)
|
4
|
+
end
|
5
|
+
alias_method :maybe, :one
|
6
|
+
alias_method :possibly, :one
|
7
|
+
|
8
|
+
module Modifier
|
9
|
+
class One
|
10
|
+
include Value
|
11
|
+
include Modifier
|
12
|
+
|
13
|
+
def initialize(value)
|
14
|
+
@value = value
|
15
|
+
@operator = "?"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
class Hexpress
|
2
|
+
private
|
3
|
+
|
4
|
+
def add_nested(hexpression, &block)
|
5
|
+
tap do
|
6
|
+
@hexpressions << hexpression.new(&block)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
module Nested
|
11
|
+
include Wrapped
|
12
|
+
|
13
|
+
def delimiter
|
14
|
+
@delimiter || ""
|
15
|
+
end
|
16
|
+
|
17
|
+
def hexpression
|
18
|
+
if joinable? then join_hexpression else @hexpression end
|
19
|
+
end
|
20
|
+
|
21
|
+
def to_s
|
22
|
+
wrapping(hexpression)
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def join_hexpression
|
28
|
+
@hexpression.join(delimiter)
|
29
|
+
end
|
30
|
+
|
31
|
+
def joinable?
|
32
|
+
@hexpression.respond_to?(:join)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
require_relative "nested/find"
|
38
|
+
require_relative "nested/matching"
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class Hexpress
|
2
|
+
def find(value = nil, &block)
|
3
|
+
value ? add_value(Nested::Find, value) : add_nested(Nested::Find, &block)
|
4
|
+
end
|
5
|
+
alias_method :capture, :find
|
6
|
+
|
7
|
+
module Nested
|
8
|
+
class Find
|
9
|
+
include Nested
|
10
|
+
|
11
|
+
def initialize(value = nil, &block)
|
12
|
+
@hexpression = value || Hexpress.new.instance_eval(&block)
|
13
|
+
@open, @close = "(", ")"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
class Hexpress
|
2
|
+
def matching(&block)
|
3
|
+
add_nested(Nested::Matching, &block)
|
4
|
+
end
|
5
|
+
alias_method :like, :matching
|
6
|
+
|
7
|
+
module Nested
|
8
|
+
class Matching
|
9
|
+
include Nested
|
10
|
+
|
11
|
+
def initialize(&block)
|
12
|
+
@hexpression = Hexpress.new.instance_eval(&block)
|
13
|
+
@open, @close = "[", "]"
|
14
|
+
end
|
15
|
+
|
16
|
+
def join_hexpression
|
17
|
+
@hexpression.map(&method(:escape)).join(delimiter)
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def escape(hexpression)
|
23
|
+
if hexpression.respond_to?(:gsub)
|
24
|
+
Regexp.escape(hexpression)
|
25
|
+
else
|
26
|
+
hexpression
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
class Hexpress
|
2
|
+
private
|
3
|
+
|
4
|
+
def add_value(hexpression, v, &block)
|
5
|
+
add(hexpression.new(block_given? ? Hexpress.new.instance_exec(&block) : v))
|
6
|
+
end
|
7
|
+
|
8
|
+
module Value
|
9
|
+
def value
|
10
|
+
if escapable? then escaped_value else @value end || ""
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def escapable?
|
16
|
+
@value.respond_to?(:gsub)
|
17
|
+
end
|
18
|
+
|
19
|
+
def escaped_value
|
20
|
+
Regexp.escape(@value)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
require_relative "value/ending"
|
26
|
+
require_relative "value/except"
|
27
|
+
require_relative "value/starting"
|
28
|
+
require_relative "value/with"
|
@@ -0,0 +1,22 @@
|
|
1
|
+
class Hexpress
|
2
|
+
def ending(value = nil)
|
3
|
+
add_value(Value::Ending, value)
|
4
|
+
end
|
5
|
+
alias_method :ending_with, :ending
|
6
|
+
alias_method :finally, :ending
|
7
|
+
alias_method :endOfLine, :ending
|
8
|
+
|
9
|
+
module Value
|
10
|
+
class Ending
|
11
|
+
include Value
|
12
|
+
|
13
|
+
def initialize(value = nil)
|
14
|
+
@value = value
|
15
|
+
end
|
16
|
+
|
17
|
+
def to_s
|
18
|
+
"#{value}$"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
class Hexpress
|
2
|
+
def except(value)
|
3
|
+
add_value(Value::Except, value)
|
4
|
+
end
|
5
|
+
alias_method :excluding, :except
|
6
|
+
alias_method :exclude, :except
|
7
|
+
alias_method :without, :except
|
8
|
+
|
9
|
+
module Value
|
10
|
+
class Except
|
11
|
+
include Value
|
12
|
+
|
13
|
+
def initialize(value)
|
14
|
+
@value = value
|
15
|
+
end
|
16
|
+
|
17
|
+
def to_s
|
18
|
+
"^#{value}"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
class Hexpress
|
2
|
+
def starting(value = nil)
|
3
|
+
add_value(Value::Starting, value)
|
4
|
+
end
|
5
|
+
alias_method :begins, :starting
|
6
|
+
alias_method :begin, :starting
|
7
|
+
alias_method :start, :starting
|
8
|
+
|
9
|
+
module Value
|
10
|
+
class Starting
|
11
|
+
include Value
|
12
|
+
|
13
|
+
def initialize(value = nil)
|
14
|
+
@value = value
|
15
|
+
end
|
16
|
+
|
17
|
+
def to_s
|
18
|
+
"^#{value}"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class Hexpress
|
2
|
+
def with(value)
|
3
|
+
add_value(Value::With, value)
|
4
|
+
end
|
5
|
+
alias_method :has, :with
|
6
|
+
alias_method :including, :with
|
7
|
+
|
8
|
+
module Value
|
9
|
+
class With
|
10
|
+
include Value
|
11
|
+
|
12
|
+
def initialize(value)
|
13
|
+
@value = value
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_s
|
17
|
+
"#{value}"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class Hexpress
|
2
|
+
def add_values(hexpression, *vs)
|
3
|
+
add(hexpression.new(*vs))
|
4
|
+
end
|
5
|
+
|
6
|
+
module Values
|
7
|
+
attr_reader :delimiter
|
8
|
+
|
9
|
+
def values
|
10
|
+
@values.map { |v| v.respond_to?(:gsub) ? Regexp.escape(v) : v }
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_s
|
14
|
+
"#{values.join(delimiter)}"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
require_relative "values/either"
|
20
|
+
require_relative "values/range"
|