diff --git a/runtime/Cpp/Apple/antlrcpp Tests/Info.plist b/runtime/Cpp/Apple/antlrcpp Tests/Info.plist new file mode 100644 index 000000000..390cade43 --- /dev/null +++ b/runtime/Cpp/Apple/antlrcpp Tests/Info.plist @@ -0,0 +1,24 @@ + + + + + CFBundleDevelopmentRegion + en + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIdentifier + com.antlr.$(PRODUCT_NAME:rfc1034identifier) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + BNDL + CFBundleShortVersionString + 1.0 + CFBundleSignature + ???? + CFBundleVersion + 1 + + diff --git a/runtime/Cpp/Apple/antlrcpp Tests/antlrcpp_Tests.mm b/runtime/Cpp/Apple/antlrcpp Tests/antlrcpp_Tests.mm new file mode 100644 index 000000000..e5d35a879 --- /dev/null +++ b/runtime/Cpp/Apple/antlrcpp Tests/antlrcpp_Tests.mm @@ -0,0 +1,73 @@ +/* + * [The "BSD license"] + * Copyright (c) 2015 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#import +#import +#include "ParserATNSimulator.h" +#include "DFA.h" + +#include + +@interface antlrcpp_Tests : XCTestCase + +@end + +@implementation antlrcpp_Tests + +- (void)setUp { + [super setUp]; + // Put setup code here. This method is called before the invocation of each test method in the class. +} + +- (void)tearDown { + // Put teardown code here. This method is called after the invocation of each test method in the class. + [super tearDown]; +} + +- (void)testExample { + try { + std::vector decisionToDFA; + + org::antlr::v4::runtime::atn::ParserATNSimulator foo(nullptr, decisionToDFA, nullptr); + } + catch (std::exception e) { + + XCTAssert(NO, @"Fail"); + } + XCTAssert(YES, @"Pass"); +} + +- (void)testPerformanceExample { + // This is an example of a performance test case. + [self measureBlock:^{ + // Put the code you want to measure the time of here. + }]; +} + +@end diff --git a/runtime/Cpp/Apple/antlrcpp.xcodeproj/project.pbxproj b/runtime/Cpp/Apple/antlrcpp.xcodeproj/project.pbxproj new file mode 100644 index 000000000..f01684399 --- /dev/null +++ b/runtime/Cpp/Apple/antlrcpp.xcodeproj/project.pbxproj @@ -0,0 +1,1709 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 46; + objects = { + +/* Begin PBXBuildFile section */ + 16F9451C1B23CB250049275C /* BitSet.h in Headers */ = {isa = PBXBuildFile; fileRef = 16F9451B1B23CB250049275C /* BitSet.h */; }; + 373775A2192FE6F400A4058D /* UUID.h in Headers */ = {isa = PBXBuildFile; fileRef = 373775A1192FE6F400A4058D /* UUID.h */; }; + 37725BA51B39B09000BDA484 /* CPPUtils.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37725BA31B39B09000BDA484 /* CPPUtils.cpp */; }; + 37725BA61B39B09000BDA484 /* CPPUtils.h in Headers */ = {isa = PBXBuildFile; fileRef = 37725BA41B39B09000BDA484 /* CPPUtils.h */; }; + 3789F5001870D1D8003E0A30 /* StringBuilder.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3789F4FE1870D1D8003E0A30 /* StringBuilder.cpp */; }; + 3789F5011870D1D8003E0A30 /* StringBuilder.h in Headers */ = {isa = PBXBuildFile; fileRef = 3789F4FF1870D1D8003E0A30 /* StringBuilder.h */; }; + 3789F5041870E074003E0A30 /* Arrays.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3789F5021870E074003E0A30 /* Arrays.cpp */; }; + 3789F5051870E074003E0A30 /* Arrays.h in Headers */ = {isa = PBXBuildFile; fileRef = 3789F5031870E074003E0A30 /* Arrays.h */; }; + 3789F5091872F5C2003E0A30 /* XPathLexer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3789F5071872F5C2003E0A30 /* XPathLexer.cpp */; }; + 3789F50A1872F5C2003E0A30 /* XPathLexer.h in Headers */ = {isa = PBXBuildFile; fileRef = 3789F5081872F5C2003E0A30 /* XPathLexer.h */; }; + 37BED8C4187304740043FB28 /* Exceptions.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37BED8C2187304740043FB28 /* Exceptions.cpp */; }; + 37BED8C5187304740043FB28 /* Exceptions.h in Headers */ = {isa = PBXBuildFile; fileRef = 37BED8C3187304740043FB28 /* Exceptions.h */; }; + 37C0684B191E636C00B4D312 /* Strings.h in Headers */ = {isa = PBXBuildFile; fileRef = 37C0684A191E636C00B4D312 /* Strings.h */; }; + 37C0684D1920EF8300B4D312 /* Strings.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37C0684C1920EF8300B4D312 /* Strings.cpp */; }; + 37D1C3FB186A31140041671A /* ANTLRErrorListener.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C2C6186A31130041671A /* ANTLRErrorListener.cpp */; }; + 37D1C3FC186A31140041671A /* ANTLRErrorListener.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C2C7186A31130041671A /* ANTLRErrorListener.h */; }; + 37D1C3FD186A31140041671A /* ANTLRErrorStrategy.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C2C8186A31130041671A /* ANTLRErrorStrategy.cpp */; }; + 37D1C3FE186A31140041671A /* ANTLRErrorStrategy.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C2C9186A31130041671A /* ANTLRErrorStrategy.h */; }; + 37D1C3FF186A31140041671A /* ANTLRFileStream.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C2CA186A31130041671A /* ANTLRFileStream.cpp */; }; + 37D1C400186A31140041671A /* ANTLRFileStream.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C2CB186A31130041671A /* ANTLRFileStream.h */; }; + 37D1C401186A31140041671A /* ANTLRInputStream.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C2CC186A31130041671A /* ANTLRInputStream.cpp */; }; + 37D1C402186A31140041671A /* ANTLRInputStream.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C2CD186A31130041671A /* ANTLRInputStream.h */; }; + 37D1C403186A31140041671A /* AbstractPredicateTransition.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C2CF186A31130041671A /* AbstractPredicateTransition.cpp */; }; + 37D1C404186A31140041671A /* AbstractPredicateTransition.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C2D0186A31130041671A /* AbstractPredicateTransition.h */; }; + 37D1C405186A31140041671A /* ActionTransition.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C2D1186A31130041671A /* ActionTransition.cpp */; }; + 37D1C406186A31140041671A /* ActionTransition.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C2D2186A31130041671A /* ActionTransition.h */; }; + 37D1C407186A31140041671A /* ArrayPredictionContext.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C2D3186A31130041671A /* ArrayPredictionContext.cpp */; }; + 37D1C408186A31140041671A /* ArrayPredictionContext.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C2D4186A31130041671A /* ArrayPredictionContext.h */; }; + 37D1C409186A31140041671A /* ATN.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C2D5186A31130041671A /* ATN.cpp */; }; + 37D1C40A186A31140041671A /* ATN.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C2D6186A31130041671A /* ATN.h */; }; + 37D1C40B186A31140041671A /* ATNConfig.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C2D7186A31130041671A /* ATNConfig.cpp */; }; + 37D1C40C186A31140041671A /* ATNConfig.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C2D8186A31130041671A /* ATNConfig.h */; }; + 37D1C40D186A31140041671A /* ATNConfigSet.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C2D9186A31130041671A /* ATNConfigSet.cpp */; }; + 37D1C40E186A31140041671A /* ATNConfigSet.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C2DA186A31130041671A /* ATNConfigSet.h */; }; + 37D1C40F186A31140041671A /* ATNDeserializationOptions.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C2DB186A31130041671A /* ATNDeserializationOptions.cpp */; }; + 37D1C410186A31140041671A /* ATNDeserializationOptions.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C2DC186A31130041671A /* ATNDeserializationOptions.h */; }; + 37D1C411186A31140041671A /* ATNDeserializer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C2DD186A31130041671A /* ATNDeserializer.cpp */; }; + 37D1C412186A31140041671A /* ATNDeserializer.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C2DE186A31130041671A /* ATNDeserializer.h */; }; + 37D1C413186A31140041671A /* ATNSerializer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C2DF186A31130041671A /* ATNSerializer.cpp */; }; + 37D1C414186A31140041671A /* ATNSerializer.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C2E0186A31130041671A /* ATNSerializer.h */; }; + 37D1C415186A31140041671A /* ATNSimulator.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C2E1186A31130041671A /* ATNSimulator.cpp */; }; + 37D1C416186A31140041671A /* ATNSimulator.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C2E2186A31130041671A /* ATNSimulator.h */; }; + 37D1C417186A31140041671A /* ATNState.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C2E3186A31130041671A /* ATNState.cpp */; }; + 37D1C418186A31140041671A /* ATNState.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C2E4186A31130041671A /* ATNState.h */; }; + 37D1C419186A31140041671A /* ATNType.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C2E5186A31130041671A /* ATNType.cpp */; }; + 37D1C41A186A31140041671A /* ATNType.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C2E6186A31130041671A /* ATNType.h */; }; + 37D1C41B186A31140041671A /* AtomTransition.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C2E7186A31130041671A /* AtomTransition.cpp */; }; + 37D1C41C186A31140041671A /* AtomTransition.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C2E8186A31130041671A /* AtomTransition.h */; }; + 37D1C41D186A31140041671A /* BasicBlockStartState.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C2E9186A31130041671A /* BasicBlockStartState.cpp */; }; + 37D1C41E186A31140041671A /* BasicBlockStartState.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C2EA186A31130041671A /* BasicBlockStartState.h */; }; + 37D1C41F186A31140041671A /* BasicState.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C2EB186A31130041671A /* BasicState.cpp */; }; + 37D1C420186A31140041671A /* BasicState.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C2EC186A31130041671A /* BasicState.h */; }; + 37D1C421186A31140041671A /* BlockEndState.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C2ED186A31130041671A /* BlockEndState.cpp */; }; + 37D1C422186A31140041671A /* BlockEndState.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C2EE186A31130041671A /* BlockEndState.h */; }; + 37D1C423186A31140041671A /* BlockStartState.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C2EF186A31130041671A /* BlockStartState.cpp */; }; + 37D1C424186A31140041671A /* BlockStartState.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C2F0186A31130041671A /* BlockStartState.h */; }; + 37D1C425186A31140041671A /* DecisionState.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C2F1186A31130041671A /* DecisionState.cpp */; }; + 37D1C426186A31140041671A /* DecisionState.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C2F2186A31130041671A /* DecisionState.h */; }; + 37D1C427186A31140041671A /* EmptyPredictionContext.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C2F3186A31130041671A /* EmptyPredictionContext.cpp */; }; + 37D1C428186A31140041671A /* EmptyPredictionContext.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C2F4186A31130041671A /* EmptyPredictionContext.h */; }; + 37D1C429186A31140041671A /* EpsilonTransition.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C2F5186A31130041671A /* EpsilonTransition.cpp */; }; + 37D1C42A186A31140041671A /* EpsilonTransition.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C2F6186A31130041671A /* EpsilonTransition.h */; }; + 37D1C42B186A31140041671A /* LexerATNConfig.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C2F7186A31130041671A /* LexerATNConfig.cpp */; }; + 37D1C42C186A31140041671A /* LexerATNConfig.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C2F8186A31130041671A /* LexerATNConfig.h */; }; + 37D1C42D186A31140041671A /* LexerATNSimulator.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C2F9186A31130041671A /* LexerATNSimulator.cpp */; }; + 37D1C42E186A31140041671A /* LexerATNSimulator.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C2FA186A31130041671A /* LexerATNSimulator.h */; }; + 37D1C42F186A31140041671A /* LL1Analyzer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C2FB186A31130041671A /* LL1Analyzer.cpp */; }; + 37D1C430186A31140041671A /* LL1Analyzer.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C2FC186A31130041671A /* LL1Analyzer.h */; }; + 37D1C431186A31140041671A /* LoopEndState.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C2FD186A31130041671A /* LoopEndState.cpp */; }; + 37D1C432186A31140041671A /* LoopEndState.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C2FE186A31130041671A /* LoopEndState.h */; }; + 37D1C433186A31140041671A /* NotSetTransition.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C2FF186A31130041671A /* NotSetTransition.cpp */; }; + 37D1C434186A31140041671A /* NotSetTransition.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C300186A31130041671A /* NotSetTransition.h */; }; + 37D1C435186A31140041671A /* OrderedATNConfigSet.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C301186A31130041671A /* OrderedATNConfigSet.cpp */; }; + 37D1C436186A31140041671A /* OrderedATNConfigSet.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C302186A31130041671A /* OrderedATNConfigSet.h */; }; + 37D1C437186A31140041671A /* ParserATNSimulator.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C303186A31130041671A /* ParserATNSimulator.cpp */; }; + 37D1C438186A31140041671A /* ParserATNSimulator.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C304186A31130041671A /* ParserATNSimulator.h */; }; + 37D1C439186A31140041671A /* PlusBlockStartState.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C305186A31130041671A /* PlusBlockStartState.cpp */; }; + 37D1C43A186A31140041671A /* PlusBlockStartState.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C306186A31130041671A /* PlusBlockStartState.h */; }; + 37D1C43B186A31140041671A /* PlusLoopbackState.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C307186A31130041671A /* PlusLoopbackState.cpp */; }; + 37D1C43C186A31140041671A /* PlusLoopbackState.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C308186A31130041671A /* PlusLoopbackState.h */; }; + 37D1C43D186A31140041671A /* PrecedencePredicateTransition.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C309186A31130041671A /* PrecedencePredicateTransition.cpp */; }; + 37D1C43E186A31140041671A /* PrecedencePredicateTransition.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C30A186A31130041671A /* PrecedencePredicateTransition.h */; }; + 37D1C43F186A31140041671A /* PredicateTransition.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C30B186A31130041671A /* PredicateTransition.cpp */; }; + 37D1C440186A31140041671A /* PredicateTransition.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C30C186A31130041671A /* PredicateTransition.h */; }; + 37D1C441186A31140041671A /* PredictionContext.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C30D186A31130041671A /* PredictionContext.cpp */; }; + 37D1C442186A31140041671A /* PredictionContext.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C30E186A31130041671A /* PredictionContext.h */; }; + 37D1C443186A31140041671A /* PredictionContextCache.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C30F186A31130041671A /* PredictionContextCache.cpp */; }; + 37D1C444186A31140041671A /* PredictionContextCache.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C310186A31130041671A /* PredictionContextCache.h */; }; + 37D1C445186A31140041671A /* PredictionMode.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C311186A31130041671A /* PredictionMode.cpp */; }; + 37D1C446186A31140041671A /* PredictionMode.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C312186A31130041671A /* PredictionMode.h */; }; + 37D1C447186A31140041671A /* RangeTransition.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C313186A31130041671A /* RangeTransition.cpp */; }; + 37D1C448186A31140041671A /* RangeTransition.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C314186A31130041671A /* RangeTransition.h */; }; + 37D1C449186A31140041671A /* RuleStartState.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C315186A31130041671A /* RuleStartState.cpp */; }; + 37D1C44A186A31140041671A /* RuleStartState.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C316186A31130041671A /* RuleStartState.h */; }; + 37D1C44B186A31140041671A /* RuleStopState.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C317186A31130041671A /* RuleStopState.cpp */; }; + 37D1C44C186A31140041671A /* RuleStopState.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C318186A31130041671A /* RuleStopState.h */; }; + 37D1C44D186A31140041671A /* RuleTransition.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C319186A31130041671A /* RuleTransition.cpp */; }; + 37D1C44E186A31140041671A /* RuleTransition.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C31A186A31130041671A /* RuleTransition.h */; }; + 37D1C44F186A31140041671A /* SemanticContext.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C31B186A31130041671A /* SemanticContext.cpp */; }; + 37D1C450186A31140041671A /* SemanticContext.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C31C186A31130041671A /* SemanticContext.h */; }; + 37D1C451186A31140041671A /* SetTransition.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C31D186A31130041671A /* SetTransition.cpp */; }; + 37D1C452186A31140041671A /* SetTransition.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C31E186A31130041671A /* SetTransition.h */; }; + 37D1C453186A31140041671A /* SingletonPredictionContext.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C31F186A31130041671A /* SingletonPredictionContext.cpp */; }; + 37D1C454186A31140041671A /* SingletonPredictionContext.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C320186A31130041671A /* SingletonPredictionContext.h */; }; + 37D1C455186A31140041671A /* StarBlockStartState.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C321186A31130041671A /* StarBlockStartState.cpp */; }; + 37D1C456186A31140041671A /* StarBlockStartState.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C322186A31130041671A /* StarBlockStartState.h */; }; + 37D1C457186A31140041671A /* StarLoopbackState.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C323186A31130041671A /* StarLoopbackState.cpp */; }; + 37D1C458186A31140041671A /* StarLoopbackState.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C324186A31130041671A /* StarLoopbackState.h */; }; + 37D1C459186A31140041671A /* StarLoopEntryState.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C325186A31130041671A /* StarLoopEntryState.cpp */; }; + 37D1C45A186A31140041671A /* StarLoopEntryState.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C326186A31130041671A /* StarLoopEntryState.h */; }; + 37D1C45B186A31140041671A /* TokensStartState.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C327186A31130041671A /* TokensStartState.cpp */; }; + 37D1C45C186A31140041671A /* TokensStartState.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C328186A31130041671A /* TokensStartState.h */; }; + 37D1C45D186A31140041671A /* Transition.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C329186A31130041671A /* Transition.cpp */; }; + 37D1C45E186A31140041671A /* Transition.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C32A186A31130041671A /* Transition.h */; }; + 37D1C45F186A31140041671A /* WildcardTransition.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C32B186A31130041671A /* WildcardTransition.cpp */; }; + 37D1C460186A31140041671A /* WildcardTransition.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C32C186A31130041671A /* WildcardTransition.h */; }; + 37D1C461186A31140041671A /* BailErrorStrategy.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C32D186A31130041671A /* BailErrorStrategy.cpp */; }; + 37D1C462186A31140041671A /* BailErrorStrategy.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C32E186A31130041671A /* BailErrorStrategy.h */; }; + 37D1C464186A31140041671A /* BaseErrorListener.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C330186A31130041671A /* BaseErrorListener.h */; }; + 37D1C465186A31140041671A /* BufferedTokenStream.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C331186A31130041671A /* BufferedTokenStream.cpp */; }; + 37D1C466186A31140041671A /* BufferedTokenStream.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C332186A31130041671A /* BufferedTokenStream.h */; }; + 37D1C467186A31140041671A /* CharStream.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C333186A31130041671A /* CharStream.cpp */; }; + 37D1C468186A31140041671A /* CharStream.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C334186A31130041671A /* CharStream.h */; }; + 37D1C469186A31140041671A /* CommonToken.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C335186A31130041671A /* CommonToken.cpp */; }; + 37D1C46A186A31140041671A /* CommonToken.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C336186A31130041671A /* CommonToken.h */; }; + 37D1C46B186A31140041671A /* CommonTokenFactory.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C337186A31130041671A /* CommonTokenFactory.cpp */; }; + 37D1C46C186A31140041671A /* CommonTokenFactory.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C338186A31130041671A /* CommonTokenFactory.h */; }; + 37D1C46D186A31140041671A /* CommonTokenStream.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C339186A31130041671A /* CommonTokenStream.cpp */; }; + 37D1C46E186A31140041671A /* CommonTokenStream.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C33A186A31130041671A /* CommonTokenStream.h */; }; + 37D1C46F186A31140041671A /* ConsoleErrorListener.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C33B186A31130041671A /* ConsoleErrorListener.cpp */; }; + 37D1C470186A31140041671A /* ConsoleErrorListener.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C33C186A31130041671A /* ConsoleErrorListener.h */; }; + 37D1C471186A31140041671A /* DefaultErrorStrategy.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C33D186A31130041671A /* DefaultErrorStrategy.cpp */; }; + 37D1C472186A31140041671A /* DefaultErrorStrategy.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C33E186A31130041671A /* DefaultErrorStrategy.h */; }; + 37D1C473186A31140041671A /* DFA.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C340186A31130041671A /* DFA.cpp */; }; + 37D1C474186A31140041671A /* DFA.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C341186A31130041671A /* DFA.h */; }; + 37D1C475186A31140041671A /* DFASerializer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C342186A31130041671A /* DFASerializer.cpp */; }; + 37D1C476186A31140041671A /* DFASerializer.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C343186A31130041671A /* DFASerializer.h */; }; + 37D1C477186A31140041671A /* DFAState.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C344186A31130041671A /* DFAState.cpp */; }; + 37D1C478186A31140041671A /* DFAState.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C345186A31130041671A /* DFAState.h */; }; + 37D1C479186A31140041671A /* LexerDFASerializer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C346186A31130041671A /* LexerDFASerializer.cpp */; }; + 37D1C47A186A31140041671A /* LexerDFASerializer.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C347186A31130041671A /* LexerDFASerializer.h */; }; + 37D1C47B186A31140041671A /* DiagnosticErrorListener.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C348186A31130041671A /* DiagnosticErrorListener.cpp */; }; + 37D1C47C186A31140041671A /* DiagnosticErrorListener.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C349186A31130041671A /* DiagnosticErrorListener.h */; }; + 37D1C47D186A31140041671A /* FailedPredicateException.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C34A186A31130041671A /* FailedPredicateException.cpp */; }; + 37D1C47E186A31140041671A /* FailedPredicateException.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C34B186A31130041671A /* FailedPredicateException.h */; }; + 37D1C47F186A31140041671A /* InputMismatchException.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C34C186A31130041671A /* InputMismatchException.cpp */; }; + 37D1C480186A31140041671A /* InputMismatchException.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C34D186A31130041671A /* InputMismatchException.h */; }; + 37D1C481186A31140041671A /* InterpreterRuleContext.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C34E186A31130041671A /* InterpreterRuleContext.cpp */; }; + 37D1C482186A31140041671A /* InterpreterRuleContext.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C34F186A31130041671A /* InterpreterRuleContext.h */; }; + 37D1C483186A31140041671A /* IntStream.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C350186A31130041671A /* IntStream.cpp */; }; + 37D1C484186A31140041671A /* IntStream.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C351186A31130041671A /* IntStream.h */; }; + 37D1C485186A31140041671A /* Lexer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C352186A31130041671A /* Lexer.cpp */; }; + 37D1C486186A31140041671A /* Lexer.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C353186A31130041671A /* Lexer.h */; }; + 37D1C487186A31140041671A /* LexerInterpreter.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C354186A31130041671A /* LexerInterpreter.cpp */; }; + 37D1C488186A31140041671A /* LexerInterpreter.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C355186A31130041671A /* LexerInterpreter.h */; }; + 37D1C489186A31140041671A /* LexerNoViableAltException.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C356186A31130041671A /* LexerNoViableAltException.cpp */; }; + 37D1C48A186A31140041671A /* LexerNoViableAltException.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C357186A31130041671A /* LexerNoViableAltException.h */; }; + 37D1C48B186A31140041671A /* ListTokenSource.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C358186A31130041671A /* ListTokenSource.cpp */; }; + 37D1C48C186A31140041671A /* ListTokenSource.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C359186A31130041671A /* ListTokenSource.h */; }; + 37D1C48D186A31140041671A /* AbstractEqualityComparator.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C35B186A31130041671A /* AbstractEqualityComparator.cpp */; }; + 37D1C48E186A31140041671A /* AbstractEqualityComparator.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C35C186A31130041671A /* AbstractEqualityComparator.h */; }; + 37D1C490186A31140041671A /* Array2DHashSet.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C35E186A31130041671A /* Array2DHashSet.h */; }; + 37D1C492186A31140041671A /* DoubleKeyMap.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C360186A31130041671A /* DoubleKeyMap.h */; }; + 37D1C493186A31140041671A /* EqualityComparator.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C361186A31130041671A /* EqualityComparator.cpp */; }; + 37D1C494186A31140041671A /* EqualityComparator.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C362186A31130041671A /* EqualityComparator.h */; }; + 37D1C497186A31140041671A /* GraphicsSupport.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C365186A31130041671A /* GraphicsSupport.cpp */; }; + 37D1C498186A31140041671A /* GraphicsSupport.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C366186A31130041671A /* GraphicsSupport.h */; }; + 37D1C49D186A31140041671A /* Interval.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C36B186A31130041671A /* Interval.cpp */; }; + 37D1C49E186A31140041671A /* Interval.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C36C186A31130041671A /* Interval.h */; }; + 37D1C49F186A31140041671A /* IntervalSet.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C36D186A31130041671A /* IntervalSet.cpp */; }; + 37D1C4A0186A31140041671A /* IntervalSet.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C36E186A31130041671A /* IntervalSet.h */; }; + 37D1C4A1186A31140041671A /* IntSet.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C36F186A31130041671A /* IntSet.cpp */; }; + 37D1C4A2186A31140041671A /* IntSet.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C370186A31130041671A /* IntSet.h */; }; + 37D1C4A5186A31140041671A /* LogManager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C373186A31130041671A /* LogManager.cpp */; }; + 37D1C4A6186A31140041671A /* LogManager.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C374186A31130041671A /* LogManager.h */; }; + 37D1C4A7186A31140041671A /* MultiMap.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C375186A31130041671A /* MultiMap.cpp */; }; + 37D1C4A8186A31140041671A /* MultiMap.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C376186A31130041671A /* MultiMap.h */; }; + 37D1C4A9186A31140041671A /* MurmurHash.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C377186A31130041671A /* MurmurHash.cpp */; }; + 37D1C4AA186A31140041671A /* MurmurHash.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C378186A31130041671A /* MurmurHash.h */; }; + 37D1C4AB186A31140041671A /* NotNull.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C379186A31130041671A /* NotNull.cpp */; }; + 37D1C4AC186A31140041671A /* NotNull.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C37A186A31130041671A /* NotNull.h */; }; + 37D1C4AF186A31140041671A /* ObjectEqualityComparator.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C37D186A31130041671A /* ObjectEqualityComparator.cpp */; }; + 37D1C4B0186A31140041671A /* ObjectEqualityComparator.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C37E186A31130041671A /* ObjectEqualityComparator.h */; }; + 37D1C4B1186A31140041671A /* OrderedHashSet.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C37F186A31130041671A /* OrderedHashSet.cpp */; }; + 37D1C4B2186A31140041671A /* OrderedHashSet.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C380186A31130041671A /* OrderedHashSet.h */; }; + 37D1C4B5186A31140041671A /* ParseCancellationException.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C383186A31130041671A /* ParseCancellationException.cpp */; }; + 37D1C4B6186A31140041671A /* ParseCancellationException.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C384186A31130041671A /* ParseCancellationException.h */; }; + 37D1C4B7186A31140041671A /* TestRig.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C385186A31130041671A /* TestRig.cpp */; }; + 37D1C4B8186A31140041671A /* TestRig.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C386186A31130041671A /* TestRig.h */; }; + 37D1C4BB186A31140041671A /* Utils.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C389186A31130041671A /* Utils.cpp */; }; + 37D1C4BC186A31140041671A /* Utils.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C38A186A31130041671A /* Utils.h */; }; + 37D1C4BD186A31140041671A /* NoViableAltException.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C38B186A31130041671A /* NoViableAltException.cpp */; }; + 37D1C4BE186A31140041671A /* NoViableAltException.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C38C186A31130041671A /* NoViableAltException.h */; }; + 37D1C4BF186A31140041671A /* Parser.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C38D186A31130041671A /* Parser.cpp */; }; + 37D1C4C0186A31140041671A /* Parser.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C38E186A31130041671A /* Parser.h */; }; + 37D1C4C1186A31140041671A /* ParserInterpreter.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C38F186A31140041671A /* ParserInterpreter.cpp */; }; + 37D1C4C2186A31140041671A /* ParserInterpreter.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C390186A31140041671A /* ParserInterpreter.h */; }; + 37D1C4C3186A31140041671A /* ParserRuleContext.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C391186A31140041671A /* ParserRuleContext.cpp */; }; + 37D1C4C4186A31140041671A /* ParserRuleContext.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C392186A31140041671A /* ParserRuleContext.h */; }; + 37D1C4C5186A31140041671A /* ProxyErrorListener.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C393186A31140041671A /* ProxyErrorListener.cpp */; }; + 37D1C4C6186A31140041671A /* ProxyErrorListener.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C394186A31140041671A /* ProxyErrorListener.h */; }; + 37D1C4C7186A31140041671A /* RecognitionException.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C395186A31140041671A /* RecognitionException.cpp */; }; + 37D1C4C8186A31140041671A /* RecognitionException.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C396186A31140041671A /* RecognitionException.h */; }; + 37D1C4CA186A31140041671A /* Recognizer.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C398186A31140041671A /* Recognizer.h */; }; + 37D1C4CB186A31140041671A /* RuleContext.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C399186A31140041671A /* RuleContext.cpp */; }; + 37D1C4CC186A31140041671A /* RuleContext.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C39A186A31140041671A /* RuleContext.h */; }; + 37D1C4CD186A31140041671A /* Token.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C39B186A31140041671A /* Token.cpp */; }; + 37D1C4CE186A31140041671A /* Token.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C39C186A31140041671A /* Token.h */; }; + 37D1C4CF186A31140041671A /* TokenFactory.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C39D186A31140041671A /* TokenFactory.cpp */; }; + 37D1C4D0186A31140041671A /* TokenFactory.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C39E186A31140041671A /* TokenFactory.h */; }; + 37D1C4D1186A31140041671A /* TokenSource.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C39F186A31140041671A /* TokenSource.cpp */; }; + 37D1C4D2186A31140041671A /* TokenSource.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C3A0186A31140041671A /* TokenSource.h */; }; + 37D1C4D3186A31140041671A /* TokenStream.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C3A1186A31140041671A /* TokenStream.cpp */; }; + 37D1C4D4186A31140041671A /* TokenStream.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C3A2186A31140041671A /* TokenStream.h */; }; + 37D1C4D5186A31140041671A /* TokenStreamRewriter.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C3A3186A31140041671A /* TokenStreamRewriter.cpp */; }; + 37D1C4D6186A31140041671A /* TokenStreamRewriter.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C3A4186A31140041671A /* TokenStreamRewriter.h */; }; + 37D1C4D7186A31140041671A /* AbstractParseTreeVisitor.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C3A6186A31140041671A /* AbstractParseTreeVisitor.cpp */; }; + 37D1C4D8186A31140041671A /* AbstractParseTreeVisitor.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C3A7186A31140041671A /* AbstractParseTreeVisitor.h */; }; + 37D1C4D9186A31140041671A /* ErrorNode.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C3A8186A31140041671A /* ErrorNode.cpp */; }; + 37D1C4DA186A31140041671A /* ErrorNode.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C3A9186A31140041671A /* ErrorNode.h */; }; + 37D1C4DB186A31140041671A /* ErrorNodeImpl.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C3AA186A31140041671A /* ErrorNodeImpl.cpp */; }; + 37D1C4DC186A31140041671A /* ErrorNodeImpl.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C3AB186A31140041671A /* ErrorNodeImpl.h */; }; + 37D1C4EB186A31140041671A /* ParseTree.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C3BB186A31140041671A /* ParseTree.cpp */; }; + 37D1C4EC186A31140041671A /* ParseTree.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C3BC186A31140041671A /* ParseTree.h */; }; + 37D1C4ED186A31140041671A /* ParseTreeListener.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C3BD186A31140041671A /* ParseTreeListener.cpp */; }; + 37D1C4EE186A31140041671A /* ParseTreeListener.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C3BE186A31140041671A /* ParseTreeListener.h */; }; + 37D1C4EF186A31140041671A /* ParseTreeProperty.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C3BF186A31140041671A /* ParseTreeProperty.cpp */; }; + 37D1C4F0186A31140041671A /* ParseTreeProperty.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C3C0186A31140041671A /* ParseTreeProperty.h */; }; + 37D1C4F1186A31140041671A /* ParseTreeVisitor.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C3C1186A31140041671A /* ParseTreeVisitor.cpp */; }; + 37D1C4F2186A31140041671A /* ParseTreeVisitor.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C3C2186A31140041671A /* ParseTreeVisitor.h */; }; + 37D1C4F3186A31140041671A /* ParseTreeWalker.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C3C3186A31140041671A /* ParseTreeWalker.cpp */; }; + 37D1C4F4186A31140041671A /* ParseTreeWalker.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C3C4186A31140041671A /* ParseTreeWalker.h */; }; + 37D1C4F5186A31140041671A /* Chunk.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C3C6186A31140041671A /* Chunk.cpp */; }; + 37D1C4F6186A31140041671A /* Chunk.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C3C7186A31140041671A /* Chunk.h */; }; + 37D1C4F7186A31140041671A /* ParseTreeMatch.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C3C8186A31140041671A /* ParseTreeMatch.cpp */; }; + 37D1C4F8186A31140041671A /* ParseTreeMatch.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C3C9186A31140041671A /* ParseTreeMatch.h */; }; + 37D1C4F9186A31140041671A /* ParseTreePattern.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C3CA186A31140041671A /* ParseTreePattern.cpp */; }; + 37D1C4FA186A31140041671A /* ParseTreePattern.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C3CB186A31140041671A /* ParseTreePattern.h */; }; + 37D1C4FB186A31140041671A /* ParseTreePatternMatcher.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C3CC186A31140041671A /* ParseTreePatternMatcher.cpp */; }; + 37D1C4FC186A31140041671A /* ParseTreePatternMatcher.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C3CD186A31140041671A /* ParseTreePatternMatcher.h */; }; + 37D1C4FD186A31140041671A /* RuleTagToken.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C3CE186A31140041671A /* RuleTagToken.cpp */; }; + 37D1C4FE186A31140041671A /* RuleTagToken.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C3CF186A31140041671A /* RuleTagToken.h */; }; + 37D1C4FF186A31140041671A /* TagChunk.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C3D0186A31140041671A /* TagChunk.cpp */; }; + 37D1C500186A31140041671A /* TagChunk.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C3D1186A31140041671A /* TagChunk.h */; }; + 37D1C501186A31140041671A /* TextChunk.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C3D2186A31140041671A /* TextChunk.cpp */; }; + 37D1C502186A31140041671A /* TextChunk.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C3D3186A31140041671A /* TextChunk.h */; }; + 37D1C503186A31140041671A /* TokenTagToken.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C3D4186A31140041671A /* TokenTagToken.cpp */; }; + 37D1C504186A31140041671A /* TokenTagToken.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C3D5186A31140041671A /* TokenTagToken.h */; }; + 37D1C505186A31140041671A /* RuleNode.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C3D6186A31140041671A /* RuleNode.cpp */; }; + 37D1C506186A31140041671A /* RuleNode.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C3D7186A31140041671A /* RuleNode.h */; }; + 37D1C507186A31140041671A /* SyntaxTree.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C3D8186A31140041671A /* SyntaxTree.cpp */; }; + 37D1C508186A31140041671A /* SyntaxTree.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C3D9186A31140041671A /* SyntaxTree.h */; }; + 37D1C509186A31140041671A /* TerminalNode.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C3DA186A31140041671A /* TerminalNode.cpp */; }; + 37D1C50A186A31140041671A /* TerminalNode.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C3DB186A31140041671A /* TerminalNode.h */; }; + 37D1C50B186A31140041671A /* TerminalNodeImpl.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C3DC186A31140041671A /* TerminalNodeImpl.cpp */; }; + 37D1C50C186A31140041671A /* TerminalNodeImpl.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C3DD186A31140041671A /* TerminalNodeImpl.h */; }; + 37D1C50D186A31140041671A /* Tree.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C3DE186A31140041671A /* Tree.cpp */; }; + 37D1C50E186A31140041671A /* Tree.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C3DF186A31140041671A /* Tree.h */; }; + 37D1C50F186A31140041671A /* Trees.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C3E0186A31140041671A /* Trees.cpp */; }; + 37D1C510186A31140041671A /* Trees.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C3E1186A31140041671A /* Trees.h */; }; + 37D1C511186A31140041671A /* XPath.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C3E3186A31140041671A /* XPath.cpp */; }; + 37D1C512186A31140041671A /* XPath.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C3E4186A31140041671A /* XPath.h */; }; + 37D1C513186A31140041671A /* XPathElement.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C3E5186A31140041671A /* XPathElement.cpp */; }; + 37D1C514186A31140041671A /* XPathElement.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C3E6186A31140041671A /* XPathElement.h */; }; + 37D1C515186A31140041671A /* XPathLexerErrorListener.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C3E7186A31140041671A /* XPathLexerErrorListener.cpp */; }; + 37D1C516186A31140041671A /* XPathLexerErrorListener.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C3E8186A31140041671A /* XPathLexerErrorListener.h */; }; + 37D1C517186A31140041671A /* XPathRuleAnywhereElement.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C3E9186A31140041671A /* XPathRuleAnywhereElement.cpp */; }; + 37D1C518186A31140041671A /* XPathRuleAnywhereElement.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C3EA186A31140041671A /* XPathRuleAnywhereElement.h */; }; + 37D1C519186A31140041671A /* XPathRuleElement.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C3EB186A31140041671A /* XPathRuleElement.cpp */; }; + 37D1C51A186A31140041671A /* XPathRuleElement.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C3EC186A31140041671A /* XPathRuleElement.h */; }; + 37D1C51B186A31140041671A /* XPathTokenAnywhereElement.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C3ED186A31140041671A /* XPathTokenAnywhereElement.cpp */; }; + 37D1C51C186A31140041671A /* XPathTokenAnywhereElement.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C3EE186A31140041671A /* XPathTokenAnywhereElement.h */; }; + 37D1C51D186A31140041671A /* XPathTokenElement.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C3EF186A31140041671A /* XPathTokenElement.cpp */; }; + 37D1C51E186A31140041671A /* XPathTokenElement.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C3F0186A31140041671A /* XPathTokenElement.h */; }; + 37D1C51F186A31140041671A /* XPathWildcardAnywhereElement.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C3F1186A31140041671A /* XPathWildcardAnywhereElement.cpp */; }; + 37D1C520186A31140041671A /* XPathWildcardAnywhereElement.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C3F2186A31140041671A /* XPathWildcardAnywhereElement.h */; }; + 37D1C521186A31140041671A /* XPathWildcardElement.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C3F3186A31140041671A /* XPathWildcardElement.cpp */; }; + 37D1C522186A31140041671A /* XPathWildcardElement.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C3F4186A31140041671A /* XPathWildcardElement.h */; }; + 37D1C523186A31140041671A /* UnbufferedCharStream.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C3F5186A31140041671A /* UnbufferedCharStream.cpp */; }; + 37D1C524186A31140041671A /* UnbufferedCharStream.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C3F6186A31140041671A /* UnbufferedCharStream.h */; }; + 37D1C526186A31140041671A /* UnbufferedTokenStream.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C3F8186A31140041671A /* UnbufferedTokenStream.h */; }; + 37D1C527186A31140041671A /* WritableToken.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D1C3F9186A31140041671A /* WritableToken.cpp */; }; + 37D1C528186A31140041671A /* WritableToken.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C3FA186A31140041671A /* WritableToken.h */; }; + 37D1C52B186A4B5B0041671A /* stringconverter.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C529186A4B5B0041671A /* stringconverter.h */; }; + 37D1C52C186A4B5B0041671A /* vectorhelper.h in Headers */ = {isa = PBXBuildFile; fileRef = 37D1C52A186A4B5B0041671A /* vectorhelper.h */; }; + 37D727B41867AF1E007B6D10 /* antlrcpp.cp in Sources */ = {isa = PBXBuildFile; fileRef = 37D727B31867AF1E007B6D10 /* antlrcpp.cp */; }; + 37DEDC1E1B30E71800A6FC5F /* BaseErrorListener.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37DEDC1D1B30E71800A6FC5F /* BaseErrorListener.cpp */; }; + 37DEDC201B384AE500A6FC5F /* UUID.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37DEDC1F1B384AE500A6FC5F /* UUID.cpp */; }; + 37F1355F1B49631900E0CACF /* IErrorListener.h in Headers */ = {isa = PBXBuildFile; fileRef = 37F1355E1B49631900E0CACF /* IErrorListener.h */; }; + 37F135631B49644000E0CACF /* IRecognizer.h in Headers */ = {isa = PBXBuildFile; fileRef = 37F135621B49644000E0CACF /* IRecognizer.h */; }; + 37F1356D1B4AC02800E0CACF /* antlrcpp_Tests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 37F1356C1B4AC02800E0CACF /* antlrcpp_Tests.mm */; }; + 37F1356E1B4AC02800E0CACF /* antlrcpp.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 37D727AA1867AF1E007B6D10 /* antlrcpp.dylib */; }; +/* End PBXBuildFile section */ + +/* Begin PBXContainerItemProxy section */ + 37F1356F1B4AC02800E0CACF /* PBXContainerItemProxy */ = { + isa = PBXContainerItemProxy; + containerPortal = 37D727A21867AF1E007B6D10 /* Project object */; + proxyType = 1; + remoteGlobalIDString = 37D727A91867AF1E007B6D10; + remoteInfo = antlrcpp; + }; +/* End PBXContainerItemProxy section */ + +/* Begin PBXFileReference section */ + 16F9451B1B23CB250049275C /* BitSet.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BitSet.h; sourceTree = ""; }; + 373775A1192FE6F400A4058D /* UUID.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UUID.h; sourceTree = ""; }; + 37725BA31B39B09000BDA484 /* CPPUtils.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CPPUtils.cpp; sourceTree = ""; }; + 37725BA41B39B09000BDA484 /* CPPUtils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CPPUtils.h; sourceTree = ""; }; + 3789F4FE1870D1D8003E0A30 /* StringBuilder.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = StringBuilder.cpp; sourceTree = ""; }; + 3789F4FF1870D1D8003E0A30 /* StringBuilder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StringBuilder.h; sourceTree = ""; }; + 3789F5021870E074003E0A30 /* Arrays.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Arrays.cpp; sourceTree = ""; }; + 3789F5031870E074003E0A30 /* Arrays.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Arrays.h; sourceTree = ""; }; + 3789F5061871AFD8003E0A30 /* Declarations.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Declarations.h; sourceTree = ""; }; + 3789F5071872F5C2003E0A30 /* XPathLexer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = XPathLexer.cpp; sourceTree = ""; }; + 3789F5081872F5C2003E0A30 /* XPathLexer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XPathLexer.h; sourceTree = ""; }; + 37BED8C2187304740043FB28 /* Exceptions.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Exceptions.cpp; sourceTree = ""; }; + 37BED8C3187304740043FB28 /* Exceptions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Exceptions.h; sourceTree = ""; }; + 37C0684A191E636C00B4D312 /* Strings.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Strings.h; sourceTree = ""; }; + 37C0684C1920EF8300B4D312 /* Strings.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Strings.cpp; sourceTree = ""; }; + 37C147171B4D5A04008EDDDB /* libantlrcpp_static.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libantlrcpp_static.a; sourceTree = BUILT_PRODUCTS_DIR; }; + 37D1C2C6186A31130041671A /* ANTLRErrorListener.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ANTLRErrorListener.cpp; sourceTree = ""; }; + 37D1C2C7186A31130041671A /* ANTLRErrorListener.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ANTLRErrorListener.h; sourceTree = ""; }; + 37D1C2C8186A31130041671A /* ANTLRErrorStrategy.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ANTLRErrorStrategy.cpp; sourceTree = ""; }; + 37D1C2C9186A31130041671A /* ANTLRErrorStrategy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ANTLRErrorStrategy.h; sourceTree = ""; }; + 37D1C2CA186A31130041671A /* ANTLRFileStream.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ANTLRFileStream.cpp; sourceTree = ""; }; + 37D1C2CB186A31130041671A /* ANTLRFileStream.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ANTLRFileStream.h; sourceTree = ""; }; + 37D1C2CC186A31130041671A /* ANTLRInputStream.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ANTLRInputStream.cpp; sourceTree = ""; }; + 37D1C2CD186A31130041671A /* ANTLRInputStream.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ANTLRInputStream.h; sourceTree = ""; }; + 37D1C2CF186A31130041671A /* AbstractPredicateTransition.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = AbstractPredicateTransition.cpp; sourceTree = ""; }; + 37D1C2D0186A31130041671A /* AbstractPredicateTransition.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AbstractPredicateTransition.h; sourceTree = ""; }; + 37D1C2D1186A31130041671A /* ActionTransition.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ActionTransition.cpp; sourceTree = ""; }; + 37D1C2D2186A31130041671A /* ActionTransition.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ActionTransition.h; sourceTree = ""; }; + 37D1C2D3186A31130041671A /* ArrayPredictionContext.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ArrayPredictionContext.cpp; sourceTree = ""; }; + 37D1C2D4186A31130041671A /* ArrayPredictionContext.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ArrayPredictionContext.h; sourceTree = ""; }; + 37D1C2D5186A31130041671A /* ATN.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ATN.cpp; sourceTree = ""; }; + 37D1C2D6186A31130041671A /* ATN.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ATN.h; sourceTree = ""; }; + 37D1C2D7186A31130041671A /* ATNConfig.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ATNConfig.cpp; sourceTree = ""; }; + 37D1C2D8186A31130041671A /* ATNConfig.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ATNConfig.h; sourceTree = ""; }; + 37D1C2D9186A31130041671A /* ATNConfigSet.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ATNConfigSet.cpp; sourceTree = ""; }; + 37D1C2DA186A31130041671A /* ATNConfigSet.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ATNConfigSet.h; sourceTree = ""; }; + 37D1C2DB186A31130041671A /* ATNDeserializationOptions.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ATNDeserializationOptions.cpp; sourceTree = ""; }; + 37D1C2DC186A31130041671A /* ATNDeserializationOptions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ATNDeserializationOptions.h; sourceTree = ""; }; + 37D1C2DD186A31130041671A /* ATNDeserializer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ATNDeserializer.cpp; sourceTree = ""; }; + 37D1C2DE186A31130041671A /* ATNDeserializer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ATNDeserializer.h; sourceTree = ""; }; + 37D1C2DF186A31130041671A /* ATNSerializer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ATNSerializer.cpp; sourceTree = ""; }; + 37D1C2E0186A31130041671A /* ATNSerializer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ATNSerializer.h; sourceTree = ""; }; + 37D1C2E1186A31130041671A /* ATNSimulator.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ATNSimulator.cpp; sourceTree = ""; }; + 37D1C2E2186A31130041671A /* ATNSimulator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ATNSimulator.h; sourceTree = ""; }; + 37D1C2E3186A31130041671A /* ATNState.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ATNState.cpp; sourceTree = ""; }; + 37D1C2E4186A31130041671A /* ATNState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ATNState.h; sourceTree = ""; }; + 37D1C2E5186A31130041671A /* ATNType.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ATNType.cpp; sourceTree = ""; }; + 37D1C2E6186A31130041671A /* ATNType.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ATNType.h; sourceTree = ""; }; + 37D1C2E7186A31130041671A /* AtomTransition.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = AtomTransition.cpp; sourceTree = ""; }; + 37D1C2E8186A31130041671A /* AtomTransition.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AtomTransition.h; sourceTree = ""; }; + 37D1C2E9186A31130041671A /* BasicBlockStartState.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = BasicBlockStartState.cpp; sourceTree = ""; }; + 37D1C2EA186A31130041671A /* BasicBlockStartState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BasicBlockStartState.h; sourceTree = ""; }; + 37D1C2EB186A31130041671A /* BasicState.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = BasicState.cpp; sourceTree = ""; }; + 37D1C2EC186A31130041671A /* BasicState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BasicState.h; sourceTree = ""; }; + 37D1C2ED186A31130041671A /* BlockEndState.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = BlockEndState.cpp; sourceTree = ""; }; + 37D1C2EE186A31130041671A /* BlockEndState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BlockEndState.h; sourceTree = ""; }; + 37D1C2EF186A31130041671A /* BlockStartState.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = BlockStartState.cpp; sourceTree = ""; }; + 37D1C2F0186A31130041671A /* BlockStartState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BlockStartState.h; sourceTree = ""; }; + 37D1C2F1186A31130041671A /* DecisionState.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = DecisionState.cpp; sourceTree = ""; }; + 37D1C2F2186A31130041671A /* DecisionState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DecisionState.h; sourceTree = ""; }; + 37D1C2F3186A31130041671A /* EmptyPredictionContext.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = EmptyPredictionContext.cpp; sourceTree = ""; }; + 37D1C2F4186A31130041671A /* EmptyPredictionContext.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EmptyPredictionContext.h; sourceTree = ""; }; + 37D1C2F5186A31130041671A /* EpsilonTransition.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = EpsilonTransition.cpp; sourceTree = ""; }; + 37D1C2F6186A31130041671A /* EpsilonTransition.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EpsilonTransition.h; sourceTree = ""; }; + 37D1C2F7186A31130041671A /* LexerATNConfig.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = LexerATNConfig.cpp; sourceTree = ""; }; + 37D1C2F8186A31130041671A /* LexerATNConfig.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LexerATNConfig.h; sourceTree = ""; }; + 37D1C2F9186A31130041671A /* LexerATNSimulator.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = LexerATNSimulator.cpp; sourceTree = ""; }; + 37D1C2FA186A31130041671A /* LexerATNSimulator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LexerATNSimulator.h; sourceTree = ""; }; + 37D1C2FB186A31130041671A /* LL1Analyzer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = LL1Analyzer.cpp; sourceTree = ""; }; + 37D1C2FC186A31130041671A /* LL1Analyzer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LL1Analyzer.h; sourceTree = ""; }; + 37D1C2FD186A31130041671A /* LoopEndState.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = LoopEndState.cpp; sourceTree = ""; }; + 37D1C2FE186A31130041671A /* LoopEndState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LoopEndState.h; sourceTree = ""; }; + 37D1C2FF186A31130041671A /* NotSetTransition.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = NotSetTransition.cpp; sourceTree = ""; }; + 37D1C300186A31130041671A /* NotSetTransition.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NotSetTransition.h; sourceTree = ""; }; + 37D1C301186A31130041671A /* OrderedATNConfigSet.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = OrderedATNConfigSet.cpp; sourceTree = ""; }; + 37D1C302186A31130041671A /* OrderedATNConfigSet.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OrderedATNConfigSet.h; sourceTree = ""; }; + 37D1C303186A31130041671A /* ParserATNSimulator.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ParserATNSimulator.cpp; sourceTree = ""; }; + 37D1C304186A31130041671A /* ParserATNSimulator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ParserATNSimulator.h; sourceTree = ""; }; + 37D1C305186A31130041671A /* PlusBlockStartState.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = PlusBlockStartState.cpp; sourceTree = ""; }; + 37D1C306186A31130041671A /* PlusBlockStartState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PlusBlockStartState.h; sourceTree = ""; }; + 37D1C307186A31130041671A /* PlusLoopbackState.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = PlusLoopbackState.cpp; sourceTree = ""; }; + 37D1C308186A31130041671A /* PlusLoopbackState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PlusLoopbackState.h; sourceTree = ""; }; + 37D1C309186A31130041671A /* PrecedencePredicateTransition.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = PrecedencePredicateTransition.cpp; sourceTree = ""; }; + 37D1C30A186A31130041671A /* PrecedencePredicateTransition.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PrecedencePredicateTransition.h; sourceTree = ""; }; + 37D1C30B186A31130041671A /* PredicateTransition.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = PredicateTransition.cpp; sourceTree = ""; }; + 37D1C30C186A31130041671A /* PredicateTransition.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PredicateTransition.h; sourceTree = ""; }; + 37D1C30D186A31130041671A /* PredictionContext.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = PredictionContext.cpp; sourceTree = ""; }; + 37D1C30E186A31130041671A /* PredictionContext.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PredictionContext.h; sourceTree = ""; }; + 37D1C30F186A31130041671A /* PredictionContextCache.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = PredictionContextCache.cpp; sourceTree = ""; }; + 37D1C310186A31130041671A /* PredictionContextCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PredictionContextCache.h; sourceTree = ""; }; + 37D1C311186A31130041671A /* PredictionMode.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = PredictionMode.cpp; sourceTree = ""; }; + 37D1C312186A31130041671A /* PredictionMode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PredictionMode.h; sourceTree = ""; }; + 37D1C313186A31130041671A /* RangeTransition.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RangeTransition.cpp; sourceTree = ""; }; + 37D1C314186A31130041671A /* RangeTransition.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RangeTransition.h; sourceTree = ""; }; + 37D1C315186A31130041671A /* RuleStartState.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RuleStartState.cpp; sourceTree = ""; }; + 37D1C316186A31130041671A /* RuleStartState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RuleStartState.h; sourceTree = ""; }; + 37D1C317186A31130041671A /* RuleStopState.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RuleStopState.cpp; sourceTree = ""; }; + 37D1C318186A31130041671A /* RuleStopState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RuleStopState.h; sourceTree = ""; }; + 37D1C319186A31130041671A /* RuleTransition.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RuleTransition.cpp; sourceTree = ""; }; + 37D1C31A186A31130041671A /* RuleTransition.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RuleTransition.h; sourceTree = ""; }; + 37D1C31B186A31130041671A /* SemanticContext.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SemanticContext.cpp; sourceTree = ""; }; + 37D1C31C186A31130041671A /* SemanticContext.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SemanticContext.h; sourceTree = ""; }; + 37D1C31D186A31130041671A /* SetTransition.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SetTransition.cpp; sourceTree = ""; }; + 37D1C31E186A31130041671A /* SetTransition.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SetTransition.h; sourceTree = ""; }; + 37D1C31F186A31130041671A /* SingletonPredictionContext.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SingletonPredictionContext.cpp; sourceTree = ""; }; + 37D1C320186A31130041671A /* SingletonPredictionContext.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SingletonPredictionContext.h; sourceTree = ""; }; + 37D1C321186A31130041671A /* StarBlockStartState.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = StarBlockStartState.cpp; sourceTree = ""; }; + 37D1C322186A31130041671A /* StarBlockStartState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StarBlockStartState.h; sourceTree = ""; }; + 37D1C323186A31130041671A /* StarLoopbackState.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = StarLoopbackState.cpp; sourceTree = ""; }; + 37D1C324186A31130041671A /* StarLoopbackState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StarLoopbackState.h; sourceTree = ""; }; + 37D1C325186A31130041671A /* StarLoopEntryState.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = StarLoopEntryState.cpp; sourceTree = ""; }; + 37D1C326186A31130041671A /* StarLoopEntryState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StarLoopEntryState.h; sourceTree = ""; }; + 37D1C327186A31130041671A /* TokensStartState.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = TokensStartState.cpp; sourceTree = ""; }; + 37D1C328186A31130041671A /* TokensStartState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TokensStartState.h; sourceTree = ""; }; + 37D1C329186A31130041671A /* Transition.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Transition.cpp; sourceTree = ""; }; + 37D1C32A186A31130041671A /* Transition.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Transition.h; sourceTree = ""; }; + 37D1C32B186A31130041671A /* WildcardTransition.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WildcardTransition.cpp; sourceTree = ""; }; + 37D1C32C186A31130041671A /* WildcardTransition.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WildcardTransition.h; sourceTree = ""; }; + 37D1C32D186A31130041671A /* BailErrorStrategy.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = BailErrorStrategy.cpp; sourceTree = ""; }; + 37D1C32E186A31130041671A /* BailErrorStrategy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BailErrorStrategy.h; sourceTree = ""; }; + 37D1C330186A31130041671A /* BaseErrorListener.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BaseErrorListener.h; sourceTree = ""; }; + 37D1C331186A31130041671A /* BufferedTokenStream.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = BufferedTokenStream.cpp; sourceTree = ""; }; + 37D1C332186A31130041671A /* BufferedTokenStream.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BufferedTokenStream.h; sourceTree = ""; }; + 37D1C333186A31130041671A /* CharStream.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CharStream.cpp; sourceTree = ""; }; + 37D1C334186A31130041671A /* CharStream.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CharStream.h; sourceTree = ""; }; + 37D1C335186A31130041671A /* CommonToken.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CommonToken.cpp; sourceTree = ""; }; + 37D1C336186A31130041671A /* CommonToken.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CommonToken.h; sourceTree = ""; }; + 37D1C337186A31130041671A /* CommonTokenFactory.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CommonTokenFactory.cpp; sourceTree = ""; }; + 37D1C338186A31130041671A /* CommonTokenFactory.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CommonTokenFactory.h; sourceTree = ""; }; + 37D1C339186A31130041671A /* CommonTokenStream.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CommonTokenStream.cpp; sourceTree = ""; }; + 37D1C33A186A31130041671A /* CommonTokenStream.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CommonTokenStream.h; sourceTree = ""; }; + 37D1C33B186A31130041671A /* ConsoleErrorListener.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ConsoleErrorListener.cpp; sourceTree = ""; }; + 37D1C33C186A31130041671A /* ConsoleErrorListener.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ConsoleErrorListener.h; sourceTree = ""; }; + 37D1C33D186A31130041671A /* DefaultErrorStrategy.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = DefaultErrorStrategy.cpp; sourceTree = ""; }; + 37D1C33E186A31130041671A /* DefaultErrorStrategy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DefaultErrorStrategy.h; sourceTree = ""; }; + 37D1C340186A31130041671A /* DFA.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = DFA.cpp; sourceTree = ""; }; + 37D1C341186A31130041671A /* DFA.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DFA.h; sourceTree = ""; }; + 37D1C342186A31130041671A /* DFASerializer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = DFASerializer.cpp; sourceTree = ""; }; + 37D1C343186A31130041671A /* DFASerializer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DFASerializer.h; sourceTree = ""; }; + 37D1C344186A31130041671A /* DFAState.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = DFAState.cpp; sourceTree = ""; }; + 37D1C345186A31130041671A /* DFAState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DFAState.h; sourceTree = ""; }; + 37D1C346186A31130041671A /* LexerDFASerializer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = LexerDFASerializer.cpp; sourceTree = ""; }; + 37D1C347186A31130041671A /* LexerDFASerializer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LexerDFASerializer.h; sourceTree = ""; }; + 37D1C348186A31130041671A /* DiagnosticErrorListener.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = DiagnosticErrorListener.cpp; sourceTree = ""; }; + 37D1C349186A31130041671A /* DiagnosticErrorListener.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DiagnosticErrorListener.h; sourceTree = ""; }; + 37D1C34A186A31130041671A /* FailedPredicateException.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = FailedPredicateException.cpp; sourceTree = ""; }; + 37D1C34B186A31130041671A /* FailedPredicateException.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FailedPredicateException.h; sourceTree = ""; }; + 37D1C34C186A31130041671A /* InputMismatchException.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = InputMismatchException.cpp; sourceTree = ""; }; + 37D1C34D186A31130041671A /* InputMismatchException.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = InputMismatchException.h; sourceTree = ""; }; + 37D1C34E186A31130041671A /* InterpreterRuleContext.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = InterpreterRuleContext.cpp; sourceTree = ""; }; + 37D1C34F186A31130041671A /* InterpreterRuleContext.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = InterpreterRuleContext.h; sourceTree = ""; }; + 37D1C350186A31130041671A /* IntStream.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = IntStream.cpp; sourceTree = ""; }; + 37D1C351186A31130041671A /* IntStream.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IntStream.h; sourceTree = ""; }; + 37D1C352186A31130041671A /* Lexer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Lexer.cpp; sourceTree = ""; }; + 37D1C353186A31130041671A /* Lexer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Lexer.h; sourceTree = ""; }; + 37D1C354186A31130041671A /* LexerInterpreter.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = LexerInterpreter.cpp; sourceTree = ""; }; + 37D1C355186A31130041671A /* LexerInterpreter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LexerInterpreter.h; sourceTree = ""; }; + 37D1C356186A31130041671A /* LexerNoViableAltException.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = LexerNoViableAltException.cpp; sourceTree = ""; }; + 37D1C357186A31130041671A /* LexerNoViableAltException.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LexerNoViableAltException.h; sourceTree = ""; }; + 37D1C358186A31130041671A /* ListTokenSource.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ListTokenSource.cpp; sourceTree = ""; }; + 37D1C359186A31130041671A /* ListTokenSource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ListTokenSource.h; sourceTree = ""; }; + 37D1C35B186A31130041671A /* AbstractEqualityComparator.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = AbstractEqualityComparator.cpp; sourceTree = ""; }; + 37D1C35C186A31130041671A /* AbstractEqualityComparator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AbstractEqualityComparator.h; sourceTree = ""; }; + 37D1C35E186A31130041671A /* Array2DHashSet.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Array2DHashSet.h; sourceTree = ""; }; + 37D1C360186A31130041671A /* DoubleKeyMap.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DoubleKeyMap.h; sourceTree = ""; }; + 37D1C361186A31130041671A /* EqualityComparator.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = EqualityComparator.cpp; sourceTree = ""; }; + 37D1C362186A31130041671A /* EqualityComparator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EqualityComparator.h; sourceTree = ""; }; + 37D1C365186A31130041671A /* GraphicsSupport.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = GraphicsSupport.cpp; sourceTree = ""; }; + 37D1C366186A31130041671A /* GraphicsSupport.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GraphicsSupport.h; sourceTree = ""; }; + 37D1C36B186A31130041671A /* Interval.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Interval.cpp; sourceTree = ""; }; + 37D1C36C186A31130041671A /* Interval.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Interval.h; sourceTree = ""; }; + 37D1C36D186A31130041671A /* IntervalSet.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = IntervalSet.cpp; sourceTree = ""; }; + 37D1C36E186A31130041671A /* IntervalSet.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IntervalSet.h; sourceTree = ""; }; + 37D1C36F186A31130041671A /* IntSet.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = IntSet.cpp; sourceTree = ""; }; + 37D1C370186A31130041671A /* IntSet.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IntSet.h; sourceTree = ""; }; + 37D1C373186A31130041671A /* LogManager.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = LogManager.cpp; sourceTree = ""; }; + 37D1C374186A31130041671A /* LogManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LogManager.h; sourceTree = ""; }; + 37D1C375186A31130041671A /* MultiMap.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = MultiMap.cpp; sourceTree = ""; }; + 37D1C376186A31130041671A /* MultiMap.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MultiMap.h; sourceTree = ""; }; + 37D1C377186A31130041671A /* MurmurHash.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = MurmurHash.cpp; sourceTree = ""; }; + 37D1C378186A31130041671A /* MurmurHash.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MurmurHash.h; sourceTree = ""; }; + 37D1C379186A31130041671A /* NotNull.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = NotNull.cpp; sourceTree = ""; }; + 37D1C37A186A31130041671A /* NotNull.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NotNull.h; sourceTree = ""; }; + 37D1C37D186A31130041671A /* ObjectEqualityComparator.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ObjectEqualityComparator.cpp; sourceTree = ""; }; + 37D1C37E186A31130041671A /* ObjectEqualityComparator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ObjectEqualityComparator.h; sourceTree = ""; }; + 37D1C37F186A31130041671A /* OrderedHashSet.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = OrderedHashSet.cpp; sourceTree = ""; }; + 37D1C380186A31130041671A /* OrderedHashSet.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OrderedHashSet.h; sourceTree = ""; }; + 37D1C383186A31130041671A /* ParseCancellationException.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ParseCancellationException.cpp; sourceTree = ""; }; + 37D1C384186A31130041671A /* ParseCancellationException.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ParseCancellationException.h; sourceTree = ""; }; + 37D1C385186A31130041671A /* TestRig.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = TestRig.cpp; sourceTree = ""; }; + 37D1C386186A31130041671A /* TestRig.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TestRig.h; sourceTree = ""; }; + 37D1C389186A31130041671A /* Utils.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Utils.cpp; sourceTree = ""; }; + 37D1C38A186A31130041671A /* Utils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Utils.h; sourceTree = ""; }; + 37D1C38B186A31130041671A /* NoViableAltException.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = NoViableAltException.cpp; sourceTree = ""; }; + 37D1C38C186A31130041671A /* NoViableAltException.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NoViableAltException.h; sourceTree = ""; }; + 37D1C38D186A31130041671A /* Parser.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Parser.cpp; sourceTree = ""; }; + 37D1C38E186A31130041671A /* Parser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Parser.h; sourceTree = ""; }; + 37D1C38F186A31140041671A /* ParserInterpreter.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ParserInterpreter.cpp; sourceTree = ""; }; + 37D1C390186A31140041671A /* ParserInterpreter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ParserInterpreter.h; sourceTree = ""; }; + 37D1C391186A31140041671A /* ParserRuleContext.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ParserRuleContext.cpp; sourceTree = ""; }; + 37D1C392186A31140041671A /* ParserRuleContext.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ParserRuleContext.h; sourceTree = ""; }; + 37D1C393186A31140041671A /* ProxyErrorListener.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ProxyErrorListener.cpp; sourceTree = ""; }; + 37D1C394186A31140041671A /* ProxyErrorListener.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ProxyErrorListener.h; sourceTree = ""; }; + 37D1C395186A31140041671A /* RecognitionException.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RecognitionException.cpp; sourceTree = ""; }; + 37D1C396186A31140041671A /* RecognitionException.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RecognitionException.h; sourceTree = ""; }; + 37D1C398186A31140041671A /* Recognizer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Recognizer.h; sourceTree = ""; }; + 37D1C399186A31140041671A /* RuleContext.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RuleContext.cpp; sourceTree = ""; }; + 37D1C39A186A31140041671A /* RuleContext.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RuleContext.h; sourceTree = ""; }; + 37D1C39B186A31140041671A /* Token.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Token.cpp; sourceTree = ""; }; + 37D1C39C186A31140041671A /* Token.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Token.h; sourceTree = ""; }; + 37D1C39D186A31140041671A /* TokenFactory.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = TokenFactory.cpp; sourceTree = ""; }; + 37D1C39E186A31140041671A /* TokenFactory.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TokenFactory.h; sourceTree = ""; }; + 37D1C39F186A31140041671A /* TokenSource.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = TokenSource.cpp; sourceTree = ""; }; + 37D1C3A0186A31140041671A /* TokenSource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TokenSource.h; sourceTree = ""; }; + 37D1C3A1186A31140041671A /* TokenStream.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = TokenStream.cpp; sourceTree = ""; }; + 37D1C3A2186A31140041671A /* TokenStream.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TokenStream.h; sourceTree = ""; }; + 37D1C3A3186A31140041671A /* TokenStreamRewriter.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = TokenStreamRewriter.cpp; sourceTree = ""; }; + 37D1C3A4186A31140041671A /* TokenStreamRewriter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TokenStreamRewriter.h; sourceTree = ""; }; + 37D1C3A6186A31140041671A /* AbstractParseTreeVisitor.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = AbstractParseTreeVisitor.cpp; sourceTree = ""; }; + 37D1C3A7186A31140041671A /* AbstractParseTreeVisitor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AbstractParseTreeVisitor.h; sourceTree = ""; }; + 37D1C3A8186A31140041671A /* ErrorNode.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ErrorNode.cpp; sourceTree = ""; }; + 37D1C3A9186A31140041671A /* ErrorNode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ErrorNode.h; sourceTree = ""; }; + 37D1C3AA186A31140041671A /* ErrorNodeImpl.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ErrorNodeImpl.cpp; sourceTree = ""; }; + 37D1C3AB186A31140041671A /* ErrorNodeImpl.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ErrorNodeImpl.h; sourceTree = ""; }; + 37D1C3BB186A31140041671A /* ParseTree.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ParseTree.cpp; sourceTree = ""; }; + 37D1C3BC186A31140041671A /* ParseTree.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ParseTree.h; sourceTree = ""; }; + 37D1C3BD186A31140041671A /* ParseTreeListener.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ParseTreeListener.cpp; sourceTree = ""; }; + 37D1C3BE186A31140041671A /* ParseTreeListener.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ParseTreeListener.h; sourceTree = ""; }; + 37D1C3BF186A31140041671A /* ParseTreeProperty.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ParseTreeProperty.cpp; sourceTree = ""; }; + 37D1C3C0186A31140041671A /* ParseTreeProperty.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ParseTreeProperty.h; sourceTree = ""; }; + 37D1C3C1186A31140041671A /* ParseTreeVisitor.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ParseTreeVisitor.cpp; sourceTree = ""; }; + 37D1C3C2186A31140041671A /* ParseTreeVisitor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ParseTreeVisitor.h; sourceTree = ""; }; + 37D1C3C3186A31140041671A /* ParseTreeWalker.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ParseTreeWalker.cpp; sourceTree = ""; }; + 37D1C3C4186A31140041671A /* ParseTreeWalker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ParseTreeWalker.h; sourceTree = ""; }; + 37D1C3C6186A31140041671A /* Chunk.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Chunk.cpp; sourceTree = ""; }; + 37D1C3C7186A31140041671A /* Chunk.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Chunk.h; sourceTree = ""; }; + 37D1C3C8186A31140041671A /* ParseTreeMatch.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ParseTreeMatch.cpp; sourceTree = ""; }; + 37D1C3C9186A31140041671A /* ParseTreeMatch.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ParseTreeMatch.h; sourceTree = ""; }; + 37D1C3CA186A31140041671A /* ParseTreePattern.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ParseTreePattern.cpp; sourceTree = ""; }; + 37D1C3CB186A31140041671A /* ParseTreePattern.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ParseTreePattern.h; sourceTree = ""; }; + 37D1C3CC186A31140041671A /* ParseTreePatternMatcher.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ParseTreePatternMatcher.cpp; sourceTree = ""; }; + 37D1C3CD186A31140041671A /* ParseTreePatternMatcher.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ParseTreePatternMatcher.h; sourceTree = ""; }; + 37D1C3CE186A31140041671A /* RuleTagToken.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RuleTagToken.cpp; sourceTree = ""; }; + 37D1C3CF186A31140041671A /* RuleTagToken.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RuleTagToken.h; sourceTree = ""; }; + 37D1C3D0186A31140041671A /* TagChunk.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = TagChunk.cpp; sourceTree = ""; }; + 37D1C3D1186A31140041671A /* TagChunk.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TagChunk.h; sourceTree = ""; }; + 37D1C3D2186A31140041671A /* TextChunk.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = TextChunk.cpp; sourceTree = ""; }; + 37D1C3D3186A31140041671A /* TextChunk.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TextChunk.h; sourceTree = ""; }; + 37D1C3D4186A31140041671A /* TokenTagToken.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = TokenTagToken.cpp; sourceTree = ""; }; + 37D1C3D5186A31140041671A /* TokenTagToken.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TokenTagToken.h; sourceTree = ""; }; + 37D1C3D6186A31140041671A /* RuleNode.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RuleNode.cpp; sourceTree = ""; }; + 37D1C3D7186A31140041671A /* RuleNode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RuleNode.h; sourceTree = ""; }; + 37D1C3D8186A31140041671A /* SyntaxTree.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SyntaxTree.cpp; sourceTree = ""; }; + 37D1C3D9186A31140041671A /* SyntaxTree.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SyntaxTree.h; sourceTree = ""; }; + 37D1C3DA186A31140041671A /* TerminalNode.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = TerminalNode.cpp; sourceTree = ""; }; + 37D1C3DB186A31140041671A /* TerminalNode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TerminalNode.h; sourceTree = ""; }; + 37D1C3DC186A31140041671A /* TerminalNodeImpl.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = TerminalNodeImpl.cpp; sourceTree = ""; }; + 37D1C3DD186A31140041671A /* TerminalNodeImpl.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TerminalNodeImpl.h; sourceTree = ""; }; + 37D1C3DE186A31140041671A /* Tree.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Tree.cpp; sourceTree = ""; }; + 37D1C3DF186A31140041671A /* Tree.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Tree.h; sourceTree = ""; }; + 37D1C3E0186A31140041671A /* Trees.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Trees.cpp; sourceTree = ""; }; + 37D1C3E1186A31140041671A /* Trees.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Trees.h; sourceTree = ""; }; + 37D1C3E3186A31140041671A /* XPath.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = XPath.cpp; sourceTree = ""; }; + 37D1C3E4186A31140041671A /* XPath.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XPath.h; sourceTree = ""; }; + 37D1C3E5186A31140041671A /* XPathElement.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = XPathElement.cpp; sourceTree = ""; }; + 37D1C3E6186A31140041671A /* XPathElement.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XPathElement.h; sourceTree = ""; }; + 37D1C3E7186A31140041671A /* XPathLexerErrorListener.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = XPathLexerErrorListener.cpp; sourceTree = ""; }; + 37D1C3E8186A31140041671A /* XPathLexerErrorListener.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XPathLexerErrorListener.h; sourceTree = ""; }; + 37D1C3E9186A31140041671A /* XPathRuleAnywhereElement.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = XPathRuleAnywhereElement.cpp; sourceTree = ""; }; + 37D1C3EA186A31140041671A /* XPathRuleAnywhereElement.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XPathRuleAnywhereElement.h; sourceTree = ""; }; + 37D1C3EB186A31140041671A /* XPathRuleElement.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = XPathRuleElement.cpp; sourceTree = ""; }; + 37D1C3EC186A31140041671A /* XPathRuleElement.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XPathRuleElement.h; sourceTree = ""; }; + 37D1C3ED186A31140041671A /* XPathTokenAnywhereElement.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = XPathTokenAnywhereElement.cpp; sourceTree = ""; }; + 37D1C3EE186A31140041671A /* XPathTokenAnywhereElement.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XPathTokenAnywhereElement.h; sourceTree = ""; }; + 37D1C3EF186A31140041671A /* XPathTokenElement.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = XPathTokenElement.cpp; sourceTree = ""; }; + 37D1C3F0186A31140041671A /* XPathTokenElement.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XPathTokenElement.h; sourceTree = ""; }; + 37D1C3F1186A31140041671A /* XPathWildcardAnywhereElement.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = XPathWildcardAnywhereElement.cpp; sourceTree = ""; }; + 37D1C3F2186A31140041671A /* XPathWildcardAnywhereElement.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XPathWildcardAnywhereElement.h; sourceTree = ""; }; + 37D1C3F3186A31140041671A /* XPathWildcardElement.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = XPathWildcardElement.cpp; sourceTree = ""; }; + 37D1C3F4186A31140041671A /* XPathWildcardElement.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XPathWildcardElement.h; sourceTree = ""; }; + 37D1C3F5186A31140041671A /* UnbufferedCharStream.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = UnbufferedCharStream.cpp; sourceTree = ""; }; + 37D1C3F6186A31140041671A /* UnbufferedCharStream.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UnbufferedCharStream.h; sourceTree = ""; }; + 37D1C3F8186A31140041671A /* UnbufferedTokenStream.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UnbufferedTokenStream.h; sourceTree = ""; }; + 37D1C3F9186A31140041671A /* WritableToken.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WritableToken.cpp; sourceTree = ""; }; + 37D1C3FA186A31140041671A /* WritableToken.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WritableToken.h; sourceTree = ""; }; + 37D1C529186A4B5B0041671A /* stringconverter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = stringconverter.h; path = ../antlrcpp/stringconverter.h; sourceTree = ""; }; + 37D1C52A186A4B5B0041671A /* vectorhelper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = vectorhelper.h; sourceTree = ""; }; + 37D727AA1867AF1E007B6D10 /* antlrcpp.dylib */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; includeInIndex = 0; path = antlrcpp.dylib; sourceTree = BUILT_PRODUCTS_DIR; }; + 37D727AE1867AF1E007B6D10 /* antlrcpp-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "antlrcpp-Prefix.pch"; sourceTree = ""; }; + 37D727AF1867AF1E007B6D10 /* antlrcppProj.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = antlrcppProj.xcconfig; sourceTree = ""; }; + 37D727B01867AF1E007B6D10 /* antlrcppTarget.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = antlrcppTarget.xcconfig; sourceTree = ""; }; + 37D727B11867AF1E007B6D10 /* antlrcpp.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = antlrcpp.h; sourceTree = ""; }; + 37D727B21867AF1E007B6D10 /* antlrcppPriv.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = antlrcppPriv.h; sourceTree = ""; }; + 37D727B31867AF1E007B6D10 /* antlrcpp.cp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = antlrcpp.cp; sourceTree = ""; }; + 37DEDC1D1B30E71800A6FC5F /* BaseErrorListener.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = BaseErrorListener.cpp; sourceTree = ""; }; + 37DEDC1F1B384AE500A6FC5F /* UUID.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = UUID.cpp; sourceTree = ""; }; + 37F1355E1B49631900E0CACF /* IErrorListener.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IErrorListener.h; sourceTree = ""; }; + 37F135621B49644000E0CACF /* IRecognizer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IRecognizer.h; sourceTree = ""; }; + 37F135681B4AC02800E0CACF /* antlrcpp Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "antlrcpp Tests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; }; + 37F1356B1B4AC02800E0CACF /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + 37F1356C1B4AC02800E0CACF /* antlrcpp_Tests.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = antlrcpp_Tests.mm; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + 37C147141B4D5A04008EDDDB /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; + 37D727A71867AF1E007B6D10 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; + 37F135651B4AC02800E0CACF /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + 37F1356E1B4AC02800E0CACF /* antlrcpp.dylib in Frameworks */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 37D1C2C5186A31130041671A /* runtime */ = { + isa = PBXGroup; + children = ( + 37F135621B49644000E0CACF /* IRecognizer.h */, + 37F1355E1B49631900E0CACF /* IErrorListener.h */, + 37BED8C2187304740043FB28 /* Exceptions.cpp */, + 37BED8C3187304740043FB28 /* Exceptions.h */, + 37D1C2C6186A31130041671A /* ANTLRErrorListener.cpp */, + 37D1C2C7186A31130041671A /* ANTLRErrorListener.h */, + 37D1C2C8186A31130041671A /* ANTLRErrorStrategy.cpp */, + 37D1C2C9186A31130041671A /* ANTLRErrorStrategy.h */, + 37D1C2CA186A31130041671A /* ANTLRFileStream.cpp */, + 37D1C2CB186A31130041671A /* ANTLRFileStream.h */, + 37D1C2CC186A31130041671A /* ANTLRInputStream.cpp */, + 37D1C2CD186A31130041671A /* ANTLRInputStream.h */, + 37D1C2CE186A31130041671A /* atn */, + 37D1C32D186A31130041671A /* BailErrorStrategy.cpp */, + 37D1C32E186A31130041671A /* BailErrorStrategy.h */, + 37D1C330186A31130041671A /* BaseErrorListener.h */, + 37D1C331186A31130041671A /* BufferedTokenStream.cpp */, + 37D1C332186A31130041671A /* BufferedTokenStream.h */, + 37D1C333186A31130041671A /* CharStream.cpp */, + 37D1C334186A31130041671A /* CharStream.h */, + 37D1C335186A31130041671A /* CommonToken.cpp */, + 37D1C336186A31130041671A /* CommonToken.h */, + 37D1C337186A31130041671A /* CommonTokenFactory.cpp */, + 37D1C338186A31130041671A /* CommonTokenFactory.h */, + 37D1C339186A31130041671A /* CommonTokenStream.cpp */, + 37D1C33A186A31130041671A /* CommonTokenStream.h */, + 37D1C33B186A31130041671A /* ConsoleErrorListener.cpp */, + 37D1C33C186A31130041671A /* ConsoleErrorListener.h */, + 37D1C33D186A31130041671A /* DefaultErrorStrategy.cpp */, + 37D1C33E186A31130041671A /* DefaultErrorStrategy.h */, + 37D1C33F186A31130041671A /* dfa */, + 37D1C348186A31130041671A /* DiagnosticErrorListener.cpp */, + 37D1C349186A31130041671A /* DiagnosticErrorListener.h */, + 37D1C34A186A31130041671A /* FailedPredicateException.cpp */, + 37D1C34B186A31130041671A /* FailedPredicateException.h */, + 37DEDC1D1B30E71800A6FC5F /* BaseErrorListener.cpp */, + 37D1C34C186A31130041671A /* InputMismatchException.cpp */, + 37D1C34D186A31130041671A /* InputMismatchException.h */, + 37D1C34E186A31130041671A /* InterpreterRuleContext.cpp */, + 37D1C34F186A31130041671A /* InterpreterRuleContext.h */, + 37D1C350186A31130041671A /* IntStream.cpp */, + 37D1C351186A31130041671A /* IntStream.h */, + 37D1C352186A31130041671A /* Lexer.cpp */, + 37D1C353186A31130041671A /* Lexer.h */, + 37D1C354186A31130041671A /* LexerInterpreter.cpp */, + 37D1C355186A31130041671A /* LexerInterpreter.h */, + 37D1C356186A31130041671A /* LexerNoViableAltException.cpp */, + 37D1C357186A31130041671A /* LexerNoViableAltException.h */, + 37D1C358186A31130041671A /* ListTokenSource.cpp */, + 37D1C359186A31130041671A /* ListTokenSource.h */, + 37D1C35A186A31130041671A /* misc */, + 37D1C38B186A31130041671A /* NoViableAltException.cpp */, + 37D1C38C186A31130041671A /* NoViableAltException.h */, + 37D1C38D186A31130041671A /* Parser.cpp */, + 37D1C38E186A31130041671A /* Parser.h */, + 37D1C38F186A31140041671A /* ParserInterpreter.cpp */, + 37D1C390186A31140041671A /* ParserInterpreter.h */, + 37D1C391186A31140041671A /* ParserRuleContext.cpp */, + 37D1C392186A31140041671A /* ParserRuleContext.h */, + 37D1C393186A31140041671A /* ProxyErrorListener.cpp */, + 37D1C394186A31140041671A /* ProxyErrorListener.h */, + 37D1C395186A31140041671A /* RecognitionException.cpp */, + 37D1C396186A31140041671A /* RecognitionException.h */, + 37D1C398186A31140041671A /* Recognizer.h */, + 37D1C399186A31140041671A /* RuleContext.cpp */, + 37D1C39A186A31140041671A /* RuleContext.h */, + 37D1C39B186A31140041671A /* Token.cpp */, + 37D1C39C186A31140041671A /* Token.h */, + 37D1C39D186A31140041671A /* TokenFactory.cpp */, + 37D1C39E186A31140041671A /* TokenFactory.h */, + 37D1C39F186A31140041671A /* TokenSource.cpp */, + 37D1C3A0186A31140041671A /* TokenSource.h */, + 37D1C3A1186A31140041671A /* TokenStream.cpp */, + 37D1C3A2186A31140041671A /* TokenStream.h */, + 37D1C3A3186A31140041671A /* TokenStreamRewriter.cpp */, + 37D1C3A4186A31140041671A /* TokenStreamRewriter.h */, + 37D1C3A5186A31140041671A /* tree */, + 37D1C3F5186A31140041671A /* UnbufferedCharStream.cpp */, + 37D1C3F6186A31140041671A /* UnbufferedCharStream.h */, + 37D1C3F8186A31140041671A /* UnbufferedTokenStream.h */, + 37D1C3F9186A31140041671A /* WritableToken.cpp */, + 37D1C3FA186A31140041671A /* WritableToken.h */, + ); + name = runtime; + path = ../org/antlr/v4/runtime; + sourceTree = ""; + }; + 37D1C2CE186A31130041671A /* atn */ = { + isa = PBXGroup; + children = ( + 37D1C2CF186A31130041671A /* AbstractPredicateTransition.cpp */, + 37D1C2D0186A31130041671A /* AbstractPredicateTransition.h */, + 37D1C2D1186A31130041671A /* ActionTransition.cpp */, + 37D1C2D2186A31130041671A /* ActionTransition.h */, + 37D1C2D3186A31130041671A /* ArrayPredictionContext.cpp */, + 37D1C2D4186A31130041671A /* ArrayPredictionContext.h */, + 37D1C2D5186A31130041671A /* ATN.cpp */, + 37D1C2D6186A31130041671A /* ATN.h */, + 37D1C2D7186A31130041671A /* ATNConfig.cpp */, + 37D1C2D8186A31130041671A /* ATNConfig.h */, + 37D1C2D9186A31130041671A /* ATNConfigSet.cpp */, + 37D1C2DA186A31130041671A /* ATNConfigSet.h */, + 37D1C2DB186A31130041671A /* ATNDeserializationOptions.cpp */, + 37D1C2DC186A31130041671A /* ATNDeserializationOptions.h */, + 37D1C2DD186A31130041671A /* ATNDeserializer.cpp */, + 37D1C2DE186A31130041671A /* ATNDeserializer.h */, + 37D1C2DF186A31130041671A /* ATNSerializer.cpp */, + 37D1C2E0186A31130041671A /* ATNSerializer.h */, + 37D1C2E1186A31130041671A /* ATNSimulator.cpp */, + 37D1C2E2186A31130041671A /* ATNSimulator.h */, + 37D1C2E3186A31130041671A /* ATNState.cpp */, + 37D1C2E4186A31130041671A /* ATNState.h */, + 37D1C2E5186A31130041671A /* ATNType.cpp */, + 37D1C2E6186A31130041671A /* ATNType.h */, + 37D1C2E7186A31130041671A /* AtomTransition.cpp */, + 37D1C2E8186A31130041671A /* AtomTransition.h */, + 37D1C2E9186A31130041671A /* BasicBlockStartState.cpp */, + 37D1C2EA186A31130041671A /* BasicBlockStartState.h */, + 37D1C2EB186A31130041671A /* BasicState.cpp */, + 37D1C2EC186A31130041671A /* BasicState.h */, + 37D1C2ED186A31130041671A /* BlockEndState.cpp */, + 37D1C2EE186A31130041671A /* BlockEndState.h */, + 37D1C2EF186A31130041671A /* BlockStartState.cpp */, + 37D1C2F0186A31130041671A /* BlockStartState.h */, + 37D1C2F1186A31130041671A /* DecisionState.cpp */, + 37D1C2F2186A31130041671A /* DecisionState.h */, + 37D1C2F3186A31130041671A /* EmptyPredictionContext.cpp */, + 37D1C2F4186A31130041671A /* EmptyPredictionContext.h */, + 37D1C2F5186A31130041671A /* EpsilonTransition.cpp */, + 37D1C2F6186A31130041671A /* EpsilonTransition.h */, + 37D1C2F7186A31130041671A /* LexerATNConfig.cpp */, + 37D1C2F8186A31130041671A /* LexerATNConfig.h */, + 37D1C2F9186A31130041671A /* LexerATNSimulator.cpp */, + 37D1C2FA186A31130041671A /* LexerATNSimulator.h */, + 37D1C2FB186A31130041671A /* LL1Analyzer.cpp */, + 37D1C2FC186A31130041671A /* LL1Analyzer.h */, + 37D1C2FD186A31130041671A /* LoopEndState.cpp */, + 37D1C2FE186A31130041671A /* LoopEndState.h */, + 37D1C2FF186A31130041671A /* NotSetTransition.cpp */, + 37D1C300186A31130041671A /* NotSetTransition.h */, + 37D1C301186A31130041671A /* OrderedATNConfigSet.cpp */, + 37D1C302186A31130041671A /* OrderedATNConfigSet.h */, + 37D1C303186A31130041671A /* ParserATNSimulator.cpp */, + 37D1C304186A31130041671A /* ParserATNSimulator.h */, + 37D1C305186A31130041671A /* PlusBlockStartState.cpp */, + 37D1C306186A31130041671A /* PlusBlockStartState.h */, + 37D1C307186A31130041671A /* PlusLoopbackState.cpp */, + 37D1C308186A31130041671A /* PlusLoopbackState.h */, + 37D1C309186A31130041671A /* PrecedencePredicateTransition.cpp */, + 37D1C30A186A31130041671A /* PrecedencePredicateTransition.h */, + 37D1C30B186A31130041671A /* PredicateTransition.cpp */, + 37D1C30C186A31130041671A /* PredicateTransition.h */, + 37D1C30D186A31130041671A /* PredictionContext.cpp */, + 37D1C30E186A31130041671A /* PredictionContext.h */, + 37D1C30F186A31130041671A /* PredictionContextCache.cpp */, + 37D1C310186A31130041671A /* PredictionContextCache.h */, + 37D1C311186A31130041671A /* PredictionMode.cpp */, + 37D1C312186A31130041671A /* PredictionMode.h */, + 37D1C313186A31130041671A /* RangeTransition.cpp */, + 37D1C314186A31130041671A /* RangeTransition.h */, + 37D1C315186A31130041671A /* RuleStartState.cpp */, + 37D1C316186A31130041671A /* RuleStartState.h */, + 37D1C317186A31130041671A /* RuleStopState.cpp */, + 37D1C318186A31130041671A /* RuleStopState.h */, + 37D1C319186A31130041671A /* RuleTransition.cpp */, + 37D1C31A186A31130041671A /* RuleTransition.h */, + 37D1C31B186A31130041671A /* SemanticContext.cpp */, + 37D1C31C186A31130041671A /* SemanticContext.h */, + 37D1C31D186A31130041671A /* SetTransition.cpp */, + 37D1C31E186A31130041671A /* SetTransition.h */, + 37D1C31F186A31130041671A /* SingletonPredictionContext.cpp */, + 37D1C320186A31130041671A /* SingletonPredictionContext.h */, + 37D1C321186A31130041671A /* StarBlockStartState.cpp */, + 37D1C322186A31130041671A /* StarBlockStartState.h */, + 37D1C323186A31130041671A /* StarLoopbackState.cpp */, + 37D1C324186A31130041671A /* StarLoopbackState.h */, + 37D1C325186A31130041671A /* StarLoopEntryState.cpp */, + 37D1C326186A31130041671A /* StarLoopEntryState.h */, + 37D1C327186A31130041671A /* TokensStartState.cpp */, + 37D1C328186A31130041671A /* TokensStartState.h */, + 37D1C329186A31130041671A /* Transition.cpp */, + 37D1C32A186A31130041671A /* Transition.h */, + 37D1C32B186A31130041671A /* WildcardTransition.cpp */, + 37D1C32C186A31130041671A /* WildcardTransition.h */, + ); + path = atn; + sourceTree = ""; + }; + 37D1C33F186A31130041671A /* dfa */ = { + isa = PBXGroup; + children = ( + 37D1C340186A31130041671A /* DFA.cpp */, + 37D1C341186A31130041671A /* DFA.h */, + 37D1C342186A31130041671A /* DFASerializer.cpp */, + 37D1C343186A31130041671A /* DFASerializer.h */, + 37D1C344186A31130041671A /* DFAState.cpp */, + 37D1C345186A31130041671A /* DFAState.h */, + 37D1C346186A31130041671A /* LexerDFASerializer.cpp */, + 37D1C347186A31130041671A /* LexerDFASerializer.h */, + ); + path = dfa; + sourceTree = ""; + }; + 37D1C35A186A31130041671A /* misc */ = { + isa = PBXGroup; + children = ( + 37D1C35B186A31130041671A /* AbstractEqualityComparator.cpp */, + 37D1C35C186A31130041671A /* AbstractEqualityComparator.h */, + 37D1C35E186A31130041671A /* Array2DHashSet.h */, + 37D1C360186A31130041671A /* DoubleKeyMap.h */, + 37D1C361186A31130041671A /* EqualityComparator.cpp */, + 37D1C362186A31130041671A /* EqualityComparator.h */, + 37D1C365186A31130041671A /* GraphicsSupport.cpp */, + 37D1C366186A31130041671A /* GraphicsSupport.h */, + 37D1C36B186A31130041671A /* Interval.cpp */, + 37D1C36C186A31130041671A /* Interval.h */, + 37D1C36D186A31130041671A /* IntervalSet.cpp */, + 37D1C36E186A31130041671A /* IntervalSet.h */, + 37D1C36F186A31130041671A /* IntSet.cpp */, + 37D1C370186A31130041671A /* IntSet.h */, + 37D1C373186A31130041671A /* LogManager.cpp */, + 37D1C374186A31130041671A /* LogManager.h */, + 37D1C375186A31130041671A /* MultiMap.cpp */, + 37D1C376186A31130041671A /* MultiMap.h */, + 37D1C377186A31130041671A /* MurmurHash.cpp */, + 37D1C378186A31130041671A /* MurmurHash.h */, + 37D1C379186A31130041671A /* NotNull.cpp */, + 37D1C37A186A31130041671A /* NotNull.h */, + 37D1C37D186A31130041671A /* ObjectEqualityComparator.cpp */, + 37D1C37E186A31130041671A /* ObjectEqualityComparator.h */, + 37D1C37F186A31130041671A /* OrderedHashSet.cpp */, + 37D1C380186A31130041671A /* OrderedHashSet.h */, + 37D1C383186A31130041671A /* ParseCancellationException.cpp */, + 37D1C384186A31130041671A /* ParseCancellationException.h */, + 37D1C385186A31130041671A /* TestRig.cpp */, + 37D1C386186A31130041671A /* TestRig.h */, + 37D1C389186A31130041671A /* Utils.cpp */, + 37D1C38A186A31130041671A /* Utils.h */, + ); + path = misc; + sourceTree = ""; + }; + 37D1C3A5186A31140041671A /* tree */ = { + isa = PBXGroup; + children = ( + 37D1C3A6186A31140041671A /* AbstractParseTreeVisitor.cpp */, + 37D1C3A7186A31140041671A /* AbstractParseTreeVisitor.h */, + 37D1C3A8186A31140041671A /* ErrorNode.cpp */, + 37D1C3A9186A31140041671A /* ErrorNode.h */, + 37D1C3AA186A31140041671A /* ErrorNodeImpl.cpp */, + 37D1C3AB186A31140041671A /* ErrorNodeImpl.h */, + 37D1C3BB186A31140041671A /* ParseTree.cpp */, + 37D1C3BC186A31140041671A /* ParseTree.h */, + 37D1C3BD186A31140041671A /* ParseTreeListener.cpp */, + 37D1C3BE186A31140041671A /* ParseTreeListener.h */, + 37D1C3BF186A31140041671A /* ParseTreeProperty.cpp */, + 37D1C3C0186A31140041671A /* ParseTreeProperty.h */, + 37D1C3C1186A31140041671A /* ParseTreeVisitor.cpp */, + 37D1C3C2186A31140041671A /* ParseTreeVisitor.h */, + 37D1C3C3186A31140041671A /* ParseTreeWalker.cpp */, + 37D1C3C4186A31140041671A /* ParseTreeWalker.h */, + 37D1C3C5186A31140041671A /* pattern */, + 37D1C3D6186A31140041671A /* RuleNode.cpp */, + 37D1C3D7186A31140041671A /* RuleNode.h */, + 37D1C3D8186A31140041671A /* SyntaxTree.cpp */, + 37D1C3D9186A31140041671A /* SyntaxTree.h */, + 37D1C3DA186A31140041671A /* TerminalNode.cpp */, + 37D1C3DB186A31140041671A /* TerminalNode.h */, + 37D1C3DC186A31140041671A /* TerminalNodeImpl.cpp */, + 37D1C3DD186A31140041671A /* TerminalNodeImpl.h */, + 37D1C3DE186A31140041671A /* Tree.cpp */, + 37D1C3DF186A31140041671A /* Tree.h */, + 37D1C3E0186A31140041671A /* Trees.cpp */, + 37D1C3E1186A31140041671A /* Trees.h */, + 37D1C3E2186A31140041671A /* xpath */, + ); + path = tree; + sourceTree = ""; + }; + 37D1C3C5186A31140041671A /* pattern */ = { + isa = PBXGroup; + children = ( + 37D1C3C6186A31140041671A /* Chunk.cpp */, + 37D1C3C7186A31140041671A /* Chunk.h */, + 37D1C3C8186A31140041671A /* ParseTreeMatch.cpp */, + 37D1C3C9186A31140041671A /* ParseTreeMatch.h */, + 37D1C3CA186A31140041671A /* ParseTreePattern.cpp */, + 37D1C3CB186A31140041671A /* ParseTreePattern.h */, + 37D1C3CC186A31140041671A /* ParseTreePatternMatcher.cpp */, + 37D1C3CD186A31140041671A /* ParseTreePatternMatcher.h */, + 37D1C3CE186A31140041671A /* RuleTagToken.cpp */, + 37D1C3CF186A31140041671A /* RuleTagToken.h */, + 37D1C3D0186A31140041671A /* TagChunk.cpp */, + 37D1C3D1186A31140041671A /* TagChunk.h */, + 37D1C3D2186A31140041671A /* TextChunk.cpp */, + 37D1C3D3186A31140041671A /* TextChunk.h */, + 37D1C3D4186A31140041671A /* TokenTagToken.cpp */, + 37D1C3D5186A31140041671A /* TokenTagToken.h */, + ); + path = pattern; + sourceTree = ""; + }; + 37D1C3E2186A31140041671A /* xpath */ = { + isa = PBXGroup; + children = ( + 3789F5071872F5C2003E0A30 /* XPathLexer.cpp */, + 3789F5081872F5C2003E0A30 /* XPathLexer.h */, + 37D1C3E3186A31140041671A /* XPath.cpp */, + 37D1C3E4186A31140041671A /* XPath.h */, + 37D1C3E5186A31140041671A /* XPathElement.cpp */, + 37D1C3E6186A31140041671A /* XPathElement.h */, + 37D1C3E7186A31140041671A /* XPathLexerErrorListener.cpp */, + 37D1C3E8186A31140041671A /* XPathLexerErrorListener.h */, + 37D1C3E9186A31140041671A /* XPathRuleAnywhereElement.cpp */, + 37D1C3EA186A31140041671A /* XPathRuleAnywhereElement.h */, + 37D1C3EB186A31140041671A /* XPathRuleElement.cpp */, + 37D1C3EC186A31140041671A /* XPathRuleElement.h */, + 37D1C3ED186A31140041671A /* XPathTokenAnywhereElement.cpp */, + 37D1C3EE186A31140041671A /* XPathTokenAnywhereElement.h */, + 37D1C3EF186A31140041671A /* XPathTokenElement.cpp */, + 37D1C3F0186A31140041671A /* XPathTokenElement.h */, + 37D1C3F1186A31140041671A /* XPathWildcardAnywhereElement.cpp */, + 37D1C3F2186A31140041671A /* XPathWildcardAnywhereElement.h */, + 37D1C3F3186A31140041671A /* XPathWildcardElement.cpp */, + 37D1C3F4186A31140041671A /* XPathWildcardElement.h */, + ); + path = xpath; + sourceTree = ""; + }; + 37D727A11867AF1E007B6D10 = { + isa = PBXGroup; + children = ( + 37D727AC1867AF1E007B6D10 /* antlrcpp */, + 37D1C2C5186A31130041671A /* runtime */, + 37F135691B4AC02800E0CACF /* antlrcpp Tests */, + 37D727AB1867AF1E007B6D10 /* Products */, + ); + sourceTree = ""; + }; + 37D727AB1867AF1E007B6D10 /* Products */ = { + isa = PBXGroup; + children = ( + 37D727AA1867AF1E007B6D10 /* antlrcpp.dylib */, + 37F135681B4AC02800E0CACF /* antlrcpp Tests.xctest */, + 37C147171B4D5A04008EDDDB /* libantlrcpp_static.a */, + ); + name = Products; + sourceTree = ""; + }; + 37D727AC1867AF1E007B6D10 /* antlrcpp */ = { + isa = PBXGroup; + children = ( + 37DEDC1F1B384AE500A6FC5F /* UUID.cpp */, + 37725BA31B39B09000BDA484 /* CPPUtils.cpp */, + 37725BA41B39B09000BDA484 /* CPPUtils.h */, + 16F9451B1B23CB250049275C /* BitSet.h */, + 373775A1192FE6F400A4058D /* UUID.h */, + 3789F5061871AFD8003E0A30 /* Declarations.h */, + 3789F5021870E074003E0A30 /* Arrays.cpp */, + 3789F5031870E074003E0A30 /* Arrays.h */, + 3789F4FE1870D1D8003E0A30 /* StringBuilder.cpp */, + 3789F4FF1870D1D8003E0A30 /* StringBuilder.h */, + 37D1C529186A4B5B0041671A /* stringconverter.h */, + 37D1C52A186A4B5B0041671A /* vectorhelper.h */, + 37D727AF1867AF1E007B6D10 /* antlrcppProj.xcconfig */, + 37D727B01867AF1E007B6D10 /* antlrcppTarget.xcconfig */, + 37D727B11867AF1E007B6D10 /* antlrcpp.h */, + 37D727B21867AF1E007B6D10 /* antlrcppPriv.h */, + 37D727B31867AF1E007B6D10 /* antlrcpp.cp */, + 37D727AD1867AF1E007B6D10 /* Supporting Files */, + 37C0684A191E636C00B4D312 /* Strings.h */, + 37C0684C1920EF8300B4D312 /* Strings.cpp */, + ); + name = antlrcpp; + path = ../antlrcpp; + sourceTree = ""; + }; + 37D727AD1867AF1E007B6D10 /* Supporting Files */ = { + isa = PBXGroup; + children = ( + 37D727AE1867AF1E007B6D10 /* antlrcpp-Prefix.pch */, + ); + name = "Supporting Files"; + sourceTree = ""; + }; + 37F135691B4AC02800E0CACF /* antlrcpp Tests */ = { + isa = PBXGroup; + children = ( + 37F1356C1B4AC02800E0CACF /* antlrcpp_Tests.mm */, + 37F1356A1B4AC02800E0CACF /* Supporting Files */, + ); + path = "antlrcpp Tests"; + sourceTree = ""; + }; + 37F1356A1B4AC02800E0CACF /* Supporting Files */ = { + isa = PBXGroup; + children = ( + 37F1356B1B4AC02800E0CACF /* Info.plist */, + ); + name = "Supporting Files"; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXHeadersBuildPhase section */ + 37C147151B4D5A04008EDDDB /* Headers */ = { + isa = PBXHeadersBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; + 37D727A81867AF1E007B6D10 /* Headers */ = { + isa = PBXHeadersBuildPhase; + buildActionMask = 2147483647; + files = ( + 37D1C484186A31140041671A /* IntStream.h in Headers */, + 37D1C4DA186A31140041671A /* ErrorNode.h in Headers */, + 37D1C4F0186A31140041671A /* ParseTreeProperty.h in Headers */, + 37D1C454186A31140041671A /* SingletonPredictionContext.h in Headers */, + 37D1C4CA186A31140041671A /* Recognizer.h in Headers */, + 37D1C4BC186A31140041671A /* Utils.h in Headers */, + 37D1C494186A31140041671A /* EqualityComparator.h in Headers */, + 37D1C4B6186A31140041671A /* ParseCancellationException.h in Headers */, + 37D1C51C186A31140041671A /* XPathTokenAnywhereElement.h in Headers */, + 37D1C4D8186A31140041671A /* AbstractParseTreeVisitor.h in Headers */, + 37D1C4A6186A31140041671A /* LogManager.h in Headers */, + 3789F5051870E074003E0A30 /* Arrays.h in Headers */, + 37D1C4D6186A31140041671A /* TokenStreamRewriter.h in Headers */, + 37D1C51E186A31140041671A /* XPathTokenElement.h in Headers */, + 37D1C510186A31140041671A /* Trees.h in Headers */, + 37D1C45A186A31140041671A /* StarLoopEntryState.h in Headers */, + 37D1C410186A31140041671A /* ATNDeserializationOptions.h in Headers */, + 37D1C44E186A31140041671A /* RuleTransition.h in Headers */, + 37D1C4FA186A31140041671A /* ParseTreePattern.h in Headers */, + 37D1C400186A31140041671A /* ANTLRFileStream.h in Headers */, + 37D1C46C186A31140041671A /* CommonTokenFactory.h in Headers */, + 37D1C4B8186A31140041671A /* TestRig.h in Headers */, + 37D1C402186A31140041671A /* ANTLRInputStream.h in Headers */, + 37D1C416186A31140041671A /* ATNSimulator.h in Headers */, + 37D1C4EE186A31140041671A /* ParseTreeListener.h in Headers */, + 37D1C48A186A31140041671A /* LexerNoViableAltException.h in Headers */, + 37D1C422186A31140041671A /* BlockEndState.h in Headers */, + 37D1C41A186A31140041671A /* ATNType.h in Headers */, + 37D1C42A186A31140041671A /* EpsilonTransition.h in Headers */, + 37D1C51A186A31140041671A /* XPathRuleElement.h in Headers */, + 37F135631B49644000E0CACF /* IRecognizer.h in Headers */, + 37D1C464186A31140041671A /* BaseErrorListener.h in Headers */, + 37D1C466186A31140041671A /* BufferedTokenStream.h in Headers */, + 37D1C488186A31140041671A /* LexerInterpreter.h in Headers */, + 37D1C480186A31140041671A /* InputMismatchException.h in Headers */, + 37F1355F1B49631900E0CACF /* IErrorListener.h in Headers */, + 37D1C3FE186A31140041671A /* ANTLRErrorStrategy.h in Headers */, + 37D1C440186A31140041671A /* PredicateTransition.h in Headers */, + 37D1C522186A31140041671A /* XPathWildcardElement.h in Headers */, + 37D1C4F6186A31140041671A /* Chunk.h in Headers */, + 37D1C514186A31140041671A /* XPathElement.h in Headers */, + 37D1C47A186A31140041671A /* LexerDFASerializer.h in Headers */, + 37D1C506186A31140041671A /* RuleNode.h in Headers */, + 37D1C46E186A31140041671A /* CommonTokenStream.h in Headers */, + 37D1C434186A31140041671A /* NotSetTransition.h in Headers */, + 37D1C4D4186A31140041671A /* TokenStream.h in Headers */, + 37D1C4C2186A31140041671A /* ParserInterpreter.h in Headers */, + 37D1C43A186A31140041671A /* PlusBlockStartState.h in Headers */, + 37D1C436186A31140041671A /* OrderedATNConfigSet.h in Headers */, + 37D1C42E186A31140041671A /* LexerATNSimulator.h in Headers */, + 37725BA61B39B09000BDA484 /* CPPUtils.h in Headers */, + 37D1C456186A31140041671A /* StarBlockStartState.h in Headers */, + 37D1C444186A31140041671A /* PredictionContextCache.h in Headers */, + 37D1C52B186A4B5B0041671A /* stringconverter.h in Headers */, + 37D1C50E186A31140041671A /* Tree.h in Headers */, + 37D1C4AA186A31140041671A /* MurmurHash.h in Headers */, + 37D1C4F4186A31140041671A /* ParseTreeWalker.h in Headers */, + 37D1C474186A31140041671A /* DFA.h in Headers */, + 37D1C48C186A31140041671A /* ListTokenSource.h in Headers */, + 37D1C498186A31140041671A /* GraphicsSupport.h in Headers */, + 37D1C4D0186A31140041671A /* TokenFactory.h in Headers */, + 37D1C504186A31140041671A /* TokenTagToken.h in Headers */, + 37D1C502186A31140041671A /* TextChunk.h in Headers */, + 37D1C4DC186A31140041671A /* ErrorNodeImpl.h in Headers */, + 37D1C4B0186A31140041671A /* ObjectEqualityComparator.h in Headers */, + 37D1C462186A31140041671A /* BailErrorStrategy.h in Headers */, + 37D1C46A186A31140041671A /* CommonToken.h in Headers */, + 37D1C482186A31140041671A /* InterpreterRuleContext.h in Headers */, + 37D1C420186A31140041671A /* BasicState.h in Headers */, + 37D1C512186A31140041671A /* XPath.h in Headers */, + 37D1C47E186A31140041671A /* FailedPredicateException.h in Headers */, + 37D1C432186A31140041671A /* LoopEndState.h in Headers */, + 37D1C508186A31140041671A /* SyntaxTree.h in Headers */, + 37D1C4FE186A31140041671A /* RuleTagToken.h in Headers */, + 37D1C442186A31140041671A /* PredictionContext.h in Headers */, + 37D1C3FC186A31140041671A /* ANTLRErrorListener.h in Headers */, + 37D1C52C186A4B5B0041671A /* vectorhelper.h in Headers */, + 37D1C412186A31140041671A /* ATNDeserializer.h in Headers */, + 37D1C418186A31140041671A /* ATNState.h in Headers */, + 37D1C4CE186A31140041671A /* Token.h in Headers */, + 37D1C414186A31140041671A /* ATNSerializer.h in Headers */, + 373775A2192FE6F400A4058D /* UUID.h in Headers */, + 37D1C4C4186A31140041671A /* ParserRuleContext.h in Headers */, + 37D1C478186A31140041671A /* DFAState.h in Headers */, + 37D1C526186A31140041671A /* UnbufferedTokenStream.h in Headers */, + 37D1C50A186A31140041671A /* TerminalNode.h in Headers */, + 37D1C500186A31140041671A /* TagChunk.h in Headers */, + 37D1C44A186A31140041671A /* RuleStartState.h in Headers */, + 37D1C4C8186A31140041671A /* RecognitionException.h in Headers */, + 37D1C486186A31140041671A /* Lexer.h in Headers */, + 37D1C404186A31140041671A /* AbstractPredicateTransition.h in Headers */, + 37D1C4A8186A31140041671A /* MultiMap.h in Headers */, + 37D1C524186A31140041671A /* UnbufferedCharStream.h in Headers */, + 37D1C4C6186A31140041671A /* ProxyErrorListener.h in Headers */, + 37D1C520186A31140041671A /* XPathWildcardAnywhereElement.h in Headers */, + 37D1C4D2186A31140041671A /* TokenSource.h in Headers */, + 37D1C41C186A31140041671A /* AtomTransition.h in Headers */, + 37D1C4B2186A31140041671A /* OrderedHashSet.h in Headers */, + 37D1C4A2186A31140041671A /* IntSet.h in Headers */, + 16F9451C1B23CB250049275C /* BitSet.h in Headers */, + 37D1C430186A31140041671A /* LL1Analyzer.h in Headers */, + 3789F5011870D1D8003E0A30 /* StringBuilder.h in Headers */, + 37D1C476186A31140041671A /* DFASerializer.h in Headers */, + 37D1C43C186A31140041671A /* PlusLoopbackState.h in Headers */, + 37D1C41E186A31140041671A /* BasicBlockStartState.h in Headers */, + 37D1C460186A31140041671A /* WildcardTransition.h in Headers */, + 37D1C406186A31140041671A /* ActionTransition.h in Headers */, + 37D1C50C186A31140041671A /* TerminalNodeImpl.h in Headers */, + 37D1C45C186A31140041671A /* TokensStartState.h in Headers */, + 37D1C426186A31140041671A /* DecisionState.h in Headers */, + 37D1C490186A31140041671A /* Array2DHashSet.h in Headers */, + 37D1C40A186A31140041671A /* ATN.h in Headers */, + 37D1C472186A31140041671A /* DefaultErrorStrategy.h in Headers */, + 37D1C4F2186A31140041671A /* ParseTreeVisitor.h in Headers */, + 37D1C438186A31140041671A /* ParserATNSimulator.h in Headers */, + 37D1C470186A31140041671A /* ConsoleErrorListener.h in Headers */, + 37D1C492186A31140041671A /* DoubleKeyMap.h in Headers */, + 37D1C446186A31140041671A /* PredictionMode.h in Headers */, + 37D1C45E186A31140041671A /* Transition.h in Headers */, + 37D1C40C186A31140041671A /* ATNConfig.h in Headers */, + 37D1C408186A31140041671A /* ArrayPredictionContext.h in Headers */, + 37D1C42C186A31140041671A /* LexerATNConfig.h in Headers */, + 37D1C424186A31140041671A /* BlockStartState.h in Headers */, + 37D1C450186A31140041671A /* SemanticContext.h in Headers */, + 37D1C448186A31140041671A /* RangeTransition.h in Headers */, + 37D1C458186A31140041671A /* StarLoopbackState.h in Headers */, + 37D1C468186A31140041671A /* CharStream.h in Headers */, + 37D1C47C186A31140041671A /* DiagnosticErrorListener.h in Headers */, + 37BED8C5187304740043FB28 /* Exceptions.h in Headers */, + 37D1C4CC186A31140041671A /* RuleContext.h in Headers */, + 37D1C48E186A31140041671A /* AbstractEqualityComparator.h in Headers */, + 37D1C516186A31140041671A /* XPathLexerErrorListener.h in Headers */, + 37D1C44C186A31140041671A /* RuleStopState.h in Headers */, + 37D1C4C0186A31140041671A /* Parser.h in Headers */, + 37D1C452186A31140041671A /* SetTransition.h in Headers */, + 37D1C4A0186A31140041671A /* IntervalSet.h in Headers */, + 37D1C4EC186A31140041671A /* ParseTree.h in Headers */, + 37D1C43E186A31140041671A /* PrecedencePredicateTransition.h in Headers */, + 37D1C49E186A31140041671A /* Interval.h in Headers */, + 37D1C4BE186A31140041671A /* NoViableAltException.h in Headers */, + 37D1C4F8186A31140041671A /* ParseTreeMatch.h in Headers */, + 37D1C428186A31140041671A /* EmptyPredictionContext.h in Headers */, + 37D1C4FC186A31140041671A /* ParseTreePatternMatcher.h in Headers */, + 37D1C518186A31140041671A /* XPathRuleAnywhereElement.h in Headers */, + 37D1C4AC186A31140041671A /* NotNull.h in Headers */, + 37D1C528186A31140041671A /* WritableToken.h in Headers */, + 37C0684B191E636C00B4D312 /* Strings.h in Headers */, + 3789F50A1872F5C2003E0A30 /* XPathLexer.h in Headers */, + 37D1C40E186A31140041671A /* ATNConfigSet.h in Headers */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXHeadersBuildPhase section */ + +/* Begin PBXNativeTarget section */ + 37C147161B4D5A04008EDDDB /* antlrcpp_static */ = { + isa = PBXNativeTarget; + buildConfigurationList = 37C147211B4D5A04008EDDDB /* Build configuration list for PBXNativeTarget "antlrcpp_static" */; + buildPhases = ( + 37C147131B4D5A04008EDDDB /* Sources */, + 37C147141B4D5A04008EDDDB /* Frameworks */, + 37C147151B4D5A04008EDDDB /* Headers */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = antlrcpp_static; + productName = antlrcpp_static; + productReference = 37C147171B4D5A04008EDDDB /* libantlrcpp_static.a */; + productType = "com.apple.product-type.library.static"; + }; + 37D727A91867AF1E007B6D10 /* antlrcpp */ = { + isa = PBXNativeTarget; + buildConfigurationList = 37D727B71867AF1E007B6D10 /* Build configuration list for PBXNativeTarget "antlrcpp" */; + buildPhases = ( + 37D727A61867AF1E007B6D10 /* Sources */, + 37D727A71867AF1E007B6D10 /* Frameworks */, + 37D727A81867AF1E007B6D10 /* Headers */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = antlrcpp; + productName = antlrcpp; + productReference = 37D727AA1867AF1E007B6D10 /* antlrcpp.dylib */; + productType = "com.apple.product-type.library.dynamic"; + }; + 37F135671B4AC02800E0CACF /* antlrcpp Tests */ = { + isa = PBXNativeTarget; + buildConfigurationList = 37F135731B4AC02800E0CACF /* Build configuration list for PBXNativeTarget "antlrcpp Tests" */; + buildPhases = ( + 37F135641B4AC02800E0CACF /* Sources */, + 37F135651B4AC02800E0CACF /* Frameworks */, + 37F135661B4AC02800E0CACF /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + 37F135701B4AC02800E0CACF /* PBXTargetDependency */, + ); + name = "antlrcpp Tests"; + productName = "antlrcpp Tests"; + productReference = 37F135681B4AC02800E0CACF /* antlrcpp Tests.xctest */; + productType = "com.apple.product-type.bundle.unit-test"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 37D727A21867AF1E007B6D10 /* Project object */ = { + isa = PBXProject; + attributes = { + LastUpgradeCheck = 0640; + ORGANIZATIONNAME = "Dan McLaughlin"; + TargetAttributes = { + 37C147161B4D5A04008EDDDB = { + CreatedOnToolsVersion = 6.3.2; + }; + 37F135671B4AC02800E0CACF = { + CreatedOnToolsVersion = 6.3.2; + }; + }; + }; + buildConfigurationList = 37D727A51867AF1E007B6D10 /* Build configuration list for PBXProject "antlrcpp" */; + compatibilityVersion = "Xcode 3.2"; + developmentRegion = English; + hasScannedForEncodings = 0; + knownRegions = ( + en, + ); + mainGroup = 37D727A11867AF1E007B6D10; + productRefGroup = 37D727AB1867AF1E007B6D10 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 37D727A91867AF1E007B6D10 /* antlrcpp */, + 37F135671B4AC02800E0CACF /* antlrcpp Tests */, + 37C147161B4D5A04008EDDDB /* antlrcpp_static */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + 37F135661B4AC02800E0CACF /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + 37C147131B4D5A04008EDDDB /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; + 37D727A61867AF1E007B6D10 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 37D1C48B186A31140041671A /* ListTokenSource.cpp in Sources */, + 37D1C40F186A31140041671A /* ATNDeserializationOptions.cpp in Sources */, + 37D1C4A1186A31140041671A /* IntSet.cpp in Sources */, + 37D1C479186A31140041671A /* LexerDFASerializer.cpp in Sources */, + 37D1C4F9186A31140041671A /* ParseTreePattern.cpp in Sources */, + 37D1C451186A31140041671A /* SetTransition.cpp in Sources */, + 37D1C4D9186A31140041671A /* ErrorNode.cpp in Sources */, + 37D1C419186A31140041671A /* ATNType.cpp in Sources */, + 37D1C51B186A31140041671A /* XPathTokenAnywhereElement.cpp in Sources */, + 37D1C46B186A31140041671A /* CommonTokenFactory.cpp in Sources */, + 37BED8C4187304740043FB28 /* Exceptions.cpp in Sources */, + 37D1C417186A31140041671A /* ATNState.cpp in Sources */, + 37D1C50B186A31140041671A /* TerminalNodeImpl.cpp in Sources */, + 37D1C4D1186A31140041671A /* TokenSource.cpp in Sources */, + 37D1C41B186A31140041671A /* AtomTransition.cpp in Sources */, + 37D1C44D186A31140041671A /* RuleTransition.cpp in Sources */, + 37D1C407186A31140041671A /* ArrayPredictionContext.cpp in Sources */, + 37D1C477186A31140041671A /* DFAState.cpp in Sources */, + 37D1C461186A31140041671A /* BailErrorStrategy.cpp in Sources */, + 37D1C467186A31140041671A /* CharStream.cpp in Sources */, + 37D1C4D3186A31140041671A /* TokenStream.cpp in Sources */, + 37D1C405186A31140041671A /* ActionTransition.cpp in Sources */, + 37D1C519186A31140041671A /* XPathRuleElement.cpp in Sources */, + 37D1C505186A31140041671A /* RuleNode.cpp in Sources */, + 37D1C45B186A31140041671A /* TokensStartState.cpp in Sources */, + 37D1C47B186A31140041671A /* DiagnosticErrorListener.cpp in Sources */, + 37D1C485186A31140041671A /* Lexer.cpp in Sources */, + 37D1C441186A31140041671A /* PredictionContext.cpp in Sources */, + 37D1C401186A31140041671A /* ANTLRInputStream.cpp in Sources */, + 37D1C4C1186A31140041671A /* ParserInterpreter.cpp in Sources */, + 37D1C4AF186A31140041671A /* ObjectEqualityComparator.cpp in Sources */, + 37D1C42F186A31140041671A /* LL1Analyzer.cpp in Sources */, + 37D1C421186A31140041671A /* BlockEndState.cpp in Sources */, + 37D1C457186A31140041671A /* StarLoopbackState.cpp in Sources */, + 37D1C427186A31140041671A /* EmptyPredictionContext.cpp in Sources */, + 37D1C4FD186A31140041671A /* RuleTagToken.cpp in Sources */, + 37D1C423186A31140041671A /* BlockStartState.cpp in Sources */, + 37D1C447186A31140041671A /* RangeTransition.cpp in Sources */, + 37D1C4FF186A31140041671A /* TagChunk.cpp in Sources */, + 37D1C4F1186A31140041671A /* ParseTreeVisitor.cpp in Sources */, + 37D1C42D186A31140041671A /* LexerATNSimulator.cpp in Sources */, + 37D1C415186A31140041671A /* ATNSimulator.cpp in Sources */, + 37D1C433186A31140041671A /* NotSetTransition.cpp in Sources */, + 37D1C49D186A31140041671A /* Interval.cpp in Sources */, + 3789F5001870D1D8003E0A30 /* StringBuilder.cpp in Sources */, + 37D1C483186A31140041671A /* IntStream.cpp in Sources */, + 37D1C453186A31140041671A /* SingletonPredictionContext.cpp in Sources */, + 37D1C4C3186A31140041671A /* ParserRuleContext.cpp in Sources */, + 37D1C449186A31140041671A /* RuleStartState.cpp in Sources */, + 37D1C475186A31140041671A /* DFASerializer.cpp in Sources */, + 37D1C43B186A31140041671A /* PlusLoopbackState.cpp in Sources */, + 37D1C487186A31140041671A /* LexerInterpreter.cpp in Sources */, + 37D1C4FB186A31140041671A /* ParseTreePatternMatcher.cpp in Sources */, + 37D1C435186A31140041671A /* OrderedATNConfigSet.cpp in Sources */, + 37D1C45F186A31140041671A /* WildcardTransition.cpp in Sources */, + 37D1C471186A31140041671A /* DefaultErrorStrategy.cpp in Sources */, + 37D1C50F186A31140041671A /* Trees.cpp in Sources */, + 37D1C469186A31140041671A /* CommonToken.cpp in Sources */, + 37D1C503186A31140041671A /* TokenTagToken.cpp in Sources */, + 37D1C51D186A31140041671A /* XPathTokenElement.cpp in Sources */, + 37D1C4A9186A31140041671A /* MurmurHash.cpp in Sources */, + 37DEDC1E1B30E71800A6FC5F /* BaseErrorListener.cpp in Sources */, + 37D1C43F186A31140041671A /* PredicateTransition.cpp in Sources */, + 37D1C51F186A31140041671A /* XPathWildcardAnywhereElement.cpp in Sources */, + 37D1C4EF186A31140041671A /* ParseTreeProperty.cpp in Sources */, + 37D1C4C5186A31140041671A /* ProxyErrorListener.cpp in Sources */, + 37D1C4BF186A31140041671A /* Parser.cpp in Sources */, + 37D1C44F186A31140041671A /* SemanticContext.cpp in Sources */, + 37D1C4A7186A31140041671A /* MultiMap.cpp in Sources */, + 37D1C481186A31140041671A /* InterpreterRuleContext.cpp in Sources */, + 37D1C40D186A31140041671A /* ATNConfigSet.cpp in Sources */, + 37D1C413186A31140041671A /* ATNSerializer.cpp in Sources */, + 37DEDC201B384AE500A6FC5F /* UUID.cpp in Sources */, + 37C0684D1920EF8300B4D312 /* Strings.cpp in Sources */, + 37D1C4B5186A31140041671A /* ParseCancellationException.cpp in Sources */, + 37D1C465186A31140041671A /* BufferedTokenStream.cpp in Sources */, + 37D1C47F186A31140041671A /* InputMismatchException.cpp in Sources */, + 37D1C409186A31140041671A /* ATN.cpp in Sources */, + 37D1C4CF186A31140041671A /* TokenFactory.cpp in Sources */, + 37D1C431186A31140041671A /* LoopEndState.cpp in Sources */, + 37D1C4ED186A31140041671A /* ParseTreeListener.cpp in Sources */, + 37D1C41D186A31140041671A /* BasicBlockStartState.cpp in Sources */, + 37D1C3FB186A31140041671A /* ANTLRErrorListener.cpp in Sources */, + 37D1C47D186A31140041671A /* FailedPredicateException.cpp in Sources */, + 37D1C4A5186A31140041671A /* LogManager.cpp in Sources */, + 37D1C4B7186A31140041671A /* TestRig.cpp in Sources */, + 37D1C42B186A31140041671A /* LexerATNConfig.cpp in Sources */, + 37D1C4B1186A31140041671A /* OrderedHashSet.cpp in Sources */, + 37D1C4CB186A31140041671A /* RuleContext.cpp in Sources */, + 37D1C4F5186A31140041671A /* Chunk.cpp in Sources */, + 37D1C403186A31140041671A /* AbstractPredicateTransition.cpp in Sources */, + 37D1C49F186A31140041671A /* IntervalSet.cpp in Sources */, + 37D1C4DB186A31140041671A /* ErrorNodeImpl.cpp in Sources */, + 37D1C455186A31140041671A /* StarBlockStartState.cpp in Sources */, + 37D1C4CD186A31140041671A /* Token.cpp in Sources */, + 37D1C437186A31140041671A /* ParserATNSimulator.cpp in Sources */, + 37D1C4F3186A31140041671A /* ParseTreeWalker.cpp in Sources */, + 37D1C41F186A31140041671A /* BasicState.cpp in Sources */, + 37D1C473186A31140041671A /* DFA.cpp in Sources */, + 37D1C517186A31140041671A /* XPathRuleAnywhereElement.cpp in Sources */, + 37D1C527186A31140041671A /* WritableToken.cpp in Sources */, + 37D1C3FF186A31140041671A /* ANTLRFileStream.cpp in Sources */, + 37D1C43D186A31140041671A /* PrecedencePredicateTransition.cpp in Sources */, + 37D1C429186A31140041671A /* EpsilonTransition.cpp in Sources */, + 37D1C493186A31140041671A /* EqualityComparator.cpp in Sources */, + 37D1C4D5186A31140041671A /* TokenStreamRewriter.cpp in Sources */, + 37D1C439186A31140041671A /* PlusBlockStartState.cpp in Sources */, + 37D1C515186A31140041671A /* XPathLexerErrorListener.cpp in Sources */, + 37D1C50D186A31140041671A /* Tree.cpp in Sources */, + 37D1C4BD186A31140041671A /* NoViableAltException.cpp in Sources */, + 37D1C4EB186A31140041671A /* ParseTree.cpp in Sources */, + 37D1C445186A31140041671A /* PredictionMode.cpp in Sources */, + 37D1C4D7186A31140041671A /* AbstractParseTreeVisitor.cpp in Sources */, + 37D1C497186A31140041671A /* GraphicsSupport.cpp in Sources */, + 37D727B41867AF1E007B6D10 /* antlrcpp.cp in Sources */, + 37D1C443186A31140041671A /* PredictionContextCache.cpp in Sources */, + 37D1C411186A31140041671A /* ATNDeserializer.cpp in Sources */, + 37D1C507186A31140041671A /* SyntaxTree.cpp in Sources */, + 37D1C513186A31140041671A /* XPathElement.cpp in Sources */, + 37D1C46D186A31140041671A /* CommonTokenStream.cpp in Sources */, + 3789F5041870E074003E0A30 /* Arrays.cpp in Sources */, + 3789F5091872F5C2003E0A30 /* XPathLexer.cpp in Sources */, + 37D1C459186A31140041671A /* StarLoopEntryState.cpp in Sources */, + 37D1C45D186A31140041671A /* Transition.cpp in Sources */, + 37D1C3FD186A31140041671A /* ANTLRErrorStrategy.cpp in Sources */, + 37D1C509186A31140041671A /* TerminalNode.cpp in Sources */, + 37725BA51B39B09000BDA484 /* CPPUtils.cpp in Sources */, + 37D1C489186A31140041671A /* LexerNoViableAltException.cpp in Sources */, + 37D1C46F186A31140041671A /* ConsoleErrorListener.cpp in Sources */, + 37D1C48D186A31140041671A /* AbstractEqualityComparator.cpp in Sources */, + 37D1C501186A31140041671A /* TextChunk.cpp in Sources */, + 37D1C4C7186A31140041671A /* RecognitionException.cpp in Sources */, + 37D1C4AB186A31140041671A /* NotNull.cpp in Sources */, + 37D1C511186A31140041671A /* XPath.cpp in Sources */, + 37D1C4BB186A31140041671A /* Utils.cpp in Sources */, + 37D1C4F7186A31140041671A /* ParseTreeMatch.cpp in Sources */, + 37D1C425186A31140041671A /* DecisionState.cpp in Sources */, + 37D1C523186A31140041671A /* UnbufferedCharStream.cpp in Sources */, + 37D1C521186A31140041671A /* XPathWildcardElement.cpp in Sources */, + 37D1C40B186A31140041671A /* ATNConfig.cpp in Sources */, + 37D1C44B186A31140041671A /* RuleStopState.cpp in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; + 37F135641B4AC02800E0CACF /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 37F1356D1B4AC02800E0CACF /* antlrcpp_Tests.mm in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin PBXTargetDependency section */ + 37F135701B4AC02800E0CACF /* PBXTargetDependency */ = { + isa = PBXTargetDependency; + target = 37D727A91867AF1E007B6D10 /* antlrcpp */; + targetProxy = 37F1356F1B4AC02800E0CACF /* PBXContainerItemProxy */; + }; +/* End PBXTargetDependency section */ + +/* Begin XCBuildConfiguration section */ + 37C1471F1B4D5A04008EDDDB /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_ENABLE_MODULES = YES; + CLANG_WARN_UNREACHABLE_CODE = YES; + COMBINE_HIDPI_IMAGES = YES; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + EXECUTABLE_PREFIX = lib; + GCC_ENABLE_CPP_EXCEPTIONS = YES; + GCC_ENABLE_CPP_RTTI = YES; + GCC_NO_COMMON_BLOCKS = YES; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_SYMBOLS_PRIVATE_EXTERN = NO; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + MACOSX_DEPLOYMENT_TARGET = 10.10; + MTL_ENABLE_DEBUG_INFO = YES; + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Debug; + }; + 37C147201B4D5A04008EDDDB /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_ENABLE_MODULES = YES; + CLANG_WARN_UNREACHABLE_CODE = YES; + COMBINE_HIDPI_IMAGES = YES; + COPY_PHASE_STRIP = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + EXECUTABLE_PREFIX = lib; + GCC_ENABLE_CPP_EXCEPTIONS = YES; + GCC_ENABLE_CPP_RTTI = YES; + GCC_NO_COMMON_BLOCKS = YES; + GCC_SYMBOLS_PRIVATE_EXTERN = NO; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + MACOSX_DEPLOYMENT_TARGET = 10.10; + MTL_ENABLE_DEBUG_INFO = NO; + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Release; + }; + 37D727B51867AF1E007B6D10 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_DYNAMIC_NO_PIC = NO; + GCC_ENABLE_OBJC_EXCEPTIONS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_SYMBOLS_PRIVATE_EXTERN = NO; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.9; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = macosx; + }; + name = Debug; + }; + 37D727B61867AF1E007B6D10 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = YES; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_ENABLE_OBJC_EXCEPTIONS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.9; + SDKROOT = macosx; + }; + name = Release; + }; + 37D727B81867AF1E007B6D10 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + COMBINE_HIDPI_IMAGES = YES; + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Debug; + }; + 37D727B91867AF1E007B6D10 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + COMBINE_HIDPI_IMAGES = YES; + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Release; + }; + 37F135711B4AC02800E0CACF /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_ENABLE_MODULES = YES; + CLANG_WARN_UNREACHABLE_CODE = YES; + COMBINE_HIDPI_IMAGES = YES; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + FRAMEWORK_SEARCH_PATHS = ( + "$(DEVELOPER_FRAMEWORKS_DIR)", + "$(inherited)", + ); + GCC_NO_COMMON_BLOCKS = YES; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + INFOPLIST_FILE = "antlrcpp Tests/Info.plist"; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks"; + MACOSX_DEPLOYMENT_TARGET = 10.10; + MTL_ENABLE_DEBUG_INFO = YES; + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Debug; + }; + 37F135721B4AC02800E0CACF /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_ENABLE_MODULES = YES; + CLANG_WARN_UNREACHABLE_CODE = YES; + COMBINE_HIDPI_IMAGES = YES; + COPY_PHASE_STRIP = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + FRAMEWORK_SEARCH_PATHS = ( + "$(DEVELOPER_FRAMEWORKS_DIR)", + "$(inherited)", + ); + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + INFOPLIST_FILE = "antlrcpp Tests/Info.plist"; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks"; + MACOSX_DEPLOYMENT_TARGET = 10.10; + MTL_ENABLE_DEBUG_INFO = NO; + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 37C147211B4D5A04008EDDDB /* Build configuration list for PBXNativeTarget "antlrcpp_static" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 37C1471F1B4D5A04008EDDDB /* Debug */, + 37C147201B4D5A04008EDDDB /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 37D727A51867AF1E007B6D10 /* Build configuration list for PBXProject "antlrcpp" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 37D727B51867AF1E007B6D10 /* Debug */, + 37D727B61867AF1E007B6D10 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 37D727B71867AF1E007B6D10 /* Build configuration list for PBXNativeTarget "antlrcpp" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 37D727B81867AF1E007B6D10 /* Debug */, + 37D727B91867AF1E007B6D10 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 37F135731B4AC02800E0CACF /* Build configuration list for PBXNativeTarget "antlrcpp Tests" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 37F135711B4AC02800E0CACF /* Debug */, + 37F135721B4AC02800E0CACF /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = 37D727A21867AF1E007B6D10 /* Project object */; +} diff --git a/runtime/Cpp/Apple/build.sh b/runtime/Cpp/Apple/build.sh new file mode 100755 index 000000000..ff991f45c --- /dev/null +++ b/runtime/Cpp/Apple/build.sh @@ -0,0 +1,43 @@ +#!/bin/sh +# [The "BSD license"] +# Copyright (c) 2013 Terence Parr +# Copyright (c) 2013 Dan McLaughlin +# All rights reserved. + +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: + +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# 3. The name of the author may not be used to endorse or promote products +# derived from this software without specific prior written permission. + +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + +CURRENT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) +ANTLRCPP_XCODEPROJ="${CURRENT_DIR}/antlrcpp.xcodeproj" + +# OS X +xcrun xcodebuild -project ${ANTLRCPP_XCODEPROJ} -target antlrcpp -configuration Release $@ +xcrun xcodebuild -project ${ANTLRCPP_XCODEPROJ} -target antlrcpp -configuration Debug $@ + +# iOS +#xcrun xcodebuild -project ${ANTLRCPP_XCODEPROJ} -target antlrcpp_iphone -configuration Release -sdk iphoneos $@ +#xcrun xcodebuild -project ${ANTLRCPP_XCODEPROJ} -target antlrcpp_iphone -configuration Debug -sdk iphoneos $@ +#xcrun xcodebuild -project ${ANTLRCPP_XCODEPROJ} -target antlrcpp_iphone_sim -configuration Release -sdk iphonesimulator $@ +#xcrun xcodebuild -project ${ANTLRCPP_XCODEPROJ} -target antlrcpp_iphone_sim -configuration Debug -sdk iphonesimulator $@ + diff --git a/runtime/Cpp/License.txt b/runtime/Cpp/License.txt new file mode 100644 index 000000000..2fdfc6a82 --- /dev/null +++ b/runtime/Cpp/License.txt @@ -0,0 +1,27 @@ + [The "BSD license"] + Copyright (c) 2013 Terence Parr + Copyright (c) 2013 Dan McLaughlin + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/runtime/Cpp/Linux/Makefile b/runtime/Cpp/Linux/Makefile new file mode 100644 index 000000000..d42310b40 --- /dev/null +++ b/runtime/Cpp/Linux/Makefile @@ -0,0 +1,141 @@ +# [The "BSD license"] +# Copyright (c) 2015 Dan McLaughlin +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# 3. The name of the author may not be used to endorse or promote products +# derived from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +LINUXROT:= `pwd` # doh +RUNTIME := ../org/antlr/v4/runtime +OBJDIR := obj + +# Stop on any error or warning, comment out if you want to blast through +NUMERRORS := -Werror + +# Another possibility +# NUMERRORS := -fmax-errors=5 + +CC := ./g++ +CFLAGS := -fPIC -shared -g -O3 -Wall -std=c++11 $(NUMERRORS) +LDFLAGS := +IFLAGS :=-I../antlrcpp -I. -I $(RUNTIME) -I $(RUNTIME)/atn -I $(RUNTIME)/dfa \ + -I $(RUNTIME)/misc -I $(RUNTIME)/tree -I $(RUNTIME)/tree/pattern \ + -I $(RUNTIME)/tree/xpath +NAME := ANTLR4 +MAJOR := 0 +MINOR := 1 +VERSION := $(MAJOR).$(MINOR) + +DFA := $(RUNTIME)/dfa +MISC := $(RUNTIME)/misc +TREE := $(RUNTIME)/tree +TREEPAT := $(TREE)/pattern +XPATH := $(TREE)/xpath + +RUNTIMESRC := $(wildcard $(RUNTIME)/*.cpp) +RUNTIMEOBJ := $(patsubst %.cpp,%.o,$(RUNTIMESRC)) +DFASRC := $(wildcard $(DFA)/*.cpp) +DFAOBJ := $(patsubst %.cpp,%.o,$(DFASRC)) +MISCSRC := $(wildcard $(MISC)/*.cpp) +MISCOBJ := $(patsubst %.cpp,%.o,$(MISCSRC)) +TREESRC := $(wildcard $(TREE)/*.cpp) +TREEOBJ := $(patsubst %.cpp,%.o,$(TREESRC)) +TREEPATSRC := $(wildcard $(TREEPAT)/*.cpp) +TREEPATOBJ := $(patsubst %.cpp,%.o,$(TREEPATSRC)) +XPATHSRC := $(wildcard $(XPATH)/*.cpp) +XPATHOBJ := $(patsubst %.cpp,%.o,$(XPATHSRC)) + +OBJS := $(notdir $(RUNTIMEOBJ) $(DFAOBJ) $(MISCOBJ) $(TREEOBJ) \ + $(TREEPATOBJ) $(XPATHOBJ) ) + +OBJECTS := $(addprefix $(OBJDIR)/,$(OBJS)) + +SOURCES := $(RUNTIMESRC) $(DFASRC) $(MISCSRC) $(TREESRC) $(TREEPATSRC) $(XPATHSRC) + +LIB := lib$(NAME).so.$(VERSION) + +all: $(LIB) + +$(LIB): $(OBJECTS) + $(CC) -shared -Wl,-soname,lib$(NAME).so.$(MAJOR) $^ -o $@ + +# pull in dependency info for *existing* .o files +-include $(OBJECTS:.o=.d) + +# TODO: Is there a way to collapse all these into one line? It doesn't +# work if I do so +$(OBJDIR)/%.o : $(RUNTIME)/%.cpp + @set -e; rm -f $@; \ + $(CC) $(IFLAGS) $(CFLAGS) -o $@ -c $< + $(CC) $(IFLAGS) $(CFLAGS) -o $(subst .o,.d,$@) -MM $< + sed -i '1s/^/obj\//' $(subst .o,.d,$@) +# This is the GNU makefile canonical way of adding the dependency file generation +# to include regenerating when any of the dependencies change. However in our +# solution the dependency is always generated whenever a compilation occurs (as it should) +# so this is un-necessary +# sed -i 's,\($*\)\.o[ :]*,\1.o $(subst .o,.d,$@) : ,g' $(subst .o,.d,$@) + +$(OBJDIR)/%.o : $(DFA)/%.cpp + @set -e; rm -f $@; \ + $(CC) $(IFLAGS) $(CFLAGS) -o $@ -c $< + $(CC) $(IFLAGS) $(CFLAGS) -o $(subst .o,.d,$@) -MM $< + sed -i '1s/^/obj\//' $(subst .o,.d,$@) +# sed -i 's,\($*\)\.o[ :]*,\1.o $(subst .o,.d,$@) : ,g' $(subst .o,.d,$@) + +$(OBJDIR)/%.o : $(MISC)/%.cpp + @set -e; rm -f $@; \ + $(CC) $(IFLAGS) $(CFLAGS) -o $@ -c $< + $(CC) $(IFLAGS) $(CFLAGS) -o $(subst .o,.d,$@) -MM $< + sed -i '1s/^/obj\//' $(subst .o,.d,$@) +# sed -i 's,\($*\)\.o[ :]*,\1.o $(subst .o,.d,$@) : ,g' $(subst .o,.d,$@) + +$(OBJDIR)/%.o : $(TREE)/%.cpp + @set -e; rm -f $@; \ + $(CC) $(IFLAGS) $(CFLAGS) -o $@ -c $< + $(CC) $(IFLAGS) $(CFLAGS) -o $(subst .o,.d,$@) -MM $< + sed -i '1s/^/obj\//' $(subst .o,.d,$@) +# sed -i 's,\($*\)\.o[ :]*,\1.o $(subst .o,.d,$@) : ,g' $(subst .o,.d,$@) + +$(OBJDIR)/%.o : $(TREEPAT)/%.cpp + @set -e; rm -f $@; \ + $(CC) $(IFLAGS) $(CFLAGS) -o $@ -c $< + $(CC) $(IFLAGS) $(CFLAGS) -o $(subst .o,.d,$@) -MM $< + sed -i '1s/^/obj\//' $(subst .o,.d,$@) +# sed -i 's,\($*\)\.o[ :]*,\1.o $(subst .o,.d,$@) : ,g' $(subst .o,.d,$@) + +$(OBJDIR)/%.o : $(XPATH)/%.cpp + @set -e; rm -f $@; \ + $(CC) $(IFLAGS) $(CFLAGS) -o $@ -c $< + $(CC) $(IFLAGS) $(CFLAGS) -o $(subst .o,.d,$@) -MM $< + sed -i '1s/^/obj\//' $(subst .o,.d,$@) +# sed -i 's,\($*\)\.o[ :]*,\1.o $(subst .o,.d,$@) : ,g' $(subst .o,.d,$@) + +$(OBJECTS): | $(OBJDIR)/ + +$(OBJDIR): FORCE + mkdir -p $(OBJDIR) + +FORCE: + +clean: + $(RM) $(NAME)_test *.so* -fr obj diff --git a/runtime/Cpp/Linux/codecvt b/runtime/Cpp/Linux/codecvt new file mode 100644 index 000000000..a17a28234 --- /dev/null +++ b/runtime/Cpp/Linux/codecvt @@ -0,0 +1,2 @@ +#include +#include \ No newline at end of file diff --git a/runtime/Cpp/Linux/color-gcc.pl b/runtime/Cpp/Linux/color-gcc.pl new file mode 100755 index 000000000..3cc789970 --- /dev/null +++ b/runtime/Cpp/Linux/color-gcc.pl @@ -0,0 +1,254 @@ +#! /usr/bin/perl -w + +# +# colorgcc +# +# Version: 1.3.2 +# +# $Id: colorgcc,v 1.10 1999/04/29 17:15:52 jamoyers Exp $ +# +# A wrapper to colorize the output from compilers whose messages +# match the "gcc" format. +# +# Requires the ANSIColor module from CPAN. +# +# Usage: +# +# In a directory that occurs in your PATH _before_ the directory +# where the compiler lives, create a softlink to colorgcc for +# each compiler you want to colorize: +# +# g++ -> colorgcc +# gcc -> colorgcc +# cc -> colorgcc +# etc. +# +# That's it. When "g++" is invoked, colorgcc is run instead. +# colorgcc looks at the program name to figure out which compiler to run. +# +# The default settings can be overridden with ~/.colorgccrc. +# See the comments in the sample .colorgccrc for more information. +# +# Note: +# +# colorgcc will only emit color codes if: +# +# (1) Its STDOUT is a tty and +# (2) the value of $TERM is not listed in the "nocolor" option. +# +# If colorgcc colorizes the output, the compiler's STDERR will be +# combined with STDOUT. Otherwise, colorgcc just passes the output from +# the compiler through without modification. +# +# Author: Jamie Moyers +# Started: April 20, 1999 +# Licence: GNU Public License +# +# Credits: +# +# I got the idea for this from a script called "color_cvs": +# color_cvs .03 Adrian Likins +# +# (Scott Harrington) +# Much improved handling of compiler command line arguments. +# exec compiler when not colorizing to preserve STDOUT, STDERR. +# Fixed my STDIN kludge. +# +# (Elias S. G. Carotti) +# Corrected handling of text like -DPACKAGE=\"Package\" +# Spotted return code bug. +# +# (Erwin S. Andreasen) +# (Steve Churchill) +# Return code bug fixes. +# +# (Rik Hemsley) +# Found STDIN bug. +# +# Changes: +# +# 1.3.2 Better handling of command line arguments to compiler. +# +# If we aren't colorizing output, we just exec the compiler which +# preserves the original STDOUT and STDERR. +# +# Removed STDIN kludge. STDIN being passed correctly now. +# +# 1.3.1 Added kludge to copy STDIN to the compiler's STDIN. +# +# 1.3.0 Now correctly returns (I hope) the return code of the compiler +# process as its own. +# +# 1.2.1 Applied patch to handle text similar to -DPACKAGE=\"Package\". +# +# 1.2.0 Added tty check. If STDOUT is not a tty, don't do color. +# +# 1.1.0 Added the "nocolor" option to turn off the color if the terminal type +# ($TERM) is listed. +# +# 1.0.0 Initial Version + +use Term::ANSIColor; +use IPC::Open3; + +sub initDefaults +{ + $compilerPaths{"gcc"} = "/usr/bin/gcc"; + $compilerPaths{"g++"} = "/usr/bin/g++"; + $compilerPaths{"cc"} = "/usr/bin/cc"; + $compilerPaths{"c++"} = "/usr/bin/c++"; + + $nocolor{"dumb"} = "true"; + + $colors{"srcColor"} = color("cyan"); + $colors{"introColor"} = color("blue"); + + $colors{"warningFileNameColor"} = color("yellow"); + $colors{"warningNumberColor"} = color("yellow"); + $colors{"warningMessageColor"} = color("yellow"); + + $colors{"errorFileNameColor"} = color("bold red"); + $colors{"errorNumberColor"} = color("bold red"); + $colors{"errorMessageColor"} = color("bold red"); +} + +sub loadPreferences +{ +# Usage: loadPreferences("filename"); + + my($filename) = @_; + + open(PREFS, "<$filename") || return; + + while() + { + next if (m/^\#.*/); # It's a comment. + next if (!m/(.*):\s*(.*)/); # It's not of the form "foo: bar". + + $option = $1; + $value = $2; + + if ($option =~ m/cc|c\+\+|gcc|g\+\+/) + { + $compilerPaths{$option} = $value; + } + elsif ($option eq "nocolor") + { + # The nocolor option lists terminal types, separated by + # spaces, not to do color on. + foreach $termtype (split(/\s+/, $value)) + { + $nocolor{$termtype} = "true"; + } + } + else + { + $colors{$option} = color($value); + } + } + close(PREFS); +} + +sub srcscan +{ +# Usage: srcscan($text, $normalColor) +# $text -- the text to colorize +# $normalColor -- The escape sequence to use for non-source text. + +# Looks for text between ` and ', and colors it srcColor. + + my($line, $normalColor) = @_; + + my($srcon) = color("reset") . $colors{"srcColor"}; + my($srcoff) = color("reset") . $normalColor; + + $line = $normalColor . $line; + + # This substitute replaces `foo' with `AfooB' where A is the escape + # sequence that turns on the the desired source color, and B is the + # escape sequence that returns to $normalColor. + $line =~ s/\`(.*?)\'/\`$srcon$1$srcoff\'/g; + + print($line, color("reset")); +} + +# +# Main program +# + +# Set up default values for colors and compilers. +initDefaults(); + +# Read the configuration file, if there is one. +$configFile = $ENV{"HOME"} . "/.colorgccrc"; +if (-f $configFile) +{ + loadPreferences($configFile); +} + +# Figure out which compiler to invoke based on our program name. +$0 =~ m%.*/(.*)$%; +$progName = $1 || $0; + +$compiler = $compilerPaths{$progName} || $compilerPaths{"gcc"}; + +# Get the terminal type. +$terminal = $ENV{"TERM"} || "dumb"; + +# If it's in the list of terminal types not to color, or if +# we're writing to something that's not a tty, don't do color. +if (! -t STDOUT || $nocolor{$terminal}) +{ + exec $compiler, @ARGV + or die("Couldn't exec"); +} + +# Keep the pid of the compiler process so we can get its return +# code and use that as our return code. +$compiler_pid = open3('<&STDIN', \*GCCOUT, '', $compiler, @ARGV); + +# Colorize the output from the compiler. +while() +{ + if (m/^(.*?):([0-9]+):(.*)$/) # filename:lineno:message + { + $field1 = $1 || ""; + $field2 = $2 || ""; + $field3 = $3 || ""; + + if ($field3 =~ m/\s+warning:.*/) + { + # Warning + print($colors{"warningFileNameColor"}, "$field1:", color("reset")); + print($colors{"warningNumberColor"}, "$field2:", color("reset")); + srcscan($field3, $colors{"warningMessageColor"}); + } + else + { + # Error + print($colors{"errorFileNameColor"}, "$field1:", color("reset")); + print($colors{"errorNumberColor"}, "$field2:", color("reset")); + srcscan($field3, $colors{"errorMessageColor"}); + } + print("\n"); + } + elsif (m/^(.*?):(.+):$/) # filename:message: + { + # No line number, treat as an "introductory" line of text. + srcscan($_, $colors{"introColor"}); + } + else # Anything else. + { + # Doesn't seem to be a warning or an error. Print normally. + print(color("reset"), $_); + } +} + +# Get the return code of the compiler and exit with that. +waitpid($compiler_pid, 0); +exit ($? >> 8); + + + + + diff --git a/runtime/Cpp/Linux/g++ b/runtime/Cpp/Linux/g++ new file mode 120000 index 000000000..744feee96 --- /dev/null +++ b/runtime/Cpp/Linux/g++ @@ -0,0 +1 @@ +color-gcc.pl \ No newline at end of file diff --git a/runtime/Cpp/README.md b/runtime/Cpp/README.md new file mode 100644 index 000000000..c5e43b314 --- /dev/null +++ b/runtime/Cpp/README.md @@ -0,0 +1,34 @@ +# C++ target for ANTLR 4 + +This fork provides C++ runtime support for C++. See [the canonical antlr4 repository](https://github.com/antlr/antlr4) for in depth detail about how to use Antlr4. + +## Authors and major contributors + +ANTLR 4 is the result of substantial effort of the following people: + +* [Terence Parr](http://www.cs.usfca.edu/~parrt/), parrt@cs.usfca.edu + ANTLR project lead and supreme dictator for life + [University of San Francisco](http://www.usfca.edu/) + +* [Sam Harwell] (http://tunnelvisionlabs.com/) + Tool co-author, Java and C# target) + +The C++ target has been the work of the following people: + +* Dan McLaughlin, dan.mclaughlin@gmail.com + C++ Target project leader + +* David Sisson, dsisson@google.com + + +Project Status +-------------------------------------------------------------------------------- + + o Building on OS X, Windows, and Linux (Ubuntu) + o No errors and warnings + o Library linking + o Simple running (e.g. heap objects initialize properly) + o Simple testing (creating significiant objects) + + + \ No newline at end of file diff --git a/runtime/Cpp/Windows/antlr4cpp.sln b/runtime/Cpp/Windows/antlr4cpp.sln new file mode 100755 index 000000000..c5bde6806 --- /dev/null +++ b/runtime/Cpp/Windows/antlr4cpp.sln @@ -0,0 +1,20 @@ + +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual Studio 2010 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "antlr4cpp", "antlr4cpp.vcxproj", "{A9762991-1B57-4DCE-90C0-EE42B96947BE}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Release|Win32 = Release|Win32 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {A9762991-1B57-4DCE-90C0-EE42B96947BE}.Debug|Win32.ActiveCfg = Debug|Win32 + {A9762991-1B57-4DCE-90C0-EE42B96947BE}.Debug|Win32.Build.0 = Debug|Win32 + {A9762991-1B57-4DCE-90C0-EE42B96947BE}.Release|Win32.ActiveCfg = Release|Win32 + {A9762991-1B57-4DCE-90C0-EE42B96947BE}.Release|Win32.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/runtime/Cpp/Windows/antlr4cpp.suo b/runtime/Cpp/Windows/antlr4cpp.suo new file mode 100755 index 000000000..e59afd1ac Binary files /dev/null and b/runtime/Cpp/Windows/antlr4cpp.suo differ diff --git a/runtime/Cpp/Windows/antlr4cpp.vcxproj b/runtime/Cpp/Windows/antlr4cpp.vcxproj new file mode 100755 index 000000000..decc326c9 --- /dev/null +++ b/runtime/Cpp/Windows/antlr4cpp.vcxproj @@ -0,0 +1,374 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {A9762991-1B57-4DCE-90C0-EE42B96947BE} + Win32Proj + antlr4cpp + + + + DynamicLibrary + true + Unicode + v120 + + + DynamicLibrary + false + true + Unicode + v120 + + + + + + + + + + + + + true + + + false + + + + + + Level3 + Disabled + WIN32;_DEBUG;_WINDOWS;_USRDLL;ANTLR4CPP_EXPORTS;%(PreprocessorDefinitions);WINDOWS + $(ProjectDir)\..\antlrcpp;$(ProjectDir)\..\org\antlr\v4\runtime;$(ProjectDir)\..\org\antlr\v4\runtime\atn;$(ProjectDir)\..\org\antlr\v4\runtime\dfa;$(ProjectDir)\..\org\antlr\v4\runtime\misc;$(ProjectDir)\..\org\antlr\v4\runtime\tree;$(ProjectDir)\..\org\antlr\v4\runtime\tree\xpath;$(ProjectDir)\..\org\antlr\v4\runtime\tree\pattern;%(AdditionalIncludeDirectories) + false + + + Windows + true + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_WINDOWS;_USRDLL;ANTLR4CPP_EXPORTS;%(PreprocessorDefinitions) + + + Windows + true + true + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/runtime/Cpp/Windows/antlr4cpp.vcxproj.filters b/runtime/Cpp/Windows/antlr4cpp.vcxproj.filters new file mode 100755 index 000000000..2f41b0d1e --- /dev/null +++ b/runtime/Cpp/Windows/antlr4cpp.vcxproj.filters @@ -0,0 +1,876 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Header Files + + + Source Files + + + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + + + + \ No newline at end of file diff --git a/runtime/Cpp/Windows/antlr4cpp.vcxproj.user b/runtime/Cpp/Windows/antlr4cpp.vcxproj.user new file mode 100755 index 000000000..3f0309112 --- /dev/null +++ b/runtime/Cpp/Windows/antlr4cpp.vcxproj.user @@ -0,0 +1,6 @@ + + + + false + + \ No newline at end of file diff --git a/runtime/Cpp/antlrcpp/Arrays.cpp b/runtime/Cpp/antlrcpp/Arrays.cpp new file mode 100644 index 000000000..a7e66620f --- /dev/null +++ b/runtime/Cpp/antlrcpp/Arrays.cpp @@ -0,0 +1,122 @@ +#include "Arrays.h" +#include "Exceptions.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +std::wstring antlrcpp::Arrays::ListToString(std::vector list,std::wstring separator) +{ + + StringBuilder *sb = new StringBuilder(); + for (size_t i = 0; i < list.size(); i++)//(std::wstring s in list) + { + sb->append(list[i]); + if (i + 1 < list.size()) sb->append(separator); + } + + return sb->toString(); +} + +std::vector antlrcpp::Arrays::asList(const std::wstring *items) +{ + std::vector returnAnswer(items->begin(), items->end()); + + return returnAnswer; +} + +std::vector antlrcpp::Arrays::asList(int nArgs, ...) +{ + + std::vector returnAnswer; + va_list ap; + va_start(ap, nArgs); + + for (int i = 0; i < nArgs; i++) { + wchar_t * tmp = va_arg(ap, wchar_t*); + returnAnswer.insert(returnAnswer.end(), tmp); + } + + return returnAnswer; +} + +bool antlrcpp::Arrays::equals(std::vector a, std::vector b) +{ + + if (a.size() != b.size()) return false; + + for (auto var: a) { + if (a[var] != b[var]) return false; + } + + return true; +} + +bool antlrcpp::Arrays::equals(void *a, void* b) +{ + throw new org::antlr::v4::runtime::TODOException(L"antlrcpp::Arrays::equals"); + return false; +} + +std::list antlrcpp::Arrays::copyOf(void * obj, int num) +{ + std::list returnAnswer; + // What ? + throw new org::antlr::v4::runtime::TODOException(L"antlrcpp::Arrays::copyOf(void*,int)"); + + return returnAnswer; +} + +std::wstring antlrcpp::Arrays::copyOf(std::wstring obj, int num) +{ + std::wstring foo; + throw new org::antlr::v4::runtime::TODOException(L"antlrcpp::Arrays::copyOf(wstring, int)"); + return foo; +} + +void antlrcpp::Arrays::arraycopy(void * arrayA, void * arrayB, int num) +{ + throw new org::antlr::v4::runtime::TODOException(L"antlrcpp::Arrays::arraycopy"); +} + +std::list antlrcpp::Arrays::StringToStringList(std::wstring items, char separator) +{ + /* + std::list *list = new std::list(); + std::wstring listItmes[] = items.Split(separator); + for (std::wstring item : listItmes) + { + list.Add(item); + } + if (list.Count > 0) { + return list; + } else { + return null; + }*/ + throw new org::antlr::v4::runtime::TODOException(L"antlrcpp::Arrays::StringToStringList"); +} diff --git a/runtime/Cpp/antlrcpp/Arrays.h b/runtime/Cpp/antlrcpp/Arrays.h new file mode 100644 index 000000000..8445d0b69 --- /dev/null +++ b/runtime/Cpp/antlrcpp/Arrays.h @@ -0,0 +1,87 @@ +#pragma once + +#include +#include +#include +#include +#include + +#include "Declarations.h" +#include "StringBuilder.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + +namespace antlrcpp { + /// + /// Converts List to string with given separator. + /// + /// The list. + /// The separator. + /// + class Arrays { + public: + + static std::wstring ListToString(std::vector list,std::wstring separator); + + /// + /// Strings to string list. + /// + /// The items. + /// The separator. + /// + static std::list StringToStringList(std::wstring items, char separator); + + + static std::vector asList(const std::wstring *items); + + // Arguments must be of type wchar_t! + static std::vector asList(int nArgs, ...); + + static bool equals(std::vector a, std::vector b); + + + static bool equals(void *a, void* b); + + /// + /// Strings to string list. + /// + /// The items. + /// The separator. + /// + + static std::list copyOf(void * obj, int num); + + static std::wstring copyOf(std::wstring obj, int num); + + static void arraycopy(void * arrayA, void * arrayB, int num); + }; + +} \ No newline at end of file diff --git a/runtime/Cpp/antlrcpp/BitSet.h b/runtime/Cpp/antlrcpp/BitSet.h new file mode 100644 index 000000000..6c423d3cd --- /dev/null +++ b/runtime/Cpp/antlrcpp/BitSet.h @@ -0,0 +1,131 @@ +#pragma once + +#include +#include +#include +#include + +/* + * [The "BSD license"] + * Copyright (c) 2015 Ana Maria Rodriguez Reyes + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace antlrcpp { + + class BitSet { + + public: + BitSet() {} + + BitSet(const BitSet &other) { + data = other.data; + } + + static const int BITSET_SIZE = 1024; + std::bitset data; + + void assign(size_t count, const BitSet & value ) { + + } + + int nextSetBit(const int & pos) { + for (size_t i = pos; i < data.size(); i++){ + if (data.test(i)) return (int)i; + } + + return -1; + } + + void set(size_t pos){ + data.set(pos); + } + + void set(){ + data.set(); + } + + size_t count(){ + return data.count(); + } + + size_t size(){ + return data.size(); + } + + // Prints a list of every index for which the bitset contains a bit in true. + friend std::wostream& operator<<(std::wostream& os, const BitSet& obj) + { + + os << L"{"; + size_t total = obj.data.count(); + for (size_t i = 0; i < obj.data.size(); i++){ + if (obj.data.test(i)){ + os << i; + --total; + if (total > 1){ + os << L", "; + } + } + } + + os << L"}"; + return os; + } + + static std::wstring subStringRepresentation(const std::vector::iterator &begin, + const std::vector::iterator &end) { + std::wstring result; + std::vector::iterator vectorIterator; + + for (vectorIterator = begin; vectorIterator != end; vectorIterator++) { + result += vectorIterator->toString(); + } + // Grab the end + result += end->toString(); + + return result; + } + std::wstring toString(){ + std::wstringstream stream; + stream << L"{"; + size_t total = data.count(); + for (size_t i = 0; i < data.size(); i++){ + if (data.test(i)){ + stream << i; + --total; + if (total > 1){ + stream << L", "; + } + } + } + + stream << L"}"; + return stream.str(); + } + + }; +} diff --git a/runtime/Cpp/antlrcpp/CPPUtils.cpp b/runtime/Cpp/antlrcpp/CPPUtils.cpp new file mode 100755 index 000000000..da16f51d7 --- /dev/null +++ b/runtime/Cpp/antlrcpp/CPPUtils.cpp @@ -0,0 +1,99 @@ +#include "CPPUtils.h" +#include // for va_start, etc +#include // for std::unique_ptr +#include +#include + +/* + * [The "BSD license"] + * Copyright (c) 2014 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + +namespace antlrcpp { + std::wstring join(std::vector strings, const std::wstring &separator) { + std::wstring str; + bool firstItem = true; + for (std::wstring s : strings) { + if (!firstItem) { + str.append(separator); + } else { + firstItem = false; + } + str.append(s); + } + return str; + } + + std::map* toMap(const std::vector &keys) { + std::map* m = new std::map(); + for (int i = 0; i < (int)keys.size(); ++i) { + m->insert(std::pair(keys[i], i)); + } + return m; + } + + std::wstring escapeWhitespace(std::wstring str, bool TODO) { + // TODO - David, what is this boolean for, and what did you want to esacpe + // whitespace with? + std::wstring returnAnswer = str.replace(str.begin(), str.end(), L' ', L'\\'); + return returnAnswer; + } + + std::wstring stringFormat(const std::wstring fmt_str, ...) + { + // Not sure this is needed, just use swprintf (into a wchar_t array). + // TODO(dsisson): Remove this function in a future change. + std::wstring blank; + return blank; + } + + wchar_t* toCharArray(const std::vector *data){ + if (data == nullptr) return nullptr; + wchar_t* cdata = new wchar_t[data->size()]; + + for (int i = 0; i < (int)data->size(); i++){ + cdata[i] = (char)data->at(i); + } + + return cdata; + } + + std::wstring toHexString(const int t){ + std::wstringstream stream; + stream << std::uppercase << std::hex << t; + return stream.str(); + } + + std::wstring arrayToString(const std::vector &data) { + std::wstring answer; + for (auto sub: data) { + answer += sub; + } + return answer; + } +} \ No newline at end of file diff --git a/runtime/Cpp/antlrcpp/CPPUtils.h b/runtime/Cpp/antlrcpp/CPPUtils.h new file mode 100644 index 000000000..378c3c004 --- /dev/null +++ b/runtime/Cpp/antlrcpp/CPPUtils.h @@ -0,0 +1,63 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +/* + * [The "BSD license"] + * Copyright (c) 2014 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + +namespace antlrcpp { + template + class CopyOnWriteArrayList { + public: + CopyOnWriteArrayList(){}; + + }; + + std::wstring join(std::vector strings, const std::wstring &separator); + + std::map* toMap(const std::vector &keys); + + std::wstring escapeWhitespace(std::wstring str, bool TODO); + + std::wstring stringFormat(const wchar_t* fmt, ...); + + wchar_t* toCharArray(const std::vector *data); + + std::wstring toHexString(const int t); + + std::wstring arrayToString(const std::vector &data); + +} diff --git a/runtime/Cpp/antlrcpp/Declarations.h b/runtime/Cpp/antlrcpp/Declarations.h new file mode 100644 index 000000000..bddbe7078 --- /dev/null +++ b/runtime/Cpp/antlrcpp/Declarations.h @@ -0,0 +1,214 @@ +#pragma once + +/* + * [The "BSD license"] + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +// This can be regenerated from the command line by using +// ls *.h | sed "s/\.h\*/;/" | sed 's/^/class /' + +// Autogenerated +class XPathLexer; +class XPathParser; + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + class IllegalStateException; + class IllegalArgumentException; + class NoSuchElementException; + class NullPointerException; + class InputMismatchException; + class ParseCancellationException; + class InputMismatchException; + class EmptyStackException; + class LexerNoViableAltException; + + class ANTLRErrorListener; + class ANTLRErrorStrategy; + class ANTLRFileStream; + class ANTLRInputStream; + class BailErrorStrategy; + class BaseErrorListener; + class BufferedTokenStream; + class CharStream; + class CommonToken; + class CommonTokenFactory; + class CommonTokenStream; + class ConsoleErrorListener; + class DefaultErrorStrategy; + class DiagnosticErrorListener; + class FailedPredicateException; + class InputMismatchException; + class IntStream; + class InterpreterRuleContext; + class IErrorListener; + template class IRecognizer; + class Lexer; + class LexerInterpreter; + class LexerNoViableAltException; + class ListTokenSource; + class NoViableAltException; + class Parser; + class ParserInterpreter; + class ParserRuleContext; + class ProxyErrorListener; + class RecognitionException; + template class Recognizer; + class RuleContext; + class Token; + template class TokenFactory; + class TokenSource; + class TokenStream; + class TokenStreamRewriter; + class UnbufferedCharStream; + template class UnbufferedTokenStream; + class WritableToken; + + namespace misc { + template class AbstractEqualityComparator; + template class Array2DHashSet; + template class DoubleKeyMap; + template class EqualityComparator; + + class GraphicsSupport; + class IntSet; + class Interval; + class IntervalSet; + class JFileChooserConfirmOverwrite; + class LogManager; + template class MultiMap; + class MurmurHash; + class NotNull; + class ObjectEqualityComparator; + template class OrderedHashSet; + class ParseCancellationException; + class TestRig; + class Utils; + } + namespace atn { + class ATN; + class ATNConfig; + class ATNConfigSet; + class ATNDeserializationOptions; + class ATNDeserializer; + class ATNSerializer; + class ATNSimulator; + class ATNState; + enum class ATNType; + class AbstractPredicateTransition; + class ActionTransition; + class ArrayPredictionContext; + class AtomTransition; + class BasicBlockStartState; + class BasicState; + class BlockEndState; + class BlockStartState; + class DecisionState; + class EmptyPredictionContext; + class EpsilonTransition; + class LL1Analyzer; + class LexerATNConfig; + class LexerATNSimulator; + class LoopEndState; + class NotSetTransition; + class OrderedATNConfigSet; + class ParserATNSimulator; + class PlusBlockStartState; + class PlusLoopbackState; + class PrecedencePredicateTransition; + class PredicateTransition; + class PredictionContext; + class PredictionContextCache; + enum class PredictionMode; + class PredictionModeClass; + class RangeTransition; + class RuleStartState; + class RuleStopState; + class RuleTransition; + class SemanticContext; + class SetTransition; + class SingletonPredictionContext; + class StarBlockStartState; + class StarLoopEntryState; + class StarLoopbackState; + class TokensStartState; + class Transition; + class WildcardTransition; + } + namespace dfa { + class DFA; + class DFASerializer; + class DFAState; + class LexerDFASerializer; + } + namespace tree { + template class AbstractParseTreeVisitor; + class ErrorNode; + class ErrorNodeImpl; + class ParseTree; + class ParseTreeListener; + template class ParseTreeProperty; + template class ParseTreeVisitor; + class ParseTreeWalker; + class RuleNode; + class SyntaxTree; + class TerminalNode; + class TerminalNodeImpl; + class Tree; + class Trees; + + namespace pattern { + class Chunk; + class ParseTreeMatch; + class ParseTreePattern; + class ParseTreePatternMatcher; + class RuleTagToken; + class TagChunk; + class TextChunk; + class TokenTagToken; + } + + namespace xpath { + class XPath; + class XPathElement; + class XPathLexerErrorListener; + class XPathRuleAnywhereElement; + class XPathRuleElement; + class XPathTokenAnywhereElement; + class XPathTokenElement; + class XPathWildcardAnywhereElement; + class XPathWildcardElement; + + } + } + } + } + } +} diff --git a/runtime/Cpp/antlrcpp/StringBuilder.cpp b/runtime/Cpp/antlrcpp/StringBuilder.cpp new file mode 100755 index 000000000..05579fcd2 --- /dev/null +++ b/runtime/Cpp/antlrcpp/StringBuilder.cpp @@ -0,0 +1,149 @@ +#include +#include +#include + +#include "StringBuilder.h" +#include "ATNState.h" +#include "SemanticContext.h" +#include "Strings.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + +namespace antlrcpp { + StringBuilder & StringBuilder::append(const std::wstring & str) { + scratch.append(str); + if (scratch.size() > ScratchSize) { + main.append(scratch); + scratch.resize(0); + } + return *this; + } + + + StringBuilder & StringBuilder::append(const org::antlr::v4::runtime::atn::ATNState* const state) { + main = main + state->toString(); + return *this; + } + + StringBuilder & StringBuilder::append(const org::antlr::v4::runtime::atn::SemanticContext* const state) { + main = main + state->toString(); + return *this; + } + + StringBuilder & StringBuilder::append(const wchar_t & str) { + scratch.append(wchar2wstring(str)); + if (scratch.size() > ScratchSize) { + main.append(scratch); + scratch.resize(0); + } + + return *this; + } + + const std::wstring & StringBuilder::str() { + if (scratch.size() > 0) { + main.append(scratch); + scratch.resize(0); + } + return main; + } + + std::wstring StringBuilder::format(const std::string fmt_str, ...) { + int final_n, n = (int)fmt_str.size() * 2; // reserve 2 times as much as the length of the fmt_str + std::string str; + std::unique_ptr formatted; + va_list ap; + while(1) { + formatted.reset(new char[n]); // wrap the plain char array into the unique_ptr + strcpy(&formatted[0], fmt_str.c_str()); + va_start(ap, fmt_str); + final_n = vsnprintf(&formatted[0], n, fmt_str.c_str(), ap); + va_end(ap); + if (final_n < 0 || final_n >= n) + n += abs(final_n - n + 1); + else + break; + } + + return std::wstring(s2ws(formatted.get())); + } + + const std::wstring & StringBuilder::toString() { + return str(); + } + + std::wstring::size_type StringBuilder::length() { + return str().size(); + } + + std::wstring StringBuilder::stringToWstring( std::string str ) + { + std::wstring wStr( str.length(), L' ' ); + std::copy( str.begin(), str.end(), wStr.begin() ); + return wStr; + } + + std::string StringBuilder::WstringTostring( std::wstring wStr ) + { + std::string str( wStr.length(), ' ' ); + std::copy( wStr.begin(), wStr.end(), str.begin() ); + return str; + } + + std::string StringBuilder::string_replace(std::string src, std::string const& target, std::string const& repl) + { + // handle error situations/trivial cases + + if (target.length() == 0) { + // searching for a match to the empty string will result in + // an infinite loop + // it might make sense to throw an exception for this case + return src; + } + + if (src.length() == 0) { + return src; // nothing to match against + } + + size_t idx = 0; + + for (;;) { + idx = src.find( target, idx); + if (idx == std::string::npos) break; + + src.replace( idx, target.length(), repl); + idx += repl.length(); + } + + return src; + } + +} \ No newline at end of file diff --git a/runtime/Cpp/antlrcpp/StringBuilder.h b/runtime/Cpp/antlrcpp/StringBuilder.h new file mode 100755 index 000000000..6e921a211 --- /dev/null +++ b/runtime/Cpp/antlrcpp/StringBuilder.h @@ -0,0 +1,72 @@ +#pragma once + +#include "Declarations.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace antlrcpp { + + class StringBuilder { + private: + std::wstring main; + std::wstring scratch; + + const std::wstring::size_type ScratchSize = 1024; // or some other arbitrary number + + public: + StringBuilder & append(const std::wstring & str); + + StringBuilder & append(const org::antlr::v4::runtime::atn::ATNState* const state); + + StringBuilder & append(const org::antlr::v4::runtime::atn::SemanticContext* const state); + + StringBuilder & append(const wchar_t & str); + + const std::wstring & str(); + + const std::wstring & toString(); + + std::wstring::size_type length(); + + // Utility function to provide "format" capability + + static std::wstring format(const std::string fmt_str, ...); + + static std::wstring stringToWstring( std::string str ); + + static std::string WstringTostring( std::wstring wStr ); + + static std::string string_replace(std::string src, + std::string const& target, + std::string const& repl); + }; + +} + diff --git a/runtime/Cpp/antlrcpp/Strings.cpp b/runtime/Cpp/antlrcpp/Strings.cpp new file mode 100644 index 000000000..60d89fc5d --- /dev/null +++ b/runtime/Cpp/antlrcpp/Strings.cpp @@ -0,0 +1,68 @@ +#include +#include +#include + +#include "Strings.h" + +/* + * [The "BSD license"] + * Copyright (c) 2014 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace antlrcpp { + std::string ws2s(const std::wstring& wstr) { + std::wstring_convert> converter; + std::string narrow = converter.to_bytes(wstr); + return narrow; + } + + std::wstring s2ws(const std::string& str) { + std::wstring_convert> converter; + std::wstring wide = converter.from_bytes(str); + return wide; + } + + void replaceAll(std::wstring& str, const std::wstring& from, const std::wstring& to) + { + if(from.empty()) { + return; + } + size_t start_pos = 0; + while((start_pos = str.find(from, start_pos)) != std::string::npos) { + str.replace(start_pos, from.length(), to); + start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx' + } + } + + + std::wstring wchar2wstring(const wchar_t & str) { + std::wstring returnAnswer(&str); + + return returnAnswer; + } + +} \ No newline at end of file diff --git a/runtime/Cpp/antlrcpp/Strings.h b/runtime/Cpp/antlrcpp/Strings.h new file mode 100644 index 000000000..ae42de33d --- /dev/null +++ b/runtime/Cpp/antlrcpp/Strings.h @@ -0,0 +1,43 @@ +#pragma once + +/* + * [The "BSD license"] + * Copyright (c) 2014 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include + +namespace antlrcpp { + void replaceAll(std::wstring& str, const std::wstring& from, const std::wstring& to); + + std::string ws2s(const std::wstring& wstr); + + std::wstring s2ws(const std::string & str); + + std::wstring wchar2wstring(const wchar_t & str); + +} \ No newline at end of file diff --git a/runtime/Cpp/antlrcpp/UUID.cpp b/runtime/Cpp/antlrcpp/UUID.cpp new file mode 100755 index 000000000..9c0fd46da --- /dev/null +++ b/runtime/Cpp/antlrcpp/UUID.cpp @@ -0,0 +1,83 @@ +/* + * [The "BSD license"] + * Copyright (c) 2015 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "UUID.h" + +// Terence thinks this is for some version control stuff or something +// or other - probably not important. So be nice in this function, be quiet and +// hope nobody complains + +namespace antlrcpp { + UUID::UUID(long long mostSigBits, long long leastSigBits) { +// throw new std::exception(); + } + UUID::UUID(const UUID &other) { +// throw new std::exception(); + }; + + UUID::~UUID() { +// throw new std::exception(); + }; + + const UUID &UUID::operator=(const UUID &rhs) { +// throw new std::exception(); + return rhs; + }; + bool UUID::operator==(const UUID &rhs) const { +// throw new std::exception(); + return true; + }; + bool UUID::equals(const UUID &rhs) const { +// throw new std::exception(); + return true; + }; + bool UUID::equals(const UUID *rhs) const { +// throw new std::exception(); + return true; + }; + + UUID *UUID::fromString(const std::wstring &name) { +// throw new std::exception(); + return new UUID(0,0); + }; + + std::wstring UUID::toString() const { +// throw new std::exception(); + return L"UUID_UNIMPLEMENTED"; + }; + + long UUID::getLeastSignificantBits() const { +// throw new std::exception(); + return 0; + }; + long UUID::getMostSignificantBits() const { +// throw new std::exception(); + return 0; + }; +} \ No newline at end of file diff --git a/runtime/Cpp/antlrcpp/UUID.h b/runtime/Cpp/antlrcpp/UUID.h new file mode 100755 index 000000000..1fac7820e --- /dev/null +++ b/runtime/Cpp/antlrcpp/UUID.h @@ -0,0 +1,54 @@ + +#pragma once + +#include + +/* + * [The "BSD license"] + * Copyright (c) 2015 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace antlrcpp { + class UUID { + public: + UUID(long long mostSigBits, long long leastSigBits); + UUID(const UUID &other); + ~UUID(); + + const UUID &operator=(const UUID &rhs); + bool operator==(const UUID &rhs) const; + bool equals(const UUID &rhs) const; + bool equals(const UUID *rhs) const; + + static UUID *fromString(const std::wstring &name); + std::wstring toString() const; + + long getLeastSignificantBits() const; + long getMostSignificantBits() const; + }; + +} \ No newline at end of file diff --git a/runtime/Cpp/antlrcpp/antlrcpp-Prefix.pch b/runtime/Cpp/antlrcpp/antlrcpp-Prefix.pch new file mode 100644 index 000000000..b300bc295 --- /dev/null +++ b/runtime/Cpp/antlrcpp/antlrcpp-Prefix.pch @@ -0,0 +1,8 @@ +// +// Prefix header +// +// The contents of this file are implicitly included at the beginning of every source file. +// + +#include + diff --git a/runtime/Cpp/antlrcpp/antlrcpp.cp b/runtime/Cpp/antlrcpp/antlrcpp.cp new file mode 100644 index 000000000..b4a2cae5a --- /dev/null +++ b/runtime/Cpp/antlrcpp/antlrcpp.cp @@ -0,0 +1,14 @@ +/* + * antlrcpp.cp + * antlrcpp + * + * Created by Dan McLaughlin on 12/22/13. + * Copyright (c) 2013 Dan McLaughlin. All rights reserved. + * + */ + +#include +#include "antlrcpp.h" +#include "antlrcppPriv.h" + + diff --git a/runtime/Cpp/antlrcpp/antlrcpp.h b/runtime/Cpp/antlrcpp/antlrcpp.h new file mode 100644 index 000000000..7b916307d --- /dev/null +++ b/runtime/Cpp/antlrcpp/antlrcpp.h @@ -0,0 +1,19 @@ +/* + * antlrcpp.h + * antlrcpp + * + * Created by Dan McLaughlin on 12/22/13. + * Copyright (c) 2013 Dan McLaughlin. All rights reserved. + * + */ + +#ifndef antlrcpp_ +#define antlrcpp_ + +/* The classes below are exported */ +#pragma GCC visibility push(default) + + + +#pragma GCC visibility pop +#endif diff --git a/runtime/Cpp/antlrcpp/antlrcppPriv.h b/runtime/Cpp/antlrcpp/antlrcppPriv.h new file mode 100644 index 000000000..58fbb0455 --- /dev/null +++ b/runtime/Cpp/antlrcpp/antlrcppPriv.h @@ -0,0 +1,19 @@ +/* + * antlrcppPriv.h + * antlrcpp + * + * Created by Dan McLaughlin on 12/22/13. + * Copyright (c) 2013 Dan McLaughlin. All rights reserved. + * + */ + +/* The classes below are not exported */ +#pragma GCC visibility push(hidden) + +class antlrcppPriv +{ + public: + void HelloWorldPriv(const char *); +}; + +#pragma GCC visibility pop diff --git a/runtime/Cpp/antlrcpp/antlrcppProj.xcconfig b/runtime/Cpp/antlrcpp/antlrcppProj.xcconfig new file mode 100644 index 000000000..959fcc207 --- /dev/null +++ b/runtime/Cpp/antlrcpp/antlrcppProj.xcconfig @@ -0,0 +1,4 @@ +GCC_WARN_ABOUT_RETURN_TYPE = YES; +GCC_WARN_UNUSED_VARIABLE = YES; +GCC_INCREASE_PRECOMPILED_HEADER_SHARING = YES; +GCC_GENERATE_DEBUGGING_SYMBOLS = YES; diff --git a/runtime/Cpp/antlrcpp/antlrcppTarget.xcconfig b/runtime/Cpp/antlrcpp/antlrcppTarget.xcconfig new file mode 100644 index 000000000..7dbb19909 --- /dev/null +++ b/runtime/Cpp/antlrcpp/antlrcppTarget.xcconfig @@ -0,0 +1,10 @@ +PRODUCT_NAME = libantlrcpp; +MACH_O_TYPE = mh_dylib; +DYLIB_COMPATIBILITY_VERSION = 1; +DYLIB_CURRENT_VERSION = 1; +GCC_PRECOMPILE_PREFIX_HEADER = YES; +GCC_PREFIX_HEADER = antlrcpp/antlrcpp-Prefix.pch; +GCC_ENABLE_CPP_EXCEPTIONS = YES; +GCC_ENABLE_CPP_RTTI = YES; +GCC_SYMBOLS_PRIVATE_EXTERN = YES; + diff --git a/runtime/Cpp/antlrcpp/stringconverter.h b/runtime/Cpp/antlrcpp/stringconverter.h new file mode 100755 index 000000000..5a012a860 --- /dev/null +++ b/runtime/Cpp/antlrcpp/stringconverter.h @@ -0,0 +1,34 @@ +#pragma once + +//---------------------------------------------------------------------------------------- +// Copyright © 2007 - 2013 Tangible Software Solutions Inc. +// This class can be used by anyone provided that the copyright notice remains intact. +// +// This class is used to replace some conversions to or from strings. +//---------------------------------------------------------------------------------------- +#include + +namespace antlrcpp { + + class StringConverterHelper + { + public: + template + static std::wstring toString(const T &subject) + { + std::wostringstream ss; + ss << subject; + return ss.str(); + } + + template + static T fromString(const std::wstring &subject) + { + std::wistringstream ss(subject); + T target; + ss >> target; + return target; + } + }; + +} \ No newline at end of file diff --git a/runtime/Cpp/antlrcpp/vectorhelper.h b/runtime/Cpp/antlrcpp/vectorhelper.h new file mode 100755 index 000000000..def58daf5 --- /dev/null +++ b/runtime/Cpp/antlrcpp/vectorhelper.h @@ -0,0 +1,31 @@ +#pragma once + +//---------------------------------------------------------------------------------------- +// Copyright © 2007 - 2013 Tangible Software Solutions Inc. +// This class can be used by anyone provided that the copyright notice remains intact. +// +// This class is used to simulate list constructor calls which reserve the list size. +//---------------------------------------------------------------------------------------- +#include +namespace antlrcpp { + + class VectorHelper + { + public: + template + static std::vector VectorWithReservedSize(int size) + { + std::vector vector; + vector.reserve(size); + return vector; + } + + template + static std::vector VectorSublist(const std::vector& vec, int start, int end) + { + std::vector vector(vec.begin() + start, vec.begin() + end); + return vector; + } + }; + +} \ No newline at end of file diff --git a/runtime/Cpp/org/antlr/v4/runtime/ANTLRErrorListener.cpp b/runtime/Cpp/org/antlr/v4/runtime/ANTLRErrorListener.cpp new file mode 100755 index 000000000..81e2b9228 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/ANTLRErrorListener.cpp @@ -0,0 +1,40 @@ +#include "ANTLRErrorListener.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/ANTLRErrorListener.h b/runtime/Cpp/org/antlr/v4/runtime/ANTLRErrorListener.h new file mode 100755 index 000000000..e10d3634f --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/ANTLRErrorListener.h @@ -0,0 +1,167 @@ +#pragma once + +#include "IRecognizer.h" +#include "Declarations.h" +#include "BitSet.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + /// + /// How to emit recognition errors. + class ANTLRErrorListener { + + /// + /// Upon syntax error, notify any interested parties. This is not how to + /// recover from errors or compute error messages. + /// specifies how to recover from syntax errors and how to compute error + /// messages. This listener's job is simply to emit a computed message, + /// though it has enough information to create its own message in many cases. + ///

+ /// The is non-null for all syntax errors except + /// when we discover mismatched token errors that we can recover from + /// in-line, without returning from the surrounding rule (via the single + /// token insertion and deletion mechanism). + ///

+ /// + /// What parser got the error. From this + /// object, you can access the context as well + /// as the input stream. + /// + /// The offending token in the input token + /// stream, unless recognizer is a lexer (then it's null). If + /// no viable alternative error, {@code e} has token at which we + /// started production for the decision. + /// + /// The line number in the input where the error occurred. + /// + /// The character position within that line where the error occurred. + /// + /// The message to emit. + /// + /// The exception generated by the parser that led to + /// the reporting of an error. It is null in the case where + /// the parser was able to recover in line without exiting the + /// surrounding rule. + public: + template + void syntaxError(IRecognizer *recognizer, void *offendingSymbol, + int line, int charPositionInLine, const std::wstring &msg, + RecognitionException *e) {} + + /// + /// This method is called by the parser when a full-context prediction + /// results in an ambiguity. + ///

+ /// When {@code exact} is {@code true}, all of the alternatives in + /// {@code ambigAlts} are viable, i.e. this is reporting an exact ambiguity. + /// When {@code exact} is {@code false}, at least two of the + /// alternatives in {@code ambigAlts} are viable for the current input, but + /// the prediction algorithm terminated as soon as it determined that at + /// least the minimum alternative in {@code ambigAlts} is viable. + ///

+ /// When the prediction mode + /// is used, the parser is required to identify exact ambiguities so + /// {@code exact} will always be {@code true}. + ///

+ /// This method is not used by lexers. + ///

+ /// the parser instance + /// the DFA for the current decision + /// the input index where the decision started + /// the input input where the ambiguity is reported + /// {@code true} if the ambiguity is exactly known, otherwise + /// {@code false}. This is always {@code true} when + /// is used. + /// the potentially ambiguous alternatives + /// the ATN configuration set where the ambiguity was + /// determined + virtual void reportAmbiguity(Parser *recognizer, dfa::DFA *dfa, int startIndex, int stopIndex, bool exact, antlrcpp::BitSet *ambigAlts, atn::ATNConfigSet *configs) = 0; + + /// + /// This method is called when an SLL conflict occurs and the parser is about + /// to use the full context information to make an LL decision. + ///

+ /// If one or more configurations in {@code configs} contains a semantic + /// predicate, the predicates are evaluated before this method is called. The + /// subset of alternatives which are still viable after predicates are + /// evaluated is reported in {@code conflictingAlts}. + ///

+ /// This method is not used by lexers. + ///

+ /// the parser instance + /// the DFA for the current decision + /// the input index where the decision started + /// the input index where the SLL conflict occurred + /// The specific conflicting alternatives. If this is + /// {@code null}, the conflicting alternatives are all alternatives + /// represented in {@code configs}. + /// the ATN configuration set where the SLL conflict was + /// detected + virtual void reportAttemptingFullContext(Parser *recognizer, dfa::DFA *dfa, int startIndex, int stopIndex, antlrcpp::BitSet *conflictingAlts, atn::ATNConfigSet *configs) = 0; + + /// + /// This method is called by the parser when a full-context prediction has a + /// unique result. + ///

+ /// For prediction implementations that only evaluate full-context + /// predictions when an SLL conflict is found (including the default + /// implementation), this method reports cases + /// where SLL conflicts were resolved to unique full-context predictions, + /// i.e. the decision was context-sensitive. This report does not necessarily + /// indicate a problem, and it may appear even in completely unambiguous + /// grammars. + ///

+ /// {@code configs} may have more than one represented alternative if the + /// full-context prediction algorithm does not evaluate predicates before + /// beginning the full-context prediction. In all cases, the final prediction + /// is passed as the {@code prediction} argument. + ///

+ /// This method is not used by lexers. + ///

+ /// the parser instance + /// the DFA for the current decision + /// the input index where the decision started + /// the input index where the context sensitivity was + /// finally determined + /// the unambiguous result of the full-context prediction + /// the ATN configuration set where the unambiguous prediction + /// was determined + virtual void reportContextSensitivity(Parser *recognizer, dfa::DFA *dfa, int startIndex, int stopIndex, int prediction, atn::ATNConfigSet *configs) = 0; + }; + + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/ANTLRErrorStrategy.cpp b/runtime/Cpp/org/antlr/v4/runtime/ANTLRErrorStrategy.cpp new file mode 100755 index 000000000..613251f23 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/ANTLRErrorStrategy.cpp @@ -0,0 +1,40 @@ +#include "ANTLRErrorStrategy.h" +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/ANTLRErrorStrategy.h b/runtime/Cpp/org/antlr/v4/runtime/ANTLRErrorStrategy.h new file mode 100755 index 000000000..06c7f16f6 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/ANTLRErrorStrategy.h @@ -0,0 +1,144 @@ +#pragma once + +#include "Declarations.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + /// + /// The interface for defining strategies to deal with syntax errors encountered + /// during a parse by ANTLR-generated parsers. We distinguish between three + /// different kinds of errors: + /// + ///
    + ///
  • The parser could not figure out which path to take in the ATN (none of + /// the available alternatives could possibly match)
  • + ///
  • The current input does not match what we were looking for
  • + ///
  • A predicate evaluated to false
  • + ///
+ /// + /// Implementations of this interface report syntax errors by calling + /// . + ///

+ /// TODO: what to do about lexers + ///

+ class ANTLRErrorStrategy { + /// + /// Reset the error handler state for the specified {@code recognizer}. + /// the parser instance + public: + virtual void reset(Parser *recognizer) = 0; + + /// + /// This method is called when an unexpected symbol is encountered during an + /// inline match operation, such as . If the error + /// strategy successfully recovers from the match failure, this method + /// returns the instance which should be treated as the + /// successful result of the match. + ///

+ /// Note that the calling code will not report an error if this method + /// returns successfully. The error strategy implementation is responsible + /// for calling as appropriate. + ///

+ /// the parser instance + /// if the error strategy was not able to + /// recover from the unexpected input symbol + virtual Token *recoverInline(Parser *recognizer) = 0; + + /// + /// This method is called to recover from exception {@code e}. This method is + /// called after by the default exception handler + /// generated for a rule method. + /// + /// + /// the parser instance + /// the recognition exception to recover from + /// if the error strategy could not recover from + /// the recognition exception + virtual void recover(Parser *recognizer, RecognitionException *e) = 0; + + /// + /// This method provides the error handler with an opportunity to handle + /// syntactic or semantic errors in the input stream before they result in a + /// . + ///

+ /// The generated code currently contains calls to after + /// entering the decision state of a closure block ({@code (...)*} or + /// {@code (...)+}). + ///

+ /// For an implementation based on Jim Idle's "magic sync" mechanism, see + /// . + ///

+ /// + /// the parser instance + /// if an error is detected by the error + /// strategy but cannot be automatically recovered at the current state in + /// the parsing process + virtual void sync(Parser *recognizer) = 0; + + /// + /// Tests whether or not {@code recognizer} is in the process of recovering + /// from an error. In error recovery mode, adds + /// symbols to the parse tree by calling + /// instead of + /// . + /// + /// the parser instance + /// {@code true} if the parser is currently recovering from a parse + /// error, otherwise {@code false} + virtual bool inErrorRecoveryMode(Parser *recognizer) = 0; + + /// + /// This method is called by when the parser successfully matches an input + /// symbol. + /// + /// the parser instance + virtual void reportMatch(Parser *recognizer) = 0; + + /// + /// Report any kind of . This method is called by + /// the default exception handler generated for a rule method. + /// + /// the parser instance + /// the recognition exception to report + virtual void reportError(Parser *recognizer, RecognitionException *e) = 0; + }; + + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/ANTLRFileStream.cpp b/runtime/Cpp/org/antlr/v4/runtime/ANTLRFileStream.cpp new file mode 100755 index 000000000..3e04773b9 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/ANTLRFileStream.cpp @@ -0,0 +1,88 @@ +#include +#include +#include +#include + +#include "ANTLRFileStream.h" +#include "Exceptions.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + ANTLRFileStream::ANTLRFileStream(const std::string &fileName) { + } + + ANTLRFileStream::ANTLRFileStream(const std::string &fileName, const std::string &encoding) { + this->fileName = fileName; + load(fileName, encoding); + } + + /* + Issue: is seems that file name in C++ are considered to be not + wide for the reason that not all systems support wchar file + names. Win32 is good about this but not all others. + TODO: this could be a place to have platform specific build + flags + */ + void ANTLRFileStream::load(const std::string &fileName, const std::string &encoding) { + if (fileName == "") { + return; + } + + std::stringstream ss; + std::wifstream f; + + // Open as a byte stream + f.open(fileName, std::ios::binary); + ss< + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + + /// + /// This is an that is loaded from a file all at once + /// when you construct the object. + /// + class ANTLRFileStream : public ANTLRInputStream { + protected: + std::string fileName; + + public: + ANTLRFileStream(const std::string &fileName); //this(fileName, nullptr); + + ANTLRFileStream(const std::string &fileName, const std::string &encoding); + + virtual void load(const std::string &fileName, const std::string &encoding); + + virtual std::string getSourceName() override; + }; + + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/ANTLRInputStream.cpp b/runtime/Cpp/org/antlr/v4/runtime/ANTLRInputStream.cpp new file mode 100755 index 000000000..279c0dfb5 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/ANTLRInputStream.cpp @@ -0,0 +1,209 @@ +#include "ANTLRInputStream.h" +#include "Exceptions.h" +#include "Interval.h" +#include "assert.h" +#include "Arrays.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + using misc::Interval; + + ANTLRInputStream::ANTLRInputStream() { + InitializeInstanceFields(); + } + + ANTLRInputStream::ANTLRInputStream(const std::wstring &input) { + InitializeInstanceFields(); + this->data = input; + this->n = (int)input.length(); + } + + ANTLRInputStream::ANTLRInputStream(wchar_t data[], int numberOfActualCharsInArray) { + InitializeInstanceFields(); + this->data = data; + this->n = numberOfActualCharsInArray; + } + + + ANTLRInputStream::ANTLRInputStream(std::wifstream *r) { + } + + + ANTLRInputStream::ANTLRInputStream(std::wifstream *r, int initialSize) { + } + + ANTLRInputStream::ANTLRInputStream(std::wifstream *r, int initialSize, int readChunkSize) { + InitializeInstanceFields(); + load(r, initialSize, readChunkSize); + } + + + void ANTLRInputStream::load(std::wifstream *r, int size, int readChunkSize) { + if (r == nullptr) { + return; + } + if (size <= 0) { + size = INITIAL_BUFFER_SIZE; + } + if (readChunkSize <= 0) { + readChunkSize = READ_BUFFER_SIZE; + } + // System.out.println("load "+size+" in chunks of "+readChunkSize); + try { + // alloc initial buffer size. + data = new wchar_t[size]; + // read all the data in chunks of readChunkSize + int numRead = 0; + int p = 0; + do { + if (p + readChunkSize > (int)data.length()) { // overflow? + // System.out.println("### overflow p="+p+", data.length="+data.length); + data = antlrcpp::Arrays::copyOf(data, (int)data.length() * 2); + } + r->read(new wchar_t[100], p); + + // System.out.println("read "+numRead+" chars; p was "+p+" is now "+(p+numRead)); + p += numRead; + } while (numRead != -1); // while not EOF + // set the actual size of the data available; + // EOF subtracted one above in p+=numRead; add one back + n = p + 1; + //System.out.println("n="+n); + } + catch (void *){ + r->close(); + } + + } + + void ANTLRInputStream::reset() { + p = 0; + } + + void ANTLRInputStream::consume() { + if (p >= n) { + assert(LA(1) == IntStream::_EOF); + throw IllegalStateException(L"cannot consume EOF"); + } + + //System.out.println("prev p="+p+", c="+(char)data[p]); + if (p < n) { + p++; + //System.out.println("p moves to "+p+" (c='"+(char)data[p]+"')"); + } + } + + int ANTLRInputStream::LA(int i) { + if (i == 0) { + return 0; // undefined + } + if (i < 0) { + i++; // e.g., translate LA(-1) to use offset i=0; then data[p+0-1] + if ((p + i - 1) < 0) { + return IntStream::_EOF; // invalid; no char before first char + } + } + + if ((p + i - 1) >= n) { + //System.out.println("char LA("+i+")=EOF; p="+p); + return IntStream::_EOF; + } + //System.out.println("char LA("+i+")="+(char)data[p+i-1]+"; p="+p); + //System.out.println("LA("+i+"); p="+p+" n="+n+" data.length="+data.length); + return data[p + i - 1]; + } + + int ANTLRInputStream::LT(int i) { + return LA(i); + } + + int ANTLRInputStream::index() { + return p; + } + + size_t ANTLRInputStream::size() { + return n; + } + + int ANTLRInputStream::mark() { + return -1; + } + + void ANTLRInputStream::release(int marker) { + } + + void ANTLRInputStream::seek(int index) { + if (index <= p) { + p = index; // just jump; don't update stream state (line, ...) + return; + } + // seek forward, consume until p hits index + while (p < index && index < n) { + consume(); + } + } + + std::wstring ANTLRInputStream::getText(Interval *interval) { + int start = interval->a; + int stop = interval->b; + if (stop >= n) { + stop = n - 1; + } + int count = stop - start + 1; + if (start >= n) { + return L""; + } + // System.err.println("data: "+Arrays.toString(data)+", n="+n+ + // ", start="+start+ + // ", stop="+stop); + return std::wstring(data, start, count); + } + + std::string ANTLRInputStream::getSourceName() { + return name; + } + + std::wstring ANTLRInputStream::toString() { + return std::wstring(data); + } + + void ANTLRInputStream::InitializeInstanceFields() { + n = 0; + p = 0; + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/ANTLRInputStream.h b/runtime/Cpp/org/antlr/v4/runtime/ANTLRInputStream.h new file mode 100755 index 000000000..1cfa469b7 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/ANTLRInputStream.h @@ -0,0 +1,145 @@ +#pragma once + + +#include +#include +#include "CharStream.h" +#include "Declarations.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + + /// + /// Vacuum all input from a / and then treat it + /// like a {@code char[]} buffer. Can also pass in a or + /// {@code char[]} to use. + ///

+ /// If you need encoding, pass in stream/reader with correct encoding. + ///

+ class ANTLRInputStream : public CharStream { + public: + static const int READ_BUFFER_SIZE = 1024; + static const int INITIAL_BUFFER_SIZE = 1024; + + /// + /// The data being scanned + protected: + std::wstring data; + + /// + /// How many characters are actually in the buffer + int n; + + /// + /// 0..n-1 index into string of next char + int p; + + /// + /// What is name or source of this char stream? + public: + std::string name; + + ANTLRInputStream(); + + /// + /// Copy data in string to a local char array + ANTLRInputStream(const std::wstring &input); + + /// + /// This is the preferred constructor for strings as no data is copied + ANTLRInputStream(wchar_t data[], int numberOfActualCharsInArray); + + ANTLRInputStream(std::wifstream *r) ; //this(r, INITIAL_BUFFER_SIZE, READ_BUFFER_SIZE); + + ANTLRInputStream(std::wifstream *r, int initialSize) ; //this(r, initialSize, READ_BUFFER_SIZE); + + ANTLRInputStream(std::wifstream *r, int initialSize, int readChunkSize); + + ANTLRInputStream(std::wiostream *input) ; //this(new InputStreamReader(input), INITIAL_BUFFER_SIZE); + + ANTLRInputStream(std::wiostream *input, int initialSize); //this(new InputStreamReader(input), initialSize); + + ANTLRInputStream(std::wiostream *input, int initialSize, int readChunkSize); //this(new InputStreamReader(input), initialSize, readChunkSize); + + virtual void load(std::wifstream *r, int size, int readChunkSize); + + /// + /// Reset the stream so that it's in the same state it was + /// when the object was created *except* the data array is not + /// touched. + /// + virtual void reset(); + + virtual void consume() override; + + virtual int LA(int i) override; + + virtual int LT(int i); + + /// + /// Return the current input symbol index 0..n where n indicates the + /// last symbol has been read. The index is the index of char to + /// be returned from LA(1). + /// + virtual int index() override; + + virtual size_t size() override; + + /// + /// mark/release do nothing; we have entire buffer + virtual int mark() override; + + virtual void release(int marker) override; + + /// + /// consume() ahead until p==index; can't just set p=index as we must + /// update line and charPositionInLine. If we seek backwards, just set p + /// + virtual void seek(int index) override; + + virtual std::wstring getText(misc::Interval *interval) override; + + virtual std::string getSourceName() override; + + virtual std::wstring toString(); + + private: + void InitializeInstanceFields(); + }; + + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/BailErrorStrategy.cpp b/runtime/Cpp/org/antlr/v4/runtime/BailErrorStrategy.cpp new file mode 100755 index 000000000..1ea81b39a --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/BailErrorStrategy.cpp @@ -0,0 +1,70 @@ +#include "BailErrorStrategy.h" +#include "Exceptions.h" +#include "ParserRuleContext.h" +#include "Parser.h" +#include "ParseCancellationException.h" +#include "InputMismatchException.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + void BailErrorStrategy::recover(Parser *recognizer, RecognitionException *e) { + for (ParserRuleContext *context = recognizer->getContext(); context != nullptr; context = context->getParent()) { + context->exception = e; + } + + throw new ParseCancellationException(e); + } + + runtime::Token *BailErrorStrategy::recoverInline(Parser *recognizer) { + + InputMismatchException *e = new InputMismatchException(recognizer); + for (ParserRuleContext *context = recognizer->getContext(); + context != nullptr; + context = context->getParent()) { + context->exception = e; + } + + throw new ParseCancellationException(e); + + } + + void BailErrorStrategy::sync(Parser *recognizer) { + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/BailErrorStrategy.h b/runtime/Cpp/org/antlr/v4/runtime/BailErrorStrategy.h new file mode 100755 index 000000000..40341f560 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/BailErrorStrategy.h @@ -0,0 +1,70 @@ +#pragma once + +#include "DefaultErrorStrategy.h" +#include "Declarations.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + /// + /// Bail out of parser at first syntax error. Do this to use it: + ///

+ /// {@code myparser.setErrorHandler(new BailErrorStrategy());} + ///

+ class BailErrorStrategy : public DefaultErrorStrategy { + /// + /// Instead of recovering from exception {@code e}, re-throw it wrapped + /// in a so it is not caught by the + /// rule function catches. Use to get the + /// original . + /// + public: + virtual void recover(Parser *recognizer, RecognitionException *e) override; + + /// + /// Make sure we don't attempt to recover inline; if the parser + /// successfully recovers, it won't throw an exception. + /// + virtual Token *recoverInline(Parser *recognizer) override; + + /// + /// Make sure we don't attempt to recover from problems in subrules. + virtual void sync(Parser *recognizer) override; + }; + + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/BaseErrorListener.cpp b/runtime/Cpp/org/antlr/v4/runtime/BaseErrorListener.cpp new file mode 100755 index 000000000..c51120de3 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/BaseErrorListener.cpp @@ -0,0 +1,49 @@ +#include "BaseErrorListener.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + void BaseErrorListener::reportAmbiguity(Parser *recognizer, dfa::DFA *dfa, int startIndex, int stopIndex, bool exact, antlrcpp::BitSet *ambigAlts, atn::ATNConfigSet *configs) { + } + + void BaseErrorListener::reportAttemptingFullContext(Parser *recognizer, dfa::DFA *dfa, int startIndex, int stopIndex, antlrcpp::BitSet *conflictingAlts, atn::ATNConfigSet *configs) { + } + + void BaseErrorListener::reportContextSensitivity(Parser *recognizer, dfa::DFA *dfa, int startIndex, int stopIndex, int prediction, atn::ATNConfigSet *configs) { + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/BaseErrorListener.h b/runtime/Cpp/org/antlr/v4/runtime/BaseErrorListener.h new file mode 100755 index 000000000..0170f3f76 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/BaseErrorListener.h @@ -0,0 +1,60 @@ +#pragma once + +#include + +#include "ANTLRErrorListener.h" +#include "IRecognizer.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + /// + /// @author Sam Harwell + /// + class BaseErrorListener : public ANTLRErrorListener { + + template + void syntaxError(IRecognizer *recognizer, void *offendingSymbol, int line, int charPositionInLine, const std::wstring &msg, RecognitionException *e) { } + + 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 reportContextSensitivity(Parser *recognizer, dfa::DFA *dfa, int startIndex, int stopIndex, int prediction, atn::ATNConfigSet *configs); + }; + + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/BufferedTokenStream.cpp b/runtime/Cpp/org/antlr/v4/runtime/BufferedTokenStream.cpp new file mode 100755 index 000000000..9b04819e2 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/BufferedTokenStream.cpp @@ -0,0 +1,410 @@ +#include +#include + +#include "BufferedTokenStream.h" +#include "WritableToken.h" +#include "Lexer.h" +#include "Exceptions.h" +#include "StringBuilder.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + BufferedTokenStream::BufferedTokenStream(TokenSource *tokenSource) { + InitializeInstanceFields(); + if (tokenSource == nullptr) { + throw new NullPointerException(L"tokenSource cannot be null"); + } + this->tokenSource = tokenSource; + } + + TokenSource *BufferedTokenStream::getTokenSource() { + return tokenSource; + } + + int BufferedTokenStream::index() { + return p; + } + + int BufferedTokenStream::mark() { + return 0; + } + + void BufferedTokenStream::release(int marker) { + // no resources to release + } + + void BufferedTokenStream::reset() { + seek(0); + } + + void BufferedTokenStream::seek(int index) { + lazyInit(); + p = adjustSeekIndex(index); + } + + size_t BufferedTokenStream::size() { + return tokens.size(); + } + + void BufferedTokenStream::consume() { + if (LA(1) == _EOF) { + throw new IllegalStateException(L"cannot consume EOF"); + } + + if (sync(p + 1)) { + p = adjustSeekIndex(p + 1); + } + } + + bool BufferedTokenStream::sync(int i) { + assert(i >= 0); + size_t n = i - tokens.size() + 1; // how many more elements we need? + //System.out.println("sync("+i+") needs "+n); + if (n > 0) { + size_t fetched = fetch((int)n); + return fetched >= n; + } + + return true; + } + + int BufferedTokenStream::fetch(int n) { + if (fetchedEOF) { + return 0; + } + + for (int i = 0; i < n; i++) { + Token *t = tokenSource->nextToken(); + if (dynamic_cast(t) != nullptr) { + (static_cast(t))->setTokenIndex((int)tokens.size()); + } + tokens.push_back(t); + if (t->getType() == Token::_EOF) { + fetchedEOF = true; + return i + 1; + } + } + + return n; + } + + Token *BufferedTokenStream::get(int i) { + if (i < 0 || i >= (int)tokens.size()) { + throw IndexOutOfBoundsException(std::wstring(L"token index ") + + std::to_wstring(i) + + std::wstring(L" out of range 0..") + + std::to_wstring(tokens.size() - 1)); + } + return tokens[i]; + } + + std::vector BufferedTokenStream::get(int start, int stop) { + if (start < 0 || stop < 0) { + return std::vector(); + } + lazyInit(); + std::vector subset = std::vector(); + if (stop >= (int)tokens.size()) { + stop = (int)tokens.size() - 1; + } + for (int i = start; i <= stop; i++) { + Token *t = tokens[i]; + if (t->getType() == Token::_EOF) { + break; + } + subset.push_back(t); + } + return subset; + } + + int BufferedTokenStream::LA(int i) { + return LT(i)->getType(); + } + + Token *BufferedTokenStream::LB(int k) { + if ((p - k) < 0) { + return nullptr; + } + return tokens[p - k]; + } + + Token *BufferedTokenStream::LT(int k) { + lazyInit(); + if (k == 0) { + return nullptr; + } + if (k < 0) { + return LB(-k); + } + + int i = p + k - 1; + sync(i); + if (i >= (int)tokens.size()) { // return EOF token + // EOF must be last token + return tokens[tokens.size() - 1]; + } + // if ( i>range ) range = i; + return tokens[i]; + } + + int BufferedTokenStream::adjustSeekIndex(int i) { + return i; + } + + void BufferedTokenStream::lazyInit() { + if (p == -1) { + setup(); + } + } + + void BufferedTokenStream::setup() { + sync(0); + p = adjustSeekIndex(0); + } + + void BufferedTokenStream::setTokenSource(TokenSource *tokenSource) { + this->tokenSource = tokenSource; + tokens.clear(); + p = -1; + } + + std::vector BufferedTokenStream::getTokens() { + return tokens; + } + + std::vector BufferedTokenStream::getTokens(int start, int stop) { + return getTokens(start, stop, nullptr); + } + + std::vector BufferedTokenStream::getTokens(int start, int stop, std::vector *types) { + lazyInit(); + if (start < 0 || stop >= (int)tokens.size() || stop < 0 || (int)start >= (int)tokens.size()) { + throw new IndexOutOfBoundsException(std::wstring(L"start ") + + std::to_wstring(start) + + std::wstring(L" or stop ") + + std::to_wstring(stop) + + std::wstring(L" not in 0..") + + std::to_wstring(tokens.size() - 1)); + } + if (start > stop) { + return std::vector(); + } + + // list = tokens[start:stop]:{T t, t.getType() in types} + std::vector filteredTokens = std::vector(); + for (int i = start; i <= stop; i++) { + Token *tok = tokens[i]; + + if (types == nullptr) { + filteredTokens.push_back(tok); + } else { + if (types == nullptr || std::find(types->begin(), types->end(), tok->getType()) != types->end()) { + filteredTokens.push_back(tok); + } + } + } + if (filteredTokens.empty()) { + filteredTokens.clear(); + } + return filteredTokens; + } + + std::vector BufferedTokenStream::getTokens(int start, int stop, int ttype) { + std::vector *s = new std::vector(); + s->insert(s->begin(), ttype); + return getTokens(start,stop, s); + } + + int BufferedTokenStream::nextTokenOnChannel(int i, int channel) { + sync(i); + Token *token = tokens[i]; + if (i >= (int)size()) { + return -1; + } + while (token->getChannel() != channel) { + if (token->getType() == Token::_EOF) { + return -1; + } + i++; + sync(i); + token = tokens[i]; + } + return i; + } + + int BufferedTokenStream::previousTokenOnChannel(int i, int channel) { + while (i >= 0 && tokens[i]->getChannel() != channel) { + i--; + } + return i; + } + + std::vector BufferedTokenStream::getHiddenTokensToRight(int tokenIndex, int channel) { + lazyInit(); + if (tokenIndex < 0 || tokenIndex >= (int)tokens.size()) { + throw new IndexOutOfBoundsException(std::to_wstring(tokenIndex) + + std::wstring(L" not in 0..") + + std::to_wstring(tokens.size() - 1)); + } + + int nextOnChannel = nextTokenOnChannel(tokenIndex + 1, Lexer::DEFAULT_TOKEN_CHANNEL); + int to; + int from = tokenIndex + 1; + // if none onchannel to right, nextOnChannel=-1 so set to = last token + if (nextOnChannel == -1) { + to = (int)size() - 1; + } else { + to = nextOnChannel; + } + + return filterForChannel(from, to, channel); + } + + std::vector BufferedTokenStream::getHiddenTokensToRight(int tokenIndex) { + return getHiddenTokensToRight(tokenIndex, -1); + } + + std::vector BufferedTokenStream::getHiddenTokensToLeft(int tokenIndex, int channel) { + lazyInit(); + if (tokenIndex < 0 || tokenIndex >= (int)tokens.size()) { + throw new IndexOutOfBoundsException(std::to_wstring(tokenIndex) + + std::wstring(L" not in 0..") + + std::to_wstring(tokens.size() - 1)); + } + + int prevOnChannel = previousTokenOnChannel(tokenIndex - 1, Lexer::DEFAULT_TOKEN_CHANNEL); + if (prevOnChannel == tokenIndex - 1) { + return std::vector(); + } + // if none onchannel to left, prevOnChannel=-1 then from=0 + int from = prevOnChannel + 1; + int to = tokenIndex - 1; + + return filterForChannel(from, to, channel); + } + + std::vector BufferedTokenStream::getHiddenTokensToLeft(int tokenIndex) { + return getHiddenTokensToLeft(tokenIndex, -1); + } + + std::vector BufferedTokenStream::filterForChannel(int from, int to, int channel) { + std::vector hidden = std::vector(); + for (int i = from; i <= to; i++) { + Token *t = tokens[i]; + if (channel == -1) { + if (t->getChannel() != Lexer::DEFAULT_TOKEN_CHANNEL) { + hidden.push_back(t); + } + } else { + if (t->getChannel() == channel) { + hidden.push_back(t); + } + } + } + if (hidden.empty()) { + return std::vector(); + } + return hidden; + } + + /** + * Get the text of all tokens in this buffer. + */ + std::string BufferedTokenStream::getSourceName() + { + return tokenSource->getSourceName(); + } + + std::wstring BufferedTokenStream::getText() { + lazyInit(); + fill(); + return getText(misc::Interval::of(0, (int)size() - 1)); + } + + std::wstring BufferedTokenStream::getText(misc::Interval *interval) { + int start = interval->a; + int stop = interval->b; + if (start < 0 || stop < 0) { + return L""; + } + lazyInit(); + if (stop >= (int)tokens.size()) { + stop = (int)tokens.size() - 1; + } + + antlrcpp::StringBuilder *buf = new antlrcpp::StringBuilder(); + for (int i = start; i <= stop; i++) { + Token *t = tokens[i]; + if (t->getType() == Token::_EOF) { + break; + } + buf->append(t->getText()); + } + return buf->toString(); + } + + std::wstring BufferedTokenStream::getText(RuleContext *ctx) { + return getText(ctx->getSourceInterval()); + } + + std::wstring BufferedTokenStream::getText(Token *start, Token *stop) { + if (start != nullptr && stop != nullptr) { + return getText(misc::Interval::of(start->getTokenIndex(), stop->getTokenIndex())); + } + + return L""; + } + + void BufferedTokenStream::fill() { + lazyInit(); + const int blockSize = 1000; + while (true) { + int fetched = fetch(blockSize); + if (fetched < blockSize) { + return; + } + } + } + + void BufferedTokenStream::InitializeInstanceFields() { + tokens = antlrcpp::VectorHelper::VectorWithReservedSize(100); + p = -1; + fetchedEOF = false; + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/BufferedTokenStream.h b/runtime/Cpp/org/antlr/v4/runtime/BufferedTokenStream.h new file mode 100755 index 000000000..1a476e080 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/BufferedTokenStream.h @@ -0,0 +1,240 @@ +#pragma once + +#include "Token.h" +#include "Interval.h" +#include "TokenStream.h" +#include "TokenSource.h" +#include "RuleContext.h" +#include "vectorhelper.h" + +#include +#include +#include + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + /// + /// Buffer all input tokens but do on-demand fetching of new tokens from lexer. + /// Useful when the parser or lexer has to set context/mode info before proper + /// lexing of future tokens. The ST template parser needs this, for example, + /// because it has to constantly flip back and forth between inside/output + /// templates. E.g., {@code }>} has to parse names as part of an + /// expression but {@code "hi, "} as a nested template. + ///

+ /// You can't use this stream if you pass whitespace or other off-channel tokens + /// to the parser. The stream can't ignore off-channel tokens. + /// ( is the same way.) Use + /// . + ///

+ class BufferedTokenStream : public TokenStream { + protected: + TokenSource *tokenSource; + + /// + /// Record every single token pulled from the source so we can reproduce + /// chunks of it later. This list captures everything so we can access + /// complete input text. + /// + std::vector tokens; + + /// + /// The index into of the current token (next token to + /// consume). {@code [}{@code ]} should be + /// . {@code =-1} indicates need to initialize + /// with first token. The constructor doesn't get a token. First call to + /// or whatever gets the first token and sets + /// {@code =0;}. + /// + int p; + + /// + /// Set to {@code true} when the EOF token is fetched. Do not continue fetching + /// tokens after that point, or multiple EOF tokens could end up in the + /// array. + /// + /// + bool fetchedEOF; + + public: + BufferedTokenStream(TokenSource *tokenSource); + + virtual TokenSource *getTokenSource() override; + virtual int index() override; + virtual int mark() override; + + virtual void release(int marker) override; + + virtual void reset(); + + virtual void seek(int index) override; + + virtual size_t size() override; + virtual void consume() override; + + /// + /// Make sure index {@code i} in tokens has a token. + /// + /// {@code true} if a token is located at index {@code i}, otherwise + /// {@code false}. + /// + protected: + virtual bool sync(int i); + + /// + /// Add {@code n} elements to buffer. + /// + /// The actual number of elements added to the buffer. + virtual int fetch(int n); + + public: + virtual Token *get(int i) override; + + /// + /// Get all tokens from start..stop inclusively + virtual std::vector get(int start, int stop); + + virtual int LA(int i) override; + + protected: + virtual Token *LB(int k); + + public: + virtual Token *LT(int k) override; + + /// + /// Allowed derived classes to modify the behavior of operations which change + /// the current stream position by adjusting the target token index of a seek + /// operation. The default implementation simply returns {@code i}. If an + /// exception is thrown in this method, the current stream index should not be + /// changed. + ///

+ /// For example, overrides this method to ensure that + /// the seek target is always an on-channel token. + ///

+ /// The target token index. + /// The adjusted target token index. + protected: + virtual int adjustSeekIndex(int i); + + void lazyInit(); + + virtual void setup(); + + /// + /// Reset this token stream by setting its token source. + public: + virtual void setTokenSource(TokenSource *tokenSource); + + virtual std::vector getTokens(); + + virtual std::vector getTokens(int start, int stop); + + /// + /// Given a start and stop index, return a List of all tokens in + /// the token type BitSet. Return null if no tokens were found. This + /// method looks at both on and off channel tokens. + /// + virtual std::vector getTokens(int start, int stop, std::vector *types); + + virtual std::vector getTokens(int start, int stop, int ttype); + + /// + /// Given a starting index, return the index of the next token on channel. + /// Return i if tokens[i] is on channel. Return -1 if there are no tokens + /// on channel between i and EOF. + /// + protected: + virtual int nextTokenOnChannel(int i, int channel); + + /// + /// Given a starting index, return the index of the previous token on channel. + /// Return i if tokens[i] is on channel. Return -1 if there are no tokens + /// on channel between i and 0. + /// + virtual int previousTokenOnChannel(int i, int channel); + + /// + /// Collect all tokens on specified channel to the right of + /// the current token up until we see a token on DEFAULT_TOKEN_CHANNEL or + /// EOF. If channel is -1, find any non default channel token. + /// + public: + virtual std::vector getHiddenTokensToRight(int tokenIndex, int channel); + + /// + /// Collect all hidden tokens (any off-default channel) to the right of + /// the current token up until we see a token on DEFAULT_TOKEN_CHANNEL + /// of EOF. + /// + virtual std::vector getHiddenTokensToRight(int tokenIndex); + + /// + /// Collect all tokens on specified channel to the left of + /// the current token up until we see a token on DEFAULT_TOKEN_CHANNEL. + /// If channel is -1, find any non default channel token. + /// + virtual std::vector getHiddenTokensToLeft(int tokenIndex, int channel); + + /// + /// Collect all hidden tokens (any off-default channel) to the left of + /// the current token up until we see a token on DEFAULT_TOKEN_CHANNEL. + /// + virtual std::vector getHiddenTokensToLeft(int tokenIndex); + + protected: + virtual std::vector filterForChannel(int from, int to, int channel); + + public: + virtual std::string getSourceName() override; + virtual std::wstring getText() override; + + virtual std::wstring getText(misc::Interval *interval) override; + + virtual std::wstring getText(RuleContext *ctx) override; + + virtual std::wstring getText(Token *start, Token *stop) override; + + /// + /// Get all tokens from lexer until EOF + virtual void fill(); + + private: + void InitializeInstanceFields(); + }; + + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/CharStream.cpp b/runtime/Cpp/org/antlr/v4/runtime/CharStream.cpp new file mode 100755 index 000000000..85d988612 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/CharStream.cpp @@ -0,0 +1,42 @@ +#include "CharStream.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + CharStream::~CharStream(){}; + + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/CharStream.h b/runtime/Cpp/org/antlr/v4/runtime/CharStream.h new file mode 100755 index 000000000..b05c43186 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/CharStream.h @@ -0,0 +1,68 @@ +#pragma once + +#include "IntStream.h" +#include "Declarations.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + /// + /// A source of characters for an ANTLR lexer. + class CharStream : public IntStream { + /// + /// This method returns the text for a range of characters within this input + /// stream. This method is guaranteed to not throw an exception if the + /// specified {@code interval} lies entirely within a marked range. For more + /// information about marked ranges, see . + /// + /// an interval within the stream + /// the text of the specified interval + /// + /// if {@code interval} is {@code null} + /// if {@code interval.a < 0}, or if + /// {@code interval.b < interval.a - 1}, or if {@code interval.b} lies at or + /// past the end of the stream + /// if the stream does not support + /// getting the text of the specified interval + public: + virtual std::wstring getText(misc::Interval *interval) = 0; + + virtual ~CharStream() = 0; + }; + + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/CommonToken.cpp b/runtime/Cpp/org/antlr/v4/runtime/CommonToken.cpp new file mode 100755 index 000000000..c41dcf5e1 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/CommonToken.cpp @@ -0,0 +1,204 @@ +#include "CommonToken.h" +#include "Interval.h" +#include "TokenSource.h" + +#include "Strings.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + std::pair *const CommonToken::EMPTY_SOURCE = new std::pair(nullptr, nullptr); + + CommonToken::CommonToken(int type) { + InitializeInstanceFields(); + this->type = type; + } + + CommonToken::CommonToken(std::pair *source, int type, int channel, int start, int stop) { + InitializeInstanceFields(); + this->source = source; + this->type = type; + this->channel = channel; + this->start = start; + this->stop = stop; + if (source->first != nullptr) { + this->line = source->first->getLine(); + this->charPositionInLine = source->first->getCharPositionInLine(); + } + } + + CommonToken::CommonToken(int type, const std::wstring &text) { + InitializeInstanceFields(); + this->type = type; + this->channel = DEFAULT_CHANNEL; + this->text = text; + this->source = EMPTY_SOURCE; + } + + CommonToken::CommonToken(Token *oldToken) { + InitializeInstanceFields(); + text = oldToken->getText(); + type = oldToken->getType(); + line = oldToken->getLine(); + index = oldToken->getTokenIndex(); + charPositionInLine = oldToken->getCharPositionInLine(); + channel = oldToken->getChannel(); + start = oldToken->getStartIndex(); + stop = oldToken->getStopIndex(); + + if (dynamic_cast(oldToken) != nullptr) { + source = (static_cast(oldToken))->source; + } else { + source = new std::pair(oldToken->getTokenSource(), oldToken->getInputStream()); + } + } + + int CommonToken::getType() { + return type; + } + + void CommonToken::setLine(int line) { + this->line = line; + } + + std::wstring CommonToken::getText() { + if (text != L"") { + return text; + } + + CharStream *input = getInputStream(); + if (input == nullptr) { + return L""; + } + size_t n = input->size(); + if ((size_t)start < n && (size_t)stop < n) { + return input->getText(misc::Interval::of(start,stop)); + } else { + return L""; + } + } + + void CommonToken::setText(const std::wstring &text) { + this->text = text; + } + + int CommonToken::getLine() { + return line; + } + + int CommonToken::getCharPositionInLine() { + return charPositionInLine; + } + + void CommonToken::setCharPositionInLine(int charPositionInLine) { + this->charPositionInLine = charPositionInLine; + } + + int CommonToken::getChannel() { + return channel; + } + + void CommonToken::setChannel(int channel) { + this->channel = channel; + } + + void CommonToken::setType(int type) { + this->type = type; + } + + int CommonToken::getStartIndex() { + return start; + } + + void CommonToken::setStartIndex(int start) { + this->start = start; + } + + int CommonToken::getStopIndex() { + return stop; + } + + void CommonToken::setStopIndex(int stop) { + this->stop = stop; + } + + int CommonToken::getTokenIndex() { + return index; + } + + void CommonToken::setTokenIndex(int index) { + this->index = index; + } + + org::antlr::v4::runtime::TokenSource *CommonToken::getTokenSource() { + return source->first; + } + + org::antlr::v4::runtime::CharStream *CommonToken::getInputStream() { + return source->second; + } + + std::wstring CommonToken::toString() { + std::wstring channelStr = L""; + if (channel > 0) { + channelStr = std::wstring(L",channel=") + std::to_wstring(channel); + } + std::wstring txt = getText(); + if (txt != L"") { + + antlrcpp::replaceAll(txt, L"\n",L"\\n"); + + antlrcpp::replaceAll(txt, L"\r",L"\\r"); + + antlrcpp::replaceAll(txt, L"\t",L"\\t"); + } else { + txt = L""; + } + return std::wstring(L"[@") + std::to_wstring(getTokenIndex()) + std::wstring(L",") + std::to_wstring(start) + std::wstring(L":") + std::to_wstring(stop) + std::wstring(L"='") + txt + std::wstring(L"',<") + std::to_wstring(type) + std::wstring(L">") + channelStr + std::wstring(L",") + std::to_wstring(line) + std::wstring(L":") + std::to_wstring(getCharPositionInLine()) + std::wstring(L"]"); + } + + void CommonToken::InitializeInstanceFields() { + type = 0; + line = 0; + charPositionInLine = -1; + channel = DEFAULT_CHANNEL; + index = -1; + start = 0; + stop = 0; + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/CommonToken.h b/runtime/Cpp/org/antlr/v4/runtime/CommonToken.h new file mode 100755 index 000000000..47307d27d --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/CommonToken.h @@ -0,0 +1,136 @@ +#pragma once + +#include "WritableToken.h" +#include "CharStream.h" +#include "Declarations.h" + +#include +#include + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + + class CommonToken : public WritableToken { + protected: + static std::pair *const EMPTY_SOURCE; + + int type; + int line; + int charPositionInLine; // set to invalid position + int channel; + std::pair *source; + + /// + /// We need to be able to change the text once in a while. If + /// this is non-null, then getText should return this. Note that + /// start/stop are not affected by changing this. + /// + // TODO: can store these in map in token stream rather than as field here + std::wstring text; + + /// + /// What token number is this from 0..n-1 tokens; < 0 implies invalid index + int index; + + /// + /// The char position into the input buffer where this token starts + int start; + + /// + /// The char position into the input buffer where this token stops + int stop; + + public: + CommonToken(int type); + + CommonToken(std::pair *source, int type, int channel, int start, int stop); + + CommonToken(int type, const std::wstring &text); + + CommonToken(Token *oldToken); + + virtual int getType() override; + + virtual void setLine(int line) override; + + virtual std::wstring getText() override; + + /// + /// Override the text for this token. getText() will return this text + /// rather than pulling from the buffer. Note that this does not mean + /// that start/stop indexes are not valid. It means that that input + /// was converted to a new string in the token object. + /// + virtual void setText(const std::wstring &text) override; + + virtual int getLine() override; + + virtual int getCharPositionInLine() override; + + virtual void setCharPositionInLine(int charPositionInLine) override; + + virtual int getChannel() override; + + virtual void setChannel(int channel) override; + + virtual void setType(int type) override; + + virtual int getStartIndex() override; + + virtual void setStartIndex(int start); + + virtual int getStopIndex() override; + + virtual void setStopIndex(int stop); + + virtual int getTokenIndex() override; + + virtual void setTokenIndex(int index) override; + + virtual TokenSource *getTokenSource() override; + + virtual CharStream *getInputStream() override; + + virtual std::wstring toString() ; + + private: + void InitializeInstanceFields(); + }; + + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/CommonTokenFactory.cpp b/runtime/Cpp/org/antlr/v4/runtime/CommonTokenFactory.cpp new file mode 100755 index 000000000..aefc7cd84 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/CommonTokenFactory.cpp @@ -0,0 +1,65 @@ +#include "CommonTokenFactory.h" +#include "Interval.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + TokenFactory *const CommonTokenFactory::DEFAULT = new CommonTokenFactory(); + + CommonTokenFactory::CommonTokenFactory(bool copyText) : copyText(copyText) { + } + + CommonTokenFactory::CommonTokenFactory() : CommonTokenFactory(false) { + } + + CommonToken *CommonTokenFactory::create(std::pair *source, int type, const std::wstring &text, int channel, int start, int stop, int line, int charPositionInLine) { + CommonToken *t = new CommonToken(source, type, channel, start, stop); + t->setLine(line); + t->setCharPositionInLine(charPositionInLine); + if (text != L"") { + t->setText(text); + } else if (copyText && source->second != nullptr) { + t->setText(source->second->getText(misc::Interval::of(start,stop))); + } + + return t; + } + + org::antlr::v4::runtime::CommonToken *CommonTokenFactory::create(int type, const std::wstring &text) { + return new CommonToken(type, text); + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/CommonTokenFactory.h b/runtime/Cpp/org/antlr/v4/runtime/CommonTokenFactory.h new file mode 100755 index 000000000..382b19767 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/CommonTokenFactory.h @@ -0,0 +1,75 @@ +#pragma once + +#include "CommonToken.h" +#include "TokenFactory.h" +#include "CharStream.h" +#include "TokenSource.h" + +#include +#include + + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + class CommonTokenFactory : public TokenFactory { + public: + static TokenFactory *const DEFAULT; + + /// + /// Copy text for token out of input char stream. Useful when input + /// stream is unbuffered. + /// + protected: + const bool copyText; + + /// + /// Create factory and indicate whether or not the factory copy + /// text out of the char stream. + /// + public: + CommonTokenFactory(bool copyText); + + CommonTokenFactory(); + + virtual CommonToken *create(std::pair *source, int type, const std::wstring &text, int channel, int start, int stop, int line, int charPositionInLine) override; + + virtual CommonToken *create(int type, const std::wstring &text) override; + }; + + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/CommonTokenStream.cpp b/runtime/Cpp/org/antlr/v4/runtime/CommonTokenStream.cpp new file mode 100755 index 000000000..01e4dc8fc --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/CommonTokenStream.cpp @@ -0,0 +1,115 @@ +#include "CommonTokenStream.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + CommonTokenStream::CommonTokenStream(TokenSource *tokenSource) : BufferedTokenStream(tokenSource) { + InitializeInstanceFields(); + } + + CommonTokenStream::CommonTokenStream(TokenSource *tokenSource, int channel) + : BufferedTokenStream(tokenSource) + { + this->channel = channel; + } + + int CommonTokenStream::adjustSeekIndex(int i) { + return nextTokenOnChannel(i, channel); + } + + Token *CommonTokenStream::LB(int k) { + if (k == 0 || (p - k) < 0) { + return nullptr; + } + + int i = p; + int n = 1; + // find k good tokens looking backwards + while (n <= k) { + // skip off-channel tokens + i = previousTokenOnChannel(i - 1, channel); + n++; + } + if (i < 0) { + return nullptr; + } + return tokens[i]; + } + + Token *CommonTokenStream::LT(int k) { + //System.out.println("enter LT("+k+")"); + lazyInit(); + if (k == 0) { + return nullptr; + } + if (k < 0) { + return LB(-k); + } + int i = p; + int n = 1; // we know tokens[p] is a good one + // find k good tokens + while (n < k) { + // skip off-channel tokens, but make sure to not look past EOF + if (sync(i + 1)) { + i = nextTokenOnChannel(i + 1, channel); + } + n++; + } + // if ( i>range ) range = i; + return tokens[i]; + } + + int CommonTokenStream::getNumberOfOnChannelTokens() { + int n = 0; + fill(); + for (size_t i = 0; i < tokens.size(); i++) { + Token *t = tokens[i]; + if (t->getChannel() == channel) { + n++; + } + if (t->getType() == Token::_EOF) { + break; + } + } + return n; + } + + void CommonTokenStream::InitializeInstanceFields() { + channel = Token::DEFAULT_CHANNEL; + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/CommonTokenStream.h b/runtime/Cpp/org/antlr/v4/runtime/CommonTokenStream.h new file mode 100755 index 000000000..3b360e82a --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/CommonTokenStream.h @@ -0,0 +1,89 @@ +#pragma once + +#include "BufferedTokenStream.h" +#include "TokenSource.h" +#include "Token.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + /// + /// The most common stream of tokens where every token is buffered up + /// and tokens are filtered for a certain channel (the parser will only + /// see these tokens). + /// + /// Even though it buffers all of the tokens, this token stream pulls tokens + /// from the tokens source on demand. In other words, until you ask for a + /// token using consume(), LT(), etc. the stream does not pull from the lexer. + /// + /// The only difference between this stream and superclass + /// is that this stream knows how to ignore off channel tokens. There may be + /// a performance advantage to using the superclass if you don't pass + /// whitespace and comments etc. to the parser on a hidden channel (i.e., + /// you set {@code $channel} instead of calling {@code skip()} in lexer rules.) + /// + /// + /// + class CommonTokenStream : public BufferedTokenStream { + /// + /// Skip tokens on any channel but this one; this is how we skip whitespace... + protected: + int channel; + + public: + CommonTokenStream(TokenSource *tokenSource); + + CommonTokenStream(TokenSource *tokenSource, int channel); + + protected: + virtual int adjustSeekIndex(int i) override; + + virtual Token *LB(int k) override; + + public: + virtual Token *LT(int k) override; + + /// + /// Count EOF just once. + virtual int getNumberOfOnChannelTokens(); + + private: + void InitializeInstanceFields(); + }; + + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/ConsoleErrorListener.cpp b/runtime/Cpp/org/antlr/v4/runtime/ConsoleErrorListener.cpp new file mode 100755 index 000000000..642b37b15 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/ConsoleErrorListener.cpp @@ -0,0 +1,43 @@ +#include "ConsoleErrorListener.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + ConsoleErrorListener *const ConsoleErrorListener::INSTANCE = new ConsoleErrorListener(); + + } // namespace runtime + } // namespace v4 + } // namespace antlr +} // namespace org diff --git a/runtime/Cpp/org/antlr/v4/runtime/ConsoleErrorListener.h b/runtime/Cpp/org/antlr/v4/runtime/ConsoleErrorListener.h new file mode 100755 index 000000000..9fc0e4b2e --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/ConsoleErrorListener.h @@ -0,0 +1,63 @@ +#pragma once + +#include + +#include "BaseErrorListener.h" +#include "RecognitionException.h" +#include "IRecognizer.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + /// + /// + /// @author Sam Harwell + /// + class ConsoleErrorListener : public BaseErrorListener { + public: + static ConsoleErrorListener *const INSTANCE; + + template + void syntaxError(IRecognizer *recognizer, void *offendingSymbol, + int line, int charPositionInLine, const std::wstring &msg, + RecognitionException *e) { + std::wcerr << L"line " << line << L":" << charPositionInLine << L" " << msg; + } + }; + + } // namespace runtime + } // namespace v4 + } // namespace antlr +} // namespace org diff --git a/runtime/Cpp/org/antlr/v4/runtime/DefaultErrorStrategy.cpp b/runtime/Cpp/org/antlr/v4/runtime/DefaultErrorStrategy.cpp new file mode 100755 index 000000000..4729c5ab8 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/DefaultErrorStrategy.cpp @@ -0,0 +1,378 @@ +#include +#include +#include + +#include "DefaultErrorStrategy.h" +#include "NoViableAltException.h" +#include "IntervalSet.h" +#include "ParserATNSimulator.h" +#include "InputMismatchException.h" +#include "Exceptions.h" +#include "ATN.h" +#include "ATNState.h" +#include "Transition.h" +#include "Strings.h" +#include "RuleTransition.h" +#include "TokenSource.h" +#include "FailedPredicateException.h" +#include "ParserRuleContext.h" + + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + void DefaultErrorStrategy::reset(Parser *recognizer) { + endErrorCondition(recognizer); + } + + void DefaultErrorStrategy::beginErrorCondition(Parser *recognizer) { + errorRecoveryMode = true; + } + + bool DefaultErrorStrategy::inErrorRecoveryMode(Parser *recognizer) { + return errorRecoveryMode; + } + + void DefaultErrorStrategy::endErrorCondition(Parser *recognizer) { + errorRecoveryMode = false; + delete lastErrorStates; + lastErrorIndex = -1; + } + + void DefaultErrorStrategy::reportMatch(Parser *recognizer) { + endErrorCondition(recognizer); + } + + void DefaultErrorStrategy::reportError(Parser *recognizer, RecognitionException *e) { + // if we've already reported an error and have not matched a token + // yet successfully, don't report any errors. + if (inErrorRecoveryMode(recognizer)) { + // System.err.print("[SPURIOUS] "); + return; // don't report spurious errors + } + beginErrorCondition(recognizer); + if (dynamic_cast(e) != nullptr) { + reportNoViableAlternative(recognizer, static_cast(e)); + } else if (dynamic_cast(e) != nullptr) { + reportInputMismatch(recognizer, static_cast(e)); + } else if (dynamic_cast(e) != nullptr) { + reportFailedPredicate(recognizer, dynamic_cast(e)); + } else { + + // This is really bush league, I hate libraries that gratuitiously print + // stuff out + std::wcerr << std::wstring(L"unknown recognition error type: " + + antlrcpp::s2ws(typeid(e).name())); + + recognizer->notifyErrorListeners(e->getOffendingToken(), antlrcpp::s2ws(e->what()), e); + + } + } + + void DefaultErrorStrategy::recover(Parser *recognizer, RecognitionException *e) { + // System.out.println("recover in "+recognizer.getRuleInvocationStack()+ + // " index="+recognizer.getInputStream().index()+ + // ", lastErrorIndex="+ + // lastErrorIndex+ + // ", states="+lastErrorStates); + if (lastErrorIndex == recognizer->getInputStream()->index() && lastErrorStates != nullptr && lastErrorStates->contains(recognizer->getState())) { + // uh oh, another error at same token index and previously-visited + // state in ATN; must be a case where LT(1) is in the recovery + // token set so nothing got consumed. Consume a single token + // at least to prevent an infinite loop; this is a failsafe. + // System.err.println("seen error condition before index="+ + // lastErrorIndex+", states="+lastErrorStates); + // System.err.println("FAILSAFE consumes "+recognizer.getTokenNames()[recognizer.getInputStream().LA(1)]); + recognizer->consume(); + } + lastErrorIndex = recognizer->getInputStream()->index(); + if (lastErrorStates == nullptr) { + lastErrorStates = new misc::IntervalSet(0); + } + lastErrorStates->add(recognizer->getState()); + misc::IntervalSet *followSet = getErrorRecoverySet(recognizer); + consumeUntil(recognizer, followSet); + } + + void DefaultErrorStrategy::sync(Parser *recognizer) { + atn::ATNState *s = recognizer->getInterpreter()->atn->states[recognizer->getState()]; + // System.err.println("sync @ "+s.stateNumber+"="+s.getClass().getSimpleName()); + // If already recovering, don't try to sync + if (inErrorRecoveryMode(recognizer)) { + return; + } + + TokenStream *tokens = recognizer->getInputStream(); + int la = tokens->LA(1); + + // try cheaper subset first; might get lucky. seems to shave a wee bit off + if (recognizer->getATN()->nextTokens(s)->contains(la) || la == Token::_EOF) { + return; + } + + // Return but don't end recovery. only do that upon valid token match + if (recognizer->isExpectedToken(la)) { + return; + } + + switch (s->getStateType()) { + case atn::ATNState::BLOCK_START: + case atn::ATNState::STAR_BLOCK_START: + case atn::ATNState::PLUS_BLOCK_START: + case atn::ATNState::STAR_LOOP_ENTRY: + // report error and recover if possible + if (singleTokenDeletion(recognizer) != nullptr) { + return; + } + + throw new InputMismatchException(recognizer); + + case atn::ATNState::PLUS_LOOP_BACK: + case atn::ATNState::STAR_LOOP_BACK: { + // System.err.println("at loop back: "+s.getClass().getSimpleName()); + reportUnwantedToken(recognizer); + misc::IntervalSet *expecting = recognizer->getExpectedTokens(); + misc::IntervalSet *whatFollowsLoopIterationOrRule = expecting->Or(getErrorRecoverySet(recognizer)); + consumeUntil(recognizer, whatFollowsLoopIterationOrRule); + } + break; + + default: + // do nothing if we can't identify the exact kind of ATN state + break; + } + } + + void DefaultErrorStrategy::reportNoViableAlternative(Parser *recognizer, NoViableAltException *e) { + TokenStream *tokens = recognizer->getInputStream(); + std::wstring input; + if (tokens != nullptr) { + if (e->getStartToken()->getType() == Token::_EOF) { + input = L""; + } else { + input = tokens->getText(e->getStartToken(), e->getOffendingToken()); + } + } else { + input = L""; + } + std::wstring msg = std::wstring(L"no viable alternative at input ") + escapeWSAndQuote(input); + recognizer->notifyErrorListeners(e->getOffendingToken(), msg, e); + } + + void DefaultErrorStrategy::reportInputMismatch(Parser *recognizer, InputMismatchException *e) { + std::wstring msg = std::wstring(L"mismatched input ") + getTokenErrorDisplay(e->getOffendingToken()) + std::wstring(L" expecting ") + e->getExpectedTokens()->toString(recognizer->getTokenNames()); + recognizer->notifyErrorListeners(e->getOffendingToken(), msg, e); + } + + void DefaultErrorStrategy::reportFailedPredicate(Parser *recognizer, FailedPredicateException *e) { + const std::wstring& ruleName = recognizer->getRuleNames()[recognizer->_ctx->getRuleIndex()]; + std::wstring msg = std::wstring(L"rule ") + ruleName + std::wstring(L" ") + e->getMessage(); + recognizer->notifyErrorListeners(e->getOffendingToken(), msg, e); + } + + void DefaultErrorStrategy::reportUnwantedToken(Parser *recognizer) { + if (inErrorRecoveryMode(recognizer)) { + return; + } + + beginErrorCondition(recognizer); + + Token *t = recognizer->getCurrentToken(); + std::wstring tokenName = getTokenErrorDisplay(t); + misc::IntervalSet *expecting = getExpectedTokens(recognizer); + + std::wstring msg = std::wstring(L"extraneous input ") + tokenName + std::wstring(L" expecting ") + expecting->toString(recognizer->getTokenNames()); + recognizer->notifyErrorListeners(t, msg, nullptr); + } + + void DefaultErrorStrategy::reportMissingToken(Parser *recognizer) { + if (inErrorRecoveryMode(recognizer)) { + return; + } + + beginErrorCondition(recognizer); + + Token *t = recognizer->getCurrentToken(); + misc::IntervalSet *expecting = getExpectedTokens(recognizer); + std::wstring msg = std::wstring(L"missing ") + expecting->toString(recognizer->getTokenNames()) + std::wstring(L" at ") + getTokenErrorDisplay(t); + + recognizer->notifyErrorListeners(t, msg, nullptr); + } + + Token *DefaultErrorStrategy::recoverInline(Parser *recognizer) { + // SINGLE TOKEN DELETION + Token *matchedSymbol = singleTokenDeletion(recognizer); + if (matchedSymbol != nullptr) { + // we have deleted the extra token. + // now, move past ttype token as if all were ok + recognizer->consume(); + return matchedSymbol; + } + + // SINGLE TOKEN INSERTION + if (singleTokenInsertion(recognizer)) { + return getMissingSymbol(recognizer); + } + + // even that didn't work; must throw the exception + throw InputMismatchException(recognizer); + } + + bool DefaultErrorStrategy::singleTokenInsertion(Parser *recognizer) { + int currentSymbolType = recognizer->getInputStream()->LA(1); + // if current token is consistent with what could come after current + // ATN state, then we know we're missing a token; error recovery + // is free to conjure up and insert the missing token + atn::ATNState *currentState = recognizer->getInterpreter()->atn->states[recognizer->getState()]; + atn::ATNState *next = currentState->transition(0)->target; + atn::ATN *atn = recognizer->getInterpreter()->atn; + misc::IntervalSet *expectingAtLL2 = atn->nextTokens(next, recognizer->_ctx); + // System.out.println("LT(2) set="+expectingAtLL2.toString(recognizer.getTokenNames())); + if (expectingAtLL2->contains(currentSymbolType)) { + reportMissingToken(recognizer); + return true; + } + return false; + } + + Token *DefaultErrorStrategy::singleTokenDeletion(Parser *recognizer) { + int nextTokenType = recognizer->getInputStream()->LA(2); + misc::IntervalSet *expecting = getExpectedTokens(recognizer); + if (expecting->contains(nextTokenType)) { + reportUnwantedToken(recognizer); + /* + System.err.println("recoverFromMismatchedToken deleting "+ + ((TokenStream)recognizer.getInputStream()).LT(1)+ + " since "+((TokenStream)recognizer.getInputStream()).LT(2)+ + " is what we want"); + */ + recognizer->consume(); // simply delete extra token + // we want to return the token we're actually matching + Token *matchedSymbol = recognizer->getCurrentToken(); + reportMatch(recognizer); // we know current token is correct + return matchedSymbol; + } + return nullptr; + } + + Token *DefaultErrorStrategy::getMissingSymbol(Parser *recognizer) { + Token *currentSymbol = recognizer->getCurrentToken(); + misc::IntervalSet *expecting = getExpectedTokens(recognizer); + int expectedTokenType = expecting->getMinElement(); // get any element + std::wstring tokenText; + if (expectedTokenType == Token::_EOF) { + tokenText = L""; + } else { + tokenText = std::wstring(L"getTokenNames()[expectedTokenType].at(expectedTokenType) + std::wstring(L">"); + } + Token *current = currentSymbol; + Token *lookback = recognizer->getInputStream()->LT(-1); + if (current->getType() == Token::_EOF && lookback != nullptr) { + current = lookback; + } + return (Token*)recognizer->getTokenFactory()->create(new std::pair(current->getTokenSource(), current->getTokenSource()->getInputStream()), expectedTokenType, tokenText, Token::DEFAULT_CHANNEL, -1, -1, current->getLine(), current->getCharPositionInLine()); + } + + misc::IntervalSet *DefaultErrorStrategy::getExpectedTokens(Parser *recognizer) { + return recognizer->getExpectedTokens(); + } + + std::wstring DefaultErrorStrategy::getTokenErrorDisplay(Token *t) { + if (t == nullptr) { + return L""; + } + std::wstring s = getSymbolText(t); + if (s == L"") { + if (getSymbolType(t) == Token::_EOF) { + s = L""; + } else { + s = std::wstring(L"<") + std::to_wstring(getSymbolType(t)) + std::wstring(L">"); + } + } + return escapeWSAndQuote(s); + } + + std::wstring DefaultErrorStrategy::getSymbolText(Token *symbol) { + return symbol->getText(); + } + + int DefaultErrorStrategy::getSymbolType(Token *symbol) { + return symbol->getType(); + } + + std::wstring DefaultErrorStrategy::escapeWSAndQuote(std::wstring &s) { + // if ( s==null ) return s; + antlrcpp::replaceAll(s, L"\n", L"\\n"); + antlrcpp::replaceAll(s, L"\r",L"\\r"); + antlrcpp::replaceAll(s, L"\t",L"\\t"); + return std::wstring(L"'") + s + std::wstring(L"'"); + } + + misc::IntervalSet *DefaultErrorStrategy::getErrorRecoverySet(Parser *recognizer) { + atn::ATN *atn = recognizer->getInterpreter()->atn; + RuleContext *ctx = recognizer->_ctx; + misc::IntervalSet *recoverSet = new misc::IntervalSet(0); + while (ctx != nullptr && ctx->invokingState >= 0) { + // compute what follows who invoked us + atn::ATNState *invokingState = atn->states[ctx->invokingState]; + atn::RuleTransition *rt = dynamic_cast(invokingState->transition(0)); + misc::IntervalSet *follow = atn->nextTokens(rt->followState); + recoverSet->addAll(follow); + ctx = ctx->parent; + } + recoverSet->remove(Token::EPSILON); + // System.out.println("recover set "+recoverSet.toString(recognizer.getTokenNames())); + return recoverSet; + } + + void DefaultErrorStrategy::consumeUntil(Parser *recognizer, misc::IntervalSet *set) { + // System.err.println("consumeUntil("+set.toString(recognizer.getTokenNames())+")"); + int ttype = recognizer->getInputStream()->LA(1); + while (ttype != Token::_EOF && !set->contains(ttype)) { + //System.out.println("consume during recover LA(1)="+getTokenNames()[input.LA(1)]); + // recognizer.getInputStream().consume(); + recognizer->consume(); + ttype = recognizer->getInputStream()->LA(1); + } + } + + void DefaultErrorStrategy::InitializeInstanceFields() { + errorRecoveryMode = false; + lastErrorIndex = -1; + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/DefaultErrorStrategy.h b/runtime/Cpp/org/antlr/v4/runtime/DefaultErrorStrategy.h new file mode 100755 index 000000000..0ff6a5325 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/DefaultErrorStrategy.h @@ -0,0 +1,496 @@ +#pragma once + +#include "ANTLRErrorStrategy.h" +#include "Declarations.h" + +#include +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + + /// + /// This is the default error handling mechanism for ANTLR parsers + /// and tree parsers. + /// + class DefaultErrorStrategy : public ANTLRErrorStrategy { + /// + /// This is true after we see an error and before having successfully + /// matched a token. Prevents generation of more than one error message + /// per error. + /// + /// + protected: + bool errorRecoveryMode; + + /// + /// The index into the input stream where the last error occurred. + /// This is used to prevent infinite loops where an error is found + /// but no token is consumed during recovery...another error is found, + /// ad nauseum. This is a failsafe mechanism to guarantee that at least + /// one token/tree node is consumed for two errors. + /// + int lastErrorIndex; + + misc::IntervalSet *lastErrorStates; + + /// + /// {@inheritDoc} + ///

+ /// The default implementation simply calls to + /// ensure that the handler is not in error recovery mode. + ///

+ public: + virtual void reset(Parser *recognizer) override; + + /// + /// This method is called to enter error recovery mode when a recognition + /// exception is reported. + /// + /// the parser instance + protected: + virtual void beginErrorCondition(Parser *recognizer); + + /// + /// {@inheritDoc} + /// + public: + virtual bool inErrorRecoveryMode(Parser *recognizer) override; + + /// + /// This method is called to leave error recovery mode after recovering from + /// a recognition exception. + /// + /// + protected: + virtual void endErrorCondition(Parser *recognizer); + + /// + /// {@inheritDoc} + ///

+ /// The default implementation simply calls . + ///

+ public: + virtual void reportMatch(Parser *recognizer) override; + + /// + /// {@inheritDoc} + ///

+ /// The default implementation returns immediately if the handler is already + /// in error recovery mode. Otherwise, it calls + /// and dispatches the reporting task based on the runtime type of {@code e} + /// according to the following table. + /// + ///

    + ///
  • : Dispatches the call to + ///
  • + ///
  • : Dispatches the call to + ///
  • + ///
  • : Dispatches the call to + ///
  • + ///
  • All other types: calls to report + /// the exception
  • + ///
+ ///
+ virtual void reportError(Parser *recognizer, RecognitionException *e) override; + + /// + /// {@inheritDoc} + ///

+ /// The default implementation resynchronizes the parser by consuming tokens + /// until we find one in the resynchronization set--loosely the set of tokens + /// that can follow the current rule. + ///

+ virtual void recover(Parser *recognizer, RecognitionException *e) override; + + /// + /// The default implementation of makes sure + /// that the current lookahead symbol is consistent with what were expecting + /// at this point in the ATN. You can call this anytime but ANTLR only + /// generates code to check before subrules/loops and each iteration. + ///

+ /// Implements Jim Idle's magic sync mechanism in closures and optional + /// subrules. E.g., + /// + ///

+                    /// a : sync ( stuff sync )* ;
+                    /// sync : {consume to what can follow sync} ;
+                    /// 
+ /// + /// At the start of a sub rule upon error, performs single + /// token deletion, if possible. If it can't do that, it bails on the current + /// rule and uses the default error recovery, which consumes until the + /// resynchronization set of the current rule. + ///

+ /// If the sub rule is optional ({@code (...)?}, {@code (...)*}, or block + /// with an empty alternative), then the expected set includes what follows + /// the subrule. + ///

+ /// During loop iteration, it consumes until it sees a token that can start a + /// sub rule or what follows loop. Yes, that is pretty aggressive. We opt to + /// stay in the loop as long as possible. + ///

+ /// ORIGINS + ///

+ /// Previous versions of ANTLR did a poor job of their recovery within loops. + /// A single mismatch token or missing token would force the parser to bail + /// out of the entire rules surrounding the loop. So, for rule + /// + ///

+                    /// classDef : 'class' ID '{' member* '}'
+                    /// 
+ /// + /// input with an extra token between members would force the parser to + /// consume until it found the next class definition rather than the next + /// member definition of the current class. + ///

+ /// This functionality cost a little bit of effort because the parser has to + /// compare token set at the start of the loop and at each iteration. If for + /// some reason speed is suffering for you, you can turn off this + /// functionality by simply overriding this method as a blank { }. + ///

+ virtual void sync(Parser *recognizer) override; + + /// + /// This is called by when the exception is a + /// . + /// + /// + /// the parser instance + /// the recognition exception + protected: + virtual void reportNoViableAlternative(Parser *recognizer, NoViableAltException *e); + + /// + /// This is called by when the exception is an + /// . + /// + /// + /// the parser instance + /// the recognition exception + virtual void reportInputMismatch(Parser *recognizer, InputMismatchException *e); + + /// + /// This is called by when the exception is a + /// . + /// + /// + /// the parser instance + /// the recognition exception + virtual void reportFailedPredicate(Parser *recognizer, FailedPredicateException *e); + + /// + /// This method is called to report a syntax error which requires the removal + /// of a token from the input stream. At the time this method is called, the + /// erroneous symbol is current {@code LT(1)} symbol and has not yet been + /// removed from the input stream. When this method returns, + /// {@code recognizer} is in error recovery mode. + ///

+ /// This method is called when identifies + /// single-token deletion as a viable recovery strategy for a mismatched + /// input error. + ///

+ /// The default implementation simply returns if the handler is already in + /// error recovery mode. Otherwise, it calls to + /// enter error recovery mode, followed by calling + /// . + ///

+ /// the parser instance + virtual void reportUnwantedToken(Parser *recognizer); + + /// + /// This method is called to report a syntax error which requires the + /// insertion of a missing token into the input stream. At the time this + /// method is called, the missing token has not yet been inserted. When this + /// method returns, {@code recognizer} is in error recovery mode. + ///

+ /// This method is called when identifies + /// single-token insertion as a viable recovery strategy for a mismatched + /// input error. + ///

+ /// The default implementation simply returns if the handler is already in + /// error recovery mode. Otherwise, it calls to + /// enter error recovery mode, followed by calling + /// . + ///

+ /// the parser instance + virtual void reportMissingToken(Parser *recognizer); + + /// + /// {@inheritDoc} + ///

+ /// The default implementation attempts to recover from the mismatched input + /// by using single token insertion and deletion as described below. If the + /// recovery attempt fails, this method throws an + /// . + ///

+ /// EXTRA TOKEN (single token deletion) + ///

+ /// {@code LA(1)} is not what we are looking for. If {@code LA(2)} has the + /// right token, however, then assume {@code LA(1)} is some extra spurious + /// token and delete it. Then consume and return the next token (which was + /// the {@code LA(2)} token) as the successful result of the match operation. + ///

+ /// This recovery strategy is implemented by . + ///

+ /// MISSING TOKEN (single token insertion) + ///

+ /// If current token (at {@code LA(1)}) is consistent with what could come + /// after the expected {@code LA(1)} token, then assume the token is missing + /// and use the parser's to create it on the fly. The + /// "insertion" is performed by returning the created token as the successful + /// result of the match operation. + ///

+ /// This recovery strategy is implemented by . + ///

+ /// EXAMPLE + ///

+ /// For example, Input {@code i=(3;} is clearly missing the {@code ')'}. When + /// the parser returns from the nested call to {@code expr}, it will have + /// call chain: + /// + ///

+                    /// stat -> expr -> atom
+                    /// 
+ /// + /// and it will be trying to match the {@code ')'} at this point in the + /// derivation: + /// + ///
+                    /// => ID '=' '(' INT ')' ('+' atom)* ';'
+                    ///                    ^
+                    /// 
+ /// + /// The attempt to match {@code ')'} will fail when it sees {@code ';'} and + /// call . To recover, it sees that {@code LA(1)==';'} + /// is in the set of tokens that can follow the {@code ')'} token reference + /// in rule {@code atom}. It can assume that you forgot the {@code ')'}. + ///
+ public: + virtual Token *recoverInline(Parser *recognizer) override; + + /// + /// This method implements the single-token insertion inline error recovery + /// strategy. It is called by if the single-token + /// deletion strategy fails to recover from the mismatched input. If this + /// method returns {@code true}, {@code recognizer} will be in error recovery + /// mode. + ///

+ /// This method determines whether or not single-token insertion is viable by + /// checking if the {@code LA(1)} input symbol could be successfully matched + /// if it were instead the {@code LA(2)} symbol. If this method returns + /// {@code true}, the caller is responsible for creating and inserting a + /// token with the correct type to produce this behavior. + ///

+ /// the parser instance + /// {@code true} if single-token insertion is a viable recovery + /// strategy for the current mismatched input, otherwise {@code false} + protected: + virtual bool singleTokenInsertion(Parser *recognizer); + + /// + /// This method implements the single-token deletion inline error recovery + /// strategy. It is called by to attempt to recover + /// from mismatched input. If this method returns null, the parser and error + /// handler state will not have changed. If this method returns non-null, + /// {@code recognizer} will not be in error recovery mode since the + /// returned token was a successful match. + ///

+ /// If the single-token deletion is successful, this method calls + /// to report the error, followed by + /// to actually "delete" the extraneous token. Then, + /// before returning is called to signal a successful + /// match. + ///

+ /// the parser instance + /// the successfully matched instance if single-token + /// deletion successfully recovers from the mismatched input, otherwise + /// {@code null} + virtual Token *singleTokenDeletion(Parser *recognizer); + + /// + /// Conjure up a missing token during error recovery. + /// + /// The recognizer attempts to recover from single missing + /// symbols. But, actions might refer to that missing symbol. + /// For example, x=ID {f($x);}. The action clearly assumes + /// that there has been an identifier matched previously and that + /// $x points at that token. If that token is missing, but + /// the next token in the stream is what we want we assume that + /// this token is missing and we keep going. Because we + /// have to return some token to replace the missing token, + /// we have to conjure one up. This method gives the user control + /// over the tokens returned for missing tokens. Mostly, + /// you will want to create something special for identifier + /// tokens. For literals such as '{' and ',', the default + /// action in the parser or tree parser works. It simply creates + /// a CommonToken of the appropriate type. The text will be the token. + /// If you change what tokens must be created by the lexer, + /// override this method to create the appropriate tokens. + /// + virtual Token *getMissingSymbol(Parser *recognizer); + + virtual misc::IntervalSet *getExpectedTokens(Parser *recognizer); + + /// + /// How should a token be displayed in an error message? The default + /// is to display just the text, but during development you might + /// want to have a lot of information spit out. Override in that case + /// to use t.toString() (which, for CommonToken, dumps everything about + /// the token). This is better than forcing you to override a method in + /// your token objects because you don't have to go modify your lexer + /// so that it creates a new Java type. + /// + virtual std::wstring getTokenErrorDisplay(Token *t); + + virtual std::wstring getSymbolText(Token *symbol); + + virtual int getSymbolType(Token *symbol); + + virtual std::wstring escapeWSAndQuote(std::wstring &s); + + /* Compute the error recovery set for the current rule. During + * rule invocation, the parser pushes the set of tokens that can + * follow that rule reference on the stack; this amounts to + * computing FIRST of what follows the rule reference in the + * enclosing rule. See LinearApproximator.FIRST(). + * This local follow set only includes tokens + * from within the rule; i.e., the FIRST computation done by + * ANTLR stops at the end of a rule. + * + * EXAMPLE + * + * When you find a "no viable alt exception", the input is not + * consistent with any of the alternatives for rule r. The best + * thing to do is to consume tokens until you see something that + * can legally follow a call to r *or* any rule that called r. + * You don't want the exact set of viable next tokens because the + * input might just be missing a token--you might consume the + * rest of the input looking for one of the missing tokens. + * + * Consider grammar: + * + * a : '[' b ']' + * | '(' b ')' + * ; + * b : c '^' INT ; + * c : ID + * | INT + * ; + * + * At each rule invocation, the set of tokens that could follow + * that rule is pushed on a stack. Here are the various + * context-sensitive follow sets: + * + * FOLLOW(b1_in_a) = FIRST(']') = ']' + * FOLLOW(b2_in_a) = FIRST(')') = ')' + * FOLLOW(c_in_b) = FIRST('^') = '^' + * + * Upon erroneous input "[]", the call chain is + * + * a -> b -> c + * + * and, hence, the follow context stack is: + * + * depth follow set start of rule execution + * 0 a (from main()) + * 1 ']' b + * 2 '^' c + * + * Notice that ')' is not included, because b would have to have + * been called from a different context in rule a for ')' to be + * included. + * + * For error recovery, we cannot consider FOLLOW(c) + * (context-sensitive or otherwise). We need the combined set of + * all context-sensitive FOLLOW sets--the set of all tokens that + * could follow any reference in the call chain. We need to + * resync to one of those tokens. Note that FOLLOW(c)='^' and if + * we resync'd to that token, we'd consume until EOF. We need to + * sync to context-sensitive FOLLOWs for a, b, and c: {']','^'}. + * In this case, for input "[]", LA(1) is ']' and in the set, so we would + * not consume anything. After printing an error, rule c would + * return normally. Rule b would not find the required '^' though. + * At this point, it gets a mismatched token error and throws an + * exception (since LA(1) is not in the viable following token + * set). The rule exception handler tries to recover, but finds + * the same recovery set and doesn't consume anything. Rule b + * exits normally returning to rule a. Now it finds the ']' (and + * with the successful match exits errorRecovery mode). + * + * So, you can see that the parser walks up the call chain looking + * for the token that was a member of the recovery set. + * + * Errors are not generated in errorRecovery mode. + * + * ANTLR's error recovery mechanism is based upon original ideas: + * + * "Algorithms + Data Structures = Programs" by Niklaus Wirth + * + * and + * + * "A note on error recovery in recursive descent parsers": + * http://portal.acm.org/citation.cfm?id=947902.947905 + * + * Later, Josef Grosch had some good ideas: + * + * "Efficient and Comfortable Error Recovery in Recursive Descent + * Parsers": + * ftp://www.cocolab.com/products/cocktail/doca4.ps/ell.ps.zip + * + * Like Grosch I implement context-sensitive FOLLOW sets that are combined + * at run-time upon error to avoid overhead during parsing. + */ + virtual misc::IntervalSet *getErrorRecoverySet(Parser *recognizer); + + /// + /// Consume tokens until one matches the given token set. + virtual void consumeUntil(Parser *recognizer, misc::IntervalSet *set); + + private: + void InitializeInstanceFields(); + + public: + DefaultErrorStrategy() { + InitializeInstanceFields(); + } + }; + + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/DiagnosticErrorListener.cpp b/runtime/Cpp/org/antlr/v4/runtime/DiagnosticErrorListener.cpp new file mode 100755 index 000000000..e8c9c7db2 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/DiagnosticErrorListener.cpp @@ -0,0 +1,116 @@ +#include "DiagnosticErrorListener.h" +#include "ATNConfig.h" +#include "ATNConfigSet.h" +#include "Parser.h" +#include "Interval.h" +#include "DFA.h" +#include "DecisionState.h" +#include "stdio.h" +#include "StringBuilder.h" +#include "ATNState.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + + //JAVA TO C++ CONVERTER TODO TASK: Calls to same-class constructors are not supported in C++ prior to C++11: + DiagnosticErrorListener::DiagnosticErrorListener() : exactOnly(true) { + } + + DiagnosticErrorListener::DiagnosticErrorListener(bool exactOnly) : exactOnly(exactOnly) { + } + + void DiagnosticErrorListener::reportAmbiguity(Parser *recognizer, dfa::DFA *dfa, int startIndex, int stopIndex, bool exact, antlrcpp::BitSet *ambigAlts, atn::ATNConfigSet *configs) { + if (exactOnly && !exact) { + return; + } + wchar_t buf[16]; + std::wstring decision = getDecisionDescription(recognizer, dfa); + antlrcpp::BitSet *conflictingAlts = getConflictingAlts(ambigAlts, configs); + std::wstring text = recognizer->getTokenStream()->getText(misc::Interval::of(startIndex, stopIndex)); + std::wstring message = L"reportAmbiguity d=" + decision + L": ambigAlts=" + conflictingAlts->toString() + L", input='" + text + L"'"; + swprintf(buf, sizeof(buf) / sizeof(*buf), L"%d", 5); + recognizer->notifyErrorListeners(message); + } + + void DiagnosticErrorListener::reportAttemptingFullContext(Parser *recognizer, dfa::DFA *dfa, int startIndex, int stopIndex, antlrcpp::BitSet *conflictingAlts, atn::ATNConfigSet *configs) { + std::wstring decision = getDecisionDescription(recognizer, dfa); + std::wstring text = recognizer->getTokenStream()->getText(misc::Interval::of(startIndex, stopIndex)); + std::wstring message = L"reportAttemptingFullContext d=" + decision + L", input='" + text + L"'"; + recognizer->notifyErrorListeners(message); + } + + void DiagnosticErrorListener::reportContextSensitivity(Parser *recognizer, dfa::DFA *dfa, int startIndex, int stopIndex, int prediction, atn::ATNConfigSet *configs) { + std::wstring decision = getDecisionDescription(recognizer, dfa); + std::wstring text = recognizer->getTokenStream()->getText(misc::Interval::of(startIndex, stopIndex)); + std::wstring message = L"reportContextSensitivity d=" + decision + L", input='" + text + L"'"; + recognizer->notifyErrorListeners(message); + } + + std::wstring DiagnosticErrorListener::getDecisionDescription(Parser *recognizer, dfa::DFA *dfa) { + int decision = dfa->decision; + int ruleIndex = ((atn::ATNState*)dfa->atnStartState)->ruleIndex; + + const std::vector& ruleNames = recognizer->getRuleNames(); + if (ruleIndex < 0 || ruleIndex >= (int)ruleNames.size()) { + return antlrcpp::StringConverterHelper::toString(decision); + } + + std::wstring ruleName = ruleNames[ruleIndex]; + if (ruleName == L"" || ruleName.empty()) { + return antlrcpp::StringConverterHelper::toString(decision); + } + + return std::to_wstring(decision) + L"(" + ruleName + L")"; + } + + antlrcpp::BitSet *DiagnosticErrorListener::getConflictingAlts(antlrcpp::BitSet *reportedAlts, atn::ATNConfigSet *configs) { + if (reportedAlts != nullptr) { + return reportedAlts; + } + + antlrcpp::BitSet *result = new antlrcpp::BitSet(); + for (size_t i = 0; i < configs->size(); i++) { + atn::ATNConfig *config = configs->get((int)i); + result->set(config->alt); + } + + return result; + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/DiagnosticErrorListener.h b/runtime/Cpp/org/antlr/v4/runtime/DiagnosticErrorListener.h new file mode 100755 index 000000000..65a97495e --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/DiagnosticErrorListener.h @@ -0,0 +1,114 @@ +#pragma once + + +#include +#include "stringconverter.h" +#include "Declarations.h" +#include "BaseErrorListener.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + + + /// + /// This implementation of can be used to identify + /// certain potential correctness and performance problems in grammars. "Reports" + /// are made by calling with the appropriate + /// message. + /// + ///
    + ///
  • Ambiguities: These are cases where more than one path through the + /// grammar can match the input.
  • + ///
  • Weak context sensitivity: These are cases where full-context + /// prediction resolved an SLL conflict to a unique alternative which equaled the + /// minimum alternative of the SLL conflict.
  • + ///
  • Strong (forced) context sensitivity: These are cases where the + /// full-context prediction resolved an SLL conflict to a unique alternative, + /// and the minimum alternative of the SLL conflict was found to not be + /// a truly viable alternative. Two-stage parsing cannot be used for inputs where + /// this situation occurs.
  • + ///
+ /// + /// @author Sam Harwell + ///
+ class DiagnosticErrorListener : public BaseErrorListener { + /// + /// When {@code true}, only exactly known ambiguities are reported. + /// + protected: + const bool exactOnly; + + /// + /// Initializes a new instance of which only + /// reports exact ambiguities. + /// + public: + DiagnosticErrorListener(); //this(true); + + /// + /// Initializes a new instance of , specifying + /// whether all ambiguities or only exact ambiguities are reported. + /// + /// {@code true} to report only exact ambiguities, otherwise + /// {@code false} to report all ambiguities. + DiagnosticErrorListener(bool exactOnly); + + 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) override; + + virtual void reportContextSensitivity(Parser *recognizer, dfa::DFA *dfa, int startIndex, int stopIndex, int prediction, atn::ATNConfigSet *configs) override; + + protected: + virtual std::wstring getDecisionDescription(Parser *recognizer, dfa::DFA *dfa); + + /// + /// Computes the set of conflicting or ambiguous alternatives from a + /// configuration set, if that information was not already provided by the + /// parser. + /// + /// The set of conflicting or ambiguous alternatives, as + /// reported by the parser. + /// The conflicting or ambiguous configuration set. + /// Returns {@code reportedAlts} if it is not {@code null}, otherwise + /// returns the set of alternatives represented in {@code configs}. + virtual antlrcpp::BitSet *getConflictingAlts(antlrcpp::BitSet *reportedAlts, atn::ATNConfigSet *configs); + }; + + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/Exceptions.cpp b/runtime/Cpp/org/antlr/v4/runtime/Exceptions.cpp new file mode 100644 index 000000000..a95d770df --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/Exceptions.cpp @@ -0,0 +1,30 @@ +/* + * [The "BSD license"] + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "Exceptions.h" diff --git a/runtime/Cpp/org/antlr/v4/runtime/Exceptions.h b/runtime/Cpp/org/antlr/v4/runtime/Exceptions.h new file mode 100644 index 000000000..94532ae17 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/Exceptions.h @@ -0,0 +1,120 @@ +/* + * [The "BSD license"] + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include "Declarations.h" + +#include +#include +#include + + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + class ANTLRException : public std::exception { + private: + std::wstring errormsg; + + public: + ANTLRException() {} + ANTLRException(const std::wstring msg) { + this->errormsg = msg; + } + std::wstring getMessage() { + return errormsg; + } + + }; + + class IllegalClassException : public ANTLRException { + public: + IllegalClassException(const std::wstring msg) : ANTLRException(msg) {}; + IllegalClassException() {}; + }; + + class IllegalStateException : public ANTLRException { + public: + IllegalStateException(const std::wstring msg) : ANTLRException(msg) {}; + IllegalStateException(){}; + }; + + class IllegalArgumentException : public ANTLRException { + public: + IllegalArgumentException(const std::wstring msg) : ANTLRException(msg) {}; + IllegalArgumentException(const std::wstring msg, std::exception e); + IllegalArgumentException(){}; + }; + + class NoSuchElementException : public ANTLRException { + public: + NoSuchElementException(const std::wstring msg) : ANTLRException(msg) {}; + NoSuchElementException(){}; + }; + + class NullPointerException : public ANTLRException { + public: + NullPointerException(const std::wstring msg) : ANTLRException(msg) {}; + NullPointerException(){}; + }; + class IndexOutOfBoundsException : public ANTLRException { + public: + IndexOutOfBoundsException(const std::wstring msg) : ANTLRException(msg) {}; + IndexOutOfBoundsException(){}; + }; + class UnsupportedOperationException : public ANTLRException { + public: + UnsupportedOperationException(const std::wstring msg) : ANTLRException(msg) {}; + UnsupportedOperationException(){}; + }; + class IOException : public ANTLRException { + public: + IOException(const std::wstring msg) : ANTLRException(msg) {}; + IOException(){}; + }; + class TODOException : public ANTLRException { + public: + TODOException(const std::wstring msg) : ANTLRException(msg) {}; + TODOException(); + }; + class ASSERTException : public ANTLRException { + public: + ASSERTException(const std::wstring location, + const std::wstring condition) + : ANTLRException(location + L" condition= " + condition) {}; + ASSERTException(); + }; + + + } + } + } +} \ No newline at end of file diff --git a/runtime/Cpp/org/antlr/v4/runtime/FailedPredicateException.cpp b/runtime/Cpp/org/antlr/v4/runtime/FailedPredicateException.cpp new file mode 100755 index 000000000..fbb8f1fba --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/FailedPredicateException.cpp @@ -0,0 +1,89 @@ +#include "FailedPredicateException.h" +#include "ATNState.h" +#include "AbstractPredicateTransition.h" +#include "PredicateTransition.h" +#include "Parser.h" +#include "ParserATNSimulator.h" +#include "ATN.h" + +/* +* [The "BSD license"] +* Copyright (c) 2013 Terence Parr +* Copyright (c) 2013 Dan McLaughlin +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* 1. Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* 2. Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* 3. The name of the author may not be used to endorse or promote products +* derived from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + FailedPredicateException::FailedPredicateException(Parser *recognizer) : RecognitionException() { + } + + FailedPredicateException::FailedPredicateException(Parser *recognizer, const std::wstring &predicate): RecognitionException() { + } + + FailedPredicateException::FailedPredicateException(Parser *recognizer, const std::wstring &predicate, const std::wstring &message) +#ifdef TODO + // Huston, a problem. "trans" isn't defined until below + : RecognitionException(formatMessage(predicate, message), recognizer, recognizer->getInputStream(), recognizer->_ctx), ruleIndex((static_cast(trans))->ruleIndex), predicateIndex((static_cast(trans))->predIndex), predicate(predicate) +#endif + + { + atn::ATNState *s = recognizer->getInterpreter()->atn->states[recognizer->getState()]; + + atn::AbstractPredicateTransition *trans = static_cast(s->transition(0)); + if (dynamic_cast(trans) != nullptr) { + } else { + this->ruleIndex = 0; + this->predicateIndex = 0; + } + + this->setOffendingToken(recognizer->getCurrentToken()); + } + + int FailedPredicateException::getRuleIndex() { + return ruleIndex; + } + + int FailedPredicateException::getPredIndex() { + return predicateIndex; + } + + std::wstring FailedPredicateException::getPredicate() { + return predicate; + } + + std::wstring FailedPredicateException::formatMessage(const std::wstring &predicate, const std::wstring &message) { + if (message != L"") { + return message; + } + return L"failed predicate: " + predicate + L"?"; + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/FailedPredicateException.h b/runtime/Cpp/org/antlr/v4/runtime/FailedPredicateException.h new file mode 100755 index 000000000..8bb528712 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/FailedPredicateException.h @@ -0,0 +1,75 @@ +#pragma once + +#include "RecognitionException.h" +#include "Declarations.h" + +#include + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + + /// + /// A semantic predicate failed during validation. Validation of predicates + /// occurs when normally parsing the alternative just like matching a token. + /// Disambiguating predicate evaluation occurs when we test a predicate during + /// prediction. + /// + class FailedPredicateException : public RecognitionException { + private: + int ruleIndex; + int predicateIndex; + const std::wstring predicate; + + public: + FailedPredicateException(Parser *recognizer); //this(recognizer, nullptr); + + FailedPredicateException(Parser *recognizer, const std::wstring &predicate); //this(recognizer, predicate, nullptr); + + FailedPredicateException(Parser *recognizer, const std::wstring &predicate, const std::wstring &message); + + virtual int getRuleIndex(); + + virtual int getPredIndex(); + + virtual std::wstring getPredicate(); + + private: + static std::wstring formatMessage(const std::wstring &predicate, const std::wstring &message); + }; + + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/IErrorListener.h b/runtime/Cpp/org/antlr/v4/runtime/IErrorListener.h new file mode 100644 index 000000000..24f917aa1 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/IErrorListener.h @@ -0,0 +1,48 @@ +#pragma once + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + class IErrorListener { + public: + + + }; + + } + } + } +} \ No newline at end of file diff --git a/runtime/Cpp/org/antlr/v4/runtime/IRecognizer.h b/runtime/Cpp/org/antlr/v4/runtime/IRecognizer.h new file mode 100644 index 000000000..ac9a31cad --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/IRecognizer.h @@ -0,0 +1,50 @@ +#pragma once + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + template + class IRecognizer { + public: + + virtual int getState() = 0; + + }; + + } + } + } +} \ No newline at end of file diff --git a/runtime/Cpp/org/antlr/v4/runtime/InputMismatchException.cpp b/runtime/Cpp/org/antlr/v4/runtime/InputMismatchException.cpp new file mode 100755 index 000000000..1056d730f --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/InputMismatchException.cpp @@ -0,0 +1,48 @@ +#include "InputMismatchException.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + InputMismatchException::InputMismatchException(Parser *recognizer) + : RecognitionException(recognizer, + recognizer->getInputStream(), + recognizer->_ctx) { + this->setOffendingToken(recognizer->getCurrentToken()); + } + + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/InputMismatchException.h b/runtime/Cpp/org/antlr/v4/runtime/InputMismatchException.h new file mode 100755 index 000000000..09fdb0efa --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/InputMismatchException.h @@ -0,0 +1,53 @@ +#pragma once + +#include "RecognitionException.h" +#include "Parser.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + /// + /// This signifies any kind of mismatched input exceptions such as + /// when the current input does not match the expected token. + /// + class InputMismatchException : public RecognitionException { + public: + InputMismatchException(Parser *recognizer); + }; + + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/IntStream.cpp b/runtime/Cpp/org/antlr/v4/runtime/IntStream.cpp new file mode 100755 index 000000000..8a3303ffe --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/IntStream.cpp @@ -0,0 +1,41 @@ +#include "IntStream.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + const std::wstring IntStream::UNKNOWN_SOURCE_NAME = L""; + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/IntStream.h b/runtime/Cpp/org/antlr/v4/runtime/IntStream.h new file mode 100755 index 000000000..9e306c528 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/IntStream.h @@ -0,0 +1,249 @@ +#pragma once + +#include +#include + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + + /// + /// A simple stream of symbols whose values are represented as integers. This + /// interface provides marked ranges with support for a minimum level + /// of buffering necessary to implement arbitrary lookahead during prediction. + /// For more information on marked ranges, see . + ///

+ /// Initializing Methods: Some methods in this interface have + /// unspecified behavior if no call to an initializing method has occurred after + /// the stream was constructed. The following is a list of initializing methods: + /// + ///

    + ///
  • + ///
  • + ///
  • + ///
+ ///
+ class IntStream { + /// + /// The value returned by when the end of the stream is + /// reached. + /// + public: +// EOF Conflict with OS X, change to _EOF + static const int _EOF = std::ios::eofbit; + + /// + /// The value returned by when the actual name of the + /// underlying source is not known. + /// + static const std::wstring UNKNOWN_SOURCE_NAME; + + /// + /// Consumes the current symbol in the stream. This method has the following + /// effects: + /// + ///
    + ///
  • Forward movement: The value of + /// before calling this method is less than the value of {@code index()} + /// after calling this method.
  • + ///
  • Ordered lookahead: The value of {@code LA(1)} before + /// calling this method becomes the value of {@code LA(-1)} after calling + /// this method.
  • + ///
+ /// + /// Note that calling this method does not guarantee that {@code index()} is + /// incremented by exactly 1, as that would preclude the ability to implement + /// filtering streams (e.g. which distinguishes + /// between "on-channel" and "off-channel" tokens). + ///
+ /// if an attempt is made to consume the the + /// end of the stream (i.e. if {@code LA(1)==} before calling + /// {@code consume}). + virtual void consume() = 0; + + /// + /// Gets the value of the symbol at offset {@code i} from the current + /// position. When {@code i==1}, this method returns the value of the current + /// symbol in the stream (which is the next symbol to be consumed). When + /// {@code i==-1}, this method returns the value of the previously read + /// symbol in the stream. It is not valid to call this method with + /// {@code i==0}, but the specific behavior is unspecified because this + /// method is frequently called from performance-critical code. + ///

+ /// This method is guaranteed to succeed if any of the following are true: + /// + ///

    + ///
  • {@code i>0}
  • + ///
  • {@code i==-1} and returns a value greater + /// than the value of {@code index()} after the stream was constructed + /// and {@code LA(1)} was called in that order. Specifying the current + /// {@code index()} relative to the index after the stream was created + /// allows for filtering implementations that do not return every symbol + /// from the underlying source. Specifying the call to {@code LA(1)} + /// allows for lazily initialized streams.
  • + ///
  • {@code LA(i)} refers to a symbol consumed within a marked region + /// that has not yet been released.
  • + ///
+ /// + /// If {@code i} represents a position at or beyond the end of the stream, + /// this method returns . + ///

+ /// The return value is unspecified if {@code i<0} and fewer than {@code -i} + /// calls to have occurred from the beginning of + /// the stream before calling this method. + ///

+ /// if the stream does not support + /// retrieving the value of the specified symbol + virtual int LA(int i) = 0; + + /// + /// A mark provides a guarantee that operations will be + /// valid over a "marked range" extending from the index where {@code mark()} + /// was called to the current . This allows the use of + /// streaming input sources by specifying the minimum buffering requirements + /// to support arbitrary lookahead during prediction. + ///

+ /// The returned mark is an opaque handle (type {@code int}) which is passed + /// to when the guarantees provided by the marked + /// range are no longer necessary. When calls to + /// {@code mark()}/{@code release()} are nested, the marks must be released + /// in reverse order of which they were obtained. Since marked regions are + /// used during performance-critical sections of prediction, the specific + /// behavior of invalid usage is unspecified (i.e. a mark is not released, or + /// a mark is released twice, or marks are not released in reverse order from + /// which they were created). + ///

+ /// The behavior of this method is unspecified if no call to an + /// has occurred after this stream was + /// constructed. + ///

+ /// This method does not change the current position in the input stream. + ///

+ /// The following example shows the use of , + /// , , and + /// as part of an operation to safely work within a + /// marked region, then restore the stream position to its original value and + /// release the mark. + ///

+                    /// IntStream stream = ...;
+                    /// int index = -1;
+                    /// int mark = stream.mark();
+                    /// try {
+                    ///   index = stream.index();
+                    ///   // perform work here...
+                    /// } finally {
+                    ///   if (index != -1) {
+                    ///     stream.seek(index);
+                    ///   }
+                    ///   stream.release(mark);
+                    /// }
+                    /// 
+ ///
+ /// An opaque marker which should be passed to + /// when the marked range is no longer required. + virtual int mark() = 0; + + /// + /// This method releases a marked range created by a call to + /// . Calls to {@code release()} must appear in the + /// reverse order of the corresponding calls to {@code mark()}. If a mark is + /// released twice, or if marks are not released in reverse order of the + /// corresponding calls to {@code mark()}, the behavior is unspecified. + ///

+ /// For more information and an example, see . + ///

+ /// A marker returned by a call to {@code mark()}. + /// + virtual void release(int marker) = 0; + + /// + /// Return the index into the stream of the input symbol referred to by + /// {@code LA(1)}. + ///

+ /// The behavior of this method is unspecified if no call to an + /// has occurred after this stream was + /// constructed. + ///

+ virtual int index() = 0; + + /// + /// Set the input cursor to the position indicated by {@code index}. If the + /// specified index lies past the end of the stream, the operation behaves as + /// though {@code index} was the index of the EOF symbol. After this method + /// returns without throwing an exception, the at least one of the following + /// will be true. + /// + ///
    + ///
  • will return the index of the first symbol + /// appearing at or after the specified {@code index}. Specifically, + /// implementations which filter their sources should automatically + /// adjust {@code index} forward the minimum amount required for the + /// operation to target a non-ignored symbol.
  • + ///
  • {@code LA(1)} returns
  • + ///
+ /// + /// This operation is guaranteed to not throw an exception if {@code index} + /// lies within a marked region. For more information on marked regions, see + /// . The behavior of this method is unspecified if no call to + /// an has occurred after this stream + /// was constructed. + ///
+ /// The absolute index to seek to. + /// + /// if {@code index} is less than 0 + /// if the stream does not support + /// seeking to the specified index + virtual void seek(int index) = 0; + + /// + /// Returns the total number of symbols in the stream, including a single EOF + /// symbol. + /// + /// if the size of the stream is + /// unknown. + virtual size_t size() = 0; + + /// + /// Gets the name of the underlying symbol source. This method returns a + /// non-null, non-empty string. If such a name is not known, this method + /// returns . + /// + virtual std::string getSourceName() = 0; + }; + + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/InterpreterRuleContext.cpp b/runtime/Cpp/org/antlr/v4/runtime/InterpreterRuleContext.cpp new file mode 100755 index 000000000..9d7be327b --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/InterpreterRuleContext.cpp @@ -0,0 +1,46 @@ +#include "InterpreterRuleContext.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + InterpreterRuleContext::InterpreterRuleContext(ParserRuleContext *parent, int invokingStateNumber, int ruleIndex) : ParserRuleContext(parent, invokingStateNumber), ruleIndex(ruleIndex) { + } + + int InterpreterRuleContext::getRuleIndex() { + return ruleIndex; + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/InterpreterRuleContext.h b/runtime/Cpp/org/antlr/v4/runtime/InterpreterRuleContext.h new file mode 100755 index 000000000..7b679256f --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/InterpreterRuleContext.h @@ -0,0 +1,58 @@ +#pragma once + +#include "ParserRuleContext.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + /// + /// This object is used by the ParserInterpreter and is the same as a regular + /// ParserRuleContext except that we need to track the rule index of the + /// current context so that we can build parse trees. + /// + class InterpreterRuleContext : public ParserRuleContext { + private: + const int ruleIndex; + + public: + InterpreterRuleContext(ParserRuleContext *parent, int invokingStateNumber, int ruleIndex); + + virtual int getRuleIndex() override; + }; + + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/Lexer.cpp b/runtime/Cpp/org/antlr/v4/runtime/Lexer.cpp new file mode 100755 index 000000000..f40aeaa1c --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/Lexer.cpp @@ -0,0 +1,365 @@ +#include "Lexer.h" + +#include "LexerATNSimulator.h" +#include "Exceptions.h" +#include "ANTLRErrorListener.h" +#include "Interval.h" +#include "StringBuilder.h" +#include "CommonTokenFactory.h" +#include "LexerNoViableAltException.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + + Lexer::Lexer() { + InitializeInstanceFields(); + } + + Lexer::Lexer(CharStream *input) { + InitializeInstanceFields(); + this->_input = input; + this->_tokenFactorySourcePair = new std::pair(this, input); + } + + void Lexer::reset() { + // wack Lexer state variables + if (_input != nullptr) { + _input->seek(0); // rewind the input + } + delete _token; + _type = Token::INVALID_TYPE; + _channel = Token::DEFAULT_CHANNEL; + _tokenStartCharIndex = -1; + _tokenStartCharPositionInLine = -1; + _tokenStartLine = -1; + _text = L""; + + _hitEOF = false; + _mode = Lexer::DEFAULT_MODE; + _modeStack.clear(); + + getInterpreter()->reset(); + } + + Token *Lexer::nextToken() { + if (_input == nullptr) { + throw new IllegalStateException(L"nextToken requires a non-null input stream."); + } + + // Mark start location in char stream so unbuffered streams are + // guaranteed at least have text of current token + int tokenStartMarker = _input->mark(); + try { + while (true) { + outerContinue: + if (_hitEOF) { + emitEOF(); + return _token; + } + + delete _token; + _channel = Token::DEFAULT_CHANNEL; + _tokenStartCharIndex = _input->index(); + _tokenStartCharPositionInLine = getInterpreter()->getCharPositionInLine(); + _tokenStartLine = getInterpreter()->getLine(); + _text = L""; + do { + _type = Token::INVALID_TYPE; + // System.out.println("nextToken line "+tokenStartLine+" at "+((char)input.LA(1))+ + // " in mode "+mode+ + // " at index "+input.index()); + int ttype; + try { + ttype = getInterpreter()->match(_input, _mode); + } catch (LexerNoViableAltException *e) { + notifyListeners(e); // report error + recover(e); + ttype = SKIP; + } + if (_input->LA(1) == IntStream::_EOF) { + _hitEOF = true; + } + if (_type == Token::INVALID_TYPE) { + _type = ttype; + } + if (_type == SKIP) { + goto outerContinue; + } + } while (_type == MORE); + if (_token == nullptr) { + emit(); + } + return _token; + } + + } + catch(...) { +#ifdef TODO + // Do something intelligent here for once +#endif + } + + // make sure we release marker after match or + // unbuffered char stream will keep buffering + _input->release(tokenStartMarker); + return nullptr; + } + + void Lexer::skip() { + _type = SKIP; + } + + void Lexer::more() { + _type = MORE; + } + + void Lexer::mode(int m) { + _mode = m; + } + + void Lexer::pushMode(int m) { + if (atn::LexerATNSimulator::debug) { + std::wcout << std::wstring(L"pushMode ") << m << std::endl; + } + _modeStack.push_back(_mode); + mode(m); + } + + int Lexer::popMode() { + if (_modeStack.empty()) { + throw EmptyStackException(); + } + if (atn::LexerATNSimulator::debug) { + std::wcout << std::wstring(L"popMode back to ") << _modeStack.back() << std::endl; + } + mode(_modeStack.back()); + _modeStack.pop_back(); + return _mode; + } + + + TokenFactory *Lexer::getTokenFactory() { + return _factory; + } + + void Lexer::setInputStream(IntStream *input) { + delete this->_input; + this->_tokenFactorySourcePair = new std::pair(this, _input); + reset(); + this->_input = static_cast(input); + this->_tokenFactorySourcePair = new std::pair(this, _input); + } + + std::string Lexer::getSourceName() { + return _input->getSourceName(); + } + + CharStream *Lexer::getInputStream() { + return _input; + } + + void Lexer::emit(Token *token) { + //System.err.println("emit "+token); + this->_token = token; + } + + Token *Lexer::emit() { + Token *t = (Token*)_factory->create(_tokenFactorySourcePair, _type, _text, _channel, _tokenStartCharIndex, getCharIndex() - 1, _tokenStartLine, _tokenStartCharPositionInLine); + emit(t); + return t; + } + + Token *Lexer::emitEOF() { + int cpos = getCharPositionInLine(); + // The character position for EOF is one beyond the position of + // the previous token's last character + if (_token != nullptr) { + int n = _token->getStopIndex() - _token->getStartIndex() + 1; + cpos = _token->getCharPositionInLine() + n; + } + Token *eof = (Token*)_factory->create(_tokenFactorySourcePair, Token::_EOF, L"", Token::DEFAULT_CHANNEL, _input->index(), _input->index() - 1, getLine(), cpos); + emit(eof); + return eof; + } + + int Lexer::getLine() { + return getInterpreter()->getLine(); + } + + int Lexer::getCharPositionInLine() { + return getInterpreter()->getCharPositionInLine(); + } + + void Lexer::setLine(int line) { + getInterpreter()->setLine(line); + } + + void Lexer::setCharPositionInLine(int charPositionInLine) { + getInterpreter()->setCharPositionInLine(charPositionInLine); + } + + int Lexer::getCharIndex() { + return _input->index(); + } + + std::wstring Lexer::getText() { + if (_text != L"") { + return _text; + } + return getInterpreter()->getText(_input); + } + + void Lexer::setText(const std::wstring &text) { + this->_text = text; + } + + Token *Lexer::getToken() { + return _token; + } + + void Lexer::setToken(Token *_token) { + this->_token = _token; + } + + void Lexer::setType(int ttype) { + _type = ttype; + } + + int Lexer::getType() { + return _type; + } + + void Lexer::setChannel(int channel) { + _channel = channel; + } + + int Lexer::getChannel() { + return _channel; + } + + const std::vector& Lexer::getModeNames() { + return _modeNames; + } + + const std::vector& Lexer::getTokenNames() { + return _tokenNames; + } + + std::vector Lexer::getAllTokens() { + std::vector tokens = std::vector(); + Token *t = nextToken(); + while (t->getType() != Token::_EOF) { + tokens.push_back(t); + t = nextToken(); + } + return tokens; + } + + void Lexer::recover(LexerNoViableAltException *e) { + if (_input->LA(1) != IntStream::_EOF) { + // skip a char and try again + getInterpreter()->consume(_input); + } + } + + void Lexer::notifyListeners(LexerNoViableAltException *e) { + std::wstring text = _input->getText(misc::Interval::of(_tokenStartCharIndex, _input->index())); + std::wstring msg = std::wstring(L"token recognition error at: '") + getErrorDisplay(text) + std::wstring(L"'"); + + ANTLRErrorListener *listener = getErrorListenerDispatch(); + listener->syntaxError(this, nullptr, _tokenStartLine, _tokenStartCharPositionInLine, msg, e); + } + + std::wstring Lexer::getErrorDisplay(const std::wstring &s) { + antlrcpp::StringBuilder *buf = new antlrcpp::StringBuilder(); + + for (size_t i = 0; i < s.length(); i++) { + char c = ((char*)s.c_str())[i]; + buf->append(getErrorDisplay(c)); + } + /* + for (auto c : s.toCharArray()) { + buf->append(getErrorDisplay(c)); + }*/ + + return buf->toString(); + } + + std::wstring Lexer::getErrorDisplay(int c) { + std::wstring s = antlrcpp::StringConverterHelper::toString(static_cast(c)); + switch (c) { + case Token::_EOF : + s = L""; + break; + case L'\n' : + s = L"\\n"; + break; + case L'\t' : + s = L"\\t"; + break; + case L'\r' : + s = L"\\r"; + break; + } + return s; + } + + std::wstring Lexer::getCharErrorDisplay(int c) { + std::wstring s = getErrorDisplay(c); + return std::wstring(L"'") + s + std::wstring(L"'"); + } + + void Lexer::recover(RecognitionException *re) { + //System.out.println("consuming char "+(char)input.LA(1)+" during recovery"); + //re.printStackTrace(); + // TODO: Do we lose character or line position information? + _input->consume(); + } + + void Lexer::InitializeInstanceFields() { + _factory = CommonTokenFactory::DEFAULT; + _tokenStartCharIndex = -1; + _tokenStartLine = 0; + _tokenStartCharPositionInLine = 0; + _hitEOF = false; + _channel = 0; + _type = 0; + _mode = Lexer::DEFAULT_MODE; + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/Lexer.h b/runtime/Cpp/org/antlr/v4/runtime/Lexer.h new file mode 100755 index 000000000..78df10171 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/Lexer.h @@ -0,0 +1,268 @@ +#pragma once + +#include "Declarations.h" +#include "Recognizer.h" +#include "Token.h" +#include "TokenSource.h" +#include "stringconverter.h" + +#include +#include +#include +#include + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + /// + /// A lexer is recognizer that draws input symbols from a character stream. + /// lexer grammars result in a subclass of this object. A Lexer object + /// uses simplified match() and error recovery mechanisms in the interest + /// of speed. + /// + class Lexer : public Recognizer, public TokenSource { + public: + static const int DEFAULT_MODE = 0; + static const int MORE = -2; + static const int SKIP = -3; + + static const int DEFAULT_TOKEN_CHANNEL = Token::DEFAULT_CHANNEL; + static const int HIDDEN = Token::HIDDEN_CHANNEL; + static const wchar_t MIN_CHAR_VALUE = L'\0'; + static const wchar_t MAX_CHAR_VALUE = L'\uFFFE'; + + CharStream *_input; + protected: + std::pair *_tokenFactorySourcePair; + + std::vector _modeNames; + std::vector _tokenNames; + + /// + /// How to create token objects + TokenFactory *_factory; + + /// + /// The goal of all lexer rules/methods is to create a token object. + /// This is an instance variable as multiple rules may collaborate to + /// create a single token. nextToken will return this object after + /// matching lexer rule(s). If you subclass to allow multiple token + /// emissions, then set this to the last token to be matched or + /// something nonnull so that the auto token emit mechanism will not + /// emit another token. + /// + public: + Token *_token; + + /// + /// What character index in the stream did the current token start at? + /// Needed, for example, to get the text for current token. Set at + /// the start of nextToken. + /// + int _tokenStartCharIndex; + + /// + /// The line on which the first character of the token resides + int _tokenStartLine; + + /// + /// The character position of first character within the line + int _tokenStartCharPositionInLine; + + /// + /// Once we see EOF on char stream, next token will be EOF. + /// If you have DONE : EOF ; then you see DONE EOF. + /// + bool _hitEOF; + + /// + /// The channel number for the current token + int _channel; + + /// + /// The token type for the current token + int _type; + + // Use the vector as a stack. + std::vector _modeStack; + int _mode; + + /// + /// You can set the text for the current token to override what is in + /// the input char buffer. Use setText() or can set this instance var. + /// + std::wstring _text; + + Lexer(); + + Lexer(CharStream *input); + + virtual void reset(); + + /// + /// Return a token from this source; i.e., match a token on the char + /// stream. + /// + virtual Token *nextToken(); + + /// + /// Instruct the lexer to skip creating a token for current lexer rule + /// and look for another token. nextToken() knows to keep looking when + /// a lexer rule finishes with token set to SKIP_TOKEN. Recall that + /// if token==null at end of any token rule, it creates one for you + /// and emits it. + /// + virtual void skip(); + + virtual void more(); + + virtual void mode(int m); + + virtual void pushMode(int m); + + virtual int popMode(); + + template + void setTokenFactory(TokenFactory *factory) { + this->_factory = factory; + } + + virtual TokenFactory *getTokenFactory() override; + + /// + /// Set the char stream and reset the lexer + virtual void setInputStream(IntStream *input); + + virtual std::string getSourceName() override; + + virtual CharStream *getInputStream() override; + + /// + /// By default does not support multiple emits per nextToken invocation + /// for efficiency reasons. Subclass and override this method, nextToken, + /// and getToken (to push tokens into a list and pull from that list + /// rather than a single variable as this implementation does). + /// + virtual void emit(Token *token); + + /// + /// The standard method called to automatically emit a token at the + /// outermost lexical rule. The token object should point into the + /// char buffer start..stop. If there is a text override in 'text', + /// use that to set the token's text. Override this method to emit + /// custom Token objects or provide a new factory. + /// + virtual Token *emit(); + + virtual Token *emitEOF(); + + virtual int getLine() override; + + virtual int getCharPositionInLine() override; + + virtual void setLine(int line); + + virtual void setCharPositionInLine(int charPositionInLine); + + /// + /// What is the index of the current character of lookahead? + virtual int getCharIndex(); + + /// + /// Return the text matched so far for the current token or any + /// text override. + /// + virtual std::wstring getText(); + + /// + /// Set the complete text of this token; it wipes any previous + /// changes to the text. + /// + virtual void setText(const std::wstring &text); + + /// + /// Override if emitting multiple tokens. + virtual Token *getToken(); + + virtual void setToken(Token *_token); + + virtual void setType(int ttype); + + virtual int getType(); + + virtual void setChannel(int channel); + + virtual int getChannel(); + + virtual const std::vector& getModeNames(); + + /// + /// 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. + /// + virtual const std::vector& getTokenNames() override; + + /// + /// Return a list of all Token objects in input char stream. + /// Forces load of all tokens. Does not include EOF token. + /// + virtual std::vector getAllTokens(); + + virtual void recover(LexerNoViableAltException *e); + + virtual void notifyListeners(LexerNoViableAltException *e); + + virtual std::wstring getErrorDisplay(const std::wstring &s); + + virtual std::wstring getErrorDisplay(int c); + + virtual std::wstring getCharErrorDisplay(int c); + + /// + /// Lexers can normally match any char in it's vocabulary after matching + /// a token, so do the easy thing and just kill a character and hope + /// it all works out. You can instead use the rule invocation stack + /// to do sophisticated error recovery if you are in a fragment rule. + /// + virtual void recover(RecognitionException *re); + + private: + void InitializeInstanceFields(); + }; + + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/LexerInterpreter.cpp b/runtime/Cpp/org/antlr/v4/runtime/LexerInterpreter.cpp new file mode 100755 index 000000000..06c1a1213 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/LexerInterpreter.cpp @@ -0,0 +1,86 @@ +#include "LexerInterpreter.h" +#include "ATNType.h" +#include "LexerATNSimulator.h" +#include "ATN.h" +#include "DFA.h" +#include "Exceptions.h" +#include "PredictionContextCache.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + LexerInterpreter::LexerInterpreter(const std::wstring &grammarFileName, std::vector *tokenNames, std::vector *ruleNames, std::vector *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."); + } + + + for (int i = 0; i < (int)_decisionToDFA.size(); i++) { + _decisionToDFA[i] = new dfa::DFA(atn->getDecisionState(i), i); + } + this->_interp = new atn::LexerATNSimulator(atn,_decisionToDFA,_sharedContextCache); + if (tokenNames) { + _tokenNames = *tokenNames; + } + if (ruleNames) { + _ruleNames = *ruleNames; + } + if (modeNames) { + _modeNames = *modeNames; + } + } + atn::ATN *LexerInterpreter::getATN() { + return atn; + } + + std::wstring LexerInterpreter::getGrammarFileName() { + return grammarFileName; + } + + const std::vector& LexerInterpreter::getTokenNames() { + return _tokenNames; + } + + const std::vector& LexerInterpreter::getRuleNames() { + return _ruleNames; + } + + const std::vector& LexerInterpreter::getModeNames() { + return _modeNames; + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/LexerInterpreter.h b/runtime/Cpp/org/antlr/v4/runtime/LexerInterpreter.h new file mode 100755 index 000000000..e70309ca9 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/LexerInterpreter.h @@ -0,0 +1,75 @@ +#pragma once + +#include "Lexer.h" +#include "Declarations.h" +#include +#include + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + class LexerInterpreter : public Lexer { + protected: + const std::wstring grammarFileName; + atn::ATN *const atn; + + + std::vector _tokenNames; + + std::vector _ruleNames; + + std::vector _modeNames; + + std::vector _decisionToDFA; + + atn::PredictionContextCache *const _sharedContextCache; + + public: + LexerInterpreter(const std::wstring &grammarFileName, std::vector *tokenNames, std::vector *ruleNames, std::vector *modeNames, atn::ATN *atn, CharStream *input); + + virtual atn::ATN *getATN() override; + + virtual std::wstring getGrammarFileName() override; + + virtual const std::vector& getTokenNames() override; + + virtual const std::vector& getRuleNames() override; + + virtual const std::vector& getModeNames() override; + }; + + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/LexerNoViableAltException.cpp b/runtime/Cpp/org/antlr/v4/runtime/LexerNoViableAltException.cpp new file mode 100755 index 000000000..a73c563e9 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/LexerNoViableAltException.cpp @@ -0,0 +1,73 @@ +#include "LexerNoViableAltException.h" +#include "RecognitionException.h" +#include "Interval.h" +#include "CPPUtils.h" +#include "CharStream.h" + +#include +#include + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + + LexerNoViableAltException::LexerNoViableAltException(Lexer *lexer, CharStream *input, int startIndex, atn::ATNConfigSet *deadEndConfigs) : RecognitionException()/*TODO RecognitionException(lexer, input, nullptr)*/, startIndex(startIndex), deadEndConfigs(deadEndConfigs) { + } + + int LexerNoViableAltException::getStartIndex() { + return startIndex; + } + + atn::ATNConfigSet *LexerNoViableAltException::getDeadEndConfigs() { + return deadEndConfigs; + } + + runtime::CharStream *LexerNoViableAltException::getInputStream() { + return (CharStream*)(RecognitionException::getInputStream()); + } + + std::wstring LexerNoViableAltException::toString() { + std::wstring symbol = L""; + if (startIndex >= 0 && startIndex < (int)getInputStream()->size()) { + symbol = getInputStream()->getText(misc::Interval::of(startIndex,startIndex)); + symbol = antlrcpp::escapeWhitespace(symbol, false); + } + std::wstring format = L"LexerNoViableAltException('" + symbol + L"')"; + return format; + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/LexerNoViableAltException.h b/runtime/Cpp/org/antlr/v4/runtime/LexerNoViableAltException.h new file mode 100755 index 000000000..88c3465c9 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/LexerNoViableAltException.h @@ -0,0 +1,72 @@ +#pragma once + +#include + +#include "RecognitionException.h" +#include "Declarations.h" + + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "CharStream.h" + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + + class LexerNoViableAltException : public RecognitionException { + /// + /// Matching attempted at what input index? + private: + const int startIndex; + + /// + /// Which configurations did we try at input.index() that couldn't match input.LA(1)? + atn::ATNConfigSet *const deadEndConfigs; + + public: + LexerNoViableAltException(Lexer *lexer, CharStream *input, int startIndex, atn::ATNConfigSet *deadEndConfigs); + + virtual int getStartIndex(); + + virtual atn::ATNConfigSet *getDeadEndConfigs(); + + virtual CharStream *getInputStream() override; + + virtual std::wstring toString(); + }; + + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/ListTokenSource.cpp b/runtime/Cpp/org/antlr/v4/runtime/ListTokenSource.cpp new file mode 100755 index 000000000..901214ee2 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/ListTokenSource.cpp @@ -0,0 +1,157 @@ +#include "ListTokenSource.h" +#include "Token.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + int ListTokenSource::getCharPositionInLine() { + if (i < tokens.size()) { + return ((Token*)tokens[i])->getCharPositionInLine(); + } else if (eofToken != nullptr) { + return eofToken->getCharPositionInLine(); + } else if (tokens.size() > 0) { + // have to calculate the result from the line/column of the previous + // token, along with the text of the token. + Token *lastToken = tokens[tokens.size() - 1]; + std::wstring tokenText = lastToken->getText(); + if (tokenText != L"") { + int lastNewLine = (int)tokenText.rfind(L'\n'); + if (lastNewLine >= 0) { + return (int)tokenText.length() - lastNewLine - 1; + } + } + + return lastToken->getCharPositionInLine() + lastToken->getStopIndex() - lastToken->getStartIndex() + 1; + } + + // only reach this if tokens is empty, meaning EOF occurs at the first + // position in the input + return 0; + } + + Token *ListTokenSource::nextToken() { + if (i >= tokens.size()) { + if (eofToken == nullptr) { + int start = -1; + if (tokens.size() > 0) { + int previousStop = ((Token*)tokens[tokens.size() - 1])->getStopIndex(); + if (previousStop != -1) { + start = previousStop + 1; + } + } + + int stop = std::max(-1, start - 1); + eofToken = _factory->create(new std::pair(this, getInputStream()), Token::_EOF, L"EOF", Token::DEFAULT_CHANNEL, start, stop, getLine(), getCharPositionInLine()); + } + + return eofToken; + } + + Token *t = tokens[i]; + if (i == tokens.size() - 1 && t->getType() == Token::_EOF) { + eofToken = t; + } + + i++; + return t; + } + + int ListTokenSource::getLine() { + if (i < tokens.size()) { + return tokens[i]->getLine(); + } else if (eofToken != nullptr) { + return eofToken->getLine(); + } else if (tokens.size() > 0) { + // have to calculate the result from the line/column of the previous + // token, along with the text of the token. + Token *lastToken = tokens[tokens.size() - 1]; + int line = lastToken->getLine(); + + std::wstring tokenText = lastToken->getText(); + if (tokenText != L"") { + for (size_t i = 0; i < tokenText.length(); i++) { + if (tokenText[i] == L'\n') { + line++; + } + } + } + + // if no text is available, assume the token did not contain any newline characters. + return line; + } + + // only reach this if tokens is empty, meaning EOF occurs at the first + // position in the input + return 1; + } + + CharStream *ListTokenSource::getInputStream() { + if (i < tokens.size()) { + return tokens[i]->getInputStream(); + } else if (eofToken != nullptr) { + return eofToken->getInputStream(); + } else if (tokens.size() > 0) { + return tokens[tokens.size() - 1]->getInputStream(); + } + + // no input stream information is available + return nullptr; + } + + std::string ListTokenSource::getSourceName() { + if (sourceName != "") { + return sourceName; + } + + CharStream *inputStream = getInputStream(); + if (inputStream != nullptr) { + return inputStream->getSourceName(); + } + + return "List"; + } + + TokenFactory *ListTokenSource::getTokenFactory() { + return _factory; + } + + void ListTokenSource::InitializeInstanceFields() { + i = 0; + _factory = CommonTokenFactory::DEFAULT; + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/ListTokenSource.h b/runtime/Cpp/org/antlr/v4/runtime/ListTokenSource.h new file mode 100755 index 000000000..60df152b3 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/ListTokenSource.h @@ -0,0 +1,170 @@ +#pragma once + +#include "TokenSource.h" +#include "TokenFactory.h" +#include "CharStream.h" +#include "CommonTokenFactory.h" +#include +#include +#include +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + /// + /// Provides an implementation of as a wrapper around a list + /// of objects. + ///

+ /// If the final token in the list is an token, it will be used + /// as the EOF token for every call to after the end of the + /// list is reached. Otherwise, an EOF token will be created. + ///

+ class ListTokenSource : public TokenSource { + /// + /// The wrapped collection of objects to return. + /// + protected: + const std::vector tokens; + + /// + /// The name of the input source. If this value is {@code null}, a call to + /// should return the source name used to create the + /// the next token in (or the previous token if the end of + /// the input has been reached). + /// + private: + const std::string sourceName; + + /// + /// The index into of token to return by the next call to + /// . The end of the input is indicated by this value + /// being greater than or equal to the number of items in . + /// + protected: + size_t i; + + /// + /// This field caches the EOF token for the token source. + /// + Token *eofToken; + + /// + /// This is the backing field for and + /// . + /// + private: + TokenFactory *_factory = CommonTokenFactory::DEFAULT; + + /// + /// Constructs a new instance from the specified + /// collection of objects. + /// + /// The collection of objects to provide as a + /// . + /// if {@code tokens} is {@code null} + public: + + template //where T1 : Token + ListTokenSource(std::vector tokens) //this(tokens, nullptr); + {} + /// + /// Constructs a new instance from the specified + /// collection of objects and source name. + /// + /// The collection of objects to provide as a + /// . + /// The name of the . If this value is + /// {@code null}, will attempt to infer the name from + /// the next (or the previous token if the end of the input has + /// been reached). + /// + /// if {@code tokens} is {@code null} + + template //where T1 : Token + ListTokenSource(std::vector tokens, const std::string &sourceName) { + InitializeInstanceFields(); + if (tokens.empty()) { + throw L"tokens cannot be null"; + } + + } + + /// + /// @inheritDoc + /// + virtual int getCharPositionInLine() override; + + /// + /// @inheritDoc + /// + virtual Token *nextToken() override; + + /// + /// @inheritDoc + /// + virtual int getLine() override; + + /// + /// @inheritDoc + /// + virtual CharStream *getInputStream() override; + + /// + /// @inheritDoc + /// + virtual std::string getSourceName() override; + + /// + /// @inheritDoc + /// + template + void setTokenFactory(TokenFactory *factory) { + this->_factory = factory; + } + + /// + /// @inheritDoc + /// + virtual TokenFactory *getTokenFactory(); + + private: + void InitializeInstanceFields(); + }; + + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/NoViableAltException.cpp b/runtime/Cpp/org/antlr/v4/runtime/NoViableAltException.cpp new file mode 100755 index 000000000..f25186ffa --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/NoViableAltException.cpp @@ -0,0 +1,63 @@ +#include "NoViableAltException.h" + +#include "atn/ATNConfigSet.h" +#include "Token.h" +#include "Parser.h" +#include "ParserRuleContext.h" +#include "TokenStream.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + + NoViableAltException::NoViableAltException(Parser *recognizer) :deadEndConfigs(nullptr), startToken(nullptr) { + } + + NoViableAltException::NoViableAltException(Parser *recognizer, TokenStream *input, Token *startToken, Token *offendingToken, ATNConfigSet *deadEndConfigs, ParserRuleContext *ctx) : RecognitionException(recognizer, input, ctx), + deadEndConfigs(deadEndConfigs), startToken(startToken) { + this->setOffendingToken(offendingToken); + } + + Token *NoViableAltException::getStartToken() { + return startToken; + } + + atn::ATNConfigSet *NoViableAltException::getDeadEndConfigs() { + return deadEndConfigs; + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/NoViableAltException.h b/runtime/Cpp/org/antlr/v4/runtime/NoViableAltException.h new file mode 100755 index 000000000..1558a6e2f --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/NoViableAltException.h @@ -0,0 +1,77 @@ +#pragma once + +#include "RecognitionException.h" + + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + using org::antlr::v4::runtime::atn::ATNConfigSet; + using org::antlr::v4::runtime::misc::NotNull; + + /// + /// Indicates that the parser could not decide which of two or more paths + /// to take based upon the remaining input. It tracks the starting token + /// of the offending input and also knows where the parser was + /// in the various paths when the error. Reported by reportNoViableAlternative() + /// + class NoViableAltException : public RecognitionException { + /// + /// Which configurations did we try at input.index() that couldn't match input.LT(1)? + private: + ATNConfigSet *const deadEndConfigs; + + /// + /// The token object at the start index; the input stream might + /// not be buffering tokens so get a reference to it. (At the + /// time the error occurred, of course the stream needs to keep a + /// buffer all of the tokens but later we might not have access to those.) + /// + Token *const startToken; + + public: + NoViableAltException(Parser *recognizer); // LL(1) error - this(recognizer, recognizer.getInputStream(), recognizer.getCurrentToken(), recognizer.getCurrentToken(), nullptr, recognizer._ctx); + + NoViableAltException(Parser *recognizer, TokenStream *input, Token *startToken, Token *offendingToken, ATNConfigSet *deadEndConfigs, ParserRuleContext *ctx); + + virtual Token *getStartToken(); + + virtual ATNConfigSet *getDeadEndConfigs(); + + }; + + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/Parser.cpp b/runtime/Cpp/org/antlr/v4/runtime/Parser.cpp new file mode 100755 index 000000000..6037808b7 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/Parser.cpp @@ -0,0 +1,625 @@ +#include +#include + +#include "Parser.h" +#include "ATNSimulator.h" +#include "ATNDeserializationOptions.h" +#include "ATNDeserializer.h" +#include "TokenSource.h" +#include "tree/pattern/ParseTreePatternMatcher.h" +#include "ANTLRErrorListener.h" +#include "ATNState.h" +#include "DFA.h" +#include "ParserRuleContext.h" +#include "Token.h" +#include "TerminalNode.h" +#include "TokenStream.h" +#include "ANTLRErrorStrategy.h" +#include "Exceptions.h" +#include "ParseTreeListener.h" +#include "ParseTree.h" +#include "Lexer.h" +#include "ATN.h" +#include "ParserATNSimulator.h" +#include "IntervalSet.h" +#include "RuleStartState.h" +#include "DefaultErrorStrategy.h" +#include "Strings.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + std::map * Parser::bypassAltsAtnCache = new std::map(); + + Parser::TraceListener::TraceListener(Parser *outerInstance) : outerInstance(outerInstance) { + } + + void Parser::TraceListener::enterEveryRule(ParserRuleContext *ctx) { + std::cout << "enter " + << antlrcpp::ws2s(outerInstance->getRuleNames()[ctx->getRuleIndex()]) + << ", LT(1)=" << antlrcpp::ws2s(outerInstance->_input->LT(1)->getText()) + << std::endl; + } + + void Parser::TraceListener::visitTerminal(tree::TerminalNode *node) { + std::cout << "consume " + << node->getSymbol() << " rule " + << antlrcpp::ws2s(outerInstance->getRuleNames()[outerInstance->_ctx->getRuleIndex()]) + << std::endl; + } + + void Parser::TraceListener::visitErrorNode(tree::ErrorNode *node) { + } + + void Parser::TraceListener::exitEveryRule(ParserRuleContext *ctx) { + std::cout << "exit " + << antlrcpp::ws2s(outerInstance->getRuleNames()[ctx->getRuleIndex()]) + << ", LT(1)=" << antlrcpp::ws2s(outerInstance->_input->LT(1)->getText()) + << std::endl; + } + + Parser::TrimToSizeListener *const Parser::TrimToSizeListener::INSTANCE = new Parser::TrimToSizeListener(); + + void Parser::TrimToSizeListener::enterEveryRule(ParserRuleContext *ctx) { + } + + void Parser::TrimToSizeListener::visitTerminal(tree::TerminalNode *node) { + } + + void Parser::TrimToSizeListener::visitErrorNode(tree::ErrorNode *node) { + } + + 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* tmp = dynamic_cast*>(&ctx->children); + if (tmp != nullptr) { + tmp->shrink_to_fit(); + } + } + + Parser::Parser(TokenStream* input) { + InitializeInstanceFields(); + setInputStream(input); + + // TODO: Initialize this safely and handle concurrent accesses. + // TODO: For now treat this as a member variable but it should be shared across instances for speed. + Parser::bypassAltsAtnCache = + new std::map(); + } + + void Parser::reset() { + if (getInputStream() != nullptr) { + getInputStream()->seek(0); + } + _errHandler->reset(this); + + delete _ctx; + _syntaxErrors = 0; + setTrace(false); + _precedenceStack.clear(); + _precedenceStack.push_back(0); + atn::ATNSimulator *interpreter = getInterpreter(); + if (interpreter != nullptr) { + interpreter->reset(); + } + } + + Token *Parser::match(int ttype) { + Token *t = getCurrentToken(); + if (t->getType() == ttype) { + _errHandler->reportMatch(this); + consume(); + } else { + t = _errHandler->recoverInline(this); + if (_buildParseTrees && t->getTokenIndex() == -1) { + // we must have conjured up a new token during single token insertion + // if it's not the current symbol + _ctx->addErrorNode(t); + } + } + return t; + } + + Token *Parser::matchWildcard() { + Token *t = getCurrentToken(); + if (t->getType() > 0) { + _errHandler->reportMatch(this); + consume(); + } else { + t = _errHandler->recoverInline(this); + if (_buildParseTrees && t->getTokenIndex() == -1) { + // we must have conjured up a new token during single token insertion + // if it's not the current symbol + _ctx->addErrorNode(t); + } + } + + return t; + } + + void Parser::setBuildParseTree(bool buildParseTrees) { + this->_buildParseTrees = buildParseTrees; + } + + bool Parser::getBuildParseTree() { + return _buildParseTrees; + } + + void Parser::setTrimParseTree(bool trimParseTrees) { + if (trimParseTrees) { + if (getTrimParseTree()) { + return; + } + addParseListener(TrimToSizeListener::INSTANCE); + } else { + removeParseListener(TrimToSizeListener::INSTANCE); + } + } + + bool Parser::getTrimParseTree() { + return std::find(getParseListeners().begin(), getParseListeners().end(), TrimToSizeListener::INSTANCE) != getParseListeners().end(); + } + + std::vector Parser::getParseListeners() { + std::vector listeners = _parseListeners; + if (listeners.empty()) { + std::vector emptyList; + return emptyList; + } + + return listeners; + } + + void Parser::addParseListener(tree::ParseTreeListener *listener) { + if (listener == nullptr) { + throw NullPointerException(L"listener"); + } + + if (_parseListeners.empty()) { + _parseListeners = std::vector(); + } + + this->_parseListeners.push_back(listener); + } + + void Parser::removeParseListener(tree::ParseTreeListener *listener) { + if (!_parseListeners.empty()) { + auto it = std::find(_parseListeners.begin(), _parseListeners.end(), listener); + if (it != _parseListeners.end()) { + _parseListeners.erase(it); + } + } + } + + void Parser::removeParseListeners() { + _parseListeners.clear(); + } + + void Parser::triggerEnterRuleEvent() { + for (auto listener : _parseListeners) { + listener->enterEveryRule(_ctx); + _ctx->enterRule(listener); + } + } + + void Parser::triggerExitRuleEvent() { + // reverse order walk of listeners + for (auto it = _parseListeners.rbegin(); it != _parseListeners.rend(); ++it) { + tree::ParseTreeListener *listener = *it; + _ctx->exitRule(*it); + listener->exitEveryRule(_ctx); + } + } + + int Parser::getNumberOfSyntaxErrors() { + return _syntaxErrors; + } + + TokenFactory *Parser::getTokenFactory() { + return _input->getTokenSource()->getTokenFactory(); + } + + + atn::ATN *Parser::getATNWithBypassAlts() { + std::wstring serializedAtn = getSerializedATN(); + if (serializedAtn == L"") { + throw UnsupportedOperationException(L"The current parser does not support an ATN with bypass alternatives."); + } + + if (bypassAltsAtnCache != nullptr) { + std::lock_guard lck(mtx); + atn::ATN *result = bypassAltsAtnCache->at(serializedAtn); + if (result == nullptr) { + atn::ATNDeserializationOptions *deserializationOptions = new atn::ATNDeserializationOptions(); + deserializationOptions->setGenerateRuleBypassTransitions(true); + result = (new atn::ATNDeserializer(deserializationOptions))->deserialize(serializedAtn); + bypassAltsAtnCache->emplace(serializedAtn, result); + } + + return result; + } + return nullptr; + } + + tree::pattern::ParseTreePattern *Parser::compileParseTreePattern(const std::wstring &pattern, int patternRuleIndex) { + if (getTokenStream() != nullptr) { + TokenSource *tokenSource = getTokenStream()->getTokenSource(); + if (dynamic_cast(tokenSource) != nullptr) { + Lexer *lexer = static_cast(tokenSource); + return compileParseTreePattern(pattern, patternRuleIndex, lexer); + } + } + throw UnsupportedOperationException(L"Parser can't discover a lexer to use"); + } + + tree::pattern::ParseTreePattern *Parser::compileParseTreePattern(const std::wstring &pattern, int patternRuleIndex, Lexer *lexer) { + tree::pattern::ParseTreePatternMatcher *m = new tree::pattern::ParseTreePatternMatcher(lexer, this); + return m->compile(pattern, patternRuleIndex); + } + + ANTLRErrorStrategy *Parser::getErrorHandler() { + return _errHandler; + } + + void Parser::setErrorHandler(ANTLRErrorStrategy *handler) { + this->_errHandler = handler; + } + + TokenStream *Parser::getInputStream() { + return getTokenStream(); + } + + void Parser::setInputStream(IntStream *input) { + setTokenStream(static_cast(input)); + } + + TokenStream *Parser::getTokenStream() { + return _input; + } + + void Parser::setTokenStream(TokenStream *input) { + delete this->_input; + reset(); + this->_input = input; + } + + Token *Parser::getCurrentToken() { + return _input->LT(1); + } + + void Parser::notifyErrorListeners(const std::wstring &msg) { + notifyErrorListeners(getCurrentToken(), msg, nullptr); + } + + void Parser::notifyErrorListeners(Token *offendingToken, const std::wstring &msg, RecognitionException *e) { + _syntaxErrors++; + int line = -1; + int charPositionInLine = -1; + line = offendingToken->getLine(); + charPositionInLine = offendingToken->getCharPositionInLine(); + + ANTLRErrorListener *listener = getErrorListenerDispatch(); + listener->syntaxError(this, offendingToken, line, charPositionInLine, msg, e); + } + + Token *Parser::consume() { + Token *o = getCurrentToken(); + if (o->getType() != EOF) { + getInputStream()->consume(); + } + bool hasListener = _parseListeners.size() > 0 && !_parseListeners.empty(); + if (_buildParseTrees || hasListener) { + if (_errHandler->inErrorRecoveryMode(this)) { + tree::ErrorNode *node = _ctx->addErrorNode(o); + if (_parseListeners.size() > 0) { + for (auto listener : _parseListeners) { + listener->visitErrorNode(node); + } + } + } else { + tree::TerminalNode *node = _ctx->addChild(o); + if (_parseListeners.size() > 0) { + for (auto listener : _parseListeners) { + listener->visitTerminal(node); + } + } + } + } + return o; + } + + void Parser::addContextToParseTree() { + ParserRuleContext *parent = static_cast(_ctx->parent); + // add current context to parent if we have a parent + if (parent != nullptr) { + parent->addChild(_ctx); + } + } + + void Parser::enterRule(ParserRuleContext *localctx, int state, int ruleIndex) { + setState(state); + _ctx = localctx; + _ctx->start = _input->LT(1); + if (_buildParseTrees) { + addContextToParseTree(); + } + if (_parseListeners.size() > 0) { + triggerEnterRuleEvent(); + } + } + + void Parser::exitRule() { + _ctx->stop = _input->LT(-1); + // trigger event on _ctx, before it reverts to parent + if (_parseListeners.size() > 0) { + triggerExitRuleEvent(); + } + setState(_ctx->invokingState); + _ctx = static_cast(_ctx->parent); + } + + void Parser::enterOuterAlt(ParserRuleContext *localctx, int altNum) { + // if we have new localctx, make sure we replace existing ctx + // that is previous child of parse tree + if (_buildParseTrees && _ctx != localctx) { + ParserRuleContext *parent = static_cast(_ctx->parent); + if (parent != nullptr) { + parent->removeLastChild(); + parent->addChild(localctx); + } + } + _ctx = localctx; + } + + void Parser::enterRecursionRule(ParserRuleContext *localctx, int ruleIndex) { + enterRecursionRule(localctx, getATN()->ruleToStartState[ruleIndex]->stateNumber, ruleIndex, 0); + } + + void Parser::enterRecursionRule(ParserRuleContext *localctx, int state, int ruleIndex, int precedence) { + setState(state); + _precedenceStack.push_back(precedence); + _ctx = localctx; + _ctx->start = _input->LT(1); + if (_parseListeners.size() > 0) { + triggerEnterRuleEvent(); // simulates rule entry for left-recursive rules + } + } + + void Parser::pushNewRecursionContext(ParserRuleContext *localctx, int state, int ruleIndex) { + ParserRuleContext *previous = _ctx; + previous->parent = localctx; + previous->invokingState = state; + previous->stop = _input->LT(-1); + + _ctx = localctx; + _ctx->start = previous->start; + if (_buildParseTrees) { + _ctx->addChild(previous); + } + + if (_parseListeners.size() > 0) { + triggerEnterRuleEvent(); // simulates rule entry for left-recursive rules + } + } + + void Parser::unrollRecursionContexts(ParserRuleContext *_parentctx) { + _precedenceStack.pop_back(); + _ctx->stop = _input->LT(-1); + ParserRuleContext *retctx = _ctx; // save current ctx (return value) + + // unroll so _ctx is as it was before call to recursive method + if (_parseListeners.size() > 0) { + while (_ctx != _parentctx) { + triggerExitRuleEvent(); + _ctx = static_cast(_ctx->parent); + } + } else { + _ctx = _parentctx; + } + + // hook into tree + retctx->parent = _parentctx; + + if (_buildParseTrees && _parentctx != nullptr) { + // add return ctx into invoking rule's tree + _parentctx->addChild(retctx); + } + } + + ParserRuleContext *Parser::getInvokingContext(int ruleIndex) { + ParserRuleContext *p = _ctx; + while (p != nullptr) { + if (p->getRuleIndex() == ruleIndex) { + return p; + } + p = static_cast(p->parent); + } + return nullptr; + } + + ParserRuleContext *Parser::getContext() { + return _ctx; + } + + void Parser::setContext(ParserRuleContext *ctx) { + _ctx = ctx; + } + + bool Parser::precpred(RuleContext *localctx, int precedence) { + return precedence >= _precedenceStack.back(); + } + + bool Parser::inContext(const std::wstring &context) { + // TODO: useful in parser? + return false; + } + + bool Parser::isExpectedToken(int symbol) { + // return getInterpreter().atn.nextTokens(_ctx); + atn::ATN *atn = getInterpreter()->atn; + ParserRuleContext *ctx = _ctx; + atn::ATNState *s = atn->states[getState()]; + misc::IntervalSet *following = atn->nextTokens(s); + if (following->contains(symbol)) { + return true; + } + // System.out.println("following "+s+"="+following); + if (!following->contains(Token::EPSILON)) { + return false; + } + + while (ctx != nullptr && ctx->invokingState >= 0 && following->contains(Token::EPSILON)) { + atn::ATNState *invokingState = atn->states[ctx->invokingState]; + atn::RuleTransition *rt = static_cast(invokingState->transition(0)); + following = atn->nextTokens(rt->followState); + if (following->contains(symbol)) { + return true; + } + + ctx = static_cast(ctx->parent); + } + + if (following->contains(Token::EPSILON) && symbol == Token::_EOF) { + return true; + } + + return false; + } + + misc::IntervalSet *Parser::getExpectedTokens() { + return getATN()->getExpectedTokens(getState(), getContext()); + } + + misc::IntervalSet *Parser::getExpectedTokensWithinCurrentRule() { + atn::ATN *atn = getInterpreter()->atn; + atn::ATNState *s = atn->states[getState()]; + return atn->nextTokens(s); + } + + int Parser::getRuleIndex(const std::wstring &ruleName) { + std::map* m = getRuleIndexMap(); + if (m->find(ruleName) == m->end()) { + return -1; + } + return m->at(ruleName); + } + + ParserRuleContext *Parser::getRuleContext() { + return _ctx; + } + + std::vector Parser::getRuleInvocationStack() { + return getRuleInvocationStack(_ctx); + } + + std::vector Parser::getRuleInvocationStack(RuleContext *p) { + std::vector ruleNames = getRuleNames(); + std::vector stack = std::vector(); + while (p != nullptr) { + // compute what follows who invoked us + int ruleIndex = p->getRuleIndex(); + if (ruleIndex < 0) { + stack.push_back(L"n/a"); + } else { + stack.push_back(ruleNames[ruleIndex]); + } + p = p->parent; + } + return stack; + } + + std::vector Parser::getDFAStrings() { + if (!_interp->_decisionToDFA.empty()) { + std::lock_guard lck(mtx); + std::vector s = std::vector(); + for (size_t d = 0; d < _interp->_decisionToDFA.size(); d++) { + dfa::DFA *dfa = &_interp->_decisionToDFA[d]; + s.push_back(dfa->toString(getTokenNames())); + } + return s; + } + return std::vector(); + } + + void Parser::dumpDFA() { + + if (!_interp->_decisionToDFA.empty()) { + std::lock_guard lck(mtx); + bool seenOne = false; + for (size_t d = 0; d < _interp->_decisionToDFA.size(); d++) { + dfa::DFA *dfa = &_interp->_decisionToDFA[d]; + if (!dfa->states->empty()) { + if (seenOne) { + std::cout << std::endl; + } + std::cout << L"Decision " << dfa->decision << L":" << std::endl; + dfa->toString(getTokenNames()); + std::wcout << dfa->toString(getTokenNames()); + seenOne = true; + } + } + } + } + + std::string Parser::getSourceName() { + return _input->getSourceName(); + } + + void Parser::setTrace(bool trace) { + if (!trace) { + removeParseListener(_tracer); + // TODO + //JAVA TO C++ CONVERTER WARNING: Java to C++ Converter converted the original 'null' assignment to a call to 'delete', but you should review memory allocation of all pointer variables in the converted code: + delete _tracer; + } else { + if (_tracer != nullptr) { + removeParseListener(_tracer); + } else { + _tracer = new TraceListener(this); + } + addParseListener(_tracer); + } + } + + void Parser::InitializeInstanceFields() { + _errHandler = new DefaultErrorStrategy(); + _precedenceStack.clear(); + _precedenceStack.push_back(0); + _buildParseTrees = true; + _syntaxErrors = 0; + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/Parser.h b/runtime/Cpp/org/antlr/v4/runtime/Parser.h new file mode 100755 index 000000000..749429703 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/Parser.h @@ -0,0 +1,490 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +#include "Recognizer.h" +#include "ParseTreeListener.h" +#include "TokenStream.h" +#include "BitSet.h" +#include "Declarations.h" +#include "TokenSource.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + + /// + /// This is all the parsing support code essentially; most of it is error recovery stuff. + class Parser : public Recognizer { + public: + class TraceListener : public tree::ParseTreeListener { + private: + Parser *const outerInstance; + + public: + TraceListener(Parser *outerInstance); + virtual ~TraceListener() {}; + + virtual void enterEveryRule(ParserRuleContext *ctx) override; + + virtual void visitTerminal(tree::TerminalNode *node) override; + + virtual void visitErrorNode(tree::ErrorNode *node) override; + + virtual void exitEveryRule(ParserRuleContext *ctx) override; + }; + + public: + class TrimToSizeListener : public tree::ParseTreeListener { + public: + static TrimToSizeListener *const INSTANCE; + + virtual void enterEveryRule(ParserRuleContext *ctx) override; + virtual void visitTerminal(tree::TerminalNode *node) override; + virtual void visitErrorNode(tree::ErrorNode *node) override; + virtual void exitEveryRule(ParserRuleContext *ctx) override; + }; + + /// + /// This field maps from the serialized ATN string to the deserialized with + /// bypass alternatives. + /// + /// + private: + static std::map * bypassAltsAtnCache; + + /// + /// The error handling strategy for the parser. The default value is a new + /// instance of . + /// + /// + /// + protected: + ANTLRErrorStrategy *_errHandler; + + /// + /// The input stream. + /// + /// + /// + TokenStream *_input; + + std::vector _precedenceStack; + //Mutex to manage synchronized access for multithreading in the parser + std::mutex mtx; + /// + /// The object for the currently executing rule. + /// This is always non-null during the parsing process. + /// + public: + ParserRuleContext *_ctx; + + /// + /// Specifies whether or not the parser should construct a parse tree during + /// the parsing process. The default value is {@code true}. + /// + /// + /// + protected: + bool _buildParseTrees; + + + /// + /// When {@code (true)} is called, a reference to the + /// is stored here so it can be easily removed in a + /// later call to {@code (false)}. The listener itself is + /// implemented as a parser listener so this field is not directly used by + /// other parser methods. + /// + private: + TraceListener *_tracer; + + /// + /// The list of listeners registered to receive + /// events during the parse. + /// + /// + protected: + std::vector _parseListeners; + + /// + /// The number of syntax errors reported during parsing. This value is + /// incremented each time is called. + /// + int _syntaxErrors; + + public: + Parser(TokenStream *input); + + /// + /// reset the parser's state + virtual void reset(); + + /// + /// Match current input symbol against {@code ttype}. If the symbol type + /// matches, and are + /// called to complete the match process. + ///

+ /// If the symbol type does not match, + /// is called on the current error + /// strategy to attempt recovery. If is + /// {@code true} and the token index of the symbol returned by + /// is -1, the symbol is added to + /// the parse tree by calling . + ///

+ /// the token type to match + /// the matched symbol + /// if the current input symbol did not match + /// {@code ttype} and the error strategy could not recover from the + /// mismatched symbol + virtual Token *match(int ttype); + + /// + /// Match current input symbol as a wildcard. If the symbol type matches + /// (i.e. has a value greater than 0), + /// and are called to complete the match process. + ///

+ /// If the symbol type does not match, + /// is called on the current error + /// strategy to attempt recovery. If is + /// {@code true} and the token index of the symbol returned by + /// is -1, the symbol is added to + /// the parse tree by calling . + ///

+ /// the matched symbol + /// if the current input symbol did not match + /// a wildcard and the error strategy could not recover from the mismatched + /// symbol + virtual Token *matchWildcard(); + + /// + /// Track the objects during the parse and hook + /// them up using the list so that it + /// forms a parse tree. The returned from the start + /// rule represents the root of the parse tree. + ///

+ /// Note that if we are not building parse trees, rule contexts only point + /// upwards. When a rule exits, it returns the context but that gets garbage + /// collected if nobody holds a reference. It points upwards but nobody + /// points at it. + ///

+ /// When we build parse trees, we are adding all of these contexts to + /// list. Contexts are then not candidates + /// for garbage collection. + ///

+ virtual void setBuildParseTree(bool buildParseTrees); + + /// + /// Gets whether or not a complete parse tree will be constructed while + /// parsing. This property is {@code true} for a newly constructed parser. + /// + /// {@code true} if a complete parse tree will be constructed while + /// parsing, otherwise {@code false} + virtual bool getBuildParseTree(); + + /// + /// Trim the internal lists of the parse tree during parsing to conserve memory. + /// This property is set to {@code false} by default for a newly constructed parser. + /// + /// {@code true} to trim the capacity of the + /// list to its size after a rule is parsed. + virtual void setTrimParseTree(bool trimParseTrees); + + /// {@code true} if the list is trimmed + /// using the default during the parse process. + virtual bool getTrimParseTree(); + + virtual std::vector getParseListeners(); + + /// + /// Registers {@code listener} to receive events during the parsing process. + ///

+ /// To support output-preserving grammar transformations (including but not + /// limited to left-recursion removal, automated left-factoring, and + /// optimized code generation), calls to listener methods during the parse + /// may differ substantially from calls made by + /// used after the parse is complete. In + /// particular, rule entry and exit events may occur in a different order + /// during the parse than after the parser. In addition, calls to certain + /// rule entry methods may be omitted. + ///

+ /// With the following specific exceptions, calls to listener events are + /// deterministic, i.e. for identical input the calls to listener + /// methods will be the same. + /// + ///

    + ///
  • Alterations to the grammar used to generate code may change the + /// behavior of the listener calls.
  • + ///
  • Alterations to the command line options passed to ANTLR 4 when + /// generating the parser may change the behavior of the listener calls.
  • + ///
  • Changing the version of the ANTLR Tool used to generate the parser + /// may change the behavior of the listener calls.
  • + ///
+ ///
+ /// the listener to add + /// + /// if {@code} listener is {@code null} + virtual void addParseListener(tree::ParseTreeListener *listener); + + /// + /// Remove {@code listener} from the list of parse listeners. + ///

+ /// If {@code listener} is {@code null} or has not been added as a parse + /// listener, this method does nothing. + ///

+ /// + /// the listener to remove + virtual void removeParseListener(tree::ParseTreeListener *listener); + + /// + /// Remove all parse listeners. + /// + /// + virtual void removeParseListeners(); + + /// + /// Notify any parse listeners of an enter rule event. + /// + /// + protected: + virtual void triggerEnterRuleEvent(); + + /// + /// Notify any parse listeners of an exit rule event. + /// + /// + virtual void triggerExitRuleEvent(); + + /// + /// Gets the number of syntax errors reported during parsing. This value is + /// incremented each time is called. + /// + /// + public: + virtual int getNumberOfSyntaxErrors(); + + virtual TokenFactory *getTokenFactory() override; + + /// + /// Tell our token source and error strategy about a new way to create tokens. + template + void setTokenFactory(TokenFactory *factory) { + _input->getTokenSource()->setTokenFactory(factory); + } + + /// + /// The ATN with bypass alternatives is expensive to create so we create it + /// lazily. + /// + /// if the current parser does not + /// implement the method. + virtual atn::ATN *getATNWithBypassAlts(); + + /// + /// The preferred method of getting a tree pattern. For example, here's a + /// sample use: + /// + ///
+                    /// ParseTree t = parser.expr();
+                    /// ParseTreePattern p = parser.compileParseTreePattern("+0", MyParser.RULE_expr);
+                    /// ParseTreeMatch m = p.match(t);
+                    /// String id = m.get("ID");
+                    /// 
+ ///
+ virtual tree::pattern::ParseTreePattern *compileParseTreePattern(const std::wstring &pattern, int patternRuleIndex); + + /// + /// The same as but specify a + /// rather than trying to deduce it from this parser. + /// + virtual tree::pattern::ParseTreePattern *compileParseTreePattern(const std::wstring &pattern, int patternRuleIndex, Lexer *lexer); + + virtual ANTLRErrorStrategy *getErrorHandler(); + + virtual void setErrorHandler(ANTLRErrorStrategy *handler); + + virtual TokenStream *getInputStream() override; + void setInputStream(IntStream *input) override; + + virtual TokenStream *getTokenStream(); + + /// + /// Set the token stream and reset the parser. + virtual void setTokenStream(TokenStream *input); + + /// + /// Match needs to return the current input symbol, which gets put + /// into the label for the associated token ref; e.g., x=ID. + /// + virtual Token *getCurrentToken(); + + void notifyErrorListeners(const std::wstring &msg); + + virtual void notifyErrorListeners(Token *offendingToken, const std::wstring &msg, RecognitionException *e); + + /// + /// Consume and return the . + ///

+ /// E.g., given the following input with {@code A} being the current + /// lookahead symbol, this function moves the cursor to {@code B} and returns + /// {@code A}. + /// + ///

+                    ///  A B
+                    ///  ^
+                    /// 
+ /// + /// If the parser is not in error recovery mode, the consumed symbol is added + /// to the parse tree using , and + /// is called on any parse listeners. + /// If the parser is in error recovery mode, the consumed symbol is + /// added to the parse tree using + /// , and + /// is called on any parse + /// listeners. + ///
+ virtual Token *consume(); + + protected: + virtual void addContextToParseTree(); + + /// + /// Always called by generated parsers upon entry to a rule. Access field + /// get the current context. + /// + public: + virtual void enterRule(ParserRuleContext *localctx, int state, int ruleIndex); + + virtual void exitRule(); + + virtual void enterOuterAlt(ParserRuleContext *localctx, int altNum); + + /// @deprecated Use + /// instead. + virtual void enterRecursionRule(ParserRuleContext *localctx, int ruleIndex); + + virtual void enterRecursionRule(ParserRuleContext *localctx, int state, int ruleIndex, int precedence); + + /// + /// Like but for recursive rules. + /// + virtual void pushNewRecursionContext(ParserRuleContext *localctx, int state, int ruleIndex); + + virtual void unrollRecursionContexts(ParserRuleContext *_parentctx); + + virtual ParserRuleContext *getInvokingContext(int ruleIndex); + + virtual ParserRuleContext *getContext(); + + virtual void setContext(ParserRuleContext *ctx); + + virtual bool precpred(RuleContext *localctx, int precedence) override; + + virtual bool inContext(const std::wstring &context); + + /// + /// Checks whether or not {@code symbol} can follow the current state in the + /// ATN. The behavior of this method is equivalent to the following, but is + /// implemented such that the complete context-sensitive follow set does not + /// need to be explicitly constructed. + /// + ///
+                    /// return getExpectedTokens().contains(symbol);
+                    /// 
+ ///
+ /// the symbol type to check + /// {@code true} if {@code symbol} can follow the current state in + /// the ATN, otherwise {@code false}. + virtual bool isExpectedToken(int symbol); + + /// + /// Computes the set of input symbols which could follow the current parser + /// state and context, as given by and , + /// respectively. + /// + /// + virtual misc::IntervalSet *getExpectedTokens(); + + virtual misc::IntervalSet *getExpectedTokensWithinCurrentRule(); + + /// + /// Get a rule's index (i.e., {@code RULE_ruleName} field) or -1 if not found. + virtual int getRuleIndex(const std::wstring &ruleName); + + virtual ParserRuleContext *getRuleContext(); + + /// + /// Return List<String> of the rule names in your parser instance + /// leading up to a call to the current rule. You could override if + /// you want more details such as the file/line info of where + /// in the ATN a rule is invoked. + /// + /// This is very useful for error messages. + /// + virtual std::vector getRuleInvocationStack(); + + virtual std::vector getRuleInvocationStack(RuleContext *p); + + /// + /// For debugging and other purposes. + virtual std::vector getDFAStrings(); + + /// + /// For debugging and other purposes. + virtual void dumpDFA(); + + virtual std::string getSourceName(); + + /// + /// During a parse is sometimes useful to listen in on the rule entry and exit + /// events as well as token matches. This is for quick and dirty debugging. + /// + virtual void setTrace(bool trace); + + private: + void InitializeInstanceFields(); + }; + + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/ParserInterpreter.cpp b/runtime/Cpp/org/antlr/v4/runtime/ParserInterpreter.cpp new file mode 100755 index 000000000..ad0df998c --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/ParserInterpreter.cpp @@ -0,0 +1,243 @@ +#include + +#include "ParserInterpreter.h" +#include "ATN.h" +#include "DFA.h" +#include "RuleStartState.h" +#include "InterpreterRuleContext.h" +#include "ParserATNSimulator.h" +#include "DecisionState.h" +#include "Transition.h" +#include "ANTLRErrorStrategy.h" +#include "Token.h" +#include "AtomTransition.h" +#include "ActionTransition.h" +#include "Exceptions.h" +#include "RuleTransition.h" +#include "PrecedencePredicateTransition.h" +#include "PredicateTransition.h" +#include "LoopEndState.h" +#include "FailedPredicateException.h" +#include "RuleStartState.h" +#include "StarLoopEntryState.h" +#include "PredictionContextCache.h" +#include "IntervalSet.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + ParserInterpreter::ParserInterpreter(const std::wstring &grammarFileName, const std::vector& tokenNames, const std::vector& 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*>()) { + + + for (int i = 0; i < atn->getNumberOfDecisions(); i++) { + _decisionToDFA.emplace_back(atn->getDecisionState(i), i); + } + + // identify the ATN states where pushNewRecursionContext must be called + for (auto state : atn->states) { + if (!(dynamic_cast(state) != nullptr)) { + continue; + } + + atn::RuleStartState *ruleStartState = atn->ruleToStartState[state->ruleIndex]; + if (!ruleStartState->isPrecedenceRule) { + continue; + } + + atn::ATNState *maybeLoopEndState = state->transition(state->getNumberOfTransitions() - 1)->target; + if (!(dynamic_cast(maybeLoopEndState) != nullptr)) { + continue; + } + + if (maybeLoopEndState->epsilonOnlyTransitions && dynamic_cast(maybeLoopEndState->transition(0)->target) != nullptr) { + this->pushRecursionContextStates->set(state->stateNumber); + } + } + + // get atn simulator that knows how to do predictions + setInterpreter(new atn::ParserATNSimulator(this, atn, _decisionToDFA, sharedContextCache)); + } + + org::antlr::v4::runtime::atn::ATN *ParserInterpreter::getATN() { + return atn; + } + + const std::vector& ParserInterpreter::getTokenNames() { + return _tokenNames; + } + + const std::vector& ParserInterpreter::getRuleNames() { + return _ruleNames; + } + + std::wstring ParserInterpreter::getGrammarFileName() { + return grammarFileName; + } + + ParserRuleContext *ParserInterpreter::parse(int startRuleIndex) { + atn::RuleStartState *startRuleStartState = atn->ruleToStartState[startRuleIndex]; + + InterpreterRuleContext *rootContext = new InterpreterRuleContext(nullptr, atn::ATNState::INVALID_STATE_NUMBER, startRuleIndex); + if (startRuleStartState->isPrecedenceRule) { + enterRecursionRule(rootContext, startRuleStartState->stateNumber, startRuleIndex, 0); + } else { + enterRule(rootContext, startRuleStartState->stateNumber, startRuleIndex); + } + + while (true) { + atn::ATNState *p = getATNState(); + switch (p->getStateType()) { + case atn::ATNState::RULE_STOP : + // pop; return from rule + if (_ctx->isEmpty()) { + exitRule(); + return rootContext; + } + + visitRuleStopState(p); + break; + + default : + visitState(p); + break; + } + } + } + + void ParserInterpreter::enterRecursionRule(ParserRuleContext *localctx, int state, int ruleIndex, int precedence) { + _parentContextStack->push_back(new std::pair(_ctx, localctx->invokingState)); + Parser::enterRecursionRule(localctx, state, ruleIndex, precedence); + } + + atn::ATNState *ParserInterpreter::getATNState() { + return atn->states.at(getState()); + } + + void ParserInterpreter::visitState(atn::ATNState *p) { + int edge; + if (p->getNumberOfTransitions() > 1) { + edge = getInterpreter()->adaptivePredict(_input, ((atn::DecisionState*)p)->decision, _ctx); + } else { + edge = 1; + } + + atn::Transition *transition = p->transition(edge - 1); + switch (transition->getSerializationType()) { + case atn::Transition::EPSILON: + if (pushRecursionContextStates->data[p->stateNumber] == 1 && !(dynamic_cast(transition->target) != nullptr)) { + InterpreterRuleContext *ctx = new InterpreterRuleContext(_parentContextStack->front()->first, _parentContextStack->front()->second, _ctx->getRuleIndex()); + pushNewRecursionContext(ctx, atn->ruleToStartState[p->ruleIndex]->stateNumber, _ctx->getRuleIndex()); + } + break; + + case atn::Transition::ATOM: + match(((atn::AtomTransition*)(transition))->_label); + break; + + case atn::Transition::RANGE: + case atn::Transition::SET: + case atn::Transition::NOT_SET: + if (!transition->matches(_input->LA(1), Token::MIN_USER_TOKEN_TYPE, 65535)) { + _errHandler->recoverInline(this); + } + matchWildcard(); + break; + + case atn::Transition::WILDCARD: + matchWildcard(); + break; + + case atn::Transition::RULE: + { + atn::RuleStartState *ruleStartState = (atn::RuleStartState*)(transition->target); + int ruleIndex = ruleStartState->ruleIndex; + InterpreterRuleContext *ctx = new InterpreterRuleContext(_ctx, p->stateNumber, ruleIndex); + if (ruleStartState->isPrecedenceRule) { + enterRecursionRule(ctx, ruleStartState->stateNumber, ruleIndex, ((atn::RuleTransition*)(transition))->precedence); + } else { + enterRule(ctx, transition->target->stateNumber, ruleIndex); + } + } + break; + + case atn::Transition::PREDICATE: + { + atn::PredicateTransition *predicateTransition = (atn::PredicateTransition*)(transition); + if (!sempred(_ctx, predicateTransition->ruleIndex, predicateTransition->predIndex)) { + throw new FailedPredicateException(this); + } + } + break; + + case atn::Transition::ACTION: + { + atn::ActionTransition *actionTransition = (atn::ActionTransition*)(transition); + action(_ctx, actionTransition->ruleIndex, actionTransition->actionIndex); + } + break; + + case atn::Transition::PRECEDENCE: + { + if (!precpred(_ctx, ((atn::PrecedencePredicateTransition*)(transition))->precedence)) { + throw new FailedPredicateException(this, L"precpred(_ctx, " + std::to_wstring(((atn::PrecedencePredicateTransition*)(transition))->precedence) + L")"); + } + } + break; + + default: + throw UnsupportedOperationException(L"Unrecognized ATN transition type."); + } + + setState(transition->target->stateNumber); + } + + void ParserInterpreter::visitRuleStopState(atn::ATNState *p) { + atn::RuleStartState *ruleStartState = atn->ruleToStartState[p->ruleIndex]; + if (ruleStartState->isPrecedenceRule) { + std::pair *parentContext = _parentContextStack->back(); // Dan - make sure this is equivalent + _parentContextStack->pop_back(); + unrollRecursionContexts(parentContext->first); + setState(parentContext->second); + } else { + exitRule(); + } + + atn::RuleTransition *ruleTransition = static_cast(atn->states.at(getState())->transition(0)); + setState(ruleTransition->followState->stateNumber); + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/ParserInterpreter.h b/runtime/Cpp/org/antlr/v4/runtime/ParserInterpreter.h new file mode 100755 index 000000000..493cb467b --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/ParserInterpreter.h @@ -0,0 +1,106 @@ +#pragma once + +#include +#include + +#include "Parser.h" +#include "Declarations.h" + + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + /// + /// A parser simulator that mimics what ANTLR's generated + /// parser code does. A ParserATNSimulator is used to make + /// predictions via adaptivePredict but this class moves a pointer through the + /// ATN to simulate parsing. ParserATNSimulator just + /// makes us efficient rather than having to backtrack, for example. + /// + /// This properly creates parse trees even for left recursive rules. + /// + /// We rely on the left recursive rule invocation and special predicate + /// transitions to make left recursive rules work. + /// + /// See TestParserInterpreter for examples. + /// + class ParserInterpreter : public Parser { + protected: + //TODO check this + static const int DEFAULT_BITSET_SIZE = 1024; // atn->states.size() ideally + + const std::wstring grammarFileName; + std::vector _tokenNames; + atn::ATN *const atn; + + std::vector _ruleNames; + antlrcpp::BitSet *const pushRecursionContextStates; + + std::vector _decisionToDFA; // not shared like it is for generated parsers + atn::PredictionContextCache *const sharedContextCache; + + + + std::deque*> *const _parentContextStack; + + public: + ParserInterpreter(const std::wstring &grammarFileName, const std::vector& tokenNames, const std::vector& ruleNames, atn::ATN *atn, TokenStream *input); + + virtual atn::ATN *getATN() override; + + virtual const std::vector& getTokenNames() override; + + virtual const std::vector& getRuleNames() override; + + virtual std::wstring getGrammarFileName() override; + + /// + /// Begin parsing at startRuleIndex + virtual ParserRuleContext *parse(int startRuleIndex); + + virtual void enterRecursionRule(ParserRuleContext *localctx, int state, int ruleIndex, int precedence) override; + + protected: + virtual atn::ATNState *getATNState(); + + virtual void visitState(atn::ATNState *p); + + virtual void visitRuleStopState(atn::ATNState *p); + }; + + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/ParserRuleContext.cpp b/runtime/Cpp/org/antlr/v4/runtime/ParserRuleContext.cpp new file mode 100755 index 000000000..3a4e0a7a9 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/ParserRuleContext.cpp @@ -0,0 +1,194 @@ +#include "ParserRuleContext.h" +#include "TerminalNodeImpl.h" +#include "ErrorNodeImpl.h" +#include "Interval.h" +#include "Parser.h" + +#include + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + ParserRuleContext::ParserRuleContext() { + } + + void ParserRuleContext::copyFrom(ParserRuleContext *ctx) { + // from RuleContext + this->parent = ctx->parent; + this->invokingState = ctx->invokingState; + + this->start = ctx->start; + this->stop = ctx->stop; + } + + ParserRuleContext::ParserRuleContext(ParserRuleContext *parent, int invokingStateNumber) : RuleContext(parent, invokingStateNumber) { + } + + void ParserRuleContext::enterRule(tree::ParseTreeListener *listener) { + } + + void ParserRuleContext::exitRule(tree::ParseTreeListener *listener) { + } + + tree::TerminalNode *ParserRuleContext::addChild(tree::TerminalNode *t) { + if (children.empty()) { + children = std::vector(); + } + children.push_back(t); + return t; + } + + RuleContext *ParserRuleContext::addChild(RuleContext *ruleInvocation) { + if (children.empty()) { + children = std::vector(); + } + children.push_back(ruleInvocation); + return ruleInvocation; + } + + void ParserRuleContext::removeLastChild() { + if (children.size() > 0) { + children.pop_back(); + } + } + + tree::TerminalNode *ParserRuleContext::addChild(Token *matchedToken) { + tree::TerminalNodeImpl *t = new tree::TerminalNodeImpl(matchedToken); + addChild(t); + t->parent = this; + return t; + } + + tree::ErrorNode *ParserRuleContext::addErrorNode(Token *badToken) { + tree::ErrorNodeImpl *t = new tree::ErrorNodeImpl(badToken); + addChild(t); + t->parent = this; + return t; + } + + /// + /// Override to make type more specific + ParserRuleContext *ParserRuleContext::getParent() + { + return static_cast(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::TerminalNode *ParserRuleContext::getToken(int ttype, int i) { + if (children.empty() || i < 0 || i >= (int)children.size()) { + return nullptr; + } + + int j = -1; // what token with ttype have we found? + for (auto o : children) { + if (dynamic_cast(o) != nullptr) { + tree::TerminalNode *tnode = static_cast(o); + Token *symbol = tnode->getSymbol(); + if (symbol->getType() == ttype) { + j++; + if (j == i) { + return tnode; + } + } + } + } + + return nullptr; + } + + // I think this should be changed to a pointer? + std::vector ParserRuleContext::getTokens(int ttype) { + if (children.empty()) { + return std::vector(); + } + + std::vector tokens; + for (auto o : children) { + if (dynamic_cast(o) != nullptr) { + tree::TerminalNode *tnode = static_cast(o); + Token *symbol = tnode->getSymbol(); + if (symbol->getType() == ttype) { + if (tokens.empty()) { + tokens = std::vector(); + } + tokens.push_back(tnode); + } + } + } + + if (tokens.empty()) { + return std::vector(); + } + + return tokens; + } + + + + int ParserRuleContext::getChildCount() { + return (int)children.size() > 0 ? (int)children.size() : 0; + } + + misc::Interval *ParserRuleContext::getSourceInterval() { + if (start == nullptr || stop == nullptr) { + return misc::Interval::INVALID; + } + return misc::Interval::of(start->getTokenIndex(), stop->getTokenIndex()); + } + + Token *ParserRuleContext::getStart() { + return start; + } + + Token *ParserRuleContext::getStop() { + return stop; + } + + std::wstring ParserRuleContext::toInfoString(Parser *recognizer) { + std::vector rules = recognizer->getRuleInvocationStack(this); + std::reverse(rules.begin(), rules.end()); + std::wstring rulesStr = antlrcpp::arrayToString(rules); + return std::wstring(L"ParserRuleContext") + rulesStr + std::wstring(L"{") + std::wstring(L"start=") + std::to_wstring(start->getTokenIndex()) + std::wstring(L", stop=") + std::to_wstring(stop->getTokenIndex()) + L'}'; + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/ParserRuleContext.h b/runtime/Cpp/org/antlr/v4/runtime/ParserRuleContext.h new file mode 100755 index 000000000..4f96288fa --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/ParserRuleContext.h @@ -0,0 +1,211 @@ +#pragma once + +#include +#include +#include + +#include "RuleContext.h" +#include "Declarations.h" + + + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + + /// + /// A rule invocation record for parsing. + /// + /// Contains all of the information about the current rule not stored in the + /// RuleContext. It handles parse tree children list, Any ATN state + /// tracing, and the default values available for rule indications: + /// start, stop, rule index, current alt number, current + /// ATN state. + /// + /// Subclasses made for each rule and grammar track the parameters, + /// return values, locals, and labels specific to that rule. These + /// are the objects that are returned from rules. + /// + /// Note text is not an actual field of a rule return value; it is computed + /// from start and stop using the input stream's toString() method. I + /// could add a ctor to this so that we can pass in and store the input + /// stream, but I'm not sure we want to do that. It would seem to be undefined + /// to get the .text property anyway if the rule matches tokens from multiple + /// input streams. + /// + /// I do not use getters for fields of objects that are used simply to + /// group values such as this aggregate. The getters/setters are there to + /// satisfy the superclass interface. + /// + class ParserRuleContext : public RuleContext { + /// + /// If we are debugging or building a parse tree for a visitor, + /// we need to track all of the tokens and rule invocations associated + /// with this rule's context. This is empty for parsing w/o tree constr. + /// operation because we don't the need to track the details about + /// how we parse this rule. + /// + public: + std::vector children; + + /// + /// For debugging/tracing purposes, we want to track all of the nodes in + /// the ATN traversed by the parser for a particular rule. + /// This list indicates the sequence of ATN nodes used to match + /// the elements of the children list. This list does not include + /// ATN nodes and other rules used to match rule invocations. It + /// traces the rule invocation node itself but nothing inside that + /// other rule's ATN submachine. + /// + /// There is NOT a one-to-one correspondence between the children and + /// states list. There are typically many nodes in the ATN traversed + /// for each element in the children list. For example, for a rule + /// invocation there is the invoking state and the following state. + /// + /// The parser setState() method updates field s and adds it to this list + /// if we are debugging/tracing. + /// + /// This does not trace states visited during prediction. + /// + // public List states; + + Token *start, *stop; + + /// + /// The exception that forced this rule to return. If the rule successfully + /// completed, this is {@code null}. + /// + RecognitionException *exception; + + ParserRuleContext(); + virtual ~ParserRuleContext() {} + + /// + /// COPY a ctx (I'm deliberately not copy constructor) + virtual void copyFrom(ParserRuleContext *ctx); + + ParserRuleContext(ParserRuleContext *parent, int invokingStateNumber); + + // Double dispatch methods for listeners + + virtual void enterRule(tree::ParseTreeListener *listener); + virtual void exitRule(tree::ParseTreeListener *listener); + + /// + /// Does not set parent link; other add methods do that + virtual tree::TerminalNode *addChild(tree::TerminalNode *t); + + virtual RuleContext *addChild(RuleContext *ruleInvocation); + + /// + /// Used by enterOuterAlt to toss out a RuleContext previously added as + /// we entered a rule. If we have # label, we will need to remove + /// generic ruleContext object. + /// + virtual void removeLastChild(); + + virtual tree::TerminalNode *addChild(Token *matchedToken); + + virtual tree::ErrorNode *addErrorNode(Token *badToken); + + virtual ParserRuleContext *getParent() override; + + virtual ParseTree *getChild(int i) override; + + template + T getChild(void *ctxType, int i) { + if (children.empty() || i < 0 || i >= (int) children.size()) { + return nullptr; + } + + int j = -1; // what element have we found with ctxType? + for (auto o : children) { + if (typeid(ctxType) == typeid(o)) { + j++; + if (j == i) { + return dynamic_cast(o); + } + } + } + return nullptr; + } + + virtual tree::TerminalNode *getToken(int ttype, int i); + + virtual std::vector getTokens(int ttype); + + template + T getRuleContext(void *ctxType, int i) { + return getChild(ctxType, i); + } + + template + std::vector getRuleContexts(void *ctxType) { + if (children.empty()) { + return std::vector(); + } + + std::vector contexts; + for (auto o : children) { + if (typeid(ctxType) == typeid(o)) { + if (contexts.empty()) { + contexts = std::vector(); + } + + contexts.push_back((ParserRuleContext*)(o)); + } + } + + if (contexts.empty()) { + return std::vector(); + } + + return contexts; + } + + virtual int getChildCount() override; + virtual misc::Interval *getSourceInterval() override; + + virtual Token *getStart(); + virtual Token *getStop(); + + /// + /// Used for rule context info debugging during parse-time, not so much for ATN debugging + virtual std::wstring toInfoString(Parser *recognizer); + }; + + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/ProxyErrorListener.cpp b/runtime/Cpp/org/antlr/v4/runtime/ProxyErrorListener.cpp new file mode 100755 index 000000000..f62aca2b2 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/ProxyErrorListener.cpp @@ -0,0 +1,61 @@ +#include "ProxyErrorListener.h" +#include "RecognitionException.h" +#include "atn/ATNConfigSet.h" +#include "dfa/DFA.h" +#include "Exceptions.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + void ProxyErrorListener::reportAmbiguity(Parser *recognizer, dfa::DFA *dfa, int startIndex, int stopIndex, bool exact, antlrcpp::BitSet *ambigAlts, atn::ATNConfigSet *configs) { + for (auto listener : *delegates) { + listener->reportAmbiguity(recognizer, dfa, startIndex, stopIndex, exact, ambigAlts, configs); + } + } + + void ProxyErrorListener::reportAttemptingFullContext(Parser *recognizer, dfa::DFA *dfa, int startIndex, int stopIndex, antlrcpp::BitSet *conflictingAlts, atn::ATNConfigSet *configs) { + for (auto listener : *delegates) { + listener->reportAttemptingFullContext(recognizer, dfa, startIndex, stopIndex, conflictingAlts, configs); + } + } + + void ProxyErrorListener::reportContextSensitivity(Parser *recognizer, dfa::DFA *dfa, int startIndex, int stopIndex, int prediction, atn::ATNConfigSet *configs) { + for (auto listener : *delegates) { + listener->reportContextSensitivity(recognizer, dfa, startIndex, stopIndex, prediction, configs); + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/ProxyErrorListener.h b/runtime/Cpp/org/antlr/v4/runtime/ProxyErrorListener.h new file mode 100755 index 000000000..651c1df98 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/ProxyErrorListener.h @@ -0,0 +1,80 @@ +#pragma once + +#include +#include + +#include "ANTLRErrorListener.h" +#include "Declarations.h" +#include "Exceptions.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + /// + /// This implementation of dispatches all calls to a + /// collection of delegate listeners. This reduces the effort required to support multiple + /// listeners. + /// + + class ProxyErrorListener : public ANTLRErrorListener { + private: + std::vector *const delegates; + + public: + template //where T1 : ANTLRErrorListener + ProxyErrorListener(std::vector *delegates) : delegates(delegates) { + if (delegates == nullptr) { + throw new NullPointerException(L"delegates"); + } + + } + + template + void syntaxError(IRecognizer *recognizer, void *offendingSymbol, int line, int charPositionInLine, const std::wstring &msg, RecognitionException *e) { + + for (auto listener : *delegates) { + listener->syntaxError(recognizer, offendingSymbol, line, charPositionInLine, msg, e); + } + } + + 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) override; + + virtual void reportContextSensitivity(Parser *recognizer, dfa::DFA *dfa, int startIndex, int stopIndex, int prediction, atn::ATNConfigSet *configs) override; + }; + + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/RecognitionException.cpp b/runtime/Cpp/org/antlr/v4/runtime/RecognitionException.cpp new file mode 100755 index 000000000..555e2fd76 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/RecognitionException.cpp @@ -0,0 +1,87 @@ +#include "RecognitionException.h" +#include "ATN.h" +#include "Recognizer.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + int RecognitionException::getOffendingState() { + return offendingState; + } + + void RecognitionException::setOffendingState(int offendingState) { + this->offendingState = offendingState; + } + + misc::IntervalSet *RecognitionException::getExpectedTokens() { + // Terence and Sam used some fancy Java wildcard generics which + // cause us trouble here. TODO - can a Recognizer + // substitute for any other type of recognizer? + if (recognizer != nullptr) { + return ((Recognizer*)recognizer)->getATN()->getExpectedTokens(offendingState, ctx); + } + return nullptr; + } + + RuleContext *RecognitionException::getCtx() { + return ctx; + } + + IntStream *RecognitionException::getInputStream() { + return input; + } + + Token *RecognitionException::getOffendingToken() { + return offendingToken; + } + + void RecognitionException::setOffendingToken(Token *offendingToken) { + this->offendingToken = offendingToken; + } + + IRecognizer *RecognitionException::getRecognizer() { + // Terence and Sam used some fancy Java wildcard generics which + // cause us trouble here. TODO - can a Recognizer + // substitute for any other type of recognizer? + return (IRecognizer *)recognizer; + } + + void RecognitionException::InitializeInstanceFields() { + offendingState = -1; + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/RecognitionException.h b/runtime/Cpp/org/antlr/v4/runtime/RecognitionException.h new file mode 100755 index 000000000..9d7b3f40d --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/RecognitionException.h @@ -0,0 +1,181 @@ +#pragma once + +#include +#include + +#include "IRecognizer.h" +#include "Declarations.h" +#include "Exceptions.h" + + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + class RuntimeException : public ANTLRException { + public: + RuntimeException(const std::wstring msg) : ANTLRException(msg) {} + RuntimeException() {}; + }; + + /// + /// The root of the ANTLR exception hierarchy. In general, ANTLR tracks just + /// 3 kinds of errors: prediction errors, failed predicate errors, and + /// mismatched input errors. In each case, the parser knows where it is + /// in the input, where it is in the ATN, the rule invocation stack, + /// and what kind of problem occurred. + /// + class RecognitionException : public RuntimeException { + /// + /// The where this exception originated. + private: + // Hairy wildcard generics from Java, attempt to fix with a raw void* + // Recognizer *const recognizer; + void * recognizer; + IntStream *const input; + RuleContext *const ctx; + + /// + /// The current when an error occurred. Since not all streams + /// support accessing symbols by index, we have to track the + /// instance itself. + /// + Token *offendingToken; + + int offendingState; + + public: + template + RecognitionException(IRecognizer *recognizer, IntStream *input, + ParserRuleContext * const ctx) + : recognizer(recognizer), input(input), ctx((RuleContext*)ctx) { + InitializeInstanceFields(); + if (recognizer != nullptr) { + this->offendingState = recognizer->getState(); + } + } + + template + RecognitionException(const std::wstring &message, IRecognizer *recognizer, IntStream *input, ParserRuleContext *ctx){ + InitializeInstanceFields(); + if (recognizer != nullptr) { + this->offendingState = recognizer->getState(); + } + } + + RecognitionException() : recognizer(nullptr), input(nullptr), ctx(nullptr), offendingToken(nullptr) {} + + /// + /// Get the ATN state number the parser was in at the time the error + /// occurred. For and + /// exceptions, this is the + /// number. For others, it is the state whose outgoing + /// edge we couldn't match. + ///

+ /// If the state number is not known, this method returns -1. + ///

+ virtual int getOffendingState(); + + protected: + void setOffendingState(int offendingState); + + /// + /// Gets the set of input symbols which could potentially follow the + /// previously matched symbol at the time this exception was thrown. + ///

+ /// If the set of expected tokens is not known and could not be computed, + /// this method returns {@code null}. + ///

+ /// The set of token types that could potentially follow the current + /// state in the ATN, or {@code null} if the information is not available. + public: + virtual misc::IntervalSet *getExpectedTokens(); + + /// + /// Gets the at the time this exception was thrown. + ///

+ /// If the context is not available, this method returns {@code null}. + ///

+ /// The at the time this exception was thrown. + /// If the context is not available, this method returns {@code null}. + virtual RuleContext *getCtx(); + + /// + /// Gets the input stream which is the symbol source for the recognizer where + /// this exception was thrown. + ///

+ /// If the input stream is not available, this method returns {@code null}. + ///

+ /// The input stream which is the symbol source for the recognizer + /// where this exception was thrown, or {@code null} if the stream is not + /// available. + virtual IntStream *getInputStream(); + + virtual Token *getOffendingToken(); + + protected: + void setOffendingToken(Token *offendingToken); + + /// + /// Gets the where this exception occurred. + ///

+ /// If the recognizer is not available, this method returns {@code null}. + ///

+ /// The recognizer where this exception occurred, or {@code null} if + /// the recognizer is not available. + public: + virtual IRecognizer *getRecognizer(); + + private: + void InitializeInstanceFields(); + }; + + // Recognition exceptions, TODO fill out the code + + class ParseCancellationException : public RecognitionException { + public: + ParseCancellationException(const std::wstring msg) {}; + ParseCancellationException(RecognitionException*) {}; + ParseCancellationException() {}; + }; + + class EmptyStackException : public RecognitionException { + public: + EmptyStackException(const std::wstring msg) {} + EmptyStackException() {}; + }; + + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/Recognizer.h b/runtime/Cpp/org/antlr/v4/runtime/Recognizer.h new file mode 100755 index 000000000..81ffc513c --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/Recognizer.h @@ -0,0 +1,232 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +#include "TokenFactory.h" +#include "ATNSimulator.h" +#include "Declarations.h" +#include "ConsoleErrorListener.h" +#include "Token.h" +#include "StringBuilder.h" +#include "ProxyErrorListener.h" +#include "Strings.h" +#include "CPPUtils.h" +#include "RecognitionException.h" +#include "IRecognizer.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + template + class Recognizer : public IRecognizer { + public: + static const int _EOF = -1; + + private: + static std::map, std::map*> _tokenTypeMapCache; + static std::map, std::map*> _ruleIndexMapCache; + + std::vector _listeners; + //Mutex to manage synchronized access for multithreading + std::mutex mtx; + +#ifdef TODO + // Damn code isn't even being used, why is it here? + private: + class CopyOnWriteArrayListAnonymousInnerClassHelper : public std::vector { + public: + CopyOnWriteArrayListAnonymousInnerClassHelper(); + }; +#endif + + protected: + ATNInterpreter _interp; + + private: + int _stateNumber; + + /// + /// 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. + /// + public: + virtual const std::vector& getTokenNames(){ + throw new ASSERTException(L"Recognizer", L"getTokenNames should never be called, abstract class"); + };// = 0; + + virtual const std::vector& getRuleNames() { + throw new ASSERTException(L"Recognizer", L"getRuleNames should never be called, abstract class"); + };// = 0; + + /// + /// Get a map from token names to token types. + ///

+ /// Used for XPath and tree pattern compilation. + ///

+ virtual std::map *getTokenTypeMap(); + + /// + /// Get a map from rule names to rule indexes. + ///

+ /// Used for XPath and tree pattern compilation. + ///

+ virtual std::map *getRuleIndexMap(); + + virtual int getTokenType(const std::wstring &tokenName); + + /// + /// If this recognizer was generated, it will have a serialized ATN + /// representation of the grammar. + ///

+ /// For interpreters, we don't know their serialized ATN despite having + /// created the interpreter from it. + ///

+ virtual std::wstring getSerializedATN() { + throw L"there is no serialized ATN"; + } + + /// + /// For debugging and other purposes, might want the grammar name. + /// Have ANTLR generate an implementation for this method. + /// + virtual std::wstring getGrammarFileName() = 0; + + /// + /// Get the used by the recognizer for prediction. + /// + /// The used by the recognizer for prediction. + virtual atn::ATN *getATN() = 0; + + /// + /// Get the ATN interpreter used by the recognizer for prediction. + /// + /// The ATN interpreter used by the recognizer for prediction. + virtual ATNInterpreter getInterpreter() { + return _interp; + } + + /// + /// Set the ATN interpreter used by the recognizer for prediction. + /// + /// The ATN interpreter used by the recognizer for + /// prediction. + virtual void setInterpreter(ATNInterpreter interpreter) { + _interp = interpreter; + } + + /// + /// What is the error header, normally line/character position information? + virtual std::wstring getErrorHeader(RecognitionException *e); + + /// + /// How should a token be displayed in an error message? The default + /// is to display just the text, but during development you might + /// want to have a lot of information spit out. Override in that case + /// to use t.toString() (which, for CommonToken, dumps everything about + /// the token). This is better than forcing you to override a method in + /// your token objects because you don't have to go modify your lexer + /// so that it creates a new Java type. + /// + virtual std::wstring getTokenErrorDisplay(Token *t); + + /// if {@code listener} is {@code null}. + virtual void addErrorListener(ANTLRErrorListener *listener); + + virtual void removeErrorListener(ANTLRErrorListener *listener); + + virtual void removeErrorListeners(); + + virtual std::vector *getErrorListeners() { + return &_listeners; + } + + virtual ANTLRErrorListener *getErrorListenerDispatch(); + + // subclass needs to override these if there are sempreds or actions + // that the ATN interp needs to execute + virtual bool sempred(RuleContext *_localctx, int ruleIndex, int actionIndex); + + virtual bool precpred(RuleContext *localctx, int precedence); + + virtual void action(RuleContext *_localctx, int ruleIndex, int actionIndex); + + int getState(); + + /// + /// Indicate that the recognizer has changed internal state that is + /// consistent with the ATN state passed in. This way we always know + /// where we are in the ATN as the parser goes along. The rule + /// context objects form a stack that lets us see the stack of + /// invoking rules. Combine this and we have complete ATN + /// configuration information. + /// + void setState(int atnState); + + virtual IntStream *getInputStream(){ + throw new ASSERTException(L"Recognizer::getInputStream", L"Should never be called, abstract class"); + + };// = 0; + + virtual void setInputStream(IntStream *input){ + throw new ASSERTException(L"Recognizer::setInputStream", L"Should never be called, abstract class"); + };// = 0; + + virtual TokenFactory *getTokenFactory(){ + throw new ASSERTException(L"Recognizer::getTokenFactory", L"Should never be called, abstract class"); + };// = 0; + + template + void setTokenFactory(TokenFactory *input); + + private: + void InitializeInstanceFields(); + + public: + Recognizer(); + }; + + } + } + } +} + +#include "Recognizer.inl" + + diff --git a/runtime/Cpp/org/antlr/v4/runtime/Recognizer.inl b/runtime/Cpp/org/antlr/v4/runtime/Recognizer.inl new file mode 100755 index 000000000..c5f2a5f1a --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/Recognizer.inl @@ -0,0 +1,218 @@ +#pragma once + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + template + std::map, std::map*> + Recognizer::_tokenTypeMapCache; + + template + std::map, std::map*> + Recognizer::_ruleIndexMapCache; + + template + std::map *Recognizer::getTokenTypeMap() { + std::vector tokenNames = getTokenNames(); + if (tokenNames.empty()) { + throw L"The current recognizer does not provide a list of token names."; + } + + + { // mutex lock + std::lock_guard lck(mtx); + std::map *result = _tokenTypeMapCache.at(tokenNames); + if (result == nullptr) { + // From Java - why ? result = misc::Utils::toMap(tokenNames); + (*result)[L"EOF"] = Token::_EOF; + + // TODO + // From Java - why ? result = std::vector::unmodifiableMap(result); + _tokenTypeMapCache[tokenNames] = result; + + } + + return result; + } + + } + + template + std::map *Recognizer::getRuleIndexMap() { + const std::vector& ruleNames = getRuleNames(); + if (ruleNames.empty()) { + throw L"The current recognizer does not provide a list of rule names."; + } + + { + std::lock_guard lck(mtx); + std::map *result = _ruleIndexMapCache.at(ruleNames); + + if (result == nullptr) { + result = antlrcpp::toMap(ruleNames); + std::pair, std::map*> tmp (ruleNames, result); + _ruleIndexMapCache.insert(_ruleIndexMapCache.begin(), tmp); + } + return result; + } + return nullptr; + } + + template + int Recognizer::getTokenType(const std::wstring &tokenName) { + + std::map * map = getTokenTypeMap(); + int ttype = map->at(tokenName); + + if (ttype != Token::INVALID_TYPE) { + return ttype; + } + return Token::INVALID_TYPE; + } + + template + std::wstring Recognizer::getErrorHeader(RecognitionException *e) { + // We're having issues with cross header dependencies, these two classes will need to be + // rewritten to remove that. + int line = e->getOffendingToken()->getLine(); + int charPositionInLine = e->getOffendingToken()->getCharPositionInLine(); + return std::wstring(L"line ") + std::to_wstring(line) + std::wstring(L":") + std::to_wstring(charPositionInLine); + + } + + template + std::wstring Recognizer::getTokenErrorDisplay(Token *t) { + if (t == nullptr) { + return L""; + } + std::wstring s = t->getText(); + if (s == L"") { + if (t->getType() == Token::_EOF) { + s = L""; + } else { + s = std::wstring(L"<") + std::to_wstring(t->getType()) + std::wstring(L">"); + } + } + + antlrcpp::replaceAll(s, L"\n", L"\\n"); + + antlrcpp::replaceAll(s, L"\r",L"\\r"); + + antlrcpp::replaceAll(s, L"\t", L"\\t"); + + return std::wstring(L"'") + s + std::wstring(L"'"); + } + + template + void Recognizer::addErrorListener(ANTLRErrorListener *listener) { + if (listener == nullptr) { + throw L"listener cannot be null."; + } + + _listeners.insert(_listeners.end(), listener); + } + + template + void Recognizer::removeErrorListener(ANTLRErrorListener *listener) { + //_listeners.remove(listener); does this work the same way? + std::vector::iterator it; + it = std::find(_listeners.begin(), _listeners.end(), listener); + _listeners.erase(it); + } + + template + void Recognizer::removeErrorListeners() { + _listeners.clear(); + } + + template + ANTLRErrorListener *Recognizer::getErrorListenerDispatch() { + return (ANTLRErrorListener *)new ProxyErrorListener(getErrorListeners()); + } + + template + bool Recognizer::sempred(RuleContext *_localctx, int ruleIndex, int actionIndex) { + return true; + } + + + template + bool Recognizer::precpred(RuleContext *localctx, int precedence) { + return true; + } + + + template + void Recognizer::action(RuleContext *_localctx, int ruleIndex, int actionIndex) { + } + + + template + int Recognizer::getState() { + return _stateNumber; + } + + template + void Recognizer::setState(int atnState) { + // System.err.println("setState "+atnState); + _stateNumber = atnState; + // if ( traceATNStates ) _ctx.trace(atnState); + } + + template + void Recognizer::InitializeInstanceFields() { + _stateNumber = -1; + } + + template + Recognizer::Recognizer() { + InitializeInstanceFields(); + _listeners = std::vector(); + } + +#ifdef TODO + template + Recognizer:: + CopyOnWriteArrayListAnonymousInnerClassHelper::CopyOnWriteArrayListAnonymousInnerClassHelper() + { + add(&ConsoleErrorListener::INSTANCE) + } +#endif + + } + } + } + +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/RuleContext.cpp b/runtime/Cpp/org/antlr/v4/runtime/RuleContext.cpp new file mode 100755 index 000000000..ac54e9d32 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/RuleContext.cpp @@ -0,0 +1,222 @@ +#include +#include + +#include "RuleContext.h" +#include "Trees.h" +#include "Interval.h" +#include "StringBuilder.h" +#include "ParserRuleContext.h" +#include "Parser.h" +#include "Arrays.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + ParserRuleContext *const RuleContext::EMPTY = new ParserRuleContext(); + + RuleContext::RuleContext() { + InitializeInstanceFields(); + } + + RuleContext::RuleContext(RuleContext *parent, int invokingState) { + InitializeInstanceFields(); + this->parent = parent; + //if ( parent!=null ) System.out.println("invoke "+stateNumber+" from "+parent); + this->invokingState = invokingState; + } + + int RuleContext::depth() { + int n = 0; + RuleContext *p = this; + while (p != nullptr) { + p = p->parent; + n++; + } + return n; + } + + bool RuleContext::isEmpty() { + return invokingState == -1; + } + + misc::Interval *RuleContext::getSourceInterval() { + return misc::Interval::INVALID; + } + + RuleContext *RuleContext::getRuleContext() { + return this; + } + + RuleContext *RuleContext::getParent() { + return parent; + } + + void *RuleContext::getPayload() + /// + /// Return the combined text of all child nodes. This method only considers + /// tokens which have been added to the parse tree. + /// + /// Since tokens on hidden channels (e.g. whitespace or comments) are not + /// added to the parse trees, they will not appear in the output of this + /// method. + /// + /// + { + return this; + } + + std::wstring RuleContext::getText() { + if (getChildCount() == 0) { + return L""; + } + + antlrcpp::StringBuilder *builder = new antlrcpp::StringBuilder(); + for (int i = 0; i < getChildCount(); i++) { + builder->append(getChild(i)->getText()); + } + + return builder->toString(); + } + + int RuleContext::getRuleIndex() { + return -1; + } + + tree::ParseTree *RuleContext::getChild(int i) { + return nullptr; + } + + int RuleContext::getChildCount() { + return 0; + } + +#ifdef TODO + Future *RuleContext::inspect(Parser *parser) { + return inspect(parser->getRuleNames()); + } + + + Future *RuleContext::inspect(const std::vector &ruleNames) { + TreeViewer *viewer = new TreeViewer(ruleNames, this); + return viewer->open(); + } +#endif + void RuleContext::save(Parser *parser, const std::wstring &fileName) { + std::vector ruleNames; + if (parser != nullptr) { + ruleNames = parser->getRuleNames(); + } + save(ruleNames, fileName); + } + + void RuleContext::save(Parser *parser, const std::wstring &fileName, const std::wstring &fontName, int fontSize) { + std::vector ruleNames; + if (parser != nullptr) { + ruleNames = parser->getRuleNames(); + } + save(ruleNames, fileName, fontName, fontSize); + } + + void RuleContext::save(std::vector &ruleNames, const std::wstring &fileName) { +#ifdef TODO + tree::Trees::writePS(this, ruleNames, fileName); +#endif + } + + void RuleContext::save(std::vector &ruleNames, const std::wstring &fileName, const std::wstring &fontName, int fontSize) { +#ifdef TODO + tree::Trees::writePS(this, ruleNames, fileName, fontName, fontSize); +#endif + } + + std::wstring RuleContext::toStringTree(Parser *recog) { + return tree::Trees::toStringTree(this, recog); + } + + std::wstring RuleContext::toStringTree(std::vector &ruleNames) { + return tree::Trees::toStringTree(this, ruleNames); + } + + std::wstring RuleContext::toStringTree() { + return toStringTree(nullptr); + } + + + std::wstring RuleContext::toString(const std::vector &ruleNames) { + return toString(ruleNames, static_cast(nullptr)); + } + + + std::wstring RuleContext::toString(const std::vector &ruleNames, RuleContext *stop) { + antlrcpp::StringBuilder *buf = new antlrcpp::StringBuilder(); + RuleContext *p = this; + buf->append(L"["); + while (p != nullptr && p != stop) { + if (ruleNames.empty()) { + if (!p->isEmpty()) { + buf->append(p->invokingState); + } + } else { + int ruleIndex = p->getRuleIndex(); + + std::wstring ruleName = ruleIndex >= 0 && ruleIndex < (int)ruleNames.size() ? ruleNames[ruleIndex] : std::to_wstring(ruleIndex); + buf->append(ruleName); + } + + if (p->parent != nullptr && (ruleNames.size() > 0 || !p->parent->isEmpty())) { + buf->append(L" "); + } + + p = p->parent; + } + + buf->append(L"]"); + + return buf->toString(); + } + + std::wstring RuleContext::toString() { +#ifdef TODO +#endif + return L"TODO"; + }; + + void RuleContext::InitializeInstanceFields() { + invokingState = -1; + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/RuleContext.h b/runtime/Cpp/org/antlr/v4/runtime/RuleContext.h new file mode 100755 index 000000000..369f6abde --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/RuleContext.h @@ -0,0 +1,185 @@ +#pragma once + +#include +#include + +#include "RuleNode.h" +#include "ParseTreeVisitor.h" +#include "Recognizer.h" +#include "Declarations.h" + + + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + + /// + /// A rule context is a record of a single rule invocation. It knows + /// which context invoked it, if any. If there is no parent context, then + /// naturally the invoking state is not valid. The parent link + /// provides a chain upwards from the current rule invocation to the root + /// of the invocation tree, forming a stack. We actually carry no + /// information about the rule associated with this context (except + /// when parsing). We keep only the state number of the invoking state from + /// the ATN submachine that invoked this. Contrast this with the s + /// pointer inside ParserRuleContext that tracks the current state + /// being "executed" for the current rule. + /// + /// The parent contexts are useful for computing lookahead sets and + /// getting error information. + /// + /// These objects are used during parsing and prediction. + /// For the special case of parsers, we use the subclass + /// ParserRuleContext. + /// + /// + class RuleContext : public tree::RuleNode { + public: + static ParserRuleContext *const EMPTY; + + /// + /// What context invoked this rule? + RuleContext *parent; + + /// + /// What state invoked the rule associated with this context? + /// The "return address" is the followState of invokingState + /// If parent is null, this should be -1. + /// + int invokingState; + + RuleContext(); + + RuleContext(RuleContext *parent, int invokingState); + + virtual int depth(); + + /// + /// A context is empty if there is no invoking state; meaning nobody call + /// current context. + /// + virtual bool isEmpty(); + + // satisfy the ParseTree / SyntaxTree interface + + virtual misc::Interval *getSourceInterval() override; + + virtual RuleContext *getRuleContext() override; + virtual RuleContext *getParent() override; + virtual void *getPayload() override; + virtual std::wstring getText() override; + + virtual int getRuleIndex(); + + virtual ParseTree *getChild(int i) override; + + virtual int getChildCount() override; + + template + T accept(tree::ParseTreeVisitor *visitor) { + return visitor->visitChildren(this); + } + + /// + /// Call this method to view a parse tree in a dialog box visually. +#ifdef TODO + virtual Future *inspect(Parser *parser); + + virtual Future *inspect(std::vector &ruleNames); +#endif + /// + /// Save this tree in a postscript file + virtual void save(Parser *parser, const std::wstring &fileName); + + /// + /// Save this tree in a postscript file using a particular font name and size + virtual void save(Parser *parser, const std::wstring &fileName, const std::wstring &fontName, int fontSize); + + /// + /// Save this tree in a postscript file + virtual void save(std::vector &ruleNames, const std::wstring &fileName); + + /// + /// Save this tree in a postscript file using a particular font name and size + virtual void save(std::vector &ruleNames, const std::wstring &fileName, const std::wstring &fontName, int fontSize); + + /// + /// Print out a whole tree, not just a node, in LISP format + /// (root child1 .. childN). Print just a node if this is a leaf. + /// We have to know the recognizer so we can get rule names. + /// + virtual std::wstring toStringTree(Parser *recog); + + /// + /// Print out a whole tree, not just a node, in LISP format + /// (root child1 .. childN). Print just a node if this is a leaf. + /// + virtual std::wstring toStringTree(std::vector &ruleNames); + + virtual std::wstring toStringTree() override; + + virtual std::wstring toString(); + + + template + std::wstring toString(Recognizer *recog) { + // Circular include issue, TODO + //return toString(recog, ParserRuleContext::EMPTY); + return toString(recog, nullptr); + } + + std::wstring toString(const std::vector &ruleNames); + + // recog null unless ParserRuleContext, in which case we use subclass toString(...) + template + std::wstring toString(Recognizer *recog, RuleContext *stop) { + return toString(recog->getRuleNames(), stop); + } + + std::wstring toString(Token *, atn::ParserATNSimulator *) { + return L"TODO"; + } + + virtual std::wstring toString(const std::vector &ruleNames, RuleContext *stop); + + private: + void InitializeInstanceFields(); + }; + + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/Token.cpp b/runtime/Cpp/org/antlr/v4/runtime/Token.cpp new file mode 100755 index 000000000..691c7a870 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/Token.cpp @@ -0,0 +1,46 @@ +#include "Token.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + const int Token::INVALID_TYPE; + const int Token::EPSILON; + const int Token::MIN_USER_TOKEN_TYPE; + const int Token::_EOF; + const int Token::DEFAULT_CHANNEL; + const int Token::HIDDEN_CHANNEL; + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/Token.h b/runtime/Cpp/org/antlr/v4/runtime/Token.h new file mode 100755 index 000000000..f7ad1d7f7 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/Token.h @@ -0,0 +1,144 @@ +#pragma once + + +#include "CharStream.h" +#include +#include "Declarations.h" + + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + + /// + /// A token has properties: text, type, line, character position in the line + /// (so we can ignore tabs), token channel, index, and source from which + /// we obtained this token. + /// + class Token { + public: + static const int INVALID_TYPE = 0; + + /// + /// During lookahead operations, this "token" signifies we hit rule end ATN state + /// and did not follow it despite needing to. + /// + static const int EPSILON = -2; + + static const int MIN_USER_TOKEN_TYPE = 1; + + static const int _EOF = IntStream::_EOF; + + // This isn't necessary + virtual ~Token() {}; + + /// + /// All tokens go to the parser (unless skip() is called in that rule) + /// on a particular "channel". The parser tunes to a particular channel + /// so that whitespace etc... can go to the parser on a "hidden" channel. + /// + static const int DEFAULT_CHANNEL = 0; + + /// + /// Anything on different channel than DEFAULT_CHANNEL is not parsed + /// by parser. + /// + static const int HIDDEN_CHANNEL = 1; + + /// + /// Get the text of the token. + /// + virtual std::wstring getText() = 0; + + /// + /// Get the token type of the token + virtual int getType() = 0; + + /// + /// The line number on which the 1st character of this token was matched, + /// line=1..n + /// + virtual int getLine() = 0; + + /// + /// The index of the first character of this token relative to the + /// beginning of the line at which it occurs, 0..n-1 + /// + virtual int getCharPositionInLine() = 0; + + /// + /// Return the channel this token. Each token can arrive at the parser + /// on a different channel, but the parser only "tunes" to a single channel. + /// The parser ignores everything not on DEFAULT_CHANNEL. + /// + virtual int getChannel() = 0; + + /// + /// An index from 0..n-1 of the token object in the input stream. + /// This must be valid in order to print token streams and + /// use TokenRewriteStream. + /// + /// Return -1 to indicate that this token was conjured up since + /// it doesn't have a valid index. + /// + virtual int getTokenIndex() = 0; + + /// + /// The starting character index of the token + /// This method is optional; return -1 if not implemented. + /// + virtual int getStartIndex() = 0; + + /// + /// The last character index of the token. + /// This method is optional; return -1 if not implemented. + /// + virtual int getStopIndex() = 0; + + /// + /// Gets the which created this token. + /// + virtual TokenSource *getTokenSource() = 0; + + /// + /// Gets the from which this token was derived. + /// + virtual CharStream *getInputStream() = 0; + }; + + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/TokenFactory.cpp b/runtime/Cpp/org/antlr/v4/runtime/TokenFactory.cpp new file mode 100755 index 000000000..591d0da7d --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/TokenFactory.cpp @@ -0,0 +1,41 @@ +#include "TokenFactory.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/TokenFactory.h b/runtime/Cpp/org/antlr/v4/runtime/TokenFactory.h new file mode 100755 index 000000000..27b176152 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/TokenFactory.h @@ -0,0 +1,67 @@ +#pragma once + +#include +#include +#include "Declarations.h" + + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + /// + /// The default mechanism for creating tokens. It's used by default in Lexer and + /// the error handling strategy (to create missing tokens). Notifying the parser + /// of a new factory means that it notifies it's token source and error strategy. + /// + + template + class TokenFactory { + /// + /// This is the method used to create tokens in the lexer and in the + /// error handling strategy. If text!=null, than the start and stop positions + /// are wiped to -1 in the text override is set in the CommonToken. + /// + public: + virtual Symbol create(std::pair *source, int type, const std::wstring &text, int channel, int start, int stop, int line, int charPositionInLine) = 0; + + /// + /// Generically useful + virtual Symbol create(int type, const std::wstring &text) = 0; + }; + + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/TokenSource.cpp b/runtime/Cpp/org/antlr/v4/runtime/TokenSource.cpp new file mode 100755 index 000000000..972e5fc98 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/TokenSource.cpp @@ -0,0 +1,40 @@ +#include "TokenSource.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/TokenSource.h b/runtime/Cpp/org/antlr/v4/runtime/TokenSource.h new file mode 100755 index 000000000..756fa068a --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/TokenSource.h @@ -0,0 +1,119 @@ +#pragma once + +#include + +#include "Declarations.h" +#include "TokenFactory.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + /// + /// A source of tokens must provide a sequence of tokens via + /// and also must reveal it's source of characters; 's text is + /// computed from a ; it only store indices into the char + /// stream. + ///

+ /// Errors from the lexer are never passed to the parser. Either you want to keep + /// going or you do not upon token recognition error. If you do not want to + /// continue lexing then you do not want to continue parsing. Just throw an + /// exception not under and Java will naturally toss + /// you all the way out of the recognizers. If you want to continue lexing then + /// you should not throw an exception to the parser--it has already requested a + /// token. Keep lexing until you get a valid one. Just report errors and keep + /// going, looking for a valid token. + ///

+ class TokenSource { + /// + /// Return a object from your input stream (usually a + /// ). Do not fail/return upon lexing error; keep chewing + /// on the characters until you get a good one; errors are not passed through + /// to the parser. + /// + public: + virtual Token *nextToken() = 0; + + /// + /// Get the line number for the current position in the input stream. The + /// first line in the input is line 1. + /// + /// The line number for the current position in the input stream, or + /// 0 if the current token source does not track line numbers. + virtual int getLine() = 0; + + /// + /// Get the index into the current line for the current position in the input + /// stream. The first character on a line has position 0. + /// + /// The line number for the current position in the input stream, or + /// -1 if the current token source does not track character positions. + virtual int getCharPositionInLine() = 0; + + /// + /// Get the from which this token source is currently + /// providing tokens. + /// + /// The associated with the current position in + /// the input, or {@code null} if no input stream is available for the token + /// source. + virtual CharStream *getInputStream() = 0; + + /// + /// Gets the name of the underlying input source. This method returns a + /// non-null, non-empty string. If such a name is not known, this method + /// returns . + /// + virtual std::string getSourceName() = 0; + + /// + /// Set the this token source should use for creating + /// objects from the input. + /// + /// The to use for creating tokens. + template + void setTokenFactory(TokenFactory *factory) {}; + + /// + /// Gets the this token source is currently using for + /// creating objects from the input. + /// + /// The currently used by this token source. + virtual TokenFactory *getTokenFactory() = 0; + }; + + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/TokenStream.cpp b/runtime/Cpp/org/antlr/v4/runtime/TokenStream.cpp new file mode 100755 index 000000000..e89dd6a8c --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/TokenStream.cpp @@ -0,0 +1,41 @@ +#include "TokenStream.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + TokenStream::~TokenStream() {} + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/TokenStream.h b/runtime/Cpp/org/antlr/v4/runtime/TokenStream.h new file mode 100755 index 000000000..b00e71e51 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/TokenStream.h @@ -0,0 +1,172 @@ +#pragma once + +#include "IntStream.h" +#include "Declarations.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + /// + /// An whose symbols are instances. + /// + class TokenStream : public IntStream { + /// + /// Get the instance associated with the value returned by + /// . This method has the same pre- and post-conditions as + /// . In addition, when the preconditions of this method + /// are met, the return value is non-null and the value of + /// {@code LT(k).getType()==LA(k)}. + /// + /// + public: + virtual ~TokenStream(); + + virtual Token *LT(int k) = 0; + + /// + /// Gets the at the specified {@code index} in the stream. When + /// the preconditions of this method are met, the return value is non-null. + ///

+ /// The preconditions for this method are the same as the preconditions of + /// . If the behavior of {@code seek(index)} is + /// unspecified for the current state and given {@code index}, then the + /// behavior of this method is also unspecified. + ///

+ /// The symbol referred to by {@code index} differs from {@code seek()} only + /// in the case of filtering streams where {@code index} lies before the end + /// of the stream. Unlike {@code seek()}, this method does not adjust + /// {@code index} to point to a non-ignored symbol. + ///

+ /// if {code index} is less than 0 + /// if the stream does not support + /// retrieving the token at the specified index + virtual Token *get(int index) = 0; + + /// + /// Gets the underlying which provides tokens for this + /// stream. + /// + virtual TokenSource *getTokenSource() = 0; + + /// + /// Return the text of all tokens within the specified {@code interval}. This + /// method behaves like the following code (including potential exceptions + /// for violating preconditions of , but may be optimized by the + /// specific implementation. + /// + ///
+                    /// TokenStream stream = ...;
+                    /// String text = "";
+                    /// for (int i = interval.a; i <= interval.b; i++) {
+                    ///   text += stream.get(i).getText();
+                    /// }
+                    /// 
+ ///
+ /// The interval of tokens within this stream to get text + /// for. + /// The text of all tokens within the specified interval in this + /// stream. + /// + /// if {@code interval} is {@code null} + virtual std::wstring getText(misc::Interval *interval) = 0; + + /// + /// Return the text of all tokens in the stream. This method behaves like the + /// following code, including potential exceptions from the calls to + /// and , but may be + /// optimized by the specific implementation. + /// + ///
+                    /// TokenStream stream = ...;
+                    /// String text = stream.getText(new Interval(0, stream.size()));
+                    /// 
+ ///
+ /// The text of all tokens in the stream. + virtual std::wstring getText() = 0; + + /// + /// Return the text of all tokens in the source interval of the specified + /// context. This method behaves like the following code, including potential + /// exceptions from the call to , but may be + /// optimized by the specific implementation. + ///

+ /// If {@code ctx.getSourceInterval()} does not return a valid interval of + /// tokens provided by this stream, the behavior is unspecified. + /// + ///
+                    /// TokenStream stream = ...;
+                    /// String text = stream.getText(ctx.getSourceInterval());
+                    /// 
+ ///
+ /// The context providing the source interval of tokens to get + /// text for. + /// The text of all tokens within the source interval of {@code ctx}. + virtual std::wstring getText(RuleContext *ctx) = 0; + + /// + /// Return the text of all tokens in this stream between {@code start} and + /// {@code stop} (inclusive). + ///

+ /// If the specified {@code start} or {@code stop} token was not provided by + /// this stream, or if the {@code stop} occurred before the {@code start} + /// token, the behavior is unspecified. + ///

+ /// For streams which ensure that the method is + /// accurate for all of its provided tokens, this method behaves like the + /// following code. Other streams may implement this method in other ways + /// provided the behavior is consistent with this at a high level. + /// + ///

+                    /// TokenStream stream = ...;
+                    /// String text = "";
+                    /// for (int i = start.getTokenIndex(); i <= stop.getTokenIndex(); i++) {
+                    ///   text += stream.get(i).getText();
+                    /// }
+                    /// 
+ ///
+ /// The first token in the interval to get text for. + /// The last token in the interval to get text for (inclusive). + /// The text of all tokens lying between the specified {@code start} + /// and {@code stop} tokens. + /// + /// if this stream does not support + /// this method for the specified tokens + virtual std::wstring getText(Token *start, Token *stop) = 0; + }; + + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/TokenStreamRewriter.cpp b/runtime/Cpp/org/antlr/v4/runtime/TokenStreamRewriter.cpp new file mode 100755 index 000000000..f58edee9e --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/TokenStreamRewriter.cpp @@ -0,0 +1,442 @@ +#include "TokenStreamRewriter.h" +#include "Interval.h" +#include "vectorhelper.h" +#include "Exceptions.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + using org::antlr::v4::runtime::misc::Interval; + + TokenStreamRewriter::RewriteOperation::RewriteOperation(TokenStreamRewriter *outerInstance, int index) : outerInstance(outerInstance) { + + InitializeInstanceFields(); + this->index = index; + } + + TokenStreamRewriter::RewriteOperation::RewriteOperation(TokenStreamRewriter *outerInstance, int index, const std::wstring& text) : outerInstance(outerInstance) { + + InitializeInstanceFields(); + this->index = index; + this->text = text; + } + + int TokenStreamRewriter::RewriteOperation::execute(std::wstring *buf) { + return index; + } + + std::wstring TokenStreamRewriter::RewriteOperation::toString() { + std::wstring opName = L"TokenStreamRewriter"; + size_t index = opName.find(L'$'); + opName = opName.substr(index + 1, opName.length() - (index + 1)); + return L"<" + opName + L"@" + outerInstance->tokens->get((int)index)->getText() + L":\"" + text + L"\">"; + } + + void TokenStreamRewriter::RewriteOperation::InitializeInstanceFields() { + instructionIndex = 0; + index = 0; + } + + TokenStreamRewriter::InsertBeforeOp::InsertBeforeOp(TokenStreamRewriter *outerInstance, int index, const std::wstring& text) : RewriteOperation(outerInstance, index, text), outerInstance(outerInstance) { + } + + int TokenStreamRewriter::InsertBeforeOp::execute(std::wstring *buf) { + buf->append(text); + if (outerInstance->tokens->get(index)->getType() != Token::_EOF) { + buf->append(outerInstance->tokens->get(index)->getText()); + } + return index + 1; + } + + TokenStreamRewriter::ReplaceOp::ReplaceOp(TokenStreamRewriter *outerInstance, int from, int to, const std::wstring& text) : RewriteOperation(outerInstance, from, text), outerInstance(outerInstance) { + + InitializeInstanceFields(); + lastIndex = to; + } + + int TokenStreamRewriter::ReplaceOp::execute(std::wstring *buf) { + buf->append(text); + return lastIndex + 1; + } + + std::wstring TokenStreamRewriter::ReplaceOp::toString() { + if (text.empty()) { + return L"tokens->get(index)->getText() + L".." + outerInstance->tokens->get(lastIndex)->getText() + L">"; + } + return L"tokens->get(index)->getText() + L".." + outerInstance->tokens->get(lastIndex)->getText() + L":\"" + text + L"\">"; + } + + void TokenStreamRewriter::ReplaceOp::InitializeInstanceFields() { + lastIndex = 0; + } + + const std::wstring TokenStreamRewriter::DEFAULT_PROGRAM_NAME = L"default"; + + TokenStreamRewriter::TokenStreamRewriter(TokenStream *tokens) : tokens(tokens), programs(new std::map>()), lastRewriteTokenIndexes(new std::map()) { + programs->insert(std::pair>(DEFAULT_PROGRAM_NAME, antlrcpp::VectorHelper::VectorWithReservedSize(PROGRAM_INIT_SIZE))); + } + + TokenStream *TokenStreamRewriter::getTokenStream() { + return tokens; + } + + void TokenStreamRewriter::rollback(int instructionIndex) { + rollback(DEFAULT_PROGRAM_NAME, instructionIndex); + } + + void TokenStreamRewriter::rollback(const std::wstring &programName, int instructionIndex) { + std::vector is = programs->at(programName); + if (is.size() > 0) { + programs->insert(std::pair >(programName, antlrcpp::VectorHelper::VectorSublist(is, MIN_TOKEN_INDEX, instructionIndex))); + } + } + + void TokenStreamRewriter::deleteProgram() { + deleteProgram(DEFAULT_PROGRAM_NAME); + } + + void TokenStreamRewriter::deleteProgram(const std::wstring &programName) { + rollback(programName, MIN_TOKEN_INDEX); + } + + void TokenStreamRewriter::insertAfter(Token *t, const std::wstring& text) { + insertAfter(DEFAULT_PROGRAM_NAME, t, text); + } + + void TokenStreamRewriter::insertAfter(int index, const std::wstring& text) { + insertAfter(DEFAULT_PROGRAM_NAME, index, text); + } + + void TokenStreamRewriter::insertAfter(const std::wstring &programName, Token *t, const std::wstring& text) { + insertAfter(programName, t->getTokenIndex(), text); + } + + void TokenStreamRewriter::insertAfter(const std::wstring &programName, int index, const std::wstring& text) { + // to insert after, just insert before next index (even if past end) + insertBefore(programName, index + 1, text); + } + + void TokenStreamRewriter::insertBefore(Token *t, const std::wstring& text) { + insertBefore(DEFAULT_PROGRAM_NAME, t, text); + } + + void TokenStreamRewriter::insertBefore(int index, const std::wstring& text) { + insertBefore(DEFAULT_PROGRAM_NAME, index, text); + } + + void TokenStreamRewriter::insertBefore(const std::wstring &programName, Token *t, const std::wstring& text) { + insertBefore(programName, t->getTokenIndex(), text); + } + + void TokenStreamRewriter::insertBefore(const std::wstring &programName, int index, const std::wstring& text) { + RewriteOperation *op = new InsertBeforeOp(this, index, text); + std::vector rewrites = getProgram(programName); + op->instructionIndex = (int)rewrites.size(); + rewrites.push_back(op); + } + + void TokenStreamRewriter::replace(int index, const std::wstring& text) { + replace(DEFAULT_PROGRAM_NAME, index, index, text); + } + + void TokenStreamRewriter::replace(int from, int to, const std::wstring& text) { + replace(DEFAULT_PROGRAM_NAME, from, to, text); + } + + void TokenStreamRewriter::replace(Token *indexT, const std::wstring& text) { + replace(DEFAULT_PROGRAM_NAME, indexT, indexT, text); + } + + void TokenStreamRewriter::replace(Token *from, Token *to, const std::wstring& text) { + replace(DEFAULT_PROGRAM_NAME, from, to, text); + } + + void TokenStreamRewriter::replace(const std::wstring &programName, int from, int to, const std::wstring& text) { + if (from > to || from < 0 || to < 0 || to >= (int)tokens->size()) { + throw IllegalArgumentException(L"replace: range invalid: " + std::to_wstring(from) + L".." + std::to_wstring(to) + L"(size=" + std::to_wstring(tokens->size()) + L")"); + } + RewriteOperation *op = new ReplaceOp(this, from, to, text); + std::vector rewrites = getProgram(programName); + op->instructionIndex = (int)rewrites.size(); + rewrites.push_back(op); + } + + void TokenStreamRewriter::replace(const std::wstring &programName, Token *from, Token *to, const std::wstring& text) { + replace(programName, from->getTokenIndex(), to->getTokenIndex(), text); + } + + void TokenStreamRewriter::delete_Renamed(int index) { + delete_Renamed(DEFAULT_PROGRAM_NAME, index, index); + } + + void TokenStreamRewriter::delete_Renamed(int from, int to) { + delete_Renamed(DEFAULT_PROGRAM_NAME, from, to); + } + + void TokenStreamRewriter::delete_Renamed(Token *indexT) { + delete_Renamed(DEFAULT_PROGRAM_NAME, indexT, indexT); + } + + void TokenStreamRewriter::delete_Renamed(Token *from, Token *to) { + delete_Renamed(DEFAULT_PROGRAM_NAME, from, to); + } + + void TokenStreamRewriter::delete_Renamed(const std::wstring &programName, int from, int to) { + replace(programName, from, to, nullptr); + } + + void TokenStreamRewriter::delete_Renamed(const std::wstring &programName, Token *from, Token *to) { + replace(programName, from, to, nullptr); + } + + int TokenStreamRewriter::getLastRewriteTokenIndex() { + return getLastRewriteTokenIndex(DEFAULT_PROGRAM_NAME); + } + + int TokenStreamRewriter::getLastRewriteTokenIndex(const std::wstring &programName) { + if (lastRewriteTokenIndexes->find(programName) == lastRewriteTokenIndexes->end()) { + return -1; + } + return lastRewriteTokenIndexes->at(programName); + } + + void TokenStreamRewriter::setLastRewriteTokenIndex(const std::wstring &programName, int i) { + lastRewriteTokenIndexes->insert({ programName, i }); + } + + std::vector TokenStreamRewriter::getProgram(const std::wstring &name) { + std::vector is = programs->at(name); + if (is.empty()) { + is = initializeProgram(name); + } + return is; + } + + std::vector TokenStreamRewriter::initializeProgram(const std::wstring &name) { + std::vector is = antlrcpp::VectorHelper::VectorWithReservedSize(PROGRAM_INIT_SIZE); + programs->insert({ name, is }); + return is; + } + + std::wstring TokenStreamRewriter::getText() { + return getText(DEFAULT_PROGRAM_NAME, Interval::of(0, (int)tokens->size() - 1)); + } + + std::wstring TokenStreamRewriter::getText(Interval *interval) { + return getText(DEFAULT_PROGRAM_NAME, interval); + } + + std::wstring TokenStreamRewriter::getText(const std::wstring &programName, Interval *interval) { + std::vector rewrites = programs->at(programName); + int start = interval->a; + int stop = interval->b; + + // ensure start/end are in range + if (stop > (int)tokens->size() - 1) { + stop = (int)tokens->size() - 1; + } + if (start < 0) { + start = 0; + } + + if (rewrites.empty() || rewrites.empty()) { + return tokens->getText(interval); // no instructions to execute + } + std::wstring buf; + + // First, optimize instruction stream + std::unordered_map *indexToOp = reduceToSingleOperationPerIndex(rewrites); + + // Walk buffer, executing instructions and emitting tokens + size_t i = (size_t)start; + while (i <= (size_t)stop && i < tokens->size()) { + RewriteOperation *op = indexToOp->at((int)i); + indexToOp->erase((int)i); // remove so any left have index size-1 + Token *t = tokens->get((int)i); + if (op == nullptr) { + // no operation at that index, just dump token + if (t->getType() != Token::_EOF) { + buf.append(t->getText()); + } + i++; // move to next token + } + else { + i = op->execute(&buf); // execute operation and skip + } + } + + // include stuff after end if it's last index in buffer + // So, if they did an insertAfter(lastValidIndex, "foo"), include + // foo if end==lastValidIndex. + if (stop == (int)tokens->size() - 1) { + // Scan any remaining operations after last token + // should be included (they will be inserts). + for (auto op : *indexToOp) { + if (op.second->index >= (int)tokens->size() - 1) { + buf.append(op.second->text); + } + } + } + return buf; + } + + std::unordered_map *TokenStreamRewriter::reduceToSingleOperationPerIndex(std::vector &rewrites) { + // System.out.println("rewrites="+rewrites); + + // WALK REPLACES + for (size_t i = 0; i < rewrites.size(); ++i) { + TokenStreamRewriter::RewriteOperation *op = rewrites[i]; + if (op == nullptr) { + continue; + } + if (dynamic_cast(op) == nullptr) { + continue; + } + ReplaceOp *rop = static_cast(op); + // Wipe prior inserts within range + InsertBeforeOp* type = nullptr; + std::vector inserts = getKindOfOps(rewrites, type, (int)i); + for (auto iop : inserts) { + if (iop->index == rop->index) { + // E.g., insert before 2, delete 2..2; update replace + // text to include insert before, kill insert + //JAVA TO C++ CONVERTER WARNING: Java to C++ Converter converted the original 'null' assignment to a call to 'delete', but you should review memory allocation of all pointer variables in the converted code: + delete rewrites[iop->instructionIndex]; + rop->text = iop->text + (!rop->text.empty() ? rop->text : L""); + } + else if (iop->index > rop->index && iop->index <= rop->lastIndex) { + // delete insert as it's a no-op. + //JAVA TO C++ CONVERTER WARNING: Java to C++ Converter converted the original 'null' assignment to a call to 'delete', but you should review memory allocation of all pointer variables in the converted code: + delete rewrites[iop->instructionIndex]; + } + } + // Drop any prior replaces contained within + ReplaceOp* type2 = nullptr; + std::vector prevReplaces = getKindOfOps(rewrites, type2, (int)i); + for (auto prevRop : prevReplaces) { + if (prevRop->index >= rop->index && prevRop->lastIndex <= rop->lastIndex) { + // delete replace as it's a no-op. + //JAVA TO C++ CONVERTER WARNING: Java to C++ Converter converted the original 'null' assignment to a call to 'delete', but you should review memory allocation of all pointer variables in the converted code: + delete rewrites[prevRop->instructionIndex]; + continue; + } + // throw exception unless disjoint or identical + bool disjoint = prevRop->lastIndex < rop->index || prevRop->index > rop->lastIndex; + bool same = prevRop->index == rop->index && prevRop->lastIndex == rop->lastIndex; + // Delete special case of replace (text==null): + // D.i-j.u D.x-y.v | boundaries overlap combine to max(min)..max(right) + if (prevRop->text.empty() && rop->text.empty() && !disjoint) { + //System.out.println("overlapping deletes: "+prevRop+", "+rop); + //JAVA TO C++ CONVERTER WARNING: Java to C++ Converter converted the original 'null' assignment to a call to 'delete', but you should review memory allocation of all pointer variables in the converted code: + delete rewrites[prevRop->instructionIndex]; // kill first delete + rop->index = std::min(prevRop->index, rop->index); + rop->lastIndex = std::max(prevRop->lastIndex, rop->lastIndex); + std::wcout << L"new rop " << rop << std::endl; + } + else if (!disjoint && !same) { + throw IllegalArgumentException(L"replace op boundaries of " + rop->toString() + L" overlap with previous " + prevRop->toString()); + } + } + } + + // WALK INSERTS + for (size_t i = 0; i < rewrites.size(); i++) { + RewriteOperation *op = rewrites[i]; + if (op == nullptr) { + continue; + } + if (!(dynamic_cast(op) != nullptr)) { + continue; + } + InsertBeforeOp *iop = static_cast(rewrites[i]); + // combine current insert with prior if any at same index + + std::vector prevInserts = getKindOfOps(rewrites, iop, (int)i); + for (auto prevIop : prevInserts) { + if (prevIop->index == iop->index) { // combine objects + // convert to strings...we're in process of toString'ing + // whole token buffer so no lazy eval issue with any templates + iop->text = catOpText(&iop->text, &prevIop->text); + // delete redundant prior insert + //JAVA TO C++ CONVERTER WARNING: Java to C++ Converter converted the original 'null' assignment to a call to 'delete', but you should review memory allocation of all pointer variables in the converted code: + delete rewrites[prevIop->instructionIndex]; + } + } + // look for replaces where iop.index is in range; error + ReplaceOp *type = nullptr; + std::vector prevReplaces = getKindOfOps(rewrites, type, (int)i); + for (auto rop : prevReplaces) { + if (iop->index == rop->index) { + rop->text = catOpText(&iop->text, &rop->text); + //JAVA TO C++ CONVERTER WARNING: Java to C++ Converter converted the original 'null' assignment to a call to 'delete', but you should review memory allocation of all pointer variables in the converted code: + delete rewrites[i]; // delete current insert + continue; + } + if (iop->index >= rop->index && iop->index <= rop->lastIndex) { + throw IllegalArgumentException(L"insert op " + iop->toString() + L" within boundaries of previous " + rop->toString()); + } + } + } + // System.out.println("rewrites after="+rewrites); + std::unordered_map *m = new std::unordered_map(); + for (TokenStreamRewriter::RewriteOperation *op : rewrites) { + if (op == nullptr) { // ignore deleted ops + continue; + } + if (m->at(op->index) != nullptr) { + // TODO: use a specific exception rather than a generic type here? + throw new ANTLRException(L"should only be one op per index"); + } + m->emplace(op->index, op); + } + //System.out.println("index to op: "+m); + return m; + } + + std::wstring TokenStreamRewriter::catOpText(std::wstring *a, std::wstring *b) { + std::wstring x = L""; + std::wstring y = L""; + if (a != nullptr) { + x = std::wstring(*a); + } + if (b != nullptr) { + y = std::wstring(*b); + } + return x + y; + } + + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/TokenStreamRewriter.h b/runtime/Cpp/org/antlr/v4/runtime/TokenStreamRewriter.h new file mode 100755 index 000000000..11b48a0af --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/TokenStreamRewriter.h @@ -0,0 +1,370 @@ +#pragma once + +#include "TokenStream.h" +#include "Token.h" +#include "misc/Interval.h" +#include +#include +#include +#include +#include +#include + +/* +* [The "BSD license"] +* Copyright (c) 2013 Terence Parr +* Copyright (c) 2013 Dan McLaughlin +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* 1. Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* 2. Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* 3. The name of the author may not be used to endorse or promote products +* derived from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + using org::antlr::v4::runtime::misc::Interval; + + + /// + /// Useful for rewriting out a buffered input token stream after doing some + /// augmentation or other manipulations on it. + /// + /// You can insert stuff, replace, and delete chunks. Note that the + /// operations are done lazily--only if you convert the buffer to a + /// String with getText(). This is very efficient because you are not moving + /// data around all the time. As the buffer of tokens is converted to strings, + /// the getText() method(s) scan the input token stream and check + /// to see if there is an operation at the current index. + /// If so, the operation is done and then normal String + /// rendering continues on the buffer. This is like having multiple Turing + /// machine instruction streams (programs) operating on a single input tape. :) + /// + /// This rewriter makes no modifications to the token stream. It does not + /// ask the stream to fill itself up nor does it advance the input cursor. + /// The token stream index() will return the same value before and after + /// any getText() call. + /// + /// The rewriter only works on tokens that you have in the buffer and + /// ignores the current input cursor. If you are buffering tokens on-demand, + /// calling getText() halfway through the input will only do rewrites + /// for those tokens in the first half of the file. + /// + /// Since the operations are done lazily at getText-time, operations do not + /// screw up the token index values. That is, an insert operation at token + /// index i does not change the index values for tokens i+1..n-1. + /// + /// Because operations never actually alter the buffer, you may always get + /// the original token stream back without undoing anything. Since + /// the instructions are queued up, you can easily simulate transactions and + /// roll back any changes if there is an error just by removing instructions. + /// For example, + /// + /// CharStream input = new ANTLRFileStream("input"); + /// TLexer lex = new TLexer(input); + /// CommonTokenStream tokens = new CommonTokenStream(lex); + /// T parser = new T(tokens); + /// TokenStreamRewriter rewriter = new TokenStreamRewriter(tokens); + /// parser.startRule(); + /// + /// Then in the rules, you can execute (assuming rewriter is visible): + /// Token t,u; + /// ... + /// rewriter.insertAfter(t, "text to put after t");} + /// rewriter.insertAfter(u, "text after u");} + /// System.out.println(tokens.toString()); + /// + /// You can also have multiple "instruction streams" and get multiple + /// rewrites from a single pass over the input. Just name the instruction + /// streams and use that name again when printing the buffer. This could be + /// useful for generating a C file and also its header file--all from the + /// same buffer: + /// + /// tokens.insertAfter("pass1", t, "text to put after t");} + /// tokens.insertAfter("pass2", u, "text after u");} + /// System.out.println(tokens.toString("pass1")); + /// System.out.println(tokens.toString("pass2")); + /// + /// If you don't use named rewrite streams, a "default" stream is used as + /// the first example shows. + /// + class TokenStreamRewriter { + public: + class RewriteOperation { + private: + TokenStreamRewriter *const outerInstance; + + /// + /// What index into rewrites List are we? + public: + virtual ~RewriteOperation() {}; + /// + /// Token buffer index. + int index; + std::wstring text; + + RewriteOperation(TokenStreamRewriter *outerInstance, int index); + + RewriteOperation(TokenStreamRewriter *outerInstance, int index, const std::wstring& text); + /// + /// Execute the rewrite operation by possibly adding to the buffer. + /// Return the index of the next token to operate on. + /// + int instructionIndex; + + virtual int execute(std::wstring *buf); + + virtual std::wstring toString(); + + private: + void InitializeInstanceFields(); + }; + + public: + class InsertBeforeOp : public RewriteOperation { + private: + TokenStreamRewriter *const outerInstance; + + public: + InsertBeforeOp(TokenStreamRewriter *outerInstance, int index, const std::wstring& text); + + virtual int execute(std::wstring *buf) override; + }; + + public: + class ReplaceOp : public RewriteOperation { + private: + TokenStreamRewriter *const outerInstance; + + public: + int lastIndex; + + ReplaceOp(TokenStreamRewriter *outerInstance, int from, int to, const std::wstring& text); + virtual int execute(std::wstring *buf) override; + virtual std::wstring toString() override; + + private: + void InitializeInstanceFields(); + }; + + public: + static const std::wstring DEFAULT_PROGRAM_NAME; + static const int PROGRAM_INIT_SIZE = 100; + static const int MIN_TOKEN_INDEX = 0; + + // Define the rewrite operation hierarchy + + /// + /// I'm going to try replacing range from x..y with (y-x)+1 ReplaceOp + /// instructions. + /// + /// + /// Our source stream + protected: + TokenStream *const tokens; + + /// + /// You may have multiple, named streams of rewrite operations. + /// I'm calling these things "programs." + /// Maps String (name) -> rewrite (List) + /// + std::map> * programs; + + /// + /// Map String (program name) -> Integer index + std::map * lastRewriteTokenIndexes; + + public: + TokenStreamRewriter(TokenStream *tokens); + virtual ~TokenStreamRewriter() {}; + + TokenStream *getTokenStream(); + + virtual void rollback(int instructionIndex); + + /// + /// Rollback the instruction stream for a program so that + /// the indicated instruction (via instructionIndex) is no + /// longer in the stream. UNTESTED! + /// + virtual void rollback(const std::wstring &programName, int instructionIndex); + + virtual void deleteProgram(); + + /// + /// Reset the program so that no instructions exist + virtual void deleteProgram(const std::wstring &programName); + + virtual void insertAfter(Token *t, const std::wstring& text); + + virtual void insertAfter(int index, const std::wstring& text); + + virtual void insertAfter(const std::wstring &programName, Token *t, const std::wstring& text); + + virtual void insertAfter(const std::wstring &programName, int index, const std::wstring& text); + + virtual void insertBefore(Token *t, const std::wstring& text); + + virtual void insertBefore(int index, const std::wstring& text); + + virtual void insertBefore(const std::wstring &programName, Token *t, const std::wstring& text); + + virtual void insertBefore(const std::wstring &programName, int index, const std::wstring& text); + + virtual void replace(int index, const std::wstring& text); + + virtual void replace(int from, int to, const std::wstring& text); + + virtual void replace(Token *indexT, const std::wstring& text); + + virtual void replace(Token *from, Token *to, const std::wstring& text); + + virtual void replace(const std::wstring &programName, int from, int to, const std::wstring& text); + + virtual void replace(const std::wstring &programName, Token *from, Token *to, const std::wstring& text); + + virtual void delete_Renamed(int index); + + virtual void delete_Renamed(int from, int to); + + virtual void delete_Renamed(Token *indexT); + + virtual void delete_Renamed(Token *from, Token *to); + + virtual void delete_Renamed(const std::wstring &programName, int from, int to); + + virtual void delete_Renamed(const std::wstring &programName, Token *from, Token *to); + + virtual int getLastRewriteTokenIndex(); + + protected: + virtual int getLastRewriteTokenIndex(const std::wstring &programName); + + virtual void setLastRewriteTokenIndex(const std::wstring &programName, int i); + + virtual std::vector getProgram(const std::wstring &name); + + private: + std::vector initializeProgram(const std::wstring &name); + + /// + /// Return the text from the original tokens altered per the + /// instructions given to this rewriter. + /// + public: + virtual std::wstring getText(); + + /// + /// Return the text associated with the tokens in the interval from the + /// original token stream but with the alterations given to this rewriter. + /// The interval refers to the indexes in the original token stream. + /// We do not alter the token stream in any way, so the indexes + /// and intervals are still consistent. Includes any operations done + /// to the first and last token in the interval. So, if you did an + /// insertBefore on the first token, you would get that insertion. + /// The same is true if you do an insertAfter the stop token. + /// + virtual std::wstring getText(Interval *interval); + + virtual std::wstring getText(const std::wstring &programName, Interval *interval); + + /// + /// We need to combine operations and report invalid operations (like + /// overlapping replaces that are not completed nested). Inserts to + /// same index need to be combined etc... Here are the cases: + /// + /// I.i.u I.j.v leave alone, nonoverlapping + /// I.i.u I.i.v combine: Iivu + /// + /// R.i-j.u R.x-y.v | i-j in x-y delete first R + /// R.i-j.u R.i-j.v delete first R + /// R.i-j.u R.x-y.v | x-y in i-j ERROR + /// R.i-j.u R.x-y.v | boundaries overlap ERROR + /// + /// Delete special case of replace (text==null): + /// D.i-j.u D.x-y.v | boundaries overlap combine to max(min)..max(right) + /// + /// I.i.u R.x-y.v | i in (x+1)-y delete I (since insert before + /// we're not deleting i) + /// I.i.u R.x-y.v | i not in (x+1)-y leave alone, nonoverlapping + /// R.x-y.v I.i.u | i in x-y ERROR + /// R.x-y.v I.x.u R.x-y.uv (combine, delete I) + /// R.x-y.v I.i.u | i not in x-y leave alone, nonoverlapping + /// + /// I.i.u = insert u before op @ index i + /// R.x-y.u = replace x-y indexed tokens with u + /// + /// First we need to examine replaces. For any replace op: + /// + /// 1. wipe out any insertions before op within that range. + /// 2. Drop any replace op before that is contained completely within + /// that range. + /// 3. Throw exception upon boundary overlap with any previous replace. + /// + /// Then we can deal with inserts: + /// + /// 1. for any inserts to same index, combine even if not adjacent. + /// 2. for any prior replace with same left boundary, combine this + /// insert with replace and delete this replace. + /// 3. throw exception if index in same range as previous replace + /// + /// Don't actually delete; make op null in list. Easier to walk list. + /// Later we can throw as we add to index -> op map. + /// + /// Note that I.2 R.2-2 will wipe out I.2 even though, technically, the + /// inserted stuff would be before the replace range. But, if you + /// add tokens in front of a method body '{' and then delete the method + /// body, I think the stuff before the '{' you added should disappear too. + /// + /// Return a map from token index to operation. + /// + protected: + virtual std::unordered_map *reduceToSingleOperationPerIndex(std::vector &rewrites); + + virtual std::wstring catOpText(std::wstring *a, std::wstring *b); + + /// + /// Get all operations before an index of a particular kind + template + std::vector getKindOfOps(std::vector rewrites, T *kind, int before) { + std::vector ops = std::vector(); + for (int i = 0; i < before && i < (int)rewrites.size(); i++) { + TokenStreamRewriter::RewriteOperation *op = dynamic_cast(rewrites[i]); + if (op == nullptr) { // ignore deleted + continue; + } + if (op != nullptr) { + ops.push_back(dynamic_cast(op)); + } + } + return ops; + } + + }; + + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/UnbufferedCharStream.cpp b/runtime/Cpp/org/antlr/v4/runtime/UnbufferedCharStream.cpp new file mode 100755 index 000000000..b4c2785b4 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/UnbufferedCharStream.cpp @@ -0,0 +1,233 @@ +#include "UnbufferedCharStream.h" + +#include + +#include "Interval.h" +#include "IntStream.h" +#include "ANTLRInputStream.h" +#include "Exceptions.h" + +/* +* [The "BSD license"] +* Copyright (c) 2013 Terence Parr +* Copyright (c) 2013 Dan McLaughlin +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* 1. Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* 2. Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* 3. The name of the author may not be used to endorse or promote products +* derived from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + UnbufferedCharStream::UnbufferedCharStream() { + } + + UnbufferedCharStream::UnbufferedCharStream(int bufferSize) { + InitializeInstanceFields(); + n = 0; + data = new wchar_t[bufferSize]; + } + + UnbufferedCharStream::UnbufferedCharStream(std::ifstream *input) { + } + + UnbufferedCharStream::UnbufferedCharStream(std::ifstream *input, int bufferSize) { + this->input = input; + fill(1); // prime + } + + void UnbufferedCharStream::consume() { + if (LA(1) == IntStream::_EOF) { + throw IllegalStateException(L"cannot consume EOF"); + } + + // buf always has at least data[p==0] in this method due to ctor + lastChar = data[p]; // track last char for LA(-1) + + if (p == n - 1 && numMarkers == 0) { + n = 0; + p = -1; // p++ will leave this at 0 + lastCharBufferStart = lastChar; + } + + p++; + currentCharIndex++; + sync(1); + } + + void UnbufferedCharStream::sync(int want) { + int need = (p + want - 1) - n + 1; // how many more elements we need? + if (need > 0) { + fill(need); + } + } + + int UnbufferedCharStream::fill(int n) { + for (int i = 0; i < n; i++) { + if (this->n > 0 && data[this->n - 1] == static_cast(IntStream::_EOF)) { + return i; + } + + try { + int c = nextChar(); + add(c); + } catch (IOException ioe) { + throw std::exception(ioe); + } + } + + return n; + } + + int UnbufferedCharStream::nextChar() { + return input->get(); + } + + void UnbufferedCharStream::add(int c) { + if (n >= (int)data.size()) { + data.reserve(data.size()*2); + } + data[n++] = static_cast(c); + } + + int UnbufferedCharStream::LA(int i) { + if (i == -1) { // special case + return lastChar; + } + sync(i); + int index = p + i - 1; + if (index < 0) { + throw new IndexOutOfBoundsException(); + } + if (index >= n) { + return IntStream::_EOF; + } + wchar_t c = data[index]; + if (c == static_cast(IntStream::_EOF)) { + return IntStream::_EOF; + } + return c; + } + + int UnbufferedCharStream::mark() { + if (numMarkers == 0) { + lastCharBufferStart = lastChar; + } + + int mark = -numMarkers - 1; + numMarkers++; + return mark; + } + + void UnbufferedCharStream::release(int marker) { + int expectedMark = -numMarkers; + if (marker != expectedMark) { + throw IllegalStateException(L"release() called with an invalid marker."); + } + + numMarkers--; + if (numMarkers == 0 && p > 0) { + data.erase(0, p); + n = n - p; + p = 0; + lastCharBufferStart = lastChar; + } + } + + int UnbufferedCharStream::index() { + return currentCharIndex; + } + + void UnbufferedCharStream::seek(int index) { + if (index == currentCharIndex) { + return; + } + + if (index > currentCharIndex) { + sync(index - currentCharIndex); + index = std::min(index, getBufferStartIndex() + n - 1); + } + + // index == to bufferStartIndex should set p to 0 + int i = index - getBufferStartIndex(); + if (i < 0) { + throw IllegalArgumentException(std::wstring(L"cannot seek to negative index ") + std::to_wstring(index)); + } else if (i >= n) { + throw UnsupportedOperationException(std::wstring(L"seek to index outside buffer: ") + std::to_wstring(index) + std::wstring(L" not in ") + std::to_wstring(getBufferStartIndex()) + std::wstring(L"..") + std::to_wstring(getBufferStartIndex() + n)); + } + + p = i; + currentCharIndex = index; + if (p == 0) { + lastChar = lastCharBufferStart; + } else { + lastChar = data[p - 1]; + } + } + + size_t UnbufferedCharStream::size() { + throw UnsupportedOperationException(std::wstring(L"Unbuffered stream cannot know its size")); + } + + std::string UnbufferedCharStream::getSourceName() { + return name; + } + + std::wstring UnbufferedCharStream::getText(misc::Interval *interval) { + if (interval->a < 0 || interval->b < interval->a - 1) { + throw IllegalArgumentException(std::wstring(L"invalid interval")); + } + + int bufferStartIndex = getBufferStartIndex(); + if (n > 0 && data[n - 1] == WCHAR_MAX) { + if (interval->a + interval->length() > bufferStartIndex + n) { + throw IllegalArgumentException(std::wstring(L"the interval extends past the end of the stream")); + } + } + + if (interval->a < bufferStartIndex || interval->b >= bufferStartIndex + n) { + throw UnsupportedOperationException(std::wstring(L"interval ") + interval->toString() + std::wstring(L" outside buffer: ") + std::to_wstring(bufferStartIndex) + std::wstring(L"..") + std::to_wstring(bufferStartIndex + n - 1)); + } + // convert from absolute to local index + int i = interval->a - bufferStartIndex; + return std::wstring(data, i, interval->length()); + } + + int UnbufferedCharStream::getBufferStartIndex() { + return currentCharIndex - p; + } + + void UnbufferedCharStream::InitializeInstanceFields() { + n = 0; + p = 0; + numMarkers = 0; + lastChar = -1; + lastCharBufferStart = 0; + currentCharIndex = 0; + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/UnbufferedCharStream.h b/runtime/Cpp/org/antlr/v4/runtime/UnbufferedCharStream.h new file mode 100755 index 000000000..67b6f2118 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/UnbufferedCharStream.h @@ -0,0 +1,191 @@ +#pragma once + +#include "CharStream.h" +#include "Declarations.h" +#include +#include +#include +#include + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + /// + /// Do not buffer up the entire char stream. It does keep a small buffer + /// for efficiency and also buffers while a mark exists (set by the + /// lookahead prediction in parser). "Unbuffered" here refers to fact + /// that it doesn't buffer all data, not that's it's on demand loading of char. + /// + class UnbufferedCharStream : public CharStream { + protected: + /// + /// A moving window buffer of the data being scanned. While there's a marker, + /// we keep adding to buffer. Otherwise, resets so + /// we start filling at index 0 again. + /// + std::wstring data; + + /// + /// The number of characters currently in . + ///

+ /// This is not the buffer capacity, that's {@code data.length}. + ///

+ int n; + + /// + /// 0..n-1 index into of next character. + ///

+ /// The {@code LA(1)} character is {@code data[p]}. If {@code p == n}, we are + /// out of buffered characters. + ///

+ int p; + + /// + /// Count up with and down with + /// . When we {@code release()} the last mark, + /// {@code numMarkers} reaches 0 and we reset the buffer. Copy + /// {@code data[p]..data[n-1]} to {@code data[0]..data[(n-1)-p]}. + /// + int numMarkers; + + /// + /// This is the {@code LA(-1)} character for the current position. + /// + int lastChar; + + /// + /// When {@code numMarkers > 0}, this is the {@code LA(-1)} character for the + /// first character in . Otherwise, this is unspecified. + /// + int lastCharBufferStart; + + /// + /// Absolute character index. It's the index of the character about to be + /// read via {@code LA(1)}. Goes from 0 to the number of characters in the + /// entire stream, although the stream size is unknown before the end is + /// reached. + /// + int currentCharIndex; + + std::ifstream *input; + + /// + /// The name or source of this char stream. + public: + std::string name; + + /// + /// Useful for subclasses that pull char from other than this.input. + + UnbufferedCharStream(); //this(256); + + /// + /// Useful for subclasses that pull char from other than this.input. + UnbufferedCharStream(int bufferSize); + + + UnbufferedCharStream(std::ifstream *input); //this(input, 256); + +//JAVA TO C++ CONVERTER TODO TASK: Calls to same-class constructors are not supported in C++ prior to C++11: + UnbufferedCharStream(std::ifstream *input, int bufferSize); //this(bufferSize); + + virtual void consume() override; + + /// + /// Make sure we have 'need' elements from current position . + /// Last valid {@code p} index is {@code data.length-1}. {@code p+need-1} is + /// the char index 'need' elements ahead. If we need 1 element, + /// {@code (p+1-1)==p} must be less than {@code data.length}. + /// + protected: + virtual void sync(int want); + + /// + /// Add {@code n} characters to the buffer. Returns the number of characters + /// actually added to the buffer. If the return value is less than {@code n}, + /// then EOF was reached before {@code n} characters could be added. + /// + virtual int fill(int n); + + /// + /// Override to provide different source of characters than + /// . + /// + virtual int nextChar(); + + virtual void add(int c); + + public: + virtual int LA(int i) override; + + /// + /// Return a marker that we can release later. + ///

+ /// The specific marker value used for this class allows for some level of + /// protection against misuse where {@code seek()} is called on a mark or + /// {@code release()} is called in the wrong order. + ///

+ virtual int mark() override; + + /// + /// Decrement number of markers, resetting buffer if we hit 0. + /// + virtual void release(int marker) override; + + virtual int index() override; + + /// + /// Seek to absolute character index, which might not be in the current + /// sliding window. Move {@code p} to {@code index-bufferStartIndex}. + /// + virtual void seek(int index) override; + + virtual size_t size() override; + + virtual std::string getSourceName() override; + + virtual std::wstring getText(misc::Interval *interval) override; + + protected: + int getBufferStartIndex(); + + private: + void InitializeInstanceFields(); + }; + + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/UnbufferedTokenStream.h b/runtime/Cpp/org/antlr/v4/runtime/UnbufferedTokenStream.h new file mode 100755 index 000000000..7363ed500 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/UnbufferedTokenStream.h @@ -0,0 +1,174 @@ +#pragma once + +#include "TokenStream.h" +#include "Declarations.h" + +#include +#include + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + + template + class UnbufferedTokenStream : public TokenStream { + protected: + TokenSource *tokenSource; + + /// + /// A moving window buffer of the data being scanned. While there's a marker, + /// we keep adding to buffer. Otherwise, resets so + /// we start filling at index 0 again. + /// + + std::vectortokens; + + /// + /// The number of tokens currently in . + ///

+ /// This is not the buffer capacity, that's {@code tokens.length}. + ///

+ int n; + + /// + /// 0..n-1 index into of next token. + ///

+ /// The {@code LT(1)} token is {@code tokens[p]}. If {@code p == n}, we are + /// out of buffered tokens. + ///

+ int p; + + /// + /// Count up with and down with + /// . When we {@code release()} the last mark, + /// {@code numMarkers} reaches 0 and we reset the buffer. Copy + /// {@code tokens[p]..tokens[n-1]} to {@code tokens[0]..tokens[(n-1)-p]}. + /// + int numMarkers; + + /// + /// This is the {@code LT(-1)} token for the current position. + /// + Token *lastToken; + + /// + /// When {@code numMarkers > 0}, this is the {@code LT(-1)} token for the + /// first token in . Otherwise, this is {@code null}. + /// + Token *lastTokenBufferStart; + + /// + /// Absolute token index. It's the index of the token about to be read via + /// {@code LT(1)}. Goes from 0 to the number of tokens in the entire stream, + /// although the stream size is unknown before the end is reached. + ///

+ /// This value is used to set the token indexes if the stream provides tokens + /// that implement . + ///

+ int currentTokenIndex; + + public: + UnbufferedTokenStream(TokenSource *tokenSource); + + UnbufferedTokenStream(TokenSource *tokenSource, int bufferSize); + + virtual Token *get(int i) override; + + virtual Token *LT(int i) override ; + + virtual int LA(int i) override ; + + virtual TokenSource *getTokenSource() override; + + virtual std::wstring getText() override ; + + virtual std::wstring getText(RuleContext *ctx) override; + + virtual std::wstring getText(Token *start, Token *stop) override; + + virtual void consume() override; + + /// + /// Make sure we have 'need' elements from current position . Last valid + /// {@code p} index is {@code tokens.length-1}. {@code p+need-1} is the tokens index 'need' elements + /// ahead. If we need 1 element, {@code (p+1-1)==p} must be less than {@code tokens.length}. + /// + protected: + virtual void sync(int want); + + /// + /// Add {@code n} elements to the buffer. Returns the number of tokens + /// actually added to the buffer. If the return value is less than {@code n}, + /// then EOF was reached before {@code n} tokens could be added. + /// + virtual int fill(int n); + + virtual void add(Token *t); + + /// + /// Return a marker that we can release later. + ///

+ /// The specific marker value used for this class allows for some level of + /// protection against misuse where {@code seek()} is called on a mark or + /// {@code release()} is called in the wrong order. + ///

+ public: + virtual int mark() override; + + virtual void release(int marker) override; + + virtual int index() override; + + virtual void seek(int index) override; + + virtual size_t size() override; + + virtual std::string getSourceName() override; + + virtual std::wstring getText(misc::Interval *interval) ; + + protected: + int getBufferStartIndex(); + + private: + void InitializeInstanceFields(); + }; + + } + } + } +} + +#include "UnbufferedTokenStream.inl" diff --git a/runtime/Cpp/org/antlr/v4/runtime/UnbufferedTokenStream.inl b/runtime/Cpp/org/antlr/v4/runtime/UnbufferedTokenStream.inl new file mode 100755 index 000000000..055aadf6c --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/UnbufferedTokenStream.inl @@ -0,0 +1,305 @@ +#pragma once + +#include "Declarations.h" +#include "Token.h" +#include "Exceptions.h" +#include "assert.h" +#include "TokenSource.h" +#include "Arrays.h" +#include "Interval.h" +#include "RuleContext.h" +#include "WritableToken.h" + + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + template + UnbufferedTokenStream::UnbufferedTokenStream(TokenSource *tokenSource) { //this(tokenSource, 256); + } + + template + UnbufferedTokenStream::UnbufferedTokenStream(TokenSource *tokenSource, int bufferSize) { + InitializeInstanceFields(); + this->tokenSource = tokenSource; + tokens = new std::vector(); // TODO do we need to actually new this? + n = 0; + fill(1); // prime the pump + } + + template + Token *UnbufferedTokenStream::get(int i) { // get absolute index + int bufferStartIndex = getBufferStartIndex(); + if (i < bufferStartIndex || i >= bufferStartIndex + n) { + throw new IndexOutOfBoundsException(std::wstring(L"get(") + std::to_wstring(i) + std::wstring(L") outside buffer: ") + std::to_wstring(bufferStartIndex) + std::wstring(L"..") + std::to_wstring(bufferStartIndex + n)); + } + return tokens[i - bufferStartIndex]; + } + + template + Token *UnbufferedTokenStream::LT(int i) { + if (i == -1) { + return lastToken; + } + + sync(i); + int index = p + i - 1; + if (index < 0) { + throw new IndexOutOfBoundsException(std::wstring(L"LT(") + std::to_wstring(i) + std::wstring(L") gives negative index")); + } + + if (index >= n) { + assert(n > 0 && tokens[n - 1]->getType() == Token::_EOF); + return tokens[n - 1]; + } + + return tokens[index]; + } + + template + int UnbufferedTokenStream::LA(int i) { + return LT(i)->getType(); + } + + template + TokenSource *UnbufferedTokenStream::getTokenSource() { + return tokenSource; + } + + template + std::wstring UnbufferedTokenStream::getText() { + return L""; + } + + template + std::wstring UnbufferedTokenStream::getText(RuleContext *ctx) { + return getText(ctx->getSourceInterval()); + } + + template + std::wstring UnbufferedTokenStream::getText(Token *start, Token *stop) { + return getText(misc::Interval::of(start->getTokenIndex(), stop->getTokenIndex())); + } + + template + void UnbufferedTokenStream::consume() { + if (LA(1) == Token::_EOF) { + throw new IllegalStateException(L"cannot consume EOF"); + } + + // buf always has at least tokens[p==0] in this method due to ctor + lastToken = tokens[p]; // track last token for LT(-1) + + // if we're at last token and no markers, opportunity to flush buffer + if (p == n - 1 && numMarkers == 0) { + n = 0; + p = -1; // p++ will leave this at 0 + lastTokenBufferStart = lastToken; + } + + p++; + currentTokenIndex++; + sync(1); + } + + /// + /// Make sure we have 'need' elements from current position . Last valid + /// {@code p} index is {@code tokens.length-1}. {@code p+need-1} is the tokens index 'need' elements + /// ahead. If we need 1 element, {@code (p+1-1)==p} must be less than {@code tokens.length}. + /// + template + void UnbufferedTokenStream::sync(int want) { + int need = (p + want - 1) - n + 1; // how many more elements we need? + if (need > 0) { + fill(need); + } + } + + /// + /// Add {@code n} elements to the buffer. Returns the number of tokens + /// actually added to the buffer. If the return value is less than {@code n}, + /// then EOF was reached before {@code n} tokens could be added. + /// + template + int UnbufferedTokenStream::fill(int n) { + for (int i = 0; i < n; i++) { + if (this->n > 0 && tokens[this->n - 1]->getType() == Token::_EOF) { + return i; + } + + Token *t = tokenSource->nextToken(); + add(t); + } + + return n; + } + template + void UnbufferedTokenStream::add(Token *t) { + if (n >= tokens.size()) { + tokens = Arrays::copyOf(tokens, tokens.size() * 2); + } + + if (dynamic_cast(t) != nullptr) { + (static_cast(t))->setTokenIndex(getBufferStartIndex() + n); + } + + tokens[n++] = t; + } + + /// + /// Return a marker that we can release later. + ///

+ /// The specific marker value used for this class allows for some level of + /// protection against misuse where {@code seek()} is called on a mark or + /// {@code release()} is called in the wrong order. + ///

+ template + int UnbufferedTokenStream::mark() { + if (numMarkers == 0) { + lastTokenBufferStart = lastToken; + } + + int mark = -numMarkers - 1; + numMarkers++; + return mark; + } + + template + void UnbufferedTokenStream::release(int marker) { + int expectedMark = -numMarkers; + if (marker != expectedMark) { + throw IllegalStateException(L"release() called with an invalid marker."); + } + + numMarkers--; + if (numMarkers == 0) { // can we release buffer? + if (p > 0) { + // Copy tokens[p]..tokens[n-1] to tokens[0]..tokens[(n-1)-p], reset ptrs + // p is last valid token; move nothing if p==n as we have no valid char + arraycopy(tokens, p, tokens, 0, n - p); // shift n-p tokens from p to 0 + n = n - p; + p = 0; + } + + lastTokenBufferStart = lastToken; + } + } + template + int UnbufferedTokenStream::index() { + return currentTokenIndex; + } + + template + void UnbufferedTokenStream::seek(int index) { // seek to absolute index + if (index == currentTokenIndex) { + return; + } + + if (index > currentTokenIndex) { + sync(index - currentTokenIndex); + index = std::min(index, getBufferStartIndex() + n - 1); + } + + int bufferStartIndex = getBufferStartIndex(); + int i = index - bufferStartIndex; + if (i < 0) { + throw new IllegalArgumentException(std::wstring(L"cannot seek to negative index ") + std::to_wstring(index)); + } + else if (i >= n) { + throw new UnsupportedOperationException(std::wstring(L"seek to index outside buffer: ") + std::to_wstring(index) + std::wstring(L" not in ") + std::to_wstring(bufferStartIndex) + std::wstring(L"..") + std::to_wstring(bufferStartIndex + n)); + } + + p = i; + currentTokenIndex = index; + if (p == 0) { + lastToken = lastTokenBufferStart; + } + else { + lastToken = tokens[p - 1]; + } + } + + template + size_t UnbufferedTokenStream::size() { + throw new UnsupportedOperationException(L"Unbuffered stream cannot know its size"); + } + + template + std::string UnbufferedTokenStream::getSourceName() { + return tokenSource->getSourceName(); + } + + template + std::wstring UnbufferedTokenStream::getText(misc::Interval *interval) { + int bufferStartIndex = getBufferStartIndex(); + int bufferStopIndex = bufferStartIndex + tokens.size() - 1; + + int start = interval->a; + int stop = interval->b; + if (start < bufferStartIndex || stop > bufferStopIndex) { + throw new UnsupportedOperationException(std::wstring(L"interval ") + interval->toString() + std::wstring(L" not in token buffer window: ") + std::to_wstring(bufferStartIndex) + std::wstring(L"..") + std::to_wstring(bufferStopIndex)); + } + + int a = start - bufferStartIndex; + int b = stop - bufferStartIndex; + + StringBuilder *buf = new StringBuilder(); + for (int i = a; i <= b; i++) { + Token *t = tokens[i]; + buf->append(t->getText()); + } + + return buf->toString(); + } + + template + int UnbufferedTokenStream::getBufferStartIndex() { + return currentTokenIndex - p; + } + + template + void UnbufferedTokenStream::InitializeInstanceFields() { + n = 0; + p = 0; + numMarkers = 0; + currentTokenIndex = 0; + } + + + } + + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/WritableToken.cpp b/runtime/Cpp/org/antlr/v4/runtime/WritableToken.cpp new file mode 100755 index 000000000..9ecb1f7d0 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/WritableToken.cpp @@ -0,0 +1,40 @@ +#include "WritableToken.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/WritableToken.h b/runtime/Cpp/org/antlr/v4/runtime/WritableToken.h new file mode 100755 index 000000000..963f1ba71 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/WritableToken.h @@ -0,0 +1,58 @@ +#pragma once + +#include "Token.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + class WritableToken : public Token { + public: + virtual void setText(const std::wstring &text) = 0; + + virtual void setType(int ttype) = 0; + + virtual void setLine(int line) = 0; + + virtual void setCharPositionInLine(int pos) = 0; + + virtual void setChannel(int channel) = 0; + + virtual void setTokenIndex(int index) = 0; + }; + + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/ATN.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/ATN.cpp new file mode 100755 index 000000000..e3dacf7b4 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/ATN.cpp @@ -0,0 +1,130 @@ +#include "ATN.h" +#include "LL1Analyzer.h" +#include "Token.h" +#include "RuleTransition.h" +#include "IntervalSet.h" +#include "Exceptions.h" +#include "RuleContext.h" +#include "DecisionState.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + //, states(new std::vector()), decisionToState(new std::vector()) + ATN::ATN(ATNType grammarType, int maxTokenType) : grammarType(grammarType), maxTokenType(maxTokenType), modeNameToStartState(new std::map()), modeToStartState(new std::vector()) { + } + + org::antlr::v4::runtime::misc::IntervalSet *ATN::nextTokens(ATNState *s, RuleContext *ctx) { + LL1Analyzer *anal = new LL1Analyzer(this); + misc::IntervalSet *next = anal->LOOK(s, ctx); + return next; + } + + org::antlr::v4::runtime::misc::IntervalSet *ATN::nextTokens(ATNState *s) { + if (s->nextTokenWithinRule != nullptr) { + return s->nextTokenWithinRule; + } + s->nextTokenWithinRule = nextTokens(s, nullptr); + s->nextTokenWithinRule->setReadonly(true); + return s->nextTokenWithinRule; + } + + void ATN::addState(ATNState *state) { + if (state != nullptr) { + state->atn = this; + state->stateNumber = (int)states.size(); + } + + states.push_back(state); + + } + + void ATN::removeState(ATNState *state) { + delete states.at(state->stateNumber);// just free mem, don't shift states in list + states.at(state->stateNumber) = nullptr; + } + + int ATN::defineDecisionState(DecisionState *s) { + decisionToState.push_back(s); + s->decision = (int)decisionToState.size() - 1; + return s->decision; + } + + org::antlr::v4::runtime::atn::DecisionState *ATN::getDecisionState(int decision) { + if (!decisionToState.empty()) { + return decisionToState.at(decision); + } + return nullptr; + } + + int ATN::getNumberOfDecisions() { + return (int)decisionToState.size(); + } + + misc::IntervalSet *ATN::getExpectedTokens(int stateNumber, RuleContext *context) { + if (stateNumber < 0 || stateNumber >= (int)states.size()) { + throw new IllegalArgumentException(L"Invalid state number."); + } + + RuleContext *ctx = context; + ATNState *s = states.at(stateNumber); + misc::IntervalSet *following = nextTokens(s); + if (!following->contains(Token::EPSILON)) { + return following; + } + + misc::IntervalSet *expected = new misc::IntervalSet(0); + expected->addAll(following); + expected->remove(Token::EPSILON); + while (ctx != nullptr && ctx->invokingState >= 0 && following->contains(Token::EPSILON)) { + ATNState *invokingState = states.at(ctx->invokingState); + RuleTransition *rt = static_cast(invokingState->transition(0)); + following = nextTokens(rt->followState); + expected->addAll(following); + expected->remove(Token::EPSILON); + ctx = ctx->parent; + } + + if (following->contains(Token::EPSILON)) { + expected->add(Token::_EOF); + } + + return expected; + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/ATN.h b/runtime/Cpp/org/antlr/v4/runtime/atn/ATN.h new file mode 100755 index 000000000..982ca7418 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/ATN.h @@ -0,0 +1,161 @@ +#pragma once + + +#include "Declarations.h" +#include "ATNType.h" + +#include +#include +#include + + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + class ATN { + public: + static const int INVALID_ALT_NUMBER = 0; + + std::vector states; + + /// + /// Each subrule/rule is a decision point and we must track them so we + /// can go back later and build DFA predictors for them. This includes + /// all the rules, subrules, optional blocks, ()+, ()* etc... + /// + // TODO: should this be const? (It used to be declared as const but was causing problems in ATNDeserializer.) + std::vector decisionToState; + + /// + /// Maps from rule index to starting state number. + /// + + std::vector ruleToStartState; + + /// + /// Maps from rule index to stop state number. + /// + // TODO: Memory Management + RuleStopState **ruleToStopState; + + std::map *const modeNameToStartState; + + /// + /// The type of the ATN. + /// + const ATNType grammarType; + + /// + /// The maximum value for any symbol recognized by a transition in the ATN. + /// + const int maxTokenType; + + /// + /// For lexer ATNs, this maps the rule index to the resulting token type. + /// For parser ATNs, this maps the rule index to the generated bypass token + /// type if the + /// + /// deserialization option was specified; otherwise, this is {@code null}. + /// + // TODO: Memory Management + int *ruleToTokenType; + + /// + /// For lexer ATNs, this maps the rule index to the action which should be + /// executed following a match. + ///

+ /// This is {@code null} for parser ATNs. + ///

+ // TODO: Memory Management + int *ruleToActionIndex; + + const std::vector * modeToStartState; + + /// + /// Used for runtime deserialization of ATNs from strings + ATN(ATNType grammarType, int maxTokenType); + + /// + /// Compute the set of valid tokens that can occur starting in state {@code s}. + /// If {@code ctx} is null, the set of tokens will not include what can follow + /// the rule surrounding {@code s}. In other words, the set will be + /// restricted to tokens reachable staying within {@code s}'s rule. + /// + virtual misc::IntervalSet *nextTokens(ATNState *s, RuleContext *ctx); + + /// + /// Compute the set of valid tokens that can occur starting in {@code s} and + /// staying in same rule. is in set if we reach end of + /// rule. + /// + virtual misc::IntervalSet *nextTokens(ATNState *s); + + virtual void addState(ATNState *state); + + virtual void removeState(ATNState *state); + + virtual int defineDecisionState(DecisionState *s); + + virtual DecisionState *getDecisionState(int decision); + + virtual int getNumberOfDecisions(); + + /// + /// Computes the set of input symbols which could follow ATN state number + /// {@code stateNumber} in the specified full {@code context}. This method + /// considers the complete parser context, but does not evaluate semantic + /// predicates (i.e. all predicates encountered during the calculation are + /// assumed true). If a path in the ATN exists from the starting state to the + /// of the outermost context without matching any + /// symbols, is added to the returned set. + ///

+ /// If {@code context} is {@code null}, it is treated as + /// . + ///

+ /// the ATN state number + /// the full parse context + /// The set of potentially valid input symbols which could follow the + /// specified state in the specified context. + /// if the ATN does not contain a state with + /// number {@code stateNumber} + virtual misc::IntervalSet *getExpectedTokens(int stateNumber, RuleContext *context); + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/ATNConfig.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/ATNConfig.cpp new file mode 100755 index 000000000..34585812f --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/ATNConfig.cpp @@ -0,0 +1,121 @@ +#include "ATNConfig.h" +#include "ATNState.h" +#include "PredictionContext.h" +#include "SemanticContext.h" +#include "MurmurHash.h" + +#include "StringBuilder.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + ATNConfig::ATNConfig(ATNConfig *old) : state(old->state), alt(old->alt), semanticContext(old->semanticContext) { + InitializeInstanceFields(); + this->context = old->context; + this->reachesIntoOuterContext = old->reachesIntoOuterContext; + } + + ATNConfig::ATNConfig(ATNState *state, int alt, PredictionContext *context) : state(state), alt(alt), context(context), semanticContext(nullptr) { + } + + ATNConfig::ATNConfig(ATNState *state, int alt, PredictionContext *context, SemanticContext *semanticContext) : state(state), alt(alt), semanticContext(semanticContext) { + InitializeInstanceFields(); + this->context = context; + } + + ATNConfig::ATNConfig(ATNConfig *c, ATNState *state) : state(state), alt(0), context(nullptr), semanticContext(nullptr) + { + } + + ATNConfig::ATNConfig(ATNConfig *c, ATNState *state, SemanticContext *semanticContext) : state(state), alt(0), context(nullptr), semanticContext(semanticContext) + { + } + + ATNConfig::ATNConfig(ATNConfig *c, SemanticContext *semanticContext) : state(nullptr), alt(0), context(nullptr), semanticContext(semanticContext) + { + } + + ATNConfig::ATNConfig(ATNConfig *c, ATNState *state, PredictionContext *context) : state(state), alt(0), context(context), semanticContext(nullptr) + { + } + + ATNConfig::ATNConfig(ATNConfig *c, ATNState *state, PredictionContext *context, SemanticContext *semanticContext) : state(state), alt(c->alt), semanticContext(semanticContext) { + InitializeInstanceFields(); + this->context = context; + this->reachesIntoOuterContext = c->reachesIntoOuterContext; + } + + bool ATNConfig::equals(void *o) { + if (!(o != nullptr/*dynamic_cast(o) != nullptr*/)) { + return false; + } + + return this->equals(static_cast(o)); + } + + bool ATNConfig::equals(ATNConfig *other) { + if (this == other) { + return true; + } + else if (other == nullptr) { + return false; + } + + return this->state->stateNumber == other->state->stateNumber && this->alt == other->alt && (this->context == other->context || (this->context != nullptr && this->context->equals(other->context))) && this->semanticContext->equals(other->semanticContext); + } + + size_t ATNConfig::hashCode() { + int hashCode = misc::MurmurHash::initialize(7); + hashCode = misc::MurmurHash::update(hashCode, state->stateNumber); + hashCode = misc::MurmurHash::update(hashCode, alt); + hashCode = misc::MurmurHash::update(hashCode, context); + hashCode = misc::MurmurHash::update(hashCode, semanticContext); + hashCode = misc::MurmurHash::finish(hashCode, 4); + return hashCode; + } + + std::wstring ATNConfig::toString() { + return toString(nullptr, true); + } + + + void ATNConfig::InitializeInstanceFields() { + reachesIntoOuterContext = 0; + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/ATNConfig.h b/runtime/Cpp/org/antlr/v4/runtime/atn/ATNConfig.h new file mode 100755 index 000000000..ce37852e5 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/ATNConfig.h @@ -0,0 +1,186 @@ +#pragma once + +#include + +#include "Declarations.h" +#include "ATNState.h" +#include "PredictionContext.h" +#include "SemanticContext.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + /// + /// A tuple: (ATN state, predicted alt, syntactic, semantic context). + /// The syntactic context is a graph-structured stack node whose + /// path(s) to the root is the rule invocation(s) + /// chain used to arrive at the state. The semantic context is + /// the tree of semantic predicates encountered before reaching + /// an ATN state. + /// + class ATNConfig { + /// + /// The ATN state associated with this configuration + public: + ATNState * state; + + /// + /// What alt (or lexer rule) is predicted by this configuration + const int alt; + + /// + /// The stack of invoking states leading to the rule/states associated + /// with this config. We track only those contexts pushed during + /// execution of the ATN simulator. + /// + PredictionContext *context; + + /// + /// We cannot execute predicates dependent upon local context unless + /// we know for sure we are in the correct context. Because there is + /// no way to do this efficiently, we simply cannot evaluate + /// dependent predicates unless we are in the rule that initially + /// invokes the ATN simulator. + /// + /// closure() tracks the depth of how far we dip into the + /// outer context: depth > 0. Note that it may not be totally + /// accurate depth since I don't ever decrement. TODO: make it a boolean then + /// + int reachesIntoOuterContext; + + SemanticContext *const semanticContext; + + ATNConfig(ATNConfig *old); // dup + + ATNConfig(ATNState *state, int alt, PredictionContext *context); //this(state, alt, context, SemanticContext.NONE); + + ATNConfig(ATNState *state, int alt, PredictionContext *context, SemanticContext *semanticContext); + + ATNConfig(ATNConfig *c, ATNState *state); //this(c, state, c.context, c.semanticContext); + + ATNConfig(ATNConfig *c, ATNState *state, SemanticContext *semanticContext); //this(c, state, c.context, semanticContext); + + ATNConfig(ATNConfig *c, SemanticContext *semanticContext); //this(c, c.state, c.context, semanticContext); + + ATNConfig(ATNConfig *c, ATNState *state, PredictionContext *context); //this(c, state, context, c.semanticContext); + + ATNConfig(ATNConfig *c, ATNState *state, PredictionContext *context, SemanticContext *semanticContext); + + /// + /// An ATN configuration is equal to another if both have + /// the same state, they predict the same alternative, and + /// syntactic/semantic contexts are the same. + /// + virtual bool equals(void *o); + + virtual bool equals(ATNConfig *other); + + virtual size_t hashCode(); + + + struct ATNConfigHasher + { + size_t operator()(const ATNConfig& k) const + { + ATNConfig a = k; + return a.hashCode(); + } + }; + + bool operator==(const ATNConfig& other) const + { + return alt == other.alt && state->stateNumber == other.state->stateNumber && + ((context == nullptr && other.context == nullptr) || (context != nullptr && context->equals(other.context))) && semanticContext->equals(other.semanticContext); + } + + virtual std::wstring toString(); + + template + std::wstring toString(Recognizer *recog, bool showAlt) { + antlrcpp::StringBuilder *buf = new antlrcpp::StringBuilder(); + // if ( state.ruleIndex>=0 ) { + // if ( recog!=null ) buf.append(recog.getRuleNames()[state.ruleIndex]+":"); + // else buf.append(state.ruleIndex+":"); + // } + buf->append(L'('); + buf->append(state); + if (showAlt) { + buf->append(L","); + buf->append(alt); + } + if (context != nullptr) { + buf->append(L",["); + // : + buf->append(context->toString()); + buf->append(L"]"); + } + if (semanticContext != nullptr && semanticContext != SemanticContext::NONE) { + buf->append(L","); + buf->append(semanticContext); + } + if (reachesIntoOuterContext > 0) { + buf->append(L",up=").append(reachesIntoOuterContext); + } + buf->append(L')'); + + return buf->toString(); + + } + + private: + void InitializeInstanceFields(); + }; + + } + + } + } + } +} + +// Hash function for ATNConfig, used in the MurmurHash::update function + +namespace std { + using org::antlr::v4::runtime::atn::ATNConfig; + + template <> struct hash + { + size_t operator()( ATNConfig & x) const + { + return x.hashCode(); + } + }; +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/ATNConfigSet.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/ATNConfigSet.cpp new file mode 100755 index 000000000..68bb1b89a --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/ATNConfigSet.cpp @@ -0,0 +1,319 @@ +#include + +#include "ATN.h" +#include "ATNState.h" +#include "ATNConfig.h" +#include "ATNConfigSet.h" +#include "SemanticContext.h" +#include "PredictionContext.h" +#include "StringBuilder.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + atn::ATNConfig *ATNConfigSet::AbstractConfigHashSet::asElementType(void *o) { + if (!(static_cast(o) != nullptr)) { + return nullptr; + } + + return static_cast(o); + } + + std::vector> * + ATNConfigSet::AbstractConfigHashSet::createBuckets(int capacity) { + return new std::vector>(); + } + + std::vector *ATNConfigSet::AbstractConfigHashSet::createBucket(int capacity) { + return new std::vector(); + } + + ATNConfigSet::ConfigHashSet::ConfigHashSet() : AbstractConfigHashSet(ConfigEqualityComparator::INSTANCE) { + } + + ATNConfigSet::ConfigEqualityComparator *const ATNConfigSet::ConfigEqualityComparator::INSTANCE = new ATNConfigSet::ConfigEqualityComparator(); + + ATNConfigSet::ConfigEqualityComparator::ConfigEqualityComparator() { + + } + + int ATNConfigSet::ConfigEqualityComparator::hashCode(ATNConfig *o) { + int hashCode = 7; + hashCode = 31 * hashCode + o->state->stateNumber; + hashCode = 31 * hashCode + o->alt; + hashCode = 31 * hashCode + o->semanticContext->hashCode(); + return hashCode; + } + + bool ATNConfigSet::ConfigEqualityComparator::equals(ATNConfig *a, ATNConfig *b) { + if (a == b) { + return true; + } + if (a == nullptr || b == nullptr) { + return false; + } + return a->state->stateNumber == b->state->stateNumber && a->alt == b->alt && a->semanticContext->equals(b->semanticContext); + } + + ATNConfigSet::ATNConfigSet(bool fullCtx) : fullCtx(fullCtx), configs(*new std::vector(/*7*/)) { + InitializeInstanceFields(); + configLookup = new ConfigHashSet(); + } + + ATNConfigSet::ATNConfigSet() : fullCtx(nullptr), configs(*new std::vector(/*7*/)) { + } + + ATNConfigSet::ATNConfigSet(ATNConfigSet *old) : fullCtx(nullptr), configs(*new std::vector(/*7*/)) { + this->addAll(old); + this->uniqueAlt = old->uniqueAlt; + this->conflictingAlts = old->conflictingAlts; + this->hasSemanticContext = old->hasSemanticContext; + this->dipsIntoOuterContext = old->dipsIntoOuterContext; + } + + bool ATNConfigSet::add(ATNConfig *config) { + return add(config, nullptr); + } + + bool ATNConfigSet::add(ATNConfig *config, misc::DoubleKeyMap *mergeCache) { + if (readonly) { + throw new IllegalStateException(L"This set is readonly"); + } + if (config->semanticContext != SemanticContext::NONE) { + hasSemanticContext = true; + } + if (config->reachesIntoOuterContext > 0) { + dipsIntoOuterContext = true; + } + ATNConfig *existing = configLookup->getOrAdd(config); + if (existing == config) { // we added this new one + cachedHashCode = -1; + configs.insert(configs.end(), config); // track order here + + return true; + } + // a previous (s,i,pi,_), merge with it and save result + bool rootIsWildcard = !fullCtx; + PredictionContext *merged = PredictionContext::merge(existing->context, config->context, rootIsWildcard, mergeCache); + // no need to check for existing.context, config.context in cache + // since only way to create new graphs is "call rule" and here. We + // cache at both places. + existing->reachesIntoOuterContext = std::max(existing->reachesIntoOuterContext, config->reachesIntoOuterContext); + existing->context = merged; // replace context; no need to alt mapping + return true; + } + + std::vector ATNConfigSet::elements() { + return configs; + } + + std::vector *ATNConfigSet::getStates() { + std::vector *states = new std::vector(); + for (auto c : configs) { + states->push_back(c->state); + } + return states; + } + + std::vector ATNConfigSet::getPredicates() { + std::vector preds = std::vector(); + for (auto c : configs) { + if (c->semanticContext != SemanticContext::NONE) { + preds.push_back(c->semanticContext); + } + } + return preds; + } + + org::antlr::v4::runtime::atn::ATNConfig *ATNConfigSet::get(int i) { + return configs[i]; + } + + void ATNConfigSet::optimizeConfigs(ATNSimulator *interpreter) { + if (readonly) { + throw IllegalStateException(L"This set is readonly"); + } + if (configLookup->isEmpty()) { + return; + } + + for (auto config : configs) { + // int before = PredictionContext.getAllContextNodes(config.context).size(); + config->context = interpreter->getCachedContext(config->context); + // int after = PredictionContext.getAllContextNodes(config.context).size(); + // System.out.println("configs "+before+"->"+after); + } + } + + + bool ATNConfigSet::equals(void *o) { + if (o == this) { + return true; + } else if (!(static_cast(o) != nullptr)) { + return false; + } + + // System.out.print("equals " + this + ", " + o+" = "); + ATNConfigSet *other = static_cast(o); + + bool configEquals = true; + + if (configs.size() == other->configs.size()) { + for (int i = 0; i < (int)configs.size(); i++) { + if ((int)other->configs.size() < i) { + configEquals = false; + break; + } + if (configs.at(i) != other->configs.at(i)) { + configEquals = false; + break; + } + } + } else { + configEquals = false; + } + + bool same = configs.size() > 0 && configEquals && this->fullCtx == other->fullCtx && this->uniqueAlt == other->uniqueAlt && this->conflictingAlts == other->conflictingAlts && this->hasSemanticContext == other->hasSemanticContext && this->dipsIntoOuterContext == other->dipsIntoOuterContext; // includes stack context + + // System.out.println(same); + return same; + } + + int ATNConfigSet::hashCode() { + // TODO - revisit and check this usage, including the seed + int hash = misc::MurmurHash::hashCode(configs.data(), configs.size(), 123456); + if (isReadonly()) { + if (cachedHashCode == -1) { + cachedHashCode = hash; + } + + return cachedHashCode; + } + + return hash; + } + + size_t ATNConfigSet::size() { + return configs.size(); + } + + bool ATNConfigSet::isEmpty() { + return configs.empty(); + } + + bool ATNConfigSet::contains(void *o) { + if (configLookup == nullptr) { + throw UnsupportedOperationException(L"This method is not implemented for readonly sets."); + } + + return configLookup->contains(o); + } + + bool ATNConfigSet::containsFast(ATNConfig *obj) { + if (configLookup == nullptr) { + throw UnsupportedOperationException(L"This method is not implemented for readonly sets."); + } + + return configLookup->containsFast(obj); + } + + std::vector::iterator const ATNConfigSet::iterator() { + return configs.begin(); + + } + + void ATNConfigSet::clear() { + if (readonly) { + throw new IllegalStateException(L"This set is readonly"); + } + configs.clear(); + cachedHashCode = -1; + configLookup->clear(); + } + + bool ATNConfigSet::isReadonly() { + return readonly; + } + + void ATNConfigSet::setReadonly(bool readonly) { + this->readonly = readonly; + delete configLookup; // can't mod, no need for lookup cache + } + + std::wstring ATNConfigSet::toString() { + antlrcpp::StringBuilder *buf = new antlrcpp::StringBuilder(); + for (int i = 0; i < (int)elements().size(); i++) { + buf->append(elements().at(i)->toString()); + } + + if (hasSemanticContext) { + buf->append(L",hasSemanticContext=").append(hasSemanticContext); + } + if (uniqueAlt != ATN::INVALID_ALT_NUMBER) { + buf->append(L",uniqueAlt=").append(uniqueAlt); + } + if (conflictingAlts != nullptr) { + buf->append(L",conflictingAlts="); + buf->append(conflictingAlts->toString()); + } + if (dipsIntoOuterContext) { + buf->append(L",dipsIntoOuterContext"); + } + return buf->toString(); + } + + ATNConfig *ATNConfigSet::toArray() { + return (ATNConfig*)configLookup->toArray(); + } + + + bool ATNConfigSet::remove(void *o) { + throw UnsupportedOperationException(); + } + + + void ATNConfigSet::InitializeInstanceFields() { + readonly = false; + uniqueAlt = 0; + hasSemanticContext = false; + dipsIntoOuterContext = false; + cachedHashCode = -1; + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/ATNConfigSet.h b/runtime/Cpp/org/antlr/v4/runtime/atn/ATNConfigSet.h new file mode 100755 index 000000000..ca7b46d2b --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/ATNConfigSet.h @@ -0,0 +1,243 @@ +#pragma once + +#include +#include +#include +#include + +#include "DoubleKeyMap.h" +#include "Declarations.h" +#include "Array2DHashSet.h" +#include "AbstractEqualityComparator.h" +#include "BitSet.h" +#include "Exceptions.h" + + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + /// + /// Specialized {@code <}{@code >} that can track + /// info about the set, with support for combining similar configurations using a + /// graph-structured stack. + /// + /// TODO: Consider going from std::set to std::vector + class ATNConfigSet : public std::set { + + public: + class AbstractConfigHashSet : public misc::Array2DHashSet { + + public: + + template + AbstractConfigHashSet(misc::AbstractEqualityComparator *comparator) {} + + template + AbstractConfigHashSet(misc::AbstractEqualityComparator *comparator, int initialCapacity, int initialBucketCapacity) {} + + protected: + ATNConfig *asElementType(void *o) override; + + std::vector> *createBuckets(int capacity) override; + + std::vector *createBucket(int capacity) override; + + }; + /// + /// The reason that we need this is because we don't want the hash map to use + /// the standard hash code and equals. We need all configurations with the same + /// {@code (s,i,_,semctx)} to be equal. Unfortunately, this key effectively doubles + /// the number of objects associated with ATNConfigs. The other solution is to + /// use a hash table that lets us specify the equals/hash code operation. + /// + public: + class ConfigHashSet : public AbstractConfigHashSet { + public: + ConfigHashSet(); + }; + + public: + class ConfigEqualityComparator : public misc::AbstractEqualityComparator { + public: + static ConfigEqualityComparator *const INSTANCE; + + private: + ConfigEqualityComparator(); + + public: + int hashCode(ATNConfig *o); + + bool equals(ATNConfig *a, ATNConfig *b); + }; + + /// + /// Indicates that the set of configurations is read-only. Do not + /// allow any code to manipulate the set; DFA states will point at + /// the sets and they must not change. This does not protect the other + /// fields; in particular, conflictingAlts is set after + /// we've made this readonly. + /// + protected: + bool readonly; + + /// + /// All configs but hashed by (s, i, _, pi) not including context. Wiped out + /// when we go readonly as this set becomes a DFA state. + /// + public: + AbstractConfigHashSet *configLookup; + + /// + /// Track the elements as they are added to the set; supports get(i) + std::vector configs; + + // TODO: these fields make me pretty uncomfortable but nice to pack up info together, saves recomputation + // TODO: can we track conflicts as they are added to save scanning configs later? + int uniqueAlt; + + antlrcpp::BitSet *conflictingAlts; + + // Used in parser and lexer. In lexer, it indicates we hit a pred + // while computing a closure operation. Don't make a DFA state from this. + public: + bool hasSemanticContext; + bool dipsIntoOuterContext; + + /// + /// Indicates that this configuration set is part of a full context + /// LL prediction. It will be used to determine how to merge $. With SLL + /// it's a wildcard whereas it is not for LL context merge. + /// + const bool fullCtx; + + private: + int cachedHashCode; + + public: + ATNConfigSet(bool fullCtx); + ATNConfigSet(); //this(true); + + ATNConfigSet(ATNConfigSet *old); //this(old.fullCtx); + + virtual bool add(ATNConfig *config); + + /// + /// Adding a new config means merging contexts with existing configs for + /// {@code (s, i, pi, _)}, where {@code s} is the + /// , {@code i} is the , and + /// {@code pi} is the . We use + /// {@code (s,i,pi)} as key. + ///

+ /// This method updates and + /// when necessary. + ///

+ virtual bool add(ATNConfig *config, misc::DoubleKeyMap *mergeCache); + + /// + /// Return a List holding list of configs + virtual std::vector elements(); + + virtual std::vector *getStates(); + + virtual std::vector getPredicates(); + + virtual ATNConfig *get(int i); + + virtual void optimizeConfigs(ATNSimulator *interpreter); + + template// where T1 : ATNConfig + bool addAll(ATNConfigSet *coll) { + for (auto c : *coll) { + add(c); + } + return false; + } + + virtual bool equals(void *o); + + virtual int hashCode(); + + virtual size_t size(); + + virtual bool isEmpty(); + + virtual bool contains(void *o); + + virtual bool containsFast(ATNConfig *obj); + + virtual std::vector::iterator const iterator(); + + virtual void clear(); + + virtual bool isReadonly(); + + virtual void setReadonly(bool readonly); + + virtual std::wstring toString(); + + // satisfy interface + + virtual ATNConfig *toArray(); + + template + T *toArray(T a[]) { + return configLookup->toArray(a); + } + virtual bool remove(void *o); + + template + bool containsAll(std::vector *c){ + throw new UnsupportedOperationException(); + } + template + bool retainAll(std::vector *c) { + throw new UnsupportedOperationException(); + } + + template + bool removeAll(std::vector *c) { + throw new UnsupportedOperationException(); + } + + + private: + void InitializeInstanceFields(); + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/ATNDeserializationOptions.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/ATNDeserializationOptions.cpp new file mode 100755 index 000000000..51869072d --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/ATNDeserializationOptions.cpp @@ -0,0 +1,97 @@ + +#include "ATNDeserializationOptions.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Sam Harwell + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + ATNDeserializationOptions * const ATNDeserializationOptions::defaultOptions = new ATNDeserializationOptions(); + + ATNDeserializationOptions::ATNDeserializationOptions() + : readOnly(true) { + } + + ATNDeserializationOptions::ATNDeserializationOptions(ATNDeserializationOptions *options) { + InitializeInstanceFields(); + this->verifyATN = options->verifyATN; + this->generateRuleBypassTransitions = options->generateRuleBypassTransitions; + } + + org::antlr::v4::runtime::atn::ATNDeserializationOptions *ATNDeserializationOptions::getDefaultOptions() { + return defaultOptions; + } + + bool ATNDeserializationOptions::isReadOnly() { + return readOnly; + } + + void ATNDeserializationOptions::makeReadOnly() { + readOnly = true; + } + + bool ATNDeserializationOptions::isVerifyATN() { + return verifyATN; + } + + void ATNDeserializationOptions::setVerifyATN(bool verifyATN) { + throwIfReadOnly(); + this->verifyATN = verifyATN; + } + + bool ATNDeserializationOptions::isGenerateRuleBypassTransitions() { + return generateRuleBypassTransitions; + } + + void ATNDeserializationOptions::setGenerateRuleBypassTransitions(bool generateRuleBypassTransitions) { + throwIfReadOnly(); + this->generateRuleBypassTransitions = generateRuleBypassTransitions; + } + + void ATNDeserializationOptions::throwIfReadOnly() { + if (isReadOnly()) { + throw L"The object is read only."; + } + } + + void ATNDeserializationOptions::InitializeInstanceFields() { + readOnly = false; + verifyATN = false; + generateRuleBypassTransitions = false; + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/ATNDeserializationOptions.h b/runtime/Cpp/org/antlr/v4/runtime/atn/ATNDeserializationOptions.h new file mode 100755 index 000000000..ad78ddbe4 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/ATNDeserializationOptions.h @@ -0,0 +1,81 @@ +#pragma once + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Sam Harwell + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + /// + /// + /// @author Sam Harwell + /// + class ATNDeserializationOptions { + private: + static ATNDeserializationOptions *const defaultOptions; + + bool readOnly; + bool verifyATN; + bool generateRuleBypassTransitions; + + public: + ATNDeserializationOptions(); + + ATNDeserializationOptions(ATNDeserializationOptions *options); + + static ATNDeserializationOptions *getDefaultOptions(); + + bool isReadOnly(); + + void makeReadOnly(); + + bool isVerifyATN(); + + void setVerifyATN(bool verifyATN); + + bool isGenerateRuleBypassTransitions(); + + void setGenerateRuleBypassTransitions(bool generateRuleBypassTransitions); + + protected: + virtual void throwIfReadOnly(); + + private: + void InitializeInstanceFields(); + }; + + } + } + } + } +} + diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/ATNDeserializer.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/ATNDeserializer.cpp new file mode 100755 index 000000000..c67bfd7be --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/ATNDeserializer.cpp @@ -0,0 +1,632 @@ +#include "ATNDeserializer.h" +#include "ATNDeserializationOptions.h" +#include "Declarations.h" +#include "ATNState.h" +#include "ATN.h" +#include "LoopEndState.h" +#include "Token.h" +#include "BlockStartState.h" +#include "BlockEndState.h" + +#include "ATNType.h" +#include "DecisionState.h" +#include "RuleStartState.h" +#include "RuleStopState.h" +#include "TokensStartState.h" +#include "RuleTransition.h" +#include "EpsilonTransition.h" +#include "PlusLoopbackState.h" +#include "PlusBlockStartState.h" +#include "StarLoopbackState.h" +#include "StarLoopEntryState.h" +#include "BasicBlockStartState.h" +#include "BasicState.h" +#include "AtomTransition.h" +#include "StarBlockStartState.h" +#include "RangeTransition.h" +#include "PredicateTransition.h" +#include "PrecedencePredicateTransition.h" +#include "ActionTransition.h" +#include "SetTransition.h" +#include "NotSetTransition.h" +#include "WildcardTransition.h" +#include "IntervalSet.h" + +#include +#include +#include + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + const int ATNDeserializer::SERIALIZED_VERSION = 3; + + /* This value should never change. Updates following this version are + * reflected as change in the unique ID SERIALIZED_UUID. + */ + antlrcpp::UUID *const ATNDeserializer::BASE_SERIALIZED_UUID = antlrcpp::UUID::fromString(L"33761B2D-78BB-4A43-8B0B-4F5BEE8AACF3"); + antlrcpp::UUID *const ATNDeserializer::ADDED_PRECEDENCE_TRANSITIONS = antlrcpp::UUID::fromString(L"1DA0C57D-6C06-438A-9B27-10BCB3CE0F61"); + const std::vector ATNDeserializer::SUPPORTED_UUIDS = supportedUUIDsInitializer(); + antlrcpp::UUID *const ATNDeserializer::SERIALIZED_UUID = ADDED_PRECEDENCE_TRANSITIONS; + + ATNDeserializer::ATNDeserializer(): deserializationOptions(deserializationOptionsInitializer(nullptr)) { + } + + ATNDeserializer::ATNDeserializer(ATNDeserializationOptions *dso): deserializationOptions(deserializationOptionsInitializer(dso)) { + } + + bool ATNDeserializer::isFeatureSupported(antlrcpp::UUID *feature, antlrcpp::UUID *actualUuid) { + // TODO: should SUPPORTED_UUIDS be of type std::vector instead? + /*int featureIndex = SUPPORTED_UUIDS.find(feature); + if (featureIndex < 0) { + return false; + } + + return SUPPORTED_UUIDS.find(actualUuid) >= featureIndex;*/ + return false; + } + + ATN *ATNDeserializer::deserialize(const std::wstring& _data) { + // TODO: data = data->clone(); + // don't adjust the first value since that's the version number + std::wstring data = _data; + for (size_t i = 1; i < sizeof(data) / sizeof(data[0]); i++) { + data[i] = static_cast(data[i] - 2); + } + + int p = 0; + int version = toInt(data[p++]); + if (version != SERIALIZED_VERSION) { + std::wstring reason = L"Could not deserialize ATN with version" + std::to_wstring(version) + L"(expected " + std::to_wstring(SERIALIZED_VERSION) + L")."; + + throw UnsupportedOperationException(reason); + } + + antlrcpp::UUID *uuid = toUUID(data, p); + p += 8; + if (!uuid->equals(SERIALIZED_UUID) && !uuid->equals(BASE_SERIALIZED_UUID)) { + std::wstring reason = L"Could not deserialize ATN with UUID " + + uuid->toString() + L" (expected " + SERIALIZED_UUID->toString() + + L" or a legacy UUID)."; + + throw UnsupportedOperationException(reason); + } + + bool supportsPrecedencePredicates = isFeatureSupported(ADDED_PRECEDENCE_TRANSITIONS, uuid); + + ATNType grammarType = (ATNType)toInt(data[p++]); + int maxTokenType = toInt(data[p++]); + ATN *atn = new ATN(grammarType, maxTokenType); + + // + // STATES + // + std::vector*> loopBackStateNumbers = std::vector*>(); + std::vector*> endStateNumbers = std::vector*>(); + int nstates = toInt(data[p++]); + for (int i = 0; i < nstates; i++) { + int stype = toInt(data[p++]); + // ignore bad type of states + if (stype == ATNState::ATN_INVALID_TYPE) { + atn->addState(nullptr); + continue; + } + + int ruleIndex = toInt(data[p++]); + if (ruleIndex == WCHAR_MAX) { + ruleIndex = -1; + } + + ATNState *s = stateFactory(stype, ruleIndex); + if (stype == ATNState::LOOP_END) { // special case + int loopBackStateNumber = toInt(data[p++]); + loopBackStateNumbers.push_back(new std::pair((LoopEndState*) s, loopBackStateNumber)); + } else if (/*dynamic_cast(*/s/*)*/ != nullptr) { + int endStateNumber = toInt(data[p++]); + endStateNumbers.push_back(new std::pair((BlockStartState*) s, endStateNumber)); + } + atn->addState(s); + } + + // delay the assignment of loop back and end states until we know all the state instances have been initialized + for (auto pair : loopBackStateNumbers) { + pair->first->loopBackState = atn->states.at(pair->second); + } + + for (auto pair : endStateNumbers) { + pair->first->endState = (BlockEndState*)atn->states.at(pair->second); + } + + int numNonGreedyStates = toInt(data[p++]); + for (int i = 0; i < numNonGreedyStates; i++) { + int stateNumber = toInt(data[p++]); + ((DecisionState*)atn->states.at(stateNumber) /*static_cast(atn->states[stateNumber])*/)->nonGreedy = true; + } + + if (supportsPrecedencePredicates) { + int numPrecedenceStates = toInt(data[p++]); + for (int i = 0; i < numPrecedenceStates; i++) { + int stateNumber = toInt(data[p++]); + (dynamic_cast(atn->states[stateNumber]))->isPrecedenceRule = true; + } + } + + // + // RULES + // + int nrules = toInt(data[p++]); + if (atn->grammarType == ATNType::LEXER) { + atn->ruleToTokenType = new int[nrules]; + atn->ruleToActionIndex = new int[nrules]; + } + + for (int i = 0; i < nrules; i++) { + int s = toInt(data[p++]); + RuleStartState *startState = /*static_cast*/(RuleStartState*)atn->states.at(s); + atn->ruleToStartState.push_back(startState); + if (atn->grammarType == ATNType::LEXER) { + int tokenType = toInt(data[p++]); + if (tokenType == 0xFFFF) { + tokenType = Token::_EOF; + } + + atn->ruleToTokenType[i] = tokenType; + int actionIndex = toInt(data[p++]); + if (actionIndex == 0xFFFF) { + actionIndex = -1; + } + + atn->ruleToActionIndex[i] = actionIndex; + } + } + + atn->ruleToStopState = new RuleStopState*[nrules]; + for (ATNState *state : atn->states) { + if (!(dynamic_cast(state) != nullptr)) { + continue; + } + + RuleStopState *stopState = static_cast(state); + atn->ruleToStopState[state->ruleIndex] = stopState; + atn->ruleToStartState[state->ruleIndex]->stopState = stopState; + } + + // + // MODES + // + int nmodes = toInt(data[p++]); + for (int i = 0; i < nmodes; i++) { + // TODO: really? atn->modeToStartState is const + //int s = toInt(data[p++]); + // atn->modeToStartState.push_back(static_cast(atn->states[s])); + } + + // + // SETS + // + std::vector sets = std::vector(); + int nsets = toInt(data[p++]); + for (int i = 0; i < nsets; i++) { + int nintervals = toInt(data[p]); + p++; + // TODO IntervalSet does not have a default constructor + //IntervalSet *set = new IntervalSet(); + misc::IntervalSet *set = nullptr; + sets.push_back(set); + + bool containsEof = toInt(data[p++]) != 0; + if (containsEof) { + set->add(-1); + } + + for (int j = 0; j < nintervals; j++) { + set->add(toInt(data[p]), toInt(data[p + 1])); + p += 2; + } + } + + // + // EDGES + // + int nedges = toInt(data[p++]); + for (int i = 0; i < nedges; i++) { + int src = toInt(data[p]); + int trg = toInt(data[p + 1]); + int ttype = toInt(data[p + 2]); + int arg1 = toInt(data[p + 3]); + int arg2 = toInt(data[p + 4]); + int arg3 = toInt(data[p + 5]); + Transition *trans = edgeFactory(atn, ttype, src, trg, arg1, arg2, arg3, sets); + // System.out.println("EDGE "+trans.getClass().getSimpleName()+" "+ + // src+"->"+trg+ + // " "+Transition.serializationNames[ttype]+ + // " "+arg1+","+arg2+","+arg3); + ATNState *srcState = atn->states[src]; + srcState->addTransition(trans); + p += 6; + } + + // edges for rule stop states can be derived, so they aren't serialized + for (ATNState *state : atn->states) { + for (int i = 0; i < state->getNumberOfTransitions(); i++) { + Transition *t = state->transition(i); + if (!(dynamic_cast(t) != nullptr)) { + continue; + } + + RuleTransition *ruleTransition = static_cast(t); + atn->ruleToStopState[ruleTransition->target->ruleIndex]->addTransition(new EpsilonTransition(ruleTransition->followState)); + } + } + + for (ATNState *state : atn->states) { + if (dynamic_cast(state) != nullptr) { + // we need to know the end state to set its start state + if ((static_cast(state))->endState == nullptr) { + throw new IllegalStateException(); + } + + // block end states can only be associated to a single block start state + if ((static_cast(state))->endState->startState != nullptr) { + throw new IllegalStateException(); + + } + + (static_cast(state))->endState->startState = static_cast(state); + } + + if (dynamic_cast(state) != nullptr) { + PlusLoopbackState *loopbackState = static_cast(state); + for (int i = 0; i < loopbackState->getNumberOfTransitions(); i++) { + ATNState *target = loopbackState->transition(i)->target; + if (dynamic_cast(target) != nullptr) { + (static_cast(target))->loopBackState = loopbackState; + } + } + } else if (dynamic_cast(state) != nullptr) { + StarLoopbackState *loopbackState = static_cast(state); + for (int i = 0; i < loopbackState->getNumberOfTransitions(); i++) { + ATNState *target = loopbackState->transition(i)->target; + if (dynamic_cast(target) != nullptr) { + (static_cast(target))->loopBackState = loopbackState; + } + } + } + } + + // + // DECISIONS + // + int ndecisions = toInt(data[p++]); + for (int i = 1; i <= ndecisions; i++) { + int s = toInt(data[p++]); + DecisionState *decState = static_cast(atn->states[s]); + // TODO: decisionToState was originally declared as const in ATN + atn->decisionToState.push_back(decState); + decState->decision = i - 1; + } + + if (deserializationOptions->isVerifyATN()) { + verifyATN(atn); + } + + if (deserializationOptions->isGenerateRuleBypassTransitions() && atn->grammarType == ATNType::PARSER) { + atn->ruleToTokenType = new int[atn->ruleToStartState.size()]; + for (std::vector::size_type i = 0; i < atn->ruleToStartState.size(); i++) { + atn->ruleToTokenType[i] = atn->maxTokenType + (int)i + 1; + } + + for (std::vector::size_type i = 0; i < atn->ruleToStartState.size(); i++) { + BasicBlockStartState *bypassStart = new BasicBlockStartState(); + bypassStart->ruleIndex = (int)i; + atn->addState(bypassStart); + + BlockEndState *bypassStop = new BlockEndState(); + bypassStop->ruleIndex = (int)i; + atn->addState(bypassStop); + + bypassStart->endState = bypassStop; + atn->defineDecisionState(bypassStart); + + bypassStop->startState = bypassStart; + + ATNState *endState; + Transition *excludeTransition = nullptr; + if (atn->ruleToStartState[i]->isPrecedenceRule) { + // wrap from the beginning of the rule to the StarLoopEntryState + endState = nullptr; + for (ATNState *state : atn->states) { + if (state->ruleIndex != (int)i) { + continue; + } + + if (!(dynamic_cast(state) != nullptr)) { + continue; + } + + ATNState *maybeLoopEndState = state->transition(state->getNumberOfTransitions() - 1)->target; + if (!(dynamic_cast(maybeLoopEndState) != nullptr)) { + continue; + } + + if (maybeLoopEndState->epsilonOnlyTransitions && dynamic_cast(maybeLoopEndState->transition(0)->target) != nullptr) { + endState = state; + break; + } + } + + if (endState == nullptr) { + throw UnsupportedOperationException(L"Couldn't identify final state of the precedence rule prefix section."); + + } + + excludeTransition = (static_cast(endState))->loopBackState->transition(0); + } else { + endState = atn->ruleToStopState[i]; + } + + // all non-excluded transitions that currently target end state need to target blockEnd instead + for (ATNState *state : atn->states) { + for (Transition *transition : state->getTransitions()) { + if (transition == excludeTransition) { + continue; + } + + if (transition->target == endState) { + transition->target = bypassStop; + } + } + } + + // all transitions leaving the rule start state need to leave blockStart instead + while (atn->ruleToStartState[i]->getNumberOfTransitions() > 0) { + Transition *transition = atn->ruleToStartState[i]->removeTransition(atn->ruleToStartState[i]->getNumberOfTransitions() - 1); + bypassStart->addTransition(transition); + } + + // link the new states + atn->ruleToStartState[i]->addTransition(new EpsilonTransition(bypassStart)); + bypassStop->addTransition(new EpsilonTransition(endState)); + + ATNState *matchState = new BasicState(); + atn->addState(matchState); + matchState->addTransition(new AtomTransition(bypassStop, atn->ruleToTokenType[i])); + bypassStart->addTransition(new EpsilonTransition(matchState)); + } + + if (deserializationOptions->isVerifyATN()) { + // reverify after modification + verifyATN(atn); + } + } + + return atn; + } + + void ATNDeserializer::verifyATN(ATN *atn) { + // verify assumptions + for (ATNState *state : atn->states) { + if (state == nullptr) { + continue; + } + + checkCondition(state->onlyHasEpsilonTransitions() || state->getNumberOfTransitions() <= 1); + + if (dynamic_cast(state) != nullptr) { + checkCondition((static_cast(state))->loopBackState != nullptr); + } + + if (dynamic_cast(state) != nullptr) { + StarLoopEntryState *starLoopEntryState = static_cast(state); + checkCondition(starLoopEntryState->loopBackState != nullptr); + checkCondition(starLoopEntryState->getNumberOfTransitions() == 2); + + if (dynamic_cast(starLoopEntryState->transition(0)->target) != nullptr) { + checkCondition(dynamic_cast(starLoopEntryState->transition(1)->target) != nullptr); + checkCondition(!starLoopEntryState->nonGreedy); + } else if (dynamic_cast(starLoopEntryState->transition(0)->target) != nullptr) { + checkCondition(dynamic_cast(starLoopEntryState->transition(1)->target) != nullptr); + checkCondition(starLoopEntryState->nonGreedy); + } else { + throw new IllegalStateException(); + + } + } + + if (dynamic_cast(state) != nullptr) { + checkCondition(state->getNumberOfTransitions() == 1); + checkCondition(dynamic_cast(state->transition(0)->target) != nullptr); + } + + if (dynamic_cast(state) != nullptr) { + checkCondition((static_cast(state))->loopBackState != nullptr); + } + + if (dynamic_cast(state) != nullptr) { + checkCondition((static_cast(state))->stopState != nullptr); + } + + if (dynamic_cast(state) != nullptr) { + checkCondition((static_cast(state))->endState != nullptr); + } + + if (dynamic_cast(state) != nullptr) { + checkCondition((static_cast(state))->startState != nullptr); + } + + if (dynamic_cast(state) != nullptr) { + DecisionState *decisionState = static_cast(state); + checkCondition(decisionState->getNumberOfTransitions() <= 1 || decisionState->decision >= 0); + } else { + checkCondition(state->getNumberOfTransitions() <= 1 || dynamic_cast(state) != nullptr); + } + } + } + + void ATNDeserializer::checkCondition(bool condition) { + checkCondition(condition, L""); + } + + void ATNDeserializer::checkCondition(bool condition, const std::wstring &message) { + if (!condition) { + // TODO: throw IllegalStateException(message); + throw std::exception(); + } + } + + int ATNDeserializer::toInt(wchar_t c) { + return c; + } + + int ATNDeserializer::toInt32(const std::wstring& data, int offset) { + return static_cast(data[offset]) | (static_cast(data[offset + 1]) << 16); + } + + long long ATNDeserializer::toLong(const std::wstring& data, int offset) { + long long lowOrder = toInt32(data, offset) & 0x00000000FFFFFFFFLL; + return lowOrder | (static_cast(toInt32(data, offset + 2)) << 32); + } + + antlrcpp::UUID *ATNDeserializer::toUUID(const std::wstring& data, int offset) { + long long leastSigBits = toLong(data, offset); + long long mostSigBits = toLong(data, offset + 4); + return new antlrcpp::UUID(mostSigBits, leastSigBits); + } + + Transition *ATNDeserializer::edgeFactory(ATN *atn, int type, int src, int trg, int arg1, int arg2, int arg3, std::vector &sets) { + ATNState *target = atn->states[trg]; + switch (type) { + case Transition::EPSILON : + return new EpsilonTransition(target); + case Transition::RANGE : + if (arg3 != 0) { + return new RangeTransition(target, Token::_EOF, arg2); + } else { + return new RangeTransition(target, arg1, arg2); + } + case Transition::RULE : + return new RuleTransition(static_cast(atn->states[arg1]), arg2, arg3, target); + case Transition::PREDICATE : + return new PredicateTransition(target, arg1, arg2, arg3 != 0); + case Transition::PRECEDENCE: + return new PrecedencePredicateTransition(target, arg1); + case Transition::ATOM : + if (arg3 != 0) { + return new AtomTransition(target, Token::_EOF); + } else { + return new AtomTransition(target, arg1); + } + case Transition::ACTION : + return new ActionTransition(target, arg1, arg2, arg3 != 0); + case Transition::SET : + return new SetTransition(target, sets[arg1]); + case Transition::NOT_SET : + return new NotSetTransition(target, sets[arg1]); + case Transition::WILDCARD : + return new WildcardTransition(target); + } + + throw IllegalArgumentException(L"The specified transition type is not valid."); + } + + ATNState *ATNDeserializer::stateFactory(int type, int ruleIndex) { + ATNState *s; + switch (type) { + case ATNState::ATN_INVALID_TYPE: + return nullptr; + case ATNState::BASIC : + s = new BasicState(); + break; + case ATNState::RULE_START : + s = new RuleStartState(); + break; + case ATNState::BLOCK_START : + s = new BasicBlockStartState(); + break; + case ATNState::PLUS_BLOCK_START : + s = new PlusBlockStartState(); + break; + case ATNState::STAR_BLOCK_START : + s = new StarBlockStartState(); + break; + case ATNState::TOKEN_START : + s = new TokensStartState(); + break; + case ATNState::RULE_STOP : + s = new RuleStopState(); + break; + case ATNState::BLOCK_END : + s = new BlockEndState(); + break; + case ATNState::STAR_LOOP_BACK : + s = new StarLoopbackState(); + break; + case ATNState::STAR_LOOP_ENTRY : + s = new StarLoopEntryState(); + break; + case ATNState::PLUS_LOOP_BACK : + s = new PlusLoopbackState(); + break; + case ATNState::LOOP_END : + s = new LoopEndState(); + break; + default : + std::wstring message = L"The specified state type " + + std::to_wstring(type) + L" is not valid."; + throw IllegalArgumentException(message); + } + + s->ruleIndex = ruleIndex; + return s; + } + + std::vector ATNDeserializer::supportedUUIDsInitializer() { + std::vector supportedUUIDs; + supportedUUIDs.push_back(BASE_SERIALIZED_UUID); + supportedUUIDs.push_back(ADDED_PRECEDENCE_TRANSITIONS); + + return supportedUUIDs; + } + + ATNDeserializationOptions *ATNDeserializer::deserializationOptionsInitializer(ATNDeserializationOptions *dso) { + if (dso == nullptr) + return ATNDeserializationOptions::getDefaultOptions(); + return dso; + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/ATNDeserializer.h b/runtime/Cpp/org/antlr/v4/runtime/atn/ATNDeserializer.h new file mode 100755 index 000000000..7d81d5473 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/ATNDeserializer.h @@ -0,0 +1,131 @@ +#pragma once + +#include "UUID.h" +#include "Declarations.h" + +#include +#include + + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + /// + /// + /// @author Sam Harwell + /// + class ATNDeserializer { + public: + static const int SERIALIZED_VERSION; + //static ATNDeserializer(); + + /// + /// This is the earliest supported serialized UUID. + /// + private: + static antlrcpp::UUID *const BASE_SERIALIZED_UUID; + /// + /// This UUID indicates an extension of for the + /// addition of precedence predicates. + /// + static antlrcpp::UUID *const ADDED_PRECEDENCE_TRANSITIONS; + /// + /// This list contains all of the currently supported UUIDs, ordered by when + /// the feature first appeared in this branch. + /// + static const std::vector SUPPORTED_UUIDS; + + /// + /// This is the current serialized UUID. + /// + public: + static antlrcpp::UUID *const SERIALIZED_UUID; + + private: + ATNDeserializationOptions *const deserializationOptions; + + public: + ATNDeserializer(); + ATNDeserializer(ATNDeserializationOptions *dso); + + /// + /// Determines if a particular serialized representation of an ATN supports + /// a particular feature, identified by the used for serializing + /// the ATN at the time the feature was first introduced. + /// + /// The marking the first time the feature was + /// supported in the serialized ATN. + /// The of the actual serialized ATN which is + /// currently being deserialized. + /// {@code true} if the {@code actualUuid} value represents a + /// serialized ATN at or after the feature identified by {@code feature} was + /// introduced; otherwise, {@code false}. + protected: + virtual bool isFeatureSupported(antlrcpp::UUID *feature, antlrcpp::UUID *actualUuid); + + public: + virtual ATN *deserialize(const std::wstring& data); + + public: + virtual void verifyATN(ATN *atn); + + virtual void checkCondition(bool condition); + + virtual void checkCondition(bool condition, const std::wstring &message); + + static int toInt(wchar_t c); + + static int toInt32(const std::wstring& data, int offset); + + static long long toLong(const std::wstring& data, int offset); + + static antlrcpp::UUID *toUUID(const std::wstring& data, int offset); + + virtual Transition *edgeFactory(ATN *atn, int type, int src, int trg, int arg1, int arg2, int arg3, std::vector &sets); + + virtual ATNState *stateFactory(int type, int ruleIndex); + + private: + static std::vector supportedUUIDsInitializer(); + ATNDeserializationOptions *deserializationOptionsInitializer(ATNDeserializationOptions *dso); + + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/ATNSerializer.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/ATNSerializer.cpp new file mode 100755 index 000000000..4e01977ed --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/ATNSerializer.cpp @@ -0,0 +1,571 @@ +#include +#include + +#include "ATNSerializer.h" +#include "ATNDeserializer.h" +#include "IntervalSet.h" +#include "ATNState.h" +#include "DecisionState.h" +#include "RuleStartState.h" +#include "LoopEndState.h" +#include "BlockStartState.h" +#include "Transition.h" +#include "SetTransition.h" +#include "ATNType.h" +#include "Token.h" +#include "Interval.h" +#include "RuleTransition.h" +#include "PrecedencePredicateTransition.h" +#include "PredicateTransition.h" +#include "RangeTransition.h" +#include "AtomTransition.h" +#include "ActionTransition.h" +#include "Utils.h" +#include "Exceptions.h" +#include "TokensStartState.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + ATNSerializer::ATNSerializer(ATN *atn) { this->atn = atn; } + + ATNSerializer::ATNSerializer(ATN *atn, const std::vector &tokenNames) { + this->atn = atn; + this->tokenNames = tokenNames; + } + + std::vector* ATNSerializer::serialize() { + std::vector* data = new std::vector(); + data->push_back(ATNDeserializer::SERIALIZED_VERSION); + serializeUUID(data, ATNDeserializer::SERIALIZED_UUID); + + // convert grammar type to ATN const to avoid dependence on ANTLRParser + data->push_back(static_cast(atn->grammarType)); + data->push_back(atn->maxTokenType); + int nedges = 0; + + std::unordered_map *setIndices = + new std::unordered_map(); + std::vector sets = std::vector(); + + // dump states, count edges and collect sets while doing so + std::vector nonGreedyStates; + std::vector precedenceStates; + data->push_back(atn->states.size()); + for (ATNState *s : atn->states) { + if (s == nullptr) { // might be optimized away + data->push_back(ATNState::ATN_INVALID_TYPE); + continue; + } + + int stateType = s->getStateType(); + if (dynamic_cast(s) != nullptr && + (static_cast(s))->nonGreedy) { + nonGreedyStates.push_back(s->stateNumber); + } + + if (dynamic_cast(s) != nullptr && + (static_cast(s))->isPrecedenceRule) { + precedenceStates.push_back(s->stateNumber); + } + + data->push_back(stateType); + + if (s->ruleIndex == -1) { + data->push_back(WCHAR_MAX); + } + else { + data->push_back(s->ruleIndex); + } + + if (s->getStateType() == ATNState::LOOP_END) { + data->push_back((static_cast(s))->loopBackState->stateNumber); + } + else if (dynamic_cast(s) != nullptr) { + data->push_back((static_cast(s))->endState->stateNumber); + } + + if (s->getStateType() != ATNState::RULE_STOP) { + // the deserializer can trivially derive these edges, so there's no need + // to serialize them + nedges += s->getNumberOfTransitions(); + } + + for (int i = 0; i < s->getNumberOfTransitions(); i++) { + Transition *t = s->transition(i); + int edgeType = t->getSerializationType(); + if (edgeType == Transition::SET || edgeType == Transition::NOT_SET) { + SetTransition *st = static_cast(t); + if (setIndices->find(st->set) != setIndices->end()) { + sets.push_back(st->set); + setIndices->insert({ st->set, sets.size() - 1 }); + } + } + } + } + + // non-greedy states + data->push_back(nonGreedyStates.size()); + for (int i = 0; i < (int)nonGreedyStates.size(); i++) { + data->push_back(nonGreedyStates.at(i)); + } + + // precedence states + data->push_back(precedenceStates.size()); + for (int i = 0; i < (int)precedenceStates.size(); i++) { + data->push_back(precedenceStates.at(i)); + } + + size_t nrules = atn->ruleToStartState.size(); + data->push_back(nrules); + for (int r = 0; r < (int)nrules; r++) { + ATNState *ruleStartState = atn->ruleToStartState[r]; + data->push_back(ruleStartState->stateNumber); + if (atn->grammarType == ATNType::LEXER) { + if (atn->ruleToTokenType[r] == Token::_EOF) { + data->push_back(WCHAR_MAX); + } + else { + data->push_back(atn->ruleToTokenType[r]); + } + + if (atn->ruleToActionIndex[r] == -1) { + data->push_back(WCHAR_MAX); + } + else { + data->push_back(atn->ruleToActionIndex[r]); + } + } + } + + size_t nmodes = atn->modeToStartState->size(); + data->push_back(nmodes); + if (nmodes > 0) { + for (const auto& modeStartState : *atn->modeToStartState) { + data->push_back(modeStartState->stateNumber); + } + } + + size_t nsets = sets.size(); + data->push_back(nsets); + for (auto set : sets) { + bool containsEof = set->contains(Token::_EOF); + if (containsEof && set->getIntervals().at(0)->b == Token::_EOF) { + data->push_back(set->getIntervals().size() - 1); + } + else { + data->push_back(set->getIntervals().size()); + } + + data->push_back(containsEof ? 1 : 0); + for (misc::Interval *I : set->getIntervals()) { + if (I->a == Token::_EOF) { + if (I->b == Token::_EOF) { + continue; + } + else { + data->push_back(0); + } + } + else { + data->push_back(I->a); + } + + data->push_back(I->b); + } + } + + data->push_back(nedges); + for (ATNState *s : atn->states) { + if (s == nullptr) { + // might be optimized away + continue; + } + + if (s->getStateType() == ATNState::RULE_STOP) { + continue; + } + + for (int i = 0; i < s->getNumberOfTransitions(); i++) { + Transition *t = s->transition(i); + + if (atn->states[t->target->stateNumber] == nullptr) { + throw IllegalStateException( + L"Cannot serialize a transition to a removed state."); + } + + int src = s->stateNumber; + int trg = t->target->stateNumber; + int edgeType = t->getSerializationType(); + int arg1 = 0; + int arg2 = 0; + int arg3 = 0; + switch (edgeType) { + case Transition::RULE: + trg = (static_cast(t))->followState->stateNumber; + arg1 = (static_cast(t))->target->stateNumber; + arg2 = (static_cast(t))->ruleIndex; + arg3 = (static_cast(t))->precedence; + break; + case Transition::PRECEDENCE: + { + PrecedencePredicateTransition *ppt = + static_cast(t); + arg1 = ppt->precedence; + } + break; + case Transition::PREDICATE: + { + PredicateTransition *pt = static_cast(t); + arg1 = pt->ruleIndex; + arg2 = pt->predIndex; + arg3 = pt->isCtxDependent ? 1 : 0; + } + break; + case Transition::RANGE: + arg1 = (static_cast(t))->from; + arg2 = (static_cast(t))->to; + if (arg1 == Token::_EOF) { + arg1 = 0; + arg3 = 1; + } + + break; + case Transition::ATOM: + arg1 = (static_cast(t))->_label; + if (arg1 == Token::_EOF) { + arg1 = 0; + arg3 = 1; + } + + break; + case Transition::ACTION: + { + ActionTransition *at = static_cast(t); + arg1 = at->ruleIndex; + arg2 = at->actionIndex; + if (arg2 == -1) { + arg2 = 0xFFFF; + } + + arg3 = at->isCtxDependent ? 1 : 0; + } + break; + case Transition::SET: + arg1 = (*setIndices)[(static_cast(t))->set]; + break; + case Transition::NOT_SET: + arg1 = (*setIndices)[(static_cast(t))->set]; + break; + case Transition::WILDCARD: + break; + } + + data->push_back(src); + data->push_back(trg); + data->push_back(edgeType); + data->push_back(arg1); + data->push_back(arg2); + data->push_back(arg3); + } + } + size_t ndecisions = atn->decisionToState.size(); + data->push_back(ndecisions); + for (DecisionState *decStartState : atn->decisionToState) { + data->push_back(decStartState->stateNumber); + } + + // don't adjust the first value since that's the version number + for (int i = 1; i < (int)data->size(); i++) { + if (data->at(i) < WCHAR_MIN || data->at(i) > WCHAR_MAX) { + throw UnsupportedOperationException( + L"Serialized ATN data element out of range."); + } + + int value = (data->at(i) + 2) & 0xFFFF; + data->assign(i, value); + } + + return data; + } + + std::wstring ATNSerializer::decode(const std::wstring& inpdata) { + std::wstring data = inpdata; + // don't adjust the first value since that's the version number + for (int i = 1; i < (int)data.size(); i++) { + data[i] = static_cast(data[i] - 2); + } + + std::wstring buf; + int p = 0; + int version = ATNDeserializer::toInt(data[p++]); + if (version != ATNDeserializer::SERIALIZED_VERSION) { + std::wstring reason = + L"Could not deserialize ATN with version " + + std::to_wstring(version) + + L"(expected " + + std::to_wstring(ATNDeserializer::SERIALIZED_VERSION) + + L")."; + throw UnsupportedOperationException(L"ATN Serializer" + reason); + } + + antlrcpp::UUID *uuid = ATNDeserializer::toUUID(data, p); + p += 8; + if (!uuid->equals(ATNDeserializer::SERIALIZED_UUID)) { + std::wstring reason = + L"Could not deserialize ATN with UUID " + + uuid->toString() + + L" (expected " + + ATNDeserializer::SERIALIZED_UUID->toString() + + L")."; + throw UnsupportedOperationException(L"ATN Serializer" + reason); + } + + p++; // skip grammarType + int maxType = ATNDeserializer::toInt(data[p++]); + buf.append(L"max type ").append(std::to_wstring(maxType)).append(L"\n"); + int nstates = ATNDeserializer::toInt(data[p++]); + for (int i = 0; i < nstates; i++) { + int stype = ATNDeserializer::toInt(data[p++]); + if (stype == ATNState::ATN_INVALID_TYPE) { // ignore bad type of states + continue; + } + int ruleIndex = ATNDeserializer::toInt(data[p++]); + if (ruleIndex == WCHAR_MAX) { + ruleIndex = -1; + } + + std::wstring arg = L""; + if (stype == ATNState::LOOP_END) { + int loopBackStateNumber = ATNDeserializer::toInt(data[p++]); + arg = std::wstring(L" ") + std::to_wstring(loopBackStateNumber); + } + else if (stype == ATNState::PLUS_BLOCK_START || + stype == ATNState::STAR_BLOCK_START || + stype == ATNState::BLOCK_START) { + int endStateNumber = ATNDeserializer::toInt(data[p++]); + arg = std::wstring(L" ") + std::to_wstring(endStateNumber); + } + buf.append(std::to_wstring(i)) + .append(L":") + .append(ATNState::serializationNames[stype]) + .append(L" ") + .append(std::to_wstring(ruleIndex)) + .append(arg) + .append(L"\n"); + } + int numNonGreedyStates = ATNDeserializer::toInt(data[p++]); + for (int i = 0; i < numNonGreedyStates; i++) { + //int stateNumber = ATNDeserializer::toInt(data[p++]); // Unused? + } + int numPrecedenceStates = ATNDeserializer::toInt(data[p++]); + for (int i = 0; i < numPrecedenceStates; i++) { + //int stateNumber = ATNDeserializer::toInt(data[p++]); // Unused? + } + int nrules = ATNDeserializer::toInt(data[p++]); + for (int i = 0; i < nrules; i++) { + int s = ATNDeserializer::toInt(data[p++]); + if (atn->grammarType == ATNType::LEXER) { + int arg1 = ATNDeserializer::toInt(data[p++]); + int arg2 = ATNDeserializer::toInt(data[p++]); + if (arg2 == WCHAR_MAX) { + arg2 = -1; + } + buf.append(L"rule ") + .append(std::to_wstring(i)) + .append(L":") + .append(std::to_wstring(s)) + .append(L" ") + .append(std::to_wstring(arg1)) + .append(L",") + .append(std::to_wstring(arg2)) + .append(L"\n"); + } + else { + buf.append(L"rule ") + .append(std::to_wstring(i)) + .append(L":") + .append(std::to_wstring(s)) + .append(L"\n"); + } + } + int nmodes = ATNDeserializer::toInt(data[p++]); + for (int i = 0; i < nmodes; i++) { + int s = ATNDeserializer::toInt(data[p++]); + buf.append(L"mode ") + .append(std::to_wstring(i)) + .append(L":") + .append(std::to_wstring(s)) + .append(L"\n"); + } + int nsets = ATNDeserializer::toInt(data[p++]); + for (int i = 0; i < nsets; i++) { + int nintervals = ATNDeserializer::toInt(data[p++]); + buf.append(std::to_wstring(i)).append(L":"); + bool containsEof = data[p++] != 0; + if (containsEof) { + buf.append(getTokenName(Token::_EOF)); + } + + for (int j = 0; j < nintervals; j++) { + if (containsEof || j > 0) { + buf.append(L", "); + } + + buf.append(getTokenName(ATNDeserializer::toInt(data[p]))) + .append(L"..") + .append(getTokenName(ATNDeserializer::toInt(data[p + 1]))); + p += 2; + } + buf.append(L"\n"); + } + int nedges = ATNDeserializer::toInt(data[p++]); + for (int i = 0; i < nedges; i++) { + int src = ATNDeserializer::toInt(data[p]); + int trg = ATNDeserializer::toInt(data[p + 1]); + int ttype = ATNDeserializer::toInt(data[p + 2]); + int arg1 = ATNDeserializer::toInt(data[p + 3]); + int arg2 = ATNDeserializer::toInt(data[p + 4]); + int arg3 = ATNDeserializer::toInt(data[p + 5]); + buf.append(std::to_wstring(src)) + .append(L"->") + .append(std::to_wstring(trg)) + .append(L" ") + .append(Transition::serializationNames[ttype]) + .append(L" ") + .append(std::to_wstring(arg1)) + .append(L",") + .append(std::to_wstring(arg2)) + .append(L",") + .append(std::to_wstring(arg3)) + .append(L"\n"); + p += 6; + } + int ndecisions = ATNDeserializer::toInt(data[p++]); + for (int i = 0; i < ndecisions; i++) { + int s = ATNDeserializer::toInt(data[p++]); + buf.append(std::to_wstring(i)).append(L":").append(std::to_wstring(s)).append(L"\n"); + } + return buf; + } + + std::wstring ATNSerializer::getTokenName(int t) { + if (t == -1) { + return L"EOF"; + } + + if (atn->grammarType == ATNType::LEXER && t >= WCHAR_MIN && + t <= WCHAR_MAX) { + switch (t) { + case L'\n': + return L"'\\n'"; + case L'\r': + return L"'\\r'"; + case L'\t': + return L"'\\t'"; + case L'\b': + return L"'\\b'"; + case L'\f': + return L"'\\f'"; + case L'\\': + return L"'\\\\'"; + case L'\'': + return L"'\\''"; + default: + std::wstring s_hex = antlrcpp::toHexString(t); + if (s_hex >= L"0" && s_hex <= L"7F" && + !iscntrl(t)) { + return L"'" + std::to_wstring(t) + L"'"; + } + // turn on the bit above max "\uFFFF" value so that we pad with zeros + // then only take last 4 digits + std::wstring hex = antlrcpp::toHexString(t | 0x10000).substr(1, 4); + std::wstring unicodeStr = std::wstring(L"'\\u") + hex + std::wstring(L"'"); + return unicodeStr; + } + } + + if (tokenNames.size() > 0 && t >= 0 && t < (int)tokenNames.size()) { + return tokenNames[t]; + } + + return antlrcpp::StringConverterHelper::toString(t); + } + + std::wstring ATNSerializer::getSerializedAsString(ATN *atn) { + return std::wstring(getSerializedAsChars(atn)); + } + + std::vector *ATNSerializer::getSerialized(ATN *atn) { + return (new ATNSerializer(atn))->serialize(); + } + + wchar_t *ATNSerializer::getSerializedAsChars(ATN *atn) { + return antlrcpp::toCharArray(getSerialized(atn)); + } + + std::wstring ATNSerializer::getDecoded(ATN *atn, + std::vector &tokenNames) { + std::vector *serialized = getSerialized(atn); + // JAVA TO C++ CONVERTER WARNING: Since the array size is not known in this + // declaration, Java to C++ Converter has converted this array to a pointer. + // You will need to call 'delete[]' where appropriate: + // ORIGINAL LINE: char[] data = + // org.antlr.v4.runtime.misc.Utils.toCharArray(serialized); + wchar_t *data = antlrcpp::toCharArray(serialized); + return (new ATNSerializer(atn, tokenNames))->decode(data); + } + + void ATNSerializer::serializeUUID(std::vector *data, antlrcpp::UUID *uuid) { + serializeLong(data, uuid->getLeastSignificantBits()); + serializeLong(data, uuid->getMostSignificantBits()); + } + + void ATNSerializer::serializeLong(std::vector *data, long long value) { + serializeInt(data, static_cast(value)); + serializeInt(data, static_cast(value >> 32)); + } + + void ATNSerializer::serializeInt(std::vector *data, int value) { + data->push_back(static_cast(value)); + data->push_back(static_cast(value >> 16)); + } + + } // namespace atn + } // namespace runtime + } // namespace v4 + } // namespace antlr +} // namespace org diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/ATNSerializer.h b/runtime/Cpp/org/antlr/v4/runtime/atn/ATNSerializer.h new file mode 100755 index 000000000..17ac0180d --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/ATNSerializer.h @@ -0,0 +1,114 @@ +#pragma once + +#include +#include +#include +#include + +#include "ATN.h" +#include "stringconverter.h" +#include "UUID.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + class ATNSerializer { + public: + ATN *atn; + + private: + std::vector tokenNames; + + public: + ATNSerializer(ATN *atn); + + ATNSerializer(ATN *atn, const std::vector &tokenNames); + + /// + /// Serialize state descriptors, edge descriptors, and decision->state map + /// into list of ints: + /// + /// grammar-type, (ANTLRParser.LEXER, ...) + /// max token type, + /// num states, + /// state-0-type ruleIndex, state-1-type ruleIndex, ... state-i-type + /// ruleIndex optional-arg ... + /// num rules, + /// rule-1-start-state rule-1-args, rule-2-start-state rule-2-args, ... + /// (args are token type,actionIndex in lexer else 0,0) + /// num modes, + /// mode-0-start-state, mode-1-start-state, ... (parser has 0 modes) + /// num sets + /// set-0-interval-count intervals, set-1-interval-count intervals, ... + /// num total edges, + /// src, trg, edge-type, edge arg1, optional edge arg2 (present always), + /// ... + /// num decisions, + /// decision-0-start-state, decision-1-start-state, ... + /// + /// Convenient to pack into unsigned shorts to make as Java string. + /// + virtual std::vector *serialize(); + + virtual std::wstring decode(const std::wstring& data); + + virtual std::wstring getTokenName(int t); + + /// + /// Used by Java target to encode short/int array as chars in string. + /// + static std::wstring getSerializedAsString(ATN *atn); + + static std::vector *getSerialized(ATN *atn); + + static wchar_t *getSerializedAsChars(ATN *atn); + + static std::wstring getDecoded(ATN *atn, + std::vector &tokenNames); + + private: + void serializeUUID(std::vector *data, antlrcpp::UUID *uuid); + + void serializeLong(std::vector *data, long long value); + + void serializeInt(std::vector *data, int value); + }; + + } // namespace org + } // namespace antlr + } // namespace v4 + } // namespace runtime +} // namespace atn diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/ATNSimulator.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/ATNSimulator.cpp new file mode 100755 index 000000000..3537b518d --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/ATNSimulator.cpp @@ -0,0 +1,118 @@ +#include + +#include "ATNSimulator.h" +#include "ATNDeserializer.h" +#include "ATNConfigSet.h" +#include "DFAState.h" +#include "limits.h" +#include "ATNType.h" +#include "PredictionContextCache.h" +#include "PredictionContext.h" +#include "ATN.h" + + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + dfa::DFAState * ATNSimulator::ERROR = new dfa::DFAState(); + + ATNSimulator::ATNSimulator() { + ERROR = new dfa::DFAState(new ATNConfigSet()); + ERROR->stateNumber = INT32_MAX; + sharedContextCache = new PredictionContextCache(); + + atn = new ATN(ATNType::LEXER, 0); + } + + ATNSimulator::ATNSimulator(ATN *atn, PredictionContextCache *sharedContextCache) : atn(atn), sharedContextCache(sharedContextCache) { + } + + atn::PredictionContextCache *ATNSimulator::getSharedContextCache() { + return sharedContextCache; + } + + atn::PredictionContext *ATNSimulator::getCachedContext(PredictionContext *context) { + if (sharedContextCache == nullptr) { + return context; + } + + { + std::lock_guard lck(mtx); + std::map *visited = new std::map(); + return PredictionContext::getCachedContext(context, sharedContextCache, visited); + } + } + + atn::ATN *ATNSimulator::deserialize(wchar_t data[]) { + return (new ATNDeserializer())->deserialize(data); + } + + void ATNSimulator::checkCondition(bool condition) { + (new ATNDeserializer())->checkCondition(condition); + } + + void ATNSimulator::checkCondition(bool condition, const std::wstring &message) { + (new ATNDeserializer())->checkCondition(condition, message); + } + + int ATNSimulator::toInt(wchar_t c) { + return ATNDeserializer::toInt(c); + } + + int ATNSimulator::toInt32(wchar_t data[], int offset) { + return ATNDeserializer::toInt32(data, offset); + } + + long long ATNSimulator::toLong(wchar_t data[], int offset) { + return ATNDeserializer::toLong(data, offset); + } + + antlrcpp::UUID *ATNSimulator::toUUID(wchar_t data[], int offset) { + return ATNDeserializer::toUUID(data, offset); + } + + atn::Transition *ATNSimulator::edgeFactory(ATN *atn, int type, int src, int trg, int arg1, int arg2, int arg3, std::vector &sets) { + return (new ATNDeserializer())->edgeFactory(atn, type, src, trg, arg1, arg2, arg3, sets); + } + + atn::ATNState *ATNSimulator::stateFactory(int type, int ruleIndex) { + return (new ATNDeserializer())->stateFactory(type, ruleIndex); + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/ATNSimulator.h b/runtime/Cpp/org/antlr/v4/runtime/atn/ATNSimulator.h new file mode 100755 index 000000000..7b9149925 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/ATNSimulator.h @@ -0,0 +1,141 @@ +#pragma once +#include +#include +#include + +#include "Declarations.h" +#include "ATNDeserializer.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + class ATNSimulator { + public: + + //Mutex to manage synchronized access for multithreading + std::mutex mtx; + ATNSimulator(); + + /// + /// This is the current serialized UUID. + /// @deprecated Use instead. +#ifdef TODO + constexpr static UUID *const SERIALIZED_UUID = ATNDeserializer::SERIALIZED_UUID; +#endif + /// + /// Must distinguish between missing edge and edge we know leads nowhere + static dfa::DFAState * ERROR; + ATN * atn; + + /// + /// The context cache maps all PredictionContext objects that are equals() + /// to a single cached copy. This cache is shared across all contexts + /// in all ATNConfigs in all DFA states. We rebuild each ATNConfigSet + /// to use only cached nodes/graphs in addDFAState(). We don't want to + /// fill this during closure() since there are lots of contexts that + /// pop up but are not used ever again. It also greatly slows down closure(). + ///

+ /// This cache makes a huge difference in memory and a little bit in speed. + /// For the Java grammar on java.*, it dropped the memory requirements + /// at the end from 25M to 16M. We don't store any of the full context + /// graphs in the DFA because they are limited to local context only, + /// but apparently there's a lot of repetition there as well. We optimize + /// the config contexts before storing the config set in the DFA states + /// by literally rebuilding them with cached subgraphs only. + ///

+ /// I tried a cache for use during closure operations, that was + /// whacked after each adaptivePredict(). It cost a little bit + /// more time I think and doesn't save on the overall footprint + /// so it's not worth the complexity. + ///

+ protected: + PredictionContextCache * sharedContextCache; + + + public: + ATNSimulator(ATN *atn, PredictionContextCache *sharedContextCache); + + virtual void reset() = 0; + + virtual PredictionContextCache *getSharedContextCache(); + + virtual PredictionContext *getCachedContext(PredictionContext *context); + + /// @deprecated Use instead. + static ATN *deserialize(wchar_t data[]); + + /// @deprecated Use instead. + static void checkCondition(bool condition); + + /// @deprecated Use instead. + static void checkCondition(bool condition, const std::wstring &message); + + /// @deprecated Use instead. + static int toInt(wchar_t c); + + /// @deprecated Use instead. + static int toInt32(wchar_t data[], int offset); + + /// @deprecated Use instead. + static long long toLong(wchar_t data[], int offset); + + + /// @deprecated Use instead. + static antlrcpp::UUID *toUUID(wchar_t data[], int offset); + + /// @deprecated Use instead. + static Transition *edgeFactory(ATN *atn, int type, int src, int trg, int arg1, int arg2, int arg3, std::vector &sets); + + /// @deprecated Use instead. + static ATNState *stateFactory(int type, int ruleIndex); + + /* + public static void dump(DFA dfa, Grammar g) { + DOTGenerator dot = new DOTGenerator(g); + String output = dot.getDOT(dfa, false); + System.out.println(output); + } + + public static void dump(DFA dfa) { + dump(dfa, null); + } + */ + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/ATNState.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/ATNState.cpp new file mode 100755 index 000000000..71da80a0f --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/ATNState.cpp @@ -0,0 +1,128 @@ +#include "ATNState.h" +#include "ATN.h" +#include "Transition.h" +#include "IntervalSet.h" +#include + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + ATNState::~ATNState() {}; + + const int ATNState::INITIAL_NUM_TRANSITIONS; + + + const wchar_t * ATNState::serializationNames[] = {L"INVALID", L"BASIC", L"RULE_START", L"BLOCK_START", + L"PLUS_BLOCK_START", L"STAR_BLOCK_START", L"TOKEN_START", L"RULE_STOP", + L"BLOCK_END", L"STAR_LOOP_BACK", L"STAR_LOOP_ENTRY", L"PLUS_LOOP_BACK", L"LOOP_END"}; + + int ATNState::hashCode() { + return stateNumber; + } + + bool ATNState::equals(void *o) { + // are these states same object? + if (o != nullptr) { + return stateNumber == (static_cast(o))->stateNumber; + } + return false; + } + + bool ATNState::isNonGreedyExitState() { + return false; + } + + std::wstring ATNState::toString() const { + return antlrcpp::StringConverterHelper::toString(stateNumber); + } + + std::vector ATNState::getTransitions() { + std::vector arr(transitions); + + return arr; + } + + int ATNState::getNumberOfTransitions() { + return (int)transitions.size(); + } + + void ATNState::addTransition(Transition *e) { + addTransition((int)transitions.size(), e); + } + + void ATNState::addTransition(int index, Transition *e) { + if (transitions.empty()) { + epsilonOnlyTransitions = e->isEpsilon(); + } else if (epsilonOnlyTransitions != e->isEpsilon()) { + std::cerr << L"ATN state %d has both epsilon and non-epsilon transitions.\n" << stateNumber; + epsilonOnlyTransitions = false; + } + + transitions[index] = e; + } + + atn::Transition *ATNState::transition(int i) { + return transitions[i]; + } + + void ATNState::setTransition(int i, Transition *e) { + transitions[i] = e; + } + + atn::Transition *ATNState::removeTransition(int index) { + transitions.erase(transitions.begin() + index); + return nullptr; + } + + bool ATNState::onlyHasEpsilonTransitions() { + return epsilonOnlyTransitions; + } + + void ATNState::setRuleIndex(int ruleIndex) { + this->ruleIndex = ruleIndex; + } + + void ATNState::InitializeInstanceFields() { + atn = 0; + stateNumber = INVALID_STATE_NUMBER; + ruleIndex = 0; + epsilonOnlyTransitions = false; + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/ATNState.h b/runtime/Cpp/org/antlr/v4/runtime/atn/ATNState.h new file mode 100755 index 000000000..901df6800 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/ATNState.h @@ -0,0 +1,196 @@ +#pragma once + +#include "Declarations.h" +#include "stringconverter.h" + +#include +#include +#include + + + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + + /// + /// The following images show the relation of states and + /// for various grammar constructs. + /// + ///
    + /// + ///
  • Solid edges marked with an ε indicate a required + /// .
  • + /// + ///
  • Dashed edges indicate locations where any transition derived from + /// might appear.
  • + /// + ///
  • Dashed nodes are place holders for either a sequence of linked + /// states or the inclusion of a block representing a nested + /// construct in one of the forms below.
  • + /// + ///
  • Nodes showing multiple outgoing alternatives with a {@code ...} support + /// any number of alternatives (one or more). Nodes without the {@code ...} only + /// support the exact number of alternatives shown in the diagram.
  • + /// + ///
+ /// + ///

Basic Blocks

+ /// + ///

Rule

+ /// + /// + /// + ///

Block of 1 or more alternatives

+ /// + /// + /// + ///

Greedy Loops

+ /// + ///

Greedy Closure: {@code (...)*}

+ /// + /// + /// + ///

Greedy Positive Closure: {@code (...)+}

+ /// + /// + /// + ///

Greedy Optional: {@code (...)?}

+ /// + /// + /// + ///

Non-Greedy Loops

+ /// + ///

Non-Greedy Closure: {@code (...)*?}

+ /// + /// + /// + ///

Non-Greedy Positive Closure: {@code (...)+?}

+ /// + /// + /// + ///

Non-Greedy Optional: {@code (...)??}

+ /// + /// + ///
+ class ATNState { + public: + virtual ~ATNState(); + + static const int INITIAL_NUM_TRANSITIONS = 4; + + enum { + ATN_INVALID_TYPE = 0, + BASIC = 1, + RULE_START = 2, + BLOCK_START = 3, + PLUS_BLOCK_START = 4, + STAR_BLOCK_START = 5, + TOKEN_START = 6, + RULE_STOP = 7, + BLOCK_END = 8, + STAR_LOOP_BACK = 9, + STAR_LOOP_ENTRY = 10, + PLUS_LOOP_BACK = 11, + LOOP_END = 12 + }; + + static const wchar_t * serializationNames[]; + + static const int INVALID_STATE_NUMBER = -1; + + /// + /// Which ATN are we in? + ATN *atn = nullptr; + + int stateNumber; + + int ruleIndex; // at runtime, we don't have Rule objects + + bool epsilonOnlyTransitions; + + /// + /// Track the transitions emanating from this ATN state. + protected: + std::vector transitions; + + /// + /// Used to cache lookahead during parsing, not used during construction + public: + misc::IntervalSet *nextTokenWithinRule; + + virtual int hashCode(); + virtual bool equals(void *o); + + virtual bool isNonGreedyExitState(); + + virtual std::wstring toString() const; + + virtual std::vector getTransitions(); + + virtual int getNumberOfTransitions(); + + virtual void addTransition(Transition *e); + + virtual void addTransition(int index, Transition *e); + + virtual Transition *transition(int i); + + virtual void setTransition(int i, Transition *e); + + virtual Transition *removeTransition(int index); + + virtual int getStateType() = 0; + + bool onlyHasEpsilonTransitions(); + + virtual void setRuleIndex(int ruleIndex); + + + private: + void InitializeInstanceFields(); + +public: +// ATNState() : transitions(new std::list()) { +// InitializeInstanceFields(); +// } + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/ATNType.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/ATNType.cpp new file mode 100755 index 000000000..b3f65d21e --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/ATNType.cpp @@ -0,0 +1,42 @@ +#include "ATNType.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/ATNType.h b/runtime/Cpp/org/antlr/v4/runtime/atn/ATNType.h new file mode 100755 index 000000000..900962ac3 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/ATNType.h @@ -0,0 +1,62 @@ +#pragma once + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Sam Harwell + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + /// + /// Represents the type of recognizer an ATN applies to. + /// + /// @author Sam Harwell + /// + enum class ATNType { + + /// + /// A lexer grammar. + /// + LEXER, + + /// + /// A parser grammar. + /// + PARSER, + + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/AbstractPredicateTransition.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/AbstractPredicateTransition.cpp new file mode 100755 index 000000000..1cd3bbdb8 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/AbstractPredicateTransition.cpp @@ -0,0 +1,45 @@ +#include "AbstractPredicateTransition.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + AbstractPredicateTransition::AbstractPredicateTransition(ATNState *target) : Transition(target) { + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/AbstractPredicateTransition.h b/runtime/Cpp/org/antlr/v4/runtime/atn/AbstractPredicateTransition.h new file mode 100755 index 000000000..5d205d8de --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/AbstractPredicateTransition.h @@ -0,0 +1,57 @@ +#pragma once + +#include "Transition.h" +#include "ATNState.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + /// + /// + /// @author Sam Harwell + /// + class AbstractPredicateTransition : public Transition { + + public: + AbstractPredicateTransition(ATNState *target); + + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/ActionTransition.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/ActionTransition.cpp new file mode 100755 index 000000000..b604ecef0 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/ActionTransition.cpp @@ -0,0 +1,64 @@ +#include "ActionTransition.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + ActionTransition::ActionTransition(ATNState *target, int ruleIndex) : Transition(target), ruleIndex(ruleIndex), actionIndex(0), isCtxDependent(false) { + } + + ActionTransition::ActionTransition(ATNState *target, int ruleIndex, int actionIndex, bool isCtxDependent) : Transition(target), ruleIndex(ruleIndex), actionIndex(actionIndex), isCtxDependent(isCtxDependent) { + } + + int ActionTransition::getSerializationType() { + return ACTION; + } + + bool ActionTransition::isEpsilon() { + return true; // we are to be ignored by analysis 'cept for predicates + } + + bool ActionTransition::matches(int symbol, int minVocabSymbol, int maxVocabSymbol) { + return false; + } + + std::wstring ActionTransition::toString() { + return std::wstring(L"action_") + std::to_wstring(ruleIndex) + std::wstring(L":") + std::to_wstring(actionIndex); + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/ActionTransition.h b/runtime/Cpp/org/antlr/v4/runtime/atn/ActionTransition.h new file mode 100755 index 000000000..55cdb4758 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/ActionTransition.h @@ -0,0 +1,69 @@ +#pragma once + +#include "Transition.h" +#include "Declarations.h" + +#include + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + + class ActionTransition final : public Transition { + public: + const int ruleIndex; + const int actionIndex; + const bool isCtxDependent; // e.g., $i ref in action + + + ActionTransition(ATNState *target, int ruleIndex); //this(target, ruleIndex, -1, false); + + ActionTransition(ATNState *target, int ruleIndex, int actionIndex, bool isCtxDependent); + + virtual int getSerializationType() override; + + virtual bool isEpsilon() override; + + virtual bool matches(int symbol, int minVocabSymbol, int maxVocabSymbol) override; + + virtual std::wstring toString(); + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/ArrayPredictionContext.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/ArrayPredictionContext.cpp new file mode 100755 index 000000000..03596db47 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/ArrayPredictionContext.cpp @@ -0,0 +1,124 @@ +#include + +#include "ArrayPredictionContext.h" +#include "StringBuilder.h" +#include "Arrays.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { +#ifdef TODO + //the base class hash code is gettings set to 0 here, what do we want? + stopffds +#endif + ArrayPredictionContext::ArrayPredictionContext(SingletonPredictionContext *a) : PredictionContext(0) { + } + + ArrayPredictionContext::ArrayPredictionContext(std::PredictionContext *parents, int returnStates[]) : PredictionContext(calculateHashCode(parents, *returnStates))/*, parents(parents)*/, returnStates(*returnStates) { +#ifdef TODO +// assert(parents != nullptr && sizeof(parents) / sizeof(parents[0]) > 0); +// assert(returnStates != nullptr && sizeof(returnStates) / sizeof(returnStates[0]) > 0); + // Setup the parents variable correctly since we're not setting it in the constructor line above + // std::vector +#endif + // System.err.println("CREATE ARRAY: "+Arrays.toString(parents)+", "+Arrays.toString(returnStates)); + } + ArrayPredictionContext::ArrayPredictionContext(std::vectorparents, + const std::vector returnStates) : PredictionContext(0) { + throw new TODOException(L"ArrayPredictionContext::ArrayPredictionContext"); + } + bool ArrayPredictionContext::isEmpty() { + // since EMPTY_RETURN_STATE can only appear in the last position, we + // don't need to verify that size==1 + return returnStates[0] == EMPTY_RETURN_STATE; + } + + int ArrayPredictionContext::size() { + return (int)returnStates.size(); + } + + atn::PredictionContext *ArrayPredictionContext::getParent(int index) { + return parents->at(index); + } + + int ArrayPredictionContext::getReturnState(int index) { + return returnStates[index]; + } + + bool ArrayPredictionContext::equals(void *o) { + if (this == o) { + return true; + } else if (!((ArrayPredictionContext*)o/*dynamic_cast(o)*/ != nullptr)) { + return false; + } + + if (this->hashCode() != ((ArrayPredictionContext*)o)->hashCode()) { + return false; // can't be same if hash is different + } + + ArrayPredictionContext *a = static_cast(o); + return antlrcpp::Arrays::equals(returnStates, a->returnStates) && antlrcpp::Arrays::equals(&parents, &a->parents); + } + + std::wstring ArrayPredictionContext::toString() { + if (isEmpty()) { + return L"[]"; + } + antlrcpp::StringBuilder *buf = new antlrcpp::StringBuilder(); + buf->append(L"["); + for (std::vector::size_type i = 0; i < returnStates.size(); i++) { + if (i > 0) { + buf->append(L", "); + } + if (returnStates[i] == EMPTY_RETURN_STATE) { + buf->append(L"$"); + continue; + } + buf->append(std::to_wstring(returnStates.at(i))); + if (parents->at(i) != nullptr) { + buf->append(L" "); + buf->append(parents->at(i)->toString()); + } else { + buf->append(L"null"); + } + } + buf->append(L"]"); + return buf->toString(); + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/ArrayPredictionContext.h b/runtime/Cpp/org/antlr/v4/runtime/atn/ArrayPredictionContext.h new file mode 100755 index 000000000..e10b0d8df --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/ArrayPredictionContext.h @@ -0,0 +1,88 @@ +#pragma once + +#include "PredictionContext.h" +#include "SingletonPredictionContext.h" +#include +#include + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + + class ArrayPredictionContext : public PredictionContext { + /// + /// Parent can be null only if full ctx mode and we make an array + /// from and non-empty. We merge by using null parent and + /// returnState == . + /// + public: + const std::vector *parents; + + /// + /// Sorted for merge, no duplicates; if present, + /// is always last. + /// + const std::vector returnStates; + + ArrayPredictionContext(SingletonPredictionContext *a); //this(new PredictionContext[] {a.parent}, new int[] {a.returnState}); + + ArrayPredictionContext(PredictionContext *parents, int returnStates[]); + ArrayPredictionContext(std::vectorparents, + const std::vector returnStates); + + virtual bool isEmpty() override; + + virtual int size() override; + + virtual PredictionContext *getParent(int index) override; + + virtual int getReturnState(int index) override; + + // @Override + // public int findReturnState(int returnState) { + // return Arrays.binarySearch(returnStates, returnState); + // } + + virtual bool equals(void *o) override; + + virtual std::wstring toString(); + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/AtomTransition.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/AtomTransition.cpp new file mode 100755 index 000000000..bcbc850f4 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/AtomTransition.cpp @@ -0,0 +1,63 @@ +#include "AtomTransition.h" +#include "IntervalSet.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + + AtomTransition::AtomTransition(ATNState *target, int label) : Transition(target), _label(label) { + } + + int AtomTransition::getSerializationType() { + return ATOM; + } + + misc::IntervalSet *AtomTransition::label() { + return misc::IntervalSet::of(_label); + } + + bool AtomTransition::matches(int symbol, int minVocabSymbol, int maxVocabSymbol) { + return _label == symbol; + } + + std::wstring AtomTransition::toString() { + return antlrcpp::StringConverterHelper::toString(_label); + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/AtomTransition.h b/runtime/Cpp/org/antlr/v4/runtime/atn/AtomTransition.h new file mode 100755 index 000000000..4399ea6fd --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/AtomTransition.h @@ -0,0 +1,66 @@ +#pragma once + + +#include +#include "Declarations.h" +#include "Transition.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + /// + /// TODO: make all transitions sets? no, should remove set edges + class AtomTransition final : public Transition { + /// + /// The token type or character value; or, signifies special label. + public: + const int _label; + + AtomTransition(ATNState *target, int label); + + virtual int getSerializationType() override; + + virtual misc::IntervalSet *label() override; + virtual bool matches(int symbol, int minVocabSymbol, int maxVocabSymbol) override; + + virtual std::wstring toString(); + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/BasicBlockStartState.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/BasicBlockStartState.cpp new file mode 100755 index 000000000..bca8ca337 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/BasicBlockStartState.cpp @@ -0,0 +1,46 @@ +#include "BasicBlockStartState.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + int BasicBlockStartState::getStateType() { + return BLOCK_START; + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/BasicBlockStartState.h b/runtime/Cpp/org/antlr/v4/runtime/atn/BasicBlockStartState.h new file mode 100755 index 000000000..7952e6103 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/BasicBlockStartState.h @@ -0,0 +1,56 @@ +#pragma once + +#include "BlockStartState.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + /// + /// + /// @author Sam Harwell + /// + class BasicBlockStartState final : public BlockStartState { + + public: + virtual int getStateType() override; + + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/BasicState.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/BasicState.cpp new file mode 100755 index 000000000..532714c5b --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/BasicState.cpp @@ -0,0 +1,46 @@ +#include "BasicState.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + int BasicState::getStateType() { + return BASIC; + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/BasicState.h b/runtime/Cpp/org/antlr/v4/runtime/atn/BasicState.h new file mode 100755 index 000000000..59de23256 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/BasicState.h @@ -0,0 +1,56 @@ +#pragma once + +#include "ATNState.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + /// + /// + /// @author Sam Harwell + /// + class BasicState final : public ATNState { + + public: + virtual int getStateType() override; + + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/BlockEndState.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/BlockEndState.cpp new file mode 100755 index 000000000..63461a9a5 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/BlockEndState.cpp @@ -0,0 +1,46 @@ +#include "BlockEndState.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + int BlockEndState::getStateType() { + return BLOCK_END; + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/BlockEndState.h b/runtime/Cpp/org/antlr/v4/runtime/atn/BlockEndState.h new file mode 100755 index 000000000..cbf967e20 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/BlockEndState.h @@ -0,0 +1,55 @@ +#pragma once + +#include "ATNState.h" +#include "BlockStartState.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + /// + /// Terminal node of a simple {@code (a|b|c)} block. + class BlockEndState final : public ATNState { + public: + BlockStartState *startState; + + virtual int getStateType() override; + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/BlockStartState.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/BlockStartState.cpp new file mode 100755 index 000000000..078c2475e --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/BlockStartState.cpp @@ -0,0 +1,42 @@ +#include "BlockStartState.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/BlockStartState.h b/runtime/Cpp/org/antlr/v4/runtime/atn/BlockStartState.h new file mode 100755 index 000000000..62ebefef2 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/BlockStartState.h @@ -0,0 +1,53 @@ +#pragma once + +#include "DecisionState.h" +#include "BlockEndState.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + /// + /// The start of a regular {@code (...)} block. + class BlockStartState : public DecisionState { + public: + BlockEndState *endState; + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/DecisionState.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/DecisionState.cpp new file mode 100755 index 000000000..047f4b609 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/DecisionState.cpp @@ -0,0 +1,47 @@ +#include "DecisionState.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + void DecisionState::InitializeInstanceFields() { + decision = -1; + nonGreedy = false; + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/DecisionState.h b/runtime/Cpp/org/antlr/v4/runtime/atn/DecisionState.h new file mode 100755 index 000000000..44487a70b --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/DecisionState.h @@ -0,0 +1,59 @@ +#pragma once + +#include "ATNState.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + class DecisionState : public ATNState { + public: + int decision; + bool nonGreedy; + + private: + void InitializeInstanceFields(); + + public: + DecisionState() { + InitializeInstanceFields(); + } + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/EmptyPredictionContext.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/EmptyPredictionContext.cpp new file mode 100755 index 000000000..5741323ea --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/EmptyPredictionContext.cpp @@ -0,0 +1,69 @@ +#include "EmptyPredictionContext.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + EmptyPredictionContext::EmptyPredictionContext() : SingletonPredictionContext(nullptr, EMPTY_RETURN_STATE) { + } + + bool EmptyPredictionContext::isEmpty() { + return true; + } + + int EmptyPredictionContext::size() { + return 1; + } + + org::antlr::v4::runtime::atn::PredictionContext *EmptyPredictionContext::getParent(int index) { + return nullptr; + } + + int EmptyPredictionContext::getReturnState(int index) { + return returnState; + } + + bool EmptyPredictionContext::equals(void *o) { + return this == o; + } + + std::wstring EmptyPredictionContext::toString() { + return L"$"; + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/EmptyPredictionContext.h b/runtime/Cpp/org/antlr/v4/runtime/atn/EmptyPredictionContext.h new file mode 100755 index 000000000..8d097555e --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/EmptyPredictionContext.h @@ -0,0 +1,62 @@ +#pragma once + +#include "SingletonPredictionContext.h" +#include + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + class EmptyPredictionContext : public SingletonPredictionContext { + public: + EmptyPredictionContext(); + + virtual bool isEmpty() override; + virtual int size() override; + + virtual PredictionContext *getParent(int index) override; + + virtual int getReturnState(int index) override; + + virtual bool equals(void *o) override; + + virtual std::wstring toString() override; + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/EpsilonTransition.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/EpsilonTransition.cpp new file mode 100755 index 000000000..4fb2df7fe --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/EpsilonTransition.cpp @@ -0,0 +1,61 @@ +#include "EpsilonTransition.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + EpsilonTransition::EpsilonTransition(ATNState *target) : Transition(target) { + } + + int EpsilonTransition::getSerializationType() { + return EPSILON; + } + + bool EpsilonTransition::isEpsilon() { + return true; + } + + bool EpsilonTransition::matches(int symbol, int minVocabSymbol, int maxVocabSymbol) { + return false; + } + + std::wstring EpsilonTransition::toString() { + return L"epsilon"; + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/EpsilonTransition.h b/runtime/Cpp/org/antlr/v4/runtime/atn/EpsilonTransition.h new file mode 100755 index 000000000..a27e4e357 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/EpsilonTransition.h @@ -0,0 +1,61 @@ +#pragma once + +#include "Transition.h" +#include "ATNState.h" +#include + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + using org::antlr::v4::runtime::misc::NotNull; + + class EpsilonTransition final : public Transition { + public: + EpsilonTransition(ATNState *target); + + virtual int getSerializationType() override; + + virtual bool isEpsilon() override; + virtual bool matches(int symbol, int minVocabSymbol, int maxVocabSymbol) override; + + virtual std::wstring toString(); + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/LL1Analyzer.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/LL1Analyzer.cpp new file mode 100755 index 000000000..ac8263c65 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/LL1Analyzer.cpp @@ -0,0 +1,185 @@ +#include "LL1Analyzer.h" +#include "RuleStopState.h" +#include "Transition.h" +#include "RuleTransition.h" +#include "SingletonPredictionContext.h" +#include "AbstractPredicateTransition.h" +#include "WildcardTransition.h" +#include "NotSetTransition.h" +#include "IntervalSet.h" +#include "ATNConfig.h" +#include "ATN.h" + +#include +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + LL1Analyzer::LL1Analyzer(ATN *atn) : atn(atn) { + } + + std::vector LL1Analyzer::getDecisionLookahead(ATNState *s) { + // System.out.println("LOOK("+s.stateNumber+")"); + std::vector look; + + if (s == nullptr) { + return look; + } + + // need s->getNumberOfTransitions()); of them + for (int alt = 0; alt < s->getNumberOfTransitions(); alt++) { + look[alt] = new misc::IntervalSet(0); + std::set *lookBusy = new std::set(); + bool seeThruPreds = false; // fail to get lookahead upon pred + _LOOK(s->transition(alt)->target, nullptr, (PredictionContext*)PredictionContext::EMPTY, look[alt], lookBusy, new antlrcpp::BitSet(), seeThruPreds, false); + // Wipe out lookahead for this alternative if we found nothing + // or we had a predicate when we !seeThruPreds + if (look[alt]->size() == 0 || look[alt]->contains(HIT_PRED)) { + // TODO: memory managment, delete + delete look[alt]; + } + } + return look; + } + + misc::IntervalSet *LL1Analyzer::LOOK(ATNState *s, RuleContext *ctx) { + return LOOK(s, nullptr, ctx); + } + + misc::IntervalSet *LL1Analyzer::LOOK(ATNState *s, ATNState *stopState, RuleContext *ctx) { + misc::IntervalSet *r = new misc::IntervalSet(0); + bool seeThruPreds = true; // ignore preds; get all lookahead + PredictionContext *lookContext = ctx != nullptr ? PredictionContext::fromRuleContext(s->atn, ctx) : nullptr; + _LOOK(s, stopState, lookContext, r, new std::set(), new antlrcpp::BitSet(), seeThruPreds, true); + return r; + } + + void LL1Analyzer::_LOOK(ATNState *s, ATNState *stopState, PredictionContext *ctx, misc::IntervalSet *look, std::set *lookBusy, antlrcpp::BitSet *calledRuleStack, bool seeThruPreds, bool addEOF) { + // System.out.println("_LOOK("+s.stateNumber+", ctx="+ctx); + ATNConfig *c = new ATNConfig(s, 0, ctx); + + if (!lookBusy->insert(c).second) { + return; + } + + if (s == stopState) { + if (ctx == nullptr) { + look->add(Token::EPSILON); + return; + } else if (ctx->isEmpty() && addEOF) { + look->add(Token::_EOF); + return; + } + } + + if (dynamic_cast(s) != nullptr) { + if (ctx == nullptr) { + look->add(Token::EPSILON); + return; + } else if (ctx->isEmpty() && addEOF) { + look->add(Token::_EOF); + return; + } + + if (ctx != (PredictionContext*)PredictionContext::EMPTY) { + // run thru all possible stack tops in ctx + for (int i = 0; i < ctx->size(); i++) { + ATNState *returnState = atn->states[ctx->getReturnState(i)]; + // System.out.println("popping back to "+retState); + + bool removed = calledRuleStack->data.test(returnState->ruleIndex); + try { + calledRuleStack->data[returnState->ruleIndex] = false; + _LOOK(returnState, stopState, ctx->getParent(i), look, lookBusy, calledRuleStack, seeThruPreds, addEOF); + } + catch(...) { + // Just move to the next steps as a "finally" clause + } + if (removed) { + calledRuleStack->set(returnState->ruleIndex); + + } + } + return; + } + } + + int n = s->getNumberOfTransitions(); + for (int i = 0; i < n; i++) { + Transition *t = s->transition(i); + + if (typeid(t) == typeid(RuleTransition)) { + if ( (*calledRuleStack).data[(static_cast(t))->target->ruleIndex]) { + continue; + } + + PredictionContext *newContext = SingletonPredictionContext::create(ctx, (static_cast(t))->followState->stateNumber); + + try { + calledRuleStack->set((static_cast(t))->target->ruleIndex); + _LOOK(t->target, stopState, newContext, look, lookBusy, calledRuleStack, seeThruPreds, addEOF); + } + catch(...) { + // Just move to the next steps as a "finally" clause + } + calledRuleStack->data[((static_cast(t))->target->ruleIndex)] = false; + + } else if (dynamic_cast(t) != nullptr) { + if (seeThruPreds) { + _LOOK(t->target, stopState, ctx, look, lookBusy, calledRuleStack, seeThruPreds, addEOF); + } else { + look->add(HIT_PRED); + } + } else if (t->isEpsilon()) { + _LOOK(t->target, stopState, ctx, look, lookBusy, calledRuleStack, seeThruPreds, addEOF); + } else if (typeid(t) == typeid(WildcardTransition)) { + look->addAll(misc::IntervalSet::of(Token::MIN_USER_TOKEN_TYPE, atn->maxTokenType)); + } else { + // System.out.println("adding "+ t); + misc::IntervalSet *set = t->label(); + if (set != nullptr) { + if (dynamic_cast(t) != nullptr) { + set = set->complement(misc::IntervalSet::of(Token::MIN_USER_TOKEN_TYPE, atn->maxTokenType)); + } + look->addAll(set); + } + } + } + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/LL1Analyzer.h b/runtime/Cpp/org/antlr/v4/runtime/atn/LL1Analyzer.h new file mode 100755 index 000000000..bc7eb1f90 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/LL1Analyzer.h @@ -0,0 +1,141 @@ +#pragma once + +#include "Token.h" +#include "Declarations.h" +#include "BitSet.h" + +#include +#include +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + class LL1Analyzer { + /// + /// Special value added to the lookahead sets to indicate that we hit + /// a predicate during analysis if {@code seeThruPreds==false}. + /// + public: + static const int HIT_PRED = Token::INVALID_TYPE; + + atn::ATN *const atn; + + LL1Analyzer(atn::ATN *atn); + + /// + /// Calculates the SLL(1) expected lookahead set for each outgoing transition + /// of an . The returned array has one element for each + /// outgoing transition in {@code s}. If the closure from transition + /// i leads to a semantic predicate before matching a symbol, the + /// element at index i of the result will be {@code null}. + /// + /// the ATN state + /// the expected symbols for each outgoing transition of {@code s}. + virtual std::vector getDecisionLookahead(ATNState *s); + + /// + /// Compute set of tokens that can follow {@code s} in the ATN in the + /// specified {@code ctx}. + ///

+ /// If {@code ctx} is {@code null} and the end of the rule containing + /// {@code s} is reached, is added to the result set. + /// If {@code ctx} is not {@code null} and the end of the outermost rule is + /// reached, is added to the result set. + ///

+ /// the ATN state + /// the complete parser context, or {@code null} if the context + /// should be ignored + /// + /// The set of tokens that can follow {@code s} in the ATN in the + /// specified {@code ctx}. + virtual misc::IntervalSet *LOOK(ATNState *s, RuleContext *ctx); + + /// + /// Compute set of tokens that can follow {@code s} in the ATN in the + /// specified {@code ctx}. + ///

+ /// If {@code ctx} is {@code null} and the end of the rule containing + /// {@code s} is reached, is added to the result set. + /// If {@code ctx} is not {@code null} and the end of the outermost rule is + /// reached, is added to the result set. + ///

+ /// the ATN state + /// the ATN state to stop at. This can be a + /// to detect epsilon paths through a closure. + /// the complete parser context, or {@code null} if the context + /// should be ignored + /// + /// The set of tokens that can follow {@code s} in the ATN in the + /// specified {@code ctx}. + virtual misc::IntervalSet *LOOK(ATNState *s, ATNState *stopState, RuleContext *ctx); + + /// + /// Compute set of tokens that can follow {@code s} in the ATN in the + /// specified {@code ctx}. + ///

+ /// If {@code ctx} is {@code null} and {@code stopState} or the end of the + /// rule containing {@code s} is reached, is added to + /// the result set. If {@code ctx} is not {@code null} and {@code addEOF} is + /// {@code true} and {@code stopState} or the end of the outermost rule is + /// reached, is added to the result set. + ///

+ /// the ATN state. + /// the ATN state to stop at. This can be a + /// to detect epsilon paths through a closure. + /// The outer context, or {@code null} if the outer context should + /// not be used. + /// The result lookahead set. + /// A set used for preventing epsilon closures in the ATN + /// from causing a stack overflow. Outside code should pass + /// {@code new HashSet} for this argument. + /// A set used for preventing left recursion in the + /// ATN from causing a stack overflow. Outside code should pass + /// {@code new BitSet()} for this argument. + /// {@code true} to true semantic predicates as + /// implicitly {@code true} and "see through them", otherwise {@code false} + /// to treat semantic predicates as opaque and add to the + /// result if one is encountered. + /// Add to the result if the end of the + /// outermost context is reached. This parameter has no effect if {@code ctx} + /// is {@code null}. + protected: + virtual void _LOOK(ATNState *s, ATNState *stopState, PredictionContext *ctx, misc::IntervalSet *look, std::set *lookBusy, antlrcpp::BitSet *calledRuleStack, bool seeThruPreds, bool addEOF); + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/LexerATNConfig.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/LexerATNConfig.cpp new file mode 100755 index 000000000..775f7756b --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/LexerATNConfig.cpp @@ -0,0 +1,108 @@ +#include "LexerATNConfig.h" +#include "MurmurHash.h" +#include "DecisionState.h" +#include "PredictionContext.h" +#include "SemanticContext.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + LexerATNConfig::LexerATNConfig(ATNState *state, int alt, PredictionContext *context) : ATNConfig(state, alt, context, SemanticContext::NONE), passedThroughNonGreedyDecision(false) { + InitializeInstanceFields(); + } + + LexerATNConfig::LexerATNConfig(ATNState *state, int alt, PredictionContext *context, int actionIndex) : ATNConfig(state, alt, context, SemanticContext::NONE), passedThroughNonGreedyDecision(false) { + InitializeInstanceFields(); + this->lexerActionIndex = actionIndex; + } + + LexerATNConfig::LexerATNConfig(LexerATNConfig *c, ATNState *state) : ATNConfig(c, state, c->context, c->semanticContext), passedThroughNonGreedyDecision(checkNonGreedyDecision(c, state)) { + InitializeInstanceFields(); + this->lexerActionIndex = c->lexerActionIndex; + } + + LexerATNConfig::LexerATNConfig(LexerATNConfig *c, ATNState *state, int actionIndex) : ATNConfig(c, state, c->context, c->semanticContext), passedThroughNonGreedyDecision(checkNonGreedyDecision(c, state)) { + InitializeInstanceFields(); + this->lexerActionIndex = actionIndex; + } + + LexerATNConfig::LexerATNConfig(LexerATNConfig *c, ATNState *state, PredictionContext *context) : ATNConfig(c, state, context, c->semanticContext), passedThroughNonGreedyDecision(checkNonGreedyDecision(c, state)) { + InitializeInstanceFields(); + this->lexerActionIndex = c->lexerActionIndex; + } + + bool LexerATNConfig::hasPassedThroughNonGreedyDecision() { + return passedThroughNonGreedyDecision; + } + + size_t LexerATNConfig::hashCode() { + int hashCode = misc::MurmurHash::initialize(7); + hashCode = misc::MurmurHash::update(hashCode, state->stateNumber); + hashCode = misc::MurmurHash::update(hashCode, alt); + hashCode = misc::MurmurHash::update(hashCode, context); + hashCode = misc::MurmurHash::update(hashCode, semanticContext); + hashCode = misc::MurmurHash::update(hashCode, passedThroughNonGreedyDecision ? 1 : 0); + hashCode = misc::MurmurHash::finish(hashCode, 5); + return hashCode; + } + + bool LexerATNConfig::equals(ATNConfig *other) { + if (this == other) { + return true; + } else if (!(dynamic_cast(other) != nullptr)) { + return false; + } + + LexerATNConfig *lexerOther = static_cast(other); + if (passedThroughNonGreedyDecision != lexerOther->passedThroughNonGreedyDecision) { + return false; + } + + return ATNConfig::equals(other); + } + + bool LexerATNConfig::checkNonGreedyDecision(LexerATNConfig *source, ATNState *target) { + return source->passedThroughNonGreedyDecision || (dynamic_cast(target) != nullptr && (static_cast(target))->nonGreedy); + } + + void LexerATNConfig::InitializeInstanceFields() { + lexerActionIndex = -1; + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/LexerATNConfig.h b/runtime/Cpp/org/antlr/v4/runtime/atn/LexerATNConfig.h new file mode 100755 index 000000000..1ab8ae150 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/LexerATNConfig.h @@ -0,0 +1,81 @@ +#pragma once + +#include "ATNConfig.h" +#include "ATNState.h" +#include "Declarations.h" + + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + class LexerATNConfig : public ATNConfig { + /// + /// Capture lexer action we traverse + public: + int lexerActionIndex; + + private: + const bool passedThroughNonGreedyDecision; + + public: + LexerATNConfig(ATNState *state, int alt, PredictionContext *context); + + LexerATNConfig(ATNState *state, int alt, PredictionContext *context, int actionIndex); + + LexerATNConfig(LexerATNConfig *c, ATNState *state); + + LexerATNConfig(LexerATNConfig *c, ATNState *state, int actionIndex); + + LexerATNConfig(LexerATNConfig *c, ATNState *state, PredictionContext *context); + + bool hasPassedThroughNonGreedyDecision(); + + virtual size_t hashCode() override; + + virtual bool equals(ATNConfig *other) override; + + private: + static bool checkNonGreedyDecision(LexerATNConfig *source, ATNState *target); + + private: + void InitializeInstanceFields(); + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/LexerATNSimulator.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/LexerATNSimulator.cpp new file mode 100755 index 000000000..921bb6710 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/LexerATNSimulator.cpp @@ -0,0 +1,613 @@ +#include +#include + +#include "LexerATNSimulator.h" +#include "IntStream.h" +#include "OrderedATNConfigSet.h" +#include "Token.h" +#include "LexerNoViableAltException.h" +#include "PredictionContext.h" +#include "RuleStopState.h" +#include "RuleTransition.h" +#include "SingletonPredictionContext.h" +#include "PredicateTransition.h" +#include "ActionTransition.h" +#include "ATNConfig.h" +#include "Interval.h" +#include "DFA.h" +#include "Lexer.h" +#include "ATN.h" +#include "DFAState.h" +#include "LexerATNConfig.h" +#include "LexerNoViableAltException.h" +#include "Exceptions.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + void LexerATNSimulator::SimState::reset() { + index = -1; + line = 0; + charPos = -1; + // TODO: Memory Management - delete + delete dfaState; + } + + void LexerATNSimulator::SimState::InitializeInstanceFields() { + index = -1; + line = 0; + charPos = -1; + } + + int LexerATNSimulator::match_calls = 0; + + + LexerATNSimulator::LexerATNSimulator(ATN *atn, std::vector decisionToDFA, PredictionContextCache *sharedContextCache) : prevAccept(new SimState()), recog(nullptr) { + } + + LexerATNSimulator::LexerATNSimulator(Lexer *recog, ATN *atn, std::vector decisionToDFA, PredictionContextCache *sharedContextCache) : ATNSimulator(atn,sharedContextCache), recog(recog), decisionToDFA(decisionToDFA), prevAccept(new SimState()) { + InitializeInstanceFields(); + } + + void LexerATNSimulator::copyState(LexerATNSimulator *simulator) { + this->charPositionInLine = simulator->charPositionInLine; + this->line = simulator->line; + this->mode = simulator->mode; + this->startIndex = simulator->startIndex; + } + + int LexerATNSimulator::match(CharStream *input, int mode) { + match_calls++; + this->mode = mode; + int mark = input->mark(); + try { + this->startIndex = input->index(); + this->prevAccept->reset(); + dfa::DFA dfa = *decisionToDFA[mode]; + if (dfa.s0 == nullptr) { + return matchATN(input); + } else { + return execATN(input, dfa.s0); + } + } + catch(...) { + input->release(mark); + } + return -1; + } + + void LexerATNSimulator::reset() { + prevAccept->reset(); + startIndex = -1; + line = 1; + charPositionInLine = 0; + mode = Lexer::DEFAULT_MODE; + } + + int LexerATNSimulator::matchATN(CharStream *input) { + ATNState *startState = (ATNState *)atn->modeToStartState->at(mode); + + if (debug) { + std::wcout << L"matchATN mode" << mode << L" start: " << startState << std::endl; + } + + int old_mode = mode; + + ATNConfigSet *s0_closure = computeStartState(input, startState); + bool suppressEdge = s0_closure->hasSemanticContext; + s0_closure->hasSemanticContext = false; + + dfa::DFAState *next = addDFAState(s0_closure); + if (!suppressEdge) { + decisionToDFA[mode]->s0 = next; + } + + int predict = execATN(input, next); + + if (debug) { + std::wcout << L"DFA after matchATN: " << decisionToDFA[old_mode]->toLexerString() << std::endl; + } + + return predict; + } + + int LexerATNSimulator::execATN(CharStream *input, dfa::DFAState *ds0) { + //System.out.println("enter exec index "+input.index()+" from "+ds0.configs); + if (debug) { + std::wcout << L"start state closure=" << ds0->configs << std::endl; + } + + int t = input->LA(1); + dfa::DFAState *s = ds0; // s is current/from DFA state + + while (true) { // while more work + if (debug) { + std::wcout << L"execATN loop starting closure: " << s->configs << std::endl; + } + + // As we move src->trg, src->trg, we keep track of the previous trg to + // avoid looking up the DFA state again, which is expensive. + // If the previous target was already part of the DFA, we might + // be able to avoid doing a reach operation upon t. If s!=null, + // it means that semantic predicates didn't prevent us from + // creating a DFA state. Once we know s!=null, we check to see if + // the DFA state has an edge already for t. If so, we can just reuse + // it's configuration set; there's no point in re-computing it. + // This is kind of like doing DFA simulation within the ATN + // simulation because DFA simulation is really just a way to avoid + // computing reach/closure sets. Technically, once we know that + // we have a previously added DFA state, we could jump over to + // the DFA simulator. But, that would mean popping back and forth + // a lot and making things more complicated algorithmically. + // This optimization makes a lot of sense for loops within DFA. + // A character will take us back to an existing DFA state + // that already has lots of edges out of it. e.g., .* in comments. + dfa::DFAState *target = getExistingTargetState(s, t); + if (target == nullptr) { + target = computeTargetState(input, s, t); + } + + if (target == ERROR) { + break; + } + + if (target->isAcceptState) { + captureSimState(prevAccept, input, target); + if (t == IntStream::_EOF) { + break; + } + } + + if (t != IntStream::_EOF) { + consume(input); + t = input->LA(1); + } + + s = target; // flip; current DFA target becomes new src/from state + } + + return failOrAccept(prevAccept, input, s->configs, t); + } + + dfa::DFAState *LexerATNSimulator::getExistingTargetState(dfa::DFAState *s, int t) { + if (s->edges.size() == 0 || t < MIN_DFA_EDGE || t > MAX_DFA_EDGE) { + return nullptr; + } + + dfa::DFAState *target = s->edges[t - MIN_DFA_EDGE]; + if (debug && target != nullptr) { + std::wcout << std::wstring(L"reuse state ") << s->stateNumber << std::wstring(L" edge to ") << target->stateNumber << std::endl; + } + + return target; + } + + dfa::DFAState *LexerATNSimulator::computeTargetState(CharStream *input, dfa::DFAState *s, int t) { + ATNConfigSet *reach = new OrderedATNConfigSet(); + + // if we don't find an existing DFA state + // Fill reach starting from closure, following t transitions + getReachableConfigSet(input, s->configs, reach, t); + + if (reach->isEmpty()) { // we got nowhere on t from s + // we got nowhere on t, don't throw out this knowledge; it'd + // cause a failover from DFA later. + addDFAEdge(s, t, ERROR); + // stop when we can't match any more char + return ERROR; + } + + // Add an edge from s to target DFA found/created for reach + return addDFAEdge(s, t, reach); + } + + int LexerATNSimulator::failOrAccept(SimState *prevAccept, CharStream *input, ATNConfigSet *reach, int t) { + if (prevAccept->dfaState != nullptr) { + int ruleIndex = prevAccept->dfaState->lexerRuleIndex; + int actionIndex = prevAccept->dfaState->lexerActionIndex; + accept(input, ruleIndex, actionIndex, prevAccept->index, prevAccept->line, prevAccept->charPos); + return prevAccept->dfaState->prediction; + } else { + // if no accept and EOF is first char, return EOF + if (t == IntStream::_EOF && input->index() == startIndex) { + return Token::_EOF; + } + + throw LexerNoViableAltException(recog, input, startIndex, reach); + } + } + + void LexerATNSimulator::getReachableConfigSet(CharStream *input, ATNConfigSet *closure, ATNConfigSet *reach, int t) { + // this is used to skip processing for configs which have a lower priority + // than a config that already reached an accept state for the same rule + int skipAlt = ATN::INVALID_ALT_NUMBER; + for (auto c : *closure) { + bool currentAltReachedAcceptState = c->alt == skipAlt; + if (currentAltReachedAcceptState && (static_cast(c))->hasPassedThroughNonGreedyDecision()) { + continue; + } + + if (debug) { + std::wcout << L"testing " << getTokenName(t) << " at " <toString(recog, true) << std::endl; + } + + int n = c->state->getNumberOfTransitions(); + for (int ti = 0; ti < n; ti++) { // for each transition + Transition *trans = c->state->transition(ti); + ATNState *target = getReachableTarget(trans, t); + if (target != nullptr) { + if (this->closure(input, new LexerATNConfig(static_cast(c), target), reach, currentAltReachedAcceptState, true)) { + // any remaining configs for this alt have a lower priority than + // the one that just reached an accept state. + skipAlt = c->alt; + break; + } + } + } + } + } + + void LexerATNSimulator::accept(CharStream *input, int ruleIndex, int actionIndex, int index, int line, int charPos) { + if (debug) { + std::wcout << L"ACTION "; + if (recog != nullptr) { + std::wcout << recog->getRuleNames()[ruleIndex]; + } else { + std::wcout << ruleIndex; + } + std::wcout << ":" << actionIndex << std::endl; + } + + if (actionIndex >= 0 && recog != nullptr) { + recog->action(nullptr, ruleIndex, actionIndex); + } + + // seek to after last char in token + input->seek(index); + this->line = line; + this->charPositionInLine = charPos; + if (input->LA(1) != IntStream::_EOF) { + consume(input); + } + } + + atn::ATNState *LexerATNSimulator::getReachableTarget(Transition *trans, int t) { + if (trans->matches(t, WCHAR_MIN, WCHAR_MAX)) { + return trans->target; + } + + return nullptr; + } + + atn::ATNConfigSet *LexerATNSimulator::computeStartState(CharStream *input, ATNState *p) { + EmptyPredictionContext * initialContext = PredictionContext::EMPTY; + ATNConfigSet *configs = new OrderedATNConfigSet(); + for (int i = 0; i < p->getNumberOfTransitions(); i++) { + ATNState *target = p->transition(i)->target; + LexerATNConfig *c = new LexerATNConfig(target, i + 1, (PredictionContext*)initialContext); + closure(input, c, configs, false, false); + } + return configs; + } + + bool LexerATNSimulator::closure(CharStream *input, LexerATNConfig *config, ATNConfigSet *configs, bool currentAltReachedAcceptState, bool speculative) { + if (debug) { + std::wcout << L"closure(" << config->toString(recog, true) << L")" << std::endl; + } + + if (dynamic_cast(config->state) != nullptr) { + if (debug) { + if (recog != nullptr) { + std::wcout << L"closure at " << recog->getRuleNames()[config->state->ruleIndex] << L" rule stop " << config << std::endl; + } else { + std::wcout << L"closure at rule stop " << config << std::endl; + } + } + + if (config->context == nullptr || config->context->hasEmptyPath()) { + if (config->context == nullptr || config->context->isEmpty()) { + configs->add(config); + return true; + } else { + configs->add(new LexerATNConfig(config, config->state, (PredictionContext*)PredictionContext::EMPTY)); + currentAltReachedAcceptState = true; + } + } + + if (config->context != nullptr && !config->context->isEmpty()) { + for (int i = 0; i < config->context->size(); i++) { + if (config->context->getReturnState(i) != PredictionContext::EMPTY_RETURN_STATE) { + PredictionContext *newContext = config->context->getParent(i); // "pop" return state + ATNState *returnState = atn->states[config->context->getReturnState(i)]; + LexerATNConfig *c = new LexerATNConfig(returnState, config->alt, newContext); + currentAltReachedAcceptState = closure(input, c, configs, currentAltReachedAcceptState, speculative); + } + } + } + + return currentAltReachedAcceptState; + } + + // optimization + if (!config->state->onlyHasEpsilonTransitions()) { + if (!currentAltReachedAcceptState || !config->hasPassedThroughNonGreedyDecision()) { + configs->add(config); + } + } + + ATNState *p = config->state; + for (int i = 0; i < p->getNumberOfTransitions(); i++) { + Transition *t = p->transition(i); + LexerATNConfig *c = getEpsilonTarget(input, config, t, configs, speculative); + if (c != nullptr) { + currentAltReachedAcceptState = closure(input, c, configs, currentAltReachedAcceptState, speculative); + } + } + + return currentAltReachedAcceptState; + } + + atn::LexerATNConfig *LexerATNSimulator::getEpsilonTarget(CharStream *input, LexerATNConfig *config, Transition *t, ATNConfigSet *configs, bool speculative) { + LexerATNConfig *c = nullptr; + switch (t->getSerializationType()) { + case Transition::RULE: { + RuleTransition *ruleTransition = static_cast(t); + PredictionContext *newContext = SingletonPredictionContext::create(config->context, ruleTransition->followState->stateNumber); + c = new LexerATNConfig(config, t->target, newContext); + } + break; + + case Transition::PRECEDENCE: + //{ + throw new UnsupportedOperationException(L"Precedence predicates are not supported in lexers."); + //} + break; + + case Transition::PREDICATE: { + /* Track traversing semantic predicates. If we traverse, + we cannot add a DFA state for this "reach" computation + because the DFA would not test the predicate again in the + future. Rather than creating collections of semantic predicates + like v3 and testing them on prediction, v4 will test them on the + fly all the time using the ATN not the DFA. This is slower but + semantically it's not used that often. One of the key elements to + this predicate mechanism is not adding DFA states that see + predicates immediately afterwards in the ATN. For example, + + a : ID {p1}? | ID {p2}? ; + + should create the start state for rule 'a' (to save start state + competition), but should not create target of ID state. The + collection of ATN states the following ID references includes + states reached by traversing predicates. Since this is when we + test them, we cannot cash the DFA state target of ID. + */ + PredicateTransition *pt = static_cast(t); + if (debug) { + std::wcout << L"EVAL rule " << pt->ruleIndex << L":" << pt->predIndex << std::endl; + } + configs->hasSemanticContext = true; + if (evaluatePredicate(input, pt->ruleIndex, pt->predIndex, speculative)) { + c = new LexerATNConfig(config, t->target); + } + } + break; + // ignore actions; just exec one per rule upon accept + case Transition::ACTION: + c = new LexerATNConfig(config, t->target, (static_cast(t))->actionIndex); + break; + case Transition::EPSILON: + c = new LexerATNConfig(config, t->target); + break; + } + + return c; + } + + bool LexerATNSimulator::evaluatePredicate(CharStream *input, int ruleIndex, int predIndex, bool speculative) { + // assume true if no recognizer was provided + if (recog == nullptr) { + return true; + } + + if (!speculative) { + return recog->sempred(nullptr, ruleIndex, predIndex); + } + + int savedCharPositionInLine = charPositionInLine; + int savedLine = line; + int index = input->index(); + int marker = input->mark(); + try { + consume(input); + return recog->sempred(nullptr, ruleIndex, predIndex); + } catch(...) { + charPositionInLine = savedCharPositionInLine; + line = savedLine; + input->seek(index); + input->release(marker); + } + return false; + } + + void LexerATNSimulator::captureSimState(SimState *settings, CharStream *input, dfa::DFAState *dfaState) { + settings->index = input->index(); + settings->line = line; + settings->charPos = charPositionInLine; + settings->dfaState = dfaState; + } + + dfa::DFAState *LexerATNSimulator::addDFAEdge(dfa::DFAState *from, int t, ATNConfigSet *q) { + /* leading to this call, ATNConfigSet.hasSemanticContext is used as a + * marker indicating dynamic predicate evaluation makes this edge + * dependent on the specific input sequence, so the static edge in the + * DFA should be omitted. The target DFAState is still created since + * execATN has the ability to resynchronize with the DFA state cache + * following the predicate evaluation step. + * + * TJP notes: next time through the DFA, we see a pred again and eval. + * If that gets us to a previously created (but dangling) DFA + * state, we can continue in pure DFA mode from there. + */ + bool suppressEdge = q->hasSemanticContext; + q->hasSemanticContext = false; + + dfa::DFAState *to = addDFAState(q); + + if (suppressEdge) { + return to; + } + + addDFAEdge(from, t, to); + return to; + } + + void LexerATNSimulator::addDFAEdge(dfa::DFAState *p, int t, dfa::DFAState *q) { + if (t < MIN_DFA_EDGE || t > MAX_DFA_EDGE) { + // Only track edges within the DFA bounds + return; + } + + if (debug) { + std::wcout << std::wstring(L"EDGE ") << p << std::wstring(L" -> ") << q << std::wstring(L" upon ") << (static_cast(t)) << std::endl; + } + + if(true) { + std::lock_guard lck(mtx); + if (p->edges.empty()) { + // make room for tokens 1..n and -1 masquerading as index 0 + p->edges = std::vector(MAX_DFA_EDGE - MIN_DFA_EDGE + 1); + } + p->edges[t - MIN_DFA_EDGE] = q; // connect + } + } + + dfa::DFAState *LexerATNSimulator::addDFAState(ATNConfigSet *configs) { + /* the lexer evaluates predicates on-the-fly; by this point configs + * should not contain any configurations with unevaluated predicates. + */ + assert(!configs->hasSemanticContext); + + dfa::DFAState *proposed = new dfa::DFAState(configs); + ATNConfig *firstConfigWithRuleStopState = nullptr; + for (auto c : *configs) { + if (dynamic_cast(c->state) != nullptr) { + firstConfigWithRuleStopState = c; + break; + } + } + + if (firstConfigWithRuleStopState != nullptr) { + proposed->isAcceptState = true; + proposed->lexerRuleIndex = firstConfigWithRuleStopState->state->ruleIndex; + proposed->lexerActionIndex = (static_cast(firstConfigWithRuleStopState))->lexerActionIndex; + proposed->prediction = atn->ruleToTokenType[proposed->lexerRuleIndex]; + } + + dfa::DFA *dfa = decisionToDFA[mode]; + + if(true) { + std::lock_guard lck(mtx); + dfa::DFAState *existing = dfa->states->at(proposed); + if (existing != nullptr) { + return existing; + } + + dfa::DFAState *newState = proposed; + + newState->stateNumber = (int)dfa->states->size(); + configs->setReadonly(true); + newState->configs = configs; + dfa->states->emplace(newState, newState); + return newState; + } + } + + dfa::DFA *LexerATNSimulator::getDFA(int mode) { + return decisionToDFA[mode]; + } + + std::wstring LexerATNSimulator::getText(CharStream *input) { + // index is first lookahead char, don't include. + return input->getText(misc::Interval::of(startIndex, input->index() - 1)); + } + + int LexerATNSimulator::getLine() { + return line; + } + + void LexerATNSimulator::setLine(int line) { + this->line = line; + } + + int LexerATNSimulator::getCharPositionInLine() { + return charPositionInLine; + } + + void LexerATNSimulator::setCharPositionInLine(int charPositionInLine) { + this->charPositionInLine = charPositionInLine; + } + + void LexerATNSimulator::consume(CharStream *input) { + int curChar = input->LA(1); + if (curChar == L'\n') { + line++; + charPositionInLine = 0; + } else { + charPositionInLine++; + } + input->consume(); + } + + std::wstring LexerATNSimulator::getTokenName(int t) { + if (t == -1) { + return L"EOF"; + } + //if ( atn.g!=null ) return atn.g.getTokenDisplayName(t); + return std::wstring(L"'") + static_cast(t) + std::wstring(L"'"); + } + + void LexerATNSimulator::InitializeInstanceFields() { + startIndex = -1; + line = 1; + charPositionInLine = 0; + mode = org::antlr::v4::runtime::Lexer::DEFAULT_MODE; + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/LexerATNSimulator.h b/runtime/Cpp/org/antlr/v4/runtime/atn/LexerATNSimulator.h new file mode 100755 index 000000000..8ba0cac33 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/LexerATNSimulator.h @@ -0,0 +1,256 @@ +#pragma once + +#include +#include + +#include "ATNSimulator.h" +#include "Declarations.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + /// + /// "dup" of ParserInterpreter + class LexerATNSimulator : public ATNSimulator { + protected: + class SimState { + protected: + int index; + int line; + int charPos; + dfa::DFAState *dfaState; + virtual void reset(); + friend class LexerATNSimulator; + + private: + void InitializeInstanceFields(); + + public: + SimState() { + InitializeInstanceFields(); + } + + }; + + + public: + static const bool debug = false; + static const bool dfa_debug = false; + + static const int MIN_DFA_EDGE = 0; + static const int MAX_DFA_EDGE = 127; // forces unicode to stay in ATN + + /// + /// When we hit an accept state in either the DFA or the ATN, we + /// have to notify the character stream to start buffering characters + /// via and record the current state. The current sim state + /// includes the current index into the input, the current line, + /// and current character position in that line. Note that the Lexer is + /// tracking the starting line and characterization of the token. These + /// variables track the "state" of the simulator when it hits an accept state. + ///

+ /// We track these variables separately for the DFA and ATN simulation + /// because the DFA simulation often has to fail over to the ATN + /// simulation. If the ATN simulation fails, we need the DFA to fall + /// back to its previously accepted state, if any. If the ATN succeeds, + /// then the ATN does the accept and the DFA simulator that invoked it + /// can simply return the predicted token type. + ///

+ protected: + Lexer *const recog; + + /// + /// The current token's starting index into the character stream. + /// Shared across DFA to ATN simulation in case the ATN fails and the + /// DFA did not have a previous accept state. In this case, we use the + /// ATN-generated exception object. + /// + int startIndex; + + /// + /// line number 1..n within the input + int line; + + /// + /// The index of the character relative to the beginning of the line 0..n-1 + int charPositionInLine; + + public: + const std::vector decisionToDFA; + protected: + int mode; + + /// + /// Used during DFA/ATN exec to record the most recent accept configuration info + SimState *const prevAccept; + + public: + static int match_calls; + + LexerATNSimulator(ATN *atn, std::vector decisionToDFA, PredictionContextCache *sharedContextCache); //this(nullptr, atn, decisionToDFA,sharedContextCache); + + LexerATNSimulator(Lexer *recog, ATN *atn, std::vector decisionToDFA, PredictionContextCache *sharedContextCache); + + virtual void copyState(LexerATNSimulator *simulator); + + virtual int match(CharStream *input, int mode); + + virtual void reset() override; + + protected: + virtual int matchATN(CharStream *input); + + virtual int execATN(CharStream *input, dfa::DFAState *ds0); + + /// + /// Get an existing target state for an edge in the DFA. If the target state + /// for the edge has not yet been computed or is otherwise not available, + /// this method returns {@code null}. + /// + /// The current DFA state + /// The next input symbol + /// The existing target DFA state for the given input symbol + /// {@code t}, or {@code null} if the target state for this edge is not + /// already cached + virtual dfa::DFAState *getExistingTargetState(dfa::DFAState *s, int t); + + /// + /// Compute a target state for an edge in the DFA, and attempt to add the + /// computed state and corresponding edge to the DFA. + /// + /// The input stream + /// The current DFA state + /// The next input symbol + /// + /// The computed target DFA state for the given input symbol + /// {@code t}. If {@code t} does not lead to a valid DFA state, this method + /// returns . + virtual dfa::DFAState *computeTargetState(CharStream *input, dfa::DFAState *s, int t); + + virtual int failOrAccept(SimState *prevAccept, CharStream *input, ATNConfigSet *reach, int t); + + /// + /// Given a starting configuration set, figure out all ATN configurations + /// we can reach upon input {@code t}. Parameter {@code reach} is a return + /// parameter. + /// + virtual void getReachableConfigSet(CharStream *input, ATNConfigSet *closure, ATNConfigSet *reach, int t); + + virtual void accept(CharStream *input, int ruleIndex, int actionIndex, int index, int line, int charPos); + + virtual ATNState *getReachableTarget(Transition *trans, int t); + + virtual atn::ATNConfigSet *computeStartState(CharStream *input, ATNState *p); + + /// + /// Since the alternatives within any lexer decision are ordered by + /// preference, this method stops pursuing the closure as soon as an accept + /// state is reached. After the first accept state is reached by depth-first + /// search from {@code config}, all other (potentially reachable) states for + /// this rule would have a lower priority. + /// + /// {@code true} if an accept state is reached, otherwise + /// {@code false}. + virtual bool closure(CharStream *input, LexerATNConfig *config, ATNConfigSet *configs, bool currentAltReachedAcceptState, bool speculative); + + // side-effect: can alter configs.hasSemanticContext + virtual LexerATNConfig *getEpsilonTarget(CharStream *input, LexerATNConfig *config, Transition *t, ATNConfigSet *configs, bool speculative); + + /// + /// Evaluate a predicate specified in the lexer. + ///

+ /// If {@code speculative} is {@code true}, this method was called before + /// for the matched character. This method should call + /// before evaluating the predicate to ensure position + /// sensitive values, including , , + /// and , properly reflect the current + /// lexer state. This method should restore {@code input} and the simulator + /// to the original state before returning (i.e. undo the actions made by the + /// call to . + ///

+ /// The input stream. + /// The rule containing the predicate. + /// The index of the predicate within the rule. + /// {@code true} if the current index in {@code input} is + /// one character before the predicate's location. + /// + /// {@code true} if the specified predicate evaluates to + /// {@code true}. + virtual bool evaluatePredicate(CharStream *input, int ruleIndex, int predIndex, bool speculative); + + virtual void captureSimState(SimState *settings, CharStream *input, dfa::DFAState *dfaState); + + virtual dfa::DFAState *addDFAEdge(dfa::DFAState *from, int t, ATNConfigSet *q); + + virtual void addDFAEdge(dfa::DFAState *p, int t, dfa::DFAState *q); + + /// + /// Add a new DFA state if there isn't one with this set of + /// configurations already. This method also detects the first + /// configuration containing an ATN rule stop state. Later, when + /// traversing the DFA, we will know which rule to accept. + /// + virtual dfa::DFAState *addDFAState(ATNConfigSet *configs); + + public: + dfa::DFA *getDFA(int mode); + + /// + /// Get the text matched so far for the current token. + /// + virtual std::wstring getText(CharStream *input); + + virtual int getLine(); + + virtual void setLine(int line); + + virtual int getCharPositionInLine(); + + virtual void setCharPositionInLine(int charPositionInLine); + + virtual void consume(CharStream *input); + + virtual std::wstring getTokenName(int t); + + private: + void InitializeInstanceFields(); + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/LoopEndState.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/LoopEndState.cpp new file mode 100755 index 000000000..7b0767184 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/LoopEndState.cpp @@ -0,0 +1,46 @@ +#include "LoopEndState.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + int LoopEndState::getStateType() { + return LOOP_END; + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/LoopEndState.h b/runtime/Cpp/org/antlr/v4/runtime/atn/LoopEndState.h new file mode 100755 index 000000000..7af890257 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/LoopEndState.h @@ -0,0 +1,54 @@ +#pragma once + +#include "ATNState.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + /// + /// Mark the end of a * or + loop. + class LoopEndState final : public ATNState { + public: + ATNState *loopBackState; + + virtual int getStateType() override; + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/Makefile b/runtime/Cpp/org/antlr/v4/runtime/atn/Makefile new file mode 100644 index 000000000..480bd8592 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/Makefile @@ -0,0 +1,67 @@ + +CXXFLAGS += -g -std=c++0x -Wall #-Wextra +CXXFLAGS += -I. -I../ -I../misc/ -I../tree/ -I../dfa/ \ + -I../../../../../antlrcpp/ + +#TODO LDFLAGS += ? + +ALL_CXXFLAGS = $(CPPFLAGS) $(CXXFLAGS) +ALL_LDFLAGS = $(LDFLAGS) + +# Escote's files +SRCS = \ + AbstractPredicateTransition.cpp \ + ActionTransition.cpp \ + ArrayPredictionContext.cpp \ + ATNDeserializationOptions.cpp \ + ATNDeserializer.cpp \ + ATNState.cpp \ + ATNType.cpp \ + AtomTransition.cpp \ + BasicBlockStartState.cpp \ + BasicState.cpp \ + BlockEndState.cpp \ + BlockStartState.cpp \ + DecisionState.cpp \ + EmptyPredictionContext.cpp \ + EpsilonTransition.cpp \ + LexerATNConfig.cpp \ + LoopEndState.cpp +# Escote's TODO: LL1Analyzer.cpp LexerATNSimulator.cpp ATNSimulator.cpp \ + ATNSerializer.cpp ATNConfigSet.cpp ATNConfig.cpp \ + ATN.cpp + +# Alejandro's files +SRCS += \ + NotSetTransition.cpp \ + OrderedATNConfigSet.cpp \ + PlusBlockStartState.cpp \ + PlusLoopbackState.cpp \ + PredicateTransition.cpp \ + PredictionMode.cpp \ + RangeTransition.cpp \ + RuleStartState.cpp \ + RuleStopState.cpp \ + RuleTransition.cpp \ + SemanticContext.cpp \ + SetTransition.cpp \ + SingletonPredictionContext.cpp \ + StarBlockStartState.cpp \ + StarLoopbackState.cpp \ + StarLoopEntryState.cpp \ + TokensStartState.cpp \ + Transition.cpp \ + WildcardTransition.cpp +# Alejandro's TODO: PredictionContext.cpp PredictionContextCache.cpp \ + PrecedencePredicateTransition.cpp ParserATNSimulator.cpp + +OBJS = $(SRCS:.cpp=.o) + +all: $(OBJS) + +%.o: %.cpp + $(CXX) -c $(ALL_CXXFLAGS) $< -o $@ + +clean: + $(RM) $(OBJS) + diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/NotSetTransition.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/NotSetTransition.cpp new file mode 100755 index 000000000..73581f6be --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/NotSetTransition.cpp @@ -0,0 +1,59 @@ +#include "NotSetTransition.h" +#include "ATNState.h" +#include "IntervalSet.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + NotSetTransition::NotSetTransition(ATNState *target, misc::IntervalSet *set) : SetTransition(target, set) { + } + + int NotSetTransition::getSerializationType() { + return NOT_SET; + } + + bool NotSetTransition::matches(int symbol, int minVocabSymbol, int maxVocabSymbol) { + return symbol >= minVocabSymbol && symbol <= maxVocabSymbol && !SetTransition::matches(symbol, minVocabSymbol, maxVocabSymbol); + } + + std::wstring NotSetTransition::toString() { + return L'~' + SetTransition::toString(); + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/NotSetTransition.h b/runtime/Cpp/org/antlr/v4/runtime/atn/NotSetTransition.h new file mode 100755 index 000000000..c8ffd881b --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/NotSetTransition.h @@ -0,0 +1,58 @@ +#pragma once + +#include "SetTransition.h" +#include "Declarations.h" +#include + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + class NotSetTransition final : public SetTransition { + public: + NotSetTransition(ATNState *target, misc::IntervalSet *set); + + virtual int getSerializationType() override; + + virtual bool matches(int symbol, int minVocabSymbol, int maxVocabSymbol) override; + + virtual std::wstring toString() override; + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/OrderedATNConfigSet.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/OrderedATNConfigSet.cpp new file mode 100755 index 000000000..33a489c67 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/OrderedATNConfigSet.cpp @@ -0,0 +1,51 @@ +#include "OrderedATNConfigSet.h" +#include "ObjectEqualityComparator.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + OrderedATNConfigSet::LexerConfigHashSet::LexerConfigHashSet() : + AbstractConfigHashSet(misc::ObjectEqualityComparator::INSTANCE){ + } + + OrderedATNConfigSet::OrderedATNConfigSet() { + this->configLookup = new LexerConfigHashSet(); + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/OrderedATNConfigSet.h b/runtime/Cpp/org/antlr/v4/runtime/atn/OrderedATNConfigSet.h new file mode 100755 index 000000000..3c4fedbc3 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/OrderedATNConfigSet.h @@ -0,0 +1,61 @@ +#pragma once + +#include "ATNConfigSet.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + /// + /// + /// @author Sam Harwell + /// + class OrderedATNConfigSet : public ATNConfigSet { + public: + class LexerConfigHashSet : public AbstractConfigHashSet { + public: + LexerConfigHashSet(); + }; + + public: + OrderedATNConfigSet(); + + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/ParserATNSimulator.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/ParserATNSimulator.cpp new file mode 100755 index 000000000..d99e00efa --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/ParserATNSimulator.cpp @@ -0,0 +1,1161 @@ +#include "BitSet.h" +#include "Token.h" +#include "ATN.h" +#include "DFA.h" +#include "Exceptions.h" +#include "NoViableAltException.h" +#include "DoubleKeyMap.h" +#include "ATNState.h" +#include "TokenStream.h" +#include "DecisionState.h" +#include "ParserRuleContext.h" +#include "ParserATNSimulator.h" +#include "PredictionContext.h" +#include "IntervalSet.h" +#include "Transition.h" +#include "Interval.h" +#include "ATNConfigSet.h" +#include "ANTLRErrorListener.h" +#include "PredictionMode.h" +#include "Utils.h" + + +// TODO: Using "wcout <<" for debug information is really lame, change out with +// some kind of listener + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + ParserATNSimulator::ParserATNSimulator(ATN *atn, const std::vector& decisionToDFA, PredictionContextCache *sharedContextCache): + parser(nullptr) { + } + + ParserATNSimulator::ParserATNSimulator(Parser *parser, ATN *atn, const std::vector& decisionToDFA, PredictionContextCache *sharedContextCache) : ATNSimulator(atn,sharedContextCache), parser(parser), _decisionToDFA(decisionToDFA) { + InitializeInstanceFields(); + // DOTGenerator dot = new DOTGenerator(null); + // System.out.println(dot.getDOT(atn.rules.get(0), parser.getRuleNames())); + // System.out.println(dot.getDOT(atn.rules.get(1), parser.getRuleNames())); + } + + void ParserATNSimulator::reset() { + } + + int ParserATNSimulator::adaptivePredict(TokenStream *input, int decision, ParserRuleContext *outerContext) { + if (debug || debug_list_atn_decisions) { + std::wcout << L"adaptivePredict decision " << decision << L" exec LA(1)==" << getLookaheadName(input) << L" line " << input->LT(1)->getLine() << L":" << input->LT(1)->getCharPositionInLine() << std::endl; + } + + _input = input; + _startIndex = input->index(); + _outerContext = outerContext; + dfa::DFA dfa = _decisionToDFA[decision]; + + int m = input->mark(); + int index = input->index(); + + // Now we are certain to have a specific decision's DFA + // But, do we still need an initial state? + try { + if (dfa.s0 == nullptr) { + if (outerContext == nullptr) { + outerContext = ParserRuleContext::EMPTY; + } + if (debug || debug_list_atn_decisions) { + std::wcout << L"predictATN decision " << dfa.decision << L" exec LA(1)==" << getLookaheadName(input) << L", outerContext=" << outerContext->toString(parser) << std::endl; + } + bool fullCtx = false; + ATNConfigSet *s0_closure = computeStartState(dynamic_cast(dfa.atnStartState), + ParserRuleContext::EMPTY, fullCtx); + dfa.s0 = addDFAState(&dfa, new dfa::DFAState(s0_closure)); + } + + // We can start with an existing DFA. + int alt = execATN(&dfa, dfa.s0, input, index, outerContext); + if (debug) { + std::wcout << "DFA after predictATN: " << dfa.toString(parser->getTokenNames()) << std::endl; + } + return alt; + } + catch (std::exception e) { + // Do nothing, failed predict + } + + delete mergeCache; // wack cache after each prediction + input->seek(index); + input->release(m); + + return 0; + } + + int ParserATNSimulator::execATN(dfa::DFA *dfa, dfa::DFAState *s0, TokenStream *input, int startIndex, ParserRuleContext *outerContext) { + if (debug || debug_list_atn_decisions) { + std::wcout << L"execATN decision " << dfa->decision << L" exec LA(1)==" << getLookaheadName(input) << L" line " << input->LT(1)->getLine() << L":" << input->LT(1)->getCharPositionInLine() << std::endl; + } + + dfa::DFAState *previousD = s0; + + if (debug) { + std::wcout << L"s0 = " << s0 << std::endl; + } + + int t = input->LA(1); + + while (true) { // while more work + dfa::DFAState *D = getExistingTargetState(previousD, t); + if (D == nullptr) { + D = computeTargetState(dfa, previousD, t); + } + + if (D == ERROR) { + // if any configs in previous dipped into outer context, that + // means that input up to t actually finished entry rule + // at least for SLL decision. Full LL doesn't dip into outer + // so don't need special case. + // We will get an error no matter what so delay until after + // decision; better error message. Also, no reachable target + // ATN states in SLL implies LL will also get nowhere. + // If conflict in states that dip out, choose min since we + // will get error no matter what. + int alt = getAltThatFinishedDecisionEntryRule(previousD->configs); + if (alt != ATN::INVALID_ALT_NUMBER) { + // return w/o altering DFA + return alt; + } + + throw noViableAlt(input, outerContext, previousD->configs, startIndex); + } + + if (D->requiresFullContext && mode != PredictionMode::SLL) { + // IF PREDS, MIGHT RESOLVE TO SINGLE ALT => SLL (or syntax error) + antlrcpp::BitSet *conflictingAlts = nullptr; + if (D->predicates.size() != 0) { + if (debug) { + std::wcout << L"DFA state has preds in DFA sim LL failover" << std::endl; + } + int conflictIndex = input->index(); + if (conflictIndex != startIndex) { + input->seek(startIndex); + } + + conflictingAlts = evalSemanticContext(D->predicates, outerContext, true); + if (conflictingAlts->count() == 1) { + if (debug) { + std::wcout << L"Full LL avoided" << std::endl; + } + return conflictingAlts->nextSetBit(0); + } + + if (conflictIndex != startIndex) { + // restore the index so reporting the fallback to full + // context occurs with the index at the correct spot + input->seek(conflictIndex); + } + } + + if (dfa_debug) { + std::wcout << L"ctx sensitive state " << outerContext << L" in " << D << std::endl; + } + bool fullCtx = true; + ATNConfigSet *s0_closure = computeStartState(dfa->atnStartState, outerContext, fullCtx); + reportAttemptingFullContext(dfa, conflictingAlts, D->configs, startIndex, input->index()); + int alt = execATNWithFullContext(dfa, D, s0_closure, input, startIndex, outerContext); + return alt; + } + + if (D->isAcceptState) { + if (D->predicates.size() == 0) { + return D->prediction; + } + + int stopIndex = input->index(); + input->seek(startIndex); + antlrcpp::BitSet *alts = evalSemanticContext(D->predicates, outerContext, true); + switch (alts->count()) { + case 0: + throw noViableAlt(input, outerContext, D->configs, startIndex); + + case 1: + return alts->nextSetBit(0); + + default: + // report ambiguity after predicate evaluation to make sure the correct + // set of ambig alts is reported. + reportAmbiguity(dfa, D, startIndex, stopIndex, false, alts, D->configs); + return alts->nextSetBit(0); + } + } + + previousD = D; + + if (t != IntStream::_EOF) { + input->consume(); + t = input->LA(1); + } + } + } + + dfa::DFAState *ParserATNSimulator::getExistingTargetState(dfa::DFAState *previousD, int t) { + std::vectoredges = previousD->edges; + if (edges.size() == 0 || t + 1 < 0 || t + 1 >= (int)edges.size()) { + return nullptr; + } + + return edges[t + 1]; + } + + dfa::DFAState *ParserATNSimulator::computeTargetState(dfa::DFA *dfa, dfa::DFAState *previousD, int t) { + ATNConfigSet *reach = computeReachSet(previousD->configs, t, false); + if (reach == nullptr) { + addDFAEdge(dfa, previousD, t, ERROR); + return ERROR; + } + + // create new target state; we'll add to DFA after it's complete + dfa::DFAState *D = new dfa::DFAState(reach); + + int predictedAlt = getUniqueAlt(reach); + + if (debug) { + + std::vector altSubSets = PredictionModeClass::getConflictingAltSubsets(reach); + std::wstring altSubSetsStr = antlrcpp::BitSet::subStringRepresentation(altSubSets.begin(), altSubSets.end()); + std::wcout << L"SLL altSubSets=" << altSubSetsStr << L", configs=" + << reach << L", predict=" << predictedAlt << L", allSubsetsConflict=" + << PredictionModeClass::allSubsetsConflict(altSubSets) + << L", conflictingAlts=" << getConflictingAlts(reach) + << std::endl; + } + + if (predictedAlt != ATN::INVALID_ALT_NUMBER) { + // NO CONFLICT, UNIQUELY PREDICTED ALT + D->isAcceptState = true; + D->configs->uniqueAlt = predictedAlt; + D->prediction = predictedAlt; + } else if (PredictionModeClass::hasSLLConflictTerminatingPrediction(&mode, reach)) { + // MORE THAN ONE VIABLE ALTERNATIVE + D->configs->conflictingAlts->data = getConflictingAlts(reach).data; + D->requiresFullContext = true; + // in SLL-only mode, we will stop at this state and return the minimum alt + D->isAcceptState = true; + D->prediction = D->configs->conflictingAlts->nextSetBit(0); + } + + if (D->isAcceptState && D->configs->hasSemanticContext) { + predicateDFAState(D, atn->getDecisionState(dfa->decision)); + if (D->predicates.size() != 0) { + D->prediction = ATN::INVALID_ALT_NUMBER; + } + } + + // all adds to dfa are done after we've created full D state + D = addDFAEdge(dfa, previousD, t, D); + return D; + } + + void ParserATNSimulator::predicateDFAState(dfa::DFAState *dfaState, DecisionState *decisionState) { + // We need to test all predicates, even in DFA states that + // uniquely predict alternative. + int nalts = decisionState->getNumberOfTransitions(); + // Update DFA so reach becomes accept state with (predicate,alt) + // pairs if preds found for conflicting alts + antlrcpp::BitSet *altsToCollectPredsFrom = nullptr; + altsToCollectPredsFrom->data = getConflictingAltsOrUniqueAlt(dfaState->configs).data; + std::vector altToPred = getPredsForAmbigAlts(altsToCollectPredsFrom, dfaState->configs, nalts); + if (!altToPred.empty()) { + dfaState->predicates = getPredicatePredictions(altsToCollectPredsFrom, altToPred); + dfaState->prediction = ATN::INVALID_ALT_NUMBER; // make sure we use preds + } else { + // There are preds in configs but they might go away + // when OR'd together like {p}? || NONE == NONE. If neither + // alt has preds, resolve to min alt + dfaState->prediction = altsToCollectPredsFrom->nextSetBit(0); + } + } + + int ParserATNSimulator::execATNWithFullContext(dfa::DFA *dfa, dfa::DFAState *D, ATNConfigSet *s0, TokenStream *input, int startIndex, ParserRuleContext *outerContext) { + if (debug || debug_list_atn_decisions) { + std::cout << "execATNWithFullContext " << s0 << std::endl; + } + bool fullCtx = true; + bool foundExactAmbig = false; + ATNConfigSet *reach = nullptr; + ATNConfigSet *previous = s0; + input->seek(startIndex); + int t = input->LA(1); + int predictedAlt; + while (true) { // while more work + // System.out.println("LL REACH "+getLookaheadName(input)+ + // " from configs.size="+previous.size()+ + // " line "+input.LT(1).getLine()+":"+input.LT(1).getCharPositionInLine()); + reach = computeReachSet(previous, t, fullCtx); + if (reach == nullptr) { + // if any configs in previous dipped into outer context, that + // means that input up to t actually finished entry rule + // at least for LL decision. Full LL doesn't dip into outer + // so don't need special case. + // We will get an error no matter what so delay until after + // decision; better error message. Also, no reachable target + // ATN states in SLL implies LL will also get nowhere. + // If conflict in states that dip out, choose min since we + // will get error no matter what. + int alt = getAltThatFinishedDecisionEntryRule(previous); + if (alt != ATN::INVALID_ALT_NUMBER) { + return alt; + } + throw noViableAlt(input, outerContext, previous, startIndex); + } + + std::vector altSubSets =PredictionModeClass::getConflictingAltSubsets(reach); + if (debug) { + std::wstring altSubSetsStr = antlrcpp::BitSet::subStringRepresentation(altSubSets.begin(), altSubSets.end()); + std::wcout << L"LL altSubSets=" << altSubSetsStr << L", predict=" + << PredictionModeClass::getUniqueAlt(altSubSets) + << L", resolvesToJustOneViableAlt=" + << PredictionModeClass::resolvesToJustOneViableAlt(altSubSets) + << std::endl; + } + + // System.out.println("altSubSets: "+altSubSets); + reach->uniqueAlt = getUniqueAlt(reach); + // unique prediction? + if (reach->uniqueAlt != ATN::INVALID_ALT_NUMBER) { + predictedAlt = reach->uniqueAlt; + break; + } + if (mode != PredictionMode::LL_EXACT_AMBIG_DETECTION) { + predictedAlt = PredictionModeClass::resolvesToJustOneViableAlt(altSubSets); + if (predictedAlt != ATN::INVALID_ALT_NUMBER) { + break; + } + } else { + // In exact ambiguity mode, we never try to terminate early. + // Just keeps scarfing until we know what the conflict is + if (PredictionModeClass::allSubsetsConflict(altSubSets) && PredictionModeClass::allSubsetsEqual(altSubSets)) { + foundExactAmbig = true; + predictedAlt = PredictionModeClass::getSingleViableAlt(altSubSets); + break; + } + // else there are multiple non-conflicting subsets or + // we're not sure what the ambiguity is yet. + // So, keep going. + } + + previous = reach; + if (t != IntStream::_EOF) { + input->consume(); + t = input->LA(1); + } + } + + // If the configuration set uniquely predicts an alternative, + // without conflict, then we know that it's a full LL decision + // not SLL. + if (reach->uniqueAlt != ATN::INVALID_ALT_NUMBER) { + reportContextSensitivity(dfa, predictedAlt, reach, startIndex, input->index()); + return predictedAlt; + } + + // We do not check predicates here because we have checked them + // on-the-fly when doing full context prediction. + + /* + In non-exact ambiguity detection mode, we might actually be able to + detect an exact ambiguity, but I'm not going to spend the cycles + needed to check. We only emit ambiguity warnings in exact ambiguity + mode. + + For example, we might know that we have conflicting configurations. + But, that does not mean that there is no way forward without a + conflict. It's possible to have nonconflicting alt subsets as in: + + LL altSubSets=[{1, 2}, {1, 2}, {1}, {1, 2}] + + from + + [(17,1,[5 $]), (13,1,[5 10 $]), (21,1,[5 10 $]), (11,1,[$]), + (13,2,[5 10 $]), (21,2,[5 10 $]), (11,2,[$])] + + In this case, (17,1,[5 $]) indicates there is some next sequence that + would resolve this without conflict to alternative 1. Any other viable + next sequence, however, is associated with a conflict. We stop + looking for input because no amount of further lookahead will alter + the fact that we should predict alternative 1. We just can't say for + sure that there is an ambiguity without looking further. + */ + reportAmbiguity(dfa, D, startIndex, input->index(), foundExactAmbig, nullptr, reach); + + return predictedAlt; + } + + atn::ATNConfigSet *ParserATNSimulator::computeReachSet(ATNConfigSet *closure, int t, bool fullCtx) { + if (debug) { + std::wcout << L"in computeReachSet, starting closure: " << closure << std::endl; + } + + if (mergeCache == nullptr) { + mergeCache = new misc::DoubleKeyMap(); + } + + ATNConfigSet *intermediate = new ATNConfigSet(fullCtx); + + /* Configurations already in a rule stop state indicate reaching the end + * of the decision rule (local context) or end of the start rule (full + * context). Once reached, these configurations are never updated by a + * closure operation, so they are handled separately for the performance + * advantage of having a smaller intermediate set when calling closure. + * + * For full-context reach operations, separate handling is required to + * ensure that the alternative matching the longest overall sequence is + * chosen when multiple such configurations can match the input. + */ + std::vector skippedStopStates; + + // First figure out where we can reach on input t + for (auto c : *closure) { + if (debug) { + std::wcout << L"testing " << getTokenName(t) << L" at " << c->toString() << std::endl; + } + + if (dynamic_cast(c->state) != nullptr) { + if (c->context->isEmpty()) { + // Converted assert, shouldn't happen + throw new ASSERTException(L"ParserATNSimulator::computeReachSet", + L"c->context->isEmpty()"); + } + if (fullCtx || t == IntStream::_EOF) { + if (skippedStopStates.empty()) { + skippedStopStates = std::vector(); + } + + skippedStopStates.push_back(c); + } + + continue; + } + + int n = c->state->getNumberOfTransitions(); + for (int ti = 0; ti < n; ti++) { // for each transition + Transition *trans = c->state->transition(ti); + ATNState *target = getReachableTarget(trans, t); + if (target != nullptr) { + intermediate->add(new ATNConfig(c, target), mergeCache); + } + } + } + + // Now figure out where the reach operation can take us... + + ATNConfigSet *reach = nullptr; + + /* This block optimizes the reach operation for intermediate sets which + * trivially indicate a termination state for the overall + * adaptivePredict operation. + * + * The conditions assume that intermediate + * contains all configurations relevant to the reach set, but this + * condition is not true when one or more configurations have been + * withheld in skippedStopStates. + */ + if (skippedStopStates.empty()) { + if (intermediate->size() == 1) { + // Don't pursue the closure if there is just one state. + // It can only have one alternative; just add to result + // Also don't pursue the closure if there is unique alternative + // among the configurations. + reach = intermediate; + } else if (getUniqueAlt(intermediate) != ATN::INVALID_ALT_NUMBER) { + // Also don't pursue the closure if there is unique alternative + // among the configurations. + reach = intermediate; + } + } + + /* If the reach set could not be trivially determined, perform a closure + * operation on the intermediate set to compute its initial value. + */ + if (reach == nullptr) { + reach = new ATNConfigSet(fullCtx); + std::set *closureBusy = new std::set(); + + for (auto c : *intermediate) { + this->closure(c, reach, closureBusy, false, fullCtx); + } + } + + if (t == IntStream::_EOF) { + /* After consuming EOF no additional input is possible, so we are + * only interested in configurations which reached the end of the + * decision rule (local context) or end of the start rule (full + * context). Update reach to contain only these configurations. This + * handles both explicit EOF transitions in the grammar and implicit + * EOF transitions following the end of the decision or start rule. + * + * When reach==intermediate, no closure operation was performed. In + * this case, removeAllConfigsNotInRuleStopState needs to check for + * reachable rule stop states as well as configurations already in + * a rule stop state. + * + * This is handled before the configurations in skippedStopStates, + * because any configurations potentially added from that list are + * already guaranteed to meet this condition whether or not it's + * required. + */ + reach = removeAllConfigsNotInRuleStopState(reach, reach == intermediate); + } + + /* If skippedStopStates is not null, then it contains at least one + * configuration. For full-context reach operations, these + * configurations reached the end of the start rule, in which case we + * only add them back to reach if no configuration during the current + * closure operation reached such a state. This ensures adaptivePredict + * chooses an alternative matching the longest overall sequence when + * multiple alternatives are viable. + */ + if (skippedStopStates.size() > 0 && (!fullCtx || !PredictionModeClass::hasConfigInRuleStopState(reach))) { + if (!skippedStopStates.empty()) { + // Shouldn't happen, converted assert + throw new ASSERTException(L"ParserATNSimulator::computeReachSet", + L"skippedStopStates.size() > 0 && (!fullCtx || !PredictionModeClass::hasConfigInRuleStopState(reach)))"); + } + for (auto c : skippedStopStates) { + reach->add(c, mergeCache); + } + } + + if (reach->isEmpty()) { + return nullptr; + } + return reach; + } + + atn::ATNConfigSet *ParserATNSimulator::removeAllConfigsNotInRuleStopState(ATNConfigSet *configs, bool lookToEndOfRule) { + if (PredictionModeClass::allConfigsInRuleStopStates(configs)) { + return configs; + } + + ATNConfigSet *result = new ATNConfigSet(configs->fullCtx); + + for (auto config : *configs) { + if (dynamic_cast(config->state) != nullptr) { + result->add(config, mergeCache); + continue; + } + + if (lookToEndOfRule && config->state->onlyHasEpsilonTransitions()) { + misc::IntervalSet *nextTokens = atn->nextTokens(config->state); + if (nextTokens->contains(Token::EPSILON)) { + ATNState *endOfRuleState = atn->ruleToStopState[config->state->ruleIndex]; + result->add(new ATNConfig(config, endOfRuleState), mergeCache); + } + } + } + + return result; + } + + atn::ATNConfigSet *ParserATNSimulator::computeStartState(ATNState *p, RuleContext *ctx, bool fullCtx) { + // always at least the implicit call to start rule + PredictionContext *initialContext = PredictionContext::fromRuleContext(atn, ctx); + ATNConfigSet *configs = new ATNConfigSet(fullCtx); + + for (int i = 0; i < p->getNumberOfTransitions(); i++) { + ATNState *target = p->transition(i)->target; + ATNConfig *c = new ATNConfig(target, i + 1, initialContext); + std::set *closureBusy = new std::set(); + closure(c, configs, closureBusy, true, fullCtx); + } + + return configs; + } + + atn::ATNState *ParserATNSimulator::getReachableTarget(Transition *trans, int ttype) { + if (trans->matches(ttype, 0, atn->maxTokenType)) { + return trans->target; + } + + return nullptr; + } + + // + // Note that caller must memory manage the returned value from this function + std::vector ParserATNSimulator::getPredsForAmbigAlts(antlrcpp::BitSet *ambigAlts, ATNConfigSet *configs, int nalts) { + // REACH=[1|1|[]|0:0, 1|2|[]|0:1] + /* altToPred starts as an array of all null contexts. The entry at index i + * corresponds to alternative i. altToPred[i] may have one of three values: + * 1. null: no ATNConfig c is found such that c.alt==i + * 2. SemanticContext.NONE: At least one ATNConfig c exists such that + * c.alt==i and c.semanticContext==SemanticContext.NONE. In other words, + * alt i has at least one un-predicated config. + * 3. Non-NONE Semantic Context: There exists at least one, and for all + * ATNConfig c such that c.alt==i, c.semanticContext!=SemanticContext.NONE. + * + * From this, it is clear that NONE||anything==NONE. + */ + //SemanticContext *altToPred = new SemanticContext[nalts + 1]; + std::vector altToPred;// = new SemanticContext[nalts + 1]; + + for (auto c : *configs) { + if (ambigAlts->data.test(c->alt)) { + altToPred[c->alt] = dynamic_cast( (new SemanticContext::OR(altToPred[c->alt], c->semanticContext))); + } + } + + int nPredAlts = 0; + for (int i = 1; i <= nalts; i++) { + if (altToPred[i] == nullptr) { + altToPred[i] = SemanticContext::NONE; + } else if (altToPred[i] != SemanticContext::NONE) { + nPredAlts++; + } + } + + // // Optimize away p||p and p&&p TODO: optimize() was a no-op + // for (int i = 0; i < altToPred.length; i++) { + // altToPred[i] = altToPred[i].optimize(); + // } + + // nonambig alts are null in altToPred + if (nPredAlts == 0) { + altToPred.clear();// = nullptr; + } + if (debug) { + std::wcout << L"getPredsForAmbigAlts result "; /* TODO << Arrays->toString(altToPred) << std::endl; */ + } + return altToPred; + } + + std::vector ParserATNSimulator::getPredicatePredictions(antlrcpp::BitSet *ambigAlts, std::vector altToPred) { + std::vector pairs = std::vector(); + bool containsPredicate = false; + for (int i = 1; i < (int)altToPred.size(); i++) { + SemanticContext *pred = altToPred[i]; + + // unpredicted is indicated by SemanticContext.NONE + if(pred != nullptr) { + // Converted assert, shouldn't happen + throw new ASSERTException(L"arserATNSimulator::getPredicatePredictions", + L"pred != nullptr"); + } + + if (ambigAlts != nullptr && ambigAlts->data.test(i)) { + pairs.push_back(new dfa::DFAState::PredPrediction(pred, i)); + } + if (pred != SemanticContext::NONE) { + containsPredicate = true; + } + } + + if (!containsPredicate) { + pairs.clear(); + } + + return pairs; + } + + int ParserATNSimulator::getAltThatFinishedDecisionEntryRule(ATNConfigSet *configs) { + misc::IntervalSet *alts = nullptr; + for (auto c : *configs) { + if (c->reachesIntoOuterContext > 0 || (dynamic_cast(c->state) != nullptr && c->context->hasEmptyPath())) { + alts->add(c->alt); + } + } + if (alts->size() == 0) { + return ATN::INVALID_ALT_NUMBER; + } + return alts->getMinElement(); + } + + antlrcpp::BitSet *ParserATNSimulator::evalSemanticContext(std::vector predPredictions, ParserRuleContext *outerContext, bool complete) { + antlrcpp::BitSet *predictions = new antlrcpp::BitSet(); + for (auto pair : predPredictions) { + if (pair->pred == SemanticContext::NONE) { + predictions->set(pair->alt); + if (!complete) { + break; + } + continue; + } + + bool predicateEvaluationResult = pair->pred->eval(parser, outerContext); + if (debug || dfa_debug) { + std::wcout << L"eval pred " << pair << L"=" << predicateEvaluationResult << std::endl; + } + + if (predicateEvaluationResult) { + if (debug || dfa_debug) { + std::wcout << L"PREDICT " << pair->alt << std::endl; + } + predictions->set(pair->alt); + if (!complete) { + break; + } + } + } + + return predictions; + } + + void ParserATNSimulator::closure(ATNConfig *config, ATNConfigSet *configs, std::set *closureBusy, bool collectPredicates, bool fullCtx) { + const int initialDepth = 0; + closureCheckingStopState(config, configs, closureBusy, collectPredicates, fullCtx, initialDepth); + if (!fullCtx || !configs->dipsIntoOuterContext) { + // Converted assert, shouldn't happen + throw new ASSERTException(L"ParserATNSimulator::closure", + L"!fullCtx || !configs->dipsIntoOuterContext"); + } + } + + void ParserATNSimulator::closureCheckingStopState(ATNConfig *config, ATNConfigSet *configs, std::set *closureBusy, bool collectPredicates, bool fullCtx, int depth) { + if (debug) { + std::wcout << L"closure(" << config->toString(parser,true) << L")" << std::endl; + } + + if (dynamic_cast(config->state) != nullptr) { + // We hit rule end. If we have context info, use it + // run thru all possible stack tops in ctx + if (!config->context->isEmpty()) { + for (int i = 0; i < config->context->size(); i++) { + if (config->context->getReturnState(i) == PredictionContext::EMPTY_RETURN_STATE) { + if (fullCtx) { + configs->add(new ATNConfig(config, config->state, dynamic_cast(PredictionContext::EMPTY)), mergeCache); + continue; + } else { + // we have no context info, just chase follow links (if greedy) + if (debug) { + std::wcout << L"FALLING off rule " << getRuleName(config->state->ruleIndex) << std::endl; + } + closure_(config, configs, closureBusy, collectPredicates, fullCtx, depth); + } + continue; + } + ATNState *returnState = atn->states[config->context->getReturnState(i)]; + PredictionContext *newContext = config->context->getParent(i); // "pop" return state + ATNConfig *c = new ATNConfig(returnState, config->alt, newContext, config->semanticContext); + // While we have context to pop back from, we may have + // gotten that context AFTER having falling off a rule. + // Make sure we track that we are now out of context. + c->reachesIntoOuterContext = config->reachesIntoOuterContext; + if (depth > INT_MIN) { + // Converted assert, shouldn't happen + throw new ASSERTException(L"ParserATNSimulator::closureCheckingStopState", + L"depth > INT_MIN"); + } + closureCheckingStopState(c, configs, closureBusy, collectPredicates, fullCtx, depth - 1); + } + return; + } else if (fullCtx) { + // reached end of start rule + configs->add(config, mergeCache); + return; + } else { + // else if we have no context info, just chase follow links (if greedy) + if (debug) { + std::wcout << L"FALLING off rule " << getRuleName(config->state->ruleIndex) << std::endl; + } + } + } + + closure_(config, configs, closureBusy, collectPredicates, fullCtx, depth); + } + + void ParserATNSimulator::closure_(ATNConfig *config, ATNConfigSet *configs, std::set *closureBusy, bool collectPredicates, bool fullCtx, int depth) { + ATNState *p = config->state; + // optimization + if (!p->onlyHasEpsilonTransitions()) { + configs->add(config, mergeCache); + // if ( debug ) System.out.println("added config "+configs); + } + + for (int i = 0; i < p->getNumberOfTransitions(); i++) { + Transition *t = p->transition(i); + bool continueCollecting = !(dynamic_cast(t) != nullptr) && collectPredicates; + ATNConfig *c = getEpsilonTarget(config, t, continueCollecting, depth == 0, fullCtx); + if (c != nullptr) { + int newDepth = depth; + if (dynamic_cast(config->state) != nullptr) { + if(!fullCtx) { + // Converted assert, shouldn't happen + throw new ASSERTException(L"ParserATNSimulator::closure_", + L"!fullCtx"); + } + // target fell off end of rule; mark resulting c as having dipped into outer context + // We can't get here if incoming config was rule stop and we had context + // track how far we dip into outer context. Might + // come in handy and we avoid evaluating context dependent + // preds if this is > 0. + + if (!closureBusy->insert(c).second) { + // avoid infinite recursion for right-recursive rules + continue; + } + + c->reachesIntoOuterContext++; + configs->dipsIntoOuterContext = true; // TODO: can remove? only care when we add to set per middle of this method + if(newDepth > INT_MIN) { + // Converted assert, shouldn't happen + throw new ASSERTException(L"ParserATNSimulator::closure_", + L"newDepth > INT_MIN"); + } + newDepth--; + if (debug) { + std::wcout << L"dips into outer ctx: " << c << std::endl; + } + } else if (dynamic_cast(t) != nullptr) { + // latch when newDepth goes negative - once we step out of the entry context we can't return + if (newDepth >= 0) { + newDepth++; + } + } + + closureCheckingStopState(c, configs, closureBusy, continueCollecting, fullCtx, newDepth); + } + } + } + + std::wstring ParserATNSimulator::getRuleName(int index) { + if (parser != nullptr && index >= 0) { + return parser->getRuleNames()[index]; + } + return L""; + } + + atn::ATNConfig *ParserATNSimulator::getEpsilonTarget(ATNConfig *config, Transition *t, bool collectPredicates, bool inContext, bool fullCtx) { + switch (t->getSerializationType()) { + case Transition::RULE: + return ruleTransition(config, static_cast(t)); + + case Transition::PRECEDENCE: + return precedenceTransition(config, static_cast(t), collectPredicates, inContext, fullCtx); + + case Transition::PREDICATE: + return predTransition(config, static_cast(t), collectPredicates, inContext, fullCtx); + + case Transition::ACTION: + return actionTransition(config, static_cast(t)); + + case Transition::EPSILON: + return new ATNConfig(config, t->target); + + default: + return nullptr; + } + } + + atn::ATNConfig *ParserATNSimulator::actionTransition(ATNConfig *config, ActionTransition *t) { + if (debug) { + std::wcout << L"ACTION edge " << t->ruleIndex << L":" << t->actionIndex << std::endl; + } + return new ATNConfig(config, t->target); + } + + atn::ATNConfig *ParserATNSimulator::precedenceTransition(ATNConfig *config, PrecedencePredicateTransition *pt, bool collectPredicates, bool inContext, bool fullCtx) { + if (debug) { + std::wcout << L"PRED (collectPredicates=" << collectPredicates << L") " << pt->precedence << L">=_p" << L", ctx dependent=true" << std::endl; + if (parser != nullptr) { + std::wcout << L"context surrounding pred is " << antlrcpp::Arrays::ListToString( parser->getRuleInvocationStack(), L", ") << std::endl; + } + } + + ATNConfig *c = nullptr; + if (collectPredicates && inContext) { + if (fullCtx) { + // In full context mode, we can evaluate predicates on-the-fly + // during closure, which dramatically reduces the size of + // the config sets. It also obviates the need to test predicates + // later during conflict resolution. + int currentPosition = _input->index(); + _input->seek(_startIndex); + bool predSucceeds = pt->getPredicate()->eval(parser, _outerContext); + _input->seek(currentPosition); + if (predSucceeds) { + c = new ATNConfig(config, pt->target); // no pred context + } + } else { + SemanticContext *newSemCtx = new SemanticContext::AND(config->semanticContext, pt->getPredicate()); + c = new ATNConfig(config, pt->target, newSemCtx); + } + } else { + c = new ATNConfig(config, pt->target); + } + + if (debug) { + std::wcout << L"config from pred transition=" << c << std::endl; + } + return c; + } + + atn::ATNConfig *ParserATNSimulator::predTransition(ATNConfig *config, PredicateTransition *pt, bool collectPredicates, bool inContext, bool fullCtx) { + if (debug) { + std::wcout << L"PRED (collectPredicates=" << collectPredicates << L") " << pt->ruleIndex << L":" << pt->predIndex << L", ctx dependent=" << pt->isCtxDependent << std::endl; + if (parser != nullptr) { + std::wcout << L"context surrounding pred is " << antlrcpp::Arrays::ListToString(parser->getRuleInvocationStack(), L", ") << std::endl; + } + } + + ATNConfig *c = nullptr; + if (collectPredicates && (!pt->isCtxDependent || (pt->isCtxDependent && inContext))) { + if (fullCtx) { + // In full context mode, we can evaluate predicates on-the-fly + // during closure, which dramatically reduces the size of + // the config sets. It also obviates the need to test predicates + // later during conflict resolution. + int currentPosition = _input->index(); + _input->seek(_startIndex); + bool predSucceeds = pt->getPredicate()->eval(parser, _outerContext); + _input->seek(currentPosition); + if (predSucceeds) { + c = new ATNConfig(config, pt->target); // no pred context + } + } else { + SemanticContext *newSemCtx = dynamic_cast(new SemanticContext::AND(config->semanticContext, pt->getPredicate())); + c = new ATNConfig(config, pt->target, newSemCtx); + } + } else { + c = new ATNConfig(config, pt->target); + } + + if (debug) { + std::wcout << L"config from pred transition=" << c << std::endl; + } + return c; + } + + atn::ATNConfig *ParserATNSimulator::ruleTransition(ATNConfig *config, RuleTransition *t) { + if (debug) { + std::wcout << L"CALL rule " << getRuleName(t->target->ruleIndex) << L", ctx=" << config->context << std::endl; + } + + atn::ATNState *returnState = t->followState; + PredictionContext *newContext = SingletonPredictionContext::create(config->context, returnState->stateNumber); + return new atn::ATNConfig(config, t->target, newContext); + } + + antlrcpp::BitSet ParserATNSimulator::getConflictingAlts(ATNConfigSet *configs) { + std::vector altsets = PredictionModeClass::getConflictingAltSubsets(configs); + return PredictionModeClass::getAlts(altsets); + } + + antlrcpp::BitSet ParserATNSimulator::getConflictingAltsOrUniqueAlt(ATNConfigSet *configs) { + antlrcpp::BitSet conflictingAlts; + if (configs->uniqueAlt != ATN::INVALID_ALT_NUMBER) { + conflictingAlts.set(configs->uniqueAlt); + } else { + conflictingAlts = *configs->conflictingAlts; + } + return conflictingAlts; + } + + std::wstring ParserATNSimulator::getTokenName(int t) { + if (t == Token::_EOF) { + return L"EOF"; + } + if (parser != nullptr) { + std::vector tokensNames = parser->getTokenNames(); + if (t >= (int)tokensNames.size()) { + std::wcerr << t << L" type out of range: " << antlrcpp::Arrays::ListToString(tokensNames, L", "); + // TODO +// std::wcerr << ((CommonTokenStream*)parser->getInputStream())->getTokens(); + } else { + return tokensNames[t] + L"<" + std::to_wstring(t) + L">"; + } + } + return antlrcpp::StringConverterHelper::toString(t); + } + + std::wstring ParserATNSimulator::getLookaheadName(TokenStream *input) { + return getTokenName(input->LA(1)); + } + + void ParserATNSimulator::dumpDeadEndConfigs(NoViableAltException *nvae) { + std::wcerr << L"dead end configs: "; + for (auto c : *nvae->getDeadEndConfigs()) { + std::wstring trans = L"no edges"; + if (c->state->getNumberOfTransitions() > 0) { + Transition *t = c->state->transition(0); + if (dynamic_cast(t) != nullptr) { + AtomTransition *at = static_cast(t); + trans = L"Atom " + getTokenName(at->_label); + } else if (dynamic_cast(t) != nullptr) { + SetTransition *st = static_cast(t); + bool is_not = dynamic_cast(st) != nullptr; + trans = (is_not ? L"~" : L""); + trans += L"Set "; + trans += st->set->toString(); + } + } + std::wcerr << c->toString(parser, true) + L":" + trans; + } + } + + runtime::NoViableAltException *ParserATNSimulator::noViableAlt(TokenStream *input, ParserRuleContext *outerContext, ATNConfigSet *configs, int startIndex) { + return new NoViableAltException(parser, input, input->get(startIndex), input->LT(1), configs, outerContext); + } + + int ParserATNSimulator::getUniqueAlt(ATNConfigSet *configs) { + int alt = ATN::INVALID_ALT_NUMBER; + for (auto c : *configs) { + if (alt == ATN::INVALID_ALT_NUMBER) { + alt = c->alt; // found first alt + } else if (c->alt != alt) { + return ATN::INVALID_ALT_NUMBER; + } + } + return alt; + } + + dfa::DFAState *ParserATNSimulator::addDFAEdge(dfa::DFA *dfa, dfa::DFAState *from, int t, dfa::DFAState *to) { + if (debug) { + std::wcout << L"EDGE " << from << L" -> " << to << L" upon " << getTokenName(t) << std::endl; + } + + if (to == nullptr) { + return nullptr; + } + + to = addDFAState(dfa, to); // used existing if possible not incoming + if (from == nullptr || t < -1 || t > atn->maxTokenType) { + return to; + } + + + if (true) { + std::lock_guard lck(mtx); + if (from->edges.size() == 0) { + from->edges = std::vector(); + } + + from->edges[t + 1] = to; // connect + } + + if (debug) { + std::vector names; + if (parser != nullptr) { + names = parser->getTokenNames(); + } + std::wcout << L"DFA=\n" << dfa->toString(names) << std::endl; + } + + return to; + } + + dfa::DFAState *ParserATNSimulator::addDFAState(dfa::DFA *dfa, dfa::DFAState *D) { + if (D == ERROR) { + return D; + } + + if (true) { + std::lock_guard lck(mtx); + dfa::DFAState *existing = dfa->states->at(D); + if (existing != nullptr) { + return existing; + } + + D->stateNumber = (int)dfa->states->size(); + if (!D->configs->isReadonly()) { + D->configs->optimizeConfigs(this); + D->configs->setReadonly(true); + } + dfa->states->insert(std::pair(D, D)); + if (debug) { + std::cout << L"adding new DFA state: " << D << std::endl; + } + return D; + } + } + + void ParserATNSimulator::reportAttemptingFullContext(dfa::DFA *dfa, antlrcpp::BitSet *conflictingAlts, ATNConfigSet *configs, int startIndex, int stopIndex) { + if (debug || retry_debug) { + misc::Interval *interval = misc::Interval::of(startIndex, stopIndex); + std::wcout << L"reportAttemptingFullContext decision=" << dfa->decision << L":" << configs << L", input=" << parser->getTokenStream()->getText(interval) << std::endl; + } + if (parser != nullptr) { + parser->getErrorListenerDispatch()->reportAttemptingFullContext(parser, dfa, startIndex, stopIndex, conflictingAlts, configs); + } + } + + void ParserATNSimulator::reportContextSensitivity(dfa::DFA *dfa, int prediction, ATNConfigSet *configs, int startIndex, int stopIndex) { + if (debug || retry_debug) { + misc::Interval *interval = misc::Interval::of(startIndex, stopIndex); + std::wcout << L"reportContextSensitivity decision=" << dfa->decision << L":" << configs << L", input=" << parser->getTokenStream()->getText(interval) << std::endl; + } + if (parser != nullptr) { + parser->getErrorListenerDispatch()->reportContextSensitivity(parser, dfa, startIndex, stopIndex, prediction, configs); + } + } + + void ParserATNSimulator::reportAmbiguity(dfa::DFA *dfa, dfa::DFAState *D, int startIndex, int stopIndex, bool exact, antlrcpp::BitSet *ambigAlts, ATNConfigSet *configs) { + if (debug || retry_debug) { + // ParserATNPathFinder finder = new ParserATNPathFinder(parser, atn); + // int i = 1; + // for (Transition t : dfa.atnStartState.transitions) { + // System.out.println("ALT "+i+"="); + // System.out.println(startIndex+".."+stopIndex+", len(input)="+parser.getInputStream().size()); + // TraceTree path = finder.trace(t.target, parser.getContext(), (TokenStream)parser.getInputStream(), + // startIndex, stopIndex); + // if ( path!=null ) { + // System.out.println("path = "+path.toStringTree()); + // for (TraceTree leaf : path.leaves) { + // List states = path.getPathToNode(leaf); + // System.out.println("states="+states); + // } + // } + // i++; + // } + misc::Interval *interval = misc::Interval::of(startIndex, stopIndex); + std::wcout << L"reportAmbiguity " << ambigAlts << L":" << configs << L", input=" << parser->getTokenStream()->getText(interval) << std::endl; + } + if (parser != nullptr) { + parser->getErrorListenerDispatch()->reportAmbiguity(parser, dfa, startIndex, stopIndex, exact, ambigAlts, configs); + } + } + + void ParserATNSimulator::setPredictionMode(PredictionMode mode) { + this->mode = mode; + } + + atn::PredictionMode ParserATNSimulator::getPredictionMode() { + return mode; + } + + void ParserATNSimulator::InitializeInstanceFields() { + mode = PredictionMode::LL; + _startIndex = 0; + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/ParserATNSimulator.h b/runtime/Cpp/org/antlr/v4/runtime/atn/ParserATNSimulator.h new file mode 100755 index 000000000..094e89309 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/ParserATNSimulator.h @@ -0,0 +1,562 @@ +#pragma once + +#include +#include +#include +#include +#include + +#include "ATNSimulator.h" +#include "PredictionMode.h" +#include "DFAState.h" +#include "stringconverter.h" +#include "Declarations.h" +#include "Arrays.h" +#include "ActionTransition.h" +#include "PrecedencePredicateTransition.h" +#include "PredicateTransition.h" +#include "RuleTransition.h" +#include "SingletonPredictionContext.h" +#include "AtomTransition.h" +#include "SetTransition.h" +#include "NotSetTransition.h" +#include "EmptyPredictionContext.h" +#include "CommonTokenStream.h" +#include "Parser.h" + + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + /// + /// The embodiment of the adaptive LL(*), ALL(*), parsing strategy. + /// + /// The basic complexity of the adaptive strategy makes it harder to + /// understand. We begin with ATN simulation to build paths in a + /// DFA. Subsequent prediction requests go through the DFA first. If + /// they reach a state without an edge for the current symbol, the + /// algorithm fails over to the ATN simulation to complete the DFA + /// path for the current input (until it finds a conflict state or + /// uniquely predicting state). + /// + /// All of that is done without using the outer context because we + /// want to create a DFA that is not dependent upon the rule + /// invocation stack when we do a prediction. One DFA works in all + /// contexts. We avoid using context not necessarily because it's + /// slower, although it can be, but because of the DFA caching + /// problem. The closure routine only considers the rule invocation + /// stack created during prediction beginning in the decision rule. For + /// example, if prediction occurs without invoking another rule's + /// ATN, there are no context stacks in the configurations. + /// When lack of context leads to a conflict, we don't know if it's + /// an ambiguity or a weakness in the strong LL(*) parsing strategy + /// (versus full LL(*)). + /// + /// When SLL yields a configuration set with conflict, we rewind the + /// input and retry the ATN simulation, this time using + /// full outer context without adding to the DFA. Configuration context + /// stacks will be the full invocation stacks from the start rule. If + /// we get a conflict using full context, then we can definitively + /// say we have a true ambiguity for that input sequence. If we don't + /// get a conflict, it implies that the decision is sensitive to the + /// outer context. (It is not context-sensitive in the sense of + /// context-sensitive grammars.) + /// + /// The next time we reach this DFA state with an SLL conflict, through + /// DFA simulation, we will again retry the ATN simulation using full + /// context mode. This is slow because we can't save the results and have + /// to "interpret" the ATN each time we get that input. + /// + /// CACHING FULL CONTEXT PREDICTIONS + /// + /// We could cache results from full context to predicted + /// alternative easily and that saves a lot of time but doesn't work + /// in presence of predicates. The set of visible predicates from + /// the ATN start state changes depending on the context, because + /// closure can fall off the end of a rule. I tried to cache + /// tuples (stack context, semantic context, predicted alt) but it + /// was slower than interpreting and much more complicated. Also + /// required a huge amount of memory. The goal is not to create the + /// world's fastest parser anyway. I'd like to keep this algorithm + /// simple. By launching multiple threads, we can improve the speed + /// of parsing across a large number of files. + /// + /// There is no strict ordering between the amount of input used by + /// SLL vs LL, which makes it really hard to build a cache for full + /// context. Let's say that we have input A B C that leads to an SLL + /// conflict with full context X. That implies that using X we + /// might only use A B but we could also use A B C D to resolve + /// conflict. Input A B C D could predict alternative 1 in one + /// position in the input and A B C E could predict alternative 2 in + /// another position in input. The conflicting SLL configurations + /// could still be non-unique in the full context prediction, which + /// would lead us to requiring more input than the original A B C. To + /// make a prediction cache work, we have to track the exact input used + /// during the previous prediction. That amounts to a cache that maps X + /// to a specific DFA for that context. + /// + /// Something should be done for left-recursive expression predictions. + /// They are likely LL(1) + pred eval. Easier to do the whole SLL unless + /// error and retry with full LL thing Sam does. + /// + /// AVOIDING FULL CONTEXT PREDICTION + /// + /// We avoid doing full context retry when the outer context is empty, + /// we did not dip into the outer context by falling off the end of the + /// decision state rule, or when we force SLL mode. + /// + /// As an example of the not dip into outer context case, consider + /// as super constructor calls versus function calls. One grammar + /// might look like this: + /// + /// ctorBody : '{' superCall? stat* '}' ; + /// + /// Or, you might see something like + /// + /// stat : superCall ';' | expression ';' | ... ; + /// + /// In both cases I believe that no closure operations will dip into the + /// outer context. In the first case ctorBody in the worst case will stop + /// at the '}'. In the 2nd case it should stop at the ';'. Both cases + /// should stay within the entry rule and not dip into the outer context. + /// + /// PREDICATES + /// + /// Predicates are always evaluated if present in either SLL or LL both. + /// SLL and LL simulation deals with predicates differently. SLL collects + /// predicates as it performs closure operations like ANTLR v3 did. It + /// delays predicate evaluation until it reaches and accept state. This + /// allows us to cache the SLL ATN simulation whereas, if we had evaluated + /// predicates on-the-fly during closure, the DFA state configuration sets + /// would be different and we couldn't build up a suitable DFA. + /// + /// When building a DFA accept state during ATN simulation, we evaluate + /// any predicates and return the sole semantically valid alternative. If + /// there is more than 1 alternative, we report an ambiguity. If there are + /// 0 alternatives, we throw an exception. Alternatives without predicates + /// act like they have true predicates. The simple way to think about it + /// is to strip away all alternatives with false predicates and choose the + /// minimum alternative that remains. + /// + /// When we start in the DFA and reach an accept state that's predicated, + /// we test those and return the minimum semantically viable + /// alternative. If no alternatives are viable, we throw an exception. + /// + /// During full LL ATN simulation, closure always evaluates predicates and + /// on-the-fly. This is crucial to reducing the configuration set size + /// during closure. It hits a landmine when parsing with the Java grammar, + /// for example, without this on-the-fly evaluation. + /// + /// SHARING DFA + /// + /// All instances of the same parser share the same decision DFAs through + /// a static field. Each instance gets its own ATN simulator but they + /// share the same decisionToDFA field. They also share a + /// PredictionContextCache object that makes sure that all + /// PredictionContext objects are shared among the DFA states. This makes + /// a big size difference. + /// + /// THREAD SAFETY + /// + /// The parser ATN simulator locks on the decisionDFA field when it adds a + /// new DFA object to that array. addDFAEdge locks on the DFA for the + /// current decision when setting the edges[] field. addDFAState locks on + /// the DFA for the current decision when looking up a DFA state to see if + /// it already exists. We must make sure that all requests to add DFA + /// states that are equivalent result in the same shared DFA object. This + /// is because lots of threads will be trying to update the DFA at + /// once. The addDFAState method also locks inside the DFA lock but this + /// time on the shared context cache when it rebuilds the configurations' + /// PredictionContext objects using cached subgraphs/nodes. No other + /// locking occurs, even during DFA simulation. This is safe as long as we + /// can guarantee that all threads referencing s.edge[t] get the same + /// physical target DFA state, or none. Once into the DFA, the DFA + /// simulation does not reference the dfa.state map. It follows the + /// edges[] field to new targets. The DFA simulator will either find + /// dfa.edges to be null, to be non-null and dfa.edges[t] null, or + /// dfa.edges[t] to be non-null. The addDFAEdge method could be racing to + /// set the field but in either case the DFA simulator works; if null, and + /// requests ATN simulation. It could also race trying to get + /// dfa.edges[t], but either way it will work because it's not doing a + /// test and set operation. + /// + /// Starting with SLL then failing to combined SLL/LL + /// + /// Sam pointed out that if SLL does not give a syntax error, then there + /// is no point in doing full LL, which is slower. We only have to try LL + /// if we get a syntax error. For maximum speed, Sam starts the parser + /// with pure SLL mode: + /// + /// parser.getInterpreter().setSLL(true); + /// + /// and with the bail error strategy: + /// + /// parser.setErrorHandler(new BailErrorStrategy()); + /// + /// If it does not get a syntax error, then we're done. If it does get a + /// syntax error, we need to retry with the combined SLL/LL strategy. + /// + /// The reason this works is as follows. If there are no SLL + /// conflicts then the grammar is SLL for sure, at least for that + /// input set. If there is an SLL conflict, the full LL analysis + /// must yield a set of ambiguous alternatives that is no larger + /// than the SLL set. If the LL set is a singleton, then the grammar + /// is LL but not SLL. If the LL set is the same size as the SLL + /// set, the decision is SLL. If the LL set has size > 1, then that + /// decision is truly ambiguous on the current input. If the LL set + /// is smaller, then the SLL conflict resolution might choose an + /// alternative that the full LL would rule out as a possibility + /// based upon better context information. If that's the case, then + /// the SLL parse will definitely get an error because the full LL + /// analysis says it's not viable. If SLL conflict resolution + /// chooses an alternative within the LL set, them both SLL and LL + /// would choose the same alternative because they both choose the + /// minimum of multiple conflicting alternatives. + /// + /// Let's say we have a set of SLL conflicting alternatives {1, 2, 3} and + /// a smaller LL set called s. If s is {2, 3}, then SLL parsing will get + /// an error because SLL will pursue alternative 1. If s is {1, 2} or {1, + /// 3} then both SLL and LL will choose the same alternative because + /// alternative one is the minimum of either set. If s is {2} or {3} then + /// SLL will get a syntax error. If s is {1} then SLL will succeed. + /// + /// Of course, if the input is invalid, then we will get an error for sure + /// in both SLL and LL parsing. Erroneous input will therefore require 2 + /// passes over the input. + /// + /// + class ParserATNSimulator : public ATNSimulator { + public: + static const bool debug = false; + static const bool debug_list_atn_decisions = false; + static const bool dfa_debug = false; + static const bool retry_debug = false; + + protected: + Parser *const parser; + + public: + std::vector _decisionToDFA; + + /// + /// SLL, LL, or LL + exact ambig detection? + private: + PredictionMode mode; + //Mutex to manage synchronized access for multithreading in the parser atn simulator + std::mutex mtx; + + /// + /// Each prediction operation uses a cache for merge of prediction contexts. + /// Don't keep around as it wastes huge amounts of memory. DoubleKeyMap + /// isn't synchronized but we're ok since two threads shouldn't reuse same + /// parser/atnsim object because it can only handle one input at a time. + /// This maps graphs a and b to merged result c. (a,b)->c. We can avoid + /// the merge if we ever see a and b again. Note that (b,a)->c should + /// also be examined during cache lookup. + /// + protected: + misc::DoubleKeyMap *mergeCache; + + // LAME globals to avoid parameters!!!!! I need these down deep in predTransition + TokenStream *_input; + int _startIndex; + ParserRuleContext *_outerContext; + + /// + /// Testing only! + public: + ParserATNSimulator(ATN *atn, const std::vector& decisionToDFA, PredictionContextCache *sharedContextCache); //this(nullptr, atn, decisionToDFA, sharedContextCache); + + ParserATNSimulator(Parser *parser, ATN *atn, const std::vector& decisionToDFA, PredictionContextCache *sharedContextCache); + + virtual void reset() override; + + virtual int adaptivePredict(TokenStream *input, int decision, ParserRuleContext *outerContext); + + /// + /// Performs ATN simulation to compute a predicted alternative based + /// upon the remaining input, but also updates the DFA cache to avoid + /// having to traverse the ATN again for the same input sequence. + /// + /// There are some key conditions we're looking for after computing a new + /// set of ATN configs (proposed DFA state): + /// if the set is empty, there is no viable alternative for current symbol + /// does the state uniquely predict an alternative? + /// does the state have a conflict that would prevent us from + /// putting it on the work list? + /// + /// We also have some key operations to do: + /// add an edge from previous DFA state to potentially new DFA state, D, + /// upon current symbol but only if adding to work list, which means in all + /// cases except no viable alternative (and possibly non-greedy decisions?) + /// collecting predicates and adding semantic context to DFA accept states + /// adding rule context to context-sensitive DFA accept states + /// consuming an input symbol + /// reporting a conflict + /// reporting an ambiguity + /// reporting a context sensitivity + /// reporting insufficient predicates + /// + /// cover these cases: + /// dead end + /// single alt + /// single alt + preds + /// conflict + /// conflict + preds + /// + protected: + virtual int execATN(dfa::DFA *dfa, dfa::DFAState *s0, TokenStream *input, int startIndex, ParserRuleContext *outerContext); + + /// + /// Get an existing target state for an edge in the DFA. If the target state + /// for the edge has not yet been computed or is otherwise not available, + /// this method returns {@code null}. + /// + /// The current DFA state + /// The next input symbol + /// The existing target DFA state for the given input symbol + /// {@code t}, or {@code null} if the target state for this edge is not + /// already cached + virtual dfa::DFAState *getExistingTargetState(dfa::DFAState *previousD, int t); + + /// + /// Compute a target state for an edge in the DFA, and attempt to add the + /// computed state and corresponding edge to the DFA. + /// + /// The DFA + /// The current DFA state + /// The next input symbol + /// + /// The computed target DFA state for the given input symbol + /// {@code t}. If {@code t} does not lead to a valid DFA state, this method + /// returns . + virtual dfa::DFAState *computeTargetState(dfa::DFA *dfa, dfa::DFAState *previousD, int t); + + virtual void predicateDFAState(dfa::DFAState *dfaState, DecisionState *decisionState); + + // comes back with reach.uniqueAlt set to a valid alt + virtual int execATNWithFullContext(dfa::DFA *dfa, dfa::DFAState *D, ATNConfigSet *s0, TokenStream *input, int startIndex, ParserRuleContext *outerContext); // how far we got before failing over + + virtual ATNConfigSet *computeReachSet(ATNConfigSet *closure, int t, bool fullCtx); + + /// + /// Return a configuration set containing only the configurations from + /// {@code configs} which are in a . If all + /// configurations in {@code configs} are already in a rule stop state, this + /// method simply returns {@code configs}. + ///

+ /// When {@code lookToEndOfRule} is true, this method uses + /// for each configuration in {@code configs} which is + /// not already in a rule stop state to see if a rule stop state is reachable + /// from the configuration via epsilon-only transitions. + ///

+ /// the configuration set to update + /// when true, this method checks for rule stop states + /// reachable by epsilon-only transitions from each configuration in + /// {@code configs}. + /// + /// {@code configs} if all configurations in {@code configs} are in a + /// rule stop state, otherwise return a new configuration set containing only + /// the configurations from {@code configs} which are in a rule stop state + virtual ATNConfigSet *removeAllConfigsNotInRuleStopState(ATNConfigSet *configs, bool lookToEndOfRule); + + virtual ATNConfigSet *computeStartState(ATNState *p, RuleContext *ctx, bool fullCtx); + + virtual ATNState *getReachableTarget(Transition *trans, int ttype); + + virtual std::vector getPredsForAmbigAlts(antlrcpp::BitSet *ambigAlts, ATNConfigSet *configs, int nalts); + + virtual std::vector getPredicatePredictions(antlrcpp::BitSet *ambigAlts, std::vector altToPred); + + virtual int getAltThatFinishedDecisionEntryRule(ATNConfigSet *configs); + + /// + /// Look through a list of predicate/alt pairs, returning alts for the + /// pairs that win. A {@code NONE} predicate indicates an alt containing an + /// unpredicated config which behaves as "always true." If !complete + /// then we stop at the first predicate that evaluates to true. This + /// includes pairs with null predicates. + /// + virtual antlrcpp::BitSet *evalSemanticContext(std::vector predPredictions, ParserRuleContext *outerContext, bool complete); + + + /* TODO: If we are doing predicates, there is no point in pursuing + closure operations if we reach a DFA state that uniquely predicts + alternative. We will not be caching that DFA state and it is a + waste to pursue the closure. Might have to advance when we do + ambig detection thought :( + */ + + virtual void closure(ATNConfig *config, ATNConfigSet *configs, std::set *closureBusy, bool collectPredicates, bool fullCtx); + + virtual void closureCheckingStopState(ATNConfig *config, ATNConfigSet *configs, std::set *closureBusy, bool collectPredicates, bool fullCtx, int depth); + + /// + /// Do the actual work of walking epsilon edges + virtual void closure_(ATNConfig *config, ATNConfigSet *configs, std::set *closureBusy, bool collectPredicates, bool fullCtx, int depth); + + public: + virtual std::wstring getRuleName(int index); + + protected: + virtual ATNConfig *getEpsilonTarget(ATNConfig *config, Transition *t, bool collectPredicates, bool inContext, bool fullCtx); + + virtual ATNConfig *actionTransition(ATNConfig *config, ActionTransition *t); + + public: + virtual ATNConfig *precedenceTransition(ATNConfig *config, PrecedencePredicateTransition *pt, bool collectPredicates, bool inContext, bool fullCtx); + + protected: + virtual ATNConfig *predTransition(ATNConfig *config, PredicateTransition *pt, bool collectPredicates, bool inContext, bool fullCtx); + + virtual ATNConfig *ruleTransition(ATNConfig *config, RuleTransition *t); + + virtual antlrcpp::BitSet getConflictingAlts(ATNConfigSet *configs); + + /// + /// Sam pointed out a problem with the previous definition, v3, of + /// ambiguous states. If we have another state associated with conflicting + /// alternatives, we should keep going. For example, the following grammar + /// + /// s : (ID | ID ID?) ';' ; + /// + /// When the ATN simulation reaches the state before ';', it has a DFA + /// state that looks like: [12|1|[], 6|2|[], 12|2|[]]. Naturally + /// 12|1|[] and 12|2|[] conflict, but we cannot stop processing this node + /// because alternative to has another way to continue, via [6|2|[]]. + /// The key is that we have a single state that has config's only associated + /// with a single alternative, 2, and crucially the state transitions + /// among the configurations are all non-epsilon transitions. That means + /// we don't consider any conflicts that include alternative 2. So, we + /// ignore the conflict between alts 1 and 2. We ignore a set of + /// conflicting alts when there is an intersection with an alternative + /// associated with a single alt state in the state->config-list map. + /// + /// It's also the case that we might have two conflicting configurations but + /// also a 3rd nonconflicting configuration for a different alternative: + /// [1|1|[], 1|2|[], 8|3|[]]. This can come about from grammar: + /// + /// a : A | A | A B ; + /// + /// After matching input A, we reach the stop state for rule A, state 1. + /// State 8 is the state right before B. Clearly alternatives 1 and 2 + /// conflict and no amount of further lookahead will separate the two. + /// However, alternative 3 will be able to continue and so we do not + /// stop working on this state. In the previous example, we're concerned + /// with states associated with the conflicting alternatives. Here alt + /// 3 is not associated with the conflicting configs, but since we can continue + /// looking for input reasonably, I don't declare the state done. We + /// ignore a set of conflicting alts when we have an alternative + /// that we still need to pursue. + /// + + virtual antlrcpp::BitSet getConflictingAltsOrUniqueAlt(ATNConfigSet *configs); + + public: + virtual std::wstring getTokenName(int t); + + virtual std::wstring getLookaheadName(TokenStream *input); + + /// + /// Used for debugging in adaptivePredict around execATN but I cut + /// it out for clarity now that alg. works well. We can leave this + /// "dead" code for a bit. + /// + virtual void dumpDeadEndConfigs(NoViableAltException *nvae); + + protected: + virtual NoViableAltException *noViableAlt(TokenStream *input, ParserRuleContext *outerContext, ATNConfigSet *configs, int startIndex); + + static int getUniqueAlt(ATNConfigSet *configs); + + /// + /// Add an edge to the DFA, if possible. This method calls + /// to ensure the {@code to} state is present in the + /// DFA. If {@code from} is {@code null}, or if {@code t} is outside the + /// range of edges that can be represented in the DFA tables, this method + /// returns without adding the edge to the DFA. + ///

+ /// If {@code to} is {@code null}, this method returns {@code null}. + /// Otherwise, this method returns the returned by calling + /// for the {@code to} state. + ///

+ /// The DFA + /// The source state for the edge + /// The input symbol + /// The target state for the edge + /// + /// If {@code to} is {@code null}, this method returns {@code null}; + /// otherwise this method returns the result of calling + /// on {@code to} + virtual dfa::DFAState *addDFAEdge(dfa::DFA *dfa, dfa::DFAState *from, int t, dfa::DFAState *to); + + /// + /// Add state {@code D} to the DFA if it is not already present, and return + /// the actual instance stored in the DFA. If a state equivalent to {@code D} + /// is already in the DFA, the existing state is returned. Otherwise this + /// method returns {@code D} after adding it to the DFA. + ///

+ /// If {@code D} is , this method returns and + /// does not change the DFA. + ///

+ /// The dfa + /// The DFA state to add + /// The state stored in the DFA. This will be either the existing + /// state if {@code D} is already in the DFA, or {@code D} itself if the + /// state was not already present. + virtual dfa::DFAState *addDFAState(dfa::DFA *dfa, dfa::DFAState *D); + + virtual void reportAttemptingFullContext(dfa::DFA *dfa, antlrcpp::BitSet *conflictingAlts, ATNConfigSet *configs, int startIndex, int stopIndex); + + virtual void reportContextSensitivity(dfa::DFA *dfa, int prediction, ATNConfigSet *configs, int startIndex, int stopIndex); + + /// + /// If context sensitive parsing, we know it's ambiguity not conflict + virtual void reportAmbiguity(dfa::DFA *dfa, dfa::DFAState *D, int startIndex, int stopIndex, bool exact, antlrcpp::BitSet *ambigAlts, ATNConfigSet *configs); + + public: + void setPredictionMode(PredictionMode mode); + + PredictionMode getPredictionMode(); + + private: + void InitializeInstanceFields(); + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/PlusBlockStartState.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/PlusBlockStartState.cpp new file mode 100755 index 000000000..affcd8d53 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/PlusBlockStartState.cpp @@ -0,0 +1,46 @@ +#include "PlusBlockStartState.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + int PlusBlockStartState::getStateType() { + return PLUS_BLOCK_START; + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/PlusBlockStartState.h b/runtime/Cpp/org/antlr/v4/runtime/atn/PlusBlockStartState.h new file mode 100755 index 000000000..51e8155dd --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/PlusBlockStartState.h @@ -0,0 +1,59 @@ +#pragma once + +#include "BlockStartState.h" +#include "PlusLoopbackState.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + /// + /// Start of {@code (A|B|...)+} loop. Technically a decision state, but + /// we don't use for code generation; somebody might need it, so I'm defining + /// it for completeness. In reality, the node is the + /// real decision-making note for {@code A+}. + /// + class PlusBlockStartState final : public BlockStartState { + public: + PlusLoopbackState *loopBackState; + + virtual int getStateType() override; + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/PlusLoopbackState.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/PlusLoopbackState.cpp new file mode 100755 index 000000000..eacca2bd0 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/PlusLoopbackState.cpp @@ -0,0 +1,46 @@ +#include "PlusLoopbackState.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + int PlusLoopbackState::getStateType() { + return PLUS_LOOP_BACK; + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/PlusLoopbackState.h b/runtime/Cpp/org/antlr/v4/runtime/atn/PlusLoopbackState.h new file mode 100755 index 000000000..fe74c79a5 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/PlusLoopbackState.h @@ -0,0 +1,55 @@ +#pragma once + +#include "DecisionState.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + /// + /// Decision state for {@code A+} and {@code (A|B)+}. It has two transitions: + /// one to the loop back to start of the block and one to exit. + /// + class PlusLoopbackState final : public DecisionState { + + public: + virtual int getStateType() override; + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/PrecedencePredicateTransition.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/PrecedencePredicateTransition.cpp new file mode 100755 index 000000000..a057bb290 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/PrecedencePredicateTransition.cpp @@ -0,0 +1,66 @@ +#include "PrecedencePredicateTransition.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + using org::antlr::v4::runtime::misc::NotNull; + + PrecedencePredicateTransition::PrecedencePredicateTransition(ATNState *target, int precedence) : AbstractPredicateTransition(target), precedence(precedence) { + } + + int PrecedencePredicateTransition::getSerializationType() { + return PRECEDENCE; + } + + bool PrecedencePredicateTransition::isEpsilon() { + return true; + } + + bool PrecedencePredicateTransition::matches(int symbol, int minVocabSymbol, int maxVocabSymbol) { + return false; + } + + org::antlr::v4::runtime::atn::SemanticContext::PrecedencePredicate *PrecedencePredicateTransition::getPredicate() { + return new SemanticContext::PrecedencePredicate(precedence); + } + + std::wstring PrecedencePredicateTransition::toString() { + return std::to_wstring(precedence) + std::wstring(L" >= _p"); + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/PrecedencePredicateTransition.h b/runtime/Cpp/org/antlr/v4/runtime/atn/PrecedencePredicateTransition.h new file mode 100755 index 000000000..5f3a38d5c --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/PrecedencePredicateTransition.h @@ -0,0 +1,74 @@ +#pragma once + +#include + +#include "AbstractPredicateTransition.h" +#include "ATNState.h" +#include "SemanticContext.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + using org::antlr::v4::runtime::misc::NotNull; + + /// + /// + /// @author Sam Harwell + /// + class PrecedencePredicateTransition final : public AbstractPredicateTransition { + public: + const int precedence; + + PrecedencePredicateTransition(ATNState *target, int precedence); + + virtual int getSerializationType() override; + + virtual bool isEpsilon() override; + + virtual bool matches(int symbol, int minVocabSymbol, int maxVocabSymbol) override; + + SemanticContext::PrecedencePredicate *getPredicate(); + + virtual std::wstring toString(); + + }; + + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/PredicateTransition.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/PredicateTransition.cpp new file mode 100755 index 000000000..558e8097e --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/PredicateTransition.cpp @@ -0,0 +1,66 @@ +#include "PredicateTransition.h" +#include "stringconverter.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + PredicateTransition::PredicateTransition(ATNState *target, int ruleIndex, int predIndex, bool isCtxDependent) : AbstractPredicateTransition(target), ruleIndex(ruleIndex), predIndex(predIndex), isCtxDependent(isCtxDependent) { + } + + int PredicateTransition::getSerializationType() { + return PREDICATE; + } + + bool PredicateTransition::isEpsilon() { + return true; + } + + bool PredicateTransition::matches(int symbol, int minVocabSymbol, int maxVocabSymbol) { + return false; + } + + atn::SemanticContext::Predicate *PredicateTransition::getPredicate() { + // TODO: who is responsible for managing this memory? + return new SemanticContext::Predicate(ruleIndex, predIndex, isCtxDependent); + } + + std::wstring PredicateTransition::toString() { + return std::wstring(L"pred_") + antlrcpp::StringConverterHelper::toString(ruleIndex) + std::wstring(L":") + antlrcpp::StringConverterHelper::toString(predIndex); + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/PredicateTransition.h b/runtime/Cpp/org/antlr/v4/runtime/atn/PredicateTransition.h new file mode 100755 index 000000000..7e7f8b8fd --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/PredicateTransition.h @@ -0,0 +1,76 @@ +#pragma once + +#include "AbstractPredicateTransition.h" +#include "ATNState.h" +#include "SemanticContext.h" +#include + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + using org::antlr::v4::runtime::misc::NotNull; + + /// + /// TODO: this is old comment: + /// A tree of semantic predicates from the grammar AST if label==SEMPRED. + /// In the ATN, labels will always be exactly one predicate, but the DFA + /// may have to combine a bunch of them as it collects predicates from + /// multiple ATN configurations into a single DFA state. + /// + class PredicateTransition final : public AbstractPredicateTransition { + public: + const int ruleIndex; + const int predIndex; + const bool isCtxDependent; // e.g., $i ref in pred + + PredicateTransition(ATNState *target, int ruleIndex, int predIndex, bool isCtxDependent); + + virtual int getSerializationType() override; + + virtual bool isEpsilon() override; + virtual bool matches(int symbol, int minVocabSymbol, int maxVocabSymbol) override; + + SemanticContext::Predicate *getPredicate(); + + virtual std::wstring toString(); + + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/PredictionContext.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/PredictionContext.cpp new file mode 100755 index 000000000..96d72027a --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/PredictionContext.cpp @@ -0,0 +1,583 @@ +#include + +#include "PredictionContext.h" +#include "EmptyPredictionContext.h" +#include "MurmurHash.h" +#include "ArrayPredictionContext.h" +#include "RuleContext.h" +#include "RuleTransition.h" +#include "Arrays.h" +#include "stringconverter.h" +#include "PredictionContextCache.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + using misc::MurmurHash; + + namespace atn { + + int PredictionContext::globalNodeCount = 0; + EmptyPredictionContext * PredictionContext::EMPTY; + const int PredictionContext::EMPTY_RETURN_STATE; + const int PredictionContext::INITIAL_HASH; + + PredictionContext::PredictionContext(int cachedHashCode) : id(globalNodeCount++), cachedHashCode(cachedHashCode) { + } + + atn::PredictionContext *PredictionContext::fromRuleContext(ATN *atn, RuleContext *outerContext) { + if (outerContext == nullptr) { + outerContext = (RuleContext*)RuleContext::EMPTY; + } + + // if we are in RuleContext of start rule, s, then PredictionContext + // is EMPTY. Nobody called us. (if we are empty, return empty) + if (outerContext->parent == nullptr || outerContext == (RuleContext*)RuleContext::EMPTY) { + return PredictionContext::EMPTY; + } + + // If we have a parent, convert it to a PredictionContext graph + PredictionContext *parent = EMPTY; + parent = PredictionContext::fromRuleContext(atn, outerContext->parent); + + ATNState *state = atn->states[outerContext->invokingState]; + RuleTransition *transition = (RuleTransition *)state->transition(0);//static_cast(state->transition(0)); + return SingletonPredictionContext::create(parent, transition->followState->stateNumber); + } + + bool PredictionContext::isEmpty() { + return this == EMPTY; + } + + bool PredictionContext::hasEmptyPath() { + return getReturnState(size() - 1) == EMPTY_RETURN_STATE; + } + + int PredictionContext::hashCode() { + return cachedHashCode; + } + + int PredictionContext::calculateEmptyHashCode() { + int hash = MurmurHash::initialize(INITIAL_HASH); + hash = MurmurHash::finish(hash, 0); + return hash; + } + + int PredictionContext::calculateHashCode(PredictionContext *parent, int returnState) { + int hash = MurmurHash::initialize(INITIAL_HASH); + hash = MurmurHash::update(hash, parent); + hash = MurmurHash::update(hash, returnState); + hash = MurmurHash::finish(hash, 2); + return hash; + } + + int PredictionContext::calculateHashCode(std::vector parents, std::vector returnStates) { + int hash = MurmurHash::initialize(INITIAL_HASH); + + for (auto parent : parents) { + hash = MurmurHash::update(hash, parent); + } + for (std::vector::size_type i = 0; i < parents.size() ; i++) { + PredictionContext * parent = parents[i]; + hash = MurmurHash::update(hash, parent); + } + + for (auto returnState : returnStates) { + hash = MurmurHash::update(hash, returnState); + } + + hash = MurmurHash::finish(hash, 2 * sizeof(parents) / sizeof(parents[0])); + return hash; + } + + atn::PredictionContext *PredictionContext::merge(PredictionContext *a, PredictionContext *b, + bool rootIsWildcard, misc::DoubleKeyMap *mergeCache) { + if (a != nullptr && b != nullptr) { + throw new ASSERTException(L"PredictionContext::merge", + L"a != nullptr && b != nullptr"); + }; + + // share same graph if both same + if (a == b || a->equals(b)) { + return a; + } + + if (dynamic_cast(a) != nullptr && dynamic_cast(b) != nullptr) { + return mergeSingletons(static_cast(a), static_cast(b), rootIsWildcard, mergeCache); + } + + // At least one of a or b is array + // If one is $ and rootIsWildcard, return $ as * wildcard + if (rootIsWildcard) { + if (dynamic_cast(a) != nullptr) { + return a; + } + if (dynamic_cast(b) != nullptr) { + return b; + } + } + + // convert singleton so both are arrays to normalize + if (dynamic_cast(a) != nullptr) { + a = (PredictionContext *)new ArrayPredictionContext(static_cast(a)); + } + if (dynamic_cast(b) != nullptr) { + b = (PredictionContext *)new ArrayPredictionContext(static_cast(b)); + } + return mergeArrays(static_cast(a), static_cast(b), rootIsWildcard, mergeCache); + } + + atn::PredictionContext *PredictionContext::mergeSingletons(SingletonPredictionContext *a, SingletonPredictionContext *b, bool rootIsWildcard, misc::DoubleKeyMap *mergeCache) { + if (mergeCache != nullptr) { + PredictionContext *previous = mergeCache->get(a,b); + if (previous != nullptr) { + return previous; + } + previous = mergeCache->get(b,a); + if (previous != nullptr) { + return previous; + } + } + + PredictionContext *rootMerge = mergeRoot(a, b, rootIsWildcard); + if (rootMerge != nullptr) { + if (mergeCache != nullptr) { + mergeCache->put((PredictionContext *)a, (PredictionContext *)b, rootMerge); + } + return rootMerge; + } + + if (a->returnState == b->returnState) { // a == b + PredictionContext *parent = merge(a->parent, b->parent, rootIsWildcard, mergeCache); + // if parent is same as existing a or b parent or reduced to a parent, return it + if (parent == a->parent) { // ax + bx = ax, if a=b + return (PredictionContext *)a; + } + if (parent == b->parent) { // ax + bx = bx, if a=b + return (PredictionContext *)b; + } + // else: ax + ay = a'[x,y] + // merge parents x and y, giving array node with x,y then remainders + // of those graphs. dup a, a' points at merged array + // new joined parent so create new singleton pointing to it, a' + PredictionContext *a_ = (PredictionContext *)SingletonPredictionContext::create(parent, a->returnState); + if (mergeCache != nullptr) { + mergeCache->put((PredictionContext *)a, (PredictionContext *)b, a_); + } + return a_; + } + else { // a != b payloads differ + // see if we can collapse parents due to $+x parents if local ctx + PredictionContext *singleParent = nullptr; + if (a == b || (a->parent != nullptr && a->parent->equals(b->parent))) { // ax + bx = [a,b]x + singleParent = a->parent; + } + if (singleParent != nullptr) { // parents are same + // sort payloads and use same parent + int payloads[2] = {a->returnState, b->returnState}; + if (a->returnState > b->returnState) { + payloads[0] = b->returnState; + payloads[1] = a->returnState; + } + PredictionContext parents[2] = {*singleParent, *singleParent}; + PredictionContext *a_ = new ArrayPredictionContext(parents, payloads); + if (mergeCache != nullptr) { + mergeCache->put(a, b, a_); + } + return a_; + } + // parents differ and can't merge them. Just pack together + // into array; can't merge. + // ax + by = [ax,by] + PredictionContext *a_; + if (a->returnState > b->returnState) { // sort by payload + int payloads[2] = {b->returnState, a->returnState}; + PredictionContext parents[2] = {*b->parent, *a->parent}; + a_ = new ArrayPredictionContext(parents, payloads); + } else { + int payloads[2] = {a->returnState, b->returnState}; + PredictionContext parents[2] = {*a->parent, *b->parent}; + a_ = new ArrayPredictionContext(parents, payloads); + } + + if (mergeCache != nullptr) { + mergeCache->put(a, b, a_); + } + return a_; + } + } + + atn::PredictionContext *PredictionContext::mergeRoot(SingletonPredictionContext *a, SingletonPredictionContext *b, bool rootIsWildcard) { + if (rootIsWildcard) { + if (a == EMPTY) { // * + b = * + return (PredictionContext *)EMPTY; + } + if (b == EMPTY) { // a + * = * + return (PredictionContext *)EMPTY; + } + } else { + if (a == EMPTY && b == EMPTY) { // $ + $ = $ + return (PredictionContext *)EMPTY; + } + if (a == EMPTY) { // $ + x = [$,x] + int payloads[2] = {b->returnState, EMPTY_RETURN_STATE}; + PredictionContext parents[2] = {*b->parent, *EMPTY}; + PredictionContext *joined = new ArrayPredictionContext(parents, payloads); + return joined; + } + if (b == EMPTY) { // x + $ = [$,x] ($ is always first if present) + int payloads[2] = {a->returnState, EMPTY_RETURN_STATE}; + PredictionContext parents[2] = {*a->parent, *EMPTY}; + PredictionContext *joined = new ArrayPredictionContext(parents, payloads); + return joined; + } + } + return nullptr; + } + + atn::PredictionContext *PredictionContext::mergeArrays(ArrayPredictionContext *a, ArrayPredictionContext *b, bool rootIsWildcard, misc::DoubleKeyMap *mergeCache) { + if (mergeCache != nullptr) { + PredictionContext *previous = mergeCache->get(a,b); + if (previous != nullptr) { + return previous; + } + previous = mergeCache->get(b,a); + if (previous != nullptr) { + return previous; + } + } + + // merge sorted payloads a + b => M + std::vector::size_type i = 0; // walks a + std::vector::size_type j = 0; // walks b + std::vector::size_type k = 0; // walks target M array + + std::vector mergedReturnStates; + std::vector mergedParents; + + // walk and merge to yield mergedParents, mergedReturnStates + while (i < a->returnStates.size() && j < b->returnStates.size()) { + PredictionContext *a_parent = a->parents->at(i);// [i]; + PredictionContext *b_parent = b->parents->at(i);//&b->parents[j]; + if (a->returnStates[i] == b->returnStates[j]) { + // same payload (stack tops are equal), must yield merged singleton + int payload = a->returnStates[i]; + // $+$ = $ + bool both$ = payload == EMPTY_RETURN_STATE && a_parent == nullptr && b_parent == nullptr; + bool ax_ax = (a_parent != nullptr && b_parent != nullptr) && a_parent == b_parent;//->equals(b_parent); // ax+ax -> ax + if (both$ || ax_ax) { + mergedParents[k] = a_parent; // choose left + mergedReturnStates[k] = payload; + } + else { // ax+ay -> a'[x,y] + PredictionContext *mergedParent = merge(a_parent, b_parent, rootIsWildcard, mergeCache); + mergedParents[k] = mergedParent; + mergedReturnStates[k] = payload; + } + i++; // hop over left one as usual + j++; // but also skip one in right side since we merge + } else if (a->returnStates[i] < b->returnStates[j]) { // copy a[i] to M + mergedParents[k] = a_parent; + mergedReturnStates[k] = a->returnStates[i]; + i++; + } + else { // b > a, copy b[j] to M + mergedParents[k] = b_parent; + mergedReturnStates[k] = b->returnStates[j]; + j++; + } + k++; + } + + // copy over any payloads remaining in either array + if (i < a->returnStates.size()) { + for (std::vector::size_type p = i; p < a->returnStates.size(); p++) { + mergedParents[k] = a->parents->at(p);//[p]; + mergedReturnStates[k] = a->returnStates[p]; + k++; + } + } else { + for (std::vector::size_type p = j; p < b->returnStates.size(); p++) { + mergedParents[k] = b->parents->at(p);// [p]; + mergedReturnStates[k] = b->returnStates[p]; + k++; + } + } + + // trim merged if we combined a few that had same stack tops + if (k < sizeof(mergedParents) / sizeof(mergedParents[0])) { // write index < last position; trim + if (k == 1) { // for just one merged element, return singleton top + PredictionContext *a_ = SingletonPredictionContext::create(mergedParents[0], mergedReturnStates[0]); + if (mergeCache != nullptr) { + mergeCache->put(a,b,a_); + } + return a_; + } + // TODO: mergedParents = Arrays::copyOf(mergedParents, k); + // TODO: mergedReturnStates = Arrays::copyOf(mergedReturnStates, k); + } + + PredictionContext *M = nullptr; + // TODO: PredictionContext *M = new ArrayPredictionContext(mergedParents, mergedReturnStates); + + // if we created same array as a or b, return that instead + // TODO: track whether this is possible above during merge sort for speed + if (M->equals(a)) { + if (mergeCache != nullptr) { + mergeCache->put(a,b,a); + } + return a; + } + if (M->equals(b)) { + if (mergeCache != nullptr) { + mergeCache->put(a,b,b); + } + return b; + } + + combineCommonParents(mergedParents); + + if (mergeCache != nullptr) { + mergeCache->put(a,b,M); + } + return M; + } + + void PredictionContext::combineCommonParents(std::vector parents) { + std::unordered_map uniqueParents = std::unordered_map(); + + for (size_t p = 0; p < parents.size(); p++) { + PredictionContext *parent = parents[p]; + if (uniqueParents.find(parent) == uniqueParents.end()) { // don't replace + uniqueParents[parent] = parent; + } + } + + for (size_t p = 0; p < parents.size(); p++) { + parents[p] = uniqueParents.at(parents[p]); + } + } + + std::wstring PredictionContext::toDOTString(PredictionContext *context) { + if (context == nullptr) { + return L""; + } + antlrcpp::StringBuilder *buf = new antlrcpp::StringBuilder(); + buf->append(L"digraph G {\n"); + buf->append(L"rankdir=LR;\n"); + + std::vector nodes = getAllContextNodes(context); + + ComparatorAnonymousInnerClassHelper tmp; + std::sort(nodes.begin(), nodes.end(), (tmp.compare)); + + for (auto current : nodes) { + if (dynamic_cast(current) != nullptr) { + std::wstring s = antlrcpp::StringConverterHelper::toString(current->id); + buf->append(L" s").append(s); + std::wstring returnState = antlrcpp::StringConverterHelper::toString(current->getReturnState(0)); + if (dynamic_cast(current) != nullptr) { + returnState = L"$"; + } + buf->append(L" [label=\"").append(returnState).append(L"\"];\n"); + continue; + } + ArrayPredictionContext *arr = static_cast(current); + buf->append(L" s").append(arr->id); + buf->append(L" [shape=box, label=\""); + buf->append(L"["); + bool first = true; + for (auto inv : arr->returnStates) { + if (!first) { + buf->append(L", "); + } + if (inv == EMPTY_RETURN_STATE) { + buf->append(L"$"); + } else { + buf->append(inv); + } + first = false; + } + buf->append(L"]"); + buf->append(L"\"];\n"); + } + + for (auto current : nodes) { + if (current == EMPTY) { + continue; + } + for (int i = 0; i < current->size(); i++) { + if (current->getParent(i) == nullptr) { + continue; + } + std::wstring s = antlrcpp::StringConverterHelper::toString(current->id); + buf->append(L" s").append(s); + buf->append(L"->"); + buf->append(L"s"); + buf->append(current->getParent(i)->id); + if (current->size() > 1) { + buf->append(std::wstring(L" [label=\"parent[") + antlrcpp::StringConverterHelper::toString(i) + std::wstring(L"]\"];\n")); + } else { + buf->append(L";\n"); + } + } + } + + buf->append(L"}\n"); + return buf->toString(); + } + + PredictionContext::ComparatorAnonymousInnerClassHelper::ComparatorAnonymousInnerClassHelper() { + } + + int PredictionContext::ComparatorAnonymousInnerClassHelper::compare(PredictionContext *o1, PredictionContext *o2) { + return o1->id - o2->id; + } + + + atn::PredictionContext *PredictionContext::getCachedContext(PredictionContext *context, PredictionContextCache *contextCache, + std::map *visited) { + if (context->isEmpty()) { + return context; + } + + PredictionContext *existing = (*visited)[context]; + if (existing != nullptr) { + return existing; + } + + existing = contextCache->get(context); + if (existing != nullptr) { + std::pair thePair(context, existing); + visited->insert(thePair); + + return existing; + } + + bool changed = false; + + std::vector parents; + + for (int i = 0; i < sizeof(parents) / sizeof(parents[0]); i++) { + PredictionContext *parent = getCachedContext(context->getParent(i), contextCache, visited); + if (changed || parent != context->getParent(i)) { + if (!changed) { + parents = std::vector(); + for (int j = 0; j < context->size(); j++) { + parents[j] = context->getParent(j); + } + + changed = true; + } + + parents[i] = parent; + } + } + + if (!changed) { + contextCache->add(context); + std::pair thePair(context,context); + visited->insert(thePair); + + return context; + } + + PredictionContext *updated; + if (sizeof(parents) / sizeof(parents[0]) == 0) { + updated = EMPTY; + } else if (sizeof(parents) / sizeof(parents[0]) == 1) { + updated = SingletonPredictionContext::create(parents[0], context->getReturnState(0)); + } else { + ArrayPredictionContext *arrayPredictionContext = static_cast(context); + updated = new ArrayPredictionContext(parents, + arrayPredictionContext->returnStates); + } + + contextCache->add(updated); + + std::pair thePair(updated, updated); + visited->insert(thePair); + + std::pair otherPair(context, updated); + visited->insert(otherPair); + + return updated; + } + + std::vector PredictionContext::getAllContextNodes(PredictionContext *context) { + std::vector nodes = std::vector(); + std::map *visited = new std::map(); + getAllContextNodes_(context, nodes, visited); + return nodes; + } + + + void PredictionContext::getAllContextNodes_(PredictionContext *context, std::vector &nodes, std::map *visited) { + + if (context == nullptr || visited->at(context)) { + return; + } + + std::pair thePair(context, context); + visited->insert(thePair); + + nodes.push_back(context); + + for (int i = 0; i < context->size(); i++) { + getAllContextNodes_(context->getParent(i), nodes, visited); + } + } + + std::wstring PredictionContext::toString() { + //TODO: what should this return? (Return empty string + // for now.) + return L"TODO PredictionContext::toString()"; + } + + int PredictionContext::size() { + throw "PredictionContext::size() should not be called"; + } + + PredictionContext *PredictionContext::getParent(int index) { + throw "PredictionContext::getParent should not be called"; + } + + int PredictionContext::getReturnState(int index) { + throw "PredictionContext::getReturnState should not be called"; + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/PredictionContext.h b/runtime/Cpp/org/antlr/v4/runtime/atn/PredictionContext.h new file mode 100755 index 000000000..9bda50256 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/PredictionContext.h @@ -0,0 +1,411 @@ +#pragma once +#include +#include +#include +#include +#include + +#include "DoubleKeyMap.h" +#include "Recognizer.h" +#include "Declarations.h" +#include "EqualityComparator.h" +#include "ATN.h" +#include "ATNState.h" + + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + + class PredictionContext { + /// + /// Represents {@code $} in local context prediction, which means wildcard. + /// {@code *+x = *}. + /// + public: + static EmptyPredictionContext * EMPTY; + + /// + /// Represents {@code $} in an array in full context mode, when {@code $} + /// doesn't mean wildcard: {@code $ + x = [$,x]}. Here, + /// {@code $} = . + /// + static const int EMPTY_RETURN_STATE = INT16_MAX; + + private: + static const int INITIAL_HASH = 1; + + public: + static int globalNodeCount; + const int id; + + /// + /// Stores the computed hash code of this . The hash + /// code is computed in parts to match the following reference algorithm. + /// + ///
+                        ///  private int referenceHashCode() {
+                        ///      int hash = ();
+                        /// 
+                        ///      for (int i = 0; i < ; i++) {
+                        ///          hash = (hash, (i));
+                        ///      }
+                        /// 
+                        ///      for (int i = 0; i < ; i++) {
+                        ///          hash = (hash, (i));
+                        ///      }
+                        /// 
+                        ///      hash = (hash, 2 * );
+                        ///      return hash;
+                        ///  }
+                        /// 
+ ///
+ const int cachedHashCode; + + protected: + PredictionContext(int cachedHashCode); + + /// + /// Convert a tree to a graph. + /// Return if {@code outerContext} is empty or null. + /// + public: + static PredictionContext *fromRuleContext(ATN *atn, RuleContext *outerContext); + + virtual int size();//= 0; + + virtual PredictionContext *getParent(int index);//= 0; + + virtual int getReturnState(int index); // = 0; + + /// + /// This means only the context is in set. + virtual bool isEmpty(); + + virtual bool hasEmptyPath(); + + virtual int hashCode() final; + + virtual bool equals(void *obj){ // = 0; + // This should be abstract but we need to create arrays of it, which will point to + // daughters in reality + throw new ASSERTException(L"PredictionContext", L"equal should never be called, abstract class"); + + } + + protected: + static int calculateEmptyHashCode(); + + static int calculateHashCode(PredictionContext *parent, int returnState); + + static int calculateHashCode(std::vector parents, std::vectorreturnStates); + + // dispatch + public: + static PredictionContext *merge(PredictionContext *a, PredictionContext *b, bool rootIsWildcard, misc::DoubleKeyMap *mergeCache); + + /// + /// Merge two instances. + /// + ///

+ /// + /// Stack tops equal, parents merge is same; return left graph.
+ /// + /// + ///

+ /// + /// Same stack top, parents differ; merge parents giving array node, then + /// remainders of those graphs. A new root node is created to point to the + /// merged parents.
+ /// + /// + ///

+ /// + /// Different stack tops pointing to same parent. Make array node for the + /// root where both element in the root point to the same (original) + /// parent.
+ /// + /// + ///

+ /// + /// Different stack tops pointing to different parents. Make array node for + /// the root where each element points to the corresponding original + /// parent.
+ /// + ///

+ /// the first + /// the second + /// {@code true} if this is a local-context merge, + /// otherwise false to indicate a full-context merge + /// + static PredictionContext *mergeSingletons(SingletonPredictionContext *a, SingletonPredictionContext *b, bool rootIsWildcard, misc::DoubleKeyMap *mergeCache); + + /// + /// Handle case where at least one of {@code a} or {@code b} is + /// . In the following diagrams, the symbol {@code $} is used + /// to represent . + /// + ///

Local-Context Merges

+ /// + /// These local-context merge operations are used when {@code rootIsWildcard} + /// is true. + /// + ///

+ /// + /// is superset of any graph; return .
+ /// + /// + ///

+ /// + /// and anything is {@code #EMPTY}, so merged parent is + /// {@code #EMPTY}; return left graph.
+ /// + /// + ///

+ /// + /// Special case of last merge if local context.
+ /// + /// + ///

Full-Context Merges

+ /// + /// These full-context merge operations are used when {@code rootIsWildcard} + /// is false. + /// + ///

+ /// + /// + /// + ///

+ /// + /// Must keep all contexts; in array is a special value (and + /// null parent).
+ /// + /// + ///

+ /// + /// + ///

+ /// the first + /// the second + /// {@code true} if this is a local-context merge, + /// otherwise false to indicate a full-context merge + static PredictionContext *mergeRoot(SingletonPredictionContext *a, SingletonPredictionContext *b, bool rootIsWildcard); + + /// + /// Merge two instances. + /// + ///

+ /// + /// Different tops, different parents.
+ /// + /// + ///

+ /// + /// Shared top, same parents.
+ /// + /// + ///

+ /// + /// Shared top, different parents.
+ /// + /// + ///

+ /// + /// Shared top, all shared parents.
+ /// + /// + ///

+ /// + /// Equal tops, merge parents and reduce top to + /// .
+ /// + ///

+ static PredictionContext *mergeArrays(ArrayPredictionContext *a, ArrayPredictionContext *b, bool rootIsWildcard, misc::DoubleKeyMap *mergeCache); + + /// + /// Make pass over all M {@code parents}; merge any {@code ()} + /// ones. + /// + protected: + static void combineCommonParents(std::vectorparents); + + public: + static std::wstring toDOTString(PredictionContext *context); + + private: + + class ComparatorAnonymousInnerClassHelper : public misc::EqualityComparator { + + public: + ComparatorAnonymousInnerClassHelper(); + + static int compare(PredictionContext *o1, PredictionContext *o2); + }; + + // From Sam + public: + static PredictionContext *getCachedContext(PredictionContext *context, PredictionContextCache *contextCache, std::map *visited); + + // // extra structures, but cut/paste/morphed works, so leave it. + // // seems to do a breadth-first walk + // public static List getAllNodes(PredictionContext context) { + // Map visited = + // new IdentityHashMap(); + // Deque workList = new ArrayDeque(); + // workList.add(context); + // visited.put(context, context); + // List nodes = new ArrayList(); + // while (!workList.isEmpty()) { + // PredictionContext current = workList.pop(); + // nodes.add(current); + // for (int i = 0; i < current.size(); i++) { + // PredictionContext parent = current.getParent(i); + // if ( parent!=null && visited.put(parent, parent) == null) { + // workList.push(parent); + // } + // } + // } + // return nodes; + // } + + // ter's recursive version of Sam's getAllNodes() + static std::vector getAllContextNodes(PredictionContext *context); + + static void getAllContextNodes_(PredictionContext *context, std::vector &nodes, std::map *visited); + + std::wstring toString(); + + template + std::wstring toString(Recognizer *recog) { + return toString(); + // return toString(recog, ParserRuleContext.EMPTY); + } + + template + std::wstring *toStrings(Recognizer *recognizer, int currentState) { + return toStrings(recognizer, EMPTY, currentState); + } + + // FROM SAM + template + std::wstring *toStrings(Recognizer *recognizer, PredictionContext *stop, int currentState) { + std::vector result = std::vector(); + + for (int perm = 0; ; perm++) { + int offset = 0; + bool last = true; + PredictionContext *p = this; + int stateNumber = currentState; + antlrcpp::StringBuilder *localBuffer = new antlrcpp::StringBuilder(); + localBuffer->append(L"["); + while (!p->isEmpty() && p != stop) { + int index = 0; + if (p->size() > 0) { + int bits = 1; + while ((1 << bits) < p->size()) { + bits++; + } + + int mask = (1 << bits) - 1; + index = (perm >> offset) & mask; + last &= index >= p->size() - 1; + if (index >= p->size()) { + goto outerContinue; + } + offset += bits; + } + + if (recognizer != nullptr) { + if (localBuffer->length() > 1) { + // first char is '[', if more than that this isn't the first rule + localBuffer->append(L' '); + } + + ATN *atn = recognizer->getATN(); + ATNState *s = atn->states[stateNumber]; + std::wstring ruleName = recognizer->getRuleNames()[s->ruleIndex]; + localBuffer->append(ruleName); + } else if (p->getReturnState(index) != EMPTY_RETURN_STATE) { + if (!p->isEmpty()) { + if (localBuffer->length() > 1) { + // first char is '[', if more than that this isn't the first rule + localBuffer->append(L' '); + } + + localBuffer->append(p->getReturnState(index)); + } + } + stateNumber = p->getReturnState(index); + p = p->getParent(index); + } + localBuffer->append(L"]"); + result.push_back(localBuffer->toString()); + + if (last) { + break; + } + outerContinue: + continue; + } + outerBreak: + + // TODO: return result.toArray(new std::wstring[result.size()]); + return nullptr; + } + + }; + + } + } + } + } +} + +// Hash function for PredictionContext, used in the MurmurHash::update function + +namespace std { + using org::antlr::v4::runtime::atn::PredictionContext; + + template <> struct hash + { + size_t operator()(PredictionContext & x) const + { + return x.hashCode(); + } + }; +} + + diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/PredictionContextCache.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/PredictionContextCache.cpp new file mode 100755 index 000000000..71755cc02 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/PredictionContextCache.cpp @@ -0,0 +1,70 @@ +#include "PredictionContextCache.h" +#include +#include "PredictionContext.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2015 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + org::antlr::v4::runtime::atn::PredictionContext *PredictionContextCache::add( + PredictionContext *ctx) { + if (ctx == PredictionContext::EMPTY) { + return PredictionContext::EMPTY; + } + PredictionContext *existing = cache->at(ctx); + if (existing != nullptr) { + // System.out.println(name+" reuses "+existing); + return existing; + } + cache->insert(std::pair(ctx, ctx)); + return ctx; + } + + org::antlr::v4::runtime::atn::PredictionContext *PredictionContextCache::get( + PredictionContext *ctx) { + return cache->at(ctx); + } + + size_t PredictionContextCache::size() { return cache->size(); } + + void PredictionContextCache::InitializeInstanceFields() { + cache = new std::map(); + } + + } // namespace atn + } // namespace runtime + } // namespace v4 + } // namespace antlr +} // namespace org diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/PredictionContextCache.h b/runtime/Cpp/org/antlr/v4/runtime/atn/PredictionContextCache.h new file mode 100755 index 000000000..7581affe0 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/PredictionContextCache.h @@ -0,0 +1,79 @@ +#pragma once + +#include + +#include "Declarations.h" +#include "EmptyPredictionContext.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "PredictionContext.h" + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + /// + /// Used to cache objects. Its used for the + /// shared + /// context cash associated with contexts in DFA states. This cache + /// can be used for both lexers and parsers. + /// + class PredictionContextCache { + protected: + std::map *cache; + + /// + /// Add a context to the cache and return it. If the context already exists, + /// return that one instead and do not add a new context to the cache. + /// Protect shared cache from unsafe thread access. + /// + public: + virtual PredictionContext *add(PredictionContext *ctx); + + virtual PredictionContext *get(PredictionContext *ctx); + + virtual size_t size(); + + private: + void InitializeInstanceFields(); + + public: + PredictionContextCache() { InitializeInstanceFields(); } + }; + + } // namespace atn + } // namespace runtime + } // namespace v4 + } // namespace antlr +} // namespace org diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/PredictionMode.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/PredictionMode.cpp new file mode 100755 index 000000000..932cf2d36 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/PredictionMode.cpp @@ -0,0 +1,241 @@ +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "PredictionMode.h" + +#include +#include "AbstractEqualityComparator.h" + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + class AltAndContextConfigEqualityComparator + : misc::AbstractEqualityComparator < ATNConfig > { + public: + int hashCode(ATNConfig* o); + bool equals(ATNConfig* a, ATNConfig* b); + + private: + AltAndContextConfigEqualityComparator() {} + }; + + // TODO -- Determine if we need this hash function. + int AltAndContextConfigEqualityComparator::hashCode(ATNConfig* o) { + int hashCode = runtime::misc::MurmurHash::initialize(7); + hashCode = runtime::misc::MurmurHash::update(hashCode, o->state->stateNumber); + hashCode = runtime::misc::MurmurHash::update(hashCode, o->context); + return runtime::misc::MurmurHash::finish(hashCode, 2); + } + + // TODO -- Determine if we need this comparator. + bool AltAndContextConfigEqualityComparator::equals(ATNConfig* a, ATNConfig* b) { + if (a == b) { + return true; + } + if (a == nullptr || b == nullptr) { + return false; + } + return a->state->stateNumber == b->state->stateNumber && + a->context->equals(b->context); + } + + /// + /// A Map that uses just the state and the stack context as the key. + class AltAndContextMap : public std::unordered_map < ATNConfig, antlrcpp::BitSet, ATNConfig::ATNConfigHasher> { + public: + AltAndContextMap() {} + }; + + + bool PredictionModeClass::hasSLLConflictTerminatingPrediction(PredictionMode* mode, + ATNConfigSet* configs) { + /* Configs in rule stop states indicate reaching the end of the decision + * rule (local context) or end of start rule (full context). If all + * configs meet this condition, then none of the configurations is able + * to match additional input so we terminate prediction. + */ + if (allConfigsInRuleStopStates(configs)) { + return true; + } + + // pure SLL mode parsing + if (*mode == PredictionMode::SLL) { + // Don't bother with combining configs from different semantic + // contexts if we can fail over to full LL; costs more time + // since we'll often fail over anyway. + if (configs->hasSemanticContext) { + // dup configs, tossing out semantic predicates + ATNConfigSet* dup = new ATNConfigSet(); + for (ATNConfig config : *configs) { + ATNConfig* c = new ATNConfig(&config, SemanticContext::NONE); + dup->add(c); + } + configs = dup; + } + // now we have combined contexts for configs with dissimilar preds + } + + // pure SLL or combined SLL+LL mode parsing + std::vector altsets = getConflictingAltSubsets(configs); + bool heuristic = + hasConflictingAltSet(altsets) && !hasStateAssociatedWithOneAlt(configs); + return heuristic; + } + + bool PredictionModeClass::hasConfigInRuleStopState(ATNConfigSet* configs) { + for (ATNConfig c : *configs) { + if (dynamic_cast(c.state) != NULL) { + return true; + } + } + + return false; + } + + bool PredictionModeClass::allConfigsInRuleStopStates(ATNConfigSet* configs) { + for (ATNConfig config : *configs) { + if (dynamic_cast(config.state) == NULL) { + return false; + } + } + + return true; + } + + int PredictionModeClass::resolvesToJustOneViableAlt(const std::vector& altsets) { + return getSingleViableAlt(altsets); + } + + bool PredictionModeClass::allSubsetsConflict(const std::vector& altsets) { + return !hasNonConflictingAltSet(altsets); + } + + bool PredictionModeClass::hasNonConflictingAltSet(const std::vector& altsets) { + for (antlrcpp::BitSet alts : altsets) { + if (alts.count() == 1) { + return true; + } + } + return false; + } + + bool PredictionModeClass::hasConflictingAltSet(const std::vector& altsets) { + for (antlrcpp::BitSet alts : altsets) { + if (alts.count() > 1) { + return true; + } + } + return false; + } + + bool PredictionModeClass::allSubsetsEqual(const std::vector& altsets) { + if (altsets.size() == 0) { + // TODO -- Determine if this should return true or false when there are no + // sets available based on the original code. + return true; + } + const antlrcpp::BitSet& first = *altsets.begin(); + for (const antlrcpp::BitSet& alts : altsets) { + if (alts.data != first.data) { + return false; + } + } + return true; + } + + int PredictionModeClass::getUniqueAlt(const std::vector& altsets) { + antlrcpp::BitSet all = getAlts(altsets); + if (all.count() == 1) { + return all.nextSetBit(0); + } + return ATN::INVALID_ALT_NUMBER; + } + + antlrcpp::BitSet PredictionModeClass::getAlts(const std::vector& altsets) { + antlrcpp::BitSet all; + for (antlrcpp::BitSet alts : altsets) { + all.data |= alts.data; + } + + return all; + } + + std::vector PredictionModeClass::getConflictingAltSubsets(ATNConfigSet* configs) { + AltAndContextMap configToAlts; + for (const ATNConfig& c : *configs) { + configToAlts[c].set(c.alt); + } + std::vector values; + for (auto it : configToAlts) { + values.push_back(it.second); + } + return values; + } + + std::map PredictionModeClass::getStateToAltMap(ATNConfigSet* configs) { + std::map m; + for (ATNConfig c : *configs) { + m[c.state].set(c.alt); + } + return m; + } + + bool PredictionModeClass::hasStateAssociatedWithOneAlt(ATNConfigSet* configs) { + std::map x = getStateToAltMap(configs); + for (std::map::iterator it = x.begin(); it != x.end(); it++){ + if (it->second.count() == 1) return true; + } + return false; + } + + int PredictionModeClass::getSingleViableAlt(const std::vector& altsets) { + antlrcpp::BitSet viableAlts; + for (antlrcpp::BitSet alts : altsets) { + int minAlt = alts.nextSetBit(0); + + assert(minAlt != -1); // TODO -- Remove this after verification. + viableAlts.set(minAlt); + if (viableAlts.count() > 1) // more than 1 viable alt + { + return ATN::INVALID_ALT_NUMBER; + } + } + + return viableAlts.nextSetBit(0); + } + + } // namespace atn + } // namespace runtime + } // namespace v4 + } // namespace antlr +} // namespace org diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/PredictionMode.h b/runtime/Cpp/org/antlr/v4/runtime/atn/PredictionMode.h new file mode 100755 index 000000000..c5328d1ce --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/PredictionMode.h @@ -0,0 +1,522 @@ +#pragma once + +#include +#include + +#include "ATN.h" +#include "ATNConfig.h" +#include "ATNConfigSet.h" +#include "BitSet.h" +#include "Declarations.h" +#include "MurmurHash.h" +#include "PredictionContext.h" +#include "RuleStopState.h" +#include "SemanticContext.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + enum class PredictionMode { + /// + /// Do only local context prediction (SLL style) and using + /// heuristic which almost always works but is much faster + /// than precise answer. + /// + SLL, + + /// + /// Full LL(*) that always gets right answer. For speed + /// reasons, we terminate the prediction process when we know for + /// sure which alt to predict. We don't always know what + /// the ambiguity is in this mode. + /// + LL, + + /// + /// Tell the full LL prediction algorithm to pursue lookahead until + /// it has uniquely predicted an alternative without conflict or it's + /// certain that it's found an ambiguous input sequence. when this + /// variable is false. When true, the prediction process will + /// continue looking for the exact ambiguous sequence even if + /// it has already figured out which alternative to predict. + /// + LL_EXACT_AMBIG_DETECTION + }; + + class PredictionModeClass { + + public: + /// + /// Computes the SLL prediction termination condition. + /// + ///

+ /// + /// This method computes the SLL prediction termination condition for both of + /// the following cases. + /// + ///

    + ///
  • The usual SLL+LL fallback upon SLL conflict
  • + ///
  • Pure SLL without LL fallback
  • + ///
+ /// + ///

+ /// + /// COMBINED SLL+LL PARSING + /// + ///

+ /// + /// When LL-fallback is enabled upon SLL conflict, correct predictions are + /// ensured regardless of how the termination condition is computed by this + /// method. Due to the substantially higher cost of LL prediction, the + /// prediction should only fall back to LL when the additional lookahead + /// cannot lead to a unique SLL prediction. + /// + ///

+ /// + /// Assuming combined SLL+LL parsing, an SLL configuration set with only + /// conflicting subsets should fall back to full LL, even if the + /// configuration sets don't resolve to the same alternative (e.g. + /// {@code {1,2}} and {@code {3,4}}. If there is at least one non-conflicting + /// configuration, SLL could continue with the hopes that more lookahead will + /// resolve via one of those non-conflicting configurations. + /// + ///

+ /// + /// Here's the prediction termination rule them: SLL (for SLL+LL parsing) + /// stops when it sees only conflicting configuration subsets. In contrast, + /// full LL keeps going when there is uncertainty. + /// + ///

+ /// + /// HEURISTIC + /// + ///

+ /// + /// As a heuristic, we stop prediction when we see any conflicting subset + /// unless we see a state that only has one alternative associated with it. + /// The single-alt-state thing lets prediction continue upon rules like + /// (otherwise, it would admit defeat too soon): + /// + ///

+ /// + /// {@code [12|1|[], 6|2|[], 12|2|[]]. s : (ID | ID ID?) ';' ;} + /// + ///

+ /// + /// When the ATN simulation reaches the state before {@code ';'}, it has a + /// DFA state that looks like: {@code [12|1|[], 6|2|[], 12|2|[]]}. Naturally + /// {@code 12|1|[]} and {@code 12|2|[]} conflict, but we cannot stop + /// processing this node because alternative to has another way to continue, + /// via {@code [6|2|[]]}. + /// + ///

+ /// + /// It also let's us continue for this rule: + /// + ///

+ /// + /// {@code [1|1|[], 1|2|[], 8|3|[]] a : A | A | A B ;} + /// + ///

+ /// + /// After matching input A, we reach the stop state for rule A, state 1. + /// State 8 is the state right before B. Clearly alternatives 1 and 2 + /// conflict and no amount of further lookahead will separate the two. + /// However, alternative 3 will be able to continue and so we do not stop + /// working on this state. In the previous example, we're concerned with + /// states associated with the conflicting alternatives. Here alt 3 is not + /// associated with the conflicting configs, but since we can continue + /// looking for input reasonably, don't declare the state done. + /// + ///

+ /// + /// PURE SLL PARSING + /// + ///

+ /// + /// To handle pure SLL parsing, all we have to do is make sure that we + /// combine stack contexts for configurations that differ only by semantic + /// predicate. From there, we can do the usual SLL termination heuristic. + /// + ///

+ /// + /// PREDICATES IN SLL+LL PARSING + /// + ///

+ /// + /// SLL decisions don't evaluate predicates until after they reach DFA stop + /// states because they need to create the DFA cache that works in all + /// semantic situations. In contrast, full LL evaluates predicates collected + /// during start state computation so it can ignore predicates thereafter. + /// This means that SLL termination detection can totally ignore semantic + /// predicates. + /// + ///

+ /// + /// Implementation-wise, combines stack contexts + /// but not + /// semantic predicate contexts so we might see two configurations like the + /// following. + /// + ///

+ /// + /// {@code (s, 1, x, {}), (s, 1, x', {p})} + /// + ///

+ /// + /// Before testing these configurations against others, we have to merge + /// {@code x} and {@code x'} (without modifying the existing configurations). + /// For example, we test {@code (x+x')==x''} when looking for conflicts in + /// the following configurations. + /// + ///

+ /// + /// {@code (s, 1, x, {}), (s, 1, x', {p}), (s, 2, x'', {})} + /// + ///

+ /// + /// If the configuration set has predicates (as indicated by + /// ), this algorithm makes a + /// copy of + /// the configurations to strip out all of the predicates so that a standard + /// will merge everything ignoring predicates. + ///

+ static bool hasSLLConflictTerminatingPrediction(PredictionMode *mode, + ATNConfigSet *configs); + + /// + /// Checks if any configuration in {@code configs} is in a + /// . Configurations meeting this condition have + /// reached + /// the end of the decision rule (local context) or end of start rule (full + /// context). + /// + /// the configuration set to test + /// {@code true} if any configuration in {@code configs} is in a + /// , otherwise {@code false} + static bool hasConfigInRuleStopState(ATNConfigSet *configs); + + /// + /// Checks if all configurations in {@code configs} are in a + /// . Configurations meeting this condition have + /// reached + /// the end of the decision rule (local context) or end of start rule (full + /// context). + /// + /// the configuration set to test + /// {@code true} if all configurations in {@code configs} are in a + /// , otherwise {@code false} + static bool allConfigsInRuleStopStates(ATNConfigSet *configs); + + /// + /// Full LL prediction termination. + /// + ///

+ /// + /// Can we stop looking ahead during ATN simulation or is there some + /// uncertainty as to which alternative we will ultimately pick, after + /// consuming more input? Even if there are partial conflicts, we might know + /// that everything is going to resolve to the same minimum alternative. That + /// means we can stop since no more lookahead will change that fact. On the + /// other hand, there might be multiple conflicts that resolve to different + /// minimums. That means we need more look ahead to decide which of those + /// alternatives we should predict. + /// + ///

+ /// + /// The basic idea is to split the set of configurations {@code C}, into + /// conflicting subsets {@code (s, _, ctx, _)} and singleton subsets with + /// non-conflicting configurations. Two configurations conflict if they have + /// identical and values + /// but different value, e.g. {@code (s, i, ctx, + /// _)} + /// and {@code (s, j, ctx, _)} for {@code i!=j}. + /// + ///

+ /// + /// Reduce these configuration subsets to the set of possible alternatives. + /// You can compute the alternative subsets in one pass as follows: + /// + ///

+ /// + /// {@code A_s,ctx = {i | (s, i, ctx, _)}} for each configuration in + /// {@code C} holding {@code s} and {@code ctx} fixed. + /// + ///

+ /// + /// Or in pseudo-code, for each configuration {@code c} in {@code C}: + /// + ///

+						/// map[c] U= c. # map hash/equals uses s and
+						/// x, not
+						/// alt and not pred
+						/// 
+ /// + ///

+ /// + /// The values in {@code map} are the set of {@code A_s,ctx} sets. + /// + ///

+ /// + /// If {@code |A_s,ctx|=1} then there is no conflict associated with + /// {@code s} and {@code ctx}. + /// + ///

+ /// + /// Reduce the subsets to singletons by choosing a minimum of each subset. If + /// the union of these alternative subsets is a singleton, then no amount of + /// more lookahead will help us. We will always pick that alternative. If, + /// however, there is more than one alternative, then we are uncertain which + /// alternative to predict and must continue looking for resolution. We may + /// or may not discover an ambiguity in the future, even if there are no + /// conflicting subsets this round. + /// + ///

+ /// + /// The biggest sin is to terminate early because it means we've made a + /// decision but were uncertain as to the eventual outcome. We haven't used + /// enough lookahead. On the other hand, announcing a conflict too late is no + /// big deal; you will still have the conflict. It's just inefficient. It + /// might even look until the end of file. + /// + ///

+ /// + /// No special consideration for semantic predicates is required because + /// predicates are evaluated on-the-fly for full LL prediction, ensuring that + /// no configuration contains a semantic context during the termination + /// check. + /// + ///

+ /// + /// CONFLICTING CONFIGS + /// + ///

+ /// + /// Two configurations {@code (s, i, x)} and {@code (s, j, x')}, conflict + /// when {@code i!=j} but {@code x=x'}. Because we merge all + /// {@code (s, i, _)} configurations together, that means that there are at + /// most {@code n} configurations associated with state {@code s} for + /// {@code n} possible alternatives in the decision. The merged stacks + /// complicate the comparison of configuration contexts {@code x} and + /// {@code x'}. Sam checks to see if one is a subset of the other by calling + /// merge and checking to see if the merged result is either {@code x} or + /// {@code x'}. If the {@code x} associated with lowest alternative {@code i} + /// is the superset, then {@code i} is the only possible prediction since the + /// others resolve to {@code min(i)} as well. However, if {@code x} is + /// associated with {@code j>i} then at least one stack configuration for + /// {@code j} is not in conflict with alternative {@code i}. The algorithm + /// should keep going, looking for more lookahead due to the uncertainty. + /// + ///

+ /// + /// For simplicity, I'm doing a equality check between {@code x} and + /// {@code x'} that lets the algorithm continue to consume lookahead longer + /// than necessary. The reason I like the equality is of course the + /// simplicity but also because that is the test you need to detect the + /// alternatives that are actually in conflict. + /// + ///

+ /// + /// CONTINUE/STOP RULE + /// + ///

+ /// + /// Continue if union of resolved alternative sets from non-conflicting and + /// conflicting alternative subsets has more than one alternative. We are + /// uncertain about which alternative to predict. + /// + ///

+ /// + /// The complete set of alternatives, {@code [i for (_,i,_)]}, tells us which + /// alternatives are still in the running for the amount of input we've + /// consumed at this point. The conflicting sets let us to strip away + /// configurations that won't lead to more states because we resolve + /// conflicts to the configuration with a minimum alternate for the + /// conflicting set. + /// + ///

+ /// + /// CASES + /// + ///

    + /// + ///
  • no conflicts and more than 1 alternative in set => continue
  • + /// + ///
  • {@code (s, 1, x)}, {@code (s, 2, x)}, {@code (s, 3, z)}, + /// {@code (s', 1, y)}, {@code (s', 2, y)} yields non-conflicting set + /// {@code {3}} U conflicting sets {@code min({1,2})} U {@code min({1,2})} = + /// {@code {1,3}} => continue + ///
  • + /// + ///
  • {@code (s, 1, x)}, {@code (s, 2, x)}, {@code (s', 1, y)}, + /// {@code (s', 2, y)}, {@code (s'', 1, z)} yields non-conflicting set + /// {@code {1}} U conflicting sets {@code min({1,2})} U {@code min({1,2})} = + /// {@code {1}} => stop and predict 1
  • + /// + ///
  • {@code (s, 1, x)}, {@code (s, 2, x)}, {@code (s', 1, y)}, + /// {@code (s', 2, y)} yields conflicting, reduced sets {@code {1}} U + /// {@code {1}} = {@code {1}} => stop and predict 1, can announce + /// ambiguity {@code {1,2}}
  • + /// + ///
  • {@code (s, 1, x)}, {@code (s, 2, x)}, {@code (s', 2, y)}, + /// {@code (s', 3, y)} yields conflicting, reduced sets {@code {1}} U + /// {@code {2}} = {@code {1,2}} => continue
  • + /// + ///
  • {@code (s, 1, x)}, {@code (s, 2, x)}, {@code (s', 3, y)}, + /// {@code (s', 4, y)} yields conflicting, reduced sets {@code {1}} U + /// {@code {3}} = {@code {1,3}} => continue
  • + /// + ///
+ /// + /// EXACT AMBIGUITY DETECTION + /// + ///

+ /// + /// If all states report the same conflicting set of alternatives, then we + /// know we have the exact ambiguity set. + /// + ///

+ /// + /// |A_i|>1 and + /// A_i = A_j for all i, j. + /// + ///

+ /// + /// In other words, we continue examining lookahead until all {@code A_i} + /// have more than one alternative and all {@code A_i} are the same. If + /// {@code A={{1,2}, {1,3}}}, then regular LL prediction would terminate + /// because the resolved set is {@code {1}}. To determine what the real + /// ambiguity is, we have to know whether the ambiguity is between one and + /// two or one and three so we keep going. We can only stop prediction when + /// we need exact ambiguity detection when the sets look like + /// {@code A={{1,2}}} or {@code {{1,2},{1,2}}}, etc... + ///

+ static int resolvesToJustOneViableAlt(const std::vector &altsets); + + /// + /// Determines if every alternative subset in {@code altsets} contains more + /// than one alternative. + /// + /// a collection of alternative subsets + /// {@code true} if every in {@code altsets} + /// has + /// > 1, otherwise {@code + /// false} + static bool allSubsetsConflict(const std::vector &altsets); + + /// + /// Determines if any single alternative subset in {@code altsets} contains + /// exactly one alternative. + /// + /// a collection of alternative subsets + /// {@code true} if {@code altsets} contains a with + /// 1, otherwise {@code false} + /// + static bool hasNonConflictingAltSet(const std::vector &altsets); + + /// + /// Determines if any single alternative subset in {@code altsets} contains + /// more than one alternative. + /// + /// a collection of alternative subsets + /// {@code true} if {@code altsets} contains a with + /// > 1, otherwise {@code + /// false} + static bool hasConflictingAltSet(const std::vector &altsets); + + /// + /// Determines if every alternative subset in {@code altsets} is equivalent. + /// + /// a collection of alternative subsets + /// {@code true} if every member of {@code altsets} is equal to the + /// others, otherwise {@code false} + static bool allSubsetsEqual(const std::vector &altsets); + + /// + /// Returns the unique alternative predicted by all alternative subsets in + /// {@code altsets}. If no such alternative exists, this method returns + /// . + /// + /// a collection of alternative subsets + static int getUniqueAlt(const std::vector &altsets); + + /// + /// Gets the complete set of represented alternatives for a collection of + /// alternative subsets. This method returns the union of each + /// in {@code altsets}. + /// + /// a collection of alternative subsets + /// the set of represented alternatives in {@code altsets} + static antlrcpp::BitSet getAlts(const std::vector &altsets); + + /// + /// This function gets the conflicting alt subsets from a configuration set. + /// For each configuration {@code c} in {@code configs}: + /// + ///
+						/// map[c] U= c. # map hash/equals uses s and
+						/// x, not
+						/// alt and not pred
+						/// 
+ ///
+ static std::vector getConflictingAltSubsets(ATNConfigSet *configs); + + /// + /// Get a map from state to alt subset from a configuration set. For each + /// configuration {@code c} in {@code configs}: + /// + ///
+						/// map[c.] U= c.
+						/// 
+ ///
+ static std::map getStateToAltMap(ATNConfigSet *configs); + + // TODO -- Add docs. + static bool hasStateAssociatedWithOneAlt(ATNConfigSet *configs); + + // TODO -- Add docs. + static int getSingleViableAlt(const std::vector &altsets); + }; + } // namespace atn + } // namespace runtime + } // namespace v4 + } // namespace antlr +} // namespace org \ No newline at end of file diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/RangeTransition.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/RangeTransition.cpp new file mode 100755 index 000000000..d93b2429f --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/RangeTransition.cpp @@ -0,0 +1,63 @@ +#include "RangeTransition.h" +#include "ATNState.h" +#include "IntervalSet.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + RangeTransition::RangeTransition(ATNState *target, int from, int to) : Transition(target), from(from), to(to) { + } + + int RangeTransition::getSerializationType() { + return RANGE; + } + + misc::IntervalSet *RangeTransition::label() { + return misc::IntervalSet::of(from, to); + } + + bool RangeTransition::matches(int symbol, int minVocabSymbol, int maxVocabSymbol) { + return symbol >= from && symbol <= to; + } + + std::wstring RangeTransition::toString() { + return std::wstring(L"'") + static_cast(from) + std::wstring(L"'..'") + static_cast(to) + std::wstring(L"'"); + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/RangeTransition.h b/runtime/Cpp/org/antlr/v4/runtime/atn/RangeTransition.h new file mode 100755 index 000000000..02242596e --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/RangeTransition.h @@ -0,0 +1,62 @@ +#pragma once + +#include "Transition.h" +#include "Declarations.h" +#include + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + class RangeTransition final : public Transition { + public: + const int from; + const int to; + + RangeTransition(ATNState *target, int from, int to); + + virtual int getSerializationType() override; + + virtual misc::IntervalSet *label() override; + virtual bool matches(int symbol, int minVocabSymbol, int maxVocabSymbol) override; + + virtual std::wstring toString(); + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/RuleStartState.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/RuleStartState.cpp new file mode 100755 index 000000000..4a63ecfc3 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/RuleStartState.cpp @@ -0,0 +1,50 @@ +#include "RuleStartState.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + int RuleStartState::getStateType() { + return RULE_START; + } + + void RuleStartState::InitializeInstanceFields() { + isPrecedenceRule = false; + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/RuleStartState.h b/runtime/Cpp/org/antlr/v4/runtime/atn/RuleStartState.h new file mode 100755 index 000000000..1094ea05c --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/RuleStartState.h @@ -0,0 +1,62 @@ +#pragma once + +#include "ATNState.h" +#include "RuleStopState.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + class RuleStartState final : public ATNState { + public: + RuleStopState *stopState; + bool isPrecedenceRule; + + virtual int getStateType(); + + private: + void InitializeInstanceFields(); + + public: + RuleStartState() { + InitializeInstanceFields(); + } + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/RuleStopState.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/RuleStopState.cpp new file mode 100755 index 000000000..b16e1ea22 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/RuleStopState.cpp @@ -0,0 +1,46 @@ +#include "RuleStopState.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + int RuleStopState::getStateType() { + return RULE_STOP; + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/RuleStopState.h b/runtime/Cpp/org/antlr/v4/runtime/atn/RuleStopState.h new file mode 100755 index 000000000..e3933328c --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/RuleStopState.h @@ -0,0 +1,58 @@ +#pragma once + +#include "ATNState.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + /// + /// The last node in the ATN for a rule, unless that rule is the start symbol. + /// In that case, there is one transition to EOF. Later, we might encode + /// references to all calls to this rule to compute FOLLOW sets for + /// error handling. + /// + class RuleStopState final : public ATNState { + + public: + virtual int getStateType() override; + + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/RuleTransition.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/RuleTransition.cpp new file mode 100755 index 000000000..374f060ee --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/RuleTransition.cpp @@ -0,0 +1,62 @@ +#include "RuleTransition.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + // this(ruleStart, ruleIndex, 0, followState); + RuleTransition::RuleTransition(RuleStartState *ruleStart, int ruleIndex, ATNState *followState) : Transition(followState), ruleIndex(ruleIndex), precedence(0) { + } + + RuleTransition::RuleTransition(RuleStartState *ruleStart, int ruleIndex, int precedence, ATNState *followState) : Transition(followState), ruleIndex(ruleIndex), precedence(precedence) { + this->followState = followState; + } + + int RuleTransition::getSerializationType() { + return RULE; + } + + bool RuleTransition::isEpsilon() { + return true; + } + + bool RuleTransition::matches(int symbol, int minVocabSymbol, int maxVocabSymbol) { + return false; + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/RuleTransition.h b/runtime/Cpp/org/antlr/v4/runtime/atn/RuleTransition.h new file mode 100755 index 000000000..e707224fa --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/RuleTransition.h @@ -0,0 +1,70 @@ +#pragma once + +#include "Transition.h" +#include "Declarations.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + class RuleTransition final : public Transition { + /// + /// Ptr to the rule definition object for this rule ref + public: + const int ruleIndex; // no Rule object at runtime + + const int precedence; + + /// + /// What node to begin computations following ref to rule + ATNState *followState; + + /// @deprecated Use + /// instead. + RuleTransition(RuleStartState *ruleStart, int ruleIndex, ATNState *followState); + + RuleTransition(RuleStartState *ruleStart, int ruleIndex, int precedence, ATNState *followState); + + virtual int getSerializationType() override; + + virtual bool isEpsilon() override; + virtual bool matches(int symbol, int minVocabSymbol, int maxVocabSymbol) override; + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/SemanticContext.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/SemanticContext.cpp new file mode 100755 index 000000000..962930af5 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/SemanticContext.cpp @@ -0,0 +1,294 @@ +#include +#include + +#include "SemanticContext.h" +#include "MurmurHash.h" +#include "Utils.h" +#include "Arrays.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + SemanticContext::Predicate::Predicate() : ruleIndex(-1), predIndex(-1), isCtxDependent(false) { + } + + SemanticContext::Predicate::Predicate(int ruleIndex, int predIndex, bool isCtxDependent) : ruleIndex(ruleIndex), predIndex(predIndex), isCtxDependent(isCtxDependent) { + } + + std::wstring SemanticContext::toString() const { + // This is a pure virtual function, why does it need an impl? + throw new ASSERTException(L"SemanticContext::toString", L"Should never be called, abstract class"); + } + + int SemanticContext::hashCode() { + // This is a pure virtual function, why does it need an impl? + throw new ASSERTException(L"SemanticContext::hashCode", L"Should never be called, abstract class"); + } + + bool SemanticContext::equals(void *obj) { + // "SemanticContext::equals should have been called on a daughter class" + throw new ASSERTException(L"SemanticContext::equals", L"Should never be called, abstract class"); + } + + + int SemanticContext::Predicate::hashCode() { + int hashCode = misc::MurmurHash::initialize(); + hashCode = misc::MurmurHash::update(hashCode, ruleIndex); + hashCode = misc::MurmurHash::update(hashCode, predIndex); + hashCode = misc::MurmurHash::update(hashCode, isCtxDependent ? 1 : 0); + hashCode = misc::MurmurHash::finish(hashCode, 3); + return hashCode; + } + + bool SemanticContext::Predicate::equals(void *obj) { + if (!((Predicate*)obj != nullptr)) { + return false; + } + if (this == obj) { + return true; + } + Predicate *p = static_cast(obj); + return this->ruleIndex == p->ruleIndex && this->predIndex == p->predIndex && this->isCtxDependent == p->isCtxDependent; + } + + std::wstring SemanticContext::Predicate::toString() const { + return std::wstring(L"{") + std::to_wstring(ruleIndex) + std::wstring(L":") + std::to_wstring(predIndex) + std::wstring(L"}?"); + } + + SemanticContext::PrecedencePredicate::PrecedencePredicate() : precedence(0) { + } + + SemanticContext::PrecedencePredicate::PrecedencePredicate(int precedence) : precedence(precedence) { + } + + int SemanticContext::PrecedencePredicate::compareTo(PrecedencePredicate *o) { + return precedence - o->precedence; + } + + int SemanticContext::PrecedencePredicate::hashCode() { + int hashCode = 1; + hashCode = 31 * hashCode + precedence; + return hashCode; + } + + bool SemanticContext::PrecedencePredicate::equals(void *obj) { + // TODO: this is wrong + if (!((Predicate*)obj/*dynamic_cast(obj)*/ != nullptr)) { + return false; + } + + if (this == obj) { + return true; + } + + PrecedencePredicate *other = static_cast(obj); + return this->precedence == other->precedence; + } + + std::wstring SemanticContext::PrecedencePredicate::toString() const { + return SemanticContext::toString(); + } + + + SemanticContext::AND::AND(SemanticContext *a, SemanticContext *b) { + std::vector *operands = new std::vector(); + + if (dynamic_cast(a) != nullptr) { + const std::vector op = ((AND*)a)->opnds; + for (auto var : op) { + operands->insert(op.end(), var); + } + } else { + operands->insert(operands->end(), a); + } + if (dynamic_cast(b) != nullptr) { + const std::vector op = ((AND*)b)->opnds; + for (auto var : op) { + operands->insert(op.end(), var); + } + } else { + operands->insert(operands->end(), b); + } + + std::vector precedencePredicates = + filterPrecedencePredicates(operands); + + if (!precedencePredicates.empty()) { + // interested in the transition with the lowest precedence + PrecedencePredicate *reduced = std::min_element(*precedencePredicates.begin(), + *precedencePredicates.end(), + (*SemanticContext::PrecedencePredicate::lessThan)); + operands->insert(operands->end(), reduced); + } + + for (auto op : *operands) { + opnds.insert(opnds.end(), op); + } + + } + + bool SemanticContext::AND::equals(void *obj) { + if (this == obj) { + return true; + } + if (!((AND*)obj != nullptr)) { + return false; + } + AND *other = static_cast(obj); + return (this->opnds == other->opnds); + } + + + int SemanticContext::AND::hashCode() { + return misc::MurmurHash::hashCode(opnds.data(), + opnds.size(), (int)typeid(AND).hash_code()); + } + + + + std::wstring SemanticContext::AND::toString() const { + std::wstring tmp; + for(auto var : opnds) { + tmp += var->toString() + L"&&"; + } + return tmp; + } + + SemanticContext::OR::OR(SemanticContext *a, SemanticContext *b){ + std::vector *operands = new std::vector(); + + //opnds = operands::toArray(new SemanticContext[operands->size()]); + + if (dynamic_cast(a) != nullptr) { + const std::vector op = ((OR*)a)->opnds; + for (auto var : op) { + operands->insert(op.end(), var); + } + } else { + operands->insert(operands->end(), a); + } + if (dynamic_cast(b) != nullptr) { + const std::vector op = ((OR*)b)->opnds; + for (auto var : op) { + operands->insert(op.end(), var); + } + } else { + operands->insert(operands->end(), b); + } + + std::vector precedencePredicates = filterPrecedencePredicates(operands); + if (!precedencePredicates.empty()) { + // interested in the transition with the highest precedence + PrecedencePredicate *reduced = std::max_element(*precedencePredicates.begin(), + *precedencePredicates.end(), + (*SemanticContext::PrecedencePredicate::greaterThan)); + operands->insert(operands->end(), reduced); + } + for (auto op : *operands) { + opnds.insert(opnds.end(), op); + } + } + + bool SemanticContext::OR::equals(SemanticContext *obj) { + if (this == obj) { + return true; + } + + if (obj == nullptr || typeid(*obj) != typeid(*this)) { + return false; + } + + OR *other = static_cast(obj); + + return this->opnds == other->opnds; + } + + int SemanticContext::OR::hashCode() { + return misc::MurmurHash::hashCode(opnds.data(), opnds.size(), (int)typeid(OR).hash_code()); + } + + + std::wstring SemanticContext::OR::toString() const { + std::wstring tmp; + for(auto var : opnds) { + tmp += var->toString() + L"||"; + } + return tmp; + } + + SemanticContext *const SemanticContext::NONE = new Predicate(); + + atn::SemanticContext *SemanticContext::And(SemanticContext *a, SemanticContext *b) { + if (a == nullptr || a == NONE) { + return b; + } + if (b == nullptr || b == NONE) { + return a; + } + AND *result = new AND(a, b); + if (result->opnds.size() == 1) { + return result->opnds[0]; + } + + return result; + } + + SemanticContext *SemanticContext::Or(SemanticContext *a, SemanticContext *b) { + if (a == nullptr) { + return b; + } + if (b == nullptr) { + return a; + } + if (a == NONE || b == NONE) { + return NONE; + } + OR *result = new OR(a, b); + if (result->opnds.size() == 1) { + return result->opnds[0]; + } + + return result; + } + + } + } + } + } +} + + + + diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/SemanticContext.h b/runtime/Cpp/org/antlr/v4/runtime/atn/SemanticContext.h new file mode 100755 index 000000000..9d1635c69 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/SemanticContext.h @@ -0,0 +1,248 @@ +#pragma once + + +#include +#include +#include + +#include "Declarations.h" +#include "Recognizer.h" +#include "SemanticContext.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + /// + /// A tree structure used to record the semantic context in which + /// an ATN configuration is valid. It's either a single predicate, + /// a conjunction {@code p1&&p2}, or a sum of products {@code p1||p2}. + ///

+ /// I have scoped the , , and subclasses of + /// within the scope of this outer class. + ///

+ class SemanticContext { + + public: + static SemanticContext *const NONE; + + virtual int hashCode() = 0; + + class Predicate; + class PrecedencePredicate; + class AND; + class OR; + + SemanticContext *parent; + + virtual std::wstring toString() const = 0; + + virtual bool equals(void *obj); + + /// + /// For context independent predicates, we evaluate them without a local + /// context (i.e., null context). That way, we can evaluate them without + /// having to create proper rule-specific context during prediction (as + /// opposed to the parser, which creates them naturally). In a practical + /// sense, this avoids a cast exception from RuleContext to myruleContext. + ///

+ /// For context dependent predicates, we must pass in a local context so that + /// references such as $arg evaluate properly as _localctx.arg. We only + /// capture context dependent predicates in the context in which we begin + /// prediction, so we passed in the outer context here in case of context + /// dependent predicate evaluation. + ///

+ + // Abstract + template + bool eval(Recognizer *parser, RuleContext *outerContext) { + // In the original Java this is abstract, but + // C++ complains with a link error, and we + // cannot make a template function abstract + throw new ASSERTException(L"SemanticContext::eval", L"Should never be called, abstract class"); + } + + static SemanticContext *And(SemanticContext *a, SemanticContext *b); + + /// + /// + static SemanticContext *Or(SemanticContext *a, SemanticContext *b); + + private: + template // where T1 : SemanticContext + static std::vector filterPrecedencePredicates(std::vector *collection) { + std::vector result; + for (std::vector::const_iterator iterator = collection->begin(); iterator != collection->end(); ++iterator) { + SemanticContext *context = *iterator; + if ((PrecedencePredicate*)(context) != nullptr) { + result.push_back((PrecedencePredicate*)context); + } + } + + return result; + } + + }; + + + class SemanticContext::Predicate : public SemanticContext { + friend class SemanticContext; + + public: + const int ruleIndex; + const int predIndex; + const bool isCtxDependent; // e.g., $i ref in pred + + protected: + Predicate(); + + public: + Predicate(int ruleIndex, int predIndex, bool isCtxDependent); + + template + bool eval(Recognizer *parser, RuleContext *outerContext) { + RuleContext *localctx = isCtxDependent ? outerContext : nullptr; + return parser->sempred(localctx, ruleIndex, predIndex); + } + + virtual int hashCode() override; + + virtual bool equals(void *obj); + + virtual std::wstring toString() const override; + }; + + class SemanticContext::PrecedencePredicate : public SemanticContext { + public: + const int precedence; + + protected: + PrecedencePredicate(); + + public: + PrecedencePredicate(int precedence); + + template + bool eval(Recognizer *parser, RuleContext *outerContext) { + return parser->precpred(outerContext, precedence); + } + + virtual int compareTo(PrecedencePredicate *o); + + virtual int hashCode() override; + + virtual bool equals(void *obj); + + virtual std::wstring toString() const override; + + static bool lessThan(const PrecedencePredicate &a, + const PrecedencePredicate &b) { + return a.precedence < b.precedence; + } + static bool greaterThan(const PrecedencePredicate &a, + const PrecedencePredicate &b) { + return a.precedence > b.precedence; + } + }; + + class SemanticContext::AND : public SemanticContext { + public: + std::vector opnds; + + AND(SemanticContext *a, SemanticContext *b); + + virtual bool equals(void *obj); + + virtual int hashCode() override; + + template + bool eval(Recognizer *parser, RuleContext *outerContext) { + for (auto opnd : opnds) { + if (!opnd->eval(parser, outerContext)) { + return false; + } + } + return true; + } + + + virtual std::wstring toString() const override; + }; + + class SemanticContext::OR : public SemanticContext { + public: + std::vector opnds; + + OR(SemanticContext *a, SemanticContext *b); + + virtual bool equals(SemanticContext *obj); + + virtual int hashCode(); + + template + bool eval(Recognizer *parser, RuleContext *outerContext) { + for (auto opnd : opnds) { + if (opnd->eval(parser, outerContext)) { + return true; + } + } + return false; + } + + virtual std::wstring toString() const override; + }; + + } + } + } + } +} + +// Hash function for SemanticContext, used in the MurmurHash::update function + +namespace std { + using org::antlr::v4::runtime::atn::SemanticContext; + + template <> struct hash + { + size_t operator()( SemanticContext & x) const + { + return x.hashCode(); + } + }; +} + + + diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/SetTransition.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/SetTransition.cpp new file mode 100755 index 000000000..8bf052226 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/SetTransition.cpp @@ -0,0 +1,65 @@ +#include "SetTransition.h" +#include "Token.h" +#include "IntervalSet.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + + SetTransition::SetTransition(ATNState *target, misc::IntervalSet *aSet) : + Transition(target), set(aSet==nullptr?misc::IntervalSet::of(Token::INVALID_TYPE):aSet) { + } + + int SetTransition::getSerializationType() { + return SET; + } + + misc::IntervalSet *SetTransition::label() { + return set; + } + + bool SetTransition::matches(int symbol, int minVocabSymbol, int maxVocabSymbol) { + return set->contains(symbol); + } + + std::wstring SetTransition::toString() { + return set->toString(); + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/SetTransition.h b/runtime/Cpp/org/antlr/v4/runtime/atn/SetTransition.h new file mode 100755 index 000000000..efebe29e8 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/SetTransition.h @@ -0,0 +1,63 @@ +#pragma once + +#include "Transition.h" +#include "Declarations.h" +#include + + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + /// + /// A transition containing a set of values. + class SetTransition : public Transition { + public: + misc::IntervalSet * const set; + + SetTransition(ATNState *target, misc::IntervalSet *set); + + virtual int getSerializationType() override; + + virtual misc::IntervalSet *label() override; + virtual bool matches(int symbol, int minVocabSymbol, int maxVocabSymbol) override; + + virtual std::wstring toString(); + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/SingletonPredictionContext.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/SingletonPredictionContext.cpp new file mode 100755 index 000000000..f6e3eebae --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/SingletonPredictionContext.cpp @@ -0,0 +1,96 @@ +#include "SingletonPredictionContext.h" +#include "ATNState.h" +#include + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + SingletonPredictionContext::SingletonPredictionContext(PredictionContext *parent, int returnState) : PredictionContext(parent != nullptr ? calculateHashCode(parent, returnState) : calculateEmptyHashCode()), parent(parent), returnState(returnState) { + assert(returnState != ATNState::INVALID_STATE_NUMBER); + } + + atn::SingletonPredictionContext *SingletonPredictionContext::create(PredictionContext *parent, int returnState) { + if (returnState == EMPTY_RETURN_STATE && parent == nullptr) { + // someone can pass in the bits of an array ctx that mean $ + return (atn::SingletonPredictionContext *)EMPTY; + } + return new SingletonPredictionContext(parent, returnState); + } + + int SingletonPredictionContext::size() { + return 1; + } + + atn::PredictionContext *SingletonPredictionContext::getParent(int index) { + assert(index == 0); + return parent; + } + + int SingletonPredictionContext::getReturnState(int index) { + assert(index == 0); + return returnState; + } + + bool SingletonPredictionContext::equals(void *o) { + if (this == o) { + return true; + } else if (!(/*dynamic_cast(o)*/o != nullptr)) { + return false; + } + + if (this->hashCode() != ((SingletonPredictionContext*)o)->hashCode()) { + return false; // can't be same if hash is different + } + + SingletonPredictionContext *s = static_cast(o); + return returnState == s->returnState && (parent != nullptr && parent->equals(s->parent)); + } + + std::wstring SingletonPredictionContext::toString() { + std::wstring up = parent != nullptr ? parent->toString() : L""; + if (up.length() == 0) { + if (returnState == EMPTY_RETURN_STATE) { + return L"$"; + } + return antlrcpp::StringConverterHelper::toString(returnState); + } + return antlrcpp::StringConverterHelper::toString(returnState) + std::wstring(L" ") + up; + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/SingletonPredictionContext.h b/runtime/Cpp/org/antlr/v4/runtime/atn/SingletonPredictionContext.h new file mode 100755 index 000000000..d15591fbb --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/SingletonPredictionContext.h @@ -0,0 +1,68 @@ +#pragma once + +#include "stringconverter.h" +#include "PredictionContext.h" +#include + + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + class SingletonPredictionContext : public PredictionContext { + public: + PredictionContext *const parent; + const int returnState; + + SingletonPredictionContext(PredictionContext *parent, int returnState); + + static SingletonPredictionContext *create(PredictionContext *parent, int returnState); + + virtual int size() override; + + virtual PredictionContext *getParent(int index) override; + + virtual int getReturnState(int index) override; + + virtual bool equals(void *o) override; + + virtual std::wstring toString() ; + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/StarBlockStartState.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/StarBlockStartState.cpp new file mode 100755 index 000000000..1a75d163f --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/StarBlockStartState.cpp @@ -0,0 +1,46 @@ +#include "StarBlockStartState.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + int StarBlockStartState::getStateType() { + return STAR_BLOCK_START; + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/StarBlockStartState.h b/runtime/Cpp/org/antlr/v4/runtime/atn/StarBlockStartState.h new file mode 100755 index 000000000..dd9fc9067 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/StarBlockStartState.h @@ -0,0 +1,53 @@ +#pragma once + +#include "BlockStartState.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + /// + /// The block that begins a closure loop. + class StarBlockStartState final : public BlockStartState { + + public: + virtual int getStateType() override; + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/StarLoopEntryState.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/StarLoopEntryState.cpp new file mode 100755 index 000000000..10ec1c755 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/StarLoopEntryState.cpp @@ -0,0 +1,46 @@ +#include "StarLoopEntryState.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + int StarLoopEntryState::getStateType() { + return STAR_LOOP_ENTRY; + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/StarLoopEntryState.h b/runtime/Cpp/org/antlr/v4/runtime/atn/StarLoopEntryState.h new file mode 100755 index 000000000..ad5ec9de8 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/StarLoopEntryState.h @@ -0,0 +1,53 @@ +#pragma once + +#include "DecisionState.h" +#include "StarLoopbackState.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + class StarLoopEntryState final : public DecisionState { + public: + StarLoopbackState *loopBackState; + + virtual int getStateType() override; + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/StarLoopbackState.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/StarLoopbackState.cpp new file mode 100755 index 000000000..f19c0af8a --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/StarLoopbackState.cpp @@ -0,0 +1,51 @@ +#include "StarLoopbackState.h" +#include "Transition.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + atn::StarLoopEntryState *StarLoopbackState::getLoopEntryState() { + return static_cast(transition(0)->target); + } + + int StarLoopbackState::getStateType() { + return STAR_LOOP_BACK; + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/StarLoopbackState.h b/runtime/Cpp/org/antlr/v4/runtime/atn/StarLoopbackState.h new file mode 100755 index 000000000..5374dc4f8 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/StarLoopbackState.h @@ -0,0 +1,53 @@ +#pragma once + +#include "ATNState.h" +#include "StarLoopEntryState.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + class StarLoopbackState final : public ATNState { + public: + StarLoopEntryState *getLoopEntryState(); + + virtual int getStateType() override; + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/TokensStartState.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/TokensStartState.cpp new file mode 100755 index 000000000..f5f356478 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/TokensStartState.cpp @@ -0,0 +1,46 @@ +#include "TokensStartState.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + int TokensStartState::getStateType() { + return TOKEN_START; + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/TokensStartState.h b/runtime/Cpp/org/antlr/v4/runtime/atn/TokensStartState.h new file mode 100755 index 000000000..a7cd38971 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/TokensStartState.h @@ -0,0 +1,53 @@ +#pragma once + +#include "DecisionState.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + /// + /// The Tokens rule start state linking to each lexer rule start state + class TokensStartState final : public DecisionState { + + public: + virtual int getStateType(); + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/Transition.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/Transition.cpp new file mode 100755 index 000000000..f36962874 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/Transition.cpp @@ -0,0 +1,63 @@ +#include "Transition.h" +#include "Exceptions.h" +#include +#include "Arrays.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + const std::vector Transition::serializationNames = *new std::vector(antlrcpp::Arrays::asList(11, L"INVALID", L"EPSILON", L"RANGE", L"RULE", L"PREDICATE", L"ATOM", L"ACTION", L"SET", L"NOT_SET", L"WILDCARD", L"PRECEDENCE")); + + Transition::Transition(ATNState *target) { + if (target == nullptr) { + throw NullPointerException(L"target cannot be null."); + } + + this->target = target; + } + + bool Transition::isEpsilon() { + return false; + } + + misc::IntervalSet *Transition::label() { + return nullptr; + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/Transition.h b/runtime/Cpp/org/antlr/v4/runtime/atn/Transition.h new file mode 100755 index 000000000..634da4b3b --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/Transition.h @@ -0,0 +1,99 @@ +#pragma once + +#include "ATNState.h" +#include "Declarations.h" + + +#include +#include +#include + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + /// + /// An ATN transition between any two ATN states. Subclasses define + /// atom, set, epsilon, action, predicate, rule transitions. + ///

+ /// This is a one way link. It emanates from a state (usually via a list of + /// transitions) and has a target state. + ///

+ /// Since we never have to change the ATN transitions once we construct it, + /// we can fix these transitions as specific classes. The DFA transitions + /// on the other hand need to update the labels as it adds transitions to + /// the states. We'll use the term Edge for the DFA to distinguish them from + /// ATN transitions. + ///

+ class Transition { + // constants for serialization + public: + static const int EPSILON = 1; + static const int RANGE = 2; + static const int RULE = 3; + static const int PREDICATE = 4; // e.g., {isType(input.LT(1))}? + static const int ATOM = 5; + static const int ACTION = 6; + static const int SET = 7; // ~(A|B) or ~atom, wildcard, which convert to next 2 + static const int NOT_SET = 8; + static const int WILDCARD = 9; + static const int PRECEDENCE = 10; + + + static const std::vector serializationNames; + + /// + /// The target of this transition. + ATNState *target; + + protected: + Transition(ATNState *target); + + public: + virtual int getSerializationType() = 0; + + /// + /// Are we epsilon, action, sempred? + virtual bool isEpsilon(); + + virtual misc::IntervalSet *label(); + + virtual bool matches(int symbol, int minVocabSymbol, int maxVocabSymbol) = 0; + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/WildcardTransition.cpp b/runtime/Cpp/org/antlr/v4/runtime/atn/WildcardTransition.cpp new file mode 100755 index 000000000..cbcc2bc4c --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/WildcardTransition.cpp @@ -0,0 +1,58 @@ +#include "WildcardTransition.h" +#include "ATNState.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + WildcardTransition::WildcardTransition(ATNState *target) : Transition(target) { + } + + int WildcardTransition::getSerializationType() { + return WILDCARD; + } + + bool WildcardTransition::matches(int symbol, int minVocabSymbol, int maxVocabSymbol) { + return symbol >= minVocabSymbol && symbol <= maxVocabSymbol; + } + + std::wstring WildcardTransition::toString() { + return L"."; + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/atn/WildcardTransition.h b/runtime/Cpp/org/antlr/v4/runtime/atn/WildcardTransition.h new file mode 100755 index 000000000..78554d8aa --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/atn/WildcardTransition.h @@ -0,0 +1,59 @@ +#pragma once + +#include "Transition.h" +#include "Declarations.h" +#include + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace atn { + + + class WildcardTransition final : public Transition { + public: + WildcardTransition(ATNState *target); + + virtual int getSerializationType() override; + + virtual bool matches(int symbol, int minVocabSymbol, int maxVocabSymbol) override; + + virtual std::wstring toString() ; + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/dfa/DFA.cpp b/runtime/Cpp/org/antlr/v4/runtime/dfa/DFA.cpp new file mode 100755 index 000000000..f4f2d5464 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/dfa/DFA.cpp @@ -0,0 +1,101 @@ +#include +#include + +#include "DFA.h" +#include "DFAState.h" +#include "DFASerializer.h" +#include "LexerDFASerializer.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace dfa { + + DFA::DFA(atn::DecisionState *atnStartState) : atnStartState(atnStartState), states(new std::map()), decision(0) { + } + + DFA::DFA(atn::DecisionState *atnStartState, int decision) : atnStartState(atnStartState), states(new std::map()), decision(decision) { + } + + std::vector DFA::getStates() { + // Get all the keys, which C++ doesn't do natively, ugh + std::map mapints; + std::vector vints; + for(auto imap: mapints) { + vints.push_back(imap.first); + } + + // This ComparatorAnonymousInnerClassHelper isn't doing much, it + // could be accomplished simply with a local comparator function + std::vector result = std::vector(vints); + ComparatorAnonymousInnerClassHelper tmp(this); + std::sort(result.begin(), result.end(), (tmp.compare)); + + return result; + } + + + DFA::ComparatorAnonymousInnerClassHelper::ComparatorAnonymousInnerClassHelper(DFA *outerInstance) : outerInstance(outerInstance) { + } + + int DFA::ComparatorAnonymousInnerClassHelper::compare(DFAState *o1, DFAState *o2) { + return o1->stateNumber - o2->stateNumber; + } + + std::wstring DFA::toString() { + std::vector tokenNames; + return toString(tokenNames); + } + + std::wstring DFA::toString(const std::vector& tokenNames) { + if (s0 == nullptr) { + return L""; + } + DFASerializer *serializer = new DFASerializer(this, tokenNames); + + return serializer->toString(); + } + + std::wstring DFA::toLexerString() { + if (s0 == nullptr) { + return L""; + } + DFASerializer *serializer = new LexerDFASerializer(this); + + return serializer->toString(); + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/dfa/DFA.h b/runtime/Cpp/org/antlr/v4/runtime/dfa/DFA.h new file mode 100755 index 000000000..e736eee21 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/dfa/DFA.h @@ -0,0 +1,92 @@ +#pragma once + +#include +#include +#include + +#include "Declarations.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace dfa { + + + + class DFA { + /// + /// A set of all DFA states. Use so we can get old state back + /// ( only allows you to see if it's there). + /// + public: + /// + /// From which ATN state did we create this DFA? + atn::DecisionState *const atnStartState; + std::map *const states; + DFAState *s0; + const int decision; + + DFA(atn::DecisionState *atnStartState); //this(atnStartState, 0); + + DFA(atn::DecisionState *atnStartState, int decision); + + /// + /// Return a list of all states in this DFA, ordered by state number. + /// + virtual std::vector getStates(); + + private: + class ComparatorAnonymousInnerClassHelper { + private: + DFA *const outerInstance; + + public: + ComparatorAnonymousInnerClassHelper(DFA *outerInstance); + + static int compare(DFAState *o1, DFAState *o2); + }; + + public: + virtual std::wstring toString(); + + virtual std::wstring toString(const std::vector& tokenNames); + virtual std::wstring toLexerString(); + + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/dfa/DFASerializer.cpp b/runtime/Cpp/org/antlr/v4/runtime/dfa/DFASerializer.cpp new file mode 100755 index 000000000..ab0599805 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/dfa/DFASerializer.cpp @@ -0,0 +1,110 @@ +#include "DFASerializer.h" +#include "DFA.h" +#include "StringBuilder.h" +#include "DFAState.h" +#include +#include + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace dfa { + + // TODO -- Make sure this reference doesn't go away prematurely. + DFASerializer::DFASerializer(DFA *dfa, const std::vector& tokenNames) : dfa(dfa), tokenNames_(tokenNames) { + } + + std::wstring DFASerializer::toString() { + if (dfa->s0 == nullptr) { + return L""; + } + antlrcpp::StringBuilder *buf = new antlrcpp::StringBuilder(); + std::vector states = dfa->getStates(); + for (auto s : states) { + int n = 0; + n = (int)s->edges.size(); + for (int i = 0; i < n; i++) { + DFAState *t = s->edges[i]; + if (t != nullptr && t->stateNumber != INT16_MAX) { + buf->append(getStateString(s)); + std::wstring label = getEdgeLabel(i); + buf->append(L"-"); buf->append(label); buf->append(L"->"); buf->append(getStateString(t)); buf->append(L"\n"); + } + } + } + + std::wstring output = buf->toString(); + if (output.length() == 0) { + return L""; + } + //return Utils.sortLinesInString(output); + return output; + } + + std::wstring DFASerializer::getEdgeLabel(int i) { + std::wstring label; + if (i == 0) { + return L"EOF"; + } + if (!tokenNames_.empty()) { + label = tokenNames_[i - 1]; + } else { + label = antlrcpp::StringConverterHelper::toString(i - 1); + } + return label; + } + + std::wstring DFASerializer::getStateString(DFAState *s) { + size_t n = (size_t)s->stateNumber; + + const std::wstring baseStateStr = (s->isAcceptState ? L":" : L"") + std::wstring(L"s") + std::to_wstring(n) + (s->requiresFullContext ? L"^" : L""); + if (s->isAcceptState) { + if (s->predicates.size() != 0) { + std::wstring buf; + for (size_t i = 0; i < s->predicates.size(); i++) { + buf.append(s->predicates[i]->toString()); + } + return baseStateStr + std::wstring(L"=>") + buf; + } else { + return baseStateStr + std::wstring(L"=>") + std::to_wstring(s->prediction); + } + } else { + return baseStateStr; + } + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/dfa/DFASerializer.h b/runtime/Cpp/org/antlr/v4/runtime/dfa/DFASerializer.h new file mode 100755 index 000000000..7c1350aee --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/dfa/DFASerializer.h @@ -0,0 +1,65 @@ +#pragma once + +#include +#include +#include "stringconverter.h" + +#include "Declarations.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace dfa { + /// + /// A DFA walker that knows how to dump them to serialized strings. + class DFASerializer { + public: + DFA *const dfa; + const std::vector& tokenNames_; + + DFASerializer(DFA *dfa, const std::vector& tokenNames); + + virtual std::wstring toString(); + + protected: + virtual std::wstring getEdgeLabel(int i); + + virtual std::wstring getStateString(DFAState *s); + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/dfa/DFAState.cpp b/runtime/Cpp/org/antlr/v4/runtime/dfa/DFAState.cpp new file mode 100755 index 000000000..1c4d791bc --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/dfa/DFAState.cpp @@ -0,0 +1,139 @@ +#include "DFAState.h" +#include "StringBuilder.h" +#include "ATNConfigSet.h" +#include "SemanticContext.h" +#include "ATNConfig.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace dfa { + + DFAState::PredPrediction::PredPrediction(atn::SemanticContext *pred, int alt) { + InitializeInstanceFields(); + this->alt = alt; + this->pred = pred; + } + + std::wstring DFAState::PredPrediction::toString() { + return std::wstring(L"(") + pred->toString() + std::wstring(L", ") + std::to_wstring(alt) + std::wstring(L")"); + } + + void DFAState::PredPrediction::InitializeInstanceFields() { + alt = 0; + } + + DFAState::DFAState() { + InitializeInstanceFields(); + } + + DFAState::DFAState(int stateNumber) { + InitializeInstanceFields(); + this->stateNumber = stateNumber; + } + + DFAState::DFAState(atn::ATNConfigSet *configs) { + InitializeInstanceFields(); + this->configs = configs; + } + + std::set *DFAState::getAltSet() { + std::set *alts = new std::set(); + if (configs != nullptr) { + for (size_t i = 0; i < configs->size(); i++) { + alts->insert(configs->get((int)i)->alt); + } + } + if (alts->size() == 0) { + return nullptr; + } + return alts; + } + + int DFAState::hashCode() { + int hash = misc::MurmurHash::initialize(7); + hash = misc::MurmurHash::update(hash, configs->hashCode()); + hash = misc::MurmurHash::finish(hash, 1); + return hash; + } + + bool DFAState::equals(void *o) { + // compare set of ATN configurations in this set with other + if (this == o) { + return true; + } + + if ((DFAState*)o == nullptr) { + return false; + } + + DFAState *other = static_cast(o); + // TODO (sam): what to do when configs==null? + bool sameSet = this->configs->equals(other->configs); + // System.out.println("DFAState.equals: "+configs+(sameSet?"==":"!=")+other.configs); + return sameSet; + } + + std::wstring DFAState::toString() { + antlrcpp::StringBuilder *buf = new antlrcpp::StringBuilder(); + buf->append(std::to_wstring(stateNumber)); buf->append(L":"); buf->append(configs->toString()); + if (isAcceptState) { + buf->append(L"=>"); + if (predicates.size() != 0) { + std::wstring tmp; + for (size_t i = 0; i < predicates.size(); i++) { + tmp.append(predicates[i]->toString()); + } + buf->append(tmp); + } else { + buf->append(std::to_wstring(prediction)); + } + } + return buf->toString(); + } + + void DFAState::InitializeInstanceFields() { + stateNumber = -1; + configs = new org::antlr::v4::runtime::atn::ATNConfigSet(); + isAcceptState = false; + prediction = 0; + lexerRuleIndex = -1; + lexerActionIndex = -1; + requiresFullContext = false; + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/dfa/DFAState.h b/runtime/Cpp/org/antlr/v4/runtime/dfa/DFAState.h new file mode 100755 index 000000000..1b10ee9bf --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/dfa/DFAState.h @@ -0,0 +1,171 @@ +#pragma once + +#include +#include +#include + +#include "Declarations.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace dfa { + /// + /// A DFA state represents a set of possible ATN configurations. + /// As Aho, Sethi, Ullman p. 117 says "The DFA uses its state + /// to keep track of all possible states the ATN can be in after + /// reading each input symbol. That is to say, after reading + /// input a1a2..an, the DFA is in a state that represents the + /// subset T of the states of the ATN that are reachable from the + /// ATN's start state along some path labeled a1a2..an." + /// In conventional NFA->DFA conversion, therefore, the subset T + /// would be a bitset representing the set of states the + /// ATN could be in. We need to track the alt predicted by each + /// state as well, however. More importantly, we need to maintain + /// a stack of states, tracking the closure operations as they + /// jump from rule to rule, emulating rule invocations (method calls). + /// I have to add a stack to simulate the proper lookahead sequences for + /// the underlying LL grammar from which the ATN was derived. + ///

+ /// I use a set of ATNConfig objects not simple states. An ATNConfig + /// is both a state (ala normal conversion) and a RuleContext describing + /// the chain of rules (if any) followed to arrive at that state. + ///

+ /// A DFA state may have multiple references to a particular state, + /// but with different ATN contexts (with same or different alts) + /// meaning that state was reached via a different set of rule invocations. + ///

+ class DFAState { + public: + class PredPrediction { + public: + atn::SemanticContext *pred; // never null; at least SemanticContext.NONE + int alt; + PredPrediction(atn::SemanticContext *pred, int alt); + virtual std::wstring toString(); + + private: + void InitializeInstanceFields(); + }; + + public: + int stateNumber; + + atn::ATNConfigSet *configs; + + /// + /// {@code edges[symbol]} points to target of symbol. Shift up by 1 so (-1) + /// maps to {@code edges[0]}. + /// +//JAVA TO C++ CONVERTER WARNING: Since the array size is not known in this declaration, Java to C++ Converter has converted this array to a pointer. You will need to call 'delete[]' where appropriate: +//ORIGINAL LINE: @Nullable public DFAState[] edges; + std::vector edges; + + bool isAcceptState; + + /// + /// if accept state, what ttype do we match or alt do we predict? + /// This is set to when {@code !=null} or + /// . + /// + int prediction; + + int lexerRuleIndex; // if accept, exec action in what rule? + int lexerActionIndex; // if accept, exec what action? + + /// + /// Indicates that this state was created during SLL prediction that + /// discovered a conflict between the configurations in the state. Future + /// invocations immediately jumped doing + /// full context prediction if this field is true. + /// + bool requiresFullContext; + + /// + /// During SLL parsing, this is a list of predicates associated with the + /// ATN configurations of the DFA state. When we have predicates, + /// is {@code false} since full context prediction evaluates predicates + /// on-the-fly. If this is not null, then is + /// . + ///

+ /// We only use these for non- but conflicting states. That + /// means we know from the context (it's $ or we don't dip into outer + /// context) that it's an ambiguity not a conflict. + ///

+ /// This list is computed by . + ///

+ std::vector predicates; + + /// + /// Map a predicate to a predicted alternative. + DFAState(); + + DFAState(int stateNumber); + + DFAState(atn::ATNConfigSet *configs); + + /// + /// Get the set of all alts mentioned by all ATN configurations in this + /// DFA state. + /// + virtual std::set *getAltSet(); + + virtual int hashCode() ; + + /// + /// Two instances are equal if their ATN configuration sets + /// are the same. This method is used to see if a state already exists. + ///

+ /// Because the number of alternatives and number of ATN configurations are + /// finite, there is a finite number of DFA states that can be processed. + /// This is necessary to show that the algorithm terminates. + ///

+ /// Cannot test the DFA state numbers here because in + /// we need to know if any other state + /// exists that has this exact set of ATN configurations. The + /// is irrelevant. + ///

+ virtual bool equals(void *o); + + virtual std::wstring toString(); + + private: + void InitializeInstanceFields(); + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/dfa/LexerDFASerializer.cpp b/runtime/Cpp/org/antlr/v4/runtime/dfa/LexerDFASerializer.cpp new file mode 100755 index 000000000..7a1d77a96 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/dfa/LexerDFASerializer.cpp @@ -0,0 +1,48 @@ +#include "LexerDFASerializer.h" +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace dfa { + + LexerDFASerializer::LexerDFASerializer(DFA *dfa) : DFASerializer(dfa, lexerTokenNames_) { + } + + std::wstring LexerDFASerializer::getEdgeLabel(int i) { + return std::wstring(L"'") + static_cast(i) + std::wstring(L"'"); + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/dfa/LexerDFASerializer.h b/runtime/Cpp/org/antlr/v4/runtime/dfa/LexerDFASerializer.h new file mode 100755 index 000000000..6d6d24a5c --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/dfa/LexerDFASerializer.h @@ -0,0 +1,57 @@ +#pragma once + +#include "DFASerializer.h" +#include + +#include "Declarations.h" + + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace dfa { + class LexerDFASerializer : public DFASerializer { + public: + LexerDFASerializer(DFA *dfa); + + protected: + std::vector lexerTokenNames_; + virtual std::wstring getEdgeLabel(int i) override; + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/misc/AbstractEqualityComparator.cpp b/runtime/Cpp/org/antlr/v4/runtime/misc/AbstractEqualityComparator.cpp new file mode 100755 index 000000000..28f3da891 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/misc/AbstractEqualityComparator.cpp @@ -0,0 +1,42 @@ +#include "AbstractEqualityComparator.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace misc { + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/misc/AbstractEqualityComparator.h b/runtime/Cpp/org/antlr/v4/runtime/misc/AbstractEqualityComparator.h new file mode 100755 index 000000000..c02bf0163 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/misc/AbstractEqualityComparator.h @@ -0,0 +1,56 @@ +#pragma once + +#include "EqualityComparator.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace misc { + + /// + /// This abstract base class is provided so performance-critical applications can + /// use virtual- instead of interface-dispatch when calling comparator methods. + /// + /// @author Sam Harwell + /// + template + class AbstractEqualityComparator : public EqualityComparator { + + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/misc/Array2DHashSet.h b/runtime/Cpp/org/antlr/v4/runtime/misc/Array2DHashSet.h new file mode 100755 index 000000000..b432e0c72 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/misc/Array2DHashSet.h @@ -0,0 +1,223 @@ +#pragma once + +#include +#include +#include +#include + +#include "AbstractEqualityComparator.h" +#include "ObjectEqualityComparator.h" +#include "MurmurHash.h" +#include "Array2DHashSet.h" +#include "Exceptions.h" +#include "StringBuilder.h" +#include "Arrays.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace misc { + + + /// + /// implementation with closed hashing (open addressing). + template + class Array2DHashSet : public std::set { + protected: + // Daughter iterator class + class SetIterator : public std::iterator { + + Array2DHashSet *const outerInstance; + + public: +#ifdef TODO + // JAVA TO C++ CONVERTER WARNING: Since the array size is not known in + // this declaration, Java to C++ Converter has converted this array to + // a pointer. You will need to call 'delete[]' where appropriate: + ORIGINAL LINE: final T[] data; +#endif + const T *data; + int nextIndex; + bool removed; + + SetIterator(Array2DHashSet * const outerInstance, T data[]); + + // TODO; these are java overrides probably, not STL + virtual bool hasNext(); + + virtual T *next(); + + virtual void remove(); + + private: + void InitializeInstanceFields(); + }; + + public: + static const int INITAL_CAPACITY = 16; // must be power of 2 + static const int INITAL_BUCKET_CAPACITY = 8; + static const double LOAD_FACTOR; + + protected: + AbstractEqualityComparator *const comparator; + + std::vector> buckets; + + /// + /// How many elements in set + int n; + + int threshold; // when to expand + + int currentPrime; // jump by 4 primes each expand or whatever + int initialBucketCapacity; + + public: + Array2DHashSet() : comparator(nullptr) { + //this(nullptr, INITAL_CAPACITY, INITAL_BUCKET_CAPACITY); + } + template + Array2DHashSet(AbstractEqualityComparator *comparator); + + template + Array2DHashSet(AbstractEqualityComparator *comparator, + int initialCapacity, int initialBucketCapacity); + + /// + /// Add {@code o} to set if not there; return existing value if already + /// there. This method performs the same operation as aside from + /// the return value. + /// + T getOrAdd(T o); + + protected: + virtual T getOrAddImpl(T o); + + public: + virtual T get(T o); + + protected: + int getBucket(T o); + + public: + virtual int hashCode(); + + virtual bool equals(T o); + + protected: + virtual void expand(); + + public: + bool add(T t); + + int size(); + + bool isEmpty(); + + bool contains(void *o); + + virtual bool containsFast(T obj); + + virtual std::iterator *iterator(); + + virtual std::vector *toArray(); + + template + U *toArray(U a[]); + + bool remove(void *o); + + virtual bool removeFast(T obj); + + template + bool containsAll(std::set *collection); + + template + bool addAll(std::set *c) ; + + template + bool retainAll(std::set *c); + + template + bool removeAll(std::set *c); + + virtual void clear(); + + virtual std::wstring toString(); + + virtual std::wstring toTableString(); + + /// + /// Return {@code o} as an instance of the element type {@code T}. If + /// {@code o} is non-null but known to not be an instance of {@code T}, this + /// method returns {@code null}. The base implementation does not perform any + /// type checks; override this method to provide strong type checks for the + /// and methods to ensure the arguments to + /// the for the set always have the expected + /// types. + /// + /// the object to try and cast to the element type of the set + /// {@code o} if it could be an instance of {@code T}, otherwise + /// {@code null}. + protected: + virtual T asElementType(void *o) { + throw new TODOException(L"Array2DHashSet::asElementType"); + }; + + /// + /// Return an array of {@code T[]} with length {@code capacity}. + /// + /// the length of the array to return + /// the newly constructed array + virtual std::vector> * createBuckets(int capacity); + + /// + /// Return an array of {@code T} with length {@code capacity}. + /// + /// the length of the array to return + /// the newly constructed array + virtual std::vector * createBucket(int capacity); + + + private: + void InitializeInstanceFields(); + }; + + } + } + } + } +} + +#include "Array2DHashSet.inl" diff --git a/runtime/Cpp/org/antlr/v4/runtime/misc/Array2DHashSet.inl b/runtime/Cpp/org/antlr/v4/runtime/misc/Array2DHashSet.inl new file mode 100755 index 000000000..f76c25249 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/misc/Array2DHashSet.inl @@ -0,0 +1,575 @@ + +#pragma once + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace misc { + template + const double Array2DHashSet::LOAD_FACTOR = 0.75; + + template + Array2DHashSet::SetIterator::SetIterator(Array2DHashSet *const outerInstance, T data[]) + : data(data), outerInstance(outerInstance) { + InitializeInstanceFields(); + } + + template + bool Array2DHashSet::SetIterator::hasNext() { + return nextIndex < data->length; + } + + template + T *Array2DHashSet::SetIterator::next() { + if (!hasNext()) { + throw new NoSuchElementException(); + } + + removed = false; + return data[nextIndex++]; + } + + template + void Array2DHashSet::SetIterator::remove() { + if (removed) { + throw new IllegalStateException(); + } + + outerInstance->remove(data[nextIndex - 1]); + removed = true; + } + + template + void Array2DHashSet::SetIterator::InitializeInstanceFields() { + nextIndex = 0; + removed = true; + } + + template + template + Array2DHashSet::Array2DHashSet(AbstractEqualityComparator *comparator) { //this(comparator, INITAL_CAPACITY, INITAL_BUCKET_CAPACITY); + } + + template + template + Array2DHashSet::Array2DHashSet(AbstractEqualityComparator *comparator, int initialCapacity, int initialBucketCapacity) : comparator(ObjectEqualityComparator::INSTANCE) { + InitializeInstanceFields(); + if (comparator == nullptr) { + } + + this->comparator = comparator; + this->buckets = createBuckets(initialCapacity); + this->initialBucketCapacity = initialBucketCapacity; + } + + template + T Array2DHashSet::getOrAdd(T o) { + if (n > threshold) { + expand(); + } + return getOrAddImpl(o); + } + + template + T Array2DHashSet::getOrAddImpl(T o) { + int b = getBucket(o); + std::vector * bucket = &buckets[b]; + + // NEW BUCKET + if (bucket->size() == 0) { + bucket = createBucket(initialBucketCapacity); + (*bucket)[0] = o; + buckets[b] = *bucket; + n++; + return o; + } + + // LOOK FOR IT IN BUCKET + for (int i = 0; i < (int)bucket->size(); i++) { + T existing = (*bucket)[i]; + if (existing == nullptr) { // empty slot; not there, add. + (*bucket)[i] = o; + n++; + return o; + } + if (comparator->equals(existing, o)) { // found existing, quit + return existing; + } + } + + bucket->insert(bucket->end(), o); + n++; + return o; + } + + template + T Array2DHashSet::get(T o) { + if (o == nullptr) { + return o; + } + int b = getBucket(o); + std::vector bucket = buckets[b]; + if (bucket.size() == 0) { // no bucket + return nullptr; + } + for (auto e : bucket) { + if (e == nullptr) { // empty slot; not there + return nullptr; + } + if (comparator->equals(e, o)) { + return e; + } + } + return nullptr; + } + + template + int Array2DHashSet::getBucket(T o) { + int hash = comparator->hashCode(o); + int b = hash & (buckets.size() - 1); // assumes len is power of 2 + return b; + } + + template + int Array2DHashSet::hashCode() { + int hash = MurmurHash::initialize(); + for (auto bucket : buckets) { + if (bucket.size() == 0) { + continue; + } + for (auto o : bucket) { + if (o == nullptr) { + break; + } + hash = MurmurHash::update(hash, comparator->hashCode(o)); + } + } + + hash = MurmurHash::finish(hash, size()); + return hash; + } + + template + bool Array2DHashSet::equals(T o) { +#ifdef TODO + barf + // Not sure what to do with this. Error is bad comparison between distinct + // pointer types, do a dynamic cast? + if (o == this) { + return true; + } +#endif + if (!((Array2DHashSet*)(o) != nullptr)) { + return false; + } + Array2DHashSet *other = (Array2DHashSet*)(o); + if (other->size() != size()) { + return false; + } + bool same = this->containsAll(other); + return same; + } + + template + void Array2DHashSet::expand() { + std::vector> old = buckets; + currentPrime += 4; + int newCapacity = (int)buckets.size() * 2; + std::vector> *newTable = createBuckets(newCapacity); + std::vector newBucketLengths; + buckets = *newTable; + threshold = static_cast(newCapacity * LOAD_FACTOR); + // System.out.println("new size="+newCapacity+", thres="+threshold); + // rehash all existing entries + int oldSize = size(); + for (auto bucket : old) { + if (bucket.size() == 0) { + continue; + } + + for (auto o : bucket) { + if (o == nullptr) { + break; + } + + int b = getBucket(o); + int bucketLength = newBucketLengths[b]; + std::vector *newBucket; + if (bucketLength == 0) { + // new bucket + newBucket = createBucket(initialBucketCapacity); + (*newTable)[b] = *newBucket; + } else { + // TODO, dammit is this right? + newBucket = &(*newTable)[b]; + if (bucketLength == newBucket->size()) { + (*newTable)[b] = *newBucket; + } + } + + (*newBucket)[bucketLength] = o; + newBucketLengths[b]++; + } + } + + if (n != oldSize) throw new std::exception(); + } + + template + bool Array2DHashSet::add(T t) { + T existing = getOrAdd(t); + return existing == t; + } + + template + int Array2DHashSet::size() { + return n; + } + + template + bool Array2DHashSet::isEmpty() { + return n == 0; + } + + template + bool Array2DHashSet::contains(void *o) { + return containsFast(asElementType(o)); + } + + template + bool Array2DHashSet::containsFast(T obj) { + if (obj == nullptr) { + return false; + } + + return get(obj) != nullptr; + } + + template + std::iterator *Array2DHashSet::iterator() { +#ifdef TODO + // I'm feeling too weak willed to finish this now, call me a wimp, I dare you + // I think we need to propagate getting rid of the raw array in this class and convert + // to vector + return new SetIterator(this, toArray()); +#else + return nullptr; +#endif + } + + template + std::vector *Array2DHashSet::toArray() { + std::vector *a = createBucket(size()); + int i = 0; + for (auto bucket : buckets) { + if (bucket.size() == 0) { + continue; + } + + for (auto o : bucket) { + if (o == nullptr) { + break; + } + + (*a)[i++] = o; + } + } + + return a; + } + + template + template + U *Array2DHashSet::toArray(U a[]) { + if (sizeof(a) / sizeof(a[0]) < size()) { + a = antlrcpp::Arrays::copyOf(a, size()); + } + + int i = 0; + for (auto bucket : buckets) { + if (bucket == nullptr) { + continue; + } + + for (auto o : bucket) { + if (o == nullptr) { + break; + } + + U targetElement = static_cast(o); // array store will check this + a[i++] = targetElement; + } + } + return a; + } + + template + bool Array2DHashSet::remove(void *o) { + return removeFast(asElementType(o)); + } + + template + bool Array2DHashSet::removeFast(T obj) { + if (obj == nullptr) { + return false; + } + + int b = getBucket(obj); + std::vector bucket = buckets[b]; + if (bucket.size() == 0) { + // no bucket + return false; + } + + for (int i = 0; i < (int)bucket.size(); i++) { + T e = bucket[i]; + if (e == nullptr) { + // empty slot; not there + return false; + } + + if (comparator->equals(e, obj)) { // found it + // shift all elements to the right down one + //System::arraycopy(bucket, i + 1, bucket, i, bucket->length - i - 1); + for (int j = i; j < (int)bucket.size() - i - 1; j++) { + bucket[j] = bucket[j + 1]; + } + bucket[bucket.size() - 1] = nullptr; + n--; + return true; + } + } + + return false; + } + + template + template + bool Array2DHashSet::containsAll(std::set *collection) { + if ((Array2DHashSet*)(collection) != nullptr) { + Array2DHashSet *s = (Array2DHashSet*)(collection); + for (auto bucket : s->buckets) { + if (bucket.size() != 0) { + continue; + } + for (auto o : bucket) { + if (o == nullptr) { + break; + } + if (!this->containsFast(asElementType(o))) { + return false; + } + } + } + } else { +#ifdef TODO + // barf ... this function isn't even being used + for (auto o : collection) { + if (!this->containsFast(asElementType(o))) { + return false; + } + } +#endif + } + return true; + } + + template + template + bool Array2DHashSet::addAll(std::set *c) { + bool changed = false; + for (auto o : c) { + T existing = getOrAdd(o); + if (existing != o) { + changed = true; + } + } + return changed; + } + + template + template + bool Array2DHashSet::retainAll(std::set *c) { + int newsize = 0; + for (auto bucket : buckets) { + if (bucket == nullptr) { + continue; + } + + int i; + int j; + for (i = 0, j = 0; i < sizeof(bucket) / sizeof(bucket[0]); i++) { + if (bucket[i] == nullptr) { + break; + } + + if (!c->contains(bucket[i])) { + // removed + continue; + } + + // keep + if (i != j) { + bucket[j] = bucket[i]; + } + + j++; + newsize++; + } + + newsize += j; + + while (j < i) { + //JAVA TO C++ CONVERTER WARNING: Java to C++ Converter converted the original 'null' assignment to a call to 'delete', but you should review memory allocation of all pointer variables in the converted code: + delete bucket[j]; + j++; + } + } + + bool changed = newsize != n; + n = newsize; + return changed; + } + + template + template + bool Array2DHashSet::removeAll(std::set *c) { + bool changed = false; + for (auto o : c) { + changed |= removeFast(asElementType(o)); + } + + return changed; + } + + template + std::wstring Array2DHashSet::toString() { + if (size() == 0) { + return L"{}"; + } + + antlrcpp::StringBuilder *buf = new antlrcpp::StringBuilder(); + buf->append(L"{"); + bool first = true; + for (auto bucket : buckets) { + if (bucket.size() == 0) { + continue; + } + for (auto o : bucket) { + if (o == nullptr) { + break; + } + if (first) { + first = false; + } else { + buf->append(L", "); + } +#ifdef TODO + buf->append(o->toString()); +#endif + buf->append(L"TODO barf"); + } + } + buf->append(L"}"); + return buf->toString(); + } + + template + std::wstring Array2DHashSet::toTableString() { + antlrcpp::StringBuilder *buf = new antlrcpp::StringBuilder(); + for (auto bucket : buckets) { + if (bucket.size() == 0) { + buf->append(L"null\n"); + continue; + } + buf->append(L"["); + bool first = true; + for (auto o : bucket) { + if (first) { + first = false; + } else { + buf->append(L" "); + } + if (o == nullptr) { + buf->append(L"_"); + } else { +#ifdef TODO + buf->append(o->toString()); +#endif + buf->append(L"TODO barf"); + } + } + buf->append(L"]\n"); + } + return buf->toString(); + } + /// + /// Return an array of {@code T[]} with length {@code capacity}. + /// + /// the length of the array to return + /// the newly constructed array + template + std::vector> *Array2DHashSet::createBuckets(int capacity) { + return new std::vector>(); + } + + /// + /// Return an array of {@code T} with length {@code capacity}. + /// + /// the length of the array to return + /// the newly constructed array + template + std::vector * Array2DHashSet::createBucket(int capacity) { + return new std::vector(); + } + + template + void Array2DHashSet::clear() { + buckets = *createBuckets(INITAL_CAPACITY); + n = 0; + } + + template + void Array2DHashSet::InitializeInstanceFields() { + n = 0; + threshold = static_cast(INITAL_CAPACITY * LOAD_FACTOR); + currentPrime = 1; + initialBucketCapacity = INITAL_BUCKET_CAPACITY; + } + + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/misc/DoubleKeyMap.h b/runtime/Cpp/org/antlr/v4/runtime/misc/DoubleKeyMap.h new file mode 100755 index 000000000..c09c89bdc --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/misc/DoubleKeyMap.h @@ -0,0 +1,133 @@ +#pragma once + +#include +#include + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace misc { + + + /// + /// Sometimes we need to map a key to a value but key is two pieces of data. + /// This nested hash table saves creating a single key each time we access + /// map; avoids mem creation. + /// + template + class DoubleKeyMap { + public: + std::map*> *data; + + Value put(Key1 k1, Key2 k2, Value v) { + auto subdata = data->find(k1); + Value prev = nullptr; + std::map *data2; + if (subdata == data->end()) { + data2 = new std::map(); + data->insert ( std::pair*>(k1, data2) ); + } else { + data2 = subdata->second; + auto elem = data2->find(k2); + if (elem != data2->end()) { + prev = elem->second; + } + } + data2->insert ( std::pair(k2, v) ); + return prev; + } + + Value get(Key1 k1, Key2 k2) { + auto interior = data->find(k1); + if (interior == data->end()) { + return nullptr; + } + std::map *data2 = interior->second; + auto v = data2->find(k2); + if (v == data2->end()) { + return nullptr; + } + return v->second; + } + + std::map *get(Key1 k1) { + auto data2 = data->find(k1); + if (data2 == data->end()) { + return nullptr; + } + return *data2; + } + + /// + /// Get all values associated with primary key + std::set *values(Key1 k1) { + auto data2 = data->find(k1); + if (data2 == data->end()) { + return nullptr; + } + return data2->values(); + } + + /// + /// get all primary keys + std::set *keySet() { + return data->keySet(); + } + + /// + /// get all secondary keys associated with a primary key + std::set *keySet(Key1 k1) { + std::map *data2 = data->get(k1); + if (data2 == nullptr) { + return nullptr; + } + return data2->keySet(); + } + + private: + void InitializeInstanceFields() { + data = new std::map*>(); + } + + public: + DoubleKeyMap() { + InitializeInstanceFields(); + } + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/misc/DoubleKeyMap.h.trvs b/runtime/Cpp/org/antlr/v4/runtime/misc/DoubleKeyMap.h.trvs new file mode 100644 index 000000000..d3a6a498a --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/misc/DoubleKeyMap.h.trvs @@ -0,0 +1,13 @@ + std::map *data2 = data->get(k1); + Value prev = nullptr; + if (data2 == nullptr) { + data2 = new std::map(); + data->put(k1, data2); + } else { + auto elem = data2->find(k2); + if (elem != data2.end()) { + prev = *elem; + } + } + data2->put(k2, v); + return prev; diff --git a/runtime/Cpp/org/antlr/v4/runtime/misc/EqualityComparator.cpp b/runtime/Cpp/org/antlr/v4/runtime/misc/EqualityComparator.cpp new file mode 100755 index 000000000..da6d2defd --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/misc/EqualityComparator.cpp @@ -0,0 +1,43 @@ +#include "EqualityComparator.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace misc { + // Sound of crickets and rustling tumbleweed ... + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/misc/EqualityComparator.h b/runtime/Cpp/org/antlr/v4/runtime/misc/EqualityComparator.h new file mode 100755 index 000000000..c8a38e9a7 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/misc/EqualityComparator.h @@ -0,0 +1,80 @@ +#pragma once + +#include "Exceptions.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace misc { + + /// + /// This interface provides an abstract concept of object equality independent of + /// (object equality) and the {@code ==} operator + /// (reference equality). It can be used to provide algorithm-specific unordered + /// comparisons without requiring changes to the object itself. + /// + /// @author Sam Harwell + /// + template + class EqualityComparator { + + /// + /// This method returns a hash code for the specified object. + /// + /// The object. + /// The hash code for {@code obj}. + public: + virtual int hashCode(T obj) { + throw new TODOException(L"EqualityComparator::hashCode"); + return 0; + } + + /// + /// This method tests if two objects are equal. + /// + /// The first object to compare. + /// The second object to compare. + /// {@code true} if {@code a} equals {@code b}, otherwise {@code false}. + virtual bool equals(T a, T b) { + new TODOException(L"EqualityComparator::equals"); + return false; + } + + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/misc/GraphicsSupport.cpp b/runtime/Cpp/org/antlr/v4/runtime/misc/GraphicsSupport.cpp new file mode 100755 index 000000000..9bc202bd1 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/misc/GraphicsSupport.cpp @@ -0,0 +1,105 @@ +#include "GraphicsSupport.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +// TODO: Come back to this after the base runtime works. +#if 0 + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace misc { + + void GraphicsSupport::saveImage(JComponent *const comp, const std::wstring &fileName) throw(IOException, PrintException) { +//JAVA TO C++ CONVERTER TODO TASK: There is no direct native C++ equivalent to the Java String 'endsWith' method: + if (fileName.endsWith(L".ps") || fileName.endsWith(L".eps")) { + DocFlavor *flavor = DocFlavor::SERVICE_FORMATTED::PRINTABLE; + std::wstring mimeType = L"application/postscript"; +//JAVA TO C++ CONVERTER WARNING: Since the array size is not known in this declaration, Java to C++ Converter has converted this array to a pointer. You will need to call 'delete[]' where appropriate: +//ORIGINAL LINE: javax.print.StreamPrintServiceFactory[] factories = javax.print.StreamPrintServiceFactory.lookupStreamPrintServiceFactories(flavor, mimeType); + StreamPrintServiceFactory *factories = StreamPrintServiceFactory::lookupStreamPrintServiceFactories(flavor, mimeType); +//JAVA TO C++ CONVERTER TODO TASK: There is no native C++ equivalent to 'toString': + std::cout << Arrays->toString(factories) << std::endl; + if (factories->length > 0) { + FileOutputStream *out = new FileOutputStream(fileName); + PrintService *service = factories[0]->getPrintService(out); + SimpleDoc *doc = new SimpleDoc(new PrintableAnonymousInnerClassHelper(comp), flavor, nullptr); + DocPrintJob *job = service->createPrintJob(); + PrintRequestAttributeSet *attributes = new HashPrintRequestAttributeSet(); + job->print(doc, attributes); + out->close(); + } + } else { + // parrt: works with [image/jpeg, image/png, image/x-png, image/vnd.wap.wbmp, image/bmp, image/gif] + Rectangle *rect = comp->getBounds(); + BufferedImage *image = new BufferedImage(rect->width, rect->height, BufferedImage::TYPE_INT_RGB); + Graphics2D *g = static_cast(image->getGraphics()); + g->setColor(Color::WHITE); + g->fill(rect); + // g.setColor(Color.BLACK); + comp->paint(g); + std::wstring extension = fileName.substr(fileName.rfind(L'.') + 1); + bool result = ImageIO::write(image, extension, new File(fileName)); + if (!result) { + System::err::println(std::wstring(L"Now imager for ") + extension); + } + g->dispose(); + } + } + + GraphicsSupport::PrintableAnonymousInnerClassHelper::PrintableAnonymousInnerClassHelper(JComponent *comp) { + this->comp = comp; + } + + int GraphicsSupport::PrintableAnonymousInnerClassHelper::print(Graphics *g, PageFormat *pf, int page) { + if (page >= 1) { + return Printable::NO_SUCH_PAGE; + } else { + Graphics2D *g2 = static_cast(g); + g2->translate((pf->getWidth() - pf->getImageableWidth()) / 2, (pf->getHeight() - pf->getImageableHeight()) / 2); + if (comp->getWidth() > pf->getImageableWidth() || comp->getHeight() > pf->getImageableHeight()) { + double sf1 = pf->getImageableWidth() / (comp->getWidth() + 1); + double sf2 = pf->getImageableHeight() / (comp->getHeight() + 1); + double s = std::min(sf1, sf2); + g2->scale(s, s); + } + + comp->paint(g); + return Printable::PAGE_EXISTS; + } + } + } + } + } + } +} +#endif diff --git a/runtime/Cpp/org/antlr/v4/runtime/misc/GraphicsSupport.h b/runtime/Cpp/org/antlr/v4/runtime/misc/GraphicsSupport.h new file mode 100755 index 000000000..cfd847d50 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/misc/GraphicsSupport.h @@ -0,0 +1,94 @@ +#pragma once + +#include +#include + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +// TODO: Come back to this after the base runtime works. +#if 0 +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace misc { + + + class GraphicsSupport { + /// + /// [The "BSD license"] + /// Copyright (c) 2011 Cay Horstmann + /// All rights reserved. + /// + /// Redistribution and use in source and binary forms, with or without + /// modification, are permitted provided that the following conditions + /// are met: + /// + /// 1. Redistributions of source code must retain the above copyright + /// notice, this list of conditions and the following disclaimer. + /// 2. Redistributions in binary form must reproduce the above copyright + /// notice, this list of conditions and the following disclaimer in the + /// documentation and/or other materials provided with the distribution. + /// 3. The name of the author may not be used to endorse or promote products + /// derived from this software without specific prior written permission. + /// + /// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + /// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + /// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + /// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + /// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + /// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + /// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + /// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + /// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + /// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + /// + public: + static void saveImage(JComponent *const comp, const std::wstring &fileName) throw(IOException, PrintException); + + private: + class PrintableAnonymousInnerClassHelper : public Printable { + private: + JComponent *comp; + + public: + PrintableAnonymousInnerClassHelper(JComponent *comp); + + virtual int print(Graphics *g, PageFormat *pf, int page) override; + }; + }; + + } + } + } + } +} +#endif \ No newline at end of file diff --git a/runtime/Cpp/org/antlr/v4/runtime/misc/IntSet.cpp b/runtime/Cpp/org/antlr/v4/runtime/misc/IntSet.cpp new file mode 100755 index 000000000..fd6a04758 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/misc/IntSet.cpp @@ -0,0 +1,42 @@ +#include "IntSet.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace misc { + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/misc/IntSet.h b/runtime/Cpp/org/antlr/v4/runtime/misc/IntSet.h new file mode 100755 index 000000000..94a678562 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/misc/IntSet.h @@ -0,0 +1,98 @@ +#pragma once +#include +#include +#include "Exceptions.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace misc { + + + + /// + /// A generic set of ints. + /// + /// + class IntSet { + /// + /// Add an element to the set + public: + virtual void add(int el) = 0; + + /// + /// Add all elements from incoming set to this set. Can limit + /// to set of its own type. Return "this" so we can chain calls. + /// + virtual IntSet *addAll(IntSet *set) = 0; + + /// + /// Return the intersection of this set with the argument, creating + /// a new set. + /// + virtual IntSet *And(IntSet *a) = 0; + + virtual IntSet *complement(IntSet *elements) = 0; + + virtual IntSet *Or(IntSet *a) = 0; + + virtual IntSet *subtract(IntSet *a) = 0; + + /// + /// Return the size of this set (not the underlying implementation's + /// allocated memory size, for example). + /// + virtual int size() = 0; + + virtual bool isNil() = 0; + + virtual bool equals(void *obj) = 0; + + virtual int getSingleElement() = 0; + + virtual bool contains(int el) = 0; + + /// + /// remove this element from this set + virtual void remove(int el) = 0; + + virtual std::vector toList() = 0; + + virtual std::wstring toString() = 0; + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/misc/Interval.cpp b/runtime/Cpp/org/antlr/v4/runtime/misc/Interval.cpp new file mode 100755 index 000000000..80a6da26f --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/misc/Interval.cpp @@ -0,0 +1,155 @@ +#include + +#include "Interval.h" + + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace misc { + + Interval *const Interval::INVALID = new Interval(-1,-2); + Interval * Interval::cache[Interval::INTERVAL_POOL_MAX_VALUE+1]; + + int Interval::creates = 0; + int Interval::misses = 0; + int Interval::hits = 0; + int Interval::outOfRange = 0; + + Interval::Interval(int a, int b) { + InitializeInstanceFields(); + this->a = a; + this->b = b; + } + + org::antlr::v4::runtime::misc::Interval *Interval::of(int a, int b) { + // cache just a..a + if (a != b || a < 0 || a>INTERVAL_POOL_MAX_VALUE) { + return new Interval(a,b); + } + if (cache[a] == nullptr) { + cache[a] = new Interval(a,a); + } + return cache[a]; + } + + int Interval::length() { + if (b < a) { + return 0; + } + return b - a + 1; + } + + bool Interval::equals(void *o) { + if (o == nullptr || !( ((Interval*)o) != nullptr)) { + return false; + } + Interval *other = static_cast(o); + return this->a == other->a && this->b == other->b; + } + + int Interval::hashCode() { + int hash = 23; + hash = hash * 31 + a; + hash = hash * 31 + b; + return hash; + } + + bool Interval::startsBeforeDisjoint(Interval *other) { + return this->a < other->a && this->b < other->a; + } + + bool Interval::startsBeforeNonDisjoint(Interval *other) { + return this->a <= other->a && this->b >= other->a; + } + + bool Interval::startsAfter(Interval *other) { + return this->a > other->a; + } + + bool Interval::startsAfterDisjoint(Interval *other) { + return this->a > other->b; + } + + bool Interval::startsAfterNonDisjoint(Interval *other) { + return this->a > other->a && this->a <= other->b; // this.b>=other.b implied + } + + bool Interval::disjoint(Interval *other) { + return startsBeforeDisjoint(other) || startsAfterDisjoint(other); + } + + bool Interval::adjacent(Interval *other) { + return this->a == other->b + 1 || this->b == other->a - 1; + } + + bool Interval::properlyContains(Interval *other) { + return other->a >= this->a && other->b <= this->b; + } + + org::antlr::v4::runtime::misc::Interval *Interval::union_Renamed(Interval *other) { + return Interval::of(std::min(a, other->a), std::max(b, other->b)); + } + + org::antlr::v4::runtime::misc::Interval *Interval::intersection(Interval *other) { + return Interval::of(std::max(a, other->a), std::min(b, other->b)); + } + + org::antlr::v4::runtime::misc::Interval *Interval::differenceNotProperlyContained(Interval *other) { + Interval *diff = nullptr; + // other.a to left of this.a (or same) + if (other->startsBeforeNonDisjoint(this)) { + diff = Interval::of(std::max(this->a, other->b + 1), this->b); + } + + // other.a to right of this.a + else if (other->startsAfterNonDisjoint(this)) { + diff = Interval::of(this->a, other->a - 1); + } + return diff; + } + + std::wstring Interval::toString() { + return std::to_wstring(a) + std::wstring(L"..") + std::to_wstring(b); + } + + void Interval::InitializeInstanceFields() { + a = 0; + b = 0; + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/misc/Interval.h b/runtime/Cpp/org/antlr/v4/runtime/misc/Interval.h new file mode 100755 index 000000000..4b32e22ec --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/misc/Interval.h @@ -0,0 +1,136 @@ +#pragma once + +#include + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace misc { + + /// + /// An immutable inclusive interval a..b + class Interval { + public: + static const int INTERVAL_POOL_MAX_VALUE = 1000; + + static Interval *const INVALID; + + static Interval *cache[INTERVAL_POOL_MAX_VALUE+1]; + + int a; + int b; + + static int creates; + static int misses; + static int hits; + static int outOfRange; + + Interval(int a, int b); + + /// + /// Interval objects are used readonly so share all with the + /// same single value a==b up to some max size. Use an array as a perfect hash. + /// Return shared object for 0..INTERVAL_POOL_MAX_VALUE or a new + /// Interval object with a..a in it. On Java.g4, 218623 IntervalSets + /// have a..a (set with 1 element). + /// + static Interval *of(int a, int b); + + /// + /// return number of elements between a and b inclusively. x..x is length 1. + /// if b < a, then length is 0. 9..10 has length 2. + /// + virtual int length(); + + virtual bool equals(void *o); + + virtual int hashCode(); + + /// + /// Does this start completely before other? Disjoint + virtual bool startsBeforeDisjoint(Interval *other); + + /// + /// Does this start at or before other? Nondisjoint + virtual bool startsBeforeNonDisjoint(Interval *other); + + /// + /// Does this.a start after other.b? May or may not be disjoint + virtual bool startsAfter(Interval *other); + + /// + /// Does this start completely after other? Disjoint + virtual bool startsAfterDisjoint(Interval *other); + + /// + /// Does this start after other? NonDisjoint + virtual bool startsAfterNonDisjoint(Interval *other); + + /// + /// Are both ranges disjoint? I.e., no overlap? + virtual bool disjoint(Interval *other); + + /// + /// Are two intervals adjacent such as 0..41 and 42..42? + virtual bool adjacent(Interval *other); + + virtual bool properlyContains(Interval *other); + + /// + /// Return the interval computed from combining this and other + virtual Interval *union_Renamed(Interval *other); + + /// + /// Return the interval in common between this and o + virtual Interval *intersection(Interval *other); + + /// + /// Return the interval with elements from this not in other; + /// other must not be totally enclosed (properly contained) + /// within this, which would result in two disjoint intervals + /// instead of the single one returned by this method. + /// + virtual Interval *differenceNotProperlyContained(Interval *other); + + virtual std::wstring toString(); + + private: + void InitializeInstanceFields(); + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/misc/IntervalSet.cpp b/runtime/Cpp/org/antlr/v4/runtime/misc/IntervalSet.cpp new file mode 100755 index 000000000..cea07e795 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/misc/IntervalSet.cpp @@ -0,0 +1,632 @@ +#include +#include +#include + +#include "IntervalSet.h" +#include "Token.h" +#include "MurmurHash.h" +#include "Exceptions.h" +#include "Interval.h" +#include "Lexer.h" +#include "StringBuilder.h" + + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace misc { + + IntervalSet *const IntervalSet::COMPLETE_CHAR_SET = IntervalSet::of(0, runtime::Lexer::MAX_CHAR_VALUE); + IntervalSet *const IntervalSet::EMPTY_SET = new IntervalSet(0); + + IntervalSet::IntervalSet(std::vector &intervals) { + InitializeInstanceFields(); + this->intervals = intervals; + } + + IntervalSet::IntervalSet(IntervalSet *set) { + addAll(set); + } + + IntervalSet::IntervalSet(int n, ...) { + InitializeInstanceFields(); + va_list vlist; + va_start(vlist, n); + + for (int i = 0; i < n; i++) { + add(va_arg(vlist, int)); + } + } + + org::antlr::v4::runtime::misc::IntervalSet *IntervalSet::of(int a) { + IntervalSet *s = new IntervalSet(1, a); + return s; + } + + org::antlr::v4::runtime::misc::IntervalSet *IntervalSet::of(int a, int b) { + IntervalSet *s = new IntervalSet(2, a, b); + return s; + } + + void IntervalSet::clear() { + if (readonly) { + throw new IllegalStateException(L"can't alter readonly IntervalSet"); + } + intervals.clear(); + } + + void IntervalSet::add(int el) { + if (readonly) { + throw new IllegalStateException(L"can't alter readonly IntervalSet"); + } + add(1,el); + } + + void IntervalSet::add(int a, int b) { + add(Interval::of(a, b)); + } + + void IntervalSet::add(Interval *addition) { + if (readonly) { + throw new IllegalStateException(L"can't alter readonly IntervalSet"); + } + //System.out.println("add "+addition+" to "+intervals.toString()); + if (addition->b < addition->a) { + return; + } + // find position in list + for (std::vector::iterator iter = intervals.begin(); iter != intervals.end(); ++iter) { + Interval *r = *iter; + if (addition->equals(r)) { + return; + } + if (addition->adjacent(r) || !addition->disjoint(r)) { + // next to each other, make a single larger interval + Interval *bigger = addition->union_Renamed(r); + (*iter) = bigger; + // make sure we didn't just create an interval that + // should be merged with next interval in list + + + while ( iter++ != intervals.end()) { + Interval *next = *iter; + if (!bigger->adjacent(next) && bigger->disjoint(next)) { + break; + } + + // if we bump up against or overlap next, merge + intervals.erase(iter);// remove this one + iter--; // move backwards to what we just set + r = *iter; + + intervals.insert(iter, bigger->union_Renamed(next));//(*iter)->set(bigger->union_Renamed(next)); // set to 3 merged ones + //*iter; // first call to next after previous duplicates the result + } + return; + } + if (addition->startsBeforeDisjoint(r)) { + // insert before r + iter--; //(*iter)->previous(); + intervals.insert(iter, addition);//(*iter)->add(addition); + return; + } + // if disjoint and after r, a future iteration will handle it + } + // ok, must be after last interval (and disjoint from last interval) + // just add it + intervals.push_back(addition); + } + + IntervalSet *IntervalSet::Or(std::vector sets) { + IntervalSet *r = new IntervalSet(0); + for (auto s : sets) { + r->addAll(s); + } + return r; + } + + IntervalSet *IntervalSet::addAll(IntSet *set) { + if (set == nullptr) { + return this; + } + if (!(dynamic_cast(set) != nullptr)) { + throw IllegalArgumentException(std::wstring(L"can't add non IntSet (") + L"IntSet" + std::wstring(L") to IntervalSet")); + } + IntervalSet *other = static_cast(set); + // walk set and add each interval + int n = (int)other->intervals.size(); + for (int i = 0; i < n; i++) { + Interval *I = other->intervals[i]; + this->add(I->a,I->b); + } + return this; + } + + org::antlr::v4::runtime::misc::IntervalSet *IntervalSet::complement(int minElement, int maxElement) { + return this->complement(IntervalSet::of(minElement,maxElement)); + } + + misc::IntervalSet *IntervalSet::complement(IntSet *vocabulary) { + if (vocabulary == nullptr) { + return nullptr; // nothing in common with null set + } + if (!(dynamic_cast(vocabulary) != nullptr)) { + throw new IllegalArgumentException(std::wstring(L"can't complement with non IntervalSet (") + std::wstring(L"IntSet") + std::wstring(L")")); + } + IntervalSet *vocabularyIS = (static_cast(vocabulary)); + int maxElement = vocabularyIS->getMaxElement(); + + IntervalSet *compliment = new IntervalSet(0); + size_t n = intervals.size(); + if (n == 0) { + return compliment; + } + Interval *first = intervals[0]; + // add a range from 0 to first.a constrained to vocab + if (first->a > 0) { + IntervalSet *s = IntervalSet::of(0, first->a - 1); + IntervalSet *a = s->And(vocabularyIS); + compliment->addAll(a); + } + for (size_t i = 1; i < n; i++) { // from 2nd interval .. nth + Interval *previous = intervals[i - 1]; + Interval *current = intervals[i]; + IntervalSet *s = IntervalSet::of(previous->b + 1, current->a - 1); + IntervalSet *a = s->And(vocabularyIS); + compliment->addAll(a); + } + Interval *last = intervals[n - 1]; + // add a range from last.b to maxElement constrained to vocab + if (last->b < maxElement) { + IntervalSet *s = IntervalSet::of(last->b + 1, maxElement); + IntervalSet *a = s->And(vocabularyIS); + compliment->addAll(a); + } + return compliment; + } + + misc::IntervalSet *IntervalSet::subtract(IntSet *other) { + // assume the whole unicode range here for the complement + // because it doesn't matter. Anything beyond the max of this' set + // will be ignored since we are doing this & ~other. The intersection + // will be empty. The only problem would be when this' set max value + // goes beyond MAX_CHAR_VALUE, but hopefully the constant MAX_CHAR_VALUE + // will prevent this. + return this->And((static_cast(other))->complement(COMPLETE_CHAR_SET)); + } + + misc::IntervalSet *IntervalSet::Or(IntSet *a) { + IntervalSet *o = new IntervalSet(0); + o->addAll(this); + o->addAll(a); + return o; + } + + misc::IntervalSet *IntervalSet::And(IntSet *other) { + if (other == nullptr) { //|| !(other instanceof IntervalSet) ) { + return nullptr; // nothing in common with null set + } + + std::vector myIntervals = this->intervals; + std::vector theirIntervals = (static_cast(other))->intervals; + IntervalSet *intersection = nullptr; + size_t mySize = myIntervals.size(); + size_t theirSize = theirIntervals.size(); + size_t i = 0; + size_t j = 0; + // iterate down both interval lists looking for nondisjoint intervals + while (i < mySize && j < theirSize) { + Interval *mine = myIntervals[i]; + Interval *theirs = theirIntervals[j]; + //System.out.println("mine="+mine+" and theirs="+theirs); + if (mine->startsBeforeDisjoint(theirs)) { + // move this iterator looking for interval that might overlap + i++; + } else if (theirs->startsBeforeDisjoint(mine)) { + // move other iterator looking for interval that might overlap + j++; + } else if (mine->properlyContains(theirs)) { + // overlap, add intersection, get next theirs + if (intersection == nullptr) { + intersection = new IntervalSet(0); + } + intersection->add(mine->intersection(theirs)); + j++; + } else if (theirs->properlyContains(mine)) { + // overlap, add intersection, get next mine + if (intersection == nullptr) { + intersection = new IntervalSet(0); + } + intersection->add(mine->intersection(theirs)); + i++; + } else if (!mine->disjoint(theirs)) { + // overlap, add intersection + if (intersection == nullptr) { + intersection = new IntervalSet(0); + } + intersection->add(mine->intersection(theirs)); + // Move the iterator of lower range [a..b], but not + // the upper range as it may contain elements that will collide + // with the next iterator. So, if mine=[0..115] and + // theirs=[115..200], then intersection is 115 and move mine + // but not theirs as theirs may collide with the next range + // in thisIter. + // move both iterators to next ranges + if (mine->startsAfterNonDisjoint(theirs)) { + j++; + } else if (theirs->startsAfterNonDisjoint(mine)) { + i++; + } + } + } + if (intersection == nullptr) { + return new IntervalSet(0); + } + return intersection; + } + + bool IntervalSet::contains(int el) { + size_t n = intervals.size(); + for (size_t i = 0; i < n; i++) { + Interval *I = intervals[i]; + int a = I->a; + int b = I->b; + if (el < a) { + break; // list is sorted and el is before this interval; not here + } + if (el >= a && el <= b) { + return true; // found in this interval + } + } + return false; + /* + for (ListIterator iter = intervals.listIterator(); iter.hasNext();) { + Interval I = (Interval) iter.next(); + if ( el=I.a && el<=I.b ) { + return true; // found in this interval + } + } + return false; + */ + } + + bool IntervalSet::isNil() { + return intervals.empty() || intervals.empty(); + } + + int IntervalSet::getSingleElement() { + if (intervals.size() > 0 && intervals.size() == 1) { + Interval *I = intervals[0]; + if (I->a == I->b) { + return I->a; + } + } + return Token::INVALID_TYPE; + } + + int IntervalSet::getMaxElement() { + if (isNil()) { + return Token::INVALID_TYPE; + } + Interval *last = intervals[intervals.size() - 1]; + return last->b; + } + + int IntervalSet::getMinElement() { + if (isNil()) { + return Token::INVALID_TYPE; + } + size_t n = intervals.size(); + for (size_t i = 0; i < n; i++) { + Interval *I = intervals[i]; + int a = I->a; + int b = I->b; + for (int v = a; v <= b; v++) { + if (v >= 0) { + return v; + } + } + } + return Token::INVALID_TYPE; + } + + std::vector IntervalSet::getIntervals() { + return intervals; + } + + int IntervalSet::hashCode() { + int hash = MurmurHash::initialize(); + for (auto I : intervals) { + hash = MurmurHash::update(hash, I->a); + hash = MurmurHash::update(hash, I->b); + } + + hash = MurmurHash::finish(hash, (int)intervals.size() * 2); + return hash; + } + + bool IntervalSet::equals(void *obj) { + if (obj == nullptr || !(static_cast(obj) != nullptr)) { + return false; + } + IntervalSet *other = static_cast(obj); + return std::equal(this->intervals.begin(), this->intervals.end(), other->intervals.begin()); + } + + std::wstring IntervalSet::toString() { + return toString(false); + } + + std::wstring IntervalSet::toString(bool elemAreChar) { + antlrcpp::StringBuilder *buf = new antlrcpp::StringBuilder(); + if (this->intervals.empty() || this->intervals.empty()) { + return L"{}"; + } + if (this->size() > 1) { + buf->append(L"{"); + } + std::vector::const_iterator iter = this->intervals.begin(); + while (iter != this->intervals.end()) { + Interval *I = *iter; + int a = I->a; + int b = I->b; + if (a == b) { + if (a == -1) { + buf->append(L""); + } else if (elemAreChar) { + buf->append(L"'").append(static_cast(a)).append(L"'"); + } else { + buf->append(a); + } + } else { + if (elemAreChar) { + buf->append(L"'").append(static_cast(a)).append(L"'..'").append(static_cast(b)).append(L"'"); + } else { + buf->append(a).append(L"..").append(b); + } + } + iter++; + if (iter == this->intervals.end()) { + buf->append(L", "); + } + } + if (this->size() > 1) { + buf->append(L"}"); + } + return buf->toString(); + } + + std::wstring IntervalSet::toString(std::wstring tokenNames[]) { + antlrcpp::StringBuilder *buf = new antlrcpp::StringBuilder(); + if (this->intervals.empty() || this->intervals.empty()) { + return L"{}"; + } + if (this->size() > 1) { + buf->append(L"{"); + } + std::vector::const_iterator iter = this->intervals.begin(); + while (iter != this->intervals.end()) { + Interval *I = *iter; + int a = I->a; + int b = I->b; + if (a == b) { + buf->append(elementName(tokenNames, a)); + } else { + for (int i = a; i <= b; i++) { + if (i > a) { + buf->append(L", "); + } + buf->append(elementName(tokenNames, i)); + } + } + iter++; + if (iter == this->intervals.end()) { + buf->append(L", "); + } + } + if (this->size() > 1) { + buf->append(L"}"); + } + return buf->toString(); + } + + std::wstring IntervalSet::toString(std::vector tokenNames) { + antlrcpp::StringBuilder *buf = new antlrcpp::StringBuilder(); + if (this->intervals.empty() || this->intervals.empty()) { + return L"{}"; + } + if (this->size() > 1) { + buf->append(L"{"); + } + std::vector::const_iterator iter = this->intervals.begin(); + while (iter != this->intervals.end()) { + Interval *I = *iter; + int a = I->a; + int b = I->b; + if (a == b) { + buf->append(elementName(tokenNames, a)); + } else { + for (int i = a; i <= b; i++) { + if (i > a) { + buf->append(L", "); + } + buf->append(elementName(tokenNames, i)); + } + } + iter++; + if (iter == this->intervals.end()) { + buf->append(L", "); + } + } + if (this->size() > 1) { + buf->append(L"}"); + } + return buf->toString(); + } + + std::wstring IntervalSet::elementName(std::wstring tokenNames[], int a) { + if (a == Token::_EOF) { + return L""; + } else if (a == Token::EPSILON) { + return L""; + } else { + return tokenNames[a]; + } + + } + + std::wstring IntervalSet::elementName(std::vector tokenNames, int a) { + if (a == Token::_EOF) { + return L""; + } else if (a == Token::EPSILON) { + return L""; + } else { + return tokenNames[a]; + } + + } + + int IntervalSet::size() { + size_t n = 0; + size_t numIntervals = intervals.size(); + if (numIntervals == 1) { + Interval *firstInterval = this->intervals[0]; + return firstInterval->b - firstInterval->a + 1; + } + for (size_t i = 0; i < numIntervals; i++) { + Interval *I = intervals[i]; + n += (I->b - I->a + 1); + } + return (int)n; + } + + std::vector IntervalSet::toList() { + std::vector values = std::vector(); + size_t n = intervals.size(); + for (size_t i = 0; i < n; i++) { + Interval *I = intervals[i]; + size_t a = I->a; + size_t b = I->b; + for (size_t v = a; v <= b; v++) { + values.push_back((int)v); + } + } + return values; + } + + std::set *IntervalSet::toSet() { + std::set *s = new std::set(); + for (auto I : intervals) { + size_t a = I->a; + size_t b = I->b; + for (size_t v = a; v <= b; v++) { + s->insert((int)v); + } + } + return s; + } + + int IntervalSet::get(int i) { + size_t n = intervals.size(); + size_t index = 0; + for (size_t j = 0; j < n; j++) { + Interval *I = intervals[j]; + size_t a = I->a; + size_t b = I->b; + for (size_t v = a; v <= b; v++) { + if (index == (size_t)i) { + return (int)v; + } + index++; + } + } + return -1; + } + + void IntervalSet::remove(int el) { + if (readonly) { + throw IllegalStateException(L"can't alter readonly IntervalSet"); + } + + size_t n = intervals.size(); + for (size_t i = 0; i < n; i++) { + Interval *I = intervals[i]; + size_t a = I->a; + size_t b = I->b; + if ((size_t)el < a) { + break; // list is sorted and el is before this interval; not here + } + // if whole interval x..x, rm + if ((size_t)el == a && (size_t)el == b) { + intervals.erase(intervals.begin() + i); + break; + } + // if on left edge x..b, adjust left + if ((size_t)el == a) { + I->a++; + break; + } + // if on right edge a..x, adjust right + if ((size_t)el == b) { + I->b--; + break; + } + // if in middle a..x..b, split interval + if ((size_t)el > a && (size_t)el < b) { // found in this interval + size_t oldb = I->b; + I->b = el - 1; // [a..x-1] + add(el + 1, (int)oldb); // add [x+1..b] + } + } + } + + bool IntervalSet::isReadonly() { + return readonly; + } + + void IntervalSet::setReadonly(bool readonly) { + this->readonly = readonly; + } + + void IntervalSet::InitializeInstanceFields() { + readonly = false; + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/misc/IntervalSet.h b/runtime/Cpp/org/antlr/v4/runtime/misc/IntervalSet.h new file mode 100755 index 000000000..69510f7e4 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/misc/IntervalSet.h @@ -0,0 +1,226 @@ +#pragma once + +#include +#include +#include + +#include "IntSet.h" +#include "vectorhelper.h" +#include "Declarations.h" + + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace misc { + + + /// + /// A set of integers that relies on ranges being common to do + /// "run-length-encoded" like compression (if you view an IntSet like + /// a BitSet with runs of 0s and 1s). Only ranges are recorded so that + /// a few ints up near value 1000 don't cause massive bitsets, just two + /// integer intervals. + /// + /// element values may be negative. Useful for sets of EPSILON and EOF. + /// + /// 0..9 char range is index pair ['\u0030','\u0039']. + /// Multiple ranges are encoded with multiple index pairs. Isolated + /// elements are encoded with an index pair where both intervals are the same. + /// + /// The ranges are ordered and disjoint so that 2..6 appears before 101..103. + /// + class IntervalSet : public IntSet { + public: + static IntervalSet *const COMPLETE_CHAR_SET; + static IntervalSet *const EMPTY_SET; + + /// + /// The list of sorted, disjoint intervals. + protected: + std::vector intervals; + + bool readonly; + + public: + IntervalSet(std::vector &intervals); + + IntervalSet(IntervalSet *set); //this(); + + IntervalSet(int numArgs, ...); + + virtual ~IntervalSet() {} + + /// + /// Create a set with a single element, el. + static IntervalSet *of(int a); + + /// + /// Create a set with all ints within range [a..b] (inclusive) + static IntervalSet *of(int a, int b); + + virtual void clear(); + + /// + /// Add a single element to the set. An isolated element is stored + /// as a range el..el. + /// + virtual void add(int el) override; + + /// + /// Add interval; i.e., add all integers from a to b to set. + /// If b + virtual void add(int a, int b); + + // copy on write so we can cache a..a intervals and sets of that + protected: + virtual void add(Interval *addition); + + /// + /// combine all sets in the array returned the or'd value + public: + static IntervalSet *Or(std::vector sets); + + virtual IntervalSet *addAll(IntSet *set) override; + + virtual IntervalSet *complement(int minElement, int maxElement); + + /// + /// Given the set of possible values (rather than, say UNICODE or MAXINT), + /// return a new set containing all elements in vocabulary, but not in + /// this. The computation is (vocabulary - this). + /// + /// 'this' is assumed to be either a subset or equal to vocabulary. + /// + virtual IntervalSet *complement(IntSet *vocabulary) override; + + /// + /// Compute this-other via this&~other. + /// Return a new set containing all elements in this but not in other. + /// other is assumed to be a subset of this; + /// anything that is in other but not in this will be ignored. + /// + virtual IntervalSet *subtract(IntSet *other) override; + + virtual IntervalSet *Or(IntSet *a) override; + + /// + /// Return a new set with the intersection of this set with other. Because + /// the intervals are sorted, we can use an iterator for each list and + /// just walk them together. This is roughly O(min(n,m)) for interval + /// list lengths n and m. + /// + virtual IntervalSet *And(IntSet *other) override; + + /// + /// Is el in any range of this set? + virtual bool contains(int el) override; + + /// + /// return true if this set has no members + virtual bool isNil() override; + + /// + /// If this set is a single integer, return it otherwise Token.INVALID_TYPE + virtual int getSingleElement() override; + + virtual int getMaxElement(); + + /// + /// Return minimum element >= 0 + virtual int getMinElement(); + + /// + /// Return a list of Interval objects. + virtual std::vector getIntervals(); + + virtual int hashCode(); + + /// + /// Are two IntervalSets equal? Because all intervals are sorted + /// and disjoint, equals is a simple linear walk over both lists + /// to make sure they are the same. Interval.equals() is used + /// by the List.equals() method to check the ranges. + /// + virtual bool equals(void *obj) override; + + virtual std::wstring toString() override; + + virtual std::wstring toString(bool elemAreChar); + + // TODO(dsisson): See if we can eliminate this version. + virtual std::wstring toString(std::wstring tokenNames[]); + + virtual std::wstring toString(std::vector tokenNames); + + protected: + // TODO(dsisson): See if we can eliminate this version. + virtual std::wstring elementName(std::wstring tokenNames[], int a); + + virtual std::wstring elementName(std::vector tokenNames, int a); + + public: + virtual int size() override; + + virtual std::vector toList() override; + + virtual std::set *toSet(); + + /// + /// Get the ith element of ordered set. Used only by RandomPhrase so + /// don't bother to implement if you're not doing that for a new + /// ANTLR code gen target. + /// + virtual int get(int i); + + virtual void remove(int el) override; + + virtual bool isReadonly(); + + virtual void setReadonly(bool readonly); + + private: + void InitializeInstanceFields(); + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/misc/JFileChooserConfirmOverwrite.cpp b/runtime/Cpp/org/antlr/v4/runtime/misc/JFileChooserConfirmOverwrite.cpp new file mode 100755 index 000000000..839ff949b --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/misc/JFileChooserConfirmOverwrite.cpp @@ -0,0 +1,32 @@ +#include "JFileChooserConfirmOverwrite.h" +#ifdef TODO +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace misc { + + JFileChooserConfirmOverwrite::JFileChooserConfirmOverwrite() { + setMultiSelectionEnabled(false); + } + + void JFileChooserConfirmOverwrite::approveSelection() { + File *selectedFile = getSelectedFile(); + + if (selectedFile->exists()) { + int answer = JOptionPane::showConfirmDialog(this, L"Overwrite existing file?", L"Overwrite?", JOptionPane::YES_NO_OPTION); + if (answer != JOptionPane::YES_OPTION) { + // do not call super.approveSelection + return; + } + } + + JFileChooser::approveSelection(); + } + } + } + } + } +} + +#endif diff --git a/runtime/Cpp/org/antlr/v4/runtime/misc/JFileChooserConfirmOverwrite.h b/runtime/Cpp/org/antlr/v4/runtime/misc/JFileChooserConfirmOverwrite.h new file mode 100755 index 000000000..3d2ef01e4 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/misc/JFileChooserConfirmOverwrite.h @@ -0,0 +1,58 @@ +#pragma once +#ifdef TODO +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Sam Harwell + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace misc { + + + + /// + /// + /// @author Sam Harwell + /// + class JFileChooserConfirmOverwrite : public JFileChooser { + + public: + JFileChooserConfirmOverwrite(); + + virtual void approveSelection() override; + + }; + + } + } + } + } +} +#endif diff --git a/runtime/Cpp/org/antlr/v4/runtime/misc/LogManager.cpp b/runtime/Cpp/org/antlr/v4/runtime/misc/LogManager.cpp new file mode 100755 index 000000000..56effd4e7 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/misc/LogManager.cpp @@ -0,0 +1,136 @@ + +#include "Strings.h" +#include "LogManager.h" +#include "StringBuilder.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace misc { + + LogManager::Record::Record() { + InitializeInstanceFields(); +#ifdef TODO + timestamp = System::currentTimeMillis(); + location = (std::exception())->getStackTrace()[0]; +#endif + } + + std::wstring LogManager::Record::toString() { + antlrcpp::StringBuilder *buf = new antlrcpp::StringBuilder(); +#ifdef TODO + buf->append((new SimpleDateFormat(L"yyyy-MM-dd HH:mm:ss:SSS"))->format(Date(timestamp))); + buf->append(L" "); + buf->append(component); + buf->append(L" "); + buf->append(location->getFileName()); + buf->append(L":"); + buf->append(location->getLineNumber()); + buf->append(L" "); + buf->append(msg); +#endif + return buf->toString(); + } + + void LogManager::Record::InitializeInstanceFields() { + timestamp = 0; + } + + void LogManager::log(const std::wstring &component, const std::wstring &msg) { + Record *r = new Record(); + r->component = component; + r->msg = msg; + if (records.empty()) { + records = std::vector(); + } + records.push_back(r); + } + + void LogManager::log(const std::wstring &msg) { + log(L""); + } + + void LogManager::save(const std::wstring &filename) { +#ifdef TODO + FileWriter *fw = new FileWriter(filename); + BufferedWriter *bw = new BufferedWriter(fw); + try { + bw->write(toString()); + } finally { + bw->close(); + } +#endif + } + + std::wstring LogManager::save() { +#ifdef TODO + //String dir = System.getProperty("java.io.tmpdir"); + std::wstring dir = L"."; + std::wstring defaultFilename = dir + std::wstring(L"/antlr-") + (new SimpleDateFormat(L"yyyy-MM-dd-HH.mm.ss"))->format(Date()) + std::wstring(L".log"); + save(defaultFilename); + return defaultFilename; +#else + return nullptr; +#endif + } + + std::wstring LogManager::toString() { +#ifdef TODO + if (records.empty()) { + return L""; + } + std::wstring nl = System::getProperty(L"line.separator"); + StringBuilder *buf = new StringBuilder(); + for (auto r : records) { + buf->append(r); + buf->append(nl); + } + return buf->toString(); +#else + return nullptr; +#endif + } + + void LogManager::main(std::wstring args[]) { + LogManager *mgr = new LogManager(); + mgr->log(L"atn", L"test msg"); + mgr->log(L"dfa", L"test msg 2"); + std::cout << mgr << std::endl; + mgr->save(); + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/misc/LogManager.h b/runtime/Cpp/org/antlr/v4/runtime/misc/LogManager.h new file mode 100755 index 000000000..b3abb015d --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/misc/LogManager.h @@ -0,0 +1,83 @@ +#pragma once + +#include +#include +#include +#include + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace misc { + + + class LogManager { + protected: + class Record { + public: + long long timestamp; + // This is used nowhere + //StackTraceElement *location; + std::wstring component; + std::wstring msg; + Record(); + + virtual std::wstring toString(); + + private: + void InitializeInstanceFields(); + }; + + protected: + std::vector records; + + public: + virtual void log(const std::wstring &component, const std::wstring &msg); + + virtual void log(const std::wstring &msg); + + virtual void save(const std::wstring &filename); + + virtual std::wstring save(); + + virtual std::wstring toString(); + + static void main(std::wstring args[]); + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/misc/MultiMap.cpp b/runtime/Cpp/org/antlr/v4/runtime/misc/MultiMap.cpp new file mode 100755 index 000000000..3077d95f7 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/misc/MultiMap.cpp @@ -0,0 +1,45 @@ +#include "MultiMap.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace misc { + + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/misc/MultiMap.h b/runtime/Cpp/org/antlr/v4/runtime/misc/MultiMap.h new file mode 100755 index 000000000..02b155ebb --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/misc/MultiMap.h @@ -0,0 +1,120 @@ +#pragma once + +#include +#include +#include +#include +#include + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace misc { +#ifdef TODO + // http://stackoverflow.com/questions/2467000/is-there-a-java-map-keyset-equivalent-for-cs-stdmap + template + class key_iterator : public std::iterator { + public: + + key_iterator() { } + explicit key_iterator(typename C::const_iterator it) : it_(it) { } + + typename const C::key_type& operator*() const { return it_->first; } + typename const C::key_type* operator->() const { return &it_->first; } + + key_iterator& operator++() { ++it_; return *this; } + key_iterator operator++(int) { key_iterator it(*this); ++*this; return it; } + + key_iterator& operator--() { --it_; return *this; } + key_iterator operator--(int) { key_iterator it(*this); --*this; return it; } + + friend bool operator==(const key_iterator& lhs, const key_iterator& rhs) + { + return lhs.it_ == rhs.it_; + } + + friend bool operator!=(const key_iterator& lhs, const key_iterator& rhs) + { + return !(lhs == rhs); + } + + private: + + typename C::const_iterator it_; + }; + + template + key_iterator begin_keys(const C& c) { return key_iterator(c.begin()); } + + template + key_iterator end_keys(const C& c) { return key_iterator(c.end()); } +#endif + +#ifdef TODO + This used to derive from LinkedHashMap + http://stackoverflow.com/questions/2889777/difference-between-hashmap-linkedhashmap-and-sortedmap-in-java +#endif + template + class MultiMap : public std::map> { + public: + + virtual void map(K key, V value) { +// std::vector elementsForKey = get(key); +// if (elementsForKey.empty()) { +// elementsForKey = std::vector(); +// std::map>::put(key, elementsForKey); +// } + } + + virtual std::vector*> getPairs() { +// std::vector*> pairs = std::vector*>(); +// for (K key : keySet()) { +// for (V value : get(key)) { +// pairs.push_back(new std::pair(key, value)); +// } +// } +// return pairs; + return std::vector*>(); + } + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/misc/MurmurHash.cpp b/runtime/Cpp/org/antlr/v4/runtime/misc/MurmurHash.cpp new file mode 100755 index 000000000..dad73c7be --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/misc/MurmurHash.cpp @@ -0,0 +1,84 @@ +#include "MurmurHash.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace misc { + + int MurmurHash::initialize() { + return initialize(DEFAULT_SEED); + } + + int MurmurHash::initialize(int seed) { + return seed; + } + + int MurmurHash::update(int hash, int value) { + const int c1 = 0xCC9E2D51; + const int c2 = 0x1B873593; + const int r1 = 15; + const int r2 = 13; + const int m = 5; + const int n = 0xE6546B64; + + int k = value; + k = k * c1; + k = (k << r1) | (static_cast(static_cast(k) >> (32 - r1))); + k = k * c2; + + hash = hash ^ k; + hash = (hash << r2) | (static_cast(static_cast(hash) >> (32 - r2))); + hash = hash * m + n; + + return hash; + } + + + int MurmurHash::finish(int hash, int numberOfWords) { + hash = hash ^ (numberOfWords * 4); + hash = hash ^ (static_cast(static_cast(hash) >> 16)); + hash = hash * 0x85EBCA6B; + hash = hash ^ (static_cast(static_cast(hash) >> 13)); + hash = hash * 0xC2B2AE35; + hash = hash ^ (static_cast(static_cast(hash) >> 16)); + return hash; + } + + MurmurHash::MurmurHash() { + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/misc/MurmurHash.h b/runtime/Cpp/org/antlr/v4/runtime/misc/MurmurHash.h new file mode 100755 index 000000000..383879fd8 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/misc/MurmurHash.h @@ -0,0 +1,124 @@ +#pragma once + +#include +#include + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace misc { + + /// + /// + /// @author Sam Harwell + /// + class MurmurHash { + + private: + static const int DEFAULT_SEED = 0; + + /// + /// Initialize the hash using the default seed value. + /// + /// the intermediate hash value + public: + static int initialize(); + + /// + /// Initialize the hash using the specified {@code seed}. + /// + /// the seed + /// the intermediate hash value + static int initialize(int seed); + + /// + /// Update the intermediate hash value for the next input {@code value}. + /// + /// the intermediate hash value + /// the value to add to the current hash + /// the updated intermediate hash value + static int update(int hash, int value); + + /// + /// Update the intermediate hash value for the next input {@code value}. + /// + /// the intermediate hash value + /// the value to add to the current hash + /// the updated intermediate hash value + template + static int update(int hash, T *value) { + std::hash hashFunction; + + return update(hash, value != nullptr ? (int)hashFunction(*value) : 0); + } + + /// + /// Apply the final computation steps to the intermediate value {@code hash} + /// to form the final result of the MurmurHash 3 hash function. + /// + /// the intermediate hash value + /// the number of integer values added to the hash + /// the final hash result + static int finish(int hash, int numberOfWords); + + /// + /// Utility function to compute the hash code of an array using the + /// MurmurHash algorithm. + /// + /// @param the array element type + /// the array data + /// the seed for the MurmurHash algorithm + /// the hash code of the data + + template // where T is C array type + static int hashCode(const T*data, std::size_t size, int seed) { + int hash = initialize(seed); + for(int i = 0; i < (int)size; i++) { + hash = update(hash, data[i]); + } + + hash = finish(hash, (int)size); + return hash; + } + + + private: + MurmurHash(); + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/misc/NotNull.cpp b/runtime/Cpp/org/antlr/v4/runtime/misc/NotNull.cpp new file mode 100755 index 000000000..10e27683f --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/misc/NotNull.cpp @@ -0,0 +1,42 @@ +#include "NotNull.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace misc { + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/misc/NotNull.h b/runtime/Cpp/org/antlr/v4/runtime/misc/NotNull.h new file mode 100755 index 000000000..33c8b0cd0 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/misc/NotNull.h @@ -0,0 +1,46 @@ +#pragma once + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace misc { + + class NotNull /*: public System::Attribute*/ { + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/misc/ObjectEqualityComparator.cpp b/runtime/Cpp/org/antlr/v4/runtime/misc/ObjectEqualityComparator.cpp new file mode 100755 index 000000000..7b2b34ad0 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/misc/ObjectEqualityComparator.cpp @@ -0,0 +1,74 @@ + + +#include "ObjectEqualityComparator.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace misc { + + + + ObjectEqualityComparator *const ObjectEqualityComparator::INSTANCE = new ObjectEqualityComparator(); + + int ObjectEqualityComparator::hashCode(void *obj) { + if (obj == nullptr) { + return 0; + } +#ifdef TODO + // This is problematic in C++ + return obj->hashCode(); +#else + throw new TODOException(L"ObjectEqualityComparator::hashCode"); +#endif + } + + bool ObjectEqualityComparator::equals(void *a, void *b) { + if (a == nullptr) { + return b == nullptr; + } + +#ifdef TODO + return a->equals(b); + +#else + throw new TODOException(L"ObjectEqualityComparator::equals"); +#endif + } + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/misc/ObjectEqualityComparator.h b/runtime/Cpp/org/antlr/v4/runtime/misc/ObjectEqualityComparator.h new file mode 100755 index 000000000..d2adbde41 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/misc/ObjectEqualityComparator.h @@ -0,0 +1,76 @@ +#pragma once + +#include "AbstractEqualityComparator.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace misc { + + /// + /// This default implementation of uses object equality + /// for comparisons by calling and . + /// + /// @author Sam Harwell + /// + class ObjectEqualityComparator : public AbstractEqualityComparator { + public: + static ObjectEqualityComparator *const INSTANCE; + + /// + /// {@inheritDoc} + ///

+ /// This implementation returns + /// {@code obj.}. + ///

+ virtual int hashCode(void* obj); + + /// + /// {@inheritDoc} + ///

+ /// This implementation relies on object equality. If both objects are + /// {@code null}, this method returns {@code true}. Otherwise if only + /// {@code a} is {@code null}, this method returns {@code false}. Otherwise, + /// this method returns the result of + /// {@code a.}{@code (b)}. + ///

+ bool equals(void* a, void* b); + + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/misc/OrderedHashSet.cpp b/runtime/Cpp/org/antlr/v4/runtime/misc/OrderedHashSet.cpp new file mode 100755 index 000000000..69e25f72a --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/misc/OrderedHashSet.cpp @@ -0,0 +1,42 @@ +#include "OrderedHashSet.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace misc { + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/misc/OrderedHashSet.h b/runtime/Cpp/org/antlr/v4/runtime/misc/OrderedHashSet.h new file mode 100755 index 000000000..28a450279 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/misc/OrderedHashSet.h @@ -0,0 +1,163 @@ +#pragma once + +#include "Exceptions.h" + +#include +#include + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace misc { + template + class LinkedHashSet { + T remove(T); + bool add(T); + }; + + /// + /// A HashMap that remembers the order that the elements were added. + /// You can alter the ith element with set(i,value) too :) Unique list. + /// I need the replace/set-element-i functionality so I'm subclassing + /// LinkedHashSet. + /// + template + class OrderedHashSet : public LinkedHashSet { + /// + /// Track the elements as they are added to the set + protected: +//JAVA TO C++ CONVERTER NOTE: The variable elements was renamed since C++ does not allow variables with the same name as methods: + std::vector elements_Renamed; + + public: + virtual T get(int i) { + return elements_Renamed[i]; + } + + /// + /// Replace an existing value with a new value; updates the element + /// list and the hash table, but not the key as that has not changed. + /// + virtual T set(int i, T value) { + T oldElement = elements_Renamed[i]; + elements_Renamed[i] = value; // update list + remove(oldElement); // now update the set: remove/add + add(value); + return oldElement; + } + + virtual bool remove(int i) { + T o = elements_Renamed.remove(i); + return remove(o); + } + + /// + /// Add a value to list; keep in hashtable for consistency also; + /// Key is object itself. Good for say asking if a certain string is in + /// a list of strings. + /// + virtual bool add(T value) override { + bool result = add(value); + if (result) { // only track if new element not in set + elements_Renamed.push_back(value); + } + return result; + } + + virtual bool remove(void *o) override { + throw new UnsupportedOperationException(); + } + + virtual void clear() override { + elements_Renamed.clear(); + LinkedHashSet::clear(); + } + + virtual int hashCode() override { + return elements_Renamed.hashCode(); + } + + virtual bool equals(void *o) override { + if (!(dynamic_cast*>(o) != nullptr)) { + return false; + } + + // System.out.print("equals " + this + ", " + o+" = "); + bool same = elements_Renamed.size() > 0 && elements_Renamed.equals((static_cast*>(o))->elements_Renamed); + // System.out.println(same); + return same; + } +#ifdef TODO + virtual std::iterator *iterator() override { + return elements_Renamed.begin(); + } +#endif + /// + /// Return the List holding list of table elements. Note that you are + /// NOT getting a copy so don't write to the list. + /// + virtual std::vector elements() { + return elements_Renamed; + } + + virtual void *clone() override { // safe (result of clone) + OrderedHashSet *dup = static_cast*>(LinkedHashSet::clone()); + dup->elements_Renamed = std::vector(this->elements_Renamed); + return dup; + } + + virtual void *toArray() override { + return elements_Renamed.toArray(); + } + + virtual std::wstring toString() override { + return elements_Renamed.toString(); + } + + private: + void InitializeInstanceFields() { + elements_Renamed = std::vector(); + } + +public: + OrderedHashSet() { + InitializeInstanceFields(); + } + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/misc/ParseCancellationException.cpp b/runtime/Cpp/org/antlr/v4/runtime/misc/ParseCancellationException.cpp new file mode 100755 index 000000000..b9b1459ee --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/misc/ParseCancellationException.cpp @@ -0,0 +1,59 @@ +#include "ParseCancellationException.h" +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace misc { + + ParseCancellationException::ParseCancellationException() { + } + + ParseCancellationException::ParseCancellationException(const std::wstring &message) { + } + + ParseCancellationException::ParseCancellationException(std::exception cause) { +#ifdef TODO + initCause(cause); +#endif + } + + ParseCancellationException::ParseCancellationException(const std::wstring &message, std::exception cause) { +#ifdef TODO + initCause(cause); +#endif + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/misc/ParseCancellationException.h b/runtime/Cpp/org/antlr/v4/runtime/misc/ParseCancellationException.h new file mode 100755 index 000000000..a7a4ced56 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/misc/ParseCancellationException.h @@ -0,0 +1,66 @@ +#pragma once + +#include +#include + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace misc { + /// + /// This exception is thrown to cancel a parsing operation. This exception does + /// not extend , allowing it to bypass the standard + /// error recovery mechanisms. throws this exception in + /// response to a parse error. + /// + /// @author Sam Harwell + /// + class ParseCancellationException : public std::exception { + + public: + ParseCancellationException(); + + ParseCancellationException(const std::wstring &message); + + ParseCancellationException(std::exception cause); + + ParseCancellationException(const std::wstring &message, std::exception cause); + + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/misc/TestRig.cpp b/runtime/Cpp/org/antlr/v4/runtime/misc/TestRig.cpp new file mode 100755 index 000000000..77acb2824 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/misc/TestRig.cpp @@ -0,0 +1,250 @@ +#include "TestRig.h" +#include "CharStream.h" +#include "TokenStream.h" +#include "ANTLRInputStream.h" +#include "CommonTokenStream.h" +#include "DiagnosticErrorListener.h" +#include "PredictionMode.h" +#include "ParserRuleContext.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace misc { +#ifdef TODO + const std::wstring TestRig::LEXER_START_RULE_NAME = L"tokens"; + + TestRig::TestRig(std::wstring args[]) throw(std::exception) : inputFiles(new java::util::ArrayList()) { + InitializeInstanceFields(); + if (sizeof(args) / sizeof(args[0]) < 2) { + System::err::println(std::wstring(L"java org.antlr.v4.runtime.misc.TestRig GrammarName startRuleName\n") + std::wstring(L" [-tokens] [-tree] [-gui] [-ps file.ps] [-encoding encodingname]\n") + std::wstring(L" [-trace] [-diagnostics] [-SLL]\n") + std::wstring(L" [input-filename(s)]")); + System::err::println(L"Use startRuleName='tokens' if GrammarName is a lexer grammar."); + System::err::println(L"Omitting input-filename makes rig read from stdin."); + return; + } + int i = 0; + grammarName = args[i]; + i++; + startRuleName = args[i]; + i++; + while (i < sizeof(args) / sizeof(args[0])) { + std::wstring arg = args[i]; + i++; + if (arg[0] != L'-') { // input file name + inputFiles.push_back(arg); + continue; + } + if (arg == L"-tree") { + printTree = true; + } + if (arg == L"-gui") { + gui = true; + } + if (arg == L"-tokens") { + showTokens = true; + } else if (arg == L"-trace") { + trace = true; + } else if (arg == L"-SLL") { + SLL = true; + } else if (arg == L"-diagnostics") { + diagnostics = true; + } else if (arg == L"-encoding") { + if (i >= sizeof(args) / sizeof(args[0])) { + System::err::println(L"missing encoding on -encoding"); + return; + } + encoding = args[i]; + i++; + } else if (arg == L"-ps") { + if (i >= sizeof(args) / sizeof(args[0])) { + System::err::println(L"missing filename on -ps"); + return; + } + psFile = args[i]; + i++; + } + } + } + + void TestRig::main(std::wstring args[]) throw(std::exception) { + TestRig *testRig = new TestRig(args); + if (sizeof(args) / sizeof(args[0]) >= 2) { + testRig->process(); + } + } + + void TestRig::process() throw(std::exception) { + // System.out.println("exec "+grammarName+"."+startRuleName); + std::wstring lexerName = grammarName + std::wstring(L"Lexer"); + ClassLoader *cl = Thread::currentThread()->getContextClassLoader(); + Class *lexerClass = nullptr; + try { + lexerClass = cl->loadClass(lexerName)->asSubclass(Lexer::typeid); + } catch (java::lang::ClassNotFoundException cnfe) { + // might be pure lexer grammar; no Lexer suffix then + lexerName = grammarName; + try { + lexerClass = cl->loadClass(lexerName)->asSubclass(Lexer::typeid); + } catch (ClassNotFoundException cnfe2) { + System::err::println(std::wstring(L"Can't load ") + lexerName + std::wstring(L" as lexer or parser")); + return; + } + } + +//JAVA TO C++ CONVERTER TODO TASK: Java wildcard generics are not converted to C++: +//ORIGINAL LINE: Constructor lexerCtor = lexerClass.getConstructor(org.antlr.v4.runtime.CharStream.class); + Constructor *lexerCtor = lexerClass->getConstructor(CharStream::typeid); + Lexer *lexer = lexerCtor->newInstance(static_cast(nullptr)); + + Class *parserClass = nullptr; + Parser *parser = nullptr; + if (startRuleName != LEXER_START_RULE_NAME) { + std::wstring parserName = grammarName + std::wstring(L"Parser"); + parserClass = cl->loadClass(parserName)->asSubclass(Parser::typeid); + if (parserClass == nullptr) { + System::err::println(std::wstring(L"Can't load ") + parserName); + } +//JAVA TO C++ CONVERTER TODO TASK: Java wildcard generics are not converted to C++: +//ORIGINAL LINE: Constructor parserCtor = parserClass.getConstructor(org.antlr.v4.runtime.TokenStream.class); + Constructor *parserCtor = parserClass->getConstructor(TokenStream::typeid); + parser = parserCtor->newInstance(static_cast(nullptr)); + } + + if (inputFiles.empty()) { + InputStream *is = System::in; + Reader *r; + if (encoding != L"") { + r = new InputStreamReader(is, encoding); + } else { + r = new InputStreamReader(is); + } + + process(lexer, parserClass, parser, is, r); + return; + } + for (auto inputFile : inputFiles) { + InputStream *is = System::in; + if (inputFile != nullptr) { + is = new FileInputStream(inputFile); + } + Reader *r; + if (encoding != L"") { + r = new InputStreamReader(is, encoding); + } else { + r = new InputStreamReader(is); + } + + if (inputFiles.size() > 1) { + System::err::println(inputFile); + } + process(lexer, parserClass, parser, is, r); + } + } + + void TestRig::process(Lexer *lexer, Class *parserClass, Parser *parser, InputStream *is, Reader *r) throw(IOException, IllegalAccessException, InvocationTargetException, PrintException) { + try { + ANTLRInputStream *input = new ANTLRInputStream(r); + lexer->setInputStream(input); + CommonTokenStream *tokens = new CommonTokenStream(lexer); + + tokens->fill(); + + if (showTokens) { + for (auto tok : tokens->getTokens()) { + std::cout << tok << std::endl; + } + } + + if (startRuleName == LEXER_START_RULE_NAME) { + return; + } + + if (diagnostics) { + parser->addErrorListener(new DiagnosticErrorListener()); + parser->getInterpreter()->setPredictionMode(PredictionMode::LL_EXACT_AMBIG_DETECTION); + } + + if (printTree || gui || psFile != L"") { + parser->setBuildParseTree(true); + } + + if (SLL) { // overrides diagnostics + parser->getInterpreter()->setPredictionMode(PredictionMode::SLL); + } + + parser->setTokenStream(tokens); + parser->setTrace(trace); + + try { + Method *startRule = parserClass->getMethod(startRuleName); + ParserRuleContext *tree = static_cast(startRule->invoke(parser, static_cast(nullptr))); + + if (printTree) { + std::cout << tree->toStringTree(parser) << std::endl; + } + if (gui) { + tree->inspect(parser); + } + if (psFile != L"") { + tree->save(parser, psFile); // Generate postscript + } + } catch (NoSuchMethodException nsme) { + System::err::println(std::wstring(L"No method for rule ") + startRuleName + std::wstring(L" or it has arguments")); + } + } finally { + if (r != nullptr) { + r->close(); + } + if (is != nullptr) { + is->close(); + } + } + } + + void TestRig::InitializeInstanceFields() { + printTree = false; + gui = false; + psFile = L""; + showTokens = false; + trace = false; + diagnostics = false; + encoding = L""; + SLL = false; + } +#endif + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/misc/TestRig.h b/runtime/Cpp/org/antlr/v4/runtime/misc/TestRig.h new file mode 100755 index 000000000..2dad96b3c --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/misc/TestRig.h @@ -0,0 +1,94 @@ +#pragma once + +#include +#include +#include +#include + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace misc { + + + + /// + /// Run a lexer/parser combo, optionally printing tree string or generating + /// postscript file. Optionally taking input file. + /// + /// $ java org.antlr.v4.runtime.misc.TestRig GrammarName startRuleName + /// [-tree] + /// [-tokens] [-gui] [-ps file.ps] + /// [-trace] + /// [-diagnostics] + /// [-SLL] + /// [input-filename(s)] + /// + class TestRig { + public: + static const std::wstring LEXER_START_RULE_NAME; + + protected: + std::wstring grammarName; + std::wstring startRuleName; + const std::vector inputFiles; + bool printTree; + bool gui; + std::wstring psFile; + bool showTokens; + bool trace; + bool diagnostics; + std::wstring encoding; + bool SLL; + + public: + TestRig(std::wstring args[]); + + static void main(std::wstring args[]); + + virtual void process(); + + protected: +#ifdef TODO + virtual void process(Lexer *lexer, Class *parserClass, Parser *parser, InputStream *is, Reader *r) throw(IOException, IllegalAccessException, InvocationTargetException, PrintException); +#endif + private: + void InitializeInstanceFields(); + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/misc/Utils.cpp b/runtime/Cpp/org/antlr/v4/runtime/misc/Utils.cpp new file mode 100755 index 000000000..6aa8c041b --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/misc/Utils.cpp @@ -0,0 +1,168 @@ +#include "Utils.h" +#include "Exceptions.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace misc { + + std::wstring Utils::escapeWhitespace(const std::wstring &s, bool escapeSpaces) { + throw new TODOException(L"Utils::escapeWhitespace"); + } + wchar_t *Utils::toCharArray(const std::vector *data) { +#ifdef TODO + if (data == nullptr) { + return nullptr; + } + wchar_t cdata[data->size()]; + for (int i = 0; i < data->size(); i++) { + cdata[i] = static_cast(data->get(i)); + } + return cdata; +#else + throw new TODOException(L"Utils::toCharArray"); +#endif + } + // TODO: Come back to this after the base runtime works. +#ifdef TODO + template + std::wstring Utils::join(Iterator *iter, const std::wstring &separator) { + StringBuilder *buf = new StringBuilder(); + while (iter->hasNext()) { + buf->append(iter->next()); + if (iter->hasNext()) { + buf->append(separator); + } + iter++; + } + return buf->toString(); + } + + template + std::wstring Utils::join(T array_Renamed[], const std::wstring &separator) { + StringBuilder *builder = new StringBuilder(); + for (int i = 0; i < sizeof(array_Renamed) / sizeof(array_Renamed[0]); i++) { + builder->append(array_Renamed[i]); + if (i < sizeof(array_Renamed) / sizeof(array_Renamed[0]) - 1) { + builder->append(separator); + } + } + + return builder->toString(); + } + + std::wstring Utils::escapeWhitespace(const std::wstring &s, bool escapeSpaces) { + StringBuilder *buf = new StringBuilder(); + for (auto c : s.toCharArray()) { + if (c == L' ' && escapeSpaces) { + buf->append(L'\u00B7'); + } else if (c == L'\t') { + buf->append(L"\\t"); + } else if (c == L'\n') { + buf->append(L"\\n"); + } else if (c == L'\r') { + buf->append(L"\\r"); + } else { + buf->append(c); + } + } + return buf->toString(); + } + + void Utils::writeFile(const std::wstring &fileName, const std::wstring &content) throw(IOException) { + FileWriter *fw = new FileWriter(fileName); + Writer *w = new BufferedWriter(fw); + try { + w->write(content); + } finally { + w->close(); + } + } + + void Utils::waitForClose(Window *const window) throw(InterruptedException) { + const auto lock = new Object(); + + Thread *t = new ThreadAnonymousInnerClassHelper(window, lock); + + t->start(); + + window->addWindowListener(new WindowAdapterAnonymousInnerClassHelper(window, lock)); + + t->join(); + } + + Utils::ThreadAnonymousInnerClassHelper::ThreadAnonymousInnerClassHelper(Window *window, auto lock) { + this->window = window; + this->lock = lock; + } + + void Utils::ThreadAnonymousInnerClassHelper::run() { + synchronized(lock) { + while (window->isVisible()) { + try { + lock->wait(500); + } catch (InterruptedException e) { + } + } + } + } + + Utils::WindowAdapterAnonymousInnerClassHelper::WindowAdapterAnonymousInnerClassHelper(Window *window, auto lock) { + this->window = window; + this->lock = lock; + } + + void Utils::WindowAdapterAnonymousInnerClassHelper::windowClosing(WindowEvent *arg0) { +//JAVA TO C++ CONVERTER TODO TASK: There is no built-in support for multithreading in native C++: + synchronized(lock) { + window->setVisible(false); + lock->notify(); + } + } + + Map *Utils::toMap(std::wstring keys[]) { + Map *m = std::unordered_map(); + for (int i = 0; i < sizeof(keys) / sizeof(keys[0]); i++) { + m->put(keys[i], i); + } + return m; + } + +#endif + } + } + } + } +} + diff --git a/runtime/Cpp/org/antlr/v4/runtime/misc/Utils.h b/runtime/Cpp/org/antlr/v4/runtime/misc/Utils.h new file mode 100755 index 000000000..230cc729c --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/misc/Utils.h @@ -0,0 +1,99 @@ +#pragma once + +#include +#include +#include + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +// TODO: Come back to this after the base runtime works. + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace misc { + + + class Utils { + // Seriously: why isn't this built in to java? ugh! + public: +// template +// static std::wstring join(Iterator *iter, const std::wstring &separator); +// +// template +// static std::wstring join(T array_Renamed[], const std::wstring &separator); + + static std::wstring escapeWhitespace(const std::wstring &s, bool escapeSpaces); + +// static void writeFile(const std::wstring &fileName, const std::wstring &content) throw(IOException); +// +// static void waitForClose(Window *const window) throw(InterruptedException); +// +// private: +// class ThreadAnonymousInnerClassHelper : public Thread { +// private: +// Window *window; +// auto lock; +// +// public: +// ThreadAnonymousInnerClassHelper(Window *window, auto lock); +// +// virtual void run() override; +// }; +// +// private: +// class WindowAdapterAnonymousInnerClassHelper : public WindowAdapter { +// private: +// Window *window; +// auto lock; +// +// public: +// WindowAdapterAnonymousInnerClassHelper(Window *window, auto lock); +// +// virtual void windowClosing(WindowEvent *arg0) override; +// }; +// +// /// +// /// Convert array of strings to string->index map. Useful for +// /// converting rulenames to name->ruleindex map. +// /// +// public: +// static Map *toMap(std::wstring keys[]); + + static wchar_t *toCharArray(const std::vector *data); + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/AbstractParseTreeVisitor.cpp b/runtime/Cpp/org/antlr/v4/runtime/tree/AbstractParseTreeVisitor.cpp new file mode 100755 index 000000000..cb1b8d019 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/AbstractParseTreeVisitor.cpp @@ -0,0 +1,43 @@ +#include "AbstractParseTreeVisitor.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/AbstractParseTreeVisitor.h b/runtime/Cpp/org/antlr/v4/runtime/tree/AbstractParseTreeVisitor.h new file mode 100755 index 000000000..cd683a8cb --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/AbstractParseTreeVisitor.h @@ -0,0 +1,177 @@ +#pragma once + +#include "Declarations.h" +#include "ParseTreeVisitor.h" + +#include "ParseTree.h" +#include "RuleNode.h" +#include "TerminalNode.h" +#include "ErrorNode.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + + template + class AbstractParseTreeVisitor : public ParseTreeVisitor { + /// + /// {@inheritDoc} + ///

+ /// The default implementation calls on the + /// specified tree. + ///

+ public: + virtual T visit(ParseTree *tree) override { + return tree->accept(this); + } + + /// + /// {@inheritDoc} + ///

+ /// The default implementation initializes the aggregate result to + /// . Before visiting each child, it + /// calls ; if the result + /// is {@code false} no more children are visited and the current aggregate + /// result is returned. After visiting a child, the aggregate result is + /// updated by calling with the + /// previous aggregate result and the result of visiting the child. + ///

+ virtual T visitChildren(RuleNode *node) override { + T result = defaultResult(); + int n = node->getChildCount(); + for (int i = 0; i < n; i++) { + if (!shouldVisitNextChild(node, result)) { + break; + } + + ParseTree *c = node->getChild(i); + T childResult = c->accept(this); + result = aggregateResult(result, childResult); + } + + return result; + } + + /// + /// {@inheritDoc} + ///

+ /// The default implementation returns the result of + /// . + ///

+ virtual T visitTerminal(TerminalNode *node) override { + return defaultResult(); + } + + /// + /// {@inheritDoc} + ///

+ /// The default implementation returns the result of + /// . + ///

+ virtual T visitErrorNode(ErrorNode *node) override { + return defaultResult(); + } + + /// + /// Gets the default value returned by visitor methods. This value is + /// returned by the default implementations of + /// , . + /// The default implementation of + /// initializes its aggregate result to this value. + ///

+ /// The base implementation returns {@code null}. + ///

+ /// The default value returned by visitor methods. + protected: + virtual T defaultResult() { + return nullptr; + } + + /// + /// Aggregates the results of visiting multiple children of a node. After + /// either all children are visited or returns + /// {@code false}, the aggregate value is returned as the result of + /// . + ///

+ /// The default implementation returns {@code nextResult}, meaning + /// will return the result of the last child visited + /// (or return the initial value if the node has no children). + ///

+ /// The previous aggregate value. In the default + /// implementation, the aggregate value is initialized to + /// , which is passed as the {@code aggregate} argument + /// to this method after the first child node is visited. + /// The result of the immediately preceeding call to visit + /// a child node. + /// + /// The updated aggregate result. + virtual T aggregateResult(T aggregate, T nextResult) { + return nextResult; + } + + /// + /// This method is called after visiting each child in + /// . This method is first called before the first + /// child is visited; at that point {@code currentResult} will be the initial + /// value (in the default implementation, the initial value is returned by a + /// call to . This method is not called after the last + /// child is visited. + ///

+ /// The default implementation always returns {@code true}, indicating that + /// {@code visitChildren} should only return after all children are visited. + /// One reason to override this method is to provide a "short circuit" + /// evaluation option for situations where the result of visiting a single + /// child has the potential to determine the result of the visit operation as + /// a whole. + ///

+ /// The whose children are currently being + /// visited. + /// The current aggregate result of the children visited + /// to the current point. + /// + /// {@code true} to continue visiting children. Otherwise return + /// {@code false} to stop visiting children and immediately return the + /// current aggregate result from . + virtual bool shouldVisitNextChild(RuleNode *node, T currentResult) { + return true; + } + + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/ErrorNode.cpp b/runtime/Cpp/org/antlr/v4/runtime/tree/ErrorNode.cpp new file mode 100755 index 000000000..285e36745 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/ErrorNode.cpp @@ -0,0 +1,42 @@ +#include "ErrorNode.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/ErrorNode.h b/runtime/Cpp/org/antlr/v4/runtime/tree/ErrorNode.h new file mode 100755 index 000000000..129316392 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/ErrorNode.h @@ -0,0 +1,48 @@ +#pragma once + +#include "TerminalNode.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + + class ErrorNode : public virtual TerminalNode { + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/ErrorNodeImpl.cpp b/runtime/Cpp/org/antlr/v4/runtime/tree/ErrorNodeImpl.cpp new file mode 100755 index 000000000..5baba0165 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/ErrorNodeImpl.cpp @@ -0,0 +1,84 @@ +#include "ErrorNodeImpl.h" +#include "Exceptions.h" + + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + + ErrorNodeImpl::ErrorNodeImpl(Token *token) : TerminalNodeImpl(token) { + } + + // From Terminal Node + Token *ErrorNodeImpl::getSymbol() { + throw new TODOException(L"ErrorNodeImpl::getSymbol"); + }; + + // From Parse Tree + ParseTree *ErrorNodeImpl::getParent(){ + throw new TODOException(L"ErrorNodeImpl::getSymbol"); + }; + ParseTree *ErrorNodeImpl::getChild(int i){ + throw new TODOException(L"ErrorNodeImpl::getChild"); + }; + std::wstring ErrorNodeImpl::getText() { + throw new TODOException(L"ErrorNodeImpl::getText"); + }; + std::wstring ErrorNodeImpl::toStringTree(Parser *parser){ + throw new TODOException(L"ErrorNodeImpl::toStringTree"); + };; + + // From SyntaxTree + misc::Interval *ErrorNodeImpl::getSourceInterval(){ + throw new TODOException(L"ErrorNodeImpl::getSourceInterval"); + };; + + + void *ErrorNodeImpl::getPayload(){ + throw new TODOException(L"ErrorNodeImpl::getPayload"); + }; + + int ErrorNodeImpl::getChildCount(){ + throw new TODOException(L"ErrorNodeImpl::getChildCount"); + };; + + std::wstring ErrorNodeImpl::toStringTree() { + throw new TODOException(L"ErrorNodeImpl::toStringTree"); + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/ErrorNodeImpl.h b/runtime/Cpp/org/antlr/v4/runtime/tree/ErrorNodeImpl.h new file mode 100755 index 000000000..1d06ec93b --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/ErrorNodeImpl.h @@ -0,0 +1,97 @@ +#pragma once + +#include "ErrorNode.h" +#include "TerminalNodeImpl.h" +#include "Declarations.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + + + /// + /// Represents a token that was consumed during resynchronization + /// rather than during a valid match operation. For example, + /// we will create this kind of a node during single token insertion + /// and deletion as well as during "consume until error recovery set" + /// upon no viable alternative exceptions. + /// + class ErrorNodeImpl : public virtual TerminalNodeImpl, public virtual ErrorNode { + public: + ErrorNodeImpl(Token *token); + + template + T accept(ParseTreeVisitor *visitor) { + return visitor->visitErrorNode(this); + } + + // From ErrorNode + + // TerminalNodeImpl->TerminalNode->ParseTree->SyntaxTree->Tree + // ErrorNode->TerminalNode ... + + + // From TerminalnodeImpl + + // From Terminal Node + public: + Token *getSymbol(); + + // From Parse Tree + public: + ParseTree *getParent() override ; + ParseTree *getChild(int i) override; + std::wstring getText(); + std::wstring toStringTree(Parser *parser); + + // From SyntaxTree + public: + misc::Interval *getSourceInterval() override; + + // From Tree + public: +// Tree *getParent() override; + void *getPayload() override; +// Tree *getChild(int i) override; + + int getChildCount() override; + std::wstring toStringTree() override; + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/ParseTree.cpp b/runtime/Cpp/org/antlr/v4/runtime/tree/ParseTree.cpp new file mode 100755 index 000000000..91ad872e7 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/ParseTree.cpp @@ -0,0 +1,42 @@ +#include "ParseTree.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/ParseTree.h b/runtime/Cpp/org/antlr/v4/runtime/tree/ParseTree.h new file mode 100755 index 000000000..eb3002af6 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/ParseTree.h @@ -0,0 +1,83 @@ +#pragma once + +#include "SyntaxTree.h" +#include "ParseTreeVisitor.h" +#include "Declarations.h" + + + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + namespace tree { + + /// + /// An interface to access the tree of objects created + /// during a parse that makes the data structure look like a simple parse tree. + /// This node represents both internal nodes, rule invocations, + /// and leaf nodes, token matches. + ///

+ /// The payload is either a or a object. + ///

+ class ParseTree : public SyntaxTree { + // the following methods narrow the return type; they are not additional methods + public: + virtual ParseTree *getParent() override = 0; + virtual ParseTree *getChild(int i) override = 0; + + /// + /// The needs a double dispatch method. + template + T *accept(ParseTreeVisitor *visitor); + + /// + /// Return the combined text of all leaf nodes. Does not get any + /// off-channel tokens (if any) so won't return whitespace and + /// comments if they are sent to parser on hidden channel. + /// + virtual std::wstring getText() = 0; + + /// + /// Specialize toStringTree so that it can print out more information + /// based upon the parser. + /// + virtual std::wstring toStringTree(Parser *parser) = 0; + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/ParseTreeListener.cpp b/runtime/Cpp/org/antlr/v4/runtime/tree/ParseTreeListener.cpp new file mode 100755 index 000000000..76486518f --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/ParseTreeListener.cpp @@ -0,0 +1,42 @@ +#include "ParseTreeListener.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/ParseTreeListener.h b/runtime/Cpp/org/antlr/v4/runtime/tree/ParseTreeListener.h new file mode 100755 index 000000000..907a14f5a --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/ParseTreeListener.h @@ -0,0 +1,53 @@ +#pragma once + +#include "Declarations.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + namespace tree { + class ParseTreeListener { + public: + virtual void visitTerminal(TerminalNode *node) = 0; + virtual void visitErrorNode(ErrorNode *node) = 0; + virtual void enterEveryRule(ParserRuleContext *ctx) = 0; + virtual void exitEveryRule(ParserRuleContext *ctx) = 0; + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/ParseTreeProperty.cpp b/runtime/Cpp/org/antlr/v4/runtime/tree/ParseTreeProperty.cpp new file mode 100755 index 000000000..b17c4ecf1 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/ParseTreeProperty.cpp @@ -0,0 +1,42 @@ +#include "ParseTreeProperty.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/ParseTreeProperty.h b/runtime/Cpp/org/antlr/v4/runtime/tree/ParseTreeProperty.h new file mode 100755 index 000000000..5938331a5 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/ParseTreeProperty.h @@ -0,0 +1,90 @@ +#pragma once + +#include "ParseTree.h" +#include + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + + + /// + /// Associate a property with a parse tree node. Useful with parse tree listeners + /// that need to associate values with particular tree nodes, kind of like + /// specifying a return value for the listener event method that visited a + /// particular node. Example: + /// + ///
+                    /// ParseTreeProperty<Integer> values = new ParseTreeProperty<Integer>();
+                    /// values.put(tree, 36);
+                    /// int x = values.get(tree);
+                    /// values.removeFrom(tree);
+                    /// 
+ /// + /// You would make one decl (values here) in the listener and use lots of times + /// in your event methods. + ///
+ template + class ParseTreeProperty { + protected: + std::map *annotations; + + public: + virtual V get(ParseTree *node) { + return annotations->get(node); + } + virtual void put(ParseTree *node, V value) { + annotations->put(node, value); + } + virtual V removeFrom(ParseTree *node) { + return annotations->remove(node); + } + + private: + void InitializeInstanceFields() { + annotations = new std::map(); + } + + public: + ParseTreeProperty() { + InitializeInstanceFields(); + } + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/ParseTreeVisitor.cpp b/runtime/Cpp/org/antlr/v4/runtime/tree/ParseTreeVisitor.cpp new file mode 100755 index 000000000..2b74c984b --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/ParseTreeVisitor.cpp @@ -0,0 +1,42 @@ +#include "ParseTreeVisitor.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/ParseTreeVisitor.h b/runtime/Cpp/org/antlr/v4/runtime/tree/ParseTreeVisitor.h new file mode 100755 index 000000000..4fed750c6 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/ParseTreeVisitor.h @@ -0,0 +1,86 @@ +#pragma once + +#include "Declarations.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + /// + /// This interface defines the basic notion of a parse tree visitor. Generated + /// visitors implement this interface and the {@code XVisitor} interface for + /// grammar {@code X}. + /// + /// @param The return type of the visit operation. Use for + /// operations with no return type. + template + class ParseTreeVisitor { + + /// + /// Visit a parse tree, and return a user-defined result of the operation. + /// + /// The to visit. + /// The result of visiting the parse tree. + public: + virtual T *visit(ParseTree *tree) = 0; + + /// + /// Visit the children of a node, and return a user-defined result of the + /// operation. + /// + /// The whose children should be visited. + /// The result of visiting the children of the node. + virtual T *visitChildren(RuleNode *node) = 0; + + /// + /// Visit a terminal node, and return a user-defined result of the operation. + /// + /// The to visit. + /// The result of visiting the node. + virtual T *visitTerminal(TerminalNode *node) = 0; + + /// + /// Visit an error node, and return a user-defined result of the operation. + /// + /// The to visit. + /// The result of visiting the node. + virtual T *visitErrorNode(ErrorNode *node) = 0; + + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/ParseTreeWalker.cpp b/runtime/Cpp/org/antlr/v4/runtime/tree/ParseTreeWalker.cpp new file mode 100755 index 000000000..efdf46aa1 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/ParseTreeWalker.cpp @@ -0,0 +1,77 @@ +#include "ParseTreeWalker.h" +#include "TerminalNode.h" +#include "ErrorNode.h" +#include "ParserRuleContext.h" + +/* +* [The "BSD license"] +* Copyright (c) 2013 Terence Parr +* Copyright (c) 2013 Dan McLaughlin +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* 1. Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* 2. Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* 3. The name of the author may not be used to endorse or promote products +* derived from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + + ParseTreeWalker *const ParseTreeWalker::DEFAULT = new ParseTreeWalker(); + + void ParseTreeWalker::walk(ParseTreeListener *listener, ParseTree *t) { + if (dynamic_cast(t) != nullptr) { + listener->visitErrorNode(dynamic_cast(t)); + return; + } else if (dynamic_cast(t) != nullptr) { + listener->visitTerminal(static_cast(t)); + return; + } + RuleNode *r = static_cast(t); + enterRule(listener, r); + int n = r->getChildCount(); + for (int i = 0; i < n; i++) { + walk(listener, r->getChild(i)); + } + exitRule(listener, r); + } + + void ParseTreeWalker::enterRule(ParseTreeListener *listener, RuleNode *r) { + ParserRuleContext *ctx = dynamic_cast(r->getRuleContext()); + listener->enterEveryRule(ctx); + ctx->enterRule(listener); + } + + void ParseTreeWalker::exitRule(ParseTreeListener *listener, RuleNode *r) { + ParserRuleContext *ctx = dynamic_cast(r->getRuleContext()); + ctx->exitRule(listener); + listener->exitEveryRule(ctx); + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/ParseTreeWalker.h b/runtime/Cpp/org/antlr/v4/runtime/tree/ParseTreeWalker.h new file mode 100755 index 000000000..34295eca2 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/ParseTreeWalker.h @@ -0,0 +1,65 @@ +#pragma once + +#include "ParseTree.h" +#include "ParseTreeListener.h" +#include "RuleNode.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + + class ParseTreeWalker { + public: + static ParseTreeWalker *const DEFAULT; + + virtual void walk(ParseTreeListener *listener, ParseTree *t); + + /// + /// The discovery of a rule node, involves sending two events: the generic + /// and a + /// -specific event. First we trigger the generic and then + /// the rule specific. We to them in reverse order upon finishing the node. + /// + protected: + virtual void enterRule(ParseTreeListener *listener, RuleNode *r); + + virtual void exitRule(ParseTreeListener *listener, RuleNode *r); + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/RuleNode.cpp b/runtime/Cpp/org/antlr/v4/runtime/tree/RuleNode.cpp new file mode 100755 index 000000000..dd480868f --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/RuleNode.cpp @@ -0,0 +1,43 @@ +#include "RuleNode.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/RuleNode.h b/runtime/Cpp/org/antlr/v4/runtime/tree/RuleNode.h new file mode 100755 index 000000000..d0abcc332 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/RuleNode.h @@ -0,0 +1,52 @@ +#pragma once + +#include "ParseTree.h" +#include "Declarations.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + namespace tree { + + class RuleNode : public ParseTree { + public: + virtual runtime::RuleContext *getRuleContext() = 0; + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/SyntaxTree.cpp b/runtime/Cpp/org/antlr/v4/runtime/tree/SyntaxTree.cpp new file mode 100755 index 000000000..5d1f0df0c --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/SyntaxTree.cpp @@ -0,0 +1,43 @@ +#include "SyntaxTree.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/SyntaxTree.h b/runtime/Cpp/org/antlr/v4/runtime/tree/SyntaxTree.h new file mode 100755 index 000000000..e50c16dfc --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/SyntaxTree.h @@ -0,0 +1,67 @@ +#pragma once + +#include "Tree.h" +#include "Declarations.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace misc { + class Interval; + } + + namespace tree { + /// + /// A tree that knows about an interval in a token stream + /// is some kind of syntax tree. Subinterfaces distinguish + /// between parse trees and other kinds of syntax trees we might want to create. + /// + class SyntaxTree : public Tree { + /// + /// Return an indicating the index in the + /// of the first and last token associated with this + /// subtree. If this node is a leaf, then the interval represents a single + /// token. + ///

+ /// If source interval is unknown, this returns . + ///

+ public: + virtual misc::Interval *getSourceInterval() = 0; + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/TerminalNode.cpp b/runtime/Cpp/org/antlr/v4/runtime/tree/TerminalNode.cpp new file mode 100755 index 000000000..44554ab1c --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/TerminalNode.cpp @@ -0,0 +1,43 @@ +#include "TerminalNode.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/TerminalNode.h b/runtime/Cpp/org/antlr/v4/runtime/tree/TerminalNode.h new file mode 100755 index 000000000..2cf0551de --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/TerminalNode.h @@ -0,0 +1,54 @@ +#pragma once + +#include "ParseTree.h" +#include "Token.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + class Token; + + namespace tree { + + + class TerminalNode : public ParseTree { + public: + virtual Token *getSymbol() = 0; + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/TerminalNodeImpl.cpp b/runtime/Cpp/org/antlr/v4/runtime/tree/TerminalNodeImpl.cpp new file mode 100755 index 000000000..59406de19 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/TerminalNodeImpl.cpp @@ -0,0 +1,96 @@ +#include "TerminalNodeImpl.h" +#include "Interval.h" +#include "Token.h" +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + + TerminalNodeImpl::TerminalNodeImpl(Token *symbol) { + this->symbol = symbol; + } + + tree::ParseTree *TerminalNodeImpl::getChild(int i) { + return nullptr; + } + + Token *TerminalNodeImpl::getSymbol() { + return symbol; + } + + tree::ParseTree *TerminalNodeImpl::getParent() { + return parent; + } + + void *TerminalNodeImpl::getPayload() { + return symbol; + } + + misc::Interval *TerminalNodeImpl::getSourceInterval() { + if (symbol == nullptr) { + return misc::Interval::INVALID; + } + + int tokenIndex = symbol->getTokenIndex(); + return new misc::Interval(tokenIndex, tokenIndex); + } + + int TerminalNodeImpl::getChildCount() { + return 0; + } + + std::wstring TerminalNodeImpl::getText() { + return symbol->getText(); + } + + std::wstring TerminalNodeImpl::toStringTree(Parser *parser) { + return toString(); + } + + std::wstring TerminalNodeImpl::toString() { + if (symbol->getType() == Token::_EOF) { + return L""; + } + return symbol->getText(); + } + + std::wstring TerminalNodeImpl::toStringTree() { + return toString(); + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/TerminalNodeImpl.h b/runtime/Cpp/org/antlr/v4/runtime/tree/TerminalNodeImpl.h new file mode 100755 index 000000000..26db30ef5 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/TerminalNodeImpl.h @@ -0,0 +1,77 @@ +#pragma once + +#include "TerminalNode.h" +#include "Declarations.h" + +#include + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + namespace tree { + + class TerminalNodeImpl : public virtual TerminalNode { + public: + Token *symbol; + ParseTree *parent; + + TerminalNodeImpl(Token *symbol); + + virtual ParseTree *getChild(int i) override; + virtual Token *getSymbol() override; + virtual ParseTree *getParent() override; + virtual void *getPayload() override; // returns "Token *" + virtual misc::Interval *getSourceInterval() override; + + virtual int getChildCount() override; + + template + T accept(ParseTreeVisitor *visitor) { + return visitor->visitTerminal(this); + } + + virtual std::wstring getText() override; + virtual std::wstring toStringTree(Parser *parser) override; + + virtual std::wstring toString(); + + virtual std::wstring toStringTree() override; + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/Tree.cpp b/runtime/Cpp/org/antlr/v4/runtime/tree/Tree.cpp new file mode 100755 index 000000000..e60744eb1 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/Tree.cpp @@ -0,0 +1,43 @@ +#include "Tree.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/Tree.h b/runtime/Cpp/org/antlr/v4/runtime/tree/Tree.h new file mode 100755 index 000000000..9bdf6c456 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/Tree.h @@ -0,0 +1,83 @@ +#pragma once +#include + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + /// + /// The basic notion of a tree has a parent, a payload, and a list of children. + /// It is the most abstract interface for all the trees used by ANTLR. + /// + class Tree { + /// + /// The parent of this node. If the return value is null, then this + /// node is the root of the tree. + /// + public: + virtual Tree *getParent() = 0; + + /// + /// This method returns whatever object represents the data at this note. For + /// example, for parse trees, the payload can be a representing + /// a leaf node or a object representing a rule + /// invocation. For abstract syntax trees (ASTs), this is a + /// object. + /// + virtual void *getPayload() = 0; + + /// + /// If there are children, get the {@code i}th value indexed from 0. + virtual Tree *getChild(int i) = 0; + + /// + /// How many children are there? If there is none, then this + /// node represents a leaf node. + /// + virtual int getChildCount() = 0; + + /// + /// Print out a whole tree, not just a node, in LISP format + /// {@code (root child1 .. childN)}. Print just a node if this is a leaf. + /// + virtual std::wstring toStringTree() = 0; + + virtual std::wstring toString() = 0; + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/Trees.cpp b/runtime/Cpp/org/antlr/v4/runtime/tree/Trees.cpp new file mode 100755 index 000000000..a05e2a146 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/Trees.cpp @@ -0,0 +1,195 @@ +#include "Trees.h" + +#include "Utils.h" +#include "RuleNode.h" +#include "ErrorNode.h" +#include "TerminalNode.h" +#include "Token.h" +#include "ParserRuleContext.h" +#include "Utils.h" +#include "Tree.h" +#include "Parser.h" + +/* +* [The "BSD license"] +* Copyright (c) 2013 Terence Parr +* Copyright (c) 2013 Dan McLaughlin +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* 1. Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* 2. Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* 3. The name of the author may not be used to endorse or promote products +* derived from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { +#ifdef TODO + // Postscript output to be supported (maybe) + std::wstring Trees::getPS(Tree *t, std::vector &ruleNames, const std::wstring &fontName, int fontSize) { + TreePostScriptGenerator *psgen = new TreePostScriptGenerator(ruleNames, t, fontName, fontSize); + return psgen->getPS(); + } + + std::wstring Trees::getPS(Tree *t, std::vector &ruleNames) { + return getPS(t, ruleNames, L"Helvetica", 11); + } + + void Trees::writePS(Tree *t, std::vector &ruleNames, const std::wstring &fileName, const std::wstring &fontName, int fontSize) throw(IOException) { + std::wstring ps = getPS(t, ruleNames, fontName, fontSize); + FileWriter *f = new FileWriter(fileName); + BufferedWriter *bw = new BufferedWriter(f); + try { + bw->write(ps); + } finally { + bw->close(); + } + } + void Trees::writePS(Tree *t, std::vector &ruleNames, const std::wstring &fileName) { + writePS(t, ruleNames, fileName, L"Helvetica", 11); + } +#endif + + std::wstring Trees::toStringTree(Tree *t) { + return toStringTree(t, nullptr);//static_cast>(nullptr)); + } + + std::wstring Trees::toStringTree(Tree *t, Parser *recog) { + return toStringTree(t, recog->getRuleNames()); + } + + std::wstring Trees::toStringTree(Tree *t, const std::vector &ruleNames) { + std::wstring tmp = Trees::getNodeText(t, ruleNames); + std::wstring s = antlrcpp::escapeWhitespace(tmp, false); + if (t->getChildCount() == 0) { + return s; + } + antlrcpp::StringBuilder *buf = new antlrcpp::StringBuilder(); + buf->append(L"("); + s = antlrcpp::escapeWhitespace(getNodeText(t, ruleNames), false); + buf->append(s); + buf->append(L' '); + for (int i = 0; i < t->getChildCount(); i++) { + if (i > 0) { + buf->append(L' '); + } + buf->append(toStringTree(t->getChild(i), ruleNames)); + } + buf->append(L")"); + return buf->toString(); + } + + std::wstring Trees::getNodeText(Tree *t, Parser *recog) { + return getNodeText(t, recog->getRuleNames()); + } + + std::wstring Trees::getNodeText(Tree *t, const std::vector &ruleNames) { + if (ruleNames.size() > 0) { + if (dynamic_cast(t) != nullptr) { + int ruleIndex = (static_cast(t))->getRuleContext()->getRuleIndex(); + std::wstring ruleName = ruleNames[ruleIndex]; + return ruleName; + } else if (dynamic_cast(t) != nullptr) { + return t->toString(); + } else if (dynamic_cast(t) != nullptr) { + Token *symbol = (static_cast(t))->getSymbol(); + if (symbol != nullptr) { + std::wstring s = symbol->getText(); + return s; + } + } + } + // no recog for rule names + auto payload = t->getPayload(); + if ((Token*)(payload) != nullptr) { + return (static_cast(payload))->getText(); + } +#ifdef TODO + // The Java boys depended on the universal toString function + // to work for essentially void (Object) pointers. We don't + // have that luxury + return t->getPayload()->toString(); +#else + return L"TODO"; +#endif + } + + std::vector Trees::getChildren(Tree *t) { + std::vector kids = std::vector(); + for (int i = 0; i < t->getChildCount(); i++) { + kids.push_back(t->getChild(i)); + } + return kids; + } + + std::vector Trees::getAncestors(Tree *t) { + if (t->getParent() == nullptr) { + return std::vector();// ::emptyList(); + } + std::vector ancestors = std::vector(); + t = t->getParent(); + while (t != nullptr) { + ancestors.insert(ancestors.begin(), t); // insert at start + t = t->getParent(); + } + return ancestors; + } + + std::vector *Trees::findAllTokenNodes(ParseTree *t, int ttype) { + return findAllNodes(t, ttype, true); + } + + std::vector *Trees::findAllRuleNodes(ParseTree *t, int ruleIndex) { + return findAllNodes(t, ruleIndex, false); + } + + std::vector *Trees::findAllNodes(ParseTree *t, int index, bool findTokens) { + std::vector *nodes = new std::vector(); + _findAllNodes(t, index, findTokens, *nodes); + return nodes; + } + + std::vector* Trees::descendants(ParseTree *t) { + std::vector *nodes = new std::vector(); + nodes->insert(nodes->end(), t); + int n = t->getChildCount(); + for (int i = 0 ; i < n ; i++) { + std::vector* tmp = descendants(t->getChild(i)); + for (auto foo:*tmp) { + nodes->insert(nodes->end(), foo); + } + } + return nodes; + } + + Trees::Trees() { + } + } + + } + } + } + +} + diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/Trees.h b/runtime/Cpp/org/antlr/v4/runtime/tree/Trees.h new file mode 100755 index 000000000..d8ec43bc3 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/Trees.h @@ -0,0 +1,132 @@ +#pragma once + +#include +#include +#include + +#include "Declarations.h" +#include "TerminalNode.h" +#include "ParserRuleContext.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + /// + /// A set of utility routines useful for all kinds of ANTLR trees. + class Trees { + + public: +#ifdef TODO + static std::wstring getPS(Tree *t, std::vector &ruleNames, const std::wstring &fontName, int fontSize); + + static std::wstring getPS(Tree *t, std::vector &ruleNames); + + static void writePS(Tree *t, std::vector &ruleNames, const std::wstring &fileName, const std::wstring &fontName, int fontSize); //throw(IOException); + + static void writePS(Tree *t, std::vector &ruleNames, const std::wstring &fileName);// throw(IOException); +#endif + /// + /// Print out a whole tree in LISP form. is used on the + /// node payloads to get the text for the nodes. Detect + /// parse trees and extract data appropriately. + /// + static std::wstring toStringTree(Tree *t); + + /// + /// Print out a whole tree in LISP form. is used on the + /// node payloads to get the text for the nodes. Detect + /// parse trees and extract data appropriately. + /// + static std::wstring toStringTree(Tree *t, Parser *recog); + + /// + /// Print out a whole tree in LISP form. is used on the + /// node payloads to get the text for the nodes. Detect + /// parse trees and extract data appropriately. + /// + static std::wstring toStringTree(Tree *t, const std::vector &ruleNames); + + static std::wstring getNodeText(Tree *t, Parser *recog); + + static std::wstring getNodeText(Tree *t, const std::vector &ruleNames); + + + /// + /// Return ordered list of all children of this node + static std::vector getChildren(Tree *t); + + /// + /// Return a list of all ancestors of this node. The first node of + /// list is the root and the last is the parent of this node. + /// + static std::vector getAncestors(Tree *t); + + static std::vector *findAllTokenNodes(ParseTree *t, int ttype); + + static std::vector *findAllRuleNodes(ParseTree *t, int ruleIndex); + + static std::vector *findAllNodes(ParseTree *t, int index, bool findTokens); + + template + static void _findAllNodes(ParseTree *t, int index, bool findTokens, std::vector nodes) { + // check this node (the root) first + if (findTokens && (TerminalNode*)(t) != nullptr) { + TerminalNode *tnode = (TerminalNode*)(t); + if (tnode->getSymbol()->getType() == index) { + nodes.push_back(t); + } + } else if (!findTokens && (ParserRuleContext*)(t) != nullptr) { + ParserRuleContext *ctx = (ParserRuleContext*)(t); + if (ctx->getRuleIndex() == index) { + nodes.push_back(t); + } + } + // check children + for (int i = 0; i < t->getChildCount(); i++) { + _findAllNodes(t->getChild(i), index, findTokens, nodes); + } + } + + static std::vector* descendants(ParseTree *t); + + private: + Trees(); + }; + + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/gui/BasicFontMetrics.cpp b/runtime/Cpp/org/antlr/v4/runtime/tree/gui/BasicFontMetrics.cpp new file mode 100755 index 000000000..4d395e8fb --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/gui/BasicFontMetrics.cpp @@ -0,0 +1,37 @@ +#include "BasicFontMetrics.h" + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + namespace gui { + + double BasicFontMetrics::getWidth(const std::wstring &s, int fontSize) { + double w = 0; + for (auto c : s.toCharArray()) { + w += getWidth(c, fontSize); + } + return w; + } + + double BasicFontMetrics::getWidth(wchar_t c, int fontSize) { + if (c > MAX_CHAR || widths[c] == 0) { // return width('m') + return widths[L'm'] / 1000.0; + } + return widths[c] / 1000.0 * fontSize; + } + + double BasicFontMetrics::getLineHeight(int fontSize) { + return maxCharHeight / 1000.0 * fontSize; + } + + void BasicFontMetrics::InitializeInstanceFields() { + maxCharHeight = 0; + } + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/gui/BasicFontMetrics.h b/runtime/Cpp/org/antlr/v4/runtime/tree/gui/BasicFontMetrics.h new file mode 100755 index 000000000..e0e9945f2 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/gui/BasicFontMetrics.h @@ -0,0 +1,112 @@ +#pragma once + +#include + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + namespace gui { + + /// + /// Font metrics. The only way to generate accurate images + /// in any format that contain text is to know the font metrics. + /// Specifically, we need to know the width of every character and the + /// maximum height (since we want all characters to fit within same line height). + /// I used ttf2tfm to dump the font metrics from Mac TrueType fonts and + /// then converted that to a Java class for use in a PostScript generator + /// for trees. Commands: + /// + ///
+                        ///	 $ ttf2tfm /Library/Fonts/Arial\ Black.ttf > metrics
+                        /// 
+ /// + /// Then run metrics into python code after stripping header/footer: + /// + ///
+                        ///	 #
+                        ///	 # Process lines from ttf2tfm that look like this:
+                        ///	 # Glyph  Code   Glyph Name                Width  llx    lly      urx    ury
+                        ///	 # ------------------------------------------------------------------------
+                        ///	 #     3  00020  space                       333  0,     0 --     0,     0
+                        ///	 #
+                        ///	 lines = open("metrics").read().split('\n')
+                        ///	 print "public class FontName {"
+                        ///	 print "    {"
+                        ///	 maxh = 0;
+                        ///	 for line in lines[4:]: # skip header 0..3
+                        ///			 all = line.split(' ')
+                        ///			 words = [x for x in all if len(x)>0]
+                        ///			 ascii = int(words[1], 16)
+                        ///			 height = int(words[8])
+                        ///			 if height>maxh: maxh = height
+                        ///			 if ascii>=128: break
+                        ///			 print "        widths[%d] = %s; // %s" % (ascii, words[3], words[2])
+                        /// 
+                        ///	 print "        maxCharHeight = "+str(maxh)+";"
+                        ///	 print "    }"
+                        ///	 print "}"
+                        /// 
+ /// + /// Units are 1000th of an 'em'. + ///
+ class BasicFontMetrics { + public: + static const int MAX_CHAR = L'\u00FF'; + protected: + int maxCharHeight; + int widths[MAX_CHAR + 1]; + + public: + virtual double getWidth(const std::wstring &s, int fontSize); + + virtual double getWidth(wchar_t c, int fontSize); + + virtual double getLineHeight(int fontSize); + + private: + void InitializeInstanceFields(); + +public: + BasicFontMetrics() { + InitializeInstanceFields(); + } + }; + + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/gui/PostScriptDocument.cpp b/runtime/Cpp/org/antlr/v4/runtime/tree/gui/PostScriptDocument.cpp new file mode 100755 index 000000000..e3730aed3 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/gui/PostScriptDocument.cpp @@ -0,0 +1,168 @@ +#include "PostScriptDocument.h" + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + namespace gui { + +const std::wstring PostScriptDocument::DEFAULT_FONT = L"Courier New"; + +//JAVA TO C++ CONVERTER TODO TASK: Static constructors are not allowed in native C++: + PostScriptDocument::PostScriptDocument() { + POSTSCRIPT_FONT_NAMES = std::unordered_map(); + POSTSCRIPT_FONT_NAMES->put(Font::SANS_SERIF + std::wstring(L".plain"), L"ArialMT"); + POSTSCRIPT_FONT_NAMES->put(Font::SANS_SERIF + std::wstring(L".bold"), L"Arial-BoldMT"); + POSTSCRIPT_FONT_NAMES->put(Font::SANS_SERIF + std::wstring(L".italic"), L"Arial-ItalicMT"); + POSTSCRIPT_FONT_NAMES->put(Font::SANS_SERIF + std::wstring(L".bolditalic"), L"Arial-BoldItalicMT"); + POSTSCRIPT_FONT_NAMES->put(Font::SERIF + std::wstring(L".plain"), L"TimesNewRomanPSMT"); + POSTSCRIPT_FONT_NAMES->put(Font::SERIF + std::wstring(L".bold"), L"TimesNewRomanPS-BoldMT"); + POSTSCRIPT_FONT_NAMES->put(Font::SERIF + std::wstring(L".italic"), L"TimesNewRomanPS-ItalicMT"); + POSTSCRIPT_FONT_NAMES->put(Font::SERIF + std::wstring(L".bolditalic"), L"TimesNewRomanPS-BoldItalicMT"); + POSTSCRIPT_FONT_NAMES->put(Font::MONOSPACED + std::wstring(L".plain"), L"CourierNewPSMT"); + POSTSCRIPT_FONT_NAMES->put(Font::MONOSPACED + std::wstring(L".bold"), L"CourierNewPS-BoldMT"); + POSTSCRIPT_FONT_NAMES->put(Font::MONOSPACED + std::wstring(L".italic"), L"CourierNewPS-ItalicMT"); + POSTSCRIPT_FONT_NAMES->put(Font::MONOSPACED + std::wstring(L".bolditalic"), L"CourierNewPS-BoldItalicMT"); + } + +//JAVA TO C++ CONVERTER TODO TASK: Calls to same-class constructors are not supported in C++ prior to C++11: + PostScriptDocument::PostScriptDocument() { + } + + PostScriptDocument::PostScriptDocument(const std::wstring &fontName, int fontSize) { + InitializeInstanceFields(); + header(); + setFont(fontName, fontSize); + } + + std::wstring PostScriptDocument::getPS() { + close(); +//JAVA TO C++ CONVERTER TODO TASK: There is no native C++ equivalent to 'toString': + return header() + ps->toString(); + } + + void PostScriptDocument::boundingBox(int w, int h) { + boundingBoxWidth = w; + boundingBoxHeight = h; + boundingBox_Renamed = std::wstring::format(Locale::US, L"%%%%BoundingBox: %d %d %d %d\n", 0,0, boundingBoxWidth,boundingBoxHeight); + } + + void PostScriptDocument::close() { + if (closed) { + return; + } + // ps.append("showpage\n"); + ps->append(L"%%Trailer\n"); + closed = true; + } + + StringBuilder *PostScriptDocument::header() { + StringBuilder *b = new StringBuilder(); + b->append(L"%!PS-Adobe-3.0 EPSF-3.0\n"); + b->append(boundingBox_Renamed)->append(L"\n"); + b->append(L"0.3 setlinewidth\n"); + b->append(std::wstring(L"%% x y w h highlight\n") + std::wstring(L"/highlight {\n") + std::wstring(L" 4 dict begin\n") + std::wstring(L" /h exch def\n") + std::wstring(L" /w exch def\n") + std::wstring(L" /y exch def\n") + std::wstring(L" /x exch def\n") + std::wstring(L" gsave\n") + std::wstring(L" newpath\n") + std::wstring(L" x y moveto\n") + std::wstring(L" 0 h rlineto % up to left corner\n") + std::wstring(L" w 0 rlineto % to upper right corner\n") + std::wstring(L" 0 h neg rlineto % to lower right corner\n") + std::wstring(L" w neg 0 rlineto % back home to lower left corner\n") + std::wstring(L" closepath\n") + std::wstring(L" .95 .83 .82 setrgbcolor\n") + std::wstring(L" fill\n") + std::wstring(L" grestore\n") + std::wstring(L" end\n") + std::wstring(L"} def\n")); + + return b; + } + + void PostScriptDocument::setFont(const std::wstring &fontName, int fontSize) { + this->fontMetrics = new SystemFontMetrics(fontName); + this->fontName = fontMetrics->getFont()->getPSName(); + this->fontSize = fontSize; + + std::wstring psname = POSTSCRIPT_FONT_NAMES->get(this->fontName); + if (psname == L"") { + psname = this->fontName; + } + + ps->append(std::wstring::format(Locale::US, L"/%s findfont %d scalefont setfont\n", psname, fontSize)); + } + + void PostScriptDocument::lineWidth(double w) { + lineWidth_Renamed = w; + ps->append(w)->append(L" setlinewidth\n"); + } + + void PostScriptDocument::move(double x, double y) { + ps->append(std::wstring::format(Locale::US, L"%1.3f %1.3f moveto\n", x, y)); + } + + void PostScriptDocument::lineto(double x, double y) { + ps->append(std::wstring::format(Locale::US, L"%1.3f %1.3f lineto\n", x, y)); + } + + void PostScriptDocument::line(double x1, double y1, double x2, double y2) { + move(x1, y1); + lineto(x2, y2); + } + + void PostScriptDocument::rect(double x, double y, double width, double height) { + line(x, y, x, y + height); + line(x, y + height, x + width, y + height); + line(x + width, y + height, x + width, y); + line(x + width, y, x, y); + } + + void PostScriptDocument::highlight(double x, double y, double width, double height) { + ps->append(std::wstring::format(Locale::US, L"%1.3f %1.3f %1.3f %1.3f highlight\n", x, y, width, height)); + } + + void PostScriptDocument::stroke() { + ps->append(L"stroke\n"); + } + + void PostScriptDocument::text(const std::wstring &s, double x, double y) { + StringBuilder *buf = new StringBuilder(); + // escape \, (, ): \\, \(, \) + for (auto c : s.toCharArray()) { + switch (c) { + case L'\\' : + case L'(' : + case L')' : + buf->append(L'\\'); + buf->append(c); + break; + default : + buf->append(c); + break; + } + } +//JAVA TO C++ CONVERTER TODO TASK: There is no native C++ equivalent to 'toString': + s = buf->toString(); + move(x,y); + ps->append(std::wstring::format(Locale::US, L"(%s) show\n", s)); + stroke(); + } + + double PostScriptDocument::getWidth(wchar_t c) { + return fontMetrics->getWidth(c, fontSize); + } + + double PostScriptDocument::getWidth(const std::wstring &s) { + return fontMetrics->getWidth(s, fontSize); + } + + double PostScriptDocument::getLineHeight() { + return fontMetrics->getLineHeight(fontSize); + } + + int PostScriptDocument::getFontSize() { + return fontSize; + } + + void PostScriptDocument::InitializeInstanceFields() { + boundingBoxWidth = 0; + boundingBoxHeight = 0; + fontSize = 12; + lineWidth_Renamed = 0.3; + ps = new StringBuilder(); + closed = false; + } + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/gui/PostScriptDocument.h b/runtime/Cpp/org/antlr/v4/runtime/tree/gui/PostScriptDocument.h new file mode 100755 index 000000000..739fb23fb --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/gui/PostScriptDocument.h @@ -0,0 +1,132 @@ +#pragma once + +#include "SystemFontMetrics.h" +#include +#include + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + namespace gui { + + + class PostScriptDocument { + public: + static const std::wstring DEFAULT_FONT; + + static Map *const POSTSCRIPT_FONT_NAMES; +//JAVA TO C++ CONVERTER TODO TASK: Static constructors are not allowed in native C++: + static PostScriptDocument(); + + protected: + int boundingBoxWidth; + int boundingBoxHeight; + + SystemFontMetrics *fontMetrics; + std::wstring fontName; + int fontSize; +//JAVA TO C++ CONVERTER NOTE: The variable lineWidth was renamed since C++ does not allow variables with the same name as methods: + double lineWidth_Renamed; +//JAVA TO C++ CONVERTER NOTE: The variable boundingBox was renamed since C++ does not allow variables with the same name as methods: + std::wstring boundingBox_Renamed; + + StringBuilder *ps; + bool closed; + + public: +//JAVA TO C++ CONVERTER TODO TASK: Calls to same-class constructors are not supported in C++ prior to C++11: + PostScriptDocument(); //this(DEFAULT_FONT, 12); + + PostScriptDocument(const std::wstring &fontName, int fontSize); + + virtual std::wstring getPS(); + + virtual void boundingBox(int w, int h); + + virtual void close(); + + /// + /// Compute the header separately because we need to wait for the bounding box + protected: + virtual StringBuilder *header(); + + public: + virtual void setFont(const std::wstring &fontName, int fontSize); + + virtual void lineWidth(double w); + + virtual void move(double x, double y); + + virtual void lineto(double x, double y); + + virtual void line(double x1, double y1, double x2, double y2); + + virtual void rect(double x, double y, double width, double height); + + /// + /// Make red box + virtual void highlight(double x, double y, double width, double height); + + virtual void stroke(); + + // public void rarrow(double x, double y) { + // ps.append(String.format(Locale.US, "%1.3f %1.3f rarrow\n", x,y)); + // } + // + // public void darrow(double x, double y) { + // ps.append(String.format(Locale.US, "%1.3f %1.3f darrow\n", x,y)); + // } + + virtual void text(const std::wstring &s, double x, double y); + + // courier new: wid/hei 7.611979 10.0625 + /// + /// All chars are 600 thousands of an 'em' wide if courier + virtual double getWidth(wchar_t c); + virtual double getWidth(const std::wstring &s); + virtual double getLineHeight(); + + virtual int getFontSize(); + + private: + void InitializeInstanceFields(); + }; + + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/gui/SystemFontMetrics.cpp b/runtime/Cpp/org/antlr/v4/runtime/tree/gui/SystemFontMetrics.cpp new file mode 100755 index 000000000..3eac54149 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/gui/SystemFontMetrics.cpp @@ -0,0 +1,33 @@ +#include "SystemFontMetrics.h" + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + namespace gui { + + SystemFontMetrics::SystemFontMetrics(const std::wstring &fontName) : font(new Font(fontName, Font::PLAIN, 1000)) { + BufferedImage *img = new BufferedImage(40, 40, BufferedImage::TYPE_4BYTE_ABGR); + Graphics2D *graphics = GraphicsEnvironment::getLocalGraphicsEnvironment()->createGraphics(img); + FontRenderContext *fontRenderContext = graphics->getFontRenderContext(); + double maxHeight = 0; + for (int i = 0; i < 255; i++) { +//JAVA TO C++ CONVERTER TODO TASK: There is no native C++ equivalent to 'toString': + TextLayout *layout = new TextLayout(wchar_t::toString(static_cast(i)), font, fontRenderContext); + maxHeight = std::max(maxHeight, layout->getBounds()->getHeight()); + BasicFontMetrics::widths[i] = static_cast(layout->getAdvance()); + } + + BasicFontMetrics::maxCharHeight = static_cast(Math::round(maxHeight)); + } + + Font *SystemFontMetrics::getFont() { + return font; + } + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/gui/SystemFontMetrics.h b/runtime/Cpp/org/antlr/v4/runtime/tree/gui/SystemFontMetrics.h new file mode 100755 index 000000000..97c774108 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/gui/SystemFontMetrics.h @@ -0,0 +1,63 @@ +#pragma once + +#include "BasicFontMetrics.h" +#include + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + namespace gui { + + + /// + /// + /// @author Sam Harwell + /// + class SystemFontMetrics : public BasicFontMetrics { + protected: + Font *const font; + + public: + SystemFontMetrics(const std::wstring &fontName); + + virtual Font *getFont(); + }; + + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/gui/TreeLayoutAdaptor.cpp b/runtime/Cpp/org/antlr/v4/runtime/tree/gui/TreeLayoutAdaptor.cpp new file mode 100755 index 000000000..54e69a447 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/gui/TreeLayoutAdaptor.cpp @@ -0,0 +1,104 @@ +#include "TreeLayoutAdaptor.h" + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + namespace gui { + using org::abego::treelayout::TreeForTreeLayout; + using org::antlr::v4::runtime::tree::Tree; + + TreeLayoutAdaptor::AntlrTreeChildrenIterable::AntlrTreeChildrenIterable(Tree *tree) : tree(tree) { + } + + Iterator *TreeLayoutAdaptor::AntlrTreeChildrenIterable::iterator() { + return new IteratorAnonymousInnerClassHelper(this); + } + + TreeLayoutAdaptor::AntlrTreeChildrenIterable::IteratorAnonymousInnerClassHelper::IteratorAnonymousInnerClassHelper(AntlrTreeChildrenIterable *outerInstance) { + this->outerInstance = outerInstance; + i = 0; + } + + bool TreeLayoutAdaptor::AntlrTreeChildrenIterable::IteratorAnonymousInnerClassHelper::hasNext() { + return outerInstance->tree->getChildCount() > i; + } + + org::antlr::v4::runtime::tree::Tree *TreeLayoutAdaptor::AntlrTreeChildrenIterable::IteratorAnonymousInnerClassHelper::next() { + if (!hasNext()) { + throw NoSuchElementException(); + } + + return outerInstance->tree->getChild(i++); + } + + void TreeLayoutAdaptor::AntlrTreeChildrenIterable::IteratorAnonymousInnerClassHelper::remove() { + throw UnsupportedOperationException(); + } + + TreeLayoutAdaptor::AntlrTreeChildrenReverseIterable::AntlrTreeChildrenReverseIterable(Tree *tree) : tree(tree) { + } + + Iterator *TreeLayoutAdaptor::AntlrTreeChildrenReverseIterable::iterator() { + return new IteratorAnonymousInnerClassHelper(this); + } + + TreeLayoutAdaptor::AntlrTreeChildrenReverseIterable::IteratorAnonymousInnerClassHelper::IteratorAnonymousInnerClassHelper(AntlrTreeChildrenReverseIterable *outerInstance) { + this->outerInstance = outerInstance; + i = outerInstance->tree->getChildCount(); + } + + bool TreeLayoutAdaptor::AntlrTreeChildrenReverseIterable::IteratorAnonymousInnerClassHelper::hasNext() { + return i > 0; + } + + org::antlr::v4::runtime::tree::Tree *TreeLayoutAdaptor::AntlrTreeChildrenReverseIterable::IteratorAnonymousInnerClassHelper::next() { + if (!hasNext()) { + throw NoSuchElementException(); + } + + return outerInstance->tree->getChild(--i); + } + + void TreeLayoutAdaptor::AntlrTreeChildrenReverseIterable::IteratorAnonymousInnerClassHelper::remove() { + throw UnsupportedOperationException(); + } + + TreeLayoutAdaptor::TreeLayoutAdaptor(Tree *root) { + this->root = root; + } + + bool TreeLayoutAdaptor::isLeaf(Tree *node) { + return node->getChildCount() == 0; + } + + bool TreeLayoutAdaptor::isChildOfParent(Tree *node, Tree *parentNode) { + return node->getParent() == parentNode; + } + + org::antlr::v4::runtime::tree::Tree *TreeLayoutAdaptor::getRoot() { + return root; + } + + org::antlr::v4::runtime::tree::Tree *TreeLayoutAdaptor::getLastChild(Tree *parentNode) { + return parentNode->getChild(parentNode->getChildCount() - 1); + } + + org::antlr::v4::runtime::tree::Tree *TreeLayoutAdaptor::getFirstChild(Tree *parentNode) { + return parentNode->getChild(0); + } + + Iterable *TreeLayoutAdaptor::getChildrenReverse(Tree *node) { + return new AntlrTreeChildrenReverseIterable(node); + } + + Iterable *TreeLayoutAdaptor::getChildren(Tree *node) { + return new AntlrTreeChildrenIterable(node); + } + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/gui/TreeLayoutAdaptor.h b/runtime/Cpp/org/antlr/v4/runtime/tree/gui/TreeLayoutAdaptor.h new file mode 100755 index 000000000..1b5a9a90b --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/gui/TreeLayoutAdaptor.h @@ -0,0 +1,135 @@ +#pragma once + +#include "Java/src/org/antlr/v4/runtime/tree/Tree.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + namespace gui { + + using org::abego::treelayout::TreeForTreeLayout; + using org::antlr::v4::runtime::tree::Tree; + + + /// + /// Adaptor ANTLR trees to . + class TreeLayoutAdaptor : public TreeForTreeLayout { + private: + class AntlrTreeChildrenIterable : public Iterable { + private: + Tree *const tree; + + public: + AntlrTreeChildrenIterable(Tree *tree); + + virtual Iterator *iterator() override; + + private: + class IteratorAnonymousInnerClassHelper : public Iterator { + private: + AntlrTreeChildrenIterable *const outerInstance; + + public: + IteratorAnonymousInnerClassHelper(AntlrTreeChildrenIterable *outerInstance); + + private: + int i; + + public: + virtual bool hasNext(); + + virtual Tree *next(); + + virtual void remove(); + }; + }; + + private: + class AntlrTreeChildrenReverseIterable : public Iterable { + private: + Tree *const tree; + + public: + AntlrTreeChildrenReverseIterable(Tree *tree); + + virtual Iterator *iterator() override; + + private: + class IteratorAnonymousInnerClassHelper : public Iterator { + private: + AntlrTreeChildrenReverseIterable *const outerInstance; + + public: + IteratorAnonymousInnerClassHelper(AntlrTreeChildrenReverseIterable *outerInstance); + + private: + int i; + + public: + virtual bool hasNext(); + + virtual Tree *next(); + + virtual void remove(); + }; + }; + + private: + Tree *root; + + public: + TreeLayoutAdaptor(Tree *root); + + virtual bool isLeaf(Tree *node) override; + + virtual bool isChildOfParent(Tree *node, Tree *parentNode) override; + + virtual Tree *getRoot() override; + + virtual Tree *getLastChild(Tree *parentNode) override; + + virtual Tree *getFirstChild(Tree *parentNode) override; + + virtual Iterable *getChildrenReverse(Tree *node) override; + + virtual Iterable *getChildren(Tree *node) override; + }; + + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/gui/TreePostScriptGenerator.cpp b/runtime/Cpp/org/antlr/v4/runtime/tree/gui/TreePostScriptGenerator.cpp new file mode 100755 index 000000000..89e52fe80 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/gui/TreePostScriptGenerator.cpp @@ -0,0 +1,142 @@ +#include "TreePostScriptGenerator.h" +#include "Java/src/org/antlr/v4/runtime/tree/gui/TreeLayoutAdaptor.h" +#include "Java/src/org/antlr/v4/runtime/tree/ErrorNode.h" +#include "Java/src/org/antlr/v4/runtime/misc/Utils.h" + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + namespace gui { + using org::abego::treelayout::Configuration; + using org::abego::treelayout::NodeExtentProvider; + using org::abego::treelayout::TreeForTreeLayout; + using org::abego::treelayout::TreeLayout; + using org::abego::treelayout::util::DefaultConfiguration; + using org::antlr::v4::runtime::misc::Nullable; + using org::antlr::v4::runtime::misc::Utils; + using org::antlr::v4::runtime::tree::ErrorNode; + using org::antlr::v4::runtime::tree::Tree; + + TreePostScriptGenerator::VariableExtentProvide::VariableExtentProvide(TreePostScriptGenerator *outerInstance) : outerInstance(outerInstance) { + } + + double TreePostScriptGenerator::VariableExtentProvide::getWidth(Tree *tree) { + std::wstring s = outerInstance->getText(tree); + return outerInstance->doc->getWidth(s) + outerInstance->nodeWidthPadding*2; + } + + double TreePostScriptGenerator::VariableExtentProvide::getHeight(Tree *tree) { + std::wstring s = outerInstance->getText(tree); + double h = outerInstance->doc->getLineHeight() + outerInstance->nodeHeightPaddingAbove + outerInstance->nodeHeightPaddingBelow; +//JAVA TO C++ CONVERTER WARNING: Since the array size is not known in this declaration, Java to C++ Converter has converted this array to a pointer. You will need to call 'delete[]' where appropriate: +//ORIGINAL LINE: String[] lines = s.split("\n"); +//JAVA TO C++ CONVERTER TODO TASK: There is no direct native C++ equivalent to the Java String 'split' method: + std::wstring *lines = s.split(L"\n"); + return h * lines->length; + } + +//JAVA TO C++ CONVERTER TODO TASK: Calls to same-class constructors are not supported in C++ prior to C++11: + TreePostScriptGenerator::TreePostScriptGenerator(std::vector &ruleNames, Tree *root) { + } + + TreePostScriptGenerator::TreePostScriptGenerator(std::vector &ruleNames, Tree *root, const std::wstring &fontName, int fontSize) { + InitializeInstanceFields(); + this->root = root; + setTreeTextProvider(new TreeViewer::DefaultTreeTextProvider(ruleNames)); + doc = new PostScriptDocument(fontName, fontSize); + bool compareNodeIdentities = true; + this->treeLayout = new TreeLayout(new TreeLayoutAdaptor(root), new VariableExtentProvide(this), new DefaultConfiguration(gapBetweenLevels, gapBetweenNodes, Configuration::Location::Bottom), compareNodeIdentities); + } + + std::wstring TreePostScriptGenerator::getPS() { + // generate the edges and boxes (with text) + generateEdges(getTree()->getRoot()); + for (Tree *node : treeLayout->getNodeBounds()->keySet()) { + generateNode(node); + } + + Dimension *size = treeLayout->getBounds()->getBounds()->getSize(); + doc->boundingBox(size->width, size->height); + doc->close(); + return doc->getPS(); + } + + void TreePostScriptGenerator::generateEdges(Tree *parent) { + if (!getTree()->isLeaf(parent)) { + Rectangle2D::Double *parentBounds = getBoundsOfNode(parent); + // System.out.println("%% parent("+getText(parent)+")="+parentBounds); + double x1 = parentBounds->getCenterX(); + double y1 = parentBounds->y; + for (auto child : getChildren(parent)) { + Rectangle2D::Double *childBounds = getBoundsOfNode(child); + // System.out.println("%% child("+getText(child)+")="+childBounds); + double x2 = childBounds->getCenterX(); + double y2 = childBounds->getMaxY(); + doc->line(x1, y1, x2, y2); + generateEdges(child); + } + } + } + + void TreePostScriptGenerator::generateNode(Tree *t) { + // draw the text on top of the box (possibly multiple lines) +//JAVA TO C++ CONVERTER WARNING: Since the array size is not known in this declaration, Java to C++ Converter has converted this array to a pointer. You will need to call 'delete[]' where appropriate: +//ORIGINAL LINE: String[] lines = getText(t).split("\n"); +//JAVA TO C++ CONVERTER TODO TASK: There is no direct native C++ equivalent to the Java String 'split' method: + std::wstring *lines = getText(t).split(L"\n"); + Rectangle2D::Double *box = getBoundsOfNode(t); + // for debugging, turn this on to see boundingbox of nodes + //doc.rect(box.x, box.y, box.width, box.height); + // make error nodes from parse tree red by default + if (dynamic_cast(t) != nullptr) { + doc->highlight(box->x, box->y, box->width, box->height); + } + double x = box->x + nodeWidthPadding; + double y = box->y + nodeHeightPaddingBelow; + for (int i = 0; i < lines->length; i++) { + doc->text(lines[i], x, y); + y += doc->getLineHeight(); + } + } + + TreeForTreeLayout *TreePostScriptGenerator::getTree() { + return treeLayout->getTree(); + } + + Iterable *TreePostScriptGenerator::getChildren(Tree *parent) { + return getTree()->getChildren(parent); + } + + Rectangle2D::Double *TreePostScriptGenerator::getBoundsOfNode(Tree *node) { + return treeLayout->getNodeBounds()->get(node); + } + + std::wstring TreePostScriptGenerator::getText(Tree *tree) { + std::wstring s = treeTextProvider->getText(tree); + s = Utils::escapeWhitespace(s, false); + return s; + } + + org::antlr::v4::runtime::tree::gui::TreeTextProvider *TreePostScriptGenerator::getTreeTextProvider() { + return treeTextProvider; + } + + void TreePostScriptGenerator::setTreeTextProvider(TreeTextProvider *treeTextProvider) { + this->treeTextProvider = treeTextProvider; + } + + void TreePostScriptGenerator::InitializeInstanceFields() { + gapBetweenLevels = 17; + gapBetweenNodes = 7; + nodeWidthPadding = 1; + nodeHeightPaddingAbove = 0; + nodeHeightPaddingBelow = 5; + } + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/gui/TreePostScriptGenerator.h b/runtime/Cpp/org/antlr/v4/runtime/tree/gui/TreePostScriptGenerator.h new file mode 100755 index 000000000..6a1646d8e --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/gui/TreePostScriptGenerator.h @@ -0,0 +1,121 @@ +#pragma once + +#include "Java/src/org/antlr/v4/runtime/tree/Tree.h" +#include "TreeViewer.h" +#include "TreeTextProvider.h" +#include "PostScriptDocument.h" +#include +#include + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + namespace gui { + + using org::abego::treelayout::Configuration; + using org::abego::treelayout::NodeExtentProvider; + using org::abego::treelayout::TreeForTreeLayout; + using org::abego::treelayout::TreeLayout; + using org::abego::treelayout::util::DefaultConfiguration; + using org::antlr::v4::runtime::misc::Nullable; + using org::antlr::v4::runtime::misc::Utils; + using org::antlr::v4::runtime::tree::ErrorNode; + using org::antlr::v4::runtime::tree::Tree; + + + class TreePostScriptGenerator { + public: + class VariableExtentProvide : public NodeExtentProvider { + private: + TreePostScriptGenerator *const outerInstance; + + public: + VariableExtentProvide(TreePostScriptGenerator *outerInstance); + + virtual double getWidth(Tree *tree) override; + + virtual double getHeight(Tree *tree) override; + }; + + protected: + double gapBetweenLevels; + double gapBetweenNodes; + int nodeWidthPadding; // added to left/right + int nodeHeightPaddingAbove; + int nodeHeightPaddingBelow; + + Tree *root; + TreeTextProvider *treeTextProvider; + TreeLayout *treeLayout; + + PostScriptDocument *doc; + + public: +//JAVA TO C++ CONVERTER TODO TASK: Calls to same-class constructors are not supported in C++ prior to C++11: + TreePostScriptGenerator(std::vector &ruleNames, Tree *root); //this(ruleNames, root, PostScriptDocument.DEFAULT_FONT, 11); + + TreePostScriptGenerator(std::vector &ruleNames, Tree *root, const std::wstring &fontName, int fontSize); + + virtual std::wstring getPS(); + + protected: + virtual void generateEdges(Tree *parent); + + virtual void generateNode(Tree *t); + + virtual TreeForTreeLayout *getTree(); + + virtual Iterable *getChildren(Tree *parent); + + virtual Rectangle2D::Double *getBoundsOfNode(Tree *node); + + virtual std::wstring getText(Tree *tree); + + public: + virtual TreeTextProvider *getTreeTextProvider(); + + virtual void setTreeTextProvider(TreeTextProvider *treeTextProvider); + + + private: + void InitializeInstanceFields(); + }; + + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/gui/TreeTextProvider.cpp b/runtime/Cpp/org/antlr/v4/runtime/tree/gui/TreeTextProvider.cpp new file mode 100755 index 000000000..dcfa44474 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/gui/TreeTextProvider.cpp @@ -0,0 +1,15 @@ +#include "TreeTextProvider.h" + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + namespace gui { + using org::antlr::v4::runtime::tree::Tree; + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/gui/TreeTextProvider.h b/runtime/Cpp/org/antlr/v4/runtime/tree/gui/TreeTextProvider.h new file mode 100755 index 000000000..74a5026fe --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/gui/TreeTextProvider.h @@ -0,0 +1,52 @@ +#pragma once + +#include "Tree.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + namespace gui { + + class TreeTextProvider { + public: + virtual std::wstring getText(tree::Tree *node) = 0; + }; + + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/gui/TreeViewer.cpp b/runtime/Cpp/org/antlr/v4/runtime/tree/gui/TreeViewer.cpp new file mode 100755 index 000000000..cf3a7cd79 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/gui/TreeViewer.cpp @@ -0,0 +1,616 @@ +#include "TreeViewer.h" +#include "Java/src/org/antlr/v4/runtime/tree/Trees.h" +#include "Java/src/org/antlr/v4/runtime/tree/gui/TreeLayoutAdaptor.h" +#include "Java/src/org/antlr/v4/runtime/tree/ErrorNode.h" +#include "Java/src/org/antlr/v4/runtime/misc/Utils.h" +#include "Java/src/org/antlr/v4/runtime/misc/JFileChooserConfirmOverwrite.h" +#include "Java/src/org/antlr/v4/runtime/misc/GraphicsSupport.h" + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + namespace gui { + using org::abego::treelayout::NodeExtentProvider; + using org::abego::treelayout::TreeForTreeLayout; + using org::abego::treelayout::TreeLayout; + using org::abego::treelayout::util::DefaultConfiguration; + using org::antlr::v4::runtime::misc::GraphicsSupport; + using org::antlr::v4::runtime::misc::JFileChooserConfirmOverwrite; + using org::antlr::v4::runtime::misc::NotNull; + using org::antlr::v4::runtime::misc::Nullable; + using org::antlr::v4::runtime::misc::Utils; + using org::antlr::v4::runtime::tree::ErrorNode; + using org::antlr::v4::runtime::tree::Tree; + using org::antlr::v4::runtime::tree::Trees; + + TreeViewer::DefaultTreeTextProvider::DefaultTreeTextProvider(std::vector &ruleNames) : ruleNames(ruleNames) { + } + + std::wstring TreeViewer::DefaultTreeTextProvider::getText(Tree *node) { + return static_cast(Trees::getNodeText(node, ruleNames)); + } + + TreeViewer::VariableExtentProvide::VariableExtentProvide(TreeViewer *viewer) { + this->viewer = viewer; + } + + double TreeViewer::VariableExtentProvide::getWidth(Tree *tree) { + FontMetrics *fontMetrics = viewer->getFontMetrics(viewer->font); + std::wstring s = viewer->getText(tree); + int w = fontMetrics->stringWidth(s) + viewer->nodeWidthPadding*2; + return w; + } + + double TreeViewer::VariableExtentProvide::getHeight(Tree *tree) { + FontMetrics *fontMetrics = viewer->getFontMetrics(viewer->font); + int h = fontMetrics->getHeight() + viewer->nodeHeightPadding*2; + std::wstring s = viewer->getText(tree); +//JAVA TO C++ CONVERTER WARNING: Since the array size is not known in this declaration, Java to C++ Converter has converted this array to a pointer. You will need to call 'delete[]' where appropriate: +//ORIGINAL LINE: String[] lines = s.split("\n"); +//JAVA TO C++ CONVERTER TODO TASK: There is no direct native C++ equivalent to the Java String 'split' method: + std::wstring *lines = s.split(L"\n"); + return h * lines->length; + } + + TreeViewer::TreeNodeWrapper::TreeNodeWrapper(Tree *tree, TreeViewer *viewer) : javax::swing::tree::DefaultMutableTreeNode(tree), viewer(viewer) { + } + + std::wstring TreeViewer::TreeNodeWrapper::toString() { + return viewer->getText(static_cast(this->getUserObject())); + } + + int TreeViewer::EmptyIcon::getIconWidth() { + return 0; + } + + int TreeViewer::EmptyIcon::getIconHeight() { + return 0; + } + + void TreeViewer::EmptyIcon::paintIcon(Component *c, Graphics *g, int x, int y) { + /* Do nothing. */ + } + +java::awt::Color *const TreeViewer::LIGHT_RED = new java::awt::Color(244, 213, 211); + + TreeViewer::TreeViewer(std::vector &ruleNames, Tree *tree) { + InitializeInstanceFields(); + setTreeTextProvider(new DefaultTreeTextProvider(ruleNames)); + bool useIdentity = true; // compare node identity + this->treeLayout = new TreeLayout(new TreeLayoutAdaptor(tree), new TreeViewer::VariableExtentProvide(this), new DefaultConfiguration(gapBetweenLevels, gapBetweenNodes), useIdentity); + updatePreferredSize(); + setFont(font); + } + + void TreeViewer::updatePreferredSize() { + setPreferredSize(getScaledTreeSize()); + invalidate(); + if (getParent() != nullptr) { + getParent()->validate(); + } + repaint(); + } + + bool TreeViewer::getUseCurvedEdges() { + return useCurvedEdges; + } + + void TreeViewer::setUseCurvedEdges(bool useCurvedEdges) { + this->useCurvedEdges = useCurvedEdges; + } + + void TreeViewer::paintEdges(Graphics *g, Tree *parent) { + if (!getTree()->isLeaf(parent)) { + BasicStroke *stroke = new BasicStroke(1.0f, BasicStroke::CAP_ROUND, BasicStroke::JOIN_ROUND); + (static_cast(g))->setStroke(stroke); + + Rectangle2D::Double *parentBounds = getBoundsOfNode(parent); + double x1 = parentBounds->getCenterX(); + double y1 = parentBounds->getMaxY(); + for (Tree *child : getTree()->getChildren(parent)) { + Rectangle2D::Double *childBounds = getBoundsOfNode(child); + double x2 = childBounds->getCenterX(); + double y2 = childBounds->getMinY(); + if (getUseCurvedEdges()) { + CubicCurve2D *c = new CubicCurve2D::Double(); + double ctrlx1 = x1; + double ctrly1 = (y1 + y2) / 2; + double ctrlx2 = x2; + double ctrly2 = y1; + c->setCurve(x1, y1, ctrlx1, ctrly1, ctrlx2, ctrly2, x2, y2); + (static_cast(g))->draw(c); + } else { + g->drawLine(static_cast(x1), static_cast(y1), static_cast(x2), static_cast(y2)); + } + paintEdges(g, child); + } + } + } + + void TreeViewer::paintBox(Graphics *g, Tree *tree) { + Rectangle2D::Double *box = getBoundsOfNode(tree); + // draw the box in the background + if (isHighlighted(tree) || boxColor != nullptr || dynamic_cast(tree) != nullptr) { + if (isHighlighted(tree)) { + g->setColor(highlightedBoxColor); + } else if (dynamic_cast(tree) != nullptr) { + g->setColor(LIGHT_RED); + } else { + g->setColor(boxColor); + } + g->fillRoundRect(static_cast(box->x), static_cast(box->y), static_cast(box->width) - 1, static_cast(box->height) - 1, arcSize, arcSize); + } + if (borderColor != nullptr) { + g->setColor(borderColor); + g->drawRoundRect(static_cast(box->x), static_cast(box->y), static_cast(box->width) - 1, static_cast(box->height) - 1, arcSize, arcSize); + } + + // draw the text on top of the box (possibly multiple lines) + g->setColor(textColor); + std::wstring s = getText(tree); +//JAVA TO C++ CONVERTER WARNING: Since the array size is not known in this declaration, Java to C++ Converter has converted this array to a pointer. You will need to call 'delete[]' where appropriate: +//ORIGINAL LINE: String[] lines = s.split("\n"); +//JAVA TO C++ CONVERTER TODO TASK: There is no direct native C++ equivalent to the Java String 'split' method: + std::wstring *lines = s.split(L"\n"); + FontMetrics *m = getFontMetrics(font); + int x = static_cast(box->x) + arcSize / 2 + nodeWidthPadding; + int y = static_cast(box->y) + m->getAscent() + m->getLeading() + 1 + nodeHeightPadding; + for (int i = 0; i < lines->length; i++) { + text(g, lines[i], x, y); + y += m->getHeight(); + } + } + + void TreeViewer::text(Graphics *g, const std::wstring &s, int x, int y) { + // System.out.println("drawing '"+s+"' @ "+x+","+y); + s = Utils::escapeWhitespace(s, true); + g->drawString(s, x, y); + } + + void TreeViewer::paint(Graphics *g) { + JComponent::paint(g); + + Graphics2D *g2 = static_cast(g); + // anti-alias the lines + g2->setRenderingHint(RenderingHints::KEY_ANTIALIASING, RenderingHints::VALUE_ANTIALIAS_ON); + + // Anti-alias the text + g2->setRenderingHint(RenderingHints::KEY_TEXT_ANTIALIASING, RenderingHints::VALUE_TEXT_ANTIALIAS_ON); + + // AffineTransform at = g2.getTransform(); + // g2.scale( + // (double) this.getWidth() / 400, + // (double) this.getHeight() / 400); + // + // g2.setTransform(at); + + paintEdges(g, getTree()->getRoot()); + + // paint the boxes + for (org.antlr::v4::runtime::tree::Tree *org : treeLayout->getNodeBounds()->keySet()) { + paintBox(g, org.antlr::v4::runtime::tree::Tree); + } + } + + Graphics *TreeViewer::getComponentGraphics(Graphics *g) { + Graphics2D *g2d = static_cast(g); + g2d->scale(scale, scale); + return JComponent::getComponentGraphics(g2d); + } + + JDialog *TreeViewer::showInDialog(TreeViewer *const viewer) { + JDialog * const dialog = new JDialog(); + dialog->setTitle(L"Parse Tree Inspector"); + + // Make new content panes + Container * const mainPane = new JPanel(new BorderLayout(5,5)); + Container * const contentPane = new JPanel(new BorderLayout(0,0)); + contentPane->setBackground(Color::white); + + // Wrap viewer in scroll pane + JScrollPane *scrollPane = new JScrollPane(viewer); + // Make the scrollpane (containing the viewer) the center component + contentPane->add(scrollPane, BorderLayout::CENTER); + + JPanel *wrapper = new JPanel(new FlowLayout()); + + // Add button to bottom + JPanel *bottomPanel = new JPanel(new BorderLayout(0,0)); + contentPane->add(bottomPanel, BorderLayout::SOUTH); + + JButton *ok = new JButton(L"OK"); + ok->addActionListener(new ActionListenerAnonymousInnerClassHelper(dialog) + ); + wrapper->add(ok); + + // Add an export-to-png button right of the "OK" button + JButton *png = new JButton(L"png"); + png->addActionListener(new ActionListenerAnonymousInnerClassHelper2(viewer, dialog) + ); + wrapper->add(png); + + bottomPanel->add(wrapper, BorderLayout::SOUTH); + + // Add scale slider + int sliderValue = static_cast((viewer->getScale() - 1.0) * 1000); + JSlider * const scaleSlider = new JSlider(JSlider::HORIZONTAL, -999,1000,sliderValue); + scaleSlider->addChangeListener(new ChangeListenerAnonymousInnerClassHelper(viewer, scaleSlider) + ); + bottomPanel->add(scaleSlider, BorderLayout::CENTER); + + // Add a JTree representing the parser tree of the input. + JPanel *treePanel = new JPanel(new BorderLayout(5, 5)); + + // An "empty" icon that will be used for the JTree's nodes. + Icon *empty = new EmptyIcon(); + + UIManager::put(L"Tree.closedIcon", empty); + UIManager::put(L"Tree.openIcon", empty); + UIManager::put(L"Tree.leafIcon", empty); + + Tree *parseTreeRoot = viewer->getTree()->getRoot(); + TreeNodeWrapper *nodeRoot = new TreeNodeWrapper(parseTreeRoot, viewer); + fillTree(nodeRoot, parseTreeRoot, viewer); + JTree * const tree = new JTree(nodeRoot); + tree->getSelectionModel()->setSelectionMode(TreeSelectionModel::SINGLE_TREE_SELECTION); + + tree->addTreeSelectionListener(new TreeSelectionListenerAnonymousInnerClassHelper(viewer)); + + treePanel->add(new JScrollPane(tree)); + + // Create the pane for both the JTree and the AST + JSplitPane *splitPane = new JSplitPane(JSplitPane::HORIZONTAL_SPLIT, treePanel, contentPane); + + mainPane->add(splitPane, BorderLayout::CENTER); + + dialog->setContentPane(mainPane); + + // make viz + dialog->setDefaultCloseOperation(JFrame::DISPOSE_ON_CLOSE); + dialog->setPreferredSize(new Dimension(600, 500)); + dialog->pack(); + + // After pack(): set the divider at 1/3 of the frame. + splitPane->setDividerLocation(0.33); + + dialog->setLocationRelativeTo(nullptr); + dialog->setVisible(true); + return dialog; + } + + TreeViewer::ActionListenerAnonymousInnerClassHelper::ActionListenerAnonymousInnerClassHelper(JDialog *dialog) { + this->dialog = dialog; + } + + void TreeViewer::ActionListenerAnonymousInnerClassHelper::actionPerformed(ActionEvent *e) { + dialog->setVisible(false); + dialog->dispose(); + } + + TreeViewer::ActionListenerAnonymousInnerClassHelper2::ActionListenerAnonymousInnerClassHelper2(org::antlr::v4::runtime::tree::gui::TreeViewer *viewer, JDialog *dialog) { + this->viewer = viewer; + this->dialog = dialog; + } + + void TreeViewer::ActionListenerAnonymousInnerClassHelper2::actionPerformed(ActionEvent *e) { + generatePNGFile(viewer, dialog); + } + + TreeViewer::ChangeListenerAnonymousInnerClassHelper::ChangeListenerAnonymousInnerClassHelper(org::antlr::v4::runtime::tree::gui::TreeViewer *viewer, JSlider *scaleSlider) { + this->viewer = viewer; + this->scaleSlider = scaleSlider; + } + + void TreeViewer::ChangeListenerAnonymousInnerClassHelper::stateChanged(ChangeEvent *e) { + int v = scaleSlider->getValue(); + viewer->setScale(v / 1000.0 + 1.0); + } + + TreeViewer::TreeSelectionListenerAnonymousInnerClassHelper::TreeSelectionListenerAnonymousInnerClassHelper(org::antlr::v4::runtime::tree::gui::TreeViewer *viewer) { + this->viewer = viewer; + } + + void TreeViewer::TreeSelectionListenerAnonymousInnerClassHelper::valueChanged(TreeSelectionEvent *e) { + + JTree *selectedTree = static_cast(e->getSource()); + TreePath *path = selectedTree->getSelectionPath(); + TreeNodeWrapper *treeNode = static_cast(path->getLastPathComponent()); + + // Set the clicked AST. + viewer->treeLayout = new TreeLayout(new TreeLayoutAdaptor(static_cast(treeNode->getUserObject())), new TreeViewer::VariableExtentProvide(viewer), new DefaultConfiguration(viewer->gapBetweenLevels, viewer->gapBetweenNodes), true); + + // Let the UI display this new AST. + viewer->updatePreferredSize(); + } + + void TreeViewer::generatePNGFile(TreeViewer *viewer, JDialog *dialog) { + BufferedImage *bi = new BufferedImage(viewer->getSize()->width, viewer->getSize()->height, BufferedImage::TYPE_INT_ARGB); + Graphics *g = bi->createGraphics(); + viewer->paint(g); + g->dispose(); + + try { + File *suggestedFile = generateNonExistingPngFile(); + JFileChooser *fileChooser = new JFileChooserConfirmOverwrite(); + fileChooser->setCurrentDirectory(suggestedFile->getParentFile()); + fileChooser->setSelectedFile(suggestedFile); + FileFilter *pngFilter = new FileFilterAnonymousInnerClassHelper(); + + fileChooser->addChoosableFileFilter(pngFilter); + fileChooser->setFileFilter(pngFilter); + + int returnValue = fileChooser->showSaveDialog(dialog); + if (returnValue == JFileChooser::APPROVE_OPTION) { + File *pngFile = fileChooser->getSelectedFile(); + ImageIO::write(bi, L"png", pngFile); + + try { + // Try to open the parent folder using the OS' native file manager. + Desktop::getDesktop()->open(pngFile->getParentFile()); + } catch (std::exception &ex) { + // We could not launch the file manager: just show a popup that we + // succeeded in saving the PNG file. + JOptionPane::showMessageDialog(dialog, std::wstring(L"Saved PNG to: ") + pngFile->getAbsolutePath()); + ex.printStackTrace(); + } + } + } catch (std::exception &ex) { + JOptionPane::showMessageDialog(dialog, std::wstring(L"Could not export to PNG: ") + ex.what(), L"Error", JOptionPane::ERROR_MESSAGE); + ex.printStackTrace(); + } + } + + TreeViewer::FileFilterAnonymousInnerClassHelper::FileFilterAnonymousInnerClassHelper() { + } + + bool TreeViewer::FileFilterAnonymousInnerClassHelper::accept(File *pathname) { + if (pathname->isFile()) { +//JAVA TO C++ CONVERTER TODO TASK: There is no direct native C++ equivalent to the Java String 'endsWith' method: + return pathname->getName()->toLowerCase()->endsWith(L".png"); + } + + return true; + } + + std::wstring TreeViewer::FileFilterAnonymousInnerClassHelper::getDescription() { + return L"PNG Files (*.png)"; + } + + File *TreeViewer::generateNonExistingPngFile() { + + const std::wstring parent = L"."; + const std::wstring name = L"antlr4_parse_tree"; + const std::wstring extension = L".png"; + + File *pngFile = new File(parent, name + extension); + + int counter = 1; + + // Keep looping until we create a File that does not yet exist. + while (pngFile->exists()) { + pngFile = new File(parent, name + std::wstring(L"_") + StringConverterHelper::toString(counter) + extension); + counter++; + } + + return pngFile; + } + + void TreeViewer::fillTree(TreeNodeWrapper *node, Tree *tree, TreeViewer *viewer) { + + if (tree == nullptr) { + return; + } + + for (int i = 0; i < tree->getChildCount(); i++) { + + Tree *childTree = tree->getChild(i); + TreeNodeWrapper *childNode = new TreeNodeWrapper(childTree, viewer); + + node->add(childNode); + + fillTree(childNode, childTree, viewer); + } + } + + Dimension *TreeViewer::getScaledTreeSize() { + Dimension *scaledTreeSize = treeLayout->getBounds()->getBounds()->getSize(); + scaledTreeSize = new Dimension(static_cast(scaledTreeSize->width*scale), static_cast(scaledTreeSize->height*scale)); + return scaledTreeSize; + } + + Future *TreeViewer::open() { + TreeViewer * const viewer = this; + viewer->setScale(1.5); + Callable *callable = new CallableAnonymousInnerClassHelper(this, viewer); + + ExecutorService *executor = Executors::newSingleThreadExecutor(); + + try { + return executor->submit(callable); + } finally { + executor->shutdown(); + } + } + + TreeViewer::CallableAnonymousInnerClassHelper::CallableAnonymousInnerClassHelper(TreeViewer *outerInstance, org::antlr::v4::runtime::tree::gui::TreeViewer *viewer) { + this->outerInstance = outerInstance; + this->viewer = viewer; + } + + JDialog *TreeViewer::CallableAnonymousInnerClassHelper::call() throw(std::exception) { + SwingUtilities::invokeAndWait(new RunnableAnonymousInnerClassHelper(this)); + + return result; + } + + TreeViewer::CallableAnonymousInnerClassHelper::RunnableAnonymousInnerClassHelper::RunnableAnonymousInnerClassHelper(CallableAnonymousInnerClassHelper *outerInstance) { + this->outerInstance = outerInstance; + } + + void TreeViewer::CallableAnonymousInnerClassHelper::RunnableAnonymousInnerClassHelper::run() { + result = showInDialog(outerInstance->viewer); + } + + void TreeViewer::save(const std::wstring &fileName) throw(IOException, PrintException) { + JDialog *dialog = new JDialog(); + Container *contentPane = dialog->getContentPane(); + (static_cast(contentPane))->setBorder(BorderFactory::createEmptyBorder(10, 10, 10, 10)); + contentPane->add(this); + contentPane->setBackground(Color::white); + dialog->pack(); + dialog->setLocationRelativeTo(nullptr); + dialog->dispose(); + GraphicsSupport::saveImage(this, fileName); + } + + Rectangle2D::Double *TreeViewer::getBoundsOfNode(Tree *node) { + return treeLayout->getNodeBounds()->get(node); + } + + std::wstring TreeViewer::getText(Tree *tree) { + std::wstring s = treeTextProvider->getText(tree); + s = Utils::escapeWhitespace(s, true); + return s; + } + + org::antlr::v4::runtime::tree::gui::TreeTextProvider *TreeViewer::getTreeTextProvider() { + return treeTextProvider; + } + + void TreeViewer::setTreeTextProvider(TreeTextProvider *treeTextProvider) { + this->treeTextProvider = treeTextProvider; + } + + void TreeViewer::setFontSize(int sz) { + fontSize = sz; + font = new Font(fontName, fontStyle, fontSize); + } + + void TreeViewer::setFontName(const std::wstring &name) { + fontName = name; + font = new Font(fontName, fontStyle, fontSize); + } + + void TreeViewer::addHighlightedNodes(Collection *nodes) { + highlightedNodes = std::vector(); + highlightedNodes.addAll(nodes); + } + + void TreeViewer::removeHighlightedNodes(Collection *nodes) { + if (highlightedNodes.size() > 0) { + // only remove exact objects defined by ==, not equals() + for (auto t : nodes) { + int i = getHighlightedNodeIndex(t); + if (i >= 0) { + highlightedNodes.remove(i); + } + } + } + } + + bool TreeViewer::isHighlighted(Tree *node) { + return getHighlightedNodeIndex(node) >= 0; + } + + int TreeViewer::getHighlightedNodeIndex(Tree *node) { + if (highlightedNodes.empty()) { + return -1; + } + for (int i = 0; i < highlightedNodes.size(); i++) { + Tree *t = highlightedNodes[i]; + if (t == node) { + return i; + } + } + return -1; + } + + Font *TreeViewer::getFont() { + return font; + } + + void TreeViewer::setFont(Font *font) { + this->font = font; + } + + int TreeViewer::getArcSize() { + return arcSize; + } + + void TreeViewer::setArcSize(int arcSize) { + this->arcSize = arcSize; + } + + Color *TreeViewer::getBoxColor() { + return boxColor; + } + + void TreeViewer::setBoxColor(Color *boxColor) { + this->boxColor = boxColor; + } + + Color *TreeViewer::getHighlightedBoxColor() { + return highlightedBoxColor; + } + + void TreeViewer::setHighlightedBoxColor(Color *highlightedBoxColor) { + this->highlightedBoxColor = highlightedBoxColor; + } + + Color *TreeViewer::getBorderColor() { + return borderColor; + } + + void TreeViewer::setBorderColor(Color *borderColor) { + this->borderColor = borderColor; + } + + Color *TreeViewer::getTextColor() { + return textColor; + } + + void TreeViewer::setTextColor(Color *textColor) { + this->textColor = textColor; + } + + TreeForTreeLayout *TreeViewer::getTree() { + return treeLayout->getTree(); + } + + double TreeViewer::getScale() { + return scale; + } + + void TreeViewer::setScale(double scale) { + if (scale <= 0) { + scale = 1; + } + this->scale = scale; + updatePreferredSize(); + } + + void TreeViewer::InitializeInstanceFields() { + fontName = L"Helvetica"; + fontStyle = java::awt::Font::PLAIN; + fontSize = 11; + font = new java::awt::Font(fontName, fontStyle, fontSize); + gapBetweenLevels = 17; + gapBetweenNodes = 7; + nodeWidthPadding = 2; + nodeHeightPadding = 0; + arcSize = 0; + scale = 1.0; + boxColor = 0; + highlightedBoxColor = java::awt::Color::lightGray; + borderColor = 0; + textColor = java::awt::Color::black; + useCurvedEdges = false; + } + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/gui/TreeViewer.h b/runtime/Cpp/org/antlr/v4/runtime/tree/gui/TreeViewer.h new file mode 100755 index 000000000..7a3308472 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/gui/TreeViewer.h @@ -0,0 +1,333 @@ +#pragma once + +#include "TreeTextProvider.h" +#include "Java/src/org/antlr/v4/runtime/tree/Tree.h" +#include "TreePostScriptGenerator.h" +#include +#include +#include +#include "stringconverter.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + namespace gui { + + using org::abego::treelayout::NodeExtentProvider; + using org::abego::treelayout::TreeForTreeLayout; + using org::abego::treelayout::TreeLayout; + using org::abego::treelayout::util::DefaultConfiguration; + using org::antlr::v4::runtime::misc::GraphicsSupport; + using org::antlr::v4::runtime::misc::JFileChooserConfirmOverwrite; + using org::antlr::v4::runtime::misc::NotNull; + using org::antlr::v4::runtime::misc::Nullable; + using org::antlr::v4::runtime::misc::Utils; + using org::antlr::v4::runtime::tree::ErrorNode; + using org::antlr::v4::runtime::tree::Tree; + using org::antlr::v4::runtime::tree::Trees; + + + class TreeViewer : public JComponent { + public: + class DefaultTreeTextProvider : public TreeTextProvider { + private: + const std::vector ruleNames; + + public: + DefaultTreeTextProvider(std::vector &ruleNames); + + virtual std::wstring getText(Tree *node) override; + }; + + public: + class VariableExtentProvide : public NodeExtentProvider { + public: + TreeViewer *viewer; + VariableExtentProvide(TreeViewer *viewer); + virtual double getWidth(Tree *tree) override; + + virtual double getHeight(Tree *tree) override; + }; + + private: + class TreeNodeWrapper : public DefaultMutableTreeNode { + + public: + TreeViewer *const viewer; + + TreeNodeWrapper(Tree *tree, TreeViewer *viewer); + + virtual std::wstring toString() override; + }; + + private: + class EmptyIcon : public Icon { + + public: + virtual int getIconWidth() override; + + virtual int getIconHeight() override; + + virtual void paintIcon(Component *c, Graphics *g, int x, int y) override; + }; + public: + static Color *const LIGHT_RED; + + protected: + TreeTextProvider *treeTextProvider; + TreeLayout *treeLayout; + std::vector highlightedNodes; + + std::wstring fontName; //Font.SANS_SERIF; + int fontStyle; + int fontSize; + Font *font; + + double gapBetweenLevels; + double gapBetweenNodes; + int nodeWidthPadding; // added to left/right + int nodeHeightPadding; // added above/below + int arcSize; // make an arc in node outline? + + double scale; + + Color *boxColor; // set to a color to make it draw background + + Color *highlightedBoxColor; + Color *borderColor; + Color *textColor; + + public: + TreeViewer(std::vector &ruleNames, Tree *tree); + + private: + void updatePreferredSize(); + + // ---------------- PAINT ----------------------------------------------- + + bool useCurvedEdges; + + public: + virtual bool getUseCurvedEdges(); + + virtual void setUseCurvedEdges(bool useCurvedEdges); + + protected: + virtual void paintEdges(Graphics *g, Tree *parent); + + virtual void paintBox(Graphics *g, Tree *tree); + + public: + virtual void text(Graphics *g, const std::wstring &s, int x, int y); + + virtual void paint(Graphics *g) override; + + protected: + virtual Graphics *getComponentGraphics(Graphics *g) override; + + // ---------------------------------------------------------------------- + + static JDialog *showInDialog(TreeViewer *const viewer); + + private: + class ActionListenerAnonymousInnerClassHelper : public ActionListener { + private: + JDialog *dialog; + + public: + ActionListenerAnonymousInnerClassHelper(JDialog *dialog); + + virtual void actionPerformed(ActionEvent *e) override; + }; + + private: + class ActionListenerAnonymousInnerClassHelper2 : public ActionListener { + private: + org::antlr::v4::runtime::tree::gui::TreeViewer *viewer; + JDialog *dialog; + + public: + ActionListenerAnonymousInnerClassHelper2(org::antlr::v4::runtime::tree::gui::TreeViewer *viewer, JDialog *dialog); + + virtual void actionPerformed(ActionEvent *e) override; + }; + + private: + class ChangeListenerAnonymousInnerClassHelper : public ChangeListener { + private: + org::antlr::v4::runtime::tree::gui::TreeViewer *viewer; + JSlider *scaleSlider; + + public: + ChangeListenerAnonymousInnerClassHelper(org::antlr::v4::runtime::tree::gui::TreeViewer *viewer, JSlider *scaleSlider); + + virtual void stateChanged(ChangeEvent *e) override; + }; + + private: + class TreeSelectionListenerAnonymousInnerClassHelper : public TreeSelectionListener { + private: + org::antlr::v4::runtime::tree::gui::TreeViewer *viewer; + + public: + TreeSelectionListenerAnonymousInnerClassHelper(org::antlr::v4::runtime::tree::gui::TreeViewer *viewer); + + virtual void valueChanged(TreeSelectionEvent *e) override; + }; + + private: + static void generatePNGFile(TreeViewer *viewer, JDialog *dialog); + + private: + class FileFilterAnonymousInnerClassHelper : public FileFilter { + public: + FileFilterAnonymousInnerClassHelper(); + + + virtual bool accept(File *pathname) override; + + virtual std::wstring getDescription() override; + }; + + private: + static File *generateNonExistingPngFile(); + + static void fillTree(TreeNodeWrapper *node, Tree *tree, TreeViewer *viewer); + + Dimension *getScaledTreeSize(); + + public: + virtual Future *open(); + + private: + class CallableAnonymousInnerClassHelper : public Callable { + private: + TreeViewer *const outerInstance; + + org::antlr::v4::runtime::tree::gui::TreeViewer *viewer; + + public: + CallableAnonymousInnerClassHelper(TreeViewer *outerInstance, org::antlr::v4::runtime::tree::gui::TreeViewer *viewer); + + JDialog *result; + + virtual JDialog *call() throw(std::exception) override; + + private: + class RunnableAnonymousInnerClassHelper : public Runnable { + private: + CallableAnonymousInnerClassHelper *const outerInstance; + + public: + RunnableAnonymousInnerClassHelper(CallableAnonymousInnerClassHelper *outerInstance); + + virtual void run() override; + }; + }; + + public: + virtual void save(const std::wstring &fileName) throw(IOException, PrintException); + + // --------------------------------------------------- + + protected: + virtual Rectangle2D::Double *getBoundsOfNode(Tree *node); + + virtual std::wstring getText(Tree *tree); + + public: + virtual TreeTextProvider *getTreeTextProvider(); + + virtual void setTreeTextProvider(TreeTextProvider *treeTextProvider); + + virtual void setFontSize(int sz); + + virtual void setFontName(const std::wstring &name); + + /// + /// Slow for big lists of highlighted nodes + virtual void addHighlightedNodes(Collection *nodes); + + virtual void removeHighlightedNodes(Collection *nodes); + + protected: + virtual bool isHighlighted(Tree *node); + + virtual int getHighlightedNodeIndex(Tree *node); + + public: + virtual Font *getFont() override; + + virtual void setFont(Font *font) override; + + virtual int getArcSize(); + + virtual void setArcSize(int arcSize); + + virtual Color *getBoxColor(); + + virtual void setBoxColor(Color *boxColor); + + virtual Color *getHighlightedBoxColor(); + + virtual void setHighlightedBoxColor(Color *highlightedBoxColor); + + virtual Color *getBorderColor(); + + virtual void setBorderColor(Color *borderColor); + + virtual Color *getTextColor(); + + virtual void setTextColor(Color *textColor); + + protected: + virtual TreeForTreeLayout *getTree(); + + public: + virtual double getScale(); + + virtual void setScale(double scale); + + + private: + void InitializeInstanceFields(); + }; + + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/Chunk.cpp b/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/Chunk.cpp new file mode 100755 index 000000000..8ca9cd989 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/Chunk.cpp @@ -0,0 +1,44 @@ +#include "Chunk.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + namespace pattern { + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/Chunk.h b/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/Chunk.h new file mode 100755 index 000000000..9c2c2d97c --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/Chunk.h @@ -0,0 +1,70 @@ +#pragma once + +#include + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Sam Harwell + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + namespace pattern { + + /// + /// A chunk is either a token tag, a rule tag, or a span of literal text within a + /// tree pattern. + ///

+ /// The method returns a list of + /// chunks in preparation for creating a token stream by + /// . From there, we get a parse + /// tree from with . These + /// chunks are converted to , , or the + /// regular tokens of the text surrounding the tags. + ///

+ class Chunk { + + /// + /// This method returns a text representation of the tag chunk. Labeled tags + /// are returned in the form {@code label:tag}, and unlabeled tags are + /// returned as just the tag name. + /// + virtual std::wstring toString() { + std::wstring str; + return str; + } + }; + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/ParseTreeMatch.cpp b/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/ParseTreeMatch.cpp new file mode 100755 index 000000000..ef970e710 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/ParseTreeMatch.cpp @@ -0,0 +1,106 @@ +#include "ParseTreeMatch.h" +#include "Exceptions.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Sam Harwell + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + namespace pattern { + + ParseTreeMatch::ParseTreeMatch(ParseTree *tree, ParseTreePattern *pattern, misc::MultiMap *labels, ParseTree *mismatchedNode) : tree(tree), pattern(pattern), labels(labels), mismatchedNode(mismatchedNode) { + if (tree == nullptr) { + throw IllegalArgumentException(L"tree cannot be null"); + } + + if (pattern == nullptr) { + throw IllegalArgumentException(L"pattern cannot be null"); + } + + if (labels == nullptr) { + throw IllegalArgumentException(L"labels cannot be null"); + } + + } + + org::antlr::v4::runtime::tree::ParseTree *ParseTreeMatch::get(const std::wstring &label) { + std::vector parseTrees = labels->at(label); + if (parseTrees.empty()) { + return nullptr; + } + + return parseTrees[parseTrees.size() - 1]; // return last if multiple + } + + std::vector ParseTreeMatch::getAll(const std::wstring &label) { + std::vector nodes = labels->at(label); + if (nodes.empty()) { + return std::vector();// Collections::emptyList(); + } + + return nodes; + } + + org::antlr::v4::runtime::misc::MultiMap *ParseTreeMatch::getLabels() { + return labels; + } + + org::antlr::v4::runtime::tree::ParseTree *ParseTreeMatch::getMismatchedNode() { + return mismatchedNode; + } + + bool ParseTreeMatch::succeeded() { + return mismatchedNode == nullptr; + } + + org::antlr::v4::runtime::tree::pattern::ParseTreePattern *ParseTreeMatch::getPattern() { + return pattern; + } + + org::antlr::v4::runtime::tree::ParseTree *ParseTreeMatch::getTree() { + return tree; + } + + std::wstring ParseTreeMatch::toString() { + if (succeeded()) { + return L"Match succeeded; found " + std::to_wstring(getLabels()->size()) + L" labels"; + } else { + return L"Match failed; found " + std::to_wstring(getLabels()->size()) + L" labels"; + } + } + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/ParseTreeMatch.h b/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/ParseTreeMatch.h new file mode 100755 index 000000000..36a4f3813 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/ParseTreeMatch.h @@ -0,0 +1,177 @@ +#pragma once + + +#include "MultiMap.h" +#include +#include + +#include "Declarations.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Sam Harwell + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + namespace pattern { + /// + /// Represents the result of matching a against a tree pattern. + /// + class ParseTreeMatch { + /// + /// This is the backing field for . + /// + private: + ParseTree *const tree; + + /// + /// This is the backing field for . + /// + ParseTreePattern *const pattern; + + /// + /// This is the backing field for . + /// + misc::MultiMap *const labels; + + /// + /// This is the backing field for . + /// + ParseTree *const mismatchedNode; + + /// + /// Constructs a new instance of from the specified + /// parse tree and pattern. + /// + /// The parse tree to match against the pattern. + /// The parse tree pattern. + /// A mapping from label names to collections of + /// objects located by the tree pattern matching process. + /// The first node which failed to match the tree + /// pattern during the matching process. + /// + /// if {@code tree} is {@code null} + /// if {@code pattern} is {@code null} + /// if {@code labels} is {@code null} + public: + ParseTreeMatch(ParseTree *tree, ParseTreePattern *pattern, misc::MultiMap *labels, ParseTree *mismatchedNode); + + /// + /// Get the last node associated with a specific {@code label}. + ///

+ /// For example, for pattern {@code }, {@code get("id")} returns the + /// node matched for that {@code ID}. If more than one node + /// matched the specified label, only the last is returned. If there is + /// no node associated with the label, this returns {@code null}. + ///

+ /// Pattern tags like {@code } and {@code } without labels are + /// considered to be labeled with {@code ID} and {@code expr}, respectively. + ///

+ /// The label to check. + /// + /// The last to match a tag with the specified + /// label, or {@code null} if no parse tree matched a tag with the label. + virtual ParseTree *get(const std::wstring &label); + + /// + /// Return all nodes matching a rule or token tag with the specified label. + ///

+ /// If the {@code label} is the name of a parser rule or token in the + /// grammar, the resulting list will contain both the parse trees matching + /// rule or tags explicitly labeled with the label and the complete set of + /// parse trees matching the labeled and unlabeled tags in the pattern for + /// the parser rule or token. For example, if {@code label} is {@code "foo"}, + /// the result will contain all of the following. + /// + ///

    + ///
  • Parse tree nodes matching tags of the form {@code } and + /// {@code }.
  • + ///
  • Parse tree nodes matching tags of the form {@code }.
  • + ///
  • Parse tree nodes matching tags of the form {@code }.
  • + ///
+ ///
+ /// The label. + /// + /// A collection of all nodes matching tags with + /// the specified {@code label}. If no nodes matched the label, an empty list + /// is returned. + virtual std::vector getAll(const std::wstring &label); + + /// + /// Return a mapping from label → [list of nodes]. + ///

+ /// The map includes special entries corresponding to the names of rules and + /// tokens referenced in tags in the original pattern. For additional + /// information, see the description of . + ///

+ /// A mapping from labels to parse tree nodes. If the parse tree + /// pattern did not contain any rule or token tags, this map will be empty. + virtual misc::MultiMap *getLabels(); + + /// + /// Get the node at which we first detected a mismatch. + /// + /// the node at which we first detected a mismatch, or {@code null} + /// if the match was successful. + virtual ParseTree *getMismatchedNode(); + + /// + /// Gets a value indicating whether the match operation succeeded. + /// + /// {@code true} if the match operation succeeded; otherwise, + /// {@code false}. + virtual bool succeeded(); + + /// + /// Get the tree pattern we are matching against. + /// + /// The tree pattern we are matching against. + virtual ParseTreePattern *getPattern(); + + /// + /// Get the parse tree we are trying to match to a pattern. + /// + /// The we are trying to match to a pattern. + virtual ParseTree *getTree(); + + /// + /// {@inheritDoc} + /// + virtual std::wstring toString(); + }; + + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/ParseTreePattern.cpp b/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/ParseTreePattern.cpp new file mode 100755 index 000000000..c44c82251 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/ParseTreePattern.cpp @@ -0,0 +1,89 @@ +#include "ParseTreePattern.h" +#include "XPath.h" +#include "ParseTreePatternMatcher.h" +#include "ParseTreeMatch.h" +#include "ParseTree.h" +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Sam Harwell + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + namespace pattern { + + ParseTreePattern::ParseTreePattern(ParseTreePatternMatcher *matcher, const std::wstring &pattern, int patternRuleIndex, ParseTree *patternTree) : patternRuleIndex(patternRuleIndex), pattern(pattern), patternTree(patternTree), matcher(matcher) { + } + + tree::pattern::ParseTreeMatch *ParseTreePattern::match(ParseTree *tree) { + return matcher->match(tree, this); + } + + bool ParseTreePattern::matches(ParseTree *tree) { + return matcher->match(tree, this)->succeeded(); + } + +// TODO: Come back to this after the base runtime works. +#if 0 + std::vector ParseTreePattern::findAll(ParseTree *tree, const std::wstring &xpath) { + std::vector *subtrees = xpath::XPath::findAll(tree, xpath, matcher->getParser()); + std::vector matches = std::vector(); + for (auto t : *subtrees) { + ParseTreeMatch *aMatch = match(t); + if (aMatch->succeeded()) { + matches.push_back(aMatch); + } + } + return matches; + } +#endif + + tree::pattern::ParseTreePatternMatcher *ParseTreePattern::getMatcher() { + return matcher; + } + + std::wstring ParseTreePattern::getPattern() { + return pattern; + } + + int ParseTreePattern::getPatternRuleIndex() { + return patternRuleIndex; + } + + tree::ParseTree *ParseTreePattern::getPatternTree() { + return patternTree; + } + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/ParseTreePattern.h b/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/ParseTreePattern.h new file mode 100755 index 000000000..43f02bebe --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/ParseTreePattern.h @@ -0,0 +1,150 @@ +#pragma once + +#include +#include + +#include "Declarations.h" + + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Sam Harwell + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + namespace pattern { + /// + /// A pattern like {@code = ;} converted to a by + /// . + /// + class ParseTreePattern { + /// + /// This is the backing field for . + /// + private: + const int patternRuleIndex; + + /// + /// This is the backing field for . + /// + const std::wstring pattern; + + /// + /// This is the backing field for . + /// + ParseTree *const patternTree; + + /// + /// This is the backing field for . + /// + ParseTreePatternMatcher *const matcher; + + /// + /// Construct a new instance of the class. + /// + /// The which created this + /// tree pattern. + /// The tree pattern in concrete syntax form. + /// The parser rule which serves as the root of the + /// tree pattern. + /// The tree pattern in form. + public: + ParseTreePattern(ParseTreePatternMatcher *matcher, const std::wstring &pattern, int patternRuleIndex, ParseTree *patternTree); + + /// + /// Match a specific parse tree against this tree pattern. + /// + /// The parse tree to match against this tree pattern. + /// A object describing the result of the + /// match operation. The method can be + /// used to determine whether or not the match was successful. + virtual ParseTreeMatch *match(ParseTree *tree); + + /// + /// Determine whether or not a parse tree matches this tree pattern. + /// + /// The parse tree to match against this tree pattern. + /// {@code true} if {@code tree} is a match for the current tree + /// pattern; otherwise, {@code false}. + virtual bool matches(ParseTree *tree); + +// TODO: Come back to this after the base runtime works. +#if 0 + /// + /// Find all nodes using XPath and then try to match those subtrees against + /// this tree pattern. + /// + /// The to match against this pattern. + /// An expression matching the nodes + /// + /// A collection of objects describing the + /// successful matches. Unsuccessful matches are omitted from the result, + /// regardless of the reason for the failure. + virtual std::vector findAll(ParseTree *tree, const std::wstring &xpath); +#endif + + /// + /// Get the which created this tree pattern. + /// + /// The which created this tree + /// pattern. + virtual ParseTreePatternMatcher *getMatcher(); + + /// + /// Get the tree pattern in concrete syntax form. + /// + /// The tree pattern in concrete syntax form. + virtual std::wstring getPattern(); + + /// + /// Get the parser rule which serves as the outermost rule for the tree + /// pattern. + /// + /// The parser rule which serves as the outermost rule for the tree + /// pattern. + virtual int getPatternRuleIndex(); + + /// + /// Get the tree pattern as a . The rule and token tags from + /// the pattern are present in the parse tree as terminal nodes with a symbol + /// of type or . + /// + /// The tree pattern as a . + virtual ParseTree *getPatternTree(); + }; + + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/ParseTreePatternMatcher.cpp b/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/ParseTreePatternMatcher.cpp new file mode 100755 index 000000000..8b40e1d8f --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/ParseTreePatternMatcher.cpp @@ -0,0 +1,381 @@ +#include "ParseTreePatternMatcher.h" + +#include + +#include "Exceptions.h" +#include "MultiMap.h" +#include "ParseTreePattern.h" +#include "ParseTreeMatch.h" +#include "TerminalNode.h" +#include "Token.h" +#include "CommonTokenStream.h" +#include "Parser.h" +#include "ParserInterpreter.h" +#include "TokenTagToken.h" +#include "ParserRuleContext.h" +#include "RuleTagToken.h" +#include "TagChunk.h" +#include "ATN.h" +#include "Lexer.h" + +#include "ListTokenSource.h" +#include "TextChunk.h" +#include "ANTLRInputStream.h" +#include "Arrays.h" + + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + namespace pattern { + ParseTreePatternMatcher::CannotInvokeStartRule::CannotInvokeStartRule(std::exception e) { + } + + ParseTreePatternMatcher::ParseTreePatternMatcher(Lexer *lexer, Parser *parser) : lexer(lexer), parser(parser) { + InitializeInstanceFields(); + } + + void ParseTreePatternMatcher::setDelimiters(const std::wstring &start, const std::wstring &stop, const std::wstring &escapeLeft) { + if (start == L"" || start.length() == 0) { + throw new IllegalArgumentException(L"start cannot be null or empty"); + } + + if (stop == L"" || stop.length() == 0) { + throw new IllegalArgumentException(L"stop cannot be null or empty"); + } + + this->start = start; + this->stop = stop; + this->escape = escapeLeft; + } + + bool ParseTreePatternMatcher::matches(ParseTree *tree, const std::wstring &pattern, int patternRuleIndex) { + ParseTreePattern *p = compile(pattern, patternRuleIndex); + return matches(tree, p); + } + + bool ParseTreePatternMatcher::matches(ParseTree *tree, ParseTreePattern *pattern) { + misc::MultiMap *labels = new misc::MultiMap(); + ParseTree *mismatchedNode = matchImpl(tree, pattern->getPatternTree(), labels); + return mismatchedNode == nullptr; + } + + ParseTreeMatch *ParseTreePatternMatcher::match(ParseTree *tree, const std::wstring &pattern, int patternRuleIndex) { + ParseTreePattern *p = compile(pattern, patternRuleIndex); + return match(tree, p); + } + + ParseTreeMatch *ParseTreePatternMatcher::match(ParseTree *tree, ParseTreePattern *pattern) { + misc::MultiMap *labels = new misc::MultiMap(); + ParseTree *mismatchedNode = matchImpl(tree, pattern->getPatternTree(), labels); + return new ParseTreeMatch(tree, pattern, labels, mismatchedNode); + } + + ParseTreePattern *ParseTreePatternMatcher::compile(const std::wstring &pattern, int patternRuleIndex) { + std::vector tokenList = tokenize(pattern); + ListTokenSource *tokenSrc = new ListTokenSource(tokenList); + CommonTokenStream *tokens = new CommonTokenStream(tokenSrc); + + ParserInterpreter *parserInterp = new ParserInterpreter( + parser->getGrammarFileName(), parser->getTokenNames(), + parser->getRuleNames(), parser->getATNWithBypassAlts(), tokens); + + ParseTree *tree = nullptr; + try { + tree = parserInterp->parse(patternRuleIndex); + // System.out.println("pattern tree = "+tree.toStringTree(parserInterp)); + } catch (std::exception &e) { + throw CannotInvokeStartRule(e); + } + + return new ParseTreePattern(this, pattern, patternRuleIndex, tree); + } + + runtime::Lexer *ParseTreePatternMatcher::getLexer() { + return lexer; + } + + org::antlr::v4::runtime::Parser *ParseTreePatternMatcher::getParser() { + return parser; + } + + tree::ParseTree *ParseTreePatternMatcher::matchImpl(ParseTree *tree, ParseTree *patternTree, misc::MultiMap *labels) { + if (tree == nullptr) { + throw new IllegalArgumentException(L"tree cannot be null"); + } + + if (patternTree == nullptr) { + throw IllegalArgumentException(L"patternTree cannot be null"); + } + + // x and , x and y, or x and x; or could be mismatched types + if (dynamic_cast(tree) != nullptr && dynamic_cast(patternTree) != nullptr) { + TerminalNode *t1 = static_cast(tree); + TerminalNode *t2 = static_cast(patternTree); + ParseTree *mismatchedNode = nullptr; + // both are tokens and they have same type + if (t1->getSymbol()->getType() == t2->getSymbol()->getType()) { + if (dynamic_cast(t2->getSymbol()) != nullptr) { // x and + TokenTagToken *tokenTagToken = static_cast(t2->getSymbol()); + // track label->list-of-nodes for both token name and label (if any) + labels->map(tokenTagToken->getTokenName(), tree); + if (tokenTagToken->getLabel() != L"") { + labels->map(tokenTagToken->getLabel(), tree); + } + } else if (t1->getText() == t2->getText()) { + // x and x + } else { + // x and y + if (mismatchedNode == nullptr) { + mismatchedNode = t1; + } + } + } else { + if (mismatchedNode == nullptr) { + mismatchedNode = t1; + } + } + + return mismatchedNode; + } + + if (dynamic_cast(tree) != nullptr && dynamic_cast(patternTree) != nullptr) { + ParserRuleContext *r1 = static_cast(tree); + ParserRuleContext *r2 = static_cast(patternTree); + ParseTree *mismatchedNode = nullptr; + // (expr ...) and + RuleTagToken *ruleTagToken = getRuleTagToken(r2); + if (ruleTagToken != nullptr) { + //ParseTreeMatch *m = nullptr; // unused? + if (r1->RuleContext::getRuleContext()->getRuleIndex() == r2->RuleContext::getRuleContext()->getRuleIndex()) { + // track label->list-of-nodes for both rule name and label (if any) + labels->map(ruleTagToken->getRuleName(), tree); + if (ruleTagToken->getLabel() != L"") { + labels->map(ruleTagToken->getLabel(), tree); + } + } else { + if (mismatchedNode == nullptr) { + mismatchedNode = r1; + } + } + + return mismatchedNode; + } + + // (expr ...) and (expr ...) + if (r1->getChildCount() != r2->getChildCount()) { + if (mismatchedNode == nullptr) { + mismatchedNode = r1; + } + + return mismatchedNode; + } + + int n = r1->getChildCount(); + for (int i = 0; i < n; i++) { + ParseTree *childMatch = matchImpl(r1->getChild(i), patternTree->getChild(i), labels); + if (childMatch != nullptr) { + return childMatch; + } + } + + return mismatchedNode; + } + + // if nodes aren't both tokens or both rule nodes, can't match + return tree; + } + + RuleTagToken *ParseTreePatternMatcher::getRuleTagToken(ParseTree *t) { + if (dynamic_cast(t) != nullptr) { + RuleNode *r = static_cast(t); + if (r->getChildCount() == 1 && dynamic_cast(r->getChild(0)) != nullptr) { + TerminalNode *c = static_cast(r->getChild(0)); + if (dynamic_cast(c->getSymbol()) != nullptr) { + // System.out.println("rule tag subtree "+t.toStringTree(parser)); + return static_cast(c->getSymbol()); + } + } + } + return nullptr; + } + + std::vector ParseTreePatternMatcher::tokenize(const std::wstring &pattern) { + // split pattern into chunks: sea (raw input) and islands (, ) + std::vector chunks = split(pattern); + + // create token stream from text and tags + std::vector tokens; + for (auto chunk : chunks) { + if (dynamic_cast(chunk) != nullptr) { + TagChunk *tagChunk = static_cast(chunk); + // add special rule token or conjure up new token from name + if (isupper(tagChunk->getTag()[0])) { + int ttype = parser->getTokenType(tagChunk->getTag()); + if (ttype == Token::INVALID_TYPE) { + throw IllegalArgumentException(std::wstring(L"Unknown token ") + tagChunk->getTag() + std::wstring(L" in pattern: ") + pattern); + } + TokenTagToken *t = new TokenTagToken(tagChunk->getTag(), ttype, tagChunk->getLabel()); + tokens.push_back(t); + } else if (islower(tagChunk->getTag()[0])) { + int ruleIndex = parser->getRuleIndex(tagChunk->getTag()); + if (ruleIndex == -1) { + throw IllegalArgumentException(std::wstring(L"Unknown rule ") + tagChunk->getTag() + std::wstring(L" in pattern: ") + pattern); + } + int ruleImaginaryTokenType = parser->getATNWithBypassAlts()->ruleToTokenType[ruleIndex]; + tokens.push_back(new RuleTagToken(tagChunk->getTag(), ruleImaginaryTokenType, tagChunk->getLabel())); + } else { + throw IllegalArgumentException(std::wstring(L"invalid tag: ") + tagChunk->getTag() + std::wstring(L" in pattern: ") + pattern); + } + } else { + TextChunk *textChunk = static_cast(chunk); + ANTLRInputStream *in_Renamed = new ANTLRInputStream(textChunk->getText()); + lexer->setInputStream(in_Renamed); + Token *t = lexer->nextToken(); + while (t->getType() != Token::_EOF) { + tokens.push_back(t); + t = lexer->nextToken(); + } + } + } + + // System.out.println("tokens="+tokens); + return tokens; + } + + std::vector ParseTreePatternMatcher::split(const std::wstring &pattern) { + size_t p = 0; + size_t n = pattern.length(); + std::vector chunks = std::vector(); + // find all start and stop indexes first, then collect + std::vector starts = std::vector(); + std::vector stops = std::vector(); + while (p < n) { + if (p == pattern.find(escape + start,p)) { + p += escape.length() + start.length(); + } else if (p == pattern.find(escape + stop,p)) { + p += escape.length() + stop.length(); + } else if (p == pattern.find(start,p)) { + starts.push_back((int)p); + p += start.length(); + } else if (p == pattern.find(stop,p)) { + stops.push_back((int)p); + p += stop.length(); + } else { + p++; + } + } + + // System.out.println(""); + // System.out.println(starts); + // System.out.println(stops); + if (starts.size() > stops.size()) { + throw IllegalArgumentException(std::wstring(L"unterminated tag in pattern: ") + pattern); + } + + if (starts.size() < stops.size()) { + throw IllegalArgumentException(std::wstring(L"missing start tag in pattern: ") + pattern); + } + + size_t ntags = starts.size(); + for (size_t i = 0; i < ntags; i++) { + if (starts[i] >= stops[i]) { + throw IllegalArgumentException(std::wstring(L"tag delimiters out of order in pattern: ") + pattern); + } + } + + // collect into chunks now + if (ntags == 0) { + std::wstring text = pattern.substr(0, n); + chunks.push_back(new TextChunk(text)); + } + + if (ntags > 0 && starts[0] > 0) { // copy text up to first tag into chunks + std::wstring text = pattern.substr(0, starts[0]); + chunks.push_back(new TextChunk(text)); + } + for (size_t i = 0; i < ntags; i++) { + // copy inside of + std::wstring tag = pattern.substr(starts[i] + start.length(), stops[i] - (starts[i] + start.length())); + std::wstring ruleOrToken = tag; + std::wstring label = L""; + size_t colon = tag.find(L':'); + if (colon != std::wstring::npos) { + label = tag.substr(0,colon); + ruleOrToken = tag.substr(colon + 1, tag.length() - (colon + 1)); + } + chunks.push_back(new TagChunk(label, ruleOrToken)); + if (i + 1 < ntags) { + // copy from end of to start of next + std::wstring text = pattern.substr(stops[i] + stop.length(), starts[i + 1] - (stops[i] + stop.length())); + chunks.push_back(new TextChunk(text)); + } + } + if (ntags > 0) { + size_t afterLastTag = stops[ntags - 1] + stop.length(); + if (afterLastTag < n) { // copy text from end of last tag to end + std::wstring text = pattern.substr(afterLastTag, n - afterLastTag); + chunks.push_back(new TextChunk(text)); + } + } + + // strip out all backslashes from text chunks but not tags + for (size_t i = 0; i < chunks.size(); i++) { + Chunk *c = chunks[i]; + if (dynamic_cast(c) != nullptr) { + TextChunk *tc = static_cast(c); + std::wstring unescaped = tc->getText(); + unescaped.erase(std::remove(unescaped.begin(), unescaped.end(), L'\\'), unescaped.end()); + if (unescaped.length() < tc->getText().length()) { + chunks[i] = new TextChunk(unescaped); + } + } + } + + return chunks; + } + + void ParseTreePatternMatcher::InitializeInstanceFields() { + start = L"<"; + stop = L">"; + escape = L"\\"; + } + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/ParseTreePatternMatcher.h b/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/ParseTreePatternMatcher.h new file mode 100755 index 000000000..9ce16c4a4 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/ParseTreePatternMatcher.h @@ -0,0 +1,227 @@ +#pragma once + +#include "Declarations.h" +#include "MultiMap.h" +#include "Token.h" + +#include +#include +#include +#include + + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + namespace pattern { + + /// + /// A tree pattern matching mechanism for ANTLR s. + ///

+ /// Patterns are strings of source input text with special tags representing + /// token or rule references such as: + ///

+ /// {@code = ;} + ///

+ /// Given a pattern start rule such as {@code statement}, this object constructs + /// a with placeholders for the {@code ID} and {@code expr} + /// subtree. Then the routines can compare an actual + /// from a parse with this pattern. Tag {@code } matches + /// any {@code ID} token and tag {@code } references the result of the + /// {@code expr} rule (generally an instance of {@code ExprContext}. + ///

+ /// Pattern {@code x = 0;} is a similar pattern that matches the same pattern + /// except that it requires the identifier to be {@code x} and the expression to + /// be {@code 0}. + ///

+ /// The routines return {@code true} or {@code false} based + /// upon a match for the tree rooted at the parameter sent in. The + /// routines return a object that + /// contains the parse tree, the parse tree pattern, and a map from tag name to + /// matched nodes (more below). A subtree that fails to match, returns with + /// set to the first tree node that did not + /// match. + ///

+ /// For efficiency, you can compile a tree pattern in string form to a + /// object. + ///

+ /// See {@code TestParseTreeMatcher} for lots of examples. + /// has two static helper methods: + /// and that + /// are easy to use but not super efficient because they create new + /// objects each time and have to compile the + /// pattern in string form before using it. + ///

+ /// The lexer and parser that you pass into the + /// constructor are used to parse the pattern in string form. The lexer converts + /// the {@code = ;} into a sequence of four tokens (assuming lexer + /// throws out whitespace or puts it on a hidden channel). Be aware that the + /// input stream is reset for the lexer (but not the parser; a + /// is created to parse the input.). Any user-defined + /// fields you have put into the lexer might get changed when this mechanism asks + /// it to scan the pattern string. + ///

+ /// Normally a parser does not accept token {@code } as a valid + /// {@code expr} but, from the parser passed in, we create a special version of + /// the underlying grammar representation (an ) that allows imaginary + /// tokens representing rules ({@code }) to match entire rules. We call + /// these bypass alternatives. + ///

+ /// Delimiters are {@code <} and {@code >}, with {@code \} as the escape string + /// by default, but you can set them to whatever you want using + /// . You must escape both start and stop strings + /// {@code \<} and {@code \>}. + ///

+ class ParseTreePatternMatcher { + public: + class CannotInvokeStartRule : public std::exception { + public: + CannotInvokeStartRule(std::exception e); + }; + + /// + /// This is the backing field for . + /// + private: + Lexer *const lexer; + + /// + /// This is the backing field for . + /// + Parser *const parser; + + protected: + std::wstring start; + std::wstring stop; + std::wstring escape; // e.g., \< and \> must escape BOTH! + + /// + /// Constructs a or from a and + /// object. The lexer input stream is altered for tokenizing + /// the tree patterns. The parser is used as a convenient mechanism to get + /// the grammar name, plus token, rule names. + /// + public: + ParseTreePatternMatcher(Lexer *lexer, Parser *parser); + + /// + /// Set the delimiters used for marking rule and token tags within concrete + /// syntax used by the tree pattern parser. + /// + /// The start delimiter. + /// The stop delimiter. + /// The escape sequence to use for escaping a start or stop delimiter. + /// + /// if {@code start} is {@code null} or empty. + /// if {@code stop} is {@code null} or empty. + virtual void setDelimiters(const std::wstring &start, const std::wstring &stop, const std::wstring &escapeLeft); + + /// + /// Does {@code pattern} matched as rule {@code patternRuleIndex} match {@code tree}? + virtual bool matches(ParseTree *tree, const std::wstring &pattern, int patternRuleIndex); + + /// + /// Does {@code pattern} matched as rule patternRuleIndex match tree? Pass in a + /// compiled pattern instead of a string representation of a tree pattern. + /// + virtual bool matches(ParseTree *tree, ParseTreePattern *pattern); + + /// + /// Compare {@code pattern} matched as rule {@code patternRuleIndex} against + /// {@code tree} and return a object that contains the + /// matched elements, or the node at which the match failed. + /// + virtual ParseTreeMatch *match(ParseTree *tree, const std::wstring &pattern, int patternRuleIndex); + + /// + /// Compare {@code pattern} matched against {@code tree} and return a + /// object that contains the matched elements, or the + /// node at which the match failed. Pass in a compiled pattern instead of a + /// string representation of a tree pattern. + /// + virtual ParseTreeMatch *match(ParseTree *tree, ParseTreePattern *pattern); + + /// + /// For repeated use of a tree pattern, compile it to a + /// using this method. + /// + virtual ParseTreePattern *compile(const std::wstring &pattern, int patternRuleIndex); + + /// + /// Used to convert the tree pattern string into a series of tokens. The + /// input stream is reset. + /// + virtual Lexer *getLexer(); + + /// + /// Used to collect to the grammar file name, token names, rule names for + /// used to parse the pattern into a parse tree. + /// + virtual Parser *getParser(); + + // ---- SUPPORT CODE ---- + + /// + /// Recursively walk {@code tree} against {@code patternTree}, filling + /// {@code match.}. + /// + /// the first node encountered in {@code tree} which does not match + /// a corresponding node in {@code patternTree}, or {@code null} if the match + /// was successful. The specific node returned depends on the matching + /// algorithm used by the implementation, and may be overridden. + protected: + // I don't know why this is failing to compile + virtual ParseTree *matchImpl(ParseTree *tree, ParseTree *patternTree, misc::MultiMap *labels); + /// + /// Is {@code t} {@code (expr )} subtree? + virtual RuleTagToken *getRuleTagToken(ParseTree *t); + + public: + virtual std::vector tokenize(const std::wstring &pattern); + + /// + /// Split {@code = ;} into 4 chunks for tokenizing by . + virtual std::vector split(const std::wstring &pattern); + + private: + void InitializeInstanceFields(); + }; + + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/RuleTagToken.cpp b/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/RuleTagToken.cpp new file mode 100755 index 000000000..6c3afa62f --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/RuleTagToken.cpp @@ -0,0 +1,113 @@ +#include "RuleTagToken.h" +#include "TokenSource.h" +#include "CharStream.h" +#include "Exceptions.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + namespace pattern { + + RuleTagToken::RuleTagToken(const std::wstring &ruleName, int _bypassTokenType) : bypassTokenType(_bypassTokenType) { + } + + RuleTagToken::RuleTagToken(const std::wstring &ruleName, int bypassTokenType, const std::wstring &label) : ruleName(ruleName), bypassTokenType(bypassTokenType), label(label) { + if (ruleName == L"" || ruleName.length() == 0) { + throw IllegalArgumentException(L"ruleName cannot be null or empty."); + } + + } + + std::wstring RuleTagToken::getRuleName() { + return ruleName; + } + + std::wstring RuleTagToken::getLabel() { + return label; + } + + int RuleTagToken::getChannel() { + return DEFAULT_CHANNEL; + } + + std::wstring RuleTagToken::getText() { + if (label != L"") { + return std::wstring(L"<") + label + std::wstring(L":") + ruleName + std::wstring(L">"); + } + + return std::wstring(L"<") + ruleName + std::wstring(L">"); + } + + int RuleTagToken::getType() { + return bypassTokenType; + } + + int RuleTagToken::getLine() { + return 0; + } + + int RuleTagToken::getCharPositionInLine() { + return -1; + } + + int RuleTagToken::getTokenIndex() { + return -1; + } + + int RuleTagToken::getStartIndex() { + return -1; + } + + int RuleTagToken::getStopIndex() { + return -1; + } + + org::antlr::v4::runtime::TokenSource *RuleTagToken::getTokenSource() { + return nullptr; + } + + org::antlr::v4::runtime::CharStream *RuleTagToken::getInputStream() { + return nullptr; + } + + std::wstring RuleTagToken::toString() { + return ruleName + std::wstring(L":") + std::to_wstring(bypassTokenType); + } + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/RuleTagToken.h b/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/RuleTagToken.h new file mode 100755 index 000000000..617daad1b --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/RuleTagToken.h @@ -0,0 +1,191 @@ +#pragma once + +#include +#include "Token.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + namespace pattern { + + + + /// + /// A object representing an entire subtree matched by a parser + /// rule; e.g., {@code }. These tokens are created for + /// chunks where the tag corresponds to a parser rule. + /// + class RuleTagToken : public Token { + /// + /// This is the backing field for . + /// + private: + const std::wstring ruleName; + /// + /// The token type for the current token. This is the token type assigned to + /// the bypass alternative for the rule during ATN deserialization. + /// + const int bypassTokenType; + /// + /// This is the backing field for . + /// + const std::wstring label; + + /// + /// Constructs a new instance of with the specified rule + /// name and bypass token type and no label. + /// + /// The name of the parser rule this rule tag matches. + /// The bypass token type assigned to the parser rule. + /// + /// if {@code ruleName} is {@code null} + /// or empty. + public: + + RuleTagToken(const std::wstring &ruleName, int bypassTokenType); //this(ruleName, bypassTokenType, nullptr); + + /// + /// Constructs a new instance of with the specified rule + /// name, bypass token type, and label. + /// + /// The name of the parser rule this rule tag matches. + /// The bypass token type assigned to the parser rule. + /// The label associated with the rule tag, or {@code null} if + /// the rule tag is unlabeled. + /// + /// if {@code ruleName} is {@code null} + /// or empty. + RuleTagToken(const std::wstring &ruleName, int bypassTokenType, const std::wstring &label); + + /// + /// Gets the name of the rule associated with this rule tag. + /// + /// The name of the parser rule associated with this rule tag. + std::wstring getRuleName(); + + /// + /// Gets the label associated with the rule tag. + /// + /// The name of the label associated with the rule tag, or + /// {@code null} if this is an unlabeled rule tag. + std::wstring getLabel(); + + /// + /// {@inheritDoc} + ///

+ /// Rule tag tokens are always placed on the . + ///

+ virtual int getChannel() override; + + /// + /// {@inheritDoc} + ///

+ /// This method returns the rule tag formatted with {@code <} and {@code >} + /// delimiters. + ///

+ virtual std::wstring getText() override; + + /// + /// {@inheritDoc} + ///

+ /// Rule tag tokens have types assigned according to the rule bypass + /// transitions created during ATN deserialization. + ///

+ virtual int getType() override; + + /// + /// {@inheritDoc} + ///

+ /// The implementation for always returns 0. + ///

+ virtual int getLine() override; + + /// + /// {@inheritDoc} + ///

+ /// The implementation for always returns -1. + ///

+ virtual int getCharPositionInLine() override; + + /// + /// {@inheritDoc} + ///

+ /// The implementation for always returns -1. + ///

+ virtual int getTokenIndex() override; + + /// + /// {@inheritDoc} + ///

+ /// The implementation for always returns -1. + ///

+ virtual int getStartIndex() override; + + /// + /// {@inheritDoc} + ///

+ /// The implementation for always returns -1. + ///

+ virtual int getStopIndex() override; + + /// + /// {@inheritDoc} + ///

+ /// The implementation for always returns {@code null}. + ///

+ virtual TokenSource *getTokenSource() override; + + /// + /// {@inheritDoc} + ///

+ /// The implementation for always returns {@code null}. + ///

+ virtual CharStream *getInputStream() override; + + /// + /// {@inheritDoc} + ///

+ /// The implementation for returns a string of the form + /// {@code ruleName:bypassTokenType}. + ///

+ virtual std::wstring toString(); + }; + + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/TagChunk.cpp b/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/TagChunk.cpp new file mode 100755 index 000000000..905628692 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/TagChunk.cpp @@ -0,0 +1,71 @@ +#include "TagChunk.h" +#include "Exceptions.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Sam Harwell + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + namespace pattern { + + TagChunk::TagChunk(const std::wstring &tag) { + } + + TagChunk::TagChunk(const std::wstring &label, const std::wstring &tag) : tag(tag), label(label) { + if (tag == L"" || tag.length() == 0) { + throw IllegalArgumentException(L"tag cannot be null or empty"); + } + + } + + std::wstring TagChunk::getTag() { + return tag; + } + + std::wstring TagChunk::getLabel() { + return label; + } + + std::wstring TagChunk::toString() { + if (label != L"") { + return label + std::wstring(L":") + tag; + } + + return tag; + } + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/TagChunk.h b/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/TagChunk.h new file mode 100755 index 000000000..c5834858d --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/TagChunk.h @@ -0,0 +1,119 @@ +#pragma once + +#include "Chunk.h" +#include + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Sam Harwell + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + namespace pattern { + + /// + /// Represents a placeholder tag in a tree pattern. A tag can have any of the + /// following forms. + /// + ///
    + ///
  • {@code expr}: An unlabeled placeholder for a parser rule {@code expr}.
  • + ///
  • {@code ID}: An unlabeled placeholder for a token of type {@code ID}.
  • + ///
  • {@code e:expr}: A labeled placeholder for a parser rule {@code expr}.
  • + ///
  • {@code id:ID}: A labeled placeholder for a token of type {@code ID}.
  • + ///
+ /// + /// This class does not perform any validation on the tag or label names aside + /// from ensuring that the tag is a non-null, non-empty string. + ///
+ class TagChunk : public Chunk { + /// + /// This is the backing field for . + /// + private: + const std::wstring tag; + /// + /// This is the backing field for . + /// + const std::wstring label; + + /// + /// Construct a new instance of using the specified tag and + /// no label. + /// + /// The tag, which should be the name of a parser rule or token + /// type. + /// + /// if {@code tag} is {@code null} or + /// empty. + public: + TagChunk(const std::wstring &tag); //this(nullptr, tag); + + /// + /// Construct a new instance of using the specified label + /// and tag. + /// + /// The label for the tag. If this is {@code null}, the + /// represents an unlabeled tag. + /// The tag, which should be the name of a parser rule or token + /// type. + /// + /// if {@code tag} is {@code null} or + /// empty. + TagChunk(const std::wstring &label, const std::wstring &tag); + + /// + /// Get the tag for this chunk. + /// + /// The tag for the chunk. + std::wstring getTag(); + + /// + /// Get the label, if any, assigned to this chunk. + /// + /// The label assigned to this chunk, or {@code null} if no label is + /// assigned to the chunk. + std::wstring getLabel(); + + /// + /// This method returns a text representation of the tag chunk. Labeled tags + /// are returned in the form {@code label:tag}, and unlabeled tags are + /// returned as just the tag name. + /// + virtual std::wstring toString(); + }; + + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/TextChunk.cpp b/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/TextChunk.cpp new file mode 100755 index 000000000..df6f6d7a1 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/TextChunk.cpp @@ -0,0 +1,61 @@ +#include "TextChunk.h" +#include "Declarations.h" +#include "Exceptions.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Sam Harwell + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + namespace pattern { + + TextChunk::TextChunk(const std::wstring &text) : text(text) { + if (text == L"") { + throw IllegalArgumentException(L"text cannot be null"); + } + + } + + std::wstring TextChunk::getText() { + return text; + } + + std::wstring TextChunk::toString() { + return std::wstring(L"'") + text + std::wstring(L"'"); + } + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/TextChunk.h b/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/TextChunk.h new file mode 100755 index 000000000..d40234479 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/TextChunk.h @@ -0,0 +1,83 @@ +#pragma once + +#include "Chunk.h" +#include + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Sam Harwell + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + namespace pattern { + + + /// + /// Represents a span of raw text (concrete syntax) between tags in a tree + /// pattern string. + /// + class TextChunk : public Chunk { + /// + /// This is the backing field for . + /// + private: + const std::wstring text; + + /// + /// Constructs a new instance of with the specified text. + /// + /// The text of this chunk. + /// if {@code text} is {@code null}. + public: + TextChunk(const std::wstring &text); + + /// + /// Gets the raw text of this chunk. + /// + /// The text of the chunk. + std::wstring getText(); + + /// + /// {@inheritDoc} + ///

+ /// The implementation for returns the result of + /// in single quotes. + ///

+ virtual std::wstring toString(); + }; + + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/TokenTagToken.cpp b/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/TokenTagToken.cpp new file mode 100755 index 000000000..caa7b9e84 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/TokenTagToken.cpp @@ -0,0 +1,72 @@ +#include "TokenTagToken.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + namespace pattern { + + TokenTagToken::TokenTagToken(const std::wstring &tokenName, int type) : + CommonToken(type), tokenName(L""), label(L"") { + } + + TokenTagToken::TokenTagToken(const std::wstring &tokenName, int type, const std::wstring &label) : + CommonToken(type), tokenName(tokenName), label(label) { + } + + std::wstring TokenTagToken::getTokenName() { + return tokenName; + } + + std::wstring TokenTagToken::getLabel() { + return label; + } + + std::wstring TokenTagToken::getText() { + if (label != L"") { + return std::wstring(L"<") + label + std::wstring(L":") + tokenName + std::wstring(L">"); + } + + return std::wstring(L"<") + tokenName + std::wstring(L">"); + } + + std::wstring TokenTagToken::toString() { + return tokenName + std::wstring(L":") + std::to_wstring(type); + } + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/TokenTagToken.h b/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/TokenTagToken.h new file mode 100755 index 000000000..0672d1982 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/pattern/TokenTagToken.h @@ -0,0 +1,113 @@ +#pragma once + +#include "CommonToken.h" +#include + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + namespace pattern { + + + /// + /// A object representing a token of a particular type; e.g., + /// {@code }. These tokens are created for chunks where the + /// tag corresponds to a lexer rule or token type. + /// + class TokenTagToken : public CommonToken { + /// + /// This is the backing field for . + /// + private: + const std::wstring tokenName; + /// + /// This is the backing field for . + /// + const std::wstring label; + + /// + /// Constructs a new instance of for an unlabeled tag + /// with the specified token name and type. + /// + /// The token name. + /// The token type. + public: + TokenTagToken(const std::wstring &tokenName, int type); //this(tokenName, type, nullptr); + + /// + /// Constructs a new instance of with the specified + /// token name, type, and label. + /// + /// The token name. + /// The token type. + /// The label associated with the token tag, or {@code null} if + /// the token tag is unlabeled. + TokenTagToken(const std::wstring &tokenName, int type, const std::wstring &label); + + /// + /// Gets the token name. + /// The token name. + std::wstring getTokenName(); + + /// + /// Gets the label associated with the rule tag. + /// + /// The name of the label associated with the rule tag, or + /// {@code null} if this is an unlabeled rule tag. + std::wstring getLabel(); + + /// + /// {@inheritDoc} + ///

+ /// The implementation for returns the token tag + /// formatted with {@code <} and {@code >} delimiters. + ///

+ virtual std::wstring getText() override; + + /// + /// {@inheritDoc} + ///

+ /// The implementation for returns a string of the form + /// {@code tokenName:type}. + ///

+ virtual std::wstring toString() override; + }; + + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPath.cpp b/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPath.cpp new file mode 100755 index 000000000..df4422ab4 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPath.cpp @@ -0,0 +1,211 @@ +#include "XPath.h" +#include "XPathLexerErrorListener.h" +#include "CommonTokenStream.h" +#include "XPathWildcardAnywhereElement.h" +#include "XPathWildcardElement.h" +#include "XPathTokenAnywhereElement.h" +#include "XPathTokenElement.h" +#include "XPathRuleAnywhereElement.h" +#include "XPathRuleElement.h" +#include "ParserRuleContext.h" +#include "Token.h" +#include "Exceptions.h" +#include "Strings.h" +#include "ANTLRInputStream.h" +#include "LexerNoViableAltException.h" +#include "XPathElement.h" +#include "Parser.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +// TODO: Come back to this after the base runtime works. +#if 0 +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + namespace xpath { + + const std::wstring XPath::WILDCARD = L"*"; + const std::wstring XPath::NOT = L"!"; + + XPath::XPath(Parser *parser, const std::wstring &path) { + this->parser = parser; + this->path = path; + elements = split(path); + // System.out.println(Arrays.toString(elements)); + } + + XPathElement *XPath::split(const std::wstring &path) { + ANTLRInputStream *in_Renamed; + try { + in_Renamed = new ANTLRInputStream(new StringReader(path)); + } catch (IOException ioe) { + throw IllegalArgumentException(std::wstring(L"Could not read path: ") + path, ioe); + } + XPathLexer *lexer = new XPathLexerAnonymousInnerClassHelper(this, in_Renamed); + lexer->removeErrorListeners(); + lexer->addErrorListener(new XPathLexerErrorListener()); + CommonTokenStream *tokenStream = new CommonTokenStream(lexer); + try { + tokenStream->fill(); + } catch (LexerNoViableAltException e) { + int pos = lexer->getCharPositionInLine(); + std::wstring msg = std::wstring(L"Invalid tokens or characters at index ") + std::to_wstring(pos) + std::wstring(L" in path '") + path + std::wstring(L"'"); + throw IllegalArgumentException(msg, e); + } + + std::vector tokens = tokenStream->getTokens(); + // System.out.println("path="+path+"=>"+tokens); + std::vector elements = std::vector(); + int n = tokens.size(); + int i = 0; + while (i < n) { + Token *el = tokens[i]; + Token *next = nullptr; + switch (el->getType()) { + case XPathLexer::ROOT : + case XPathLexer::ANYWHERE : + bool anywhere = el->getType() == XPathLexer::ANYWHERE; + i++; + next = tokens[i]; + bool invert = next->getType() == XPathLexer::BANG; + if (invert) { + i++; + next = tokens[i]; + } + XPathElement *pathElement = getXPathElement(next, anywhere); + pathElement->invert = invert; + elements.push_back(pathElement); + i++; + break; + + case XPathLexer::TOKEN_REF : + case XPathLexer::RULE_REF : + case XPathLexer::WILDCARD : + elements.push_back(getXPathElement(el, false)); + i++; + break; + + case Token::_EOF : + goto loopBreak; + + default : + throw IllegalArgumentException(std::wstring(L"Unknowth path element ") + el); + } + loopContinue: + } + loopBreak: + return elements.toArray(new XPathElement[0]); + } + + XPath::XPathLexerAnonymousInnerClassHelper::XPathLexerAnonymousInnerClassHelper(XPath *outerInstance, ANTLRInputStream *in_Renamed) : XPathLexer(in_Renamed) { + this->outerInstance = outerInstance; + } + + void XPath::XPathLexerAnonymousInnerClassHelper::recover(LexerNoViableAltException *e) { + throw e; + } + + org::antlr::v4::runtime::tree::xpath::XPathElement *XPath::getXPathElement(Token *wordToken, bool anywhere) { + if (wordToken->getType() == Token::_EOF) { + throw IllegalArgumentException(L"Missing path element at end of path"); + } + std::wstring word = wordToken->getText(); + int ttype = parser->getTokenType(word); + int ruleIndex = parser->getRuleIndex(word); + switch (wordToken->getType()) { + case XPathLexer::WILDCARD : + return anywhere ? new XPathWildcardAnywhereElement() : new XPathWildcardElement(); + case XPathLexer::TOKEN_REF : + case XPathLexer::STRING : + if (ttype == Token::INVALID_TYPE) { + throw IllegalArgumentException(word + std::wstring(L" at index ") + std::to_wstring(wordToken->getStartIndex()) + std::wstring(L" isn't a valid token name")); + } + return anywhere ? new XPathTokenAnywhereElement(word, ttype) : new XPathTokenElement(word, ttype); + default : + if (ruleIndex == -1) { + throw IllegalArgumentException(word + std::wstring(L" at index ") + std::to_wstring(wordToken->getStartIndex()) + std::wstring(L" isn't a valid rule name")); + } + return anywhere ? new XPathRuleAnywhereElement(word, ruleIndex) : new XPathRuleElement(word, ruleIndex); + } + } + + std::vector *XPath::findAll(ParseTree *tree, const std::wstring &xpath, Parser *parser) { + XPath *p = new XPath(parser, xpath); + return p->evaluate(tree); + } + + set::vector *XPath::evaluate(ParseTree *const t) { + ParserRuleContext *dummyRoot = new ParserRuleContext(); + dummyRoot->children = new ArrayListAnonymousInnerClassHelper(this, t); // don't set t's parent. + + set::vector *work = std::vector(); + work->add(dummyRoot); + + int i = 0; + while (i < elements->length) { + set::vector *next = std::vector(); + for (auto node : work) { + if (node->getChildCount() > 0) { + // only try to match next element if it has children + // e.g., //func/*/stat might have a token node for which + // we can't go looking for stat nodes. +//JAVA TO C++ CONVERTER TODO TASK: Java wildcard generics are not converted to C++: +//ORIGINAL LINE: java.util.Collection matching = elements[i].evaluate(node); + set::vector *matching = elements[i]->evaluate(node); + next->addAll(matching); + } + } + i++; + work = next; + } + + return work; + } + + XPath::ArrayListAnonymousInnerClassHelper::ArrayListAnonymousInnerClassHelper(XPath *outerInstance, ParseTree *t) { + this->outerInstance = outerInstance; + this->t = t; + } + // Dan - this code erronously shows up in the header file, TODO which one is correct? + XPath::ArrayListAnonymousInnerClassHelper::ArrayListAnonymousInnerClassHelper(XPath *outerInstance, ParseTree *t); + { + this->add(t); + } + } + } + } + } + } +} +#endif \ No newline at end of file diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPath.h b/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPath.h new file mode 100755 index 000000000..e09e79f9d --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPath.h @@ -0,0 +1,150 @@ +#pragma once + +#include "XPathElement.h" +#include "Declarations.h" +#include "XPathLexer.h" + +#include +#include +#include "set" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +// TODO: Come back to this after the base runtime works. +#if 0 + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + + namespace tree { + namespace xpath { + + + + /// + /// Represent a subset of XPath XML path syntax for use in identifying nodes in + /// parse trees. + /// + /// Split path into words and separators / and // via ANTLR itself then walk + /// path elements from left to right. At each separator-word pair, find set + /// of nodes. Next stage uses those as work list. + /// + /// The basic interface is ParseTree.findAll(parser, pathString). But that is + /// just shorthand for: + /// + /// XPath p = new XPath(parser, xpath); + /// return p.evaluate(this); + /// + /// See {@code org.antlr.v4.test.TestXPath} for descriptions. In short, this allows + /// operators: + /// + /// / root + /// // anywhere + /// ! invert; this must appear directly after root or anywhere operator + /// + /// and path elements: + /// + /// ID token name + /// 'string' any string literal token from the grammar + /// expr rule name + /// * wildcard matching any node + /// + /// Whitespace is not allowed. + /// + class XPath { + public: + static const std::wstring WILDCARD; // word not operator/separator + static const std::wstring NOT; // word for invert operator + + protected: + std::wstring path; +//JAVA TO C++ CONVERTER WARNING: Since the array size is not known in this declaration, Java to C++ Converter has converted this array to a pointer. You will need to call 'delete[]' where appropriate: +//ORIGINAL LINE: protected XPathElement[] elements; + XPathElement *elements; + Parser *parser; + + public: + XPath(Parser *parser, const std::wstring &path); + + // TODO: check for invalid token/rule names, bad syntax + + virtual XPathElement *split(const std::wstring &path); + + private: + class XPathLexerAnonymousInnerClassHelper : public XPathLexer { + private: + XPath *const outerInstance; + + public: + XPathLexerAnonymousInnerClassHelper(XPath *outerInstance, ANTLRInputStream *in_Renamed); + + virtual void recover(LexerNoViableAltException *e); + }; + + /// + /// Convert word like * or ID or expr to a path element. anywhere is true + /// if // precedes the word. + /// + protected: + virtual XPathElement *getXPathElement(Token *wordToken, bool anywhere); + + + public: + static std::vector *findAll(ParseTree *tree, const std::wstring &xpath, Parser *parser); + + /// + /// Return a list of all nodes starting at t as root that satisfy the path. + /// The root / is relative to the node passed to evaluate(). + /// + virtual std::set *evaluate(ParseTree *const t); + + private: + class ArrayListAnonymousInnerClassHelper : public std::vector { + private: + XPath *const outerInstance; + + ParseTree *t; + + public: + ArrayListAnonymousInnerClassHelper(XPath *outerInstance, ParseTree *t); + }; + }; + + } + } + } + } + } +} + +#endif diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathElement.cpp b/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathElement.cpp new file mode 100755 index 000000000..a49f0b226 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathElement.cpp @@ -0,0 +1,59 @@ +#include "XPathElement.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + namespace xpath { + using org::antlr::v4::runtime::tree::ParseTree; + + XPathElement::XPathElement(const std::wstring &nodeName) { + InitializeInstanceFields(); + this->nodeName = nodeName; + } + + std::wstring XPathElement::toString() { + std::wstring inv = invert ? L"!" : L""; + return std::wstring(L"XPathElement") + std::wstring(L"[") + inv + nodeName + std::wstring(L"]"); + } + + void XPathElement::InitializeInstanceFields() { + invert = false; + } + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathElement.h b/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathElement.h new file mode 100755 index 000000000..41f0e29f2 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathElement.h @@ -0,0 +1,72 @@ +#pragma once + +#include +#include + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + class ParseTree; + + namespace xpath { + class XPathElement { + protected: + std::wstring nodeName; + bool invert; + + /// + /// Construct element like /ID or or ID or "/*" etc... + /// op is null if just node + /// + public: + XPathElement(const std::wstring &nodeName); + + /// + /// Given tree rooted at t return all nodes matched by this path element + virtual std::vector *evaluate(ParseTree *t) = 0; + + virtual std::wstring toString(); + + private: + void InitializeInstanceFields(); + }; + + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathLexer.cpp b/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathLexer.cpp new file mode 100755 index 000000000..2885036e3 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathLexer.cpp @@ -0,0 +1,90 @@ +// Generated from java-escape by ANTLR 4.x +#include "Lexer.h" +#include "CharStream.h" +#include "Token.h" +#include "TokenStream.h" +#include "PredictionContextCache.h" +#include "DFA.h" +#include "ATN.h" + +class XPathLexer : public org::antlr::v4::runtime::Lexer { + static org::antlr::v4::runtime::dfa::DFA _decisionToDFA[]; + org::antlr::v4::runtime::atn::PredictionContextCache *_sharedContextCache = + new org::antlr::v4::runtime::atn::PredictionContextCache(); + int + TOKEN_REF=1, RULE_REF=2, ANYWHERE=3, ROOT=4, WILDCARD=5, BANG=6, ID=7, + STRING=8; + + static std::vector ruleNames = { + "ANYWHERE", "ROOT", "WILDCARD", "BANG", "ID", "NameChar", "NameStartChar", + "STRING" + }; + + + XPathLexer(CharStream input) { + super(input); + _interp = new LexerATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache); + _modeNames.insert(_modeNames.end(),L"DEFAULT_MODE"); + _tokenNames.insert(_tokenNames.end(),L"",L"TOKEN_REF",L"RULE_REF", + L"'//'",L"'/'",L"'*'", + L"'!'",L"ID",L"STRING"); + } + + const std::wstring& getGrammarFileName() { return "XPathLexer.g4"; } + + const std::vector& getTokenNames() { return tokenNames; } + + const std::vector& getRuleNames() { return ruleNames; } + + const std::wstring& getSerializedATN() { return _serializedATN; } + + const std::vector& getModeNames() { return modeNames; } + + @Override + const ATN& getATN() { return _ATN; } + + @Override + void action(RuleContext _localctx, int ruleIndex, int actionIndex) { + switch (ruleIndex) { + case 4: ID_action((RuleContext)_localctx, actionIndex); break; + } + } + private: + void ID_action(RuleContext _localctx, int actionIndex) { + switch (actionIndex) { + case 0: + String text = getText(); + if ( Character.isUpperCase(text.charAt(0)) ) setType(TOKEN_REF); + else setType(RULE_REF); + break; + } + } + public: + + static std::wstring _serializedATN = + "\3\u0f63\ub3d0\u10be\u9b29\u438c\u6c08\uc57f\u1da2\2\n\64\b\1\4\2\t\2"+ + "\4\3\t\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\4\b\t\b\4\t\t\t\3\2\3\2\3\2\3"+ + "\3\3\3\3\4\3\4\3\5\3\5\3\6\3\6\7\6\37\n\6\f\6\16\6\"\13\6\3\6\3\6\3\7"+ + "\3\7\5\7(\n\7\3\b\3\b\3\t\3\t\7\t.\n\t\f\t\16\t\61\13\t\3\t\3\t\3/\2\n"+ + "\3\5\1\5\6\1\7\7\1\t\b\1\13\t\2\r\2\1\17\2\1\21\n\1\3\2\4\7\2\62;aa\u00b9"+ + "\u00b9\u0302\u0371\u2041\u2042\17\2C\\c|\u00c2\u00d8\u00da\u00f8\u00fa"+ + "\u0301\u0372\u037f\u0381\u2001\u200e\u200f\u2072\u2191\u2c02\u2ff1\u3003"+ + "\ud801\uf902\ufdd1\ufdf2\uffff\64\2\3\3\2\2\2\2\5\3\2\2\2\2\7\3\2\2\2"+ + "\2\t\3\2\2\2\2\13\3\2\2\2\2\21\3\2\2\2\3\23\3\2\2\2\5\26\3\2\2\2\7\30"+ + "\3\2\2\2\t\32\3\2\2\2\13\34\3\2\2\2\r\'\3\2\2\2\17)\3\2\2\2\21+\3\2\2"+ + "\2\23\24\7\61\2\2\24\25\7\61\2\2\25\4\3\2\2\2\26\27\7\61\2\2\27\6\3\2"+ + "\2\2\30\31\7,\2\2\31\b\3\2\2\2\32\33\7#\2\2\33\n\3\2\2\2\34 \5\17\b\2"+ + "\35\37\5\r\7\2\36\35\3\2\2\2\37\"\3\2\2\2 \36\3\2\2\2 !\3\2\2\2!#\3\2"+ + "\2\2\" \3\2\2\2#$\b\6\2\2$\f\3\2\2\2%(\5\17\b\2&(\t\2\2\2\'%\3\2\2\2\'"+ + "&\3\2\2\2(\16\3\2\2\2)*\t\3\2\2*\20\3\2\2\2+/\7)\2\2,.\13\2\2\2-,\3\2"+ + "\2\2.\61\3\2\2\2/\60\3\2\2\2/-\3\2\2\2\60\62\3\2\2\2\61/\3\2\2\2\62\63"+ + "\7)\2\2\63\22\3\2\2\2\6\2 \'/"; + static ATN _ATN = + new ATNDeserializer().deserialize(_serializedATN.toCharArray()); + static { + _decisionToDFA = new DFA[_ATN.getNumberOfDecisions()]; + for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) { + _decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i); + } + } +}; diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathLexer.h b/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathLexer.h new file mode 100755 index 000000000..4747eace8 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathLexer.h @@ -0,0 +1,69 @@ +#pragma once + +#include +#include +#include "Lexer.h" +#include "Declarations.h" + +// Generated from XPathLexer.g4 by ANTLR 4.1 +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + +class XPathLexer : public org::antlr::v4::runtime::Lexer { +protected: + //ORIGINAL LINE: protected static final org.antlr.v4.runtime.dfa.DFA[] _decisionToDFA; + static const org::antlr::v4::runtime::dfa::DFA *_decisionToDFA; + static org::antlr::v4::runtime::atn::PredictionContextCache *const _sharedContextCache; +public: + static const int TOKEN_REF = 1, RULE_REF = 2, ANYWHERE = 3, ROOT = 4, WILDCARD = 5, BANG = 6, ID = 7, STRING = 8; + static std::vector _modeNames; + + static const std::vector _tokenNames; + static const std::vector _ruleNames; + + + XPathLexer(org::antlr::v4::runtime::CharStream *input); + + virtual std::wstring getGrammarFileName() override; + virtual const std::vector& getTokenNames() override; + virtual const std::vector& getRuleNames() override; + virtual const std::vector& getModeNames() override; + virtual org::antlr::v4::runtime::atn::ATN *getATN() override; + virtual void action(org::antlr::v4::runtime::RuleContext *_localctx, int ruleIndex, int actionIndex) override; +private: + void ID_action(org::antlr::v4::runtime::RuleContext *_localctx, int actionIndex); + +public: + static const std::wstring _serializedATN; + static org::antlr::v4::runtime::atn::ATN *const _ATN; + + XPathLexer(); +}; diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathLexerErrorListener.cpp b/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathLexerErrorListener.cpp new file mode 100755 index 000000000..de01bd2f1 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathLexerErrorListener.cpp @@ -0,0 +1,47 @@ +#include "XPathLexerErrorListener.h" +/* +* [The "BSD license"] +* Copyright (c) 2013 Terence Parr +* Copyright (c) 2013 Dan McLaughlin +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* 1. Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* 2. Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* 3. The name of the author may not be used to endorse or promote products +* derived from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +// TODO: Come back to this after the base runtime works. +#if 0 +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + namespace xpath { + + } + } + } + } + } +} +#endif \ No newline at end of file diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathLexerErrorListener.h b/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathLexerErrorListener.h new file mode 100755 index 000000000..b90be7de3 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathLexerErrorListener.h @@ -0,0 +1,59 @@ +#pragma once + +#include "BaseErrorListener.h" +#include "Declarations.h" +#include + +/* +* [The "BSD license"] +* Copyright (c) 2013 Terence Parr +* Copyright (c) 2013 Dan McLaughlin +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* 1. Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* 2. Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* 3. The name of the author may not be used to endorse or promote products +* derived from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +// TODO: Come back to this after the base runtime works. +#if 0 +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + namespace xpath { + + class XPathLexerErrorListener : public BaseErrorListener { + public: + // I don't think we require a specific type of recognizer here and can drop the template. If there's a need to have a specific type of recognizer, the caller can simply cast the type to the correct type themselves. + template + virtual void syntaxError(Recognizer *recognizer, void *offendingSymbol, int line, int charPositionInLine, const std::wstring &msg, RecognitionException *e) override {}; + }; + + } + } + } + } + } +} +#endif diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathRuleAnywhereElement.cpp b/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathRuleAnywhereElement.cpp new file mode 100755 index 000000000..3bad0bef1 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathRuleAnywhereElement.cpp @@ -0,0 +1,60 @@ +#include "XPathRuleAnywhereElement.h" +#include "Trees.h" +#include + +/* +* [The "BSD license"] +* Copyright (c) 2013 Terence Parr +* Copyright (c) 2013 Dan McLaughlin +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* 1. Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* 2. Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* 3. The name of the author may not be used to endorse or promote products +* derived from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + namespace xpath { + + + XPathRuleAnywhereElement::XPathRuleAnywhereElement(const std::wstring &ruleName, int ruleIndex) : XPathElement(ruleName) { + InitializeInstanceFields(); + this->ruleIndex = ruleIndex; + } + + std::vector *XPathRuleAnywhereElement::evaluate(ParseTree *t) { + return Trees::findAllRuleNodes(t, ruleIndex); + } + + void XPathRuleAnywhereElement::InitializeInstanceFields() { + ruleIndex = 0; + } + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathRuleAnywhereElement.h b/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathRuleAnywhereElement.h new file mode 100755 index 000000000..43ad70c42 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathRuleAnywhereElement.h @@ -0,0 +1,62 @@ +#pragma once + +#include "XPathElement.h" +#include + +/* +* [The "BSD license"] +* Copyright (c) 2013 Terence Parr +* Copyright (c) 2013 Dan McLaughlin +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* 1. Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* 2. Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* 3. The name of the author may not be used to endorse or promote products +* derived from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + namespace xpath { + + /// + /// Either ID at start of path or ...//ID in middle of path + class XPathRuleAnywhereElement : public XPathElement { + protected: + int ruleIndex; + public: + XPathRuleAnywhereElement(const std::wstring &ruleName, int ruleIndex); + + virtual std::vector *evaluate(ParseTree *t) override; + + private: + void InitializeInstanceFields(); + }; + + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathRuleElement.cpp b/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathRuleElement.cpp new file mode 100755 index 000000000..3a2cce705 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathRuleElement.cpp @@ -0,0 +1,72 @@ +#include "XPathRuleElement.h" +#include "ParseTree.h" +#include "Trees.h" +#include "ParserRuleContext.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + namespace xpath { + + + XPathRuleElement::XPathRuleElement(const std::wstring &ruleName, int ruleIndex) : XPathElement(ruleName) { + InitializeInstanceFields(); + this->ruleIndex = ruleIndex; + } + + std::vector *XPathRuleElement::evaluate(ParseTree *t) { + // return all children of t that match nodeName + std::vector *nodes = new std::vector(); + for (auto c : Trees::getChildren(t)) { + if (dynamic_cast(c) != nullptr) { + ParserRuleContext *ctx = static_cast(c); + if ((((RuleContext*)ctx)->getRuleIndex() == ruleIndex && !invert) || (((RuleContext*)ctx)->getRuleIndex() != ruleIndex && invert)) { + nodes->push_back((ParseTree*)ctx); + + } + } + } + return nodes; + } + + void XPathRuleElement::InitializeInstanceFields() { + ruleIndex = 0; + } + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathRuleElement.h b/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathRuleElement.h new file mode 100755 index 000000000..0421d148c --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathRuleElement.h @@ -0,0 +1,61 @@ +#pragma once + +#include "XPathElement.h" +#include +#include +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + namespace xpath { + + + class XPathRuleElement : public XPathElement { + protected: + int ruleIndex; + public: + XPathRuleElement(const std::wstring &ruleName, int ruleIndex); + + virtual std::vector *evaluate(ParseTree *t) override; + + private: + void InitializeInstanceFields(); + }; + + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathTokenAnywhereElement.cpp b/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathTokenAnywhereElement.cpp new file mode 100755 index 000000000..4f96de9fd --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathTokenAnywhereElement.cpp @@ -0,0 +1,57 @@ +#include "XPathTokenAnywhereElement.h" +#include "Trees.h" + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + namespace xpath { + XPathTokenAnywhereElement::XPathTokenAnywhereElement(const std::wstring &tokenName, int tokenType) : XPathElement(tokenName) { + InitializeInstanceFields(); + this->tokenType = tokenType; + } + + std::vector * XPathTokenAnywhereElement::evaluate(ParseTree *t) { + return Trees::findAllTokenNodes(t, tokenType); + } + + void XPathTokenAnywhereElement::InitializeInstanceFields() { + tokenType = 0; + } + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathTokenAnywhereElement.h b/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathTokenAnywhereElement.h new file mode 100755 index 000000000..898df7139 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathTokenAnywhereElement.h @@ -0,0 +1,65 @@ +#pragma once + +#include "XPathElement.h" +#include +#include + +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Dan McLaughlin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + class ParseTree; + + namespace xpath { + + class XPathTokenAnywhereElement : public XPathElement { + protected: + int tokenType; + public: + XPathTokenAnywhereElement(const std::wstring &tokenName, int tokenType); + + virtual std::vector * evaluate(ParseTree *t) override; + + private: + void InitializeInstanceFields(); + }; + + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathTokenElement.cpp b/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathTokenElement.cpp new file mode 100755 index 000000000..0ad393282 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathTokenElement.cpp @@ -0,0 +1,71 @@ +#include "XPathTokenElement.h" +#include "Trees.h" +#include "TerminalNode.h" +#include "TerminalNode.h" + +/* +* [The "BSD license"] +* Copyright (c) 2013 Terence Parr +* Copyright (c) 2013 Dan McLaughlin +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* 1. Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* 2. Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* 3. The name of the author may not be used to endorse or promote products +* derived from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + namespace xpath { + + + XPathTokenElement::XPathTokenElement(const std::wstring &tokenName, int tokenType) : XPathElement(tokenName) { + InitializeInstanceFields(); + this->tokenType = tokenType; + } + + std::vector *XPathTokenElement::evaluate(ParseTree *t) { + // return all children of t that match nodeName + std::vector *nodes = new std::vector(); + for (auto c : Trees::getChildren(t)) { + if (dynamic_cast(c) != nullptr) { + TerminalNode *tnode = static_cast(c); + if ((tnode->getSymbol()->getType() == tokenType && !invert) || (tnode->getSymbol()->getType() != tokenType && invert)) { + nodes->push_back(tnode); + } + } + } + return nodes; + } + + void XPathTokenElement::InitializeInstanceFields() { + tokenType = 0; + } + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathTokenElement.h b/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathTokenElement.h new file mode 100755 index 000000000..b3d6657a8 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathTokenElement.h @@ -0,0 +1,64 @@ +#pragma once + +#include "XPathElement.h" +#include "Declarations.h" +#include +#include + +/* +* [The "BSD license"] +* Copyright (c) 2013 Terence Parr +* Copyright (c) 2013 Dan McLaughlin +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* 1. Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* 2. Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* 3. The name of the author may not be used to endorse or promote products +* derived from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + namespace xpath { + + + + class XPathTokenElement : public XPathElement { + protected: + int tokenType; + public: + XPathTokenElement(const std::wstring &tokenName, int tokenType); + + virtual std::vector *evaluate(ParseTree *t) override; + + private: + void InitializeInstanceFields(); + }; + + } + } + } + } + } +} diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathWildcardAnywhereElement.cpp b/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathWildcardAnywhereElement.cpp new file mode 100755 index 000000000..50353e360 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathWildcardAnywhereElement.cpp @@ -0,0 +1,59 @@ +#include "XPathWildcardAnywhereElement.h" +#include "Trees.h" +#include "XPath.h" + +/* +* [The "BSD license"] +* Copyright (c) 2013 Terence Parr +* Copyright (c) 2013 Dan McLaughlin +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* 1. Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* 2. Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* 3. The name of the author may not be used to endorse or promote products +* derived from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +// TODO: Come back to this after the base runtime works. +#if 0 +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + namespace xpath { + + XPathWildcardAnywhereElement::XPathWildcardAnywhereElement() : XPathElement(XPath::WILDCARD) { + } + + std::vector *XPathWildcardAnywhereElement::evaluate(ParseTree *t) { + if (invert) { // !* is weird but valid (empty) + return new std::vector(); + } + return Trees::descendants(t); + } + } + } + } + } + } +} +#endif \ No newline at end of file diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathWildcardAnywhereElement.h b/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathWildcardAnywhereElement.h new file mode 100755 index 000000000..e121b15d2 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathWildcardAnywhereElement.h @@ -0,0 +1,59 @@ +#pragma once + +#include "XPathElement.h" +#include "Declarations.h" +#include + +/* +* [The "BSD license"] +* Copyright (c) 2013 Terence Parr +* Copyright (c) 2013 Dan McLaughlin +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* 1. Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* 2. Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* 3. The name of the author may not be used to endorse or promote products +* derived from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +// TODO: Come back to this after the base runtime works. +#if 0 +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + namespace xpath { + + class XPathWildcardAnywhereElement : public XPathElement { + public: + XPathWildcardAnywhereElement(); + + virtual std::vector *evaluate(ParseTree *t) override; + }; + + } + } + } + } + } +} +#endif \ No newline at end of file diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathWildcardElement.cpp b/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathWildcardElement.cpp new file mode 100755 index 000000000..958f813a2 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathWildcardElement.cpp @@ -0,0 +1,63 @@ +#include "XPathWildcardElement.h" +#include "XPath.h" +#include "Trees.h" + +/* +* [The "BSD license"] +* Copyright (c) 2013 Terence Parr +* Copyright (c) 2013 Dan McLaughlin +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* 1. Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* 2. Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* 3. The name of the author may not be used to endorse or promote products +* derived from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +// TODO: Come back to this after the base runtime works. +#if 0 +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + namespace xpath { + + XPathWildcardElement::XPathWildcardElement() : XPathElement(XPath::WILDCARD) { + } + + std::vector *XPathWildcardElement::evaluate(ParseTree *const t) { + if (invert) { // !* is weird but valid (empty) + return new std::vector(); + } + std::vector *kids = new std::vector(); + for (auto c : Trees::getChildren((Tree*)t)) { + kids->push_back((ParseTree*)(c)); + } + return kids; + } + } + } + } + } + } +} +#endif diff --git a/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathWildcardElement.h b/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathWildcardElement.h new file mode 100755 index 000000000..4e57a6dd5 --- /dev/null +++ b/runtime/Cpp/org/antlr/v4/runtime/tree/xpath/XPathWildcardElement.h @@ -0,0 +1,60 @@ +#pragma once + +#include "XPathElement.h" +#include "Declarations.h" +#include + +/* +* [The "BSD license"] +* Copyright (c) 2013 Terence Parr +* Copyright (c) 2013 Dan McLaughlin +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* 1. Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* 2. Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* 3. The name of the author may not be used to endorse or promote products +* derived from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +// TODO: Come back to this after the base runtime works. +#if 0 +namespace org { + namespace antlr { + namespace v4 { + namespace runtime { + namespace tree { + namespace xpath { + + + class XPathWildcardElement : public XPathElement { + public: + XPathWildcardElement(); + + virtual std::vector *evaluate(ParseTree *const t) override; + }; + + } + } + } + } + } +} +#endif