Commit 17019850a1513d773ccc0a7e9d6f545ea930dfb7
1 parent
7cbc8ced
change the principle of reading by get bytes instead of whole line
Showing
1 changed file
with
117 additions
and
28 deletions
Show diff stats
file_processor_io/yaml/yaml_parser.anubis
| @@ -12,6 +12,81 @@ read tools/streams.anubis | @@ -12,6 +12,81 @@ read tools/streams.anubis | ||
| 12 | read tools/string.anubis | 12 | read tools/string.anubis |
| 13 | read system/string.anubis | 13 | read system/string.anubis |
| 14 | 14 | ||
| 15 | +type YAML_Parser_Info: | ||
| 16 | + yaml_p_info(Int indent_size, Int level). | ||
| 17 | + | ||
| 18 | +define Maybe(String) | ||
| 19 | + _read_key | ||
| 20 | + ( | ||
| 21 | + Stream current_line, | ||
| 22 | + List(Word8) so_far | ||
| 23 | + )= | ||
| 24 | + failure. | ||
| 25 | + | ||
| 26 | + if read_byte(current_line) is | ||
| 27 | + { | ||
| 28 | + failure then failure, | ||
| 29 | + success(char) then | ||
| 30 | + if in_word then | ||
| 31 | + | ||
| 32 | + else if in_single_quote then | ||
| 33 | + else if | ||
| 34 | + if char | ||
| 35 | + . | ||
| 36 | + | ||
| 37 | +define Maybe(String) | ||
| 38 | + read_key | ||
| 39 | + ( | ||
| 40 | + Stream current_line | ||
| 41 | + )= | ||
| 42 | + _read_key(current_line, []). | ||
| 43 | + | ||
| 44 | +define Maybe(YAML_Key_Value) | ||
| 45 | + read_key_value | ||
| 46 | + ( | ||
| 47 | + Stream s, | ||
| 48 | + YAML_Parser_Info p_info, | ||
| 49 | + Stream current_line, | ||
| 50 | + )= | ||
| 51 | + if read_key(current_line) is | ||
| 52 | + { | ||
| 53 | + failure then failure, | ||
| 54 | + success(key) then failure | ||
| 55 | +// if read_value(s, p_info, current_line) is | ||
| 56 | +// { | ||
| 57 | +// failure then failure, | ||
| 58 | +// success(value) then | ||
| 59 | +// success(key_value(key, var(value))) | ||
| 60 | +// } | ||
| 61 | + } | ||
| 62 | + . | ||
| 63 | + | ||
| 64 | +public define List(YAML_Key_Value) | ||
| 65 | + get_yaml_map | ||
| 66 | + ( | ||
| 67 | + Stream s, | ||
| 68 | + YAML_Parser_Info p_info, | ||
| 69 | + Stream current_line, | ||
| 70 | + List(YAML_Key_Value) so_far | ||
| 71 | + )= | ||
| 72 | + if read_key_value(s, p_info, current_line) is | ||
| 73 | + { | ||
| 74 | + failure then | ||
| 75 | + println("can't read key value"); | ||
| 76 | + reverse(so_far), | ||
| 77 | + success(key_value) then | ||
| 78 | + if read_line(s) is | ||
| 79 | + { | ||
| 80 | + failure then reverse(so_far), | ||
| 81 | + success(line) then | ||
| 82 | + with line_s = make_stream(line), | ||
| 83 | + //skip all blanks of begin of line | ||
| 84 | + skip_blanks(line_s); | ||
| 85 | + with indent = current_column(line_s), | ||
| 86 | + get_yaml_map(s, p_info, line_s, [key_value . so_far]) | ||
| 87 | + } | ||
| 88 | + }. | ||
| 89 | + | ||
| 15 | public define Maybe(YAML_Document) | 90 | public define Maybe(YAML_Document) |
| 16 | get_yaml_document_content | 91 | get_yaml_document_content |
| 17 | ( | 92 | ( |
| @@ -20,35 +95,49 @@ public define Maybe(YAML_Document) | @@ -20,35 +95,49 @@ public define Maybe(YAML_Document) | ||
| 20 | )= | 95 | )= |
| 21 | if read_line(s) is | 96 | if read_line(s) is |
| 22 | { | 97 | { |
| 23 | - failure then success(doc), | ||
| 24 | - success(_line) then | ||
| 25 | - with line = trim(_line), | ||
| 26 | - //check for comment | ||
| 27 | - if start_with(line, "#") then | ||
| 28 | - //println("skipping comment ["+line+"]"); | ||
| 29 | - get_yaml_document_content(s, doc) | ||
| 30 | - //check for blank line | ||
| 31 | - else if length(line) = 0 then | ||
| 32 | - //println("skipping blank line"); | ||
| 33 | - get_yaml_document_content(s, doc) | ||
| 34 | - //check for item of the list | ||
| 35 | - else if start_with(line, "- ") then | ||
| 36 | - with indent = count_blank(_line), | ||
| 37 | - println("list item indent["+indent+"]["+line+"]"); | ||
| 38 | - get_yaml_document_content(s, doc) | ||
| 39 | - //check for entry of the map | ||
| 40 | - else | ||
| 41 | - if find(":", line, 0) is | ||
| 42 | - { | ||
| 43 | - failure then | ||
| 44 | - println("unrecognize line ["+line+"]"); | ||
| 45 | - get_yaml_document_content(s, doc), | ||
| 46 | - success(pos) then | ||
| 47 | - with indent = count_blank(_line), | 98 | + failure then success(doc) |
| 99 | + success(line) then | ||
| 100 | + with line_s = make_stream(line), | ||
| 101 | + | ||
| 102 | + //skip all blanks of begin of line | ||
| 103 | + skip_blanks(line_s); | ||
| 104 | + with indent = current_column(line_s), | ||
| 105 | + | ||
| 106 | + if read_byte(line_s) is | ||
| 107 | + { | ||
| 108 | + failure then | ||
| 109 | + println("skipping blank line"); | ||
| 110 | + get_yaml_document_content(s, doc), //blank line | ||
| 111 | + | ||
| 112 | + success(c) then | ||
| 113 | + //check for comment | ||
| 114 | + if c = '#' then | ||
| 115 | + //println("skipping comment ["+line+"]"); | ||
| 116 | + get_yaml_document_content(s, doc) | ||
| 117 | + | ||
| 118 | + //check for item of the list | ||
| 119 | + else if c = '-' then | ||
| 120 | + println("list item indent["+indent+"]["+line+"]"); | ||
| 121 | + get_yaml_document_content(s, doc) | ||
| 122 | + | ||
| 123 | + //check for entry of the map | ||
| 124 | + else | ||
| 48 | println("map entry indent["+indent+"]["+line+"]"); | 125 | println("map entry indent["+indent+"]["+line+"]"); |
| 49 | - get_yaml_document_content(s, doc) | ||
| 50 | - } | ||
| 51 | - }. | 126 | + with list_key_val = get_yaml_map(s, yaml_p_info(indent, 0), line_s, []), |
| 127 | + | ||
| 128 | + if list_key_val is | ||
| 129 | + { | ||
| 130 | + [] then | ||
| 131 | + println("Can't get map entries"); | ||
| 132 | + get_yaml_document_content(s, doc), | ||
| 133 | + [_ . _] then | ||
| 134 | + since doc is yaml_document(nodes), | ||
| 135 | + nodes <- [map(var(list_key_val)) . *nodes]; | ||
| 136 | + get_yaml_document_content(s, yaml_document(nodes)) | ||
| 137 | + } | ||
| 138 | + | ||
| 139 | + } //read_byte | ||
| 140 | + }. //read_line | ||
| 52 | 141 | ||
| 53 | public define Maybe(YAML_Document) | 142 | public define Maybe(YAML_Document) |
| 54 | get_yaml_document | 143 | get_yaml_document |