comments and docs as ast nodes

This commit is contained in:
janis 2025-10-01 00:31:08 +02:00
parent 45ba06db43
commit c270fe5add
Signed by: janis
SSH key fingerprint: SHA256:bB1qbbqmDXZNT0KKD5c2Dfjg53JGhj7B3CFcLIzSqq8

View file

@ -302,6 +302,7 @@ impl Ast {
#[derive(Debug)] #[derive(Debug)]
struct FunctionDecl { struct FunctionDecl {
attrs: Option<Index>,
name: String, name: String,
visibility: Visibility, visibility: Visibility,
return_type: Type, return_type: Type,
@ -338,7 +339,7 @@ pomelo! {
%extra_argument Ast; %extra_argument Ast;
%parser pub struct Parser<'a>{}; %parser pub struct Parser<'a>{};
%token #[derive(Debug)] pub enum Token<'a> {}; %token #[derive(Debug)] pub enum Token<'a> {};
// %default_type &'a str;
%type Ident &'a str; %type Ident &'a str;
%type DocComment &'a str; %type DocComment &'a str;
%type Comment &'a str; %type Comment &'a str;
@ -363,6 +364,12 @@ pomelo! {
list list
}; };
%type attrs Index;
attrs ::= DocComment(text) {
let idx = extra.push(AstNode::Doc { text: text.to_string() });
extra.push(AstNode::Attributes { attrs: vec![idx] })
};
typ ::= Bool { internment::Intern::new(InnerType::Bool) }; typ ::= Bool { internment::Intern::new(InnerType::Bool) };
typ ::= I1 { internment::Intern::new(InnerType::Int { signed: true, size: IntSize::Bits(1) }) }; typ ::= I1 { internment::Intern::new(InnerType::Int { signed: true, size: IntSize::Bits(1) }) };
typ ::= I8 { internment::Intern::new(InnerType::Int { signed: true, size: IntSize::Bits(8) }) }; typ ::= I8 { internment::Intern::new(InnerType::Int { signed: true, size: IntSize::Bits(8) }) };
@ -427,10 +434,12 @@ pomelo! {
pl pl
}; };
decl ::= Comment(text) { extra.push(AstNode::Comment { text: text.to_string() }) };
decl ::= fn_decl(f) { extra.push(AstNode::FunctionDecl(f)) }; decl ::= fn_decl(f) { extra.push(AstNode::FunctionDecl(f)) };
fn_decl ::= vis?(visibility) Fn Ident(name) LParen parameter_list?(parameters) RParen return_type(rtype) block(body) { fn_decl ::= attrs?(attrs) vis?(visibility) Fn Ident(name) LParen parameter_list?(parameters) RParen return_type(rtype) block(body) {
let name = name.to_string(); let name = name.to_string();
FunctionDecl { FunctionDecl {
attrs,
name, name,
visibility: visibility.unwrap_or_default(), visibility: visibility.unwrap_or_default(),
return_type: rtype, return_type: rtype,
@ -555,7 +564,11 @@ mod tests {
#[test] #[test]
fn parse() { fn parse() {
use crate::parser::{Parser, Token}; use crate::parser::{Parser, Token};
let input = "fn main(a: u32, b: u32) -> u32 {}"; let input = r#"
// A simple test case
/// A function that takes two u32 parameters and returns a u32
fn main(a: u32, b: u32) -> u32 {}
"#;
let mut lex = lexer::TokenIterator::new(input); let mut lex = lexer::TokenIterator::new(input);
let mut mapped = lex.inspect(|t| eprintln!("{t:?}")).map(Token::from); let mut mapped = lex.inspect(|t| eprintln!("{t:?}")).map(Token::from);
let mut ast = crate::Ast::new(); let mut ast = crate::Ast::new();