CodeParser
CodeParser is a package for parsing Wolfram Language source code as abstract syntax trees (ASTs) or concrete syntax trees (CSTs). CodeParser is useful for inspecting code, formatting code, and instrumenting code (for e.g., coverage reporting or profiling), and much more!
CodeParser has many key features:
- Understands practically entire Wolfram Language syntax.
- Fast native library implementation.
- Tested with combination of suite of hand-written tests and fuzz testing.
- Gracious error handling and recovery
Needs["CodeParser`"]
CodeParse["1+1"]
Out[2]= ContainerNode[String, {CallNode[LeafNode[Symbol, "Plus", <||>], {LeafNode[Integer, "1", <|Source -> {{1, 1}, {1, 2}}|>], LeafNode[Integer, "1", <|Source -> {{1, 3}, {1, 4}}|>]}, <|Source -> {{1, 1}, {1, 4}}|>]}, <||>]
"CodeParser and CodeInspector" on community.wolfram.com
Parsing the Wolfram Language from WTC 2019: Watch Video
Parsing the Wolfram Language from WTC 2019: Download Presentation
Setup
CodeParser is included in Mathematica 12.2.
For older versions, you can install from the paclet server.
The minimum version for CodeParser is Mathematica 11.0.
Install CodeParser from the paclet server:
PacletInstall["CodeParser"]
Make sure that the paclet can be found on your system:
Needs["CodeParser`"]
Using CodeParser
After CodeParser is installed, it can be used.
Needs["CodeParser`"]
CodeParse["1+1"]
Out[2]= ContainerNode[String, {CallNode[LeafNode[Symbol, "Plus", <||>], {LeafNode[Integer, "1", <|Source -> {{1, 1}, {1, 2}}|>], LeafNode[Integer, "1", <|Source -> {{1, 3}, {1, 4}}|>]}, <|Source -> {{1, 1}, {1, 4}}|>]}, <||>]
The input to CodeParse may be a string, a File, or a list of bytes.
Command-line tool (Optional)
An optional codeparser command-line tool is also built and can be used.
cmake -DBUILD_EXE=ON ..
cmake --build . --target codeparser-exe
$cpp/src/exe/codeparser
>>> 1+1
InfixNode[Plus, {LeafNode[Integer, "1", <|Source->{{1, 2}, {1, 2}}|>], LeafNode[Integer, 1, <|Source->{{1, 3}, {1, 4}}|>]}, <|Source->{{1, 1}, {1, 4}}|>]
>>>