Pure OCaml Yaml 1.2 reader and writer using Bytesrw

simplify parse stack

+9 -38
-1
lib/dune
··· 2 2 (name yamlrw) 3 3 (public_name yamlrw) 4 4 (libraries bytesrw) 5 - (flags (:standard -w -37-69)) 6 5 (modules 7 6 ; Core types 8 7 position
-22
lib/parser.ml
··· 9 9 type state = 10 10 | Stream_start 11 11 | Implicit_document_start 12 - | Document_start 13 12 | Document_content 14 13 | Document_content_done (* After parsing a node, check for unexpected content *) 15 14 | Document_end 16 - | Block_node 17 - | Block_node_or_indentless_sequence 18 - | Flow_node 19 15 | Block_sequence_first_entry 20 16 | Block_sequence_entry 21 17 | Indentless_sequence_entry ··· 30 26 | Flow_mapping_first_key 31 27 | Flow_mapping_key 32 28 | Flow_mapping_value 33 - | Flow_mapping_empty_value 34 29 | End 35 30 36 31 type t = { 37 32 scanner : Scanner.t; 38 33 mutable state : state; 39 34 mutable states : state list; (** State stack *) 40 - mutable marks : Span.t list; (** Mark stack for span tracking *) 41 35 mutable version : (int * int) option; 42 36 mutable tag_directives : (string * string) list; 43 37 mutable current_token : Token.spanned option; ··· 50 44 scanner; 51 45 state = Stream_start; 52 46 states = []; 53 - marks = []; 54 47 version = None; 55 48 tag_directives = [ 56 49 ("!", "!"); ··· 645 638 | _ -> 646 639 parse_document_start t ~implicit:true) 647 640 648 - | Document_start -> 649 - parse_document_start t ~implicit:false 650 - 651 641 | Document_content -> 652 642 if check t (function 653 643 | Token.Version_directive _ | Token.Tag_directive _ ··· 684 674 | Document_end -> 685 675 parse_document_end t 686 676 687 - | Block_node -> 688 - parse_node t ~block:true ~indentless:false 689 - 690 - | Block_node_or_indentless_sequence -> 691 - parse_node t ~block:true ~indentless:true 692 - 693 - | Flow_node -> 694 - parse_node t ~block:false ~indentless:false 695 - 696 677 | Block_sequence_first_entry -> 697 678 t.state <- Block_sequence_entry; 698 679 parse_block_sequence_entry t ··· 736 717 737 718 | Flow_mapping_value -> 738 719 parse_flow_mapping_value t ~empty:false 739 - 740 - | Flow_mapping_empty_value -> 741 - parse_flow_mapping_value t ~empty:true 742 720 743 721 | End -> 744 722 let span = Span.point Position.initial in
+9 -15
lib/scanner.ml
··· 17 17 type indent = { 18 18 indent : int; 19 19 needs_block_end : bool; 20 - sequence : bool; (** true if this is a sequence indent *) 21 20 } 22 21 23 22 type t = { 24 23 input : Input.t; 25 - mutable tokens : Token.spanned Queue.t; 24 + tokens : Token.spanned Queue.t; 26 25 mutable token_number : int; 27 26 mutable tokens_taken : int; 28 27 mutable stream_started : bool; ··· 35 34 mutable leading_whitespace : bool; (** True when at start of line (only whitespace seen) *) 36 35 mutable document_has_content : bool; (** True if we've emitted content tokens in current document *) 37 36 mutable adjacent_value_allowed_at : Position.t option; (** Position where adjacent : is allowed *) 38 - mutable pending_value : bool; (** True if we've emitted a KEY and are waiting for VALUE *) 39 37 mutable flow_mapping_stack : bool list; (** Stack of whether each flow level is a mapping *) 40 38 } 41 39 ··· 55 53 leading_whitespace = true; (* Start at beginning of stream *) 56 54 document_has_content = false; 57 55 adjacent_value_allowed_at = None; 58 - pending_value = false; 59 56 flow_mapping_stack = []; 60 57 } 61 58 ··· 173 170 end 174 171 175 172 (** Roll the indentation level *) 176 - let roll_indent t col ~sequence = 173 + let roll_indent t col = 177 174 if t.flow_level = 0 && col > current_indent t then begin 178 - t.indent_stack <- { indent = col; needs_block_end = true; sequence } :: t.indent_stack; 175 + t.indent_stack <- { indent = col; needs_block_end = true } :: t.indent_stack; 179 176 true 180 177 end else 181 178 false ··· 1261 1258 if not t.allow_simple_key then 1262 1259 Error.raise_at (Input.mark t.input) Block_sequence_disallowed; 1263 1260 let col = column t in 1264 - if roll_indent t col ~sequence:true then begin 1261 + if roll_indent t col then begin 1265 1262 let span = Span.point (Input.mark t.input) in 1266 1263 emit t span Token.Block_sequence_start 1267 1264 end ··· 1302 1299 if not t.allow_simple_key then 1303 1300 Error.raise_at (Input.mark t.input) Expected_key; 1304 1301 let col = column t in 1305 - if roll_indent t col ~sequence:false then begin 1302 + if roll_indent t col then begin 1306 1303 let span = Span.point (Input.mark t.input) in 1307 1304 emit t span Token.Block_mapping_start 1308 1305 end ··· 1321 1318 end; 1322 1319 1323 1320 let span = Span.make ~start ~stop:(Input.mark t.input) in 1324 - emit t span Token.Key; 1325 - t.pending_value <- true (* We've emitted a KEY, now waiting for VALUE *) 1321 + emit t span Token.Key 1326 1322 1327 1323 and check_value t = 1328 1324 (* : followed by whitespace in block, or whitespace/flow indicator in flow, or adjacent value *) ··· 1366 1362 if insert_pos >= Array.length tokens then 1367 1363 Queue.add key_token t.tokens; 1368 1364 t.token_number <- t.token_number + 1; 1369 - t.pending_value <- true; (* We've inserted a KEY token, now waiting for VALUE *) 1370 1365 (* Roll indent for implicit block mapping *) 1371 1366 if t.flow_level = 0 then begin 1372 1367 let col = sk.sk_position.column in 1373 - if roll_indent t col ~sequence:false then begin 1368 + if roll_indent t col then begin 1374 1369 let span = Span.point sk.sk_position in 1375 1370 (* Insert block mapping start before key *) 1376 1371 let bm_token = { Token.token = Token.Block_mapping_start; span } in ··· 1393 1388 if not t.allow_simple_key then 1394 1389 Error.raise_at (Input.mark t.input) Expected_key; 1395 1390 let col = column t in 1396 - if roll_indent t col ~sequence:false then begin 1391 + if roll_indent t col then begin 1397 1392 let span = Span.point (Input.mark t.input) in 1398 1393 emit t span Token.Block_mapping_start 1399 1394 end ··· 1428 1423 skip_whitespace_and_comment t; 1429 1424 1430 1425 let span = Span.make ~start ~stop:(Input.mark t.input) in 1431 - emit t span Token.Value; 1432 - t.pending_value <- false (* We've emitted a VALUE, no longer pending *) 1426 + emit t span Token.Value 1433 1427 1434 1428 and fetch_anchor_or_alias t ~is_alias = 1435 1429 save_simple_key t;