Added paren checks

pull/2/head
Mark 2023-03-23 15:08:48 -07:00
parent 0633ae5335
commit a734bdc4fe
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
3 changed files with 24 additions and 1 deletions

View File

@ -1,7 +1,6 @@
# TODO # TODO
- Fix documentation and variable names - Fix documentation and variable names
- Function application - Function application
- Syntax check (parenthesis, argument type)
# Eventually # Eventually
- tests - tests

View File

@ -75,6 +75,8 @@ pub struct LineLocation {
pub enum ParserError { pub enum ParserError {
InvalidChar, InvalidChar,
MissingCloseParen, MissingCloseParen,
ExtraCloseParen,
EmptyGroup,
Syntax, Syntax,
BadNumber BadNumber
} }

View File

@ -128,6 +128,7 @@ fn push_token(
pub fn tokenize(input: &String) -> Result<Token, (LineLocation, ParserError)> { pub fn tokenize(input: &String) -> Result<Token, (LineLocation, ParserError)> {
let mut t: Option<Token> = None; // The current token we're reading let mut t: Option<Token> = None; // The current token we're reading
let mut g: Vec<Token> = Vec::with_capacity(8); // Vector of "grouping levels" let mut g: Vec<Token> = Vec::with_capacity(8); // Vector of "grouping levels"
let mut i_level = 0;
g.push(Token::PreGroup(LineLocation{pos: 0, len: 0}, VecDeque::with_capacity(8))); g.push(Token::PreGroup(LineLocation{pos: 0, len: 0}, VecDeque::with_capacity(8)));
@ -229,8 +230,29 @@ pub fn tokenize(input: &String) -> Result<Token, (LineLocation, ParserError)> {
'(' => { '(' => {
push_token(g_now, i, t)?; t = None; push_token(g_now, i, t)?; t = None;
g.push(Token::PreGroup(LineLocation{pos: i, len: 0}, VecDeque::with_capacity(8))); g.push(Token::PreGroup(LineLocation{pos: i, len: 0}, VecDeque::with_capacity(8)));
i_level += 1;
}, },
')' => { ')' => {
// Catch extra close parens
if i_level == 0 {
return Err((
LineLocation{pos: i, len: 1},
ParserError::ExtraCloseParen
))
}
i_level -= 1;
// Catch empty groups
if t.is_none() {
let mut last = g.pop().unwrap();
last = update_line_location(last, i+1);
let Token::PreGroup(l, _) = last else {panic!()};
return Err((
l,
ParserError::EmptyGroup
))
}
push_token(g_now, i, t)?; push_token(g_now, i, t)?;
t = Some(g.pop().unwrap()); t = Some(g.pop().unwrap());
}, },