Skip to content

Commit 4a4c43f

Browse files
committed
Make Grammar.patterns an array.
1 parent 8b656ad commit 4a4c43f

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

Sources/Patterns/Grammar.swift

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,43 @@
88
@dynamicMemberLookup
99
public class Grammar: Pattern {
1010
public struct CallPattern: Pattern {
11-
public var description: String { "g." + name }
1211
public let grammar: Grammar
1312
public let name: String
13+
public var description: String { "<\(name)>" }
1414

15+
@inlinable
16+
init(grammar: Grammar, name: String) {
17+
self.grammar = grammar
18+
self.name = name
19+
}
20+
21+
@inlinable
1522
public func createInstructions(_ instructions: inout Instructions) {
1623
instructions.append(.openCall(name: name))
1724
}
1825
}
1926

20-
public var description: String { "Grammar" }
27+
public var description: String { "Grammar" } // TODO:
2128

22-
public fileprivate(set) var patterns: [String: AnyPattern] = [:] {
23-
didSet {
24-
if firstPattern == nil {
25-
firstPattern = patterns.first?.key
26-
}
27-
}
28-
}
29+
public internal(set) var patterns: [(name: String, pattern: AnyPattern)] = []
2930

30-
public fileprivate(set) var firstPattern: String?
31+
public var firstPattern: String? { patterns.first?.name }
3132

33+
@inlinable
3234
public init() {}
3335

36+
@inlinable
3437
public convenience init(_ closure: (Grammar) -> Void) {
3538
self.init()
3639
closure(self)
3740
}
3841

42+
@inlinable
3943
public subscript(dynamicMember name: String) -> CallPattern {
4044
CallPattern(grammar: self, name: name)
4145
}
4246

47+
@inlinable
4348
public func createInstructions(_ finalInstructions: inout Instructions) throws {
4449
var instructions = finalInstructions
4550
let startIndex = instructions.endIndex
@@ -71,12 +76,12 @@ public class Grammar: Pattern {
7176
infix operator <-: AssignmentPrecedence
7277

7378
public func <- <P: Pattern>(call: Grammar.CallPattern, pattern: P) {
74-
call.grammar.patterns[call.name] = AnyPattern(pattern)
79+
call.grammar.patterns.append((call.name, AnyPattern(pattern)))
7580
}
7681

7782
public func <- <P: Pattern>(call: Grammar.CallPattern, capture: Capture<P>) {
7883
let newPattern = capture.name == nil
7984
? Capture(name: call.name, capture.wrapped)
8085
: capture
81-
call.grammar.patterns[call.name] = AnyPattern(newPattern)
86+
call.grammar.patterns.append((call.name, AnyPattern(newPattern)))
8287
}

Tests/PatternsTests/GrammarTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class GrammarTests: XCTestCase {
1717
}()
1818

1919
func testNamesAnonymousCaptures() {
20-
XCTAssertEqual((grammar1.patterns["letter"]?.wrapped as? Capture<OneOf>)?.name, "letter")
20+
XCTAssertEqual((grammar1.patterns.first?.pattern.wrapped as? Capture<OneOf>)?.name, "letter")
2121
}
2222

2323
func testSetsFirstPattern() {

0 commit comments

Comments
 (0)