Added separate self param
This commit is contained in:
@@ -375,20 +375,6 @@ impl ClassProcessedData {
|
|||||||
cg.indent -= 1;
|
cg.indent -= 1;
|
||||||
cg.add_indentedln("}\n");
|
cg.add_indentedln("}\n");
|
||||||
|
|
||||||
let mut constructor_params = vec![VarDecl {
|
|
||||||
name: Pattern::Path(false, Path(vec![Identifier(String::from("self"))])),
|
|
||||||
type_: Some(TypeExpr::Ref {
|
|
||||||
lifetime: None,
|
|
||||||
mutable: true,
|
|
||||||
ty: Box::new(TypeExpr::Path(
|
|
||||||
Path(vec![Identifier(String::from("Self"))]),
|
|
||||||
None,
|
|
||||||
)),
|
|
||||||
}),
|
|
||||||
}];
|
|
||||||
|
|
||||||
constructor_params.append(&mut constructor.item.params.0.clone());
|
|
||||||
|
|
||||||
Spanned {
|
Spanned {
|
||||||
line: constructor.line,
|
line: constructor.line,
|
||||||
column: constructor.column,
|
column: constructor.column,
|
||||||
@@ -397,7 +383,8 @@ impl ClassProcessedData {
|
|||||||
is_override: None,
|
is_override: None,
|
||||||
name: Identifier(String::from("constructor")),
|
name: Identifier(String::from("constructor")),
|
||||||
generics: constructor.item.generics.clone(),
|
generics: constructor.item.generics.clone(),
|
||||||
params: ParamList(constructor_params),
|
params: constructor.item.params.clone(),
|
||||||
|
self_param: Some((true, None, true)),
|
||||||
return_type: Some(TypeExpr::Tuple(Vec::new())),
|
return_type: Some(TypeExpr::Tuple(Vec::new())),
|
||||||
body: Some(constructor.item.body.clone()),
|
body: Some(constructor.item.body.clone()),
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -100,6 +100,7 @@ pub struct FunctionDecl {
|
|||||||
pub is_override: Option<Override>,
|
pub is_override: Option<Override>,
|
||||||
pub name: Identifier,
|
pub name: Identifier,
|
||||||
pub generics: GenericsDecl,
|
pub generics: GenericsDecl,
|
||||||
|
pub self_param: Option<(bool, Option<Identifier>, bool)>,
|
||||||
pub params: ParamList,
|
pub params: ParamList,
|
||||||
pub return_type: Option<TypeExpr>,
|
pub return_type: Option<TypeExpr>,
|
||||||
pub body: Option<Block>,
|
pub body: Option<Block>,
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ use crate::{
|
|||||||
ast::*,
|
ast::*,
|
||||||
ast_ensure,
|
ast_ensure,
|
||||||
error::{self, AstError, AstResult},
|
error::{self, AstError, AstResult},
|
||||||
parser::{consume_rule, listen_rule},
|
parser::{consume_rule, consume_rule_map, listen_rule},
|
||||||
};
|
};
|
||||||
|
|
||||||
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for FunctionDecl {
|
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for FunctionDecl {
|
||||||
@@ -25,43 +25,19 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for FunctionDecl {
|
|||||||
.transpose()
|
.transpose()
|
||||||
.map(|v| v.unwrap_or_default())?;
|
.map(|v| v.unwrap_or_default())?;
|
||||||
|
|
||||||
let self_param = consume_rule(&mut inner, Rule::self_param).map(|param| {
|
let self_param = consume_rule_map(&mut inner, Rule::self_param, |param| {
|
||||||
let mut param_inner = param.into_inner();
|
let mut param_inner = param.into_inner();
|
||||||
|
|
||||||
let is_ref = listen_rule(&mut param_inner, Rule::ref_px);
|
let is_ref = listen_rule(&mut param_inner, Rule::ref_px);
|
||||||
let lifetime = consume_rule(&mut param_inner, Rule::lifetime);
|
let lifetime = consume_rule(&mut param_inner, Rule::lifetime);
|
||||||
let mutable = listen_rule(&mut param_inner, Rule::mutable);
|
let mutable = listen_rule(&mut param_inner, Rule::mutable);
|
||||||
let name = Pattern::Path(mutable && !is_ref, Path(vec![Identifier(String::from("self"))]));
|
|
||||||
let self_ty = TypeExpr::Path(Path(vec![Identifier(String::from("Self"))]), None);
|
|
||||||
|
|
||||||
VarDecl {
|
Ok((is_ref, lifetime.map(|v| -> AstResult<Identifier> {
|
||||||
name: name.clone(),
|
Identifier::try_from(v.into_inner().next().unwrap())
|
||||||
type_: Some(if is_ref {
|
}).transpose()?, mutable))
|
||||||
TypeExpr::Ref {
|
})?;
|
||||||
lifetime:
|
|
||||||
lifetime.map(|v| Lifetime::try_from(v.into_inner().next().unwrap()))
|
|
||||||
.transpose()
|
|
||||||
.expect("Failed to get lifetime identifier"),
|
|
||||||
mutable,
|
|
||||||
ty: Box::new(self_ty)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
self_ty
|
|
||||||
}),
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
let params = consume_rule(&mut inner, Rule::param_list)
|
let params = consume_rule(&mut inner, Rule::param_list).map(ParamList::try_from).transpose()?.unwrap_or_default();
|
||||||
.map({
|
|
||||||
let self_param = self_param.clone();
|
|
||||||
|params_pair| -> AstResult<'a, ParamList> {
|
|
||||||
let mut params = ParamList::try_from(params_pair)?;
|
|
||||||
if let Some(x) = self_param {
|
|
||||||
params.0.insert(0, x);
|
|
||||||
}
|
|
||||||
Ok(params)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.unwrap_or_else(|| Ok(ParamList(self_param.into_iter().collect())))?;
|
|
||||||
|
|
||||||
let is_override = consume_rule(&mut inner, Rule::override_kw).map(Override::try_from).transpose()?;
|
let is_override = consume_rule(&mut inner, Rule::override_kw).map(Override::try_from).transpose()?;
|
||||||
|
|
||||||
@@ -73,6 +49,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for FunctionDecl {
|
|||||||
return_type: return_type,
|
return_type: return_type,
|
||||||
name: name,
|
name: name,
|
||||||
generics: generics,
|
generics: generics,
|
||||||
|
self_param: self_param,
|
||||||
params: params,
|
params: params,
|
||||||
body: body,
|
body: body,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,7 +1,11 @@
|
|||||||
pub mod common;
|
pub mod common;
|
||||||
pub mod items;
|
pub mod items;
|
||||||
|
|
||||||
use crate::{Rule, ast::Spanned, error::AstError};
|
use crate::{
|
||||||
|
Rule,
|
||||||
|
ast::Spanned,
|
||||||
|
error::{AstError, AstResult},
|
||||||
|
};
|
||||||
|
|
||||||
pub fn listen_rule(pairs: &mut pest::iterators::Pairs<'_, Rule>, rule: Rule) -> bool {
|
pub fn listen_rule(pairs: &mut pest::iterators::Pairs<'_, Rule>, rule: Rule) -> bool {
|
||||||
let consumed = pairs
|
let consumed = pairs
|
||||||
@@ -28,6 +32,20 @@ pub fn consume_rule<'a>(
|
|||||||
if consumed { pairs.next() } else { None }
|
if consumed { pairs.next() } else { None }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn consume_rule_map<'a, T>(
|
||||||
|
pairs: &mut pest::iterators::Pairs<'a, Rule>,
|
||||||
|
rule: Rule,
|
||||||
|
map: impl Fn(pest::iterators::Pair<'a, Rule>) -> AstResult<'a, T>,
|
||||||
|
) -> AstResult<'a, Option<T>> {
|
||||||
|
let pair = consume_rule(pairs, rule);
|
||||||
|
|
||||||
|
if let Some(pair) = pair {
|
||||||
|
Some(map(pair)).transpose()
|
||||||
|
} else {
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T> Spanned<T> {
|
impl<T> Spanned<T> {
|
||||||
fn new_pair(pair: pest::iterators::Pair<'_, Rule>, item: T) -> Self {
|
fn new_pair(pair: pest::iterators::Pair<'_, Rule>, item: T) -> Self {
|
||||||
let span = pair.as_span().start_pos().line_col();
|
let span = pair.as_span().start_pos().line_col();
|
||||||
|
|||||||
Reference in New Issue
Block a user