Speeding up the code some more. Big step was made by simplifying the cursor, continuing with that in the next commit.
This commit is contained in:
parent
7116aa47df
commit
09746c0d2e
|
@ -215,10 +215,14 @@ func (i *API) Fork() int {
|
||||||
|
|
||||||
parent := i.stackFrame
|
parent := i.stackFrame
|
||||||
|
|
||||||
i.stackFrame = &i.stackFrames[i.stackLevel]
|
f := &i.stackFrames[i.stackLevel]
|
||||||
*i.stackFrame = *parent
|
f.offset = parent.offset
|
||||||
i.stackFrame.runeStart = parent.runeEnd
|
f.cursor = parent.cursor
|
||||||
i.stackFrame.tokenStart = parent.tokenEnd
|
f.runeStart = parent.runeEnd
|
||||||
|
f.runeEnd = parent.runeEnd
|
||||||
|
f.tokenStart = parent.tokenEnd
|
||||||
|
f.tokenEnd = parent.tokenEnd
|
||||||
|
i.stackFrame = f
|
||||||
|
|
||||||
return i.stackLevel
|
return i.stackLevel
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,13 +2,10 @@ package tokenize
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"unicode/utf8"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Cursor represents the position of a cursor in various ways.
|
// Cursor represents the position of a cursor in various ways.
|
||||||
type Cursor struct {
|
type Cursor struct {
|
||||||
Byte int // The cursor offset in bytes
|
|
||||||
Rune int // The cursor offset in UTF8 runes
|
|
||||||
Column int // The column at which the cursor is (0-indexed)
|
Column int // The column at which the cursor is (0-indexed)
|
||||||
Line int // The line at which the cursor is (0-indexed)
|
Line int // The line at which the cursor is (0-indexed)
|
||||||
}
|
}
|
||||||
|
@ -33,8 +30,6 @@ func (c *Cursor) move(input string) *Cursor {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Cursor) moveByRune(r rune) *Cursor {
|
func (c *Cursor) moveByRune(r rune) *Cursor {
|
||||||
c.Byte += utf8.RuneLen(r)
|
|
||||||
c.Rune++
|
|
||||||
if r == '\n' {
|
if r == '\n' {
|
||||||
c.Column = 0
|
c.Column = 0
|
||||||
c.Line++
|
c.Line++
|
||||||
|
|
|
@ -53,12 +53,6 @@ func TestGivenCursor_WhenMoving_CursorIsUpdated(t *testing.T) {
|
||||||
for _, s := range test.input {
|
for _, s := range test.input {
|
||||||
c.move(s)
|
c.move(s)
|
||||||
}
|
}
|
||||||
if c.Byte != test.byte {
|
|
||||||
t.Errorf("[%s] Unexpected byte offset %d (expected %d)", test.name, c.Byte, test.byte)
|
|
||||||
}
|
|
||||||
if c.Rune != test.rune {
|
|
||||||
t.Errorf("[%s] Unexpected rune offset %d (expected %d)", test.name, c.Rune, test.rune)
|
|
||||||
}
|
|
||||||
if c.Line != test.line {
|
if c.Line != test.line {
|
||||||
t.Errorf("[%s] Unexpected line offset %d (expected %d)", test.name, c.Line, test.line)
|
t.Errorf("[%s] Unexpected line offset %d (expected %d)", test.name, c.Line, test.line)
|
||||||
}
|
}
|
||||||
|
|
|
@ -129,7 +129,7 @@ func TestAtoms(t *testing.T) {
|
||||||
{"bb", a.RuneRange('b', 'e'), true, "b"},
|
{"bb", a.RuneRange('b', 'e'), true, "b"},
|
||||||
{"cc", a.RuneRange('b', 'e'), true, "c"},
|
{"cc", a.RuneRange('b', 'e'), true, "c"},
|
||||||
{"", a.EndOfFile, true, ""},
|
{"", a.EndOfFile, true, ""},
|
||||||
{"⌘", a.AnyRune, true, "⌘"},
|
{"😂", a.AnyRune, true, "😂"},
|
||||||
{"\xbc with AnyRune", a.AnyRune, true, "<22>"},
|
{"\xbc with AnyRune", a.AnyRune, true, "<22>"},
|
||||||
{"", a.AnyRune, false, ""},
|
{"", a.AnyRune, false, ""},
|
||||||
{"⌘", a.ValidRune, true, "⌘"},
|
{"⌘", a.ValidRune, true, "⌘"},
|
||||||
|
|
|
@ -12,9 +12,7 @@ func TestFork_CreatesForkOfInputAtSameCursorPosition(t *testing.T) {
|
||||||
AssertEqual(t, "T", i.String(), "accepted rune in input")
|
AssertEqual(t, "T", i.String(), "accepted rune in input")
|
||||||
// Fork
|
// Fork
|
||||||
child := i.Fork()
|
child := i.Fork()
|
||||||
AssertEqual(t, 1, i.stackFrame.cursor.Byte, "parent cursor.Byte")
|
|
||||||
AssertEqual(t, 1, i.stackFrame.offset, "parent offset")
|
AssertEqual(t, 1, i.stackFrame.offset, "parent offset")
|
||||||
AssertEqual(t, 1, i.stackFrame.cursor.Byte, "child cursor.Byte")
|
|
||||||
AssertEqual(t, 1, i.stackFrame.offset, "child offset")
|
AssertEqual(t, 1, i.stackFrame.offset, "child offset")
|
||||||
// Accept two runes via fork.
|
// Accept two runes via fork.
|
||||||
i.NextRune()
|
i.NextRune()
|
||||||
|
@ -22,15 +20,12 @@ func TestFork_CreatesForkOfInputAtSameCursorPosition(t *testing.T) {
|
||||||
i.NextRune()
|
i.NextRune()
|
||||||
i.Accept() // s
|
i.Accept() // s
|
||||||
AssertEqual(t, "es", i.String(), "result runes in fork")
|
AssertEqual(t, "es", i.String(), "result runes in fork")
|
||||||
AssertEqual(t, 1, i.stackFrames[i.stackLevel-1].cursor.Byte, "parent cursor.Byte")
|
|
||||||
AssertEqual(t, 1, i.stackFrames[i.stackLevel-1].offset, "parent offset")
|
AssertEqual(t, 1, i.stackFrames[i.stackLevel-1].offset, "parent offset")
|
||||||
AssertEqual(t, 3, i.stackFrame.cursor.Byte, "child cursor.Byte")
|
|
||||||
AssertEqual(t, 3, i.stackFrame.offset, "child offset")
|
AssertEqual(t, 3, i.stackFrame.offset, "child offset")
|
||||||
// Merge fork back into parent
|
// Merge fork back into parent
|
||||||
i.Merge(child)
|
i.Merge(child)
|
||||||
i.Dispose(child)
|
i.Dispose(child)
|
||||||
AssertEqual(t, "Tes", i.String(), "result runes in parent Input after Merge()")
|
AssertEqual(t, "Tes", i.String(), "result runes in parent Input after Merge()")
|
||||||
AssertEqual(t, 3, i.stackFrame.cursor.Byte, "parent cursor.Byte")
|
|
||||||
AssertEqual(t, 3, i.stackFrame.offset, "parent offset")
|
AssertEqual(t, 3, i.stackFrame.offset, "parent offset")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue