mirror of https://github.com/prometheus/prometheus
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
101 lines
2.9 KiB
101 lines
2.9 KiB
8 years ago
|
%{
|
||
8 years ago
|
// Copyright 2017 The Prometheus Authors
|
||
|
// 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.
|
||
|
|
||
8 years ago
|
package textparse
|
||
|
|
||
|
import (
|
||
7 years ago
|
"fmt"
|
||
8 years ago
|
)
|
||
|
|
||
8 years ago
|
const (
|
||
7 years ago
|
sInit = iota
|
||
|
sComment
|
||
|
sMeta1
|
||
|
sMeta2
|
||
|
sLabels
|
||
|
sLValue
|
||
|
sValue
|
||
|
sTimestamp
|
||
8 years ago
|
)
|
||
8 years ago
|
|
||
|
// 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.
|
||
6 years ago
|
func (l *promlexer) Lex() token {
|
||
8 years ago
|
if l.i >= len(l.b) {
|
||
7 years ago
|
return tEOF
|
||
8 years ago
|
}
|
||
|
c := l.b[l.i]
|
||
7 years ago
|
l.start = l.i
|
||
8 years ago
|
|
||
8 years ago
|
%}
|
||
|
|
||
|
D [0-9]
|
||
|
L [a-zA-Z_]
|
||
|
M [a-zA-Z_:]
|
||
7 years ago
|
C [^\n]
|
||
8 years ago
|
|
||
7 years ago
|
%x sComment sMeta1 sMeta2 sLabels sLValue sValue sTimestamp
|
||
8 years ago
|
|
||
|
%yyc c
|
||
|
%yyn c = l.next()
|
||
8 years ago
|
%yyt l.state
|
||
8 years ago
|
|
||
|
|
||
|
%%
|
||
|
|
||
7 years ago
|
\0 return tEOF
|
||
|
\n l.state = sInit; return tLinebreak
|
||
|
<*>[ \t]+ return tWhitespace
|
||
|
|
||
|
#[ \t]+ l.state = sComment
|
||
|
# return l.consumeComment()
|
||
|
<sComment>HELP[\t ]+ l.state = sMeta1; return tHelp
|
||
|
<sComment>TYPE[\t ]+ l.state = sMeta1; return tType
|
||
|
<sMeta1>{M}({M}|{D})* l.state = sMeta2; return tMName
|
||
6 years ago
|
<sMeta2>{C}* l.state = sInit; return tText
|
||
7 years ago
|
|
||
|
{M}({M}|{D})* l.state = sValue; return tMName
|
||
|
<sValue>\{ l.state = sLabels; return tBraceOpen
|
||
|
<sLabels>{L}({L}|{D})* return tLName
|
||
|
<sLabels>\} l.state = sValue; return tBraceClose
|
||
|
<sLabels>= l.state = sLValue; return tEqual
|
||
|
<sLabels>, return tComma
|
||
|
<sLValue>\"(\\.|[^\\"])*\" l.state = sLabels; return tLValue
|
||
|
<sValue>[^{ \t\n]+ l.state = sTimestamp; return tValue
|
||
|
<sTimestamp>{D}+ return tTimestamp
|
||
|
<sTimestamp>\n l.state = sInit; return tLinebreak
|
||
7 years ago
|
|
||
8 years ago
|
%%
|
||
7 years ago
|
// Workaround to gobble up comments that started with a HELP or TYPE
|
||
|
// prefix. We just consume all characters until we reach a newline.
|
||
|
// This saves us from adding disproportionate complexity to the parser.
|
||
|
if l.state == sComment {
|
||
|
return l.consumeComment()
|
||
|
}
|
||
|
return tInvalid
|
||
|
}
|
||
|
|
||
6 years ago
|
func (l *promlexer) consumeComment() token {
|
||
7 years ago
|
for c := l.cur(); ; c = l.next() {
|
||
|
switch c {
|
||
|
case 0:
|
||
|
return tEOF
|
||
|
case '\n':
|
||
|
l.state = sInit
|
||
|
return tComment
|
||
|
}
|
||
|
}
|
||
8 years ago
|
}
|