Skip to content

Commit

Permalink
Increasing parsing coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
notdanilo committed Oct 30, 2023
1 parent 33e920e commit 31ee16f
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 94 deletions.
16 changes: 4 additions & 12 deletions ecosystem/python/parser/src/function/full_parser.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::prelude::*;
use rustpython_parser::ast::{Arguments, Expr, Ranged, Stmt, StmtAsyncFunctionDef, StmtFunctionDef};
use ligen::ir::{Function, Synchrony, Visibility, Parameter, Type, Attributes};
use rustpython_parser::ast::{Arguments, Expr, Stmt, StmtAsyncFunctionDef, StmtFunctionDef};
use ligen::ir::{Function, Synchrony, Visibility, Parameter, Type};
use crate::function::DynamicParser;
use crate::function::parameter::ParameterParser;
use crate::identifier::IdentifierParser;
Expand Down Expand Up @@ -29,7 +29,7 @@ impl Parser<&str> for FullParser {
impl Parser<WithSource<StmtFunctionDef>> for FullParser {
type Output = Function;
fn parse(&self, input: WithSource<StmtFunctionDef>) -> Result<Self::Output> {
let attributes = self.parse_attributes(input.sub(input.ast.decorator_list.clone()))?;
let attributes = AttributesParser::default().parse(input.sub(input.ast.decorator_list.clone()))?;
let visibility = Visibility::Public;
let synchrony = Synchrony::Synchronous;
let identifier = IdentifierParser::new().parse(input.ast.name.as_str())?;
Expand All @@ -45,7 +45,7 @@ impl Parser<WithSource<StmtAsyncFunctionDef>> for FullParser {
fn parse(&self, input: WithSource<StmtAsyncFunctionDef>) -> Result<Self::Output> {
let source = input.source;
let input = input.ast;
let attributes = self.parse_attributes(WithSource::new(source, input.decorator_list))?;
let attributes = AttributesParser::default().parse(WithSource::new(source, input.decorator_list))?;
let visibility = Visibility::Public;
let synchrony = Synchrony::Asynchronous;
let identifier = IdentifierParser::new().parse(input.name.as_str())?;
Expand All @@ -56,14 +56,6 @@ impl Parser<WithSource<StmtAsyncFunctionDef>> for FullParser {
}

impl FullParser {
fn parse_attributes(&self, attributes: WithSource<Vec<Expr>>) -> Result<Attributes> {
let source = if attributes.ast.is_empty() {
Default::default()
} else {
attributes.source[attributes.ast.first().unwrap().start().to_usize()..attributes.ast.last().unwrap().end().to_usize()].to_string()
};
AttributesParser::default().parse(source)
}
fn parse_inputs(&self, args: Arguments) -> Result<Vec<Parameter>> {
let mut parameters = Vec::new();
for arg in args.args {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,2 @@
use crate::literal::LiteralParser;
pub type AttributeParser = ligen::parsing::parser::universal::attributes::attribute::AttributeParser<LiteralParser>;

// use rustpython_parser::ast::ExprAttribute;
// use ligen::ir::Attribute;
// use crate::identifier::IdentifierParser;
// use crate::macro_attributes::attributes::AttributesParser;
// use crate::prelude::*;
//
// pub struct AttributeParser;
//
// impl Parser<WithSource<ExprAttribute>> for AttributeParser {
// type Output = Attribute;
// fn parse(&self, input: WithSource<ExprAttribute>) -> Result<Self::Output> {
// let source = input.source;
// let input = input.ast;
// let identifier = IdentifierParser::new().parse(input.attr)?;
// let attributes = AttributesParser::default().parse(WithSource::new(&source, input.value))?;
// Ok(Attribute::Group(identifier, attributes))
// }
// }
68 changes: 20 additions & 48 deletions ecosystem/python/parser/src/macro_attributes/attributes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,52 +1,24 @@
pub mod attribute;

use crate::prelude::*;
use crate::literal::LiteralParser;
pub type AttributesParser = ligen::parsing::parser::universal::attributes::AttributesParser<LiteralParser>;
use ligen::parsing::parser::universal::attributes::AttributesParser as InternalParser;
use ligen::ir::Attributes;
use rustpython_parser::ast::{Expr, Ranged};

// pub mod attribute;
//
// use rustpython_parser::ast::{Expr, ExprCall, Ranged};
// use ligen::ir::Attributes;
// use crate::macro_attributes::attributes::attribute::AttributeParser;
// use crate::prelude::*;
//
// pub struct AttributesParser;
//
// impl Parser<WithSource<Vec<Expr>>> for AttributesParser {
// type Output = Attributes;
// fn parse(&self, input: WithSource<Vec<Expr>>) -> Result<Self::Output> {
// let source = input.source;
// let input = input.ast;
// if !input.is_empty() {
// println!("{}", &source[input.first().unwrap().start().to_usize()..input.last().unwrap().end().to_usize()]);
// }
// let mut attributes = Attributes::default();
// for expr in input {
// if let Expr::Attribute(expr) = expr {
// attributes.attributes.push(AttributeParser.parse(WithSource::new(&source, expr))?);
// }
// }
// Ok(attributes)
// }
// }
//
// impl Parser<WithSource<Box<Expr>>> for AttributesParser {
// type Output = Attributes;
// fn parse(&self, input: WithSource<Box<Expr>>) -> Result<Self::Output> {
// let source = input.source;
// let input = input.ast;
// let mut attributes = Attributes::default();
// match input.as_ref() {
// Expr::Call(call) => attributes.attributes.append(&mut AttributesParser::default().parse(WithSource::new(&source, call.clone()))?.attributes),
// _ => ()
// }
// Ok(attributes)
// }
// }
//
// impl Parser<WithSource<ExprCall>> for AttributesParser {
// type Output = Attributes;
// fn parse(&self, _input: WithSource<ExprCall>) -> Result<Self::Output> {
// Ok(Default::default())
// }
// }
#[derive(Default)]
pub struct AttributesParser {
parser: InternalParser<LiteralParser>
}

impl Parser<WithSource<Vec<Expr>>> for AttributesParser {
type Output = Attributes;
fn parse(&self, input: WithSource<Vec<Expr>>) -> Result<Self::Output> {
let source = if input.ast.is_empty() {
Default::default()
} else {
input.source[input.ast.first().unwrap().start().to_usize()..input.ast.last().unwrap().end().to_usize()].to_string()
};
self.parser.parse(source)
}
}
4 changes: 2 additions & 2 deletions ecosystem/python/parser/src/object/full_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ impl Parser<&Expr> for FullParser {
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()))?;
let type_ = Default::default();
let literal = Default::default();
Ok(Object { identifier, mutability, literal, type_ })
}
}
Expand Down
9 changes: 8 additions & 1 deletion ecosystem/python/parser/src/types/type_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@ use rustpython_parser::ast::ExprName;
use ligen::ir::{Type, Primitive, Integer, Float};
use crate::prelude::*;

#[derive(Default)]
pub struct TypeParser;

impl TypeParser {
pub fn new() -> Self {
Default::default()
}
}

impl Parser<ExprName> for TypeParser {
type Output = Type;
fn parse(&self, input: ExprName) -> Result<Self::Output> {
Expand All @@ -14,7 +21,7 @@ impl Parser<ExprName> for TypeParser {
"byte" => Ok(Integer::I8.into()),
"int" => Ok(Integer::I32.into()),
"float" => Ok(Float::F32.into()),
_ => Err(Error::Message(format!("Unknown type: {}", name)))
name => Ok(Type::Composite(name.into(), Default::default()))
}
}
}
43 changes: 36 additions & 7 deletions ecosystem/python/parser/src/types/type_definition/full_parser.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{prelude::*, identifier::IdentifierParser};
use ligen::ir::{TypeDefinition, Visibility};
use rustpython_parser::ast::StmtClassDef;
use crate::{prelude::*, identifier::IdentifierParser, macro_attributes::attributes::AttributesParser, function::FunctionParser};
use ligen::ir::{TypeDefinition, Visibility, Path, KindDefinition, Structure, Attribute, Field};
use rustpython_parser::ast::{StmtClassDef, Expr};

use super::DynamicParser;

Expand All @@ -12,11 +12,40 @@ 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 attributes = AttributesParser::default().parse(input.sub(input.ast.decorator_list.clone()))?;
let identifier = IdentifierParser::new().parse(input.ast.name.as_str())?;
let visibility = Visibility::Public;
let definition = Err(Error::Message("Not implemented".into()))?;
let interfaces = Err(Error::Message("Not implemented".into()))?;
let interfaces = self.parse_interfaces(&input.ast.bases)?;
let definition = self.parse_kind_definition(&input)?;
Ok(TypeDefinition { attributes, visibility, identifier, definition, interfaces })
}
}
}

impl FullParser {
fn parse_interfaces(&self, input: &Vec<Expr>) -> Result<Vec<Path>> {
let mut interfaces = Vec::new();
for expr in input {
if let Some(expr) = expr.as_name_expr() {
interfaces.push(IdentifierParser::default().parse(expr.id.as_str())?.into());
}
}
Ok(interfaces)
}

fn parse_kind_definition(&self, input: &WithSource<StmtClassDef>) -> Result<KindDefinition> {
let mut fields = Vec::new();
for stmt in &input.ast.body {
if let Some(function_def) = stmt.as_function_def_stmt() {
let function = FunctionParser::full().parse(input.sub(function_def.clone())).expect("Aqui");
if function.attributes.contains(&Attribute::Group("property".into(), Default::default())) {
let identifier = Some(function.identifier);
let type_ = function.output.unwrap_or_default();
let field = Field { identifier, type_, ..Default::default() };
fields.push(field);
}
}
}
let structure = Structure { fields };
Ok(structure.into())
}
}
8 changes: 6 additions & 2 deletions ligen/ir/src/literal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ pub enum Literal {
UnsignedInteger(u64),
/// Float variant
Float(f64),
/// None variant
None
}

impl Literal {
Expand All @@ -31,6 +33,7 @@ impl Literal {
Literal::Integer(_) => type_.is_integer(),
Literal::UnsignedInteger(_) => type_.is_unsigned_integer(),
Literal::Float(_) => type_.is_float(),
Literal::None => false
}
}

Expand All @@ -48,14 +51,14 @@ impl Literal {
} else if type_.is_float() {
Self::Float(0.0)
} else {
Self::String(Default::default())
Self::None
}
}
}

