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