1
0
Fork 0

Make skipping spaces more simlpe

This commit is contained in:
Gregory Eremin 2015-02-18 00:12:23 +07:00
parent bfdb311495
commit 72f1734218
1 changed files with 22 additions and 26 deletions

View File

@ -14,8 +14,10 @@ type (
Lexer struct {
input string // The string being scanned
lineNum int // Line number
colNum int // Column number
pos int // Current position in the input
start int // Start position of this item
startCol int // Start column of this item
width int // Width of last rune read from input
items chan Item // Channel of scanned items
}
@ -60,6 +62,8 @@ func New(input string) *Lexer {
return &Lexer{
input: input,
items: make(chan Item),
lineNum: 1,
colNum: 0,
}
}
@ -85,6 +89,15 @@ func (l *Lexer) next() rune {
r, w := utf8.DecodeRuneInString(l.input[l.pos:])
l.width = w
l.pos += l.width
// Counting lines and columns - token coordinates
if r == '\n' {
l.lineNum++
l.colNum = 0
} else {
l.colNum++
}
return r
}
@ -150,10 +163,7 @@ func (l *Lexer) errorf(format string, args ...interface{}) stateFn {
func lexInitial(l *Lexer) stateFn {
for {
switch r := l.next(); r {
case ' ', '\t':
return lexSpace(l)
case '\n':
l.lineNum++
case ' ', '\t', '\n':
l.ignore()
case 'n':
l.backup()
@ -187,18 +197,6 @@ func lexInitial(l *Lexer) stateFn {
}
}
// Skips all spaces in the input until a visible character is found
func lexSpace(l *Lexer) stateFn {
for {
if r := l.next(); r != ' ' && r != '\t' {
l.backup()
break
}
}
l.ignore()
return lexInitial
}
func lexNull(l *Lexer) stateFn {
if l.acceptString("null") {
l.emit(Null)
@ -255,8 +253,6 @@ func lexString(l *Lexer) stateFn {
l.ignore()
return lexInitial
}
case '\n':
l.lineNum++
case 0:
return l.errorf("Unterminated string")
default: