impral/parser/
structure.rs

1//! Parsing of token-stream into data-structures.
2
3use super::*;
4
5/// Parses the stream of tokens into a list.
6pub fn parse_list(
7    tokens: &mut PeekableTokenStream<impl TokenStream>
8) -> Result<ExpressionVec, ParseError> {
9    let mut list = ExpressionVec::default();
10    
11    loop {
12        if tokens.peek().is_none() {
13            break;
14        }
15        
16        if consume_symbol(tokens, Symbol::BraketRight) {
17            break;
18        }
19        
20        if consume_symbol(tokens, Symbol::Comma) {
21            continue;
22        }
23        
24        let expr = parse_expression(tokens, false, true)?;
25        list.push(expr);
26    }
27
28    Ok(list)
29}
30
31/// Parses the stream of tokens into a key/value-map.
32pub fn parse_map(
33    tokens: &mut PeekableTokenStream<impl TokenStream>
34) -> Result<FxHashMap<CompactString, Expression>, ParseError> {
35    let mut map = FxHashMap::default();
36    
37    loop {
38        if tokens.peek().is_none() {
39            break;
40        }
41        
42        if consume_symbol(tokens, Symbol::CurlyRight) {
43            break;
44        }
45        
46        if consume_symbol(tokens, Symbol::Comma) {
47            continue;
48        }
49        
50        if let Some(key) = consume_string(tokens) {
51            if ! consume_symbol(tokens, Symbol::EqualSign) {
52                return Err(ParseError::ExpectButGot("equal-sign".into(), "something else".into()));
53            }
54            // else: everything checks out, continue on...
55            
56            let expr = parse_expression(tokens, false, true)?;
57            map.insert(key, expr);
58            continue;
59        }
60    }
61    
62    Ok(map)
63}