mirror of
https://gitlab.com/bramw/baserow.git
synced 2025-04-22 20:32:24 +00:00
36 lines
1.3 KiB
JavaScript
36 lines
1.3 KiB
JavaScript
import antlr4 from 'antlr4'
|
|
import BaserowFormulaLexer from '@baserow/modules/database/formula/parser/generated/BaserowFormulaLexer'
|
|
import BaserowFormula from '@baserow/modules/database/formula/parser/generated/BaserowFormula'
|
|
import BaserowFormulaParserError from '@baserow/modules/database/formula/parser/errors'
|
|
|
|
/**
|
|
* Attempts to parse an input string into a Baserow Formula. If it fails a
|
|
* BaserowFormulaParserError will be raised.
|
|
*
|
|
* @param formula
|
|
* @return {*} The resulting antlr4 parse tree of the formula
|
|
*/
|
|
export default function parseBaserowFormula(formula) {
|
|
const chars = new antlr4.InputStream(formula)
|
|
const lexer = new BaserowFormulaLexer(chars)
|
|
const tokens = new antlr4.CommonTokenStream(lexer)
|
|
const parser = new BaserowFormula(tokens)
|
|
parser.removeErrorListeners()
|
|
// noinspection JSUnusedLocalSymbols
|
|
parser.addErrorListener({
|
|
syntaxError: (recognizer, offendingSymbol, line, column, msg, err) => {
|
|
throw new BaserowFormulaParserError(offendingSymbol, line, column, msg)
|
|
},
|
|
})
|
|
parser.buildParseTrees = true
|
|
return parser.root()
|
|
}
|
|
|
|
export function getTokenStreamForFormula(formula) {
|
|
const chars = new antlr4.InputStream(formula)
|
|
const lexer = new BaserowFormulaLexer(chars)
|
|
const stream = new antlr4.CommonTokenStream(lexer)
|
|
stream.lazyInit()
|
|
stream.fill()
|
|
return stream
|
|
}
|