This lab is your first encounter with Stratego. You add an outline view and a desugaring transformation to an initial editor provided by us.
to-outline-label
which map AST nodes to labels in an outline view. You should include:
desugar
which desugar
UnExp(op, exp)
,BinExp(op, exp1, exp2)
anddesugar
into a strategy desugar-all
which desugars subtrees in an AST.For grading, it is required to comply with all constructor names, rule names, and strategy names literally.
We provide you with a template for this assignment.
See the Git documentation on how to check out this branch.
Make sure to read the README.md
in the root of the repository, as it contains further instructions that are specific to this template.
Until now, you mainly worked on files in the syntax
folder of your project.
During this lab you will also edit files in the trans
folder.
So, this is a good point to talk about the general structure of a Spoofax project.
Note: this general structure does not apply to the template we’ve given you for the project, considering it contains a compiled version of the front-end already.
First of all, every Spoofax project may be part of an Eclipse plug-in project. This allows you to deploy your editor as a plugin using the Eclipse update site mechanism. Users do not need to have Spoofax installed for using your editor.
The actual language definition is spread over three folders:
syntax
contains all syntax definition files, including the main file chocopy.sdf3
.trans
contains all transformation files, including the main file chocopy.str
.editor
contains editor service definition files, including the main file Main.esv
.In the src-gen
folder, you will find files which were generated from your syntax definition.
For each file <name>.sdf3
, there are generated files:
syntax/<name>.sdf
: an SDF2 definition which is equivalent to the SDF3 definition.completions/<name>-cp.str
: abstract placeholder expansions for syntactic code completion derived from SDF3 templates.completions/colorer/<name>-cc-esv.esv
: instructions to color placeholders for syntactic completions in gray.pp/<name>-pp.str
: pretty-printing strategies derived from SDF3 templates.signatures/<name>-sig.str
: signatures derived from SDF3 templates.You can find more generated files in src-gen
and target/metaborg
folders:
src-gen/syntax/ChocoPy.def
: your complete syntax definition in SDF2.src-gen/syntax/ChocoPy-Permissive.def
: a permissive version of the syntax definition, which supports error recovery.target/metaborg/ChocoPy.tbl
: the parse table of your language.src-gen/pp/ChocoPy-parenthesize.str
: strategies to add parentheses to an AST according to the priorities of your language.target/metaborg/stratego.ctree
and/or target/metaborg/stratego.jar
: compiled Stratego code of your language.We provide you with an initial ChocoPy project in the branch milestone-3-template
.
This project is a common starting point for all of you.
It includes:
reference/src-gen/signatures/*-sig.str
,reference/src-gen/pp/*-pp.str
, and areference/src-gen/completion/*-cp.str
(errors in the completion files can be ignored).During the building of the project the contents of reference
will be copied over to src-gen
.
So, when importing any module from the reference, you can just use the ‘local’ variant.
I.e. instead of importing reference/src-gen/signatures/chocopy-sig
, please import signatures/chocopy-sig
.
Signatures declare sorts and constructors for terms.
In Spoofax, terms are used to represent abstract syntax trees.
The corresponding signature is generated from the constructors in a syntax definition.
You can find the signature for ChocoPy in the files reference/src-gen/signatures/*-sig.str
.
The signature was generated from a syntax definition, which itself is not included in the initial project.
If you write your own syntax definition, the generated signatures can be found in src-gen/signatures/
.
An outline view can be specified by rewrite rules to-outline-label
in trans/outline.str
.
These rules should rewrite AST nodes to their label in an outline view.
For example, the following rule rewrites a variable declaration to its name, which will be used as a label.
rules
to-outline-label: Var(t, v) -> v
On the left-hand side, the rule matches a variable declaration.
During the match, variables t
and v
are bound to actual terms.
On the right-hand side, the rule instantiates a label.
During the instantiation, variable v
is replaced with the term it is bound to.
You can extend to-outline-label
to provide labels for
When you build the project and open a ChocoPy file, you will get an outline of this program in the outline view. In case you do not see any outline view, you can select it in Show View from Eclipse’s Window menu.
In Stratego, we use the following naming conventions:
Add
, BinExp
)-
(e.g. e1
, project-path
)In many cases, you want to provide more information than just the name. For example, you might want to show not only a variable’s name, but also it’s type. The following rule achieves this:
to-outline-label:
Var(t, v) -> label
where
t' := <pp-partial-ChocoPy-string> t
; label := <concat-strings> [v, ": ", t']
On its right-hand side, it produces a label
, which is bound in the where
clause.
First, the term bound to t
is turned into a string bound to t'
by applying a strategy pp-partial-ChocoPy-string
.
This strategy is defined in ChocoPy/trans/pp.str
and uses the pretty-printing rules generated from our SDF3 grammar for ChocoPy.
Next, the label is bound to the concatenation of
the string bound to v
,
a constant string ": "
,
and the string bound to t'
.
String concatenation is not very intuitive. Instead, you can also use string interpolation:
to-outline-label:
Var(t, v) -> $[[v]: [t']]
where
t' := <pp-partial-ChocoPy-string> t
String interpolation allows you to combine text with variables.
Text is enclosed in $[
and ]
, while variables inside the text are enclosed in [
and ]
.
These variables need to be bound to strings.
You should provide the following information in your outline labels:
For parameter types, you need to turn a list of parameters into a string. You can do this with a recursive strategy:
pp-params: // empty parameter list
[] -> ...
pp-params: // single parameter
[TypedVar(p, t)] -> ...
pp-params: // at least two parameters
[TypedVar(p, t), param | params] -> ...
where
// do something on first parameter
...
// recursive call on remaining parameters
... := <pp-params> [param | params]
For grading, it is required to define a strategy pp-params
that always rewrites a list of TypedVar
s to a string.
Your current outline view is missing a root node. You can add a root node by providing a label for programs.
In Stratego, terms can be annotated with additional information. The Spoofax outline view uses annotations to determine the icon of a node. You can specify the icon to use in an annotation:
to-outline-label:
Var(t, v) -> label{icon}
where
t' := <pp-partial-ChocoPy-string> t
; label := $[[v]: [t']]
; icon := "icons/var.gif"
We do not require you to use icons and you will not earn any points with them.
If you want to use them anyway, you should put the icons into the folder icons
and place a proper attribution or license file next to them.
Challenges are meant to distinguish excellent solutions from good solutions. Typically, they are less guided and require more investigation or higher programming skills.
Provide the file name as the root node label. In outline.str
: import, command/control click and study libspoofax/term/origin
for a suitable strategy.
Outline the main method as a subnode of the main class.
You need to change this strategy in the outline.str
file:
outline := <custom-label-outline(to-outline-label, to-outline-node)> ast
Also import, command/control click, and investigate libspoofax/editor/outline
for inspiration.
Use one of the library strategies for folding to implement pp-params
. You can find various folding strategies in the API docs.
A uniform representation of unary and binary expressions eases static analysis and code generation. To get such a uniform representation, you need to desugar abstract syntax trees during the analysis phase.
Before you can implement a desugaring,
you need to define a signature for the uniform representation of expressions in trans/desugar.str
:
UnOp
and BinOp
as types of these operators.
Again, you should use names based on the semantics of an operator, not on its syntax.
Reading an expression aloud might help you to find suitable constructor names.UnExp
and BinExp
,
which combine an operator and an expression (respectively two expressions) to an expression.The following rewrite rule defines a rule to desugar an addition:
rules
desugar: Add(e1, e2) -> BinExp(Plus(), e1, e2)
This rewrite rule is named desugar
.
On the left-hand side, the rule matches an addition.
During the match, variables e1
and e2
are bound to actual terms.
On the right-hand side, the rule instantiates a binary expression (in a uniform representation).
During the instantiation, variables e1
and e2
are replaced with the terms they are bound to.
You can extend desugar
to replace the different unary and binary expressions in the abstract syntax tree
with a uniform representation of these expressions.
Define a rewrite rule desugar
in trans/desugar.str
for every unary or binary operator,
which transforms the original expression into a uniform representation.
To test your transformation, you need to define a builder.
This is done similar to the builder for pretty-printing.
First, import the desugar
module (defined in trans/desugar.str
) into the main chocopy
module.
Then, add the following rewrite rule to trans/chocopy.str
:
editor-desugar:
(_, _, ast, path, project-path) -> (filename, text)
where
filename := <guarantee-extension(|"desugared.aterm")> path ;
text := <desugar-all> ast
This rule follows Spoofax’ convention for strategies which implement editor services. On the left-hand site, it matches a tuple of
path
of the current file andproject path
.On the right-hand site, it instantiates a pair, consisting of a filename
and the designated text
of the file.
Both variables are bound in the where
clause.
The file name is derived from the path of the current file,
while the content of the file is a desugared version of the selected AST node.
You also need to hook your strategy into the editor, making desugaring available in the Syntax menu.
You can do this in editor/Syntax.esv
under the Show Parsed AST action:
action : "Show desugared syntax" = editor-desugar (source)
This rule defines
editor-desugar
.Annotations can be used for different variants of builders:
(openeditor)
from the Syntax menu ensures that a new editor window is opened for the result.(source)
tells Spoofax to run the builder on an unanalysed (and also not desugared) AST.Note that the editor-desugar
rule uses desugar-all
. You should implement this strategy as explained in the next section. Until you do, the menu option will not work.
Rewrite rules typically define local transformations inside an AST. Rewrite rules with the same name define a strategy of this name. Furthermore, strategies can be defined to orchestrate rewrite rules to complex transformations of complete ASTs. A strategy consists of a name and a definition, which is typically a combination of strategy applications. For example, the following strategy orchestrates local desugarings to a desugaring of complete ASTs:
strategies
desugar-all = innermost(desugar)
This strategy is named desugar-all
.
It applies local desugar
rules.
The application is guided by a generic traversal strategy innermost
,
which tries to apply its parameter inside a tree, starting at the leaves (bottom-up, left-to-right).
Whenever an application is successful, the result is traversed again.
Same results can be achieved with different generic traversals.
You should try different traversals in trans/desugar.str
:
desugar-all = innermost(desugar)
desugar-all = topdown(desugar)
desugar-all = topdown(try(desugar))
desugar-all = bottomup(desugar)
desugar-all = bottomup(try(desugar))
desugar-all = alltd(desugar)
Try to understand what is going on and decide for a suitable one.
You can use the library strategy debug
to print the currently visited node.
For example, innermost(debug; desugar)
will debug
all nodes before it tries to desugar
them.
As preparation for the exam, provide an explanation of
Try changing the editor-desugar
rule to use desugar-all
instead of desugar
. If you chose a suitable strategy, the builder should succeed even when no node is selected in the ChocoPy program.