1
0
dust/dust-lang/src/parse.rs

350 lines
10 KiB
Rust
Raw Normal View History

2024-08-04 00:23:52 +00:00
use crate::{
lex::{LexError, Lexer},
2024-08-05 03:11:04 +00:00
Node, Span, Statement, Token, Value,
2024-08-04 00:23:52 +00:00
};
2024-08-05 03:11:04 +00:00
pub fn parse(input: &str) -> Result<Vec<Node>, ParseError> {
2024-08-04 23:25:44 +00:00
let lexer = Lexer::new(input);
let mut parser = Parser::new(lexer);
2024-08-05 02:15:31 +00:00
let mut instructions = Vec::new();
2024-08-04 23:25:44 +00:00
2024-08-05 02:15:31 +00:00
loop {
let instruction = parser.parse()?;
2024-08-04 23:25:44 +00:00
2024-08-05 02:15:31 +00:00
instructions.push(instruction);
2024-08-05 00:08:43 +00:00
2024-08-05 02:15:31 +00:00
if let Token::Eof = parser.current.0 {
break;
}
2024-08-05 00:08:43 +00:00
}
2024-08-05 02:15:31 +00:00
Ok(instructions)
2024-08-04 00:23:52 +00:00
}
pub struct Parser<'src> {
lexer: Lexer<'src>,
current: (Token, Span),
}
impl<'src> Parser<'src> {
pub fn new(lexer: Lexer<'src>) -> Self {
let mut lexer = lexer;
2024-08-05 00:08:43 +00:00
let current = lexer.next_token().unwrap_or((Token::Eof, (0, 0)));
Parser { lexer, current }
2024-08-04 00:23:52 +00:00
}
2024-08-05 03:11:04 +00:00
pub fn parse(&mut self) -> Result<Node, ParseError> {
2024-08-04 00:23:52 +00:00
self.parse_instruction(0)
}
fn next_token(&mut self) -> Result<(), ParseError> {
self.current = self.lexer.next_token()?;
Ok(())
}
2024-08-05 03:11:04 +00:00
fn parse_instruction(&mut self, precedence: u8) -> Result<Node, ParseError> {
2024-08-05 00:08:43 +00:00
let left_instruction = self.parse_primary()?;
let left_start = left_instruction.span.0;
2024-08-04 00:23:52 +00:00
if precedence < self.current_precedence() {
match &self.current {
(Token::Plus, _) => {
self.next_token()?;
2024-08-05 00:08:43 +00:00
let right_instruction = self.parse_instruction(self.current_precedence())?;
let right_end = right_instruction.span.1;
2024-08-04 00:23:52 +00:00
2024-08-05 03:11:04 +00:00
return Ok(Node::new(
Statement::Add(Box::new((left_instruction, right_instruction))),
2024-08-05 00:08:43 +00:00
(left_start, right_end),
2024-08-04 00:23:52 +00:00
));
}
(Token::Star, _) => {
self.next_token()?;
2024-08-05 00:08:43 +00:00
let right_instruction = self.parse_instruction(self.current_precedence())?;
let right_end = right_instruction.span.1;
2024-08-04 00:23:52 +00:00
2024-08-05 03:11:04 +00:00
return Ok(Node::new(
Statement::Multiply(Box::new((left_instruction, right_instruction))),
2024-08-05 00:08:43 +00:00
(left_start, right_end),
2024-08-04 00:23:52 +00:00
));
}
(Token::Equal, _) => {
self.next_token()?;
2024-08-05 00:08:43 +00:00
let right_instruction = self.parse_instruction(self.current_precedence())?;
let right_end = right_instruction.span.1;
2024-08-04 00:23:52 +00:00
2024-08-05 03:11:04 +00:00
return Ok(Node::new(
Statement::Assign(Box::new((left_instruction, right_instruction))),
2024-08-05 00:08:43 +00:00
(left_start, right_end),
2024-08-04 00:23:52 +00:00
));
}
_ => {}
}
}
2024-08-05 00:08:43 +00:00
Ok(left_instruction)
2024-08-04 00:23:52 +00:00
}
2024-08-05 03:11:04 +00:00
fn parse_primary(&mut self) -> Result<Node, ParseError> {
2024-08-04 00:23:52 +00:00
match self.current.clone() {
2024-08-05 00:08:43 +00:00
(Token::Float(float), span) => {
2024-08-04 00:23:52 +00:00
self.next_token()?;
2024-08-05 00:08:43 +00:00
2024-08-05 03:11:04 +00:00
Ok(Node::new(Statement::Constant(Value::float(float)), span))
2024-08-05 00:08:43 +00:00
}
(Token::Integer(int), span) => {
self.next_token()?;
2024-08-05 03:11:04 +00:00
Ok(Node::new(Statement::Constant(Value::integer(int)), span))
2024-08-04 00:23:52 +00:00
}
(Token::Identifier(identifier), span) => {
self.next_token()?;
2024-08-05 00:08:43 +00:00
2024-08-05 03:11:04 +00:00
Ok(Node::new(Statement::Identifier(identifier), span))
2024-08-04 00:23:52 +00:00
}
(Token::LeftParenthesis, left_span) => {
self.next_token()?;
2024-08-05 00:08:43 +00:00
let instruction = self.parse_instruction(0)?;
2024-08-04 00:23:52 +00:00
if let (Token::RightParenthesis, right_span) = self.current {
self.next_token()?;
2024-08-05 03:11:04 +00:00
Ok(Node::new(
2024-08-05 00:08:43 +00:00
instruction.operation,
(left_span.0, right_span.1),
))
2024-08-04 00:23:52 +00:00
} else {
2024-08-05 01:31:18 +00:00
Err(ParseError::ExpectedClosingParenthesis {
actual: self.current.0.clone(),
span: self.current.1,
})
}
}
(Token::LeftSquareBrace, left_span) => {
self.next_token()?;
let mut instructions = Vec::new();
loop {
if let (Token::RightSquareBrace, right_span) = self.current {
self.next_token()?;
2024-08-05 03:11:04 +00:00
return Ok(Node::new(
Statement::List(instructions),
2024-08-05 01:31:18 +00:00
(left_span.0, right_span.1),
));
}
if let (Token::Comma, _) = self.current {
self.next_token()?;
continue;
}
if let Ok(instruction) = self.parse_instruction(0) {
instructions.push(instruction);
} else {
return Err(ParseError::ExpectedClosingSquareBrace {
actual: self.current.0.clone(),
span: self.current.1,
});
}
2024-08-04 00:23:52 +00:00
}
}
_ => Err(ParseError::UnexpectedToken(self.current.0.clone())),
}
}
fn current_precedence(&self) -> u8 {
2024-08-05 01:31:18 +00:00
match self.current.0 {
Token::Equal => 3,
Token::Plus => 1,
Token::Star => 2,
2024-08-04 00:23:52 +00:00
_ => 0,
}
}
}
2024-08-04 23:25:44 +00:00
#[derive(Debug, PartialEq, Clone)]
pub enum ParseError {
2024-08-05 01:31:18 +00:00
ExpectedClosingParenthesis { actual: Token, span: Span },
ExpectedClosingSquareBrace { actual: Token, span: Span },
2024-08-04 23:25:44 +00:00
LexError(LexError),
UnexpectedToken(Token),
}
impl From<LexError> for ParseError {
fn from(v: LexError) -> Self {
Self::LexError(v)
}
}
2024-08-04 00:23:52 +00:00
#[cfg(test)]
mod tests {
2024-08-05 02:15:31 +00:00
use crate::Identifier;
2024-08-04 00:23:52 +00:00
2024-08-04 23:25:44 +00:00
use super::*;
2024-08-04 00:23:52 +00:00
2024-08-05 01:39:57 +00:00
#[test]
fn complex_list() {
let input = "[1, 1 + 1, 2 + (4 * 10)]";
assert_eq!(
parse(input),
2024-08-05 03:11:04 +00:00
Ok(vec![Node::new(
Statement::List(vec![
Node::new(Statement::Constant(Value::integer(1)), (1, 2)),
Node::new(
Statement::Add(Box::new((
Node::new(Statement::Constant(Value::integer(1)), (4, 5)),
Node::new(Statement::Constant(Value::integer(1)), (8, 9)),
2024-08-05 01:39:57 +00:00
))),
(4, 9)
),
2024-08-05 03:11:04 +00:00
Node::new(
Statement::Add(Box::new((
Node::new(Statement::Constant(Value::integer(2)), (11, 12)),
Node::new(
Statement::Multiply(Box::new((
Node::new(Statement::Constant(Value::integer(4)), (16, 17)),
Node::new(Statement::Constant(Value::integer(10)), (20, 22)),
2024-08-05 01:39:57 +00:00
))),
(15, 23)
),
))),
(11, 23)
)
]),
(0, 24)
2024-08-05 02:15:31 +00:00
)])
2024-08-05 01:39:57 +00:00
);
}
2024-08-05 01:31:18 +00:00
#[test]
fn list() {
let input = "[1, 2]";
assert_eq!(
parse(input),
2024-08-05 03:11:04 +00:00
Ok(vec![Node::new(
Statement::List(vec![
Node::new(Statement::Constant(Value::integer(1)), (1, 2)),
Node::new(Statement::Constant(Value::integer(2)), (4, 5)),
2024-08-05 01:31:18 +00:00
]),
(0, 6)
2024-08-05 02:15:31 +00:00
)])
2024-08-05 01:31:18 +00:00
);
}
#[test]
fn empty_list() {
let input = "[]";
assert_eq!(
parse(input),
2024-08-05 03:11:04 +00:00
Ok(vec![Node::new(Statement::List(vec![]), (0, 2))])
2024-08-05 01:31:18 +00:00
);
}
2024-08-05 00:08:43 +00:00
#[test]
fn float() {
let input = "42.0";
assert_eq!(
parse(input),
2024-08-05 03:11:04 +00:00
Ok(vec![Node::new(
Statement::Constant(Value::float(42.0)),
2024-08-05 00:08:43 +00:00
(0, 4)
2024-08-05 02:15:31 +00:00
)])
2024-08-05 00:08:43 +00:00
);
}
2024-08-04 00:23:52 +00:00
#[test]
fn add() {
let input = "1 + 2";
assert_eq!(
2024-08-04 23:25:44 +00:00
parse(input),
2024-08-05 03:11:04 +00:00
Ok(vec![Node::new(
Statement::Add(Box::new((
Node::new(Statement::Constant(Value::integer(1)), (0, 1)),
Node::new(Statement::Constant(Value::integer(2)), (4, 5)),
2024-08-04 00:23:52 +00:00
))),
(0, 5)
2024-08-05 02:15:31 +00:00
)])
2024-08-04 00:23:52 +00:00
);
}
#[test]
fn multiply() {
let input = "1 * 2";
assert_eq!(
2024-08-04 23:25:44 +00:00
parse(input),
2024-08-05 03:11:04 +00:00
Ok(vec![Node::new(
Statement::Multiply(Box::new((
Node::new(Statement::Constant(Value::integer(1)), (0, 1)),
Node::new(Statement::Constant(Value::integer(2)), (4, 5)),
2024-08-04 00:23:52 +00:00
))),
(0, 5)
2024-08-05 02:15:31 +00:00
)])
2024-08-04 00:23:52 +00:00
);
}
#[test]
fn add_and_multiply() {
let input = "1 + 2 * 3";
assert_eq!(
2024-08-04 23:25:44 +00:00
parse(input),
2024-08-05 03:11:04 +00:00
Ok(vec![Node::new(
Statement::Add(Box::new((
Node::new(Statement::Constant(Value::integer(1)), (0, 1)),
Node::new(
Statement::Multiply(Box::new((
Node::new(Statement::Constant(Value::integer(2)), (4, 5)),
Node::new(Statement::Constant(Value::integer(3)), (8, 9)),
2024-08-05 00:08:43 +00:00
))),
(4, 9)
),
2024-08-04 00:23:52 +00:00
))),
(0, 9)
2024-08-05 02:15:31 +00:00
)])
2024-08-04 00:23:52 +00:00
);
}
#[test]
fn assignment() {
let input = "a = 1 + 2 * 3";
assert_eq!(
2024-08-04 23:25:44 +00:00
parse(input),
2024-08-05 03:11:04 +00:00
Ok(vec![Node::new(
Statement::Assign(Box::new((
Node::new(Statement::Identifier(Identifier::new("a")), (0, 1)),
Node::new(
Statement::Add(Box::new((
Node::new(Statement::Constant(Value::integer(1)), (4, 5)),
Node::new(
Statement::Multiply(Box::new((
Node::new(Statement::Constant(Value::integer(2)), (8, 9)),
Node::new(Statement::Constant(Value::integer(3)), (12, 13)),
2024-08-05 00:08:43 +00:00
))),
(8, 13)
),
))),
(4, 13)
),
2024-08-04 00:23:52 +00:00
))),
(0, 13)
2024-08-05 02:15:31 +00:00
)])
2024-08-04 00:23:52 +00:00
);
}
}