Skip to content

Commit

Permalink
move capture validation logic to method
Browse files Browse the repository at this point in the history
  • Loading branch information
rkh committed May 12, 2013
1 parent 3abf3fb commit d8808ce
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/mustermann/ast/validation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ def self.validate(ast)
translate(Object, :splat) {}
translate(:node) { t(payload) }
translate(Array) { each { |p| t(p)} }
translate(:capture, :variable, :named_splat) { t.check_name(name) }

translate(:capture, :variable, :named_splat) do
# @raises [Mustermann::CompileError] if name is not acceptable
# @!visibility private
def check_name(name)
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 name == "splat" or name == "captures"
raise CompileError, "can't use the same capture name twice" if t.names.include? name
t.names << name
raise CompileError, "can't use the same capture name twice" if names.include? name
names << name
end

# @return [Array<String>] list of capture names in tree
Expand Down

0 comments on commit d8808ce

Please sign in to comment.