Package: antlr4-vba-parser
antlr4-vba-parserNavigate antlr VBA Parse Trees in python.This python package provides an interface to the the antlr4 tooling and allows parsing
and lexing of VBA grammar.>>>fromantlr4_vba_parser.vba_parserimportAntlr4VbaParser>>>parsed=Antlr4VbaParser("""... SUB square(x)...   DIM y: REM Some comment...   y = x * x  ' same as x**2... END SUB... """)# also accepts a filepath>>>frompprintimportpprint>>>pprint(parsed)('(startRule (module (endOfLine\\n) (moduleBody (moduleBodyElement (subStmt ''SUB   (ambiguousIdentifier square) (argList ( (arg (ambiguousIdentifier x)) '')) (endOfStatement (endOfLine\\n   )) (block (blockStmt (variableStmt DIM   ''(variableListStmt (variableSubStmt (ambiguousIdentifier y))))) ''(endOfStatement :   (endOfLine (remComment REM Some comment)) (endOfLine ''\\n   )) (blockStmt (letStmt (implicitCallStmt_InStmt ''(iCS_S_VariableOrProcedureCall (ambiguousIdentifier y)))   =   (valueStmt ''(valueStmt (implicitCallStmt_InStmt (iCS_S_VariableOrProcedureCall ''(ambiguousIdentifier x))))   *   (valueStmt (implicitCallStmt_InStmt ''(iCS_S_VariableOrProcedureCall (ambiguousIdentifier x))))))) (endOfStatement '"(endOfLine    (comment ' same as x**2)) (endOfLine\\n))) END SUB)) "'(endOfLine\\n))) <EOF>)')Installationantlr4_vba_parseritself is a pure python package, but depends on ajavaruntime in order to run.
The ANTLR4 jar needed to perform the parsing/lexing is included in the package distribution and
is bundled from third-party sources at the time of packaging withsetup.py build.To install, simply try:pipinstallantlr4_vba_parserDevelopmentTo set up a development environment, first create either a new virtual or
conda environment before activating it and then run the following:gitclonehttps://github.com/Liam-Deacon/antlr4-vba-parsercdantlr4-vba-parser
pipinstall-rrequirements-dev.txtrequirements-test.txt-rrequirements.txt
pythonsetup.pybuild_antlr4# needed to generate python bindingspipinstall-e.This will install the package in development mode. Note that is you have forked
the repo then change the URL as appropriate.DocumentationDocumentation can be found within thedocs/directory. This project
uses sphinx to autogenerate API documentation by scraping python docstrings.To generate the HTML documentation, simply do the following:cddocs
makehtmlContribution GuidelinesContributions are extremely welcome and highly encouraged. To help with consistency
please can the following areas be considered before submitting a PR for review:Useautopep8 -a -a -i -r .to run over any modified files to ensure basic pep8 conformance,
allowing the code to be read in a style expected for most python projects.New or changed functionality should be tested, runningpytestshouldTry to document any new or changed functionality. Note: this project usesnumpydocfor it's
docstring documentation style.LicenseReleased under the BSD license.TODOThis package is mostly a proof of concept and as such there are a number of
areas to add to, fix and improve.Create listener(s) capable of capturing contextual information and creating a JSON-friendly dictionary output.Produce simple script turns the above into a command line tool.Contribute tooletools.vbato hopefully extend capabilities using this package.AcknowledgementsAndrew Lockhart for the initial idea of combining ANTLR4 and python to handle VBA grammar