Class: Strling::Simply

Inherits:
Object
  • Object
show all
Defined in:
lib/strling/simply.rb

Overview

Fluent API for building STRling patterns

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#nodeObject (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

.digitObject



31
32
33
# File 'lib/strling/simply.rb', line 31

def self.digit
  new(Strling::Core::Esc.new('d'))
end

.endObject



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

.startObject



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

#captureObject



66
67
68
# File 'lib/strling/simply.rb', line 66

def capture
  Simply.new(Strling::Core::Group.new(true, @node))
end

#mayObject



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

#times(min, max = nil) ⇒ Object



79
80
81
82
# File 'lib/strling/simply.rb', line 79

def times(min, max = nil)
  max = min if max.nil?
  Simply.new(Strling::Core::Quant.new(@node, min, max, 'Greedy'))
end

#to_sObject



84
85
86
87
88
# File 'lib/strling/simply.rb', line 84

def to_s
  ir = Strling::IR::Compiler.compile(@node)
  flags = Strling::Core::Flags.new
  Strling::Emitters::PCRE2.emit(ir, flags)
end