Skip to content
105 changes: 64 additions & 41 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ pub struct KotlinFile {

#[derive(Debug, PartialEq, Clone)]
pub struct Package {
pub modifiers: Vec<Modifier>,
pub names: Vec<String>,
pub path: Path,
}

#[derive(Debug, PartialEq, Clone)]
pub struct Import {
pub names: Vec<String>,
pub path: Path,
pub is_wildcard: bool,
pub alias: Option<String>,
}
Expand Down Expand Up @@ -46,12 +45,12 @@ pub enum DeclarationKind {
pub struct EntityDeclaration {
pub modifiers: Vec<Modifier>,
pub kind: EntityDeclarationKind,
pub name: String,
pub type_params: Vec<TypeParam>,
pub name: Option<String>,
pub type_params: Vec<BoundedTypeParam>,
pub primary_constructor: Option<PrimaryConstructorDeclaration>,
pub constructors: Vec<ConstructorDeclaration>,
pub extends: Vec<Type>,
pub bounds: Vec<TypeBound>,
pub inner: Vec<Declaration>,
pub body: Option<Block>,
}

#[derive(Debug, PartialEq, Clone)]
Expand All @@ -61,13 +60,12 @@ pub enum EntityDeclarationKind {
Object,
CompanionObject,
Enum,
ObjectInstance,
}

#[derive(Debug, PartialEq, Clone)]
pub struct PrimaryConstructorDeclaration {
pub modifiers: Vec<Modifier>,
pub params: Vec<Param>,
pub params: Vec<ConstructorParam>,
}

#[derive(Debug, PartialEq, Clone)]
Expand Down Expand Up @@ -114,7 +112,7 @@ pub struct PropertyDeclaration {
pub is_mutable: bool,
pub is_delegated: bool,
pub type_params: Vec<TypeParam>,
pub vars: Tuple,
pub vars: Vars,
pub receiver: Option<Type>,
pub bounds: Vec<TypeBound>,
pub init: Option<Expression>,
Expand All @@ -132,7 +130,8 @@ pub enum PropertyAccessor {
Setter {
annotations: Vec<AnnotationSet>,
modifiers: Vec<Modifier>,
field: PropertySetterField,
field: Option<PropertySetterField>,
return_ty: Option<Type>,
body: Option<Block>,
},
}
Expand All @@ -141,8 +140,6 @@ pub enum PropertyAccessor {
pub struct PropertySetterField {
pub name: String,
pub ty: Option<Type>,
pub return_ty: Option<Type>,
pub body: Option<Block>,
}

#[derive(Debug, PartialEq, Clone)]
Expand All @@ -158,13 +155,13 @@ pub struct EnumEntryDeclaration {
pub modifiers: Vec<Modifier>,
pub name: String,
pub args: Vec<CallArg>,
pub inner: Vec<Declaration>,
pub body: Vec<Declaration>,
}

#[derive(Debug, PartialEq, Clone)]
pub enum Expression {
Literal(Literal),
ArrayAccess(ArrayAccessExpression),
Bracket(BracketExpression),
BinaryOp(BinaryOperation),
Break(BreakExpression),
Call(CallExpression),
Expand All @@ -175,7 +172,7 @@ pub enum Expression {
Labeled(LabeledExpression),
Object(ObjectExpression),
Parenthesized(ParenthesizedExpression),
PropertyReference(PropertyReferenceExpression),
MemberReference(MemberReferenceExpression),
Reference(ReferenceExpression),
Return(ReturnExpression),
StringTemplate(StringTemplateExpression),
Expand All @@ -202,21 +199,21 @@ pub enum Literal {
#[derive(Debug, PartialEq, Clone)]
pub struct IfExpression {
pub expr: Box<Expression>,
pub then: Box<Expression>,
pub then: Block,
pub otherwise: Option<Box<Expression>>,
}

#[derive(Debug, PartialEq, Clone)]
pub struct ForExpression {
pub vars: Tuple,
pub vars: Vars,
pub iterable: Box<Expression>,
pub body: Box<Expression>,
pub body: Block,
}

#[derive(Debug, PartialEq, Clone)]
pub struct WhileExpression {
pub expr: Box<Expression>,
pub body: Box<Expression>,
pub body: Block,
pub is_do_while: bool,
}

Expand Down Expand Up @@ -319,14 +316,14 @@ pub struct WhenExpression {
#[derive(Debug, PartialEq, Clone)]
pub struct WhenEntry {
pub exprs: Vec<Expression>,
pub body: Box<Expression>,
pub body: Block,
}

#[derive(Debug, PartialEq, Clone)]
pub struct ObjectExpression {
pub annotations: Vec<AnnotationSet>,
pub extends: Vec<EntityDeclaration>,
pub inner: Vec<Declaration>,
pub extends: Vec<Type>,
pub body: Block,
}

#[derive(Debug, PartialEq, Clone)]
Expand Down Expand Up @@ -357,7 +354,7 @@ pub struct BreakExpression {

#[derive(Debug, PartialEq, Clone)]
pub struct ReferenceExpression {
pub parts: Vec<String>,
pub path: Path,
}

#[derive(Debug, PartialEq, Clone)]
Expand All @@ -368,27 +365,26 @@ pub struct LabeledExpression {

#[derive(Debug, PartialEq, Clone)]
pub struct CallExpression {
pub expr: Box<Expression>,
pub path: Path,
pub args: Vec<CallArg>,
pub type_args: Vec<Type>,
pub lambda: Option<Box<Expression>>,
pub lambda: Option<Box<LambdaBlock>>,
}

#[derive(Debug, PartialEq, Clone)]
pub struct LambdaBlock {
pub label: Option<String>,
pub vars: Tuple,
pub vars: Option<Vars>,
pub body: Option<Block>,
}

#[derive(Debug, PartialEq, Clone)]
pub struct ArrayAccessExpression {
pub struct BracketExpression {
pub expr: Box<Expression>,
pub index: Vec<Expression>,
}

#[derive(Debug, PartialEq, Clone)]
pub struct PropertyReferenceExpression {
pub struct MemberReferenceExpression {
pub lhs: Option<Box<Expression>>,
pub rhs: Box<Expression>,
}
Expand All @@ -407,7 +403,7 @@ pub enum Type {

#[derive(Debug, PartialEq, Clone)]
pub struct SimpleType {
pub name: Option<String>,
pub name: String,
pub type_args: Vec<Type>,
pub is_nullable: bool,
}
Expand All @@ -433,24 +429,43 @@ pub struct Param {
pub ty: Type,
}

#[derive(Debug, PartialEq, Clone)]
pub struct ConstructorParam {
pub modifiers: Vec<Modifier>,
pub property_type: Option<PropertyType>,
pub param: Param,
}

#[derive(Debug, PartialEq, Clone)]
pub enum PropertyType {
Var,
Val,
}

#[derive(Debug, PartialEq, Clone)]
pub struct TypeParam {
pub annotations: Vec<AnnotationSet>,
pub name: String,
pub ty: Option<Type>,
}

#[derive(Debug, PartialEq, Clone)]
pub struct BoundedTypeParam {
pub annotations: Vec<AnnotationSet>,
pub bounds: Vec<TypeBound>,
}

#[derive(Debug, PartialEq, Clone)]
pub struct TypeBound {
pub ty: Type,
pub kind: BoundKind,
pub name: String,
pub ty: Option<Type>,
pub kind: Option<BoundKind>,
}

#[derive(Debug, PartialEq, Clone)]
pub enum BoundKind {
Unconstrained,
Covariant,
Contravariant,
In,
Out,
}

#[derive(Debug, PartialEq, Clone)]
Expand All @@ -461,8 +476,8 @@ pub struct AnnotationSet {

#[derive(Debug, PartialEq, Clone)]
pub struct Annotation {
pub parts: Vec<String>,
pub args: Vec<CallArg>,
pub path: Path,
pub args: Vec<InvocationArg>,
}

#[derive(Debug, PartialEq, Clone)]
Expand All @@ -473,13 +488,19 @@ pub struct CallArg {
}

#[derive(Debug, PartialEq, Clone)]
pub struct Tuple {
pub struct InvocationArg {
pub name: Option<String>,
pub value: Box<Expression>,
}

#[derive(Debug, PartialEq, Clone)]
pub struct Vars {
pub is_destructured: bool,
pub vars: Vec<VarDefinition>,
pub vars: Vec<Var>,
}

#[derive(Debug, PartialEq, Clone)]
pub struct VarDefinition {
pub struct Var {
pub name: String,
pub ty: Option<Type>,
}
Expand All @@ -496,6 +517,8 @@ pub enum AnnotationSite {
Delegate,
}

pub type Path = Vec<String>;

#[derive(Debug, PartialEq, Clone)]
pub enum Modifier {
Abstract,
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub mod ast;
// mod parse;
pub mod parse;
pub mod parser;

// pub use parser::parser;
58 changes: 58 additions & 0 deletions src/parse/declaration/annotation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use crate::{
ast::*,
parse::expression::{
call::invocation_args_parser, path::path_parser,
},
};
use chumsky::prelude::*;

pub fn annotations_parser(
expr_parser: impl Parser<char, Expression, Error = Simple<char>>,
) -> impl Parser<char, AnnotationSet, Error = Simple<char>> {
just('@')
.ignore_then(annotation_site_parser().then_ignore(just(':')).or_not())
.then(annotation_parser(expr_parser).repeated())
.map(|(site, annotations)| AnnotationSet { site, annotations })
}

pub fn file_annotations_parser(
expr_parser: impl Parser<char, Expression, Error = Simple<char>>,
) -> impl Parser<char, AnnotationSet, Error = Simple<char>> {
just('@')
.ignore_then(just("file:"))
.ignore_then(annotation_parser(expr_parser).repeated())
.map(|annotations| AnnotationSet {
site: None,
annotations,
})
}

pub fn annotation_site_parser(
) -> impl Parser<char, AnnotationSite, Error = Simple<char>> {
choice((
just("field").to(AnnotationSite::Field),
just("property").to(AnnotationSite::Property),
just("get").to(AnnotationSite::Get),
just("set").to(AnnotationSite::Set),
just("receiver").to(AnnotationSite::Receiver),
just("param").to(AnnotationSite::Param),
just("setparam").to(AnnotationSite::SetParam),
just("delegate").to(AnnotationSite::Delegate),
))
}

pub fn annotation_parser(
expr_parser: impl Parser<char, Expression, Error = Simple<char>>,
) -> impl Parser<char, Annotation, Error = Simple<char>> {
path_parser()
.then(
just('(')
.ignore_then(invocation_args_parser(expr_parser))
.then_ignore(just(')'))
.or_not(),
)
.map(|(path, args)| Annotation {
path,
args: args.unwrap_or_default(),
})
}
Loading