Skip to content

Commit

Permalink
lex ;
Browse files Browse the repository at this point in the history
  • Loading branch information
xffxff committed Sep 20, 2023
1 parent 8e753cc commit 3d66271
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 3 deletions.
6 changes: 6 additions & 0 deletions components/lox-ir/src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,9 @@ impl<'db> salsa::DebugWithDb<dyn crate::Db + 'db> for Expr {
}
}
}


pub enum Stmt {
// expression statement, like `1 + 2;`
Expr(Expr),
}
5 changes: 5 additions & 0 deletions components/lox-ir/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ pub enum Token {
// String
String(Word),

// Semicolon
Semicolon,

// Unkown token
Unknown(char),
}
Expand All @@ -52,6 +55,7 @@ impl<'db> DebugWithDb<dyn crate::Db + 'db> for Token {
Token::Tree(tree) => f.debug_tuple("Tree").field(tree).finish(),
Token::String(word) => write!(f, "String({})", word.as_str(db)),
Token::Unknown(ch) => write!(f, "Unknown({})", ch),
Token::Semicolon => write!(f, "Semicolon"),
}
}
}
Expand All @@ -66,6 +70,7 @@ impl Token {
Token::Comment(s) => *s,
Token::Tree(tree) => tree.span(db).len(),
Token::String(s) => s.as_str(db).len() as u32,
Token::Semicolon => 1,
}
}

Expand Down
3 changes: 3 additions & 0 deletions components/lox-lex/src/lex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ where
' ' => {
push_token(Token::Whitespace(ch));
}
';' => {
push_token(Token::Semicolon);
}
_ => {
if ch.is_whitespace() {
push_token(Token::Whitespace(ch))
Expand Down
8 changes: 7 additions & 1 deletion components/lox-parse/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use lox_ir::{
input_file::InputFile,
kw::Keyword,
span::Span,
syntax::{Expr, Op},
syntax::{Expr, Op, Stmt},
token::Token,
token_tree::TokenTree,
};
Expand Down Expand Up @@ -42,6 +42,12 @@ impl<'me> Parser<'me> {
exprs
}

// fn expr_stmt(&mut self) -> Option<Stmt> {
// let expr = self.parse_expr()?;
// self.eat(Token::Semicolon);
// Some(Stmt::Expr(expr))
// }

// expression → equality ;
// equality → comparison ( ( "!=" | "==" ) comparison )* ;
// comparison → term ( ( ">" | ">=" | "<" | "<=" ) term )* ;
Expand Down
2 changes: 1 addition & 1 deletion lox_tests/string.lox
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"hello" + " world"
"hello" + " world";
12 changes: 12 additions & 0 deletions lox_tests/string.syntax
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,15 @@ BinaryOp {
op: Plus,
right: StringLiteral( world),
}
Diagnostic {
severity: Error,
span: @14..@15,
message: "extra tokens after expression",
labels: [
Label {
span: @14..@15,
message: "here",
},
],
children: [],
}
3 changes: 2 additions & 1 deletion lox_tests/string.token
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
TokenTree {
source text: "\"hello\" + \" world\"",
source text: "\"hello\" + \" world\";",
tokens: [
String(hello),
Whitespace(' '),
Op(+),
Whitespace(' '),
String( world),
Semicolon,
],
}

0 comments on commit 3d66271

Please sign in to comment.