First working version of a demo app that uses the new C++ target.

- Created a new command line target in the XCode project. Win + Linux yet outstanding.
- Reorganized the C++ runtime folder structure a bit
  * Put everything in a new folder "runtime"
  * Added a "demo" folder for the demo grammar + app
  * Renamed Apple folder to Mac in demo folder
  * Added a script with some descriptions to run parser regeneration (via jar or classes). This is also used in the XCode project to regenerate the files.
  * Moved all C++ runtime files up in the folder hierarchy. No need to mimic the deep nesting from Java.
- Some adjustments here and there in the C++ runtime for consistency.
- Overhaul of the Cpp.stg file to produce compilable code. Extracted file level templates into a new template Files.stg. Experimented with new named actions (@parser::listenerheader) but the result is not satisfying yet. Need to investigate more.
- Extended ANTLR to produce header files if a target class returns true in the new function needsHeader().
- Added generated folder from demo to .gitignore
- Added myself to contributors file + maven xml.
This commit is contained in:
Mike Lischke 2016-03-15 21:02:47 +01:00
parent 29ee73bd0a
commit 7e09002ebc
351 changed files with 4587 additions and 3340 deletions

2
.gitignore vendored
View File

@ -61,3 +61,5 @@ runtime/Java/target
tool/target
runtime-testsuite/target
tool-testsuite/target
runtime/Cpp/demo/generated
runtime/Cpp/demo/Mac/antlrcpp.xcodeproj/xcuserdata

View File

@ -88,3 +88,4 @@ YYYY/MM/DD, github id, Full name, email
2015/12/17, sebadur, Sebastian Badur, sebadur@users.noreply.github.com
2015/12/23, pboyer, Peter Boyer, peter.b.boyer@gmail.com
2015/12/24, dtymon, David Tymon, david.tymon@gmail.com
2016/03/10, mike-lischke, Mike Lischke, mike@lischke-online.de

View File

@ -56,6 +56,12 @@
<role>Developer - Maven Plugin</role>
</roles>
</developer>
<developer>
<name>Mike Lischke</name>
<roles>
<role>Developer - C++ Target</role>
</roles>
</developer>
</developers>
<modules>

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,14 @@
//
// main.cpp
// antlr4-cpp-demo
//
// Created by Mike Lischke on 13.03.16.
//
#include <iostream>
int main(int argc, const char * argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
return 0;
}

View File

@ -7,7 +7,7 @@
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>com.antlr.$(PRODUCT_NAME:rfc1034identifier)</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "self:">
</FileRef>
</Workspace>

View File

@ -0,0 +1,56 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0720"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "37F135671B4AC02800E0CACF"
BuildableName = "antlrcpp Tests.xctest"
BlueprintName = "antlrcpp Tests"
ReferencedContainer = "container:antlrcpp.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>

View File

@ -0,0 +1,99 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0720"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "37D727A91867AF1E007B6D10"
BuildableName = "antlrcpp.dylib"
BlueprintName = "antlrcpp"
ReferencedContainer = "container:antlrcpp.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "37F135671B4AC02800E0CACF"
BuildableName = "antlrcpp Tests.xctest"
BlueprintName = "antlrcpp Tests"
ReferencedContainer = "container:antlrcpp.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "37D727A91867AF1E007B6D10"
BuildableName = "antlrcpp.dylib"
BlueprintName = "antlrcpp"
ReferencedContainer = "container:antlrcpp.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "37D727A91867AF1E007B6D10"
BuildableName = "antlrcpp.dylib"
BlueprintName = "antlrcpp"
ReferencedContainer = "container:antlrcpp.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "37D727A91867AF1E007B6D10"
BuildableName = "antlrcpp.dylib"
BlueprintName = "antlrcpp"
ReferencedContainer = "container:antlrcpp.xcodeproj">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>

View File

@ -0,0 +1,80 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0720"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "37C147161B4D5A04008EDDDB"
BuildableName = "libantlrcpp_static.a"
BlueprintName = "antlrcpp_static"
ReferencedContainer = "container:antlrcpp.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "37C147161B4D5A04008EDDDB"
BuildableName = "libantlrcpp_static.a"
BlueprintName = "antlrcpp_static"
ReferencedContainer = "container:antlrcpp.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "37C147161B4D5A04008EDDDB"
BuildableName = "libantlrcpp_static.a"
BlueprintName = "antlrcpp_static"
ReferencedContainer = "container:antlrcpp.xcodeproj">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>

