Skip to content

Commit

Permalink
formalize grammar definition process
Browse files Browse the repository at this point in the history
also includes some WIP changes for getting saving/loading working

there's still an issue with the top-level defined grammar not being
included in the suggestions. i suspect it's because the name is
overwritten at some point.
  • Loading branch information
Korede-TA committed Apr 29, 2020
1 parent f6b620a commit 4b50ad2
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 34 deletions.
1 change: 0 additions & 1 deletion src/coordinate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ impl Coordinate {
pub fn child_of(parent: &Self, child_coord: (NonZeroU32, NonZeroU32)) -> Coordinate {
let mut new_row_col = parent.clone().row_cols;
new_row_col.push(child_coord);
info!("pareb = {:?}, child_coord = {:?}", parent, child_coord);

Coordinate {
row_cols: new_row_col,
Expand Down
18 changes: 13 additions & 5 deletions src/grammar_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct GrammarMap(HashMap<Coordinate, Grammar>);
pub enum MapEntry {
G(Grammar),
// using `Box` here is necessary so Rust can infer a proper size of enum
Grid(Vec<Vec<Box<MapEntry>>>),
Grid(String /* name */, Vec<Vec<Box<MapEntry>>>),
}

pub fn build_grammar_map(
Expand All @@ -32,7 +32,7 @@ pub fn build_grammar_map(
MapEntry::G(grammar) => {
map.insert(root_coord, grammar);
}
MapEntry::Grid(entry_table) => {
MapEntry::Grid(name, entry_table) => {
let mut sub_coords = vec![];
let mut num_rows = 0;
let mut num_cols = 0;
Expand All @@ -53,7 +53,7 @@ pub fn build_grammar_map(
map.insert(
root_coord,
Grammar {
name: String::new(),
name,
style: {
let mut s = Style::default();
s.width = 90.0 * (num_cols as f64);
Expand All @@ -77,12 +77,20 @@ macro_rules! g {
#[macro_export]
macro_rules! grid {
[ $( [ $( $d:expr ),* ] ),* ] => {
MapEntry::Grid(vec![
MapEntry::Grid(String::new(), vec![
$(
vec![$(Box::new($d)),*],
)*
])
}
};

($name:expr, [ $( [ $( $d:expr ),* ] ),* ]) => {
MapEntry::Grid($name, vec![
$(
vec![$(Box::new($d)),*],
)*
])
};
}

// #[cfg(test)]
Expand Down
120 changes: 92 additions & 28 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,28 +365,52 @@ impl Component for Model {
coord!("root"),
grid![
[
g!(Grammar::input("", "")),
g!(Grammar::input("", "")),
g!(Grammar::input("", "")),
g!(Grammar::input("", "")),
g!(Grammar::input("", "")),
g!(Grammar::input("", ""))
],
[
g!(Grammar::input("", "")),
g!(Grammar::input("", "")),
g!(Grammar::input("", "")),
g!(Grammar::input("", "")),
g!(Grammar::input("", "")),
g!(Grammar::input("", ""))
],
[
g!(Grammar::input("", "")),
g!(Grammar::input("", "")),
grid![
[
g!(Grammar::text("", "LAMBDA")),
g!(Grammar::input("", "flags"))
],
[
g!(Grammar::input("", "input")),
g!(Grammar::input("", "output"))
]
]
g!(Grammar::input("", "")),
g!(Grammar::input("", "")),
g!(Grammar::input("", "")),
g!(Grammar::input("", ""))
],
[
g!(Grammar::input("", "")),
g!(Grammar::input("", "")),
g!(Grammar::input("", "")),
g!(Grammar::input("", "")),
g!(Grammar::input("", "")),
g!(Grammar::input("", ""))
],
[
g!(Grammar::input("", "")),
g!(Grammar::input("", "")),
g!(Grammar::input("", "")),
g!(Grammar::input("", "")),
g!(Grammar::input("", "")),
g!(Grammar::input("", ""))
],
[
g!(Grammar::input("", "")),
g!(Grammar::input("", "")),
g!(Grammar::input("", "")),
g!(Grammar::input("", "")),
g!(Grammar::input("", "")),
g!(Grammar::input("", ""))
]
],
);
Expand All @@ -398,7 +422,39 @@ impl Component for Model {
[g!(Grammar::input("", "A2"))],
[g!(Grammar::default_button())],
[g!(Grammar::default_slider())],
[g!(Grammar::default_toggle())]
[g!(Grammar::default_toggle())],
[g!(Grammar::input("js", ""))],
[grid!(
"js::var".to_string(),
[[
g!(Grammar::text("", "var")),
g!(Grammar::input("", "")),
g!(Grammar::text("", "=")),
g!(Grammar::input("", ""))
]]
)],
[grid!(
"js::assn".to_string(),
[[
g!(Grammar::input("", "")),
g!(Grammar::text("", "=")),
g!(Grammar::input("", ""))
]]
)],
[grid!(
"js::function".to_string(),
[
[
g!(Grammar::text("function", "")),
g!(Grammar::text("", "(")),
g!(Grammar::input("", "")),
g!(Grammar::text("", ")")),
g!(Grammar::text("", "{"))
],
[g!(Grammar::input("", ""))],
[g!(Grammar::text("", "}"))]
]
)]
],
);
build_grammar_map(
Expand Down Expand Up @@ -434,7 +490,7 @@ impl Component for Model {
],
);
assert!(map.contains_key(&(coord!("root"))));
assert!(map.contains_key(&(coord!("root-C3-B2"))));
assert!(map.contains_key(&(coord!("meta-A6-A1"))));
map
},
}],
Expand Down Expand Up @@ -788,20 +844,19 @@ impl Component for Model {
true
}
Action::SaveSession() => {
/* TODO: uncomment when this is working
use node_sys::fs as node_fs;
use node_sys::Buffer;
use js_sys::{
JsString,
Function
};
/* TODO: new approach to saving session, should work better but there's still some
* compile errors to be handled later
let session = self.to_session();
let j = serde_json::to_string(&session.clone());
let filename = session.title.to_string();
let jsfilename = JsString::from(filename);
let jsbuffer = Buffer::from_string(&JsString::from(j.unwrap()), None);
let jscallback = Function::new_no_args("{}");
node_fs::append_file(&jsfilename, &jsbuffer, None, &jscallback);
let json = serde_json::to_string(&session.clone());
let filename = format! {"{}.json", session.title.to_string()};
let _ = js! {
let dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(@{&json}));
let dlAnchorElem = document.getElementById("downloadAnchorElem");
dlAnchorElem.setAttribute("href", dataStr);
dlAnchorElem.setAttribute("download", @{&filename});
dlAnchorElem.click();
return 0;
};
*/
false
}
Expand Down Expand Up @@ -1414,15 +1469,19 @@ impl Component for Model {
Action::ToggleLookup(coord) => {
match self.get_session_mut().grammars.get_mut(&coord) {
Some(
g @ Grammar {
g
@
Grammar {
kind: Kind::Input(_),
..
},
) => {
g.kind = Kind::Lookup("".to_string(), None);
}
Some(
g @ Grammar {
g
@
Grammar {
kind: Kind::Lookup(_, _),
..
},
Expand Down Expand Up @@ -1504,7 +1563,7 @@ impl Component for Model {
}
// copy definition from the coordinate it's being staged from, to the cell
let defn_coord = Coordinate::child_of(&(coord!("meta")), defn_meta_sub_coord);
info! {"Adding Definition: {} to {}", coord.to_string(), defn_coord.to_string()};
info! {"staging definition: {} to {}", coord.to_string(), defn_coord.to_string()};
move_grammar(self, coord.clone(), defn_coord.clone());
// rename the definition grammar to the definition name in the menu bar
if let Some(g) = self.get_session_mut().grammars.get_mut(&defn_coord) {
Expand Down Expand Up @@ -1563,6 +1622,7 @@ impl Component for Model {
}
// copy definition from the coordinate it's being staged from, to the cell
let defn_coord = Coordinate::child_of(&(coord!("meta")), defn_meta_sub_coord);
info! {"binding {} to defn {}", binding_coord.to_string(), defn_coord.to_string()};
move_grammar(self, binding_coord.clone(), defn_coord.clone());
true
} else {
Expand Down Expand Up @@ -1632,6 +1692,8 @@ impl Component for Model {
.collect(),
);

info! {"all suggestions: {:?}", self.suggestions};

should_render
}

Expand Down Expand Up @@ -1748,6 +1810,8 @@ impl Component for Model {
</div>
</div>

<a id="downloadAnchorElem" style="display:none"></a>

</div>
}
}
Expand Down

0 comments on commit 4b50ad2

Please sign in to comment.