2013-07-30 15:18:07 +00:00
|
|
|
/* Copyright 2013 Prometheus Team
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License. */
|
2013-02-07 10:49:04 +00:00
|
|
|
|
2013-01-07 22:24:26 +00:00
|
|
|
%{
|
|
|
|
package rules
|
|
|
|
|
|
|
|
import (
|
2013-07-11 16:38:44 +00:00
|
|
|
"fmt"
|
2013-01-07 22:24:26 +00:00
|
|
|
"strconv"
|
2013-04-09 12:00:25 +00:00
|
|
|
"strings"
|
2013-06-25 12:02:27 +00:00
|
|
|
|
|
|
|
clientmodel "github.com/prometheus/client_golang/model"
|
2013-01-07 22:24:26 +00:00
|
|
|
)
|
2013-07-11 16:38:44 +00:00
|
|
|
|
|
|
|
// Lex is called by the parser generated by "go tool yacc" to obtain each
|
|
|
|
// token. The method is opened before the matching rules block and closed at
|
|
|
|
// the end of the file.
|
|
|
|
func (lexer *RulesLexer) Lex(lval *yySymType) int {
|
|
|
|
// Internal lexer states.
|
|
|
|
const (
|
|
|
|
S_INITIAL = iota
|
|
|
|
S_COMMENTS
|
|
|
|
)
|
|
|
|
|
|
|
|
// We simulate multiple start symbols for closely-related grammars via dummy tokens. See
|
|
|
|
// http://www.gnu.org/software/bison/manual/html_node/Multiple-start_002dsymbols.html
|
|
|
|
// Reason: we want to be able to parse lists of named rules as well as single expressions.
|
|
|
|
if lexer.startToken != 0 {
|
|
|
|
startToken := lexer.startToken
|
|
|
|
lexer.startToken = 0
|
|
|
|
return startToken
|
|
|
|
}
|
|
|
|
|
|
|
|
c := lexer.current
|
|
|
|
currentState := 0
|
|
|
|
|
|
|
|
if lexer.empty {
|
|
|
|
c, lexer.empty = lexer.getChar(), false
|
|
|
|
}
|
|
|
|
|
2013-01-07 22:24:26 +00:00
|
|
|
%}
|
|
|
|
|
|
|
|
D [0-9]
|
|
|
|
L [a-zA-Z_:]
|
2013-01-22 00:51:26 +00:00
|
|
|
U [smhdwy]
|
2013-01-07 22:24:26 +00:00
|
|
|
|
|
|
|
%x S_COMMENTS
|
|
|
|
|
2013-07-11 16:38:44 +00:00
|
|
|
%yyc c
|
|
|
|
%yyn c = lexer.getChar()
|
|
|
|
%yyt currentState
|
|
|
|
|
2013-01-07 22:24:26 +00:00
|
|
|
%%
|
2013-07-11 16:38:44 +00:00
|
|
|
lexer.buf = lexer.buf[:0] // The code before the first rule executed before every scan cycle (rule #0 / state 0 action)
|
|
|
|
|
|
|
|
"/*" currentState = S_COMMENTS
|
|
|
|
<S_COMMENTS>"*/" currentState = S_INITIAL
|
|
|
|
<S_COMMENTS>.|\n /* ignore chars within multi-line comments */
|
|
|
|
|
|
|
|
\/\/[^\r\n]*\n /* gobble up one-line comments */
|
|
|
|
|
|
|
|
ALERT|alert return ALERT
|
|
|
|
IF|if return IF
|
|
|
|
FOR|for return FOR
|
|
|
|
WITH|with return WITH
|
2013-07-30 15:18:07 +00:00
|
|
|
SUMMARY|summary return SUMMARY
|
|
|
|
DESCRIPTION|description return DESCRIPTION
|
2013-07-11 16:38:44 +00:00
|
|
|
|
|
|
|
PERMANENT|permanent return PERMANENT
|
|
|
|
BY|by return GROUP_OP
|
|
|
|
AVG|SUM|MAX|MIN|COUNT lval.str = lexer.token(); return AGGR_OP
|
|
|
|
avg|sum|max|min|count lval.str = strings.ToUpper(lexer.token()); return AGGR_OP
|
|
|
|
\<|>|AND|OR|and|or lval.str = strings.ToUpper(lexer.token()); return CMP_OP
|
|
|
|
==|!=|>=|<= lval.str = lexer.token(); return CMP_OP
|
|
|
|
[+\-] lval.str = lexer.token(); return ADDITIVE_OP
|
|
|
|
[*/%] lval.str = lexer.token(); return MULT_OP
|
|
|
|
|
|
|
|
{D}+{U} lval.str = lexer.token(); return DURATION
|
|
|
|
{L}({L}|{D})* lval.str = lexer.token(); return IDENTIFIER
|
|
|
|
|
|
|
|
\-?{D}+(\.{D}*)? num, err := strconv.ParseFloat(lexer.token(), 64);
|
|
|
|
if (err != nil && err.(*strconv.NumError).Err == strconv.ErrSyntax) {
|
|
|
|
panic("Invalid float")
|
|
|
|
}
|
|
|
|
lval.num = clientmodel.SampleValue(num)
|
|
|
|
return NUMBER
|
|
|
|
|
|
|
|
\"(\\.|[^\\"])*\" lval.str = lexer.token()[1:len(lexer.token()) - 1]; return STRING
|
|
|
|
\'(\\.|[^\\'])*\' lval.str = lexer.token()[1:len(lexer.token()) - 1]; return STRING
|
|
|
|
|
|
|
|
[{}\[\]()=,] return int(lexer.buf[0])
|
|
|
|
[\t\n\r ] /* gobble up any whitespace */
|
2013-01-07 22:24:26 +00:00
|
|
|
%%
|
2013-07-11 16:38:44 +00:00
|
|
|
|
|
|
|
lexer.empty = true
|
|
|
|
return int(c)
|
|
|
|
}
|