class Mustermann::AST::Validation

Checks the AST for certain validations, like correct capture names.

Internally a poor man's visitor (abusing translator to not have to implement a visitor). @!visibility private

Public Class Methods

validate(ast) click to toggle source

Runs validations.

@param [Mustermann::AST::Node] ast to be validated @return [Mustermann::AST::Node] the validated ast @raise [Mustermann::AST::CompileError] if validation fails @!visibility private

# File lib/mustermann/ast/validation.rb, line 16
def self.validate(ast)
  new.translate(ast)
  ast
end

Public Instance Methods

check_name(name, forbidden: []) click to toggle source

@raise [Mustermann::CompileError] if name is not acceptable @!visibility private

# File lib/mustermann/ast/validation.rb, line 29
def check_name(name, forbidden: [])
  raise CompileError, "capture name can't be empty" if name.nil? or name.empty?
  raise CompileError, "capture name must start with underscore or lower case letter" unless name =~ /^[a-z_]/
  raise CompileError, "capture name can't be #{name}" if Array(forbidden).include? name
  raise CompileError, "can't use the same capture name twice" if names.include? name
  names << name
end
names() click to toggle source

@return [Array<String>] list of capture names in tree @!visibility private

# File lib/mustermann/ast/validation.rb, line 39
def names
  @names ||= []
end