Skip to content

Commit

Permalink
feat: import other bindings from files
Browse files Browse the repository at this point in the history
  • Loading branch information
lavafroth committed Jul 4, 2024
1 parent 2d06f50 commit c23baca
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
5 changes: 5 additions & 0 deletions modules/other.swhkd
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# something something
super + g
lololol

include hotkeys.swhkd
25 changes: 22 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use itertools::Itertools;
use pest::{iterators::Pair, Parser};
use pest_derive::Parser;
use range::Bounds;
use std::fmt::Display;
use std::{collections::BTreeSet, fmt::Display, fs};
use thiserror::Error;
mod range;
pub mod token;
Expand Down Expand Up @@ -33,21 +33,29 @@ pub struct Mode {
pub struct SwhkdParser {
pub bindings: Vec<Binding>,
pub unbinds: Vec<Definition>,
pub imports: Vec<String>,
pub imports: BTreeSet<String>,
pub modes: Vec<Mode>,
}

impl SwhkdParser {
pub fn from(raw: &str) -> Result<Self, ParseError> {
let mut root_imports = BTreeSet::new();
let mut root = Self::as_import(raw, &mut root_imports)?;
root.imports = root_imports;
Ok(root)
}
pub fn as_import(raw: &str, seen: &mut BTreeSet<String>) -> Result<Self, ParseError> {
let parse_result = SwhkdGrammar::parse(Rule::main, raw)
.map_err(|err| ParseError::Grammar(Box::new(err)))?;
// TODO: set the source for raw strings as `anonymous`

let Some(contents) = parse_result.into_iter().next() else {
return Err(ParseError::MainSection);
};

let mut bindings = vec![];
let mut unbinds = vec![];
let mut imports = vec![];
let mut imports = BTreeSet::new();
let mut modes = vec![];
for decl in contents.into_inner() {
match decl.as_rule() {
Expand All @@ -61,6 +69,17 @@ impl SwhkdParser {
_ => unreachable!(),
}
}

while let Some(import) = imports.pop_first() {
if !seen.insert(import.clone()) {
continue;
}
let child = Self::as_import(ParserInput::Path(Path::new(&import)), seen)?;
imports.extend(child.imports);
bindings.extend(child.bindings);
unbinds.extend(child.unbinds);
modes.extend(child.modes);
}
Ok(SwhkdParser {
bindings,
unbinds,
Expand Down

0 comments on commit c23baca

Please sign in to comment.