-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken.go
154 lines (141 loc) · 3.33 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
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
package goatlang
import (
"fmt"
"strconv"
"strings"
"text/scanner"
)
type token struct {
Pos scanner.Position
Symbol string
Text string
Tokens []*token
}
func (t *token) Copy() *token {
toks := make([]*token, len(t.Tokens))
for i, tt := range t.Tokens {
toks[i] = tt.Copy()
}
return &token{
Pos: t.Pos,
Symbol: t.Symbol,
Text: t.Text,
Tokens: toks,
}
}
func (t *token) Replace(sym, newSym, newText string) {
if t.Symbol == sym {
t.Symbol = newSym
t.Text = newText
t.Tokens = nil
return
}
for _, tt := range t.Tokens {
tt.Replace(sym, newSym, newText)
}
}
func (t *token) String() string {
if len(t.Tokens) > 0 {
var tt []string
for _, v := range t.Tokens {
tt = append(tt, v.String())
}
return fmt.Sprintf("(%v %v)", t.Text, strings.Join(tt, " "))
}
return t.Text
}
func (t *token) Char() rune {
value, _, _, _ := strconv.UnquoteChar(t.Text[1:len(t.Text)-1], '\\')
return value
}
func (t *token) rename(v string) {
t.Symbol, t.Text = v, v
}
func (t *token) Int() int {
if len(t.Text) > 2 && t.Text[:2] == "0x" {
v, err := strconv.ParseInt(t.Text[2:], 16, 0)
if err != nil {
panicf("error parsing hex: %v", err)
}
return int(v)
}
if len(t.Text) > 1 && t.Text[0] == '0' {
v, err := strconv.ParseInt(t.Text[1:], 8, 0)
if err != nil {
panicf("error parsing octal: %v", err)
}
return int(v)
}
v, err := strconv.Atoi(t.Text)
if err != nil {
panicf("error parsing int: %v", err)
}
return int(v)
}
func (t *token) Unquote() string {
v, err := strconv.Unquote(t.Text)
if err != nil {
panicf("error parsing string: %v", err)
}
return v
}
func (t *token) Float64() float64 {
v, err := strconv.ParseFloat(t.Text, 64)
if err != nil {
panicf("error parsing float: %v", err)
}
return v
}
func (t *token) Append(b *token) {
t.Tokens = append(t.Tokens, b)
}
var symMap map[int]string = map[int]string{
scanner.Int: "(int)",
scanner.Float: "(float)",
scanner.Char: "(char)",
scanner.String: "(string)",
scanner.RawString: "(string)",
}
// tokenize a string into a list of Tokens
func tokenize(filename string, in string) ([]*token, error) {
var res []*token
var err error
var s scanner.Scanner
s.Init(strings.NewReader(in))
s.Mode = scanner.ScanIdents | scanner.ScanChars | scanner.ScanStrings | scanner.ScanRawStrings | scanner.ScanFloats | scanner.ScanComments | scanner.SkipComments
s.Error = func(s *scanner.Scanner, msg string) {
err = fmt.Errorf("%v: %v", s.Position, msg)
}
const symChars = "`~!.#$%^&*()-=+[{]}\\|;:,<.>/?"
s.Filename = filename
for ch := s.Scan(); err == nil && ch != scanner.EOF; ch = s.Scan() {
if strings.ContainsRune(symChars, ch) {
sym := string(ch)
if strings.ContainsRune(symChars, ch) {
pk := s.Peek()
sym2 := sym + string(pk)
if _, ok := symbols[sym2]; ok || sym2 == ".." {
sym = sym2
s.Scan()
sym3 := sym + string(s.Peek())
if _, ok := symbols[sym3]; ok {
sym = sym3
s.Scan()
}
}
}
res = append(res, &token{Pos: s.Position, Symbol: sym, Text: sym})
continue
}
sym := "(name)"
text := s.TokenText()
if v, ok := symMap[int(ch)]; ok {
sym = v
} else if symbols[text] != nil {
sym = text
}
res = append(res, &token{Pos: s.Position, Symbol: sym, Text: text})
}
res = append(res, &token{Pos: s.Pos(), Symbol: "(eof)", Text: "(eof)"})
return res, err
}