summaryrefslogtreecommitdiff
path: root/src/syntax/expr.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2021-01-30 12:09:26 +0100
committerLaurenz <laurmaedje@gmail.com>2021-01-30 12:09:26 +0100
commit89eb8bae49edb71d9a9279a2210bb1ccaf4dd707 (patch)
tree160b1a2ff41b5bba8a58f882df9d10c9eb036cf2 /src/syntax/expr.rs
parentac24075469f171fe83a976b9a97b9b1ea078a7e3 (diff)
New syntax 💎
- Everything everywhere! - Blocks with curly braces: {} - Templates with brackets: [] - Function templates with hashtag: `#[f]` - Headings with equals sign: `= Introduction`
Diffstat (limited to 'src/syntax/expr.rs')
-rw-r--r--src/syntax/expr.rs33
1 files changed, 20 insertions, 13 deletions
diff --git a/src/syntax/expr.rs b/src/syntax/expr.rs
index ca55bdd0..7a055cc7 100644
--- a/src/syntax/expr.rs
+++ b/src/syntax/expr.rs
@@ -43,7 +43,7 @@ pub enum Expr {
Unary(ExprUnary),
/// A binary operation: `a + b`.
Binary(ExprBinary),
- /// An invocation of a function: `foo(...)`, `[foo ...]`.
+ /// An invocation of a function: `foo(...)`, `#[foo ...]`.
Call(ExprCall),
/// A let expression: `#let x = 1`.
Let(ExprLet),
@@ -75,11 +75,7 @@ impl Pretty for Expr {
Self::Str(v) => write!(p, "{:?}", &v).unwrap(),
Self::Array(v) => v.pretty(p),
Self::Dict(v) => v.pretty(p),
- Self::Template(v) => {
- p.push_str("[");
- v.pretty(p);
- p.push_str("]");
- }
+ Self::Template(v) => pretty_template(v, p),
Self::Group(v) => {
p.push_str("(");
v.v.pretty(p);
@@ -146,6 +142,17 @@ impl Pretty for Named {
/// A template expression: `[*Hi* there!]`.
pub type ExprTemplate = Tree;
+/// Pretty print a template.
+pub fn pretty_template(template: &ExprTemplate, p: &mut Printer) {
+ if let [Spanned { v: Node::Expr(Expr::Call(call)), .. }] = template.as_slice() {
+ pretty_func_template(call, p, false)
+ } else {
+ p.push_str("[");
+ template.pretty(p);
+ p.push_str("]");
+ }
+}
+
/// A grouped expression: `(1 + 2)`.
pub type ExprGroup = SpanBox<Expr>;
@@ -400,7 +407,7 @@ pub enum Associativity {
Right,
}
-/// An invocation of a function: `foo(...)`, `[foo ...]`.
+/// An invocation of a function: `foo(...)`, `#[foo ...]`.
#[derive(Debug, Clone, PartialEq)]
pub struct ExprCall {
/// The callee of the function.
@@ -418,12 +425,12 @@ impl Pretty for ExprCall {
}
}
-/// Pretty print a bracketed function call, with body or chaining when possible.
-pub fn pretty_bracket_call(call: &ExprCall, p: &mut Printer, chained: bool) {
+/// Pretty print a function template, with body or chaining when possible.
+pub fn pretty_func_template(call: &ExprCall, p: &mut Printer, chained: bool) {
if chained {
p.push_str(" | ");
} else {
- p.push_str("[");
+ p.push_str("#[");
}
// Function name.
@@ -431,7 +438,7 @@ pub fn pretty_bracket_call(call: &ExprCall, p: &mut Printer, chained: bool) {
// Find out whether this can be written with a body or as a chain.
//
- // Example: Transforms "[v [Hi]]" => "[v][Hi]".
+ // Example: Transforms "#[v [Hi]]" => "#[v][Hi]".
if let [head @ .., Argument::Pos(Spanned { v: Expr::Template(template), .. })] =
call.args.v.as_slice()
{
@@ -443,9 +450,9 @@ pub fn pretty_bracket_call(call: &ExprCall, p: &mut Printer, chained: bool) {
// Find out whether this can written as a chain.
//
- // Example: Transforms "[v][[f]]" => "[v | f]".
+ // Example: Transforms "#[v][[f]]" => "#[v | f]".
if let [Spanned { v: Node::Expr(Expr::Call(call)), .. }] = template.as_slice() {
- return pretty_bracket_call(call, p, true);
+ return pretty_func_template(call, p, true);
} else {
p.push_str("][");
template.pretty(p);