| 1 |
// Copyright 2020 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 |
// Example: |
| 16 |
// "commit: " ++ short(commit_id) ++ "\n" |
| 17 |
// predecessors.map(|p| "predecessor: " ++ p.commit_id) |
| 18 |
// parents.map(|p| p.commit_id ++ " is a parent of " ++ commit_id) |
| 19 |
|
| 20 |
// If making significant changes to this grammar, consider also updating the |
| 21 |
// tree-sitter grammar: https://github.com/bryceberger/tree-sitter-jjtemplate |
| 22 |
|
| 23 |
WHITESPACE = _{ " " | "\t" | "\r" | "\n" | "\x0c" } |
| 24 |
|
| 25 |
string_escape = @{ |
| 26 |
"\\" |
| 27 |
~ ("t" | "r" | "n" | "0" | "e" | ("x" ~ ASCII_HEX_DIGIT{2}) | "\"" | "\\") |
| 28 |
} |
| 29 |
string_content_char = @{ !("\"" | "\\") ~ ANY } |
| 30 |
string_content = @{ string_content_char+ } |
| 31 |
string_literal = ${ "\"" ~ (string_content | string_escape)* ~ "\"" } |
| 32 |
|
| 33 |
raw_string_content = @{ (!"'" ~ ANY)* } |
| 34 |
raw_string_literal = ${ "'" ~ raw_string_content ~ "'" } |
| 35 |
|
| 36 |
integer_literal = @{ |
| 37 |
ASCII_NONZERO_DIGIT ~ ASCII_DIGIT* |
| 38 |
| "0" |
| 39 |
} |
| 40 |
|
| 41 |
identifier = @{ (ASCII_ALPHA | "_") ~ (ASCII_ALPHANUMERIC | "_")* } |
| 42 |
|
| 43 |
concat_op = { "++" } |
| 44 |
logical_or_op = { "||" } |
| 45 |
logical_and_op = { "&&" } |
| 46 |
eq_op = { "==" } |
| 47 |
ne_op = { "!=" } |
| 48 |
ge_op = { ">=" } |
| 49 |
gt_op = { ">" } |
| 50 |
le_op = { "<=" } |
| 51 |
lt_op = { "<" } |
| 52 |
add_op = { "+" } |
| 53 |
sub_op = { "-" } |
| 54 |
mul_op = { "*" } |
| 55 |
div_op = { "/" } |
| 56 |
rem_op = { "%" } |
| 57 |
logical_not_op = { "!" } |
| 58 |
negate_op = { "-" } |
| 59 |
pattern_kind_op = { ":" } |
| 60 |
prefix_ops = _{ logical_not_op | negate_op } |
| 61 |
infix_ops = _{ |
| 62 |
logical_or_op |
| 63 |
| logical_and_op |
| 64 |
| eq_op |
| 65 |
| ne_op |
| 66 |
| ge_op |
| 67 |
| gt_op |
| 68 |
| le_op |
| 69 |
| lt_op |
| 70 |
| add_op |
| 71 |
| sub_op |
| 72 |
| mul_op |
| 73 |
| div_op |
| 74 |
| rem_op |
| 75 |
} |
| 76 |
|
| 77 |
function = { identifier ~ "(" ~ function_arguments ~ ")" } |
| 78 |
keyword_argument = { identifier ~ "=" ~ template } |
| 79 |
argument = _{ keyword_argument | template } |
| 80 |
function_arguments = { |
| 81 |
argument ~ ("," ~ argument)* ~ ","? |
| 82 |
| "" |
| 83 |
} |
| 84 |
lambda = { |
| 85 |
"|" ~ formal_parameters ~ "|" ~ template |
| 86 |
} |
| 87 |
formal_parameters = { |
| 88 |
identifier ~ ("," ~ identifier)* ~ ","? |
| 89 |
| "" |
| 90 |
} |
| 91 |
|
| 92 |
// NOTE: Pattern identifiers additionally allow "-" in them, which results in |
| 93 |
// some oddness with the `-` operator. |
| 94 |
pattern_identifier = @{ (ASCII_ALPHA | "_") ~ (ASCII_ALPHANUMERIC | "_" | "-")* } |
| 95 |
pattern = ${ pattern_identifier ~ pattern_kind_op ~ pattern_value_expression } |
| 96 |
pattern_value_expression = { prefixed_term } |
| 97 |
|
| 98 |
primary = { |
| 99 |
("(" ~ template ~ ")") |
| 100 |
| function |
| 101 |
| lambda |
| 102 |
| pattern |
| 103 |
| identifier |
| 104 |
| string_literal |
| 105 |
| raw_string_literal |
| 106 |
| integer_literal |
| 107 |
} |
| 108 |
|
| 109 |
term = { |
| 110 |
primary ~ ("." ~ function)* |
| 111 |
} |
| 112 |
|
| 113 |
prefixed_term = _{ |
| 114 |
prefix_ops* ~ term |
| 115 |
} |
| 116 |
|
| 117 |
expression = { |
| 118 |
prefixed_term ~ (infix_ops ~ prefixed_term)* |
| 119 |
} |
| 120 |
|
| 121 |
// Whitespace is allowed in value part of <name>:(<template>) expression. |
| 122 |
template = !{ |
| 123 |
expression ~ (concat_op ~ expression)* |
| 124 |
} |
| 125 |
|
| 126 |
program = _{ SOI ~ template? ~ EOI } |
| 127 |
|
| 128 |
function_alias_declaration = { |
| 129 |
identifier ~ "(" ~ formal_parameters ~ ")" |
| 130 |
} |
| 131 |
pattern_alias_declaration = { |
| 132 |
pattern_identifier ~ pattern_kind_op ~ identifier |
| 133 |
} |
| 134 |
alias_declaration = _{ |
| 135 |
SOI ~ (function_alias_declaration |
| 136 |
| pattern_alias_declaration |
| 137 |
| identifier) ~ EOI |
| 138 |
} |