Repository: alecthomas/chroma
Pull request #474 (2021-04-24 19:49:56)

 Improve PromQL lexer with new test cases
This PR adds 2 minor improvements/fixes into the PromQL lexer:

* Values for labels can now be enclosed within single or double quotes (only double quotes were supported before). Eg: `metric{proc='web'}` and `metric{proc="web"}` will work as expected. Initial report [here](https://github.com/pabluk/pygments-promql/issues/4).
*  There was a typo into the regex used for labels. Correct label matching operator is `!~` instead of `~!`. See the [label matching operators documentation](https://prometheus.io/docs/prometheus/latest/querying/basics/#instant-vector-selectors) for more details.

These changes were already applied into https://github.com/pygments/pygments/pull/1783.

Please feel free to ask for any changes, or edit it as you want.

Thank you for your time and all the work done on Chroma! 


Patch:
diff --git a/lexers/p/promql.go b/lexers/p/promql.go
index 8d20b89..e80b926 100644
--- a/lexers/p/promql.go
+++ b/lexers/p/promql.go
@@ -44,7 +44,7 @@ func promqlRules() Rules {
 			{`\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},
+			{`([_a-zA-Z][a-zA-Z0-9_]*?)(\s*?)(=~|!=|=|!~)(\s*?)("|')(.*?)("|')`, ByGroups(NameLabel, TextWhitespace, Operator, TextWhitespace, Punctuation, LiteralString, Punctuation), nil},
 		},
 		"range": {
 			{`\]`, Punctuation, Pop(1)},


Test patch:
diff --git a/lexers/testdata/promql.actual b/lexers/testdata/promql.actual
index 6bc3e0f..9392bb2 100644
--- a/lexers/testdata/promql.actual
+++ b/lexers/testdata/promql.actual
@@ -28,3 +28,9 @@ label_replace(
     "instance",
     ".*"
 )
+
+# Values for labels enclosed within single quotes
+metric_test_app{app='turtle',proc='web'}
+
+# Use label matching operator `!~`
+metric_test_app{status!~'(4|5)..'}
diff --git a/lexers/testdata/promql.expected b/lexers/testdata/promql.expected
index dd02289..9d572e6 100644
--- a/lexers/testdata/promql.expected
+++ b/lexers/testdata/promql.expected
@@ -165,5 +165,30 @@
   {"type":"Punctuation","value":"\""},
   {"type":"TextWhitespace","value":"\n"},
   {"type":"Operator","value":")"},
+  {"type":"TextWhitespace","value":"\n\n"},
+  {"type":"CommentSingle","value":"# Values for labels enclosed within single quotes"},
+  {"type":"TextWhitespace","value":"\n"},
+  {"type":"NameVariable","value":"metric_test_app"},
+  {"type":"Punctuation","value":"{"},
+  {"type":"NameLabel","value":"app"},
+  {"type":"Operator","value":"="},
+  {"type":"Punctuation","value":"'"},
+  {"type":"LiteralString","value":"turtle"},
+  {"type":"Punctuation","value":"',"},
+  {"type":"NameLabel","value":"proc"},
+  {"type":"Operator","value":"="},
+  {"type":"Punctuation","value":"'"},
+  {"type":"LiteralString","value":"web"},
+  {"type":"Punctuation","value":"'}"},
+  {"type":"TextWhitespace","value":"\n\n"},
+  {"type":"CommentSingle","value":"# Use label matching operator `!~`"},
+  {"type":"TextWhitespace","value":"\n"},
+  {"type":"NameVariable","value":"metric_test_app"},
+  {"type":"Punctuation","value":"{"},
+  {"type":"NameLabel","value":"status"},
+  {"type":"Operator","value":"!~"},
+  {"type":"Punctuation","value":"'"},
+  {"type":"LiteralString","value":"(4|5).."},
+  {"type":"Punctuation","value":"'}"},
   {"type":"TextWhitespace","value":"\n"}
 ]