Visibility
This commit is contained in:
+4
-4
@@ -69,13 +69,13 @@ pub enum TopLevelKind {
|
||||
ModAttribute,
|
||||
Include(Path),
|
||||
StructDecl {
|
||||
export: Visibility,
|
||||
visibility: Visibility,
|
||||
name: String,
|
||||
fields: FieldList,
|
||||
},
|
||||
FunctionDecl(FunctionDecl),
|
||||
ClassDecl {
|
||||
export: Visibility,
|
||||
visibility: Visibility,
|
||||
name: String,
|
||||
fields: Vec<VarDeclStmt>,
|
||||
constructor: ClassConstructor,
|
||||
@@ -85,14 +85,14 @@ pub enum TopLevelKind {
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct ClassConstructor {
|
||||
pub export: Visibility,
|
||||
pub visibility: Visibility,
|
||||
pub params: ParamList,
|
||||
pub body: Block,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct FunctionDecl {
|
||||
pub export: Visibility,
|
||||
pub visibility: Visibility,
|
||||
pub name: String,
|
||||
pub params: ParamList,
|
||||
pub return_type: TypeExpr,
|
||||
|
||||
+19
-50
@@ -96,15 +96,10 @@ impl From<pest::iterators::Pair<'_, Rule>> for FieldList {
|
||||
.into_inner()
|
||||
.map(|p| {
|
||||
let mut param_inner = p.into_inner();
|
||||
let export = if param_inner.peek().unwrap().as_rule() == Rule::export {
|
||||
param_inner.next().unwrap();
|
||||
true
|
||||
} else {
|
||||
false
|
||||
};
|
||||
let visibility = Visibility::from(&mut param_inner);
|
||||
let param_type = TypeExpr::from(param_inner.next().unwrap());
|
||||
let param_name = param_inner.next().unwrap().as_str().to_string();
|
||||
(param_name, export, param_type)
|
||||
(param_name, visibility, param_type)
|
||||
})
|
||||
.collect();
|
||||
|
||||
@@ -214,16 +209,7 @@ impl From<pest::iterators::Pair<'_, Rule>> for ClassConstructor {
|
||||
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
|
||||
let mut inner = pair.into_inner();
|
||||
|
||||
let export = if let Some(first) = inner.peek() {
|
||||
if first.as_rule() == Rule::export {
|
||||
inner.next();
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
} else {
|
||||
false
|
||||
};
|
||||
let visibility = Visibility::from(&mut inner);
|
||||
|
||||
let params = if inner.peek().unwrap().as_rule() == Rule::param_list {
|
||||
ParamList::from(inner.next().unwrap())
|
||||
@@ -232,7 +218,7 @@ impl From<pest::iterators::Pair<'_, Rule>> for ClassConstructor {
|
||||
};
|
||||
|
||||
Self {
|
||||
export,
|
||||
visibility,
|
||||
params,
|
||||
body: Block::from(inner.next().unwrap()),
|
||||
}
|
||||
@@ -250,38 +236,20 @@ impl From<pest::iterators::Pair<'_, Rule>> for TopLevelKind {
|
||||
Rule::function_decl => TopLevelKind::FunctionDecl(FunctionDecl::from(pair)),
|
||||
|
||||
Rule::struct_decl => {
|
||||
let export = if let Some(first) = inner.peek() {
|
||||
if first.as_rule() == Rule::export {
|
||||
inner.next();
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
} else {
|
||||
false
|
||||
};
|
||||
let visibility = Visibility::from(&mut inner);
|
||||
let name = inner.next().unwrap().as_str().to_string();
|
||||
let fields_pair = inner.next().unwrap();
|
||||
let fields = FieldList::from(fields_pair);
|
||||
|
||||
TopLevelKind::StructDecl {
|
||||
export,
|
||||
visibility,
|
||||
name,
|
||||
fields,
|
||||
}
|
||||
}
|
||||
|
||||
Rule::class_decl => TopLevelKind::ClassDecl {
|
||||
export: if let Some(first) = inner.peek() {
|
||||
if first.as_rule() == Rule::export {
|
||||
inner.next();
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
} else {
|
||||
false
|
||||
},
|
||||
visibility: Visibility::from(&mut inner),
|
||||
name: inner.next().unwrap().as_str().to_string(),
|
||||
fields: inner
|
||||
.next()
|
||||
@@ -566,16 +534,7 @@ impl From<pest::iterators::Pair<'_, Rule>> for FunctionDecl {
|
||||
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
|
||||
let mut inner = pair.into_inner();
|
||||
|
||||
let export = if let Some(first) = inner.peek() {
|
||||
if first.as_rule() == Rule::export {
|
||||
inner.next();
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
} else {
|
||||
false
|
||||
};
|
||||
let visibility = Visibility::from(&mut inner);
|
||||
|
||||
let return_type = TypeExpr::from(inner.next().unwrap());
|
||||
|
||||
@@ -621,7 +580,7 @@ impl From<pest::iterators::Pair<'_, Rule>> for FunctionDecl {
|
||||
let body = Block::from(inner.next().unwrap());
|
||||
|
||||
Self {
|
||||
export,
|
||||
visibility,
|
||||
name,
|
||||
params,
|
||||
return_type,
|
||||
@@ -630,6 +589,16 @@ impl From<pest::iterators::Pair<'_, Rule>> for FunctionDecl {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&mut pest::iterators::Pairs<'_, Rule>> for Visibility {
|
||||
fn from(pairs: &mut pest::iterators::Pairs<'_, Rule>) -> Self {
|
||||
if listen_rule(pairs, Rule::export) {
|
||||
Visibility::Public
|
||||
} else {
|
||||
Visibility::Private
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn listen_rule(pairs: &mut pest::iterators::Pairs<'_, Rule>, rule: Rule) -> bool {
|
||||
let consumed = pairs
|
||||
.peek()
|
||||
|
||||
+18
-12
@@ -1,7 +1,7 @@
|
||||
use parser::ast::{
|
||||
Attribute, BinaryOp, Block, Expression, FunctionDecl, Literal, Path, Postfix, Prefix,
|
||||
Statement, StatementBranch, TopLevel, TopLevelKind, TypeExpr, TypeExprKind, TypePostfix,
|
||||
VarAssignStmt, VarDecl, VarDeclStmt,
|
||||
VarAssignStmt, VarDecl, VarDeclStmt, Visibility,
|
||||
};
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -312,12 +312,11 @@ impl ToRust for TopLevelKind {
|
||||
}
|
||||
Self::FunctionDecl(decl) => decl.to_rust(cg),
|
||||
Self::StructDecl {
|
||||
export,
|
||||
visibility,
|
||||
name,
|
||||
fields,
|
||||
} => {
|
||||
let vis = if *export { "pub " } else { "" };
|
||||
cg.addln(&format!("{}struct {} {{", vis, name));
|
||||
cg.addln(&format!("{}struct {} {{", visibility.get_rust(), name));
|
||||
cg.indent += 1;
|
||||
|
||||
for (field_name, _, ty) in &fields.0 {
|
||||
@@ -329,15 +328,14 @@ impl ToRust for TopLevelKind {
|
||||
cg.addln("}\n");
|
||||
}
|
||||
Self::ClassDecl {
|
||||
export,
|
||||
visibility,
|
||||
name,
|
||||
fields,
|
||||
constructor,
|
||||
methods,
|
||||
} => {
|
||||
// Struct decl
|
||||
let vis = if *export { "pub " } else { "" };
|
||||
cg.addln(&format!("{}struct {} {{", vis, name));
|
||||
cg.addln(&format!("{}struct {} {{", visibility.get_rust(), name));
|
||||
cg.indent += 1;
|
||||
|
||||
for field in fields {
|
||||
@@ -362,7 +360,7 @@ impl ToRust for TopLevelKind {
|
||||
|
||||
cg.add_indentedln(&format!(
|
||||
"{}fn new({}) -> Self {{",
|
||||
if constructor.export { "pub " } else { "" },
|
||||
constructor.visibility.get_rust(),
|
||||
params_str
|
||||
));
|
||||
cg.indent += 1;
|
||||
@@ -398,7 +396,7 @@ impl ToRust for TopLevelKind {
|
||||
// Constructor function
|
||||
cg.add_indentedln(&format!(
|
||||
"{}fn construct_class(&mut self, {}) {{",
|
||||
if constructor.export { "pub " } else { "" },
|
||||
constructor.visibility.get_rust(),
|
||||
params_str
|
||||
));
|
||||
cg.indent += 1;
|
||||
@@ -520,8 +518,6 @@ impl ToRust for Statement {
|
||||
|
||||
impl ToRust for FunctionDecl {
|
||||
fn to_rust(&self, cg: &mut RustCodegen) {
|
||||
let vis = if self.export { "pub " } else { "" };
|
||||
|
||||
let params_str = self
|
||||
.params
|
||||
.0
|
||||
@@ -532,7 +528,7 @@ impl ToRust for FunctionDecl {
|
||||
|
||||
cg.add_indentedln(&format!(
|
||||
"{}fn {}({}) -> {} {{",
|
||||
vis,
|
||||
self.visibility.get_rust(),
|
||||
self.name,
|
||||
params_str,
|
||||
self.return_type.get_rust()
|
||||
@@ -573,6 +569,16 @@ impl GetRust for TypePostfix {
|
||||
}
|
||||
}
|
||||
|
||||
impl GetRust for Visibility {
|
||||
fn get_rust(&self) -> String {
|
||||
match self {
|
||||
Visibility::Public => "pub ",
|
||||
Visibility::Private => "",
|
||||
}
|
||||
.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_static_type_path(path: &Path) -> String {
|
||||
let rust_path = path.get_rust();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user