From dc53be3544a755dcfc2817bc1e184304c0274863 Mon Sep 17 00:00:00 2001
From: Philippe PITTOLI
Date: Fri, 2 Aug 2019 03:48:34 +0200
Subject: [PATCH] Check for simple bugs in the spec file.
---
src/spec.cr | 109 ++++++++++++++++++++++++++++++++++++++++++----------
1 file changed, 88 insertions(+), 21 deletions(-)
diff --git a/src/spec.cr b/src/spec.cr
index d47f099..804c528 100644
--- a/src/spec.cr
+++ b/src/spec.cr
@@ -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