Skip to content

Commit

Permalink
Rename Chart.Item->ItemID
Browse files Browse the repository at this point in the history
Progress on #15
  • Loading branch information
dabrahams committed Dec 30, 2022
1 parent c71a5d9 commit 6133906
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
22 changes: 11 additions & 11 deletions Sources/Lotsawa/Chart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ extension Chart {

extension Chart {
/// An Earley or Leo item.
struct Item: Comparable, Hashable {
struct ItemID: Comparable, Hashable {
/// The raw storage.
///
/// It is arranged to avoid 64-bit alignment, since this will be combined into an `Entry`.
Expand Down Expand Up @@ -220,7 +220,7 @@ extension Chart {
/// Returns `self` with the dot advanced over one symbol.
///
/// - Precondition: `self` is an incomplete Earley item,
func advanced<S>(in g: Grammar<S>) -> Item {
func advanced<S>(in g: Grammar<S>) -> ItemID {
assert(isEarley)
assert(!isCompletion)

Expand All @@ -243,7 +243,7 @@ extension Chart {
/// Returns `self` with the dot moved back over one symbol.
///
/// - Precondition: `self` is a non-prediction Earley item.
func mainstem<S>(in g: Grammar<S>) -> Item {
func mainstem<S>(in g: Grammar<S>) -> ItemID {
assert(isEarley)

var r = self
Expand All @@ -262,7 +262,7 @@ extension Chart {

/// If `self` is a Leo item, returns the Earley item it memoizes, assuming `g` is the grammar
/// being parsed; returns `nil` otherwise.
func leoMemo<S>(in g: Grammar<S>) -> Item? {
func leoMemo<S>(in g: Grammar<S>) -> ItemID? {
if isEarley { return nil }
var r = self
r.symbolID = Symbol.ID(g.ruleStore[Int(dotPosition)])
Expand All @@ -274,7 +274,7 @@ extension Chart {

/// A Leo or Earley item bundled with a single mainstem cause.
public struct Entry: Comparable, Hashable {
var item: Item
var item: ItemID

/// The chart position where derivations of this entry's mainstem start, if any.
var mainstemIndex: Entries.Index? {
Expand All @@ -285,7 +285,7 @@ extension Chart {
/// Creates an instance with the given properties
///
/// - Precondition: `0 <= mainstemIndex && mainstemIndex <= UInt.max`
init(item: Item, mainstemIndex: Entries.Index?) {
init(item: ItemID, mainstemIndex: Entries.Index?) {
self.item = item
self.mainstemIndexStorage = 0 // About to be overridden
self.mainstemIndex = mainstemIndex
Expand Down Expand Up @@ -320,15 +320,15 @@ extension Chart {
func transitionEntries(on s: Symbol, inEarleySet i: UInt32) -> Entries.SubSequence
{
let ithSet = i == currentEarleme ? currentEarleySet : earleySet(i)
let k = Item.transitionKey(s)
let k = ItemID.transitionKey(s)

let j = ithSet.partitionPoint { d in d.item.transitionKey >= k }
let items = ithSet[j...]
return items.prefix(while: { x in x.item.symbolID == s.id })
}

/// Returns the items in Earley set `i` whose use is triggered by the recognition of `s`.
func transitionItems(on s: Symbol, inEarleySet i: UInt32) -> some BidirectionalCollection<Item>
func transitionItems(on s: Symbol, inEarleySet i: UInt32) -> some BidirectionalCollection<ItemID>
{
transitionEntries(on: s, inEarleySet: i).lazy.map(\.item).droppingAdjacentDuplicates()
}
Expand All @@ -339,7 +339,7 @@ extension Chart {
func completions(of lhs: Symbol, over extent: Range<SourcePosition>) -> Entries.SubSequence
{
let ithSet = earleySet(extent.upperBound)
let k = Item.completionKey(lhs, origin: extent.lowerBound)
let k = ItemID.completionKey(lhs, origin: extent.lowerBound)

let j = ithSet.partitionPoint { d in d.item.key >= k }
let r0 = ithSet[j...]
Expand All @@ -348,7 +348,7 @@ extension Chart {
}

/// Given an item `x`, found in earley set `i`, returns the chart positions of its mainstem items.
func mainstemIndices(of x: Item, inEarleySet i: UInt32)
func mainstemIndices(of x: ItemID, inEarleySet i: UInt32)
-> some BidirectionalCollection<Entries.Index>
{
let ithSet = earleySet(i)
Expand Down Expand Up @@ -428,7 +428,7 @@ extension DebuggableProductType {
}
}

extension Chart.Item: DebuggableProductType {
extension Chart.ItemID: DebuggableProductType {
enum Kind { case completion, mainstem, leo }
var reflectedChildren: KeyValuePairs<String, Any> {
[
Expand Down
2 changes: 1 addition & 1 deletion Sources/Lotsawa/Recognizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ extension Recognizer {
}
}

func leoPredecessorIndex(_ x: Chart.Item) -> Chart.Entries.Index? {
func leoPredecessorIndex(_ x: Chart.ItemID) -> Chart.Entries.Index? {
assert(g.recognized(at: x.dotPosition) == nil, "unexpectedly complete item")
let s = g.recognized(at: x.dotPosition + 1)!

Expand Down
2 changes: 1 addition & 1 deletion Tests/LotsawaTests/ChartInternalTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ChartInternalTests: XCTestCase {
"""
.asTestGrammar(recognizing: "A")

let i0 = Chart.Item(predicting: g.raw.ruleIDs.first!, in: g.raw, at: 42)
let i0 = Chart.ItemID(predicting: g.raw.ruleIDs.first!, in: g.raw, at: 42)
XCTAssert(i0.isEarley)
XCTAssertFalse(i0.isLeo)
XCTAssertEqual(i0.origin, 42)
Expand Down

0 comments on commit 6133906

Please sign in to comment.