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