This repository has been archived by the owner on Nov 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 75
/
lexer.go
154 lines (134 loc) · 2.67 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
// Copyright 2017 The ql Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ql
import (
"fmt"
"go/scanner"
"go/token"
"math"
"strconv"
"strings"
"unicode"
"github.com/cznic/golex/lex"
)
const (
ccEOF = iota + 0x80
ccLetter
ccDigit
ccOther
)
func runeClass(r rune) int {
switch {
case r == lex.RuneEOF:
return ccEOF
case r < 0x80:
return int(r)
case unicode.IsLetter(r):
return ccLetter
case unicode.IsDigit(r):
return ccDigit
default:
return ccOther
}
}
type lexer struct {
*lex.Lexer
agg []bool
col int
errs scanner.ErrorList
expr expression
file *token.File
inj int
line int
list []stmt
params int
root bool
sc int
}
func newLexer(src string) (*lexer, error) {
fset := token.NewFileSet()
file := fset.AddFile("", -1, len(src))
l := &lexer{
file: file,
}
l0, err := lex.New(
file,
strings.NewReader(src),
lex.ErrorFunc(func(pos token.Pos, msg string) {
l.errPos(pos, msg)
}),
lex.RuneClass(runeClass),
lex.BOMMode(lex.BOMIgnoreFirst),
)
if err != nil {
return nil, err
}
l.Lexer = l0
return l, nil
}
func (l *lexer) errPos(pos token.Pos, format string, arg ...interface{}) {
l.errs.Add(l.file.Position(pos), fmt.Sprintf(format, arg...))
}
func (l *lexer) err(s string, arg ...interface{}) {
l.errPos(l.Last.Pos(), s, arg...)
}
// Implements yyLexer.
func (l *lexer) Error(s string) {
l.err(s)
}
func (l *lexer) int(lval *yySymType, im bool) int {
val := l.TokenBytes(nil)
if im {
val = val[:len(val)-1]
}
n, err := strconv.ParseUint(string(val), 0, 64)
if err != nil {
l.err("integer literal: %v", err)
return int(unicode.ReplacementChar)
}
if im {
lval.item = idealComplex(complex(0, float64(n)))
return imaginaryLit
}
switch {
case n < math.MaxInt64:
lval.item = idealInt(n)
default:
lval.item = idealUint(n)
}
return intLit
}
func (l *lexer) float(lval *yySymType, im bool) int {
val := l.TokenBytes(nil)
if im {
val = val[:len(val)-1]
}
n, err := strconv.ParseFloat(string(val), 64)
if err != nil {
l.err("float literal: %v", err)
return int(unicode.ReplacementChar)
}
if im {
lval.item = idealComplex(complex(0, n))
return imaginaryLit
}
lval.item = idealFloat(n)
return floatLit
}
func (l *lexer) str(lval *yySymType, pref string) int {
val := l.TokenBytes(nil)
l.sc = 0
s := pref + string(val)
s, err := strconv.Unquote(s)
if err != nil {
l.err("string literal: %v", err)
return int(unicode.ReplacementChar)
}
lval.item = s
return stringLit
}
func (l *lexer) npos() (line, col int) {
pos := l.file.Position(l.Last.Pos())
return pos.Line, pos.Column
}