ANTLR-与javascript匹配后如何解析规则并返回数组

时间:2019-07-04 12:16:47

标签: javascript antlr antlr4

我有这个语法:

grammar Cpl;

fragment A: ('A' | 'a');
fragment E: ('E' | 'e');
fragment M: ('M' | 'm');
fragment N: ('N' | 'n');

NAME: N A M E;
WHITESPACE: (' ' | '\t')+;
OPERATOR: '<' | '=' | '>';
DIGIT: '0' ..'9';
LETTER: ('a' ..'z' | 'A' ..'Z');
END: ';';
NEWLINE: ('\r'? '\n' | '\r')+;
WORD: LETTER (LETTER | DIGIT | '_')*;

name: NAME;
question: name OPERATOR|NAME;
command: question WORD END|question;
line: command EOF;

我有一个带有输入的表格。我想要在用户键入“名称”并提交表单时检查名称,如果正确,建议使用operator

CplVisitor.js:

// Generated from .\Cpl.g4 by ANTLR 4.7.2
var antlr4 = require('antlr4/index');

function CplVisitor() {
    antlr4.tree.ParseTreeVisitor.call(this);
    return this;
}

CplVisitor.prototype = Object.create(antlr4.tree.ParseTreeVisitor.prototype);
CplVisitor.prototype.constructor = CplVisitor;

// Visit a parse tree produced by CplParser#name.
CplVisitor.prototype.visitName = function(ctx) {
  return this.visitChildren(ctx);
};

// Visit a parse tree produced by CplParser#question.
CplVisitor.prototype.visitQuestion = function(ctx) {
  return this.visitChildren(ctx);
};

// Visit a parse tree produced by CplParser#command.
CplVisitor.prototype.visitCommand = function(ctx) {
  return this.visitChildren(ctx);
};

// Visit a parse tree produced by CplParser#line.
CplVisitor.prototype.visitLine = function(ctx) {
  return this.visitChildren(ctx);
};
exports.CplVisitor = CplVisitor;

我实现了这个访客:

HtmlCplVisitor.js:

// Generated from .\Cpl.g4 by ANTLR 4.7.2
var antlr4 = require('antlr4/index');
var CplVisitor = require('./CplVisitor').CplVisitor;

function HtmlCplVisitor() {
    CplVisitor.call(this);
    return this;
}

HtmlCplVisitor.prototype = Object.create(CplVisitor.prototype);
HtmlCplVisitor.prototype.constructor = HtmlCplVisitor;

// Visit a parse tree produced by CplParser#name.
HtmlCplVisitor.prototype.visitName = function(ctx) {
    return this.visitChildren(ctx);
};

// Visit a parse tree produced by CplParser#question.
HtmlCplVisitor.prototype.visitQuestion = function(ctx) {
    return this.visitChildren(ctx);
};

// Visit a parse tree produced by CplParser#command.
HtmlCplVisitor.prototype.visitCommand = function(ctx) {
    return this.visitChildren(ctx);
};

// Visit a parse tree produced by CplParser#line.
CplVisitor.prototype.visitLine = function(ctx) {
    return this.visitChildren(ctx);
};

exports.HtmlCplVisitor = HtmlCplVisitor;

与nodejs一起提供此文件 CplTest.js

const http = require('http');
var express = require('express');
const antlr4 = require('antlr4/index');
const CplLexer = require('./CplLexer');
const CplParser = require('./CplParser');
const HtmlCplVisitor = require('./HtmlCplVisitor').HtmlCplVisitor;

const HtmlCplListener = require('./HtmlCplListener').HtmlCplListener;

var app = express();
var bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({ extended: true });

var server = app.listen(9999, function() {
    var host = server.address().address;
    var port = server.address().port;
    console.log('Example app listening at %s:%s Port', host, port);
});

var html =
    '<body>' +
    "<form action='/'  method='post' name='form1'>" +
    "Name:</p><input type= 'text' name='name'>" +
    "<input type='submit' value='submit'>" +
    '</form>' +
    '</body>';

app.get('/', function(req, res) {
    res.send(html);
});

app.post('/', urlencodedParser, function(req, res) {
    var input = req.body.name;
    var chars = new antlr4.InputStream(input);
    var lexer = new CplLexer.CplLexer(chars);
    var tokens = new antlr4.CommonTokenStream(lexer);
    var parser = new CplParser.CplParser(tokens);
    parser.buildParseTrees = true;
    var tree = parser.name();
    var visitor = HtmlCplVisitor.prototype.visit(tree);

    res.send(html);
});

如何检查是否插入了name,我将operators数组返回给客户端以提出建议。

0 个答案:

没有答案