diff --git a/src/org/rascalmpl/library/lang/yaml/Model.rsc b/src/org/rascalmpl/library/lang/yaml/Model.rsc index 6fb6d212af6..cd540308230 100644 --- a/src/org/rascalmpl/library/lang/yaml/Model.rsc +++ b/src/org/rascalmpl/library/lang/yaml/Model.rsc @@ -14,25 +14,26 @@ import List; import Map; import Set; -// Tagging will be used to do typed -// serialization for ADTs in the future. +@synopsis{Generic representation for YAML nodes} +@description{ +Tagging (using the `\tag` field) will be used to do typed +serialization for ADTs in the future. -// in valid YAML anchors always occur before any references -// this should also hold in our YAML data type -// dumping will throw index out of bound exception. +In valid YAML anchors always occur before any references +this should also hold in our YAML data type. +Dumping will throw index out of bound exception if references +are out of order. -data Node +Anchors are only currently valid on seq/map nodes. +and should be unique. +} +data Node(int anchor=-1, type[value] \tag = #void) = sequence(list[Node] \list) | scalar(value \value) - | reference(int anchor) + | reference() | mapping(map[Node, Node] \map) ; -// Anchors are only currently valid on seq/map nodes. -// and should be unique. -anno int Node@anchor; -anno type[value] Node@\tag; - private set[type[value]] SUPPORTED_TYPES = {#int, #str, #real, #datetime, #str, #bool, #loc} ; @@ -57,14 +58,14 @@ public str TEST_YAML = public Node BAD_YAML = mapping(( - scalar(3)[@anchor=3]: + scalar(3,anchor=3): sequence([ - scalar("abc")[@\tag=#int], - scalar("cde")[@\tag=#str], - scalar("unsupported")[@\tag=#node], - reference(4), - sequence([])[@anchor=4] - ])[@anchor=2]))[@anchor=2]; + scalar("abc", \tag=#int), + scalar("cde", \tag=#str), + scalar("unsupported")[\tag=#node], + reference(anchor=4), + sequence([])[anchor=4] + ])[@anchor=2]))[anchor=2]; public test bool testLoadDump() { Node n = loadYAML(TEST_YAML); @@ -83,21 +84,21 @@ public set[str] checkYAML(Node n) ; public set[Node] badAnchors(Node n) - = { s | /s:scalar(_) <- n, (s@anchor)? } - + { r | /r:reference(_) <- n, (r@anchor)? }; + = { s | /s:scalar(_) <- n, (s.anchor)? } + + { r | /r:reference() <- n, (r.anchor)? }; public set[Node] wronglyTypedScalars(Node n) - = { s | /s:scalar(value v) <- n, s@\tag?, type[&T] t := s@\tag, !okValue(t, v) }; + = { s | /s:scalar(value v) <- n, s.\tag?, type[&T] t := s.\tag, !okValue(t, v) }; // Doesn't work: always succeeds. public bool okValue(type[&T <: value] _, value v) = (&T _ := v); public set[type[value]] unsupportedTypes(Node n) - = { t | /s:scalar(_) <- n, s@\tag?, type[value] t := s@\tag, t notin SUPPORTED_TYPES }; + = { t | /s:scalar(_) <- n, s.\tag?, type[value] t := s.\tag, t notin SUPPORTED_TYPES }; public set[Node] untaggedScalars(Node n) - = { s | /s:scalar(_) <- n, !(s@\tag?) } + = { s | /s:scalar(_) <- n, !(s.\tag?) } ; public set[int] duplicateAnchors(Node n) { @@ -105,11 +106,11 @@ public set[int] duplicateAnchors(Node n) { duplicate = {}; void record(Node s) { - if (!(s@anchor?)) return; - if (s@anchor in seen) - duplicate += {s@anchor}; + if (!(s.anchor?)) return; + if (s.anchor in seen) + duplicate += {s.anchor}; else - seen += {s@anchor}; + seen += {s.anchor}; } visit (n) { @@ -120,14 +121,14 @@ public set[int] duplicateAnchors(Node n) { } -public tuple[set[int], set[int]] undefinedRefs(reference(i), set[int] seen, set[int] dupl) +public tuple[set[int], set[int]] undefinedRefs(reference(anchor=i), set[int] seen, set[int] dupl) = when i notin seen; public tuple[set[int], set[int]] undefinedRefs(s:sequence(ns), set[int] seen, set[int] dupl) { undefs = {}; - if (s@anchor?) { - seen += {s@anchor}; + if (s.anchor?) { + seen += {s.anchor}; } for (n <- ns) = undefinedRefs(n, seen, dupl); @@ -136,8 +137,8 @@ public tuple[set[int], set[int]] undefinedRefs(s:sequence(ns), set[int] seen, se public tuple[set[int], set[int]] undefinedRefs(nod:mapping(m), set[int] seen, set[int] dupl) { undefs = {}; - if (nod@anchor?) { - seen += {nod@anchor}; + if (nod.anchor?) { + seen += {nod.anchor}; } for (Node n <- m) { = undefinedRefs(n, seen, dupl); @@ -155,9 +156,9 @@ bool equalNodes(Node x, Node y) { m = (); visit (n) { case s:sequence(_): - if (s@anchor?) m[s@anchor] = s; + if (s.anchor?) m[s.anchor] = s; case mp:mapping(_): - if (mp@anchor?) m[mp@anchor] = mp; + if (mp.anchor?) m[mp.anchor] = mp; } return m; } @@ -179,7 +180,7 @@ bool equalNodes(Node x, Node y) { case : return v1 == v2; - case : { + case : { if (r1 in matching, matching[r1] == r2) { return true; // avoid infinite loop; }