3
0

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

This commit is contained in:
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 "./src/specfileparser"
require "./src/specparser"
recipe_file_name = "some-non-existant-file"
@ -18,39 +18,53 @@ end
options = Hash(String,String).new
options["someoptionexample"] = "option"
specs = SpecFileParser.parse recipe_file_name, options
begin
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
sectioncontainer = SpecFileParser::Section.new "val"
sectioncontainer = SpecParser::Section.new "val"
begin
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
puts "(OK) #{e}"
end
begin
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
puts "(OK) #{e}"
end
arraycontainer = SpecFileParser::ArrayContainer.new Array(String).new.push "value"
arraycontainer = SpecParser::ArrayContainer.new Array(String).new.push "value"
begin
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
puts "(OK) #{e}"
end
begin
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
puts "(NOT OK) #{e}"
end

View File

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

View File

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