fix processor nested map in seq + enhance dumper

This commit is contained in:
a-Sansara 2018-08-24 03:59:54 +02:00
parent a6d16dee96
commit c9f17fbef9
5 changed files with 196 additions and 89 deletions

View File

@ -26,6 +26,23 @@
- 2.2
- 3.2
- 4.2
#~ !v!Gee.ArrayList type_gee_alobject :
#~ - toto : totovalue1
#~ tata : tatavalue1
#~ titi : 789
#~ tutu : true
#~ - toto : totovalue2
#~ tata : tatavalue2
#~ titi : 456
#~ tutu : false
#~ - toto : totovalue3
#~ tata : tatavalue3
#~ titi : 123
#~ tutu : 1
#~ - toto : totovalue4
#~ tata : tatavalue4
#~ titi : 44
#~ tutu : 0
!v!Gee.ArrayList type_gee_alobject :
- !v!Pluie.Yaml.ExampleChild entry1:
toto : totovalue1

View File

@ -49,7 +49,6 @@ int main (string[] args)
of.action("Yaml.Node", "to_yaml_string");
string yaml = root.to_yaml_string ();
print (yaml);
try {
// an output file in the current working directory
var file = File.new_for_path ( "./tag-generated.yml");
@ -64,6 +63,7 @@ int main (string[] args)
// sum of the bytes of 'text' that already have been written to the stream
written += dos.write (data[written:data.length]);
}
Yaml.Dumper.show_yaml_string (root, true, true, true);
} catch (Error e) {
stderr.printf ("%s\n", e.message);
return 1;

View File

@ -110,9 +110,17 @@ public abstract class Pluie.Yaml.AbstractNode : GLib.Object
int indent = Yaml.Dumper.DEFAULT_INDENT,
bool show_doc = Yaml.Dumper.SHOW_DOC,
bool show_tags = Yaml.Dumper.SHOW_TAGS,
bool show_fullkeys = Yaml.Dumper.SHOW_FULL_KEYS
bool show_fullkeys = Yaml.Dumper.SHOW_FULL_KEYS,
bool show_colors = Yaml.Dumper.SHOW_COLORS
)
{
return Yaml.Dumper.dump ((Yaml.Node) this, indent, show_doc, show_tags);
return Yaml.Dumper.dump (
(Yaml.Node) this,
indent,
show_doc,
show_tags,
show_fullkeys,
show_colors
);
}
}

View File

