-
Notifications
You must be signed in to change notification settings - Fork 8
/
lexer.go
38 lines (32 loc) · 814 Bytes
/
lexer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//go:generate goyacc -p bibtex -o bibtex.y.go bibtex.y
package bibtex
import (
"fmt"
"io"
)
// lexer for bibtex.
type lexer struct {
scanner *scanner
ParseErrors []error // Parse errors from yacc
Errors []error // Other errors
}
// newLexer returns a new yacc-compatible lexer.
func newLexer(r io.Reader) *lexer {
return &lexer{
scanner: newScanner(r),
}
}
// Lex is provided for yacc-compatible parser.
func (l *lexer) Lex(yylval *bibtexSymType) int {
token, strval, err := l.scanner.Scan()
if err != nil {
l.Errors = append(l.Errors, fmt.Errorf("%w at %s", err, l.scanner.pos))
return int(0)
}
yylval.strval = strval
return int(token)
}
// Error handles error.
func (l *lexer) Error(err string) {
l.ParseErrors = append(l.ParseErrors, &ErrParse{Err: err, Pos: l.scanner.pos})
}