Skip to content

Commit

Permalink
Partially re-applying repo patch:
Browse files Browse the repository at this point in the history
  • Loading branch information
LuizZak committed Sep 27, 2022
1 parent a627f8d commit f999ab7
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 49 deletions.
47 changes: 41 additions & 6 deletions Tests/Antlr4Tests/ThreadingTests.swift
@@ -1,15 +1,19 @@
/// Copyright (c) 2012-2021 The ANTLR Project. All rights reserved.
/// Use of this file is governed by the BSD 3-clause license that
/// can be found in the LICENSE.txt file in the project root.

import XCTest
import Antlr4

class ThreadingTests: XCTestCase {
static let allTests = [
("testParallelExecution", testParallelExecution),
// ("testParallelExecution", testParallelExecution),
("testParallelExecution_stateful", testParallelExecution_stateful),
]

// NOTE: This test is now unsafe as state differentiation is left up to
// NOTE: generated Parser/Lexer classes
#if false

///
/// This test verifies parallel execution of the parser
///
Expand All @@ -23,8 +27,8 @@ class ThreadingTests: XCTestCase {
"2 + 8 + 4",
"890",
]
let exp = expectation(description: "Waiting on async-task")
exp.expectedFulfillmentCount = 100
let expectation = expectation(description: "Waiting on async-task")
expectation.expectedFulfillmentCount = 100
for i in 1...100 {
DispatchQueue.global().async {
let lexer = ThreadingLexer(ANTLRInputStream(input[i % 7]))
Expand All @@ -33,12 +37,43 @@ class ThreadingTests: XCTestCase {

let _ = try? parser?.s()

exp.fulfill()
expectation.fulfill()
}
}

waitForExpectations(timeout: 30.0) { (_) in
print("Completed")
}
}

#endif

func testParallelExecution_stateful() throws {
let input = [
"2 * 8 - 4",
"2 + 8 / 4",
"2 - 8 - 4",
"2 * 8 * 4",
"2 / 8 / 4",
"2 + 8 + 4",
"890",
]
let expectation = expectation(description: "Waiting on async-task")
expectation.expectedFulfillmentCount = 100
for i in 1...100 {
DispatchQueue.global().async {
let lexer = ThreadingLexer(ANTLRInputStream(input[i % 7]), .init())
let tokenStream = CommonTokenStream(lexer)
let parser = try? ThreadingParser(tokenStream, .init())

let _ = try? parser?.s()

expectation.fulfill()
}
}

waitForExpectations(timeout: 30.0) { (_) in
print("Completed")
}
}
}
}
55 changes: 38 additions & 17 deletions Tests/Antlr4Tests/gen/ThreadingLexer.swift
Expand Up @@ -3,17 +3,34 @@ import Antlr4

