1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268


//people who read this code often remark about how horrible it is. The truth is, it's the format that's horrible. By horrible, of course, you mean "complicated". I think the word you're really looking for might be "flexible". There is probably no way to write a parser for a format as sensitive as termpose that isn't "horrible". (I would love to find out about it, if there is)

use super::*;


#[inline(always)]
fn get_back_mut<T>(v:&mut Vec<T>)-> &mut T {
	if let Some(v) = v.last_mut() { v }
	else{ panic!("this vec should never be empty"); }
}

#[derive(Debug)]
pub struct PositionedError{
	pub line:isize,
	pub column:isize,
	pub msg:String,
}

impl Error for PositionedError {
	fn description(&self) -> &str { self.msg.as_str() }
	fn cause(&self) -> Option<&Error> { None }
}
impl Display for PositionedError {
	fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> {
		Debug::fmt(self, f)
	}
}




#[inline(always)]
fn assume_branch_mut(v:&mut Wood)-> &mut Branch {
	match *v {
		Branchv(ref mut ls)=> ls,
		Leafv(_)=> panic!("this Wood is supposed to be a branch"),
	}
}
#[inline(always)]
fn assume_leaf_mut(v:&mut Wood)-> &mut Leaf {
	match *v {
		Branchv(_)=> panic!("this Wood is supposed to be an leaf"),
		Leafv(ref mut ar)=> ar,
	}
}
#[inline(always)]
fn assume_branch(v:Wood)-> Branch {
	match v {
		Branchv(ls)=> ls,
		Leafv(_)=> panic!("this Wood is supposed to be a branch"),
	}
}

fn yank_first<T>(v:Vec<T>)-> T { //you must ensure that the v contains at least one item
	match v.into_iter().next() {
		Some(a)=> a,
		None=> panic!("tried to yank the first thing from a vec that contained no things"),
	}
}

fn accrete_branch(v:&mut Wood)-> &mut Vec<Wood> {
	unsafe{
		replace_self(v, |vv|{
			let (line, col) = vv.line_and_col();
			Branchv(Branch{line:line, column:col, v:vec!(vv)})
		}); //safe: branch creation doesn't panic
	}
	&mut assume_branch_mut(v).v
}

unsafe fn replace_self<T, F>(v:&mut T, f:F) where F : FnOnce(T)-> T { //for replacing a thing with a transformation of itself. It's actually fairly safe, you just have to guarantee that this transformation, f, doesn't panic, or else this function will drop an uninitialized, or v's drop will be called twice, or something. I don't even know, so don't don't panic or else!
	let res = f(replace(v, uninitialized()));
	forget(replace(v, res));
}

unsafe fn str_from_bounds<'a>(o:*const u8, f:*const u8)-> &'a str {
	std::str::from_utf8_unchecked(std::slice::from_raw_parts(o, f as usize - o as usize))
}

fn is_whitespace(c:char)-> bool { c == ' ' || c == '\t' }

fn do_indent(indent:&str, indent_depth:usize, out:&mut String){
	for _ in 0..indent_depth { out.push_str(indent); }
}

// pub fn parse_nakedbranch<'a>(v:&'a str)-> Result<Wood, PositionedError> {
// 	NakedbranchParserState::<'a>::begin(v).parse()
// }









mod termpose_parser;
pub use self::termpose_parser::*;

mod woodslist_parser;
pub use self::woodslist_parser::*;



#[cfg(test)]
mod tests {
	extern crate test;
	use super::*;
	use std::fs::File;
	use std::io::Read;
	use std::path::PathBuf;
	
	fn read_file_from_root(file_name:&str)-> String {
		let mut d = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
		d.push(file_name);
		let mut ret = String::new();
		let mut file = File::open(d).unwrap();
		file.read_to_string(&mut ret).unwrap();
		ret
	}
	
	// #[test]
	// fn it_work() {
	// 	let ti = branch!(branch!("hue", branch!("when", "you", "now"), "then", "else"));
	// 	let to:Wood<&'static str> = parse_sexp("hue (when you now) then else").unwrap();
	// 	println!("the parsed is {}", to.to_string());
	// 	assert!(ti == to);
	// }
	
