惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Jina AI
Jina AI
I
Intezer
F
Fortinet All Blogs
S
SegmentFault 最新的问题
罗磊的独立博客
V
Visual Studio Blog
V
V2EX
大猫的无限游戏
大猫的无限游戏
The Cloudflare Blog
J
Java Code Geeks
美团技术团队
B
Blog
U
Unit 42
F
Full Disclosure
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
P
Privacy International News Feed
G
Google Developers Blog
雷峰网
雷峰网
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy & Cybersecurity Law Blog
T
Tor Project blog
酷 壳 – CoolShell
酷 壳 – CoolShell
量子位
GbyAI
GbyAI
S
Schneier on Security
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Google DeepMind News
Google DeepMind News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
L
LINUX DO - 热门话题
Recorded Future
Recorded Future
D
Docker
博客园 - 聂微东
Project Zero
Project Zero
Know Your Adversary
Know Your Adversary
P
Palo Alto Networks Blog
K
Kaspersky official blog
Martin Fowler
Martin Fowler
H
Hackread – Cybersecurity News, Data Breaches, AI and More
L
Lohrmann on Cybersecurity
A
Arctic Wolf
T
The Blog of Author Tim Ferriss
Microsoft Security Blog
Microsoft Security Blog
T
Threat Research - Cisco Blogs
T
The Exploit Database - CXSecurity.com
V
Vulnerabilities – Threatpost
Simon Willison's Weblog
Simon Willison's Weblog
Cisco Talos Blog
Cisco Talos Blog
T
Threatpost
Hugging Face - Blog
Hugging Face - Blog
博客园_首页

博客园 - 莫耶

学习RxJS:Cycle.js 学习RxJS: 导入 [Node.js] Node.js项目的持续集成 [Node.js] 使用node-forge保障Javascript应用的传输安全 [Node.js] 对称加密、公钥加密和RSA [Node.js] 使用TypeScript编写Node项目 [Node.js] Node.js中的流 [Node.js] 基于Socket.IO 的私聊 [Node.js] 闭包和高阶函数 [Node.js] Promise,Q及Async [Node.js] Express的测试覆盖率 [Node.js] BDD和Mocha框架 [Node.js] 也说this [Node.js] ECMAScript 6中的生成器及koa小析 [Node.js] 使用File API 异步上传文件 [Node.js] OAuth 2 和 passport框架 [Node.js] Node + Redis 实现分布式Session方案 [Node.js] Cluster,把多核用起来 Python开源框架Scrapy安装及使用
[Node.js] DSL in action
莫耶 · 2015-05-30 · via 博客园 - 莫耶

2015-05-30 20:46  莫耶  阅读(1749)  评论()    收藏  举报

原文地址:http://www.moye.me/2015/05/30/dsl-in-action/

最近看了本有意思的书,受到了一些启发,在此记录一下:

DSLs in action

DSL in Action

DSL是什么

即 domain-specific language ,是指和业务域模型相关的语言,粗糙的说法:行(业黑)话。关于什么是DSL,见仁见智,比如我认为SQL是一种DSL,有人却认为不是。

用途

对于“然并卵”一族来说,世界上大多数事情对他们来说都没什么用,DSL也不例外;于我而言,用DSL的一套理论能实现一个查询JSON对象的库,类SQL的语法,几百行就能搞定,使用起来像是这样的:

var result = Query(dataSource, '*.name, *.sex where who.sex=MALE && who.name=CRAP');

dataSource是类似这样的数据源:

var dataSource = {
    '1': {
        who: {name: 'CRAP', sex: 'MALE'}
    },
    '2': {
        who: {name: 'HOLY', sex: 'FEMALE'}
    },
    '3': {
        who: {name: 'WEIRD', sex: 'FEMALE'}
    }
};

怎么做

书中提到了LR,我确信自己没有理解这个概念,但是受到了启发,决定用它提到的Bottom-up(自底向上)的方式试试。所以我的DSL大概分两部分:

  1. 生成AST(抽象句法树
    1. 按优先级(括号)从低到高提取分组,暂存到分组数据
    2. 按(与/或)逻辑门分组支干,生成语法树AST
    3. 由顶向下遍历AST,对之前暂存的分组进行还原替换
    4. 对AST每一级的条件表达式进行解析并原地产生表达式对象
  2. 使用AST
    1. 对数据源进行遍历:根据AST树,进行与或逻辑及条件表达式的解析筛选
    2.  只返回查询语句指定的字段

DSL in action

实现

基于如上思路,我用Javascript实现了这个简单的DSL,它能使用SQL的语法查询JSON对象,希望能为您提供一些方便。

更多文章请移步我的blog新地址: http://www.moye.me/ 

Creative Commons License