1
0
mirror of https://github.com/alecthomas/chroma.git synced 2025-02-09 13:23:51 +02:00

Add PromQL lexer

It also includes several test examples
This commit is contained in:
Pablo SEMINARIO 2020-11-11 00:01:39 +01:00 committed by Alec Thomas
parent 552ad244cf
commit eb0e31fc05
3 changed files with 260 additions and 0 deletions

55
lexers/p/promql.go Normal file
View File

@ -0,0 +1,55 @@
package p
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Promql lexer.
var Promql = internal.Register(MustNewLexer(
&Config{
Name: "PromQL",
Aliases: []string{"promql"},
Filenames: []string{"*.promql"},
MimeTypes: []string{},
},
Rules{
"root": {
{`\n`, TextWhitespace, nil},
{`\s+`, TextWhitespace, nil},
{`,`, Punctuation, nil},
{Words(``, `\b`, `bool`, `by`, `group_left`, `group_right`, `ignoring`, `offset`, `on`, `without`), Keyword, nil},
{Words(``, `\b`, `sum`, `min`, `max`, `avg`, `group`, `stddev`, `stdvar`, `count`, `count_values`, `bottomk`, `topk`, `quantile`), Keyword, nil},
{Words(``, `\b`, `abs`, `absent`, `absent_over_time`, `avg_over_time`, `ceil`, `changes`, `clamp_max`, `clamp_min`, `count_over_time`, `day_of_month`, `day_of_week`, `days_in_month`, `delta`, `deriv`, `exp`, `floor`, `histogram_quantile`, `holt_winters`, `hour`, `idelta`, `increase`, `irate`, `label_join`, `label_replace`, `ln`, `log10`, `log2`, `max_over_time`, `min_over_time`, `minute`, `month`, `predict_linear`, `quantile_over_time`, `rate`, `resets`, `round`, `scalar`, `sort`, `sort_desc`, `sqrt`, `stddev_over_time`, `stdvar_over_time`, `sum_over_time`, `time`, `timestamp`, `vector`, `year`), KeywordReserved, nil},
{`[1-9][0-9]*[smhdwy]`, LiteralString, nil},
{`-?[0-9]+\.[0-9]+`, LiteralNumberFloat, nil},
{`-?[0-9]+`, LiteralNumberInteger, nil},
{`#.*?$`, CommentSingle, nil},
{`(\+|\-|\*|\/|\%|\^)`, Operator, nil},
{`==|!=|>=|<=|<|>`, Operator, nil},
{`and|or|unless`, OperatorWord, nil},
{`[_a-zA-Z][a-zA-Z0-9_]+`, NameVariable, nil},
{`(["\'])(.*?)(["\'])`, ByGroups(Punctuation, LiteralString, Punctuation), nil},
{`\(`, Operator, Push("function")},
{`\)`, Operator, nil},
{`\{`, Punctuation, Push("labels")},
{`\[`, Punctuation, Push("range")},
},
"labels": {
{`\}`, Punctuation, Pop(1)},
{`\n`, TextWhitespace, nil},
{`\s+`, TextWhitespace, nil},
{`,`, Punctuation, nil},
{`([_a-zA-Z][a-zA-Z0-9_]*?)(\s*?)(=~|!=|=|~!)(\s*?)(")(.*?)(")`, ByGroups(NameLabel, TextWhitespace, Operator, TextWhitespace, Punctuation, LiteralString, Punctuation), nil},
},
"range": {
{`\]`, Punctuation, Pop(1)},
{`[1-9][0-9]*[smhdwy]`, LiteralString, nil},
},
"function": {
{`\)`, Operator, Pop(1)},
{`\(`, Operator, Push()},
Default(Pop(1)),
},
},
))

30
lexers/testdata/promql.actual vendored Normal file
View File

@ -0,0 +1,30 @@
# A metric with label filtering
go_gc_duration_seconds{instance="localhost:9090", job="alertmanager"}
# Aggregation operators
sum by (app, proc) (
instance_memory_limit_bytes - instance_memory_usage_bytes
) / 1024 / 1024
# Metric with multiple lables and whitespaces
go_gc_duration_seconds{ instance="localhost:9090", job="alertmanager" }
# Expression and comment
go_gc_duration_seconds{instance="localhost:9090"} # single comment
# Delta function
delta(cpu_temp_celsius{host="zeus"}[2h])
# Sum with arguments
sum by (app, proc) (instance_memory_usage_bytes)
# Multi-line with offset
label_replace(
avg by(instance)
(irate(node_cpu_seconds_total{mode = "idle"}[5m] offset 3s)
) * 100,
"device",
"cpu",
"instance",
".*"
)

