oxedyne/fe2o3/fe2o3_text/src/fmt/cst.rs
6.4 KiB, 1 run
created by r1870400018:11622, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! Concrete syntax tree types. |
| 2 | //! |
| 3 | //! The CST preserves every byte of the source: tokens, whitespace, |
| 4 | //! and comments. This is essential for round-trip formatting. |
| 5 | //! |
| 6 | |
| 7 | /// Byte range in the source text. |
| 8 | #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] |
| 9 | pub struct Span { |
| 10 | /// Start byte offset (inclusive). |
| 11 | pub start: usize, |
| 12 | /// End byte offset (exclusive). |
| 13 | pub end: usize, |
| 14 | } |
| 15 | |
| 16 | impl Span { |
| 17 | /// Length in bytes. |
| 18 | pub fn len(&self) -> usize { self.end - self.start } |
| 19 | /// Whether the span is empty. |
| 20 | pub fn is_empty(&self) -> bool { self.start == self.end } |
| 21 | } |
| 22 | |
| 23 | /// Trivia — whitespace and comments that appear between tokens. |
| 24 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 25 | pub enum Trivia { |
| 26 | /// Contiguous whitespace (spaces, tabs). |
| 27 | Whitespace(String), |
| 28 | /// A newline sequence (LF or CRLF). |
| 29 | Newline, |
| 30 | /// A line comment (e.g. `// ...`), including the delimiter. |
| 31 | LineComment(String), |
| 32 | /// A block comment (e.g. `/* ... */`), including delimiters. |
| 33 | BlockComment(String), |
| 34 | } |
| 35 | |
| 36 | /// A token in the source text, with attached trivia. |
| 37 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 38 | pub struct Token { |
| 39 | /// The syntactic kind of this token. |
| 40 | pub kind: TokenKind, |
| 41 | /// The literal text of the token. |
| 42 | pub text: String, |
| 43 | /// Trivia (whitespace, comments) that appeared before this token. |
| 44 | pub leading_trivia: Vec<Trivia>, |
| 45 | /// Byte span in the source. |
| 46 | pub span: Span, |
| 47 | } |
| 48 | |
| 49 | /// Broad token categories. Language-specific token kinds are mapped |
| 50 | /// into these categories by the lexer. |
| 51 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 52 | pub enum TokenKind { |
| 53 | /// A keyword in the language (e.g. `fn`, `let`, `if`). |
| 54 | Keyword(String), |
| 55 | /// An identifier. |
| 56 | Ident, |
| 57 | /// A numeric literal. |
| 58 | Number, |
| 59 | /// A string literal (including delimiters). |
| 60 | StringLit, |
| 61 | /// A character literal. |
| 62 | CharLit, |
| 63 | /// A single punctuation character (e.g. `(`, `,`, `;`). |
| 64 | Punct(char), |
| 65 | /// A multi-character operator (e.g. `->`, `=>`, `::`, `<=`). |
| 66 | Operator(String), |
| 67 | /// A doc comment (e.g. `///` or `//!`). |
| 68 | DocComment(String), |
| 69 | /// An attribute (e.g. `#[derive(...)]`). |
| 70 | Attribute, |
| 71 | /// A lifetime (e.g. `'a`). |
| 72 | Lifetime, |
| 73 | /// A macro invocation name (e.g. `vec` in `vec![...]`). |
| 74 | MacroName, |
| 75 | /// End of file. |
| 76 | Eof, |
| 77 | } |
| 78 | |
| 79 | /// A node in the concrete syntax tree. |
| 80 | #[derive(Clone, Debug)] |
| 81 | pub struct CstNode { |
| 82 | /// What kind of syntactic construct this node represents. |
| 83 | pub kind: NodeKind, |
| 84 | /// The children of this node (tokens and sub-nodes), in source order. |
| 85 | pub children: Vec<CstChild>, |
| 86 | /// Byte span covering the entire node in the source. |
| 87 | pub span: Span, |
| 88 | } |
| 89 | |
| 90 | /// What a CST child is. |
| 91 | #[derive(Clone, Debug)] |
| 92 | pub enum CstChild { |
| 93 | /// A leaf token. |
| 94 | Token(Token), |
| 95 | /// A sub-tree with a labelled role. |
| 96 | Node { |
| 97 | role: ChildRole, |
| 98 | node: CstNode, |
| 99 | }, |
| 100 | } |
| 101 | |
| 102 | /// The syntactic kind of a CST node. These are broad categories |
| 103 | /// that apply across brace-delimited languages. Language-specific |
| 104 | /// refinements add detail but the formatter operates on these. |
| 105 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 106 | pub enum NodeKind { |
| 107 | /// The root node of a source file. |
| 108 | SourceFile, |
| 109 | /// A function or method definition. |
| 110 | FnDef, |
| 111 | /// A parameter list (parenthesised). |
| 112 | ParamList, |
| 113 | /// A single parameter. |
| 114 | Param, |
| 115 | /// A struct definition. |
| 116 | StructDef, |
| 117 | /// A field list (braced). |
| 118 | FieldList, |
| 119 | /// A single field. |
| 120 | Field, |
| 121 | /// An enum definition. |
| 122 | EnumDef, |
| 123 | /// A variant list (braced). |
| 124 | VariantList, |
| 125 | /// A single enum variant. |
| 126 | Variant, |
| 127 | /// A trait definition. |
| 128 | TraitDef, |
| 129 | /// An impl block. |
| 130 | ImplBlock, |
| 131 | /// A type alias. |
| 132 | TypeAlias, |
| 133 | /// A use/import declaration. |
| 134 | UseDecl, |
| 135 | /// A use tree (the braced contents of a use declaration). |
| 136 | UseTree, |
| 137 | /// A match/switch expression. |
| 138 | MatchExpr, |
| 139 | /// A single match arm. |
| 140 | MatchArm, |
| 141 | /// A block (braced sequence of statements). |
| 142 | Block, |
| 143 | /// An if/else expression or statement. |
| 144 | IfExpr, |
| 145 | /// A for loop. |
| 146 | ForLoop, |
| 147 | /// A while loop. |
| 148 | WhileLoop, |
| 149 | /// A loop expression. |
| 150 | LoopExpr, |
| 151 | /// A let binding. |
| 152 | LetBinding, |
| 153 | /// A return expression. |
| 154 | ReturnExpr, |
| 155 | /// A closure/lambda. |
| 156 | Closure, |
| 157 | /// A function call or macro invocation. |
| 158 | CallExpr, |
| 159 | /// An argument list (parenthesised, in a call). |
| 160 | ArgList, |
| 161 | /// A method chain (a.b().c()). |
| 162 | ChainExpr, |
| 163 | /// A binary expression. |
| 164 | BinaryExpr, |
| 165 | /// A where clause. |
| 166 | WhereClause, |
| 167 | /// A where predicate. |
| 168 | WherePredicate, |
| 169 | /// A generic parameter list (angle-bracketed). |
| 170 | GenericParams, |
| 171 | /// A type expression. |
| 172 | TypeExpr, |
| 173 | /// An attribute (e.g. `#[derive(...)]`). |
| 174 | Attribute, |
| 175 | /// A struct literal (e.g. `Foo { x: 1, y: 2 }`). |
| 176 | StructLit, |
| 177 | /// A tuple expression. |
| 178 | TupleExpr, |
| 179 | /// An array expression. |
| 180 | ArrayExpr, |
| 181 | /// A module declaration. |
| 182 | ModDecl, |
| 183 | /// A constant or static item. |
| 184 | ConstItem, |
| 185 | /// A macro definition (`macro_rules!`). |
| 186 | MacroDef, |
| 187 | /// An expression statement. |
| 188 | ExprStmt, |
| 189 | /// A token sequence that the parser did not refine further. |
| 190 | /// The formatter passes it through unchanged. |
| 191 | Verbatim, |
| 192 | /// A comment group (consecutive line comments or a block comment). |
| 193 | CommentGroup, |
| 194 | } |
| 195 | |
| 196 | /// The role a child plays within its parent node. |
| 197 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 198 | pub enum ChildRole { |
| 199 | /// The name of the item (fn name, struct name, etc.). |
| 200 | Name, |
| 201 | /// Visibility qualifier (pub, pub(crate), etc.). |
| 202 | Visibility, |
| 203 | /// Generic parameters. |
| 204 | Generics, |
| 205 | /// The parameter list of a function. |
| 206 | Params, |
| 207 | /// The return type of a function. |
| 208 | ReturnType, |
| 209 | /// A where clause. |
| 210 | Where, |
| 211 | /// The body of a function, struct, enum, impl, etc. |
| 212 | Body, |
| 213 | /// A condition (if, while). |
| 214 | Condition, |
| 215 | /// The "else" branch. |
| 216 | Else, |
| 217 | /// A pattern (in let, match arm). |
| 218 | Pattern, |
| 219 | /// The initialiser / value. |
| 220 | Value, |
| 221 | /// A type annotation. |
| 222 | Type, |
| 223 | /// An attribute. |
| 224 | Attr, |
| 225 | /// The separator (comma, semicolon). |
| 226 | Separator, |
| 227 | /// The operator in a binary expression. |
| 228 | Op, |
| 229 | /// Left-hand side. |
| 230 | Lhs, |
| 231 | /// Right-hand side. |
| 232 | Rhs, |
| 233 | /// Receiver (self argument or method chain base). |
| 234 | Receiver, |
| 235 | /// The iterator in a for loop. |
| 236 | Iterator, |
| 237 | /// A module path. |
| 238 | Path, |
| 239 | /// Unspecified / fallback. |
| 240 | Misc, |
| 241 | } |