use crate::parser::JsonParser;
use crate::syntax::parse_root;
use biome_json_factory::JsonSyntaxFactory;
use biome_json_syntax::{JsonLanguage, JsonRoot, JsonSyntaxNode};
pub use biome_parser::prelude::*;
use biome_parser::tree_sink::LosslessTreeSink;
use biome_rowan::{AstNode, NodeCache};
pub use parser::JsonParserOptions;
mod lexer;
mod parser;
mod prelude;
mod syntax;
mod token_source;
pub(crate) type JsonLosslessTreeSink<'source> =
LosslessTreeSink<'source, JsonLanguage, JsonSyntaxFactory>;
pub fn parse_json(source: &str, options: JsonParserOptions) -> JsonParse {
let mut cache = NodeCache::default();
parse_json_with_cache(source, &mut cache, options)
}
pub fn parse_json_with_cache(
source: &str,
cache: &mut NodeCache,
config: JsonParserOptions,
) -> JsonParse {
tracing::debug_span!("parse").in_scope(move || {
let mut parser = JsonParser::new(source, config);
parse_root(&mut parser);
let (events, diagnostics, trivia) = parser.finish();
let mut tree_sink = JsonLosslessTreeSink::with_cache(source, &trivia, cache);
biome_parser::event::process(&mut tree_sink, events, diagnostics);
let (green, diagnostics) = tree_sink.finish();
JsonParse::new(green, diagnostics)
})
}
#[derive(Debug)]
pub struct JsonParse {
root: JsonSyntaxNode,
diagnostics: Vec<ParseDiagnostic>,
}
impl JsonParse {
pub fn new(root: JsonSyntaxNode, diagnostics: Vec<ParseDiagnostic>) -> JsonParse {
JsonParse { root, diagnostics }
}
pub fn syntax(&self) -> JsonSyntaxNode {
self.root.clone()
}
pub fn diagnostics(&self) -> &[ParseDiagnostic] {
&self.diagnostics
}
pub fn into_diagnostics(self) -> Vec<ParseDiagnostic> {
self.diagnostics
}
pub fn has_errors(&self) -> bool {
self.diagnostics
.iter()
.any(|diagnostic| diagnostic.is_error())
}
pub fn tree(&self) -> JsonRoot {
JsonRoot::unwrap_cast(self.syntax())
}
}