|
8 | 8 | @dynamicMemberLookup |
9 | 9 | public class Grammar: Pattern { |
10 | 10 | public struct CallPattern: Pattern { |
11 | | - public var description: String { "g." + name } |
12 | 11 | public let grammar: Grammar |
13 | 12 | public let name: String |
| 13 | + public var description: String { "<\(name)>" } |
14 | 14 |
|
| 15 | + @inlinable |
| 16 | + init(grammar: Grammar, name: String) { |
| 17 | + self.grammar = grammar |
| 18 | + self.name = name |
| 19 | + } |
| 20 | + |
| 21 | + @inlinable |
15 | 22 | public func createInstructions(_ instructions: inout Instructions) { |
16 | 23 | instructions.append(.openCall(name: name)) |
17 | 24 | } |
18 | 25 | } |
19 | 26 |
|
20 | | - public var description: String { "Grammar" } |
| 27 | + public var description: String { "Grammar" } // TODO: |
21 | 28 |
|
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)] = [] |
29 | 30 |
|
30 | | - public fileprivate(set) var firstPattern: String? |
| 31 | + public var firstPattern: String? { patterns.first?.name } |
31 | 32 |
|
| 33 | + @inlinable |
32 | 34 | public init() {} |
33 | 35 |
|
| 36 | + @inlinable |
34 | 37 | public convenience init(_ closure: (Grammar) -> Void) { |
35 | 38 | self.init() |
36 | 39 | closure(self) |
37 | 40 | } |
38 | 41 |
|
| 42 | + @inlinable |
39 | 43 | public subscript(dynamicMember name: String) -> CallPattern { |
40 | 44 | CallPattern(grammar: self, name: name) |
41 | 45 | } |
42 | 46 |
|
| 47 | + @inlinable |
43 | 48 | public func createInstructions(_ finalInstructions: inout Instructions) throws { |
44 | 49 | var instructions = finalInstructions |
45 | 50 | let startIndex = instructions.endIndex |
@@ -71,12 +76,12 @@ public class Grammar: Pattern { |
71 | 76 | infix operator <-: AssignmentPrecedence |
72 | 77 |
|
73 | 78 | 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))) |
75 | 80 | } |
76 | 81 |
|
77 | 82 | public func <- <P: Pattern>(call: Grammar.CallPattern, capture: Capture<P>) { |
78 | 83 | let newPattern = capture.name == nil |
79 | 84 | ? Capture(name: call.name, capture.wrapped) |
80 | 85 | : capture |
81 | | - call.grammar.patterns[call.name] = AnyPattern(newPattern) |
| 86 | + call.grammar.patterns.append((call.name, AnyPattern(newPattern))) |
82 | 87 | } |
0 commit comments