Expressions

< CLI Reference Sets & Lists >

What are terp expressions?

At the heart of terp's capabilities is an expression language that allows you to take full advantage of Java's features, customized and expanded with features that are important when the predominant usage is text-related.

terp expressions evaluate to typed objects, not to strings. This is an important distinction, particularly if you are mainly interested in using terp from ANT. In ANT all your variables (properties) have string values and you always access the string value by enclosing your property name in ${}. In terp you can do the same thing and we call this an embedded expression. An embedded expression is essentially a terp expression enclosed in ${}. The ${} causes the expression to be converted to a string value, just like a property is converted to its value in ANT.

Like any expression language, terp's expression language supports arithmetic operators, integer and floating point literals, and character and string literals. It also supports a number of additional literals, like collections and maps. It has a unified syntax for ranges, selectors, transformers, and formatters.

Importantly, you can also access a Java object's properties and call its methods.

Examples of terp expressions include:

{0,1,2,3}[join(',')]
'this is a test'[split('[ ]')]
3+45*pi
name='world'
'hello ' + name + "!"
'hello'[0..2][upper]    
^file('test.cpp').getParentFile().name

Numeric Literals

terp supports integers, longs, floats, and doubles. Any integer number without a modifier is interpreted as an integer. Appending an 'L' makes it a long. Similarly, any floating point number without a modifier is a double. Appending an 'f' makes it a float.

Examples

13                 an Integer literal
13L                a Long literal
2.5E-4             a Double literal
2.503f             a Float literal

Character and String literals

In many higher level languages, character literals are enclosed by single quotes and string literals by double quotes. That is also the convention in terp™. Additionally though, terp supports string literals enclosed in single quotes. When the enclosed item is of length 1 it is interepreted as a character literal, otherwise as a string literal.

While this adds potential confusion it makes terp much easier to use in embedded use cases like ANT, because string literal terminators don't need to be escaped with a backslash.

The usual escape sequences are supported in character literals as well as in string literals:

  • \b bell
  • \n newline
  • \r return
  • \t tab
  • \ (octal digits)
  • \u (4digit hex number)

Sometimes you want to avoid backslashes from being interpreted as the escape character, for example when strings contain a Windows file path. You can prefix the literal with an @ character to turn the backslashes contained within into normal characters.

Examples

"This is a test"     a String literal
'\t is a tab'        a String literal
@'\t is not a tab'   a String literal 
"a"                  a String literal
'a'                  a Character literal
'\n'                 a Character literal
'\740'               a Character literal
'\u00A0'             a Character literal

Identifiers

terp identifiers are a little different from what you are used to. terp supports the period (dot) in identifiers. For example com.codemesh.terp.testVar is a legal, non-hierarchical identifier. This causes potential ambiguity because the period is also used to separate name components in hierarchical identifiers. the scope terp explicitly allows this ambiguity. Why?

terp is designed to support a lot of external tools, including Java. It is very common for Java system properties to include periods in their names. We want to support such identifiers in terp without requiring terp users to be aware of special escape mechanisms or access policies.

Extending terp programmatically

terp stands for "Modular Template Interpreter" and that implies that it is extensible. See an example of a programmatic extension of terp's built-in features in the doc/examples/extend directory.


Copyright 2006-2016 by Codemesh, Inc., ALL RIGHTS RESERVED

:
terp expressions
codemesh.com home expressions templates ant about us contact us download   

Commandline