-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlexer.go
62 lines (53 loc) · 1.02 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"fmt"
"strings"
"text/scanner"
"unicode"
"github.com/quasilyte/parsing-in-go/phpdoc"
)
type PhpdocLex struct {
s scanner.Scanner
result phpdoc.Type
}
func NewLexer() *PhpdocLex {
lexer := &PhpdocLex{}
lexer.s.IsIdentRune = func(ch rune, i int) bool {
return unicode.IsLetter(ch) ||
(unicode.IsDigit(ch) && i > 0) ||
ch == '\\'
}
return lexer
}
func (l *PhpdocLex) Init(s string) {
l.s.Init(strings.NewReader(s))
}
var nameToToken = map[string]rune{
"int": T_INT,
"float": T_FLOAT,
"null": T_NULL,
"string": T_STRING,
"false": T_FALSE,
"bool": T_BOOL,
}
func (l *PhpdocLex) nextToken(sym *PhpdocSymType) rune {
tok := l.s.Scan()
if tok == scanner.Ident {
text := l.s.TokenText()
tok, ok := nameToToken[text]
if ok {
return tok
}
sym.text = text
return T_NAME
}
return tok
}
func (l *PhpdocLex) Lex(sym *PhpdocSymType) int {
tok := l.nextToken(sym)
sym.tok = tok
return int(tok)
}
func (l *PhpdocLex) Error(s string) {
fmt.Printf("syntax error: %s\n", s)
}