Original address: http://www.moye.me/2015/05/30/dsl-in-action/
Recently read an interesting book, inspired by some, here to record:
What is the DSLs in action DSL
That is domain-specific language, refers to the business domain model related to the language, rough saying: Line (industry black) words. about what a DSL is, a matter of opinion, for example, I think SQL is a DSL, but some people think it's not.
Use
Most of the world's things are useless to them for the "spat" family, and DSL is no exception; In my case, a set of DSL theory can be used to query the JSON object library, SQL-like syntax, hundreds of lines can be done, use it as follows:
var result = Query (DataSource, ' *.name, *.sex where Who.sex=male && who.name=crap ');
DataSource is a data source similar to this:
var dataSource = { ' 1 ': {who : {name: ' CRAP ', Sex: ' MALE '} , ' 2 ': {who : {name: ' HOLY ', Sex: ' FEMALE ' } }, ' 3 ': {who : {name: ' WEIRD ', Sex: ' FEMALE '}} ;
What to do
The book mentions LR, and I'm sure I didn't understand it, but I was inspired to try it with the bottom-up (bottom-up) that it mentions. So my DSL is probably divided into two parts:
- Generate AST (Abstract syntax tree
- Extract groups by priority (parentheses) from low to high, staging to grouped data
- Grouping branches by (and/or) logical gates, generating a syntax tree AST
- Iterate through the AST from top to bottom to restore and replace previously staged groupings
- Parse the conditional expression at each level of the AST and produce an expression object in place
- Using the AST
- Traversal of data sources: parsing and filtering of logical and conditional expressions based on the AST tree
- Returns only the fields specified by the query statement
Realize
Based on the idea, I used JavaScript to implement this simple DSL, it can use SQL syntax query JSON object, I hope to provide you with some convenience.
- Warehouse Address: Https://github.com/rockdragon/jsonSQL
- Npm:https://www.npmjs.com/package/jsonsql
More articles please visit my blog new address: http://www.moye.me/
[node. js] DSL in action