-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenize-scientific-float.go
47 lines (44 loc) · 1.34 KB
/
tokenize-scientific-float.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
package lexer
import "errors"
var (
ScientificFloatInvalidSyntax = errors.New("invalid scientific float syntax")
)
func (lexer *Lexer) tokenizeScientificFloat() error {
if !lexer.reader.HasNext() {
lexer.currentToken.Kind = Literal
lexer.currentToken.DirectValue = InvalidDirectValue
return ScientificFloatInvalidSyntax
}
direction := lexer.reader.Char()
if (direction != '-') && (direction != '+') {
lexer.currentToken.Kind = Literal
lexer.currentToken.DirectValue = InvalidDirectValue
return ScientificFloatInvalidSyntax
}
lexer.reader.Next()
// Ensure next is a number
if !lexer.reader.HasNext() {
lexer.currentToken.Kind = Literal
lexer.currentToken.DirectValue = InvalidDirectValue
return ScientificFloatInvalidSyntax
}
lexer.currentToken.append(direction)
nextDigit := lexer.reader.Char()
if !('0' <= nextDigit && nextDigit <= '9') {
lexer.currentToken.Kind = Literal
lexer.currentToken.DirectValue = InvalidDirectValue
return ScientificFloatInvalidSyntax
}
lexer.currentToken.append(nextDigit)
lexer.reader.Next()
for ; lexer.reader.HasNext(); lexer.reader.Next() {
nextDigit = lexer.reader.Char()
if !('0' <= nextDigit && nextDigit <= '9') && nextDigit != '_' {
break
}
lexer.currentToken.append(nextDigit)
}
lexer.currentToken.Kind = Literal
lexer.currentToken.DirectValue = ScientificFloat
return nil
}