stringscanner
Version:
Stringscanner performs lexical scanning operations on a string.
131 lines (100 loc) • 2.93 kB
text/coffeescript
class StringScanner
constructor: ( = '') ->
= '' +
= 0
= {
reset: ->
= null
= []
this
}.reset()
this
bol: -> <=0 || (this.str[this.pos-1] is "\n")
captures: -> .captures
check: (pattern) ->
if .substr( ).search(pattern) isnt 0
.reset()
return null
matches = .substr( ).match pattern
.str = matches[0]
.captures = matches.slice 1
this.lastMatch.str
checkUntil: (pattern) ->
patternPos = .substr( ).search pattern
if patternPos < 0
.reset()
return null
matches = .substr( +patternPos).match pattern
.captures = matches.slice 1
.str = .substr( ,patternPos) + matches[0]
clone: ->
clone = new
clone.pos =
clone.lastMatch = {}
clone.lastMatch[prop] = value for prop, value of
clone
concat: (str) ->
+= str
this
eos: -> is .length
exists: (pattern) ->
patternPos = .substr( ).search pattern
if patternPos < 0
.reset()
return null
matches = .substr( +patternPos).match pattern
.str = matches[0]
.captures = matches.slice 1
patternPos
getch: -> /./
# ruby equivalent: matched
match: -> .str
# ruby equivalent: match?
matches: (pattern) ->
pattern
# ruby equivalent: matched?
matched: -> .str?
matchSize: -> if then this.match().length else null
peek: (len) -> .substr , len
pointer: ->
setPointer: (pos) ->
pos = +pos
pos = 0 if pos < 0
pos = .length if pos > .length
= pos
reset: ->
.reset()
= 0
this
rest: -> .substr
scan: (pattern) ->
chk = pattern
this.pos += chk.length if chk?
chk
scanUntil: (pattern) ->
chk = pattern
this.pos += chk.length if chk?
chk
skip: (pattern) ->
pattern
skipUntil: (pattern) ->
pattern
string: ->
terminate: ->
= .length
.reset()
this
toString: ->
"#<StringScanner #{if @eos() then 'fin' else "#{ }/#{ .length} @ #{if .length>8 then "#{@str.substr(0,5)}..." else }"}>"
StringScanner::beginningOfLine = StringScanner::bol
StringScanner::clear = StringScanner::terminate
StringScanner::dup = StringScanner::clone
StringScanner::endOfString = StringScanner::eos
StringScanner::exist = StringScanner::exists
StringScanner::getChar = StringScanner::getch
StringScanner::position = StringScanner::pointer
StringScanner.StringScanner = StringScanner
module.exports = StringScanner