/
token.go
64 lines (49 loc) · 1.29 KB
/
token.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
// Copyright (c) 2020, Cogent Core. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Copied and only lightly modified from:
// https://github.com/nickng/bibtex
// Licensed under an Apache-2.0 license
// and presumably Copyright (c) 2017 by Nick Ng
package bibtex
import (
"fmt"
"strings"
)
// Lexer token.
type Token int
const (
// ILLEGAL stands for an invalid token.
ILLEGAL Token = iota
)
var eof = rune(0)
// TokenPos is a pair of coordinate to identify start of token.
type TokenPos struct {
Char int
Lines []int
}
func (p TokenPos) String() string {
return fmt.Sprintf("%d:%d", len(p.Lines)+1, p.Char)
}
func isWhitespace(ch rune) bool {
return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r'
}
func isAlpha(ch rune) bool {
return ('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z')
}
func isDigit(ch rune) bool {
return ('0' <= ch && ch <= '9')
}
func isAlphanum(ch rune) bool {
return isAlpha(ch) || isDigit(ch)
}
func isBareSymbol(ch rune) bool {
return strings.ContainsRune("-_:./+", ch)
}
// isSymbol returns true if ch is a valid symbol
func isSymbol(ch rune) bool {
return strings.ContainsRune("!?&*+-./:;<>[]^_`|~@", ch)
}
func isOpenQuote(ch rune) bool {
return ch == '{' || ch == '"'
}