Skip to content

Commit

Permalink
break and continue are not items (FuelLabs#2536)
Browse files Browse the repository at this point in the history
Co-authored-by: Mohammad Fawaz <[email protected]>
  • Loading branch information
canndrew and mohammadfawaz authored Aug 16, 2022
1 parent fddb206 commit f8c5d57
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 89 deletions.
8 changes: 8 additions & 0 deletions sway-ast/src/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ pub enum Expr {
reassignment_op: ReassignmentOp,
expr: Box<Expr>,
},
Break {
break_token: BreakToken,
},
Continue {
continue_token: ContinueToken,
},
}

impl Spanned for Expr {
Expand Down Expand Up @@ -233,6 +239,8 @@ impl Spanned for Expr {
Expr::Reassignment {
assignable, expr, ..
} => Span::join(assignable.span(), expr.span()),
Expr::Break { break_token } => break_token.span(),
Expr::Continue { continue_token } => continue_token.span(),
}
}
}
Expand Down
29 changes: 0 additions & 29 deletions sway-ast/src/item/item_control_flow.rs

This file was deleted.

5 changes: 0 additions & 5 deletions sway-ast/src/item/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::priv_prelude::*;

pub mod item_abi;
pub mod item_const;
pub mod item_control_flow;
pub mod item_enum;
pub mod item_fn;
pub mod item_impl;
Expand Down Expand Up @@ -34,8 +33,6 @@ pub enum ItemKind {
Abi(ItemAbi),
Const(ItemConst),
Storage(ItemStorage),
Break(ItemBreak),
Continue(ItemContinue),
}

impl Spanned for ItemKind {
Expand All @@ -50,8 +47,6 @@ impl Spanned for ItemKind {
ItemKind::Abi(item_abi) => item_abi.span(),
ItemKind::Const(item_const) => item_const.span(),
ItemKind::Storage(item_storage) => item_storage.span(),
ItemKind::Break(item_break) => item_break.span(),
ItemKind::Continue(item_continue) => item_continue.span(),
}
}
}
Expand Down
1 change: 0 additions & 1 deletion sway-ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ pub use crate::{
item::{
item_abi::ItemAbi,
item_const::ItemConst,
item_control_flow::{ItemBreak, ItemContinue},
item_enum::ItemEnum,
item_fn::ItemFn,
item_impl::ItemImpl,
Expand Down
1 change: 0 additions & 1 deletion sway-ast/src/priv_prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ pub use {
item::{
item_abi::ItemAbi,
item_const::ItemConst,
item_control_flow::{ItemBreak, ItemContinue},
item_enum::ItemEnum,
item_fn::ItemFn,
item_impl::ItemImpl,
Expand Down
32 changes: 22 additions & 10 deletions sway-core/src/convert_parse_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,16 +347,6 @@ fn item_to_ast_nodes(ec: &mut ErrorContext, item: Item) -> Result<Vec<AstNode>,
Declaration::StorageDeclaration(storage_declaration),
)]
}
ItemKind::Break(_) => {
vec![AstNodeContent::Declaration(Declaration::Break {
span: span.clone(),
})]
}
ItemKind::Continue(_) => {
vec![AstNodeContent::Declaration(Declaration::Continue {
span: span.clone(),
})]
}
};
Ok(contents
.into_iter()
Expand Down Expand Up @@ -1890,6 +1880,28 @@ fn expr_to_expression(ec: &mut ErrorContext, expr: Expr) -> Result<Expression, E
let error = ConvertParseTreeError::ReassignmentOutsideOfBlock { span };
return Err(ec.error(error));
}
Expr::Break { .. } => Expression {
kind: ExpressionKind::CodeBlock(CodeBlock {
contents: vec![AstNode {
content: AstNodeContent::Declaration(Declaration::Break { span: span.clone() }),
span: span.clone(),
}],
whole_block_span: span.clone(),
}),
span,
},
Expr::Continue { .. } => Expression {
kind: ExpressionKind::CodeBlock(CodeBlock {
contents: vec![AstNode {
content: AstNodeContent::Declaration(Declaration::Continue {
span: span.clone(),
}),
span: span.clone(),
}],
whole_block_span: span.clone(),
}),
span,
},
};
Ok(expression)
}
Expand Down
8 changes: 8 additions & 0 deletions sway-fmt-v2/src/utils/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,8 @@ impl Format for Expr {
reassignment_op.format(formatted_code, formatter)?;
expr.format(formatted_code, formatter)?;
}
Self::Break { .. } => write!(formatted_code, "break")?,
Self::Continue { .. } => write!(formatted_code, "continue")?,
}