open class ThreadingLexer: Lexer {

internal static var _decisionToDFA: [DFA] = {
var decisionToDFA = [DFA]()
let length = ThreadingLexer._ATN.getNumberOfDecisions()
for i in 0..<length {
decisionToDFA.append(DFA(ThreadingLexer._ATN.getDecisionState(i)!, i))
}
return decisionToDFA
}()

internal static let _sharedContextCache = PredictionContextCache()

public class State {
public let _ATN: ATN = try! ATNDeserializer().deserialize(_serializedATN)

internal var _decisionToDFA: [DFA]
internal let _sharedContextCache: PredictionContextCache = PredictionContextCache()

public init() {
var decisionToDFA = [DFA]()
let length = _ATN.getNumberOfDecisions()
for i in 0..<length {
decisionToDFA.append(DFA(_ATN.getDecisionState(i)!, i))
}
_decisionToDFA = decisionToDFA
}
}

public var _ATN: ATN {
return state._ATN
}
internal var _decisionToDFA: [DFA] {
return state._decisionToDFA
}
internal var _sharedContextCache: PredictionContextCache {
return state._sharedContextCache
}

public var state: State

public
static let INT=1, MUL=2, DIV=3, ADD=4, SUB=5, WS=6

Expand Down Expand Up @@ -47,11 +64,18 @@ open class ThreadingLexer: Lexer {
return ThreadingLexer.VOCABULARY
}

public required convenience
init(_ input: CharStream) {
self.init(input, State())
}

public
required init(_ input: CharStream) {
required init(_ input: CharStream, _ state: State) {
self.state = state

RuntimeMetaData.checkVersion("4.11.1", RuntimeMetaData.VERSION)
super.init(input)
_interp = LexerATNSimulator(self, ThreadingLexer._ATN, ThreadingLexer._decisionToDFA, ThreadingLexer._sharedContextCache)
_interp = LexerATNSimulator(self, _ATN, _decisionToDFA, _sharedContextCache)
}

override open
Expand All @@ -70,7 +94,7 @@ open class ThreadingLexer: Lexer {
func getModeNames() -> [String] { return ThreadingLexer.modeNames }

override open
func getATN() -> ATN { return ThreadingLexer._ATN }
func getATN() -> ATN { return _ATN }

static let _serializedATN:[Int] = [
4,0,6,33,6,-1,2,0,7,0,2,1,7,1,2,2,7,2,2,3,7,3,2,4,7,4,2,5,7,5,1,0,4,0,
Expand All @@ -84,7 +108,4 @@ open class ThreadingLexer: Lexer {
0,0,0,26,28,7,1,0,0,27,26,1,0,0,0,28,29,1,0,0,0,29,27,1,0,0,0,29,30,1,
0,0,0,30,31,1,0,0,0,31,32,6,5,0,0,32,12,1,0,0,0,3,0,16,29,1,0,1,0
]

public
static let _ATN: ATN = try! ATNDeserializer().deserialize(_serializedATN)
}
66 changes: 40 additions & 26 deletions Tests/Antlr4Tests/gen/ThreadingParser.swift
Expand Up @@ -3,17 +3,33 @@ import Antlr4

open class ThreadingParser: Parser {

internal static var _decisionToDFA: [DFA] = {
var decisionToDFA = [DFA]()
let length = ThreadingParser._ATN.getNumberOfDecisions()
for i in 0..<length {
decisionToDFA.append(DFA(ThreadingParser._ATN.getDecisionState(i)!, i))
}
return decisionToDFA
}()

internal static let _sharedContextCache = PredictionContextCache()

public class State {
public let _ATN: ATN = try! ATNDeserializer().deserialize(_serializedATN)

internal var _decisionToDFA: [DFA]
internal let _sharedContextCache: PredictionContextCache = PredictionContextCache()

public init() {
var decisionToDFA = [DFA]()
let length = _ATN.getNumberOfDecisions()
for i in 0..<length {
decisionToDFA.append(DFA(_ATN.getDecisionState(i)!, i))
}
_decisionToDFA = decisionToDFA
}
}

public var _ATN: ATN {
return state._ATN
}
internal var _decisionToDFA: [DFA] {
return state._decisionToDFA
}
internal var _sharedContextCache: PredictionContextCache {
return state._sharedContextCache
}

public var state: State
public
enum Tokens: Int {
case EOF = -1, INT = 1, MUL = 2, DIV = 3, ADD = 4, SUB = 5, WS = 6
Expand Down Expand Up @@ -46,20 +62,21 @@ open class ThreadingParser: Parser {
func getSerializedATN() -> [Int] { return ThreadingParser._serializedATN }

override open
func getATN() -> ATN { return ThreadingParser._ATN }
func getATN() -> ATN { return _ATN }


override open
func getVocabulary() -> Vocabulary {
return ThreadingParser.VOCABULARY
}

override public
init(_ input:TokenStream) throws {
RuntimeMetaData.checkVersion("4.11.1", RuntimeMetaData.VERSION)
try super.init(input)
_interp = ParserATNSimulator(self,ThreadingParser._ATN,ThreadingParser._decisionToDFA, ThreadingParser._sharedContextCache)
}
override public convenience
init(_ input: TokenStream) throws {
try self.init(input, State())
}

public
init(_ input: TokenStream, _ state: State) throws {
self.state = state
RuntimeMetaData.checkVersion("4.11.1", RuntimeMetaData.VERSION)
try super.init(input)
_interp = ParserATNSimulator(self, _ATN, _decisionToDFA, _sharedContextCache)
}


public class SContext: ParserRuleContext {
Expand Down Expand Up @@ -385,7 +402,4 @@ open class ThreadingParser: Parser {
20,1,0,0,0,18,16,1,0,0,0,18,19,1,0,0,0,19,3,1,0,0,0,20,18,1,0,0,0,2,16,
18
]

public
static let _ATN = try! ATNDeserializer().deserialize(_serializedATN)
}

0 comments on commit f999ab7

Please sign in to comment.