Skip to content

Commit

Permalink
Adding ParserConfig UI
Browse files Browse the repository at this point in the history
  • Loading branch information
notdanilo committed Nov 6, 2023
1 parent 042dfc3 commit 69b9754
Show file tree
Hide file tree
Showing 14 changed files with 157 additions and 71 deletions.
2 changes: 1 addition & 1 deletion ecosystem/python/parser/src/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub mod parameter;
pub mod method;

use crate::prelude::*;
use ligen::parsing::parser::{ParserConfig, ParserConfigGet};
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;
Expand Down
2 changes: 1 addition & 1 deletion ecosystem/python/parser/src/object.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ligen::parsing::parser::{ParserConfig, ParserConfigGet};
use ligen::parsing::parser::ParserConfig;
use rustpython_parser::ast::{Expr, StmtAnnAssign, StmtAssign, StmtAugAssign};
use ligen::ir::Object;
use crate::identifier::IdentifierParser;
Expand Down
8 changes: 7 additions & 1 deletion ecosystem/python/parser/src/parser/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl<T> From<T> for PythonParserConfig<T> {
}
}

impl<T: Default + ParserConfigSet> Default for PythonParserConfig<T> {
impl Default for PythonParserConfig<ParserConfig> {
fn default() -> Self {
let config = Default::default();
let mut config = Self { config };
Expand All @@ -23,6 +23,12 @@ impl<T: Default + ParserConfigSet> Default for PythonParserConfig<T> {
}
}

impl From<PythonParserConfig<ParserConfig>> for ParserConfig {
fn from(config: PythonParserConfig<ParserConfig>) -> Self {
config.config
}
}

impl PythonParserConfig<ParserConfig> {
pub fn new() -> PythonParserConfig<ParserConfig> {
Default::default()
Expand Down
6 changes: 6 additions & 0 deletions ecosystem/python/parser/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,10 @@ impl Parser<&std::path::Path> for PythonParser {
let root_module = self.parse(SubPath(input), config)?;
Ok(Library { identifier: name, root_module })
}
fn name(&self) -> &str {
"Python"
}
fn config(&self) -> ParserConfig {
PythonParserConfig::default().into()
}
}
10 changes: 3 additions & 7 deletions ecosystem/python/parser/src/scope/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
mod scope_type;

use rustpython_parser::ast::{Arguments, Expr, Stmt};
use ligen::{ir::{Interface, Object, Function, Method, TypeDefinition}, parsing::parser::{ParserConfig, ParserConfigGet}};
use crate::prelude::*;
use ligen::{ir::{Interface, Object, Function, Method, TypeDefinition}, parsing::parser::ParserConfig};
use crate::{prelude::*, parser::PythonParserConfig};

pub use scope_type::*;
use crate::parser::PythonParser;
Expand Down Expand Up @@ -162,11 +162,7 @@ impl PythonParser {

fn parse_objects(&self, statements: &WithSource<&[Stmt]>, config: &ParserConfig) -> Result<Vec<Object>> {
let mut objects = Vec::new();
let class_variables_as_properties = config
.get("class_variables_as_properties")
.and_then(|x| x.as_boolean())
.cloned()
.unwrap_or_default();
let class_variables_as_properties = PythonParserConfig::from(config).get_class_variables_as_properties();
if !class_variables_as_properties {
for statement in statements.ast {
match statement {
Expand Down
2 changes: 1 addition & 1 deletion ecosystem/python/parser/src/types/type_definition.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{prelude::*, identifier::IdentifierParser, macro_attributes::attributes::AttributesParser, function::FunctionParser, types::type_::TypeParser, parser::PythonParserConfig};
use ligen::{ir::{TypeDefinition, Visibility, Path, KindDefinition, Structure, Attribute, Field}, parsing::parser::{ParserConfig, ParserConfigGet}};
use ligen::{ir::{TypeDefinition, Visibility, Path, KindDefinition, Structure, Attribute, Field}, parsing::parser::ParserConfig};
use ligen::ir::macro_attributes::Group;
use rustpython_parser::ast::{StmtClassDef, Expr, Stmt, StmtAnnAssign, StmtAugAssign, StmtAssign};

Expand Down
4 changes: 2 additions & 2 deletions ligen/parsing/src/parser/config/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ use ligen_ir::{Literal, Path};

use crate::prelude::*;

#[derive(Serialize, Deserialize, Clone)]
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(untagged)]
pub enum Value {
Literal(Literal),
Group(Group)
}

#[derive(Default, Serialize, Deserialize, Clone)]
#[derive(Debug, Default, Serialize, Deserialize, Clone)]
pub struct Group {
#[serde(flatten)]
map: HashMap<String, Value>
Expand Down
26 changes: 13 additions & 13 deletions ligen/parsing/src/parser/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::prelude::*;

use ligen_ir::{Literal, Path};

#[derive(Serialize, Deserialize, Clone)]
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ParserConfig {
#[serde(flatten)]
map: Group
Expand All @@ -24,29 +24,29 @@ impl ParserConfig {
pub fn iter(&self) -> impl Iterator<Item = (Path, Literal)> {
self.map.iter()
}
}

pub trait ParserConfigGet {
/// Gets the value at the given path.
fn get<P: Into<Path>>(&self, path: P) -> Option<&Literal>;
/// Sets whether to parse all symbols or only the ones that are explicitly marked as such.
pub fn set_only_parse_symbols(&mut self, value: bool) {
self.set("ligen::only-parse-symbols", value);
}

/// Whether to parse all symbols or only the ones that are explicitly marked as such.
fn get_only_parse_symbols(&self) -> bool {
pub fn get_only_parse_symbols(&self) -> bool {
self.get("ligen::only-parse-symbols")
.and_then(|literal| literal.as_boolean())
.cloned()
.unwrap_or(false)
}
}
}

pub trait ParserConfigGet {
/// Gets the value at the given path.
fn get<P: Into<Path>>(&self, path: P) -> Option<&Literal>;
}

pub trait ParserConfigSet {
/// Sets the value at the given path.
fn set<P: Into<Path>, L: Into<Literal>>(&mut self, path: P, value: L);

/// Sets whether to parse all symbols or only the ones that are explicitly marked as such.
fn set_only_parse_symbols(&mut self, value: bool) {
self.set("ligen::only-parse-symbols", value);
}
fn set<P: Into<Path>, L: Into<Literal>>(&mut self, path: P, value: L);
}

impl ParserConfig {
Expand Down
6 changes: 6 additions & 0 deletions ligen/parsing/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@ use ligen_common::Result;
pub trait Parser<Input> {
type Output;
fn parse(&self, input: Input, config: &ParserConfig) -> Result<Self::Output>;
fn name(&self) -> &str {
"Parser"
}
fn config(&self) -> ParserConfig {
Default::default()
}
}
44 changes: 1 addition & 43 deletions tools/editor/src/gui/ui/layout/editor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,46 +10,4 @@ pub mod ir;
pub mod settings;
pub mod widget;
pub mod menu_button;
pub mod parsing;

#[derive(Default)]
pub struct Parsing {

}

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

impl Pane for Parsing {
fn title(&self) -> String {
"Parsing".to_string()
}
fn show(&mut self, ui: &mut ligen_gui_runtime::egui::Ui, pane_manager: &mut PaneManager) -> egui_tiles::UiResponse {
ui.indent("indent", |ui| {
ui.add_space(16.0);
if ui.button("Parse Python").clicked() {
let entry = rfd::FileDialog::new()
.pick_folder();

if let Some(entry) = entry {
stacker::grow(1024 * 1024 * 10, || {
let mut full_config = PythonParserConfig::new();
full_config.set_class_variables_as_properties(true);
let mut symbols_config = full_config.clone();
symbols_config.set_only_parse_symbols(true);
let parser = PythonParser::default();
let symbols = parser.parse(entry.as_path(), &symbols_config).unwrap();
let full = parser.parse(entry.as_path(), &full_config).unwrap();
pane_manager.new_pane(Box::new(Editor::new(symbols)));
pane_manager.new_pane(Box::new(Editor::new(full)));
});
}
}
});

Default::default()
}
}
pub mod parsing;
45 changes: 45 additions & 0 deletions tools/editor/src/gui/ui/layout/editor/parsing/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use crate::{prelude::*, gui::ui::panes::Pane};
use ligen_parsing::parser::ParserConfigSet;
use ligen_python_parser::parser::{PythonParserConfig, PythonParser};

pub mod parser;
pub use parser::*;

use crate::gui::ui::panes::PaneManager;

use super::{ir::Editor, widget::Widget, settings::Settings};

pub struct Parsing {
parsers: Vec<Parser>
}

impl Default for Parsing {
fn default() -> Self {
let parsers = vec![
Parser::new(Box::new(PythonParser::default()))
];
Self { parsers }
}
}

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

impl Pane for Parsing {
fn title(&self) -> String {
"Parsing".to_string()
}
fn show(&mut self, ui: &mut ligen_gui_runtime::egui::Ui, pane_manager: &mut PaneManager) -> egui_tiles::UiResponse {
let mut settings = Settings::default();
settings.editor.editable_fields = true;
for (index, parser) in self.parsers.iter_mut().enumerate() {
ui.push_id(index, |ui| {
parser.show(&settings, ui, pane_manager);
});
}
Default::default()
}
}
29 changes: 29 additions & 0 deletions tools/editor/src/gui/ui/layout/editor/parsing/parser/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use crate::{prelude::*, gui::ui::editor::ir::{Path, Literal}};
use ligen_parsing::parser::{self, ParserConfigSet};

use crate::gui::ui::editor::{widget::Widget, settings::Settings};

#[derive(Default)]
pub struct ParserConfig {}

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

impl Widget for ParserConfig {
type Input = parser::ParserConfig;
fn show(&mut self, settings: &Settings, ui: &mut egui::Ui, input: &mut Self::Input) {
ui.label("Configuration");
for (index, (mut path, mut literal)) in input.iter().enumerate() {
ui.push_id(index, |ui| {
ui.horizontal(|ui| {
Path::default().show(settings, ui, &mut path);
Literal::default().show(settings, ui, &mut literal);
input.set(path, literal);
});
});
}
}
}
40 changes: 40 additions & 0 deletions tools/editor/src/gui/ui/layout/editor/parsing/parser/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
pub mod config;
pub use config::*;

use crate::{prelude::*, gui::ui::{editor::{widget::Widget, settings::Settings, ir::Editor}, panes::PaneManager}};
use std::path::Path;
use ligen_parsing::parser::{self, Parser as ParserTrait, ParserConfigSet, ParserConfig, ParserConfigGet};
use ligen_python_parser::parser::{PythonParser, PythonParserConfig};

pub struct Parser {
parser: Box<dyn for<'a> parser::Parser<&'a Path, Output = ligen_ir::Library>>,
config: ParserConfig
}

impl Parser {
pub fn new(parser: Box<dyn for<'a> parser::Parser<&'a Path, Output = ligen_ir::Library>>) -> Self {
let config = parser.config();
Self { parser, config }
}
}

impl Widget for Parser {
type Input = PaneManager;
fn show(&mut self, settings: &Settings, ui: &mut egui::Ui, pane_manager: &mut PaneManager) {
ui.indent("indent", |ui| {
ui.add_space(16.0);
ui.label(self.parser.name());
config::ParserConfig::new().show(settings, ui, &mut self.config);
if ui.button("Parse").clicked() {
let entry = rfd::FileDialog::new()
.pick_folder();
if let Some(entry) = entry {
stacker::grow(1024 * 1024 * 10, || {
let library = self.parser.parse(entry.as_path(), &self.config).unwrap();
pane_manager.new_pane(Box::new(Editor::new(library)));
});
}
}
});
}
}
4 changes: 2 additions & 2 deletions tools/editor/src/gui/ui/layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use crate::gui::ui::menu::Menu;
use crate::gui::ui::panes::Panes;
use crate::prelude::*;

use self::editor::Parsing;
use self::editor::ir::Editor;
use self::editor::parsing::Parsing;

#[derive(Serialize, Deserialize)]
#[serde(default)] // if we add new fields, give them default values when deserializing old state
Expand All @@ -33,4 +33,4 @@ impl Layout {
self.menu.show(ctx, frame, &mut self.panes);
self.panes.show(ctx);
}
}
}

0 comments on commit 69b9754

Please sign in to comment.