Skip to main content

oxilean_parse/command/
commandparser_parsing_1.rs

1//! # CommandParser - parsing Methods
2//!
3//! This module contains method implementations for `CommandParser`.
4//!
5//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)
6
7use crate::{ParseError, Span, Token, TokenKind};
8
9use super::types::Command;
10
11use super::commandparser_type::CommandParser;
12
13impl CommandParser {
14    /// Get the current token (if any).
15    pub(super) fn current(&self) -> Option<&Token> {
16        self.tokens.get(self.pos)
17    }
18    /// Check if the current token matches a given kind (without consuming).
19    pub(super) fn check(&self, kind: &TokenKind) -> bool {
20        self.current().is_some_and(|t| &t.kind == kind)
21    }
22    /// Check if the current token is an identifier with a specific name.
23    pub(super) fn check_ident(&self, name: &str) -> bool {
24        self.current()
25            .is_some_and(|t| matches!(& t.kind, TokenKind::Ident(s) if s == name))
26    }
27    /// Return the span of the current token, or a synthetic eof span.
28    pub(super) fn current_span(&self) -> Span {
29        self.current()
30            .map(|t| t.span.clone())
31            .unwrap_or_else(|| self.eof_span())
32    }
33    /// Parse `syntax <name> [<prec>] := <pattern>`.
34    pub(super) fn parse_syntax(&mut self) -> Result<Command, ParseError> {
35        let start_span = self.current_span();
36        self.advance();
37        let name = self.parse_ident()?;
38        let prec = if let Some(token) = self.current() {
39            if let TokenKind::Nat(n) = &token.kind {
40                let p = *n as u32;
41                self.advance();
42                Some(p)
43            } else {
44                None
45            }
46        } else {
47            None
48        };
49        self.expect(&TokenKind::Assign)?;
50        let pattern = self.collect_rest_as_string();
51        let span = start_span.merge(&self.current_span());
52        Ok(Command::Syntax {
53            name,
54            prec,
55            pattern,
56            span,
57        })
58    }
59    /// Helper: check if current is 'keyword'
60    pub(super) fn check_keyword(&self, keyword: &str) -> bool {
61        self.current()
62            .is_some_and(|t| matches!(& t.kind, TokenKind::Ident(s) if s == keyword))
63    }
64    /// Helper: safely check if at end of token stream
65    pub(super) fn at_end(&self) -> bool {
66        self.current().is_none() || self.check(&TokenKind::Eof)
67    }
68}