Skip to content

Commit

Permalink
Making sure full_parser is parsing only valid full IR
Browse files Browse the repository at this point in the history
  • Loading branch information
notdanilo committed Oct 30, 2023
1 parent 9bd8bec commit a4d7c19
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 32 deletions.
10 changes: 9 additions & 1 deletion ecosystem/python/parser/src/identifier.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ligen::ir::Identifier;
use ligen::ir::{Identifier, Mutability};
use ligen::parsing::parser::Parser;
use ligen::parsing::parser::universal::identifier::IdentifierParser as InternalParser;
use crate::prelude::*;
Expand All @@ -24,4 +24,12 @@ impl IdentifierParser {
pub fn is_private(&self, identifier: &Identifier) -> bool {
identifier.name.starts_with('_') && !identifier.name.starts_with("__")
}

pub fn get_mutability(&self, identifier: &Identifier) -> Mutability {
if identifier.name.to_uppercase() == identifier.name {
Mutability::Constant
} else {
Mutability::Mutable
}
}
}
22 changes: 7 additions & 15 deletions ecosystem/python/parser/src/object/full_parser.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// FIXME: Duplicated from symbol_parser.

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

Expand Down Expand Up @@ -34,9 +32,12 @@ impl Parser<&Expr> for FullParser {
.ok_or(Error::Message("Expected identifier".into()))?
.id
.as_str();
let identifier = IdentifierParser::new().parse(identifier)?;
let mutability = self.get_mutability(&identifier);
Ok(Object { identifier, mutability, ..Default::default() })
let identifier_parser = IdentifierParser::new();
let identifier = identifier_parser.parse(identifier)?;
let mutability = identifier_parser.get_mutability(&identifier);
let literal = Err(Error::Message("Not implemented".into()))?;
let type_ = Err(Error::Message("Not implemented".into()))?;
Ok(Object { identifier, mutability, literal, type_ })
}
}

Expand All @@ -53,12 +54,3 @@ impl Parser<&StmtAssign> for FullParser {
}
}

impl FullParser {
fn get_mutability(&self, identifier: &Identifier) -> Mutability {
if identifier.name.to_uppercase() == identifier.name {
Mutability::Constant
} else {
Mutability::Mutable
}
}
}
16 changes: 2 additions & 14 deletions ecosystem/python/parser/src/object/symbol_parser.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use ligen::ir::Mutability;
use rustpython_parser::ast::{Expr, StmtAnnAssign, StmtAssign, StmtAugAssign};
use ligen::ir::{Object, Identifier};
use ligen::ir::Object;
use crate::identifier::IdentifierParser;
use crate::prelude::*;

Expand Down Expand Up @@ -34,8 +33,7 @@ impl Parser<&Expr> for SymbolParser {
.id
.as_str();
let identifier = IdentifierParser::new().parse(identifier)?;
let mutability = self.get_mutability(&identifier);
Ok(Object { identifier, mutability, ..Default::default() })
Ok(Object { identifier, ..Default::default() })
}
}

Expand All @@ -51,13 +49,3 @@ impl Parser<&StmtAssign> for SymbolParser {
Ok(objects)
}
}

impl SymbolParser {
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,5 +1,5 @@
use crate::{prelude::*, identifier::IdentifierParser};
use ligen::ir::TypeDefinition;
use ligen::ir::{TypeDefinition, Visibility};
use rustpython_parser::ast::StmtClassDef;

use super::DynamicParser;
Expand All @@ -12,7 +12,11 @@ impl<'a> DynamicParser<'a> for FullParser {}
impl Parser<WithSource<StmtClassDef>> for FullParser {
type Output = TypeDefinition;
fn parse(&self, input: WithSource<StmtClassDef>) -> Result<Self::Output> {
let attributes = Err(Error::Message("Not implemented".into()))?;
let identifier = IdentifierParser::new().parse(input.ast.name.as_str())?;
Ok(TypeDefinition { identifier, ..Default::default() })
let visibility = Visibility::Public;
let definition = Err(Error::Message("Not implemented".into()))?;
let interfaces = Err(Error::Message("Not implemented".into()))?;
Ok(TypeDefinition { attributes, visibility, identifier, definition, interfaces })
}
}

0 comments on commit a4d7c19

Please sign in to comment.