Skip to content

Commit

Permalink
Add support for grammars that are split into separate lexer/parser fi…
Browse files Browse the repository at this point in the history
…les (#5)
  • Loading branch information
amykyta3 committed Dec 18, 2020
1 parent 89c44b9 commit 8bc36f2
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 18 deletions.
16 changes: 10 additions & 6 deletions docs/example.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ You'll notice this last step generates the following files:
* cpp_src/sa_mygrammar_translator.cpp/.h
* cpp_src/speedy_antlr.cpp/.h

.. note::
If your language grammar is split into separate Lexer and Parser files, see
the alternate `src/spam/parser/generate_parsers_split.sh <https://github.com/amykyta3/speedy-antlr-example/blob/master/src/spam/parser/generate_parsers_split.sh>`_
example script.

sa_mygrammar.py
---------------
Expand Down Expand Up @@ -118,10 +122,10 @@ Since you'll be bundling the Antlr C++ runtime in your package's distribution
(source and binary), be a good steward of open-source software and include a
copy of Antlr's BSD license.

.travis.yml
-----------
.github/workflows/build.yml
---------------------------

`.travis.yml <https://github.com/amykyta3/speedy-antlr-example/blob/master/.travis.yml>`_
`.github/workflows/build.yml <https://github.com/amykyta3/speedy-antlr-example/blob/master/.github/workflows/build.yml>`_

If you've attempted to install this example by now, you've probably noticed that
it takes a *looong* time. This is because all the C++ files (antlr has many)
Expand All @@ -132,8 +136,8 @@ binary distributions. This eliminates the need for the end-user to install a
compiler and build everything from source.

Since you probably dont have access to every variant of Windows/Linux/macOS,
this is typically done using a continuous integration service like
`Travis-CI <https://travis-ci.org>`_. This YAML file tells Travis-CI how to
run your project's tests, and how to deploy to PyPi. I'm also using
this is typically done using a continuous integration service like Github
Actions. This YAML file tells Github Actions how to run your project's tests,
and how to deploy to PyPi. I'm also using
`cibuildwheel <https://github.com/joerick/cibuildwheel>`_ to automate building
all the different distribution variants.
2 changes: 1 addition & 1 deletion docs/speedy-antlr-example
Submodule speedy-antlr-example updated 68 files
+120 −0 .github/workflows/build.yml
+0 −86 .travis.yml
+2 −0 README.md
+2 −2 setup.py
+2 −2 src/spam/parser/MyGrammarLexer.py
+2 −2 src/spam/parser/MyGrammarParser.py
+1 −1 src/spam/parser/cpp_src/MyGrammarBaseVisitor.cpp
+1 −1 src/spam/parser/cpp_src/MyGrammarBaseVisitor.h
+8 −9 src/spam/parser/cpp_src/MyGrammarLexer.cpp
+2 −2 src/spam/parser/cpp_src/MyGrammarLexer.h
+24 −2 src/spam/parser/cpp_src/MyGrammarParser.cpp
+4 −4 src/spam/parser/cpp_src/MyGrammarParser.h
+1 −1 src/spam/parser/cpp_src/MyGrammarVisitor.cpp
+1 −1 src/spam/parser/cpp_src/MyGrammarVisitor.h
+15 −0 src/spam/parser/cpp_src/antlr4-cpp-runtime/ANTLRInputStream.cpp
+8 −0 src/spam/parser/cpp_src/antlr4-cpp-runtime/ANTLRInputStream.h
+1 −1 src/spam/parser/cpp_src/antlr4-cpp-runtime/CommonTokenFactory.cpp
+1 −1 src/spam/parser/cpp_src/antlr4-cpp-runtime/CommonTokenFactory.h
+7 −1 src/spam/parser/cpp_src/antlr4-cpp-runtime/IntStream.h
+2 −2 src/spam/parser/cpp_src/antlr4-cpp-runtime/Lexer.cpp
+23 −10 src/spam/parser/cpp_src/antlr4-cpp-runtime/Lexer.h
+2 −2 src/spam/parser/cpp_src/antlr4-cpp-runtime/ListTokenSource.cpp
+2 −2 src/spam/parser/cpp_src/antlr4-cpp-runtime/ListTokenSource.h
+4 −4 src/spam/parser/cpp_src/antlr4-cpp-runtime/Parser.cpp
+1 −1 src/spam/parser/cpp_src/antlr4-cpp-runtime/Parser.h
+3 −3 src/spam/parser/cpp_src/antlr4-cpp-runtime/ParserRuleContext.cpp
+1 −1 src/spam/parser/cpp_src/antlr4-cpp-runtime/ParserRuleContext.h
+4 −4 src/spam/parser/cpp_src/antlr4-cpp-runtime/ProxyErrorListener.cpp
+8 −2 src/spam/parser/cpp_src/antlr4-cpp-runtime/Recognizer.h
+1 −1 src/spam/parser/cpp_src/antlr4-cpp-runtime/RuntimeMetaData.cpp
+39 −7 src/spam/parser/cpp_src/antlr4-cpp-runtime/Token.h
+1 −1 src/spam/parser/cpp_src/antlr4-cpp-runtime/TokenSource.h
+5 −5 src/spam/parser/cpp_src/antlr4-cpp-runtime/TokenStreamRewriter.cpp
+9 −2 src/spam/parser/cpp_src/antlr4-cpp-runtime/TokenStreamRewriter.h
+11 −19 src/spam/parser/cpp_src/antlr4-cpp-runtime/Vocabulary.cpp
+3 −4 src/spam/parser/cpp_src/antlr4-cpp-runtime/Vocabulary.h
+4 −1 src/spam/parser/cpp_src/antlr4-cpp-runtime/antlr4-common.h
+2 −2 src/spam/parser/cpp_src/antlr4-cpp-runtime/atn/ATN.cpp
+7 −1 src/spam/parser/cpp_src/antlr4-cpp-runtime/atn/ATN.h
+0 −2 src/spam/parser/cpp_src/antlr4-cpp-runtime/atn/ATNConfig.cpp
+8 −2 src/spam/parser/cpp_src/antlr4-cpp-runtime/atn/ATNConfig.h
+0 −2 src/spam/parser/cpp_src/antlr4-cpp-runtime/atn/ATNDeserializer.cpp
+7 −1 src/spam/parser/cpp_src/antlr4-cpp-runtime/atn/ATNDeserializer.h
+1 −1 src/spam/parser/cpp_src/antlr4-cpp-runtime/atn/ATNSerializer.cpp
+1 −1 src/spam/parser/cpp_src/antlr4-cpp-runtime/atn/ATNState.cpp
+9 −2 src/spam/parser/cpp_src/antlr4-cpp-runtime/atn/ATNState.h
+7 −1 src/spam/parser/cpp_src/antlr4-cpp-runtime/atn/LL1Analyzer.h
+9 −2 src/spam/parser/cpp_src/antlr4-cpp-runtime/atn/LexerATNSimulator.h
+1 −1 src/spam/parser/cpp_src/antlr4-cpp-runtime/atn/ParserATNSimulator.cpp
+14 −2 src/spam/parser/cpp_src/antlr4-cpp-runtime/atn/PredictionContext.h
+2 −2 src/spam/parser/cpp_src/antlr4-cpp-runtime/dfa/DFA.cpp
+1 −1 src/spam/parser/cpp_src/antlr4-cpp-runtime/dfa/DFASerializer.cpp
+1 −1 src/spam/parser/cpp_src/antlr4-cpp-runtime/dfa/DFAState.cpp
+9 −9 src/spam/parser/cpp_src/antlr4-cpp-runtime/misc/IntervalSet.cpp
+7 −1 src/spam/parser/cpp_src/antlr4-cpp-runtime/misc/MurmurHash.h
+1 −1 src/spam/parser/cpp_src/antlr4-cpp-runtime/support/Any.h
+2 −2 src/spam/parser/cpp_src/antlr4-cpp-runtime/support/Arrays.cpp
+1 −1 src/spam/parser/cpp_src/antlr4-cpp-runtime/support/CPPUtils.cpp
+11 −1 src/spam/parser/cpp_src/antlr4-cpp-runtime/support/StringUtils.cpp
+40 −18 src/spam/parser/cpp_src/antlr4-cpp-runtime/support/StringUtils.h
+1 −1 src/spam/parser/cpp_src/antlr4-cpp-runtime/tree/ParseTree.h
+29 −5 src/spam/parser/cpp_src/antlr4-cpp-runtime/tree/ParseTreeWalker.h
+1 −1 src/spam/parser/cpp_src/antlr4-cpp-runtime/tree/Trees.cpp
+1 −1 src/spam/parser/cpp_src/antlr4-cpp-runtime/tree/pattern/ParseTreePattern.cpp
+1 −1 src/spam/parser/cpp_src/antlr4-cpp-runtime/tree/xpath/XPath.cpp
+1 −1 src/spam/parser/cpp_src/antlr4-cpp-runtime/tree/xpath/XPathRuleElement.cpp
+1 −1 src/spam/parser/cpp_src/antlr4-cpp-runtime/tree/xpath/XPathTokenElement.cpp
+2 −2 src/spam/parser/generate_parsers.sh
2 changes: 1 addition & 1 deletion speedy_antlr_tool/__about__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.0.0"
__version__ = "1.1.0"
9 changes: 6 additions & 3 deletions speedy_antlr_tool/extractor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
import os
import importlib.util
from typing import List

Expand Down Expand Up @@ -32,7 +33,7 @@ def iter_rule_context_classes(parser_cls:antlr4.Parser):
# Detect ParserRuleContext classes
if not (isinstance(obj, type) and issubclass(obj, antlr4.ParserRuleContext)):
continue

yield obj


Expand Down Expand Up @@ -83,8 +84,10 @@ def get_context_data(context_cls:antlr4.ParserRuleContext) -> ContextData:
def extract(parser_path:str) -> List[ContextData]:
parser_cls = get_parser_class(parser_path)

parser_basename = os.path.splitext(os.path.basename(parser_cls.grammarFileName))[0]

cds = []
for context_cls in iter_rule_context_classes(parser_cls):
cds.append(get_context_data(context_cls))
return cds

return cds, parser_basename
12 changes: 7 additions & 5 deletions speedy_antlr_tool/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from .extractor import extract
from .__about__ import __version__

def write_cpp_files(grammar_name:str, context_data:str, output_dir:str):
def write_cpp_files(grammar_name:str, parser_basename:str, context_data:str, output_dir:str):
loader = jj.FileSystemLoader(os.path.join(os.path.dirname(__file__), "templates"))

jj_env = jj.Environment(
Expand All @@ -15,6 +15,7 @@ def write_cpp_files(grammar_name:str, context_data:str, output_dir:str):

context = {
"grammar_name": grammar_name,
"parser_basename": parser_basename,
"context_data": context_data,
"__version__": __version__,
}
Expand Down Expand Up @@ -50,7 +51,7 @@ def write_cpp_files(grammar_name:str, context_data:str, output_dir:str):
stream.dump(output_path)


def write_py_files(grammar_name:str, context_data:str, output_dir:str):
def write_py_files(grammar_name:str, parser_basename:str, context_data:str, output_dir:str):
loader = jj.FileSystemLoader(os.path.join(os.path.dirname(__file__), "templates"))

jj_env = jj.Environment(
Expand All @@ -60,6 +61,7 @@ def write_py_files(grammar_name:str, context_data:str, output_dir:str):

context = {
"grammar_name": grammar_name,
"parser_basename": parser_basename,
"context_data": context_data,
"__version__": __version__,
}
Expand All @@ -83,8 +85,8 @@ def generate(py_parser_path:str, cpp_output_dir:str):
py_output_dir = os.path.dirname(py_parser_path)

# Parse the Parser.py file and extract context data
context_data = extract(py_parser_path)
context_data, parser_basename = extract(py_parser_path)

# Write out files
write_py_files(grammar_name, context_data, py_output_dir)
write_cpp_files(grammar_name, context_data, cpp_output_dir)
write_py_files(grammar_name, parser_basename, context_data, py_output_dir)
write_cpp_files(grammar_name, parser_basename, context_data, cpp_output_dir)
4 changes: 2 additions & 2 deletions speedy_antlr_tool/templates/sa_X_translator.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

#pragma once

#include "{{grammar_name}}BaseVisitor.h"
#include "{{parser_basename}}BaseVisitor.h"
#include "speedy_antlr.h"

class SA_{{grammar_name}}Translator : public {{grammar_name}}BaseVisitor {
class SA_{{grammar_name}}Translator : public {{parser_basename}}BaseVisitor {
speedy_antlr::Translator *translator;

// Cached context classes
Expand Down

0 comments on commit 8bc36f2

Please sign in to comment.