-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathlexeme.go
77 lines (65 loc) · 1.27 KB
/
lexeme.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
package lingo
import (
"fmt"
"unicode"
)
//go:generate stringer -type=LexemeType
type LexemeType byte
const (
EOF LexemeType = iota
Word
Disambig
URI
Number
Date
Time
Punctuation
Symbol
Space
SystemUse
)
type Lexeme struct {
Value string
LexemeType LexemeType
Line int
Col int
Pos int
}
func MakeLexeme(s string, t LexemeType) Lexeme {
return Lexeme{
Value: s,
LexemeType: t,
Line: -1,
Col: -1,
Pos: -1,
}
}
func (l Lexeme) Fix() Lexeme {
if StringIs(l.Value, unicode.IsDigit) {
l.LexemeType = Number
return l
}
return l
}
func (l Lexeme) String() string {
switch l.LexemeType {
case EOF:
return "EOF"
default:
return fmt.Sprintf("%q/%v", l.Value, l.LexemeType)
}
}
func (l Lexeme) GoString() string {
switch l.LexemeType {
case EOF:
return fmt.Sprintf("EOF: %q (%d, %d, %d)", l.Value, l.Line, l.Col, l.Pos)
default:
return fmt.Sprintf("%s: %q (%d, %d, %d)", l.LexemeType, l.Value, l.Line, l.Col, l.Pos)
}
}
var startLexeme = MakeLexeme("START_LEXEME", SystemUse)
var rootLexeme = MakeLexeme("-ROOT-", SystemUse)
var nullLexeme = MakeLexeme("", SystemUse)
func StartLexeme() Lexeme { return startLexeme }
func RootLexeme() Lexeme { return rootLexeme }
func NullLexeme() Lexeme { return nullLexeme }