Expression Syntax

Many thanks to the developers of Jexl. Much of this documentation is stolen word-for-word from their documentation at http://jakarta.apache.org/commons/jexl/reference/syntax.html

 

Language Elements

ItemDescription
Comments

There are 3 possible comment styles:

  • Specified using ## and extend to the end of line, e.g.
## This is a comment
  • C-style single-line comments, specified using // and extend to the end of line, e.g.
// This is a comment too
  • C-style block comments, beginning with /* and ending with */ 
/**
 * This is a
 * multi-line
 * comment
 */
Identifiers / variables Must start with a-z, A-Z, _ or $. Can then be followed by 0-9, a-z, A-Z, _ or $. e.g.
  • Valid: var1,_a99,$1
  • Invalid: 9v,!a99,1$

OSL also supports ant-style variables, e.g.

my.dotted.var
is a valid variable name.

NOTE: OSL does not support variables with hyphens in them, e.g.

commons-logging
is not a valid variable, but instead is treated as a subtraction of the variable logging from the variable commons

Scripts A script in OSL is made up of zero or more statements.
Statements A statement can be the empty statement, the semicolon (;) , block, assignment or an expression. Statements are optionally terminated with a semicolon.
Block A block is simply multiple statements inside curly braces ({, }).

Literals

ItemDescription
Integer Literals 1 or more digits from 0 to 9
Floating point Literals 1 or more digits from 0 to 9, followed by a decimal point and then one or more digits from 0 to 9.
String literals Can start and end with either ' or "e;, e.g.
"Hello world"
and
'Hello world'
are equivalent.
Boolean literals The literals true and false can be used, e.g.
val1 == true
Null literal The null value is represented as in java using the literal null, e.g.
val1 == null

Built-In Functions

FunctionDescription
empty Returns true if the expression following is either:
  1. null
  2. An empty string
  3. An array of length zero
  4. A collection of size zero
  5. An empty map
empty(var1)
size Returns the information about the expression:
  1. Length of an array
  2. Size of a List
  3. Size of a Map
  4. Size of a Set
  5. Length of a string
size("Hello")
returns 5.
print Prints a string to stdout.
print("Hello world")
Prints out the phrase Hello world. Returns null.
println Prints a string to stdout, followed by a newline.
println("Hello world")
Prints out the phrase Hello world and a newline. Returns null.

Function Declarations

ItemDescription
function declarations

Functions are declared using one of the following forms:

  • function <function_name> ( <argument1>, <argument2>, ... <argumentN> ) {
      <function_body>
    }
  • function <function_name> ( <argument_list_name> * ) {
      <function_body>
    }
  • function <function_name> () {
      <function_body>
    }

Function declarations must appear at the beginning of a file, before any other statements. See function calls and arguments for more information on the use of these different forms.

Scope Modifiers

ItemDescription
export

Makes a local variable into a global variable.

USER_NAME = "jrichter";
export USER_NAME;

Makes USER_NAME a global variable with the value "jrichter".

global

Declares that a function is global, rather than local. This keyword can only appear immediately before a function declaration.

Operators

OperatorDescription
Boolean and The usual && operator can be used as well as the word and, e.g.
cond1 and cond2
and
cond1 && cond2
are equivalent
Boolean or The usual || operator can be used as well as the word or, e.g.
cond1 or cond2
and
cond1 || cond2
are equivalent
Boolean not The usual ! operator can be used as well as the word not, e.g.
!cond1
and
not cond1
are equivalent
Bitwise and The usual & operator is used, e.g.
33 & 4
, 0010 0001 & 0000 0100 = 0.
Bitwise or The usual | operator is used, e.g.
33 | 4
, 0010 0001 | 0000 0100 = 0010 0101 = 37.
Bitwise xor The usual ^ operator is used, e.g.
33 ^ 4
, 0010 0001 ^ 0000 0100 = 0010 0100 = 37.
Bitwise complement The usual ~ operator is used, e.g.
~33
, ~0010 0001 = 1101 1110 = -34.
Equality The usual == operator can be used as well as the abbreviation eq. For example
val1 == val2
and
val1 eq val2
are equivalent.
  1. null is only ever equal to null, that is if you compare null to any non-null value, the result is false.
  2. Equality uses the java equals method
