From 17019850a1513d773ccc0a7e9d6f545ea930dfb7 Mon Sep 17 00:00:00 2001 From: totoro Date: Wed, 22 Jun 2016 10:28:48 +0200 Subject: [PATCH] change the principle of reading by get bytes instead of whole line --- file_processor_io/yaml/yaml_parser.anubis | 145 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 117 insertions(+), 28 deletions(-) diff --git a/file_processor_io/yaml/yaml_parser.anubis b/file_processor_io/yaml/yaml_parser.anubis index b06670b..89c3878 100644 --- a/file_processor_io/yaml/yaml_parser.anubis +++ b/file_processor_io/yaml/yaml_parser.anubis @@ -12,6 +12,81 @@ read tools/streams.anubis read tools/string.anubis read system/string.anubis +type YAML_Parser_Info: + yaml_p_info(Int indent_size, Int level). + +define Maybe(String) + _read_key + ( + Stream current_line, + List(Word8) so_far + )= + failure. + + if read_byte(current_line) is + { + failure then failure, + success(char) then + if in_word then + + else if in_single_quote then + else if + if char + . + +define Maybe(String) + read_key + ( + Stream current_line + )= + _read_key(current_line, []). + +define Maybe(YAML_Key_Value) + read_key_value + ( + Stream s, + YAML_Parser_Info p_info, + Stream current_line, + )= + if read_key(current_line) is + { + failure then failure, + success(key) then failure +// if read_value(s, p_info, current_line) is +// { +// failure then failure, +// success(value) then +// success(key_value(key, var(value))) +// } + } + . + +public define List(YAML_Key_Value) + get_yaml_map + ( + Stream s, + YAML_Parser_Info p_info, + Stream current_line, + List(YAML_Key_Value) so_far + )= + if read_key_value(s, p_info, current_line) is + { + failure then + println("can't read key value"); + reverse(so_far), + success(key_value) then + if read_line(s) is + { + failure then reverse(so_far), + success(line) then + with line_s = make_stream(line), + //skip all blanks of begin of line + skip_blanks(line_s); + with indent = current_column(line_s), + get_yaml_map(s, p_info, line_s, [key_value . so_far]) + } + }. + public define Maybe(YAML_Document) get_yaml_document_content ( @@ -20,35 +95,49 @@ public define Maybe(YAML_Document) )= if read_line(s) is { - failure then success(doc), - success(_line) then - with line = trim(_line), - //check for comment - if start_with(line, "#") then - //println("skipping comment ["+line+"]"); - get_yaml_document_content(s, doc) - //check for blank line - else if length(line) = 0 then - //println("skipping blank line"); - get_yaml_document_content(s, doc) - //check for item of the list - else if start_with(line, "- ") then - with indent = count_blank(_line), - println("list item indent["+indent+"]["+line+"]"); - get_yaml_document_content(s, doc) - //check for entry of the map - else - if find(":", line, 0) is - { - failure then - println("unrecognize line ["+line+"]"); - get_yaml_document_content(s, doc), - success(pos) then - with indent = count_blank(_line), + failure then success(doc) + success(line) then + with line_s = make_stream(line), + + //skip all blanks of begin of line + skip_blanks(line_s); + with indent = current_column(line_s), + + if read_byte(line_s) is + { + failure then + println("skipping blank line"); + get_yaml_document_content(s, doc), //blank line + + success(c) then + //check for comment + if c = '#' then + //println("skipping comment ["+line+"]"); + get_yaml_document_content(s, doc) + + //check for item of the list + else if c = '-' then + println("list item indent["+indent+"]["+line+"]"); + get_yaml_document_content(s, doc) + + //check for entry of the map + else println("map entry indent["+indent+"]["+line+"]"); - get_yaml_document_content(s, doc) - } - }. + with list_key_val = get_yaml_map(s, yaml_p_info(indent, 0), line_s, []), + + if list_key_val is + { + [] then + println("Can't get map entries"); + get_yaml_document_content(s, doc), + [_ . _] then + since doc is yaml_document(nodes), + nodes <- [map(var(list_key_val)) . *nodes]; + get_yaml_document_content(s, yaml_document(nodes)) + } + + } //read_byte + }. //read_line public define Maybe(YAML_Document) get_yaml_document -- libgit2 0.21.4