ascoders / syntax-parser Goto Github PK
View Code? Open in Web Editor NEWLight and fast 🚀parser! With zero dependents. - Sql Parser Demo added!
License: MIT License
Light and fast 🚀parser! With zero dependents. - Sql Parser Demo added!
License: MIT License
mysql 词法解析器中匹配注释时,换行符使用的是\n
。
syntax-parser/src/demo/sql-parser/mysql/lexer.ts
Lines 9 to 16 in 5818f52
在 monaco-editor 编辑器默认的换行符是跟随内容(EndOfLinePreference),我这边默认是\r\n
,会导致无法匹配到注释。
重现代码如下(在线示例):
const editor = monaco.editor.create(document.getElementById("container"), {
value: "--\r\n",
language: "javascript"
});
function test(input) {
console.log(input.split(''), input.match(/^((?:#|--).*?(?:\n|$))/))
}
test(editor.getModel()?.getValue());
test(editor.getModel()?.getValue(monaco.editor.EndOfLinePreference.CRLF));
test(editor.getModel()?.getValue(monaco.editor.EndOfLinePreference.LF));
解决办法:
EndOfLinePreference.LF
选项,这样返回的内容使用\n
换行符。/^((?:#|--).*?(?:\r\n|\n|$))/
,兼容\n
和\r\n
换行符INSERT INTO
rds_open_api_log
(
【 id
,
created_at
,
updated_at
,
remote_ip
,
access_key
,
action
,
content
】
)
VALUES
(
【 2,
'2022-01-14 18:25:46',
'2022-01-14 18:25:46',
'',
'ddddddd',
'ListBusinessAction_v1',
'{"accessKey": "ddddddd", "ts": 1234567890, "s": "ADBCDEE"}'】
);
是在我找不到的地方吗?
还是需要人一起写??
node版本: v8.11.3
步骤:
npm i syntax-parser --save
代码
const { createLexer } = require('syntax-parser');
const myLexer = createLexer([
{
type: 'whitespace',
regexes: [/^(\s+)/],
ignore: true
},
{
type: 'word',
regexes: [/^([a-zA-Z0-9]+)/]
},
{
type: 'operator',
regexes: [
/^(\(|\))/, // '(' ')'.
/^(\+|\-)/ // operators for + -.
]
}
]);
console.log(myLexer('a + b'));
module.js:549
throw err;
^
Error: Cannot find module 'syntax-parser'
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._load (module.js:474:25)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (~/sqlweb/test.js:2:25)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
我稍微修改了一下四则运算的例子,用递归声明文法, 发现死循环了。 是我的语法不对吗
const myLexer = createLexer([
{
type: 'whitespace',
regexes: [/^(\s+)/],
ignore: true
},
{
type: 'word',
regexes: [/^([a-zA-Z0-9]+)/]
},
{
type: 'add',
regexes: [/^(\+)/]
},
{
type: 'minus',
regexes: [/^(\-)/]
},
{
type: 'times',
regexes: [/^(\*)/]
},
{
type: 'div',
regexes: [/^(\/)/]
},
{
type: 'lparen',
regexes: [/^(\()/]
},
{
type: 'lparen',
regexes: [/^(\))/]
}
]);
const addExpr= () => chain([
chain(matchTokenType('word'), '+', matchTokenType('word'))(ast => ({
left: ast[0],
operator: '+',
right: ast[2]
})),
chain(matchTokenType('word'), '-', matchTokenType('word')) (ast => ({
left: ast[0],
operator: '-',
right: ast[2]
})),
chain(addExpr, '+', matchTokenType('word')) (ast => ({
left: ast[0],
operator: '+',
right: ast[2]
})),
chain(addExpr, '-', matchTokenType('word')) (ast => ({
left: ast[0],
operator: '-',
right: ast[2]
}))
]) (ast => ast[0])
const root = () => chain(addExpr)(ast => ast[0]);
const myParser = createParser(
root, // Root grammar.
myLexer // Created in lexer example.
);
console.log(JSON.stringify(myParser('a + b - c').ast))
最近在研究web版sql编辑器相关的东西,看到你好像做过很多相关的东西。想问下
在 src/demo/sql-parser/mysql/parser.ts 中,博主的的语法是这样的:
chain('insert', optional('ignore'), 'into', tableName, optional(selectFieldsInfo),[selectStatement])
这好像与mysql的语法:
INSERT INTO table_name (column1,column2,column3,...) VALUES (value1,value2,value3,...)
不太符合。
博主有时间麻烦修改一下,或者看看我发起的这个Pull request ##9 ,请指教。
English:
Hello,
I'm developing an application based on your repository on GitHub. I respect and appreciate your work greatly. I've noticed that your repository does not include a LICENSE file, and I'm a bit unsure about how I can properly and legally use and modify your code. If it's possible, could you please add an open-source license to clearly outline the terms of use and distribution of your code? This will not only protect your rights but also provide clear guidance for other users. Thank you very much for your understanding and support.
Looking forward to your response.
Thank you
中文:
您好,
我正在基于您在GitHub上的仓库创建我的应用。我非常尊重并欣赏您的工作。我注意到,您的仓库没有包含LICENSE文件,因此对于我如何正确和合法地使用并修改您的代码,我感到有些困惑。如果可能的话,您是否可以添加一个开源许可证,以明确说明对您代码的使用和分发条件?这样不仅可以保护您的权益,同时也能为其他使用者提供明确的指引。非常感谢您的理解和支持。
期待您的回复。
谢谢
你好~有几个问题想请教下
1.想问下这个项目是否有实际的项目有在使用呢?
2.注意到阿里云的dataWorks编辑器也有语法解析的功能,那么和这个项目有联系吗?
:D
Error: Cannot find module 'update-notifier'
npm run docs 跑不起来且在package.json里面的命令都无法运行项目
执行pnpm docs
,link 到 https://www.npmjs.com/package/syntax-parser 这个地方了。没有debugger
编辑器界面
当 SQL 语句前面存在空白行时,解析结果中的 error.suggestions.token 为null
,导致无法正确提示发生错误的位置,现象如下:
解析结果如下:
{
"success": false,
"ast": null,
"cursorKeyPath": [],
"nextMatchings": [
{
"type": "string",
"value": "UPDATE"
},
{
"type": "string",
"value": "set"
},
{
"type": "string",
"value": "insert"
},
{
"type": "string",
"value": "create"
},
{
"type": "string",
"value": "select"
}
],
"error": {
"suggestions": [
{
"type": "string",
"value": "select"
},
{
"type": "string",
"value": "create"
},
{
"type": "string",
"value": "insert"
},
{
"type": "string",
"value": "set"
},
{
"type": "string",
"value": "UPDATE"
}
],
"token": null,
"reason": "incomplete"
},
"debugInfo": {
"tokens": [
{
"type": "word",
"value": "selec",
"position": [
2,
6
]
}
],
"callVisiterCount": 38,
"costs": {
"lexer": 0,
"parser": 0
}
}
}
Demo 中关于错误提示位置的处理:
syntax-parser/src/demo/monaco-plugin/index.ts
Lines 60 to 72 in 5818f52
期望结果:
未完整的语句,error.suggestion.token 能给出错误位置的token,如{type: "string", value: "selec", position: [1, 6]}
我按照首页的解析四则运算表达时的例子运行一下发现打印的语法书不全。
import { createLexer } from './lexer'
import { chain, createParser, many, matchTokenType } from './parse'
const myLexer = createLexer([
{
type: 'whitespace',
regexes: [/^(\s+)/],
ignore: true
},
{
type: 'word',
regexes: [/^([a-zA-Z0-9]+)/]
},
{
type: 'operator',
regexes: [/^(\+)/]
}
]);
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.
);
console.log(myParser('a + b + c + d + f + e + h').ast)
打印的结果是
{ left: 'a',
operator: '+',
right:
{ left: 'b',
operator: '+',
right: { left: 'c', operator: '+', right: [Object] } } }
要使用sql提示,看到monacoSqlAutocomplete放在demo里,是否测试过可以导入正常使用
在 TS 项目中使用时没有类型提示。
有些着急,13581780960 我微信号,希望帮助下,感谢🙏
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.