Class: Strling::Simply
- Inherits:
-
Object
- Object
- Strling::Simply
- Defined in:
- lib/strling/simply.rb
Overview
Fluent API for building STRling patterns
Instance Attribute Summary collapse
-
#node ⇒ Object
readonly
Returns the value of attribute node.
Class Method Summary collapse
- .any_of(*patterns) ⇒ Object
- .capture(pattern) ⇒ Object
- .digit ⇒ Object
- .end ⇒ Object
- .may(pattern) ⇒ Object
-
.merge(*patterns) ⇒ Object
Class methods (Constructors).
- .start ⇒ Object
-
.to_node(pattern) ⇒ Object
Helper to convert input to Node.
Instance Method Summary collapse
- #any_of(*others) ⇒ Object
- #capture ⇒ Object
-
#initialize(node) ⇒ Simply
constructor
A new instance of Simply.
- #may ⇒ Object
-
#merge(*others) ⇒ Object
Instance methods (Chainable).
- #times(min, max = nil) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(node) ⇒ Simply
Returns a new instance of Simply.
12 13 14 |
# File 'lib/strling/simply.rb', line 12 def initialize(node) @node = node end |
Instance Attribute Details
#node ⇒ Object (readonly)
Returns the value of attribute node.
10 11 12 |
# File 'lib/strling/simply.rb', line 10 def node @node end |
Class Method Details
.any_of(*patterns) ⇒ Object
43 44 45 46 |
# File 'lib/strling/simply.rb', line 43 def self.any_of(*patterns) branches = patterns.map { |p| to_node(p) } new(Strling::Core::Alt.new(branches)) end |
.capture(pattern) ⇒ Object
23 24 25 |
# File 'lib/strling/simply.rb', line 23 def self.capture(pattern) new(Strling::Core::Group.new(true, to_node(pattern))) end |
.digit ⇒ Object
31 32 33 |
# File 'lib/strling/simply.rb', line 31 def self.digit new(Strling::Core::Esc.new('d')) end |
.end ⇒ Object
39 40 41 |
# File 'lib/strling/simply.rb', line 39 def self.end new(Strling::Core::Anchor.new('End')) end |
.may(pattern) ⇒ Object
27 28 29 |
# File 'lib/strling/simply.rb', line 27 def self.may(pattern) new(Strling::Core::Quant.new(to_node(pattern), 0, 1, 'Greedy')) end |
.merge(*patterns) ⇒ Object
Class methods (Constructors)
18 19 20 21 |
# File 'lib/strling/simply.rb', line 18 def self.merge(*patterns) parts = patterns.map { |p| to_node(p) } new(Strling::Core::Seq.new(parts)) end |
.start ⇒ Object
35 36 37 |
# File 'lib/strling/simply.rb', line 35 def self.start new(Strling::Core::Anchor.new('Start')) end |
.to_node(pattern) ⇒ Object
Helper to convert input to Node
49 50 51 52 53 54 55 56 57 |
# File 'lib/strling/simply.rb', line 49 def self.to_node(pattern) if pattern.is_a?(Simply) pattern.node elsif pattern.is_a?(String) Strling::Core::Lit.new(pattern) else raise ArgumentError, "Expected Simply instance or String, got #{pattern.class}" end end |
Instance Method Details
#any_of(*others) ⇒ Object
74 75 76 77 |
# File 'lib/strling/simply.rb', line 74 def any_of(*others) branches = [@node] + others.map { |p| Simply.to_node(p) } Simply.new(Strling::Core::Alt.new(branches)) end |
#capture ⇒ Object
66 67 68 |
# File 'lib/strling/simply.rb', line 66 def capture Simply.new(Strling::Core::Group.new(true, @node)) end |
#may ⇒ Object
70 71 72 |
# File 'lib/strling/simply.rb', line 70 def may Simply.new(Strling::Core::Quant.new(@node, 0, 1, 'Greedy')) end |
#merge(*others) ⇒ Object
Instance methods (Chainable)
61 62 63 64 |
# File 'lib/strling/simply.rb', line 61 def merge(*others) parts = [@node] + others.map { |p| Simply.to_node(p) } Simply.new(Strling::Core::Seq.new(parts)) end |