introduce hacks module

This commit is contained in:
Aleksey Kladov
2021-12-28 18:57:13 +03:00
parent aa1788dc71
commit bfc263f1f9
6 changed files with 34 additions and 51 deletions

View File

@@ -0,0 +1,14 @@
//! Things which exist to solve practial issues, but which shouldn't exist.
//!
//! Please avoid adding new usages of the functions in this module
use crate::{ast, AstNode};
pub fn parse_expr_from_str(s: &str) -> Option<ast::Expr> {
let file = ast::SourceFile::parse(&format!("const _: () = {};", s));
let expr = file.syntax_node().descendants().find_map(ast::Expr::cast)?;
if expr.syntax().text() != s {
return None;
}
Some(expr)
}

View File

@@ -40,6 +40,7 @@ pub mod ast;
pub mod fuzz;
pub mod utils;
pub mod ted;
pub mod hacks;
use std::{marker::PhantomData, sync::Arc};
@@ -167,26 +168,6 @@ impl SourceFile {
}
}
// FIXME: `parse` functions shouldn't hang directly from AST nodes, and they
// shouldn't return `Result`.
//
// We need a dedicated module for parser entry points, and they should always
// return `Parse`.
impl ast::Path {
/// Returns `text`, parsed as a path, but only if it has no errors.
pub fn parse(text: &str) -> Result<Self, ()> {
parsing::parse_text_as(text, parser::ParserEntryPoint::Path)
}
}
impl ast::Expr {
/// Returns `text`, parsed as an expression, but only if it has no errors.
pub fn parse(text: &str) -> Result<Self, ()> {
parsing::parse_text_as(text, parser::ParserEntryPoint::Expr)
}
}
/// Matches a `SyntaxNode` against an `ast` type.
///
/// # Example:

View File

@@ -5,7 +5,7 @@ mod reparsing;
use rowan::TextRange;
use crate::{syntax_node::GreenNode, AstNode, SyntaxError, SyntaxNode, SyntaxTreeBuilder};
use crate::{syntax_node::GreenNode, SyntaxError, SyntaxTreeBuilder};
pub(crate) use crate::parsing::reparsing::incremental_reparse;
@@ -17,26 +17,6 @@ pub(crate) fn parse_text(text: &str) -> (GreenNode, Vec<SyntaxError>) {
(node, errors)
}
/// Returns `text` parsed as a `T` provided there are no parse errors.
pub(crate) fn parse_text_as<T: AstNode>(
text: &str,
entry_point: parser::ParserEntryPoint,
) -> Result<T, ()> {
let lexed = parser::LexedStr::new(text);
if lexed.errors().next().is_some() {
return Err(());
}
let parser_input = lexed.to_input();
let parser_output = parser::parse(&parser_input, entry_point);
let (node, errors, eof) = build_tree(lexed, parser_output, true);
if !errors.is_empty() || !eof {
return Err(());
}
SyntaxNode::new_root(node).first_child().and_then(T::cast).ok_or(())
}
pub(crate) fn build_tree(
lexed: parser::LexedStr<'_>,
parser_output: parser::Output,