Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse tuples and more #71

Merged
merged 1 commit into from
Sep 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[workspace]
resolver = "2"
members = [
"reptr",
"dart-parser",
Expand Down
2 changes: 2 additions & 0 deletions dart-parser/src/dart/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pub struct Class<'s> {
pub with: Vec<NotFuncType<'s>>,
/// Interfaces.
pub implements: Vec<NotFuncType<'s>>,
/// Types a mix-in can be added to.
pub mixin_on: Vec<NotFuncType<'s>>,
pub body: Vec<WithMeta<'s, ClassMember<'s>>>,
}

Expand Down
11 changes: 10 additions & 1 deletion dart-parser/src/dart/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::{func_like::FuncParams, TypeParam};
pub enum Type<'s> {
NotFunc(NotFuncType<'s>),
Func(Box<FuncType<'s>>),
Tuple(Vec<Type<'s>>),
Tuple(Tuple<'s>),
}

impl<'s> Type<'s> {
Expand Down Expand Up @@ -67,3 +67,12 @@ pub struct FuncTypeParamNamed<'s> {
pub param_type: Type<'s>,
pub name: &'s str,
}

#[derive(PartialEq, Eq, Debug)]
pub struct Tuple<'s> {
/// A name can be specified for a positional tuple parameter,
/// but has no meaning whatsoever.
pub params_pos: Vec<FuncTypeParamPos<'s>>,
pub params_named: Vec<FuncTypeParamNamed<'s>>,
pub is_nullable: bool,
}
3 changes: 3 additions & 0 deletions dart-parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod expr;
mod extension;
mod func_call;
mod func_like;
mod maybe_required;
mod meta;
mod string;
mod ty;
Expand Down Expand Up @@ -137,6 +138,7 @@ mod tests {
extends: None,
with: Vec::new(),
implements: Vec::default(),
mixin_on: Vec::default(),
body: vec![
WithMeta::value(ClassMember::Constructor(Constructor {
modifier: None,
Expand Down Expand Up @@ -192,6 +194,7 @@ mod tests {
},
NotFuncType::name("C")
],
mixin_on: Vec::default(),
body: vec![WithMeta::value(ClassMember::Var(Var {
modifiers: VarModifierSet::default(),
var_type: Some(Type::NotFunc(NotFuncType::name("String"))),
Expand Down
4 changes: 2 additions & 2 deletions dart-parser/src/parser/annotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use nom::{

use crate::dart::Annotation;

use super::{func_call::func_call, ty::identifier, PResult};
use super::{func_call::annotation_func_call, ty::identifier, PResult};

pub fn annotation<'s, E>(s: &'s str) -> PResult<Annotation, E>
where
Expand All @@ -20,7 +20,7 @@ where
preceded(
tag("@"),
cut(alt((
func_call.map(Annotation::FuncCall),
annotation_func_call.map(Annotation::FuncCall),
identifier.map(Annotation::Ident),
))),
),
Expand Down
33 changes: 27 additions & 6 deletions dart-parser/src/parser/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,18 @@ where
opt(terminated(extends_clause, opt(spbr))),
opt(terminated(with_clause, opt(spbr))),
opt(terminated(implements_clause, opt(spbr))),
opt(terminated(mixin_on_clause, opt(spbr))),
class_body,
))
.map(
|(modifiers, name, type_params, extends, with, implements, body)| Class {
|(modifiers, name, type_params, extends, with, implements, on, body)| Class {
modifiers,
name,
type_params: type_params.unwrap_or(Vec::new()),
extends,
with: with.unwrap_or(Vec::new()),
implements: implements.unwrap_or(Vec::new()),
mixin_on: on.unwrap_or(Vec::new()),
body,
},
),
Expand Down Expand Up @@ -94,10 +96,26 @@ where
context(
"implements_clause",
preceded(
pair(tag("implements"), spbr),
pair(tag("implements"), spbrc),
cut(separated_list1(
tuple((opt(spbr), tag(","), opt(spbr))),
not_func_type,
pair(tag(","), opt(spbrc)),
terminated(not_func_type, opt(spbrc)),
)),
),
)(s)
}

pub fn mixin_on_clause<'s, E>(s: &'s str) -> PResult<Vec<NotFuncType>, E>
where
E: ParseError<&'s str> + ContextError<&'s str>,
{
context(
"mixin_on_clause",
preceded(
pair(tag("on"), spbrc),
cut(separated_list1(
pair(tag(","), opt(spbrc)),
terminated(not_func_type, opt(spbrc)),
)),
),
)(s)
Expand Down Expand Up @@ -235,9 +253,9 @@ mod tests {
#[test]
fn implements_test() {
assert_eq!(
implements_clause::<VerboseError<_>>("implements A, B, C "),
implements_clause::<VerboseError<_>>("implements A, B, C x"),
Ok((
" ",
"x",
vec![
NotFuncType::name("A"),
NotFuncType::name("B"),
Expand Down Expand Up @@ -271,6 +289,7 @@ mod tests {
extends: Some(NotFuncType::name("Base")),
with: Vec::new(),
implements: vec![NotFuncType::name("A"), NotFuncType::name("B")],
mixin_on: Vec::default(),
body: Vec::new(),
}
))
Expand All @@ -290,6 +309,7 @@ mod tests {
extends: None,
with: Vec::new(),
implements: Vec::new(),
mixin_on: Vec::default(),
body: vec![WithMeta::new(
vec![Meta::Annotation(Annotation::Ident("override"))],
ClassMember::Var(Var {
Expand Down Expand Up @@ -329,6 +349,7 @@ mod tests {
})],
is_nullable: false,
}],
mixin_on: Vec::default(),
body: Vec::new(),
}
))
Expand Down
17 changes: 10 additions & 7 deletions dart-parser/src/parser/enum_ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ where
context(
"enum_ty",
tuple((
terminated(preceded(pair(tag("enum"), spbr), identifier), opt(spbr)),
opt(terminated(implements_clause, opt(spbr))),
terminated(preceded(pair(tag("enum"), spbrc), identifier), opt(spbr)),
opt(terminated(implements_clause, opt(spbrc))),
enum_body,
)),
)
Expand All @@ -53,11 +53,14 @@ where
pair(tag("{"), opt(spbr)),
cut(terminated(
pair(
sep_list(
0,
SepMode::AllowTrailing,
pair(tag(","), opt(spbr)),
terminated(with_meta(enum_value), opt(spbrc)),
terminated(
sep_list(
0,
SepMode::AllowTrailing,
pair(tag(","), opt(spbr)),
terminated(with_meta(enum_value), opt(spbrc)),
),
opt(spbrc),
),
alt((
preceded(
Expand Down
19 changes: 11 additions & 8 deletions dart-parser/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,18 @@ pub fn expr<'s, E>(s: &'s str) -> PResult<Expr, E>
where
E: ParseError<&'s str> + ContextError<&'s str>,
{
recognize(
// Make sure something other than whitespace is consumed
preceded(opt(spbr), expr_body),
context(
"expr",
recognize(
// Make sure something other than whitespace is consumed
preceded(opt(spbr), expr_body),
)
.and_then(alt((
terminated(identifier, eof).map(Expr::Ident),
terminated(string, eof).map(Expr::String),
|s: &'s str| Ok((&s[s.len()..], Expr::Verbatim(s))),
))),
)
.and_then(alt((
terminated(identifier, eof).map(Expr::Ident),
terminated(string, eof).map(Expr::String),
|s: &'s str| Ok((&s[s.len()..], Expr::Verbatim(s))),
)))
.parse(s)
}

Expand Down
28 changes: 22 additions & 6 deletions dart-parser/src/parser/func_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,39 @@ use nom::{
use crate::dart::func_call::{FuncArg, FuncCall};

use super::{
common::{sep_list, spbr, SepMode},
common::{sep_list, spbr, spbrc, SepMode},
expr::expr,
ty::{identifier, not_func_type},
PResult,
};

pub fn func_call<'s, E>(s: &'s str) -> PResult<FuncCall, E>
pub fn _func_call<'s, E>(s: &'s str) -> PResult<FuncCall, E>
where
E: ParseError<&'s str> + ContextError<&'s str>,
{
context(
"func_call",
pair(terminated(not_func_type, opt(spbr)), func_args)
pair(terminated(not_func_type, opt(spbrc)), func_args)
.map(|(ident, args)| FuncCall { ident, args }),
)(s)
}

/// In function/constructor-call annotations whitespace is not allowed in front
/// of the parentheses grouping the call arguments.
///
/// When an annotation is applied to a function declaration, this helps
/// differentiating between the annotation arguments and the return type of the
/// annotated function (which may be a tuple).
pub fn annotation_func_call<'s, E>(s: &'s str) -> PResult<FuncCall, E>
where
E: ParseError<&'s str> + ContextError<&'s str>,
{
context(
"func_call",
pair(not_func_type, func_args).map(|(ident, args)| FuncCall { ident, args }),
)(s)
}

pub fn func_args<'s, E>(s: &'s str) -> PResult<Vec<FuncArg>, E>
where
E: ParseError<&'s str> + ContextError<&'s str>,
Expand Down Expand Up @@ -54,7 +70,7 @@ where
{
alt((
pair(
terminated(identifier, tuple((opt(spbr), tag(":"), opt(spbr)))),
terminated(identifier, tuple((opt(spbrc), tag(":"), opt(spbrc)))),
expr,
)
.map(|(name, value)| FuncArg {
Expand All @@ -76,7 +92,7 @@ mod tests {
#[test]
fn func_call_simple_test() {
assert_eq!(
func_call::<VerboseError<_>>("f() x"),
_func_call::<VerboseError<_>>("f() x"),
Ok((
" x",
FuncCall {
Expand All @@ -90,7 +106,7 @@ mod tests {
#[test]
fn func_call_mixed_test() {
assert_eq!(
func_call::<VerboseError<_>>("f<int>(1, named: two, verbatim: 1 + 2) x"),
_func_call::<VerboseError<_>>("f<int>(1, named: two, verbatim: 1 + 2) x"),
Ok((
" x",
FuncCall {
Expand Down
Loading