oxilean_parse/command/
commandparser_parsing_1.rs1use crate::{ParseError, Span, Token, TokenKind};
8
9use super::types::Command;
10
11use super::commandparser_type::CommandParser;
12
13impl CommandParser {
14 pub(super) fn current(&self) -> Option<&Token> {
16 self.tokens.get(self.pos)
17 }
18 pub(super) fn check(&self, kind: &TokenKind) -> bool {
20 self.current().is_some_and(|t| &t.kind == kind)
21 }
22 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 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 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 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 pub(super) fn at_end(&self) -> bool {
66 self.current().is_none() || self.check(&TokenKind::Eof)
67 }
68}