From c229ed17e218f7f2106dda62ccf8d84bf2c3b09e Mon Sep 17 00:00:00 2001 From: Tobias Guggenmos Date: Tue, 26 Nov 2019 13:28:36 +0000 Subject: [PATCH] promql: Implement yyLexer interface (#6370) This is the first step towards a generated lexer as described in #6256. It adds methods to the parser struct, that make it implement the yyLexer interface required by a yacc generated parser, as described here: https://godoc.org/golang.org/x/tools/cmd/goyacc . The yyLexer interface is implemented by the parser struct instead of the lexer struct for the following reasons: * Both parsers have a lookahead that the lexer does not know about. This solution makes it possible to synchronize these lookaheads when switching parsers. * The routines to handle parser errors are not accessible to the lexer. Signed-off-by: Tobias Guggenmos --- promql/parse.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/promql/parse.go b/promql/parse.go index 8d6c6121e..51b7906fe 100644 --- a/promql/parse.go +++ b/promql/parse.go @@ -337,6 +337,33 @@ func (p *parser) recover(errp *error) { p.lex.close() } +// yySymType is the Type the yacc generated parser expects for lexer items. +// +// For more information, see https://godoc.org/golang.org/x/tools/cmd/goyacc. +type yySymType item + +// Lex is expected by the yyLexer interface of the yacc generated parser. +// It writes the next item provided by the lexer to the provided pointer address. +// Comments are skipped. +// +// The yyLexer interface is currently implemented by the parser to allow +// the generated and non-generated parts to work together with regards to lookahead +// and error handling. +// +// For more information, see https://godoc.org/golang.org/x/tools/cmd/goyacc. +func (p *parser) Lex(lval *yySymType) int { + *lval = yySymType(p.next()) + + return int(item(*lval).typ) +} + +// Error is expected by the yyLexer interface of the yacc generated parser. +// +// For more information, see https://godoc.org/golang.org/x/tools/cmd/goyacc. +func (p *parser) Error(e string) { + p.errorf(e) +} + // expr parses any expression. func (p *parser) expr() Expr { // Parse the starting expression.