1 Star 1 Fork 0

源码尚云 / syntax-parser

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

syntax-parser

syntax-parser is a parser using pure javascript, so it can both run in browser and nodejs.

CircleCI Status NPM Version Code Coverage

syntax-parser supports:

  • lexer.
  • parser.

Lexer

createLexer can help you create a lexer.

Example

import { createLexer } from 'syntax-parser';

const myLexer = createLexer([
  {
    type: 'whitespace',
    regexes: [/^(\s+)/],
    ignore: true
  },
  {
    type: 'word',
    regexes: [/^([a-zA-Z0-9]+)/]
  },
  {
    type: 'operator',
    regexes: [/^(\+)/]
  }
]);

myLexer('a + b');
// [
//   { "type": "word", "value": "a", "position": [0, 1] },
//   { "type": "operator", "value": "+", "position": [2, 3] },
//   { "type": "word", "value": "b", "position": [4, 5] }
// ]

type

Token type name, you can use any value here, and you will use it in the parser stage.

regexes

Regexes that use to be matched for each Token type.

ignore

The matching Token will not be added to the Token result queue.

In general, whitespace can be ignored in syntax parsing.

Parser

createParser can help you create a parser. Parser requires a lexer.

import { createParser, chain, matchTokenType, many } from 'syntax-parser';

const root = () => chain(addExpr)(ast => ast[0]);

const addExpr = () =>
  chain(matchTokenType('word'), many(addPlus))(ast => ({
    left: ast[0].value,
    operator: ast[1] && ast[1][0].operator,
    right: ast[1] && ast[1][0].term
  }));

const addPlus = () =>
  chain('+', root)(ast => ({
    operator: ast[0].value,
    term: ast[1]
  }));

const myParser = createParser(
  root, // Root grammar.
  myLexer // Created in lexer example.
);

myParser('a + b');
// ast:
// [{
//   "left": "a",
//   "operator": "+",
//   "right": {
//     "left": "b",
//     "operator": null,
//     "right": null
//   }
// }]

chain

Basic grammatical element, support four parameters:

string

String means match token:

chain('select', 'table'); // Match 'select table'

array

Array means 'or':

chain('select', ['table', 'chart']); // Match both 'select table' and 'select chart'

matchTokenType

matchTokenType allow you match Token type defined in lexer.

chain('select', matchTokenType('word')); // Match 'select [any word!]'

function

It's easy to call another chain function:

const a = () => chain('select', b);
const b = () => chain('table');

many/optional

Just as literal meaning:

const a = () => chain('select', optional('table')); // Match both 'select' and 'select table'
const b = () => chain('select', many(',', matchTokenType('word'))); // Match both 'select' and 'select a' and 'select a, b' .. and so on.

optional many can also use chain as parameter. many(chain(..))

The last callback allow partial redefin of local ast:

chain('select', 'table')(
  ast => ast[0] // return 'select'
);

Tests

npm test

Monaco Editor Sql Editor

If you want to see this demo, run this command:

npm run docs

Then select demo Monaco Editor.

MIT License Copyright (c) 2020 傅长路 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

源码阅读 https://github.com/ascoders/syntax-parser 文档:https://github.com/dt-fe/weekly/blob/v2/064.%E7%B2%BE%E8%AF%BB%E3%80%8A%E6%89%8B%E5%86%99%20SQL%20%E7%BC%96%E8%AF%91%E5%99%A8%20-%20%E8%AF%8D%E6%B3%95%E5%88%86%E6%9E%90%E3%80%8B.md 展开 收起
TypeScript
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
TypeScript
1
https://gitee.com/source-code-online/syntax-parser.git
git@gitee.com:source-code-online/syntax-parser.git
source-code-online
syntax-parser
syntax-parser
master

搜索帮助

14c37bed 8189591 565d56ea 8189591