@ -53,6 +53,18 @@ public class Pluie.Yaml.Dumper
*
*/
public static bool SHOW_FULL_KEYS { get; internal set; default = false; }
/**
*
*/
public static bool SHOW_COLORS { get; internal set; default = false; }
/**
*
*/
public static bool SHOW_LINE { get; internal set; default = false; }
/**
*
*/
static int line { get; internal set; default = 0; }
/**
*
@ -62,65 +74,69 @@ public class Pluie.Yaml.Dumper
}
/**
* get a yaml presentation of current Yaml.Node
* get a gracefull yaml presentation of current Yaml.Node
*/
public static string dump (
Yaml.Node node,
int indent = Yaml.Dumper.DEFAULT_INDENT,
bool show_doc = Yaml.Dumper.SHOW_DOC,
public static void show_yaml_string (
Yaml.Node? node,
bool show_line = true,
bool show_color = true,
bool show_tags = Yaml.Dumper.SHOW_TAGS,
bool show_doc = Yaml.Dumper.SHOW_DOC,
int indent = Yaml.Dumper.DEFAULT_INDENT,
bool show_fullkeys = Yaml.Dumper.SHOW_FULL_KEYS
)
{
bool prev1 = SHOW_LINE;
bool prev2 = SHOW_COLORS;
bool prev3 = SHOW_TAGS;
SHOW_LINE = show_line;
SHOW_COLORS = show_color;
SHOW_TAGS = show_tags;
string yaml = dump (node, indent, show_doc, show_tags, show_fullkeys, show_color);
SHOW_LINE = prev1;
SHOW_COLORS = prev2;
SHOW_TAGS = prev3;
of.action ("Yaml string representation for", node!= null ? node.name : "null");
print ("%s%s%s", "\n", yaml, "\n");
}
/**
* get a yaml presentation of current Yaml.Node
*/
public static string dump (
Yaml.Node? node,
int indent = Yaml.Dumper.DEFAULT_INDENT,
bool show_doc = Yaml.Dumper.SHOW_DOC,
bool show_tags = Yaml.Dumper.SHOW_TAGS,
bool show_fullkeys = Yaml.Dumper.SHOW_FULL_KEYS,
bool show_colors = Yaml.Dumper.SHOW_COLORS
)
{
var yaml = new StringBuilder("");
if (node != null) {
if (node.ntype.is_root ()) {
line = 0;
yaml_root (ref yaml, node as Yaml.Root, show_doc);
foreach (var child in node) {
yaml.append (Yaml.Dumper.dump (child, indent, show_doc, show_tags, show_fullkeys));
yaml.append (Yaml.Dumper.dump (child, indent, show_doc, show_tags, show_fullkeys, show_colors));
}
}
else if (node.ntype.is_single_pair ()) {
yaml_key (ref yaml, node, indent);
yaml_scalar (ref yaml, node.first (), indent);
yaml_key (ref yaml, node, indent, show_tags);
yaml_scalar (ref yaml, node.first (), indent, show_tags);
}
else if (node.ntype.is_collection ()) {
yaml_key (ref yaml, node, indent);
yaml_key (ref yaml, node, indent, show_tags);
foreach (var child in node) {
yaml.append (Yaml.Dumper.dump (child, indent, show_doc, show_tags, show_fullkeys));
yaml.append (Yaml.Dumper.dump (child, indent, show_doc, show_tags, show_fullkeys, show_colors));
}
}
else if (node.ntype.is_scalar ()) {
yaml_scalar (ref yaml, node, indent);
yaml_scalar (ref yaml, node, indent, show_tags);
}
}
line++;
return yaml.str;
//~ return "%s%s%s%s%s%s%s%s%s%s%s".printf (
//~ this.level == 0 ? "" : of.s_indent ((int8) (withIndent ? (this.level-1)*4 : 0)),
//~ of.c (ECHO.OPTION).s ("["),
//~ this.name != null && !this.ntype.is_scalar ()
//~ ? of.c (ntype.is_root () ? ECHO.MICROTIME : ECHO.TIME).s ("%s".printf (this.name))
//~ : (
//~ this.ntype.is_scalar ()
//~ ? of.c(ECHO.DATE).s ("%s".printf (this.data))
//~ : ""
//~ ),
//~ withRefCount ? of.c (ECHO.COMMAND).s ("[%lu]".printf (this.ref_count)) : "",
//~ !withParent || this.parent == null
//~ ? withLevel ? of.c (ECHO.NUM).s (" %d".printf (this.level)) : ""
//~ : of.c (ECHO.SECTION).s (" "+this.parent.name)+(
//~ withLevel ? of.c (ECHO.NUM).s (" %d".printf (this.level)) : " "
//~ ),
//~ withType ? of.c (ECHO.OPTION_SEP).s (" %s".printf(this.ntype.infos ())) : "",
//~ withCount && this.ntype.is_collection () ? of.c (ECHO.MICROTIME).s (" %d".printf(this.count ())) : "",
//~ withUuid ? of.c (ECHO.COMMENT).s (" %s".printf(this.uuid[0:8]+"...")) : "",
//~ this.tag != null && withTag
//~ ? " %s%s".printf (
//~ of.c (ECHO.TITLE).s (" %s ".printf(this.tag.handle)),
//~ of.c (ECHO.DEFAULT).s (" %s".printf(this.tag.value))
//~ )
//~ : "",
//~ of.c (ECHO.OPTION).s ("]"),
//~ withTag && this.ntype.is_root () ? (this as Yaml.Root).get_display_tag_directives () : ""
//~ );
}
/**
@ -128,15 +144,25 @@ public class Pluie.Yaml.Dumper
*/
private static void yaml_indent (ref StringBuilder yaml, Yaml.Node node, int indent, bool wrapchild = false)
{
yaml.append("%s".printf (string.nfill ((node.level-1) * indent - (wrapchild ? 2 : 0), ' ')));
yaml.append("%s%s".printf (
! SHOW_LINE ? "" : of.c (ECHO.NUM).s ("%03d %s".printf (line, of.c (ECHO.FILE).s ("|"))),
string.nfill ((node.level-1) * indent - (wrapchild ? 2 : 0), ' ')
));
}
/**
*
*/
private static void yaml_tag (ref StringBuilder yaml, Yaml.Node node)
private static void yaml_tag (ref StringBuilder yaml, Yaml.Node node, bool show_tags)
{
if (node.tag != null) yaml.append ("!%s!%s ".printf (node.tag.handle, node.tag.value));
if (node.tag != null && show_tags) {
if (SHOW_COLORS) yaml.append (of.c (ECHO.COMMAND).to_string ());
yaml.append ("!%s!%s".printf (
node.tag.handle,
node.tag.value
));
yaml.append ("%s ".printf (SHOW_COLORS ? Color.off () : ""));
}
}
/**
@ -144,43 +170,58 @@ public class Pluie.Yaml.Dumper
*/
private static void yaml_root (ref StringBuilder yaml, Yaml.Root node, bool show_doc)
{
yaml.append(! SHOW_LINE ? "" : of.c (ECHO.NUM).s ("%03d %s".printf (line++, of.c (ECHO.FILE).s ("|"))));
yaml.append("%YAML %s\n".printf (Yaml.YAML_VERSION));
foreach (var entry in node.tag_directives.entries) {
yaml.append(! SHOW_LINE ? "" : of.c (ECHO.NUM).s ("%03d %s".printf (line, of.c (ECHO.FILE).s ("|"))));
yaml.append ("%TAG %s %s\n".printf (entry.key, entry.value));
}
if (show_doc) yaml.append ("---\n");
if (show_doc) {
yaml.append(! SHOW_LINE ? "" : of.c (ECHO.NUM).s ("%03d %s".printf (line, of.c (ECHO.FILE).s ("|"))));
yaml.append ("---\n");
}
}
/**
*
*/
private static void yaml_key (ref StringBuilder yaml, Yaml.Node node, int indent)
private static void yaml_key (ref StringBuilder yaml, Yaml.Node node, int indent, bool show_tags)
{
bool wrapseq = node.parent.ntype.is_sequence () && node.ntype.is_mapping () && node.name[0]=='_';
bool childwrap = node.parent.parent.ntype.is_sequence () && node.parent.ntype.is_mapping () && node.parent.name[0]=='_';
bool childwrap = node.parent != null && node.parent.parent != null && node.parent.parent.ntype.is_sequence () && node.parent.ntype.is_mapping () && node.parent.name[0]=='_';
if (!childwrap) yaml_indent (ref yaml, node, indent);
else if (!node.is_first ()) {
yaml_indent (ref yaml, node, indent, true);
}
if (node.parent.ntype.is_sequence ()) yaml.append ("- ");
if (node.parent != null && node.parent.ntype.is_sequence ()) yaml.append (!SHOW_COLORS ? "- " : of.c (ECHO.DATE).s ("- "));
if (wrapseq) {
of.warn ("node %s wrapseq ? %s".printf (node.name, wrapseq.to_string ()));
}
yaml_tag (ref yaml, node);
yaml_tag (ref yaml, node, show_tags);
if (!wrapseq) {
yaml.append("%s:%s".printf(node.name, node.ntype.is_collection () ? "\n" : " "));
int len = 0;
foreach (var child in node.parent) {
if (child.name.length > len) len = child.name.length;
}
len = (!show_tags || (node.tag == null && !node.ntype.is_collection ()) && len > 0) ? len +1 - node.name.length : 0;
yaml.append("%s%s%s".printf(
!SHOW_COLORS ? @"%s%$(len)s ".printf (node.name, " ") : of.c (node.ntype.is_collection() ? ECHO.TIME : ECHO.OPTION).s(@"%s%$(len)s".printf (node.name, " ")),
!SHOW_COLORS ? ":" : of.c (ECHO.DATE).s(":"),
node.ntype.is_collection () ? "\n" : " "
));
}
}
/**
*
*/
private static void yaml_scalar (ref StringBuilder yaml, Yaml.Node node, int indent)
private static void yaml_scalar (ref StringBuilder yaml, Yaml.Node node, int indent, bool show_tags)
{
if (!node.parent.ntype.is_single_pair ()) yaml_indent (ref yaml, node, indent);
if (node.parent.ntype.is_sequence ()) yaml.append ("- ");
yaml_tag (ref yaml, node);
yaml.append ("%s\n".printf (node.data));
if (!(node.parent !=null && node.parent.ntype.is_single_pair ())) yaml_indent (ref yaml, node, indent);
if (node.parent != null && node.parent.ntype.is_sequence ()) yaml.append (!SHOW_COLORS ? "- " : of.c (ECHO.DATE).s ("- "));
yaml_tag (ref yaml, node, show_tags);
yaml.append ("%s\n".printf (
!SHOW_COLORS ? node.data : of.c (ECHO.OPTION_SEP).s (node.data)
));
}
}

