This commit is contained in:
Aliaksandr Valialkin 2024-05-09 14:02:53 +02:00
parent 5a883be63f
commit 790a9491b2
No known key found for this signature in database
GPG key ID: 52C003EE2BCDB9EB
8 changed files with 8 additions and 13 deletions

View file

@ -39,12 +39,17 @@ type lexer struct {
currentTimestamp int64 currentTimestamp int64
} }
// newLexer returns new lexer for the given s.
//
// The lex.token points to the first token in s.
func newLexer(s string) *lexer { func newLexer(s string) *lexer {
return &lexer{ lex := &lexer{
s: s, s: s,
sOrig: s, sOrig: s,
currentTimestamp: time.Now().UnixNano(), currentTimestamp: time.Now().UnixNano(),
} }
lex.nextToken()
return lex
} }
func (lex *lexer) isEnd() bool { func (lex *lexer) isEnd() bool {
@ -240,7 +245,7 @@ func ParseQuery(s string) (*Query, error) {
} }
func parseFilter(lex *lexer) (filter, error) { func parseFilter(lex *lexer) (filter, error) {
if !lex.mustNextToken() || lex.isKeyword("|") { if lex.isKeyword("|", "") {
return nil, fmt.Errorf("missing query") return nil, fmt.Errorf("missing query")
} }
fo, err := parseFilterOr(lex, "") fo, err := parseFilterOr(lex, "")
@ -1026,9 +1031,6 @@ func parseFilterStream(lex *lexer) (*filterStream, error) {
func newStreamFilter(s string) (*StreamFilter, error) { func newStreamFilter(s string) (*StreamFilter, error) {
lex := newLexer(s) lex := newLexer(s)
if !lex.mustNextToken() {
return nil, fmt.Errorf("missing '{' in _stream filter")
}
fs, err := parseFilterStream(lex) fs, err := parseFilterStream(lex)
if err != nil { if err != nil {
return nil, err return nil, err

View file

@ -12,12 +12,11 @@ func TestLexer(t *testing.T) {
t.Helper() t.Helper()
lex := newLexer(s) lex := newLexer(s)
for _, tokenExpected := range tokensExpected { for _, tokenExpected := range tokensExpected {
lex.nextToken()
if lex.token != tokenExpected { if lex.token != tokenExpected {
t.Fatalf("unexpected token; got %q; want %q", lex.token, tokenExpected) t.Fatalf("unexpected token; got %q; want %q", lex.token, tokenExpected)
} }
lex.nextToken()
} }
lex.nextToken()
if lex.token != "" { if lex.token != "" {
t.Fatalf("unexpected tail token: %q", lex.token) t.Fatalf("unexpected tail token: %q", lex.token)
} }

View file

@ -13,7 +13,6 @@ func TestPipeCopyUpdateNeededFields(t *testing.T) {
unfs := newTestFieldsSet(unneededFields) unfs := newTestFieldsSet(unneededFields)
lex := newLexer(s) lex := newLexer(s)
lex.nextToken()
p, err := parsePipeCopy(lex) p, err := parsePipeCopy(lex)
if err != nil { if err != nil {
t.Fatalf("cannot parse %s: %s", s, err) t.Fatalf("cannot parse %s: %s", s, err)

View file

@ -12,7 +12,6 @@ func TestPipeDeleteUpdateNeededFields(t *testing.T) {
unfs := newTestFieldsSet(unneededFields) unfs := newTestFieldsSet(unneededFields)
lex := newLexer(s) lex := newLexer(s)
lex.nextToken()
p, err := parsePipeDelete(lex) p, err := parsePipeDelete(lex)
if err != nil { if err != nil {
t.Fatalf("cannot parse %s: %s", s, err) t.Fatalf("cannot parse %s: %s", s, err)

View file

@ -12,7 +12,6 @@ func TestPipeFieldsUpdateNeededFields(t *testing.T) {
unfs := newTestFieldsSet(unneededFields) unfs := newTestFieldsSet(unneededFields)
lex := newLexer(s) lex := newLexer(s)
lex.nextToken()
p, err := parsePipeFields(lex) p, err := parsePipeFields(lex)
if err != nil { if err != nil {
t.Fatalf("cannot parse %s: %s", s, err) t.Fatalf("cannot parse %s: %s", s, err)

View file

@ -12,7 +12,6 @@ func TestPipeRenameUpdateNeededFields(t *testing.T) {
unfs := newTestFieldsSet(unneededFields) unfs := newTestFieldsSet(unneededFields)
lex := newLexer(s) lex := newLexer(s)
lex.nextToken()
p, err := parsePipeRename(lex) p, err := parsePipeRename(lex)
if err != nil { if err != nil {
t.Fatalf("cannot parse %s: %s", s, err) t.Fatalf("cannot parse %s: %s", s, err)

View file

@ -12,7 +12,6 @@ func TestPipeSortUpdateNeededFields(t *testing.T) {
unfs := newTestFieldsSet(unneededFields) unfs := newTestFieldsSet(unneededFields)
lex := newLexer(s) lex := newLexer(s)
lex.nextToken()
p, err := parsePipeSort(lex) p, err := parsePipeSort(lex)
if err != nil { if err != nil {
t.Fatalf("cannot parse %s: %s", s, err) t.Fatalf("cannot parse %s: %s", s, err)

View file

@ -12,7 +12,6 @@ func TestPipeStatsUpdateNeededFields(t *testing.T) {
unfs := newTestFieldsSet(unneededFields) unfs := newTestFieldsSet(unneededFields)
lex := newLexer(s) lex := newLexer(s)
lex.nextToken()
p, err := parsePipeStats(lex) p, err := parsePipeStats(lex)
if err != nil { if err != nil {
t.Fatalf("unexpected error when parsing %s: %s", s, err) t.Fatalf("unexpected error when parsing %s: %s", s, err)