impl Default for Literal {
fn default() -> Self {
Self::String(String::default())
Self::None
}
}

Expand Down Expand Up @@ -122,6 +125,7 @@ impl std::fmt::Display for Literal {
Literal::Integer(value) => write!(f, "{}", value),
Literal::UnsignedInteger(value) => write!(f, "{}", value),
Literal::Float(value) => write!(f, "{}", value),
Literal::None => write!(f, "None")
}
}
}
5 changes: 2 additions & 3 deletions ligen/parsing/src/parser/universal/identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ impl Parser<String> for IdentifierParser {
impl Parser<&str> for IdentifierParser {
type Output = Identifier;
fn parse(&self, input: &str) -> Result<Self::Output> {
syn::parse_str::<syn::Ident>(input)
.map_err(|e| Error::Message(format!("Failed to parse identifier: {:?}", e)))
.and_then(|ident| self.parse(ident))
let name = input.into();
Ok(Identifier { name })
}
}

Expand Down
4 changes: 4 additions & 0 deletions tools/editor/src/gui/ui/layout/editor/ir/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ impl Widget for Literal {
ligen_ir::Literal::Float(_) => "Float",
ligen_ir::Literal::Character(_) => "Char",
ligen_ir::Literal::UnsignedInteger(_) => "Unsigned Integer",
ligen_ir::Literal::None => "None"
};
ui.horizontal_top(|ui| {
ComboBox::new("Literal", "")
Expand Down Expand Up @@ -57,6 +58,9 @@ impl Widget for Literal {
},
ligen_ir::Literal::Float(value) => {
ui.add(egui::DragValue::new(value));
},
ligen_ir::Literal::None => {
ui.label("None");
}
}
});
Expand Down

0 comments on commit 31ee16f

Please sign in to comment.