@@ -57,47 +57,45 @@ fn build_exprs(tokens: &[CirruLexItem]) -> Result<Vec<Cirru>, String> {
57
57
loop {
58
58
let chunk = pull_token ( ) ;
59
59
60
- if chunk. is_none ( ) {
61
- return Ok ( acc) ;
62
- }
63
- match chunk. unwrap ( ) {
64
- CirruLexItem :: Open => {
65
- let mut pointer: Vec < Cirru > = vec ! [ ] ;
66
- // guess a nested level of 16
67
- let mut pointer_stack: Vec < Vec < Cirru > > = Vec :: with_capacity ( 16 ) ;
68
- loop {
69
- let cursor = pull_token ( ) ;
70
- if cursor. is_none ( ) {
71
- return Err ( String :: from ( "unexpected end of file" ) ) ;
72
- }
73
- match cursor. unwrap ( ) {
74
- CirruLexItem :: Close => {
75
- if pointer_stack. is_empty ( ) {
76
- acc. push ( Cirru :: List ( pointer. to_owned ( ) ) ) ;
77
- break ;
78
- } else {
79
- let v = pointer_stack. pop ( ) ;
80
- let prev_p = pointer;
81
- match v {
82
- Some ( collected) => {
83
- pointer = collected;
84
- pointer. push ( Cirru :: List ( prev_p) )
60
+ match & chunk {
61
+ None => return Ok ( acc) ,
62
+ Some ( ck) => {
63
+ match ck {
64
+ CirruLexItem :: Open => {
65
+ let mut pointer: Vec < Cirru > = vec ! [ ] ;
66
+ // guess a nested level of 16
67
+ let mut pointer_stack: Vec < Vec < Cirru > > = Vec :: with_capacity ( 16 ) ;
68
+ loop {
69
+ let cursor = pull_token ( ) ;
70
+
71
+ match & cursor {
72
+ None => return Err ( String :: from ( "unexpected end of file" ) ) ,
73
+ Some ( c) => match c {
74
+ CirruLexItem :: Close => match pointer_stack. pop ( ) {
75
+ None => {
76
+ acc. push ( Cirru :: List ( pointer) ) ;
77
+ break ;
78
+ }
79
+ Some ( v) => {
80
+ let prev_p = pointer;
81
+ pointer = v;
82
+ pointer. push ( Cirru :: List ( prev_p) ) ;
83
+ }
84
+ } ,
85
+ CirruLexItem :: Open => {
86
+ pointer_stack. push ( pointer) ;
87
+ pointer = vec ! [ ] ;
85
88
}
86
- None => return Err ( String :: from ( "unknown close item" ) ) ,
87
- }
89
+ CirruLexItem :: Str ( s) => pointer. push ( Cirru :: Leaf ( ( * * s) . into ( ) ) ) ,
90
+ CirruLexItem :: Indent ( n) => return Err ( format ! ( "unknown indent: {}" , n) ) ,
91
+ } ,
88
92
}
89
93
}
90
- CirruLexItem :: Open => {
91
- pointer_stack. push ( pointer) ;
92
- pointer = vec ! [ ] ;
93
- }
94
- CirruLexItem :: Str ( s) => pointer. push ( Cirru :: Leaf ( s. as_str ( ) . into ( ) ) ) ,
95
- CirruLexItem :: Indent ( n) => return Err ( format ! ( "unknown indent: {}" , n) ) ,
96
94
}
95
+ CirruLexItem :: Close => return Err ( String :: from ( "unexpected \" )\" " ) ) ,
96
+ a => return Err ( format ! ( "unknown item: {:?}" , a) ) ,
97
97
}
98
98
}
99
- CirruLexItem :: Close => return Err ( String :: from ( "unexpected \" )\" " ) ) ,
100
- a => return Err ( format ! ( "unknown item: {:?}" , a) ) ,
101
99
}
102
100
}
103
101
}
@@ -151,28 +149,28 @@ pub fn lex(initial_code: &str) -> Result<CirruLexItemList, String> {
151
149
} ,
152
150
CirruLexState :: Token => match c {
153
151
' ' => {
154
- acc. push ( CirruLexItem :: Str ( buffer) ) ;
152
+ acc. push ( CirruLexItem :: Str ( buffer. into ( ) ) ) ;
155
153
state = CirruLexState :: Space ;
156
154
buffer = String :: from ( "" ) ;
157
155
}
158
156
'"' => {
159
- acc. push ( CirruLexItem :: Str ( buffer) ) ;
157
+ acc. push ( CirruLexItem :: Str ( buffer. into ( ) ) ) ;
160
158
state = CirruLexState :: Str ;
161
159
buffer = String :: from ( "" ) ;
162
160
}
163
161
'\n' => {
164
- acc. push ( CirruLexItem :: Str ( buffer) ) ;
162
+ acc. push ( CirruLexItem :: Str ( buffer. into ( ) ) ) ;
165
163
state = CirruLexState :: Indent ;
166
164
buffer = String :: from ( "" ) ;
167
165
}
168
166
'(' => {
169
- acc. push ( CirruLexItem :: Str ( buffer) ) ;
167
+ acc. push ( CirruLexItem :: Str ( buffer. into ( ) ) ) ;
170
168
acc. push ( CirruLexItem :: Open ) ;
171
169
state = CirruLexState :: Space ;
172
170
buffer = String :: from ( "" )
173
171
}
174
172
')' => {
175
- acc. push ( CirruLexItem :: Str ( buffer) ) ;
173
+ acc. push ( CirruLexItem :: Str ( buffer. into ( ) ) ) ;
176
174
acc. push ( CirruLexItem :: Close ) ;
177
175
state = CirruLexState :: Space ;
178
176
buffer = String :: from ( "" )
@@ -184,7 +182,7 @@ pub fn lex(initial_code: &str) -> Result<CirruLexItemList, String> {
184
182
} ,
185
183
CirruLexState :: Str => match c {
186
184
'"' => {
187
- acc. push ( CirruLexItem :: Str ( buffer) ) ;
185
+ acc. push ( CirruLexItem :: Str ( buffer. into ( ) ) ) ;
188
186
state = CirruLexState :: Space ;
189
187
buffer = String :: from ( "" ) ;
190
188
}
@@ -270,7 +268,7 @@ pub fn lex(initial_code: &str) -> Result<CirruLexItemList, String> {
270
268
match state {
271
269
CirruLexState :: Space => Ok ( acc) ,
272
270
CirruLexState :: Token => {
273
- acc. push ( CirruLexItem :: Str ( buffer) ) ;
271
+ acc. push ( CirruLexItem :: Str ( buffer. into ( ) ) ) ;
274
272
Ok ( acc)
275
273
}
276
274
CirruLexState :: Escape => Err ( String :: from ( "unknown escape" ) ) ,
0 commit comments