A lexer and parser of Excel formulas with focus on performance. Its goal is to create an abstract syntax tree from a formula text to enable formula evaluation.
$ dotnet add package ClosedXML.ParserClosedParser is a project to parse OOXML grammar to create an abstract syntax tree that can be later evaluated.
Official source for the grammar is MS-XML, chapter 2.2.2 Formulas. The provided grammar is not usable for parser generators, it's full of ambiguities and the rules don't take into account operator precedence.
IAstFactory interface.FormulaParser<TScalarValue, TNode>.CellFormulaA1("Sum(A1, 2)", astFactory)FormulaParser<TScalarValue, TNode>.CellFormulaR1C1("Sum(R1C1, 2)", astFactory)There is a visualizer to display AST in a browser at https://parser.closedxml.io
SUM(R5) can mean return sum of cell R5 in A1 notation, but return sum of all cells on row 5 in R1C1 notation.Project uses ANTLR4 grammar file as the source of truth and a lexer. There is also ANTLR parser is not used, but is used as a basis of recursive descent parser (ANTLR takes up 8 seconds vs RDS 700ms for parsing of enron dataset).
ANTLR4 one of few maintained parser generators with C# target.
The project has a low priority, XLParser mostly works, but in the long term, replacement is likely.
ENRON dataset parsed using recursive descent parser and DFA lexer in Release mode:
2μs per formula should be something like 6000 instructions (under unrealistic assumption 1 instruction per 1 Hz), so basically fast enough.
The primary goal is to parse formulas stored in file, not user supplied formulas. The formulas displayed in the GUI is not the same as formula stored in the file. Several examples:
_xlfn.IFS, but user sees IFS[#This Row]Therefore:
[5])[1]!'Some name in external wb'). They are out of scope of the project.ClosedXML is currently using XLParser and transforming the concrete syntax tree to abstract syntax tree.
prefix hints.IronParser, an unmaintained projectANTLR lexer takes up about 3.2 seconds for Enron dataset. With ANTLR parsing, it takes up 11 seconds. I want that 7+ seconds in performance and no allocation, so RDS that takes up 700 ms.
Use vscode-antlr4 plugin for debugging the grammar.
A1_REFERENCE C5 to row 5, column 3) has a separate test class in Lexers directory with a {TokenPascalName}TokenTests.csRules directory. It should contain all possible combinatins of a rule and comparing it with the AST nodes.DataSetTests.cs. Each test tries to parse formula and ensures that ANTLR can parse it RDS can and can't parse a formula when ANTLR can't. There is no check of the output, just that formulas can be parsed. Data are contained in a data directory in CSV format with a one column.Rolex is a DFA based lexer released under MIT license (see Rolex: Unicode Enabled Lexer Generator in C# ). ANTLR is still the source of truth, but it is used to generate Rolex grammar and then DFA for a lexer.
It is rather complicated, but two times faster than ANTLR lexer (1.9 us vs 3.676 us per formula).
Prepare rolex grammars
/* at the beginning of Local A1 References section. It comments out A1_REFERENCE and all its fragments/* at the beinning of Local R1C1 References section. It contains a different tokens for A1_REFERENCE and its fragmentsFix Rolex generator
pc.Advance() to FFA.cs _ParseEscapePart and _ParseRangeEscapePart]Generate a DFA through Rolex
Rolex.exe ClosedXML.Parser\Rolex\LexerA1.rl /noshared /output ClosedXML.Parser\Rolex\RolexA1Dfa.cs /namespace ClosedXML.Parser.RolexRolex.exe ClosedXML.Parser\Rolex\LexerR1C1.rl /noshared /output ClosedXML.Parser\Rolex\RolexR1C1Dfa.cs /namespace ClosedXML.Parser.Rolex