Navigation
Synopsis Declare an annotation type for nodes.
Syntax anno AnnoType OnType @ Name
Description An annotation may be associated with any node value, be it a pure node or some AlgebraicDataType derived from it.

Annotations are intended to attach application data to values, like adding position information or control flow information to source code or adding visualization information to a graph.

An annotation declaration defines:
  • AnnoType, the type of the annotation values,
  • OnTYpe, the type of the values that are being annotated,
  • Name, the name of the annotation.
Any value of any named type can be annotated and the type of these annotations can be declared precisely.

The following constructs are provided for handling annotations:
  • Val @ Anno: is an expression that retrieves the value of annotation Anno of value Val (may be undefined!). See Selection.
  • Val1[@Anno = Val2]: is an expression that sets the value of annotation Anno of the value Val1 to Val2 and returns Val1 with the new annotation value as result. See Replacement.
  • Var @ Anno = Val: is an assignment statement that sets the value of annotation Anno of the value of variable Var to Val.
Examples Here is a declaration of an annotation that attaches location information to certain syntactic constructs of programs (e.g., EXPRESSION) with name posinfo:
anno loc EXPRESSION @ posinfo;
One can also add such location information to all syntax trees:
anno loc Tree @ posinfo;
Given a graph datatype, one can define an annotation with name LayoutStrategy that defines which graph layout algorithm to apply to a particular graph, e.g.,
rascal>data Graph = vertex(str name, int x, int y) | edge(str from, str to);
ok
rascal>data LayoutStrategy = dot() | tree() | force() | hierarchy() | fisheye();
ok
rascal>anno LayoutStrategy Graph @ strategy;
ok
rascal>G = vertex("root", 0, 0);
Graph: vertex("root",0,0)
rascal>G @ strategy = fisheye();
Graph: vertex("root",0,0)[
  @strategy=fisheye()
]
Is this page unclear, or have you spotted an error? Please add a comment below and help us to improve it. For all other questions and remarks, visit ask.rascal-mpl.org.