Inequality The usual != operator can be used as well as the abbreviation ne. For example
val1 != val2
and
val1 ne val2
are equivalent.
Less Than The usual < operator can be used as well as the abbreviation lt. For example
val1 < val2
and
val1 lt val2
are equivalent.
Less Than Or Equal To The usual <= operator can be used as well as the abbreviation le. For example
val1 <= val2
and
val1 le val2
are equivalent.
Greater Than The usual > operator can be used as well as the abbreviation gt. For example
val1 > val2
and
val1 gt val2
are equivalent.
Greater Than Or Equal To The usual >= operator can be used as well as the abbreviation ge. For example
val1 >= val2
and
val1 ge val2
are equivalent.
Addition The usual + operator is used. For example
val1 + val2
Subtraction The usual - operator is used. For example
val1 - val2
Multiplication The usual * operator is used. For example
val1 * val2
Division The usual / operator is used. For example
val1 / val2
Integer Division The div operator is used. For example
4 div 3
gives 1.
Modulus (or remainder) The % operator is used. An alternative is the mod operator. For example
5 mod 2
gives 1 and is equivalent to
5 % 2
Negation The unary - operator is used. For example
-12
Array access Array elements may be accessed using either square brackets or a dotted numeral, e.g.
arr1[0]
and
arr1.0
are equivalent
Assignment The usual = operator is used. The item to the left of the = must be a variable name or array reference. The value to the right may be any expression. For example
x = 3
Post-increment (++) The post increment ++ operator adds 1 to a variable. The value of a post-incremented variable is the variable's value before 1 was added. For example:
x = 3;
print(x++);
print(x);
would print:
3
4


Post-decrement (--) The post decrement  -- operator subtracts 1 from a variable. The value of a post-decremented variable is the variable's value before 1 was subtracted. For example:
x = 3;
print(x--);
print(x);
would print:
3
2s


(++) Pre-increment The ++ pre increment operator adds 1 to a variable. The value of a post-incremented variable is the variable's value plus 1. For example:
x = 3;
print(++x);
print(x);
would print:
4
4


(--) Pre-decrement The pre decrement operator subtracts 1 from a variable. The value of a pre-decremented variable is the variable's value minus 1. For example:
x = 3;
print(--x);
print(x);
would print:
2
2


Flow Control

OperatorDescription
if Classic, if/else statement, e.g. if ((x * 2) == 5) {y = 1;} else {y = 2;}
foreach Loop through items of an Array, Collection, Map, Iterator or Enumeration, e.g. foreach (item in list) { x = x + item; } Where item and list are variables.
for Loops while updating the value of an iterator variable until a condition is satisfied. This statement works exactly like a Java or C for statement, e.g.
for (i= 0;i <  10; i=i+1) {
  print(x+" squared = "+(x*x));
}

Where i is the iteration variable. The code above prints the numbers from zero to 9 and their squares.


Note that both the initializer and the increment section of the for statement can be left blank. The condition does not need to refer to the increment variable.

while Loop until a condition is satisfied, e.g. while (x lt 10) { x = x + 2; }
break Immediately breaks execution of the current loop, and jumps to the end of the loop structure. This must be used inside of a for, foreach, or while loop.
for (i= 0;i <  10; i=     i+1){ if
  (x*x >  20)
    break;
  print(x+" squared = "+(x*x));
}
The code above prints the numbers from zero to 4 and their squares. The loop is halted when x equals 4, because of the break statement. The break statement always returns null.
return Immediately ends execution of the current code context and returns a value.
function factorial(x) { if
  (x < = 2)
    return x;
  else
    return x * factorial(x - 1);
}

return factorial(5);
factorial(4);
The code above returns the value 120. The final statement (factorial(4)) is never executed, because the statement return factorial(5) halts the program immediately.