Ok(())
Expand Down Expand Up @@ -937,5 +939,11 @@ fn visit_expr(expr: &Expr) -> Vec<ByteSpan> {
collected_spans.append(&mut expr.leaf_spans());
collected_spans
}
Expr::Break { break_token } => {
vec![ByteSpan::from(break_token.span())]
}
Expr::Continue { continue_token } => {
vec![ByteSpan::from(continue_token.span())]
}
}
}
4 changes: 0 additions & 4 deletions sway-fmt-v2/src/utils/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ impl LeafSpans for Item {
Trait(item_trait) => item_trait.leaf_spans(),
Impl(item_impl) => item_impl.leaf_spans(),
Use(item_use) => item_use.leaf_spans(),
Break(_) => todo!(),
Continue(_) => todo!(),
}
}
}
Expand All @@ -38,8 +36,6 @@ impl Format for Item {
Abi(item_abi) => item_abi.format(formatted_code, formatter),
Const(item_const) => item_const.format(formatted_code, formatter),
Storage(item_storage) => item_storage.format(formatted_code, formatter),
Break(_item_break) => todo!(),
Continue(_item_continue) => todo!(),
}
}
}
Expand Down
16 changes: 10 additions & 6 deletions sway-parse/src/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use core::ops::ControlFlow;
use sway_ast::brackets::{Braces, Parens, SquareBrackets};
use sway_ast::expr::{ReassignmentOp, ReassignmentOpVariant};
use sway_ast::keywords::{
AbiToken, AddEqToken, AsmToken, BreakToken, CommaToken, ConstToken, ContinueToken, DivEqToken,
DoubleColonToken, EnumToken, EqToken, FalseToken, FnToken, IfToken, ImplToken, LetToken,
OpenAngleBracketToken, PubToken, SemicolonToken, ShlEqToken, ShrEqToken, StarEqToken,
StorageToken, StructToken, SubEqToken, TildeToken, TraitToken, TrueToken, UseToken,
AbiToken, AddEqToken, AsmToken, CommaToken, ConstToken, DivEqToken, DoubleColonToken,
EnumToken, EqToken, FalseToken, FnToken, IfToken, ImplToken, LetToken, OpenAngleBracketToken,
PubToken, SemicolonToken, ShlEqToken, ShrEqToken, StarEqToken, StorageToken, StructToken,
SubEqToken, TildeToken, TraitToken, TrueToken, UseToken,
};
use sway_ast::literal::{LitBool, LitBoolType};
use sway_ast::punctuated::Punctuated;
Expand Down Expand Up @@ -155,8 +155,6 @@ fn parse_stmt<'a>(parser: &mut Parser<'a, '_>) -> ParseResult<StmtOrTail<'a>> {
|| parser.peek::<ImplToken>().is_some()
|| parser.peek::<(AbiToken, Ident)>().is_some()
|| parser.peek::<ConstToken>().is_some()
|| parser.peek::<BreakToken>().is_some()
|| parser.peek::<ContinueToken>().is_some()
|| matches!(
parser.peek::<(StorageToken, Delimiter)>(),
Some((_, Delimiter::Brace))
Expand Down Expand Up @@ -614,6 +612,12 @@ fn parse_atom(parser: &mut Parser, ctx: ParseExprCtx) -> ParseResult<Expr> {
if let Some(asm_block) = parser.guarded_parse::<AsmToken, _>()? {
return Ok(Expr::Asm(asm_block));
}
if let Some(break_token) = parser.take() {
return Ok(Expr::Break { break_token });
}
if let Some(continue_token) = parser.take() {
return Ok(Expr::Continue { continue_token });
}
if let Some(abi_token) = parser.take() {
let args = parser.parse()?;
return Ok(Expr::AbiCast { abi_token, args });
Expand Down
25 changes: 0 additions & 25 deletions sway-parse/src/item/item_control_flow.rs

This file was deleted.

10 changes: 2 additions & 8 deletions sway-parse/src/item/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::{Parse, ParseErrorKind, ParseResult, ParseToEnd, Parser, ParserConsumed};

use sway_ast::keywords::{
AbiToken, BreakToken, ConstToken, ContinueToken, EnumToken, FnToken, ImplToken, MutToken,
OpenAngleBracketToken, RefToken, SelfToken, StorageToken, StructToken, TraitToken, UseToken,
WhereToken,
AbiToken, ConstToken, EnumToken, FnToken, ImplToken, MutToken, OpenAngleBracketToken, RefToken,
SelfToken, StorageToken, StructToken, TraitToken, UseToken, WhereToken,
};
use sway_ast::token::{DocComment, DocStyle};
use sway_ast::{
Expand All @@ -13,7 +12,6 @@ use sway_ast::{

mod item_abi;
mod item_const;
mod item_control_flow;
mod item_enum;
mod item_fn;
mod item_impl;
Expand Down Expand Up @@ -54,10 +52,6 @@ impl Parse for ItemKind {
ItemKind::Const(item)
} else if let Some(item) = parser.guarded_parse::<StorageToken, _>()? {
ItemKind::Storage(item)
} else if let Some(item) = parser.guarded_parse::<BreakToken, _>()? {
ItemKind::Break(item)
} else if let Some(item) = parser.guarded_parse::<ContinueToken, _>()? {
ItemKind::Continue(item)
} else {
return Err(parser.emit_error(ParseErrorKind::ExpectedAnItem));
};
Expand Down

0 comments on commit f8c5d57

Please sign in to comment.