Skip to main content

normalize_surface_syntax/
traits.rs

1//! Traits for language readers and writers.
2
3use crate::ir::Program;
4
5/// Error that can occur when reading source code into IR.
6#[derive(Debug, thiserror::Error)]
7pub enum ReadError {
8    #[error("parse error: {0}")]
9    Parse(String),
10
11    #[error("unsupported syntax: {0}")]
12    Unsupported(String),
13
14    #[error("expected {expected}, got {got}")]
15    UnexpectedNode { expected: String, got: String },
16}
17
18/// A reader parses source code into the surface-syntax IR.
19pub trait Reader: Send + Sync {
20    /// Language identifier (e.g., "typescript", "lua").
21    fn language(&self) -> &'static str;
22
23    /// File extensions this reader handles (e.g., &["ts", "tsx"]).
24    fn extensions(&self) -> &'static [&'static str];
25
26    /// Parse source code into the IR.
27    fn read(&self, source: &str) -> Result<Program, ReadError>;
28}
29
30/// A writer emits the IR as source code in a target language.
31pub trait Writer: Send + Sync {
32    /// Language identifier (e.g., "lua", "typescript").
33    fn language(&self) -> &'static str;
34
35    /// File extension for output (e.g., "lua").
36    fn extension(&self) -> &'static str;
37
38    /// Emit the IR as source code.
39    fn write(&self, program: &Program) -> String;
40}