Fixed bug w/ lines in functions

Also improved debugging
This commit is contained in:
greg 2018-03-02 00:42:52 -08:00
parent 57c7858c87
commit f0e7c9906e
3 changed files with 16 additions and 5 deletions

View File

@ -1,2 +1,8 @@
fn main() {
const a = 10
const b = 20
a + b
}
main()
1 + 2

View File

@ -134,7 +134,9 @@ impl Parser {
fn peek(&mut self) -> TokenType { fn peek(&mut self) -> TokenType {
self.tokens.peek().map(|ref t| { t.token_type.clone() }).unwrap_or(TokenType::EOF) self.tokens.peek().map(|ref t| { t.token_type.clone() }).unwrap_or(TokenType::EOF)
} }
fn peek_with_token_offset(&mut self) -> Token {
self.tokens.peek().map(|t: &Token| { t.clone()}).unwrap_or(Token { token_type: TokenType::EOF, offset: 0})
}
fn next(&mut self) -> TokenType { fn next(&mut self) -> TokenType {
self.tokens.next().map(|ref t| { t.token_type.clone() }).unwrap_or(TokenType::EOF) self.tokens.next().map(|ref t| { t.token_type.clone() }).unwrap_or(TokenType::EOF)
} }
@ -259,7 +261,7 @@ pub struct Pattern(Rc<String>);
macro_rules! parse_method { macro_rules! parse_method {
($name:ident(&mut $self:ident) -> $type:ty $body:block) => { ($name:ident(&mut $self:ident) -> $type:ty $body:block) => {
fn $name(&mut $self) -> $type { fn $name(&mut $self) -> $type {
let next_token = $self.peek(); let next_token = $self.peek_with_token_offset();
let record = ParseRecord { let record = ParseRecord {
production_name: stringify!($name).to_string(), production_name: stringify!($name).to_string(),
next_token: format!("{:?}", next_token), next_token: format!("{:?}", next_token),
@ -405,7 +407,7 @@ impl Parser {
parse_method!(func_declaration(&mut self) -> ParseResult<Declaration> { parse_method!(func_declaration(&mut self) -> ParseResult<Declaration> {
let signature = self.signature()?; let signature = self.signature()?;
if let LCurlyBrace = self.peek() { if let LCurlyBrace = self.peek() {
let statements = delimited!(self, LCurlyBrace, '{', statement, Newline | Semicolon, RCurlyBrace, '}'); let statements = delimited!(self, LCurlyBrace, '{', statement, Newline | Semicolon, RCurlyBrace, '}', nonstrict);
Ok(Declaration::FuncDecl(signature, statements)) Ok(Declaration::FuncDecl(signature, statements))
} else { } else {
Ok(Declaration::FuncSig(signature)) Ok(Declaration::FuncSig(signature))
@ -982,6 +984,9 @@ mod parse_tests {
parse_test!("fn a(x) { x() }", AST(vec![Declaration( parse_test!("fn a(x) { x() }", AST(vec![Declaration(
FuncDecl(Signature { name: rc!(a), params: vec![(rc!(x),None)], type_anno: None }, FuncDecl(Signature { name: rc!(a), params: vec![(rc!(x),None)], type_anno: None },
vec![exprstatement!(Call { f: bx!(ex!(val!("x"))), arguments: vec![] })]))])); vec![exprstatement!(Call { f: bx!(ex!(val!("x"))), arguments: vec![] })]))]));
parse_test!("fn a(x) {\n x() }", AST(vec![Declaration(
FuncDecl(Signature { name: rc!(a), params: vec![(rc!(x),None)], type_anno: None },
vec![exprstatement!(Call { f: bx!(ex!(val!("x"))), arguments: vec![] })]))]));
} }
#[test] #[test]

View File

@ -67,7 +67,7 @@ lazy_static! {
}; };
} }
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct Token { pub struct Token {
pub token_type: TokenType, pub token_type: TokenType,
pub offset: usize, pub offset: usize,