diff options
| author | Martin Haug <mhaug@live.de> | 2021-10-23 19:03:27 +0200 |
|---|---|---|
| committer | Martin Haug <mhaug@live.de> | 2021-11-05 13:44:49 +0100 |
| commit | 4875633acf4701705b9b3b014eb7d94268b897c2 (patch) | |
| tree | 0aedda87c8c2dc65316e2455c35e72054d9bae0e /src/source.rs | |
| parent | ea6ee3f667e922ed2f21b08719a45d2395787932 (diff) | |
Change parser
Diffstat (limited to 'src/source.rs')
| -rw-r--r-- | src/source.rs | 24 |
1 files changed, 22 insertions, 2 deletions
diff --git a/src/source.rs b/src/source.rs index c9164f90..e33e146c 100644 --- a/src/source.rs +++ b/src/source.rs @@ -8,8 +8,10 @@ use std::rc::Rc; use serde::{Deserialize, Serialize}; +use crate::diag::{Error, TypResult}; use crate::loading::{FileHash, Loader}; -use crate::parse::{is_newline, Scanner}; +use crate::parse::{is_newline, parse, Scanner}; +use crate::syntax::{GreenNode, Markup, NodeKind, RedNode}; use crate::util::PathExt; #[cfg(feature = "codespan-reporting")] @@ -124,6 +126,7 @@ pub struct SourceFile { path: PathBuf, src: String, line_starts: Vec<usize>, + root: Rc<GreenNode>, } impl SourceFile { @@ -131,11 +134,28 @@ impl SourceFile { pub fn new(id: SourceId, path: &Path, src: String) -> Self { let mut line_starts = vec![0]; line_starts.extend(newlines(&src)); - Self { + let mut init = Self { id, path: path.normalize(), src, line_starts, + root: Rc::new(GreenNode::new(NodeKind::Markup, 0)), + }; + + let root = parse(&init); + init.root = root; + init + } + + pub fn ast(&self) -> TypResult<Markup> { + let res = RedNode::new_root(self.root.clone(), self.id); + let errors = res.errors(); + if errors.is_empty() { + Ok(res.ticket().cast().unwrap()) + } else { + Err(Box::new( + errors.into_iter().map(|(span, msg)| Error::new(span, msg)).collect(), + )) } } |
