Skip to content

Commit

Permalink
fix: error in if-chain
Browse files Browse the repository at this point in the history
  • Loading branch information
Ph0enixKM committed Sep 7, 2023
1 parent 889a49f commit 0a237d8
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 33 deletions.
30 changes: 21 additions & 9 deletions src/modules/command/modifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ impl CommandModifier {
self.is_expr = true;
self
}

fn flip_unsafe(&mut self, meta: &mut ParserMetadata, is_unsafe: bool) {
if is_unsafe {
swap(&mut self.is_unsafe, &mut meta.context.is_unsafe_ctx);
}
}
}

impl SyntaxModule<ParserMetadata> for CommandModifier {
Expand Down Expand Up @@ -79,30 +85,36 @@ impl SyntaxModule<ParserMetadata> for CommandModifier {
}
sequence = sequence.trim().to_string();
let is_unsafe = self.is_unsafe;
if is_unsafe {
swap(&mut self.is_unsafe, &mut meta.context.is_unsafe_ctx);
}
self.flip_unsafe(meta, is_unsafe);
if self.is_expr {
syntax(meta, &mut *self.expr)?;
if let Err(err) = syntax(meta, &mut *self.expr) {
self.flip_unsafe(meta, is_unsafe);
return Err(err)
}
if !matches!(self.expr.value, Some(ExprType::CommandExpr(_) | ExprType::FunctionInvocation(_))) {
self.flip_unsafe(meta, is_unsafe);
return error!(meta, tok, format!("Expected command or function call, after '{sequence}' command modifiers."));
}
} else {
match token(meta, "{") {
Ok(_) => {
syntax(meta, &mut *self.block)?;
if let Err(err) = syntax(meta, &mut *self.block) {
self.flip_unsafe(meta, is_unsafe);
return Err(err)
}
token(meta, "}")?;
},
Err(_) => {
let mut statement = Statement::new();
syntax(meta, &mut statement)?;
if let Err(err) = syntax(meta, &mut statement) {
self.flip_unsafe(meta, is_unsafe);
return Err(err)
}
self.block.push_statement(statement);
}
}
}
if is_unsafe {
swap(&mut self.is_unsafe, &mut meta.context.is_unsafe_ctx);
}
self.flip_unsafe(meta, is_unsafe);
Ok(())
}
}
Expand Down
1 change: 1 addition & 0 deletions src/modules/condition/failed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ impl SyntaxModule<ParserMetadata> for Failed {
self.is_parsed = true;
return Ok(());
} else {
dbg!("FAILED");
return error!(meta, tok, "Failed expression must be followed by a block or statement")
}
}
Expand Down
42 changes: 18 additions & 24 deletions src/modules/condition/ifchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ impl SyntaxModule<ParserMetadata> for IfChain {
}

fn parse(&mut self, meta: &mut ParserMetadata) -> SyntaxResult {
let mut is_else = false;
token(meta, "if")?;
// Parse true block
token(meta, "{")?;
Expand All @@ -35,11 +34,26 @@ impl SyntaxModule<ParserMetadata> for IfChain {
}
// Handle else keyword
if token(meta, "else").is_ok() {
is_else = true;
break
match token(meta, "{") {
Ok(_) => {
let mut false_block = Box::new(Block::new());
syntax(meta, &mut *false_block)?;
self.false_block = Some(false_block);
token(meta, "}")?;
}
Err(_) => {
let mut statement = Statement::new();
token(meta, ":")?;
syntax(meta, &mut statement)?;
self.false_block = Some(Box::new(Block::new()));
self.false_block.as_mut().unwrap().push_statement(statement);
}
}
token(meta, "}")?;
return Ok(())
}
if token(meta, "}").is_ok() {
break
return Ok(())
}
// Handle end of the if chain
if let Err(err) = syntax(meta, &mut cond) {
Expand All @@ -60,26 +74,6 @@ impl SyntaxModule<ParserMetadata> for IfChain {
}
}
}
// Parse false block
if is_else {
match token(meta, "{") {
Ok(_) => {
let mut false_block = Box::new(Block::new());
syntax(meta, &mut *false_block)?;
self.false_block = Some(false_block);
token(meta, "}")?;
}
Err(_) => {
let mut statement = Statement::new();
token(meta, ":")?;
syntax(meta, &mut statement)?;
self.false_block = Some(Box::new(Block::new()));
self.false_block.as_mut().unwrap().push_statement(statement);
}
}
}
token(meta, "}")?;
Ok(())
}
}

Expand Down
14 changes: 14 additions & 0 deletions src/tests/validity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,20 @@ fn if_statements_else() {

#[test]
fn if_statement_chain() {
let code = "
let x = 42
let y = 42
if {
x == y {
echo x
}
}
";
test_amber!(code, "42");
}

#[test]
fn if_statement_chain_else() {
let code = "
let x = 42
let y = 24
Expand Down

0 comments on commit 0a237d8

Please sign in to comment.