Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions compiler/pipec-ast/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ impl<'this> ASTGenerator<'this> {
Token::FunctionKeyword => self.consume_function_keyword(),
Token::PublicKeyword => self.consume_public_keyword(),
Token::TypeKeyword => self.consume_type_keyword(),
Token::AtSign => self.consume_attributes(),
_v => {
println!("{_v:#?}");
todo!();
Expand All @@ -87,6 +88,34 @@ impl<'this> ASTGenerator<'this> {
}
}

#[inline]
pub(crate) fn consume_attributes(&mut self) -> ASTNode {
let mut attributes = Vec::new();
loop {
if self.next_is(Token::AtSign) {
self.advance_stream();
let name = self
.must_ident()
.parse_arena(self.loader.load(self.src), self.arena);
match name {
"language" => attributes.push(self.consume_language_attribute()),
_ => todo!(),
}
} else {
break;
}
}
ASTNode::Attributed(attributes, Box::new(self.parse_value()))
}

#[inline]
pub(crate) fn consume_language_attribute(&mut self) -> Attribute {
self.must(Token::LeftParenthesis);
let name = self.must_string();
self.must(Token::RightParenthesis);
Attribute::LanguageAttribute(name)
}

#[inline]
pub(crate) fn consume_type_keyword(&mut self) -> ASTNode {
self.advance_stream();
Expand Down Expand Up @@ -275,6 +304,14 @@ impl<'this> ASTGenerator<'this> {
unreachable!()
}

#[inline]
pub(crate) fn must_string(&mut self) -> Span {
if let Some(Token::String(v)) = self.advance_stream() {
return v;
}
unreachable!()
}

#[inline]
pub(crate) fn consume_function_parameter(&mut self) -> FunctionDeclarationParameter {
let name = self.must_ident();
Expand Down Expand Up @@ -1127,9 +1164,15 @@ pub enum ASTNode {
subtype: SubType,
},
Public(Box<Self>),
Attributed(Vec<Attribute>, Box<Self>),
EOF,
}

#[derive(Debug, Clone)]
pub enum Attribute {
LanguageAttribute(Span),
}

#[derive(Debug, Clone)]
pub enum SubType {
Map(HashMap<String, Self>),
Expand Down