use sim_codec_javascript::{Node, Span, Token};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Language {
TypeScript,
Tsx,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Limits {
pub max_bytes: usize,
pub max_nodes: usize,
pub max_nesting: usize,
}
impl Default for Limits {
fn default() -> Self {
Self {
max_bytes: 4 * 1024 * 1024,
max_nodes: 1_000_000,
max_nesting: 256,
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum SyntaxKind {
Declaration,
Annotation,
TypeArguments,
TypeNode,
Modifier,
Jsx,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum SyntaxNode {
JavaScript(Node),
TypeScript {
kind: SyntaxKind,
span: Span,
context: Vec<String>,
},
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SyntaxTree {
pub(crate) source: String,
pub language: Language,
pub tokens: Vec<Token>,
pub nodes: Vec<SyntaxNode>,
}
impl SyntaxTree {
#[must_use]
pub fn source(&self) -> &str {
&self.source
}
#[must_use]
pub fn preserve_source(&self) -> String {
self.source.clone()
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum DiagnosticCode {
ResourceLimit,
UnclosedSyntax,
JsxInTypeScript,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Diagnostic {
pub code: DiagnosticCode,
pub span: Span,
pub line: usize,
pub column: usize,
pub message: String,
}
impl std::fmt::Display for Diagnostic {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}:{}: {}", self.line, self.column, self.message)
}
}
impl std::error::Error for Diagnostic {}