BitBindle.ClosedXML.Parser
3.0.1
dotnet add package BitBindle.ClosedXML.Parser --version 3.0.1
NuGet\Install-Package BitBindle.ClosedXML.Parser -Version 3.0.1
<PackageReference Include="BitBindle.ClosedXML.Parser" Version="3.0.1" />
<PackageVersion Include="BitBindle.ClosedXML.Parser" Version="3.0.1" />
<PackageReference Include="BitBindle.ClosedXML.Parser" />
paket add BitBindle.ClosedXML.Parser --version 3.0.1
#r "nuget: BitBindle.ClosedXML.Parser, 3.0.1"
#:package BitBindle.ClosedXML.Parser@3.0.1
#addin nuget:?package=BitBindle.ClosedXML.Parser&version=3.0.1
#tool nuget:?package=BitBindle.ClosedXML.Parser&version=3.0.1
ClosedXML.Parser
ClosedXML.Parser is a .NET library that parses Excel formulas into an abstract syntax tree (AST). ClosedXML uses it to evaluate formulas and to change references in formulas.
This repository is a fork of ClosedXML.Parser by Jan Havlíček. The fork has its own releases, issues and changes. The license is MIT (see LICENSE.txt).
Package
The BitBindle.ClosedXML.Parser package is on nuget.org. The package targets net10.0.
dotnet add package BitBindle.ClosedXML.Parser
The package ID differs from the ID of the upstream package (ClosedXML.Parser). The assembly name and the namespaces stay ClosedXML.Parser. A project cannot reference both packages, because they contain the same assembly name. The upstream ClosedXML package depends on the upstream ClosedXML.Parser package. The fork does not support a project that uses both ClosedXML and this package.
How to use
- Implement the
IAstFactory<TScalarValue, TNode, TContext>interface. The parser calls the factory for each part of the formula, and the factory creates your nodes. - Call a parse method:
FormulaParser<TScalarValue, TNode, TContext>.CellFormulaA1("SUM(A1, 2)", context, factory)FormulaParser<TScalarValue, TNode, TContext>.CellFormulaR1C1("SUM(R1C1, 2)", context, factory)
The library also has these helpers:
FormulaConverterchanges a formula from A1 to R1C1 and from R1C1 to A1. ItsModifyA1method changes a formula with a factory that you give, for example a subclass ofRefModVisitor.ReferenceParserparses a reference, for exampleSheet1!A1:B2.NameUtilsfinds when a sheet name needs quotes in a formula.
The ClosedXML.Parser.Ast project has a simple AST and a factory. The tests use it. It shows how to implement IAstFactory.
Goals
- Performance: ClosedXML must parse formulas fast and with few allocations.
- Evaluation: The parser creates an abstract syntax tree, not a concrete syntax tree. The goal is the evaluation of formulas.
- Multi-use: Formulas are mostly in cells, but other parts of a workbook have formulas with different grammar rules, for example sparklines and data validation.
- A1 and R1C1 notation: The parser reads formulas in A1 and R1C1 notation. For example,
SUM(R5)is the sum of cellR5in A1 notation, but the sum of row 5 in R1C1 notation.
Design
The official source of the grammar is MS-XLSX, chapter 2.2.2 Formulas. A copy is in MS-XLSX-parser-grammar.abnf. A parser generator can not use this grammar directly: it has ambiguities, and its rules do not show the operator precedence.
The ANTLR4 grammar in src/ClosedXML.ANTLR is the source of truth for the lexer and the parser:
- Lexer: Rolex generates a DFA lexer from the ANTLR lexer grammar (see Generate the lexer). It is approximately two times faster than the ANTLR lexer.
- Parser:
FormulaParseris a recursive descent parser. It follows the rules of the ANTLR parser grammar, but it does not use ANTLR at runtime.
Performance
The ENRON data set has 946320 formulas. The recursive descent parser with the DFA lexer parses them in approximately 1.8 seconds in Release mode. That is approximately 2 μs for each formula.
Limitations
The primary goal is to parse the formulas in a file, not the formulas that a user types. The formula in the file is not always the same as the formula that Excel shows. For example:
- The
IFSfunction is a future function. The file has_xlfn.IFS, but the user seesIFS. - In a structured reference, the user sees
@for the current row. The file has the[#This Row]specifier.
Thus:
- An external reference must use the index of an external file, for example
[5]. - Other formula grammars, for example
[1]!'Some name in external wb', are out of scope.
The maximum of nesting levels depends on the stack of the thread. If the stack does not have sufficient space for the next level, the parser throws ParsingException. The runtime keeps the last 128 KB of the stack free, so a thread with a stack of 1 MB or more parses much more than the 64 nesting levels of Excel.
Development
AGENTS.md gives the rules for each change, and the commands to build, test and check the code. The short form:
dotnet build ClosedXML.Parser.slnx --configuration Release
dotnet test src/ClosedXML.Parser.Tests/ClosedXML.Parser.Tests.csproj --configuration Release --no-build
The base branch is develop. The CI workflow builds and tests each pull request to develop. A person starts the release workflow on develop.
Testing strategy
- Some tokens have data for a node, for example
A1_CELLC5gives row 5 and column 3. TheLexersfolder has test classes for most of these tokens, for exampleA1CellTokenTests.cs. - The
Rulesfolder has test classes for the parser rules, for exampleCellReferenceRuleTests.cs. Each test class has combinations of the rule, and it compares the result with the AST nodes. DataSetTests.cshas the data set tests. Each test parses each formula indata/<set>/formulas.csv. The parse must fail for the formulas in theknown-fails.csvandinvalid-external-cell-reference.csvfiles of the set, if the set has them. The parse must not fail for the other formulas. The tests do not check the AST. The CSV files have one column.AntlrCompatibilityTests.csmakes sure that the Rolex lexer gives the same tokens as the ANTLR lexer.
Use the vscode-antlr4 extension to debug the ANTLR grammar.
Generate the lexer
Rolex is a DFA lexer generator with the MIT license. Antlr2Rolex makes a Rolex grammar from the ANTLR grammar, and Rolex makes the DFA.
Prepare the Rolex grammars:
- Run Antlr2Rolex on
src/ClosedXML.ANTLR/FormulaLexer.g4with the A1 version. Write the result tosrc/ClosedXML.Parser/Rolex/LexerA1.rl. - Add
/*at the start of the Local A1 References section. This removesA1_CELL,A1_SPAN_REFERENCEand their fragments. - Remove
/*at the start of the Local R1C1 References section. This section has different tokens forA1_CELL,A1_SPAN_REFERENCEand their fragments. - Run Antlr2Rolex on
src/ClosedXML.ANTLR/FormulaLexer.g4with the R1C1 version. Write the result tosrc/ClosedXML.Parser/Rolex/LexerR1C1.rl.
Fix the Rolex generator: Rolex does not read the \u1234 escape (a Unicode escape with four hex digits) correctly. Add pc.Advance() to _ParseEscapePart and _ParseRangeEscapePart in FFA.cs.
Generate the DFA with Rolex:
Rolex.exe src\ClosedXML.Parser\Rolex\LexerA1.rl /noshared /output src\ClosedXML.Parser\Rolex\RolexA1Dfa.cs /namespace ClosedXML.Parser.Rolex
Rolex.exe src\ClosedXML.Parser\Rolex\LexerR1C1.rl /noshared /output src\ClosedXML.Parser\Rolex\RolexR1C1Dfa.cs /namespace ClosedXML.Parser.Rolex
Resources
- MS-XLSX, chapter 2.2.2 Formulas
- Simplified XLParser grammar and tokens
- Getting Started With ANTLR in C#
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on BitBindle.ClosedXML.Parser:
| Package | Downloads |
|---|---|
|
BitBindle.ClosedXML
See release notes https://github.com/BitBindle/ClosedXML/releases/tag/0.107.7 ClosedXML is a .NET library that reads, changes and writes Excel 2007+ files (.xlsx, .xlsm). It gives an easy interface to the Open XML SDK. |
GitHub repositories
This package is not used by any popular GitHub repositories.