3
0
Fork 0

Better assignments: class separation for sections, simple assignments, etc.

master
Philippe PITTOLI 2019-08-01 20:21:21 +02:00
parent 275c85db79
commit 34875be8c7
2 changed files with 98 additions and 16 deletions

View File

@ -1,5 +1,5 @@
name: spec
version: 0.1.0
version: 0.2.0
authors:
- Philippe Pittoli <karchnu@karchnu.fr>

View File

@ -1,10 +1,39 @@
require "./parser"
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
@assignments = Hash(String, String).new
@assignments = Hash(String, StringContainer | LongStringContainer | SectionContainer | ArrayContainer).new
end
def parse_hashglob(tree)
@ -89,7 +118,7 @@ class Specs
return
end
@assignments[name] = value
@assignments[name] = ArrayContainer.new value.split("\n")
end
def parse_freetextblock(tree)
@ -106,7 +135,7 @@ class Specs
return
end
@assignments[name] = value
@assignments[name] = SectionContainer.new value
end
def parse_glob (tree)
@ -165,7 +194,7 @@ class Specs
return
end
@assignments[name] = value
@assignments[name] = StringContainer.new value
end
def parse_tree(tree)
@ -186,22 +215,75 @@ class Specs
end
end
def rewrite
# replaces all occurences of %{variable} by the content of @assignments[variable]
@assignments.map do |k, v|
reg = /%\{([^}]*)\}/
def replace_string_obj (v : StringContainer | SectionContainer | LongStringContainer)
reg = /%\{([^}]*)\}/
while v =~ reg
x = reg.match(v)
unless x.nil?
var = x.captures()
value = v.value
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
@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