Skip to content

Commit 24862cd

Browse files
feat: Add syntax highlighting for PRQL (#5307)
Adds a syntax highlighting mode for the PRQL query language. PRQL is a modern language for transforming data — a simple, powerful, pipelined SQL replacement. https://prql-lang.org/ https://github.com/PRQL/prql
1 parent 6c18338 commit 24862cd

File tree

4 files changed

+161
-0
lines changed

4 files changed

+161
-0
lines changed

demo/kitchen-sink/docs/prql.prql

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from invoices
2+
filter invoice_date >= @1970-01-16
3+
derive {
4+
transaction_fees = 0.8,
5+
income = total - transaction_fees
6+
}
7+
filter income > 1
8+
group customer_id (
9+
aggregate {
10+
average total,
11+
sum_income = sum income,
12+
ct = count total,
13+
}
14+
)
15+
sort {-sum_income}
16+
take 10
17+
join c=customers (==customer_id)
18+
derive name = f"{c.last_name}, {c.first_name}"
19+
select {
20+
c.customer_id, name, sum_income
21+
}
22+
derive db_version = s"version()"

src/ext/modelist.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ var supportedModes = {
170170
Prolog: ["plg|prolog"],
171171
Properties: ["properties"],
172172
Protobuf: ["proto"],
173+
PRQL: ["prql"],
173174
Puppet: ["epp|pp"],
174175
Python: ["py"],
175176
QML: ["qml"],

src/mode/prql.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"use strict";
2+
3+
var oop = require("../lib/oop");
4+
var TextMode = require("./text").Mode;
5+
var HighlightRules = require("./prql_highlight_rules").PrqlHighlightRules;
6+
var FoldMode = require("./folding/cstyle").FoldMode;
7+
8+
var Mode = function() {
9+
this.HighlightRules = HighlightRules;
10+
this.foldingRules = new FoldMode();
11+
this.$behaviour = this.$defaultBehaviour;
12+
};
13+
oop.inherits(Mode, TextMode);
14+
15+
(function() {
16+
this.lineCommentStart = "#";
17+
// Extra logic goes here.
18+
this.$id = "ace/mode/prql";
19+
}).call(Mode.prototype);
20+
21+
exports.Mode = Mode;

src/mode/prql_highlight_rules.js

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// https://prql-lang.org/
2+
// https://github.com/PRQL/prql
3+
4+
"use strict";
5+
6+
var oop = require("../lib/oop");
7+
var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
8+
9+
var PrqlHighlightRules = function() {
10+
var builtinFunctions = "min|max|sum|average|stddev|every|any|concat_array|count|" +
11+
"lag|lead|first|last|rank|rank_dense|row_number|" +
12+
"round|as|in|" +
13+
"tuple_every|tuple_map|tuple_zip|_eq|_is_null|" +
14+
"from_text|" +
15+
"lower|upper|" +
16+
"read_parquet|read_csv";
17+
18+
var builtinTypes = [
19+
"bool",
20+
"int",
21+
"int8",
22+
"int16",
23+
"int32",
24+
"int64",
25+
"float",
26+
"text",
27+
"set"].join("|");
28+
29+
var keywordMapper = this.createKeywordMapper({
30+
"constant.language": "null",
31+
"constant.language.boolean": "true|false",
32+
"keyword": "let|into|case|prql|type|module|internal",
33+
"storage.type": "let|func",
34+
"support.function": builtinFunctions,
35+
"support.type": builtinTypes
36+
}, "identifier");
37+
38+
var escapeRe = /\\(\d+|['"\\&bfnrt]|u[0-9a-fA-F]{4})/;
39+
var identifierRe = /[A-Za-z_][a-z_A-Z0-9]/.source;
40+
var bidi = "[\\u202A\\u202B\\u202D\\u202E\\u2066\\u2067\\u2068\\u202C\\u2069]";
41+
42+
this.$rules = {
43+
start: [{
44+
token: "string.start",
45+
regex: '"',
46+
next: "string"
47+
}, {
48+
token: "string.character",
49+
regex: "'(?:" + escapeRe.source + "|.)'?"
50+
}, {
51+
token : "constant.language",
52+
regex : "^" + identifierRe + "*"
53+
}, {
54+
token : "constant.numeric", // hexadecimal, octal and binary
55+
regex : /0(?:[xX][0-9a-fA-F]+|[oO][0-7]+|[bB][01]+)\b/
56+
}, {
57+
token : "constant.numeric", // decimal integers and floats
58+
regex : /(?:\d\d*(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+\b)?/
59+
}, {
60+
token: "comment.block",
61+
regex: "#!.*"
62+
}, {
63+
token: "comment.line",
64+
regex: "#.*"
65+
}, {
66+
token : "keyword.operator",
67+
regex : /->|=>|==|!=|>=|<=|~=|&&|\|\||\?\?|\/\/|@/
68+
}, {
69+
token: "invalid.illegal",
70+
regex: bidi
71+
}, {
72+
token : "punctuation.operator",
73+
regex : /[,`]/
74+
}, {
75+
token : keywordMapper,
76+
regex : "[\\w\\xff-\\u218e\\u2455-\\uffff]+\\b"
77+
}, {
78+
token: "paren.lparen",
79+
regex: /[\[({]/
80+
}, {
81+
token: "paren.rparen",
82+
regex: /[\])}]/
83+
} ],
84+
string: [{
85+
token: "constant.character.escape",
86+
regex: escapeRe
87+
}, {
88+
token: "text",
89+
regex: /\\(\s|$)/,
90+
next: "stringGap"
91+
}, {
92+
token: "string.end",
93+
regex: '"',
94+
next: "start"
95+
}, {
96+
token: "invalid.illegal",
97+
regex: bidi
98+
}, {
99+
defaultToken: "string"
100+
}],
101+
stringGap: [{
102+
token: "text",
103+
regex: /\\/,
104+
next: "string"
105+
}, {
106+
token: "error",
107+
regex: "",
108+
next: "start"
109+
}]
110+
};
111+
112+
this.normalizeRules();
113+
};
114+
115+
oop.inherits(PrqlHighlightRules, TextHighlightRules);
116+
117+
exports.PrqlHighlightRules = PrqlHighlightRules;

0 commit comments

Comments
 (0)