Split files and make yacc more redable

Signed-off-by: Ganesh Vernekar <cs15btech11018@iith.ac.in>
pull/969/head
Ganesh Vernekar 6 years ago committed by Cyril Tovena
parent 625c4f1f53
commit 184927ccdc
  1. 40
      pkg/logql/expr.y
  2. 198
      pkg/logql/expr.y.go
  3. 96
      pkg/logql/lex.go
  4. 50
      pkg/logql/lex_test.go
  5. 91
      pkg/logql/parser.go
  6. 43
      pkg/logql/parser_test.go

@ -9,18 +9,18 @@ import (
%union{ %union{
Expr Expr Expr Expr
LogExpr LogSelectorExpr
RangeAggregationExpr SampleExpr
VectorAggregationExpr SampleExpr
LogRangeExpr *logRange
Filter labels.MatchType Filter labels.MatchType
Selector []*labels.Matcher
Matchers []*labels.Matcher
Matcher *labels.Matcher
Grouping *grouping Grouping *grouping
Labels []string Labels []string
VectorOp string LogExpr LogSelectorExpr
LogRangeExpr *logRange
Matcher *labels.Matcher
Matchers []*labels.Matcher
RangeAggregationExpr SampleExpr
RangeOp string RangeOp string
Selector []*labels.Matcher
VectorAggregationExpr SampleExpr
VectorOp string
str string str string
duration time.Duration duration time.Duration
int int64 int int64
@ -30,17 +30,17 @@ import (
%type <Expr> expr %type <Expr> expr
%type <Filter> filter %type <Filter> filter
%type <Selector> selector
%type <Matchers> matchers
%type <Matcher> matcher
%type <VectorOp> vectorOp
%type <RangeOp> rangeOp
%type <Labels> labels
%type <Grouping> grouping %type <Grouping> grouping
%type <Labels> labels
%type <LogExpr> logExpr %type <LogExpr> logExpr
%type <LogRangeExpr> logRangeExpr
%type <Matcher> matcher
%type <Matchers> matchers
%type <RangeAggregationExpr> rangeAggregationExpr %type <RangeAggregationExpr> rangeAggregationExpr
%type <RangeOp> rangeOp
%type <Selector> selector
%type <VectorAggregationExpr> vectorAggregationExpr %type <VectorAggregationExpr> vectorAggregationExpr
%type <LogRangeExpr> logRangeExpr %type <VectorOp> vectorOp
%token <str> IDENTIFIER STRING %token <str> IDENTIFIER STRING
%token <duration> DURATION %token <duration> DURATION
@ -72,16 +72,18 @@ logRangeExpr: logExpr DURATION { $$ = mustNewRange($1, $2) };
rangeAggregationExpr: rangeOp OPEN_PARENTHESIS logRangeExpr CLOSE_PARENTHESIS { $$ = newRangeAggregationExpr($3,$1) }; rangeAggregationExpr: rangeOp OPEN_PARENTHESIS logRangeExpr CLOSE_PARENTHESIS { $$ = newRangeAggregationExpr($3,$1) };
vectorAggregationExpr: vectorAggregationExpr:
// Aggregations with 1 argument.
vectorOp OPEN_PARENTHESIS rangeAggregationExpr CLOSE_PARENTHESIS { $$ = mustNewVectorAggregationExpr($3, $1, nil, nil) } vectorOp OPEN_PARENTHESIS rangeAggregationExpr CLOSE_PARENTHESIS { $$ = mustNewVectorAggregationExpr($3, $1, nil, nil) }
| vectorOp grouping OPEN_PARENTHESIS rangeAggregationExpr CLOSE_PARENTHESIS { $$ = mustNewVectorAggregationExpr($4, $1, $2, nil,) }
| vectorOp OPEN_PARENTHESIS rangeAggregationExpr CLOSE_PARENTHESIS grouping { $$ = mustNewVectorAggregationExpr($3, $1, $5, nil) }
| vectorOp OPEN_PARENTHESIS IDENTIFIER COMMA rangeAggregationExpr CLOSE_PARENTHESIS { $$ = mustNewVectorAggregationExpr($5, $1, nil, &$3) }
| vectorOp OPEN_PARENTHESIS IDENTIFIER COMMA rangeAggregationExpr CLOSE_PARENTHESIS grouping { $$ = mustNewVectorAggregationExpr($5, $1, $7, &$3) }
| vectorOp OPEN_PARENTHESIS vectorAggregationExpr CLOSE_PARENTHESIS { $$ = mustNewVectorAggregationExpr($3, $1, nil, nil) } | vectorOp OPEN_PARENTHESIS vectorAggregationExpr CLOSE_PARENTHESIS { $$ = mustNewVectorAggregationExpr($3, $1, nil, nil) }
| vectorOp grouping OPEN_PARENTHESIS rangeAggregationExpr CLOSE_PARENTHESIS { $$ = mustNewVectorAggregationExpr($4, $1, $2, nil,) }
| vectorOp grouping OPEN_PARENTHESIS vectorAggregationExpr CLOSE_PARENTHESIS { $$ = mustNewVectorAggregationExpr($4, $1, $2, nil,) } | vectorOp grouping OPEN_PARENTHESIS vectorAggregationExpr CLOSE_PARENTHESIS { $$ = mustNewVectorAggregationExpr($4, $1, $2, nil,) }
| vectorOp OPEN_PARENTHESIS rangeAggregationExpr CLOSE_PARENTHESIS grouping { $$ = mustNewVectorAggregationExpr($3, $1, $5, nil) }
| vectorOp OPEN_PARENTHESIS vectorAggregationExpr CLOSE_PARENTHESIS grouping { $$ = mustNewVectorAggregationExpr($3, $1, $5, nil) } | vectorOp OPEN_PARENTHESIS vectorAggregationExpr CLOSE_PARENTHESIS grouping { $$ = mustNewVectorAggregationExpr($3, $1, $5, nil) }
// Aggregations with 2 arguments.
| vectorOp OPEN_PARENTHESIS IDENTIFIER COMMA vectorAggregationExpr CLOSE_PARENTHESIS { $$ = mustNewVectorAggregationExpr($5, $1, nil, &$3) } | vectorOp OPEN_PARENTHESIS IDENTIFIER COMMA vectorAggregationExpr CLOSE_PARENTHESIS { $$ = mustNewVectorAggregationExpr($5, $1, nil, &$3) }
| vectorOp OPEN_PARENTHESIS IDENTIFIER COMMA vectorAggregationExpr CLOSE_PARENTHESIS grouping { $$ = mustNewVectorAggregationExpr($5, $1, $7, &$3) } | vectorOp OPEN_PARENTHESIS IDENTIFIER COMMA vectorAggregationExpr CLOSE_PARENTHESIS grouping { $$ = mustNewVectorAggregationExpr($5, $1, $7, &$3) }
| vectorOp OPEN_PARENTHESIS IDENTIFIER COMMA rangeAggregationExpr CLOSE_PARENTHESIS { $$ = mustNewVectorAggregationExpr($5, $1, nil, &$3) }
| vectorOp OPEN_PARENTHESIS IDENTIFIER COMMA rangeAggregationExpr CLOSE_PARENTHESIS grouping { $$ = mustNewVectorAggregationExpr($5, $1, $7, &$3) }
; ;
filter: filter:

@ -16,18 +16,18 @@ import (
type exprSymType struct { type exprSymType struct {
yys int yys int
Expr Expr Expr Expr
LogExpr LogSelectorExpr
RangeAggregationExpr SampleExpr
VectorAggregationExpr SampleExpr
LogRangeExpr *logRange
Filter labels.MatchType Filter labels.MatchType
Selector []*labels.Matcher
Matchers []*labels.Matcher
Matcher *labels.Matcher
Grouping *grouping Grouping *grouping
Labels []string Labels []string
VectorOp string LogExpr LogSelectorExpr
LogRangeExpr *logRange
Matcher *labels.Matcher
Matchers []*labels.Matcher
RangeAggregationExpr SampleExpr
RangeOp string RangeOp string
Selector []*labels.Matcher
VectorAggregationExpr SampleExpr
VectorOp string
str string str string
duration time.Duration duration time.Duration
int int64 int int64
@ -109,7 +109,7 @@ const exprEofCode = 1
const exprErrCode = 2 const exprErrCode = 2
const exprInitialStackSize = 16 const exprInitialStackSize = 16
//line pkg/logql/expr.y:138 //line pkg/logql/expr.y:140
//line yacctab:1 //line yacctab:1
var exprExca = [...]int{ var exprExca = [...]int{
@ -127,64 +127,64 @@ const exprLast = 123
var exprAct = [...]int{ var exprAct = [...]int{
31, 36, 5, 4, 10, 64, 30, 48, 32, 33, 31, 5, 4, 36, 64, 10, 30, 78, 32, 33,
32, 33, 7, 47, 44, 82, 11, 12, 13, 14, 32, 33, 80, 7, 45, 82, 81, 11, 12, 13,
16, 17, 15, 18, 19, 20, 21, 81, 77, 76, 14, 16, 17, 15, 18, 19, 20, 21, 77, 78,
61, 59, 46, 45, 43, 11, 12, 13, 14, 16, 76, 60, 44, 43, 79, 11, 12, 13, 14, 16,
17, 15, 18, 19, 20, 21, 78, 3, 57, 63, 17, 15, 18, 19, 20, 21, 3, 59, 63, 62,
62, 80, 78, 67, 66, 28, 60, 79, 29, 52, 57, 10, 48, 66, 28, 67, 47, 46, 29, 7,
72, 71, 75, 74, 73, 11, 12, 13, 14, 16, 72, 73, 61, 74, 75, 11, 12, 13, 14, 16,
17, 15, 18, 19, 20, 21, 23, 42, 70, 83, 17, 15, 18, 19, 20, 21, 42, 23, 50, 52,
10, 23, 84, 85, 27, 58, 26, 23, 7, 27, 9, 39, 84, 85, 38, 27, 71, 26, 23, 70,
37, 26, 69, 24, 25, 27, 40, 26, 24, 25, 49, 23, 58, 51, 24, 25, 27, 40, 26, 27,
50, 68, 41, 65, 24, 25, 53, 54, 55, 56, 69, 26, 68, 83, 37, 24, 25, 65, 24, 25,
39, 8, 49, 38, 35, 51, 37, 9, 34, 6, 53, 54, 55, 56, 35, 6, 37, 8, 34, 41,
22, 2, 1, 22, 2, 1,
} }
var exprPact = [...]int{ var exprPact = [...]int{
-9, -1000, -1000, 85, -1000, -1000, -1000, 67, 37, -15, -8, -1000, -1000, 89, -1000, -1000, -1000, 38, 37, -15,
112, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, 112, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000,
-1000, -1000, 108, -1000, -1000, -1000, -1000, -1000, 74, 67, -1000, -1000, 79, -1000, -1000, -1000, -1000, -1000, 75, 38,
10, 11, -8, -14, 98, 45, -1000, 97, -1000, -1000, 10, 36, 35, 31, 76, 65, -1000, 101, -1000, -1000,
-1000, 26, 79, 9, 39, 8, 40, 99, 99, -1000, -1000, 28, 86, 25, 9, 45, 40, 103, 103, -1000,
-1000, 86, -1000, 96, 87, 73, 56, -1000, -1000, -13, -1000, 100, -1000, 97, 95, 84, 81, -1000, -1000, -13,
40, -13, 7, 6, 35, -1000, 29, -1000, -1000, -1000, -13, 40, 8, 6, 12, -1000, -10, -1000, -1000, -1000,
-1000, -1000, -1000, 5, -7, -1000, -1000, -1000, 75, -1000, -1000, -1000, -1000, -1000, -6, -7, -1000, -1000, 99, -1000,
-1000, -13, -13, -1000, -1000, -1000, -1000, -13, -13, -1000, -1000, -1000,
} }
var exprPgo = [...]int{ var exprPgo = [...]int{
0, 122, 121, 120, 119, 118, 1, 117, 111, 5, 0, 122, 121, 120, 0, 4, 46, 119, 3, 118,
0, 47, 3, 2, 102, 2, 117, 115, 1, 80,
} }
var exprR1 = [...]int{ var exprR1 = [...]int{
0, 1, 2, 2, 2, 11, 11, 11, 11, 11, 0, 1, 2, 2, 2, 6, 6, 6, 6, 6,
14, 12, 13, 13, 13, 13, 13, 13, 13, 13, 7, 10, 13, 13, 13, 13, 13, 13, 13, 13,
13, 13, 3, 3, 3, 3, 4, 4, 4, 5, 13, 13, 3, 3, 3, 3, 12, 12, 12, 9,
5, 6, 6, 6, 6, 7, 7, 7, 7, 7, 9, 8, 8, 8, 8, 14, 14, 14, 14, 14,
7, 7, 7, 7, 8, 8, 9, 9, 10, 10, 14, 14, 14, 14, 11, 11, 5, 5, 4, 4,
} }
var exprR2 = [...]int{ var exprR2 = [...]int{
0, 1, 1, 1, 1, 1, 3, 3, 3, 2, 0, 1, 1, 1, 1, 1, 3, 3, 3, 2,
2, 4, 4, 5, 5, 6, 7, 4, 5, 5, 2, 4, 4, 4, 5, 5, 5, 5, 6, 7,
6, 7, 1, 1, 1, 1, 3, 3, 3, 1, 6, 7, 1, 1, 1, 1, 3, 3, 3, 1,
3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 3, 4, 4, 1, 1, 1, 1, 1, 1, 1, 3, 4, 4,
} }
var exprChk = [...]int{ var exprChk = [...]int{
-1000, -1, -2, -11, -12, -13, -4, 21, -8, -7, -1000, -1, -2, -6, -10, -13, -12, 21, -11, -14,
13, 25, 26, 27, 28, 31, 29, 30, 32, 33, 13, 25, 26, 27, 28, 31, 29, 30, 32, 33,
34, 35, -3, 2, 19, 20, 12, 10, -11, 21, 34, 35, -3, 2, 19, 20, 12, 10, -6, 21,
21, -10, 23, 24, -5, 2, -6, 4, 5, 2, 21, -4, 23, 24, -9, 2, -8, 4, 5, 2,
22, -14, -11, -12, 4, -13, 21, 21, 21, 14, 22, -7, -6, -10, -13, 4, 21, 21, 21, 14,
2, 17, 14, 9, 10, 11, 12, 22, 6, 22, 2, 17, 14, 9, 10, 11, 12, 22, 6, 22,
17, 22, -12, -13, -9, 4, -9, -6, 5, 5, 22, 17, -10, -13, -5, 4, -5, -8, 5, 5,
5, 5, -10, -12, -13, -10, 22, 22, 17, 22, 5, 5, -4, -4, -13, -10, 22, 22, 17, 22,
22, 22, 22, 4, -10, -10, 22, 22, 22, 4, -4, -4,
} }
var exprDef = [...]int{ var exprDef = [...]int{
@ -194,9 +194,9 @@ var exprDef = [...]int{
0, 0, 0, 0, 0, 0, 29, 0, 6, 8, 0, 0, 0, 0, 0, 0, 29, 0, 6, 8,
7, 0, 0, 0, 0, 0, 0, 0, 0, 26, 7, 0, 0, 0, 0, 0, 0, 0, 0, 26,
27, 0, 28, 0, 0, 0, 0, 11, 10, 12, 27, 0, 28, 0, 0, 0, 0, 11, 10, 12,
0, 17, 0, 0, 0, 46, 0, 30, 31, 32, 13, 0, 0, 0, 0, 46, 0, 30, 31, 32,
33, 34, 14, 0, 0, 19, 13, 18, 0, 48, 33, 34, 16, 17, 0, 0, 14, 15, 0, 48,
49, 15, 20, 47, 16, 21, 49, 18, 20, 47, 19, 21,
} }
var exprTok1 = [...]int{ var exprTok1 = [...]int{
@ -606,228 +606,228 @@ exprdefault:
} }
case 12: case 12:
exprDollar = exprS[exprpt-4 : exprpt+1] exprDollar = exprS[exprpt-4 : exprpt+1]
//line pkg/logql/expr.y:75 //line pkg/logql/expr.y:76
{ {
exprVAL.VectorAggregationExpr = mustNewVectorAggregationExpr(exprDollar[3].RangeAggregationExpr, exprDollar[1].VectorOp, nil, nil) exprVAL.VectorAggregationExpr = mustNewVectorAggregationExpr(exprDollar[3].RangeAggregationExpr, exprDollar[1].VectorOp, nil, nil)
} }
case 13: case 13:
exprDollar = exprS[exprpt-5 : exprpt+1] exprDollar = exprS[exprpt-4 : exprpt+1]
//line pkg/logql/expr.y:76 //line pkg/logql/expr.y:77
{ {
exprVAL.VectorAggregationExpr = mustNewVectorAggregationExpr(exprDollar[4].RangeAggregationExpr, exprDollar[1].VectorOp, exprDollar[2].Grouping, nil) exprVAL.VectorAggregationExpr = mustNewVectorAggregationExpr(exprDollar[3].VectorAggregationExpr, exprDollar[1].VectorOp, nil, nil)
} }
case 14: case 14:
exprDollar = exprS[exprpt-5 : exprpt+1] exprDollar = exprS[exprpt-5 : exprpt+1]
//line pkg/logql/expr.y:77 //line pkg/logql/expr.y:78
{ {
exprVAL.VectorAggregationExpr = mustNewVectorAggregationExpr(exprDollar[3].RangeAggregationExpr, exprDollar[1].VectorOp, exprDollar[5].Grouping, nil) exprVAL.VectorAggregationExpr = mustNewVectorAggregationExpr(exprDollar[4].RangeAggregationExpr, exprDollar[1].VectorOp, exprDollar[2].Grouping, nil)
} }
case 15: case 15:
exprDollar = exprS[exprpt-6 : exprpt+1] exprDollar = exprS[exprpt-5 : exprpt+1]
//line pkg/logql/expr.y:78 //line pkg/logql/expr.y:79
{ {
exprVAL.VectorAggregationExpr = mustNewVectorAggregationExpr(exprDollar[5].RangeAggregationExpr, exprDollar[1].VectorOp, nil, &exprDollar[3].str) exprVAL.VectorAggregationExpr = mustNewVectorAggregationExpr(exprDollar[4].VectorAggregationExpr, exprDollar[1].VectorOp, exprDollar[2].Grouping, nil)
} }
case 16: case 16:
exprDollar = exprS[exprpt-7 : exprpt+1] exprDollar = exprS[exprpt-5 : exprpt+1]
//line pkg/logql/expr.y:79 //line pkg/logql/expr.y:80
{ {
exprVAL.VectorAggregationExpr = mustNewVectorAggregationExpr(exprDollar[5].RangeAggregationExpr, exprDollar[1].VectorOp, exprDollar[7].Grouping, &exprDollar[3].str) exprVAL.VectorAggregationExpr = mustNewVectorAggregationExpr(exprDollar[3].RangeAggregationExpr, exprDollar[1].VectorOp, exprDollar[5].Grouping, nil)
} }
case 17: case 17:
exprDollar = exprS[exprpt-4 : exprpt+1] exprDollar = exprS[exprpt-5 : exprpt+1]
//line pkg/logql/expr.y:80 //line pkg/logql/expr.y:81
{ {
exprVAL.VectorAggregationExpr = mustNewVectorAggregationExpr(exprDollar[3].VectorAggregationExpr, exprDollar[1].VectorOp, nil, nil) exprVAL.VectorAggregationExpr = mustNewVectorAggregationExpr(exprDollar[3].VectorAggregationExpr, exprDollar[1].VectorOp, exprDollar[5].Grouping, nil)
} }
case 18: case 18:
exprDollar = exprS[exprpt-5 : exprpt+1] exprDollar = exprS[exprpt-6 : exprpt+1]
//line pkg/logql/expr.y:81 //line pkg/logql/expr.y:83
{ {
exprVAL.VectorAggregationExpr = mustNewVectorAggregationExpr(exprDollar[4].VectorAggregationExpr, exprDollar[1].VectorOp, exprDollar[2].Grouping, nil) exprVAL.VectorAggregationExpr = mustNewVectorAggregationExpr(exprDollar[5].VectorAggregationExpr, exprDollar[1].VectorOp, nil, &exprDollar[3].str)
} }
case 19: case 19:
exprDollar = exprS[exprpt-5 : exprpt+1] exprDollar = exprS[exprpt-7 : exprpt+1]
//line pkg/logql/expr.y:82 //line pkg/logql/expr.y:84
{ {
exprVAL.VectorAggregationExpr = mustNewVectorAggregationExpr(exprDollar[3].VectorAggregationExpr, exprDollar[1].VectorOp, exprDollar[5].Grouping, nil) exprVAL.VectorAggregationExpr = mustNewVectorAggregationExpr(exprDollar[5].VectorAggregationExpr, exprDollar[1].VectorOp, exprDollar[7].Grouping, &exprDollar[3].str)
} }
case 20: case 20:
exprDollar = exprS[exprpt-6 : exprpt+1] exprDollar = exprS[exprpt-6 : exprpt+1]
//line pkg/logql/expr.y:83 //line pkg/logql/expr.y:85
{ {
exprVAL.VectorAggregationExpr = mustNewVectorAggregationExpr(exprDollar[5].VectorAggregationExpr, exprDollar[1].VectorOp, nil, &exprDollar[3].str) exprVAL.VectorAggregationExpr = mustNewVectorAggregationExpr(exprDollar[5].RangeAggregationExpr, exprDollar[1].VectorOp, nil, &exprDollar[3].str)
} }
case 21: case 21:
exprDollar = exprS[exprpt-7 : exprpt+1] exprDollar = exprS[exprpt-7 : exprpt+1]
//line pkg/logql/expr.y:84 //line pkg/logql/expr.y:86
{ {
exprVAL.VectorAggregationExpr = mustNewVectorAggregationExpr(exprDollar[5].VectorAggregationExpr, exprDollar[1].VectorOp, exprDollar[7].Grouping, &exprDollar[3].str) exprVAL.VectorAggregationExpr = mustNewVectorAggregationExpr(exprDollar[5].RangeAggregationExpr, exprDollar[1].VectorOp, exprDollar[7].Grouping, &exprDollar[3].str)
} }
case 22: case 22:
exprDollar = exprS[exprpt-1 : exprpt+1] exprDollar = exprS[exprpt-1 : exprpt+1]
//line pkg/logql/expr.y:88 //line pkg/logql/expr.y:90
{ {
exprVAL.Filter = labels.MatchRegexp exprVAL.Filter = labels.MatchRegexp
} }
case 23: case 23:
exprDollar = exprS[exprpt-1 : exprpt+1] exprDollar = exprS[exprpt-1 : exprpt+1]
//line pkg/logql/expr.y:89 //line pkg/logql/expr.y:91
{ {
exprVAL.Filter = labels.MatchEqual exprVAL.Filter = labels.MatchEqual
} }
case 24: case 24:
exprDollar = exprS[exprpt-1 : exprpt+1] exprDollar = exprS[exprpt-1 : exprpt+1]
//line pkg/logql/expr.y:90 //line pkg/logql/expr.y:92
{ {
exprVAL.Filter = labels.MatchNotRegexp exprVAL.Filter = labels.MatchNotRegexp
} }
case 25: case 25:
exprDollar = exprS[exprpt-1 : exprpt+1] exprDollar = exprS[exprpt-1 : exprpt+1]
//line pkg/logql/expr.y:91 //line pkg/logql/expr.y:93
{ {
exprVAL.Filter = labels.MatchNotEqual exprVAL.Filter = labels.MatchNotEqual
} }
case 26: case 26:
exprDollar = exprS[exprpt-3 : exprpt+1] exprDollar = exprS[exprpt-3 : exprpt+1]
//line pkg/logql/expr.y:95 //line pkg/logql/expr.y:97
{ {
exprVAL.Selector = exprDollar[2].Matchers exprVAL.Selector = exprDollar[2].Matchers
} }
case 27: case 27:
exprDollar = exprS[exprpt-3 : exprpt+1] exprDollar = exprS[exprpt-3 : exprpt+1]
//line pkg/logql/expr.y:96 //line pkg/logql/expr.y:98
{ {
exprVAL.Selector = exprDollar[2].Matchers exprVAL.Selector = exprDollar[2].Matchers
} }
case 28: case 28:
exprDollar = exprS[exprpt-3 : exprpt+1] exprDollar = exprS[exprpt-3 : exprpt+1]
//line pkg/logql/expr.y:97 //line pkg/logql/expr.y:99
{ {
} }
case 29: case 29:
exprDollar = exprS[exprpt-1 : exprpt+1] exprDollar = exprS[exprpt-1 : exprpt+1]
//line pkg/logql/expr.y:101 //line pkg/logql/expr.y:103
{ {
exprVAL.Matchers = []*labels.Matcher{exprDollar[1].Matcher} exprVAL.Matchers = []*labels.Matcher{exprDollar[1].Matcher}
} }
case 30: case 30:
exprDollar = exprS[exprpt-3 : exprpt+1] exprDollar = exprS[exprpt-3 : exprpt+1]
//line pkg/logql/expr.y:102 //line pkg/logql/expr.y:104
{ {
exprVAL.Matchers = append(exprDollar[1].Matchers, exprDollar[3].Matcher) exprVAL.Matchers = append(exprDollar[1].Matchers, exprDollar[3].Matcher)
} }
case 31: case 31:
exprDollar = exprS[exprpt-3 : exprpt+1] exprDollar = exprS[exprpt-3 : exprpt+1]
//line pkg/logql/expr.y:106 //line pkg/logql/expr.y:108
{ {
exprVAL.Matcher = mustNewMatcher(labels.MatchEqual, exprDollar[1].str, exprDollar[3].str) exprVAL.Matcher = mustNewMatcher(labels.MatchEqual, exprDollar[1].str, exprDollar[3].str)
} }
case 32: case 32:
exprDollar = exprS[exprpt-3 : exprpt+1] exprDollar = exprS[exprpt-3 : exprpt+1]
//line pkg/logql/expr.y:107 //line pkg/logql/expr.y:109
{ {
exprVAL.Matcher = mustNewMatcher(labels.MatchNotEqual, exprDollar[1].str, exprDollar[3].str) exprVAL.Matcher = mustNewMatcher(labels.MatchNotEqual, exprDollar[1].str, exprDollar[3].str)
} }
case 33: case 33:
exprDollar = exprS[exprpt-3 : exprpt+1] exprDollar = exprS[exprpt-3 : exprpt+1]
//line pkg/logql/expr.y:108 //line pkg/logql/expr.y:110
{ {
exprVAL.Matcher = mustNewMatcher(labels.MatchRegexp, exprDollar[1].str, exprDollar[3].str) exprVAL.Matcher = mustNewMatcher(labels.MatchRegexp, exprDollar[1].str, exprDollar[3].str)
} }
case 34: case 34:
exprDollar = exprS[exprpt-3 : exprpt+1] exprDollar = exprS[exprpt-3 : exprpt+1]
//line pkg/logql/expr.y:109 //line pkg/logql/expr.y:111
{ {
exprVAL.Matcher = mustNewMatcher(labels.MatchNotRegexp, exprDollar[1].str, exprDollar[3].str) exprVAL.Matcher = mustNewMatcher(labels.MatchNotRegexp, exprDollar[1].str, exprDollar[3].str)
} }
case 35: case 35:
exprDollar = exprS[exprpt-1 : exprpt+1] exprDollar = exprS[exprpt-1 : exprpt+1]
//line pkg/logql/expr.y:113 //line pkg/logql/expr.y:115
{ {
exprVAL.VectorOp = OpTypeSum exprVAL.VectorOp = OpTypeSum
} }
case 36: case 36:
exprDollar = exprS[exprpt-1 : exprpt+1] exprDollar = exprS[exprpt-1 : exprpt+1]
//line pkg/logql/expr.y:114 //line pkg/logql/expr.y:116
{ {
exprVAL.VectorOp = OpTypeAvg exprVAL.VectorOp = OpTypeAvg
} }
case 37: case 37:
exprDollar = exprS[exprpt-1 : exprpt+1] exprDollar = exprS[exprpt-1 : exprpt+1]
//line pkg/logql/expr.y:115 //line pkg/logql/expr.y:117
{ {
exprVAL.VectorOp = OpTypeCount exprVAL.VectorOp = OpTypeCount
} }
case 38: case 38:
exprDollar = exprS[exprpt-1 : exprpt+1] exprDollar = exprS[exprpt-1 : exprpt+1]
//line pkg/logql/expr.y:116 //line pkg/logql/expr.y:118
{ {
exprVAL.VectorOp = OpTypeMax exprVAL.VectorOp = OpTypeMax
} }
case 39: case 39:
exprDollar = exprS[exprpt-1 : exprpt+1] exprDollar = exprS[exprpt-1 : exprpt+1]
//line pkg/logql/expr.y:117 //line pkg/logql/expr.y:119
{ {
exprVAL.VectorOp = OpTypeMin exprVAL.VectorOp = OpTypeMin
} }
case 40: case 40:
exprDollar = exprS[exprpt-1 : exprpt+1] exprDollar = exprS[exprpt-1 : exprpt+1]
//line pkg/logql/expr.y:118 //line pkg/logql/expr.y:120
{ {
exprVAL.VectorOp = OpTypeStddev exprVAL.VectorOp = OpTypeStddev
} }
case 41: case 41:
exprDollar = exprS[exprpt-1 : exprpt+1] exprDollar = exprS[exprpt-1 : exprpt+1]
//line pkg/logql/expr.y:119 //line pkg/logql/expr.y:121
{ {
exprVAL.VectorOp = OpTypeStdvar exprVAL.VectorOp = OpTypeStdvar
} }
case 42: case 42:
exprDollar = exprS[exprpt-1 : exprpt+1] exprDollar = exprS[exprpt-1 : exprpt+1]
//line pkg/logql/expr.y:120 //line pkg/logql/expr.y:122
{ {
exprVAL.VectorOp = OpTypeBottomK exprVAL.VectorOp = OpTypeBottomK
} }
case 43: case 43:
exprDollar = exprS[exprpt-1 : exprpt+1] exprDollar = exprS[exprpt-1 : exprpt+1]
//line pkg/logql/expr.y:121 //line pkg/logql/expr.y:123
{ {
exprVAL.VectorOp = OpTypeTopK exprVAL.VectorOp = OpTypeTopK
} }
case 44: case 44:
exprDollar = exprS[exprpt-1 : exprpt+1] exprDollar = exprS[exprpt-1 : exprpt+1]
//line pkg/logql/expr.y:125 //line pkg/logql/expr.y:127
{ {
exprVAL.RangeOp = OpTypeCountOverTime exprVAL.RangeOp = OpTypeCountOverTime
} }
case 45: case 45:
exprDollar = exprS[exprpt-1 : exprpt+1] exprDollar = exprS[exprpt-1 : exprpt+1]
//line pkg/logql/expr.y:126 //line pkg/logql/expr.y:128
{ {
exprVAL.RangeOp = OpTypeRate exprVAL.RangeOp = OpTypeRate
} }
case 46: case 46:
exprDollar = exprS[exprpt-1 : exprpt+1] exprDollar = exprS[exprpt-1 : exprpt+1]
//line pkg/logql/expr.y:130 //line pkg/logql/expr.y:132
{ {
exprVAL.Labels = []string{exprDollar[1].str} exprVAL.Labels = []string{exprDollar[1].str}
} }
case 47: case 47:
exprDollar = exprS[exprpt-3 : exprpt+1] exprDollar = exprS[exprpt-3 : exprpt+1]
//line pkg/logql/expr.y:131 //line pkg/logql/expr.y:133
{ {
exprVAL.Labels = append(exprDollar[1].Labels, exprDollar[3].str) exprVAL.Labels = append(exprDollar[1].Labels, exprDollar[3].str)
} }
case 48: case 48:
exprDollar = exprS[exprpt-4 : exprpt+1] exprDollar = exprS[exprpt-4 : exprpt+1]
//line pkg/logql/expr.y:135 //line pkg/logql/expr.y:137
{ {
exprVAL.Grouping = &grouping{without: false, groups: exprDollar[3].Labels} exprVAL.Grouping = &grouping{without: false, groups: exprDollar[3].Labels}
} }
case 49: case 49:
exprDollar = exprS[exprpt-4 : exprpt+1] exprDollar = exprS[exprpt-4 : exprpt+1]
//line pkg/logql/expr.y:136 //line pkg/logql/expr.y:138
{ {
exprVAL.Grouping = &grouping{without: true, groups: exprDollar[3].Labels} exprVAL.Grouping = &grouping{without: true, groups: exprDollar[3].Labels}
} }

@ -0,0 +1,96 @@
package logql
import (
"strconv"
"text/scanner"
"time"
)
var tokens = map[string]int{
",": COMMA,
".": DOT,
"{": OPEN_BRACE,
"}": CLOSE_BRACE,
"=": EQ,
"!=": NEQ,
"=~": RE,
"!~": NRE,
"|=": PIPE_EXACT,
"|~": PIPE_MATCH,
"(": OPEN_PARENTHESIS,
")": CLOSE_PARENTHESIS,
"by": BY,
"without": WITHOUT,
OpTypeCountOverTime: COUNT_OVER_TIME,
"[": OPEN_BRACKET,
"]": CLOSE_BRACKET,
OpTypeRate: RATE,
OpTypeSum: SUM,
OpTypeAvg: AVG,
OpTypeMax: MAX,
OpTypeMin: MIN,
OpTypeCount: COUNT,
OpTypeStddev: STDDEV,
OpTypeStdvar: STDVAR,
OpTypeBottomK: BOTTOMK,
OpTypeTopK: TOPK,
}
type lexer struct {
scanner.Scanner
errs []ParseError
expr Expr
parser *exprParserImpl
}
func (l *lexer) Lex(lval *exprSymType) int {
r := l.Scan()
switch r {
case scanner.EOF:
return 0
case scanner.String:
var err error
lval.str, err = strconv.Unquote(l.TokenText())
if err != nil {
l.Error(err.Error())
return 0
}
return STRING
}
// scaning duration tokens
if l.TokenText() == "[" {
d := ""
for r := l.Next(); r != scanner.EOF; r = l.Next() {
if string(r) == "]" {
i, err := time.ParseDuration(d)
if err != nil {
l.Error(err.Error())
return 0
}
lval.duration = i
return DURATION
}
d += string(r)
}
l.Error("missing closing ']' in duration")
return 0
}
if tok, ok := tokens[l.TokenText()+string(l.Peek())]; ok {
l.Next()
return tok
}
if tok, ok := tokens[l.TokenText()]; ok {
return tok
}
lval.str = l.TokenText()
return IDENTIFIER
}
func (l *lexer) Error(msg string) {
l.errs = append(l.errs, newParseError(msg, l.Line, l.Column))
}

@ -0,0 +1,50 @@
package logql
import (
"strings"
"testing"
"text/scanner"
"github.com/stretchr/testify/require"
)
func TestLex(t *testing.T) {
for _, tc := range []struct {
input string
expected []int
}{
{`{foo="bar"}`, []int{OPEN_BRACE, IDENTIFIER, EQ, STRING, CLOSE_BRACE}},
{`{ foo = "bar" }`, []int{OPEN_BRACE, IDENTIFIER, EQ, STRING, CLOSE_BRACE}},
{`{ foo != "bar" }`, []int{OPEN_BRACE, IDENTIFIER, NEQ, STRING, CLOSE_BRACE}},
{`{ foo =~ "bar" }`, []int{OPEN_BRACE, IDENTIFIER, RE, STRING, CLOSE_BRACE}},
{`{ foo !~ "bar" }`, []int{OPEN_BRACE, IDENTIFIER, NRE, STRING, CLOSE_BRACE}},
{`{ foo = "bar", bar != "baz" }`, []int{OPEN_BRACE, IDENTIFIER, EQ, STRING,
COMMA, IDENTIFIER, NEQ, STRING, CLOSE_BRACE}},
{`{ foo = "ba\"r" }`, []int{OPEN_BRACE, IDENTIFIER, EQ, STRING, CLOSE_BRACE}},
{`rate({foo="bar"}[10s])`, []int{RATE, OPEN_PARENTHESIS, OPEN_BRACE, IDENTIFIER, EQ, STRING, CLOSE_BRACE, DURATION, CLOSE_PARENTHESIS}},
{`count_over_time({foo="bar"}[5m])`, []int{COUNT_OVER_TIME, OPEN_PARENTHESIS, OPEN_BRACE, IDENTIFIER, EQ, STRING, CLOSE_BRACE, DURATION, CLOSE_PARENTHESIS}},
{`sum(count_over_time({foo="bar"}[5m])) by (foo,bar)`, []int{SUM, OPEN_PARENTHESIS, COUNT_OVER_TIME, OPEN_PARENTHESIS, OPEN_BRACE, IDENTIFIER, EQ, STRING, CLOSE_BRACE, DURATION, CLOSE_PARENTHESIS, CLOSE_PARENTHESIS, BY, OPEN_PARENTHESIS, IDENTIFIER, COMMA, IDENTIFIER, CLOSE_PARENTHESIS}},
{`topk(3,count_over_time({foo="bar"}[5m])) by (foo,bar)`, []int{TOPK, OPEN_PARENTHESIS, IDENTIFIER, COMMA, COUNT_OVER_TIME, OPEN_PARENTHESIS, OPEN_BRACE, IDENTIFIER, EQ, STRING, CLOSE_BRACE, DURATION, CLOSE_PARENTHESIS, CLOSE_PARENTHESIS, BY, OPEN_PARENTHESIS, IDENTIFIER, COMMA, IDENTIFIER, CLOSE_PARENTHESIS}},
{`bottomk(10,sum(count_over_time({foo="bar"}[5m])) by (foo,bar))`, []int{BOTTOMK, OPEN_PARENTHESIS, IDENTIFIER, COMMA, SUM, OPEN_PARENTHESIS, COUNT_OVER_TIME, OPEN_PARENTHESIS, OPEN_BRACE, IDENTIFIER, EQ, STRING, CLOSE_BRACE, DURATION, CLOSE_PARENTHESIS, CLOSE_PARENTHESIS, BY, OPEN_PARENTHESIS, IDENTIFIER, COMMA, IDENTIFIER, CLOSE_PARENTHESIS, CLOSE_PARENTHESIS}},
{`sum(max(rate({foo="bar"}[5m])) by (foo,bar)) by (foo)`, []int{SUM, OPEN_PARENTHESIS, MAX, OPEN_PARENTHESIS, RATE, OPEN_PARENTHESIS, OPEN_BRACE, IDENTIFIER, EQ, STRING, CLOSE_BRACE, DURATION, CLOSE_PARENTHESIS, CLOSE_PARENTHESIS, BY, OPEN_PARENTHESIS, IDENTIFIER, COMMA, IDENTIFIER, CLOSE_PARENTHESIS, CLOSE_PARENTHESIS, BY, OPEN_PARENTHESIS, IDENTIFIER, CLOSE_PARENTHESIS}},
} {
t.Run(tc.input, func(t *testing.T) {
actual := []int{}
l := lexer{
Scanner: scanner.Scanner{
Mode: scanner.SkipComments | scanner.ScanStrings,
},
}
l.Init(strings.NewReader(tc.input))
var lval exprSymType
for {
tok := l.Lex(&lval)
if tok == 0 {
break
}
actual = append(actual, tok)
}
require.Equal(t, tc.expected, actual)
})
}
}

@ -3,10 +3,8 @@ package logql
import ( import (
"errors" "errors"
"fmt" "fmt"
"strconv"
"strings" "strings"
"text/scanner" "text/scanner"
"time"
"github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/pkg/labels"
) )
@ -71,95 +69,6 @@ func ParseLogSelector(input string) (LogSelectorExpr, error) {
return logSelector, nil return logSelector, nil
} }
var tokens = map[string]int{
",": COMMA,
".": DOT,
"{": OPEN_BRACE,
"}": CLOSE_BRACE,
"=": EQ,
"!=": NEQ,
"=~": RE,
"!~": NRE,
"|=": PIPE_EXACT,
"|~": PIPE_MATCH,
"(": OPEN_PARENTHESIS,
")": CLOSE_PARENTHESIS,
"by": BY,
"without": WITHOUT,
OpTypeCountOverTime: COUNT_OVER_TIME,
"[": OPEN_BRACKET,
"]": CLOSE_BRACKET,
OpTypeRate: RATE,
OpTypeSum: SUM,
OpTypeAvg: AVG,
OpTypeMax: MAX,
OpTypeMin: MIN,
OpTypeCount: COUNT,
OpTypeStddev: STDDEV,
OpTypeStdvar: STDVAR,
OpTypeBottomK: BOTTOMK,
OpTypeTopK: TOPK,
}
type lexer struct {
scanner.Scanner
errs []ParseError
expr Expr
parser *exprParserImpl
}
func (l *lexer) Lex(lval *exprSymType) int {
r := l.Scan()
switch r {
case scanner.EOF:
return 0
case scanner.String:
var err error
lval.str, err = strconv.Unquote(l.TokenText())
if err != nil {
l.Error(err.Error())
return 0
}
return STRING
}
// scaning duration tokens
if l.TokenText() == "[" {
d := ""
for r := l.Next(); r != scanner.EOF; r = l.Next() {
if string(r) == "]" {
i, err := time.ParseDuration(d)
if err != nil {
l.Error(err.Error())
return 0
}
lval.duration = i
return DURATION
}
d += string(r)
}
l.Error("missing closing ']' in duration")
return 0
}
if tok, ok := tokens[l.TokenText()+string(l.Peek())]; ok {
l.Next()
return tok
}
if tok, ok := tokens[l.TokenText()]; ok {
return tok
}
lval.str = l.TokenText()
return IDENTIFIER
}
func (l *lexer) Error(msg string) {
l.errs = append(l.errs, newParseError(msg, l.Line, l.Column))
}
// ParseError is what is returned when we failed to parse. // ParseError is what is returned when we failed to parse.
type ParseError struct { type ParseError struct {
msg string msg string

@ -2,56 +2,13 @@ package logql
import ( import (
"reflect" "reflect"
"strings"
"testing" "testing"
"text/scanner"
"time" "time"
"github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/pkg/labels"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
func TestLex(t *testing.T) {
for _, tc := range []struct {
input string
expected []int
}{
{`{foo="bar"}`, []int{OPEN_BRACE, IDENTIFIER, EQ, STRING, CLOSE_BRACE}},
{`{ foo = "bar" }`, []int{OPEN_BRACE, IDENTIFIER, EQ, STRING, CLOSE_BRACE}},
{`{ foo != "bar" }`, []int{OPEN_BRACE, IDENTIFIER, NEQ, STRING, CLOSE_BRACE}},
{`{ foo =~ "bar" }`, []int{OPEN_BRACE, IDENTIFIER, RE, STRING, CLOSE_BRACE}},
{`{ foo !~ "bar" }`, []int{OPEN_BRACE, IDENTIFIER, NRE, STRING, CLOSE_BRACE}},
{`{ foo = "bar", bar != "baz" }`, []int{OPEN_BRACE, IDENTIFIER, EQ, STRING,
COMMA, IDENTIFIER, NEQ, STRING, CLOSE_BRACE}},
{`{ foo = "ba\"r" }`, []int{OPEN_BRACE, IDENTIFIER, EQ, STRING, CLOSE_BRACE}},
{`rate({foo="bar"}[10s])`, []int{RATE, OPEN_PARENTHESIS, OPEN_BRACE, IDENTIFIER, EQ, STRING, CLOSE_BRACE, DURATION, CLOSE_PARENTHESIS}},
{`count_over_time({foo="bar"}[5m])`, []int{COUNT_OVER_TIME, OPEN_PARENTHESIS, OPEN_BRACE, IDENTIFIER, EQ, STRING, CLOSE_BRACE, DURATION, CLOSE_PARENTHESIS}},
{`sum(count_over_time({foo="bar"}[5m])) by (foo,bar)`, []int{SUM, OPEN_PARENTHESIS, COUNT_OVER_TIME, OPEN_PARENTHESIS, OPEN_BRACE, IDENTIFIER, EQ, STRING, CLOSE_BRACE, DURATION, CLOSE_PARENTHESIS, CLOSE_PARENTHESIS, BY, OPEN_PARENTHESIS, IDENTIFIER, COMMA, IDENTIFIER, CLOSE_PARENTHESIS}},
{`topk(3,count_over_time({foo="bar"}[5m])) by (foo,bar)`, []int{TOPK, OPEN_PARENTHESIS, IDENTIFIER, COMMA, COUNT_OVER_TIME, OPEN_PARENTHESIS, OPEN_BRACE, IDENTIFIER, EQ, STRING, CLOSE_BRACE, DURATION, CLOSE_PARENTHESIS, CLOSE_PARENTHESIS, BY, OPEN_PARENTHESIS, IDENTIFIER, COMMA, IDENTIFIER, CLOSE_PARENTHESIS}},
{`bottomk(10,sum(count_over_time({foo="bar"}[5m])) by (foo,bar))`, []int{BOTTOMK, OPEN_PARENTHESIS, IDENTIFIER, COMMA, SUM, OPEN_PARENTHESIS, COUNT_OVER_TIME, OPEN_PARENTHESIS, OPEN_BRACE, IDENTIFIER, EQ, STRING, CLOSE_BRACE, DURATION, CLOSE_PARENTHESIS, CLOSE_PARENTHESIS, BY, OPEN_PARENTHESIS, IDENTIFIER, COMMA, IDENTIFIER, CLOSE_PARENTHESIS, CLOSE_PARENTHESIS}},
{`sum(max(rate({foo="bar"}[5m])) by (foo,bar)) by (foo)`, []int{SUM, OPEN_PARENTHESIS, MAX, OPEN_PARENTHESIS, RATE, OPEN_PARENTHESIS, OPEN_BRACE, IDENTIFIER, EQ, STRING, CLOSE_BRACE, DURATION, CLOSE_PARENTHESIS, CLOSE_PARENTHESIS, BY, OPEN_PARENTHESIS, IDENTIFIER, COMMA, IDENTIFIER, CLOSE_PARENTHESIS, CLOSE_PARENTHESIS, BY, OPEN_PARENTHESIS, IDENTIFIER, CLOSE_PARENTHESIS}},
} {
t.Run(tc.input, func(t *testing.T) {
actual := []int{}
l := lexer{
Scanner: scanner.Scanner{
Mode: scanner.SkipComments | scanner.ScanStrings,
},
}
l.Init(strings.NewReader(tc.input))
var lval exprSymType
for {
tok := l.Lex(&lval)
if tok == 0 {
break
}
actual = append(actual, tok)
}
require.Equal(t, tc.expected, actual)
})
}
}
func newString(s string) *string { func newString(s string) *string {
return &s return &s
} }

Loading…
Cancel
Save