github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/pkg/logql/log/pattern/parser.go (about)

     1  package pattern
     2  
     3  import "fmt"
     4  
     5  const underscore = "_"
     6  
     7  var tokens = map[int]string{
     8  	LESS_THAN:  "<",
     9  	MORE_THAN:  ">",
    10  	UNDERSCORE: underscore,
    11  }
    12  
    13  func init() {
    14  	// Improve the error messages coming out of yacc.
    15  	exprErrorVerbose = true
    16  	for tok, str := range tokens {
    17  		exprToknames[tok-exprPrivate+1] = str
    18  	}
    19  }
    20  
    21  func parseExpr(input string) (expr, error) {
    22  	l := newLexer()
    23  	l.setData([]byte(input))
    24  	e := exprNewParser().Parse(l)
    25  	if e != 0 || len(l.errs) > 0 {
    26  		return nil, l.errs[0]
    27  	}
    28  	return l.expr, nil
    29  }
    30  
    31  // parseError is what is returned when we failed to parse.
    32  type parseError struct {
    33  	msg       string
    34  	line, col int
    35  }
    36  
    37  func (p parseError) Error() string {
    38  	if p.col == 0 && p.line == 0 {
    39  		return p.msg
    40  	}
    41  	return fmt.Sprintf("parse error at line %d, col %d: %s", p.line, p.col, p.msg)
    42  }
    43  
    44  func newParseError(msg string, line, col int) parseError {
    45  	return parseError{
    46  		msg:  msg,
    47  		line: line,
    48  		col:  col,
    49  	}
    50  }