2

I have been struggling with using templates in my JAPE grammars. I was trying to use them in right hand side Java blocks but I understand now that they only work in normal JAPE constructs.

Works:

Phase: FooPhase
Input: Token
Options: control=Appelt

Template: variable_name = "testing"

Rule: foo_1
(
    {Token.string == "foo"}
):annot
--> 
:annot.Foo = {var_name = [variable_name]}

Doesn't work:

Phase: FooPhase
Input: Token
Options: control=Appelt

Template: variable_name = "testing"

Rule: foo_1
(
    {Token.string == "foo"}
):annot
--> 
: annot{
    FeatureMap newFeatures = Factory.newFeatureMap();
    newFeatures.put("var_name", [variable_name]);
    outputAS.add(bindings.get("annot").firstNode(),bindings.get("annot").lastNode(),"Foo", newFeatures);
}
    

1 Answers1

0

A solution that worked for me was to tag the left hand side grammar with an annotation containing the template as a feature, and then extract that feature in a subsequent Java block.

For example:

Phase: FooPhase
Input: Token
Options: control=Appelt

Template: variable_name = "testing"

Rule: foo_1
(
    {Token.string == "foo"}
):annot
--> 
:annot.Foo = {var_name = [variable_name]},
:annot{

    AnnotationSet thisAnnot = bindings.get("annot");
    AnnotationSet tmpFooAnn = inputAS.get("Foo", thisAnnot.firstNode().getOffset(), thisAnnot.lastNode().getOffset());
    String var_name_str     = (String) tmpAnn.iterator().next().getFeatures().get("var_name");
    inputAS.removeAll(tmpFooAnn);

    FeatureMap newFeatures = Factory.newFeatureMap();
    newFeatures.put("var_name", var_name_str);

    outputAS.add(bindings.get("annot").firstNode(),bindings.get("annot").lastNode(),"Foo", newFeatures);
}
    

code simplified for clarity, not tested