mirror of
https://github.com/zaphar/ucg.git
synced 2025-07-22 18:19:54 -04:00
Refactor the Format Expression to use a FormatDef struct.
This commit is contained in:
parent
42fe368dae
commit
a8b6e7f851
11
src/ast.rs
11
src/ast.rs
@ -192,7 +192,8 @@ impl MacroDef {
|
||||
&Expression::Grouped(ref expr) => {
|
||||
stack.push(expr);
|
||||
},
|
||||
&Expression::Format(_, ref exprs) => {
|
||||
&Expression::Format(ref def) => {
|
||||
let exprs = &def.args;
|
||||
for arg_expr in exprs.iter() {
|
||||
stack.push(arg_expr);
|
||||
}
|
||||
@ -243,6 +244,12 @@ pub struct CopyDef {
|
||||
pub fields: FieldList
|
||||
}
|
||||
|
||||
#[derive(Debug,PartialEq,Clone)]
|
||||
pub struct FormatDef {
|
||||
pub template: String,
|
||||
pub args: Vec<Expression>
|
||||
}
|
||||
|
||||
/// Expression encodes an expression. Expressions compute a value from operands.
|
||||
#[derive(Debug,PartialEq,Clone)]
|
||||
pub enum Expression {
|
||||
@ -260,7 +267,7 @@ pub enum Expression {
|
||||
Copy(CopyDef),
|
||||
Grouped(Box<Expression>),
|
||||
|
||||
Format(String, Vec<Expression>),
|
||||
Format(FormatDef),
|
||||
|
||||
Call(CallDef),
|
||||
|
||||
|
@ -545,7 +545,9 @@ impl Builder {
|
||||
Expression::Grouped(expr) => {
|
||||
return self.eval_expr(*expr);
|
||||
},
|
||||
Expression::Format(tmpl, mut args) => {
|
||||
Expression::Format(def) => {
|
||||
let tmpl = def.template;
|
||||
let mut args = def.args;
|
||||
let mut vals = Vec::new();
|
||||
for v in args.drain(0..) {
|
||||
let rcv = try!(self.eval_expr(v));
|
||||
|
14
src/parse.rs
14
src/parse.rs
@ -356,7 +356,7 @@ named!(select_expression<Expression>,
|
||||
);
|
||||
|
||||
fn tuple_to_format(t: (String, Vec<Expression>)) -> ParseResult<Expression> {
|
||||
Ok(Expression::Format(t.0, t.1))
|
||||
Ok(Expression::Format(FormatDef{template: t.0, args: t.1}))
|
||||
}
|
||||
|
||||
named!(format_expression<Expression>,
|
||||
@ -712,16 +712,16 @@ mod test {
|
||||
|
||||
assert_eq!(format_expression(&b"\"foo @ @\" % (1, 2)"[..]),
|
||||
IResult::Done(&b""[..],
|
||||
Expression::Format("foo @ @".to_string(),
|
||||
vec![Expression::Simple(Value::Int(make_value_node(1))),
|
||||
Expression::Simple(Value::Int(make_value_node(2)))])
|
||||
Expression::Format(FormatDef{template: "foo @ @".to_string(),
|
||||
args: vec![Expression::Simple(Value::Int(make_value_node(1))),
|
||||
Expression::Simple(Value::Int(make_value_node(2)))]})
|
||||
)
|
||||
);
|
||||
assert_eq!(format_expression(&b"\"foo @ @\"%(1, 2)"[..]),
|
||||
IResult::Done(&b""[..],
|
||||
Expression::Format("foo @ @".to_string(),
|
||||
vec![Expression::Simple(Value::Int(make_value_node(1))),
|
||||
Expression::Simple(Value::Int(make_value_node(2)))])
|
||||
Expression::Format(FormatDef{template: "foo @ @".to_string(),
|
||||
args: vec![Expression::Simple(Value::Int(make_value_node(1))),
|
||||
Expression::Simple(Value::Int(make_value_node(2)))]})
|
||||
)
|
||||
);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user