summaryrefslogtreecommitdiffstats
path: root/src/formatter/spec.pest
diff options
context:
space:
mode:
authorZhenhui Xie <xiezh0831@yahoo.co.jp>2020-07-08 06:45:32 +0800
committerGitHub <noreply@github.com>2020-07-07 18:45:32 -0400
commitec76fafff08933f6f31fb99ea974bdb5ae97a0af (patch)
treebb2c822cdf291635f03d27677c419488ecf77f53 /src/formatter/spec.pest
parent0f52b7b12e8c1a2060aa873a68032937dfa2c044 (diff)
feat: refactor modules to use format strings (#1374)
Diffstat (limited to 'src/formatter/spec.pest')
-rw-r--r--src/formatter/spec.pest51
1 files changed, 43 insertions, 8 deletions
diff --git a/src/formatter/spec.pest b/src/formatter/spec.pest
index 36be53be5..400c58ef7 100644
--- a/src/formatter/spec.pest
+++ b/src/formatter/spec.pest
@@ -1,16 +1,51 @@
+// Expression
+//
+// The expression of the format string.
+//
+// Should be started with SOI and ended with EOI, with a format string in it.
expression = _{ SOI ~ value* ~ EOI }
-value = _{ text | variable | textgroup }
+value = _{ text | variable | textgroup | conditional }
-variable = { "$" ~ variable_name }
-variable_name = @{ char+ }
+// Variable
+//
+// A variable is defined as one of the following:
+//
+// - A valid variable name followed by a `$` character (`$[a-zA-Z_][a-zA-Z0-9_]*`),
+// e.g. `$variable`.
+//
+// - Some texts wrapped in a curly bracket (`${[^\(\)\[\]\\\${}]+}`),
+// e.g. `${env:HOST}`.
+variable = { "$" ~ (variable_name | variable_scope) }
+variable_name = @{ ('a'..'z' | 'A'..'Z' | "_") ~ char* }
char = _{ 'a'..'z' | 'A'..'Z' | '0'..'9' | "_" }
-text = { text_inner+ }
-text_inner = _{ text_inner_char | escape }
-text_inner_char = { !("[" | "]" | "(" | ")" | "$" | "\\") ~ ANY }
+variable_scope = _{ "{" ~ variable_scoped_name ~ "}" }
+variable_scoped_name = { scoped_char+ }
+scoped_char = _{ !(escaped_char | "{" | "}") ~ ANY }
+
+// Text
+//
+// Texts can be one of `string` or `escaped_char`, where string is one or more of
+// unescapable chars.
+//
+// This is implemented so as to ensure all functional characters are escaped.
+text = { (string | escape)+ }
+string = @{ text_inner_char+ }
+text_inner_char = { !escaped_char ~ ANY }
escape = _{ "\\" ~ escaped_char }
escaped_char = { "[" | "]" | "(" | ")" | "\\" | "$" }
+// TextGroup
+//
+// A textgroup is a pair of `format` and `style` (`[format](style)`)
+//
+// - `format`: A format string, can contain any number of variables, texts or textgroups.
+// - `style`: A style string, can contain any number of variables or texts.
textgroup = { "[" ~ format ~ "]" ~ "(" ~ style ~ ")" }
-format = { (variable | text | textgroup)* }
-style = { (variable | text)* }
+format = { value* }
+style = { (variable | string)* }
+
+// Conditional
+//
+// A conditional format string that won't render if all the containing variables are empty.
+conditional = { "(" ~ format ~ ")" }