use crate::ast::{self, kw};
use crate::parser::{Cursor, Parse, Parser, Peek, Result};
#[derive(Debug)]
pub struct NestedModule<'a> {
pub span: ast::Span,
pub id: Option<ast::Id<'a>>,
pub name: Option<ast::NameAnnotation<'a>>,
pub exports: ast::InlineExport<'a>,
pub kind: NestedModuleKind<'a>,
}
#[derive(Debug)]
pub enum NestedModuleKind<'a> {
Import {
import: ast::InlineImport<'a>,
ty: ast::TypeUse<'a, ast::ModuleType<'a>>,
},
Inline {
fields: Vec<ast::ModuleField<'a>>,
},
}
impl<'a> Parse<'a> for NestedModule<'a> {
fn parse(parser: Parser<'a>) -> Result<Self> {
if parser.parens_depth() > 100 {
return Err(parser.error("module nesting too deep"));
}
let span = parser.parse::<kw::module>()?.0;
let id = parser.parse()?;
let name = parser.parse()?;
let exports = parser.parse()?;
let kind = if let Some(import) = parser.parse()? {
NestedModuleKind::Import {
import,
ty: parser.parse()?,
}
} else {
let mut fields = Vec::new();
while !parser.is_empty() {
fields.push(parser.parens(|p| p.parse())?);
}
NestedModuleKind::Inline { fields }
};
Ok(NestedModule {
span,
id,
name,
exports,
kind,
})
}
}
struct InlineType;
impl Peek for InlineType {
fn peek(cursor: Cursor<'_>) -> bool {
let cursor = match cursor.lparen() {
Some(cursor) => cursor,
None => return false,
};
let cursor = match cursor.keyword() {
Some(("type", cursor)) => cursor,
_ => return false,
};
let cursor = match cursor.id() {
Some((_, cursor)) => cursor,
None => match cursor.integer() {
Some((_, cursor)) => cursor,
None => return false,
},
};
cursor.rparen().is_some()
}
fn display() -> &'static str {
"inline type"
}
}