2017-01-14 07:39:04 -08:00
|
|
|
%{
|
2017-04-19 05:43:09 -07:00
|
|
|
// 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.
|
|
|
|
|
2017-01-14 07:39:04 -08:00
|
|
|
package textparse
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"strconv"
|
2017-06-16 05:09:50 -07:00
|
|
|
"unicode/utf8"
|
2017-04-13 02:33:08 -07:00
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/pkg/value"
|
2017-01-14 07:39:04 -08:00
|
|
|
)
|
|
|
|
|
2017-07-07 01:29:38 -07:00
|
|
|
const (
|
|
|
|
lstateInit = iota
|
|
|
|
lstateName
|
|
|
|
lstateValue
|
|
|
|
lstateTimestamp
|
|
|
|
lstateLabels
|
|
|
|
lstateLName
|
|
|
|
lstateLValue
|
|
|
|
lstateLValueIn
|
|
|
|
)
|
2017-01-14 07:39:04 -08: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 (l *lexer) Lex() int {
|
2017-07-07 01:29:38 -07:00
|
|
|
l.state = lstateInit
|
2017-07-18 03:35:41 -07:00
|
|
|
|
2017-01-14 07:39:04 -08:00
|
|
|
if l.i >= len(l.b) {
|
|
|
|
return eof
|
|
|
|
}
|
|
|
|
c := l.b[l.i]
|
2017-01-14 10:30:19 -08:00
|
|
|
|
2017-04-27 08:02:07 -07:00
|
|
|
l.ts = nil
|
|
|
|
l.mstart = l.nextMstart
|
2017-01-14 10:30:19 -08:00
|
|
|
l.offsets = l.offsets[:0]
|
2017-01-14 07:39:04 -08:00
|
|
|
%}
|
|
|
|
|
|
|
|
D [0-9]
|
|
|
|
L [a-zA-Z_]
|
|
|
|
M [a-zA-Z_:]
|
|
|
|
|
2017-06-22 00:38:55 -07:00
|
|
|
%x lstateName lstateValue lstateTimestamp lstateLabels lstateLName lstateLValue lstateLValueIn
|
2017-01-14 07:39:04 -08:00
|
|
|
|
|
|
|
|
|
|
|
%yyc c
|
|
|
|
%yyn c = l.next()
|
2017-07-07 01:29:38 -07:00
|
|
|
%yyt l.state
|
2017-01-14 07:39:04 -08:00
|
|
|
|
|
|
|
|
|
|
|
%%
|
|
|
|
|
2017-01-15 04:55:53 -08:00
|
|
|
\0 return eof
|
|
|
|
#[^\r\n]*\n l.mstart = l.i
|
|
|
|
[\r\n \t]+ l.mstart = l.i
|
2017-01-14 07:39:04 -08:00
|
|
|
|
2017-07-18 03:35:41 -07:00
|
|
|
{M}({M}|{D})* l.state = lstateName
|
2017-01-15 04:55:53 -08:00
|
|
|
l.offsets = append(l.offsets, l.i)
|
2017-06-22 00:38:55 -07:00
|
|
|
l.mend = l.i
|
|
|
|
|
2017-07-07 01:29:38 -07:00
|
|
|
<lstateName>([ \t]*)\{ l.state = lstateLabels
|
2017-06-22 00:38:55 -07:00
|
|
|
|
2017-07-07 01:29:38 -07:00
|
|
|
<lstateName>[ \t]+ l.state = lstateValue
|
2017-06-22 00:38:55 -07:00
|
|
|
l.vstart = l.i
|
|
|
|
|
2017-01-14 07:39:04 -08:00
|
|
|
|
|
|
|
<lstateLabels>[ \t]+
|
2017-07-07 01:29:38 -07:00
|
|
|
<lstateLabels>,?\} l.state = lstateValue
|
2017-01-15 04:55:53 -08:00
|
|
|
l.mend = l.i
|
2017-07-07 01:29:38 -07:00
|
|
|
<lstateLabels>(,?[ \t]*) l.state = lstateLName
|
2017-01-15 04:55:53 -08:00
|
|
|
l.offsets = append(l.offsets, l.i)
|
2017-01-14 07:39:04 -08:00
|
|
|
|
2017-07-18 03:35:41 -07:00
|
|
|
<lstateLName>{L}({L}|{D})* l.offsets = append(l.offsets, l.i)
|
2017-07-07 01:29:38 -07:00
|
|
|
<lstateLName>[ \t]*= l.state = lstateLValue
|
2017-01-14 07:39:04 -08:00
|
|
|
|
2017-06-22 00:38:55 -07:00
|
|
|
<lstateLValue>[ \t]+
|
2017-07-07 01:29:38 -07:00
|
|
|
<lstateLValue>\" l.state = lstateLValueIn
|
2017-06-22 00:38:55 -07:00
|
|
|
l.offsets = append(l.offsets, l.i)
|
2017-07-07 01:29:38 -07:00
|
|
|
<lstateLValueIn>(\\.|[^\\"])*\" l.state = lstateLabels
|
2017-06-22 00:38:55 -07:00
|
|
|
if !utf8.Valid(l.b[l.offsets[len(l.offsets)-1]:l.i-1]) {
|
2017-10-23 06:57:30 -07:00
|
|
|
l.err = fmt.Errorf("invalid UTF-8 label value")
|
2017-06-16 05:09:50 -07:00
|
|
|
return -1
|
|
|
|
}
|
2017-01-14 10:30:19 -08:00
|
|
|
l.offsets = append(l.offsets, l.i-1)
|
2017-01-14 07:39:04 -08:00
|
|
|
|
2017-01-15 04:55:53 -08:00
|
|
|
<lstateValue>[ \t]+ l.vstart = l.i
|
2017-04-13 02:33:08 -07:00
|
|
|
<lstateValue>(NaN) l.val = math.Float64frombits(value.NormalNaN)
|
2017-07-07 01:29:38 -07:00
|
|
|
l.state = lstateTimestamp
|
2017-04-13 02:33:08 -07:00
|
|
|
|
2017-01-15 04:55:53 -08:00
|
|
|
<lstateValue>[^\n \t\r]+ // We don't parse strictly correct floats as the conversion
|
|
|
|
// repeats the effort anyway.
|
|
|
|
l.val, l.err = strconv.ParseFloat(yoloString(l.b[l.vstart:l.i]), 64)
|
|
|
|
if l.err != nil {
|
|
|
|
return -1
|
|
|
|
}
|
2017-07-07 01:29:38 -07:00
|
|
|
l.state = lstateTimestamp
|
2017-04-27 08:02:07 -07:00
|
|
|
|
2017-07-18 03:35:41 -07:00
|
|
|
<lstateTimestamp>[ \t]+ l.tstart = l.i
|
2017-04-27 08:02:07 -07:00
|
|
|
<lstateTimestamp>{D}+ ts, err := strconv.ParseInt(yoloString(l.b[l.tstart:l.i]), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
l.err = err
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
l.ts = &ts
|
|
|
|
<lstateTimestamp>[\r\n]+ l.nextMstart = l.i
|
2017-01-15 04:55:53 -08:00
|
|
|
return 1
|
2017-07-18 03:35:41 -07:00
|
|
|
<lstateTimestamp>\0 return 1
|
|
|
|
|
2017-01-14 07:39:04 -08:00
|
|
|
%%
|
2017-04-27 08:02:07 -07:00
|
|
|
l.err = fmt.Errorf("no token found")
|
2017-01-14 07:39:04 -08:00
|
|
|
return -1
|
2017-04-19 05:43:09 -07:00
|
|
|
}
|