Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions experimental/token/cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func NewCursorAt(tok Token) *Cursor {

return &Cursor{
withContext: tok.withContext,
idx: int(tok.ID() - 1), // Convert to 0-based index.
idx: tok.ID().naturalIndex(), // Convert to 0-based index.
}
}

Expand Down Expand Up @@ -178,7 +178,7 @@ func (c *Cursor) PrevSkippable() Token {
if c.IsSynthetic() {
c.idx--
} else {
c.idx = int(tok.ID() - 1)
c.idx = tok.ID().naturalIndex()
}
return tok
}
Expand Down
29 changes: 27 additions & 2 deletions experimental/token/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,35 @@ func (t ID) String() string {
return "Token(<nil>)"
}
if t < 0 {
return fmt.Sprintf("Token(^%d)", ^int(t))
return fmt.Sprintf("Token(%d)", t.syntheticIndex())
}
return fmt.Sprintf("Token(%d)", t.naturalIndex())
}

// naturalIndex returns the index of this token in the natural stream.
func (t ID) naturalIndex() int {
if t.IsZero() {
panic("protocompile/token: called naturalIndex on zero token")
}
if t < 0 {
panic("protocompile/token: called naturalIndex on synthetic token")
}
// Need to subtract off one, because the zeroth
// ID is used as a "missing" sentinel.
return int(t) - 1
}

return fmt.Sprintf("Token(%d)", int(t)-1)
// syntheticIndex returns the index of this token in the synthetic stream.
func (t ID) syntheticIndex() int {
if t.IsZero() {
panic("protocompile/token: called syntheticIndex on zero token")
}
if t > 0 {
panic("protocompile/token: called syntheticIndex on natural token")
}
// Need to invert the bits, because synthetic tokens are
// stored as negative numbers.
return ^int(t)
}

// Constants for extracting the parts of tokenImpl.kindAndOffset.
Expand Down
8 changes: 3 additions & 5 deletions experimental/token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ func (t Token) Children() *Cursor {
start, _ := t.StartEnd()
return &Cursor{
withContext: t.withContext,
idx: int(start.id), // Skip the start!
idx: start.id.naturalIndex() + 1, // Skip the start!
}
}

Expand Down Expand Up @@ -497,14 +497,12 @@ func (t Token) nat() *nat {
if t.IsSynthetic() {
return nil
}
// Need to subtract off one, because the zeroth
// ID is used as a "missing" sentinel.
return &t.Context().Stream().nats[t.id-1]
return &t.Context().Stream().nats[t.id.naturalIndex()]
}

func (t Token) synth() *synth {
if !t.IsSynthetic() {
return nil
}
return &t.Context().Stream().synths[^t.id]
return &t.Context().Stream().synths[t.id.syntheticIndex()]
}
Loading