diff --git a/Sources/Lotsawa/Chart.swift b/Sources/Lotsawa/Chart.swift index e601913..48e154d 100644 --- a/Sources/Lotsawa/Chart.swift +++ b/Sources/Lotsawa/Chart.swift @@ -297,7 +297,7 @@ extension Chart { /// Returns `true` iff `lhs` should precede `rhs` in a derivation set. public static func < (lhs: Self, rhs: Self) -> Bool { - (lhs.item.key, lhs.mainstemIndexStorage) < (rhs.item.key, rhs.mainstemIndexStorage) + (lhs.key, lhs.mainstemIndexStorage) < (rhs.key, rhs.mainstemIndexStorage) } subscript(dynamicMember m: KeyPath) -> Target { @@ -327,9 +327,9 @@ extension Chart { let ithSet = i == currentEarleme ? currentEarleySet : earleySet(i) let k = ItemID.transitionKey(s) - let j = ithSet.partitionPoint { d in d.item.transitionKey >= k } + let j = ithSet.partitionPoint { d in d.transitionKey >= k } let items = ithSet[j...] - return items.prefix(while: { x in x.item.symbolID == s.id }) + return items.prefix(while: { x in x.symbolID == s.id }) } /// Returns the items in Earley set `i` whose use is triggered by the recognition of `s`. @@ -346,9 +346,9 @@ extension Chart { let ithSet = earleySet(extent.upperBound) let k = ItemID.completionKey(lhs, origin: extent.lowerBound) - let j = ithSet.partitionPoint { d in d.item.key >= k } + let j = ithSet.partitionPoint { d in d.key >= k } let r0 = ithSet[j...] - let r = r0.prefix(while: { x in x.item.lhs == lhs && x.item.origin == extent.lowerBound }) + let r = r0.prefix(while: { x in x.lhs == lhs && x.origin == extent.lowerBound }) return r } @@ -386,7 +386,7 @@ extension Chart { /// Injects a Leo item memoizing `x` with transition symbol `t` before entries[i].item, returning /// true if it was not already present. mutating func insertLeo(_ leo: Entry, at i: Int) -> Bool { - assert(leo.item.isLeo) + assert(leo.isLeo) if entries[i].item == leo.item { // FIXME: can we prove we never arrive here? Should be possible. assert( @@ -475,6 +475,6 @@ extension Chart { { let predecessors = transitionEntries(on: s, inEarleySet: i) - return predecessors.first.map { $0.item.isLeo ? predecessors : nil } ?? nil + return predecessors.first.map { $0.isLeo ? predecessors : nil } ?? nil } } diff --git a/Sources/Lotsawa/Forest.swift b/Sources/Lotsawa/Forest.swift index bd55178..6ceb5ed 100644 --- a/Sources/Lotsawa/Forest.swift +++ b/Sources/Lotsawa/Forest.swift @@ -115,7 +115,7 @@ extension Forest { public mutating func requireCompletion( _ c: Chart.Entry, inEarleme i: SourcePosition ) -> Bool { - assert(c.item.isCompletion, "requiring non-completion \(c)") + assert(c.isCompletion, "requiring non-completion \(c)") let setI = chart.earleySet(i) let p = setI.partitionPoint { $0 >= c } /// Derivation already in the chart? We're done @@ -125,7 +125,7 @@ extension Forest { let r0 = setI[p...].first?.item == c.item || setI[..= c } // Derivation already in the leoSet? We're done @@ -141,8 +141,8 @@ extension Forest { public mutating func collectLeoCompletions( causing top: Chart.Entry, endingAt endEarleme: SourcePosition ) { - assert(top.item.isCompletion, "collecting leo completions on non-completion \(top)") - guard let lim0Index = top.mainstemIndex, chart.entries[lim0Index].item.isLeo else { return } + assert(top.isCompletion, "collecting leo completions on non-completion \(top)") + guard let lim0Index = top.mainstemIndex, chart.entries[lim0Index].isLeo else { return } var workingLIMIndex = lim0Index while true { let workingBaseIndex = workingLIMIndex + 1 @@ -164,7 +164,7 @@ extension Forest { collectLeoCompletions(causing: top, endingAt: locus.upperBound) } let storedCompletionsWithEarleyMainstem - = storedCompletions.lazy.filter { [chart] in chart.entries[$0.mainstemIndex!].item.isEarley } + = storedCompletions.lazy.filter { [chart] in chart.entries[$0.mainstemIndex!].isEarley } let leos = leoCompletions[.init(locus: locus, lhs: lhs), default: []] @@ -180,7 +180,7 @@ extension Forest { func first(of d: DerivationSet.Storage) -> Derivation { .init( path: d, domain: self, - rule: grammar.rule(containing: d.completions.first!.item.dotPosition)) + rule: grammar.rule(containing: d.completions.first!.dotPosition)) } } diff --git a/Sources/Lotsawa/Recognizer.swift b/Sources/Lotsawa/Recognizer.swift index e4588b4..2116b0b 100644 --- a/Sources/Lotsawa/Recognizer.swift +++ b/Sources/Lotsawa/Recognizer.swift @@ -63,7 +63,7 @@ extension Recognizer { mutating func insert(_ newEntry: Chart.Entry) -> Bool { if !chart.insert(newEntry) { return false } - if leoPositions.contains(newEntry.item.dotPosition) { + if leoPositions.contains(newEntry.dotPosition) { self.leoCandidateFound = true } return true @@ -75,14 +75,14 @@ extension Recognizer { let mainstems = chart.transitionEntries(on: s, inEarleySet: origin) if let head = mainstems.first, - let p = head.item.memoizedPenultIndex + let p = head.memoizedPenultIndex { derive( .init(item: chart.entries[p].item.advanced(in: g), mainstemIndex: mainstems.startIndex)) } else { assert( - mainstems.allSatisfy(\.item.isEarley), + mainstems.allSatisfy(\.isEarley), "Leo item is not first in mainstems.") let transitionItemIndices @@ -101,7 +101,7 @@ extension Recognizer { let s = g.recognized(at: x.dotPosition + 1)! let mainstems = chart.transitionEntries(on: s, inEarleySet: x.origin) - if let head = mainstems.first, head.item.isLeo { + if let head = mainstems.first, head.isLeo { return mainstems.startIndex } return nil @@ -110,14 +110,14 @@ extension Recognizer { /// Ensures that `x` is represented in the current derivation set, and draws any consequent /// conclusions. private mutating func derive(_ x: Chart.Entry) { - assert(!x.item.isLeo) + assert(!x.isLeo) if !insert(x) { return } - if let t = x.item.transitionSymbol { + if let t = x.transitionSymbol { predict(t) } else { // it's complete - discover(g.recognized(at: x.item.dotPosition)!, startingAt: x.item.origin) + discover(g.recognized(at: x.dotPosition)!, startingAt: x.origin) } } @@ -139,14 +139,14 @@ extension Recognizer { // If the dot is in a Leo position and the transition symbol was unique in this set if leoPositions.contains(x.dotPosition) && (endOfItem == chart.currentEarleySet.endIndex - || chart.currentEarleySet[endOfItem].item.transitionSymbol != t) + || chart.currentEarleySet[endOfItem].transitionSymbol != t) { let l: Chart.Entry if let p = leoPredecessorIndex(x) { l = .init( item: .init( - memoizingItemIndex: chart.entries[p].item.memoizedPenultIndex!, transitionSymbol: t), + memoizingItemIndex: chart.entries[p].memoizedPenultIndex!, transitionSymbol: t), mainstemIndex: p) } else { @@ -161,7 +161,7 @@ extension Recognizer { else { // Else skip over everything with this transition symbol i = chart.currentEarleySet[endOfItem...].prefix { - $0.item.transitionSymbol == t + $0.transitionSymbol == t }.endIndex } } diff --git a/Tests/LotsawaTests/ChartInternalTests.swift b/Tests/LotsawaTests/ChartInternalTests.swift index b6d510a..8e0cc93 100644 --- a/Tests/LotsawaTests/ChartInternalTests.swift +++ b/Tests/LotsawaTests/ChartInternalTests.swift @@ -93,8 +93,8 @@ class ChartInternalTests: XCTestCase { let chart = r.base.chart let top3 = try chart.completions(of: sum, over: 0..<3).checkedOnlyElement() - XCTAssert(top3.item.isCompletion) - let topRuleID = g.raw.rule(containing: top3.item.dotPosition) + XCTAssert(top3.isCompletion) + let topRuleID = g.raw.rule(containing: top3.dotPosition) let topRule = g.raw.storedRule(topRuleID) XCTAssertEqual(topRule.lhs, sum) XCTAssertEqual(Array(topRule.rhs), [sum, additive, product]) @@ -104,20 +104,20 @@ class ChartInternalTests: XCTestCase { let rhsProduct = try chart.completions(of: product, over: 2..<3) .checkedOnlyElement() - XCTAssert(rhsProduct.item.isCompletion) + XCTAssert(rhsProduct.isCompletion) let top2 = try chart.earleyMainstem(of: top3).checkedOnlyElement() - XCTAssertEqual(g.raw.rule(containing: top2.item.dotPosition), topRuleID) + XCTAssertEqual(g.raw.rule(containing: top2.dotPosition), topRuleID) - XCTAssertEqual(top2.item.transitionSymbol, product) + XCTAssertEqual(top2.transitionSymbol, product) let top1 = try chart.earleyMainstem(of: top2).checkedOnlyElement() - XCTAssertEqual(g.raw.rule(containing: top1.item.dotPosition), topRuleID) + XCTAssertEqual(g.raw.rule(containing: top1.dotPosition), topRuleID) let top0 = try chart.earleyMainstem(of: top1).checkedOnlyElement() - XCTAssertEqual(g.raw.rule(containing: top0.item.dotPosition), topRuleID) + XCTAssertEqual(g.raw.rule(containing: top0.dotPosition), topRuleID) // Could explore more, but it's time to create a better abstraction. diff --git a/Tests/LotsawaTests/Utils/DebugChart.swift b/Tests/LotsawaTests/Utils/DebugChart.swift index 2eceacf..d8d7063 100644 --- a/Tests/LotsawaTests/Utils/DebugChart.swift +++ b/Tests/LotsawaTests/Utils/DebugChart.swift @@ -20,19 +20,19 @@ extension DebugChart: CustomStringConvertible { while let head = remainingDerivations.first { let itemDerivations = remainingDerivations.prefix { x in x.item == head.item } result += "\(itemDerivations.startIndex): " - if !head.item.isLeo { - result.append("<\(head.item.origin)> ") + if !head.isLeo { + result.append("<\(head.origin)> ") } if head.mainstemIndex != nil { result.append( "{\(itemDerivations.map { String($0.mainstemIndex!) }.joined(separator: ", "))} ") } - if head.item.isLeo { - result += "L(\(head.item.memoizedPenultIndex!)) •\(head.item.transitionSymbol!)" + if head.isLeo { + result += "L(\(head.memoizedPenultIndex!)) •\(head.transitionSymbol!)" } else { - result += language.dottedText(head.item.dotPosition) + result += language.dottedText(head.dotPosition) } result += "\n" remainingDerivations.removeFirst(itemDerivations.count)