Run rustfmt on ast module
This commit is contained in:
parent
765a0bec58
commit
5d04a020dc
@ -1,18 +1,22 @@
|
||||
#![allow(clippy::upper_case_acronyms)]
|
||||
#![allow(clippy::enum_variant_names)]
|
||||
|
||||
use std::rc::Rc;
|
||||
use std::convert::{AsRef, From};
|
||||
use std::{
|
||||
convert::{AsRef, From},
|
||||
rc::Rc,
|
||||
};
|
||||
|
||||
mod visitor;
|
||||
mod operators;
|
||||
mod visitor;
|
||||
|
||||
pub use operators::{PrefixOp, BinOp};
|
||||
pub use operators::{BinOp, PrefixOp};
|
||||
pub use visitor::*;
|
||||
|
||||
use crate::derivative::Derivative;
|
||||
use crate::tokenizing::Location;
|
||||
use crate::identifier::{Id, define_id_kind};
|
||||
use crate::{
|
||||
derivative::Derivative,
|
||||
identifier::{define_id_kind, Id},
|
||||
tokenizing::Location,
|
||||
};
|
||||
|
||||
define_id_kind!(ASTItem);
|
||||
|
||||
@ -29,7 +33,7 @@ pub type ItemId = Id<ASTItem>;
|
||||
#[derive(Derivative, Debug)]
|
||||
#[derivative(PartialEq)]
|
||||
pub struct AST {
|
||||
#[derivative(PartialEq="ignore")]
|
||||
#[derivative(PartialEq = "ignore")]
|
||||
pub id: ItemId,
|
||||
pub statements: Block,
|
||||
}
|
||||
@ -37,9 +41,9 @@ pub struct AST {
|
||||
#[derive(Derivative, Debug, Clone)]
|
||||
#[derivative(PartialEq)]
|
||||
pub struct Statement {
|
||||
#[derivative(PartialEq="ignore")]
|
||||
#[derivative(PartialEq = "ignore")]
|
||||
pub id: ItemId,
|
||||
#[derivative(PartialEq="ignore")]
|
||||
#[derivative(PartialEq = "ignore")]
|
||||
pub location: Location,
|
||||
pub kind: StatementKind,
|
||||
}
|
||||
@ -54,7 +58,7 @@ pub enum StatementKind {
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Default)]
|
||||
pub struct Block {
|
||||
pub statements: Vec<Statement>
|
||||
pub statements: Vec<Statement>,
|
||||
}
|
||||
|
||||
impl From<Vec<Statement>> for Block {
|
||||
@ -80,7 +84,7 @@ pub type ParamName = Rc<String>;
|
||||
#[derive(Debug, Derivative, Clone)]
|
||||
#[derivative(PartialEq)]
|
||||
pub struct QualifiedName {
|
||||
#[derivative(PartialEq="ignore")]
|
||||
#[derivative(PartialEq = "ignore")]
|
||||
pub id: ItemId,
|
||||
pub components: Vec<Rc<String>>,
|
||||
}
|
||||
@ -89,42 +93,20 @@ pub struct QualifiedName {
|
||||
pub struct FormalParam {
|
||||
pub name: ParamName,
|
||||
pub default: Option<Expression>,
|
||||
pub anno: Option<TypeIdentifier>
|
||||
pub anno: Option<TypeIdentifier>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub enum Declaration {
|
||||
FuncSig(Signature),
|
||||
FuncDecl(Signature, Block),
|
||||
TypeDecl {
|
||||
name: TypeSingletonName,
|
||||
body: TypeBody,
|
||||
mutable: bool
|
||||
},
|
||||
TypeDecl { name: TypeSingletonName, body: TypeBody, mutable: bool },
|
||||
//TODO this needs to be more sophisticated
|
||||
TypeAlias {
|
||||
alias: Rc<String>,
|
||||
original: Rc<String>,
|
||||
},
|
||||
Binding {
|
||||
name: Rc<String>,
|
||||
constant: bool,
|
||||
type_anno: Option<TypeIdentifier>,
|
||||
expr: Expression,
|
||||
},
|
||||
Impl {
|
||||
type_name: TypeIdentifier,
|
||||
interface_name: Option<TypeSingletonName>,
|
||||
block: Vec<Declaration>,
|
||||
},
|
||||
Interface {
|
||||
name: Rc<String>,
|
||||
signatures: Vec<Signature>
|
||||
},
|
||||
Annotation {
|
||||
name: Rc<String>,
|
||||
arguments: Vec<Expression>
|
||||
}
|
||||
TypeAlias { alias: Rc<String>, original: Rc<String> },
|
||||
Binding { name: Rc<String>, constant: bool, type_anno: Option<TypeIdentifier>, expr: Expression },
|
||||
Impl { type_name: TypeIdentifier, interface_name: Option<TypeSingletonName>, block: Vec<Declaration> },
|
||||
Interface { name: Rc<String>, signatures: Vec<Signature> },
|
||||
Annotation { name: Rc<String>, arguments: Vec<Expression> },
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
@ -141,26 +123,26 @@ pub struct TypeBody(pub Vec<Variant>);
|
||||
#[derive(Debug, Derivative, Clone)]
|
||||
#[derivative(PartialEq)]
|
||||
pub struct Variant {
|
||||
#[derivative(PartialEq="ignore")]
|
||||
#[derivative(PartialEq = "ignore")]
|
||||
pub id: ItemId,
|
||||
pub name: Rc<String>,
|
||||
pub kind: VariantKind,
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub enum VariantKind {
|
||||
UnitStruct,
|
||||
TupleStruct(Vec<TypeIdentifier>),
|
||||
Record(Vec<(Rc<String>, TypeIdentifier)>),
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Derivative, Clone)]
|
||||
#[derivative(PartialEq)]
|
||||
pub struct Expression {
|
||||
#[derivative(PartialEq="ignore")]
|
||||
#[derivative(PartialEq = "ignore")]
|
||||
pub id: ItemId,
|
||||
pub kind: ExpressionKind,
|
||||
pub type_anno: Option<TypeIdentifier>
|
||||
pub type_anno: Option<TypeIdentifier>,
|
||||
}
|
||||
|
||||
impl Expression {
|
||||
@ -177,7 +159,7 @@ impl Expression {
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub enum TypeIdentifier {
|
||||
Tuple(Vec<TypeIdentifier>),
|
||||
Singleton(TypeSingletonName)
|
||||
Singleton(TypeSingletonName),
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
@ -196,60 +178,28 @@ pub enum ExpressionKind {
|
||||
PrefixExp(PrefixOp, Box<Expression>),
|
||||
TupleLiteral(Vec<Expression>),
|
||||
Value(QualifiedName),
|
||||
NamedStruct {
|
||||
name: QualifiedName,
|
||||
fields: Vec<(Rc<String>, Expression)>,
|
||||
},
|
||||
Call {
|
||||
f: Box<Expression>,
|
||||
arguments: Vec<InvocationArgument>,
|
||||
},
|
||||
Index {
|
||||
indexee: Box<Expression>,
|
||||
indexers: Vec<Expression>,
|
||||
},
|
||||
IfExpression {
|
||||
discriminator: Option<Box<Expression>>,
|
||||
body: Box<IfExpressionBody>,
|
||||
},
|
||||
WhileExpression {
|
||||
condition: Option<Box<Expression>>,
|
||||
body: Block,
|
||||
},
|
||||
ForExpression {
|
||||
enumerators: Vec<Enumerator>,
|
||||
body: Box<ForBody>,
|
||||
},
|
||||
Lambda {
|
||||
params: Vec<FormalParam>,
|
||||
type_anno: Option<TypeIdentifier>,
|
||||
body: Block,
|
||||
},
|
||||
NamedStruct { name: QualifiedName, fields: Vec<(Rc<String>, Expression)> },
|
||||
Call { f: Box<Expression>, arguments: Vec<InvocationArgument> },
|
||||
Index { indexee: Box<Expression>, indexers: Vec<Expression> },
|
||||
IfExpression { discriminator: Option<Box<Expression>>, body: Box<IfExpressionBody> },
|
||||
WhileExpression { condition: Option<Box<Expression>>, body: Block },
|
||||
ForExpression { enumerators: Vec<Enumerator>, body: Box<ForBody> },
|
||||
Lambda { params: Vec<FormalParam>, type_anno: Option<TypeIdentifier>, body: Block },
|
||||
ListLiteral(Vec<Expression>),
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub enum InvocationArgument {
|
||||
Positional(Expression),
|
||||
Keyword {
|
||||
name: Rc<String>,
|
||||
expr: Expression,
|
||||
},
|
||||
Ignored
|
||||
Keyword { name: Rc<String>, expr: Expression },
|
||||
Ignored,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub enum IfExpressionBody {
|
||||
SimpleConditional {
|
||||
then_case: Block,
|
||||
else_case: Option<Block>
|
||||
},
|
||||
SimplePatternMatch {
|
||||
pattern: Pattern,
|
||||
then_case: Block,
|
||||
else_case: Option<Block>
|
||||
},
|
||||
CondList(Vec<ConditionArm>)
|
||||
SimpleConditional { then_case: Block, else_case: Option<Block> },
|
||||
SimplePatternMatch { pattern: Pattern, then_case: Block, else_case: Option<Block> },
|
||||
CondList(Vec<ConditionArm>),
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
@ -279,10 +229,7 @@ pub enum Pattern {
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub enum PatternLiteral {
|
||||
NumPattern {
|
||||
neg: bool,
|
||||
num: ExpressionKind,
|
||||
},
|
||||
NumPattern { neg: bool, num: ExpressionKind },
|
||||
StringPattern(Rc<String>),
|
||||
BoolPattern(bool),
|
||||
}
|
||||
@ -302,23 +249,21 @@ pub enum ForBody {
|
||||
#[derive(Debug, Derivative, Clone)]
|
||||
#[derivative(PartialEq)]
|
||||
pub struct ImportSpecifier {
|
||||
#[derivative(PartialEq="ignore")]
|
||||
#[derivative(PartialEq = "ignore")]
|
||||
pub id: ItemId,
|
||||
pub path_components: Vec<Rc<String>>,
|
||||
pub imported_names: ImportedNames
|
||||
pub imported_names: ImportedNames,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub enum ImportedNames {
|
||||
All,
|
||||
LastOfPath,
|
||||
List(Vec<Rc<String>>)
|
||||
List(Vec<Rc<String>>),
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct ModuleSpecifier {
|
||||
pub name: Rc<String>,
|
||||
pub contents: Block,
|
||||
}
|
||||
|
||||
|
@ -49,7 +49,7 @@ impl BinOp {
|
||||
}
|
||||
}
|
||||
|
||||
fn token_kind_to_sigil(tok: & TokenKind) -> Option<&str> {
|
||||
fn token_kind_to_sigil(tok: &TokenKind) -> Option<&str> {
|
||||
use self::TokenKind::*;
|
||||
Some(match tok {
|
||||
Operator(op) => op.as_str(),
|
||||
@ -59,7 +59,7 @@ fn token_kind_to_sigil(tok: & TokenKind) -> Option<&str> {
|
||||
LAngleBracket => "<",
|
||||
RAngleBracket => ">",
|
||||
Equals => "=",
|
||||
_ => return None
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -3,17 +3,27 @@ use crate::ast::*;
|
||||
#[derive(Debug)]
|
||||
pub enum Recursion {
|
||||
Continue,
|
||||
Stop
|
||||
Stop,
|
||||
}
|
||||
|
||||
pub trait ASTVisitor: Sized {
|
||||
fn expression(&mut self, _expression: &Expression) -> Recursion { Recursion::Continue }
|
||||
fn declaration(&mut self, _declaration: &Declaration, _id: &ItemId) -> Recursion { Recursion::Continue }
|
||||
fn expression(&mut self, _expression: &Expression) -> Recursion {
|
||||
Recursion::Continue
|
||||
}
|
||||
fn declaration(&mut self, _declaration: &Declaration, _id: &ItemId) -> Recursion {
|
||||
Recursion::Continue
|
||||
}
|
||||
|
||||
fn import(&mut self, _import: &ImportSpecifier) -> Recursion { Recursion::Continue }
|
||||
fn module(&mut self, _module: &ModuleSpecifier) -> Recursion { Recursion::Continue }
|
||||
fn import(&mut self, _import: &ImportSpecifier) -> Recursion {
|
||||
Recursion::Continue
|
||||
}
|
||||
fn module(&mut self, _module: &ModuleSpecifier) -> Recursion {
|
||||
Recursion::Continue
|
||||
}
|
||||
|
||||
fn pattern(&mut self, _pat: &Pattern) -> Recursion { Recursion::Continue }
|
||||
fn pattern(&mut self, _pat: &Pattern) -> Recursion {
|
||||
Recursion::Continue
|
||||
}
|
||||
}
|
||||
|
||||
pub fn walk_ast<V: ASTVisitor>(v: &mut V, ast: &AST) {
|
||||
@ -32,12 +42,11 @@ pub fn walk_block<V: ASTVisitor>(v: &mut V, block: &Block) {
|
||||
}
|
||||
Import(ref import_spec) => {
|
||||
v.import(import_spec);
|
||||
},
|
||||
Module(ref module_spec) => {
|
||||
}
|
||||
Module(ref module_spec) =>
|
||||
if let Recursion::Continue = v.module(module_spec) {
|
||||
walk_block(v, &module_spec.contents);
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -50,12 +59,7 @@ pub fn walk_declaration<V: ASTVisitor>(v: &mut V, decl: &Declaration, id: &ItemI
|
||||
FuncDecl(_sig, block) => {
|
||||
walk_block(v, block);
|
||||
}
|
||||
Binding {
|
||||
name: _,
|
||||
constant: _,
|
||||
type_anno: _,
|
||||
expr,
|
||||
} => {
|
||||
Binding { name: _, constant: _, type_anno: _, expr } => {
|
||||
walk_expression(v, expr);
|
||||
}
|
||||
_ => (),
|
||||
@ -76,16 +80,14 @@ pub fn walk_expression<V: ASTVisitor>(v: &mut V, expr: &Expression) {
|
||||
PrefixExp(_, arg) => {
|
||||
walk_expression(v, arg);
|
||||
}
|
||||
TupleLiteral(exprs) => {
|
||||
TupleLiteral(exprs) =>
|
||||
for expr in exprs {
|
||||
walk_expression(v, expr);
|
||||
}
|
||||
}
|
||||
NamedStruct { name: _, fields } => {
|
||||
},
|
||||
NamedStruct { name: _, fields } =>
|
||||
for (_, expr) in fields.iter() {
|
||||
walk_expression(v, expr);
|
||||
}
|
||||
}
|
||||
},
|
||||
Call { f, arguments } => {
|
||||
walk_expression(v, f);
|
||||
for arg in arguments.iter() {
|
||||
@ -102,10 +104,7 @@ pub fn walk_expression<V: ASTVisitor>(v: &mut V, expr: &Expression) {
|
||||
walk_expression(v, indexer);
|
||||
}
|
||||
}
|
||||
IfExpression {
|
||||
discriminator,
|
||||
body,
|
||||
} => {
|
||||
IfExpression { discriminator, body } => {
|
||||
if let Some(d) = discriminator.as_ref() {
|
||||
walk_expression(v, d);
|
||||
}
|
||||
@ -126,18 +125,13 @@ pub fn walk_expression<V: ASTVisitor>(v: &mut V, expr: &Expression) {
|
||||
ForBody::StatementBlock(block) => walk_block(v, block),
|
||||
};
|
||||
}
|
||||
Lambda {
|
||||
params: _,
|
||||
type_anno: _,
|
||||
body,
|
||||
} => {
|
||||
Lambda { params: _, type_anno: _, body } => {
|
||||
walk_block(v, body);
|
||||
}
|
||||
ListLiteral(exprs) => {
|
||||
ListLiteral(exprs) =>
|
||||
for expr in exprs {
|
||||
walk_expression(v, expr);
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
@ -146,27 +140,20 @@ pub fn walk_if_expr_body<V: ASTVisitor>(v: &mut V, body: &IfExpressionBody) {
|
||||
use IfExpressionBody::*;
|
||||
|
||||
match body {
|
||||
SimpleConditional {
|
||||
then_case,
|
||||
else_case,
|
||||
} => {
|
||||
SimpleConditional { then_case, else_case } => {
|
||||
walk_block(v, then_case);
|
||||
if let Some(block) = else_case.as_ref() {
|
||||
walk_block(v, block)
|
||||
}
|
||||
}
|
||||
SimplePatternMatch {
|
||||
pattern,
|
||||
then_case,
|
||||
else_case,
|
||||
} => {
|
||||
SimplePatternMatch { pattern, then_case, else_case } => {
|
||||
walk_pattern(v, pattern);
|
||||
walk_block(v, then_case);
|
||||
if let Some(block) = else_case.as_ref() {
|
||||
walk_block(v, block)
|
||||
}
|
||||
}
|
||||
CondList(arms) => {
|
||||
CondList(arms) =>
|
||||
for arm in arms {
|
||||
match arm.condition {
|
||||
Condition::Pattern(ref pat) => {
|
||||
@ -184,8 +171,7 @@ pub fn walk_if_expr_body<V: ASTVisitor>(v: &mut V, body: &IfExpressionBody) {
|
||||
walk_expression(v, guard);
|
||||
}
|
||||
walk_block(v, &arm.body);
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@ -194,21 +180,18 @@ pub fn walk_pattern<V: ASTVisitor>(v: &mut V, pat: &Pattern) {
|
||||
|
||||
if let Recursion::Continue = v.pattern(pat) {
|
||||
match pat {
|
||||
TuplePattern(patterns) => {
|
||||
TuplePattern(patterns) =>
|
||||
for pat in patterns {
|
||||
walk_pattern(v, pat);
|
||||
}
|
||||
}
|
||||
TupleStruct(_, patterns) => {
|
||||
},
|
||||
TupleStruct(_, patterns) =>
|
||||
for pat in patterns {
|
||||
walk_pattern(v, pat);
|
||||
}
|
||||
}
|
||||
Record(_, name_and_patterns) => {
|
||||
},
|
||||
Record(_, name_and_patterns) =>
|
||||
for (_, pat) in name_and_patterns {
|
||||
walk_pattern(v, pat);
|
||||
}
|
||||
}
|
||||
},
|
||||
_ => (),
|
||||
};
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user