-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathlexer.go
186 lines (159 loc) · 3.37 KB
/
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Package lexer contains a simple lexer for reading an input-string
// and converting it into a series of tokens.
package lexer
import (
"errors"
"fmt"
"github.com/skx/deployr/token"
)
// Lexer is used as the lexer for our deployr "language".
type Lexer struct {
position int //current character position
readPosition int //next character position
ch rune //current character
characters []rune //rune slice of input string
}
// New a Lexer instance from string input.
func New(input string) *Lexer {
l := &Lexer{characters: []rune(input)}
l.readChar()
return l
}
// Dump outputs the complete stream of tokens from the lexer,
// consuming all input as it does so.
func (l *Lexer) Dump() {
for {
tok := l.NextToken()
fmt.Printf("%v\n", tok)
if tok.Type == "EOF" {
break
}
}
}
// read one forward character
func (l *Lexer) readChar() {
if l.readPosition >= len(l.characters) {
l.ch = rune(0)
} else {
l.ch = l.characters[l.readPosition]
}
l.position = l.readPosition
l.readPosition++
}
// NextToken to read next token, skipping the white space.
func (l *Lexer) NextToken() token.Token {
var tok token.Token
l.skipWhitespace()
// skip shebang
if l.ch == rune('#') && l.peekChar() == rune('!') && l.position == 0 {
l.skipComment()
return (l.NextToken())
}
// skip single-line comments
if l.ch == rune('#') {
l.skipComment()
return (l.NextToken())
}
switch l.ch {
case rune('"'):
str, err := l.readString()
if err == nil {
tok.Type = token.STRING
tok.Literal = str
} else {
tok.Type = token.ILLEGAL
tok.Literal = err.Error()
}
case rune(0):
tok.Literal = ""
tok.Type = token.EOF
default:
tok.Literal = l.readIdentifier()
tok.Type = token.LookupIdentifier(tok.Literal)
return tok
}
l.readChar()
return tok
}
// read Identifier
func (l *Lexer) readIdentifier() string {
position := l.position
for isIdentifier(l.ch) {
l.readChar()
}
return string(l.characters[position:l.position])
}
// skip white space
func (l *Lexer) skipWhitespace() {
for isWhitespace(l.ch) {
l.readChar()
}
}
// skip comment (until the end of the line).
func (l *Lexer) skipComment() {
for l.ch != '\n' && l.ch != rune(0) {
l.readChar()
}
l.skipWhitespace()
}
// read string
func (l *Lexer) readString() (string, error) {
out := ""
for {
l.readChar()
if l.ch == '"' {
break
}
if l.ch == rune(0) {
return "", errors.New("unterminated string")
}
//
// Handle \n, \r, \t, \", etc.
//
if l.ch == '\\' {
// Line ending with "\" + newline
if l.peekChar() == '\n' {
// consume the newline.
l.readChar()
continue
}
l.readChar()
if l.ch == rune('n') {
l.ch = '\n'
}
if l.ch == rune('r') {
l.ch = '\r'
}
if l.ch == rune('t') {
l.ch = '\t'
}
if l.ch == rune('"') {
l.ch = '"'
}
if l.ch == rune('\\') {
l.ch = '\\'
}
}
out = out + string(l.ch)
}
return out, nil
}
// peek character
func (l *Lexer) peekChar() rune {
if l.readPosition >= len(l.characters) {
return rune(0)
}
return l.characters[l.readPosition]
}
// determinate ch is identifier or not
func isIdentifier(ch rune) bool {
return !isWhitespace(ch) && !isEmpty(ch)
}
// is white space
func isWhitespace(ch rune) bool {
return ch == rune(' ') || ch == rune('\t') || ch == rune('\n') || ch == rune('\r')
}
// is empty
func isEmpty(ch rune) bool {
return rune(0) == ch
}