Skip to content

Commit

Permalink
replaced annotations by keyword parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
jurgenvinju committed Mar 14, 2024
1 parent 6071b8a commit 389203f
Showing 1 changed file with 37 additions and 36 deletions.
73 changes: 37 additions & 36 deletions src/org/rascalmpl/library/lang/yaml/Model.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -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}
;
Expand All @@ -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);
Expand All @@ -83,33 +84,33 @@ 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) {
seen = {};
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) {
Expand All @@ -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)
= <seen, dupl + {i}>
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)
<seen, dupl> = undefinedRefs(n, seen, dupl);
Expand All @@ -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) {
<seen, dupl> = undefinedRefs(n, seen, dupl);
Expand All @@ -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;
}
Expand All @@ -179,7 +180,7 @@ bool equalNodes(Node x, Node y) {
case <scalar(v1), scalar(v2)>:
return v1 == v2;
case <reference(r1), reference(r2)>: {
case <reference(anchor=r1), reference(anchor=r2)>: {
if (r1 in matching, matching[r1] == r2) {
return true; // avoid infinite loop;
}
Expand Down

0 comments on commit 389203f

Please sign in to comment.