tango provides a C-like syntax for logical and relational expressions, parsing them directly into Mango selectors.
In my previous post, I introduced couchilla, a command-line tool for bundling CouchDB design documents. While MapReduce views are powerful, the /{db}/_find
API typically provides faster querying in most cases by using Mango selector expressions.
For instance, in Mango, you'd express a query for "movies directed by Roy Andersson after 2007" with this JSON structure:
{
"$and": [
{
"director": {
"$eq": "Roy Andersson"
}
},
{
"year": {
"$gt": 2007
}
}
]
}
... which is, as you can see, a hand-written AST!
With tango, you could write this instead:
director == "Roy Andersson" && year > 2007
This syntax includes standard C operators and supports parentheses to specify explicit precedence. At present, tango does not support unary operators or the $in
operator for matching elements within arrays.
📄 See the lexer implementation in
scan.js
.
To parse Tango is to turn it into Mango
The shunting yard algorithm is a linear-time algorithm for parsing expressions using operator precedence parsing. It employs a stack to manage operators and a queue to construct an abstract syntax tree, which, in this context, becomes a Mango expression.
📄 See the parser implementation in
parse.js
.