Skip to content

Commit

Permalink
Parsing project name
Browse files Browse the repository at this point in the history
  • Loading branch information
notdanilo committed Oct 25, 2023
1 parent 498d264 commit 9f47ac7
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
15 changes: 8 additions & 7 deletions ecosystem/python/parser/src/module/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ impl Parser<WithSource<ModModule>> for PythonParser {
}
}

struct Directory<'a>(pub &'a std::path::Path);
struct File<'a>(pub &'a std::path::Path);
pub(crate) struct Directory<'a>(pub &'a std::path::Path);
pub(crate) struct File<'a>(pub &'a std::path::Path);
pub(crate) struct SubPath<'a>(pub &'a std::path::Path);

impl Parser<File<'_>> for PythonParser {
type Output = Module;
Expand Down Expand Up @@ -58,7 +59,7 @@ impl Parser<Directory<'_>> for PythonParser {
.map(String::from)
.unwrap_or_default();
if extension == "py" || extension == "pyi" || path.is_dir() {
if let Ok(module) = self.parse(path.as_path()) {
if let Ok(module) = self.parse(SubPath(path.as_path())) {
if let Some(existing) = modules
.iter_mut()
.find(|existing| existing.identifier == module.identifier)
Expand All @@ -84,13 +85,13 @@ impl Parser<Directory<'_>> for PythonParser {
}
}

impl Parser<&std::path::Path> for PythonParser {
impl Parser<SubPath<'_>> for PythonParser {
type Output = Module;
fn parse(&self, input: &std::path::Path) -> Result<Self::Output> {
fn parse(&self, SubPath(input): SubPath<'_>) -> Result<Self::Output> {
if input.is_dir() {
self.parse(Directory(input))
} else {
self.parse(File(input)).map_err(|error| Error::Message(format!("Failed to read {}. Cause: {:?}", input.display(), error)))
}
}
}
}
}
13 changes: 13 additions & 0 deletions ecosystem/python/parser/src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use crate::{prelude::*, module::SubPath};
use crate::object::ObjectParser;
use crate::function::FunctionParser;
use crate::identifier::IdentifierParser;
use crate::types::type_definition::TypeDefinitionParser;

use ligen::ir::Project;

#[derive(Default)]
pub struct PythonParser {
pub identifier_parser: IdentifierParser,
Expand All @@ -24,3 +27,13 @@ impl PythonParser {
Self { identifier_parser, function_parser, type_definition_parser, object_parser }
}
}

impl Parser<&std::path::Path> for PythonParser {
type Output = Project;
fn parse(&self, input: &std::path::Path) -> Result<Self::Output> {
let name = self.identifier_parser.parse(input)?;
let name = name.name.as_str().try_into()?;
let root_module = self.parse(SubPath(input))?;
Ok(Project { name, root_module })
}
}
3 changes: 1 addition & 2 deletions tools/editor/src/gui/ui/layout/editor/ir/menu_button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ impl MenuButton for EditorMenuButton {

if let Some(entry) = entry {
stacker::grow(1024 * 1024 * 10, || {
let root_module = PythonParser::full().parse(entry.as_path()).unwrap();
let project = Project { root_module, ..Default::default() };
let project = PythonParser::full().parse(entry.as_path()).unwrap();
panes.new_pane(Box::new(Editor::new(project)));
});
}
Expand Down
3 changes: 1 addition & 2 deletions tools/editor/src/gui/ui/layout/editor/symbols/menu_button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ impl MenuButton for EditorMenuButton {

if let Some(entry) = entry {
stacker::grow(1024 * 1024 * 10, || {
let root_module = PythonParser::symbol().parse(entry.as_path()).unwrap();
let project = Project { root_module, ..Default::default() };
let project = PythonParser::symbol().parse(entry.as_path()).unwrap();
panes.new_pane(Box::new(Editor::new(project)));
});
}
Expand Down

0 comments on commit 9f47ac7

Please sign in to comment.