Given some concrete syntax value, how I can I map it to a different type of value (in this case an int)?
// Syntax
start syntax MyTree = \node: "(" MyTree left "," MyTree right ")"
| leaf: Leaf leaf
;
layout MyLayout = [\ \t\n\r]*;
lexical Leaf = [0-9]+;
This does not work unfortunately:
public Tree increment() {
MyTree tree = (MyTree)`(3, (1, 10))`;
return visit(tree) {
case l:(Leaf)`3` => l + 1
};
}
Or is the only way to implode into an ADT where I specified the types?
Your question has different possible answers:
implodeyou can convert a parse tree to an abstract tree. If the constructors of the target abstract language expectint, then lexical trees which happen to match[0-9]+will be automatically converted. For example the syntax tree forsyntax Exp = intValue: IntValue;could be converted to constructordata Exp = intValue(int i);and it will actually build ani.int eval (MyTree t)andint (Leaf l).intback to theLeaf.Example:
First the lexical is converted to a string
"<l>", this is then parsed as anintusingtoInt()and we add 1 using+ 1and then map theintback to a string"< ... >", after which we can call theLeafparser using[Leaf].