Add a few more tests, some miscellaneous cleanup/fixes

This commit is contained in:
Jesse Braham 2024-12-26 13:42:52 +01:00
parent 223554d49d
commit 8dcdd34b65
4 changed files with 39 additions and 5 deletions

View File

@ -1,6 +1,11 @@
//! Onihime programming language. //! Onihime programming language.
#![deny(missing_debug_implementations, missing_docs, rust_2018_idioms)] #![deny(
missing_debug_implementations,
missing_docs,
rust_2018_idioms,
unsafe_code
)]
mod lexer; mod lexer;
mod parser; mod parser;

View File

@ -1,4 +1,4 @@
use super::error::{ParserError, ParserErrorKind}; use super::error::ParserError;
use crate::{ use crate::{
lexer::{Symbol, Token, TokenKind}, lexer::{Symbol, Token, TokenKind},
span::Span, span::Span,

View File

@ -42,7 +42,7 @@ impl<'parser> Parser<'parser> {
// run out of tokens, or an error occurs: // run out of tokens, or an error occurs:
while !self.lexer.eof() { while !self.lexer.eof() {
if let Some(node) = self.expr()? { if let Some(node) = self.expr()? {
self.current.push_node(node); self.current.push_node(node)?;
} }
} }
@ -88,7 +88,7 @@ impl<'parser> Parser<'parser> {
let node = Node::try_from(token)?; let node = Node::try_from(token)?;
let span = node.span.clone(); let span = node.span.clone();
self.current.push_node(node); self.current.push_node(node)?;
self.current.span.extend(&span); self.current.span.extend(&span);
Ok(None) Ok(None)
@ -160,7 +160,7 @@ mod tests {
( $name:ident: $input:literal, $src:ident => $ast:expr ) => { ( $name:ident: $input:literal, $src:ident => $ast:expr ) => {
#[test] #[test]
fn $name() { fn $name() {
let mut parser = Parser::new($input); let parser = Parser::new($input);
let $src = parser.lexer.source(); let $src = parser.lexer.source();
assert_eq!(parser.parse(), $ast); assert_eq!(parser.parse(), $ast);
} }

View File

@ -197,4 +197,33 @@ mod tests {
assert_eq!(source.get_line(1), "line 2"); assert_eq!(source.get_line(1), "line 2");
assert_eq!(source.get_line(2), "line 3"); assert_eq!(source.get_line(2), "line 3");
} }
#[test]
fn span_partial_eq() {
let source = Arc::new(Source::new(None, String::new()));
let a = Span::new(0..0, source.clone());
assert_eq!(a, a);
let b = Span::new(1..10, source.clone());
assert_ne!(a, b);
let source2 = Arc::new(Source::new(None, String::from("foo")));
let c = Span::new(0..0, source2.clone());
assert_ne!(a, c);
}
#[test]
fn span_start_end_location() {
let source = Arc::new(Source::new(None, "foo\nbar\nbaz".into()));
let span = Span::new(2..9, source);
let start = span.location();
assert_eq!(start.line, 0);
assert_eq!(start.column, 2);
let end = span.end_location();
assert_eq!(end.line, 2);
assert_eq!(end.column, 1);
}
} }