Skip to content

Commit

Permalink
Migrate for From PEG
Browse files Browse the repository at this point in the history
  • Loading branch information
mmstick committed Jul 30, 2017
1 parent b19966c commit c28aec0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
15 changes: 2 additions & 13 deletions src/parser/grammar.rustpeg
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use parser::{pipelines, ArgumentSplitter};
use parser::pipelines;
use parser::peg::get_function_args;
use shell::flow_control::*;

#[pub]
parse_ -> Statement
= for_
/ fn_
= fn_
/ match_
/ case_
/ pipelines
Expand Down Expand Up @@ -39,16 +38,6 @@ _arg -> String
_fn_arg -> String
= n:$([A-z0-9_:]+) { n.into()}

#[pub]
for_ -> Statement
= "for" whitespace+ n:_name whitespace+ "in" whitespace+ expr:$(.*) {
Statement::For {
variable: n.into(),
values: ArgumentSplitter::new(expr).map(String::from).collect(),
statements: Vec::new(),
}
}

wildcard_ -> Option<String> = "_" { None }
value_ -> Option<String> = contents:$(.*) { Some(contents.into())}

Expand Down
29 changes: 28 additions & 1 deletion src/parser/peg.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::char;
use std::fmt;
use std::io::{Write, stderr};

use self::grammar::parse_;
use super::{ArgumentSplitter, pipelines};
use super::{ExpanderFunctions, Select, expand_string};
use super::assignments::parse_assignment;
use super::pipelines;
use shell::{Job, JobKind};
use shell::directory_stack::DirectoryStack;
use shell::flow_control::{ElseIf, FunctionArgument, Statement, Type};
Expand Down Expand Up @@ -167,6 +168,32 @@ pub fn parse(code: &str) -> Statement {
}
});
}
_ if cmd.starts_with("for ") => {
let mut cmd = cmd[4..].trim_left();
let pos = match cmd.find(char::is_whitespace) {
Some(pos) => pos,
None => {
eprintln!("ion: syntax error: incorrect for loop syntax");
return Statement::Default;
}
};

let variable = &cmd[..pos];
cmd = &cmd[pos..].trim_left();

if cmd.starts_with("in ") {
cmd = cmd[3..].trim_left();
} else {
eprintln!("ion: syntax error: incorrect for loop syntax");
return Statement::Default;
}

return Statement::For {
variable: variable.into(),
values: ArgumentSplitter::new(cmd).map(String::from).collect(),
statements: Vec::new(),
};
}
_ => (),
}

Expand Down

0 comments on commit c28aec0

Please sign in to comment.