Skip to content

Commit fa487d4

Browse files
author
Ray Redondo
committed
feat(rust-parse): support if-elseif-else
1 parent a1a3d75 commit fa487d4

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

rust/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ impl XLangFrontend for RustFrontend {
8686
filter_comments(&mut lexed);
8787
println!("{:?}", lexed);
8888
let parsed = do_mod(&mut lexed.into_iter().peekmore()).unwrap();
89+
println!("{:?}", parsed);
8990
let mut defs = Definitions::new(props);
9091
convert_crate(&mut defs, &parsed, CrateType::Bin).unwrap();
9192

rust/src/parse.rs

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
Label, LetStatement, Lifetime, LifetimeParam, Literal, LiteralKind, Mod, Param, Path,
1111
PathSegment, Pattern, Safety, SelfParam, SimplePath, SimplePathSegment, Spanned, Statement,
1212
StructCtor, StructField, StructKind, TraitDef, TupleCtor, TupleField, Type, TypeParam,
13-
UnaryOp, UserType, UserTypeBody, Visibility, WhereClause,
13+
UnaryOp, UserType, UserTypeBody, Visibility, WhereClause, IfBlock, CondBlock,
1414
},
1515
interning::Symbol,
1616
lex::{
@@ -754,10 +754,56 @@ pub fn do_item_user_type(
754754
})
755755
}
756756

757+
pub fn do_if_block(
758+
tree: &mut PeekMoreIterator<impl Iterator<Item = Lexeme>>,
759+
) -> Result<Spanned<IfBlock>> {
760+
let mut tree = tree.into_rewinder();
761+
let span_start = do_lexeme_class(&mut tree, LexemeClass::Keyword(Keyword::If))?.span;
762+
let cond = Box::new(do_expression_without_constructor(&mut tree)?);
763+
let block = do_block(&mut tree)?;
764+
let mut span_end = block.span;
765+
let mut elseifs = Vec::new();
766+
let mut elseblock = None;
767+
while let Ok(Lexeme { span: else_span, .. }) = do_lexeme_class(&mut tree, LexemeClass::Keyword(Keyword::Else)) {
768+
match do_lexeme_class(&mut tree, LexemeClass::Keyword(Keyword::If)) {
769+
Ok(Lexeme { span: if_span, .. }) => {
770+
let cond = Box::new(do_expression_without_constructor(&mut tree)?);
771+
let block = do_block(&mut tree)?;
772+
span_end = block.span;
773+
let span = Span::between(if_span, block.span);
774+
elseifs.push(Spanned {
775+
body: CondBlock {
776+
cond,
777+
block,
778+
},
779+
span,
780+
});
781+
}
782+
Err(_) => {
783+
let block = do_block(&mut tree)?;
784+
span_end = block.span;
785+
elseblock = Some(block);
786+
break;
787+
}
788+
}
789+
}
790+
tree.accept();
791+
let span = Span::between(span_start, span_end);
792+
Ok(Spanned {
793+
body: IfBlock { cond, block, elseifs, elseblock },
794+
span,
795+
})
796+
}
797+
757798
pub fn do_compound_block(
758799
tree: &mut PeekMoreIterator<impl Iterator<Item = Lexeme>>,
759800
) -> Result<Spanned<CompoundBlock>> {
760-
// TODO: support more than `unsafe` and `loop`
801+
if let Ok(if_block) = do_if_block(tree) {
802+
return Ok(Spanned {
803+
span: if_block.span,
804+
body: CompoundBlock::If(if_block),
805+
});
806+
}
761807
let mut tree = tree.into_rewinder();
762808
let (
763809
Lexeme {

0 commit comments

Comments
 (0)