Skip to content

Commit

Permalink
Removing DynamicParser and using ParserConfig instead
Browse files Browse the repository at this point in the history
  • Loading branch information
notdanilo committed Nov 5, 2023
1 parent 69b4e84 commit de18d57
Show file tree
Hide file tree
Showing 14 changed files with 157 additions and 381 deletions.
75 changes: 0 additions & 75 deletions ecosystem/python/parser/src/function/full_parser.rs

This file was deleted.

94 changes: 78 additions & 16 deletions ecosystem/python/parser/src/function/mod.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,84 @@
use ligen::parsing::dynamic_parser;
use rustpython_parser::ast::{StmtAsyncFunctionDef, StmtFunctionDef};
use ligen::ir::Function;
use crate::prelude::*;

pub mod parameter;
pub mod method;

mod full_parser;
mod symbol_parser;

dynamic_parser!{
FunctionParser,
full_parser::FullParser,
symbol_parser::SymbolParser,
Function,
WithSource<StmtFunctionDef>,
WithSource<StmtAsyncFunctionDef>,
&str | &'a str
use crate::prelude::*;
use ligen::parsing::parser::ParserConfig;
use rustpython_parser::ast::{Arguments, Expr, Stmt, StmtAsyncFunctionDef, StmtFunctionDef};
use ligen::ir::{Function, Synchrony, Visibility, Parameter, Type};
use crate::function::parameter::ParameterParser;
use crate::identifier::IdentifierParser;
use crate::macro_attributes::attributes::AttributesParser;
use crate::types::type_::TypeParser;


#[derive(Default)]
pub struct FunctionParser;

impl Parser<&str> for FunctionParser {
type Output = Function;
fn parse(&self, input: &str, config: &ParserConfig) -> Result<Self::Output> {
let statement = Stmt::parse(input, "<embedded>")
.map_err(|error| Error::Message(format!("Failed to parse statement: {}", error)))?;
match statement {
Stmt::FunctionDef(function) => self.parse(WithSource::new(input, function), config),
Stmt::AsyncFunctionDef(function) => self.parse(WithSource::new(input, function), config),
_ => Err(Error::Message("No function found".into()))
}
}
}

impl Parser<WithSource<StmtFunctionDef>> for FunctionParser {
type Output = Function;
fn parse(&self, input: WithSource<StmtFunctionDef>, config: &ParserConfig) -> Result<Self::Output> {
let identifier = IdentifierParser::new().parse(input.ast.name.as_str(), config)?;
if config.only_parse_symbols() {
Ok(Function { identifier, ..Default::default() })
} else {
let attributes = AttributesParser::default().parse(input.sub(input.ast.decorator_list.clone()), config)?;
let visibility = Visibility::Public;
let synchrony = Synchrony::Synchronous;
let inputs = self.parse_inputs(*input.ast.args, config)?;
let output = self.parse_output(input.ast.returns, config)?;
Ok(Function { attributes, visibility, synchrony, identifier, inputs, output })
}
}
}

impl Parser<WithSource<StmtAsyncFunctionDef>> for FunctionParser {
type Output = Function;
fn parse(&self, input: WithSource<StmtAsyncFunctionDef>, config: &ParserConfig) -> Result<Self::Output> {
let source = input.source;
let input = input.ast;
let identifier = IdentifierParser::new().parse(input.name.as_str(), config)?;
if config.only_parse_symbols() {
Ok(Function { identifier, ..Default::default() })
} else {
let attributes = AttributesParser::default().parse(WithSource::new(source, input.decorator_list), config)?;
let visibility = Visibility::Public;
let synchrony = Synchrony::Asynchronous;
let inputs = self.parse_inputs(*input.args, config)?;
let output = self.parse_output(input.returns, config)?;
Ok(Function { attributes, visibility, synchrony, identifier, inputs, output })
}
}
}

impl FunctionParser {
fn parse_inputs(&self, args: Arguments, config: &ParserConfig) -> Result<Vec<Parameter>> {
let mut parameters = Vec::new();
for arg in args.args {
parameters.push(ParameterParser.parse(arg, config)?);
}
Ok(parameters)
}

fn parse_output(&self, output: Option<Box<Expr>>, config: &ParserConfig) -> Result<Option<Type>> {
if let Some(expr) = output.and_then(|expr| expr.name_expr()) {
Ok(Some(TypeParser::default().parse(&expr, config)?))
} else {
Ok(None)
}
}
}

#[cfg(test)]
Expand Down
41 changes: 0 additions & 41 deletions ecosystem/python/parser/src/function/symbol_parser.rs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,28 @@ use crate::identifier::IdentifierParser;
use crate::prelude::*;
use crate::types::type_::TypeParser;

use super::DynamicParser;

impl<'a> DynamicParser<'a> for FullParser {}

#[derive(Default)]
pub struct FullParser;
pub struct ObjectParser;

impl Parser<&StmtAnnAssign> for FullParser {
impl Parser<&StmtAnnAssign> for ObjectParser {
type Output = Object;
fn parse(&self, input: &StmtAnnAssign, config: &ParserConfig) -> Result<Self::Output> {
let mut object = self.parse(input.target.as_ref(), config)?;
object.type_ = TypeParser::new().parse(&*input.annotation, config)?;
if !config.only_parse_symbols() {
object.type_ = TypeParser::new().parse(&*input.annotation, config)?;
}
Ok(object)
}
}

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

impl Parser<&Expr> for FullParser {
impl Parser<&Expr> for ObjectParser {
type Output = Object;
fn parse(&self, expr: &Expr, config: &ParserConfig) -> Result<Self::Output> {
let identifier = expr
Expand All @@ -38,14 +36,18 @@ impl Parser<&Expr> for FullParser {
.as_str();
let identifier_parser = IdentifierParser::new();
let identifier = identifier_parser.parse(identifier, config)?;
let mutability = identifier_parser.get_mutability(&identifier);
let type_ = Default::default();
let literal = Default::default();
Ok(Object { identifier, mutability, literal, type_ })
if config.only_parse_symbols() {
Ok(Object { identifier, ..Default::default() })
} else {
let mutability = identifier_parser.get_mutability(&identifier);
let type_ = Default::default();
let literal = Default::default();
Ok(Object { identifier, mutability, literal, type_ })
}
}
}

impl Parser<&StmtAssign> for FullParser {
impl Parser<&StmtAssign> for ObjectParser {
type Output = Vec<Object>;
fn parse(&self, input: &StmtAssign, config: &ParserConfig) -> Result<Self::Output> {
let mut objects = Vec::new();
Expand Down
17 changes: 0 additions & 17 deletions ecosystem/python/parser/src/object/mod.rs

This file was deleted.

52 changes: 0 additions & 52 deletions ecosystem/python/parser/src/object/symbol_parser.rs

This file was deleted.

10 changes: 1 addition & 9 deletions ecosystem/python/parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,9 @@ pub struct PythonParser {
}

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

pub fn symbol() -> Self {
let identifier_parser = IdentifierParser::new();
let function_parser = FunctionParser::symbol();
let type_definition_parser = TypeDefinitionParser::symbol();
let object_parser = ObjectParser::symbol();
Self { identifier_parser, function_parser, type_definition_parser, object_parser }
}
}

impl Parser<&std::path::Path> for PythonParser {
Expand Down
Loading

0 comments on commit de18d57

Please sign in to comment.