summaryrefslogtreecommitdiff
path: root/vendor/github.com/uptrace/bun/internal/parser/parser.go
blob: 1f27044783d9676a862de7ee878236e3024a3939 (plain)
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
package parser

import (
	"bytes"
	"fmt"
	"io"
	"strconv"

	"github.com/uptrace/bun/internal"
)

type Parser struct {
	b []byte
	i int
}

func New(b []byte) *Parser {
	return &Parser{
		b: b,
	}
}

func NewString(s string) *Parser {
	return New(internal.Bytes(s))
}

func (p *Parser) Reset(b []byte) {
	p.b = b
	p.i = 0
}

func (p *Parser) Valid() bool {
	return p.i < len(p.b)
}

func (p *Parser) Remaining() []byte {
	return p.b[p.i:]
}

func (p *Parser) ReadByte() (byte, error) {
	if p.Valid() {
		ch := p.b[p.i]
		p.Advance()
		return ch, nil
	}
	return 0, io.ErrUnexpectedEOF
}

func (p *Parser) Read() byte {
	if p.Valid() {
		ch := p.b[p.i]
		p.Advance()
		return ch
	}
	return 0
}

func (p *Parser) Unread() {
	if p.i > 0 {
		p.i--
	}
}

func (p *Parser) Peek() byte {
	if p.Valid() {
		return p.b[p.i]
	}
	return 0
}

func (p *Parser) Advance() {
	p.i++
}

func (p *Parser) Skip(skip byte) error {
	ch := p.Peek()
	if ch == skip {
		p.Advance()
		return nil
	}
	return fmt.Errorf("got %q, wanted %q", ch, skip)
}

func (p *Parser) SkipPrefix(skip []byte) error {
	if !bytes.HasPrefix(p.b[p.i:], skip) {
		return fmt.Errorf("got %q, wanted prefix %q", p.b, skip)
	}
	p.i += len(skip)
	return nil
}

func (p *Parser) CutPrefix(skip []byte) bool {
	if !bytes.HasPrefix(p.b[p.i:], skip) {
		return false
	}
	p.i += len(skip)
	return true
}

func (p *Parser) ReadSep(sep byte) ([]byte, bool) {
	ind := bytes.IndexByte(p.b[p.i:], sep)
	if ind == -1 {
		b := p.b[p.i:]
		p.i = len(p.b)
		return b, false
	}

	b := p.b[p.i : p.i+ind]
	p.i += ind + 1
	return b, true
}

func (p *Parser) ReadIdentifier() (string, bool) {
	if p.i < len(p.b) && p.b[p.i] == '(' {
		s := p.i + 1
		if ind := bytes.IndexByte(p.b[s:], ')'); ind != -1 {
			b := p.b[s : s+ind]
			p.i = s + ind + 1
			return internal.String(b), false
		}
	}

	ind := len(p.b) - p.i
	var alpha bool
	for i, c := range p.b[p.i:] {
		if isNum(c) {
			continue
		}
		if isAlpha(c) || (i > 0 && alpha && c == '_') {
			alpha = true
			continue
		}
		ind = i
		break
	}
	if ind == 0 {
		return "", false
	}
	b := p.b[p.i : p.i+ind]
	p.i += ind
	return internal.String(b), !alpha
}

func (p *Parser) ReadNumber() int {
	ind := len(p.b) - p.i
	for i, c := range p.b[p.i:] {
		if !isNum(c) {
			ind = i
			break
		}
	}
	if ind == 0 {
		return 0
	}
	n, err := strconv.Atoi(string(p.b[p.i : p.i+ind]))
	if err != nil {
		panic(err)
	}
	p.i += ind
	return n
}

func isNum(c byte) bool {
	return c >= '0' && c <= '9'
}

func isAlpha(c byte) bool {
	return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
}