Pure OCaml Yaml 1.2 reader and writer using Bytesrw

simplify parse stack

Changed files
+9 -38
lib
-1
lib/dune
···
(name yamlrw)
(public_name yamlrw)
(libraries bytesrw)
-
(flags (:standard -w -37-69))
(modules
; Core types
position
-22
lib/parser.ml
···
type state =
| Stream_start
| Implicit_document_start
-
| Document_start
| Document_content
| Document_content_done (* After parsing a node, check for unexpected content *)
| Document_end
-
| Block_node
-
| Block_node_or_indentless_sequence
-
| Flow_node
| Block_sequence_first_entry
| Block_sequence_entry
| Indentless_sequence_entry
···
| Flow_mapping_first_key
| Flow_mapping_key
| Flow_mapping_value
-
| Flow_mapping_empty_value
| End
type t = {
scanner : Scanner.t;
mutable state : state;
mutable states : state list; (** State stack *)
-
mutable marks : Span.t list; (** Mark stack for span tracking *)
mutable version : (int * int) option;
mutable tag_directives : (string * string) list;
mutable current_token : Token.spanned option;
···
scanner;
state = Stream_start;
states = [];
-
marks = [];
version = None;
tag_directives = [
("!", "!");
···
| _ ->
parse_document_start t ~implicit:true)
-
| Document_start ->
-
parse_document_start t ~implicit:false
-
| Document_content ->
if check t (function
| Token.Version_directive _ | Token.Tag_directive _
···
| Document_end ->
parse_document_end t
-
| Block_node ->
-
parse_node t ~block:true ~indentless:false
-
-
| Block_node_or_indentless_sequence ->
-
parse_node t ~block:true ~indentless:true
-
-
| Flow_node ->
-
parse_node t ~block:false ~indentless:false
-
| Block_sequence_first_entry ->
t.state <- Block_sequence_entry;
parse_block_sequence_entry t
···
| Flow_mapping_value ->
parse_flow_mapping_value t ~empty:false
-
-
| Flow_mapping_empty_value ->
-
parse_flow_mapping_value t ~empty:true
| End ->
let span = Span.point Position.initial in
+9 -15
lib/scanner.ml
···
type indent = {
indent : int;
needs_block_end : bool;
-
sequence : bool; (** true if this is a sequence indent *)
}
type t = {
input : Input.t;
-
mutable tokens : Token.spanned Queue.t;
+
tokens : Token.spanned Queue.t;
mutable token_number : int;
mutable tokens_taken : int;
mutable stream_started : bool;
···
mutable leading_whitespace : bool; (** True when at start of line (only whitespace seen) *)
mutable document_has_content : bool; (** True if we've emitted content tokens in current document *)
mutable adjacent_value_allowed_at : Position.t option; (** Position where adjacent : is allowed *)
-
mutable pending_value : bool; (** True if we've emitted a KEY and are waiting for VALUE *)
mutable flow_mapping_stack : bool list; (** Stack of whether each flow level is a mapping *)
}
···
leading_whitespace = true; (* Start at beginning of stream *)
document_has_content = false;
adjacent_value_allowed_at = None;
-
pending_value = false;
flow_mapping_stack = [];
}
···
end
(** Roll the indentation level *)
-
let roll_indent t col ~sequence =
+
let roll_indent t col =
if t.flow_level = 0 && col > current_indent t then begin
-
t.indent_stack <- { indent = col; needs_block_end = true; sequence } :: t.indent_stack;
+
t.indent_stack <- { indent = col; needs_block_end = true } :: t.indent_stack;
true
end else
false
···
if not t.allow_simple_key then
Error.raise_at (Input.mark t.input) Block_sequence_disallowed;
let col = column t in
-
if roll_indent t col ~sequence:true then begin
+
if roll_indent t col then begin
let span = Span.point (Input.mark t.input) in
emit t span Token.Block_sequence_start
end
···
if not t.allow_simple_key then
Error.raise_at (Input.mark t.input) Expected_key;
let col = column t in
-
if roll_indent t col ~sequence:false then begin
+
if roll_indent t col then begin
let span = Span.point (Input.mark t.input) in
emit t span Token.Block_mapping_start
end
···
end;
let span = Span.make ~start ~stop:(Input.mark t.input) in
-
emit t span Token.Key;
-
t.pending_value <- true (* We've emitted a KEY, now waiting for VALUE *)
+
emit t span Token.Key
and check_value t =
(* : followed by whitespace in block, or whitespace/flow indicator in flow, or adjacent value *)
···
if insert_pos >= Array.length tokens then
Queue.add key_token t.tokens;
t.token_number <- t.token_number + 1;
-
t.pending_value <- true; (* We've inserted a KEY token, now waiting for VALUE *)
(* Roll indent for implicit block mapping *)
if t.flow_level = 0 then begin
let col = sk.sk_position.column in
-
if roll_indent t col ~sequence:false then begin
+
if roll_indent t col then begin
let span = Span.point sk.sk_position in
(* Insert block mapping start before key *)
let bm_token = { Token.token = Token.Block_mapping_start; span } in
···
if not t.allow_simple_key then
Error.raise_at (Input.mark t.input) Expected_key;
let col = column t in
-
if roll_indent t col ~sequence:false then begin
+
if roll_indent t col then begin
let span = Span.point (Input.mark t.input) in
emit t span Token.Block_mapping_start
end
···
skip_whitespace_and_comment t;
let span = Span.make ~start ~stop:(Input.mark t.input) in
-
emit t span Token.Value;
-
t.pending_value <- false (* We've emitted a VALUE, no longer pending *)
+
emit t span Token.Value
and fetch_anchor_or_alias t ~is_alias =
save_simple_key t;