Skip to content

Commit

Permalink
Merge pull request #55 from sustech-cs304/refactor-parser
Browse files Browse the repository at this point in the history
Refactor parser
  • Loading branch information
vollate authored Apr 16, 2024
2 parents 8142c9d + 015d84b commit 5fed936
Show file tree
Hide file tree
Showing 33 changed files with 1,778 additions and 1,550 deletions.
6 changes: 6 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ change_current_tab(newpath: String) -> bool;
设置新聚焦 tab 的 filepath
当仅后端不存在该 tab 时返回 false,否则返回 true。

### assemble

```rust
assemble()->
```

## event

### front_file_open
Expand Down
23 changes: 23 additions & 0 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ tauri = { version = "1.6.0", features = [ "fs-read-dir", "fs-create-dir", "fs-ex
ropey = "1.6.1"
logos = "0.14.0"
lazy_static = "1.4.0"
strum = { version = "0.26", features = ["derive"] }

[features]
# this feature is used for production builds or when `devPath` points to the filesystem and the built-in dev server is disabled.
Expand Down
75 changes: 71 additions & 4 deletions src-tauri/src/interface/parser.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
pub trait Parser<CODE, IS>: Send + Sync
use serde::Serialize;

pub trait Parser<IS>: Send + Sync
where
IS: ParserInstSet,
{
fn parse(&mut self, code: &CODE) -> Result<ParserResult<IS>, Vec<ParserError>>;
fn parse(&mut self, code: String) -> Result<ParserResult<IS>, Vec<ParserError>>;
}

// in crate::modules::[instruction_set]::basic::interface::parser
Expand All @@ -15,7 +17,7 @@ where
type Operand;
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[derive(Serialize, Clone, Copy, Debug, PartialEq, Eq)]
pub struct Pos(pub usize, pub usize);

#[derive(Clone, Debug)]
Expand All @@ -27,7 +29,7 @@ where
pub text: Vec<ParserResultText<IS>>,
}

#[derive(Clone, Debug)]
#[derive(Serialize, Clone, Debug)]
pub struct ParserError {
pub pos: Pos,
pub msg: String,
Expand Down Expand Up @@ -57,3 +59,68 @@ where
pub op: IS::Operator,
pub opd: Vec<IS::Operand>,
}

impl<IS> std::fmt::Display for ParserResult<IS>
where
IS: ParserInstSet,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(
"ParserResult {\n\
data:\n",
)?;
for (i, d) in self.data.iter().enumerate() {
write!(f, "{:3} {}\n", i + 1, d.to_string())?;
}
f.write_str("text:\n")?;
for (i, t) in self.text.iter().enumerate() {
write!(f, "{:3} {}\n", i + 1, t.to_string())?;
}
Ok(())
}
}

impl std::fmt::Display for ParserResultData {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ParserResultData::Data(data) => {
f.write_str("Data: 0x")?;
for d in data {
write!(f, "{:02x}", d)?;
}
Ok(())
}
ParserResultData::Align(a) => write!(f, "Align {}", a),
}
}
}

impl<IS> std::fmt::Display for ParserResultText<IS>
where
IS: ParserInstSet,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ParserResultText::Text(inst) => {
write!(f, "{:3}: {:?}", inst.line + 1, inst.op)?;
for opd in &inst.opd {
write!(f, " {:?}", opd)?;
}
Ok(())
}
ParserResultText::Align(a) => write!(f, "Align {}", a),
}
}
}

impl std::fmt::Display for ParserError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"line:{} col:{} {}",
self.pos.0 + 1,
self.pos.1 + 1,
self.msg
)
}
}
6 changes: 5 additions & 1 deletion src-tauri/src/interface/storage.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
pub trait MFile<ERR>: Send + Sync {
fn get_string(&self) -> String;
fn is_dirty(&self) -> bool;

fn set_dirty(&mut self, dirty: bool);

fn to_string(&self) -> String;

fn save(&mut self) -> Option<ERR>;
}
18 changes: 8 additions & 10 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@
mod interface;
mod io;
mod menu;
mod middleware;
mod modules;
mod parser;
mod simulator;
mod storage;
mod test;
mod types;
mod utility;

