Пример #1
0
// New returns a new scanner lexing from r.
func New(r io.Reader) (Scanner, error) {
	toks, err := lexer.Parse(r)
	if err != nil {
		return nil, err
	}
	return &scanner{toks: toks}, nil
}
Пример #2
0
Файл: ulex.go Проект: mewmew/uc
// lexFileHand lexes the given file and pretty-prints the n first tokens to
// standard output, using the hand-written lexer.
func lexFileHand(path string, n int) (err error) {
	var toks []token.Token
	if path == "-" {
		fmt.Fprintln(os.Stderr, "Lexing from standard input")
		toks, err = lexer.Parse(os.Stdin)
	} else {
		fmt.Fprintf(os.Stderr, "Lexing %q\n", path)
		toks, err = lexer.ParseFile(path)
	}
	if err != nil {
		return errutil.Err(err)
	}

	ntoks := len(toks)
	if n > ntoks {
		ntoks = n
	}
	pad := int(math.Ceil(math.Log10(float64(ntoks))))
	for i, tok := range toks {
		if n != 0 && i == n {
			break
		}
		if tok.Kind == token.Error {
			elog.Printf("ERROR %*d:   %v\n", pad, i, tok)
		} else {
			fmt.Printf("token %*d:   %v\n", pad, i, tok)
		}
	}
	fmt.Fprintln(os.Stderr)
	return nil
}