about summary refs log tree commit diff
path: root/crates/syntax/src/lib.rs
diff options
context:
space:
mode:
authorVictor Song <vsong1618@gmail.com>2024-01-30 03:23:39 -0600
committerVictor Song <vsong1618@gmail.com>2024-02-13 00:00:02 -0600
commit1918f9b9e0fe2e5b090491596a8d41b5ed2ed3bd (patch)
tree35df35d5a9f370b258f034bd49704c9404e08b03 /crates/syntax/src/lib.rs
parentcdb8a88ea3a4dfb2837e66e6344cff00783d84b7 (diff)
Address PR comments
Diffstat (limited to 'crates/syntax/src/lib.rs')
-rw-r--r--crates/syntax/src/lib.rs19
1 files changed, 19 insertions, 0 deletions
diff --git a/crates/syntax/src/lib.rs b/crates/syntax/src/lib.rs
index 960889b7421..2390cacda28 100644
--- a/crates/syntax/src/lib.rs
+++ b/crates/syntax/src/lib.rs
@@ -187,6 +187,25 @@ impl SourceFile {
     }
 }
 
+impl ast::Literal {
+    pub fn parse(text: &str) -> Parse<ast::Literal> {
+        let lexed = parser::LexedStr::new(text);
+        let parser_input = lexed.to_input();
+        let parser_output = parser::TopEntryPoint::Expr.parse(&parser_input);
+        let (green, mut errors, _) = parsing::build_tree(lexed, parser_output);
+        let root = SyntaxNode::new_root(green.clone());
+
+        errors.extend(validation::validate(&root));
+
+        assert_eq!(root.kind(), SyntaxKind::LITERAL);
+        Parse {
+            green,
+            errors: if errors.is_empty() { None } else { Some(errors.into()) },
+            _ty: PhantomData,
+        }
+    }
+}
+
 impl ast::TokenTree {
     pub fn reparse_as_comma_separated_expr(self) -> Parse<ast::MacroEagerInput> {
         let tokens = self.syntax().descendants_with_tokens().filter_map(NodeOrToken::into_token);