Better assignments: class separation for sections, simple assignments, etc.
This commit is contained in:
parent
275c85db79
commit
34875be8c7
@ -1,5 +1,5 @@
|
|||||||
name: spec
|
name: spec
|
||||||
version: 0.1.0
|
version: 0.2.0
|
||||||
|
|
||||||
authors:
|
authors:
|
||||||
- Philippe Pittoli <karchnu@karchnu.fr>
|
- Philippe Pittoli <karchnu@karchnu.fr>
|
||||||
|
112
src/spec.cr
112
src/spec.cr
@ -1,10 +1,39 @@
|
|||||||
require "./parser"
|
require "./parser"
|
||||||
|
|
||||||
class Specs
|
class Specs
|
||||||
property assignments : Hash(String, String)
|
class StringContainer
|
||||||
|
property value : String
|
||||||
|
|
||||||
|
def initialize(@value)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class LongStringContainer
|
||||||
|
property value : String
|
||||||
|
|
||||||
|
def initialize(@value)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class SectionContainer
|
||||||
|
property value : String
|
||||||
|
|
||||||
|
def initialize(@value)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class ArrayContainer
|
||||||
|
property value : Array(String)
|
||||||
|
|
||||||
|
def initialize(@value)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
property assignments : Hash(String, StringContainer | LongStringContainer | SectionContainer | ArrayContainer)
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@assignments = Hash(String, String).new
|
@assignments = Hash(String, StringContainer | LongStringContainer | SectionContainer | ArrayContainer).new
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_hashglob(tree)
|
def parse_hashglob(tree)
|
||||||
@ -89,7 +118,7 @@ class Specs
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@assignments[name] = value
|
@assignments[name] = ArrayContainer.new value.split("\n")
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_freetextblock(tree)
|
def parse_freetextblock(tree)
|
||||||
@ -106,7 +135,7 @@ class Specs
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@assignments[name] = value
|
@assignments[name] = SectionContainer.new value
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_glob (tree)
|
def parse_glob (tree)
|
||||||
@ -165,7 +194,7 @@ class Specs
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@assignments[name] = value
|
@assignments[name] = StringContainer.new value
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_tree(tree)
|
def parse_tree(tree)
|
||||||
@ -186,22 +215,75 @@ class Specs
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def rewrite
|
def replace_string_obj (v : StringContainer | SectionContainer | LongStringContainer)
|
||||||
# replaces all occurences of %{variable} by the content of @assignments[variable]
|
reg = /%\{([^}]*)\}/
|
||||||
@assignments.map do |k, v|
|
|
||||||
reg = /%\{([^}]*)\}/
|
|
||||||
|
|
||||||
while v =~ reg
|
|
||||||
|
|
||||||
x = reg.match(v)
|
value = v.value
|
||||||
unless x.nil?
|
|
||||||
var = x.captures()
|
|
||||||
|
|
||||||
v = v.gsub "%{#{var[0]}}", "#{@assignments[var[0]]}"
|
while value =~ reg
|
||||||
|
|
||||||
|
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}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@assignments[k] = v
|
v.value = value
|
||||||
|
|
||||||
|
value = v.value
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def replace_array_obj (v : ArrayContainer)
|
||||||
|
reg = /%\{([^}]*)\}/
|
||||||
|
|
||||||
|
str_array = v.value
|
||||||
|
|
||||||
|
newarray = Array(String).new
|
||||||
|
|
||||||
|
str_array.each do |value|
|
||||||
|
tmp = value
|
||||||
|
while tmp =~ reg
|
||||||
|
|
||||||
|
x = reg.match(tmp)
|
||||||
|
unless x.nil?
|
||||||
|
var = x.captures()
|
||||||
|
|
||||||
|
replacement_object = @assignments[var[0]]
|
||||||
|
case replacement_object
|
||||||
|
when StringContainer
|
||||||
|
replacement_value = replacement_object.value
|
||||||
|
tmp = tmp.gsub "%{#{var[0]}}", "#{replacement_value}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
newarray.push tmp
|
||||||
|
end
|
||||||
|
|
||||||
|
v.value = newarray
|
||||||
|
end
|
||||||
|
|
||||||
|
def rewrite
|
||||||
|
# replaces all occurences of %{variable} by the content of @assignments[variable]
|
||||||
|
@assignments.map do |k, v|
|
||||||
|
case v
|
||||||
|
when StringContainer
|
||||||
|
replace_string_obj v
|
||||||
|
when SectionContainer
|
||||||
|
replace_string_obj v
|
||||||
|
when LongStringContainer
|
||||||
|
replace_string_obj v
|
||||||
|
when ArrayContainer
|
||||||
|
replace_array_obj v
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user