From 8a4764080b11e3f1a7403cc5df0d0fc532c793f9 Mon Sep 17 00:00:00 2001 From: a-sansara Date: Wed, 1 Aug 2018 14:20:55 +0200 Subject: [PATCH] add samples json & yaml + fix flow entries + update build.sh --- .gitignore | 2 +- bin/.gitkeep | 0 build.sh | 79 ++++++++++++++++++++++++----- meson.build | 2 +- resources/test.json | 22 ++++++++ samples/json-load.vala | 23 +++++++++ main.vala => samples/yaml-load.vala | 0 src/vala/Pluie/Yaml.Processor.vala | 42 +++++++++------ 8 files changed, 140 insertions(+), 30 deletions(-) create mode 100644 bin/.gitkeep create mode 100644 resources/test.json create mode 100644 samples/json-load.vala rename main.vala => samples/yaml-load.vala (100%) diff --git a/.gitignore b/.gitignore index 18d2eaf..d5697ec 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ build/ -main +bin/ diff --git a/bin/.gitkeep b/bin/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/build.sh b/build.sh index b35ada3..4cbb618 100755 --- a/build.sh +++ b/build.sh @@ -1,16 +1,69 @@ #!/bin/bash +# -------------------------------------------------------- DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -cd "$DIR" -meson --prefix=/usr ./ build -sudo ninja -v install -C build -if [ $? -eq 0 ]; then - echo "== LIB BUILD SUCCESS ==" - valac -v --pkg gee-0.8 --pkg pluie-echo-0.2 --pkg pluie-yaml-0.3 main.vala - if [ $? -eq 0 ]; then - echo "== BUILD SUCCESS ==" - else - echo "== BUILD FAILED ==" +lib="pluie-yaml-0.3" + c1="\033[1;38;5;215m" + c2="\033[1;38;5;97m" +cok="\033[1;38;5;37m" +cko="\033[1;38;5;204m" +off="\033[m" +# -------------------------------------------------------- +function build.title() +{ + local s="$cko>" + local c3="" + local state="" + if [ ! -z "$2" ]; then + state="${cko}FAILED" + if [ $2 -eq 0 ]; then + state="${cok}SUCCESS" + fi + s="$cko<" fi -else - echo "== LIB BUILD FAILED ==" -fi + echo -e "\n $s $c1[$c2$1$c1] $state$off" +} +# -------------------------------------------------------- +function build.lib() +{ + cd "$DIR" + build.title "$lib LIB BUILD" + echo + meson --prefix=/usr ./ build + sudo ninja -v install -C build + local rs=$? + build.title "$lib LIB BUILD" $rs + return $rs +} +# -------------------------------------------------------- +function build.samples() +{ + for file in ./samples/*.vala + do + if [[ -f $file ]]; then + build.sample "$file" + fi + done + echo +} +# -------------------------------------------------------- +function build.sample() +{ + local f="$(basename $1)" + local fx="${f:0:-5}" + local state="FAILED" + local cmd="valac -v --pkg gee-0.8 --pkg pluie-echo-0.2 --pkg $lib $1 -o ./bin/$fx" + build.title "$f SAMPLE BUILD" + echo -e "\n$cmd" + $cmd + build.title "$f SAMPLE BUILD" $? +} +# -------------------------------------------------------- +function build.main() +{ + build.lib + if [ $? -eq 0 ]; then + build.samples + fi +} + +build.main diff --git a/meson.build b/meson.build index 9f535fe..8ae9e7e 100644 --- a/meson.build +++ b/meson.build @@ -28,7 +28,7 @@ configure_file( configuration: conf ) -sources = [ +sources = [ 'build/install.vala', 'src/vala/Pluie/Io.Reader.vala', 'src/vala/Pluie/Io.StreamLineMark.vala', diff --git a/resources/test.json b/resources/test.json new file mode 100644 index 0000000..eacfbf5 --- /dev/null +++ b/resources/test.json @@ -0,0 +1,22 @@ +{ + "glossary": { + "title": "example glossary", + "GlossDiv": { + "title": "S", + "GlossList": { + "GlossEntry": { + "ID": "SGML", + "SortAs": "SGML", + "GlossTerm": "Standard Generalized Markup Language", + "Acronym": "SGML", + "Abbrev": "ISO 8879:1986", + "GlossDef": { + "para": "A meta-markup language, used to create markup languages such as DocBook.", + "GlossSeeAlso": ["GML", "XML"] + }, + "GlossSee": "markup" + } + } + } + } +} diff --git a/samples/json-load.vala b/samples/json-load.vala new file mode 100644 index 0000000..1257350 --- /dev/null +++ b/samples/json-load.vala @@ -0,0 +1,23 @@ +using GLib; +using Gee; +using Pluie; + +int main (string[] args) +{ + Echo.init(false); + + var path = "resources/test.json"; + 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 (); + } + + of.rs (done); + of.echo (); + return (int) done; + +} diff --git a/main.vala b/samples/yaml-load.vala similarity index 100% rename from main.vala rename to samples/yaml-load.vala diff --git a/src/vala/Pluie/Yaml.Processor.vala b/src/vala/Pluie/Yaml.Processor.vala index 438ff4d..1b683c5 100644 --- a/src/vala/Pluie/Yaml.Processor.vala +++ b/src/vala/Pluie/Yaml.Processor.vala @@ -67,7 +67,6 @@ public class Pluie.Yaml.Processor public void read () { of.action ("Reading events"); - EVT? prevEvent = null; 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"); @@ -81,7 +80,7 @@ public class Pluie.Yaml.Processor } stdout.printf (" }"); } - of.echo ("]\n"); + of.echo ("]"); } } @@ -132,16 +131,17 @@ public class Pluie.Yaml.Processor */ public bool run () { - this.root = new Yaml.NodeRoot (); - this.prev_node = this.root; - this.parent_node = this.root; - this.prev_indent = this.root.indent; - int indent = this.root.indent +4; - EVT? prevEvent = null; - var it = this.events.iterator (); - var change = false; - string? key = null; - string? id = null; +//~ if (Pluie.Yaml.Scanner.DEBUG) this.read (); + this.root = new Yaml.NodeRoot (); + this.prev_node = this.root; + this.parent_node = this.root; + this.prev_indent = this.root.indent; + int indent = this.root.indent +4; + var it = this.events.iterator (); + var change = false; + string? key = null; + string? id = null; + bool beginFlowSeq = false; Yaml.Event? evt; if (Pluie.Yaml.Scanner.DEBUG) of.action ("Processing events"); for (var has_next = it.next (); has_next; has_next = it.next ()) { @@ -165,6 +165,17 @@ public class Pluie.Yaml.Processor indent += 4; change = true; } + else if (evt.evtype.is_scalar ()) { + var content = evt.data["data"]; + this.node = new Yaml.NodeScalar (this.parent_node, indent, content); + change = true; + } + } + if (beginFlowSeq && evt.evtype.is_scalar ()) { + var content = evt.data["data"]; + this.node = new Yaml.NodeScalar (this.parent_node, indent, content); + change = true; + beginFlowSeq = false; } if (evt.evtype.is_key () && (evt = this.get_value_key_event (it)) != null) { key = evt.data["data"]; @@ -197,9 +208,10 @@ public class Pluie.Yaml.Processor change = true; } else if (evt.evtype.is_sequence_start ()) { - this.node = new Yaml.NodeSequence (this.parent_node, indent, key); - indent += 4; - change = true; + this.node = new Yaml.NodeSequence (this.parent_node, indent, key); + indent += 4; + change = true; + beginFlowSeq = true; } if (id != null) { if (this.node != null) {