github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/pkg/logql/log/jsonexpr/jsonexpr.y (about)

     1  // Inspired by https://github.com/sjjian/yacc-examples
     2  
     3  %{
     4  package jsonexpr
     5  
     6  func setScannerData(lex interface{}, data []interface{}) {
     7  	lex.(*Scanner).data = data
     8  }
     9  
    10  %}
    11  
    12  %union {
    13      empty   struct{}
    14      str     string
    15      field   string
    16      list    []interface{}
    17      int     int
    18  }
    19  
    20  %token<empty>   DOT LSB RSB
    21  %token<str>     STRING
    22  %token<field>   FIELD
    23  %token<int>     INDEX
    24  
    25  %type<int>  index index_access
    26  %type<str>  field key key_access
    27  %type<list> values
    28  
    29  %%
    30  
    31  json:
    32    values             { setScannerData(JSONExprlex, $1) }
    33  
    34  values:
    35      field                   { $$ = []interface{}{$1} }
    36    | key_access              { $$ = []interface{}{$1} }
    37    | index_access            { $$ = []interface{}{$1} }
    38    | values key_access       { $$ = append($1, $2) }
    39    | values index_access     { $$ = append($1, $2) }
    40    | values DOT field        { $$ = append($1, $3) }
    41    ;
    42  
    43  key_access:
    44      LSB key RSB     { $$ = $2 }
    45  
    46  index_access:
    47      LSB index RSB   { $$ = $2 }
    48  
    49  field:
    50    FIELD             { $$ = $1 }
    51  
    52  key:
    53    STRING            { $$ = $1 }
    54  
    55  index:
    56    INDEX             { $$ = $1 }