mirror of
https://github.com/tiffany352/rink-rs
synced 2024-11-10 13:44:15 +00:00
Add parsing for substance definitions
This commit is contained in:
parent
8b1c1a6186
commit
ed945e4b6e
3 changed files with 65 additions and 0 deletions
|
@ -81,6 +81,14 @@ pub enum DatePattern {
|
|||
Space,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Property {
|
||||
pub name: String,
|
||||
pub input: Expr,
|
||||
pub output: Expr,
|
||||
pub doc: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Def {
|
||||
Dimension,
|
||||
|
@ -89,6 +97,7 @@ pub enum Def {
|
|||
SPrefix(Expr),
|
||||
Unit(Expr),
|
||||
Quantity(Expr),
|
||||
Substance(Vec<Property>),
|
||||
Error(String),
|
||||
}
|
||||
|
||||
|
|
|
@ -25,6 +25,8 @@ pub enum Token {
|
|||
Dash,
|
||||
Asterisk,
|
||||
Question,
|
||||
LeftBrace,
|
||||
RightBrace,
|
||||
Error(String),
|
||||
}
|
||||
|
||||
|
@ -65,6 +67,8 @@ impl<'a> Iterator for TokenIterator<'a> {
|
|||
'-' => Token::Dash,
|
||||
'+' => Token::Plus,
|
||||
'*' => Token::Asterisk,
|
||||
'{' => Token::LeftBrace,
|
||||
'}' => Token::RightBrace,
|
||||
'?' => if self.0.peek() == Some(&'?') {
|
||||
self.0.next();
|
||||
let mut out = String::new();
|
||||
|
@ -315,6 +319,54 @@ pub fn parse(mut iter: &mut Iter) -> Defs {
|
|||
iter.next();
|
||||
let expr = parse_expr(iter);
|
||||
map.push((name, Rc::new(Def::Quantity(expr)), doc.take()));
|
||||
} else if let Some(&Token::LeftBrace) = iter.peek() {
|
||||
// substance
|
||||
iter.next();
|
||||
let mut props = vec![];
|
||||
let mut prop_doc = None;
|
||||
loop {
|
||||
let name = match iter.next().unwrap() {
|
||||
Token::Ident(name) => name,
|
||||
Token::Newline => {
|
||||
line += 1;
|
||||
continue
|
||||
},
|
||||
Token::Eof => break,
|
||||
Token::Doc(line) => {
|
||||
prop_doc = match prop_doc.take() {
|
||||
None => Some(line.trim().to_owned()),
|
||||
Some(old) => Some(format!(
|
||||
"{} {}", old.trim(), line.trim())),
|
||||
};
|
||||
continue
|
||||
},
|
||||
Token::RightBrace =>
|
||||
break,
|
||||
x => {
|
||||
println!("Expected property, got {:?}", x);
|
||||
break
|
||||
},
|
||||
};
|
||||
let input = parse_mul(iter);
|
||||
match iter.next().unwrap() {
|
||||
Token::Slash => (),
|
||||
x => {
|
||||
println!("Expected /, got {:?}", x);
|
||||
break
|
||||
}
|
||||
}
|
||||
let output = parse_mul(iter);
|
||||
props.push(Property {
|
||||
name: name,
|
||||
input: input,
|
||||
output: output,
|
||||
doc: prop_doc.take()
|
||||
});
|
||||
}
|
||||
map.push((
|
||||
name,
|
||||
Rc::new(Def::Substance(props)),
|
||||
doc.take()));
|
||||
} else {
|
||||
// derived
|
||||
let expr = parse_expr(iter);
|
||||
|
|
|
@ -268,6 +268,10 @@ impl Context {
|
|||
Ok(_) => println!("Quantity {} is not a number", name),
|
||||
Err(e) => println!("Quantity {} is malformed: {}", name, e)
|
||||
},
|
||||
Def::Substance(ref props) => {
|
||||
println!("{:#?}", props);
|
||||
unimplemented!()
|
||||
},
|
||||
Def::Error(ref err) => println!("Def {}: {}", name, err),
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue