Skip to content

Navigation Menu

Sign up

Expressions Blocks Basics

firebaseco edited this page Oct 11, 2011 · 4 revisions

All the expressions begins with the at symbol @ in the key or the name of the JSON property:

Example:

{
 "@return":50
}

Result

50

If the key doesn't have an @ symbol then is not a priest expression, it's a regular JSON value.

Example:

{
 "return":50
}

Result

{
 "return":50
}

Parts

Any expression has at least 3 parts.

  • Key: Name of the expression.
  • hint: (Optional) A string that can be used by the expression.
  • Input: (Optional) a JSON value used as arguments of the expression.

In the following example set is the name of the expression, variable1 is the hint and 2 is the input.

{
 "@set(variable1)": 2
}

The syntax would be like this:

{
 "@<expression-name>(<hint>)": <input>
}

When a expression key is present in a object definition that object becomes an expression-block and no JSON regular keys are allowed anymore.

The following expression-block is illegal, it contains both expressions and regular JSON keys at the same level of the document:

{
 "@set(name)": "Name",
 "regularJSONNumber": 2000 // This is illegal, @set already converted this object into a expression-block.
}

The previous code should be formatted as follows:

{
 "@set(name)": "Name",
 "@return":{
 "regularJSONNumber": 2000
 }
}

And the result would be:

{
 "regularJSONNumber": 2000	
}

The expression block only returns the last result.

Example:

{
 "@return": 2,
 " @return": 5,
}

The result is:

5

Dealing with JSON Keys with the same name

JSON keys must be unique at the same level, if there is a repeated key the parser will ignore it.

Example, the following priest.js is a valid JSON Document but only one @set expression will reach the priest.js compiler becase the parser will ignore the second @set expression:

{
 "@set(x)": 23,
 "@set(x)": 25
 "@return": {
 "@get(x)": null
 }
}

The result will be usually 23 since this is an error of the JSON Document and the parser is free to take any decision. To solve this situation the priest.js compiler will ignore any whitespace before the @ symbol, so for JSON you actually have different keys but for the compiler is a different instruction:

Example:

{
	"@set(x)": 23,
	" @set(x)": 25
 "@return": {
		"@get(x)": null
	}
}

You can use as many white-spaces as you need.

Input Processing

Since the input can be any JSON value it can also become another priest.js expression.

In the following example you can see how the input of the high level @return is actually the result of nested @return Example:

{
 "@return": {
 	"@return": {
			"point": {
				"x": 22.4,
				"y": 45.8
			}
		}
	}
}

The result:

{
 "point": {
		"x": 22.4,
		"y": 45.8
	}
}

Depending of how the expression works the input can be ignored or processed. Conditional expressions like @if and @else uses this technique.

Example:

{
	"@set(chuckNorrisIsHere)": false,
	"@if(chuckNorrisIsHere)": "Chuck is here, You are dead!",
	"@else": "It's never here!, you are safe!"
}

The result is:

"It's never here!, you are safe!"

Thanks for Reading. Please take your time to read other tutorials.

Contributors

If you find any error in this tutorial(misspelling errors, syntax errors, etc) please send me an email with your corrections, I'll be more than happy to add you in this list.

Clone this wiki locally

AltStyle によって変換されたページ (->オリジナル) /