I'm trying to write a simple template engine but I'm not able to figure out how Antlr can be used for parsing random strings. Antlr seems to be a good choice if you have a syntax but our template engine doesn't have a syntax. It has the following rule-set:
The template engine will have only two syntax: {variableName} and [ expression ](fallback). {variableName} will be replaced with the value of the variable, for [ expression ](fallback expression) if there is any null variable in expression, the whole block will be replaced by the fallback expression.
Here are some of the examples:
1. select date_trunc({segment}, _time), count(*) from table where _time between {date.start} and {date.end}
* The variables segment and date will be replaced, if one of their values is null then we won't render, instead throw an exception.
2. select count(*) from table where [product_id = {product}](true)
* If the value is not set for the variable `product` the output will be:
* select count(*) from table where true
* If the value is set:
* select count(*) from table where product_id = VALUE
Please note that won't really try to parse the whole SQL syntax, instead just replace the values in our script. Is Antlr a good choice for writing such template? I took a look at the example grammars but couldn't find a good one to start with so I'm a bit stuck here. I few keywords for me to search would be really helpful.