Class: Strling::Core::Validator

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

Overview

Validator class for semantic AST validation

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(flags) ⇒ Validator

Returns a new instance of Validator.



30
31
32
33
34
# File 'lib/strling/core/validator.rb', line 30

def initialize(flags)
  @flags = flags
  @capture_count = 0
  @capture_names = Set.new
end

Class Method Details

.validate(node, flags) ⇒ Boolean

Validate an AST node tree

Parameters:

  • node (Node)

    The root AST node to validate

  • flags (Flags)

    The flags for this pattern

Returns:

  • (Boolean)

    True if valid

Raises:



25
26
27
28
# File 'lib/strling/core/validator.rb', line 25

def self.validate(node, flags)
  new(flags).validate_node(node)
  true
end

Instance Method Details

#validate_node(node) ⇒ void

This method returns an undefined value.

Validate a single node and its children

Parameters:

  • node (Node)

    The AST node to validate



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/strling/core/validator.rb', line 40

def validate_node(node)
  case node
  when Alt
    node.branches.each { |b| validate_node(b) }
  when Seq
    node.parts.each { |p| validate_node(p) }
  when CharClass
    node.items.each { |item| validate_class_item(item) }
  when Quant
    validate_node(node.child)
  when Group
    if node.capturing
      @capture_count += 1
      if node.name
        if @capture_names.include?(node.name)
          raise STRlingParseError.new(
            "Duplicate capture group name: #{node.name}",
            0,
            text: '',
            hint: 'Each named capture group must have a unique name'
          )
        end
        @capture_names.add(node.name)
      end
    end
    validate_node(node.body)
  when Backref
    # TODO: Validate backreference numbers and names
  when Look
    validate_node(node.body)
  when Lit, Dot, Anchor
    # No validation needed for these leaf nodes
  end
end