Skip to content

Commit da17465

Browse files
Feedback
Signed-off-by: George Robinson <[email protected]>
1 parent 8da53de commit da17465

File tree

4 files changed

+28
-28
lines changed

4 files changed

+28
-28
lines changed

matchers/parse/lexer.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,11 @@ func (e UnterminatedError) Error() string {
104104
type Lexer struct {
105105
input string
106106
err error
107-
start int // the offset of the current token
108-
pos int // the position of the cursor in the input
109-
width int // the width of the last rune
110-
column int // the column offset of the current token
111-
cols int // the number of columns (runes) decoded from the input
107+
start int // The offset of the current token.
108+
pos int // The position of the cursor in the input.
109+
width int // The width of the last rune.
110+
column int // The column offset of the current token.
111+
cols int // The number of columns (runes) decoded from the input.
112112
}
113113

114114
func NewLexer(input string) Lexer {
@@ -123,7 +123,7 @@ func (l *Lexer) Peek() (Token, error) {
123123
width := l.width
124124
column := l.column
125125
cols := l.cols
126-
// Do not reset l.err because we can return it on the next call to Scan()
126+
// Do not reset l.err because we can return it on the next call to Scan().
127127
defer func() {
128128
l.start = start
129129
l.pos = pos
@@ -137,12 +137,12 @@ func (l *Lexer) Peek() (Token, error) {
137137
func (l *Lexer) Scan() (Token, error) {
138138
tok := Token{}
139139

140-
// Do not attempt to emit more tokens if the input is invalid
140+
// Do not attempt to emit more tokens if the input is invalid.
141141
if l.err != nil {
142142
return tok, l.err
143143
}
144144

145-
// Iterate over each rune in the input and either emit a token or an error
145+
// Iterate over each rune in the input and either emit a token or an error.
146146
for r := l.next(); r != eof; r = l.next() {
147147
switch {
148148
case r == '{':
@@ -198,11 +198,11 @@ func (l *Lexer) scanOperator() (Token, error) {
198198
return Token{}, err
199199
}
200200

201-
// Rewind because we need to know if the rune was an '!' or an '='
201+
// Rewind because we need to know if the rune was an '!' or an '='.
202202
l.rewind()
203203

204204
// If the first rune is an '!' then it must be followed with either an
205-
// '=' or '~' to not match a string or regex
205+
// '=' or '~' to not match a string or regex.
206206
if l.accept("!") {
207207
if err := l.expect("=~"); err != nil {
208208
return Token{}, err
@@ -211,7 +211,7 @@ func (l *Lexer) scanOperator() (Token, error) {
211211
}
212212

213213
// If the first rune is an '=' then it can be followed with an optional
214-
// '~' to match a regex
214+
// '~' to match a regex.
215215
l.accept("=")
216216
l.accept("~")
217217
return l.emit(TokenOperator), nil

matchers/parse/lexer_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func TestLexer_Scan(t *testing.T) {
3939
},
4040
}},
4141
}, {
42-
name: "open brace with space",
42+
name: "open brace with leading space",
4343
input: " {",
4444
expected: []Token{{
4545
Kind: TokenOpenBrace,
@@ -65,8 +65,8 @@ func TestLexer_Scan(t *testing.T) {
6565
},
6666
}},
6767
}, {
68-
name: "close brace with space",
69-
input: "}",
68+
name: "close brace with leading space",
69+
input: " }",
7070
expected: []Token{{
7171
Kind: TokenCloseBrace,
7272
Value: "}",

matchers/parse/parse.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func (p *Parser) parse() (labels.Matchers, error) {
139139
type parseFn func(l *Lexer) (parseFn, error)
140140

141141
func (p *Parser) parseOpenParen(l *Lexer) (parseFn, error) {
142-
// Can start with an optional open brace
142+
// Can start with an optional open brace.
143143
hasOpenParen, err := p.accept(l.Peek, TokenOpenBrace)
144144
if err != nil {
145145
if errors.Is(err, ErrEOF) {
@@ -149,14 +149,14 @@ func (p *Parser) parseOpenParen(l *Lexer) (parseFn, error) {
149149
}
150150
if hasOpenParen {
151151
// If the token was an open brace it must be scanned so the token
152-
// following it can be peeked
152+
// following it can be peeked.
153153
if _, err = l.Scan(); err != nil {
154154
panic("Unexpected error scanning open brace")
155155
}
156156
}
157157
p.hasOpenParen = hasOpenParen
158158
// If the next token is a close brace there are no matchers in the input
159-
// and we can just parse the close brace
159+
// and we can just parse the close brace.
160160
if hasCloseParen, err := p.accept(l.Peek, TokenCloseBrace); err != nil {
161161
return nil, fmt.Errorf("%s: %w", err, ErrNoCloseBrace)
162162
} else if hasCloseParen {
@@ -167,12 +167,12 @@ func (p *Parser) parseOpenParen(l *Lexer) (parseFn, error) {
167167

168168
func (p *Parser) parseCloseParen(l *Lexer) (parseFn, error) {
169169
if p.hasOpenParen {
170-
// If there was an open brace there must be a matching close brace
170+
// If there was an open brace there must be a matching close brace.
171171
if _, err := p.expect(l.Scan, TokenCloseBrace); err != nil {
172172
return nil, fmt.Errorf("%s: %w", err, ErrNoCloseBrace)
173173
}
174174
} else {
175-
// If there was no open brace there must not be a close brace either
175+
// If there was no open brace there must not be a close brace either.
176176
if _, err := p.expect(l.Peek, TokenCloseBrace); err == nil {
177177
return nil, fmt.Errorf("0:%d: }: %w", len(p.input), ErrNoOpenBrace)
178178
}
@@ -185,7 +185,7 @@ func (p *Parser) parseComma(l *Lexer) (parseFn, error) {
185185
return nil, fmt.Errorf("%s: %s", err, "expected a comma")
186186
}
187187
// The token after the comma can be another matcher, a close brace or the
188-
// end of input
188+
// end of input.
189189
tok, err := p.expect(l.Peek, TokenCloseBrace, TokenIdent, TokenQuoted)
190190
if err != nil {
191191
if errors.Is(err, ErrEOF) {
@@ -219,13 +219,13 @@ func (p *Parser) parseLabelMatcher(l *Lexer) (parseFn, error) {
219219

220220
// The next token is the label name. This can either be an ident which
221221
// accepts just [a-zA-Z_] or a quoted which accepts all UTF-8 characters
222-
// in double quotes
222+
// in double quotes.
223223
if tok, err = p.expect(l.Scan, TokenIdent, TokenQuoted); err != nil {
224224
return nil, fmt.Errorf("%s: %w", err, ErrNoLabelName)
225225
}
226226
labelName = tok.Value
227227

228-
// The next token is the operator such as '=', '!=', '=~' and '!~'
228+
// The next token is the operator such as '=', '!=', '=~' and '!~'.
229229
if tok, err = p.expect(l.Scan, TokenOperator); err != nil {
230230
return nil, fmt.Errorf("%s: %s", err, ErrNoOperator)
231231
}
@@ -235,7 +235,7 @@ func (p *Parser) parseLabelMatcher(l *Lexer) (parseFn, error) {
235235

236236
// The next token is the label value. This too can either be an ident
237237
// which accepts just [a-zA-Z_] or a quoted which accepts all UTF-8
238-
// characters in double quotes
238+
// characters in double quotes.
239239
if tok, err = p.expect(l.Scan, TokenIdent, TokenQuoted); err != nil {
240240
return nil, fmt.Errorf("%s: %s", err, ErrNoLabelValue)
241241
}
@@ -261,7 +261,7 @@ func (p *Parser) parseLabelMatcherEnd(l *Lexer) (parseFn, error) {
261261
tok, err := p.expect(l.Peek, TokenComma, TokenCloseBrace)
262262
if err != nil {
263263
// If this is the end of input we still need to check if the optional
264-
// open brace has a matching close brace
264+
// open brace has a matching close brace.
265265
if errors.Is(err, ErrEOF) {
266266
return p.parseCloseParen, nil
267267
}

matchers/parse/token.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ func IsNone(t Token) bool {
6363
}
6464

6565
type Position struct {
66-
OffsetStart int // The start position in the input
67-
OffsetEnd int // The end position in the input
68-
ColumnStart int // The column number
69-
ColumnEnd int // The end of the column
66+
OffsetStart int // The start position in the input.
67+
OffsetEnd int // The end position in the input.
68+
ColumnStart int // The column number.
69+
ColumnEnd int // The end of the column.
7070
}

0 commit comments

Comments
 (0)