add impl works with lifetimes

This commit is contained in:
Aleksey Kladov 2018-08-28 23:59:57 +03:00
parent ba02a55330
commit 15f15d92eb
6 changed files with 202 additions and 17 deletions

View file

@ -4,6 +4,28 @@ use {
SyntaxKind::*,
};
// ArgList
#[derive(Debug, Clone, Copy)]
pub struct ArgList<'a> {
syntax: SyntaxNodeRef<'a>,
}
impl<'a> AstNode<'a> for ArgList<'a> {
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() {
ARG_LIST => Some(ArgList { syntax }),
_ => None,
}
}
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
}
impl<'a> ArgList<'a> {
pub fn args(self) -> impl Iterator<Item = Expr<'a>> + 'a {
super::children(self)
}
}
// ArrayExpr
#[derive(Debug, Clone, Copy)]
pub struct ArrayExpr<'a> {
@ -181,7 +203,15 @@ impl<'a> AstNode<'a> for CallExpr<'a> {
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
}
impl<'a> CallExpr<'a> {}
impl<'a> CallExpr<'a> {
pub fn expr(self) -> Option<Expr<'a>> {
super::child_opt(self)
}
pub fn arg_list(self) -> Option<ArgList<'a>> {
super::child_opt(self)
}
}
// CastExpr
#[derive(Debug, Clone, Copy)]
@ -705,7 +735,15 @@ impl<'a> AstNode<'a> for LambdaExpr<'a> {
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
}
impl<'a> LambdaExpr<'a> {}
impl<'a> LambdaExpr<'a> {
pub fn param_list(self) -> Option<ParamList<'a>> {
super::child_opt(self)
}
pub fn body(self) -> Option<Expr<'a>> {
super::child_opt(self)
}
}
// LetStmt
#[derive(Debug, Clone, Copy)]
@ -733,6 +771,46 @@ impl<'a> LetStmt<'a> {
}
}
// Lifetime
#[derive(Debug, Clone, Copy)]
pub struct Lifetime<'a> {
syntax: SyntaxNodeRef<'a>,
}
impl<'a> AstNode<'a> for Lifetime<'a> {
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() {
LIFETIME => Some(Lifetime { syntax }),
_ => None,
}
}
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
}
impl<'a> Lifetime<'a> {}
// LifetimeParam
#[derive(Debug, Clone, Copy)]
pub struct LifetimeParam<'a> {
syntax: SyntaxNodeRef<'a>,
}
impl<'a> AstNode<'a> for LifetimeParam<'a> {
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() {
LIFETIME_PARAM => Some(LifetimeParam { syntax }),
_ => None,
}
}
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
}
impl<'a> LifetimeParam<'a> {
pub fn lifetime(self) -> Option<Lifetime<'a>> {
super::child_opt(self)
}
}
// Literal
#[derive(Debug, Clone, Copy)]
pub struct Literal<'a> {
@ -1813,6 +1891,10 @@ impl<'a> TypeParamList<'a> {
pub fn type_params(self) -> impl Iterator<Item = TypeParam<'a>> + 'a {
super::children(self)
}
pub fn lifetime_params(self) -> impl Iterator<Item = LifetimeParam<'a>> + 'a {
super::children(self)
}
}
// TypeRef

View file

@ -67,6 +67,12 @@ impl<'a> Attr<'a> {
}
}
impl<'a> Lifetime<'a> {
pub fn text(&self) -> SmolStr {
self.syntax().leaf_text().unwrap()
}
}
impl<'a> Name<'a> {
pub fn text(&self) -> SmolStr {
let ident = self.syntax().first_child()

View file

@ -344,7 +344,12 @@ Grammar(
"ArrayExpr": (),
"ParenExpr": (),
"PathExpr": (),
"LambdaExpr": (),
"LambdaExpr": (
options: [
["param_list", "ParamList"],
["body", "Expr"],
]
),
"IfExpr": (
options: [ ["condition", "Condition"] ]
),
@ -378,7 +383,12 @@ Grammar(
"StructLit": (),
"NamedFieldList": (),
"NamedField": (),
"CallExpr": (),
"CallExpr": (
options: [
[ "expr", "Expr" ],
[ "arg_list", "ArgList" ],
]
),
"IndexExpr": (),
"MethodCallExpr": (),
"FieldExpr": (),
@ -457,8 +467,15 @@ Grammar(
"NameRef": (),
"Attr": ( options: [ ["value", "TokenTree"] ] ),
"TokenTree": (),
"TypeParamList": ( collections: [ ["type_params", "TypeParam" ] ]),
"TypeParamList": (
collections: [
["type_params", "TypeParam" ],
["lifetime_params", "LifetimeParam" ],
]
),
"TypeParam": ( traits: ["NameOwner"] ),
"LifetimeParam": ( options: [ ["lifetime", "Lifetime"] ] ),
"Lifetime": (),
"WhereClause": (),
"ExprStmt": (
options: [ ["expr", "Expr"] ]
@ -492,5 +509,10 @@ Grammar(
),
"UseItem": (),
"ExternCrateItem": (),
"ArgList": (
collections: [
["args", "Expr"]
]
)
},
)

View file

@ -151,10 +151,11 @@ fn lambda_expr(p: &mut Parser) -> CompletedMarker {
p.eat(MOVE_KW);
params::param_list_opt_types(p);
if opt_fn_ret_type(p) {
block(p);
} else {
expr(p);
if !p.at(L_CURLY) {
p.error("expected `{`");
}
}
expr(p);
m.complete(p, LAMBDA_EXPR)
}