-
Notifications
You must be signed in to change notification settings - Fork 3
/
verlexer.go
148 lines (131 loc) · 2.44 KB
/
verlexer.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//go:generate go tool yacc -p ver -o verparser.go ver.go.y
//
package dpkg
import (
"fmt"
"github.com/bbuck/go-lexer"
"strings"
)
type MM struct {
*lexer.L
info DepInfo
str string
}
const (
_DIGIT = "0123456789"
_ALPHA = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
_ALPHANUM = _DIGIT + _ALPHA
_PKGNAMESPECS = "+-~."
_PKGNAME = _ALPHANUM + _PKGNAMESPECS
)
func lexPkgName(l *lexer.L) lexer.StateFunc {
ignoreSpace(l)
if !takeAny(l, _PKGNAME) {
return nil
}
l.Emit(lexer.TokenType(PKGNAME))
return lexArchQualifer
}
func lexArchQualifer(l *lexer.L) lexer.StateFunc {
ignoreSpace(l)
if l.Peek() == ':' {
l.Next()
l.Ignore()
if !takeAny(l, _ALPHANUM) {
return nil
}
l.Emit(lexer.TokenType(ARCH_QUALIFIER))
}
return lexPkgOthers
}
func lexPkgOthers(l *lexer.L) lexer.StateFunc {
ignoreSpace(l)
switch l.Peek() {
case '[':
if takeMatch(l, ARCH_SPEC, '[', ']') {
return lexPkgOthers
}
case '(':
if takeMatch(l, VERSION, '(', ')') {
return lexPkgOthers
}
case '<':
if takeMatch(l, PROFILE, '<', '>') {
return lexPkgOthers
}
case '|':
l.Next()
l.Emit(lexer.TokenType('|'))
return lexPkgName
case ',':
l.Next()
l.Emit(lexer.TokenType(','))
return lexPkgName
}
return nil
}
func ParseDepInfo(str string) (*DepInfo, error) {
if str == "" {
return nil, nil
}
m := &MM{
L: lexer.New(str, lexPkgName),
str: str,
}
m.Start()
verNewParser().Parse(m)
if m.Err != nil {
return nil, fmt.Errorf("Parsing %q failed: %v", str, m.Err)
}
return &m.info, nil
}
func saveResult(l verLexer, info DepInfo) {
m := l.(*MM)
m.info = info
}
func (m MM) Error(err string) {
fmt.Printf("EEEE %q --> %s\n", m.str, err)
m.Err = fmt.Errorf(err)
}
func (m MM) Lex(lval *verSymType) int {
tok, done := m.NextToken()
// fmt.Printf("%q TOKEN: %v %v\n", m.str, tok, done)
if done {
return 0
} else {
lval.val = tok.Value
return int(tok.Type)
}
}
func ignoreSpace(l *lexer.L) {
l.Take(" \n\t")
l.Ignore()
}
func takeAny(l *lexer.L, strs string) bool {
r := l.Next()
found := false
for strings.ContainsRune(strs, r) {
r = l.Next()
found = true
}
l.Rewind()
return found
}
func takeMatch(l *lexer.L, tok int, left rune, right rune) bool {
c := l.Peek()
if c != left {
return false
}
l.Next()
l.Ignore()
for i := 0; i < 1000; i++ {
if l.Peek() == right {
l.Emit(lexer.TokenType(tok))
l.Next()
l.Ignore()
return true
}
l.Next()
}
return false
}