cl-parser: cleanup doc comments + add new error type
This commit is contained in:
parent
6ea99fc6f5
commit
3bebac6798
@ -22,10 +22,13 @@ pub enum ErrorKind {
|
|||||||
UnmatchedCurlyBraces,
|
UnmatchedCurlyBraces,
|
||||||
UnmatchedSquareBrackets,
|
UnmatchedSquareBrackets,
|
||||||
Unexpected(TokenKind),
|
Unexpected(TokenKind),
|
||||||
Expected {
|
ExpectedToken {
|
||||||
want: TokenKind,
|
want: TokenKind,
|
||||||
got: TokenKind,
|
got: TokenKind,
|
||||||
},
|
},
|
||||||
|
ExpectedParsing {
|
||||||
|
want: Parsing,
|
||||||
|
},
|
||||||
/// No rules matched
|
/// No rules matched
|
||||||
Nothing,
|
Nothing,
|
||||||
/// Indicates unfinished code
|
/// Indicates unfinished code
|
||||||
@ -67,6 +70,7 @@ pub enum Parsing {
|
|||||||
Variant,
|
Variant,
|
||||||
VariantKind,
|
VariantKind,
|
||||||
Impl,
|
Impl,
|
||||||
|
ImplKind,
|
||||||
|
|
||||||
Ty,
|
Ty,
|
||||||
TyKind,
|
TyKind,
|
||||||
@ -129,9 +133,12 @@ impl Display for ErrorKind {
|
|||||||
ErrorKind::UnmatchedCurlyBraces => write!(f, "Unmatched curly braces"),
|
ErrorKind::UnmatchedCurlyBraces => write!(f, "Unmatched curly braces"),
|
||||||
ErrorKind::UnmatchedSquareBrackets => write!(f, "Unmatched square brackets"),
|
ErrorKind::UnmatchedSquareBrackets => write!(f, "Unmatched square brackets"),
|
||||||
ErrorKind::Unexpected(t) => write!(f, "Encountered unexpected token `{t}`"),
|
ErrorKind::Unexpected(t) => write!(f, "Encountered unexpected token `{t}`"),
|
||||||
ErrorKind::Expected { want: e, got: g } => {
|
ErrorKind::ExpectedToken { want: e, got: g } => {
|
||||||
write!(f, "Expected `{e}`, got `{g}`")
|
write!(f, "Expected `{e}`, got `{g}`")
|
||||||
}
|
}
|
||||||
|
ErrorKind::ExpectedParsing { want } => {
|
||||||
|
write!(f, "Expected {want}")
|
||||||
|
}
|
||||||
ErrorKind::Nothing => write!(f, "Nothing found"),
|
ErrorKind::Nothing => write!(f, "Nothing found"),
|
||||||
ErrorKind::Todo => write!(f, "TODO:"),
|
ErrorKind::Todo => write!(f, "TODO:"),
|
||||||
}
|
}
|
||||||
@ -164,6 +171,7 @@ impl Display for Parsing {
|
|||||||
Parsing::Variant => "an enum variant",
|
Parsing::Variant => "an enum variant",
|
||||||
Parsing::VariantKind => "an enum variant",
|
Parsing::VariantKind => "an enum variant",
|
||||||
Parsing::Impl => "an impl block",
|
Parsing::Impl => "an impl block",
|
||||||
|
Parsing::ImplKind => "the target of an impl block",
|
||||||
|
|
||||||
Parsing::Ty => "a type",
|
Parsing::Ty => "a type",
|
||||||
Parsing::TyKind => "a type",
|
Parsing::TyKind => "a type",
|
||||||
|
@ -80,7 +80,7 @@ impl<'t> Parser<'t> {
|
|||||||
if got == want {
|
if got == want {
|
||||||
Ok(self.consume_peeked().expect("should not fail after peek"))
|
Ok(self.consume_peeked().expect("should not fail after peek"))
|
||||||
} else {
|
} else {
|
||||||
Err(self.error(Expected { want, got }, while_parsing))
|
Err(self.error(ExpectedToken { want, got }, while_parsing))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#[inline]
|
#[inline]
|
||||||
@ -263,9 +263,11 @@ impl<'t> Parser<'t> {
|
|||||||
);
|
);
|
||||||
Ok(Attrs { meta: meta(self)? })
|
Ok(Attrs { meta: meta(self)? })
|
||||||
}
|
}
|
||||||
|
/// Parses a single [attribute](Meta)
|
||||||
pub fn meta(&mut self) -> PResult<Meta> {
|
pub fn meta(&mut self) -> PResult<Meta> {
|
||||||
Ok(Meta { name: self.identifier()?, kind: self.meta_kind()? })
|
Ok(Meta { name: self.identifier()?, kind: self.meta_kind()? })
|
||||||
}
|
}
|
||||||
|
/// Parses data associated with a [Meta] attribute
|
||||||
pub fn meta_kind(&mut self) -> PResult<MetaKind> {
|
pub fn meta_kind(&mut self) -> PResult<MetaKind> {
|
||||||
const PARSING: Parsing = Parsing::Meta;
|
const PARSING: Parsing = Parsing::Meta;
|
||||||
let lit_tuple = delim(
|
let lit_tuple = delim(
|
||||||
@ -303,9 +305,11 @@ impl<'t> Parser<'t> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Parses a [`type` alias](Alias)
|
||||||
pub fn parse_alias(&mut self) -> PResult<Alias> {
|
pub fn parse_alias(&mut self) -> PResult<Alias> {
|
||||||
const PARSING: Parsing = Parsing::Alias;
|
const PARSING: Parsing = Parsing::Alias;
|
||||||
self.match_type(TokenKind::Type, PARSING)?;
|
self.match_type(TokenKind::Type, PARSING)?;
|
||||||
|
|
||||||
let out = Ok(Alias {
|
let out = Ok(Alias {
|
||||||
to: self.identifier()?,
|
to: self.identifier()?,
|
||||||
from: if self.match_op(Punct::Eq, PARSING).is_ok() {
|
from: if self.match_op(Punct::Eq, PARSING).is_ok() {
|
||||||
@ -318,9 +322,11 @@ impl<'t> Parser<'t> {
|
|||||||
out
|
out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Parses a [compile-time constant](Const)
|
||||||
pub fn parse_const(&mut self) -> PResult<Const> {
|
pub fn parse_const(&mut self) -> PResult<Const> {
|
||||||
const PARSING: Parsing = Parsing::Const;
|
const PARSING: Parsing = Parsing::Const;
|
||||||
self.match_type(TokenKind::Const, PARSING)?;
|
self.match_type(TokenKind::Const, PARSING)?;
|
||||||
|
|
||||||
let out = Ok(Const {
|
let out = Ok(Const {
|
||||||
name: self.identifier()?,
|
name: self.identifier()?,
|
||||||
ty: {
|
ty: {
|
||||||
@ -335,9 +341,12 @@ impl<'t> Parser<'t> {
|
|||||||
self.match_op(Punct::Semi, PARSING)?;
|
self.match_op(Punct::Semi, PARSING)?;
|
||||||
out
|
out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Parses a [`static` item](Static)
|
||||||
pub fn parse_static(&mut self) -> PResult<Static> {
|
pub fn parse_static(&mut self) -> PResult<Static> {
|
||||||
const PARSING: Parsing = Parsing::Static;
|
const PARSING: Parsing = Parsing::Static;
|
||||||
self.match_type(TokenKind::Static, PARSING)?;
|
self.match_type(TokenKind::Static, PARSING)?;
|
||||||
|
|
||||||
let out = Ok(Static {
|
let out = Ok(Static {
|
||||||
mutable: self.mutability()?,
|
mutable: self.mutability()?,
|
||||||
name: self.identifier()?,
|
name: self.identifier()?,
|
||||||
@ -353,11 +362,16 @@ impl<'t> Parser<'t> {
|
|||||||
self.match_op(Punct::Semi, PARSING)?;
|
self.match_op(Punct::Semi, PARSING)?;
|
||||||
out
|
out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Parses a [Module]
|
||||||
pub fn parse_module(&mut self) -> PResult<Module> {
|
pub fn parse_module(&mut self) -> PResult<Module> {
|
||||||
const PARSING: Parsing = Parsing::Module;
|
const PARSING: Parsing = Parsing::Module;
|
||||||
self.match_type(TokenKind::Mod, PARSING)?;
|
self.match_type(TokenKind::Mod, PARSING)?;
|
||||||
|
|
||||||
Ok(Module { name: self.identifier()?, kind: self.modulekind()? })
|
Ok(Module { name: self.identifier()?, kind: self.modulekind()? })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Parses the item list associated with a [Module], if present
|
||||||
pub fn modulekind(&mut self) -> PResult<ModuleKind> {
|
pub fn modulekind(&mut self) -> PResult<ModuleKind> {
|
||||||
const PARSING: Parsing = Parsing::ModuleKind;
|
const PARSING: Parsing = Parsing::ModuleKind;
|
||||||
let inline = delim(Self::file, CURLIES, PARSING);
|
let inline = delim(Self::file, CURLIES, PARSING);
|
||||||
@ -369,14 +383,17 @@ impl<'t> Parser<'t> {
|
|||||||
Ok(ModuleKind::Outline)
|
Ok(ModuleKind::Outline)
|
||||||
}
|
}
|
||||||
got => Err(self.error(
|
got => Err(self.error(
|
||||||
Expected { want: TokenKind::Punct(Punct::Semi), got },
|
ExpectedToken { want: TokenKind::Punct(Punct::Semi), got },
|
||||||
PARSING,
|
PARSING,
|
||||||
)),
|
)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Parses a [Function] definition
|
||||||
pub fn parse_function(&mut self) -> PResult<Function> {
|
pub fn parse_function(&mut self) -> PResult<Function> {
|
||||||
const PARSING: Parsing = Parsing::Function;
|
const PARSING: Parsing = Parsing::Function;
|
||||||
self.match_type(TokenKind::Fn, PARSING)?;
|
self.match_type(TokenKind::Fn, PARSING)?;
|
||||||
|
|
||||||
Ok(Function {
|
Ok(Function {
|
||||||
name: self.identifier()?,
|
name: self.identifier()?,
|
||||||
args: self.parse_params()?,
|
args: self.parse_params()?,
|
||||||
@ -387,7 +404,7 @@ impl<'t> Parser<'t> {
|
|||||||
Some(self.ty()?.into())
|
Some(self.ty()?.into())
|
||||||
}
|
}
|
||||||
got => Err(self.error(
|
got => Err(self.error(
|
||||||
Expected { want: TokenKind::Punct(Punct::Arrow), got },
|
ExpectedToken { want: TokenKind::Punct(Punct::Arrow), got },
|
||||||
PARSING,
|
PARSING,
|
||||||
))?,
|
))?,
|
||||||
},
|
},
|
||||||
@ -401,6 +418,8 @@ impl<'t> Parser<'t> {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Parses the [parameters](Param) associated with a Function
|
||||||
pub fn parse_params(&mut self) -> PResult<Vec<Param>> {
|
pub fn parse_params(&mut self) -> PResult<Vec<Param>> {
|
||||||
const PARSING: Parsing = Parsing::Function;
|
const PARSING: Parsing = Parsing::Function;
|
||||||
delim(
|
delim(
|
||||||
@ -409,6 +428,8 @@ impl<'t> Parser<'t> {
|
|||||||
PARSING,
|
PARSING,
|
||||||
)(self)
|
)(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Parses a single function [parameter](Param)
|
||||||
pub fn parse_param(&mut self) -> PResult<Param> {
|
pub fn parse_param(&mut self) -> PResult<Param> {
|
||||||
Ok(Param {
|
Ok(Param {
|
||||||
mutability: self.mutability()?,
|
mutability: self.mutability()?,
|
||||||
@ -419,9 +440,12 @@ impl<'t> Parser<'t> {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Parses a [`struct` definition](Struct)
|
||||||
pub fn parse_struct(&mut self) -> PResult<Struct> {
|
pub fn parse_struct(&mut self) -> PResult<Struct> {
|
||||||
const PARSING: Parsing = Parsing::Struct;
|
const PARSING: Parsing = Parsing::Struct;
|
||||||
self.match_type(TokenKind::Struct, PARSING)?;
|
self.match_type(TokenKind::Struct, PARSING)?;
|
||||||
|
|
||||||
Ok(Struct {
|
Ok(Struct {
|
||||||
name: self.identifier()?,
|
name: self.identifier()?,
|
||||||
kind: match self.peek_kind(PARSING)? {
|
kind: match self.peek_kind(PARSING)? {
|
||||||
@ -432,12 +456,14 @@ impl<'t> Parser<'t> {
|
|||||||
StructKind::Empty
|
StructKind::Empty
|
||||||
}
|
}
|
||||||
got => Err(self.error(
|
got => Err(self.error(
|
||||||
Expected { want: TokenKind::Punct(Punct::Semi), got },
|
ExpectedToken { want: TokenKind::Punct(Punct::Semi), got },
|
||||||
PARSING,
|
PARSING,
|
||||||
))?,
|
))?,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Parses a [tuple-`struct`](StructKind::Tuple)'s members
|
||||||
pub fn structkind_tuple(&mut self) -> PResult<StructKind> {
|
pub fn structkind_tuple(&mut self) -> PResult<StructKind> {
|
||||||
const PARSING: Parsing = Parsing::StructKind;
|
const PARSING: Parsing = Parsing::StructKind;
|
||||||
|
|
||||||
@ -447,14 +473,19 @@ impl<'t> Parser<'t> {
|
|||||||
PARSING,
|
PARSING,
|
||||||
)(self)?))
|
)(self)?))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Parses a [`struct`](StructKind::Struct)s members
|
||||||
pub fn structkind_struct(&mut self) -> PResult<StructKind> {
|
pub fn structkind_struct(&mut self) -> PResult<StructKind> {
|
||||||
const PARSING: Parsing = Parsing::StructKind;
|
const PARSING: Parsing = Parsing::StructKind;
|
||||||
|
|
||||||
Ok(StructKind::Struct(delim(
|
Ok(StructKind::Struct(delim(
|
||||||
sep(Self::struct_member, Punct::Comma, CURLIES.1, PARSING),
|
sep(Self::struct_member, Punct::Comma, CURLIES.1, PARSING),
|
||||||
CURLIES,
|
CURLIES,
|
||||||
PARSING,
|
PARSING,
|
||||||
)(self)?))
|
)(self)?))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Parses a single [StructMember]
|
||||||
pub fn struct_member(&mut self) -> PResult<StructMember> {
|
pub fn struct_member(&mut self) -> PResult<StructMember> {
|
||||||
const PARSING: Parsing = Parsing::StructMember;
|
const PARSING: Parsing = Parsing::StructMember;
|
||||||
Ok(StructMember {
|
Ok(StructMember {
|
||||||
@ -466,10 +497,13 @@ impl<'t> Parser<'t> {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Parses an [`enum`](Enum) definition
|
||||||
pub fn parse_enum(&mut self) -> PResult<Enum> {
|
pub fn parse_enum(&mut self) -> PResult<Enum> {
|
||||||
// Enum = "enum" Identifier '{' (Variant ',')* Variant? '}' ;
|
|
||||||
const PARSING: Parsing = Parsing::Enum;
|
const PARSING: Parsing = Parsing::Enum;
|
||||||
|
|
||||||
self.match_type(TokenKind::Enum, PARSING)?;
|
self.match_type(TokenKind::Enum, PARSING)?;
|
||||||
|
|
||||||
Ok(Enum {
|
Ok(Enum {
|
||||||
name: self.identifier()?,
|
name: self.identifier()?,
|
||||||
kind: match self.peek_kind(PARSING)? {
|
kind: match self.peek_kind(PARSING)? {
|
||||||
@ -487,8 +521,10 @@ impl<'t> Parser<'t> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Parses an [`enum`](Enum) [Variant]
|
||||||
pub fn enum_variant(&mut self) -> PResult<Variant> {
|
pub fn enum_variant(&mut self) -> PResult<Variant> {
|
||||||
const PARSING: Parsing = Parsing::Variant;
|
const PARSING: Parsing = Parsing::Variant;
|
||||||
|
|
||||||
Ok(Variant {
|
Ok(Variant {
|
||||||
name: self.identifier()?,
|
name: self.identifier()?,
|
||||||
kind: match self.peek_kind(PARSING)? {
|
kind: match self.peek_kind(PARSING)? {
|
||||||
@ -499,15 +535,21 @@ impl<'t> Parser<'t> {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Parses a [C-like](VariantKind::CLike) [`enum`](Enum) [Variant]
|
||||||
pub fn variantkind_clike(&mut self) -> PResult<VariantKind> {
|
pub fn variantkind_clike(&mut self) -> PResult<VariantKind> {
|
||||||
const PARSING: Parsing = Parsing::VariantKind;
|
const PARSING: Parsing = Parsing::VariantKind;
|
||||||
|
|
||||||
self.match_op(Punct::Eq, PARSING)?;
|
self.match_op(Punct::Eq, PARSING)?;
|
||||||
let tok = self.match_type(TokenKind::Literal, PARSING)?;
|
let tok = self.match_type(TokenKind::Literal, PARSING)?;
|
||||||
|
|
||||||
Ok(VariantKind::CLike(match tok.data() {
|
Ok(VariantKind::CLike(match tok.data() {
|
||||||
TokenData::Integer(i) => *i,
|
TokenData::Integer(i) => *i,
|
||||||
_ => panic!("Expected token data for {tok:?} while parsing {PARSING}"),
|
_ => panic!("Expected token data for {tok:?} while parsing {PARSING}"),
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Parses a [struct-like](VariantKind::Struct) [`enum`](Enum) [Variant]
|
||||||
pub fn variantkind_struct(&mut self) -> PResult<VariantKind> {
|
pub fn variantkind_struct(&mut self) -> PResult<VariantKind> {
|
||||||
const PARSING: Parsing = Parsing::VariantKind;
|
const PARSING: Parsing = Parsing::VariantKind;
|
||||||
Ok(VariantKind::Struct(delim(
|
Ok(VariantKind::Struct(delim(
|
||||||
@ -516,6 +558,8 @@ impl<'t> Parser<'t> {
|
|||||||
PARSING,
|
PARSING,
|
||||||
)(self)?))
|
)(self)?))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Parses a [tuple-like](VariantKind::Tuple) [`enum`](Enum) [Variant]
|
||||||
pub fn variantkind_tuple(&mut self) -> PResult<VariantKind> {
|
pub fn variantkind_tuple(&mut self) -> PResult<VariantKind> {
|
||||||
const PARSING: Parsing = Parsing::VariantKind;
|
const PARSING: Parsing = Parsing::VariantKind;
|
||||||
Ok(VariantKind::Tuple(delim(
|
Ok(VariantKind::Tuple(delim(
|
||||||
@ -527,6 +571,7 @@ impl<'t> Parser<'t> {
|
|||||||
|
|
||||||
pub fn parse_impl(&mut self) -> PResult<Impl> {
|
pub fn parse_impl(&mut self) -> PResult<Impl> {
|
||||||
const PARSING: Parsing = Parsing::Impl;
|
const PARSING: Parsing = Parsing::Impl;
|
||||||
|
|
||||||
self.match_type(TokenKind::Impl, PARSING)?;
|
self.match_type(TokenKind::Impl, PARSING)?;
|
||||||
Err(self.error(Todo, PARSING))
|
Err(self.error(Todo, PARSING))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user