Skip to content

Commit

Permalink
Refactoring Constant -> Object with mutability
Browse files Browse the repository at this point in the history
  • Loading branch information
notdanilo committed Oct 24, 2023
1 parent 0534d22 commit 9170807
Show file tree
Hide file tree
Showing 25 changed files with 168 additions and 194 deletions.
6 changes: 3 additions & 3 deletions ecosystem/python/parser/src/interface.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::prelude::*;
use rustpython_parser::ast::{StmtClassDef};
use rustpython_parser::ast::StmtClassDef;
use ligen::ir::Interface;
use crate::identifier::IdentifierParser;
use crate::parser::PythonParser;
Expand All @@ -9,9 +9,9 @@ impl Parser<WithSource<&StmtClassDef>> for PythonParser {
fn parse(&self, input: WithSource<&StmtClassDef>) -> Result<Self::Output> {
let scope = self.parse(input.sub(input.ast.body.as_slice()))?;
let identifier = IdentifierParser::new().parse(input.ast.name.as_str())?;
let constants = scope.constants;
let objects = scope.objects;
let functions = scope.functions;
let methods = scope.methods;
Ok(Interface { identifier, constants, functions, methods, .. Default::default() })
Ok(Interface { identifier, objects, functions, methods, .. Default::default() })
}
}
2 changes: 1 addition & 1 deletion ecosystem/python/parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ pub mod types;
pub mod literal;
pub mod scope;
pub mod interface;
pub mod constant;
pub mod object;
pub mod parser;
4 changes: 2 additions & 2 deletions ecosystem/python/parser/src/module/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ impl Parser<WithSource<ModModule>> for PythonParser {
type Output = Module;
fn parse(&self, input: WithSource<ModModule>) -> Result<Self::Output> {
let scope = self.parse(input.sub(input.ast.body.as_slice()))?;
let constants = scope.constants;
let objects = scope.objects;
let types = scope.types;
let functions = scope.functions;
let interfaces = scope.interfaces;
Ok(Module { constants, functions, types, interfaces, ..Default::default() })
Ok(Module { objects, functions, types, interfaces, ..Default::default() })
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// FIXME: Duplicated from symbol_parser.

use rustpython_parser::ast::{Expr, StmtAnnAssign, StmtAssign, StmtAugAssign};
use ligen::ir::{Constant, Identifier};
use ligen::ir::{Object, Identifier, Mutability};
use crate::identifier::IdentifierParser;
use crate::prelude::*;

Expand All @@ -11,51 +13,51 @@ impl<'a> DynamicParser<'a> for FullParser {}
pub struct FullParser;

impl Parser<&StmtAnnAssign> for FullParser {
type Output = Constant;
type Output = Object;
fn parse(&self, input: &StmtAnnAssign) -> Result<Self::Output> {
self.parse(input.target.as_ref())
}
}

impl Parser<&StmtAugAssign> for FullParser {
type Output = Constant;
type Output = Object;
fn parse(&self, input: &StmtAugAssign) -> Result<Self::Output> {
self.parse(input.target.as_ref())
}
}

impl Parser<&Expr> for FullParser {
type Output = Constant;
type Output = Object;
fn parse(&self, expr: &Expr) -> Result<Self::Output> {
let identifier = expr
.as_name_expr()
.ok_or(Error::Message("Expected identifier".into()))?
.id
.as_str();
let identifier = IdentifierParser::new().parse(identifier)?;
if self.is_constant(&identifier) {
Ok(Constant { identifier, ..Default::default() })
} else {
Err(Error::Message("Expected constant".into()))
}
}
let mutability = self.get_mutability(&identifier);
Ok(Object { identifier, mutability, ..Default::default() })
}

impl Parser<&StmtAssign> for FullParser {
type Output = Vec<Constant>;
type Output = Vec<Object>;
fn parse(&self, input: &StmtAssign) -> Result<Self::Output> {
let mut constants = Vec::new();
let mut objects = Vec::new();
for target in &input.targets {
if let Ok(constant) = self.parse(target) {
constants.push(constant);
if let Ok(object) = self.parse(target) {
objects.push(object);
}
}
Ok(constants)
Ok(objects)
}
}

impl FullParser {
fn is_constant(&self, identifier: &Identifier) -> bool {
identifier.name.to_uppercase() == identifier.name
fn get_mutability(&self, identifier: &Identifier) -> Mutability {
if identifier.name.to_uppercase() == identifier.name {
Mutability::Constant
} else {
Mutability::Mutable
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use ligen::parsing::dynamic_parser;
use ligen::ir::Constant;
use ligen::ir::Object;
use rustpython_parser::ast::{StmtAnnAssign, StmtAugAssign, Expr, StmtAssign};

mod full_parser;
mod symbol_parser;

dynamic_parser!{
ConstantParser,
ObjectParser,
full_parser::FullParser,
symbol_parser::SymbolParser,
Constant,
Object,
&StmtAnnAssign | &'a StmtAnnAssign,
&StmtAugAssign | &'a StmtAugAssign,
&Expr | &'a Expr,
&StmtAssign | &'a StmtAssign => Vec<Constant>
&StmtAssign | &'a StmtAssign => Vec<Object>
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use ligen::ir::Mutability;
use rustpython_parser::ast::{Expr, StmtAnnAssign, StmtAssign, StmtAugAssign};
use ligen::ir::{Constant, Identifier};
use ligen::ir::{Object, Identifier};
use crate::identifier::IdentifierParser;
use crate::prelude::*;

Expand All @@ -11,51 +12,52 @@ impl<'a> DynamicParser<'a> for SymbolParser {}
pub struct SymbolParser;

impl Parser<&StmtAnnAssign> for SymbolParser {
type Output = Constant;
type Output = Object;
fn parse(&self, input: &StmtAnnAssign) -> Result<Self::Output> {
self.parse(input.target.as_ref())
}
}

impl Parser<&StmtAugAssign> for SymbolParser {
type Output = Constant;
type Output = Object;
fn parse(&self, input: &StmtAugAssign) -> Result<Self::Output> {
self.parse(input.target.as_ref())
}
}

impl Parser<&Expr> for SymbolParser {
type Output = Constant;
type Output = Object;
fn parse(&self, expr: &Expr) -> Result<Self::Output> {
let identifier = expr
.as_name_expr()
.ok_or(Error::Message("Expected identifier".into()))?
.id
.as_str();
let identifier = IdentifierParser::new().parse(identifier)?;
if self.is_constant(&identifier) {
Ok(Constant { identifier, ..Default::default() })
} else {
Err(Error::Message("Expected constant".into()))
}
let mutability = self.get_mutability(&identifier);
Ok(Object { identifier, mutability, ..Default::default() })
}
}

impl Parser<&StmtAssign> for SymbolParser {
type Output = Vec<Constant>;
type Output = Vec<Object>;
fn parse(&self, input: &StmtAssign) -> Result<Self::Output> {
let mut constants = Vec::new();
let mut objects = Vec::new();
for target in &input.targets {
if let Ok(constant) = self.parse(target) {
constants.push(constant);
if let Ok(object) = self.parse(target) {
objects.push(object);
}
}
Ok(constants)
Ok(objects)
}
}

impl SymbolParser {
fn is_constant(&self, identifier: &Identifier) -> bool {
identifier.name.to_uppercase() == identifier.name
fn get_mutability(&self, identifier: &Identifier) -> Mutability {
if identifier.name.to_uppercase() == identifier.name {
Mutability::Constant
} else {
Mutability::Mutable
}
}
}
8 changes: 4 additions & 4 deletions ecosystem/python/parser/src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::constant::ConstantParser;
use crate::object::ObjectParser;
use crate::function::FunctionParser;
use crate::identifier::IdentifierParser;
use crate::types::type_definition::TypeDefinitionParser;
Expand All @@ -8,7 +8,7 @@ pub struct PythonParser {
pub identifier_parser: IdentifierParser,
pub function_parser: FunctionParser,
pub type_definition_parser: TypeDefinitionParser,
pub constant_parser: ConstantParser
pub object_parser: ObjectParser
}

impl PythonParser {
Expand All @@ -20,7 +20,7 @@ impl PythonParser {
let identifier_parser = IdentifierParser::new();
let function_parser = FunctionParser::symbol();
let type_definition_parser = TypeDefinitionParser::symbol();
let constant_parser = ConstantParser::symbol();
Self { identifier_parser, function_parser, type_definition_parser, constant_parser }
let object_parser = ObjectParser::symbol();
Self { identifier_parser, function_parser, type_definition_parser, object_parser }
}
}
24 changes: 12 additions & 12 deletions ecosystem/python/parser/src/scope/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod scope_type;

use rustpython_parser::ast::{Arguments, Expr, Stmt};
use ligen::ir::{Interface, Constant, Function, Method, TypeDefinition};
use ligen::ir::{Interface, Object, Function, Method, TypeDefinition};
use crate::prelude::*;

pub use scope_type::*;
Expand All @@ -10,12 +10,12 @@ use crate::parser::PythonParser;
impl Parser<WithSource<&[Stmt]>> for PythonParser {
type Output = Scope;
fn parse(&self, input: WithSource<&[Stmt]>) -> Result<Self::Output> {
let constants = self.parse_constants(&input)?;
let objects = self.parse_objects(&input)?;
let types = self.parse_types(&input)?;
let functions = self.parse_functions(&input)?;
let interfaces = self.parse_interfaces(&input)?;
let methods = self.parse_methods(&input)?;
let mut scope = Scope { constants, types, functions, methods, interfaces };
let mut scope = Scope { objects, types, functions, methods, interfaces };
let sub_scopes = self.parse_sub_scopes(&input)?;
for sub_scope in sub_scopes {
scope.join(sub_scope);
Expand Down Expand Up @@ -144,28 +144,28 @@ impl PythonParser {
Ok(interfaces)
}

fn parse_constants(&self, statements: &WithSource<&[Stmt]>) -> Result<Vec<Constant>> {
let mut constants = Vec::new();
fn parse_objects(&self, statements: &WithSource<&[Stmt]>) -> Result<Vec<Object>> {
let mut objects = Vec::new();
for statement in statements.ast {
match statement {
Stmt::Assign(assign) => {
if let Ok(more_constants) = self.constant_parser.parse(assign) {
constants.extend(more_constants)
if let Ok(more_objects) = self.object_parser.parse(assign) {
objects.extend(more_objects)
}
},
Stmt::AnnAssign(assign) => {
if let Ok(constant) = self.constant_parser.parse(assign) {
constants.push(constant)
if let Ok(object) = self.object_parser.parse(assign) {
objects.push(object)
}
},
Stmt::AugAssign(assign) => {
if let Ok(constant) = self.constant_parser.parse(assign) {
constants.push(constant)
if let Ok(object) = self.object_parser.parse(assign) {
objects.push(object)
}
},
_ => ()
}
}
Ok(constants)
Ok(objects)
}
}
6 changes: 3 additions & 3 deletions ecosystem/python/parser/src/scope/scope_type.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ligen::ir::{Interface, Constant, Function, Method, TypeDefinition};
use ligen::ir::{Interface, Object, Function, Method, TypeDefinition};

pub struct Scope {
pub constants: Vec<Constant>,
pub objects: Vec<Object>,
pub types: Vec<TypeDefinition>,
pub functions: Vec<Function>,
pub methods: Vec<Method>,
Expand All @@ -10,7 +10,7 @@ pub struct Scope {

impl Scope {
pub fn join(&mut self, other: Self) {
self.constants.extend(other.constants);
self.objects.extend(other.objects);
self.types.extend(other.types);
self.functions.extend(other.functions);
self.methods.extend(other.methods);
Expand Down
2 changes: 1 addition & 1 deletion ecosystem/rust/parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub mod path;
pub mod literal;
pub mod identifier;
pub mod module;
pub mod constant;
pub mod object;

extern crate proc_macro;
extern crate core;
16 changes: 8 additions & 8 deletions ecosystem/rust/parser/src/module/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
mod import;

use syn::spanned::Spanned;
use ligen::ir::Constant;
use ligen::ir::Object;
use ligen::parsing::parser::Parser;
use crate::prelude::*;
use ligen::ir::{Function, Module, Import, TypeDefinition, Interface};
use crate::constant::ConstantParser;
use crate::object::ObjectParser;
use crate::function::FunctionParser;
use crate::identifier::IdentifierParser;
use crate::macro_attributes::attributes::AttributesParser;
Expand Down Expand Up @@ -40,11 +40,11 @@ impl Parser<syn::ItemMod> for ModuleParser {

let imports = self.extract_imports(items.as_slice())?;
let functions = self.extract_functions(items.as_slice())?;
let constants = self.extract_constants(items.as_slice())?;
let objects = self.extract_objects(items.as_slice())?;
let types = self.extract_types(items.as_slice())?;
let interfaces = self.extract_interfaces(items.as_slice())?;
let modules = self.extract_modules(items)?;
Ok(Self::Output { attributes, visibility, identifier, imports, functions, constants, types, interfaces, modules })
Ok(Self::Output { attributes, visibility, identifier, imports, functions, objects, types, interfaces, modules })
}
}

Expand Down Expand Up @@ -124,14 +124,14 @@ impl ModuleParser {
Ok(modules)
}

fn extract_constants(&self, items: &[syn::Item]) -> Result<Vec<Constant>> {
let mut constants = Vec::new();
fn extract_objects(&self, items: &[syn::Item]) -> Result<Vec<Object>> {
let mut objects = Vec::new();
for item in items {
if let syn::Item::Const(constant) = item {
constants.push(ConstantParser.parse(constant.clone())?);
objects.push(ObjectParser.parse(constant.clone())?);
}
}
Ok(constants)
Ok(objects)
}

}
Expand Down
Loading

0 comments on commit 9170807

Please sign in to comment.