Code: Select all
import antlr4 from 'antlr4';
import MyGrammarLexer from './QueryLexer.js';
import MyGrammarParser from './QueryParser.js';
import MyGrammarListener from './QueryListener.js';
const input = "field = 123 AND items in (1,2,3)"
const chars = new antlr4.InputStream(input);
const lexer = new MyGrammarLexer(chars);
const tokens = new antlr4.CommonTokenStream(lexer);
const parser = new MyGrammarParser(tokens);
const tree = parser.MyQuery();
Zuerst habe ich Folgendes ausprobiert:
Code: Select all
// imports...
import _ from "lodash/fp.js";
const parse = _.compose([
_.invoke("MyQuery"),
MyGrammarParser,
antlr4.CommonTokenStream,
MyGrammarLexer,
antlr4.InputStream,
]);
Stattdessen kann ich eine Helferfunktion verwenden: < /p>
Code: Select all
// imports...
import _ from "lodash/fp.js";
_.construct = Constructor => param => new Constructor(param);
const parse = _.compose([
_.invoke("MyQuery"),
_.construct(MyGrammarParser),
_.construct(antlr4.CommonTokenStream),
_.construct(MyGrammarLexer),
_.construct(antlr4.InputStream),
]);
const tree = parse("field = 123 AND items in (1,2,3)");
< /code>
Dies funktioniert und IMO sieht viel besser aus als das Original, aber ich möchte wissen, ob es eine Möglichkeit gibt, dies mit integrierten JavaScript- oder Lodash-Funktionen zu schreiben. Soweit ich weiß, ist es nicht möglich, einen Konstruktor als Parameter zu übergeben, als wäre es eine reguläre Funktion, daher _.Construct