From c4901f3ad86d4bbc9049d7c2afc830068be41933 Mon Sep 17 00:00:00 2001 From: Philippe PITTOLI Date: Sat, 10 Aug 2019 14:13:52 +0200 Subject: [PATCH] Raise error on list items or free texts outside a list or a section --- src/spec.cr | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/src/spec.cr b/src/spec.cr index bf1cc34..27f7e3b 100644 --- a/src/spec.cr +++ b/src/spec.cr @@ -101,7 +101,7 @@ class Specs end def parse_list(header : String, content : Array(String)) - name = /[a-zA-Z][a-zA-Z0-9]*/.match(header).try &.[0] + name = /[a-zA-Z][a-zA-Z0-9-_]*/.match(header).try &.[0] # puts "new list: #{name}" list = Array(String).new @@ -130,7 +130,7 @@ class Specs end def parse_code_block(header : String, content : Array(String)) - name = /[a-zA-Z][a-zA-Z0-9]*/.match(header).try &.[0] + name = /[a-zA-Z][a-zA-Z0-9-_]*/.match(header).try &.[0] # puts "new code block: #{name}" value = String.build do |str| @@ -159,7 +159,7 @@ class Specs end def parse_section(header : String, content : Array(String)) - results = /^[%]([a-zA-Z][a-zA-Z0-9]*)[ \t]*([^#]*)/.match(header) + results = /^[%]([a-zA-Z][a-zA-Z0-9-_]*)[ \t]*([^#]*)/.match(header) name = results[1]? if results options = results[2]? if results @@ -241,31 +241,23 @@ class Specs end def parse_lines(content : Array(String)) - count = 0 - while line = content.shift? case line - when /^[ \t]*$/ - else - # puts "line #{count}: #{line}" - end - - case line - when /^[a-zA-Z][a-zA-Z0-9]*:[ \t]*([#].*)?$/ + when /^[a-zA-Z][a-zA-Z0-9-_]*:[ \t]*([#].*)?$/ parse_list line, content - when /^[a-zA-Z][a-zA-Z]*:[ \t]+[^#]+/ + when /^[a-zA-Z][a-zA-Z0-9-_]*:[ \t]+[^#]+/ parse_assignment line - when /^[@][a-zA-Z][a-zA-Z]*[ \t]*([#].*)?/ + when /^[@][a-zA-Z][a-zA-Z0-9-_]*[ \t]*([#].*)?/ parse_code_block line, content - when /^[%][a-zA-Z][a-zA-Z]*[ \t]*([#].*)?/ + when /^[%][a-zA-Z][a-zA-Z0-9-_]*[ \t]*([#].*)?/ parse_section line, content when /^[ \t]*#/ # puts "comment" - when /^[ \t]+/ - puts "tab!! should not happen" + when /^[ \t]+$/ + # puts "empty line" + when /^[ \t]+[^ \t#]/ + raise "line starting with spaces or a tabulation outside a list or a section: should not happen" end - - count += 1 end end