Binary file not shown.

View File

@ -0,0 +1,41 @@
lexer grammar TLexer;
@lexer::header {
// Lexer header
}
@lexer::members {
bool canTestFoo() { return true; }
bool isItFoo() { return true; }
bool isItBar() { return true; }
void myFooLexerAction() { /* do something*/ };
void myBarLexerAction() { /* do something*/ };
}
@lexer::context
{
// Lexer context.
}
@lexer::apifuncs
{
// Lexer API functions.
}
channels { COMMENTS_CHANNEL, DIRECTIVE }
ID: [a-z]+ ;
LessThan: '<' -> pushMode(Mode1);
GreaterThan: '>' -> popMode;
Foo: {canTestFoo()}? 'foo' {isItFoo()}? { myFooLexerAction(); };
Bar: 'bar' {isItBar()}? { myBarLexerAction(); };
Any: Foo Dot Bar? DotDot Baz;
fragment Baz: 'Baz';
mode Mode1;
Dot: '.';
mode Mode2;
DotDot: '..';

View File

@ -0,0 +1,66 @@
parser grammar TParser;
options {
tokenVocab = TLexer;
}
@parser::header {
// Parser header
}
@parser::members {
bool myAction() {
return true;
}
bool doesItBlend() {
return true;
}
void cleanUp() {
}
void doInit() {
}
void doAfter() {
}
}
@parser::listenerheader {
// Listener header.
}
@parser::listenermembers {
// Listener members.
}
@parser::visitorheader {
// Visitor header.
}
@parser::visitormembers {
// Visitor members.
}
main: divide and_? conquer;
divide : LessThan and_ GreaterThan {doesItBlend()}?;
and_ @init{ doInit(); } @after { doAfter(); } : ID ;
conquer:
divide+
| {doesItBlend()}? and_ { myAction(); }
| conquer LessThan* divide
;
// Unused rule to demonstrate some of the special features.
// Note: returns and throws are ignored in the C++ target.
unused returns [double] throws stones, flowers @init{ doInit(); } @after { doAfter(); } :
;
catch [...] {
// Replaces the standard exception handling.
}
finally {
cleanUp();
}

30
runtime/Cpp/demo/generate.sh Executable file
View File

@ -0,0 +1,30 @@
#!/bin/bash
set -o errexit
# Created 2016, Mike Lischke (public domain)
# This script is used to generate source files from the test grammars in the same folder. The generated files are placed
# into a subfolder "generated" which the demo project uses to compile a demo binary.
# There are 3 ways of running the ANTLR generator here.
# 1) Download the pre-built antlr jar, put in a folder of your choice and run the generation using the command in 2) but
# with a LOCATION set to your downloaded jar.
# 2) Run "mvn -DskipTests install" in the root of the antlr4 repo to compile a fresh (snapshot) jar (requires Maven to be installed).
# There are 2 variants to run the generation here, one with and the other w/o template debugger option.
#LOCATION=../../../tool/target/antlr4-4.5.3-SNAPSHOT.jar
#java -jar $LOCATION -Dlanguage=Cpp -listener -visitor -o generated/ -package antlrcpptest TLexer.g4 TParser.g4
#java -jar $LOCATION -Dlanguage=Cpp -listener -visitor -o generated/ -package antlrcpptest -XdbgST TLexer.g4 TParser.g4
# 3) Running from class path. This requires that you have both antlr3 and antlr4 compiled. In this scenario no installation
# is needed. You just compile the java class files (using "mvn compile" in both the antlr4 and the antlr3 root folders).
# The script then runs the generation using these class files, by specifying them on the classpath.
# Also the string template jar is needed. Adjust CLASSPATH if you have stored the jar in a different folder as this script assumes.
# Furthermore is assumed that the antlr3 folder is located side-by-side with the antlr4 folder. Adjust CLASSPATH if not.
# This approach is especially useful if you are working on a target stg file, as it doesn't require to regenerate the
# antlr jar over and over again.
CLASSPATH=../../../tool/resources/:ST-4.0.8.jar:../../../tool/target/classes:../../../runtime/Java/target/classes:../../../../antlr3/runtime/Java/target/classes
java -cp $CLASSPATH org.antlr.v4.Tool -Dlanguage=Cpp -listener -visitor -o generated/ -package antlrcpptest TLexer.g4 TParser.g4
#java -cp $CLASSPATH org.antlr.v4.Tool -Dlanguage=Java -listener -visitor -o generated/ -package antlrcpptest TLexer.g4 TParser.g4
#java -cp $CLASSPATH org.antlr.v4.Tool -Dlanguage=Cpp -listener -visitor -o generated/ -package antlrcpptest -XdbgST TLexer.g4 TParser.g4

