From 52db4f70a668da60f2417f49b8b5c8e78aa6ea30 Mon Sep 17 00:00:00 2001 From: a-Sansara Date: Wed, 1 Aug 2018 03:23:25 +0200 Subject: [PATCH] better traces & debug --- .gitignore | 1 + main.vala | 2 +- src/vala/Pluie/Yaml.BaseNode.vala | 30 ++++++++++++------- src/vala/Pluie/Yaml.Loader.vala | 47 +++++++++++++++++++++++++----- src/vala/Pluie/Yaml.Node.vala | 2 +- src/vala/Pluie/Yaml.Processor.vala | 27 +++++++++++------ src/vala/Pluie/Yaml.Scanner.vala | 15 ++++++++-- src/vala/Pluie/Yaml.global.vala | 8 +++++ 8 files changed, 99 insertions(+), 33 deletions(-) diff --git a/.gitignore b/.gitignore index 567609b..18d2eaf 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ build/ +main diff --git a/main.vala b/main.vala index d07751a..6e0c7f4 100644 --- a/main.vala +++ b/main.vala @@ -10,7 +10,7 @@ int main (string[] args) var done = false; of.title ("Pluie Yaml Parser", Pluie.Yaml.VERSION, "a-sansara"); - + Pluie.Yaml.Scanner.DEBUG = true; var loader = new Yaml.Loader (path, true, true); if ((done = loader.done)) { Yaml.NodeRoot root = loader.get_nodes (); diff --git a/src/vala/Pluie/Yaml.BaseNode.vala b/src/vala/Pluie/Yaml.BaseNode.vala index 587d4bb..6edb3f3 100644 --- a/src/vala/Pluie/Yaml.BaseNode.vala +++ b/src/vala/Pluie/Yaml.BaseNode.vala @@ -188,20 +188,28 @@ public class Pluie.Yaml.BaseNode : Object, Pluie.Yaml.Node /** * get a presentation string of current Yaml.Node */ - public string to_string (bool withIndent = true) + public string to_string (bool indentFormat = true, bool withParent = false, bool withUuid = false, bool withIndent = false, bool withRefCount = false) { - return "%s%s%s%s%s%s%s".printf ( - this.node_type.is_root () ? "" : of.s_indent ((int8) (withIndent ? this.indent : 0)), - of.c (ECHO.OPTION).s ("<"), + return "%s%s%s%s%s%s%s%s".printf ( + this.node_type.is_root () ? "" : of.s_indent ((int8) (indentFormat ? this.indent : 0)), + of.c (ECHO.OPTION).s ("["), of.c (ECHO.OPTION_SEP).s (this.node_type.infos ()), - this.name != null ? " %s".printf (this.name) - : (this.node_type.is_scalar () ? " %s".printf (this.data) : ""), - "[%x]".printf (this.ref_count), - this.parent == null ? "" : this.parent.name+"|"+this.indent.to_string(), -//~ " (%d) ".printf (this.indent), - of.c (ECHO.OPTION).s (">")/*, - of.c (ECHO.DATE).s (" %s".printf(this.uuid))*/ + this.name != null && !this.node_type.is_scalar () + ? of.c (ECHO.TIME).s (" %s".printf (this.name)) + : ( + this.node_type.is_scalar () + ? of.c(ECHO.DATE).s (" %s".printf (this.data)) + : "" + ), + withRefCount ? of.c (ECHO.COMMAND).s ("[%x]".printf (this.ref_count)) : "", + !withParent || this.parent == null + ? "" + : of.c (ECHO.SECTION).s (" "+this.parent.name)+( + withIndent ? of.c (ECHO.NUM).s (" "+this.indent.to_string()) : "" + ), + withUuid ? of.c (ECHO.COMMENT).s (" %s".printf(this.uuid[0:8]+"...")) : "", + of.c (ECHO.OPTION).s ("]") ); } diff --git a/src/vala/Pluie/Yaml.Loader.vala b/src/vala/Pluie/Yaml.Loader.vala index 35b50ab..97c8e74 100644 --- a/src/vala/Pluie/Yaml.Loader.vala +++ b/src/vala/Pluie/Yaml.Loader.vala @@ -38,6 +38,11 @@ public class Pluie.Yaml.Loader this.get_nodes ().display_childs (); of.state(true); } + if (!this.done) { + var evt = this.scanner.get_error_event (); + of.error ("line %d (%s)".printf (evt.line, evt.data["error"])); + this.displayFile (evt.line); + } } /** @@ -51,17 +56,43 @@ public class Pluie.Yaml.Loader /** * display original file */ - public void displayFile () + public void displayFile (int errorLine = 0) { - of.action ("Reading file", this.reader.path); + of.action (errorLine == 0 ? "Reading file" : "Invalid Yaml File", this.reader.path); of.echo (); + this.reader.rewind(new Io.StreamLineMark(0, 0)); + int line = 0; + string? data = null;; while (this.reader.readable) { - of.echo ("%s %s".printf ( - of.c (ECHO.DATE ).s ("%03d |".printf (this.reader.line)), - of.c (ECHO.OPTION_SEP).s (this.reader.read ())) - ); + line = this.reader.line + 1; + data = this.reader.read (); + if (errorLine > 0 && line > 0) { + if (line < errorLine - 7) continue; + else if (line == errorLine - 7) { + of.echo ("%s%s%s".printf ( + of.c (ECHO.MICROTIME ).s (" %03d ".printf (line-1)), + of.c (ECHO.DATE).s ("| "), + of.c (ECHO.COMMAND).s ("... ") + )); + } + } + of.echo ("%s%s%s".printf ( + of.c (ECHO.MICROTIME ).s (" %03d ".printf (line)), + of.c (ECHO.DATE).s ("| "), + errorLine > 0 && line == errorLine + ? of.c (ECHO.FAIL).s (data) + : of.c (ECHO.COMMAND).s (data) + ), errorLine == 0 || line < errorLine); + if (errorLine > 0 && line == errorLine) { + int len = of.term_width - data.length - 13; + stdout.printf (of.c (ECHO.FAIL).s (@" %$(len)s ".printf (" "))); + of.echo (Color.off (), true); + } + if (errorLine > 0 && line > errorLine) { + break; + } } - of.echo ("EOF"); - of.state (true); + of.echo (errorLine == 0 ? "EOF" : ""); + of.state (errorLine == 0); } } diff --git a/src/vala/Pluie/Yaml.Node.vala b/src/vala/Pluie/Yaml.Node.vala index eb62a89..e62a4ca 100644 --- a/src/vala/Pluie/Yaml.Node.vala +++ b/src/vala/Pluie/Yaml.Node.vala @@ -77,6 +77,6 @@ public interface Pluie.Yaml.Node : Object /** * get a presentation string of current Yaml.Node */ - public abstract string to_string (bool withIndent = true); + public abstract string to_string (bool indentFormat = true, bool withParent = false, bool withUuid = true, bool withIndent = false, bool withRefCount = true); } diff --git a/src/vala/Pluie/Yaml.Processor.vala b/src/vala/Pluie/Yaml.Processor.vala index 9032f7c..438ff4d 100644 --- a/src/vala/Pluie/Yaml.Processor.vala +++ b/src/vala/Pluie/Yaml.Processor.vala @@ -10,7 +10,7 @@ public class Pluie.Yaml.Processor /** * indicates if processing is sucess */ - public bool done; + public bool done; /** * Events list @@ -20,32 +20,37 @@ public class Pluie.Yaml.Processor /** * Anchor map */ - Gee.HashMap anchors { get; internal set; } + Gee.HashMap anchors { get; internal set; } + + /** + * Error event + */ + public Yaml.Event? error_event { get; internal set; } /** * the root Yaml.Node */ - public Yaml.Node root; + public Yaml.Node root; /** * current previous Yaml.Node */ - Yaml.Node? prev_node; + Yaml.Node? prev_node; /** * current parent Yaml.Node */ - Yaml.Node? parent_node; + Yaml.Node? parent_node; /** * current Yaml.Node */ - Yaml.Node node; + Yaml.Node node; /** * previous indent */ - int prev_indent; + int prev_indent; /** * @@ -138,9 +143,13 @@ public class Pluie.Yaml.Processor string? key = null; string? id = null; Yaml.Event? evt; - of.action ("Processing events"); + if (Pluie.Yaml.Scanner.DEBUG) of.action ("Processing events"); for (var has_next = it.next (); has_next; has_next = it.next ()) { evt = it.get (); + if (evt.evtype.is_error ()) { + error_event = evt; + break; + } if (evt.evtype.is_mapping_end () || evt.evtype.is_sequence_end ()) { indent -= 4; this.parent_node = this.prev_node.parent != this.root ? this.prev_node.parent.parent : this.root; @@ -211,7 +220,7 @@ public class Pluie.Yaml.Processor change = false; } } - this.done = this.root != null; + this.done = error_event == null && this.root != null; return done; } diff --git a/src/vala/Pluie/Yaml.Scanner.vala b/src/vala/Pluie/Yaml.Scanner.vala index 4ea98ee..390bcb5 100644 --- a/src/vala/Pluie/Yaml.Scanner.vala +++ b/src/vala/Pluie/Yaml.Scanner.vala @@ -8,6 +8,7 @@ extern void yaml_parse_file(string srcPath, string destPath); */ public class Pluie.Yaml.Scanner { + public static bool DEBUG = false; /** * Regex pattern use to find EVENT */ @@ -95,6 +96,14 @@ public class Pluie.Yaml.Scanner return (this.processor.root as Yaml.NodeRoot); } + /** + * return error Yaml Event + */ + public Yaml.Event? get_error_event () + { + return this.processor.error_event; + } + /** * scan specifiyed file generated throught yaml.c * @param optional file path to scan @@ -110,14 +119,14 @@ public class Pluie.Yaml.Scanner } this.processor = new Yaml.Processor (); this.done = false; - of.action ("Scanning events", path); + if (Pluie.Yaml.Scanner.DEBUG) of.action ("Scanning events", path); while (this.reader.readable) { this.scan_event (this.reader.read ()); } this.done = true; - of.state (this.done); + if (Pluie.Yaml.Scanner.DEBUG) of.state (this.done); this.done = this.done && this.processor.run (); - of.state (this.done); + if (Pluie.Yaml.Scanner.DEBUG) of.state (this.done); Dbg.out (Log.METHOD, "done:%d".printf ((int)done), Log.LINE, Log.FILE); return this.done; } diff --git a/src/vala/Pluie/Yaml.global.vala b/src/vala/Pluie/Yaml.global.vala index 409ae65..424d323 100644 --- a/src/vala/Pluie/Yaml.global.vala +++ b/src/vala/Pluie/Yaml.global.vala @@ -134,6 +134,14 @@ namespace Pluie return this == EVT.BLOCK_END; } + /** + * @return event is error event + */ + public bool is_error () + { + return this == EVT.NONE; + } + } /**