use middleware::implementation::tab_management;
use modules::riscv;
use types::middleware_types;

fn main() {
Expand All @@ -36,12 +33,13 @@ fn main() {
Ok(())
})
.invoke_handler(tauri::generate_handler![
tab_management::create_tab,
tab_management::close_tab,
tab_management::change_current_tab,
tab_management::update_tab,
tab_management::read_tab,
tab_management::write_tab
riscv::middleware::tab_management::create_tab,
riscv::middleware::tab_management::close_tab,
riscv::middleware::tab_management::change_current_tab,
riscv::middleware::tab_management::update_tab,
riscv::middleware::tab_management::read_tab,
riscv::middleware::tab_management::write_tab,
riscv::middleware::frontend_api::assemble,
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
Expand Down
49 changes: 42 additions & 7 deletions src-tauri/src/menu/menu_file.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
use std::path::Path;

use tauri::{
api::dialog::{FileDialogBuilder, MessageDialogKind},
CustomMenuItem, Menu, Submenu, WindowMenuEvent,
CustomMenuItem, Manager, Menu, Submenu, WindowMenuEvent,
};

use super::display_alert_dialog;
use crate::{
io::file_io,
middleware::implementation::tab_management::new_tab,
types::menu_types,
utility::state_helper::{get_current_tab, get_current_tab_name},
modules::riscv::basic::parser::parser::RISCVParser,
storage::rope_store,
types::{
menu_types,
middleware_types::{Tab, TabMap},
},
utility::state_helper::event::{get_current_tab, get_current_tab_name, set_current_tab_name},
};

pub fn new() -> Submenu {
Expand Down Expand Up @@ -62,7 +68,7 @@ fn open_handler(event: WindowMenuEvent) {
|_| {},
),
_ => {
let content = get_current_tab(&event).as_ref().text.get_string();
let content = get_current_tab(&event).as_ref().text.to_string();
event
.window()
.emit(
Expand Down Expand Up @@ -102,7 +108,7 @@ fn save_handler<'a>(event: WindowMenuEvent) {
fn save_as_handler(event: WindowMenuEvent) {
let tab_ptr = get_current_tab(&event);
let tab = tab_ptr.as_ref();
let content = tab.text.get_string();
let content = tab.text.to_string();
let picker = tauri::api::dialog::FileDialogBuilder::new();
picker.save_file(move |file_path| match file_path {
Some(file_path) => match file_io::write_file(file_path.as_path(), &content) {
Expand All @@ -126,9 +132,38 @@ fn share_handler(event: WindowMenuEvent) {
todo!("Share file with socket");
}

fn close_handler(event: WindowMenuEvent) {}
fn close_handler(event: WindowMenuEvent) {
//TODO: check if the file is dirty
}

fn exit_handler(event: WindowMenuEvent) {
event.window().close().unwrap();
todo!("check all dirty file before exit");
}

fn new_tab(event: &WindowMenuEvent, file_path: &Path) -> Option<String> {
match rope_store::Text::from_path(file_path) {
Ok(content) => {
let tab_map = event.window().state::<TabMap>();
let tab = Tab {
text: Box::new(content),
parser: Box::new(RISCVParser::new()),
//assembler: Box::new(Default::default()),
//simulator: Box::new(Default::default()),
};
tab_map
.tabs
.lock()
.unwrap()
.insert(file_path.to_str().unwrap().to_string(), tab);
set_current_tab_name(&event, file_path.to_str().unwrap());
None
}
Err(e) => Some(e),
}
}

fn dirty_close_checker(event: &WindowMenuEvent, tab: &mut Tab) -> bool {
if tab.text.is_dirty() {}
true
}
1 change: 0 additions & 1 deletion src-tauri/src/middleware/backend_test.rs

This file was deleted.

1 change: 0 additions & 1 deletion src-tauri/src/middleware/frontend_test.rs

This file was deleted.

3 changes: 0 additions & 3 deletions src-tauri/src/middleware/mod.rs

This file was deleted.

Loading

0 comments on commit 5fed936

Please sign in to comment.