suiron/
parse_stack.rs

1//! LIFO stack for parsing. Used by the tokenizer.
2//!
3//! The parse stack holds [token types](../token/enum.TokenType.html).
4//!
5// Cleve Lendon 2023
6
7use super::token::*;
8
9pub type ParseStack = Vec<TokenType>;
10
11/// Peeks at the top (last in item) of the parse stack.
12/// # Arguments
13/// * `stack` - ParseStack
14/// # Return
15/// * `token type`
16pub fn peek(stack: &mut ParseStack) -> TokenType {
17    let length = stack.len();
18    if length == 0 { return TokenType::Empty; }
19    return stack[length - 1];
20}
21
22/// Pops the top item from the stack.
23/// # Arguments
24/// * `stack` - ParseStack
25/// # Return
26/// * `token type`
27pub fn pop(stack: &mut ParseStack) -> TokenType {
28    let p = stack.pop();
29    match p {
30        Some(tt) => tt,
31        None => TokenType::Empty,
32    }
33}