Check for simple bugs in the spec file.
This commit is contained in:
parent
82b8b9a265
commit
dc53be3544
109
src/spec.cr
109
src/spec.cr
@ -219,25 +219,39 @@ class Specs
|
|||||||
reg = /%\{([^}]*)\}/
|
reg = /%\{([^}]*)\}/
|
||||||
|
|
||||||
|
|
||||||
|
is_missing_references = false
|
||||||
value = v.value
|
value = v.value
|
||||||
|
|
||||||
while value =~ reg
|
begin
|
||||||
|
while value =~ reg
|
||||||
|
|
||||||
x = reg.match(value)
|
x = reg.match(value)
|
||||||
unless x.nil?
|
unless x.nil?
|
||||||
var = x.captures()
|
var = x.captures()
|
||||||
|
|
||||||
replacement_object = @assignments[var[0]]
|
if var[0]? && @assignments[var[0]]?
|
||||||
case replacement_object
|
replacement_object = @assignments[var[0]]
|
||||||
when StringContainer
|
case replacement_object
|
||||||
replacement_value = replacement_object.value
|
when StringContainer
|
||||||
value = value.gsub "%{#{var[0]}}", "#{replacement_value}"
|
replacement_value = replacement_object.value
|
||||||
|
value = value.gsub "%{#{var[0]}}", "#{replacement_value}"
|
||||||
|
end
|
||||||
|
else
|
||||||
|
is_missing_references = true
|
||||||
|
raise "cannot find variable \033[31m#{var[0]}\033[00m"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
v.value = value
|
||||||
|
|
||||||
|
value = v.value
|
||||||
end
|
end
|
||||||
|
rescue e
|
||||||
|
puts "#{e}"
|
||||||
|
end
|
||||||
|
|
||||||
v.value = value
|
if is_missing_references
|
||||||
|
raise "there are missing references in the document: fix it"
|
||||||
value = v.value
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -248,26 +262,41 @@ class Specs
|
|||||||
|
|
||||||
newarray = Array(String).new
|
newarray = Array(String).new
|
||||||
|
|
||||||
|
is_missing_references = false
|
||||||
|
|
||||||
str_array.each do |value|
|
str_array.each do |value|
|
||||||
tmp = value
|
tmp = value
|
||||||
while tmp =~ reg
|
begin
|
||||||
|
|
||||||
x = reg.match(tmp)
|
while tmp =~ reg
|
||||||
unless x.nil?
|
|
||||||
var = x.captures()
|
|
||||||
|
|
||||||
replacement_object = @assignments[var[0]]
|
x = reg.match(tmp)
|
||||||
case replacement_object
|
unless x.nil?
|
||||||
when StringContainer
|
var = x.captures()
|
||||||
replacement_value = replacement_object.value
|
|
||||||
tmp = tmp.gsub "%{#{var[0]}}", "#{replacement_value}"
|
if var[0]? && @assignments[var[0]]?
|
||||||
|
replacement_object = @assignments[var[0]]
|
||||||
|
case replacement_object
|
||||||
|
when StringContainer
|
||||||
|
replacement_value = replacement_object.value
|
||||||
|
tmp = tmp.gsub "%{#{var[0]}}", "#{replacement_value}"
|
||||||
|
end
|
||||||
|
else
|
||||||
|
is_missing_references = true
|
||||||
|
raise "cannot find variable \033[31m#{var[0]}\033[00m"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
rescue e
|
||||||
|
puts "#{e}"
|
||||||
end
|
end
|
||||||
|
|
||||||
newarray.push tmp
|
newarray.push tmp
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if is_missing_references
|
||||||
|
raise "there are missing references in the document: fix it"
|
||||||
|
end
|
||||||
v.value = newarray
|
v.value = newarray
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -287,11 +316,49 @@ class Specs
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.has_file_bugs? (content : String)
|
||||||
|
lines = content.split "\n"
|
||||||
|
|
||||||
|
previous_line_started_with_tab = false
|
||||||
|
|
||||||
|
line_count = 1
|
||||||
|
|
||||||
|
has_bugs = false
|
||||||
|
|
||||||
|
# 1. detect lacking "\n" after a block of text
|
||||||
|
lines.each do |line|
|
||||||
|
if previous_line_started_with_tab
|
||||||
|
unless /^$|^\t/.match(line)
|
||||||
|
puts "there is a problem line #{line_count}: lacking a carriage return"
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
line_count += 1
|
||||||
|
|
||||||
|
previous_line_started_with_tab = /^\t/.match(line)
|
||||||
|
|
||||||
|
if line_count - 1 == lines.size
|
||||||
|
if previous_line_started_with_tab
|
||||||
|
puts "there is a problem with end of file: lacking a carriage return"
|
||||||
|
has_bugs = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
has_bugs
|
||||||
|
end
|
||||||
|
|
||||||
def self.parse(file_name : String) : Specs | Nil
|
def self.parse(file_name : String) : Specs | Nil
|
||||||
begin
|
begin
|
||||||
content = File.read(file_name)
|
content = File.read(file_name)
|
||||||
content = content.rchop
|
content = content.rchop
|
||||||
|
|
||||||
|
# XXX: detect simple and known grammar bugs
|
||||||
|
if Specs.has_file_bugs? (content)
|
||||||
|
raise "the file #{file_name} has bugs"
|
||||||
|
end
|
||||||
|
|
||||||
specs = Specs.new
|
specs = Specs.new
|
||||||
tree = Pegasus::Generated.process(content)
|
tree = Pegasus::Generated.process(content)
|
||||||
specs.parse_tree tree
|
specs.parse_tree tree
|
||||||
|
Reference in New Issue
Block a user