Skip to main content

ParsedSource

Struct ParsedSource 

Source
pub struct ParsedSource { /* private fields */ }
Expand description

A source file plus its parse tree; the input to lowering and to the syntax-diagnostic pass.

Implementations§

Source§

impl ParsedSource

Source

pub fn source_id(&self) -> &SourceId

The identity of the parsed source.

Source

pub fn text(&self) -> &str

The original source text.

Source

pub fn tree(&self) -> &Tree

The tree-sitter parse tree.

Examples found in repository?
examples/conformance.rs (line 29)
13fn main() {
14    let path = std::env::args()
15        .nth(1)
16        .expect("usage: conformance <corpus.json>");
17    let raw = std::fs::read_to_string(&path).expect("corpus file readable");
18    let corpus: Vec<String> = parse_json_strings(&raw);
19
20    let mut failures = 0usize;
21    let mut report = String::new();
22    for (index, query) in corpus.iter().enumerate() {
23        let source = surrealguard_syntax::source::SourceId::new(format!("corpus:{index}"));
24        let Ok(parsed) = surrealguard_syntax::parse::parse_source(source, query.as_str()) else {
25            failures += 1;
26            let _ = writeln!(report, "#{index}: parser returned no tree");
27            continue;
28        };
29        if let Some(range) = first_broken_range(parsed.tree().root_node()) {
30            failures += 1;
31            let snippet: String = query[range.clone()].chars().take(60).collect();
32            let context: String = query.chars().take(80).collect();
33            let _ = writeln!(
34                report,
35                "#{index}: ERROR at {range:?}: `{}`\n    in: {}",
36                snippet.replace('\n', " "),
37                context.replace('\n', " ")
38            );
39        }
40    }
41
42    print!("{report}");
43    println!(
44        "---\n{failures}/{} corpus entries fail to parse",
45        corpus.len()
46    );
47    if failures > 0 {
48        std::process::exit(1);
49    }
50}
Source

pub fn root_kind(&self) -> &str

Kind of the root CST node ("SurrealQL" for a well-formed parse).

Source

pub fn has_error(&self) -> bool

Whether the tree contains any error or missing nodes.

Source

pub fn syntax_diagnostics(&self) -> &[SyntaxDiagnostic]

The recoverable syntax problems collected during parsing.

Trait Implementations§

Source§

impl Debug for ParsedSource

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.