forked from jasder/antlr
Remove generated parser files and added testRig.
Current version of swift package manager doesn't support shell command or any mechanism that we can leverage to generate parser files. Adding a python script to kick off the unit tests.
This commit is contained in:
parent
bf0a7a64b1
commit
b66ffe86f9
|
@ -1,2 +0,0 @@
|
||||||
T__0=1
|
|
||||||
'A'=1
|
|
|
@ -1,50 +0,0 @@
|
||||||
// Generated from SwiftTest.g4 by ANTLR 4.6
|
|
||||||
|
|
||||||
import Antlr4
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This class provides an empty implementation of {@link SwiftTestListener},
|
|
||||||
* which can be extended to create a listener which only needs to handle a subset
|
|
||||||
* of the available methods.
|
|
||||||
*/
|
|
||||||
open class SwiftTestBaseListener: SwiftTestListener {
|
|
||||||
public init() { }
|
|
||||||
/**
|
|
||||||
* {@inheritDoc}
|
|
||||||
*
|
|
||||||
* <p>The default implementation does nothing.</p>
|
|
||||||
*/
|
|
||||||
open func enterS(_ ctx: SwiftTestParser.SContext) { }
|
|
||||||
/**
|
|
||||||
* {@inheritDoc}
|
|
||||||
*
|
|
||||||
* <p>The default implementation does nothing.</p>
|
|
||||||
*/
|
|
||||||
open func exitS(_ ctx: SwiftTestParser.SContext) { }
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritDoc}
|
|
||||||
*
|
|
||||||
* <p>The default implementation does nothing.</p>
|
|
||||||
*/
|
|
||||||
open func enterEveryRule(_ ctx: ParserRuleContext) { }
|
|
||||||
/**
|
|
||||||
* {@inheritDoc}
|
|
||||||
*
|
|
||||||
* <p>The default implementation does nothing.</p>
|
|
||||||
*/
|
|
||||||
open func exitEveryRule(_ ctx: ParserRuleContext) { }
|
|
||||||
/**
|
|
||||||
* {@inheritDoc}
|
|
||||||
*
|
|
||||||
* <p>The default implementation does nothing.</p>
|
|
||||||
*/
|
|
||||||
open func visitTerminal(_ node: TerminalNode) { }
|
|
||||||
/**
|
|
||||||
* {@inheritDoc}
|
|
||||||
*
|
|
||||||
* <p>The default implementation does nothing.</p>
|
|
||||||
*/
|
|
||||||
open func visitErrorNode(_ node: ErrorNode) { }
|
|
||||||
}
|
|
|
@ -1,90 +0,0 @@
|
||||||
// Generated from SwiftTest.g4 by ANTLR 4.6
|
|
||||||
import Antlr4
|
|
||||||
|
|
||||||
open class SwiftTestLexer: Lexer {
|
|
||||||
internal static var _decisionToDFA: [DFA] = {
|
|
||||||
var decisionToDFA = [DFA]()
|
|
||||||
let length = SwiftTestLexer._ATN.getNumberOfDecisions()
|
|
||||||
for i in 0..<length {
|
|
||||||
decisionToDFA.append(DFA(SwiftTestLexer._ATN.getDecisionState(i)!, i))
|
|
||||||
}
|
|
||||||
return decisionToDFA
|
|
||||||
}()
|
|
||||||
|
|
||||||
internal static let _sharedContextCache:PredictionContextCache = PredictionContextCache()
|
|
||||||
public static let T__0=1
|
|
||||||
public static let channelNames: [String] = [
|
|
||||||
"DEFAULT_TOKEN_CHANNEL", "HIDDEN"
|
|
||||||
]
|
|
||||||
|
|
||||||
public static let modeNames: [String] = [
|
|
||||||
"DEFAULT_MODE"
|
|
||||||
]
|
|
||||||
|
|
||||||
public static let ruleNames: [String] = [
|
|
||||||
"T__0"
|
|
||||||
]
|
|
||||||
|
|
||||||
private static let _LITERAL_NAMES: [String?] = [
|
|
||||||
nil, "'A'"
|
|
||||||
]
|
|
||||||
private static let _SYMBOLIC_NAMES: [String?] = [
|
|
||||||
]
|
|
||||||
public static let VOCABULARY: Vocabulary = Vocabulary(_LITERAL_NAMES, _SYMBOLIC_NAMES)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @deprecated Use {@link #VOCABULARY} instead.
|
|
||||||
*/
|
|
||||||
//@Deprecated
|
|
||||||
public let tokenNames: [String?]? = {
|
|
||||||
let length = _SYMBOLIC_NAMES.count
|
|
||||||
var tokenNames = [String?](repeating: nil, count: length)
|
|
||||||
for i in 0..<length {
|
|
||||||
var name = VOCABULARY.getLiteralName(i)
|
|
||||||
if name == nil {
|
|
||||||
name = VOCABULARY.getSymbolicName(i)
|
|
||||||
}
|
|
||||||
if name == nil {
|
|
||||||
name = "<INVALID>"
|
|
||||||
}
|
|
||||||
tokenNames[i] = name
|
|
||||||
}
|
|
||||||
return tokenNames
|
|
||||||
}()
|
|
||||||
|
|
||||||
override
|
|
||||||
open func getTokenNames() -> [String?]? {
|
|
||||||
return tokenNames
|
|
||||||
}
|
|
||||||
|
|
||||||
open override func getVocabulary() -> Vocabulary {
|
|
||||||
return SwiftTestLexer.VOCABULARY
|
|
||||||
}
|
|
||||||
|
|
||||||
public override init(_ input: CharStream) {
|
|
||||||
RuntimeMetaData.checkVersion("4.7", RuntimeMetaData.VERSION)
|
|
||||||
super.init(input)
|
|
||||||
_interp = LexerATNSimulator(self, SwiftTestLexer._ATN, SwiftTestLexer._decisionToDFA, SwiftTestLexer._sharedContextCache)
|
|
||||||
}
|
|
||||||
|
|
||||||
override
|
|
||||||
open func getGrammarFileName() -> String { return "SwiftTest.g4" }
|
|
||||||
|
|
||||||
override
|
|
||||||
open func getRuleNames() -> [String] { return SwiftTestLexer.ruleNames }
|
|
||||||
|
|
||||||
override
|
|
||||||
open func getSerializedATN() -> String { return SwiftTestLexer._serializedATN }
|
|
||||||
|
|
||||||
override
|
|
||||||
open func getChannelNames() -> [String] { return HelloLexer.channelNames }
|
|
||||||
|
|
||||||
override
|
|
||||||
open func getModeNames() -> [String] { return SwiftTestLexer.modeNames }
|
|
||||||
|
|
||||||
override
|
|
||||||
open func getATN() -> ATN { return SwiftTestLexer._ATN }
|
|
||||||
|
|
||||||
public static let _serializedATN: String = SwiftTestLexerATN().jsonString
|
|
||||||
public static let _ATN: ATN = ATNDeserializer().deserializeFromJson(_serializedATN)
|
|
||||||
}
|
|
|
@ -1,2 +0,0 @@
|
||||||
T__0=1
|
|
||||||
'A'=1
|
|
|
@ -1,3 +0,0 @@
|
||||||
class SwiftTestLexerATN {
|
|
||||||
let jsonString: String = "{\"version\":3,\"uuid\":\"aadb8d7e-aeef-4415-ad2b-8204d6cf042e\",\"grammarType\":0,\"maxTokenType\":1,\"states\":[{\"stateType\":6,\"ruleIndex\":-1},{\"stateType\":2,\"ruleIndex\":0},{\"stateType\":7,\"ruleIndex\":0},{\"stateType\":1,\"ruleIndex\":0},{\"stateType\":1,\"ruleIndex\":0}],\"nonGreedyStates\":[],\"precedenceStates\":[],\"ruleToStartState\":[{\"stateNumber\":1,\"ruleToTokenType\":1}],\"modeToStartState\":[0],\"nsets\":0,\"IntervalSet\":[],\"allTransitionsBuilder\":[[{\"src\":0,\"trg\":1,\"edgeType\":1,\"arg1\":0,\"arg2\":0,\"arg3\":0}],[{\"src\":1,\"trg\":3,\"edgeType\":1,\"arg1\":0,\"arg2\":0,\"arg3\":0}],[{\"src\":3,\"trg\":4,\"edgeType\":5,\"arg1\":65,\"arg2\":0,\"arg3\":0}],[{\"src\":4,\"trg\":2,\"edgeType\":1,\"arg1\":0,\"arg2\":0,\"arg3\":0}]],\"decisionToState\":[0],\"lexerActions\":[]}"
|
|
||||||
}
|
|
|
@ -1,21 +0,0 @@
|
||||||
// Generated from SwiftTest.g4 by ANTLR 4.6
|
|
||||||
import Antlr4
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This interface defines a complete listener for a parse tree produced by
|
|
||||||
* {@link SwiftTestParser}.
|
|
||||||
*/
|
|
||||||
public protocol SwiftTestListener: ParseTreeListener {
|
|
||||||
/**
|
|
||||||
* Enter a parse tree produced by {@link SwiftTestParser#s}.
|
|
||||||
- Parameters:
|
|
||||||
- ctx: the parse tree
|
|
||||||
*/
|
|
||||||
func enterS(_ ctx: SwiftTestParser.SContext)
|
|
||||||
/**
|
|
||||||
* Exit a parse tree produced by {@link SwiftTestParser#s}.
|
|
||||||
- Parameters:
|
|
||||||
- ctx: the parse tree
|
|
||||||
*/
|
|
||||||
func exitS(_ ctx: SwiftTestParser.SContext)
|
|
||||||
}
|
|
|
@ -1,118 +0,0 @@
|
||||||
// Generated from SwiftTest.g4 by ANTLR 4.6
|
|
||||||
import Antlr4
|
|
||||||
|
|
||||||
open class SwiftTestParser: Parser {
|
|
||||||
|
|
||||||
internal static var _decisionToDFA: [DFA] = {
|
|
||||||
var decisionToDFA = [DFA]()
|
|
||||||
let length = SwiftTestParser._ATN.getNumberOfDecisions()
|
|
||||||
for i in 0..<length {
|
|
||||||
decisionToDFA.append(DFA(SwiftTestParser._ATN.getDecisionState(i)!, i))
|
|
||||||
}
|
|
||||||
return decisionToDFA
|
|
||||||
}()
|
|
||||||
internal static let _sharedContextCache: PredictionContextCache = PredictionContextCache()
|
|
||||||
public enum Tokens: Int {
|
|
||||||
case EOF = -1, T__0 = 1
|
|
||||||
}
|
|
||||||
public static let RULE_s = 0
|
|
||||||
public static let ruleNames: [String] = [
|
|
||||||
"s"
|
|
||||||
]
|
|
||||||
|
|
||||||
private static let _LITERAL_NAMES: [String?] = [
|
|
||||||
nil, "'A'"
|
|
||||||
]
|
|
||||||
private static let _SYMBOLIC_NAMES: [String?] = [
|
|
||||||
]
|
|
||||||
public static let VOCABULARY: Vocabulary = Vocabulary(_LITERAL_NAMES, _SYMBOLIC_NAMES)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @deprecated Use {@link #VOCABULARY} instead.
|
|
||||||
*/
|
|
||||||
//@Deprecated
|
|
||||||
public let tokenNames: [String?]? = {
|
|
||||||
let length = _SYMBOLIC_NAMES.count
|
|
||||||
var tokenNames = [String?](repeating: nil, count: length)
|
|
||||||
for i in 0..<length {
|
|
||||||
var name = VOCABULARY.getLiteralName(i)
|
|
||||||
if name == nil {
|
|
||||||
name = VOCABULARY.getSymbolicName(i)
|
|
||||||
}
|
|
||||||
if name == nil {
|
|
||||||
name = "<INVALID>"
|
|
||||||
}
|
|
||||||
tokenNames[i] = name
|
|
||||||
}
|
|
||||||
return tokenNames
|
|
||||||
}()
|
|
||||||
|
|
||||||
override
|
|
||||||
open func getTokenNames() -> [String?]? {
|
|
||||||
return tokenNames
|
|
||||||
}
|
|
||||||
|
|
||||||
override
|
|
||||||
open func getGrammarFileName() -> String { return "SwiftTest.g4" }
|
|
||||||
|
|
||||||
override
|
|
||||||
open func getRuleNames() -> [String] { return SwiftTestParser.ruleNames }
|
|
||||||
|
|
||||||
override
|
|
||||||
open func getSerializedATN() -> String { return SwiftTestParser._serializedATN }
|
|
||||||
|
|
||||||
override
|
|
||||||
open func getATN() -> ATN { return SwiftTestParser._ATN }
|
|
||||||
|
|
||||||
open override func getVocabulary() -> Vocabulary {
|
|
||||||
return SwiftTestParser.VOCABULARY
|
|
||||||
}
|
|
||||||
|
|
||||||
public override init(_ input:TokenStream)throws {
|
|
||||||
RuntimeMetaData.checkVersion("4.6", RuntimeMetaData.VERSION)
|
|
||||||
try super.init(input)
|
|
||||||
_interp = ParserATNSimulator(self,SwiftTestParser._ATN,SwiftTestParser._decisionToDFA, SwiftTestParser._sharedContextCache)
|
|
||||||
}
|
|
||||||
open class SContext:ParserRuleContext {
|
|
||||||
open func EOF() -> TerminalNode? { return getToken(SwiftTestParser.Tokens.EOF.rawValue, 0) }
|
|
||||||
open override func getRuleIndex() -> Int { return SwiftTestParser.RULE_s }
|
|
||||||
override
|
|
||||||
open func enterRule(_ listener: ParseTreeListener) {
|
|
||||||
if listener is SwiftTestListener {
|
|
||||||
(listener as! SwiftTestListener).enterS(self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
override
|
|
||||||
open func exitRule(_ listener: ParseTreeListener) {
|
|
||||||
if listener is SwiftTestListener {
|
|
||||||
(listener as! SwiftTestListener).exitS(self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@discardableResult
|
|
||||||
open func s() throws -> SContext {
|
|
||||||
var _localctx: SContext = SContext(_ctx, getState())
|
|
||||||
try enterRule(_localctx, 0, SwiftTestParser.RULE_s)
|
|
||||||
defer {
|
|
||||||
try! exitRule()
|
|
||||||
}
|
|
||||||
do {
|
|
||||||
try enterOuterAlt(_localctx, 1)
|
|
||||||
setState(2)
|
|
||||||
try match(SwiftTestParser.Tokens.T__0.rawValue)
|
|
||||||
setState(3)
|
|
||||||
try match(SwiftTestParser.Tokens.EOF.rawValue)
|
|
||||||
|
|
||||||
}
|
|
||||||
catch ANTLRException.recognition(let re) {
|
|
||||||
_localctx.exception = re
|
|
||||||
_errHandler.reportError(self, re)
|
|
||||||
try _errHandler.recover(self, re)
|
|
||||||
}
|
|
||||||
|
|
||||||
return _localctx
|
|
||||||
}
|
|
||||||
|
|
||||||
public static let _serializedATN : String = SwiftTestParserATN().jsonString
|
|
||||||
public static let _ATN: ATN = ATNDeserializer().deserializeFromJson(_serializedATN)
|
|
||||||
}
|
|
|
@ -1,3 +0,0 @@
|
||||||
class SwiftTestParserATN {
|
|
||||||
let jsonString: String = "{\"version\":3,\"uuid\":\"aadb8d7e-aeef-4415-ad2b-8204d6cf042e\",\"grammarType\":1,\"maxTokenType\":1,\"states\":[{\"stateType\":2,\"ruleIndex\":0},{\"stateType\":7,\"ruleIndex\":0},{\"stateType\":1,\"ruleIndex\":0},{\"stateType\":1,\"ruleIndex\":0},{\"stateType\":1,\"ruleIndex\":0},{\"stateType\":1,\"ruleIndex\":0}],\"nonGreedyStates\":[],\"precedenceStates\":[],\"ruleToStartState\":[{\"stateNumber\":0}],\"modeToStartState\":[],\"nsets\":0,\"IntervalSet\":[],\"allTransitionsBuilder\":[[{\"src\":0,\"trg\":2,\"edgeType\":1,\"arg1\":0,\"arg2\":0,\"arg3\":0}],[{\"src\":2,\"trg\":3,\"edgeType\":5,\"arg1\":1,\"arg2\":0,\"arg3\":0}],[{\"src\":3,\"trg\":4,\"edgeType\":5,\"arg1\":0,\"arg2\":0,\"arg3\":1}],[{\"src\":4,\"trg\":1,\"edgeType\":1,\"arg1\":0,\"arg2\":0,\"arg3\":0}],[]],\"decisionToState\":[],\"lexerActions\":[]}"
|
|
||||||
}
|
|
|
@ -0,0 +1,66 @@
|
||||||
|
#! /usr/bin/python
|
||||||
|
|
||||||
|
"""
|
||||||
|
Find all .g4 files and generate parsers in the same directory.
|
||||||
|
the antlr used should be the one located at user's mvn directory
|
||||||
|
the filename is antlr4-ANTLR_VERSION-SNAPSHOT.jar. You can get it
|
||||||
|
by running: "mvn install"
|
||||||
|
|
||||||
|
NOTE: In case of customized location of .m2 folder, you can change the
|
||||||
|
USER_M2 constant below.
|
||||||
|
|
||||||
|
the java version is used according to environment variable $JAVA_HOME.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import fnmatch
|
||||||
|
import os.path
|
||||||
|
from subprocess import call
|
||||||
|
|
||||||
|
ANTLR_VERSION = '4.7'
|
||||||
|
USER_M2 = os.path.expanduser('~') + '/.m2/'
|
||||||
|
ANTLR4_FOLDER = USER_M2 + 'repository/org/antlr/antlr4/' + ANTLR_VERSION + '-SNAPSHOT/'
|
||||||
|
ANTLR4_JAR = ANTLR4_FOLDER + 'antlr4-' + ANTLR_VERSION + '-SNAPSHOT-complete.jar'
|
||||||
|
|
||||||
|
|
||||||
|
def jar_exists():
|
||||||
|
"""
|
||||||
|
Finds the antlr4 jar.
|
||||||
|
"""
|
||||||
|
return os.path.exists(ANTLR4_JAR)
|
||||||
|
|
||||||
|
|
||||||
|
def find_g4():
|
||||||
|
"""
|
||||||
|
Find all g4 files and return a list of them.
|
||||||
|
The recursive search starts from the directory containing
|
||||||
|
this python file.
|
||||||
|
"""
|
||||||
|
file_path = os.path.realpath(__file__)
|
||||||
|
parent_folder = file_path[0:file_path.rindex('/')+1]
|
||||||
|
res = []
|
||||||
|
for cur, _, filenames in os.walk(parent_folder):
|
||||||
|
cur_files = fnmatch.filter(filenames, "*.g4")
|
||||||
|
res += [cur+'/'+cur_file for cur_file in cur_files]
|
||||||
|
return res
|
||||||
|
|
||||||
|
|
||||||
|
def gen_parser(grammar):
|
||||||
|
"""
|
||||||
|
Generate parser for the input g4 file.
|
||||||
|
"""
|
||||||
|
java_home = os.environ['JAVA_HOME']
|
||||||
|
java = java_home + '/bin/java'
|
||||||
|
if not os.path.exists(java):
|
||||||
|
print 'Cannot find java. Check your JAVA_HOME setting.'
|
||||||
|
return
|
||||||
|
|
||||||
|
call([java, '-jar', ANTLR4_JAR, '-Dlanguage=Swift', grammar])
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
if not jar_exists():
|
||||||
|
print 'Run "mvn install" in antlr4 project root' + \
|
||||||
|
'first or check mvn settings'
|
||||||
|
exit()
|
||||||
|
|
||||||
|
_ = [gen_parser(f) for f in find_g4()]
|
Loading…
Reference in New Issue