Log in Help
Print
Homewikijape-repository 〉 shorthands.html
 

JAPE Shorthands

Contents

1. Copy a feature value from the LHS to RHS

Rule: Rule1
(
  {Lookup.majorType == "location"}
):look
-->
:look.Location = { kind = :look.Lookup.minorType }

This only works if you know that there is only one annotation of type Lookup in the look binding.

2. Use matched annotations in a Java RHS

This:

Rule: Rule2
(
  {Token}*
):tok
-->
:tok{
  // some Java code here
}

is equivalent to this:

Rule: Rule2
(
  {Token}*
):tok
-->
{
  AnnotationSet tokAnnots = (AnnotationSet)bindings.get("tok");
  if(tokAnnots != null && tokAnnots.size() != 0) {
    // some Java code here
  }
}

i.e. you can use tokAnnots in your Java code, and you can be sure that it isn't empty when you use it.

3. Using optional annotations

Rule: MeasureSpan
/* 
 * Matches loosely an interval.
 */
  ({Token.kind == "word"})?:before
(
  ({Number}):amount1
  ((UNIT):unit1({Token.string == "."})?
  )?
  ({Token.string == "-"}
  |{Token.string == "±"}
  |({Token.string == "+"}{Token.string == "/"}{Token.string == "-"})
  ):conj
 ({Number}):amount2
 (UNIT)?:unit2
):span
-->
:amount1.Measurement = { type = "scalarValue"},
:unit1.Measurement = { type = "unit"},
:amount2.Measurement = { type = "scalarValue"},
:unit2.Measurement = { type = "unit"},
:span.Measurement = { type = "interval",
                      conj = :conj.Token.string,
                      before = :before.Token.string }

Warning: in the case of ':conj.Token.string' you get only one of the matched Tokens, and can't guarantee which one. This shorthand is best reserved for cases where you know the binding contains only one annotation.