Debian Codegraph @

src/grammar.pest

raw
1 //! Pest grammar for handlebars templating
2 WHITESPACE = _{ " "|"\t"|"\n"|"\r" }
3 keywords = { "as" | "else" }
4
5 escape = @{ ("\\" ~ "{{" ~ "{{"?) | ("\\" ~ "\\"+ ~ &"{{") }
6 raw_text = ${ ( escape | (!"{{" ~ ANY) )+ }
7 raw_block_text = ${ ( escape | (!"{{{{" ~ ANY) )* }
8
9 literal = { string_literal |
10 array_literal |
11 object_literal |
12 number_literal |
13 null_literal |
14 boolean_literal }
15
16 null_literal = @{ "null" ~ !symbol_char }
17 boolean_literal = @{ ("true"|"false") ~ !symbol_char }
18 number_literal = @{ "-"? ~ ASCII_DIGIT+ ~ "."? ~ ASCII_DIGIT* ~ ("E" ~ "-"? ~ ASCII_DIGIT+)? ~ !symbol_char }
19 json_char_double_quote = {
20 !("\"" | "\\") ~ ANY
21 | "\\" ~ ("\"" | "\\" | "/" | "b" | "f" | "n" | "r" | "t")
22 | "\\" ~ ("u" ~ ASCII_HEX_DIGIT{4})
23 }
24 json_char_single_quote = {
25 !("'" | "\\") ~ ANY
26 | "\\" ~ ("'" | "\\" | "/" | "b" | "f" | "n" | "r" | "t")
27 | "\\" ~ ("u" ~ ASCII_HEX_DIGIT{4})
28 }
29 string_inner_double_quote = @{ json_char_double_quote* }
30 string_inner_single_quote = @{ json_char_single_quote* }
31 string_literal = ${ ("\"" ~ string_inner_double_quote ~ "\"") | ("'" ~ string_inner_single_quote ~ "'") }
32 array_literal = { "[" ~ literal? ~ ("," ~ literal)* ~ "]" }
33 object_literal = { "{" ~ (string_literal ~ ":" ~ literal)?
34 ~ ("," ~ string_literal ~ ":" ~ literal)* ~ "}" }
35
36 symbol_char = _{ASCII_ALPHANUMERIC|"-"|"_"|"$"|'\u{80}'..'\u{7ff}'|'\u{800}'..'\u{ffff}'|'\u{10000}'..'\u{10ffff}'}
37 partial_symbol_char = _{ASCII_ALPHANUMERIC|"-"|"_"|'\u{80}'..'\u{7ff}'|'\u{800}'..'\u{ffff}'|'\u{10000}'..'\u{10ffff}'|"/"|"."}
38 path_char = _{ "/" }
39
40 identifier = @{ symbol_char+ }
41 partial_identifier = @{ partial_symbol_char+ | ("[" ~ ANY+ ~ "]") | ("'" ~ (!"'" ~ ("\\'" | ANY))+ ~ "'") }
42 reference = ${ path_inline }
43
44 name = _{ subexpression | reference }
45
46 helper_parameter = { !(keywords ~ !symbol_char) ~ (literal | reference | subexpression) }
47 hash = { identifier ~ "=" ~ helper_parameter }
48 block_param = { "as" ~ "|" ~ identifier ~ identifier? ~ "|"}
49 exp_line = _{ identifier ~ (hash|helper_parameter)* ~ block_param?}
50 partial_exp_line = _{ ((partial_identifier|name) ~ (hash|helper_parameter)*) }
51
52 subexpression = { "(" ~ ((identifier ~ (hash|helper_parameter)+) | reference) ~ ")" }
53
54 leading_tilde_to_omit_whitespace = { "~" }
55 trailing_tilde_to_omit_whitespace = { "~" }
56
57 expression = { !(invert_tag|invert_chain_tag) ~ "{{" ~ leading_tilde_to_omit_whitespace? ~
58 ((identifier ~ (hash|helper_parameter)+) | name )
59 ~ trailing_tilde_to_omit_whitespace? ~ "}}" }
60 html_expression_triple_bracket_legacy = _{ "{{{" ~ leading_tilde_to_omit_whitespace? ~
61 ((identifier ~ (hash|helper_parameter)+) | name ) ~
62 trailing_tilde_to_omit_whitespace? ~ "}}}" }
63 html_expression_triple_bracket = _{ "{{" ~ leading_tilde_to_omit_whitespace? ~ "{" ~
64 ((identifier ~ (hash|helper_parameter)+) | name ) ~
65 "}" ~ trailing_tilde_to_omit_whitespace? ~ "}}" }
66
67 amp_expression = _{ "{{" ~ leading_tilde_to_omit_whitespace? ~ "&" ~ name ~
68 trailing_tilde_to_omit_whitespace? ~ "}}" }
69 html_expression = { (html_expression_triple_bracket_legacy | html_expression_triple_bracket)
70 | amp_expression }
71
72 decorator_expression = { "{{" ~ leading_tilde_to_omit_whitespace? ~ "*" ~ exp_line ~
73 trailing_tilde_to_omit_whitespace? ~ "}}" }
74 partial_expression = { "{{" ~ leading_tilde_to_omit_whitespace? ~ ">" ~ partial_exp_line
75 ~ trailing_tilde_to_omit_whitespace? ~ "}}" }
76
77 invert_tag_item = { "else"|"^" }
78 invert_tag = { !escape ~ "{{" ~ leading_tilde_to_omit_whitespace? ~ invert_tag_item
79 ~ trailing_tilde_to_omit_whitespace? ~ "}}" }
80 invert_chain_tag = { !escape ~ "{{" ~ leading_tilde_to_omit_whitespace? ~ invert_tag_item
81 ~ exp_line ~ trailing_tilde_to_omit_whitespace? ~ "}}" }
82 helper_block_start = { "{{" ~ leading_tilde_to_omit_whitespace? ~ "#" ~ exp_line ~
83 trailing_tilde_to_omit_whitespace? ~ "}}" }
84 helper_block_end = { "{{" ~ leading_tilde_to_omit_whitespace? ~ "/" ~ identifier ~
85 trailing_tilde_to_omit_whitespace? ~ "}}" }
86 helper_block = _{ helper_block_start ~ template ~
87 (invert_chain_tag ~ template)* ~ (invert_tag ~ template)? ~ helper_block_end }
88
89 decorator_block_start = { "{{" ~ leading_tilde_to_omit_whitespace? ~ "#" ~ "*"
90 ~ exp_line ~ trailing_tilde_to_omit_whitespace? ~ "}}" }
91 decorator_block_end = { "{{" ~ leading_tilde_to_omit_whitespace? ~ "/" ~ identifier ~
92 trailing_tilde_to_omit_whitespace? ~ "}}" }
93 decorator_block = _{ decorator_block_start ~ template ~
94 decorator_block_end }
95
96 partial_block_start = { "{{" ~ leading_tilde_to_omit_whitespace? ~ "#" ~ ">"
97 ~ partial_exp_line ~ trailing_tilde_to_omit_whitespace? ~ "}}" }
98 partial_block_end = { "{{" ~ leading_tilde_to_omit_whitespace? ~ "/" ~ partial_identifier ~
99 trailing_tilde_to_omit_whitespace? ~ "}}" }
100 partial_block = _{ partial_block_start ~ template ~ partial_block_end }
101
102 raw_block_start = { "{{{{" ~ leading_tilde_to_omit_whitespace? ~ exp_line ~
103 trailing_tilde_to_omit_whitespace? ~ "}}}}" }
104 raw_block_end = { "{{{{" ~ leading_tilde_to_omit_whitespace? ~ "/" ~ identifier ~
105 trailing_tilde_to_omit_whitespace? ~ "}}}}" }
106 raw_block = _{ raw_block_start ~ raw_block_text ~ raw_block_end }
107
108 hbs_comment = { "{{!" ~ "--" ~ (!"--}}" ~ ANY)* ~ "--" ~ "}}" }
109 hbs_comment_compact = { "{{!" ~ (!"}}" ~ ANY)* ~ "}}" }
110
111 template = { (
112 raw_text |
113 expression |
114 html_expression |
115 helper_block |
116 raw_block |
117 hbs_comment |
118 hbs_comment_compact |
119 decorator_expression |
120 decorator_block |
121 partial_expression |
122 partial_block )* }
123
124 parameter = _{ helper_parameter ~ EOI }
125 handlebars = _{ template ~ EOI }
126
127 /// json path visitor
128 /// Disallowed chars: Whitespace ! " # % & ' ( ) * + , . / ; < = > @ [ \ ] ^ ` { | } ~
129 path_id = @{ symbol_char+ }
130
131 path_raw_id = { (!"]" ~ ANY)* }
132 path_sep = _{ "/" | "." }
133 path_up = { ".." }
134 path_key = _{ "[" ~ path_raw_id ~ "]" }
135 path_root = { "@root" }
136 path_current = _{ "this" ~ path_sep | "./" }
137 path_item = _{ path_id|path_key }
138 path_local = { "@" }
139 path_inline = ${ path_current? ~ (path_root ~ path_sep)? ~ path_local? ~ (path_up ~ path_sep)* ~ path_item ~ (path_sep ~ path_item)* }
140 path = _{ path_inline ~ EOI }