View File

@ -7,6 +7,7 @@
/*
* [The "BSD license"]
* Copyright (c) 2016 Mike Lischke
* Copyright (c) 2013 Terence Parr
* Copyright (c) 2013 Dan McLaughlin
* All rights reserved.
@ -49,9 +50,9 @@ namespace org {
virtual void reportAmbiguity(Parser *recognizer, dfa::DFA *dfa, int startIndex, int stopIndex, bool exact, antlrcpp::BitSet *ambigAlts, atn::ATNConfigSet *configs) override;
virtual void reportAttemptingFullContext(Parser *recognizer, dfa::DFA *dfa, int startIndex, int stopIndex, antlrcpp::BitSet *conflictingAlts, atn::ATNConfigSet *configs);
virtual void reportAttemptingFullContext(Parser *recognizer, dfa::DFA *dfa, int startIndex, int stopIndex, antlrcpp::BitSet *conflictingAlts, atn::ATNConfigSet *configs) override;
virtual void reportContextSensitivity(Parser *recognizer, dfa::DFA *dfa, int startIndex, int stopIndex, int prediction, atn::ATNConfigSet *configs);
virtual void reportContextSensitivity(Parser *recognizer, dfa::DFA *dfa, int startIndex, int stopIndex, int prediction, atn::ATNConfigSet *configs) override;
};
}

View File

@ -10,6 +10,7 @@
/*
* [The "BSD license"]
* Copyright (c) 2016 Mike Lischke
* Copyright (c) 2013 Terence Parr
* Copyright (c) 2013 Dan McLaughlin
* All rights reserved.
@ -270,14 +271,6 @@ namespace org {
return _channel;
}
const std::vector<std::wstring>& Lexer::getModeNames() {
return _modeNames;
}
const std::vector<std::wstring>& Lexer::getTokenNames() {
return _tokenNames;
}
std::vector<Token*> Lexer::getAllTokens() {
std::vector<Token*> tokens = std::vector<Token*>();
Token *t = nextToken();

View File

@ -13,6 +13,7 @@
/*
* [The "BSD license"]
* Copyright (c) 2016 Mike Lischke
* Copyright (c) 2013 Terence Parr
* Copyright (c) 2013 Dan McLaughlin
* All rights reserved.
@ -66,9 +67,6 @@ namespace org {
protected:
std::pair<TokenSource*, CharStream*> *_tokenFactorySourcePair;
std::vector<std::wstring> _modeNames;
std::vector<std::wstring> _tokenNames;
/// <summary>
/// How to create token objects </summary>
TokenFactory<CommonToken*> *_factory;
@ -134,7 +132,7 @@ namespace org {
/// Return a token from this source; i.e., match a token on the char
/// stream.
/// </summary>
virtual Token *nextToken();
virtual Token *nextToken() override;
/// <summary>
/// Instruct the lexer to skip creating a token for current lexer rule
@ -162,7 +160,7 @@ namespace org {
/// <summary>
/// Set the char stream and reset the lexer </summary>
virtual void setInputStream(IntStream *input);
virtual void setInputStream(IntStream *input) override;
virtual std::string getSourceName() override;
@ -225,14 +223,7 @@ namespace org {
virtual int getChannel();
virtual const std::vector<std::wstring>& getModeNames();
/// <summary>
/// Used to print out token names like ID during debugging and
/// error reporting. The generated parsers implement a method
/// that overrides this to point to their String[] tokenNames.
/// </summary>
virtual const std::vector<std::wstring>& getTokenNames() override;
virtual const std::vector<std::wstring>& getModeNames() const = 0;
/// <summary>
/// Return a list of all Token objects in input char stream.

View File

@ -8,6 +8,7 @@
/*
* [The "BSD license"]
* Copyright (c) 2016 Mike Lischke
* Copyright (c) 2013 Terence Parr
* Copyright (c) 2013 Dan McLaughlin
* All rights reserved.
@ -40,7 +41,7 @@ namespace org {
namespace antlr {
namespace v4 {
namespace runtime {
LexerInterpreter::LexerInterpreter(const std::wstring &grammarFileName, std::vector<std::wstring> *tokenNames, std::vector<std::wstring> *ruleNames, std::vector<std::wstring> *modeNames, atn::ATN *atn, CharStream *input) : Lexer(_input), grammarFileName(grammarFileName), atn(atn), _sharedContextCache(new atn::PredictionContextCache()) {
LexerInterpreter::LexerInterpreter(const std::wstring &grammarFileName, std::vector<std::wstring> *tokenNames, std::vector<std::wstring> *ruleNames, std::vector<std::wstring> *modeNames, atn::ATN *atn, CharStream *input) : Lexer(input), grammarFileName(grammarFileName), atn(atn), _sharedContextCache(new atn::PredictionContextCache()) {
if (atn->grammarType != atn::ATNType::LEXER) {
throw new IllegalArgumentException(L"The ATN must be a lexer ATN.");
@ -61,23 +62,23 @@ namespace org {
_modeNames = *modeNames;
}
}
atn::ATN *LexerInterpreter::getATN() {
atn::ATN *LexerInterpreter::getATN() const {
return atn;
}
std::wstring LexerInterpreter::getGrammarFileName() {
std::wstring LexerInterpreter::getGrammarFileName() const {
return grammarFileName;
}
const std::vector<std::wstring>& LexerInterpreter::getTokenNames() {
const std::vector<std::wstring>& LexerInterpreter::getTokenNames() const {
return _tokenNames;
}
const std::vector<std::wstring>& LexerInterpreter::getRuleNames() {
const std::vector<std::wstring>& LexerInterpreter::getRuleNames() const {
return _ruleNames;
}
const std::vector<std::wstring>& LexerInterpreter::getModeNames() {
const std::vector<std::wstring>& LexerInterpreter::getModeNames() const {
return _modeNames;
}
}

View File

@ -7,6 +7,7 @@
/*
* [The "BSD license"]
* Copyright (c) 2016 Mike Lischke
* Copyright (c) 2013 Terence Parr
* Copyright (c) 2013 Dan McLaughlin
* All rights reserved.
@ -58,15 +59,15 @@ namespace org {
public:
LexerInterpreter(const std::wstring &grammarFileName, std::vector<std::wstring> *tokenNames, std::vector<std::wstring> *ruleNames, std::vector<std::wstring> *modeNames, atn::ATN *atn, CharStream *input);
virtual atn::ATN *getATN() override;
virtual atn::ATN *getATN() const override;
virtual std::wstring getGrammarFileName() override;
virtual std::wstring getGrammarFileName() const override;
virtual const std::vector<std::wstring>& getTokenNames() override;
virtual const std::vector<std::wstring>& getTokenNames() const override;
virtual const std::vector<std::wstring>& getRuleNames() override;
virtual const std::vector<std::wstring>& getRuleNames() const override;
virtual const std::vector<std::wstring>& getModeNames() override;
virtual const std::vector<std::wstring>& getModeNames() const override;
};
}

View File

@ -9,6 +9,7 @@
#include <algorithm>
/*
* [The "BSD license"]
* Copyright (c) 2016 Mike Lischke
* Copyright (c) 2013 Terence Parr
* Copyright (c) 2013 Dan McLaughlin
* All rights reserved.
@ -158,7 +159,7 @@ namespace org {
/// <summary>
/// @inheritDoc
/// </summary>
virtual TokenFactory<CommonToken *> *getTokenFactory();
virtual TokenFactory<CommonToken *> *getTokenFactory() override;
private:
void InitializeInstanceFields();

View File

@ -28,6 +28,7 @@
/*
* [The "BSD license"]
* Copyright (c) 2016 Mike Lischke
* Copyright (c) 2013 Terence Parr
* Copyright (c) 2013 Dan McLaughlin
* All rights reserved.
@ -102,7 +103,7 @@ namespace org {
void Parser::TrimToSizeListener::exitEveryRule(ParserRuleContext *ctx) {
// TODO: Need to figure out what type this is going to be. In Java we expect it to be set by the generator.
std::vector<tree::ParseTree*>* tmp = dynamic_cast<std::vector<tree::ParseTree*>*>(&ctx->children);
std::vector<tree::ParseTree*>* tmp = dynamic_cast<std::vector<tree::ParseTree*>*>(&ctx->_children);
if (tmp != nullptr) {
tmp->shrink_to_fit();
}
@ -564,7 +565,7 @@ namespace org {
std::lock_guard<std::mutex> lck(mtx);
std::vector<std::wstring> s = std::vector<std::wstring>();
for (size_t d = 0; d < _interp->_decisionToDFA.size(); d++) {
dfa::DFA *dfa = &_interp->_decisionToDFA[d];
dfa::DFA *dfa = _interp->_decisionToDFA[d];
s.push_back(dfa->toString(getTokenNames()));
}
return s;
@ -578,7 +579,7 @@ namespace org {
std::lock_guard<std::mutex> lck(mtx);
bool seenOne = false;
for (size_t d = 0; d < _interp->_decisionToDFA.size(); d++) {
dfa::DFA *dfa = &_interp->_decisionToDFA[d];
dfa::DFA *dfa = _interp->_decisionToDFA[d];
if (!dfa->states->empty()) {
if (seenOne) {
std::cout << std::endl;

View File

@ -25,6 +25,7 @@
/*
* [The "BSD license"]
* Copyright (c) 2016 Mike Lischke
* Copyright (c) 2013 Terence Parr
* Copyright (c) 2013 Dan McLaughlin
* All rights reserved.
@ -58,11 +59,11 @@ namespace org {
namespace v4 {
namespace runtime {
ParserInterpreter::ParserInterpreter(const std::wstring &grammarFileName, const std::vector<std::wstring>& tokenNames, const std::vector<std::wstring>& ruleNames, atn::ATN *atn, TokenStream *input) : Parser(_input), grammarFileName(grammarFileName), _tokenNames(tokenNames), atn(atn), _ruleNames(ruleNames), pushRecursionContextStates(new antlrcpp::BitSet()), sharedContextCache(new atn::PredictionContextCache()), _parentContextStack(new std::deque<std::pair<ParserRuleContext *, int>*>()) {
ParserInterpreter::ParserInterpreter(const std::wstring &grammarFileName, const std::vector<std::wstring>& tokenNames, const std::vector<std::wstring>& ruleNames, atn::ATN *atn, TokenStream *input) : Parser(input), grammarFileName(grammarFileName), _tokenNames(tokenNames), atn(atn), _ruleNames(ruleNames), pushRecursionContextStates(new antlrcpp::BitSet()), sharedContextCache(new atn::PredictionContextCache()), _parentContextStack(new std::deque<std::pair<ParserRuleContext *, int>*>()) {
for (int i = 0; i < atn->getNumberOfDecisions(); i++) {
_decisionToDFA.emplace_back(atn->getDecisionState(i), i);
_decisionToDFA.push_back(new dfa::DFA(atn->getDecisionState(i), i));
}
// identify the ATN states where pushNewRecursionContext must be called
@ -90,19 +91,19 @@ namespace org {
setInterpreter(new atn::ParserATNSimulator(this, atn, _decisionToDFA, sharedContextCache));
}
org::antlr::v4::runtime::atn::ATN *ParserInterpreter::getATN() {
org::antlr::v4::runtime::atn::ATN *ParserInterpreter::getATN() const {
return atn;
}
const std::vector<std::wstring>& ParserInterpreter::getTokenNames() {
const std::vector<std::wstring>& ParserInterpreter::getTokenNames() const {
return _tokenNames;
}
const std::vector<std::wstring>& ParserInterpreter::getRuleNames() {
const std::vector<std::wstring>& ParserInterpreter::getRuleNames() const {
return _ruleNames;
}
std::wstring ParserInterpreter::getGrammarFileName() {
std::wstring ParserInterpreter::getGrammarFileName() const {
return grammarFileName;
}

View File

@ -9,6 +9,7 @@
/*
* [The "BSD license"]
* Copyright (c) 2016 Mike Lischke
* Copyright (c) 2013 Terence Parr
* Copyright (c) 2013 Dan McLaughlin
* All rights reserved.
@ -68,7 +69,7 @@ namespace org {
std::vector<std::wstring> _ruleNames;
antlrcpp::BitSet *const pushRecursionContextStates;
std::vector<dfa::DFA> _decisionToDFA; // not shared like it is for generated parsers
std::vector<dfa::DFA *> _decisionToDFA; // not shared like it is for generated parsers
atn::PredictionContextCache *const sharedContextCache;
@ -78,13 +79,13 @@ namespace org {
public:
ParserInterpreter(const std::wstring &grammarFileName, const std::vector<std::wstring>& tokenNames, const std::vector<std::wstring>& ruleNames, atn::ATN *atn, TokenStream *input);
virtual atn::ATN *getATN() override;
virtual atn::ATN *getATN() const override;
virtual const std::vector<std::wstring>& getTokenNames() override;
virtual const std::vector<std::wstring>& getTokenNames() const override;
virtual const std::vector<std::wstring>& getRuleNames() override;
virtual const std::vector<std::wstring>& getRuleNames() const override;
virtual std::wstring getGrammarFileName() override;
virtual std::wstring getGrammarFileName() const override;
/// <summary>
/// Begin parsing at startRuleIndex </summary>

View File

@ -8,6 +8,7 @@
/*
* [The "BSD license"]
* Copyright (c) 2016 Mike Lischke
* Copyright (c) 2013 Terence Parr
* Copyright (c) 2013 Dan McLaughlin
* All rights reserved.
@ -63,24 +64,24 @@ namespace org {
}
tree::TerminalNode *ParserRuleContext::addChild(tree::TerminalNode *t) {
if (children.empty()) {
children = std::vector<ParseTree*>();
if (_children.empty()) {
_children = std::vector<ParseTree*>();
}
children.push_back(t);
_children.push_back(t);
return t;
}
RuleContext *ParserRuleContext::addChild(RuleContext *ruleInvocation) {
if (children.empty()) {
children = std::vector<ParseTree*>();
if (_children.empty()) {
_children = std::vector<ParseTree*>();
}
children.push_back(ruleInvocation);
_children.push_back(ruleInvocation);
return ruleInvocation;
}
void ParserRuleContext::removeLastChild() {
if (children.size() > 0) {
children.pop_back();
if (_children.size() > 0) {
_children.pop_back();
}
}
@ -105,20 +106,19 @@ namespace org {
return static_cast<ParserRuleContext*>(RuleContext::getParent());
}
tree::ParseTree *ParserRuleContext::getChild(int i) {
// TODO: i should really be size_t
return children.size() > 0 && i >= 0 && i < (int)children.size() ? children[i] : nullptr;
tree::ParseTree *ParserRuleContext::getChild(std::size_t i) {
return _children.size() > 0 && i < _children.size() ? _children[i] : nullptr;
}
tree::TerminalNode *ParserRuleContext::getToken(int ttype, int i) {
if (children.empty() || i < 0 || i >= (int)children.size()) {
tree::TerminalNode *ParserRuleContext::getToken(int ttype, std::size_t i) {
if (_children.empty() || i >= _children.size()) {
return nullptr;
}
int j = -1; // what token with ttype have we found?
for (auto o : children) {
for (auto &o : _children) {
if (dynamic_cast<tree::TerminalNode*>(o) != nullptr) {
tree::TerminalNode *tnode = static_cast<tree::TerminalNode*>(o);
Token *symbol = tnode->getSymbol();
@ -136,12 +136,12 @@ namespace org {
// I think this should be changed to a pointer?
std::vector<tree::TerminalNode*> ParserRuleContext::getTokens(int ttype) {
if (children.empty()) {
if (_children.empty()) {
return std::vector<tree::TerminalNode*>();
}
std::vector<tree::TerminalNode*> tokens;
for (auto o : children) {
for (auto &o : _children) {
if (dynamic_cast<tree::TerminalNode*>(o) != nullptr) {
tree::TerminalNode *tnode = static_cast<tree::TerminalNode*>(o);
Token *symbol = tnode->getSymbol();
@ -163,8 +163,8 @@ namespace org {
int ParserRuleContext::getChildCount() {
return (int)children.size() > 0 ? (int)children.size() : 0;
std::size_t ParserRuleContext::getChildCount() {
return _children.size() > 0 ? _children.size() : 0;
}
misc::Interval *ParserRuleContext::getSourceInterval() {

View File

@ -7,10 +7,9 @@
#include "RuleContext.h"
#include "Declarations.h"
/*
* [The "BSD license"]
* Copyright (c) 2016 Mike Lischke
* Copyright (c) 2013 Terence Parr
* Copyright (c) 2013 Dan McLaughlin
* All rights reserved.
@ -77,7 +76,7 @@ namespace org {
/// how we parse this rule.
/// </summary>
public:
std::vector<ParseTree*> children;
std::vector<ParseTree*> _children;
/// <summary>
/// For debugging/tracing purposes, we want to track all of the nodes in
@ -124,9 +123,9 @@ namespace org {
/// <summary>
/// Does not set parent link; other add methods do that </summary>
virtual tree::TerminalNode *addChild(tree::TerminalNode *t);
virtual tree::TerminalNode* addChild(tree::TerminalNode *t);
virtual RuleContext *addChild(RuleContext *ruleInvocation);
virtual RuleContext* addChild(RuleContext *ruleInvocation);
/// <summary>
/// Used by enterOuterAlt to toss out a RuleContext previously added as
@ -135,66 +134,66 @@ namespace org {
/// </summary>
virtual void removeLastChild();
virtual tree::TerminalNode *addChild(Token *matchedToken);
virtual tree::TerminalNode* addChild(Token *matchedToken);
virtual tree::ErrorNode *addErrorNode(Token *badToken);
virtual tree::ErrorNode* addErrorNode(Token *badToken);
virtual ParserRuleContext *getParent() override;
virtual ParserRuleContext* getParent() override;
virtual ParseTree *getChild(int i) override;
virtual ParseTree* getChild(std::size_t i) override;
template<typename T>
T getChild(void *ctxType, int i) {
if (children.empty() || i < 0 || i >= (int) children.size()) {
T* getChild(size_t i) {
if (_children.empty() || i >= _children.size()) {
return nullptr;
}
int j = -1; // what element have we found with ctxType?
for (auto o : children) {
if (typeid(ctxType) == typeid(o)) {
for (auto &child : _children) {
if (dynamic_cast<T *>(child) != nullptr) {
j++;
if (j == i) {
return dynamic_cast<T>(o);
return dynamic_cast<T *>(child);
}
}
}
return nullptr;
}
virtual tree::TerminalNode *getToken(int ttype, int i);
virtual tree::TerminalNode* getToken(int ttype, std::size_t i);
virtual std::vector<tree::TerminalNode*> getTokens(int ttype);
virtual std::vector<tree::TerminalNode *> getTokens(int ttype);
template<typename T>
T getRuleContext(void *ctxType, int i) {
return getChild<T>(ctxType, i);
T* getRuleContext(int i) {
return getChild<T>(i);
}
template<typename T>
std::vector<T> getRuleContexts(void *ctxType) {
if (children.empty()) {
return std::vector<T>();
std::vector<T*> getRuleContexts() {
if (_children.empty()) {
return std::vector<T *>();
}
std::vector<T> contexts;
for (auto o : children) {
if (typeid(ctxType) == typeid(o)) {
std::vector<T *> contexts;
for (auto &child : _children) {
if (dynamic_cast<T *>(child)) {
if (contexts.empty()) {
contexts = std::vector<T>();
contexts = std::vector<T *>();
}
contexts.push_back((ParserRuleContext*)(o));
contexts.push_back((T *)(child));
}
}
if (contexts.empty()) {
return std::vector<T>();
return std::vector<T *>();
}
return contexts;
}
virtual int getChildCount() override;
virtual std::size_t getChildCount() override;
virtual misc::Interval *getSourceInterval() override;
virtual Token *getStart();

View File

@ -21,6 +21,7 @@
/*
* [The "BSD license"]
* Copyright (c) 2016 Mike Lischke
* Copyright (c) 2013 Terence Parr
* Copyright (c) 2013 Dan McLaughlin
* All rights reserved.
@ -84,16 +85,12 @@ namespace org {
/// <summary>
/// Used to print out token names like ID during debugging and
/// error reporting. The generated parsers implement a method
/// that overrides this to point to their String[] tokenNames.
/// that overrides this to point to their generated token names list.
/// </summary>
public:
virtual const std::vector<std::wstring>& getTokenNames(){
throw new ASSERTException(L"Recognizer", L"getTokenNames should never be called, abstract class");
};// = 0;
virtual const std::vector<std::wstring>& getTokenNames() const = 0;
virtual const std::vector<std::wstring>& getRuleNames() {
throw new ASSERTException(L"Recognizer", L"getRuleNames should never be called, abstract class");
};// = 0;
virtual const std::vector<std::wstring>& getRuleNames() const = 0;
/// <summary>
/// Get a map from token names to token types.
@ -126,19 +123,19 @@ namespace org {
/// For debugging and other purposes, might want the grammar name.
/// Have ANTLR generate an implementation for this method.
/// </summary>
virtual std::wstring getGrammarFileName() = 0;
virtual std::wstring getGrammarFileName() const = 0;
/// <summary>
/// Get the <seealso cref="ATN"/> used by the recognizer for prediction.
/// </summary>
/// <returns> The <seealso cref="ATN"/> used by the recognizer for prediction. </returns>
virtual atn::ATN *getATN() = 0;
virtual atn::ATN *getATN() const = 0;
/// <summary>
/// Get the ATN interpreter used by the recognizer for prediction.
/// </summary>
/// <returns> The ATN interpreter used by the recognizer for prediction. </returns>
virtual ATNInterpreter getInterpreter() {
virtual ATNInterpreter getInterpreter() const {
return _interp;
}
@ -199,18 +196,11 @@ namespace org {
/// </summary>
void setState(int atnState);
virtual IntStream *getInputStream(){
throw new ASSERTException(L"Recognizer::getInputStream", L"Should never be called, abstract class");
virtual IntStream *getInputStream() = 0;
};// = 0;
virtual void setInputStream(IntStream *input) = 0;
virtual void setInputStream(IntStream *input){
throw new ASSERTException(L"Recognizer::setInputStream", L"Should never be called, abstract class");
};// = 0;
virtual TokenFactory<CommonToken *> *getTokenFactory(){
throw new ASSERTException(L"Recognizer::getTokenFactory", L"Should never be called, abstract class");
};// = 0;
virtual TokenFactory<CommonToken *> *getTokenFactory() = 0;
template<typename T1>
void setTokenFactory(TokenFactory<T1> *input);

View File

@ -11,6 +11,7 @@
/*
* [The "BSD license"]
* Copyright (c) 2016 Mike Lischke
* Copyright (c) 2013 Terence Parr
* Copyright (c) 2013 Dan McLaughlin
* All rights reserved.
@ -114,11 +115,11 @@ namespace org {
return -1;
}
tree::ParseTree *RuleContext::getChild(int i) {
tree::ParseTree *RuleContext::getChild(std::size_t i) {
return nullptr;
}
int RuleContext::getChildCount() {
std::size_t RuleContext::getChildCount() {
return 0;
}

View File

@ -12,6 +12,7 @@
/*
* [The "BSD license"]
* Copyright (c) 2016 Mike Lischke
* Copyright (c) 2013 Terence Parr
* Copyright (c) 2013 Dan McLaughlin
* All rights reserved.
@ -104,22 +105,15 @@ namespace org {
virtual int getRuleIndex();
virtual ParseTree *getChild(int i) override;
virtual ParseTree *getChild(std::size_t i) override;
virtual int getChildCount() override;
virtual std::size_t getChildCount() override;
template<typename T, typename T1>
T accept(tree::ParseTreeVisitor<T1> *visitor) {
return visitor->visitChildren(this);
}
/// <summary>
/// Call this method to view a parse tree in a dialog box visually. </summary>
#ifdef TODO
virtual Future<JDialog*> *inspect(Parser *parser);
virtual Future<JDialog*> *inspect(std::vector<std::wstring> &ruleNames);
#endif
/// <summary>
/// Save this tree in a postscript file </summary>
virtual void save(Parser *parser, const std::wstring &fileName);
@ -141,7 +135,7 @@ namespace org {
/// (root child1 .. childN). Print just a node if this is a leaf.
/// We have to know the recognizer so we can get rule names.
/// </summary>
virtual std::wstring toStringTree(Parser *recog);
virtual std::wstring toStringTree(Parser *recog) override;
/// <summary>
/// Print out a whole tree, not just a node, in LISP format
@ -151,7 +145,7 @@ namespace org {
virtual std::wstring toStringTree() override;
virtual std::wstring toString();
virtual std::wstring toString() override;
template<typename T1, typename T2>

Some files were not shown because too many files have changed in this diff Show More