3
0
Fork 0

Check for simple bugs in the spec file.

master
Philippe PITTOLI 2019-08-02 03:48:34 +02:00
parent 82b8b9a265
commit dc53be3544
1 changed files with 88 additions and 21 deletions

View File

@ -219,25 +219,39 @@ class Specs
reg = /%\{([^}]*)\}/
is_missing_references = false
value = v.value
while value =~ reg
begin
while value =~ reg
x = reg.match(value)
unless x.nil?
var = x.captures()
x = reg.match(value)
unless x.nil?
var = x.captures()
replacement_object = @assignments[var[0]]
case replacement_object
when StringContainer
replacement_value = replacement_object.value
value = value.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
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
v.value = value
value = v.value
end
rescue e
puts "#{e}"
end
v.value = value
value = v.value
if is_missing_references
raise "there are missing references in the document: fix it"
end
end
@ -248,26 +262,41 @@ class Specs
newarray = Array(String).new
is_missing_references = false
str_array.each do |value|
tmp = value
while tmp =~ reg
begin
x = reg.match(tmp)
unless x.nil?
var = x.captures()
while tmp =~ reg
replacement_object = @assignments[var[0]]
case replacement_object
when StringContainer
replacement_value = replacement_object.value
tmp = tmp.gsub "%{#{var[0]}}", "#{replacement_value}"
x = reg.match(tmp)
unless x.nil?
var = x.captures()
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
rescue e
puts "#{e}"
end
newarray.push tmp
end
if is_missing_references
raise "there are missing references in the document: fix it"
end
v.value = newarray
end
@ -287,11 +316,49 @@ class Specs
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
begin
content = File.read(file_name)
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
tree = Pegasus::Generated.process(content)
specs.parse_tree tree