175
lexers/testdata/promql.expected vendored Normal file
View File

@ -0,0 +1,175 @@
[
{"type":"CommentSingle","value":"# A metric with label filtering"},
{"type":"TextWhitespace","value":"\n"},
{"type":"NameVariable","value":"go_gc_duration_seconds"},
{"type":"Punctuation","value":"{"},
{"type":"NameLabel","value":"instance"},
{"type":"Operator","value":"="},
{"type":"Punctuation","value":"\""},
{"type":"LiteralString","value":"localhost:9090"},
{"type":"Punctuation","value":"\","},
{"type":"TextWhitespace","value":" "},
{"type":"NameLabel","value":"job"},
{"type":"Operator","value":"="},
{"type":"Punctuation","value":"\""},
{"type":"LiteralString","value":"alertmanager"},
{"type":"Punctuation","value":"\"}"},
{"type":"TextWhitespace","value":"\n\n"},
{"type":"CommentSingle","value":"# Aggregation operators"},
{"type":"TextWhitespace","value":"\n"},
{"type":"Keyword","value":"sum"},
{"type":"TextWhitespace","value":" "},
{"type":"Keyword","value":"by"},
{"type":"TextWhitespace","value":" "},
{"type":"Operator","value":"("},
{"type":"NameVariable","value":"app"},
{"type":"Punctuation","value":","},
{"type":"TextWhitespace","value":" "},
{"type":"NameVariable","value":"proc"},
{"type":"Operator","value":")"},
{"type":"TextWhitespace","value":" "},
{"type":"Operator","value":"("},
{"type":"TextWhitespace","value":"\n "},
{"type":"NameVariable","value":"instance_memory_limit_bytes"},
{"type":"TextWhitespace","value":" "},
{"type":"Operator","value":"-"},
{"type":"TextWhitespace","value":" "},
{"type":"NameVariable","value":"instance_memory_usage_bytes"},
{"type":"TextWhitespace","value":"\n"},
{"type":"Operator","value":")"},
{"type":"TextWhitespace","value":" "},
{"type":"Operator","value":"/"},
{"type":"TextWhitespace","value":" "},
{"type":"LiteralNumberInteger","value":"1024"},
{"type":"TextWhitespace","value":" "},
{"type":"Operator","value":"/"},
{"type":"TextWhitespace","value":" "},
{"type":"LiteralNumberInteger","value":"1024"},
{"type":"TextWhitespace","value":"\n\n"},
{"type":"CommentSingle","value":"# Metric with multiple lables and whitespaces"},
{"type":"TextWhitespace","value":"\n"},
{"type":"NameVariable","value":"go_gc_duration_seconds"},
{"type":"Punctuation","value":"{"},
{"type":"TextWhitespace","value":" "},
{"type":"NameLabel","value":"instance"},
{"type":"Operator","value":"="},
{"type":"Punctuation","value":"\""},
{"type":"LiteralString","value":"localhost:9090"},
{"type":"Punctuation","value":"\","},
{"type":"TextWhitespace","value":" "},
{"type":"NameLabel","value":"job"},
{"type":"Operator","value":"="},
{"type":"Punctuation","value":"\""},
{"type":"LiteralString","value":"alertmanager"},
{"type":"Punctuation","value":"\""},
{"type":"TextWhitespace","value":" "},
{"type":"Punctuation","value":"}"},
{"type":"TextWhitespace","value":"\n\n"},
{"type":"CommentSingle","value":"# Expression and comment"},
{"type":"TextWhitespace","value":"\n"},
{"type":"NameVariable","value":"go_gc_duration_seconds"},
{"type":"Punctuation","value":"{"},
{"type":"NameLabel","value":"instance"},
{"type":"Operator","value":"="},
{"type":"Punctuation","value":"\""},
{"type":"LiteralString","value":"localhost:9090"},
{"type":"Punctuation","value":"\"}"},
{"type":"TextWhitespace","value":" "},
{"type":"CommentSingle","value":"# single comment"},
{"type":"TextWhitespace","value":"\n\n"},
{"type":"CommentSingle","value":"# Delta function"},
{"type":"TextWhitespace","value":"\n"},
{"type":"KeywordReserved","value":"delta"},
{"type":"Operator","value":"("},
{"type":"NameVariable","value":"cpu_temp_celsius"},
{"type":"Punctuation","value":"{"},
{"type":"NameLabel","value":"host"},
{"type":"Operator","value":"="},
{"type":"Punctuation","value":"\""},
{"type":"LiteralString","value":"zeus"},
{"type":"Punctuation","value":"\"}["},
{"type":"LiteralString","value":"2h"},
{"type":"Punctuation","value":"]"},
{"type":"Operator","value":")"},
{"type":"TextWhitespace","value":"\n\n"},
{"type":"CommentSingle","value":"# Sum with arguments"},
{"type":"TextWhitespace","value":"\n"},
{"type":"Keyword","value":"sum"},
{"type":"TextWhitespace","value":" "},
{"type":"Keyword","value":"by"},
{"type":"TextWhitespace","value":" "},
{"type":"Operator","value":"("},
{"type":"NameVariable","value":"app"},
{"type":"Punctuation","value":","},
{"type":"TextWhitespace","value":" "},
{"type":"NameVariable","value":"proc"},
{"type":"Operator","value":")"},
{"type":"TextWhitespace","value":" "},
{"type":"Operator","value":"("},
{"type":"NameVariable","value":"instance_memory_usage_bytes"},
{"type":"Operator","value":")"},
{"type":"TextWhitespace","value":"\n\n"},
{"type":"CommentSingle","value":"# Multi-line with offset"},
{"type":"TextWhitespace","value":"\n"},
{"type":"KeywordReserved","value":"label_replace"},
{"type":"Operator","value":"("},
{"type":"TextWhitespace","value":"\n "},
{"type":"Keyword","value":"avg"},
{"type":"TextWhitespace","value":" "},
{"type":"Keyword","value":"by"},
{"type":"Operator","value":"("},
{"type":"NameVariable","value":"instance"},
{"type":"Operator","value":")"},
{"type":"TextWhitespace","value":"\n "},
{"type":"Operator","value":"("},
{"type":"KeywordReserved","value":"irate"},
{"type":"Operator","value":"("},
{"type":"NameVariable","value":"node_cpu_seconds_total"},
{"type":"Punctuation","value":"{"},
{"type":"NameLabel","value":"mode"},
{"type":"TextWhitespace","value":" "},
{"type":"Operator","value":"="},
{"type":"TextWhitespace","value":" "},
{"type":"Punctuation","value":"\""},
{"type":"LiteralString","value":"idle"},
{"type":"Punctuation","value":"\"}["},
{"type":"LiteralString","value":"5m"},
{"type":"Punctuation","value":"]"},
{"type":"TextWhitespace","value":" "},
{"type":"Keyword","value":"offset"},
{"type":"TextWhitespace","value":" "},
{"type":"LiteralString","value":"3s"},
{"type":"Operator","value":")"},
{"type":"TextWhitespace","value":"\n "},
{"type":"Operator","value":")"},
{"type":"TextWhitespace","value":" "},
{"type":"Operator","value":"*"},
{"type":"TextWhitespace","value":" "},
{"type":"LiteralNumberInteger","value":"100"},
{"type":"Punctuation","value":","},
{"type":"TextWhitespace","value":"\n "},
{"type":"Punctuation","value":"\""},
{"type":"LiteralString","value":"device"},
{"type":"Punctuation","value":"\","},
{"type":"TextWhitespace","value":"\n "},
{"type":"Punctuation","value":"\""},
{"type":"LiteralString","value":"cpu"},
{"type":"Punctuation","value":"\","},
{"type":"TextWhitespace","value":"\n "},
{"type":"Punctuation","value":"\""},
{"type":"LiteralString","value":"instance"},
{"type":"Punctuation","value":"\","},
{"type":"TextWhitespace","value":"\n "},
{"type":"Punctuation","value":"\""},
{"type":"LiteralString","value":".*"},
{"type":"Punctuation","value":"\""},
{"type":"TextWhitespace","value":"\n"},
{"type":"Operator","value":")"},
{"type":"TextWhitespace","value":"\n"}
]