use crate::{
grammar::ParseResult,
kind::{CppSyntaxKind, CppTokenKind},
parser::{CompleteMarker, CppParser, MarkerEventContainer},
parser_error::CppParseError,
};
use super::{expect_token, exprs::parse_expr, stats::parse_stats};
pub fn starts_module_related_declaration(p: &CppParser) -> bool {
is_contextual(p, "module")
&& (module_declaration_shape(p)
|| p.peek_token_kind_at(1..2)[0] == CppTokenKind::Colon
|| p.peek_token_kind_at(1..2)[0] == CppTokenKind::Semicolon)
|| starts_module_declaration(p)
|| starts_import_declaration(p)
}
pub fn starts_module_declaration(p: &CppParser) -> bool {
is_contextual(p, "module") && module_declaration_shape(p)
}
pub fn starts_import_declaration(p: &CppParser) -> bool {
is_contextual(p, "import") && import_declaration_shape(p)
}
fn is_contextual(p: &CppParser, text: &str) -> bool {
super::is_contextual_keyword(p, text)
}
fn module_declaration_shape(p: &CppParser) -> bool {
matches!(
p.peek_token_kind_at(1..2)[0],
CppTokenKind::Identifier | CppTokenKind::Colon | CppTokenKind::Semicolon
)
}
fn import_declaration_shape(p: &CppParser) -> bool {
matches!(
p.peek_token_kind_at(1..2)[0],
CppTokenKind::Identifier
| CppTokenKind::Colon
| CppTokenKind::Less
| CppTokenKind::StringLiteral
| CppTokenKind::HeaderName
)
}
pub fn parse_exported_declaration(p: &mut CppParser) -> ParseResult {
if is_contextual_at(p, 1, "module") {
return parse_module_declaration(p);
}
if is_contextual_at(p, 1, "import") {
return parse_import_declaration(p);
}
p.bump();
super::decls::parse_declaration(p)
}
fn is_contextual_at(p: &CppParser, offset: usize, text: &str) -> bool {
p.peek_token_kind_at(offset..offset + 1)[0] == CppTokenKind::Identifier
&& p.peek_token_text_at(offset) == text
}
pub fn parse_module_declaration(p: &mut CppParser) -> ParseResult {
let base = p.open_marks();
let m = p.mark(CppSyntaxKind::ModuleDecl);
if p.current_token() == CppTokenKind::ExportKeyword {
p.bump();
}
if !is_contextual(p, "module") {
p.close_marks_above(base);
return Err(CppParseError::syntax_error_from(
&t!("expected `module`"),
p.current_token_range(),
));
}
p.bump();
parse_optional_module_name(p)?;
parse_optional_module_partition(p)?;
eat_attributes(p);
expect_token(p, CppTokenKind::Semicolon)?;
Ok(m.complete(p))
}
pub fn parse_import_declaration(p: &mut CppParser) -> ParseResult {
let base = p.open_marks();
let m = p.mark(CppSyntaxKind::ImportDecl);
if p.current_token() == CppTokenKind::ExportKeyword {
p.bump();
}
if !is_contextual(p, "import") {
p.close_marks_above(base);
return Err(CppParseError::syntax_error_from(
&t!("expected `import`"),
p.current_token_range(),
));
}
p.bump();
match p.current_token() {
CppTokenKind::Colon => {
parse_optional_module_partition(p)?;
}
CppTokenKind::Less | CppTokenKind::StringLiteral | CppTokenKind::HeaderName => {
parse_header_unit_name(p)?;
}
_ => {
parse_optional_module_name(p)?;
}
}
eat_attributes(p);
expect_token(p, CppTokenKind::Semicolon)?;
Ok(m.complete(p))
}
pub fn parse_export_block(p: &mut CppParser) -> ParseResult {
let _ = p.open_marks();
let m = p.mark(CppSyntaxKind::ExportBlock);
expect_token(p, CppTokenKind::ExportKeyword)?;
expect_token(p, CppTokenKind::LeftBrace)?;
parse_stats(p);
if p.current_token() == CppTokenKind::RightBrace {
p.bump();
} else {
p.emit_missing_node();
}
Ok(m.complete(p))
}
pub fn parse_global_module_fragment(p: &mut CppParser) -> ParseResult {
let base = p.open_marks();
let m = p.mark(CppSyntaxKind::GlobalModuleFragment);
if !is_contextual(p, "module") {
p.close_marks_above(base);
return Err(CppParseError::syntax_error_from(
&t!("expected `module`"),
p.current_token_range(),
));
}
p.bump();
expect_token(p, CppTokenKind::Semicolon)?;
Ok(m.complete(p))
}
pub fn parse_private_module_fragment(p: &mut CppParser) -> ParseResult {
let base = p.open_marks();
let m = p.mark(CppSyntaxKind::PrivateModuleFragment);
if !is_contextual(p, "module") {
p.close_marks_above(base);
return Err(CppParseError::syntax_error_from(
&t!("expected `module`"),
p.current_token_range(),
));
}
p.bump();
expect_token(p, CppTokenKind::Colon)?;
if !is_contextual(p, "private") && p.current_token() != CppTokenKind::PrivateKeyword {
p.close_marks_above(base);
return Err(CppParseError::syntax_error_from(
&t!("expected `private`"),
p.current_token_range(),
));
}
p.bump();
expect_token(p, CppTokenKind::Semicolon)?;
Ok(m.complete(p))
}
fn parse_optional_module_name(p: &mut CppParser) -> ParseResult {
let base = p.open_marks();
let m = p.mark(CppSyntaxKind::ModuleName);
if p.current_token() != CppTokenKind::Identifier {
p.emit_missing_node();
p.close_marks_above(base);
return Ok(CompleteMarker::empty());
}
p.bump();
while p.current_token() == CppTokenKind::Dot {
p.bump();
if p.current_token() == CppTokenKind::Identifier {
p.bump();
} else {
p.emit_missing_node();
break;
}
}
Ok(m.complete(p))
}
fn parse_optional_module_partition(p: &mut CppParser) -> ParseResult {
let base = p.open_marks();
if p.current_token() != CppTokenKind::Colon {
p.close_marks_above(base);
return Ok(CompleteMarker::empty());
}
let m = p.mark(CppSyntaxKind::ModulePartition);
p.bump(); parse_optional_module_name(p)?;
Ok(m.complete(p))
}
fn parse_header_unit_name(p: &mut CppParser) -> ParseResult {
let base = p.open_marks();
let m = p.mark(CppSyntaxKind::HeaderName);
if p.try_lex_header_name() {
return Ok(m.complete(p));
}
if let Err(err) = parse_expr(p) {
p.close_marks_above(base);
return Err(err);
}
Ok(m.complete(p))
}
fn eat_attributes(p: &mut CppParser) {
while p.current_token() == CppTokenKind::LeftBracket
&& p.peek_next_token() == CppTokenKind::LeftBracket
{
if super::types::parse_attribute_specifier(p).is_err() {
return;
}
}
}
pub fn starts_global_module_fragment(p: &CppParser) -> bool {
is_contextual(p, "module") && p.peek_token_kind_at(1..2)[0] == CppTokenKind::Semicolon
}
pub fn starts_private_module_fragment(p: &CppParser) -> bool {
is_contextual(p, "module")
&& p.peek_token_kind_at(1..4).as_slice()
== [
CppTokenKind::Colon,
CppTokenKind::PrivateKeyword,
CppTokenKind::Semicolon,
]
}