[node. js] DSL in action

Source: Internet
Author: User

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:

    1. Generate AST (Abstract syntax tree
      1. Extract groups by priority (parentheses) from low to high, staging to grouped data
      2. Grouping branches by (and/or) logical gates, generating a syntax tree AST
      3. Iterate through the AST from top to bottom to restore and replace previously staged groupings
      4. Parse the conditional expression at each level of the AST and produce an expression object in place
    2. Using the AST
      1. Traversal of data sources: parsing and filtering of logical and conditional expressions based on the AST tree
      2. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.