Skip to content

Commit

Permalink
tumbling windows (#18)
Browse files Browse the repository at this point in the history
* add window clause

* add window clause to FLWOR examples

* build wasm

* bump version

* fix: typo
  • Loading branch information
grantmacken committed Jun 5, 2023
1 parent dea4e0f commit 714cf41
Show file tree
Hide file tree
Showing 9 changed files with 161,616 additions and 118,755 deletions.
Binary file modified docs/tree-sitter-xquery.wasm
Binary file not shown.
10 changes: 10 additions & 0 deletions examples/spec/FLWOR_expressions.xq
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ let $x := $expr1, $y := $expr2
return ( $x + $y ),
let $e := fn:doc("emps.xml")/emps/emp[deptno eq $d]
return $e,
'3.12.4.1 Tumbling Windows',
for tumbling window $w in (2, 4, 6, 8, 10, 12, 14)
start at $s when fn:true()
only end at $e when $e - $s eq 2
return <window>{ $w }</window>,
'3.12.4.2 Sliding Windows',
for sliding window $w in (2, 4, 6, 8, 10, 12, 14)
start at $s when fn:true()
only end at $e when $e - $s eq 2
return avg($w),
'3.12.5 Where Clause',
for $x at $i in $inputvalues
where $i mod 100 = 0
Expand Down
19 changes: 9 additions & 10 deletions examples/spec/example.xq
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
"Hello, tree-sitter! ",
``[ time: `{current-time()}` ]``,
my:three-argument-function(1,2,3),
local:two-argument-function((1,2),3),
map {"A":1,"B":2},
['1',2, 3 ],
( 1 to 10 ) => sum(),
for $v in $doc//video return $v,
doc("books.xml")/bookstore/book[price<30],
element date { current-date()}
'3.12.4 Window Clause',
for tumbling window $w in (2, 4, 6, 8, 10, 12, 14)
start at $s when fn:true()
only end at $e when $e - $s eq 2
return avg($w),
for sliding window $w in (2, 4, 6, 8, 10, 12, 14)
start at $s when fn:true()
only end at $e when $e - $s eq 2
return <window>{ $w }</window>
48 changes: 33 additions & 15 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,21 +164,44 @@ module.exports = grammar({
//$.qname
),
flwor_expr: ($) => prec(2, seq($._initial_clause, repeat($._intermediate_clause), $.return_clause)), // 41
_initial_clause: ($) => choice($.for_clause, $.let_clause), // 42
_initial_clause: ($) => choice($.for_clause, $.let_clause, $._window_clause), // 42
_intermediate_clause: ($) => choice($._initial_clause, $.where_clause, $.group_by_clause, $.order_by_clause, $.count_clause), // 43',
for_clause: ($) => seq('for', $.for_binding, repeat(seq(',', $.for_binding))), // 44',
for_binding: ($) =>
seq(
'$',
$._EQName,
optional($.type_declaration),
optional(seq('allowing', 'empty')),
optional(seq('at', field('positional_variable', $.variable))),
'in',
field('binding_sequence', $._expr_single)
), // 45
seq('$', $._EQName, optional($.type_declaration), optional(seq('allowing', 'empty')), optional($._positional_var), 'in', field('binding_sequence', $._expr_single)), // 45
let_clause: ($) => seq('let', $.let_binding, repeat(seq(',', $.let_binding))), // 48
let_binding: ($) => seq('$', $._EQName, optional($.type_declaration), ':=', $._expr_single), // 49
_window_clause: ($) => choice($.tumbling_window_clause, $.sliding_window_clause),
//, optional($.type_declaration), 'in'
tumbling_window_clause: ($) =>
seq('for', 'tumbling', 'window', $.variable, optional($.type_declaration), 'in', $._expr_single, $.window_start_condition, optional($.window_end_condition)),
sliding_window_clause: ($) =>
seq('for', 'sliding', 'window', $.variable, optional($.type_declaration), 'in', $._expr_single, $.window_start_condition, optional($.window_end_condition)),
window_start_condition: ($) =>
seq(
'start',
optional(field('current_item', $.variable)),
optional($._positional_var),
optional(seq('previous', field('previous_item', $.variable))),
optional(seq('next', field('next_item', $.variable))),
'when',
$._expr_single
),
window_end_condition: ($) =>
seq(
optional('only'),
'end',
optional(field('current_item', $.variable)),
optional($._positional_var),
optional(seq('previous', field('previous_item', $.variable))),
optional(seq('next', field('next_item', $.variable))),
'when',
$._expr_single
),
_positional_var: ($) => seq('at', field('positional_variable', $.variable)),
current_item: ($) => seq('$', $._EQName),
previous_item: ($) => seq(''),
next_item: ($) => seq(''),
count_clause: ($) => seq('count', $.variable), // 59
where_clause: ($) => seq('where', $._expr_single), // 60
group_by_clause: ($) => seq('group', 'by', $.grouping_spec, repeat(seq(',', $.grouping_spec))), // 61
Expand Down Expand Up @@ -412,12 +435,7 @@ module.exports = grammar({
// 5 [{][`{\]] allow standalone { or {` or {{ or {[ which is not a interpolation start
// 6 `[^{] allow standalone ` which is not a interpolation start
interpolation: ($) => seq('`{', $._expr, '}`'), // 180',
// string_literal: ($) => choice(seq('"', $.string_quote_content, token.immediate('"')), seq("'", $.string_apos_content, token.immediate("'"))),
//string_literal: ($) => choice(seq('"', $.string_quote_content, token.immediate('"')), seq("'", $.string_apos_content, token.immediate("'"))),
string_literal: ($) => choice($._string_quote, $._string_apos),
//_direct_attribute_value: ($) => choice($.attr_quote_value, $.attr_apos_value),
//attr_quote_value: ($) => seq('"', repeat(choice($._common_content, $.escape_quote, alias(/[^"{}<&]+/, $.char_data))), '"'),
//attr_apos_value: ($) => seq("'", repeat(choice($._common_content, $.escape_apos, alias(/[^'{}<&]+/, $.char_data))), "'"),
_string_quote: ($) => seq('"', repeat($._string_quote_content), '"'),
_string_quote_content: ($) => choice($.predefined_entity_ref, $.char_ref, $.escape_quote, alias(/[^"&]+/, $.char_data)),
_string_apos: ($) => seq("'", repeat($._string_apos_content), "'"),
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "tree-sitter-xquery",
"description": "XQuery tree-sitter",
"version": "0.1.1",
"version": "0.1.2",
"author": "Grant Mackenzie <grantmacken@gmail.com>",
"license": "MIT",
"keywords": [
Expand Down
13 changes: 12 additions & 1 deletion queries/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
; (identifier) @type.builtin.atomic (#any-of? @type.builtin.atomic
; "anyAtomicType" "untypedAtomic" "dateTime" "dateTimeStamp" "time" "date" "duration" "yearMonthDuration" "dayTimeDuration" "float" "double" "decimal" "integer" "nonPositiveInteger" "negativeInteger" "long" "int" "short" "byte" "nonNegativeInteger" "unsignedLong" "unsignedInt" "unsignedShort" "unsignedByte" "positiveInteger" "gYearMonth" "gYear" "gMonthDay" "gDay" "gMonth" "string" "normalizedString" "token" "language" "NMTOKEN" "Name" "NCName" "ID" "IDREF" "ENTITY" "boolean" "base64Binary" "hexBinary" "anyURI" "QName" "NOTATION"
; ))
; 2.5.1 Predefined Schema Types
; 2.5.4 SequenceType Syntax
(type_declaration "as" @keyword) @type.declaration
(parenthesized_item_type . "(" ")" ) @type.parenthesized_item
Expand Down Expand Up @@ -110,6 +109,18 @@ computed_constructor: (_ .
;3.11.3.1 Unary Lookup
(unary_lookup "?" @operator.lookup.unary key: (_) @constant.key_specifier)
; 3.12 FLWOR Expressions TODO
(tumbling_window_clause . "for" "tumbling" "window" "in" ) @keyword.tumbling_window_clause
(sliding_window_clause . "for" "sliding" "window" "in" ) @keyword.sliding_window_clause
(window_start_condition "start" "at""previous" "next" "when" @keyword.window_start_condition )
(window_end_condition "end" "at" "previous" "next" "when" @keyword.window_end_condition)
(_
[
current_item: (variable) @variable.current
positional_variable: (variable) @variable.positional
previous_item: (variable) @variable.previous
next_item: (variable) @variable.next
]
)
(for_clause . "for") @keyword.for_clause
(for_binding (["allowing" "empty" "at" "in" ] @keyword.for_binding)*) @variable.for_binding
(let_clause . "let" ) @keyword.let_clause
Expand Down

0 comments on commit 714cf41

Please sign in to comment.