Parse arguments

This commit is contained in:
mars 2022-10-10 15:55:51 -06:00
parent 8cbd303510
commit a6cfb7c180
2 changed files with 21 additions and 7 deletions

View File

@ -12,8 +12,9 @@ use style::Stylesheet;
fn main() {
let src_path = std::env::args().nth(1).unwrap();
let src = std::fs::read_to_string(src_path).unwrap();
let options =
lexpr::parse::Options::new().with_string_syntax(lexpr::parse::StringSyntax::Elisp);
let options = lexpr::parse::Options::new()
.with_string_syntax(lexpr::parse::StringSyntax::Elisp)
.with_keyword_syntax(lexpr::parse::KeywordSyntax::ColonPrefix);
let expr = lexpr::from_str_custom(&src, options).unwrap();
println!("{:#?}", expr);

View File

@ -9,11 +9,12 @@ pub struct Parser {
impl Parser {
pub fn parse(document: &Value) -> Dom {
let parser = Self::from_value(document);
let mut parser = Self::from_value(document);
let (args, children) = parser.parse_args();
let mut tags = Vec::new();
for val in parser.vals.into_iter().rev() {
for val in children.into_iter() {
let mut parser = Self::from_value(&val);
let kind = parser.parse_block_kind();
let tag = parser.parse_block_tag(kind);
@ -35,9 +36,21 @@ impl Parser {
}
pub fn parse_args(&mut self) -> (Vec<(String, Value)>, Vec<Value>) {
let args = Vec::new();
let mut children = std::mem::replace(&mut self.vals, Vec::new());
children.reverse();
let mut args = Vec::new();
let mut children = Vec::new();
while let Some(next) = self.vals.pop() {
if let Value::Keyword(arg_name) = next {
let arg_val = self.vals.pop().expect("Expected value after keyword");
args.push((arg_name.to_string(), arg_val));
} else {
children.push(next);
break;
}
}
children.extend(self.vals.drain(..).rev());
(args, children)
}