202 lines
7.7 KiB
JavaScript
202 lines
7.7 KiB
JavaScript
"use strict";
|
|||
|
|
|
||
|
|
exports.__esModule = true;
|
||
|
|
exports.buildLexerConfig = exports.TimesOp = exports.StringLiteral = exports.RowRange = exports.RangeSeparator = exports.RParen = exports.ProcedureName = exports.PowerOp = exports.PlusOp = exports.PercentOp = exports.NotEqualOp = exports.NamedExpression = exports.MultiplicationOp = exports.MinusOp = exports.LessThanOrEqualOp = exports.LessThanOp = exports.LParen = exports.GreaterThanOrEqualOp = exports.GreaterThanOp = exports.ErrorLiteral = exports.EqualsOp = exports.DivOp = exports.ConcatenateOp = exports.ColumnRange = exports.CellReference = exports.BooleanOp = exports.ArrayRParen = exports.ArrayLParen = exports.AdditionOp = void 0;
|
||
|
|
var _chevrotain = require("chevrotain");
|
||
|
|
var _parserConsts = require("./parser-consts");
|
||
|
|
var _CellReferenceMatcher = require("./CellReferenceMatcher");
|
||
|
|
var _NamedExpressionMatcher = require("./NamedExpressionMatcher");
|
||
|
|
/**
|
||
|
|
* @license
|
||
|
|
* Copyright (c) 2024 Handsoncode. All rights reserved.
|
||
|
|
*/
|
||
|
|
|
||
|
|
const AdditionOp = exports.AdditionOp = (0, _chevrotain.createToken)({
|
||
|
|
name: 'AdditionOp',
|
||
|
|
pattern: _chevrotain.Lexer.NA
|
||
|
|
});
|
||
|
|
const PlusOp = exports.PlusOp = (0, _chevrotain.createToken)({
|
||
|
|
name: 'PlusOp',
|
||
|
|
pattern: /\+/,
|
||
|
|
categories: AdditionOp
|
||
|
|
});
|
||
|
|
const MinusOp = exports.MinusOp = (0, _chevrotain.createToken)({
|
||
|
|
name: 'MinusOp',
|
||
|
|
pattern: /-/,
|
||
|
|
categories: AdditionOp
|
||
|
|
});
|
||
|
|
const MultiplicationOp = exports.MultiplicationOp = (0, _chevrotain.createToken)({
|
||
|
|
name: 'MultiplicationOp',
|
||
|
|
pattern: _chevrotain.Lexer.NA
|
||
|
|
});
|
||
|
|
const TimesOp = exports.TimesOp = (0, _chevrotain.createToken)({
|
||
|
|
name: 'TimesOp',
|
||
|
|
pattern: /\*/,
|
||
|
|
categories: MultiplicationOp
|
||
|
|
});
|
||
|
|
const DivOp = exports.DivOp = (0, _chevrotain.createToken)({
|
||
|
|
name: 'DivOp',
|
||
|
|
pattern: /\//,
|
||
|
|
categories: MultiplicationOp
|
||
|
|
});
|
||
|
|
const PowerOp = exports.PowerOp = (0, _chevrotain.createToken)({
|
||
|
|
name: 'PowerOp',
|
||
|
|
pattern: /\^/
|
||
|
|
});
|
||
|
|
const PercentOp = exports.PercentOp = (0, _chevrotain.createToken)({
|
||
|
|
name: 'PercentOp',
|
||
|
|
pattern: /%/
|
||
|
|
});
|
||
|
|
const BooleanOp = exports.BooleanOp = (0, _chevrotain.createToken)({
|
||
|
|
name: 'BooleanOp',
|
||
|
|
pattern: _chevrotain.Lexer.NA
|
||
|
|
});
|
||
|
|
const EqualsOp = exports.EqualsOp = (0, _chevrotain.createToken)({
|
||
|
|
name: 'EqualsOp',
|
||
|
|
pattern: /=/,
|
||
|
|
categories: BooleanOp
|
||
|
|
});
|
||
|
|
const NotEqualOp = exports.NotEqualOp = (0, _chevrotain.createToken)({
|
||
|
|
name: 'NotEqualOp',
|
||
|
|
pattern: /<>/,
|
||
|
|
categories: BooleanOp
|
||
|
|
});
|
||
|
|
const GreaterThanOp = exports.GreaterThanOp = (0, _chevrotain.createToken)({
|
||
|
|
name: 'GreaterThanOp',
|
||
|
|
pattern: />/,
|
||
|
|
categories: BooleanOp
|
||
|
|
});
|
||
|
|
const LessThanOp = exports.LessThanOp = (0, _chevrotain.createToken)({
|
||
|
|
name: 'LessThanOp',
|
||
|
|
pattern: /</,
|
||
|
|
categories: BooleanOp
|
||
|
|
});
|
||
|
|
const GreaterThanOrEqualOp = exports.GreaterThanOrEqualOp = (0, _chevrotain.createToken)({
|
||
|
|
name: 'GreaterThanOrEqualOp',
|
||
|
|
pattern: />=/,
|
||
|
|
categories: BooleanOp
|
||
|
|
});
|
||
|
|
const LessThanOrEqualOp = exports.LessThanOrEqualOp = (0, _chevrotain.createToken)({
|
||
|
|
name: 'LessThanOrEqualOp',
|
||
|
|
pattern: /<=/,
|
||
|
|
categories: BooleanOp
|
||
|
|
});
|
||
|
|
const ConcatenateOp = exports.ConcatenateOp = (0, _chevrotain.createToken)({
|
||
|
|
name: 'ConcatenateOp',
|
||
|
|
pattern: /&/
|
||
|
|
});
|
||
|
|
const LParen = exports.LParen = (0, _chevrotain.createToken)({
|
||
|
|
name: 'LParen',
|
||
|
|
pattern: /\(/
|
||
|
|
});
|
||
|
|
const RParen = exports.RParen = (0, _chevrotain.createToken)({
|
||
|
|
name: 'RParen',
|
||
|
|
pattern: /\)/
|
||
|
|
});
|
||
|
|
const ArrayLParen = exports.ArrayLParen = (0, _chevrotain.createToken)({
|
||
|
|
name: 'ArrayLParen',
|
||
|
|
pattern: /{/
|
||
|
|
});
|
||
|
|
const ArrayRParen = exports.ArrayRParen = (0, _chevrotain.createToken)({
|
||
|
|
name: 'ArrayRParen',
|
||
|
|
pattern: /}/
|
||
|
|
});
|
||
|
|
const StringLiteral = exports.StringLiteral = (0, _chevrotain.createToken)({
|
||
|
|
name: 'StringLiteral',
|
||
|
|
pattern: /"([^"\\]*(\\.[^"\\]*)*)"/
|
||
|
|
});
|
||
|
|
const ErrorLiteral = exports.ErrorLiteral = (0, _chevrotain.createToken)({
|
||
|
|
name: 'ErrorLiteral',
|
||
|
|
pattern: /#[A-Za-z0-9\/]+[?!]?/
|
||
|
|
});
|
||
|
|
const RangeSeparator = exports.RangeSeparator = (0, _chevrotain.createToken)({
|
||
|
|
name: 'RangeSeparator',
|
||
|
|
pattern: new RegExp(_parserConsts.RANGE_OPERATOR)
|
||
|
|
});
|
||
|
|
const ColumnRange = exports.ColumnRange = (0, _chevrotain.createToken)({
|
||
|
|
name: 'ColumnRange',
|
||
|
|
pattern: new RegExp(`${_parserConsts.COLUMN_REFERENCE_PATTERN}${_parserConsts.RANGE_OPERATOR}${_parserConsts.COLUMN_REFERENCE_PATTERN}`)
|
||
|
|
});
|
||
|
|
const RowRange = exports.RowRange = (0, _chevrotain.createToken)({
|
||
|
|
name: 'RowRange',
|
||
|
|
pattern: new RegExp(`${_parserConsts.ROW_REFERENCE_PATTERN}${_parserConsts.RANGE_OPERATOR}${_parserConsts.ROW_REFERENCE_PATTERN}`)
|
||
|
|
});
|
||
|
|
const ProcedureName = exports.ProcedureName = (0, _chevrotain.createToken)({
|
||
|
|
name: 'ProcedureName',
|
||
|
|
pattern: new RegExp(`([${_parserConsts.UNICODE_LETTER_PATTERN}][${_parserConsts.NON_RESERVED_CHARACTER_PATTERN}]*)\\(`)
|
||
|
|
});
|
||
|
|
const cellReferenceMatcher = new _CellReferenceMatcher.CellReferenceMatcher();
|
||
|
|
const CellReference = exports.CellReference = (0, _chevrotain.createToken)({
|
||
|
|
name: 'CellReference',
|
||
|
|
pattern: cellReferenceMatcher.match.bind(cellReferenceMatcher),
|
||
|
|
start_chars_hint: cellReferenceMatcher.POSSIBLE_START_CHARACTERS,
|
||
|
|
line_breaks: false
|
||
|
|
});
|
||
|
|
const namedExpressionMatcher = new _NamedExpressionMatcher.NamedExpressionMatcher();
|
||
|
|
const NamedExpression = exports.NamedExpression = (0, _chevrotain.createToken)({
|
||
|
|
name: 'NamedExpression',
|
||
|
|
pattern: namedExpressionMatcher.match.bind(namedExpressionMatcher),
|
||
|
|
start_chars_hint: namedExpressionMatcher.POSSIBLE_START_CHARACTERS,
|
||
|
|
line_breaks: false
|
||
|
|
});
|
||
|
|
/**
|
||
|
|
* Builds the configuration object for the lexer
|
||
|
|
*/
|
||
|
|
const buildLexerConfig = config => {
|
||
|
|
const offsetProcedureNameLiteral = config.translationPackage.getFunctionTranslation('OFFSET');
|
||
|
|
const errorMapping = config.errorMapping;
|
||
|
|
const functionMapping = config.translationPackage.buildFunctionMapping();
|
||
|
|
const whitespaceTokenRegexp = new RegExp(config.ignoreWhiteSpace === 'standard' ? _parserConsts.ODFF_WHITESPACE_PATTERN : _parserConsts.ALL_WHITESPACE_PATTERN);
|
||
|
|
const WhiteSpace = (0, _chevrotain.createToken)({
|
||
|
|
name: 'WhiteSpace',
|
||
|
|
pattern: whitespaceTokenRegexp
|
||
|
|
});
|
||
|
|
const ArrayRowSeparator = (0, _chevrotain.createToken)({
|
||
|
|
name: 'ArrayRowSep',
|
||
|
|
pattern: config.arrayRowSeparator
|
||
|
|
});
|
||
|
|
const ArrayColSeparator = (0, _chevrotain.createToken)({
|
||
|
|
name: 'ArrayColSep',
|
||
|
|
pattern: config.arrayColumnSeparator
|
||
|
|
});
|
||
|
|
const NumberLiteral = (0, _chevrotain.createToken)({
|
||
|
|
name: 'NumberLiteral',
|
||
|
|
pattern: new RegExp(`(([${config.decimalSeparator}]\\d+)|(\\d+([${config.decimalSeparator}]\\d*)?))(e[+-]?\\d+)?`)
|
||
|
|
});
|
||
|
|
const OffsetProcedureName = (0, _chevrotain.createToken)({
|
||
|
|
name: 'OffsetProcedureName',
|
||
|
|
pattern: new RegExp(offsetProcedureNameLiteral, 'i')
|
||
|
|
});
|
||
|
|
let ArgSeparator;
|
||
|
|
let inject;
|
||
|
|
if (config.functionArgSeparator === config.arrayColumnSeparator) {
|
||
|
|
ArgSeparator = ArrayColSeparator;
|
||
|
|
inject = [];
|
||
|
|
} else if (config.functionArgSeparator === config.arrayRowSeparator) {
|
||
|
|
ArgSeparator = ArrayRowSeparator;
|
||
|
|
inject = [];
|
||
|
|
} else {
|
||
|
|
ArgSeparator = (0, _chevrotain.createToken)({
|
||
|
|
name: 'ArgSeparator',
|
||
|
|
pattern: config.functionArgSeparator
|
||
|
|
});
|
||
|
|
inject = [ArgSeparator];
|
||
|
|
}
|
||
|
|
/* order is important, first pattern is used */
|
||
|
|
const allTokens = [WhiteSpace, PlusOp, MinusOp, TimesOp, DivOp, PowerOp, EqualsOp, NotEqualOp, PercentOp, GreaterThanOrEqualOp, LessThanOrEqualOp, GreaterThanOp, LessThanOp, LParen, RParen, ArrayLParen, ArrayRParen, OffsetProcedureName, ProcedureName, RangeSeparator, ...inject, ColumnRange, RowRange, NumberLiteral, StringLiteral, ErrorLiteral, ConcatenateOp, BooleanOp, AdditionOp, MultiplicationOp, CellReference, NamedExpression, ArrayRowSeparator, ArrayColSeparator];
|
||
|
|
return {
|
||
|
|
ArgSeparator,
|
||
|
|
NumberLiteral,
|
||
|
|
OffsetProcedureName,
|
||
|
|
ArrayRowSeparator,
|
||
|
|
ArrayColSeparator,
|
||
|
|
WhiteSpace,
|
||
|
|
allTokens,
|
||
|
|
errorMapping,
|
||
|
|
functionMapping,
|
||
|
|
decimalSeparator: config.decimalSeparator,
|
||
|
|
maxColumns: config.maxColumns,
|
||
|
|
maxRows: config.maxRows
|
||
|
|
};
|
||
|
|
};
|
||
|
|
exports.buildLexerConfig = buildLexerConfig;
|