yog_codegen/syntax.rs
1//! What a language generated code is written in has to say for itself:
2//! each concrete language (its own crate) implements the parts it has.
3
4/// How the language writes comments.
5pub trait Comments {
6 /// `text` as a one-line comment (no line break).
7 fn comment(text: &str) -> String;
8 /// `text` as a one-line doc comment on what follows.
9 fn doc(text: &str) -> String;
10}
11
12/// How the language writes string literals.
13pub trait Literals {
14 /// A string literal reading back as `text`.
15 fn string(text: &str) -> String;
16}
17
18/// The language's names: its reserved words, and how a name spelled like
19/// one is made an identifier.
20pub trait Identifiers {
21 /// The reserved words.
22 const KEYWORDS: &'static [&'static str];
23
24 /// `keyword` made an identifier.
25 fn escape_keyword(keyword: &str) -> String;
26
27 /// `name` as an identifier: escaped when it's a reserved word.
28 fn ident(name: &str) -> String {
29 if Self::KEYWORDS.contains(&name) {
30 Self::escape_keyword(name)
31 } else {
32 name.to_string()
33 }
34 }
35}
36
37/// How the language nests: its blocks and their indentation.
38pub trait Blocks {
39 /// One level of indentation.
40 const INDENT: &'static str;
41
42 /// The line opening a block after `head`.
43 fn open(head: &str) -> String;
44 /// The line closing a block, then `tail`.
45 fn close(tail: &str) -> String;
46}