refactor: extract shared token package #46
@@ -81,6 +81,17 @@ func (i Iterator[T]) Done() bool {
|
|||||||
return i.index == len(i.items)
|
return i.index == len(i.items)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// While increments the iterator as long as the current item satisfies the
|
||||||
|
// predicate. The first item that does not match is left unconsumed.
|
||||||
|
func (i *Iterator[T]) While(fn func(T) bool) {
|
||||||
|
for !i.Done() {
|
||||||
|
if !fn(i.MustGet()) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
i.Forward()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Try attempts to perform an operation using the iterator. If the operation
|
// Try attempts to perform an operation using the iterator. If the operation
|
||||||
// succeeds, the iterator is updated. If the operation fails, the iterator is
|
// succeeds, the iterator is updated. If the operation fails, the iterator is
|
||||||
// rolled back, and an error is returned.
|
// rolled back, and an error is returned.
|
||||||
|
|||||||
@@ -47,18 +47,7 @@ func scanToken(i *iterator.Iterator[rune]) (*Token, error) {
|
|||||||
return token.New(TokenHardBreak, index), nil
|
return token.New(TokenHardBreak, index), nil
|
||||||
case letter == '#':
|
case letter == '#':
|
||||||
// Skip everything until the next newline or EOF.
|
// Skip everything until the next newline or EOF.
|
||||||
for !i.Done() {
|
i.While(func(r rune) bool { return r != '\n' })
|
||||||
r, err := i.Next()
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("error while parsing comment: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if r == '\n' {
|
|
||||||
// Put the newline back so it can be processed as a soft break.
|
|
||||||
i.Back()
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil, nil
|
return nil, nil
|
||||||
case unicode.IsSpace(letter):
|
case unicode.IsSpace(letter):
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
|||||||
Reference in New Issue
Block a user