Decaffeinate src/token-iterator.coffee

This commit is contained in:
Jason Rudolph
2017-11-03 07:44:06 -04:00
parent a3bb0bbb44
commit 1ee1c6c30e
2 changed files with 79 additions and 56 deletions

View File

@@ -1,56 +0,0 @@
module.exports =
class TokenIterator
constructor: (@tokenizedBuffer) ->
reset: (@line) ->
@index = null
@startColumn = 0
@endColumn = 0
@scopes = @line.openScopes.map (id) => @tokenizedBuffer.grammar.scopeForId(id)
@scopeStarts = @scopes.slice()
@scopeEnds = []
this
next: ->
{tags} = @line
if @index?
@startColumn = @endColumn
@scopeEnds.length = 0
@scopeStarts.length = 0
@index++
else
@index = 0
while @index < tags.length
tag = tags[@index]
if tag < 0
scope = @tokenizedBuffer.grammar.scopeForId(tag)
if tag % 2 is 0
if @scopeStarts[@scopeStarts.length - 1] is scope
@scopeStarts.pop()
else
@scopeEnds.push(scope)
@scopes.pop()
else
@scopeStarts.push(scope)
@scopes.push(scope)
@index++
else
@endColumn += tag
@text = @line.text.substring(@startColumn, @endColumn)
return true
false
getScopes: -> @scopes
getScopeStarts: -> @scopeStarts
getScopeEnds: -> @scopeEnds
getText: -> @text
getBufferStart: -> @startColumn
getBufferEnd: -> @endColumn

79
src/token-iterator.js Normal file
View File

@@ -0,0 +1,79 @@
module.exports =
class TokenIterator {
constructor (tokenizedBuffer) {
this.tokenizedBuffer = tokenizedBuffer
}
reset (line) {
this.line = line
this.index = null
this.startColumn = 0
this.endColumn = 0
this.scopes = this.line.openScopes.map(id => this.tokenizedBuffer.grammar.scopeForId(id))
this.scopeStarts = this.scopes.slice()
this.scopeEnds = []
return this
}
next () {
const {tags} = this.line
if (this.index != null) {
this.startColumn = this.endColumn
this.scopeEnds.length = 0
this.scopeStarts.length = 0
this.index++
} else {
this.index = 0
}
while (this.index < tags.length) {
const tag = tags[this.index]
if (tag < 0) {
const scope = this.tokenizedBuffer.grammar.scopeForId(tag)
if ((tag % 2) === 0) {
if (this.scopeStarts[this.scopeStarts.length - 1] === scope) {
this.scopeStarts.pop()
} else {
this.scopeEnds.push(scope)
}
this.scopes.pop()
} else {
this.scopeStarts.push(scope)
this.scopes.push(scope)
}
this.index++
} else {
this.endColumn += tag
this.text = this.line.text.substring(this.startColumn, this.endColumn)
return true
}
}
return false
}
getScopes () {
return this.scopes
}
getScopeStarts () {
return this.scopeStarts
}
getScopeEnds () {
return this.scopeEnds
}
getText () {
return this.text
}
getBufferStart () {
return this.startColumn
}
getBufferEnd () {
return this.endColumn
}
}