Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PRQL language #3736

Open
wants to merge 3 commits into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/languages/prism-prql.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { rest } from '../shared/symbols';
import type { LanguageProto } from '../types';

export default {
id: 'prql',
grammar: {
'doc-comment': {
pattern: /(^|[^\\])#!.*/,
lookbehind: true,
greedy: true
},
'comment': {
pattern: /(^|[^\\])#.*/,
lookbehind: true,
greedy: true
},
'string-interpolation': {
pattern: /(?:[frs])(?:("""|''')[\s\S]*?\1|("|')(?:\\.|(?!\2)[^\\\r\n])*\2)/i,
greedy: true,
inside: {
'interpolation': {
pattern: /((?:^|[^{])(?:\{\{)*)\{(?!\{)(?:[^{}]|\{(?!\{)(?:[^{}]|\{(?!\{)(?:[^{}])+\})+\})+\}/,
lookbehind: true,
inside: {
'format-spec': {
pattern: /(:)[^:(){}]+(?=\}$)/,
lookbehind: true
},
[rest]: 'prql'
}
},
'string': /[\s\S]+/
}
},
'triple-quoted-string': {
pattern: /(?:[frs]?("""|''')[\s\S]*?\1/i,
greedy: true,
alias: 'string'
},
'string': {
pattern: /(?:[frs])?("|')(?:\\.|(?!\1)[^\\\r\n])*\1/i,
greedy: true
},
'keyword': /\b(?:_(?=\s*:)aggregate|derive|filter|from|group|join|select|sort|take|window|let|into|case|prql|type|module|internal)\b/,
'builtin': /\b(?:min|max|sum|average|stddev|every|any|concat_array|count|lag|lead|first|last|rank|rank_dense|row_number|round|as|in|tuple_every|tuple_map|tuple_zip|_eq|_is_null|from_text|lower|upper|read_parquet|read_csv)\b/,
'boolean': /\b(?:false|null|true)\b/,
'number': /\b0(?:b(?:_?[01])+|o(?:_?[0-7])+|x(?:_?[a-f0-9])+)\b|(?:\b\d+(?:_\d+)*(?:\.(?:\d+(?:_\d+)*)?)?|\B\.\d+(?:_\d+)*)(?:e[+-]?\d+(?:_\d+)*)?j?(?!\w)/i,
'operator': /[-+%=]=?|!=|~=|\*\*?=?|\/\/?=?|<[<=>]?|>[=>]?|[&|^~]/,
'punctuation': /[{}[\](),.:]/
}
} as LanguageProto<'prql'>;
15 changes: 15 additions & 0 deletions tests/languages/prql/boolean_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
true
false
null

----------------------------------------------------

[
["boolean", "true"],
["boolean", "false"],
["boolean", "null"]
]

----------------------------------------------------

Checks for booleans.
15 changes: 15 additions & 0 deletions tests/languages/prql/builtin_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
min max sum
average stddev
every any

----------------------------------------------------

[
["builtin", "min"], ["builtin", "max"], ["builtin", "sum"],
["builtin", "average"], ["builtin", "stddev"],
["builtin", "every"], ["builtin", "any"]
]

----------------------------------------------------

Checks for builtins.
13 changes: 13 additions & 0 deletions tests/languages/prql/comment_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#
# foobar

----------------------------------------------------

[
["comment", "#"],
["comment", "# foobar"]
]

----------------------------------------------------

Checks for comments.
15 changes: 15 additions & 0 deletions tests/languages/prql/keyword_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
aggregate derive filter
from group join
select sort take

----------------------------------------------------

[
["keyword", "aggregate"], ["keyword", "derive"], ["keyword", "filter"]
["keyword", "from"], ["keyword", "group"], ["keyword", "join"],
["keyword", "select"], ["keyword", "sort"], ["keyword", "take"]
]

----------------------------------------------------

Checks for keywords.
45 changes: 45 additions & 0 deletions website/examples/prism-prql.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<h2>Comments</h2>
<pre><code># This is a comment</code></pre>

<h2>Strings</h2>
<pre><code>"foo \"bar\" baz"
'foo \'bar\' baz'
""" "Multi-line" strings
are supported."""
''' 'Multi-line' strings
are supported.'''</code></pre>

<h2>Numbers</h2>
<pre><code>7
2147483647
3
79228162514264337593543950336
3.14
1e100
3.14e-10
0e0
</code></pre>

<h2>Full example</h2>
<pre><code>from invoices
filter invoice_date >= @1970-01-16
derive {
transaction_fees = 0.8,
income = total - transaction_fees
}
filter income > 1
group customer_id (
aggregate {
average total,
sum_income = sum income,
ct = count total,
}
)
sort {-sum_income}
take 10
join c=customers (==customer_id)
derive name = f"{c.last_name}, {c.first_name}"
select {
c.customer_id, name, sum_income
}
derive db_version = s"version()"</code></pre>