/
nexus_lexer.go
167 lines (150 loc) · 3.53 KB
/
nexus_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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package nexus
import (
"bufio"
"bytes"
"errors"
"io"
"strconv"
"strings"
treeio "github.com/evolbioinfo/gotree/io"
)
// Scanner represents a lexical scanner.
type Scanner struct {
r *bufio.Reader
}
// NewScanner returns a new instance of Scanner.
func NewScanner(r io.Reader) *Scanner {
return &Scanner{r: bufio.NewReader(r)}
}
// read reads the next rune from the bufferred reader.
// Returns the rune(0) if an error occurs (or io.EOF is returned).
func (s *Scanner) read() rune {
ch, _, err := s.r.ReadRune()
if err != nil {
return eof
}
return ch
}
// unread places the previously read rune back on the reader.
func (s *Scanner) unread() {
_ = s.r.UnreadRune()
}
// Scan returns the next token and literal value.
func (s *Scanner) Scan() (tok Token, lit string) {
// Read the next rune.
ch := s.read()
// If we see whitespace then consume all contiguous whitespace.
// If we see a letter then consume as an ident or reserved word.
if isWhitespace(ch) {
s.unread()
return s.scanWhitespace()
}
if isEndOfLine(ch) {
if isCR(ch) {
ch := s.read()
if isNL(ch) {
return ENDOFLINE, ""
} else {
treeio.LogWarning(errors.New("\\r without \\n detected..."))
}
} else {
return ENDOFLINE, ""
}
}
switch ch {
case eof:
return EOF, ""
case '[':
return OPENBRACK, string(ch)
case ']':
return CLOSEBRACK, string(ch)
case ';':
return ENDOFCOMMAND, string(ch)
case '=':
return EQUAL, string(ch)
case ',':
return COMMA, string(ch)
}
s.unread()
return s.scanIdent()
}
// scanWhitespace consumes the current rune and all contiguous whitespace.
func (s *Scanner) scanWhitespace() (tok Token, lit string) {
// Create a buffer and read the current character into it.
var buf bytes.Buffer
buf.WriteRune(s.read())
// Read every subsequent whitespace character into the buffer.
// Non-whitespace characters and EOF will cause the loop to exit.
for {
if ch := s.read(); ch == eof {
break
} else if !isWhitespace(ch) {
s.unread()
break
} else {
buf.WriteRune(ch)
}
}
return WS, buf.String()
}
// scanIdent consumes the current rune and all contiguous ident runes.
func (s *Scanner) scanIdent() (tok Token, lit string) {
// Create a buffer and read the current character into it.
var buf bytes.Buffer
buf.WriteRune(s.read())
// Read every subsequent ident character into the buffer.
// Non-ident characters and EOF will cause the loop to exit.
for {
if ch := s.read(); ch == eof {
break
} else if !isIdent(ch) {
s.unread()
break
} else {
_, _ = buf.WriteRune(ch)
}
}
_, err := strconv.ParseInt(buf.String(), 10, 64)
if err != nil {
switch strings.ToUpper(buf.String()) {
case "#NEXUS":
return NEXUS, buf.String()
case "BEGIN":
return BEGIN, buf.String()
case "DATA", "CHARACTERS":
return DATA, buf.String()
case "TAXA":
return TAXA, buf.String()
case "TAXLABELS":
return TAXLABELS, buf.String()
case "TREES":
return TREES, buf.String()
case "TREE":
return TREE, buf.String()
case "TRANSLATE":
return TRANSLATE, buf.String()
case "DIMENSIONS":
return DIMENSIONS, buf.String()
case "NTAX":
return NTAX, buf.String()
case "NCHAR":
return NCHAR, buf.String()
case "FORMAT":
return FORMAT, buf.String()
case "DATATYPE":
return DATATYPE, buf.String()
case "MISSING":
return MISSING, buf.String()
case "GAP":
return GAP, buf.String()
case "MATRIX":
return MATRIX, buf.String()
case "END":
return END, buf.String()
default:
return IDENT, buf.String()
}
} else {
return NUMERIC, buf.String()
}
}