docs: update visitor demo and listener demo
This commit is contained in:
parent
d4c97ef833
commit
447ab7f331
@ -156,27 +156,38 @@ console.log(tokens)
|
|||||||
|
|
||||||
使用 Visitor 模式访问 AST 中的指定节点
|
使用 Visitor 模式访问 AST 中的指定节点
|
||||||
|
|
||||||
```javascript
|
```typescript
|
||||||
import { MySQL, MySqlParserVisitor } from 'dt-sql-parser';
|
import { MySQL, AbstractParseTreeVisitor } from 'dt-sql-parser';
|
||||||
|
import type { MySqlParserVisitor } from 'dt-sql-parser';
|
||||||
|
|
||||||
const parser = new MySQL()
|
const parser = new MySQL();
|
||||||
const sql = `select id,name from user1;`
|
const sql = `select id,name from user1;`;
|
||||||
// parseTree
|
const tree = parser.parse(sql);
|
||||||
const tree = parser.parse(sql)
|
|
||||||
class MyVisitor extends MySqlParserVisitor {
|
type Result = string;
|
||||||
// 重写 visitTableName 方法
|
|
||||||
|
class MyVisitor extends AbstractParseTreeVisitor<Result> implements MySqlParserVisitor<Result> {
|
||||||
|
protected defaultResult() {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
visitTableName(ctx) {
|
visitTableName(ctx) {
|
||||||
let tableName = ctx.getText().toLowerCase()
|
let tableName = ctx.text.toLowerCase();
|
||||||
console.log('TableName', tableName)
|
console.log('TableName', tableName);
|
||||||
|
return '';
|
||||||
}
|
}
|
||||||
// 重写 visitSelectElements 方法
|
|
||||||
visitSelectElements(ctx) {
|
visitSelectElements(ctx) {
|
||||||
let selectElements = ctx.getText().toLowerCase()
|
let selectElements = ctx.text.toLowerCase();
|
||||||
console.log('SelectElements', selectElements)
|
console.log('SelectElements', selectElements);
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
visitProgram(ctx) {
|
||||||
|
return 'Return by program node'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const visitor = new MyVisitor()
|
const visitor = new MyVisitor();
|
||||||
visitor.visit(tree)
|
const result = visitor.visit(tree);
|
||||||
|
|
||||||
|
console.log(result);
|
||||||
```
|
```
|
||||||
|
|
||||||
*输出:*
|
*输出:*
|
||||||
@ -186,6 +197,9 @@ visitor.visit(tree)
|
|||||||
SelectElements id,name
|
SelectElements id,name
|
||||||
TableName user1
|
TableName user1
|
||||||
*/
|
*/
|
||||||
|
/*
|
||||||
|
Return by program node
|
||||||
|
*/
|
||||||
```
|
```
|
||||||
|
|
||||||
> 提示:使用 Visitor 模式时,节点的方法名称可以在对应 SQL 目录下的 Visitor 文件中查找
|
> 提示:使用 Visitor 模式时,节点的方法名称可以在对应 SQL 目录下的 Visitor 文件中查找
|
||||||
@ -194,25 +208,26 @@ TableName user1
|
|||||||
|
|
||||||
Listener 模式,利用 [ANTLR4](https://github.com/antlr/antlr4) 提供的 `ParseTreeWalker` 对象遍历 AST,进入各个节点时调用对应的方法。
|
Listener 模式,利用 [ANTLR4](https://github.com/antlr/antlr4) 提供的 `ParseTreeWalker` 对象遍历 AST,进入各个节点时调用对应的方法。
|
||||||
|
|
||||||
```javascript
|
```typescript
|
||||||
import { MySQL, MySqlParserListener } from 'dt-sql-parser';
|
import { MySQL } from 'dt-sql-parser';
|
||||||
|
import type { MySqlParserListener } from 'dt-sql-parser';
|
||||||
|
|
||||||
const parser = new MySQL();
|
const parser = new MySQL();
|
||||||
const sql = 'select id,name from user1;'
|
const sql = 'select id,name from user1;';
|
||||||
// parseTree
|
const parseTree = parser.parse(sql);
|
||||||
const tree = parser.parse(sql)
|
|
||||||
class MyListener extends MySqlParserListener {
|
class MyListener implements MySqlParserListener {
|
||||||
enterTableName(ctx) {
|
enterTableName(ctx) {
|
||||||
let tableName = ctx.getText().toLowerCase()
|
let tableName = ctx.text.toLowerCase();
|
||||||
console.log('TableName', tableName)
|
console.log('TableName:', tableName);
|
||||||
}
|
}
|
||||||
enterSelectElements(ctx) {
|
enterSelectElements(ctx) {
|
||||||
let selectElements = ctx.getText().toLowerCase()
|
let selectElements = ctx.text.toLowerCase();
|
||||||
log('SelectElements', selectElements)
|
console.log('SelectElements:', selectElements);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const listenTableName = new MyListener();
|
const listenTableName = new MyListener();
|
||||||
parser.listen(listenTableName, tree);
|
parser.listen(listenTableName as MySqlParserListener, parseTree);
|
||||||
```
|
```
|
||||||
|
|
||||||
*输出:*
|
*输出:*
|
||||||
|
76
README.md
76
README.md
@ -163,35 +163,49 @@ console.log(tokens)
|
|||||||
|
|
||||||
Traverse the tree node by the Visitor:
|
Traverse the tree node by the Visitor:
|
||||||
|
|
||||||
```javascript
|
```typescript
|
||||||
import { MySQL, MySqlParserVisitor } from 'dt-sql-parser';
|
import { MySQL, AbstractParseTreeVisitor } from 'dt-sql-parser';
|
||||||
|
import type { MySqlParserVisitor } from 'dt-sql-parser';
|
||||||
|
|
||||||
const parser = new MySQL()
|
const parser = new MySQL();
|
||||||
const sql = `select id,name from user1;`
|
const sql = `select id,name from user1;`;
|
||||||
// parseTree
|
const tree = parser.parse(sql);
|
||||||
const tree = parser.parse(sql)
|
|
||||||
class MyVisitor extends MySqlParserVisitor {
|
type Result = string;
|
||||||
// overwrite visitTableName
|
|
||||||
|
class MyVisitor extends AbstractParseTreeVisitor<Result> implements MySqlParserVisitor<Result> {
|
||||||
|
protected defaultResult() {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
visitTableName(ctx) {
|
visitTableName(ctx) {
|
||||||
let tableName = ctx.getText().toLowerCase()
|
let tableName = ctx.text.toLowerCase();
|
||||||
console.log('TableName', tableName)
|
console.log('TableName:', tableName);
|
||||||
|
return '';
|
||||||
}
|
}
|
||||||
// overwrite visitSelectElements
|
|
||||||
visitSelectElements(ctx) {
|
visitSelectElements(ctx) {
|
||||||
let selectElements = ctx.getText().toLowerCase()
|
let selectElements = ctx.text.toLowerCase();
|
||||||
console.log('SelectElements', selectElements)
|
console.log('SelectElements:', selectElements);
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
visitProgram(ctx) {
|
||||||
|
return 'Return by program node'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const visitor = new MyVisitor()
|
const visitor = new MyVisitor();
|
||||||
visitor.visit(tree)
|
const result = visitor.visit(tree);
|
||||||
|
|
||||||
|
console.log(result);
|
||||||
```
|
```
|
||||||
|
|
||||||
*output:*
|
*output:*
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
/*
|
/*
|
||||||
SelectElements id,name
|
SelectElements: id,name
|
||||||
TableName user1
|
TableName: user1
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
Return by program node
|
||||||
*/
|
*/
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -201,34 +215,34 @@ TableName user1
|
|||||||
|
|
||||||
Access the specified node in the AST by the Listener
|
Access the specified node in the AST by the Listener
|
||||||
|
|
||||||
```javascript
|
```typescript
|
||||||
import { MySQL, MySqlParserListener } from 'dt-sql-parser';
|
import { MySQL } from 'dt-sql-parser';
|
||||||
|
import type { MySqlParserListener } from 'dt-sql-parser';
|
||||||
|
|
||||||
const parser = new MySQL();
|
const parser = new MySQL();
|
||||||
const sql = 'select id,name from user1;'
|
const sql = 'select id,name from user1;';
|
||||||
// parseTree
|
const parseTree = parser.parse(sql);
|
||||||
const tree = parser.parse(sql)
|
|
||||||
class MyListener extends MySqlParserListener {
|
class MyListener implements MySqlParserListener {
|
||||||
enterTableName(ctx) {
|
enterTableName(ctx) {
|
||||||
let tableName = ctx.getText().toLowerCase()
|
let tableName = ctx.text.toLowerCase();
|
||||||
console.log('TableName', tableName)
|
console.log('TableName:', tableName);
|
||||||
}
|
}
|
||||||
enterSelectElements(ctx) {
|
enterSelectElements(ctx) {
|
||||||
let selectElements = ctx.getText().toLowerCase()
|
let selectElements = ctx.text.toLowerCase();
|
||||||
console.log('SelectElements', selectElements)
|
console.log('SelectElements:', selectElements);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const listenTableName = new MyListener();
|
const listenTableName = new MyListener();
|
||||||
parser.listen(listenTableName, tree);
|
parser.listen(listenTableName as MySqlParserListener, parseTree);
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
*output:*
|
*output:*
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
/*
|
/*
|
||||||
SelectElements id,name
|
SelectElements: id,name
|
||||||
TableName user1
|
TableName: user1
|
||||||
*/
|
*/
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user