Last active
February 26, 2016 17:05
-
-
Save erickt/d0cbaed7dddabb022b6b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
src/main.rs:111:22: 111:26 error: no method named `init` found for type `parsell::impls::AndThen<parsell::impls::Star<ValueParser, fn() -> collections::vec::Vec<_> {collections::vec::Vec<T>::new}>, parsell::impls::Character<fn(char) -> bool {ListParser.Uncommitted<char, Chars<'a>>::init::is_rbracket}>>` in the current scope | |
src/main.rs:111 match parser.init(data).unwrap() { | |
^~~~ | |
src/main.rs:111:22: 111:26 note: the method `init` exists but the following trait bounds were not satisfied: `parsell::impls::Character<fn(char) -> bool {ListParser.Uncommitted<char, Chars<'a>>::init::is_rbracket}> : parsell::Committed<_, _>` | |
error: aborting due to previous error |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![allow(unused_imports)] | |
#![allow(dead_code)] | |
#![allow(non_snake_case)] | |
extern crate parsell; | |
use std::str::Chars; | |
use parsell::{ | |
Boxable, | |
CHARACTER, | |
Committed, | |
ParseResult, | |
Parser, | |
Stateful, | |
StatefulStr, | |
StaticMarker, | |
Uncommitted, | |
UncommittedStr, | |
}; | |
use parsell::ParseResult::{Done,Continue}; | |
use std::borrow::Cow; | |
use std::char; | |
////////////////////////////////////////////////////////////////////////////// | |
#[derive(Debug)] | |
enum Value { | |
String(String), | |
List(List), | |
} | |
#[derive(Clone, Debug)] | |
struct List { | |
items: Vec<char>, | |
} | |
////////////////////////////////////////////////////////////////////////////// | |
type ValueParserState = Box<for<'a> Boxable<char, Chars<'a>, Output=Value>>; | |
#[derive(Copy,Clone,Debug)] | |
struct ValueParser; | |
impl Parser for ValueParser {} | |
impl<'a> Uncommitted<char, Chars<'a>> for ValueParser { | |
type Output = Value; | |
type State = ValueParserState; | |
fn init(&self, data: &mut Chars<'a>) -> Option<ParseResult<Self::State, Self::Output>> { | |
fn is_quote(ch: char) -> bool { ch == '"' } | |
fn is_lbracket(ch: char) -> bool { ch == '[' } | |
fn is_rbracket(ch: char) -> bool { ch == ']' } | |
fn mk_list(list: List) -> Value { Value::List(list) } | |
let lbracket = parsell::character(is_lbracket); | |
let parser = ListParser | |
; | |
match parser.init(data).unwrap() { | |
Continue(_) => { | |
panic!("continue") | |
} | |
Done(x) => { | |
panic!("value done {:?}", x) | |
} | |
} | |
panic!() | |
} | |
} | |
////////////////////////////////////////////////////////////////////////////// | |
type ListParserState = Box<for<'a> Boxable<char, Chars<'a>, Output=List>>; | |
#[derive(Copy,Clone,Debug)] | |
struct ListParser; | |
impl Parser for ListParser {} | |
impl<'a> Uncommitted<char, Chars<'a>> for ListParser { | |
type Output = List; | |
type State = ListParserState; | |
fn init(&self, data: &mut Chars<'a>) -> Option<ParseResult<Self::State, Self::Output>> { | |
fn is_rbracket(ch: char) -> bool { ch == ']' } | |
fn mk_vec() -> Vec<char> { | |
Vec::new() | |
} | |
fn mk_box<P>(state: P) -> ListParserState | |
where P: 'static + for<'a> Boxable<char, Chars<'a>, Output=List> | |
{ | |
Box::new(state) | |
} | |
fn mk_list(items: Vec<char>) -> List { | |
List { | |
items: items, | |
} | |
} | |
let rbracket = parsell::character(is_rbracket); | |
let parser = ValueParser.star(Vec::new) | |
.and_then(rbracket) | |
; | |
match parser.init(data).unwrap() { | |
Continue(_) => { | |
panic!("continue") | |
} | |
Done(x) => { | |
panic!("done {:?}", x) | |
} | |
} | |
/* | |
.boxed(Box::<ListParserState>::new) | |
.init(data) | |
; | |
*/ | |
/* | |
fn mk_list() -> ValueList { | |
List { items: Vec::new() } | |
} | |
let lbracket = parsell::character(is_lbracket); | |
let rbracket = parsell::character(is_rbracket) | |
.map(mk_ok::<char>) | |
.or_else(parsell::CHARACTER.map(mk_err::<char>)); | |
let parser = lbracket | |
//.discard_and_then(ListParser.star(mk_list)) | |
.discard_and_then(parsell::CHARACTER) | |
.try_and_then_try_discard(rbracket) | |
.try_map(mk_list); | |
parser.boxed(Box::<ListParserState>::new).init(data); | |
*/ | |
panic!() | |
} | |
} | |
////////////////////////////////////////////////////////////////////////////// | |
fn parse(s: &str) -> String { | |
/* | |
let lquote = parsell::character(is_quote); | |
let rquote = parsell::character(is_quote) | |
.map(mk_ok) | |
.or_else(parsell::CHARACTER.map(mk_err)); | |
; | |
let character = parsell::character(is_not_quote); | |
let characters = character | |
.plus(mk_string) | |
.map(mk_ok) | |
.or_else(parsell::CHARACTER.map(mk_err)); | |
let string = lquote | |
.discard_and_then(characters) | |
.try_and_then_try_discard(rquote) | |
.try_map(mk_string_value); | |
*/ | |
/* | |
let list = lbracket | |
.discard_and_then_try_discard(rbracket) | |
.try_map(mk_list_value); | |
let parser = string | |
.or_else(lbracket); | |
*/ | |
let parser = ValueParser; | |
match parser.init_str(s).unwrap() { | |
Continue(x) => { | |
//panic!("continue {:?}", x) | |
panic!("continue") | |
} | |
Done(x) => { | |
panic!("done {:?}", x) | |
} | |
} | |
/* | |
let TREE = TreeParser; | |
match TREE.init_str(s).unwrap() { | |
Continue(parsing) => match parsing.more_str(")())") { | |
Done(result) => assert_eq!(result, Ok(Tree(vec![Tree(vec![]),Tree(vec![])]))), | |
_ => panic!("Can't happen"), | |
}, | |
_ => panic!("Can't happen"), | |
} | |
*/ | |
/* | |
struct Tree(Vec<Tree>); | |
impl StaticMarker for Tree {} | |
type TreeParserState = Box<for<'a> Boxable<char, Chars<'a>, Output=Result<Tree, String>>>; | |
fn is_lparen(ch: char) -> bool { ch == '(' } | |
fn is_rparen(ch: char) -> bool { ch == ')' } | |
fn mk_vec() -> Result<Vec<Tree>,String> { Ok(Vec::new()) } | |
fn mk_ok<T>(ok: T) -> Result<T,String> { Ok(ok) } | |
fn mk_err<T>(_: Option<char>) -> Result<T,String> { Err(String::from("Expected a ( or ).")) } | |
fn mk_tree(_: char, children: Vec<Tree>, _: char) -> Tree { Tree(children) } | |
fn mk_box<P>(state: P) -> TreeParserState | |
where P: 'static + for<'a> Boxable<char, Chars<'a>, Output=Result<Tree,String>> | |
{ | |
Box::new(state) | |
} | |
struct TreeParser; | |
impl Parser for TreeParser {} | |
impl<'a> Uncommitted<char, Chars<'a>> for TreeParser { | |
type Output = Result<Tree, String>; | |
type State = TreeParserState; | |
fn init(&self, data: &mut Chars<'a>) -> Option<ParseResult<Self::State, Self::Output>> { | |
let LPAREN = character(is_lparen); | |
let RPAREN = character(is_rparen).map(mk_ok).or_else(CHARACTER.map(mk_err)); | |
let parser = LPAREN | |
.and_then_try(TreeParser.star(mk_vec)) | |
.try_and_then_try(RPAREN) | |
.try_map3(mk_tree); | |
parser.boxed(mk_box).init(data) | |
} | |
} | |
let TREE = TreeParser; | |
*/ | |
/* | |
let quote = parsell::character(is_quote); | |
let characters = parsell::character(is_character) | |
.plus(String::new); | |
/* | |
let parser = quote | |
.and_then(characters) | |
// .and_then(quote); | |
; | |
*/ | |
let parser = | |
quote.and_then_try(characters) | |
//.plus(String::new) | |
//.or_else(character(char::is_alphabetic).plus(String::new)) | |
//.or_else(CHARACTER.map(default)); | |
; | |
match parser.init_str(s) { | |
None => { | |
panic!("failed to parse"); | |
} | |
Some(ParseResult::Done(result)) => { | |
result | |
} | |
Some(ParseResult::Continue(state)) => { | |
panic!("incomplete") | |
} | |
} | |
*/ | |
String::from("") | |
} | |
fn main() { | |
let s = "\"abcd\""; | |
println!("{}", parse(s)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment