Code cleanup.
This commit is contained in:
parent
62cd84bb74
commit
548289560b
|
@ -149,18 +149,18 @@ func (tokenAPI *API) Fork() int {
|
||||||
if cap(frames) < (newStackLevel + 1) {
|
if cap(frames) < (newStackLevel + 1) {
|
||||||
newFrames := make([]stackFrame, cap(frames)*2)
|
newFrames := make([]stackFrame, cap(frames)*2)
|
||||||
copy(newFrames, frames)
|
copy(newFrames, frames)
|
||||||
frames = newFrames
|
tokenAPI.stackFrames = newFrames
|
||||||
}
|
}
|
||||||
|
|
||||||
parent := tokenAPI.stackFrame
|
parent := tokenAPI.stackFrame
|
||||||
frames[newStackLevel] = stackFrame{
|
tokenAPI.stackFrames[newStackLevel] = stackFrame{
|
||||||
offset: parent.offset,
|
offset: parent.offset,
|
||||||
bytesStart: parent.bytesEnd,
|
bytesStart: parent.bytesEnd,
|
||||||
bytesEnd: parent.bytesEnd,
|
bytesEnd: parent.bytesEnd,
|
||||||
tokenStart: parent.tokenEnd,
|
tokenStart: parent.tokenEnd,
|
||||||
tokenEnd: parent.tokenEnd,
|
tokenEnd: parent.tokenEnd,
|
||||||
}
|
}
|
||||||
tokenAPI.stackFrame = &frames[newStackLevel]
|
tokenAPI.stackFrame = &tokenAPI.stackFrames[newStackLevel]
|
||||||
|
|
||||||
return newStackLevel
|
return newStackLevel
|
||||||
}
|
}
|
||||||
|
@ -205,16 +205,19 @@ func (tokenAPI *API) Merge(stackLevel int) {
|
||||||
parent.tokenEnd = f.tokenEnd
|
parent.tokenEnd = f.tokenEnd
|
||||||
f.tokenStart = f.tokenEnd
|
f.tokenStart = f.tokenEnd
|
||||||
|
|
||||||
|
// Update the parent read offset.
|
||||||
parent.offset = f.offset
|
parent.offset = f.offset
|
||||||
|
|
||||||
|
// Update the parent cursor position.
|
||||||
if f.line > parent.line {
|
if f.line > parent.line {
|
||||||
parent.line += f.line
|
parent.line += f.line
|
||||||
parent.column = f.column
|
parent.column = f.column
|
||||||
} else {
|
} else {
|
||||||
parent.column += f.column
|
parent.column += f.column
|
||||||
}
|
}
|
||||||
|
|
||||||
f.line = 0
|
f.line = 0
|
||||||
f.column = 0
|
f.column = 0
|
||||||
|
|
||||||
f.err = nil
|
f.err = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -223,32 +226,19 @@ func (tokenAPI *API) Merge(stackLevel int) {
|
||||||
// cleared as well.
|
// cleared as well.
|
||||||
func (tokenAPI *API) Reset() {
|
func (tokenAPI *API) Reset() {
|
||||||
f := tokenAPI.stackFrame
|
f := tokenAPI.stackFrame
|
||||||
if tokenAPI.stackLevel == 0 {
|
|
||||||
f.column = 0
|
|
||||||
f.line = 0
|
|
||||||
f.offset = 0
|
|
||||||
} else {
|
|
||||||
parent := tokenAPI.stackFrames[tokenAPI.stackLevel-1]
|
|
||||||
f.column = 0
|
|
||||||
f.line = 0
|
|
||||||
f.offset = parent.offset
|
|
||||||
}
|
|
||||||
f.bytesEnd = f.bytesStart
|
f.bytesEnd = f.bytesStart
|
||||||
f.tokenEnd = f.tokenStart
|
f.tokenEnd = f.tokenStart
|
||||||
|
f.column = 0
|
||||||
|
f.line = 0
|
||||||
|
if tokenAPI.stackLevel == 0 {
|
||||||
|
f.offset = 0
|
||||||
|
} else {
|
||||||
|
f.offset = tokenAPI.stackFrames[tokenAPI.stackLevel-1].offset
|
||||||
|
}
|
||||||
f.err = nil
|
f.err = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tokenAPI *API) Dispose(stackLevel int) {
|
func (tokenAPI *API) Dispose(stackLevel int) {
|
||||||
if stackLevel == 0 {
|
|
||||||
callerPanic("Dispose", "tokenize.API.{name}(): {name}() called at {caller} "+
|
|
||||||
"on the top-level API stack level 0")
|
|
||||||
}
|
|
||||||
if stackLevel != tokenAPI.stackLevel {
|
|
||||||
callerPanic("Dispose", "tokenize.API.{name}(): {name}() called at {caller} "+
|
|
||||||
"on API stack level %d, but the current stack level is %d "+
|
|
||||||
"(forgot to Dispose() a forked child?)", stackLevel, tokenAPI.stackLevel)
|
|
||||||
}
|
|
||||||
|
|
||||||
tokenAPI.stackLevel = stackLevel - 1
|
tokenAPI.stackLevel = stackLevel - 1
|
||||||
tokenAPI.stackFrame = &tokenAPI.stackFrames[stackLevel-1]
|
tokenAPI.stackFrame = &tokenAPI.stackFrames[stackLevel-1]
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue