Skip to main content

oxilean_parse/command/
commandparser_parsing_2.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, TokenKind};
8
9use super::types::Command;
10
11use super::commandparser_type::CommandParser;
12
13impl CommandParser {
14    /// Advance the position by one token.
15    pub(super) fn advance(&mut self) {
16        if self.pos < self.tokens.len() {
17            self.pos += 1;
18        }
19    }
20    /// If the current token matches `kind`, consume it and return true.
21    pub(super) fn consume(&mut self, kind: &TokenKind) -> bool {
22        if self.check(kind) {
23            self.advance();
24            true
25        } else {
26            false
27        }
28    }
29    /// Consume an identifier with a specific name.
30    #[allow(dead_code)]
31    pub(super) fn consume_ident(&mut self, name: &str) -> bool {
32        if self.check_ident(name) {
33            self.advance();
34            true
35        } else {
36            false
37        }
38    }
39    /// Parse `import <module>`.
40    pub(super) fn parse_import(&mut self) -> Result<Command, ParseError> {
41        let start_span = self.current_span();
42        self.advance();
43        let module = self.parse_dotted_name()?;
44        let span = start_span.merge(&self.current_span());
45        Ok(Command::Import { module, span })
46    }
47    /// Parse `namespace <name>`.
48    pub(super) fn parse_namespace(&mut self) -> Result<Command, ParseError> {
49        let start_span = self.current_span();
50        self.advance();
51        let name = self.parse_ident()?;
52        let span = start_span.merge(&self.current_span());
53        Ok(Command::Namespace { name, span })
54    }
55    /// Parse `end`.
56    pub(super) fn parse_end(&mut self) -> Result<Command, ParseError> {
57        let span = self.current_span();
58        self.advance();
59        Ok(Command::End { span })
60    }
61    /// Parse `section <name>`.
62    pub(super) fn parse_section(&mut self) -> Result<Command, ParseError> {
63        let start_span = self.current_span();
64        self.advance();
65        let name = self.parse_ident()?;
66        let span = start_span.merge(&self.current_span());
67        Ok(Command::Section { name, span })
68    }
69    /// Parse `set_option <name> <value>`.
70    pub(super) fn parse_set_option(&mut self) -> Result<Command, ParseError> {
71        let start_span = self.current_span();
72        self.advance();
73        let name = self.parse_dotted_name()?;
74        let value = self.collect_rest_as_string();
75        let span = start_span.merge(&self.current_span());
76        Ok(Command::SetOption { name, value, span })
77    }
78}