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
Item | Description |
---|---|
Comments |
There are 3 possible comment styles:
## This is a comment
// This is a comment too
/** * 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.
OSL also supports my.dotted.var NOTE: OSL does not support variables with hyphens in them, e.g. commons-logging 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 ({, } ).
|
Item | Description |
---|---|
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" 'Hello world' |
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 |
Function | Description |
---|---|
empty |
Returns true if the expression following is either:
empty(var1) |
size |
Returns the information about the expression:
size("Hello") |
Prints a string to stdout.
print("Hello world") |
|
println |
Prints a string to stdout, followed by a newline.
println("Hello world") |
Item | Description |
---|---|
function declarations |
Functions are declared using one of the following forms:
|
Item | Description |
---|---|
export |
Makes a local variable into a global variable. USER_NAME = "jrichter"; export USER_NAME; Makes |
global |
Declares that a function is global, rather than local. This keyword can only appear immediately before a function declaration. |
Operator | Description |
---|---|
Boolean and |
The usual && operator can be used as well as the word and , e.g.
cond1 and cond2 cond1 && cond2 |
Boolean or |
The usual || operator can be used as well as the word or , e.g.
cond1 or cond2 cond1 || cond2 |
Boolean not |
The usual ! operator can be used as well as the word not , e.g.
!cond1 not cond1 |
Bitwise and |
The usual & operator is used, e.g.
33 & 4 |
Bitwise or |
The usual | operator is used, e.g.
33 | 4 |
Bitwise xor |
The usual ^ operator is used, e.g.
33 ^ 4 |
Bitwise complement |
The usual ~ operator is used, e.g.
~33 |
Equality |
The usual == operator can be used as well as the abbreviation eq .
For example
val1 == val2 val1 eq val2
|
Inequality |
The usual != operator can be used as well as the abbreviation ne .
For example
val1 != val2 val1 ne val2 |
Less Than |
The usual < operator can be used as well as the abbreviation lt .
For example
val1 < val2 val1 lt val2 |
Less Than Or Equal To |
The usual <= operator can be used as well as the abbreviation le .
For example
val1 <= val2 val1 le val2 |
Greater Than |
The usual > operator can be used as well as the abbreviation gt .
For example
val1 > val2 val1 gt val2 |
Greater Than Or Equal To |
The usual >= operator can be used as well as the abbreviation ge .
For example
val1 >= val2 val1 ge val2 |
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 |
Modulus (or remainder) |
The % operator is used. An alternative is the mod
operator.
For example
5 mod 2 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] arr1.0 |
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); 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); 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); 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); 2 2 |
Operator | Description |
---|---|
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 |
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)); } |
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); factorial(4) ) is never executed, because the statement return factorial(5) halts the program immediately.
|