ty-parser 0.1.0

C-syntax parser for the Ty programming language
Documentation
#![deny(missing_docs)]

//! C-syntax parser for the Ty programming language.

mod lexer;
mod parser;
mod token;

/// Source location (line and column).
#[derive(Copy, Clone, Debug)]
pub struct Span {
    /// Line number (1-based).
    pub line: u32,
    /// Column number (1-based).
    pub column: u32,
}

impl std::fmt::Display for Span {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(formatter, "{}:{}", self.line, self.column)
    }
}

/// A parse error with optional source location.
#[derive(Debug)]
pub struct Error {
    /// Human-readable error description.
    pub message: String,
    /// Source location where the error occurred.
    pub span: Option<Span>,
}

impl std::fmt::Display for Error {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if let Some(span) = self.span {
            write!(formatter, "{span}: {}", self.message)
        } else {
            write!(formatter, "{}", self.message)
        }
    }
}

impl std::error::Error for Error {}

type Result<T> = std::result::Result<T, Error>;

/// Parses Ty source code (C-syntax) into a typed AST module.
pub fn parse(source: &str) -> Result<ty_ree::declaration::Module> {
    let mut lexer = lexer::Lexer::new(source);
    let tokens = lexer.tokenize()?;
    let mut parser = parser::Parser::new(tokens);
    parser.parse_module()
}