Repository: alecthomas/chroma
Pull request #422 (2020-11-10 23:20:52)

Add PromQL lexer
Hi,

I ported this PromQL lexer that I wrote for Pygments (it was [merged](https://github.com/pygments/pygments/pull/1506) on it some months ago) and I would like to use it on a [Hugo site](https://gohugo.io/content-management/syntax-highlighting/).
By the way, PromQL it's the Prometheus Query Language https://prometheus.io/docs/prometheus/latest/querying/basics/.

This PR also includes several test cases, passing with no issues and running:
```console
$ cmd/chroma/chroma -l promql lexers/testdata/promql.actual
```
work as expected.

But please don't hesitate to request any changes you think could be appropriate or provide any feedback. Thanks.




Patch:
diff --git a/lexers/p/promql.go b/lexers/p/promql.go
new file mode 100644
index 0000000..38982e1
--- /dev/null
+++ b/lexers/p/promql.go
@@ -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)),
+		},
+	},
+))


Test patch:
diff --git a/lexers/testdata/promql.actual b/lexers/testdata/promql.actual
new file mode 100644
index 0000000..6bc3e0f
--- /dev/null
+++ b/lexers/testdata/promql.actual
@@ -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",
+    ".*"
+)
diff --git a/lexers/testdata/promql.expected b/lexers/testdata/promql.expected
new file mode 100644
index 0000000..2560137
--- /dev/null
+++ b/lexers/testdata/promql.expected
@@ -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"}
+]