jj/src/template.pest
Yuya Nishihara 1c0bde1a2b templater: add parsing rule for lambda expression
A lambda expression will be allowed only in .map() operation. The syntax is
borrowed from Rust closure.

In Mercurial, a map operation is implemented by context substitution. For
example, 'parents % "{node}"' prints parents[i].node for each. There are two
major problems: 1. the top-level context cannot be referred from the inner map
expression. 2. context of different types inserts arbitrarily-named keywords
(e.g. a dict type inserts "{key}" and "{value}", but how we could know.)

These issues should be avoided by using explicitly named parameters.

    parents.map(|parent| parent.commit_id ++ " " ++ commit_id)
                                                    ^^^^^^^^^ global keyword

A downside is that we can't reuse template fragment in map expression. Suppose
we have -T commit_summary, -T 'parents.map(commit_summary)' doesn't work.

    # only usable as a top-level template
    'commit_summary' = 'commit_id.short() ++ " " ++ description.first_line()'

Another problem is that a lambda expression might be confused with an alias
function.

    # .map(f) doesn't work, but .map(g) does
    'f(x)' = 'x'
    'g' = '|x| x'
2023-03-18 12:04:00 +09:00

74 lines
2 KiB
Text

// Copyright 2020 The Jujutsu Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Example:
// "commit: " ++ short(commit_id) ++ "\n"
// predecessors.map(|p| "predecessor: " ++ p.commit_id)
// parents.map(|p| p.commit_id ++ " is a parent of " ++ commit_id)
whitespace = _{ " " | "\t" | "\r" | "\n" | "\x0c" }
escape = @{ "\\" ~ ("t" | "r" | "n" | "\"" | "\\") }
literal_char = @{ !("\"" | "\\") ~ ANY }
raw_literal = @{ literal_char+ }
literal = { "\"" ~ (raw_literal | escape)* ~ "\"" }
integer_literal = {
ASCII_NONZERO_DIGIT ~ ASCII_DIGIT*
| "0"
}
identifier = @{ (ASCII_ALPHA | "_") ~ (ASCII_ALPHANUMERIC | "_")* }
function = { identifier ~ "(" ~ whitespace* ~ function_arguments ~ whitespace* ~ ")" }
function_arguments = {
template ~ (whitespace* ~ "," ~ whitespace* ~ template)* ~ (whitespace* ~ ",")?
| ""
}
lambda = {
"|" ~ whitespace* ~ formal_parameters ~ whitespace* ~ "|"
~ whitespace* ~ template
}
formal_parameters = {
identifier ~ (whitespace* ~ "," ~ whitespace* ~ identifier)* ~ (whitespace* ~ ",")?
| ""
}
primary = _{
("(" ~ whitespace* ~ template ~ whitespace* ~ ")")
| function
| lambda
| identifier
| literal
| integer_literal
}
term = {
primary ~ ("." ~ function)*
}
concat = _{
term ~ (whitespace* ~ "++" ~ whitespace* ~ term)+
}
template = { concat | term }
program = _{ SOI ~ whitespace* ~ template? ~ whitespace* ~ EOI }
function_alias_declaration = {
identifier ~ "(" ~ whitespace* ~ formal_parameters ~ whitespace* ~ ")"
}
alias_declaration = _{
SOI ~ (function_alias_declaration | identifier) ~ EOI
}