lisette_syntax/parse/
directives.rs1use super::Parser;
2use crate::ast::{Expression, Literal};
3use crate::lex::TokenKind::*;
4use crate::types::Type;
5
6impl<'source> Parser<'source> {
7 pub fn parse_directive(&mut self) -> Expression {
8 let start = self.current_token();
9 let directive_name = start.text.strip_prefix('@').unwrap_or(start.text);
10
11 match directive_name {
12 "rawgo" => self.parse_rawgo_directive(),
13 _ => {
14 self.track_error("unknown directive", "There is no such directive.");
15 self.next();
16 if self.is(LeftParen) {
17 self.collect_delimited_expressions(LeftParen, RightParen);
18 }
19 Expression::Unit {
20 ty: Type::uninferred(),
21 span: self.span_from_tokens(start),
22 }
23 }
24 }
25 }
26
27 fn parse_rawgo_directive(&mut self) -> Expression {
28 self.ensure(Directive);
29
30 let (args, _) = self.collect_delimited_expressions(LeftParen, RightParen);
31
32 let text = match args.into_iter().next() {
33 Some(Expression::Literal {
34 literal: Literal::String(s),
35 ..
36 }) => s,
37
38 _ => {
39 self.track_error("invalid call to rawgo", "Use `@rawgo(\"go code\")`.");
40 "?".to_string()
41 }
42 };
43
44 Expression::RawGo { text }
45 }
46}