user17894590
user17894590

Reputation:

text/scanner hangs with IsIdentRune

If I have this code:

package main

import (
   "strings"
   "text/scanner"
)

func main() {
   src := strings.NewReader("hello\nworld\n")
   var s scanner.Scanner
   s.Init(src)
   s.IsIdentRune = func(ch rune, i int) bool {
      return ch != '\n'
   }
   s.Whitespace = 1<<'\n'
   for s.Scan() != scanner.EOF {
      println(s.TokenText())
   }
}

The program will print the two lines, but then it just hangs forever. I see this in the docs:

The set of valid characters must not intersect with the set of white space characters.

but it seems that I am following the direction given. What am I doing wrong?

  1. https://godocs.io/text/scanner#Scanner.IsIdentRune

Upvotes: 0

Views: 63

Answers (1)

Thundercat
Thundercat

Reputation: 121129

IsIdentRune is called with scanner.EOF at the end of the input. Do not accept scanner.EOF as an identifier rune.

s.IsIdentRune = func(ch rune, i int) bool {
    return ch != scanner.EOF && ch != '\n'
}

Upvotes: 0

Related Questions