s/SpecFileParser/SpecParser/, non-catch'ed exceptions, parsing data and files

master
Philippe PITTOLI 2019-09-27 13:54:10 +02:00
parent 7eb76ae8c9
commit 2906e03ff7
3 changed files with 56 additions and 38 deletions

View File

@ -1,5 +1,5 @@
require "option_parser" require "option_parser"
require "./src/specfileparser" require "./src/specparser"
recipe_file_name = "some-non-existant-file" recipe_file_name = "some-non-existant-file"
@ -18,39 +18,53 @@ end
options = Hash(String,String).new options = Hash(String,String).new
options["someoptionexample"] = "option" options["someoptionexample"] = "option"
specs = SpecFileParser.parse recipe_file_name, options begin
pp! specs specs = SpecParser.parse_file recipe_file_name, options
pp! specs
rescue e
puts "(OK) #{e}"
end
data = "
name: the-name
version: the-version
dirname: %{name}-%{version}
"
begin
specs = SpecParser.parse data, options
pp! specs
rescue e
puts "(NOT OK) #{e}"
end
# low level stuff # low level stuff
sectioncontainer = SpecFileParser::Section.new "val" sectioncontainer = SpecParser::Section.new "val"
begin begin
puts sectioncontainer.as_s puts sectioncontainer.as_s
puts "(NOT OK) SpecFileParser::Section should not accept .as_s" puts "(NOT OK) SpecParser::Section should not accept .as_s"
rescue e rescue e
puts "(OK) #{e}" puts "(OK) #{e}"
end end
begin begin
pp! sectioncontainer.as_a_or_s pp! sectioncontainer.as_a_or_s
puts "(NOT OK) SpecFileParser::Section should not accept .as_a_or_s" puts "(NOT OK) SpecParser::Section should not accept .as_a_or_s"
rescue e rescue e
puts "(OK) #{e}" puts "(OK) #{e}"
end end
arraycontainer = SpecFileParser::ArrayContainer.new Array(String).new.push "value" arraycontainer = SpecParser::ArrayContainer.new Array(String).new.push "value"
begin begin
puts arraycontainer.as_s puts arraycontainer.as_s
puts "(NOT OK) SpecFileParser::ArrayContainer should not accept .as_s" puts "(NOT OK) SpecParser::ArrayContainer should not accept .as_s"
rescue e rescue e
puts "(OK) #{e}" puts "(OK) #{e}"
end end
begin begin
pp! arraycontainer.as_a_or_s pp! arraycontainer.as_a_or_s
puts "(OK) SpecFileParser::ArrayContainer should accept .as_a_or_s" puts "(OK) SpecParser::ArrayContainer should accept .as_a_or_s"
rescue e rescue e
puts "(NOT OK) #{e}" puts "(NOT OK) #{e}"
end end

View File

@ -1,5 +1,5 @@
name: specfileparser name: specparser
version: 0.4.5 version: 0.5.0
authors: authors:
- Philippe Pittoli <karchnu@karchnu.fr> - Philippe Pittoli <karchnu@karchnu.fr>

View File

@ -1,5 +1,8 @@
class SpecFileParser class SpecParser
class Exception < ::Exception
end
macro incompatible_methods(*names) macro incompatible_methods(*names)
{% for name in names %} {% for name in names %}
@ -40,7 +43,7 @@ class SpecFileParser
end end
class LongStringContainer class LongStringContainer
SpecFileParser.incompatible_methods as_s, as_a_or_s SpecParser.incompatible_methods as_s, as_a_or_s
property value : String property value : String
def as_s_or_ls : String def as_s_or_ls : String
@ -52,7 +55,7 @@ class SpecFileParser
end end
class ArrayContainer class ArrayContainer
SpecFileParser.incompatible_methods as_s, as_s_or_ls SpecParser.incompatible_methods as_s, as_s_or_ls
property value : Array(String) property value : Array(String)
def as_a_or_s : Array(String) def as_a_or_s : Array(String)
@ -64,7 +67,7 @@ class SpecFileParser
end end
class Section class Section
SpecFileParser.incompatible_methods as_s, as_a_or_s, as_s_or_ls SpecParser.incompatible_methods as_s, as_a_or_s, as_s_or_ls
property name : String property name : String
property options : Array(String) property options : Array(String)
property content : Hash(String, StringContainer | ArrayContainer | LongStringContainer) property content : Hash(String, StringContainer | ArrayContainer | LongStringContainer)
@ -365,30 +368,31 @@ class SpecFileParser
end end
# The only function to use from outside. #
# public functions
#
def self.parse(file_name : String, options : Hash(String, String) | Nil = nil) : SpecFileParser | Nil def self.parse_file(file_name : String, options : Hash(String, String) | Nil = nil) : SpecParser
begin content = File.read(file_name)
content = File.read(file_name) content = content.rchop
content = content.rchop
specs = SpecFileParser.new specs = parse content, options
unless options.nil?
options.each do |opt, val|
specs.assignments[opt] = StringContainer.new val
end
end
specs.parse_lines (content.split("\n"))
specs.rewrite
specs
rescue e
puts "Exception: #{e}"
nil
end
end end
def self.parse(data : String, options : Hash(String, String) | Nil = nil) : SpecParser
specs = SpecParser.new
unless options.nil?
options.each do |opt, val|
specs.assignments[opt] = StringContainer.new val
end
end
specs.parse_lines (data.split("\n"))
specs.rewrite
specs
rescue e
raise Exception.new "unexpected parser error: #{e}"
end
end end