mod decls;
mod exprs;
mod modules;
mod stats;
mod types;
use stats::parse_stats;
use crate::{
kind::{CppSyntaxKind, CppTokenKind},
parser::{CppParser, MarkerEventContainer},
parser_error::CppParseError,
};
pub fn parse_cpp_unit(p: &mut CppParser) {
let base = p.open_marks();
let m = p.mark(CppSyntaxKind::TranslationUnit);
p.init();
while p.current_token() != CppTokenKind::Eof {
let consume_count = p.current_token_index();
parse_stats(p);
if p.current_token_index() == consume_count {
let m = p.mark(CppSyntaxKind::ErrorNode);
p.bump();
p.push_error(CppParseError::syntax_error_from(
&t!("unexpected token"),
p.current_token_range(),
));
m.complete(p);
}
}
debug_assert_eq!(
p.open_marks(),
base + 1,
"a grammar rule unwound past the translation unit marker"
);
m.complete(p);
p.close_marks_above(base);
}
fn expect_token(p: &mut CppParser, token: CppTokenKind) -> Result<(), CppParseError> {
if p.current_token() == token {
p.bump();
Ok(())
} else {
Err(CppParseError::syntax_error_from(
&t!(
"expected %{token}, but get %{current}",
token = token,
current = p.current_token()
),
p.current_token_range(),
))
}
}
pub fn is_contextual_keyword(p: &CppParser, text: &str) -> bool {
p.current_token() == CppTokenKind::Identifier && p.current_token_text() == text
}
pub fn at_requires(p: &CppParser) -> bool {
is_contextual_keyword(p, "requires")
}
pub fn at_concept(p: &CppParser) -> bool {
is_contextual_keyword(p, "concept")
}
pub fn expect_contextual_keyword(p: &mut CppParser, text: &str) -> Result<(), CppParseError> {
if is_contextual_keyword(p, text) {
p.bump();
Ok(())
} else {
Err(CppParseError::syntax_error_from(
&t!(
"expected %{token}, but get %{current}",
token = text,
current = p.current_token()
),
p.current_token_range(),
))
}
}