Debian Codegraph @

lib/src/revset.pest

raw
1 // Copyright 2021 The Jujutsu Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // If making significant changes to this grammar, consider also updating the
16 // tree-sitter grammar: https://github.com/bryceberger/tree-sitter-jjrevset
17
18 whitespace = _{ " " | "\t" | "\r" | "\n" | "\x0c" }
19
20 // XID_CONTINUE: https://www.unicode.org/reports/tr31/#Default_Identifier_Syntax
21 // +, -, .: often included in tag/bookmark name or version number
22 // *: glob characters that won't be used as operators
23 // /: sometimes used as a tag/bookmark namespace separator
24 identifier_part = @{ (XID_CONTINUE | "_" | "*" | "/")+ }
25 // '..' conflicts with the range operator, '++' conflicts with template concatenation,
26 // but '--' has no known conflicts.
27 identifier = @{
28 identifier_part ~ (("." | "-"+ | "+") ~ identifier_part)*
29 }
30 // TODO: remove "/", ".", "+" for consistency with fileset?
31 strict_identifier_part = @{ (ASCII_ALPHANUMERIC | "_" | "/")+ }
32 strict_identifier = @{
33 strict_identifier_part ~ (("." | "-" | "+") ~ strict_identifier_part)*
34 }
35
36 symbol = _{
37 identifier
38 | string_literal
39 | raw_string_literal
40 }
41
42 string_escape = @{
43 "\\"
44 ~ ("t" | "r" | "n" | "0" | "e" | ("x" ~ ASCII_HEX_DIGIT{2}) | "\"" | "\\")
45 }
46 string_content_char = @{ !("\"" | "\\") ~ ANY }
47 string_content = @{ string_content_char+ }
48 string_literal = ${ "\"" ~ (string_content | string_escape)* ~ "\"" }
49
50 raw_string_content = @{ (!"'" ~ ANY)* }
51 raw_string_literal = ${ "'" ~ raw_string_content ~ "'" }
52
53 at_op = { "@" }
54 pattern_kind_op = { ":" }
55
56 parents_op = { "-" }
57 children_op = { "+" }
58 compat_parents_op = { "^" }
59
60 dag_range_op = { "::" }
61 dag_range_pre_op = { "::" }
62 dag_range_post_op = { "::" }
63 dag_range_all_op = { "::" }
64 compat_dag_range_op = { ":" }
65 compat_dag_range_pre_op = { ":" }
66 compat_dag_range_post_op = { ":" }
67 range_op = { ".." }
68 range_pre_op = { ".." }
69 range_post_op = { ".." }
70 range_all_op = { ".." }
71 range_ops = _{ dag_range_op | compat_dag_range_op | range_op }
72 range_pre_ops = _{ dag_range_pre_op | compat_dag_range_pre_op | range_pre_op }
73 range_post_ops = _{ dag_range_post_op | compat_dag_range_post_op | range_post_op }
74 range_all_ops = _{ dag_range_all_op | range_all_op }
75
76 negate_op = { "~" }
77 union_op = { "|" }
78 intersection_op = { "&" }
79 difference_op = { "~" }
80 compat_add_op = { "+" }
81 compat_sub_op = { "-" }
82 infix_op = _{ union_op | intersection_op | difference_op | compat_add_op | compat_sub_op }
83
84 function = { function_name ~ "(" ~ whitespace* ~ function_arguments ~ whitespace* ~ ")" }
85 function_name = @{ (ASCII_ALPHA | "_") ~ (ASCII_ALPHANUMERIC | "_")* }
86 keyword_argument = { strict_identifier ~ whitespace* ~ "=" ~ whitespace* ~ expression }
87 argument = _{ keyword_argument | expression }
88 function_arguments = {
89 argument ~ (whitespace* ~ "," ~ whitespace* ~ argument)* ~ (whitespace* ~ ",")?
90 | ""
91 }
92 formal_parameters = {
93 strict_identifier ~ (whitespace* ~ "," ~ whitespace* ~ strict_identifier)* ~ (whitespace* ~ ",")?
94 | ""
95 }
96
97 pattern = { strict_identifier ~ pattern_kind_op ~ pattern_value_expression }
98 pattern_value_expression = { neighbors_expression }
99
100 primary = {
101 "(" ~ whitespace* ~ expression ~ whitespace* ~ ")"
102 | function
103 | pattern
104 // "@" operator cannot be nested
105 | symbol ~ at_op ~ symbol
106 | symbol ~ at_op
107 | symbol
108 | at_op
109 }
110
111 neighbors_expression = _{ primary ~ (parents_op | children_op | compat_parents_op)* }
112
113 range_expression = _{
114 neighbors_expression ~ range_ops ~ neighbors_expression
115 | neighbors_expression ~ range_post_ops
116 | range_pre_ops ~ neighbors_expression
117 | neighbors_expression
118 | range_all_ops
119 }
120
121 expression = {
122 (negate_op ~ whitespace*)* ~ range_expression
123 ~ (whitespace* ~ infix_op ~ whitespace* ~ (negate_op ~ whitespace*)* ~ range_expression)*
124 }
125
126 program = _{
127 SOI ~ whitespace* ~ expression ~ whitespace* ~ EOI
128 }
129
130 symbol_name = _{ SOI ~ symbol ~ EOI }
131
132 function_alias_declaration = {
133 function_name ~ "(" ~ whitespace* ~ formal_parameters ~ whitespace* ~ ")"
134 }
135 pattern_alias_declaration = {
136 strict_identifier ~ pattern_kind_op ~ strict_identifier
137 }
138 alias_declaration = _{
139 SOI ~ (function_alias_declaration
140 | pattern_alias_declaration
141 | strict_identifier) ~ EOI
142 }