From ec884ec1d85f6e1d7868db3e82d572579cc5d345 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Wed, 5 Oct 2022 12:49:39 +0200 Subject: Refactor syntax module --- src/syntax/mod.rs | 614 +----------------------------------------------------- 1 file changed, 7 insertions(+), 607 deletions(-) (limited to 'src/syntax/mod.rs') diff --git a/src/syntax/mod.rs b/src/syntax/mod.rs index 367d0062..5ff99d03 100644 --- a/src/syntax/mod.rs +++ b/src/syntax/mod.rs @@ -1,21 +1,20 @@ //! Syntax types. pub mod ast; -mod highlight; +pub mod highlight; +mod kind; mod span; -use std::fmt::{self, Debug, Display, Formatter}; -use std::hash::{Hash, Hasher}; +use std::fmt::{self, Debug, Formatter}; use std::ops::Range; use std::sync::Arc; -pub use highlight::*; +pub use kind::*; pub use span::*; -use self::ast::{RawNode, TypedNode, Unit}; +use self::ast::TypedNode; use crate::diag::SourceError; use crate::source::SourceId; -use crate::util::EcoString; /// An inner or leaf node in the untyped syntax tree. #[derive(Clone, PartialEq, Hash)] @@ -73,8 +72,8 @@ impl SyntaxNode { } match self.kind() { - &NodeKind::Error(pos, ref message) => { - vec![SourceError::new(self.span().with_pos(pos), message)] + NodeKind::Error(pos, message) => { + vec![SourceError::new(self.span().with_pos(*pos), message)] } _ => self .children() @@ -564,602 +563,3 @@ impl PartialEq for NodeData { self.kind == other.kind && self.len == other.len } } - -/// All syntactical building blocks that can be part of a Typst document. -/// -/// Can be emitted as a token by the tokenizer or as part of a syntax node by -/// the parser. -#[derive(Debug, Clone, PartialEq)] -pub enum NodeKind { - /// A line comment, two slashes followed by inner contents, terminated with - /// a newline: `//\n`. - LineComment, - /// A block comment, a slash and a star followed by inner contents, - /// terminated with a star and a slash: `/**/`. - /// - /// The comment can contain nested block comments. - BlockComment, - /// One or more whitespace characters. Single spaces are collapsed into text - /// nodes if they would otherwise be surrounded by text nodes. - /// - /// Also stores how many newlines are contained. - Space { newlines: usize }, - - /// A left curly brace, starting a code block: `{`. - LeftBrace, - /// A right curly brace, terminating a code block: `}`. - RightBrace, - /// A left square bracket, starting a content block: `[`. - LeftBracket, - /// A right square bracket, terminating a content block: `]`. - RightBracket, - /// A left round parenthesis, starting a grouped expression, collection, - /// argument or parameter list: `(`. - LeftParen, - /// A right round parenthesis, terminating a grouped expression, collection, - /// argument or parameter list: `)`. - RightParen, - /// A comma separator in a sequence: `,`. - Comma, - /// A semicolon terminating an expression: `;`. - Semicolon, - /// A colon between name / key and value in a dictionary, argument or - /// parameter list, or between the term and body of a description list - /// term: `:`. - Colon, - /// The strong text toggle, multiplication operator, and wildcard import - /// symbol: `*`. - Star, - /// Toggles emphasized text and indicates a subscript in a formula: `_`. - Underscore, - /// Starts and ends a math formula. - Dollar, - /// The non-breaking space: `~`. - Tilde, - /// The soft hyphen: `-?`. - HyphQuest, - /// The en-dash: `--`. - Hyph2, - /// The em-dash: `---`. - Hyph3, - /// The ellipsis: `...`. - Dot3, - /// A smart quote: `'` or `"`. - Quote { double: bool }, - /// The unary plus and addition operator, and start of enum items: `+`. - Plus, - /// The unary negation and subtraction operator, and start of list - /// items: `-`. - Minus, - /// The division operator, start of description list items, and fraction - /// operator in a formula: `/`. - Slash, - /// The superscript operator: `^`. - Hat, - /// The math alignment operator: `&`. - Amp, - /// The field access and method call operator: `.`. - Dot, - /// The assignment operator: `=`. - Eq, - /// The equality operator: `==`. - EqEq, - /// The inequality operator: `!=`. - ExclEq, - /// The less-than operator: `<`. - Lt, - /// The less-than or equal operator: `<=`. - LtEq, - /// The greater-than operator: `>`. - Gt, - /// The greater-than or equal operator: `>=`. - GtEq, - /// The add-assign operator: `+=`. - PlusEq, - /// The subtract-assign operator: `-=`. - HyphEq, - /// The multiply-assign operator: `*=`. - StarEq, - /// The divide-assign operator: `/=`. - SlashEq, - /// The spread operator: `..`. - Dots, - /// An arrow between a closure's parameters and body: `=>`. - Arrow, - - /// The `not` operator. - Not, - /// The `and` operator. - And, - /// The `or` operator. - Or, - /// The `none` literal. - None, - /// The `auto` literal. - Auto, - /// The `let` keyword. - Let, - /// The `set` keyword. - Set, - /// The `show` keyword. - Show, - /// The `wrap` keyword. - Wrap, - /// The `if` keyword. - If, - /// The `else` keyword. - Else, - /// The `for` keyword. - For, - /// The `in` keyword. - In, - /// The `while` keyword. - While, - /// The `break` keyword. - Break, - /// The `continue` keyword. - Continue, - /// The `return` keyword. - Return, - /// The `import` keyword. - Import, - /// The `include` keyword. - Include, - /// The `from` keyword. - From, - /// The `as` keyword. - As, - - /// Markup of which all lines must have a minimal indentation. - /// - /// Notably, the number does not determine in which column the markup - /// started, but to the right of which column all markup elements must be, - /// so it is zero except for headings and lists. - Markup { min_indent: usize }, - /// A forced line break in markup or math. - Linebreak, - /// Consecutive text without markup. While basic text with just single - /// spaces is collapsed into a single node, certain symbols that could - /// possibly be markup force text into multiple nodes. - Text(EcoString), - /// A slash and the letter "u" followed by a hexadecimal unicode entity - /// enclosed in curly braces: `\u{1F5FA}`. - Escape(char), - /// Strong content: `*Strong*`. - Strong, - /// Emphasized content: `_Emphasized_`. - Emph, - /// A hyperlink. - Link(EcoString), - /// A raw block with optional syntax highlighting: `` `...` ``. - Raw(Arc), - /// A math formula: `$x$`, `$ x^2 $`. - Math, - /// A section heading: `= Introduction`. - Heading, - /// An item in an unordered list: `- ...`. - List, - /// An item in an enumeration (ordered list): `+ ...` or `1. ...`. - Enum, - /// An explicit enumeration numbering: `23.`. - EnumNumbering(usize), - /// An item in a description list: `/ Term: Details. - Desc, - /// A label: `