View File

@ -52,6 +52,11 @@ public class Pluie.Yaml.Processor
*/
bool beginFlowSeq;
/**
*
*/
int indexEvt;
/**
* current anchor id
*/
@ -142,9 +147,10 @@ public class Pluie.Yaml.Processor
public void read ()
{
of.action ("Reading events");
var i = 0;
foreach (Yaml.Event event in this.events) {
int len = 24 - event.evtype.infos ().length;
stdout.printf (" [ %s"+@" %$(len)s "+", %d, %s", event.evtype.infos (), " ", event.line, event.style != null ? event.style.to_string () : "0");
stdout.printf (" %03d [ %s"+@" %$(len)s "+", %d, %s", i++, event.evtype.infos (), " ", event.line, event.style != null ? event.style.to_string () : "0");
if (event.data != null && event.data.size > 0) {
stdout.printf (", {");
var it = event.data.map_iterator ();
@ -169,7 +175,7 @@ public class Pluie.Yaml.Processor
this.reset ();
for (var has_next = this.iterator.next (); has_next; has_next = this.iterator.next ()) {
this.event = this.iterator.get ();
Yaml.dbg ("Processing event %s".printf (this.event.evtype.infos ()));
Yaml.dbg (" 0>>>>> [EVENT event [%d] %s <<<<< %s".printf (this.indexEvt++, this.event.evtype.infos (), Log.METHOD));
if (this.event.evtype.is_tag_directive ()) {
this.on_tag_directive ();
}
@ -227,6 +233,7 @@ public class Pluie.Yaml.Processor
this.valueTag = null;
this.beginFlowSeq = false;
this.nextValueEvt = null;
this.indexEvt = 0;
}
/**
@ -237,6 +244,7 @@ public class Pluie.Yaml.Processor
Yaml.Event? evt = null;
if (this.iterator.has_next () && this.iterator.next ()) {
evt = this.iterator.get ();
Yaml.dbg (" 1>>>>> [EVENT event [%d] %s <<<<< %s".printf (this.indexEvt++, evt.evtype.infos (), Log.METHOD));
}
return evt;
}
@ -269,6 +277,32 @@ public class Pluie.Yaml.Processor
return evt;
}
/**
* retriew the next Yaml Value Event
*/
private Yaml.Event? get_next_value_event ()
{
Yaml.Event? evt = null;
of.echo(" :::: current event %s - index %d".printf (this.event.evtype.infos (), this.indexEvt));
var i = this.indexEvt+1;
of.echo(" :::: i = %d".printf (i));
var search = true;
while (search) {
if (i < this.events.size) {
var e = this.events.get (i++);
of.echo(" :::: i = %d => event : %s".printf (i-1, e.evtype.infos ()));
if (e != null && e.evtype.is_value ()) {
of.echo(" :::: IS VALUE RETURN NEXT EVENTi = %d => event : %s".printf (i, e.evtype.infos ()));
evt = this.events.get (i);
break;
}
}
else search = false;
}
return evt;
}
/**
*
*/
@ -323,9 +357,15 @@ public class Pluie.Yaml.Processor
*/
private void on_entry ()
{
of.echo (" >>> on_ENTRY : current event is %s".printf (this.event.evtype.infos ()));
this.event = this.next_event();
of.echo (" >>> ON ENTRY : next value event is %s".printf (this.nextValueEvt.evtype.infos ()));
if (this.event.evtype.is_mapping_start () && this.nextValueEvt.evtype.is_mapping_start ()) {
of.echo (" >>> on_ENTRY : ENTRY TYPE %s".printf (this.event.evtype.infos ()));
Yaml.Event? e = null;
e = get_next_value_event ();
if (e != null) {
of.echo (" >>> on_ENTRY : NEXT value event is %s".printf (e.evtype.infos ()));
}
if (this.event.evtype.is_mapping_start () && (e!= null && !e.evtype.is_mapping_start ())) {
of.echo (" THE mapping start");
this.on_mapping_start (true);
}
@ -370,6 +410,7 @@ public class Pluie.Yaml.Processor
*/
private void on_value ()
{
of.echo (" >>> ON VALUE : next value event is %s".printf (this.nextValueEvt.evtype.infos ()));
this.on_tag (false);
if (this.event.evtype.is_scalar ()) {
this.on_scalar ();