Skip to content

Commit

Permalink
Better metadata representation and parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
werediver committed Sep 2, 2023
1 parent 0bf39f7 commit b0ef107
Show file tree
Hide file tree
Showing 16 changed files with 337 additions and 270 deletions.
4 changes: 2 additions & 2 deletions dart-parser/src/dart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod extension;
pub mod func_call;
pub mod func_like;
pub mod maybe_required;
pub mod meta;
pub mod ty;
pub mod type_param;
pub mod typedef;
Expand All @@ -23,16 +24,15 @@ pub use extension::Extension;
pub use func_call::FuncCall;
pub use func_like::FuncLike;
pub use maybe_required::MaybeRequired;
pub use meta::WithMeta;
pub use ty::NotFuncType;
pub use type_param::TypeParam;
pub use typedef::TypeDef;
pub use var::Var;

#[derive(PartialEq, Eq, Debug)]
pub enum Dart<'s> {
Comment(Comment<'s>),
Directive(Directive<'s>),
Annotation(Annotation<'s>),
TypeDef(TypeDef<'s>),
Var(Var<'s>),
FuncLike(FuncLike<'s>),
Expand Down
2 changes: 2 additions & 0 deletions dart-parser/src/dart/annotation.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use super::FuncCall;

/// An annotation must precede a declaration.
#[derive(PartialEq, Eq, Debug)]
pub enum Annotation<'s> {
Ident(&'s str),
/// Type arguments are not allowed in annotations.
FuncCall(FuncCall<'s>),
}
8 changes: 3 additions & 5 deletions dart-parser/src/dart/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use tiny_set::with_tiny_set;

use super::{
func_like::{FuncBodyContent, FuncParam, FuncParams},
Annotation, Comment, FuncLike, NotFuncType, TypeParam, Var,
FuncLike, NotFuncType, TypeParam, Var, WithMeta,
};

#[derive(PartialEq, Eq, Debug)]
Expand All @@ -16,7 +16,7 @@ pub struct Class<'s> {
pub with: Vec<NotFuncType<'s>>,
/// Interfaces.
pub implements: Vec<NotFuncType<'s>>,
pub body: Vec<ClassMember<'s>>,
pub body: Vec<WithMeta<'s, ClassMember<'s>>>,
}

/// The possible combinations are:
Expand All @@ -42,8 +42,6 @@ pub enum ClassModifier {

#[derive(PartialEq, Eq, Debug)]
pub enum ClassMember<'s> {
Comment(Comment<'s>),
Annotation(Annotation<'s>),
Constructor(Constructor<'s>),
Var(Var<'s>),
FuncLike(FuncLike<'s>),
Expand All @@ -53,7 +51,7 @@ pub enum ClassMember<'s> {
pub struct Constructor<'s> {
pub modifier: Option<ConstructorModifier>,
pub name: &'s str,
pub params: FuncParams<FuncParam<'s>>,
pub params: FuncParams<'s, FuncParam<'s>>,
pub body: Option<FuncBodyContent<'s>>,
}

Expand Down
13 changes: 3 additions & 10 deletions dart-parser/src/dart/enum_ty.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
use super::{class::ClassMember, func_call::FuncArg, Annotation, Comment, NotFuncType};
use super::{class::ClassMember, func_call::FuncArg, NotFuncType, WithMeta};

#[derive(PartialEq, Eq, Debug)]
pub struct EnumTy<'s> {
pub name: &'s str,
pub implements: Vec<NotFuncType<'s>>,
pub values: Vec<EnumMember<'s>>,
pub members: Vec<ClassMember<'s>>,
}

#[derive(PartialEq, Eq, Debug)]
pub enum EnumMember<'s> {
Comment(Comment<'s>),
Annotation(Annotation<'s>),
Value(EnumValue<'s>),
pub values: Vec<WithMeta<'s, EnumValue<'s>>>,
pub members: Vec<WithMeta<'s, ClassMember<'s>>>,
}

#[derive(PartialEq, Eq, Debug)]
Expand Down
20 changes: 10 additions & 10 deletions dart-parser/src/dart/func_like.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use tiny_set::with_tiny_set;

use super::{ty::Type, Expr, MaybeRequired, TypeParam};
use super::{ty::Type, Expr, MaybeRequired, TypeParam, WithMeta};

#[derive(PartialEq, Eq, Debug)]
pub enum FuncLike<'s> {
Expand All @@ -15,7 +15,7 @@ pub struct Func<'s> {
pub return_type: Type<'s>,
pub name: &'s str,
pub type_params: Vec<TypeParam<'s>>,
pub params: FuncParams<FuncParam<'s>>,
pub params: FuncParams<'s, FuncParam<'s>>,
pub body: Option<FuncBody<'s>>,
}

Expand All @@ -32,7 +32,7 @@ pub struct Setter<'s> {
pub modifiers: FuncModifierSet,
pub name: &'s str,
/// Setters must declare exactly one required positional parameter.
pub params: FuncParams<FuncParam<'s>>,
pub params: FuncParams<'s, FuncParam<'s>>,
pub body: Option<FuncBody<'s>>,
}

Expand All @@ -45,12 +45,12 @@ pub enum FuncModifier {
}

#[derive(PartialEq, Eq, Debug)]
pub struct FuncParams<ParamPos, ParamNamed = ParamPos> {
pub positional_req: Vec<ParamPos>,
pub extra: Option<FuncParamsExtra<ParamPos, ParamNamed>>,
pub struct FuncParams<'s, ParamPos, ParamNamed = ParamPos> {
pub positional_req: Vec<WithMeta<'s, ParamPos>>,
pub extra: Option<FuncParamsExtra<'s, ParamPos, ParamNamed>>,
}

impl<T, U> Default for FuncParams<T, U> {
impl<'s, T, U> Default for FuncParams<'s, T, U> {
fn default() -> Self {
Self {
positional_req: Vec::new(),
Expand All @@ -60,9 +60,9 @@ impl<T, U> Default for FuncParams<T, U> {
}

#[derive(PartialEq, Eq, Debug)]
pub enum FuncParamsExtra<ParamPos, ParamNamed = ParamPos> {
PositionalOpt(Vec<ParamPos>),
Named(Vec<MaybeRequired<ParamNamed>>),
pub enum FuncParamsExtra<'s, ParamPos, ParamNamed = ParamPos> {
PositionalOpt(Vec<WithMeta<'s, ParamPos>>),
Named(Vec<WithMeta<'s, MaybeRequired<ParamNamed>>>),
}

#[derive(PartialEq, Eq, Debug)]
Expand Down
24 changes: 1 addition & 23 deletions dart-parser/src/dart/maybe_required.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::fmt::Debug;

#[derive(PartialEq, Eq, Debug)]
pub struct MaybeRequired<T> {
pub is_required: bool,
value: T,
Expand Down Expand Up @@ -30,26 +31,3 @@ impl<T> AsRef<T> for MaybeRequired<T> {
&self.value
}
}

impl<T> PartialEq<MaybeRequired<T>> for MaybeRequired<T>
where
T: PartialEq<T>,
{
fn eq(&self, other: &MaybeRequired<T>) -> bool {
self.is_required == other.is_required && self.value == other.value
}
}

impl<T> Eq for MaybeRequired<T> where MaybeRequired<T>: PartialEq {}

impl<T> Debug for MaybeRequired<T>
where
T: Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("MaybeRequired")
.field("is_required", &self.is_required)
.field("value", &self.value)
.finish()
}
}
32 changes: 32 additions & 0 deletions dart-parser/src/dart/meta.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use super::{Annotation, Comment};

#[derive(PartialEq, Eq, Debug)]
pub struct WithMeta<'s, T> {
pub meta: Vec<Meta<'s>>,
value: T,
}

impl<'s, T> WithMeta<'s, T> {
pub fn new(meta: Vec<Meta<'s>>, value: T) -> Self {
Self { meta, value }
}

pub fn value(value: T) -> Self {
Self {
meta: Vec::new(),
value,
}
}
}

impl<'s, T> AsRef<T> for WithMeta<'s, T> {
fn as_ref(&self) -> &T {
&self.value
}
}

#[derive(PartialEq, Eq, Debug)]
pub enum Meta<'s> {
Annotation(Annotation<'s>),
Comment(Comment<'s>),
}
2 changes: 1 addition & 1 deletion dart-parser/src/dart/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl<'s> NotFuncType<'s> {
pub struct FuncType<'s> {
pub return_type: Type<'s>,
pub type_params: Vec<TypeParam<'s>>,
pub params: FuncParams<FuncTypeParamPos<'s>, FuncTypeParamNamed<'s>>,
pub params: FuncParams<'s, FuncTypeParamPos<'s>, FuncTypeParamNamed<'s>>,
pub is_nullable: bool,
}

Expand Down
3 changes: 2 additions & 1 deletion dart-parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ mod dart;
mod parser;

pub use dart::Dart;
use dart::WithMeta;
use nom::{
error::{convert_error, VerboseError},
Err,
};

pub fn parse(s: &impl AsRef<str>) -> Result<Vec<Dart>, String> {
pub fn parse(s: &impl AsRef<str>) -> Result<Vec<WithMeta<Dart>>, String> {
let s = s.as_ref();

// Using the simple `nom::error::Error` may be more efficient,
Expand Down
Loading

0 comments on commit b0ef107

Please sign in to comment.