Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_austenite/src/syntax/kind.rs

9.3 KiB, 1 run

created by r1870400018:63562, 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// SPDX-License-Identifier: Apache-2.0
2// Original work copyright (c) the Typst project authors (typst-syntax `kind.rs`, version 0.15.1).
3// Modified for Austenite: ported to Hematite's types, IR and error handling.
4// U1 owns this file. The variant list mirrors typst-syntax 0.14's `SyntaxKind`; add a kind rather than
5// overloading one, since U2 dispatches on it.
6
7macro_rules! syntax_kinds {
8 ($($(#[$m:meta])* $v:ident => $name:literal,)*) => {
9 #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
10 pub enum SyntaxKind {
11 $($(#[$m])* $v,)*
12 }
13
14 impl SyntaxKind {
15 /// A human-readable name, as a diagnostic quotes it ("expected closing bracket").
16 pub fn name(self) -> &'static str {
17 match self {
18 $(SyntaxKind::$v => $name,)*
19 }
20 }
21 }
22 };
23}
24
25syntax_kinds! {
26 // Trivia and errors
27 End => "end of tokens",
28 Error => "syntax error",
29 Shebang => "shebang",
30 LineComment => "line comment",
31 BlockComment => "block comment",
32 // Markup
33 Markup => "markup",
34 Text => "text",
35 Space => "space",
36 Linebreak => "line break",
37 Parbreak => "paragraph break",
38 Escape => "escape sequence",
39 Shorthand => "shorthand",
40 SmartQuote => "smart quote",
41 Strong => "strong content",
42 Emph => "emphasized content", // Typst's spelling: the name reaches its messages
43 Raw => "raw block",
44 RawLang => "raw language tag",
45 RawDelim => "raw delimiter",
46 RawTrimmed => "raw trimmed",
47 Link => "link",
48 Label => "label",
49 Ref => "reference",
50 RefMarker => "reference marker",
51 Heading => "heading",
52 HeadingMarker => "heading marker",
53 ListItem => "list item",
54 ListMarker => "list marker",
55 EnumItem => "enum item",
56 EnumMarker => "enum marker",
57 TermItem => "term list item",
58 TermMarker => "term marker",
59 Equation => "equation",
60 // Maths
61 Math => "math",
62 MathText => "math text",
63 MathIdent => "math identifier",
64 MathFieldAccess => "math field access",
65 MathShorthand => "math shorthand",
66 MathAlignPoint => "math alignment point",
67 MathCall => "math function call",
68 MathArgs => "math call arguments",
69 MathDelimited => "delimited math",
70 MathAttach => "math attachments",
71 MathPrimes => "math primes",
72 MathFrac => "math fraction",
73 MathRoot => "math root",
74 // Punctuation
75 Hash => "hash",
76 LeftBrace => "opening brace",
77 RightBrace => "closing brace",
78 LeftBracket => "opening bracket",
79 RightBracket => "closing bracket",
80 LeftParen => "opening paren",
81 RightParen => "closing paren",
82 Comma => "comma",
83 Semicolon => "semicolon",
84 Colon => "colon",
85 Star => "star",
86 Underscore => "underscore",
87 Dollar => "dollar sign",
88 Plus => "plus",
89 Minus => "minus",
90 Slash => "slash",
91 Hat => "hat",
92 Prime => "prime", // unused since 0.15: primes lex as one `MathPrimes` leaf
93 Dot => "dot",
94 Eq => "equals sign",
95 EqEq => "equality operator",
96 ExclEq => "inequality operator",
97 Lt => "less-than operator",
98 LtEq => "less-than or equal operator",
99 Gt => "greater-than operator",
100 GtEq => "greater-than or equal operator",
101 PlusEq => "add-assign operator",
102 HyphEq => "subtract-assign operator",
103 StarEq => "multiply-assign operator",
104 SlashEq => "divide-assign operator",
105 Dots => "dots",
106 Arrow => "arrow",
107 Root => "root",
108 Bang => "exclamation mark",
109 // Keywords
110 Not => "operator `not`",
111 And => "operator `and`",
112 Or => "operator `or`",
113 None => "`none`",
114 Auto => "`auto`",
115 Let => "keyword `let`",
116 Set => "keyword `set`",
117 Show => "keyword `show`",
118 Context => "keyword `context`",
119 If => "keyword `if`",
120 Else => "keyword `else`",
121 For => "keyword `for`",
122 In => "keyword `in`",
123 While => "keyword `while`",
124 Break => "keyword `break`",
125 Continue => "keyword `continue`",
126 Return => "keyword `return`",
127 Import => "keyword `import`",
128 Include => "keyword `include`",
129 As => "keyword `as`",
130 // Code
131 Code => "code",
132 Ident => "identifier",
133 Bool => "boolean",
134 Int => "integer",
135 Float => "float",
136 Numeric => "numeric value",
137 Str => "string",
138 CodeBlock => "code block",
139 ContentBlock => "content block",
140 Parenthesized => "group",
141 Array => "array",
142 Dict => "dictionary",
143 Named => "named pair",
144 Keyed => "keyed pair",
145 Unary => "unary expression",
146 Binary => "binary expression",
147 FieldAccess => "field access",
148 FuncCall => "function call",
149 Args => "call arguments",
150 Spread => "spread",
151 Closure => "closure",
152 Params => "closure parameters",
153 LetBinding => "`let` expression",
154 SetRule => "`set` expression",
155 ShowRule => "`show` expression",
156 Contextual => "`context` expression",
157 Conditional => "`if` expression",
158 WhileLoop => "while-loop expression",
159 ForLoop => "for-loop expression",
160 ModuleImport => "`import` expression",
161 ImportItems => "import items",
162 ImportItemPath => "imported item path",
163 RenamedImportItem => "renamed import item",
164 ModuleInclude => "`include` expression",
165 LoopBreak => "`break` expression",
166 LoopContinue => "`continue` expression",
167 FuncReturn => "`return` expression",
168 Destructuring => "destructuring pattern",
169 DestructAssignment => "destructuring assignment expression",
170}
171
172impl SyntaxKind {
173 /// Is this a comment or whitespace a typed accessor skips?
174 pub fn is_trivia(self) -> bool {
175 matches!(self, SyntaxKind::Shebang | SyntaxKind::LineComment | SyntaxKind::BlockComment
176 | SyntaxKind::Space | SyntaxKind::Parbreak)
177 }
178
179 pub fn is_error(self) -> bool { self == SyntaxKind::Error }
180
181 pub fn is_keyword(self) -> bool {
182 matches!(self,
183 SyntaxKind::Not | SyntaxKind::And | SyntaxKind::Or | SyntaxKind::None | SyntaxKind::Auto
184 | SyntaxKind::Let | SyntaxKind::Set | SyntaxKind::Show | SyntaxKind::Context | SyntaxKind::If
185 | SyntaxKind::Else | SyntaxKind::For | SyntaxKind::In | SyntaxKind::While | SyntaxKind::Break
186 | SyntaxKind::Continue | SyntaxKind::Return | SyntaxKind::Import | SyntaxKind::Include
187 | SyntaxKind::As)
188 }
189}
190
191impl SyntaxKind {
192 /// Is this an opening or closing bracket, brace or parenthesis?
193 pub fn is_grouping(self) -> bool {
194 matches!(self,
195 SyntaxKind::LeftBracket | SyntaxKind::LeftBrace | SyntaxKind::LeftParen
196 | SyntaxKind::RightBracket | SyntaxKind::RightBrace | SyntaxKind::RightParen)
197 }
198
199 /// Does this token end the expression before it?
200 pub fn is_terminator(self) -> bool {
201 matches!(self,
202 SyntaxKind::End | SyntaxKind::Semicolon | SyntaxKind::RightBrace
203 | SyntaxKind::RightParen | SyntaxKind::RightBracket)
204 }
205
206 /// Is this a code or content block?
207 pub fn is_block(self) -> bool {
208 matches!(self, SyntaxKind::CodeBlock | SyntaxKind::ContentBlock)
209 }
210
211 /// Must this node be ended by a semicolon or a line break when embedded in markup?
212 pub fn is_stmt(self) -> bool {
213 matches!(self,
214 SyntaxKind::LetBinding | SyntaxKind::SetRule | SyntaxKind::ShowRule
215 | SyntaxKind::ModuleImport | SyntaxKind::ModuleInclude)
216 }
217}
218
219/// A set of syntax kinds, one bit each, built at compile time with [`syntax_set!`].
220#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
221pub struct SyntaxSet(u128, u128);
222
223impl SyntaxSet {
224 pub const fn new() -> Self { Self(0, 0) }
225
226 pub const fn add(self, kind: SyntaxKind) -> Self {
227 let i = kind as usize;
228 if i < 128 { Self(self.0 | (1u128 << i), self.1) } else { Self(self.0, self.1 | (1u128 << (i - 128))) }
229 }
230
231 pub const fn remove(self, kind: SyntaxKind) -> Self {
232 let i = kind as usize;
233 if i < 128 { Self(self.0 & !(1u128 << i), self.1) } else { Self(self.0, self.1 & !(1u128 << (i - 128))) }
234 }
235
236 pub const fn union(self, other: Self) -> Self { Self(self.0 | other.0, self.1 | other.1) }
237
238 pub const fn contains(&self, kind: SyntaxKind) -> bool {
239 let i = kind as usize;
240 if i < 128 { self.0 & (1u128 << i) != 0 } else { self.1 & (1u128 << (i - 128)) != 0 }
241 }
242}
243
244/// A compile-time [`SyntaxSet`] of the named kinds.
245macro_rules! syntax_set {
246 ($($kind:ident),* $(,)?) => {{
247 const SET: $crate::syntax::kind::SyntaxSet = $crate::syntax::kind::SyntaxSet::new()
248 $(.add($crate::syntax::SyntaxKind::$kind))*;
249 SET
250 }};
251}
252
253pub(crate) use syntax_set;
254
255// Named sets, as typst-syntax declares them.
256pub const STMT: SyntaxSet = syntax_set!(Let, Set, Show, Import, Include, Return);
257
258pub const MATH_EXPR: SyntaxSet = syntax_set!(
259 Hash, MathIdent, MathFieldAccess, LeftBrace, RightBrace, LeftParen, RightParen, MathText,
260 MathShorthand, Linebreak, MathAlignPoint, MathPrimes, Escape, Str, Root, Dot, Comma, Semicolon, Bang,
261);
262
263pub const ATOMIC_CODE_EXPR: SyntaxSet = syntax_set!(
264 Ident, LeftBrace, LeftBracket, LeftParen, Dollar, Let, Set, Show, Context, If, While, For, Import,
265 Include, Break, Continue, Return, None, Auto, Int, Float, Bool, Numeric, Str, Label, Raw,
266);
267
268pub const UNARY_OP: SyntaxSet = syntax_set!(Plus, Minus, Not);
269
270// An underscore starts only `_ => ..` or `_ = ..`.
271pub const CODE_EXPR: SyntaxSet = ATOMIC_CODE_EXPR.union(UNARY_OP).add(SyntaxKind::Underscore);
272
273pub const BINARY_OP: SyntaxSet = syntax_set!(
274 Plus, Minus, Star, Slash, And, Or, EqEq, ExclEq, Lt, LtEq, Gt, GtEq, Eq, In, PlusEq, HyphEq, StarEq,
275 SlashEq,
276);
277
278pub const ARRAY_OR_DICT_ITEM: SyntaxSet = CODE_EXPR.add(SyntaxKind::Dots);
279pub const ARG: SyntaxSet = CODE_EXPR.add(SyntaxKind::Dots);
280pub const PATTERN_LEAF: SyntaxSet = ATOMIC_CODE_EXPR;
281pub const PATTERN: SyntaxSet = PATTERN_LEAF.add(SyntaxKind::LeftParen).add(SyntaxKind::Underscore);
282pub const PARAM: SyntaxSet = PATTERN.add(SyntaxKind::Dots);
283pub const DESTRUCTURING_ITEM: SyntaxSet = PATTERN.add(SyntaxKind::Dots);