Skip to content

Instantly share code, notes, and snippets.

@tilpner
Created May 18, 2015 18:38
Show Gist options
  • Save tilpner/2c71034b10afb5b685f1 to your computer and use it in GitHub Desktop.
Save tilpner/2c71034b10afb5b685f1 to your computer and use it in GitHub Desktop.
use std::io;
use std::io::prelude::*;
extern crate parser_combinators;
use parser_combinators::{spaces, many1, sep_by, digit, satisfy, Parser, ParserExt, ParseError};
fn main() {
let mut stdin = io::stdin();
let mut stdout = io::stdout();
loop {
write!(&mut stdout, "Icelight > ");
stdout.flush();
let mut input = String::new();
stdin.read_line(&mut input);
if input == "exit\n" {
break;
}
let spaces = spaces();
let integer = spaces.clone()//Parse spaces first and use the with method to only keep the result of the next parser
.with(many1(digit()).map(|s: String| s.parse::<i32>().unwrap()));//parse a string of digits into an i32
//Parse integers separated by commas, skipping whitespace
let mut integer_list = sep_by(integer, spaces.skip(satisfy(|c| c == ',')));
//Call parse with the input to execute the parser
let result: Result<(Vec<i32>, &str), ParseError> = integer_list.parse(input.trim_right());
match result {
Ok((value, _remaining_input)) => println!("{:?}", value),
Err(err) => println!("{}", err)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment