Move ast nodes to new ast/node module.

This commit is contained in:
Jesse Brault 2025-05-26 08:30:15 -05:00
parent cbf7921c95
commit 22deb90c3e
32 changed files with 2915 additions and 2742 deletions

View File

@ -1,6 +1,26 @@
use crate::ast::*; use crate::ast::node::call_expression::*;
use crate::ast::node::class::*;
use crate::ast::node::closure::*;
use crate::ast::node::compilation_unit::*;
use crate::ast::node::d_string::*;
use crate::ast::node::expression::*;
use crate::ast::node::function::*;
use crate::ast::node::generics::*;
use crate::ast::node::implements_list::*;
use crate::ast::node::interface::*;
use crate::ast::node::level::*;
use crate::ast::node::literal::*;
use crate::ast::node::module::*;
use crate::ast::node::names::*;
use crate::ast::node::object_access::*;
use crate::ast::node::operators::*;
use crate::ast::node::statement::*;
use crate::ast::node::tuple_arguments::*;
use crate::ast::node::type_use::*;
use crate::ast::node::use_statement::*;
use crate::parser::Rule; use crate::parser::Rule;
use pest::iterators::{Pair, Pairs}; use pest::iterators::{Pair, Pairs};
use std::range::Range;
use std::str::FromStr; use std::str::FromStr;
fn expect_and_use<T>( fn expect_and_use<T>(
@ -897,7 +917,7 @@ fn build_interface_operator_function_declaration(
} }
fn build_class_constructor(file_id: usize, class_constructor_pair: Pair<Rule>) -> ClassConstructor { fn build_class_constructor(file_id: usize, class_constructor_pair: Pair<Rule>) -> ClassConstructor {
ClassConstructor( ClassConstructor::new(
class_constructor_pair class_constructor_pair
.into_inner() .into_inner()
.map(|data_member_pair| { .map(|data_member_pair| {
@ -1188,7 +1208,7 @@ fn build_if_statement(file_id: usize, if_statement_pair: Pair<Rule>) -> IfStatem
fn build_if_else_statement(file_id: usize, if_else_statement_pair: Pair<Rule>) -> IfElseStatement { fn build_if_else_statement(file_id: usize, if_else_statement_pair: Pair<Rule>) -> IfElseStatement {
let mut if_statement = None; let mut if_statement = None;
let mut else_ifs = ElseIfs::default(); let mut else_ifs = vec![];
let mut else_block = None; let mut else_block = None;
for inner_pair in if_else_statement_pair.into_inner() { for inner_pair in if_else_statement_pair.into_inner() {
@ -1199,7 +1219,7 @@ fn build_if_else_statement(file_id: usize, if_else_statement_pair: Pair<Rule>) -
Rule::ElseIf => { Rule::ElseIf => {
let mut else_if_inner = inner_pair.into_inner(); let mut else_if_inner = inner_pair.into_inner();
else_if_inner.next().unwrap(); // else else_if_inner.next().unwrap(); // else
else_ifs.0.push(Box::new(expect_and_use( else_ifs.push(Box::new(expect_and_use(
file_id, file_id,
else_if_inner.next().unwrap(), else_if_inner.next().unwrap(),
Rule::IfStatement, Rule::IfStatement,
@ -1220,7 +1240,11 @@ fn build_if_else_statement(file_id: usize, if_else_statement_pair: Pair<Rule>) -
} }
} }
IfElseStatement::new(if_statement.unwrap(), Box::new(else_ifs), else_block) IfElseStatement::new(
if_statement.unwrap(),
Box::new(ElseIfs::new(else_ifs)),
else_block,
)
} }
fn build_while_statement(file_id: usize, while_statement_pair: Pair<Rule>) -> WhileStatement { fn build_while_statement(file_id: usize, while_statement_pair: Pair<Rule>) -> WhileStatement {
@ -1791,7 +1815,7 @@ fn build_double_quote_string(file_id: usize, pair: Pair<Rule>) -> Literal {
if parts.len() == 1 && parts[0].is_string() { if parts.len() == 1 && parts[0].is_string() {
Literal::String(parts.pop().unwrap().unwrap_string()) Literal::String(parts.pop().unwrap().unwrap_string())
} else { } else {
Literal::DString(Box::new(DString(parts))) Literal::DString(Box::new(DString::new(parts)))
} }
} }
@ -1801,7 +1825,7 @@ fn build_backtick_string(file_id: usize, pair: Pair<Rule>) -> Literal {
if parts.len() == 1 && parts[0].is_string() { if parts.len() == 1 && parts[0].is_string() {
Literal::String(parts.pop().unwrap().unwrap_string()) Literal::String(parts.pop().unwrap().unwrap_string())
} else { } else {
Literal::BacktickString(Box::new(DString(parts))) Literal::BacktickString(Box::new(DString::new(parts)))
} }
} }
@ -1837,6 +1861,7 @@ mod tests {
use crate::ast::pretty_print::PrettyPrint; use crate::ast::pretty_print::PrettyPrint;
use crate::parser::DeimosParser; use crate::parser::DeimosParser;
use indoc::indoc; use indoc::indoc;
use pest::Parser;
fn assert_builds(src: &str) { fn assert_builds(src: &str) {
let parse_result = DeimosParser::parse(Rule::CompilationUnit, src); let parse_result = DeimosParser::parse(Rule::CompilationUnit, src);

View File

@ -1,4 +1,22 @@
use crate::ast::*; use crate::ast::node::call_expression::*;
use crate::ast::node::class::*;
use crate::ast::node::closure::*;
use crate::ast::node::compilation_unit::*;
use crate::ast::node::d_string::*;
use crate::ast::node::expression::*;
use crate::ast::node::function::*;
use crate::ast::node::generics::*;
use crate::ast::node::implements_list::*;
use crate::ast::node::interface::*;
use crate::ast::node::level::*;
use crate::ast::node::literal::*;
use crate::ast::node::module::*;
use crate::ast::node::names::*;
use crate::ast::node::object_access::*;
use crate::ast::node::statement::*;
use crate::ast::node::tuple_arguments::*;
use crate::ast::node::type_use::*;
use crate::ast::node::use_statement::*;
pub enum ChildRef<'a> { pub enum ChildRef<'a> {
Identifier(&'a Identifier), Identifier(&'a Identifier),
@ -216,7 +234,7 @@ impl HasChildren for UseStatement {
impl HasChildren for ModuleLevelDeclaration { impl HasChildren for ModuleLevelDeclaration {
fn children(&self) -> Vec<ChildRef> { fn children(&self) -> Vec<ChildRef> {
use ModuleLevelDeclaration::*; use crate::ast::node::level::ModuleLevelDeclaration::*;
match self { match self {
Module(module_declaration) => module_declaration.children(), Module(module_declaration) => module_declaration.children(),
Interface(interface_declaration) => interface_declaration.children(), Interface(interface_declaration) => interface_declaration.children(),
@ -231,7 +249,7 @@ impl HasChildren for ModuleLevelDeclaration {
impl HasChildren for InterfaceLevelDeclaration { impl HasChildren for InterfaceLevelDeclaration {
fn children(&self) -> Vec<ChildRef> { fn children(&self) -> Vec<ChildRef> {
use InterfaceLevelDeclaration::*; use crate::ast::node::level::InterfaceLevelDeclaration::*;
match self { match self {
Module(module_declaration) => module_declaration.children(), Module(module_declaration) => module_declaration.children(),
Interface(interface_declaration) => interface_declaration.children(), Interface(interface_declaration) => interface_declaration.children(),
@ -246,7 +264,7 @@ impl HasChildren for InterfaceLevelDeclaration {
impl HasChildren for ClassLevelDeclaration { impl HasChildren for ClassLevelDeclaration {
fn children(&self) -> Vec<ChildRef> { fn children(&self) -> Vec<ChildRef> {
use ClassLevelDeclaration::*; use crate::ast::node::level::ClassLevelDeclaration::*;
match self { match self {
Module(module_declaration) => module_declaration.children(), Module(module_declaration) => module_declaration.children(),
Interface(interface_declaration) => interface_declaration.children(), Interface(interface_declaration) => interface_declaration.children(),
@ -436,7 +454,7 @@ impl HasChildren for BlockStatement {
impl HasChildren for Statement { impl HasChildren for Statement {
fn children(&self) -> Vec<ChildRef> { fn children(&self) -> Vec<ChildRef> {
use Statement::*; use crate::ast::node::statement::Statement::*;
match self { match self {
BlockStatement(block_statement) => block_statement.children(), BlockStatement(block_statement) => block_statement.children(),
VariableDeclarationStatement(variable_declaration_statement) => { VariableDeclarationStatement(variable_declaration_statement) => {
@ -549,7 +567,7 @@ impl HasChildren for ForStatement {
impl HasChildren for Expression { impl HasChildren for Expression {
fn children(&self) -> Vec<ChildRef> { fn children(&self) -> Vec<ChildRef> {
use Expression::*; use crate::ast::node::expression::Expression::*;
match self { match self {
Ternary(ternary) => ternary.children(), Ternary(ternary) => ternary.children(),
Binary(binary) => binary.children(), Binary(binary) => binary.children(),

File diff suppressed because it is too large Load Diff

View File

@ -1,88 +0,0 @@
use crate::ast::{FullyQualifiedName, Identifier};
use crate::name_analysis::symbol::Symbol;
use std::borrow::Cow;
use std::range::Range;
pub trait Named {
fn name(&self) -> Cow<'_, str>;
fn file_id(&self) -> usize;
fn range(&self) -> Range<usize>;
fn set_scope_id(&mut self, scope_id: usize);
fn scope_id(&self) -> Option<usize>;
fn set_saved_symbol(&mut self, symbol: Symbol);
fn saved_symbol(&self) -> Option<Symbol>;
}
impl Named for Identifier {
fn name(&self) -> Cow<'_, str> {
Cow::Borrowed(&self.name)
}
fn file_id(&self) -> usize {
self.file_id
}
fn range(&self) -> Range<usize> {
self.range
}
fn set_scope_id(&mut self, id: usize) {
self.scope_id = Some(id);
}
fn scope_id(&self) -> Option<usize> {
self.scope_id
}
fn set_saved_symbol(&mut self, saved_symbol: Symbol) {
self.saved_symbol = Some(saved_symbol);
}
fn saved_symbol(&self) -> Option<Symbol> {
self.saved_symbol.clone()
}
}
impl Named for FullyQualifiedName {
fn name(&self) -> Cow<'_, str> {
if self.identifiers.len() == 1 {
self.identifiers[0].name()
} else {
let mut acc = String::new();
for (i, identifier) in self.identifiers.iter().enumerate() {
acc += &identifier.name();
if i < self.identifiers.len() - 1 {
acc += "::";
}
}
Cow::Owned(acc)
}
}
fn file_id(&self) -> usize {
self.file_id
}
fn range(&self) -> Range<usize> {
self.range
}
fn set_scope_id(&mut self, id: usize) {
self.scope_id = Some(id);
}
fn scope_id(&self) -> Option<usize> {
self.scope_id
}
fn set_saved_symbol(&mut self, symbol: Symbol) {
self.saved_symbol = Some(symbol);
}
fn saved_symbol(&self) -> Option<Symbol> {
self.saved_symbol.clone()
}
}

View File

@ -0,0 +1,106 @@
use crate::ast::node::expression::Expression;
use crate::ast::node::generics::GenericArguments;
#[derive(Debug)]
pub struct CallExpression {
callee: Box<Expression>,
turbo_fish: Option<Box<TurboFish>>,
arguments: Box<CallArguments>,
}
impl CallExpression {
pub fn new(
callee: Box<Expression>,
turbo_fish: Option<Box<TurboFish>>,
arguments: Box<CallArguments>,
) -> Self {
Self {
callee,
turbo_fish,
arguments,
}
}
pub fn callee(&self) -> &Expression {
&self.callee
}
pub fn callee_mut(&mut self) -> &mut Expression {
&mut self.callee
}
pub fn turbo_fish(&self) -> Option<&TurboFish> {
if let Some(turbo_fish) = &self.turbo_fish {
Some(turbo_fish.as_ref())
} else {
None
}
}
pub fn turbo_fish_mut(&mut self) -> Option<&mut TurboFish> {
if let Some(turbo_fish) = &mut self.turbo_fish {
Some(turbo_fish.as_mut())
} else {
None
}
}
pub fn arguments(&self) -> &CallArguments {
&self.arguments
}
pub fn arguments_mut(&mut self) -> &mut CallArguments {
&mut self.arguments
}
}
#[derive(Debug)]
pub struct TurboFish(Box<GenericArguments>);
impl TurboFish {
pub fn new(generics: Box<GenericArguments>) -> Self {
Self(generics)
}
pub fn generics(&self) -> &GenericArguments {
&self.0
}
pub fn generics_mut(&mut self) -> &mut GenericArguments {
&mut self.0
}
}
#[derive(Debug)]
pub struct CallArguments(Vec<Box<CallArgument>>);
impl CallArguments {
pub fn new(arguments: Vec<Box<CallArgument>>) -> Self {
Self(arguments)
}
pub fn arguments(&self) -> Vec<&CallArgument> {
self.0.iter().map(Box::as_ref).collect()
}
pub fn arguments_mut(&mut self) -> Vec<&mut CallArgument> {
self.0.iter_mut().map(Box::as_mut).collect()
}
}
#[derive(Debug)]
pub struct CallArgument(Box<Expression>);
impl CallArgument {
pub fn new(expression: Box<Expression>) -> Self {
Self(expression)
}
pub fn expression(&self) -> &Expression {
&self.0
}
pub fn expression_mut(&mut self) -> &mut Expression {
&mut self.0
}
}

184
src/ast/node/class.rs Normal file
View File

@ -0,0 +1,184 @@
use crate::ast::node::generics::GenericParameters;
use crate::ast::node::implements_list::ImplementsList;
use crate::ast::node::level::ClassLevelDeclaration;
use crate::ast::node::names::Identifier;
use crate::ast::node::type_use::TypeUse;
#[derive(Debug)]
pub struct ClassDeclaration {
is_public: bool,
identifier: Box<Identifier>,
generics: Box<GenericParameters>,
class_constructor: Option<Box<ClassConstructor>>,
implements: Box<ImplementsList>,
declarations: Vec<Box<ClassLevelDeclaration>>,
}
impl ClassDeclaration {
pub fn new(
is_public: bool,
identifier: Box<Identifier>,
generics: Box<GenericParameters>,
class_constructor: Option<Box<ClassConstructor>>,
implements: Box<ImplementsList>,
declarations: Vec<Box<ClassLevelDeclaration>>,
) -> Self {
Self {
is_public,
identifier,
generics,
class_constructor,
implements,
declarations,
}
}
pub fn is_public(&self) -> bool {
self.is_public
}
pub fn identifier(&self) -> &Identifier {
&self.identifier
}
pub fn identifier_mut(&mut self) -> &mut Identifier {
&mut self.identifier
}
pub fn generics(&self) -> &GenericParameters {
&self.generics
}
pub fn generics_mut(&mut self) -> &mut GenericParameters {
&mut self.generics
}
pub fn class_constructor(&self) -> Option<&ClassConstructor> {
if let Some(class_constructor) = &self.class_constructor {
Some(class_constructor.as_ref())
} else {
None
}
}
pub fn class_constructor_mut(&mut self) -> Option<&mut ClassConstructor> {
if let Some(class_constructor) = &mut self.class_constructor {
Some(class_constructor.as_mut())
} else {
None
}
}
pub fn implements(&self) -> &ImplementsList {
&self.implements
}
pub fn implements_mut(&mut self) -> &mut ImplementsList {
&mut self.implements
}
pub fn declarations(&self) -> Vec<&ClassLevelDeclaration> {
self.declarations.iter().map(Box::as_ref).collect()
}
pub fn declarations_mut(&mut self) -> Vec<&mut ClassLevelDeclaration> {
self.declarations.iter_mut().map(Box::as_mut).collect()
}
}
#[derive(Debug)]
pub struct ClassConstructor(Vec<Box<ClassConstructorParameter>>);
impl ClassConstructor {
pub fn new(parameters: Vec<Box<ClassConstructorParameter>>) -> Self {
Self(parameters)
}
pub fn parameters(&self) -> Vec<&ClassConstructorParameter> {
self.0.iter().map(|p| p.as_ref()).collect()
}
pub fn parameters_mut(&mut self) -> Vec<&mut ClassConstructorParameter> {
self.0.iter_mut().map(|p| p.as_mut()).collect()
}
}
#[derive(Debug)]
pub enum ClassConstructorParameter {
Property(Box<PropertyDeclaration>),
Field(Box<FieldDeclaration>),
}
#[derive(Debug)]
pub struct PropertyDeclaration {
is_mutable: bool,
identifier: Box<Identifier>,
declared_type: Box<TypeUse>,
}
impl PropertyDeclaration {
pub fn new(is_mutable: bool, identifier: Box<Identifier>, declared_type: Box<TypeUse>) -> Self {
Self {
is_mutable,
identifier,
declared_type,
}
}
pub fn is_mutable(&self) -> bool {
self.is_mutable
}
pub fn identifier(&self) -> &Identifier {
&self.identifier
}
pub fn identifier_mut(&mut self) -> &mut Identifier {
&mut self.identifier
}
pub fn declared_type(&self) -> &TypeUse {
&self.declared_type
}
pub fn declared_type_mut(&mut self) -> &mut TypeUse {
&mut self.declared_type
}
}
#[derive(Debug)]
pub struct FieldDeclaration {
is_mutable: bool,
identifier: Box<Identifier>,
declared_type: Box<TypeUse>,
}
impl FieldDeclaration {
pub fn new(is_mutable: bool, identifier: Box<Identifier>, declared_type: Box<TypeUse>) -> Self {
Self {
is_mutable,
identifier,
declared_type,
}
}
pub fn is_mutable(&self) -> bool {
self.is_mutable
}
pub fn identifier(&self) -> &Identifier {
&self.identifier
}
pub fn identifier_mut(&mut self) -> &mut Identifier {
&mut self.identifier
}
pub fn declared_type(&self) -> &TypeUse {
&self.declared_type
}
pub fn declared_type_mut(&mut self) -> &mut TypeUse {
&mut self.declared_type
}
}

204
src/ast/node/closure.rs Normal file
View File

@ -0,0 +1,204 @@
use crate::ast::node::expression::Expression;
use crate::ast::node::names::Identifier;
use crate::ast::node::statement::Statement;
use crate::ast::node::type_use::TypeUse;
#[derive(Debug)]
pub struct Closure {
modifier: Option<ClosureModifier>,
is_move: bool,
captures: Box<ClosureCaptures>,
parameters: Box<ClosureParameters>,
statements: Vec<Box<Statement>>,
expression: Option<Box<Expression>>,
}
impl Closure {
pub fn new(
modifier: Option<ClosureModifier>,
is_move: bool,
captures: Box<ClosureCaptures>,
parameters: Box<ClosureParameters>,
statements: Vec<Box<Statement>>,
expression: Option<Box<Expression>>,
) -> Self {
Self {
modifier,
is_move,
captures,
parameters,
statements,
expression,
}
}
pub fn modifier(&self) -> Option<&ClosureModifier> {
self.modifier.as_ref()
}
pub fn is_move(&self) -> bool {
self.is_move
}
pub fn captures(&self) -> &ClosureCaptures {
&self.captures
}
pub fn captures_mut(&mut self) -> &mut ClosureCaptures {
&mut self.captures
}
pub fn parameters(&self) -> &ClosureParameters {
&self.parameters
}
pub fn parameters_mut(&mut self) -> &mut ClosureParameters {
&mut self.parameters
}
pub fn statements(&self) -> Vec<&Statement> {
self.statements.iter().map(Box::as_ref).collect()
}
pub fn statements_mut(&mut self) -> Vec<&mut Statement> {
self.statements.iter_mut().map(Box::as_mut).collect()
}
pub fn expression(&self) -> Option<&Expression> {
if let Some(expression) = &self.expression {
Some(expression)
} else {
None
}
}
pub fn expression_mut(&mut self) -> Option<&mut Expression> {
if let Some(expression) = &mut self.expression {
Some(expression)
} else {
None
}
}
}
#[derive(Debug)]
pub enum ClosureModifier {
Cons,
Mut,
}
#[derive(Debug, Default)]
pub struct ClosureCaptures(Vec<Box<ClosureCapture>>);
impl ClosureCaptures {
pub fn new(captures: Vec<Box<ClosureCapture>>) -> Self {
Self(captures)
}
pub fn captures(&self) -> Vec<&ClosureCapture> {
self.0.iter().map(Box::as_ref).collect()
}
pub fn captures_mut(&mut self) -> Vec<&mut ClosureCapture> {
self.0.iter_mut().map(Box::as_mut).collect()
}
}
impl ClosureCaptures {
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
#[derive(Debug)]
pub struct ClosureCapture {
borrow_count: usize,
is_mutable: bool,
identifier: Box<Identifier>,
}
impl ClosureCapture {
pub fn new(borrow_count: usize, is_mutable: bool, identifier: Box<Identifier>) -> Self {
Self {
borrow_count,
is_mutable,
identifier,
}
}
pub fn borrow_count(&self) -> usize {
self.borrow_count
}
pub fn is_mutable(&self) -> bool {
self.is_mutable
}
pub fn identifier(&self) -> &Identifier {
&self.identifier
}
pub fn identifier_mut(&mut self) -> &mut Identifier {
&mut self.identifier
}
}
#[derive(Debug, Default)]
pub struct ClosureParameters(Vec<Box<ClosureParameter>>);
impl ClosureParameters {
pub fn new(parameters: Vec<Box<ClosureParameter>>) -> Self {
Self(parameters)
}
pub fn parameters(&self) -> Vec<&ClosureParameter> {
self.0.iter().map(Box::as_ref).collect()
}
pub fn parameters_mut(&mut self) -> Vec<&mut ClosureParameter> {
self.0.iter_mut().map(Box::as_mut).collect()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
#[derive(Debug)]
pub struct ClosureParameter {
identifier: Box<Identifier>,
type_use: Option<Box<TypeUse>>,
}
impl ClosureParameter {
pub fn new(identifier: Box<Identifier>, type_use: Option<Box<TypeUse>>) -> Self {
Self {
identifier,
type_use,
}
}
pub fn identifier(&self) -> &Identifier {
&self.identifier
}
pub fn identifier_mut(&mut self) -> &mut Identifier {
&mut self.identifier
}
pub fn type_use(&self) -> Option<&TypeUse> {
if let Some(type_use) = &self.type_use {
Some(type_use)
} else {
None
}
}
pub fn type_use_mut(&mut self) -> Option<&mut TypeUse> {
if let Some(type_use) = &mut self.type_use {
Some(type_use.as_mut())
} else {
None
}
}
}

View File

@ -0,0 +1,63 @@
use crate::ast::node::level::ModuleLevelDeclaration;
use crate::ast::node::names::FullyQualifiedName;
use crate::ast::node::use_statement::UseStatement;
#[derive(Debug)]
pub struct CompilationUnit {
file_name: String,
file_id: usize,
namespace: Option<Box<FullyQualifiedName>>,
use_statements: Vec<Box<UseStatement>>,
declarations: Vec<Box<ModuleLevelDeclaration>>,
}
impl CompilationUnit {
pub fn new(
file_name: String,
file_id: usize,
namespace: Option<Box<FullyQualifiedName>>,
use_statements: Vec<Box<UseStatement>>,
declarations: Vec<Box<ModuleLevelDeclaration>>,
) -> Self {
Self {
file_name,
file_id,
namespace,
use_statements,
declarations,
}
}
pub fn file_name(&self) -> &str {
&self.file_name
}
pub fn file_id(&self) -> usize {
self.file_id
}
pub fn namespace(&self) -> Option<&FullyQualifiedName> {
// We have to use this non-map syntax because we need a reference like `&self.namespace`
if let Some(namespace) = &self.namespace {
Some(namespace.as_ref())
} else {
None
}
}
pub fn use_statements(&self) -> Vec<&UseStatement> {
self.use_statements.iter().map(Box::as_ref).collect()
}
pub fn use_statements_mut(&mut self) -> Vec<&mut UseStatement> {
self.use_statements.iter_mut().map(Box::as_mut).collect()
}
pub fn declarations(&self) -> Vec<&ModuleLevelDeclaration> {
self.declarations.iter().map(Box::as_ref).collect()
}
pub fn declarations_mut(&mut self) -> Vec<&mut ModuleLevelDeclaration> {
self.declarations.iter_mut().map(Box::as_mut).collect()
}
}

55
src/ast/node/d_string.rs Normal file
View File

@ -0,0 +1,55 @@
use crate::ast::node::expression::Expression;
#[derive(Debug)]
pub struct DString(Vec<Box<DStringPart>>);
impl DString {
pub fn new(parts: Vec<Box<DStringPart>>) -> Self {
Self(parts)
}
pub fn parts(&self) -> Vec<&DStringPart> {
self.0.iter().map(Box::as_ref).collect()
}
pub fn parts_mut(&mut self) -> Vec<&mut DStringPart> {
self.0.iter_mut().map(Box::as_mut).collect()
}
}
#[derive(Debug)]
pub enum DStringPart {
String(String),
Expression(Box<Expression>),
}
impl DStringPart {
pub fn from_string(s: &str) -> DStringPart {
DStringPart::String(String::from(s))
}
pub fn from_expression(e: Expression) -> DStringPart {
DStringPart::Expression(Box::new(e))
}
pub fn is_string(&self) -> bool {
match self {
DStringPart::String(_) => true,
_ => false,
}
}
pub fn unwrap_string(self) -> String {
match self {
DStringPart::String(s) => s,
_ => panic!(),
}
}
pub fn is_expression(&self) -> bool {
match self {
DStringPart::Expression(_) => true,
_ => false,
}
}
}

155
src/ast/node/expression.rs Normal file
View File

@ -0,0 +1,155 @@
use crate::ast::node::call_expression::CallExpression;
use crate::ast::node::closure::Closure;
use crate::ast::node::literal::Literal;
use crate::ast::node::names::FullyQualifiedName;
use crate::ast::node::object_access::ObjectAccess;
use crate::ast::node::operators::{BinaryOperator, PrefixUnaryOperator, SuffixUnaryOperator};
#[derive(Debug)]
pub enum Expression {
Ternary(Box<TernaryExpression>),
Binary(Box<BinaryExpression>),
UnaryPrefix(Box<PrefixExpression>),
UnarySuffix(Box<SuffixExpression>),
Call(Box<CallExpression>),
ObjectAccess(Box<ObjectAccess>),
Literal(Box<Literal>),
FullyQualifiedName(Box<FullyQualifiedName>),
Closure(Box<Closure>),
}
#[derive(Debug)]
pub struct TernaryExpression {
condition: Box<Expression>,
true_expression: Box<Expression>,
false_expression: Box<Expression>,
}
impl TernaryExpression {
pub fn new(
condition: Box<Expression>,
true_expression: Box<Expression>,
false_expression: Box<Expression>,
) -> Self {
Self {
condition,
true_expression,
false_expression,
}
}
pub fn condition(&self) -> &Expression {
&self.condition
}
pub fn condition_mut(&mut self) -> &mut Expression {
&mut self.condition
}
pub fn true_expression(&self) -> &Expression {
&self.true_expression
}
pub fn true_expression_mut(&mut self) -> &mut Expression {
&mut self.true_expression
}
pub fn false_expression(&self) -> &Expression {
&self.false_expression
}
pub fn false_expression_mut(&mut self) -> &mut Expression {
&mut self.false_expression
}
}
#[derive(Debug)]
pub struct BinaryExpression {
left: Box<Expression>,
operator: BinaryOperator,
right: Box<Expression>,
}
impl BinaryExpression {
pub fn new(left: Box<Expression>, operator: BinaryOperator, right: Box<Expression>) -> Self {
Self {
left,
operator,
right,
}
}
pub fn left(&self) -> &Expression {
&self.left
}
pub fn left_mut(&mut self) -> &mut Expression {
&mut self.left
}
pub fn operator(&self) -> &BinaryOperator {
&self.operator
}
pub fn right(&self) -> &Expression {
&self.right
}
pub fn right_mut(&mut self) -> &mut Expression {
&mut self.right
}
}
#[derive(Debug)]
pub struct PrefixExpression {
operator: PrefixUnaryOperator,
expression: Box<Expression>,
}
impl PrefixExpression {
pub fn new(operator: PrefixUnaryOperator, expression: Box<Expression>) -> Self {
Self {
operator,
expression,
}
}
pub fn operator(&self) -> &PrefixUnaryOperator {
&self.operator
}
pub fn expression(&self) -> &Expression {
&self.expression
}
pub fn expression_mut(&mut self) -> &mut Expression {
&mut self.expression
}
}
#[derive(Debug)]
pub struct SuffixExpression {
expression: Box<Expression>,
operator: SuffixUnaryOperator,
}
impl SuffixExpression {
pub fn new(expression: Box<Expression>, operator: SuffixUnaryOperator) -> Self {
Self {
expression,
operator,
}
}
pub fn expression(&self) -> &Expression {
&self.expression
}
pub fn expression_mut(&mut self) -> &mut Expression {
&mut self.expression
}
pub fn operator(&self) -> &SuffixUnaryOperator {
&self.operator
}
}

543
src/ast/node/function.rs Normal file
View File

@ -0,0 +1,543 @@
use crate::ast::node::expression::Expression;
use crate::ast::node::generics::GenericParameters;
use crate::ast::node::names::Identifier;
use crate::ast::node::operators::Operator;
use crate::ast::node::statement::BlockStatement;
use crate::ast::node::type_use::{PrimitiveTypeUse, TypeUse};
#[derive(Debug)]
pub enum FunctionTypeModifier {
Cons,
MutRef,
Mut,
Ref,
}
#[derive(Debug)]
pub struct Parameters(Vec<Box<Parameter>>);
impl Parameters {
pub fn new(parameters: Vec<Box<Parameter>>) -> Self {
Self(parameters)
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn parameters(&self) -> Vec<&Parameter> {
self.0.iter().map(Box::as_ref).collect()
}
pub fn parameters_mut(&mut self) -> Vec<&mut Parameter> {
self.0.iter_mut().map(Box::as_mut).collect()
}
}
impl Default for Parameters {
fn default() -> Self {
Parameters(Vec::new())
}
}
#[derive(Debug)]
pub struct Parameter {
identifier: Box<Identifier>,
type_use: Box<TypeUse>,
}
impl Parameter {
pub fn new(identifier: Box<Identifier>, type_use: Box<TypeUse>) -> Self {
Self {
identifier,
type_use,
}
}
pub fn identifier(&self) -> &Identifier {
&self.identifier
}
pub fn identifier_mut(&mut self) -> &mut Identifier {
&mut self.identifier
}
pub fn type_use(&self) -> &TypeUse {
&self.type_use
}
pub fn type_use_mut(&mut self) -> &mut TypeUse {
&mut self.type_use
}
}
#[derive(Debug)]
pub struct ReturnType {
declared_type: Box<TypeUse>,
references: Box<References>,
}
impl ReturnType {
pub fn new(declared_type: Box<TypeUse>, references: Box<References>) -> Self {
Self {
declared_type,
references,
}
}
pub fn void() -> Self {
ReturnType {
declared_type: Box::new(TypeUse::Primitive(Box::new(PrimitiveTypeUse::Void))),
references: Box::new(References::default()),
}
}
pub fn declared_type(&self) -> &TypeUse {
&self.declared_type
}
pub fn declared_type_mut(&mut self) -> &mut TypeUse {
&mut self.declared_type
}
pub fn references(&self) -> &References {
&self.references
}
pub fn references_mut(&mut self) -> &mut References {
&mut self.references
}
}
#[derive(Debug)]
pub struct References(Vec<Box<Identifier>>);
impl References {
pub fn new(identifiers: Vec<Box<Identifier>>) -> Self {
Self(identifiers)
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn identifiers(&self) -> Vec<&Identifier> {
self.0.iter().map(Box::as_ref).collect()
}
pub fn identifiers_mut(&mut self) -> Vec<&mut Identifier> {
self.0.iter_mut().map(Box::as_mut).collect()
}
}
impl Default for References {
fn default() -> Self {
References(Vec::new())
}
}
#[derive(Debug)]
pub struct FunctionDefinition {
is_public: bool,
modifier: Option<FunctionModifier>,
generics: Box<GenericParameters>,
identifier: Box<Identifier>,
parameters: Box<Parameters>,
return_type: Box<ReturnType>,
body: Box<FunctionBody>,
}
impl FunctionDefinition {
pub fn new(
is_public: bool,
modifier: Option<FunctionModifier>,
generics: Box<GenericParameters>,
identifier: Box<Identifier>,
parameters: Box<Parameters>,
return_type: Box<ReturnType>,
body: Box<FunctionBody>,
) -> Self {
Self {
is_public,
modifier,
generics,
identifier,
parameters,
return_type,
body,
}
}
pub fn is_public(&self) -> bool {
self.is_public
}
pub fn modifier(&self) -> Option<&FunctionModifier> {
self.modifier.as_ref()
}
pub fn generics(&self) -> &GenericParameters {
&self.generics
}
pub fn generics_mut(&mut self) -> &mut GenericParameters {
&mut self.generics
}
pub fn identifier(&self) -> &Identifier {
&self.identifier
}
pub fn identifier_mut(&mut self) -> &mut Identifier {
&mut self.identifier
}
pub fn parameters(&self) -> &Parameters {
&self.parameters
}
pub fn parameters_mut(&mut self) -> &mut Parameters {
&mut self.parameters
}
pub fn return_type(&self) -> &ReturnType {
&self.return_type
}
pub fn return_type_mut(&mut self) -> &mut ReturnType {
&mut self.return_type
}
pub fn body(&self) -> &FunctionBody {
&self.body
}
pub fn body_mut(&mut self) -> &mut FunctionBody {
&mut self.body
}
}
#[derive(Debug)]
pub struct OperatorFunctionDefinition {
is_public: bool,
modifier: Option<FunctionModifier>,
generics: Box<GenericParameters>,
operator: Operator,
parameters: Box<Parameters>,
return_type: Box<ReturnType>,
body: Box<FunctionBody>,
}
impl OperatorFunctionDefinition {
pub fn new(
is_public: bool,
modifier: Option<FunctionModifier>,
generics: Box<GenericParameters>,
operator: Operator,
parameters: Box<Parameters>,
return_type: Box<ReturnType>,
body: Box<FunctionBody>,
) -> Self {
Self {
is_public,
modifier,
generics,
operator,
parameters,
return_type,
body,
}
}
pub fn is_public(&self) -> bool {
self.is_public
}
pub fn modifier(&self) -> Option<&FunctionModifier> {
self.modifier.as_ref()
}
pub fn generics(&self) -> &GenericParameters {
&self.generics
}
pub fn generics_mut(&mut self) -> &mut GenericParameters {
&mut self.generics
}
pub fn operator(&self) -> &Operator {
&self.operator
}
pub fn parameters(&self) -> &Parameters {
&self.parameters
}
pub fn parameters_mut(&mut self) -> &mut Parameters {
&mut self.parameters
}
pub fn return_type(&self) -> &ReturnType {
&self.return_type
}
pub fn return_type_mut(&mut self) -> &mut ReturnType {
&mut self.return_type
}
pub fn body(&self) -> &FunctionBody {
&self.body
}
pub fn body_mut(&mut self) -> &mut FunctionBody {
&mut self.body
}
}
#[derive(Debug)]
pub struct PlatformFunctionDeclaration {
is_public: bool,
modifier: Option<FunctionModifier>,
generics: Box<GenericParameters>,
identifier: Box<Identifier>,
parameters: Box<Parameters>,
return_type: Box<ReturnType>,
}
impl PlatformFunctionDeclaration {
pub fn new(
is_public: bool,
modifier: Option<FunctionModifier>,
generics: Box<GenericParameters>,
identifier: Box<Identifier>,
parameters: Box<Parameters>,
return_type: Box<ReturnType>,
) -> Self {
Self {
is_public,
modifier,
generics,
identifier,
parameters,
return_type,
}
}
pub fn is_public(&self) -> bool {
self.is_public
}
pub fn modifier(&self) -> Option<&FunctionModifier> {
self.modifier.as_ref()
}
pub fn generics(&self) -> &GenericParameters {
&self.generics
}
pub fn generics_mut(&mut self) -> &mut GenericParameters {
&mut self.generics
}
pub fn identifier(&self) -> &Identifier {
&self.identifier
}
pub fn identifier_mut(&mut self) -> &mut Identifier {
&mut self.identifier
}
pub fn parameters(&self) -> &Parameters {
&self.parameters
}
pub fn parameters_mut(&mut self) -> &mut Parameters {
&mut self.parameters
}
pub fn return_type(&self) -> &ReturnType {
&self.return_type
}
pub fn return_type_mut(&mut self) -> &mut ReturnType {
&mut self.return_type
}
}
#[derive(Debug)]
pub struct InterfaceFunctionDeclaration {
modifier: Option<FunctionModifier>,
generics: Box<GenericParameters>,
identifier: Box<Identifier>,
parameters: Box<Parameters>,
return_type: Box<ReturnType>,
body: Option<Box<FunctionBody>>,
}
impl InterfaceFunctionDeclaration {
pub fn new(
modifier: Option<FunctionModifier>,
generics: Box<GenericParameters>,
identifier: Box<Identifier>,
parameters: Box<Parameters>,
return_type: Box<ReturnType>,
body: Option<Box<FunctionBody>>,
) -> Self {
Self {
modifier,
generics,
identifier,
parameters,
return_type,
body,
}
}
pub fn modifier(&self) -> Option<&FunctionModifier> {
self.modifier.as_ref()
}
pub fn generics(&self) -> &GenericParameters {
&self.generics
}
pub fn generics_mut(&mut self) -> &mut GenericParameters {
&mut self.generics
}
pub fn identifier(&self) -> &Identifier {
&self.identifier
}
pub fn identifier_mut(&mut self) -> &mut Identifier {
&mut self.identifier
}
pub fn parameters(&self) -> &Parameters {
&self.parameters
}
pub fn parameters_mut(&mut self) -> &mut Parameters {
&mut self.parameters
}
pub fn return_type(&self) -> &ReturnType {
&self.return_type
}
pub fn return_type_mut(&mut self) -> &mut ReturnType {
&mut self.return_type
}
pub fn body(&self) -> Option<&FunctionBody> {
FunctionBody::unwrap_option(&self.body)
}
pub fn body_mut(&mut self) -> Option<&mut FunctionBody> {
FunctionBody::unwrap_option_mut(&mut self.body)
}
}
#[derive(Debug)]
pub struct InterfaceOperatorFunctionDeclaration {
modifier: Option<FunctionModifier>,
generics: Box<GenericParameters>,
operator: Operator,
parameters: Box<Parameters>,
return_type: Box<ReturnType>,
body: Option<Box<FunctionBody>>,
}
impl InterfaceOperatorFunctionDeclaration {
pub fn new(
modifier: Option<FunctionModifier>,
generics: Box<GenericParameters>,
operator: Operator,
parameters: Box<Parameters>,
return_type: Box<ReturnType>,
body: Option<Box<FunctionBody>>,
) -> Self {
Self {
modifier,
generics,
operator,
parameters,
return_type,
body,
}
}
pub fn modifier(&self) -> Option<&FunctionModifier> {
self.modifier.as_ref()
}
pub fn generics(&self) -> &GenericParameters {
&self.generics
}
pub fn generics_mut(&mut self) -> &mut GenericParameters {
&mut self.generics
}
pub fn operator(&self) -> &Operator {
&self.operator
}
pub fn parameters(&self) -> &Box<Parameters> {
&self.parameters
}
pub fn parameters_mut(&mut self) -> &mut Box<Parameters> {
&mut self.parameters
}
pub fn return_type(&self) -> &ReturnType {
&self.return_type
}
pub fn return_type_mut(&mut self) -> &mut ReturnType {
&mut self.return_type
}
pub fn body(&self) -> Option<&FunctionBody> {
FunctionBody::unwrap_option(&self.body)
}
pub fn body_mut(&mut self) -> Option<&mut FunctionBody> {
FunctionBody::unwrap_option_mut(&mut self.body)
}
}
#[derive(Debug)]
pub enum FunctionModifier {
Static,
Cons,
Mut,
Ref,
MutRef,
}
#[derive(Debug)]
pub enum FunctionBody {
Equals(Box<Expression>),
Block(Box<BlockStatement>),
Alias(Box<Identifier>),
}
impl FunctionBody {
fn unwrap_option(opt: &Option<Box<FunctionBody>>) -> Option<&FunctionBody> {
if let Some(body) = opt {
Some(body.as_ref())
} else {
None
}
}
fn unwrap_option_mut(opt: &mut Option<Box<FunctionBody>>) -> Option<&mut FunctionBody> {
if let Some(body) = opt {
Some(body.as_mut())
} else {
None
}
}
}

56
src/ast/node/generics.rs Normal file
View File

@ -0,0 +1,56 @@
use crate::ast::node::names::Identifier;
use crate::ast::node::type_use::TypeUse;
#[derive(Debug)]
pub struct GenericArguments(Vec<Box<TypeUse>>);
impl GenericArguments {
pub fn new(arguments: Vec<Box<TypeUse>>) -> Self {
Self(arguments)
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn arguments(&self) -> Vec<&TypeUse> {
self.0.iter().map(Box::as_ref).collect()
}
pub fn arguments_mut(&mut self) -> Vec<&mut TypeUse> {
self.0.iter_mut().map(Box::as_mut).collect()
}
}
impl Default for GenericArguments {
fn default() -> Self {
GenericArguments(Vec::new())
}
}
#[derive(Debug)]
pub struct GenericParameters(Vec<Box<Identifier>>);
impl GenericParameters {
pub fn new(identifiers: Vec<Box<Identifier>>) -> Self {
Self(identifiers)
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn identifiers(&self) -> Vec<&Identifier> {
self.0.iter().map(Box::as_ref).collect()
}
pub fn identifiers_mut(&mut self) -> Vec<&mut Identifier> {
self.0.iter_mut().map(Box::as_mut).collect()
}
}
impl Default for GenericParameters {
fn default() -> Self {
GenericParameters(Vec::new())
}
}

View File

@ -0,0 +1,28 @@
use crate::ast::node::type_use::TypeUse;
#[derive(Debug)]
pub struct ImplementsList(Vec<Box<TypeUse>>);
impl ImplementsList {
pub fn new(type_uses: Vec<Box<TypeUse>>) -> Self {
Self(type_uses)
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn type_uses(&self) -> Vec<&TypeUse> {
self.0.iter().map(Box::as_ref).collect()
}
pub fn type_uses_mut(&mut self) -> Vec<&mut TypeUse> {
self.0.iter_mut().map(Box::as_mut).collect()
}
}
impl Default for ImplementsList {
fn default() -> Self {
ImplementsList(Vec::new())
}
}

67
src/ast/node/interface.rs Normal file
View File

@ -0,0 +1,67 @@
use crate::ast::node::generics::GenericParameters;
use crate::ast::node::implements_list::ImplementsList;
use crate::ast::node::level::InterfaceLevelDeclaration;
use crate::ast::node::names::Identifier;
#[derive(Debug)]
pub struct InterfaceDeclaration {
is_public: bool,
identifier: Box<Identifier>,
generics: Box<GenericParameters>,
implements: Box<ImplementsList>,
declarations: Vec<Box<InterfaceLevelDeclaration>>,
}
impl InterfaceDeclaration {
pub fn new(
is_public: bool,
identifier: Box<Identifier>,
generics: Box<GenericParameters>,
implements: Box<ImplementsList>,
declarations: Vec<Box<InterfaceLevelDeclaration>>,
) -> Self {
Self {
is_public,
identifier,
generics,
implements,
declarations,
}
}
pub fn is_public(&self) -> bool {
self.is_public
}
pub fn identifier(&self) -> &Identifier {
&self.identifier
}
pub fn identifier_mut(&mut self) -> &mut Identifier {
&mut self.identifier
}
pub fn generics(&self) -> &GenericParameters {
&self.generics
}
pub fn generics_mut(&mut self) -> &mut GenericParameters {
&mut self.generics
}
pub fn implements(&self) -> &ImplementsList {
&self.implements
}
pub fn implements_mut(&mut self) -> &mut ImplementsList {
&mut self.implements
}
pub fn declarations(&self) -> Vec<&InterfaceLevelDeclaration> {
self.declarations.iter().map(Box::as_ref).collect()
}
pub fn declarations_mut(&mut self) -> Vec<&mut InterfaceLevelDeclaration> {
self.declarations.iter_mut().map(Box::as_mut).collect()
}
}

37
src/ast/node/level.rs Normal file
View File

@ -0,0 +1,37 @@
use crate::ast::node::class::{ClassDeclaration, FieldDeclaration, PropertyDeclaration};
use crate::ast::node::function::{
FunctionDefinition, InterfaceFunctionDeclaration, InterfaceOperatorFunctionDeclaration,
OperatorFunctionDefinition, PlatformFunctionDeclaration,
};
use crate::ast::node::interface::InterfaceDeclaration;
use crate::ast::node::module::ModuleDeclaration;
#[derive(Debug)]
pub enum ModuleLevelDeclaration {
Module(Box<ModuleDeclaration>),
Interface(Box<InterfaceDeclaration>),
Class(Box<ClassDeclaration>),
Function(Box<FunctionDefinition>),
PlatformFunction(Box<PlatformFunctionDeclaration>),
}
#[derive(Debug)]
pub enum InterfaceLevelDeclaration {
Module(Box<ModuleDeclaration>),
Interface(Box<InterfaceDeclaration>),
Class(Box<ClassDeclaration>),
Function(Box<InterfaceFunctionDeclaration>),
OperatorFunction(Box<InterfaceOperatorFunctionDeclaration>),
}
#[derive(Debug)]
pub enum ClassLevelDeclaration {
Module(Box<ModuleDeclaration>),
Interface(Box<InterfaceDeclaration>),
Class(Box<ClassDeclaration>),
Function(Box<FunctionDefinition>),
OperatorFunction(Box<OperatorFunctionDefinition>),
PlatformFunction(Box<PlatformFunctionDeclaration>),
Property(Box<PropertyDeclaration>),
Field(Box<FieldDeclaration>),
}

13
src/ast/node/literal.rs Normal file
View File

@ -0,0 +1,13 @@
use crate::ast::node::d_string::DString;
#[derive(Debug)]
pub enum Literal {
Integer(i32),
Long(i64),
Double(f64),
USize(usize),
String(String),
DString(Box<DString>),
BacktickString(Box<DString>),
Boolean(bool),
}

21
src/ast/node/mod.rs Normal file
View File

@ -0,0 +1,21 @@
pub mod call_expression;
pub mod class;
pub mod closure;
pub mod compilation_unit;
pub mod d_string;
pub mod expression;
pub mod function;
pub mod generics;
pub mod implements_list;
pub mod interface;
pub mod level;
pub mod literal;
pub mod module;
pub mod named;
pub mod names;
pub mod object_access;
pub mod operators;
pub mod statement;
pub mod tuple_arguments;
pub mod type_use;
pub mod use_statement;

43
src/ast/node/module.rs Normal file
View File

@ -0,0 +1,43 @@
use crate::ast::node::level::ModuleLevelDeclaration;
use crate::ast::node::names::Identifier;
#[derive(Debug)]
pub struct ModuleDeclaration {
is_public: bool,
identifier: Box<Identifier>,
declarations: Vec<Box<ModuleLevelDeclaration>>,
}
impl ModuleDeclaration {
pub fn new(
is_public: bool,
identifier: Box<Identifier>,
declarations: Vec<Box<ModuleLevelDeclaration>>,
) -> Self {
Self {
is_public,
identifier,
declarations,
}
}
pub fn is_public(&self) -> bool {
self.is_public
}
pub fn identifier(&self) -> &Identifier {
&self.identifier
}
pub fn identifier_mut(&mut self) -> &mut Identifier {
&mut self.identifier
}
pub fn declarations(&self) -> Vec<&ModuleLevelDeclaration> {
self.declarations.iter().map(Box::as_ref).collect()
}
pub fn declarations_mut(&mut self) -> Vec<&mut ModuleLevelDeclaration> {
self.declarations.iter_mut().map(Box::as_mut).collect()
}
}

16
src/ast/node/named.rs Normal file
View File

@ -0,0 +1,16 @@
use crate::name_analysis::symbol::Symbol;
use std::borrow::Cow;
use std::range::Range;
pub trait Named {
fn name(&self) -> Cow<'_, str>;
fn file_id(&self) -> usize;
fn range(&self) -> Range<usize>;
fn set_scope_id(&mut self, scope_id: usize);
fn scope_id(&self) -> Option<usize>;
fn set_saved_symbol(&mut self, symbol: Symbol);
fn saved_symbol(&self) -> Option<Symbol>;
}

146
src/ast/node/names.rs Normal file
View File

@ -0,0 +1,146 @@
use std::borrow::Cow;
use crate::name_analysis::symbol::Symbol;
use std::range::Range;
use crate::ast::node::named::Named;
#[derive(Debug)]
pub struct Identifier {
name: String,
file_id: usize,
range: Range<usize>,
scope_id: Option<usize>,
saved_symbol: Option<Symbol>,
}
impl Identifier {
pub fn new(name: &str, file_id: usize, range: Range<usize>) -> Self {
Self {
name: name.to_string(),
file_id,
range,
scope_id: None,
saved_symbol: None,
}
}
}
impl Named for Identifier {
fn name(&self) -> Cow<'_, str> {
Cow::Borrowed(&self.name)
}
fn file_id(&self) -> usize {
self.file_id
}
fn range(&self) -> Range<usize> {
self.range
}
fn set_scope_id(&mut self, id: usize) {
self.scope_id = Some(id);
}
fn scope_id(&self) -> Option<usize> {
self.scope_id
}
fn set_saved_symbol(&mut self, saved_symbol: Symbol) {
self.saved_symbol = Some(saved_symbol);
}
fn saved_symbol(&self) -> Option<Symbol> {
self.saved_symbol.clone()
}
}
#[derive(Debug)]
pub struct FullyQualifiedName {
identifiers: Vec<Box<Identifier>>,
file_id: usize,
range: Range<usize>,
scope_id: Option<usize>,
saved_symbol: Option<Symbol>,
}
impl FullyQualifiedName {
pub fn new(identifiers: Vec<Box<Identifier>>, file_id: usize, range: Range<usize>) -> Self {
Self {
identifiers,
range,
file_id,
scope_id: None,
saved_symbol: None,
}
}
pub fn identifiers(&self) -> Vec<&Identifier> {
self.identifiers.iter().map(Box::as_ref).collect()
}
pub fn identifiers_mut(&mut self) -> Vec<&mut Identifier> {
self.identifiers.iter_mut().map(Box::as_mut).collect()
}
#[deprecated(note = "Grammar will instead distinguish between fqns and identifiers")]
pub fn last(&self) -> &Identifier {
&self.identifiers[self.identifiers.len() - 1]
}
#[deprecated(note = "Grammar will instead distinguish between fqns and identifiers")]
pub fn last_mut(&mut self) -> &mut Identifier {
let last_index = self.identifiers.len() - 1;
&mut self.identifiers[last_index]
}
#[deprecated(note = "Grammar will instead distinguish between fqns and identifiers")]
pub fn len(&self) -> usize {
self.identifiers.len()
}
#[deprecated(note = "Grammar will instead distinguish between fqns and identifiers")]
pub fn is_single_identifier(&self) -> bool {
self.identifiers.len() == 1
}
}
impl Named for FullyQualifiedName {
fn name(&self) -> Cow<'_, str> {
if self.identifiers.len() == 1 {
self.identifiers[0].name()
} else {
let mut acc = String::new();
for (i, identifier) in self.identifiers.iter().enumerate() {
acc += &identifier.name();
if i < self.identifiers.len() - 1 {
acc += "::";
}
}
Cow::Owned(acc)
}
}
fn file_id(&self) -> usize {
self.file_id
}
fn range(&self) -> Range<usize> {
self.range
}
fn set_scope_id(&mut self, id: usize) {
self.scope_id = Some(id);
}
fn scope_id(&self) -> Option<usize> {
self.scope_id
}
fn set_saved_symbol(&mut self, symbol: Symbol) {
self.saved_symbol = Some(symbol);
}
fn saved_symbol(&self) -> Option<Symbol> {
self.saved_symbol.clone()
}
}

View File

@ -0,0 +1,56 @@
use crate::ast::node::expression::Expression;
use crate::ast::node::names::Identifier;
#[derive(Debug)]
pub struct ObjectAccess {
receiver: Box<Expression>,
navigations: Box<ObjectNavigations>,
}
impl ObjectAccess {
pub fn new(receiver: Box<Expression>, navigations: Box<ObjectNavigations>) -> Self {
Self {
receiver,
navigations,
}
}
pub fn receiver(&self) -> &Expression {
&self.receiver
}
pub fn receiver_mut(&mut self) -> &mut Expression {
&mut self.receiver
}
pub fn navigations(&self) -> &ObjectNavigations {
&self.navigations
}
pub fn navigations_mut(&mut self) -> &mut ObjectNavigations {
&mut self.navigations
}
}
#[derive(Debug)]
pub struct ObjectNavigations(Vec<Box<ObjectNavigation>>);
impl ObjectNavigations {
pub fn new(navigations: Vec<Box<ObjectNavigation>>) -> Self {
Self(navigations)
}
pub fn navigations(&self) -> Vec<&ObjectNavigation> {
self.0.iter().map(Box::as_ref).collect()
}
pub fn navigations_mut(&mut self) -> Vec<&mut ObjectNavigation> {
self.0.iter_mut().map(Box::as_mut).collect()
}
}
#[derive(Debug)]
pub enum ObjectNavigation {
Index(Box<Expression>),
Identifier(Box<Identifier>),
}

44
src/ast/node/operators.rs Normal file
View File

@ -0,0 +1,44 @@
#[derive(Debug)]
pub enum Operator {
Binary(BinaryOperator),
PrefixUnary(PrefixUnaryOperator),
SuffixUnary(SuffixUnaryOperator),
}
#[derive(Debug)]
pub enum BinaryOperator {
Or,
And,
EqualTo,
NotEqualTo,
Greater,
Less,
GreaterEqual,
LessEqual,
Add,
Subtract,
Multiply,
Divide,
Modulo,
LeftShift,
RightShift,
}
#[derive(Debug)]
pub enum PrefixUnaryOperator {
Spread,
BorrowMut,
Borrow,
Star,
Mut,
Not,
Negative,
}
#[derive(Debug)]
pub enum SuffixUnaryOperator {
PlusPlus,
MinusMinus,
Call,
Index,
}

384
src/ast/node/statement.rs Normal file
View File

@ -0,0 +1,384 @@
use crate::ast::node::expression::Expression;
use crate::ast::node::names::Identifier;
use crate::ast::node::type_use::TypeUse;
#[derive(Debug)]
pub struct BlockStatement {
statements: Vec<Box<Statement>>,
expression: Option<Box<Expression>>,
}
impl BlockStatement {
pub fn new(statements: Vec<Box<Statement>>, expression: Option<Box<Expression>>) -> Self {
Self {
statements,
expression,
}
}
pub fn statements(&self) -> Vec<&Statement> {
self.statements.iter().map(AsRef::as_ref).collect()
}
pub fn statements_mut(&mut self) -> Vec<&mut Statement> {
self.statements.iter_mut().map(AsMut::as_mut).collect()
}
pub fn expression(&self) -> Option<&Expression> {
if let Some(ref expression) = self.expression {
Some(expression.as_ref())
} else {
None
}
}
pub fn expression_mut(&mut self) -> Option<&mut Expression> {
if let Some(ref mut expression) = self.expression {
Some(expression.as_mut())
} else {
None
}
}
}
#[derive(Debug)]
pub enum Statement {
BlockStatement(Box<BlockStatement>),
VariableDeclarationStatement(Box<VariableDeclarationStatement>),
AssignStatement(Box<AssignStatement>),
CallStatement(Box<CallStatement>),
ReturnStatement(Box<ReturnStatement>),
IfStatement(Box<IfStatement>),
IfElseStatement(Box<IfElseStatement>),
WhileStatement(Box<WhileStatement>),
ForStatement(Box<ForStatement>),
}
#[derive(Debug)]
pub struct VariableDeclarationStatement {
is_mutable: bool,
identifier: Box<Identifier>,
declared_type: Option<Box<TypeUse>>,
initializer: Option<Box<Expression>>,
}
impl VariableDeclarationStatement {
pub fn new(
is_mutable: bool,
identifier: Box<Identifier>,
declared_type: Option<Box<TypeUse>>,
initializer: Option<Box<Expression>>,
) -> Self {
Self {
is_mutable,
identifier,
declared_type,
initializer,
}
}
pub fn is_mutable(&self) -> bool {
self.is_mutable
}
pub fn identifier(&self) -> &Identifier {
&self.identifier
}
pub fn identifier_mut(&mut self) -> &mut Identifier {
&mut self.identifier
}
pub fn declared_type(&self) -> Option<&TypeUse> {
if let Some(type_use) = &self.declared_type {
Some(type_use.as_ref())
} else {
None
}
}
pub fn declared_type_mut(&mut self) -> Option<&mut TypeUse> {
if let Some(type_use) = &mut self.declared_type {
Some(type_use.as_mut())
} else {
None
}
}
pub fn initializer(&self) -> Option<&Expression> {
if let Some(initializer) = &self.initializer {
Some(initializer.as_ref())
} else {
None
}
}
pub fn initializer_mut(&mut self) -> Option<&mut Expression> {
if let Some(initializer) = &mut self.initializer {
Some(initializer.as_mut())
} else {
None
}
}
}
#[derive(Debug)]
pub struct AssignStatement {
lhs: Box<Expression>,
rhs: Box<Expression>,
}
impl AssignStatement {
pub fn new(lhs: Box<Expression>, rhs: Box<Expression>) -> Self {
Self { lhs, rhs }
}
pub fn lhs(&self) -> &Expression {
&self.lhs
}
pub fn lhs_mut(&mut self) -> &mut Expression {
&mut self.lhs
}
pub fn rhs(&self) -> &Expression {
&self.rhs
}
pub fn rhs_mut(&mut self) -> &mut Expression {
&mut self.rhs
}
}
#[derive(Debug)]
pub struct CallStatement(Box<Expression>);
impl CallStatement {
pub fn new(call_expression: Box<Expression>) -> Self {
Self(call_expression)
}
pub fn expression(&self) -> &Expression {
&self.0
}
pub fn expression_mut(&mut self) -> &mut Expression {
&mut self.0
}
}
#[derive(Debug)]
pub struct ReturnStatement(Option<Box<Expression>>);
impl ReturnStatement {
pub fn new(expression: Option<Box<Expression>>) -> Self {
Self(expression)
}
pub fn expression(&self) -> Option<&Expression> {
if let Some(expression) = &self.0 {
Some(expression.as_ref())
} else {
None
}
}
pub fn expression_mut(&mut self) -> Option<&mut Expression> {
if let Some(expression) = &mut self.0 {
Some(expression.as_mut())
} else {
None
}
}
}
#[derive(Debug)]
pub struct IfStatement {
condition: Box<Expression>,
then_block: Box<BlockStatement>,
}
impl IfStatement {
pub fn new(condition: Box<Expression>, then_block: Box<BlockStatement>) -> Self {
Self {
condition,
then_block,
}
}
pub fn condition(&self) -> &Expression {
&self.condition
}
pub fn condition_mut(&mut self) -> &mut Expression {
&mut self.condition
}
pub fn then_block(&self) -> &BlockStatement {
&self.then_block
}
pub fn then_block_mut(&mut self) -> &mut BlockStatement {
&mut self.then_block
}
}
#[derive(Debug)]
pub struct IfElseStatement {
if_statement: Box<IfStatement>,
else_ifs: Box<ElseIfs>,
else_block: Option<Box<ElseBlock>>,
}
impl IfElseStatement {
pub fn new(
if_statement: Box<IfStatement>,
else_ifs: Box<ElseIfs>,
else_block: Option<Box<ElseBlock>>,
) -> Self {
Self {
if_statement,
else_ifs,
else_block,
}
}
pub fn if_statement(&self) -> &IfStatement {
&self.if_statement
}
pub fn if_statement_mut(&mut self) -> &mut IfStatement {
&mut self.if_statement
}
pub fn else_ifs(&self) -> &ElseIfs {
&self.else_ifs
}
pub fn else_ifs_mut(&mut self) -> &mut ElseIfs {
&mut self.else_ifs
}
pub fn else_block(&self) -> Option<&ElseBlock> {
if let Some(else_block) = &self.else_block {
Some(else_block.as_ref())
} else {
None
}
}
pub fn else_block_mut(&mut self) -> Option<&mut ElseBlock> {
if let Some(else_block) = &mut self.else_block {
Some(else_block.as_mut())
} else {
None
}
}
}
#[derive(Debug, Default)]
pub struct ElseIfs(Vec<Box<IfStatement>>);
impl ElseIfs {
pub fn new(if_statements: Vec<Box<IfStatement>>) -> Self {
Self(if_statements)
}
pub fn if_statements(&self) -> Vec<&IfStatement> {
self.0.iter().map(Box::as_ref).collect()
}
pub fn if_statements_mut(&mut self) -> Vec<&mut IfStatement> {
self.0.iter_mut().map(Box::as_mut).collect()
}
}
#[derive(Debug)]
pub struct ElseBlock(Box<BlockStatement>);
impl ElseBlock {
pub fn new(block: Box<BlockStatement>) -> Self {
Self(block)
}
pub fn block_statement(&self) -> &BlockStatement {
&self.0
}
pub fn block_statement_mut(&mut self) -> &mut BlockStatement {
&mut self.0
}
}
#[derive(Debug)]
pub struct WhileStatement {
condition: Box<Expression>,
body: Box<BlockStatement>,
}
impl WhileStatement {
pub fn new(condition: Box<Expression>, body: Box<BlockStatement>) -> Self {
Self { condition, body }
}
pub fn condition(&self) -> &Expression {
&self.condition
}
pub fn condition_mut(&mut self) -> &mut Expression {
&mut self.condition
}
pub fn body(&self) -> &BlockStatement {
&self.body
}
pub fn body_mut(&mut self) -> &mut BlockStatement {
&mut self.body
}
}
#[derive(Debug)]
pub struct ForStatement {
variable: Box<Identifier>,
iterator: Box<Expression>,
body: Box<BlockStatement>,
}
impl ForStatement {
pub fn new(
variable: Box<Identifier>,
iterator: Box<Expression>,
body: Box<BlockStatement>,
) -> Self {
Self {
variable,
iterator,
body,
}
}
pub fn variable(&self) -> &Identifier {
&self.variable
}
pub fn variable_mut(&mut self) -> &mut Identifier {
&mut self.variable
}
pub fn iterator(&self) -> &Expression {
&self.iterator
}
pub fn iterator_mut(&mut self) -> &mut Expression {
&mut self.iterator
}
pub fn body(&self) -> &BlockStatement {
&self.body
}
pub fn body_mut(&mut self) -> &mut BlockStatement {
&mut self.body
}
}

View File

@ -0,0 +1,22 @@
use crate::ast::node::type_use::TypeUse;
#[derive(Debug)]
pub struct TupleArguments(Vec<Box<TypeUse>>);
impl TupleArguments {
pub fn new(arguments: Vec<Box<TypeUse>>) -> Self {
Self(arguments)
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn type_uses(&self) -> Vec<&TypeUse> {
self.0.iter().map(Box::as_ref).collect()
}
pub fn type_uses_mut(&mut self) -> Vec<&mut TypeUse> {
self.0.iter_mut().map(Box::as_mut).collect()
}
}

167
src/ast/node/type_use.rs Normal file
View File

@ -0,0 +1,167 @@
use crate::ast::node::function::{FunctionTypeModifier, Parameters, ReturnType};
use crate::ast::node::generics::{GenericArguments, GenericParameters};
use crate::ast::node::names::FullyQualifiedName;
use crate::ast::node::tuple_arguments::TupleArguments;
#[derive(Debug)]
pub enum TypeUse {
Primitive(Box<PrimitiveTypeUse>),
InterfaceOrClass(Box<InterfaceOrClassTypeUse>),
Tuple(Box<TupleTypeUse>),
Function(Box<FunctionTypeUse>),
}
#[derive(Debug)]
pub enum PrimitiveTypeUse {
Byte,
Short,
Char,
Int,
Long,
Double,
Bool,
String,
Array(Option<Box<GenericArguments>>),
Any,
Void,
}
#[derive(Debug)]
pub struct InterfaceOrClassTypeUse {
borrow_count: usize,
is_mutable: bool,
fqn: Box<FullyQualifiedName>,
generics: Box<GenericArguments>,
}
impl InterfaceOrClassTypeUse {
pub fn new(
borrow_count: usize,
is_mutable: bool,
fqn: Box<FullyQualifiedName>,
generics: Box<GenericArguments>,
) -> Self {
Self {
borrow_count,
is_mutable,
fqn,
generics,
}
}
pub fn borrow_count(&self) -> usize {
self.borrow_count
}
pub fn is_mutable(&self) -> bool {
self.is_mutable
}
pub fn fqn(&self) -> &FullyQualifiedName {
&self.fqn
}
pub fn fqn_mut(&mut self) -> &mut FullyQualifiedName {
&mut self.fqn
}
pub fn generics(&self) -> &GenericArguments {
&self.generics
}
pub fn generics_mut(&mut self) -> &mut GenericArguments {
&mut self.generics
}
}
#[derive(Debug)]
pub struct TupleTypeUse {
borrow_count: usize,
is_mutable: bool,
arguments: Box<TupleArguments>,
}
impl TupleTypeUse {
pub fn new(borrow_count: usize, is_mutable: bool, arguments: Box<TupleArguments>) -> Self {
Self {
borrow_count,
is_mutable,
arguments,
}
}
pub fn borrow_count(&self) -> usize {
self.borrow_count
}
pub fn is_mutable(&self) -> bool {
self.is_mutable
}
pub fn arguments(&self) -> &TupleArguments {
&self.arguments
}
pub fn arguments_mut(&mut self) -> &mut TupleArguments {
&mut self.arguments
}
}
#[derive(Debug)]
pub struct FunctionTypeUse {
borrow_count: usize,
function_modifier: Option<FunctionTypeModifier>,
generics: Box<GenericParameters>,
parameters: Box<Parameters>,
return_type: Box<ReturnType>,
}
impl FunctionTypeUse {
pub fn new(
borrow_count: usize,
function_modifier: Option<FunctionTypeModifier>,
generics: Box<GenericParameters>,
parameters: Box<Parameters>,
return_type: Box<ReturnType>,
) -> Self {
Self {
borrow_count,
function_modifier,
generics,
parameters,
return_type,
}
}
pub fn borrow_count(&self) -> usize {
self.borrow_count
}
pub fn function_modifier(&self) -> Option<&FunctionTypeModifier> {
self.function_modifier.as_ref()
}
pub fn generics(&self) -> &GenericParameters {
&self.generics
}
pub fn generics_mut(&mut self) -> &mut GenericParameters {
&mut self.generics
}
pub fn parameters(&self) -> &Box<Parameters> {
&self.parameters
}
pub fn parameters_mut(&mut self) -> &mut Box<Parameters> {
&mut self.parameters
}
pub fn return_type(&self) -> &ReturnType {
&self.return_type
}
pub fn return_type_mut(&mut self) -> &mut ReturnType {
&mut self.return_type
}
}

View File

@ -0,0 +1,87 @@
use crate::ast::node::named::Named;
use crate::ast::node::names::Identifier;
use std::borrow::Cow;
use std::range::Range;
#[derive(Debug)]
pub struct UseStatement {
identifiers: Vec<Box<Identifier>>,
last: Box<UseStatementLast>,
file_id: usize,
range: Range<usize>,
}
impl UseStatement {
pub fn new(
identifiers: Vec<Box<Identifier>>,
last: Box<UseStatementLast>,
file_id: usize,
range: Range<usize>,
) -> Self {
UseStatement {
identifiers,
last,
file_id,
range,
}
}
pub fn base_name(&self) -> Cow<'_, str> {
use UseStatementLast::*;
if self.identifiers.is_empty() {
match self.last.as_ref() {
Identifier(_) => Cow::from(""),
Star | Identifiers(_) => panic!(), // should never get here because of grammar
}
} else if self.identifiers.len() == 1 {
self.identifiers[0].name()
} else {
let mut acc = String::new();
for (i, identifier) in self.identifiers.iter().enumerate() {
acc.push_str(&identifier.name());
if i != self.identifiers.len() - 1 {
acc.push_str("::");
}
}
Cow::from(acc)
}
}
pub fn is_star(&self) -> bool {
match self.last.as_ref() {
UseStatementLast::Star => true,
_ => false,
}
}
pub fn identifiers(&self) -> Vec<&Identifier> {
self.identifiers.iter().map(Box::as_ref).collect()
}
pub fn identifiers_mut(&mut self) -> Vec<&mut Identifier> {
self.identifiers.iter_mut().map(Box::as_mut).collect()
}
pub fn last(&self) -> &UseStatementLast {
&self.last
}
pub fn last_mut(&mut self) -> &mut UseStatementLast {
&mut self.last
}
pub fn file_id(&self) -> usize {
self.file_id
}
pub fn range(&self) -> Range<usize> {
self.range
}
}
#[derive(Debug)]
pub enum UseStatementLast {
Identifier(Box<Identifier>),
Identifiers(Vec<Box<Identifier>>),
Star,
}

View File

@ -1,7 +1,27 @@
use crate::ast::*; use crate::ast::node::call_expression::*;
use crate::ast::node::class::*;
use crate::ast::node::closure::*;
use crate::ast::node::compilation_unit::*;
use crate::ast::node::d_string::*;
use crate::ast::node::expression::*;
use crate::ast::node::function::*;
use crate::ast::node::generics::*;
use crate::ast::node::implements_list::*;
use crate::ast::node::interface::*;
use crate::ast::node::level::*;
use crate::ast::node::literal::*;
use crate::ast::node::module::*;
use crate::ast::node::named::Named;
use crate::ast::node::names::*;
use crate::ast::node::object_access::*;
use crate::ast::node::operators::*;
use crate::ast::node::statement::*;
use crate::ast::node::tuple_arguments::*;
use crate::ast::node::type_use::*;
use crate::ast::node::use_statement::*;
use crate::ast::unparse::Unparse;
use crate::util::indent_writer::IndentWriter; use crate::util::indent_writer::IndentWriter;
use std::fmt::Debug; use std::fmt::Debug;
use crate::ast::unparse::Unparse;
pub trait PrettyPrint { pub trait PrettyPrint {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()>; fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()>;
@ -9,7 +29,7 @@ pub trait PrettyPrint {
impl PrettyPrint for Operator { impl PrettyPrint for Operator {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
use Operator::*; use crate::ast::node::operators::Operator::*;
match self { match self {
Binary(o) => o.pretty_print(writer), Binary(o) => o.pretty_print(writer),
PrefixUnary(o) => o.pretty_print(writer), PrefixUnary(o) => o.pretty_print(writer),
@ -47,7 +67,7 @@ impl PrettyPrint for SuffixUnaryOperator {
impl PrettyPrint for Identifier { impl PrettyPrint for Identifier {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented(&format!("Identifier({})", self.name)) writer.writeln_indented(&format!("Identifier({})", self.name()))
} }
} }
@ -55,7 +75,7 @@ impl PrettyPrint for FullyQualifiedName {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented("FullyQualifiedName")?; writer.writeln_indented("FullyQualifiedName")?;
writer.increase_indent(); writer.increase_indent();
for identifier in &self.identifiers { for identifier in self.identifiers() {
identifier.pretty_print(writer)?; identifier.pretty_print(writer)?;
} }
writer.decrease_indent(); writer.decrease_indent();
@ -65,7 +85,7 @@ impl PrettyPrint for FullyQualifiedName {
impl PrettyPrint for TypeUse { impl PrettyPrint for TypeUse {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
use TypeUse::*; use crate::ast::node::type_use::TypeUse::*;
match self { match self {
Primitive(primitive_type_use) => { Primitive(primitive_type_use) => {
writer.writeln_indented("PrimitiveTypeUse")?; writer.writeln_indented("PrimitiveTypeUse")?;
@ -98,7 +118,7 @@ impl PrettyPrint for TypeUse {
impl PrettyPrint for PrimitiveTypeUse { impl PrettyPrint for PrimitiveTypeUse {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
use PrimitiveTypeUse::*; use crate::ast::node::type_use::PrimitiveTypeUse::*;
match self { match self {
Byte => writer.writeln_indented("Byte"), Byte => writer.writeln_indented("Byte"),
Short => writer.writeln_indented("Short"), Short => writer.writeln_indented("Short"),
@ -127,11 +147,12 @@ impl PrettyPrint for InterfaceOrClassTypeUse {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented(&format!( writer.writeln_indented(&format!(
"InterfaceOrClassTypeUse(borrow_count = {}, is_mutable = {})", "InterfaceOrClassTypeUse(borrow_count = {}, is_mutable = {})",
self.borrow_count, self.is_mutable self.borrow_count(),
self.is_mutable()
))?; ))?;
writer.increase_indent(); writer.increase_indent();
self.fqn.pretty_print(writer)?; self.fqn().pretty_print(writer)?;
self.generics.pretty_print(writer)?; self.generics().pretty_print(writer)?;
writer.decrease_indent(); writer.decrease_indent();
Ok(()) Ok(())
} }
@ -141,10 +162,11 @@ impl PrettyPrint for TupleTypeUse {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented(&format!( writer.writeln_indented(&format!(
"TupleTypeUse(borrow_count = {}, is_mutable = {})", "TupleTypeUse(borrow_count = {}, is_mutable = {})",
self.borrow_count, self.is_mutable self.borrow_count(),
self.is_mutable()
))?; ))?;
writer.increase_indent(); writer.increase_indent();
self.arguments.pretty_print(writer)?; self.arguments().pretty_print(writer)?;
writer.decrease_indent(); writer.decrease_indent();
Ok(()) Ok(())
} }
@ -154,15 +176,15 @@ impl PrettyPrint for FunctionTypeUse {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented(&format!( writer.writeln_indented(&format!(
"FunctionTypeUse(borrow_count = {})", "FunctionTypeUse(borrow_count = {})",
self.borrow_count self.borrow_count()
))?; ))?;
writer.increase_indent(); writer.increase_indent();
if let Some(function_type_modifier) = &self.function_modifier { if let Some(function_type_modifier) = self.function_modifier() {
function_type_modifier.pretty_print(writer)?; function_type_modifier.pretty_print(writer)?;
} }
self.generics.pretty_print(writer)?; self.generics().pretty_print(writer)?;
self.parameters.pretty_print(writer)?; self.parameters().pretty_print(writer)?;
self.return_type.pretty_print(writer)?; self.return_type().pretty_print(writer)?;
writer.decrease_indent(); writer.decrease_indent();
Ok(()) Ok(())
} }
@ -172,7 +194,7 @@ impl PrettyPrint for GenericArguments {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented("GenericArguments")?; writer.writeln_indented("GenericArguments")?;
writer.increase_indent(); writer.increase_indent();
for type_use in &self.0 { for type_use in self.arguments() {
type_use.pretty_print(writer)?; type_use.pretty_print(writer)?;
} }
writer.decrease_indent(); writer.decrease_indent();
@ -184,7 +206,7 @@ impl PrettyPrint for GenericParameters {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented("GenericParameters")?; writer.writeln_indented("GenericParameters")?;
writer.increase_indent(); writer.increase_indent();
for identifier in &self.0 { for identifier in self.identifiers() {
identifier.pretty_print(writer)?; identifier.pretty_print(writer)?;
} }
writer.decrease_indent(); writer.decrease_indent();
@ -196,7 +218,7 @@ impl PrettyPrint for TupleArguments {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented("TupleArguments")?; writer.writeln_indented("TupleArguments")?;
writer.increase_indent(); writer.increase_indent();
for type_use in &self.0 { for type_use in self.type_uses() {
type_use.pretty_print(writer)?; type_use.pretty_print(writer)?;
} }
writer.decrease_indent(); writer.decrease_indent();
@ -208,7 +230,7 @@ impl PrettyPrint for ImplementsList {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented("ImplementsList")?; writer.writeln_indented("ImplementsList")?;
writer.increase_indent(); writer.increase_indent();
for type_use in &self.0 { for type_use in self.type_uses() {
type_use.pretty_print(writer)?; type_use.pretty_print(writer)?;
} }
writer.decrease_indent(); writer.decrease_indent();
@ -218,7 +240,7 @@ impl PrettyPrint for ImplementsList {
impl PrettyPrint for FunctionTypeModifier { impl PrettyPrint for FunctionTypeModifier {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
use FunctionTypeModifier::*; use crate::ast::node::function::FunctionTypeModifier::*;
writer.writeln_indented(&format!( writer.writeln_indented(&format!(
"FunctionTypeModifier({})", "FunctionTypeModifier({})",
match self { match self {
@ -235,7 +257,7 @@ impl PrettyPrint for Parameters {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented("Parameters")?; writer.writeln_indented("Parameters")?;
writer.increase_indent(); writer.increase_indent();
for parameter in &self.0 { for parameter in self.parameters() {
parameter.pretty_print(writer)?; parameter.pretty_print(writer)?;
} }
writer.decrease_indent(); writer.decrease_indent();
@ -247,8 +269,8 @@ impl PrettyPrint for Parameter {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented("Parameter")?; writer.writeln_indented("Parameter")?;
writer.increase_indent(); writer.increase_indent();
self.identifier.pretty_print(writer)?; self.identifier().pretty_print(writer)?;
self.type_use.pretty_print(writer)?; self.type_use().pretty_print(writer)?;
writer.decrease_indent(); writer.decrease_indent();
Ok(()) Ok(())
} }
@ -258,8 +280,8 @@ impl PrettyPrint for ReturnType {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented("ReturnType")?; writer.writeln_indented("ReturnType")?;
writer.increase_indent(); writer.increase_indent();
self.declared_type.pretty_print(writer)?; self.declared_type().pretty_print(writer)?;
self.references.pretty_print(writer)?; self.references().pretty_print(writer)?;
writer.decrease_indent(); writer.decrease_indent();
Ok(()) Ok(())
} }
@ -269,7 +291,7 @@ impl PrettyPrint for References {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented("References")?; writer.writeln_indented("References")?;
writer.increase_indent(); writer.increase_indent();
for identifier in &self.0 { for identifier in self.identifiers() {
identifier.pretty_print(writer)?; identifier.pretty_print(writer)?;
} }
writer.decrease_indent(); writer.decrease_indent();
@ -281,16 +303,16 @@ impl PrettyPrint for CompilationUnit {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented("CompilationUnit")?; writer.writeln_indented("CompilationUnit")?;
writer.increase_indent(); writer.increase_indent();
if let Some(namespace) = &self.namespace { if let Some(namespace) = self.namespace() {
writer.writeln_indented("Namespace")?; writer.writeln_indented("Namespace")?;
writer.increase_indent(); writer.increase_indent();
namespace.pretty_print(writer)?; namespace.pretty_print(writer)?;
writer.decrease_indent(); writer.decrease_indent();
} }
for use_statement in &self.use_statements { for use_statement in self.use_statements() {
use_statement.pretty_print(writer)?; use_statement.pretty_print(writer)?;
} }
for declaration in &self.declarations { for declaration in self.declarations() {
declaration.pretty_print(writer)?; declaration.pretty_print(writer)?;
} }
writer.decrease_indent(); writer.decrease_indent();
@ -300,7 +322,7 @@ impl PrettyPrint for CompilationUnit {
impl PrettyPrint for ModuleLevelDeclaration { impl PrettyPrint for ModuleLevelDeclaration {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
use ModuleLevelDeclaration::*; use crate::ast::node::level::ModuleLevelDeclaration::*;
match self { match self {
Module(module) => module.pretty_print(writer), Module(module) => module.pretty_print(writer),
Interface(interface) => interface.pretty_print(writer), Interface(interface) => interface.pretty_print(writer),
@ -313,7 +335,7 @@ impl PrettyPrint for ModuleLevelDeclaration {
impl PrettyPrint for InterfaceLevelDeclaration { impl PrettyPrint for InterfaceLevelDeclaration {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
use InterfaceLevelDeclaration::*; use crate::ast::node::level::InterfaceLevelDeclaration::*;
match self { match self {
Module(module) => module.pretty_print(writer), Module(module) => module.pretty_print(writer),
Interface(interface) => interface.pretty_print(writer), Interface(interface) => interface.pretty_print(writer),
@ -326,7 +348,7 @@ impl PrettyPrint for InterfaceLevelDeclaration {
impl PrettyPrint for ClassLevelDeclaration { impl PrettyPrint for ClassLevelDeclaration {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
use ClassLevelDeclaration::*; use crate::ast::node::level::ClassLevelDeclaration::*;
match self { match self {
Module(module) => module.pretty_print(writer), Module(module) => module.pretty_print(writer),
Interface(interface) => interface.pretty_print(writer), Interface(interface) => interface.pretty_print(writer),
@ -344,11 +366,11 @@ impl PrettyPrint for ModuleDeclaration {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented(&format!( writer.writeln_indented(&format!(
"ModuleDeclaration(is_public = {})", "ModuleDeclaration(is_public = {})",
self.is_public self.is_public()
))?; ))?;
writer.increase_indent(); writer.increase_indent();
self.identifier.pretty_print(writer)?; self.identifier().pretty_print(writer)?;
for declaration in &self.declarations { for declaration in self.declarations() {
declaration.pretty_print(writer)?; declaration.pretty_print(writer)?;
} }
writer.decrease_indent(); writer.decrease_indent();
@ -360,13 +382,13 @@ impl PrettyPrint for InterfaceDeclaration {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented(&format!( writer.writeln_indented(&format!(
"InterfaceDeclaration(is_public = {})", "InterfaceDeclaration(is_public = {})",
self.is_public self.is_public()
))?; ))?;
writer.increase_indent(); writer.increase_indent();
self.identifier.pretty_print(writer)?; self.identifier().pretty_print(writer)?;
self.generics.pretty_print(writer)?; self.generics().pretty_print(writer)?;
self.implements.pretty_print(writer)?; self.implements().pretty_print(writer)?;
for declaration in &self.declarations { for declaration in self.declarations() {
declaration.pretty_print(writer)?; declaration.pretty_print(writer)?;
} }
writer.decrease_indent(); writer.decrease_indent();
@ -376,15 +398,18 @@ impl PrettyPrint for InterfaceDeclaration {
impl PrettyPrint for ClassDeclaration { impl PrettyPrint for ClassDeclaration {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented(&format!("ClassDeclaration(is_public = {})", self.is_public))?; writer.writeln_indented(&format!(
"ClassDeclaration(is_public = {})",
self.is_public()
))?;
writer.increase_indent(); writer.increase_indent();
self.identifier.pretty_print(writer)?; self.identifier().pretty_print(writer)?;
self.generics.pretty_print(writer)?; self.generics().pretty_print(writer)?;
if let Some(class_constructor) = &self.class_constructor { if let Some(class_constructor) = self.class_constructor() {
class_constructor.pretty_print(writer)?; class_constructor.pretty_print(writer)?;
} }
self.implements.pretty_print(writer)?; self.implements().pretty_print(writer)?;
for declaration in &self.declarations { for declaration in self.declarations() {
declaration.pretty_print(writer)?; declaration.pretty_print(writer)?;
} }
writer.decrease_indent(); writer.decrease_indent();
@ -396,17 +421,17 @@ impl PrettyPrint for FunctionDefinition {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented(&format!( writer.writeln_indented(&format!(
"FunctionDefinition(is_public = {})", "FunctionDefinition(is_public = {})",
self.is_public self.is_public()
))?; ))?;
writer.increase_indent(); writer.increase_indent();
if let Some(modifier) = &self.modifier { if let Some(modifier) = self.modifier() {
modifier.pretty_print(writer)?; modifier.pretty_print(writer)?;
} }
self.generics.pretty_print(writer)?; self.generics().pretty_print(writer)?;
self.identifier.pretty_print(writer)?; self.identifier().pretty_print(writer)?;
self.parameters.pretty_print(writer)?; self.parameters().pretty_print(writer)?;
self.return_type.pretty_print(writer)?; self.return_type().pretty_print(writer)?;
self.body.pretty_print(writer)?; self.body().pretty_print(writer)?;
writer.decrease_indent(); writer.decrease_indent();
Ok(()) Ok(())
} }
@ -416,17 +441,17 @@ impl PrettyPrint for OperatorFunctionDefinition {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented(&format!( writer.writeln_indented(&format!(
"OperatorFunctionDefinition(is_public = {})", "OperatorFunctionDefinition(is_public = {})",
self.is_public self.is_public()
))?; ))?;
writer.increase_indent(); writer.increase_indent();
if let Some(modifier) = &self.modifier { if let Some(modifier) = self.modifier() {
modifier.pretty_print(writer)?; modifier.pretty_print(writer)?;
} }
self.generics.pretty_print(writer)?; self.generics().pretty_print(writer)?;
self.operator.pretty_print(writer)?; self.operator().pretty_print(writer)?;
self.parameters.pretty_print(writer)?; self.parameters().pretty_print(writer)?;
self.return_type.pretty_print(writer)?; self.return_type().pretty_print(writer)?;
self.body.pretty_print(writer)?; self.body().pretty_print(writer)?;
writer.decrease_indent(); writer.decrease_indent();
Ok(()) Ok(())
} }
@ -436,16 +461,16 @@ impl PrettyPrint for PlatformFunctionDeclaration {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented(&format!( writer.writeln_indented(&format!(
"PlatformFunctionDeclaration(is_public = {})", "PlatformFunctionDeclaration(is_public = {})",
self.is_public self.is_public()
))?; ))?;
writer.increase_indent(); writer.increase_indent();
if let Some(modifier) = &self.modifier { if let Some(modifier) = self.modifier() {
modifier.pretty_print(writer)?; modifier.pretty_print(writer)?;
} }
self.generics.pretty_print(writer)?; self.generics().pretty_print(writer)?;
self.identifier.pretty_print(writer)?; self.identifier().pretty_print(writer)?;
self.parameters.pretty_print(writer)?; self.parameters().pretty_print(writer)?;
self.return_type.pretty_print(writer)?; self.return_type().pretty_print(writer)?;
writer.decrease_indent(); writer.decrease_indent();
Ok(()) Ok(())
} }
@ -455,14 +480,14 @@ impl PrettyPrint for InterfaceFunctionDeclaration {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented("InterfaceFunctionDeclaration")?; writer.writeln_indented("InterfaceFunctionDeclaration")?;
writer.increase_indent(); writer.increase_indent();
if let Some(modifier) = &self.modifier { if let Some(modifier) = self.modifier() {
modifier.pretty_print(writer)?; modifier.pretty_print(writer)?;
} }
self.generics.pretty_print(writer)?; self.generics().pretty_print(writer)?;
self.identifier.pretty_print(writer)?; self.identifier().pretty_print(writer)?;
self.parameters.pretty_print(writer)?; self.parameters().pretty_print(writer)?;
self.return_type.pretty_print(writer)?; self.return_type().pretty_print(writer)?;
if let Some(body) = &self.body { if let Some(body) = self.body() {
body.pretty_print(writer)?; body.pretty_print(writer)?;
} }
writer.decrease_indent(); writer.decrease_indent();
@ -474,14 +499,14 @@ impl PrettyPrint for InterfaceOperatorFunctionDeclaration {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented("InterfaceOperatorFunctionDeclaration")?; writer.writeln_indented("InterfaceOperatorFunctionDeclaration")?;
writer.increase_indent(); writer.increase_indent();
if let Some(modifier) = &self.modifier { if let Some(modifier) = self.modifier() {
modifier.pretty_print(writer)?; modifier.pretty_print(writer)?;
} }
self.generics.pretty_print(writer)?; self.generics().pretty_print(writer)?;
self.operator.pretty_print(writer)?; self.operator().pretty_print(writer)?;
self.parameters.pretty_print(writer)?; self.parameters().pretty_print(writer)?;
self.return_type.pretty_print(writer)?; self.return_type().pretty_print(writer)?;
if let Some(body) = &self.body { if let Some(body) = self.body() {
body.pretty_print(writer)?; body.pretty_print(writer)?;
} }
writer.decrease_indent(); writer.decrease_indent();
@ -491,7 +516,7 @@ impl PrettyPrint for InterfaceOperatorFunctionDeclaration {
impl PrettyPrint for FunctionModifier { impl PrettyPrint for FunctionModifier {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
use FunctionModifier::*; use crate::ast::node::function::FunctionModifier::*;
writer.writeln_indented(&format!( writer.writeln_indented(&format!(
"FunctionModifier({})", "FunctionModifier({})",
match self { match self {
@ -507,7 +532,7 @@ impl PrettyPrint for FunctionModifier {
impl PrettyPrint for FunctionBody { impl PrettyPrint for FunctionBody {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
use FunctionBody::*; use crate::ast::node::function::FunctionBody::*;
match self { match self {
Equals(expression) => { Equals(expression) => {
writer.writeln_indented("EqualsFunctionBody")?; writer.writeln_indented("EqualsFunctionBody")?;
@ -536,7 +561,7 @@ impl PrettyPrint for ClassConstructor {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented("ClassConstructor")?; writer.writeln_indented("ClassConstructor")?;
writer.increase_indent(); writer.increase_indent();
for constructor_parameter in &self.0 { for constructor_parameter in self.parameters() {
constructor_parameter.pretty_print(writer)?; constructor_parameter.pretty_print(writer)?;
} }
writer.decrease_indent(); writer.decrease_indent();
@ -546,7 +571,7 @@ impl PrettyPrint for ClassConstructor {
impl PrettyPrint for ClassConstructorParameter { impl PrettyPrint for ClassConstructorParameter {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
use ClassConstructorParameter::*; use crate::ast::node::class::ClassConstructorParameter::*;
match self { match self {
Property(property) => { Property(property) => {
writer.writeln_indented("PropertyConstructorParameter")?; writer.writeln_indented("PropertyConstructorParameter")?;
@ -569,11 +594,11 @@ impl PrettyPrint for PropertyDeclaration {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented(&format!( writer.writeln_indented(&format!(
"PropertyDeclaration(is_mutable = {})", "PropertyDeclaration(is_mutable = {})",
self.is_mutable self.is_mutable()
))?; ))?;
writer.increase_indent(); writer.increase_indent();
self.identifier.pretty_print(writer)?; self.identifier().pretty_print(writer)?;
self.declared_type.pretty_print(writer)?; self.declared_type().pretty_print(writer)?;
writer.decrease_indent(); writer.decrease_indent();
Ok(()) Ok(())
} }
@ -583,11 +608,11 @@ impl PrettyPrint for FieldDeclaration {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented(&format!( writer.writeln_indented(&format!(
"FieldDeclaration(is_mutable = {})", "FieldDeclaration(is_mutable = {})",
self.is_mutable self.is_mutable()
))?; ))?;
writer.increase_indent(); writer.increase_indent();
self.identifier.pretty_print(writer)?; self.identifier().pretty_print(writer)?;
self.declared_type.pretty_print(writer)?; self.declared_type().pretty_print(writer)?;
writer.decrease_indent(); writer.decrease_indent();
Ok(()) Ok(())
} }
@ -597,10 +622,10 @@ impl PrettyPrint for UseStatement {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented("UseStatement")?; writer.writeln_indented("UseStatement")?;
writer.increase_indent(); writer.increase_indent();
for identifier in &self.identifiers { for identifier in self.identifiers() {
identifier.pretty_print(writer)?; identifier.pretty_print(writer)?;
} }
self.last.pretty_print(writer)?; self.last().pretty_print(writer)?;
writer.decrease_indent(); writer.decrease_indent();
Ok(()) Ok(())
} }
@ -630,10 +655,10 @@ impl PrettyPrint for BlockStatement {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented("BlockStatement")?; writer.writeln_indented("BlockStatement")?;
writer.increase_indent(); writer.increase_indent();
for statement in &self.statements { for statement in self.statements() {
statement.pretty_print(writer)?; statement.pretty_print(writer)?;
} }
if let Some(expression) = &self.expression { if let Some(expression) = self.expression() {
expression.pretty_print(writer)?; expression.pretty_print(writer)?;
} }
Ok(()) Ok(())
@ -642,7 +667,7 @@ impl PrettyPrint for BlockStatement {
impl PrettyPrint for Statement { impl PrettyPrint for Statement {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
use Statement::*; use crate::ast::node::statement::Statement::*;
match self { match self {
BlockStatement(block_statement) => block_statement.pretty_print(writer), BlockStatement(block_statement) => block_statement.pretty_print(writer),
VariableDeclarationStatement(s) => s.pretty_print(writer), VariableDeclarationStatement(s) => s.pretty_print(writer),
@ -661,14 +686,14 @@ impl PrettyPrint for VariableDeclarationStatement {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented(&format!( writer.writeln_indented(&format!(
"VariableDeclarationStatement(is_mutable = {})", "VariableDeclarationStatement(is_mutable = {})",
self.is_mutable self.is_mutable()
))?; ))?;
writer.increase_indent(); writer.increase_indent();
self.identifier.pretty_print(writer)?; self.identifier().pretty_print(writer)?;
if let Some(declared_type) = &self.declared_type { if let Some(declared_type) = self.declared_type() {
declared_type.pretty_print(writer)?; declared_type.pretty_print(writer)?;
} }
if let Some(initializer) = &self.initializer { if let Some(initializer) = self.initializer() {
initializer.pretty_print(writer)?; initializer.pretty_print(writer)?;
} }
writer.decrease_indent(); writer.decrease_indent();
@ -680,8 +705,8 @@ impl PrettyPrint for AssignStatement {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented("AssignStatement")?; writer.writeln_indented("AssignStatement")?;
writer.increase_indent(); writer.increase_indent();
self.lhs.pretty_print(writer)?; self.lhs().pretty_print(writer)?;
self.rhs.pretty_print(writer)?; self.rhs().pretty_print(writer)?;
writer.decrease_indent(); writer.decrease_indent();
Ok(()) Ok(())
} }
@ -691,7 +716,7 @@ impl PrettyPrint for CallStatement {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented("CallStatement")?; writer.writeln_indented("CallStatement")?;
writer.increase_indent(); writer.increase_indent();
self.0.pretty_print(writer)?; self.expression().pretty_print(writer)?;
writer.decrease_indent(); writer.decrease_indent();
Ok(()) Ok(())
} }
@ -700,7 +725,7 @@ impl PrettyPrint for CallStatement {
impl PrettyPrint for ReturnStatement { impl PrettyPrint for ReturnStatement {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented("ReturnStatement")?; writer.writeln_indented("ReturnStatement")?;
if let Some(expression) = &self.0 { if let Some(expression) = self.expression() {
writer.increase_indent(); writer.increase_indent();
expression.pretty_print(writer)?; expression.pretty_print(writer)?;
writer.decrease_indent(); writer.decrease_indent();
@ -714,8 +739,8 @@ impl PrettyPrint for IfStatement {
writer.writeln_indented("IfStatement")?; writer.writeln_indented("IfStatement")?;
writer.increase_indent(); writer.increase_indent();
writer.increase_indent(); writer.increase_indent();
self.condition.pretty_print(writer)?; self.condition().pretty_print(writer)?;
self.then_block.pretty_print(writer)?; self.then_block().pretty_print(writer)?;
writer.decrease_indent(); writer.decrease_indent();
Ok(()) Ok(())
} }
@ -725,9 +750,9 @@ impl PrettyPrint for IfElseStatement {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented("IfElseStatement")?; writer.writeln_indented("IfElseStatement")?;
writer.increase_indent(); writer.increase_indent();
self.if_statement.pretty_print(writer)?; self.if_statement().pretty_print(writer)?;
self.else_ifs.pretty_print(writer)?; self.else_ifs().pretty_print(writer)?;
if let Some(else_block) = &self.else_block { if let Some(else_block) = self.else_block() {
else_block.pretty_print(writer)?; else_block.pretty_print(writer)?;
} }
writer.decrease_indent(); writer.decrease_indent();
@ -739,7 +764,7 @@ impl PrettyPrint for ElseIfs {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented("ElseIfs")?; writer.writeln_indented("ElseIfs")?;
writer.increase_indent(); writer.increase_indent();
for if_statement in &self.0 { for if_statement in self.if_statements() {
if_statement.pretty_print(writer)?; if_statement.pretty_print(writer)?;
} }
writer.decrease_indent(); writer.decrease_indent();
@ -751,7 +776,7 @@ impl PrettyPrint for ElseBlock {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented("ElseBlock")?; writer.writeln_indented("ElseBlock")?;
writer.increase_indent(); writer.increase_indent();
self.0.pretty_print(writer)?; self.block_statement().pretty_print(writer)?;
writer.decrease_indent(); writer.decrease_indent();
Ok(()) Ok(())
} }
@ -761,8 +786,8 @@ impl PrettyPrint for WhileStatement {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented("WhileStatement")?; writer.writeln_indented("WhileStatement")?;
writer.increase_indent(); writer.increase_indent();
self.condition.pretty_print(writer)?; self.condition().pretty_print(writer)?;
self.body.pretty_print(writer)?; self.body().pretty_print(writer)?;
writer.decrease_indent(); writer.decrease_indent();
Ok(()) Ok(())
} }
@ -772,9 +797,9 @@ impl PrettyPrint for ForStatement {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented("ForStatement")?; writer.writeln_indented("ForStatement")?;
writer.increase_indent(); writer.increase_indent();
self.variable.pretty_print(writer)?; self.variable().pretty_print(writer)?;
self.iterator.pretty_print(writer)?; self.iterator().pretty_print(writer)?;
self.body.pretty_print(writer)?; self.body().pretty_print(writer)?;
writer.decrease_indent(); writer.decrease_indent();
Ok(()) Ok(())
} }
@ -782,7 +807,7 @@ impl PrettyPrint for ForStatement {
impl PrettyPrint for Expression { impl PrettyPrint for Expression {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
use Expression::*; use crate::ast::node::expression::Expression::*;
match self { match self {
Ternary(e) => e.pretty_print(writer), Ternary(e) => e.pretty_print(writer),
Binary(e) => e.pretty_print(writer), Binary(e) => e.pretty_print(writer),
@ -801,9 +826,9 @@ impl PrettyPrint for TernaryExpression {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented("TernaryExpression")?; writer.writeln_indented("TernaryExpression")?;
writer.increase_indent(); writer.increase_indent();
self.condition.pretty_print(writer)?; self.condition().pretty_print(writer)?;
self.true_expression.pretty_print(writer)?; self.true_expression().pretty_print(writer)?;
self.false_expression.pretty_print(writer)?; self.false_expression().pretty_print(writer)?;
writer.decrease_indent(); writer.decrease_indent();
Ok(()) Ok(())
} }
@ -813,9 +838,9 @@ impl PrettyPrint for BinaryExpression {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented("BinaryExpression")?; writer.writeln_indented("BinaryExpression")?;
writer.increase_indent(); writer.increase_indent();
self.left.pretty_print(writer)?; self.left().pretty_print(writer)?;
self.operator.pretty_print(writer)?; self.operator().pretty_print(writer)?;
self.right.pretty_print(writer)?; self.right().pretty_print(writer)?;
writer.decrease_indent(); writer.decrease_indent();
Ok(()) Ok(())
} }
@ -825,8 +850,8 @@ impl PrettyPrint for PrefixExpression {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented("PrefixExpression")?; writer.writeln_indented("PrefixExpression")?;
writer.increase_indent(); writer.increase_indent();
self.operator.pretty_print(writer)?; self.operator().pretty_print(writer)?;
self.expression.pretty_print(writer)?; self.expression().pretty_print(writer)?;
writer.decrease_indent(); writer.decrease_indent();
Ok(()) Ok(())
} }
@ -836,8 +861,8 @@ impl PrettyPrint for SuffixExpression {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented("SuffixExpression")?; writer.writeln_indented("SuffixExpression")?;
writer.increase_indent(); writer.increase_indent();
self.expression.pretty_print(writer)?; self.expression().pretty_print(writer)?;
self.operator.pretty_print(writer)?; self.operator().pretty_print(writer)?;
writer.decrease_indent(); writer.decrease_indent();
Ok(()) Ok(())
} }
@ -847,11 +872,11 @@ impl PrettyPrint for CallExpression {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented("CallExpression")?; writer.writeln_indented("CallExpression")?;
writer.increase_indent(); writer.increase_indent();
self.callee.pretty_print(writer)?; self.callee().pretty_print(writer)?;
if let Some(turbo_fish) = &self.turbo_fish { if let Some(turbo_fish) = self.turbo_fish() {
turbo_fish.pretty_print(writer)?; turbo_fish.pretty_print(writer)?;
} }
self.arguments.pretty_print(writer)?; self.arguments().pretty_print(writer)?;
writer.decrease_indent(); writer.decrease_indent();
Ok(()) Ok(())
} }
@ -861,7 +886,7 @@ impl PrettyPrint for TurboFish {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented("TurboFish")?; writer.writeln_indented("TurboFish")?;
writer.increase_indent(); writer.increase_indent();
self.0.pretty_print(writer)?; self.generics().pretty_print(writer)?;
writer.decrease_indent(); writer.decrease_indent();
Ok(()) Ok(())
} }
@ -871,7 +896,7 @@ impl PrettyPrint for CallArguments {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented("CallArguments")?; writer.writeln_indented("CallArguments")?;
writer.increase_indent(); writer.increase_indent();
for call_argument in &self.0 { for call_argument in self.arguments() {
call_argument.pretty_print(writer)?; call_argument.pretty_print(writer)?;
} }
writer.decrease_indent(); writer.decrease_indent();
@ -883,7 +908,7 @@ impl PrettyPrint for CallArgument {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented("CallArgument")?; writer.writeln_indented("CallArgument")?;
writer.increase_indent(); writer.increase_indent();
self.0.pretty_print(writer)?; self.expression().pretty_print(writer)?;
writer.decrease_indent(); writer.decrease_indent();
Ok(()) Ok(())
} }
@ -891,22 +916,22 @@ impl PrettyPrint for CallArgument {
impl PrettyPrint for Closure { impl PrettyPrint for Closure {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented(&format!("Closure(is_move = {})", self.is_move))?; writer.writeln_indented(&format!("Closure(is_move = {})", self.is_move()))?;
writer.increase_indent(); writer.increase_indent();
if let Some(modifier) = &self.modifier { if let Some(modifier) = self.modifier() {
modifier.pretty_print(writer)?; modifier.pretty_print(writer)?;
} }
self.captures.pretty_print(writer)?; self.captures().pretty_print(writer)?;
self.parameters.pretty_print(writer)?; self.parameters().pretty_print(writer)?;
if !self.statements.is_empty() { if !self.statements().is_empty() {
writer.writeln_indented("ClosureStatements")?; writer.writeln_indented("ClosureStatements")?;
writer.increase_indent(); writer.increase_indent();
for statement in &self.statements { for statement in self.statements() {
statement.pretty_print(writer)?; statement.pretty_print(writer)?;
} }
writer.decrease_indent(); writer.decrease_indent();
} }
if let Some(expression) = &self.expression { if let Some(expression) = self.expression() {
expression.pretty_print(writer)?; expression.pretty_print(writer)?;
} }
writer.decrease_indent(); writer.decrease_indent();
@ -930,7 +955,7 @@ impl PrettyPrint for ClosureCaptures {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented("ClosureCaptures")?; writer.writeln_indented("ClosureCaptures")?;
writer.increase_indent(); writer.increase_indent();
for capture in &self.0 { for capture in self.captures() {
capture.pretty_print(writer)?; capture.pretty_print(writer)?;
} }
writer.decrease_indent(); writer.decrease_indent();
@ -942,10 +967,11 @@ impl PrettyPrint for ClosureCapture {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented(&format!( writer.writeln_indented(&format!(
"ClosureCapture(borrow_count = {}, is_mutable = {})", "ClosureCapture(borrow_count = {}, is_mutable = {})",
self.borrow_count, self.is_mutable self.borrow_count(),
self.is_mutable()
))?; ))?;
writer.increase_indent(); writer.increase_indent();
self.identifier.pretty_print(writer)?; self.identifier().pretty_print(writer)?;
writer.decrease_indent(); writer.decrease_indent();
Ok(()) Ok(())
} }
@ -955,7 +981,7 @@ impl PrettyPrint for ClosureParameters {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented("ClosureParameters")?; writer.writeln_indented("ClosureParameters")?;
writer.increase_indent(); writer.increase_indent();
for parameter in &self.0 { for parameter in self.parameters() {
parameter.pretty_print(writer)?; parameter.pretty_print(writer)?;
} }
writer.decrease_indent(); writer.decrease_indent();
@ -967,8 +993,8 @@ impl PrettyPrint for ClosureParameter {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented("ClosureParameter")?; writer.writeln_indented("ClosureParameter")?;
writer.increase_indent(); writer.increase_indent();
self.identifier.pretty_print(writer)?; self.identifier().pretty_print(writer)?;
if let Some(type_use) = &self.type_use { if let Some(type_use) = self.type_use() {
type_use.pretty_print(writer)?; type_use.pretty_print(writer)?;
} }
writer.decrease_indent(); writer.decrease_indent();
@ -980,8 +1006,8 @@ impl PrettyPrint for ObjectAccess {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented("ObjectAccess")?; writer.writeln_indented("ObjectAccess")?;
writer.increase_indent(); writer.increase_indent();
self.receiver.pretty_print(writer)?; self.receiver().pretty_print(writer)?;
self.navigations.pretty_print(writer)?; self.navigations().pretty_print(writer)?;
writer.decrease_indent(); writer.decrease_indent();
Ok(()) Ok(())
} }
@ -991,7 +1017,7 @@ impl PrettyPrint for ObjectNavigations {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented("ObjectNavigations")?; writer.writeln_indented("ObjectNavigations")?;
writer.increase_indent(); writer.increase_indent();
for object_navigation in &self.0 { for object_navigation in self.navigations() {
object_navigation.pretty_print(writer)?; object_navigation.pretty_print(writer)?;
} }
writer.decrease_indent(); writer.decrease_indent();
@ -1001,7 +1027,7 @@ impl PrettyPrint for ObjectNavigations {
impl PrettyPrint for ObjectNavigation { impl PrettyPrint for ObjectNavigation {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
use ObjectNavigation::*; use crate::ast::node::object_access::ObjectNavigation::*;
match self { match self {
Index(e) => { Index(e) => {
writer.writeln_indented("Index")?; writer.writeln_indented("Index")?;
@ -1022,7 +1048,7 @@ impl PrettyPrint for ObjectNavigation {
impl PrettyPrint for Literal { impl PrettyPrint for Literal {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
use Literal::*; use crate::ast::node::literal::Literal::*;
match self { match self {
Integer(i) => writer.writeln_indented(&format!("Integer({})", i)), Integer(i) => writer.writeln_indented(&format!("Integer({})", i)),
Long(l) => writer.writeln_indented(&format!("Long({})", l)), Long(l) => writer.writeln_indented(&format!("Long({})", l)),
@ -1040,7 +1066,7 @@ impl PrettyPrint for DString {
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.writeln_indented("DString")?; writer.writeln_indented("DString")?;
writer.increase_indent(); writer.increase_indent();
for part in &self.0 { for part in self.parts() {
part.pretty_print(writer)?; part.pretty_print(writer)?;
} }
Ok(()) Ok(())

View File

@ -1,4 +1,24 @@
use crate::ast::*; use crate::ast::node::call_expression::*;
use crate::ast::node::class::*;
use crate::ast::node::closure::*;
use crate::ast::node::compilation_unit::*;
use crate::ast::node::d_string::*;
use crate::ast::node::expression::*;
use crate::ast::node::function::*;
use crate::ast::node::generics::*;
use crate::ast::node::implements_list::*;
use crate::ast::node::interface::*;
use crate::ast::node::level::*;
use crate::ast::node::literal::*;
use crate::ast::node::module::*;
use crate::ast::node::named::Named;
use crate::ast::node::names::*;
use crate::ast::node::object_access::*;
use crate::ast::node::operators::*;
use crate::ast::node::statement::*;
use crate::ast::node::tuple_arguments::*;
use crate::ast::node::type_use::*;
use crate::ast::node::use_statement::*;
use crate::util::indent_writer::IndentWriter; use crate::util::indent_writer::IndentWriter;
macro_rules! to_unparse_vec { macro_rules! to_unparse_vec {
@ -73,7 +93,7 @@ fn unparse_comma_list(writer: &mut IndentWriter, items: &[&dyn Unparse]) -> std:
impl Unparse for Operator { impl Unparse for Operator {
fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
use Operator::*; use crate::ast::node::operators::Operator::*;
match self { match self {
Binary(op) => op.unparse(writer), Binary(op) => op.unparse(writer),
PrefixUnary(op) => op.unparse(writer), PrefixUnary(op) => op.unparse(writer),
@ -84,7 +104,7 @@ impl Unparse for Operator {
impl Unparse for BinaryOperator { impl Unparse for BinaryOperator {
fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
use BinaryOperator::*; use crate::ast::node::operators::BinaryOperator::*;
match self { match self {
Or => writer.write("||"), Or => writer.write("||"),
And => writer.write("&&"), And => writer.write("&&"),
@ -107,7 +127,7 @@ impl Unparse for BinaryOperator {
impl Unparse for PrefixUnaryOperator { impl Unparse for PrefixUnaryOperator {
fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
use PrefixUnaryOperator::*; use crate::ast::node::operators::PrefixUnaryOperator::*;
match self { match self {
Spread => writer.write("..."), Spread => writer.write("..."),
BorrowMut => writer.write("&mut"), BorrowMut => writer.write("&mut"),
@ -122,7 +142,7 @@ impl Unparse for PrefixUnaryOperator {
impl Unparse for SuffixUnaryOperator { impl Unparse for SuffixUnaryOperator {
fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
use SuffixUnaryOperator::*; use crate::ast::node::operators::SuffixUnaryOperator::*;
match self { match self {
PlusPlus => writer.write("++"), PlusPlus => writer.write("++"),
MinusMinus => writer.write("--"), MinusMinus => writer.write("--"),
@ -136,7 +156,7 @@ impl Unparse for SuffixUnaryOperator {
impl Unparse for Identifier { impl Unparse for Identifier {
fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.write(&self.name) writer.write(&self.name())
} }
} }
@ -154,7 +174,7 @@ impl ListUnparse for FullyQualifiedName {
impl Unparse for TypeUse { impl Unparse for TypeUse {
fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
use TypeUse::*; use crate::ast::node::type_use::TypeUse::*;
match self { match self {
Void => writer.write("Void"), Void => writer.write("Void"),
InterfaceOrClass(interface_or_class_type_use) => { InterfaceOrClass(interface_or_class_type_use) => {
@ -302,7 +322,7 @@ impl ListUnparse for ImplementsList {
impl Unparse for FunctionTypeModifier { impl Unparse for FunctionTypeModifier {
fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
use FunctionTypeModifier::*; use crate::ast::node::function::FunctionTypeModifier::*;
match self { match self {
Cons => writer.write("cons"), Cons => writer.write("cons"),
MutRef => writer.write("mut ref"), MutRef => writer.write("mut ref"),
@ -426,7 +446,7 @@ impl Unparse for UseStatementLast {
impl Unparse for ModuleLevelDeclaration { impl Unparse for ModuleLevelDeclaration {
fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
use ModuleLevelDeclaration::*; use crate::ast::node::level::ModuleLevelDeclaration::*;
match self { match self {
Module(module_declaration) => module_declaration.unparse(writer), Module(module_declaration) => module_declaration.unparse(writer),
Interface(interface_declaration) => interface_declaration.unparse(writer), Interface(interface_declaration) => interface_declaration.unparse(writer),
@ -441,7 +461,7 @@ impl Unparse for ModuleLevelDeclaration {
impl Unparse for InterfaceLevelDeclaration { impl Unparse for InterfaceLevelDeclaration {
fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
use InterfaceLevelDeclaration::*; use crate::ast::node::level::InterfaceLevelDeclaration::*;
match self { match self {
Module(module_declaration) => module_declaration.unparse(writer), Module(module_declaration) => module_declaration.unparse(writer),
Interface(interface_declaration) => interface_declaration.unparse(writer), Interface(interface_declaration) => interface_declaration.unparse(writer),
@ -458,7 +478,7 @@ impl Unparse for InterfaceLevelDeclaration {
impl Unparse for ClassLevelDeclaration { impl Unparse for ClassLevelDeclaration {
fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
use ClassLevelDeclaration::*; use crate::ast::node::level::ClassLevelDeclaration::*;
match self { match self {
Module(module_declaration) => module_declaration.unparse(writer), Module(module_declaration) => module_declaration.unparse(writer),
Interface(interface_declaration) => interface_declaration.unparse(writer), Interface(interface_declaration) => interface_declaration.unparse(writer),
@ -672,7 +692,7 @@ impl Unparse for InterfaceOperatorFunctionDeclaration {
impl Unparse for FunctionModifier { impl Unparse for FunctionModifier {
fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
use FunctionModifier::*; use crate::ast::node::function::FunctionModifier::*;
match self { match self {
Static => writer.write("static"), Static => writer.write("static"),
Cons => writer.write("cons"), Cons => writer.write("cons"),
@ -687,7 +707,7 @@ impl Unparse for FunctionModifier {
impl Unparse for FunctionBody { impl Unparse for FunctionBody {
fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
use FunctionBody::*; use crate::ast::node::function::FunctionBody::*;
match self { match self {
Equals(expression) => { Equals(expression) => {
writer.write("= ")?; writer.write("= ")?;
@ -730,7 +750,7 @@ impl ListUnparse for ClassConstructor {
impl Unparse for ClassConstructorParameter { impl Unparse for ClassConstructorParameter {
fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
use ClassConstructorParameter::*; use crate::ast::node::class::ClassConstructorParameter::*;
match self { match self {
Property(property) => property.unparse(writer), Property(property) => property.unparse(writer),
Field(field) => field.unparse(writer), Field(field) => field.unparse(writer),
@ -777,10 +797,10 @@ impl Unparse for BlockStatement {
impl BlockStatement { impl BlockStatement {
fn unparse_inner(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn unparse_inner(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.increase_indent(); writer.increase_indent();
for statement in &self.statements { for statement in self.statements() {
statement.unparse(writer)?; statement.unparse(writer)?;
} }
if let Some(expression) = &self.expression { if let Some(expression) = self.expression() {
expression.unparse(writer)?; expression.unparse(writer)?;
writer.writeln("")?; writer.writeln("")?;
} }
@ -791,7 +811,7 @@ impl BlockStatement {
impl Unparse for Statement { impl Unparse for Statement {
fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
use Statement::*; use crate::ast::node::statement::Statement::*;
match self { match self {
BlockStatement(statement) => statement.unparse(writer), BlockStatement(statement) => statement.unparse(writer),
CallStatement(call) => call.unparse(writer), CallStatement(call) => call.unparse(writer),
@ -820,15 +840,15 @@ impl Unparse for Statement {
impl Unparse for VariableDeclarationStatement { impl Unparse for VariableDeclarationStatement {
fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.write_indented("let ")?; writer.write_indented("let ")?;
if self.is_mutable { if self.is_mutable() {
writer.write("mut ")?; writer.write("mut ")?;
} }
self.identifier.unparse(writer)?; self.identifier().unparse(writer)?;
if let Some(declared_type) = &self.declared_type { if let Some(declared_type) = self.declared_type() {
writer.write(": ")?; writer.write(": ")?;
declared_type.unparse(writer)?; declared_type.unparse(writer)?;
} }
if let Some(initializer) = &self.initializer { if let Some(initializer) = self.initializer() {
writer.write(" = ")?; writer.write(" = ")?;
initializer.unparse(writer)?; initializer.unparse(writer)?;
} }
@ -839,9 +859,9 @@ impl Unparse for VariableDeclarationStatement {
impl Unparse for AssignStatement { impl Unparse for AssignStatement {
fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.write_indented("")?; writer.write_indented("")?;
self.lhs.unparse(writer)?; self.lhs().unparse(writer)?;
writer.write(" = ")?; writer.write(" = ")?;
self.rhs.unparse(writer)?; self.rhs().unparse(writer)?;
Ok(()) Ok(())
} }
} }
@ -849,7 +869,7 @@ impl Unparse for AssignStatement {
impl Unparse for CallStatement { impl Unparse for CallStatement {
fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.write_indented("")?; writer.write_indented("")?;
self.0.unparse(writer)?; self.expression().unparse(writer)?;
Ok(()) Ok(())
} }
} }
@ -857,7 +877,7 @@ impl Unparse for CallStatement {
impl Unparse for ReturnStatement { impl Unparse for ReturnStatement {
fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.write_indented("return")?; writer.write_indented("return")?;
if let Some(expression) = &self.0 { if let Some(expression) = self.expression() {
writer.write(" ")?; writer.write(" ")?;
expression.unparse(writer)?; expression.unparse(writer)?;
} }
@ -868,9 +888,9 @@ impl Unparse for ReturnStatement {
impl Unparse for IfStatement { impl Unparse for IfStatement {
fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.write_indented("if (")?; writer.write_indented("if (")?;
self.condition.unparse(writer)?; self.condition().unparse(writer)?;
writer.writeln(") {")?; writer.writeln(") {")?;
self.then_block.unparse_inner(writer)?; self.then_block().unparse_inner(writer)?;
writer.writeln_indented("}")?; writer.writeln_indented("}")?;
Ok(()) Ok(())
} }
@ -878,9 +898,9 @@ impl Unparse for IfStatement {
impl Unparse for IfElseStatement { impl Unparse for IfElseStatement {
fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
self.if_statement.unparse(writer)?; self.if_statement().unparse(writer)?;
self.else_ifs.unparse(writer)?; self.else_ifs().unparse(writer)?;
if let Some(else_block) = &self.else_block { if let Some(else_block) = self.else_block() {
else_block.unparse(writer)?; else_block.unparse(writer)?;
} }
Ok(()) Ok(())
@ -889,11 +909,11 @@ impl Unparse for IfElseStatement {
impl Unparse for ElseIfs { impl Unparse for ElseIfs {
fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
for if_statement in &self.0 { for if_statement in self.if_statements() {
writer.write_indented("else if (")?; writer.write_indented("else if (")?;
if_statement.condition.unparse(writer)?; if_statement.condition().unparse(writer)?;
writer.writeln(") {")?; writer.writeln(") {")?;
if_statement.then_block.unparse_inner(writer)?; if_statement.then_block().unparse_inner(writer)?;
writer.writeln_indented("}")?; writer.writeln_indented("}")?;
} }
Ok(()) Ok(())
@ -903,7 +923,7 @@ impl Unparse for ElseIfs {
impl Unparse for ElseBlock { impl Unparse for ElseBlock {
fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.write_indented("else ")?; writer.write_indented("else ")?;
self.0.unparse(writer)?; self.block_statement().unparse(writer)?;
Ok(()) Ok(())
} }
} }
@ -911,9 +931,9 @@ impl Unparse for ElseBlock {
impl Unparse for WhileStatement { impl Unparse for WhileStatement {
fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.write_indented("while ")?; writer.write_indented("while ")?;
self.condition.unparse(writer)?; self.condition().unparse(writer)?;
writer.writeln(" {")?; writer.writeln(" {")?;
self.body.unparse_inner(writer)?; self.body().unparse_inner(writer)?;
writer.writeln_indented("}")?; writer.writeln_indented("}")?;
Ok(()) Ok(())
} }
@ -922,11 +942,11 @@ impl Unparse for WhileStatement {
impl Unparse for ForStatement { impl Unparse for ForStatement {
fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.write_indented("for ")?; writer.write_indented("for ")?;
self.variable.unparse(writer)?; self.variable().unparse(writer)?;
writer.write(" in ")?; writer.write(" in ")?;
self.iterator.unparse(writer)?; self.iterator().unparse(writer)?;
writer.writeln(" {")?; writer.writeln(" {")?;
self.body.unparse_inner(writer)?; self.body().unparse_inner(writer)?;
writer.writeln_indented("}")?; writer.writeln_indented("}")?;
Ok(()) Ok(())
} }
@ -936,7 +956,7 @@ impl Unparse for ForStatement {
impl Unparse for Expression { impl Unparse for Expression {
fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
use Expression::*; use crate::ast::node::expression::Expression::*;
match self { match self {
Ternary(ternary) => ternary.unparse(writer), Ternary(ternary) => ternary.unparse(writer),
Binary(binary) => binary.unparse(writer), Binary(binary) => binary.unparse(writer),
@ -953,54 +973,54 @@ impl Unparse for Expression {
impl Unparse for TernaryExpression { impl Unparse for TernaryExpression {
fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
self.condition.unparse(writer)?; self.condition().unparse(writer)?;
writer.write(" ? ")?; writer.write(" ? ")?;
self.true_expression.unparse(writer)?; self.true_expression().unparse(writer)?;
writer.write(" : ")?; writer.write(" : ")?;
self.false_expression.unparse(writer)?; self.false_expression().unparse(writer)?;
Ok(()) Ok(())
} }
} }
impl Unparse for BinaryExpression { impl Unparse for BinaryExpression {
fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
self.left.unparse(writer)?; self.left().unparse(writer)?;
writer.write(" ")?; writer.write(" ")?;
self.operator.unparse(writer)?; self.operator().unparse(writer)?;
writer.write(" ")?; writer.write(" ")?;
self.right.unparse(writer)?; self.right().unparse(writer)?;
Ok(()) Ok(())
} }
} }
impl Unparse for PrefixExpression { impl Unparse for PrefixExpression {
fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
use PrefixUnaryOperator::*; use crate::ast::node::operators::PrefixUnaryOperator::*;
match &self.operator { match self.operator() {
BorrowMut => writer.write("&mut "), BorrowMut => writer.write("&mut "),
Mut => writer.write("mut "), Mut => writer.write("mut "),
other => other.unparse(writer), other => other.unparse(writer),
}?; }?;
self.expression.unparse(writer)?; self.expression().unparse(writer)?;
Ok(()) Ok(())
} }
} }
impl Unparse for SuffixExpression { impl Unparse for SuffixExpression {
fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
self.expression.unparse(writer)?; self.expression().unparse(writer)?;
self.operator.unparse(writer)?; self.operator().unparse(writer)?;
Ok(()) Ok(())
} }
} }
impl Unparse for CallExpression { impl Unparse for CallExpression {
fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
self.callee.unparse(writer)?; self.callee().unparse(writer)?;
if let Some(turbo_fish) = &self.turbo_fish { if let Some(turbo_fish) = self.turbo_fish() {
turbo_fish.unparse(writer)?; turbo_fish.unparse(writer)?;
} }
self.arguments.unparse(writer)?; self.arguments().unparse(writer)?;
Ok(()) Ok(())
} }
} }
@ -1008,7 +1028,7 @@ impl Unparse for CallExpression {
impl Unparse for TurboFish { impl Unparse for TurboFish {
fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.write("::")?; writer.write("::")?;
self.0.unparse(writer)?; self.generics().unparse(writer)?;
Ok(()) Ok(())
} }
} }
@ -1033,43 +1053,43 @@ impl ListUnparse for CallArguments {
impl Unparse for CallArgument { impl Unparse for CallArgument {
fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
self.0.unparse(writer) self.expression().unparse(writer)
} }
} }
impl Unparse for Closure { impl Unparse for Closure {
fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
if let Some(modifier) = &self.modifier { if let Some(modifier) = self.modifier() {
match modifier { match modifier {
ClosureModifier::Cons => writer.write("cons "), ClosureModifier::Cons => writer.write("cons "),
ClosureModifier::Mut => writer.write("mut "), ClosureModifier::Mut => writer.write("mut "),
}?; }?;
} }
if self.is_move { if self.is_move() {
writer.write("move ")?; writer.write("move ")?;
} }
if !self.captures.is_empty() { if !self.captures().is_empty() {
self.captures.unparse(writer)?; self.captures().unparse(writer)?;
writer.write(" ")?; writer.write(" ")?;
} }
writer.write("{ ")?; writer.write("{ ")?;
if !self.parameters.is_empty() { if !self.parameters().is_empty() {
self.parameters.unparse(writer)?; self.parameters().unparse(writer)?;
writer.write(" -> ")?; writer.write(" -> ")?;
} }
if !self.statements.is_empty() { if !self.statements().is_empty() {
writer.write("\n")?; writer.write("\n")?;
writer.increase_indent(); writer.increase_indent();
for statement in &self.statements { for statement in self.statements() {
statement.unparse(writer)?; statement.unparse(writer)?;
} }
writer.decrease_indent(); writer.decrease_indent();
} }
if let Some(expression) = &self.expression { if let Some(expression) = self.expression() {
if self.statements.is_empty() { if self.statements().is_empty() {
expression.unparse(writer)?; expression.unparse(writer)?;
writer.write(" }")?; writer.write(" }")?;
} else { } else {
@ -1080,7 +1100,7 @@ impl Unparse for Closure {
writer.decrease_indent(); writer.decrease_indent();
writer.write_indented("}")?; writer.write_indented("}")?;
} }
} else if !self.statements.is_empty() { } else if !self.statements().is_empty() {
writer.write_indented(" }")?; writer.write_indented(" }")?;
} else { } else {
writer.write(" }")?; writer.write(" }")?;
@ -1092,9 +1112,9 @@ impl Unparse for Closure {
impl Unparse for ClosureCaptures { impl Unparse for ClosureCaptures {
fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.write("|")?; writer.write("|")?;
for (i, capture) in self.0.iter().enumerate() { for (i, capture) in self.captures().iter().enumerate() {
capture.unparse(writer)?; capture.unparse(writer)?;
if i != self.0.len() - 1 { if i != self.captures().len() - 1 {
writer.write(", ")?; writer.write(", ")?;
} }
} }
@ -1105,20 +1125,20 @@ impl Unparse for ClosureCaptures {
impl Unparse for ClosureCapture { impl Unparse for ClosureCapture {
fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
for _ in 0..self.borrow_count { for _ in 0..self.borrow_count() {
writer.write("&")?; writer.write("&")?;
} }
if self.is_mutable { if self.is_mutable() {
writer.write("mut ")?; writer.write("mut ")?;
} }
self.identifier.unparse(writer)?; self.identifier().unparse(writer)?;
Ok(()) Ok(())
} }
} }
impl Unparse for ClosureParameters { impl Unparse for ClosureParameters {
fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
for parameter in &self.0 { for parameter in self.parameters() {
parameter.unparse(writer)?; parameter.unparse(writer)?;
} }
Ok(()) Ok(())
@ -1127,8 +1147,8 @@ impl Unparse for ClosureParameters {
impl Unparse for ClosureParameter { impl Unparse for ClosureParameter {
fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
self.identifier.unparse(writer)?; self.identifier().unparse(writer)?;
if let Some(type_use) = &self.type_use { if let Some(type_use) = self.type_use() {
writer.write(": ")?; writer.write(": ")?;
type_use.unparse(writer)?; type_use.unparse(writer)?;
} }
@ -1138,15 +1158,15 @@ impl Unparse for ClosureParameter {
impl Unparse for ObjectAccess { impl Unparse for ObjectAccess {
fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
self.receiver.unparse(writer)?; self.receiver().unparse(writer)?;
self.navigations.unparse(writer)?; self.navigations().unparse(writer)?;
Ok(()) Ok(())
} }
} }
impl Unparse for ObjectNavigations { impl Unparse for ObjectNavigations {
fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
for navigation in &self.0 { for navigation in self.navigations() {
navigation.unparse(writer)?; navigation.unparse(writer)?;
} }
Ok(()) Ok(())
@ -1155,7 +1175,7 @@ impl Unparse for ObjectNavigations {
impl Unparse for ObjectNavigation { impl Unparse for ObjectNavigation {
fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
use ObjectNavigation::*; use crate::ast::node::object_access::ObjectNavigation::*;
match self { match self {
Index(expression) => { Index(expression) => {
writer.write("[")?; writer.write("[")?;
@ -1173,7 +1193,7 @@ impl Unparse for ObjectNavigation {
impl Unparse for Literal { impl Unparse for Literal {
fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
use Literal::*; use crate::ast::node::literal::Literal::*;
match self { match self {
Integer(i) => writer.write(&format!("{}", i)), Integer(i) => writer.write(&format!("{}", i)),
Long(l) => writer.write(&format!("{}", l)), Long(l) => writer.write(&format!("{}", l)),
@ -1190,7 +1210,7 @@ impl Unparse for Literal {
impl Unparse for DString { impl Unparse for DString {
fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
writer.write("\"")?; writer.write("\"")?;
for part in &self.0 { for part in self.parts() {
part.unparse(writer)?; part.unparse(writer)?;
} }
writer.write("\"")?; writer.write("\"")?;

View File

@ -1,5 +1,22 @@
use crate::ast::named::Named; use crate::ast::node::call_expression::*;
use crate::ast::*; use crate::ast::node::class::*;
use crate::ast::node::closure::*;
use crate::ast::node::compilation_unit::*;
use crate::ast::node::d_string::*;
use crate::ast::node::expression::*;
use crate::ast::node::function::*;
use crate::ast::node::generics::*;
use crate::ast::node::implements_list::*;
use crate::ast::node::interface::*;
use crate::ast::node::level::*;
use crate::ast::node::literal::*;
use crate::ast::node::module::*;
use crate::ast::node::named::Named;
use crate::ast::node::names::*;
use crate::ast::node::object_access::*;
use crate::ast::node::statement::*;
use crate::ast::node::type_use::*;
use crate::ast::node::use_statement::*;
use crate::diagnostic::DmDiagnostic; use crate::diagnostic::DmDiagnostic;
use crate::name_analysis::fqn_context::FqnContext; use crate::name_analysis::fqn_context::FqnContext;
use crate::name_analysis::symbol::*; use crate::name_analysis::symbol::*;
@ -372,7 +389,7 @@ fn gather_module_level_declaration(
fqn_context: &mut FqnContext, fqn_context: &mut FqnContext,
diagnostics: &mut Vec<DmDiagnostic>, diagnostics: &mut Vec<DmDiagnostic>,
) { ) {
use ModuleLevelDeclaration::*; use crate::ast::node::level::ModuleLevelDeclaration::*;
match declaration { match declaration {
Module(module_declaration) => { Module(module_declaration) => {
gather_module_declaration(module_declaration, symbol_table, fqn_context, diagnostics); gather_module_declaration(module_declaration, symbol_table, fqn_context, diagnostics);
@ -408,7 +425,7 @@ fn gather_interface_level_declaration(
fqn_context: &mut FqnContext, fqn_context: &mut FqnContext,
diagnostics: &mut Vec<DmDiagnostic>, diagnostics: &mut Vec<DmDiagnostic>,
) { ) {
use InterfaceLevelDeclaration::*; use crate::ast::node::level::InterfaceLevelDeclaration::*;
match declaration { match declaration {
Module(module_declaration) => { Module(module_declaration) => {
gather_module_declaration(module_declaration, symbol_table, fqn_context, diagnostics); gather_module_declaration(module_declaration, symbol_table, fqn_context, diagnostics);
@ -449,7 +466,7 @@ fn gather_class_level_declaration(
fqn_context: &mut FqnContext, fqn_context: &mut FqnContext,
diagnostics: &mut Vec<DmDiagnostic>, diagnostics: &mut Vec<DmDiagnostic>,
) { ) {
use ClassLevelDeclaration::*; use crate::ast::node::level::ClassLevelDeclaration::*;
match declaration { match declaration {
Module(module_declaration) => { Module(module_declaration) => {
gather_module_declaration(module_declaration, symbol_table, fqn_context, diagnostics); gather_module_declaration(module_declaration, symbol_table, fqn_context, diagnostics);
@ -880,7 +897,7 @@ fn gather_function_body(
fqn_context: &mut FqnContext, fqn_context: &mut FqnContext,
diagnostics: &mut Vec<DmDiagnostic>, diagnostics: &mut Vec<DmDiagnostic>,
) { ) {
use crate::ast::FunctionBody::*; use crate::ast::node::function::FunctionBody::*;
match function_body { match function_body {
Equals(expression) => gather_expression(expression, symbol_table, diagnostics), Equals(expression) => gather_expression(expression, symbol_table, diagnostics),
Block(block) => gather_block_statement_inner(block, symbol_table, fqn_context, diagnostics), Block(block) => gather_block_statement_inner(block, symbol_table, fqn_context, diagnostics),
@ -994,7 +1011,7 @@ fn gather_statement(
fqn_context: &mut FqnContext, fqn_context: &mut FqnContext,
diagnostics: &mut Vec<DmDiagnostic>, diagnostics: &mut Vec<DmDiagnostic>,
) { ) {
use crate::ast::Statement::*; use crate::ast::node::statement::Statement::*;
match statement { match statement {
BlockStatement(block) => { BlockStatement(block) => {
gather_block_statement(block, symbol_table, fqn_context, diagnostics) gather_block_statement(block, symbol_table, fqn_context, diagnostics)
@ -1175,7 +1192,7 @@ fn gather_expression(
symbol_table: &mut SymbolTable, symbol_table: &mut SymbolTable,
diagnostics: &mut Vec<DmDiagnostic>, diagnostics: &mut Vec<DmDiagnostic>,
) { ) {
use crate::ast::Expression::*; use crate::ast::node::expression::Expression::*;
match expression { match expression {
Ternary(ternary_expression) => { Ternary(ternary_expression) => {
gather_ternary_expression(ternary_expression, symbol_table, diagnostics); gather_ternary_expression(ternary_expression, symbol_table, diagnostics);

View File

@ -5,7 +5,7 @@ There are two phases in name analysis.
## 1. Gather ## 1. Gather
The gather phases has three responsibilities: The gather phase has three responsibilities:
1. Add all declared symbols to the symbol table. 1. Add all declared symbols to the symbol table.
2. Set the `scope_id` property of all identifiers and fully-qualified-names. 2. Set the `scope_id` property of all identifiers and fully-qualified-names.
@ -19,8 +19,8 @@ The resolve phase has one main responsibility: resolve all references based on t
`scope_id` property. `scope_id` property.
*/ */
use crate::ast::named::Named; use crate::ast::node::compilation_unit::CompilationUnit;
use crate::ast::CompilationUnit; use crate::ast::node::named::Named;
use crate::diagnostic::DmDiagnostic; use crate::diagnostic::DmDiagnostic;
use crate::name_analysis::gather::gather_compilation_unit; use crate::name_analysis::gather::gather_compilation_unit;
use crate::name_analysis::resolve::resolve_compilation_unit; use crate::name_analysis::resolve::resolve_compilation_unit;
@ -196,7 +196,7 @@ mod tests {
let mut symbol_table = SymbolTable::new(); let mut symbol_table = SymbolTable::new();
assert_no_diagnostics(sources, &mut symbol_table); assert_no_diagnostics(sources, &mut symbol_table);
} }
#[test] #[test]
fn shadow_import() { fn shadow_import() {
let sources: HashMap<&str, &str> = HashMap::from([ let sources: HashMap<&str, &str> = HashMap::from([
@ -206,7 +206,7 @@ mod tests {
use greeter::Greeter; use greeter::Greeter;
class Greeter {} class Greeter {}
"} "},
), ),
( (
"greeter.dm", "greeter.dm",
@ -214,8 +214,8 @@ mod tests {
ns greeter; ns greeter;
class Greeter {} class Greeter {}
"} "},
) ),
]); ]);
assert_number_of_diagnostics(sources, &mut SymbolTable::new(), 1); assert_number_of_diagnostics(sources, &mut SymbolTable::new(), 1);
} }

View File

@ -1,12 +1,29 @@
use crate::ast::named::Named; use crate::ast::node::call_expression::*;
use crate::ast::*; use crate::ast::node::class::*;
use crate::ast::node::closure::*;
use crate::ast::node::compilation_unit::*;
use crate::ast::node::d_string::*;
use crate::ast::node::expression::*;
use crate::ast::node::function::*;
use crate::ast::node::generics::*;
use crate::ast::node::implements_list::*;
use crate::ast::node::interface::*;
use crate::ast::node::level::*;
use crate::ast::node::literal::*;
use crate::ast::node::module::*;
use crate::ast::node::names::*;
use crate::ast::node::object_access::*;
use crate::ast::node::statement::*;
use crate::ast::node::tuple_arguments::*;
use crate::ast::node::type_use::*;
use crate::ast::node::use_statement::*;
use crate::diagnostic::DmDiagnostic; use crate::diagnostic::DmDiagnostic;
use crate::name_analysis::symbol::Symbol; use crate::name_analysis::symbol::Symbol;
use crate::name_analysis::symbol_table::{SymbolLookupError, SymbolTable}; use crate::name_analysis::symbol_table::{SymbolLookupError, SymbolTable};
use codespan_reporting::diagnostic::{Diagnostic, Label}; use codespan_reporting::diagnostic::{Diagnostic, Label};
use std::ops::DerefMut; use std::ops::DerefMut;
use std::range::Range; use std::range::Range;
use crate::ast::node::named::Named;
/* Type Use */ /* Type Use */
fn resolve_type_use( fn resolve_type_use(
@ -282,7 +299,7 @@ fn resolve_module_level_declaration(
symbol_table: &mut SymbolTable, symbol_table: &mut SymbolTable,
diagnostics: &mut Vec<DmDiagnostic>, diagnostics: &mut Vec<DmDiagnostic>,
) { ) {
use crate::ast::ModuleLevelDeclaration::*; use crate::ast::node::level::ModuleLevelDeclaration::*;
match declaration { match declaration {
Module(module_declaration) => { Module(module_declaration) => {
resolve_module_declaration(module_declaration, symbol_table, diagnostics); resolve_module_declaration(module_declaration, symbol_table, diagnostics);
@ -309,7 +326,7 @@ fn resolve_interface_level_declaration(
symbol_table: &mut SymbolTable, symbol_table: &mut SymbolTable,
diagnostics: &mut Vec<DmDiagnostic>, diagnostics: &mut Vec<DmDiagnostic>,
) { ) {
use crate::ast::InterfaceLevelDeclaration::*; use crate::ast::node::level::InterfaceLevelDeclaration::*;
match declaration { match declaration {
Module(module_declaration) => { Module(module_declaration) => {
resolve_module_declaration(module_declaration, symbol_table, diagnostics); resolve_module_declaration(module_declaration, symbol_table, diagnostics);
@ -342,7 +359,7 @@ fn resolve_class_level_declaration(
symbol_table: &mut SymbolTable, symbol_table: &mut SymbolTable,
diagnostics: &mut Vec<DmDiagnostic>, diagnostics: &mut Vec<DmDiagnostic>,
) { ) {
use crate::ast::ClassLevelDeclaration::*; use crate::ast::node::level::ClassLevelDeclaration::*;
match declaration { match declaration {
Module(module_declaration) => { Module(module_declaration) => {
resolve_module_declaration(module_declaration, symbol_table, diagnostics); resolve_module_declaration(module_declaration, symbol_table, diagnostics);
@ -414,7 +431,11 @@ fn resolve_class_declaration(
if let Some(class_constructor) = class_declaration.class_constructor_mut() { if let Some(class_constructor) = class_declaration.class_constructor_mut() {
resolve_class_constructor(class_constructor, symbol_table, diagnostics); resolve_class_constructor(class_constructor, symbol_table, diagnostics);
} }
resolve_implements_list(class_declaration.implements_mut(), symbol_table, diagnostics); resolve_implements_list(
class_declaration.implements_mut(),
symbol_table,
diagnostics,
);
for declaration in class_declaration.declarations_mut() { for declaration in class_declaration.declarations_mut() {
resolve_class_level_declaration(declaration, symbol_table, diagnostics); resolve_class_level_declaration(declaration, symbol_table, diagnostics);
} }
@ -498,7 +519,7 @@ fn resolve_function_body(
symbol_table: &mut SymbolTable, symbol_table: &mut SymbolTable,
diagnostics: &mut Vec<DmDiagnostic>, diagnostics: &mut Vec<DmDiagnostic>,
) { ) {
use crate::ast::FunctionBody::*; use crate::ast::node::function::FunctionBody::*;
match function_body { match function_body {
Equals(expression) => resolve_expression(expression, symbol_table, diagnostics), Equals(expression) => resolve_expression(expression, symbol_table, diagnostics),
Block(block) => resolve_block_statement(block, symbol_table, diagnostics), Block(block) => resolve_block_statement(block, symbol_table, diagnostics),
@ -521,7 +542,7 @@ fn resolve_class_constructor(
symbol_table: &mut SymbolTable, symbol_table: &mut SymbolTable,
diagnostics: &mut Vec<DmDiagnostic>, diagnostics: &mut Vec<DmDiagnostic>,
) { ) {
use crate::ast::ClassConstructorParameter::*; use crate::ast::node::class::ClassConstructorParameter::*;
for parameter in class_constructor.parameters_mut() { for parameter in class_constructor.parameters_mut() {
match parameter { match parameter {
Property(property) => resolve_property_declaration(property, symbol_table, diagnostics), Property(property) => resolve_property_declaration(property, symbol_table, diagnostics),
@ -574,7 +595,7 @@ fn resolve_statement(
symbol_table: &mut SymbolTable, symbol_table: &mut SymbolTable,
diagnostics: &mut Vec<DmDiagnostic>, diagnostics: &mut Vec<DmDiagnostic>,
) { ) {
use crate::ast::Statement::*; use crate::ast::node::statement::Statement::*;
match statement { match statement {
BlockStatement(block) => resolve_block_statement(block, symbol_table, diagnostics), BlockStatement(block) => resolve_block_statement(block, symbol_table, diagnostics),
VariableDeclarationStatement(variable_declaration) => { VariableDeclarationStatement(variable_declaration) => {
@ -658,7 +679,7 @@ fn resolve_expression(
symbol_table: &mut SymbolTable, symbol_table: &mut SymbolTable,
diagnostics: &mut Vec<DmDiagnostic>, diagnostics: &mut Vec<DmDiagnostic>,
) { ) {
use crate::ast::Expression::*; use crate::ast::node::expression::Expression::*;
match expression { match expression {
Ternary(ternary_expression) => { Ternary(ternary_expression) => {
resolve_ternary_expression(ternary_expression, symbol_table, diagnostics); resolve_ternary_expression(ternary_expression, symbol_table, diagnostics);
@ -741,7 +762,11 @@ fn resolve_ternary_expression(
symbol_table: &mut SymbolTable, symbol_table: &mut SymbolTable,
diagnostics: &mut Vec<DmDiagnostic>, diagnostics: &mut Vec<DmDiagnostic>,
) { ) {
resolve_expression(ternary_expression.condition_mut(), symbol_table, diagnostics); resolve_expression(
ternary_expression.condition_mut(),
symbol_table,
diagnostics,
);
resolve_expression( resolve_expression(
ternary_expression.true_expression_mut(), ternary_expression.true_expression_mut(),
symbol_table, symbol_table,
@ -768,7 +793,11 @@ fn resolve_prefix_expression(
symbol_table: &mut SymbolTable, symbol_table: &mut SymbolTable,
diagnostics: &mut Vec<DmDiagnostic>, diagnostics: &mut Vec<DmDiagnostic>,
) { ) {
resolve_expression(prefix_expression.expression_mut(), symbol_table, diagnostics); resolve_expression(
prefix_expression.expression_mut(),
symbol_table,
diagnostics,
);
} }
fn resolve_suffix_expression( fn resolve_suffix_expression(
@ -776,7 +805,11 @@ fn resolve_suffix_expression(
symbol_table: &mut SymbolTable, symbol_table: &mut SymbolTable,
diagnostics: &mut Vec<DmDiagnostic>, diagnostics: &mut Vec<DmDiagnostic>,
) { ) {
resolve_expression(suffix_expression.expression_mut(), symbol_table, diagnostics); resolve_expression(
suffix_expression.expression_mut(),
symbol_table,
diagnostics,
);
} }
fn resolve_call_expression( fn resolve_call_expression(

View File

@ -1,5 +1,6 @@
use crate::ast::named::Named; use crate::ast::node::named::Named;
use crate::ast::{Identifier, UseStatement}; use crate::ast::node::names::Identifier;
use crate::ast::node::use_statement::UseStatement;
use std::cell::RefCell; use std::cell::RefCell;
use std::fmt::{Debug, Display, Formatter}; use std::fmt::{Debug, Display, Formatter};
use std::ops::Deref; use std::ops::Deref;
@ -97,11 +98,7 @@ pub struct UseStatementSymbol {
} }
impl UseStatementSymbol { impl UseStatementSymbol {
pub fn new( pub fn new(fqn: &str, declared_name: &str, identifier: Option<&Identifier>) -> Self {
fqn: &str,
declared_name: &str,
identifier: Option<&Identifier>,
) -> Self {
UseStatementSymbol { UseStatementSymbol {
fqn: fqn.to_string(), fqn: fqn.to_string(),
declared_name: declared_name.to_string(), declared_name: declared_name.to_string(),