summaryrefslogtreecommitdiff
path: root/src/source.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2021-11-08 13:08:15 +0100
committerGitHub <noreply@github.com>2021-11-08 13:08:15 +0100
commitc6f8ad35f45248f1fd36ee00195966f1629c6ca7 (patch)
tree51faa3f6bbc56f75636823adeea135ed76e1b33b /src/source.rs
parentea6ee3f667e922ed2f21b08719a45d2395787932 (diff)
parent38c5c362419c5eee7a4fdc0b43d3a9dfb339a6d2 (diff)
Merge pull request #46 from typst/parser-ng
Next Generation Parser
Diffstat (limited to 'src/source.rs')
-rw-r--r--src/source.rs18
1 files changed, 17 insertions, 1 deletions
diff --git a/src/source.rs b/src/source.rs
index c9164f90..85f48491 100644
--- a/src/source.rs
+++ b/src/source.rs
@@ -8,8 +8,11 @@ use std::rc::Rc;
use serde::{Deserialize, Serialize};
+use crate::diag::TypResult;
use crate::loading::{FileHash, Loader};
-use crate::parse::{is_newline, Scanner};
+use crate::parse::{is_newline, parse, Scanner};
+use crate::syntax::ast::Markup;
+use crate::syntax::{GreenNode, RedNode};
use crate::util::PathExt;
#[cfg(feature = "codespan-reporting")]
@@ -124,6 +127,7 @@ pub struct SourceFile {
path: PathBuf,
src: String,
line_starts: Vec<usize>,
+ root: Rc<GreenNode>,
}
impl SourceFile {
@@ -134,11 +138,23 @@ impl SourceFile {
Self {
id,
path: path.normalize(),
+ root: parse(&src),
src,
line_starts,
}
}
+ /// The file's abstract syntax tree.
+ pub fn ast(&self) -> TypResult<Markup> {
+ let red = RedNode::from_root(self.root.clone(), self.id);
+ let errors = red.errors();
+ if errors.is_empty() {
+ Ok(red.cast().unwrap())
+ } else {
+ Err(Box::new(errors))
+ }
+ }
+
/// Create a source file without a real id and path, usually for testing.
pub fn detached(src: impl Into<String>) -> Self {
Self::new(SourceId(0), Path::new(""), src.into())