	#[test]
	fn test_tests_term_file() {
		let teststr = read_file_from_root("tests.term");
		let testt = parse_multiline_termpose(teststr.as_str()).unwrap();
		let testb = testt.find("tests").unwrap();
		
		for tc in testb.tail() {
			let test_name = tc.initial_str();
			let mut ttests = tc.tail();
			if let Some(first_case) = ttests.next() {
				for remaining_case in ttests {
					if first_case != remaining_case {
						panic!(format!("in \"{}\" case, {} did not equal {}", test_name, first_case.to_string(), remaining_case.to_string()));
					}
				}
			}
		}
		
		let failts = testt.find("failing").unwrap();
		for tc in failts.tail() {
			let test_name = tc.initial_str();
			for t in tc.tail() {
				if let Ok(v) = parse_multiline_termpose(t.initial_str()) {
					panic!("in \"{}\" case, this string should not have parsed successfully: {}\n. It parsed to {}", test_name, t.initial_str(), v.to_string());
				}
			}
		}
	}
	
	
	#[test]
	fn test_tests_sexp_file() {
		let teststr = read_file_from_root("sexp tests.sexp");
		let testt = parse_multiline_woodslist(teststr.as_str()).unwrap();
		println!("{}", testt.to_string());
		let testb = testt.find("tests").unwrap();
		
		for tc in testb.tail() {
			let test_name = tc.initial_str();
			let mut ttests = tc.tail();
			if let Some(first_case) = ttests.next() {
				for remaining_case in ttests {
					if first_case != remaining_case {
						panic!(format!("in \"{}\" case, {} did not equal {}", test_name, first_case.to_string(), remaining_case.to_string()));
					}
				}
			}
		}
		
		if let Some(failts) = testt.find("failing") {
			for tc in failts.tail() {
				let test_name = tc.initial_str();
				for t in tc.tail() {
					if let Ok(v) = parse_multiline_termpose(t.initial_str()) {
						panic!("in \"{}\" case, this string should not have parsed successfully: {}\n. It parsed to {}", test_name, t.initial_str(), v.to_string());
					}
				}
			}
		}
	}
	
	
	#[test]
	fn idempotence() {
		let term = branch!("how", branch!("about", "that"));
		assert!(term == parse_termpose(parse_termpose(term.to_string().as_str()).unwrap().to_string().as_str()).unwrap())
	}
	
	use self::test::Bencher;
	#[bench]
	fn bench_long_long_term(b: &mut Bencher) {
		let data = read_file_from_root("longterm.term");
		b.iter(||{
			parse_termpose(data.as_ref())
		});
	}
	
	#[test]
	fn longterm_termpose_idempotence(){
		let w = parse_termpose(&read_file_from_root("longterm.term")).unwrap();
		let wiw = parse_termpose(&pretty_termpose(&w)).unwrap();
		assert_eq!(&w, &wiw);
	}
	
	#[test]
	fn longterm_woodslist_idempotence(){
		let w = parse_woodslist(&read_file_from_root("longterm.term")).unwrap();
		let wiw = parse_woodslist(&indented_woodslist(&w)).unwrap();
		assert_eq!(&w, &wiw);
	}
	
	fn windowsify(v:&str)-> String {
		let mut out = String::new();
		let mut vc = v.chars().peekable();
		while let Some(c) = vc.next() {
			out.push(c);
			if c == '\n' {
				if vc.peek() != Some(&'\r') {
					out.push('\r');
				}
			}
		}
		out
	}
	
	#[test]
	fn windows_style_line_endings() {
		let shortterm = windowsify(read_file_from_root("shortterm.term").as_str());
		
		let expected_data = branch!(
			branch!("let", "a", "2"),
			branch!(
				branch!("if", "a"),
				branch!("then", branch!("+", "a", "2")),
				branch!("else", "0")
			)
		);
		
		let parsed = parse_multiline_termpose(shortterm.as_str()).unwrap();
		if expected_data != parsed {
			panic!("{} did not equal expected_data", parsed.to_string());
		}
	}
	
	#[test]
	fn woodslist_parser_skips_first_newline() {
		let w = parse_woodslist("aaa \"\naa sdi \n  idj\" a").unwrap();
		assert_eq!(&branch!("aaa", "aa sdi \n  idj", "a"), &w, "uh");
	}
	
	#[test]
	fn woodslist_parser_doesnt_skip_first_non_newline() {
		let w = parse_woodslist("aaa \"aa sdi \n  idj\" a").unwrap();
		assert_eq!(&branch!("aaa", "aa sdi \n  idj", "a"), &w, "uh");
	}
}