gdritter repos utmyen / 43d000f
Got rid of tabs Getty Ritter 7 years ago
4 changed file(s) with 2 addition(s) and 369 deletion(s). Collapse all Expand all
88 # ...a set of properties
99 properties {
1010 light true
11 fruit true
11 fruit true
1212 }
1313 # ...a bit of descriptive in-game text
1414 description [
1515 "The starberry is a beautiful, bright yellow fruit that glows "
16 "ever-so-slightly in the darkness."
16 "ever-so-slightly in the darkness."
1717 ]
1818 }
+0
-168
src/adnot.rs less more
1 use std::collections::HashMap;
2 use std::iter::Peekable;
3 use std::str::{Chars,FromStr};
4
5 #[derive(Clone,Debug,PartialEq)]
6 pub enum Adnot {
7 Sum(String, Vec<Adnot>),
8 Prod(HashMap<String, Adnot>),
9 List(Vec<Adnot>),
10 Str(String),
11 Sym(String),
12 Num(i64),
13 Dbl(f64),
14 }
15
16 #[derive(Debug)]
17 pub struct AdnotError {
18 message: String,
19 }
20
21 type Stream<'a> = Peekable<Chars<'a>>;
22
23 fn fail<T>(s: &str) -> Result<T, AdnotError> {
24 Err(AdnotError { message: s.to_string()})
25 }
26
27 fn parse_val(s: &mut Stream) -> Result<Adnot, AdnotError> {
28 if let Some(c) = s.next() {
29 match c {
30 '[' => parse_list(s),
31 '(' => parse_sum(s),
32 '{' => parse_prod(s),
33 '"' => parse_string(s),
34 _ if c.is_digit(10) || c == '-' => parse_num(c, s),
35 _ if c.is_alphabetic() =>
36 Ok(Adnot::Sym(try!(parse_sym(c, s)))),
37 _ => fail(&format!("Invalid character: {:?}", c)),
38 }
39 } else {
40 fail("Unexpected end of input")
41 }
42 }
43
44 fn parse_list(s: &mut Stream) -> Result<Adnot, AdnotError> {
45 let mut vec = vec![];
46 loop {
47 skip_space(s);
48 if let Some(&']') = s.peek() {
49 s.next();
50 return Ok(Adnot::List(vec));
51 } else {
52 vec.push(try!(parse_val(s)));
53 }
54 }
55 }
56
57 fn parse_sum(s: &mut Stream) -> Result<Adnot, AdnotError> {
58 let mut vec = vec![];
59 skip_space(s);
60 let first = match s.next() {
61 Some(c) if c.is_alphabetic() => c,
62 Some(c) => { return fail("Expected a tagname character") }
63 None => { return fail("Unexpected end of input") }
64 };
65 let name = try!(parse_sym(first, s));
66 loop {
67 skip_space(s);
68 if let Some(&')') = s.peek() {
69 s.next();
70 return Ok(Adnot::Sum(name, vec));
71 } else {
72 vec.push(try!(parse_val(s)));
73 }
74 }
75 }
76
77 fn parse_prod(s: &mut Stream) -> Result<Adnot, AdnotError> {
78 let mut map = HashMap::new();
79 loop {
80 skip_space(s);
81 if let Some(&'}') = s.peek() {
82 s.next();
83 return Ok(Adnot::Prod(map));
84 } else {
85 skip_space(s);
86 let first = match s.next() {
87 Some(c) if c.is_alphabetic() => c,
88 Some(c) => { return fail("Expected a tagname character") }
89 None => { return fail("Unexpected end of input") }
90 };
91 let key = try!(parse_sym(first, s));
92 skip_space(s);
93 let val = try!(parse_val(s));
94 map.insert(key, val);
95 }
96 }
97 }
98
99 fn parse_string(s: &mut Stream) -> Result<Adnot, AdnotError> {
100 let mut chars = Vec::new();
101 while let Some(c) = s.next() {
102 if c == '"' {
103 break;
104 } else if c == '\\' {
105 match s.next() {
106 Some('n') => chars.push('\n'),
107 Some('r') => chars.push('\r'),
108 Some('t') => chars.push('\t'),
109 Some('\'') => chars.push('\''),
110 Some('\"') => chars.push('\"'),
111 Some('\\') => chars.push('\\'),
112 _ => return fail("Invalid escape sequence"),
113 }
114 } else {
115 chars.push(c);
116 }
117 };
118 Ok(Adnot::Str(chars.iter().cloned().collect()))
119 }
120
121 fn parse_num(c: char, s: &mut Stream) -> Result<Adnot, AdnotError> {
122 let mut str = vec![c];
123 str.extend(s.take_while(|c| c.is_digit(10)));
124 let string: String = str.iter().cloned().collect();
125 Ok(Adnot::Num(i64::from_str(&string).unwrap()))
126 }
127
128 fn parse_sym(c: char, s: &mut Stream) -> Result<String, AdnotError> {
129 let mut chars = vec![c];
130 while let Some(&c) = s.peek() {
131 if c.is_alphanumeric() || c == '_' {
132 chars.push(s.next().unwrap());
133 } else {
134 break;
135 }
136 };
137 Ok(chars.iter().cloned().collect())
138 }
139
140 fn skip_space(s: &mut Stream) {
141 while let Some(&c) = s.peek() {
142 match c {
143 '#' => { skip_comment(s); }
144 _ if c.is_whitespace() => { s.next(); }
145 _ => break,
146 }
147 }
148 }
149
150 fn skip_comment(s: &mut Stream) {
151 s.next();
152 while let Some(&c) = s.peek() {
153 if c == '\n' || c == '\r' {
154 s.next();
155 return;
156 } else {
157 s.next();
158 }
159 }
160 }
161
162 impl Adnot {
163 pub fn parse(s: &str) -> Result<Adnot, AdnotError> {
164 let mut stream = s.chars().peekable();
165 skip_space(&mut stream);
166 parse_val(&mut stream)
167 }
168 }
+0
-187
src/bencode.rs less more
1 use core::slice::Iter;
2 use std::collections::HashMap;
3 use std::iter::Peekable;
4 use std::mem;
5
6 #[derive(Clone,Debug,PartialEq)]
7 pub enum Bencode {
8 Int(i64),
9 Float(f32),
10 Str(Vec<u8>),
11 List(Vec<Bencode>),
12 Dict(HashMap<Vec<u8>, Bencode>),
13 }
14
15 type Stream<'a> = Peekable<Iter<'a, u8>>;
16
17 fn fail<T>(s: &str) -> Result<T, String> {
18 Err(s.to_string())
19 }
20
21 fn is_digit(b: u8) -> bool {
22 b >= 0x30 && b < 0x3a
23 }
24
25 fn to_digit_usize(b: u8) -> usize {
26 (b - 0x30) as usize
27 }
28
29 fn to_digit_i64(b: u8) -> i64 {
30 (b - 0x30) as i64
31 }
32
33 fn parse_helper(is: &mut Stream) -> Result<Bencode, String> {
34 match is.next() {
35 Some(&0x69) => {
36 let mut num = 0;
37 for &n in is.take_while(|&&x| is_digit(x)) {
38 num = num * 10 + to_digit_i64(n);
39 }
40 Ok(Bencode::Int(num))
41 },
42 Some(&0x66) => {
43 let mut next_byte = || {
44 match is.next() {
45 Some(n) => Ok(*n),
46 None => Err("Unexpected end-of-stream"),
47 }
48 };
49 let buf = [ try!(next_byte()),
50 try!(next_byte()),
51 try!(next_byte()),
52 try!(next_byte()) ];
53 let _ = next_byte();
54 let f = unsafe { mem::transmute::<[u8;4], f32>(buf) };
55 Ok(Bencode::Float(f))
56 },
57 Some(&0x6c) => {
58 let mut list = Vec::new();
59 loop {
60 match is.peek() {
61 Some(&&0x65) => {
62 let _ = is.next();
63 break;
64 }
65 _ => {
66 list.push(try!(parse_helper(is)));
67 }
68 }
69 };
70 Ok(Bencode::List(list))
71 },
72 Some(&0x64) => {
73 let mut dict = HashMap::new();
74 loop {
75 match is.peek() {
76 Some(&&0x65) => {
77 let _ = is.next();
78 break;
79 }
80 Some(&&d) if is_digit(d) => {
81 let _ = is.next();
82 let key = try!(parse_string(d, is));
83 let val = try!(parse_helper(is));
84 dict.insert(key, val);
85 }
86 _ => {
87 return fail("error while parsing dictionary key");
88 }
89 }
90 }
91 Ok(Bencode::Dict(dict))
92 },
93 Some(&c) if is_digit(c) => {
94 Ok(Bencode::Str(try!(parse_string(c, is))))
95 },
96 _ => fail("error while parsing expression"),
97 }
98 }
99
100 fn parse_string(init: u8, is: &mut Stream) -> Result<Vec<u8>, String> {
101 let mut size = to_digit_usize(init);
102 for &n in is.take_while(|&&x| is_digit(x)) {
103 size = size * 10 + to_digit_usize(n);
104 }
105 Ok(is.take(size).cloned().collect())
106 }
107
108 impl Bencode {
109 pub fn parse(bs: &[u8]) -> Result<Bencode, String> {
110 parse_helper(&mut bs.iter().peekable())
111 }
112
113 pub fn get_field(&self, name: &str) -> Result<&Bencode, String> {
114 match self {
115 &Bencode::Dict(ref ds) => {
116 let key = name.as_bytes().to_vec();
117 match ds.get(&key) {
118 Some(r) => Ok(r),
119 None => fail(&format!("no such key: {:?}", name)),
120 }
121 }
122 _ => fail("not a dictionary"),
123 }
124 }
125
126 pub fn get_idx(&self, idx: usize) -> Result<&Bencode, String> {
127 match self {
128 &Bencode::List(ref ds) => {
129 match ds.get(idx) {
130 Some(r) => Ok(r),
131 None => fail(&format!("no such index: {:?}", idx)),
132 }
133 }
134 _ => fail("not a list"),
135 }
136 }
137
138 pub fn as_u16(&self) -> Result<u16, String> {
139 match self {
140 &Bencode::Int(n) => Ok(n as u16),
141 _ => fail("not a number"),
142 }
143 }
144
145 pub fn as_i64(&self) -> Result<i64, String> {
146 match self {
147 &Bencode::Int(n) => Ok(n),
148 _ => fail("not a number"),
149 }
150 }
151
152 pub fn as_usize(&self) -> Result<usize, String> {
153 match self {
154 &Bencode::Int(n) => Ok(n as usize),
155 _ => fail("not a number"),
156 }
157 }
158
159 pub fn as_float(&self) -> Result<f32, String> {
160 match self {
161 &Bencode::Float(f) => Ok(f),
162 _ => fail("not a number"),
163 }
164 }
165
166 pub fn as_bytes(&self) -> Result<Vec<u8>, String> {
167 match self {
168 &Bencode::Str(ref bs) => Ok(bs.clone()),
169 _ => fail("not a string"),
170 }
171 }
172
173 pub fn map<A, F>(&self, func: F) -> Result<Vec<A>, String>
174 where F: 'static + Fn(&Bencode) -> Result<A, String>
175 {
176 match self {
177 &Bencode::List(ref lst) => {
178 let mut vec = Vec::with_capacity(lst.len());
179 for x in lst {
180 vec.insert(0, try!(func(x)));
181 }
182 Ok(vec)
183 },
184 _ => fail("not a list"),
185 }
186 }
187 }
+0
-12
src/graph.rs less more
1 #[derive(Copy, Clone, Debug)]
2 struct V3 {
3 position: [f32; 3],
4 }
5
6 implement_vertex!(V3, position);
7
8 enum SceneGraph {
9 }
10
11 struct Model {
12 }