JayData.org

Create code expressions from JavaScript code with the $data.Expressions.CodeParser class

Author: jorsolya April 17th, 2012


The JayData JavaScript Language Query API makes heavy use of language expressions as the means of intercepting storage agnostic developer intention and translating it to storage specific query actions. During the long journey while this JavaScript Language Query expression

gets converted into this sqLite statement

we use the CodeParser class all the way to transform JavaScript code into a more meaningful form: code expressions. Code expressions are effectively many content in a tree or graph that can be traversed (visited) and transformed on our likes.

To create code expression from actual JavaScript functions or simple strings you need an instance of the code parser class.

The CodeParser uses JSLINT at its core to get the abstract syntax tree of the JavaScript code, then based on the AST the JS Code Expressions are created.
You can create expressions from any language element that produces a value, the simplest one being a constant expression / string literal. We can use the CodeParser.createExpression  as it accepts strings as well as functions.
image
Let’s see some examples

Passing code as a string

Simple constant expression.

result:

Its code equivalent is

A simple binary expression

result:

A more complex expression

Indulge me for selecting this awkward way to express true. My intention was to produce a tree of deepness which I could only manage this way.

Passing code as a function

Passing code as an actual JavaScript function has the benefits of lambda and expression parameter support.

image

A simple true predicate

A filter predicate

A projection expression