use crate::ast;
use crate::ast::Span;
#[derive(Debug, PartialEq)]
#[allow(missing_docs)]
pub struct InnerDocComment {
pub span: Span,
pub line: String,
}
#[derive(Debug, PartialEq)]
pub enum Statement {
Workbench(WorkbenchDefinition),
InlineModule(InlineModule),
FileModule(FileModule),
Function(FunctionDefinition),
Use(UseStatement),
Const(ConstAssignment),
Init(InitDefinition),
Return(Return),
InnerAttribute(Attribute),
InnerDocComment(InnerDocComment),
LocalAssignment(LocalAssignment),
Property(PropertyAssignment),
Expression(ExpressionStatement),
Error(Span),
}
impl Statement {
pub fn span(&self) -> Span {
use Statement::*;
match self {
Workbench(st) => st.span.clone(),
InlineModule(st) => st.span.clone(),
FileModule(st) => st.span.clone(),
Function(st) => st.span.clone(),
Use(st) => st.span.clone(),
Const(st) => st.span.clone(),
Init(st) => st.span.clone(),
Return(st) => st.span.clone(),
InnerAttribute(st) => st.span.clone(),
LocalAssignment(st) => st.span.clone(),
Property(st) => st.span.clone(),
Expression(st) => st.span.clone(),
InnerDocComment(st) => st.span.clone(),
Error(span) => span.clone(),
}
}
pub fn ends_with_semicolon(&self) -> bool {
match self {
Statement::Workbench(_) => false,
Statement::InlineModule(_) => false,
Statement::Function(_) => false,
Statement::InnerAttribute(_) => false,
Statement::InnerDocComment(_) => false,
Statement::Init(_) => false,
Statement::Error(_) => false,
Statement::Use(_) => true,
Statement::Const(_) => true,
Statement::Return(_) => true,
Statement::FileModule(_) => true,
Statement::LocalAssignment(_) => true,
Statement::Property(_) => true,
Statement::Expression(e) => !matches!(
&e.expression,
ast::Expression::Body(_) | ast::Expression::If(_)
),
}
}
}
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum WorkbenchKind {
Sketch,
Part,
Op,
}
impl std::fmt::Display for WorkbenchKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match &self {
WorkbenchKind::Sketch => "sketch",
WorkbenchKind::Part => "part",
WorkbenchKind::Op => "op",
}
)
}
}
#[derive(Debug, PartialEq)]
#[allow(missing_docs)]
pub struct WorkbenchDefinition {
pub span: Span,
pub keyword_span: Span,
pub extras: ast::ItemExtras,
pub doc: DocBlock,
pub kind: WorkbenchKind,
pub attributes: Vec<Attribute>,
pub visibility: Option<Visibility>,
pub name: ast::Identifier,
pub plan: ParameterList,
pub body: ast::Body,
}
#[derive(Debug, PartialEq)]
#[allow(missing_docs)]
pub struct InlineModule {
pub span: Span,
pub keyword_span: Span,
pub extras: ast::ItemExtras,
pub doc: DocBlock,
pub attributes: Vec<Attribute>,
pub visibility: Option<Visibility>,
pub name: ast::Identifier,
pub body: ast::Body,
}
#[derive(Debug, PartialEq)]
#[allow(missing_docs)]
pub struct FileModule {
pub span: Span,
pub keyword_span: Span,
pub extras: ast::ItemExtras,
pub doc: DocBlock,
pub attributes: Vec<Attribute>,
pub visibility: Option<Visibility>,
pub name: ast::Identifier,
}
#[derive(Debug, PartialEq)]
#[allow(missing_docs)]
pub struct FunctionDefinition {
pub span: Span,
pub keyword_span: Span,
pub extras: ast::ItemExtras,
pub doc: DocBlock,
pub attributes: Vec<Attribute>,
pub visibility: Option<Visibility>,
pub name: ast::Identifier,
pub parameters: ParameterList,
pub return_type: Option<ast::Type>,
pub body: ast::Body,
}
#[derive(Debug, PartialEq)]
#[allow(missing_docs)]
pub struct InitDefinition {
pub span: Span,
pub keyword_span: Span,
pub extras: ast::ItemExtras,
pub doc: DocBlock,
pub attributes: Vec<Attribute>,
pub parameters: ParameterList,
pub body: ast::Body,
}
#[derive(Debug, PartialEq)]
#[allow(missing_docs)]
pub struct UseStatement {
pub span: Span,
pub attributes: Vec<Attribute>,
pub keyword_span: Span,
pub extras: ast::ItemExtras,
pub visibility: Option<Visibility>,
pub name: UseName,
pub use_as: Option<ast::Identifier>,
}
#[derive(Debug, PartialEq)]
#[allow(missing_docs)]
pub struct UseName {
pub span: Span,
pub extras: ast::ItemExtras,
pub parts: Vec<UseStatementPart>,
}
#[derive(Debug, PartialEq)]
#[allow(missing_docs)]
pub enum UseStatementPart {
Identifier(ast::Identifier),
Glob(Span),
Error(Span),
}
#[derive(Debug, PartialEq)]
#[allow(missing_docs)]
pub struct Return {
pub span: Span,
pub keyword_span: Span,
pub extras: ast::ItemExtras,
pub value: Option<ast::Expression>,
}
#[derive(Debug, PartialEq)]
#[allow(missing_docs)]
pub struct ParameterList {
pub span: Span,
pub extras: ast::ItemExtras,
pub parameters: Vec<Parameter>,
}
impl ast::Dummy for ParameterList {
fn dummy(span: Span) -> Self {
Self {
span,
extras: ast::ItemExtras::default(),
parameters: Vec::default(),
}
}
}
#[derive(Debug, PartialEq)]
#[allow(missing_docs)]
pub struct Parameter {
pub span: Span,
pub extras: ast::ItemExtras,
pub name: ast::Identifier,
pub ty: Option<ast::Type>,
pub default: Option<ast::Expression>,
}
#[derive(Debug, PartialEq)]
#[allow(missing_docs)]
pub struct Attribute {
pub span: Span,
pub is_inner: bool,
pub extras: ast::ItemExtras,
pub commands: Vec<AttributeCommand>,
}
#[derive(Debug, PartialEq)]
pub enum AttributeCommand {
Ident(ast::Identifier),
Assignment(LocalAssignment),
Call(ast::Call),
}
#[derive(Debug, PartialEq)]
#[allow(missing_docs)]
pub struct LocalAssignment {
pub span: Span,
pub extras: ast::ItemExtras,
pub attributes: Vec<Attribute>,
pub name: ast::Identifier,
pub ty: Option<ast::Type>,
pub value: Box<ast::Expression>,
}
#[derive(Debug, PartialEq)]
#[allow(missing_docs)]
pub struct ConstAssignment {
pub span: Span,
pub keyword_span: Span,
pub extras: ast::ItemExtras,
pub doc: DocBlock,
pub attributes: Vec<Attribute>,
pub visibility: Option<Visibility>,
pub name: ast::Identifier,
pub ty: Option<ast::Type>,
pub value: Box<ast::Expression>,
}
#[derive(Debug, PartialEq)]
#[allow(missing_docs)]
pub struct PropertyAssignment {
pub span: Span,
pub keyword_span: Span,
pub extras: ast::ItemExtras,
pub doc: DocBlock,
pub attributes: Vec<Attribute>,
pub name: ast::Identifier,
pub ty: Option<ast::Type>,
pub value: Box<ast::Expression>,
}
#[derive(Debug, Clone, PartialEq)]
#[allow(missing_docs)]
pub enum CommentInner {
SingleLine(String),
MultiLine(String),
}
#[derive(Debug, Clone, PartialEq)]
#[allow(missing_docs)]
pub struct Comment {
pub span: Span,
pub inner: CommentInner,
}
#[derive(Debug, PartialEq)]
#[allow(missing_docs)]
pub struct DocBlock {
pub span: Span,
pub lines: Vec<String>,
}
#[derive(Debug, PartialEq)]
pub enum Visibility {
Public,
}
#[derive(Debug, PartialEq)]
#[allow(missing_docs)]
pub struct ExpressionStatement {
pub span: Span,
pub extras: ast::ItemExtras,
pub attributes: Vec<Attribute>,
pub expression: ast::Expression,
}
#[derive(Debug, PartialEq)]
#[allow(missing_docs)]
pub struct StatementList {
pub span: Span,
pub extras: ast::ItemExtras,
pub statements: Vec<(Statement, ast::TrailingExtras)>,
pub tail: Option<Box<ExpressionStatement>>,
}
impl ast::Dummy for StatementList {
fn dummy(span: Span) -> Self {
Self {
span,
extras: ast::ItemExtras::default(),
statements: Vec::default(),
tail: None,
}
}
}