rust: get rid of parsing trait parameters
This commit is contained in:
parent
468474fcb2
commit
b2c7935968
|
@ -14,7 +14,7 @@ pub enum Expr {
|
|||
|
||||
Lambda(Lambda),
|
||||
TyLambda(Lambda),
|
||||
LinTyLambda(Lambda),
|
||||
TyLambdaLin(Lambda),
|
||||
Apply(Box<Expr>, Vec<(EvEqSourceSpan, Expr)>),
|
||||
|
||||
Record(Record<Expr>),
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
use crate::{lex, Env as ParseEnv, Error, ErrorCtx, EvEqSourceSpan, MaybeParse, ParseDefaultCtx};
|
||||
use crate::{lex, Env as ParseEnv, Error, ErrorCtx, EvEqSourceSpan, MaybeParse};
|
||||
|
||||
pub use yanais_literal::{IntSize, Literal, TyLit};
|
||||
|
||||
|
@ -16,12 +16,10 @@ pub enum Kw {
|
|||
TyLambdaLin,
|
||||
}
|
||||
|
||||
impl ParseDefaultCtx for (EvEqSourceSpan, Literal) {
|
||||
impl MaybeParse for (EvEqSourceSpan, Literal) {
|
||||
const DFL_CTX: ErrorCtx = ErrorCtx::Literal;
|
||||
}
|
||||
|
||||
impl MaybeParse<Kw> for (EvEqSourceSpan, Literal) {
|
||||
fn maybe_parse(env: &mut ParseEnv<'_, Kw>) -> Result<Option<Self>, Error> {
|
||||
fn maybe_parse(env: &mut ParseEnv<'_>) -> Result<Option<Self>, Error> {
|
||||
let mut nxtlxr = env.lxr.clone();
|
||||
let lex::Token {
|
||||
kind,
|
||||
|
|
|
@ -97,12 +97,12 @@ impl<'a> From<&'a EvEqSourceSpan> for SourceSpan {
|
|||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Env<'a, Kw> {
|
||||
pub struct Env<'a> {
|
||||
pub lxr: lex::Lexer<'a, Kw>,
|
||||
pub names: Vec<Arc<str>>,
|
||||
}
|
||||
|
||||
impl<'a, Kw> Env<'a, Kw> {
|
||||
impl<'a> Env<'a> {
|
||||
pub fn new(lxr: lex::Lexer<'a, Kw>) -> Self {
|
||||
Self {
|
||||
lxr,
|
||||
|
@ -123,32 +123,26 @@ impl<'a, Kw> Env<'a, Kw> {
|
|||
}
|
||||
}
|
||||
|
||||
pub trait Keywords: Sized + cmp::PartialEq + core::str::FromStr {}
|
||||
impl<T: Sized + cmp::PartialEq + core::str::FromStr> Keywords for T {}
|
||||
|
||||
pub trait Parse<Kw: Keywords>: Sized {
|
||||
fn parse(env: &mut Env<'_, Kw>) -> Result<Self>;
|
||||
pub trait Parse: Sized {
|
||||
fn parse(env: &mut Env<'_>) -> Result<Self>;
|
||||
}
|
||||
|
||||
pub trait ParseDefaultCtx: Sized {
|
||||
pub trait MaybeParse: Sized {
|
||||
const DFL_CTX: ErrorCtx;
|
||||
}
|
||||
|
||||
pub trait MaybeParse<Kw: Keywords>: ParseDefaultCtx {
|
||||
/// this function allows to clearly differentiate between
|
||||
/// recoverable failures and unrecoverable ones.
|
||||
fn maybe_parse(env: &mut Env<'_, Kw>) -> Result<Option<Self>>;
|
||||
fn maybe_parse(env: &mut Env<'_>) -> Result<Option<Self>>;
|
||||
}
|
||||
|
||||
impl<Kw: Keywords, T: MaybeParse<Kw>> Parse<Kw> for T {
|
||||
fn parse(env: &mut Env<'_, Kw>) -> Result<Self> {
|
||||
impl<T: MaybeParse> Parse for T {
|
||||
fn parse(env: &mut Env<'_>) -> Result<Self> {
|
||||
let knamcnt = env.names.len();
|
||||
let span = env.lxr.peek_span();
|
||||
let mres = T::maybe_parse(env);
|
||||
assert_eq!(env.names.len(), knamcnt);
|
||||
mres?.ok_or_else(|| Error {
|
||||
span,
|
||||
kind: ErrorKind::Expected(<T as ParseDefaultCtx>::DFL_CTX),
|
||||
kind: ErrorKind::Expected(<T as MaybeParse>::DFL_CTX),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -166,12 +160,10 @@ pub struct SelIdent {
|
|||
pub dbidx: usize,
|
||||
}
|
||||
|
||||
impl ParseDefaultCtx for TaggedIdent {
|
||||
impl MaybeParse for TaggedIdent {
|
||||
const DFL_CTX: ErrorCtx = ErrorCtx::Ident;
|
||||
}
|
||||
|
||||
impl<Kw: Keywords> MaybeParse<Kw> for TaggedIdent {
|
||||
fn maybe_parse(env: &mut Env<'_, Kw>) -> Result<Option<Self>> {
|
||||
fn maybe_parse(env: &mut Env<'_>) -> Result<Option<Self>> {
|
||||
let lxrbak = env.lxr.clone();
|
||||
let lex::Token { span, kind } = none_up!(env.lxr.next())?;
|
||||
|
||||
|
@ -184,12 +176,10 @@ impl<Kw: Keywords> MaybeParse<Kw> for TaggedIdent {
|
|||
}
|
||||
}
|
||||
|
||||
impl ParseDefaultCtx for SelIdent {
|
||||
impl MaybeParse for SelIdent {
|
||||
const DFL_CTX: ErrorCtx = ErrorCtx::Ident;
|
||||
}
|
||||
|
||||
impl<Kw: Keywords> MaybeParse<Kw> for SelIdent {
|
||||
fn maybe_parse(env: &mut Env<'_, Kw>) -> Result<Option<Self>> {
|
||||
fn maybe_parse(env: &mut Env<'_>) -> Result<Option<Self>> {
|
||||
let lxrbak = env.lxr.clone();
|
||||
let lex::Token { span, kind } = none_up!(env.lxr.next())?;
|
||||
|
||||
|
|
|
@ -5,8 +5,8 @@
|
|||
*/
|
||||
|
||||
use crate::{
|
||||
lex, record::Record, Env as ParseEnv, Error, ErrorCtx, EvEqSourceSpan, Keywords, MaybeParse,
|
||||
Parse, ParseDefaultCtx,
|
||||
lex, record::Record, Env as ParseEnv, Error, ErrorCtx, EvEqSourceSpan, MaybeParse,
|
||||
Parse
|
||||
};
|
||||
use std::sync::Arc;
|
||||
|
||||
|
@ -32,12 +32,10 @@ impl Pattern {
|
|||
}
|
||||
}
|
||||
|
||||
impl ParseDefaultCtx for Pattern {
|
||||
impl MaybeParse for Pattern {
|
||||
const DFL_CTX: ErrorCtx = ErrorCtx::Pattern;
|
||||
}
|
||||
|
||||
impl<Kw: Keywords> MaybeParse<Kw> for Pattern {
|
||||
fn maybe_parse(env: &mut ParseEnv<'_, Kw>) -> Result<Option<Self>, Error> {
|
||||
fn maybe_parse(env: &mut ParseEnv<'_>) -> Result<Option<Self>, Error> {
|
||||
let mut nxtlxr = env.lxr.clone();
|
||||
let lex::Token {
|
||||
kind,
|
||||
|
|
|
@ -4,13 +4,12 @@
|
|||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
use core::cmp;
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::{
|
||||
lex::{Token, TokenKind as Tok},
|
||||
none_up, Env as ParseEnv, Error as Perr, ErrorCtx as PeCtx, ErrorKind as Pek, EvEqSourceSpan,
|
||||
Keywords, MaybeParse, Parse, ParseDefaultCtx, Result as Pres,
|
||||
MaybeParse, Parse, Result as Pres,
|
||||
};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
|
@ -19,12 +18,10 @@ pub struct Record<V> {
|
|||
pub fields: Vec<(Option<Arc<str>>, V)>,
|
||||
}
|
||||
|
||||
impl<V> ParseDefaultCtx for Record<V> {
|
||||
impl<V: Parse> MaybeParse for Record<V> {
|
||||
const DFL_CTX: PeCtx = PeCtx::Record;
|
||||
}
|
||||
|
||||
impl<Kw: Keywords + cmp::PartialEq, V: Parse<Kw>> MaybeParse<Kw> for Record<V> {
|
||||
fn maybe_parse(env: &mut ParseEnv<'_, Kw>) -> Pres<Option<Self>> {
|
||||
fn maybe_parse(env: &mut ParseEnv<'_>) -> Pres<Option<Self>> {
|
||||
let start_span = none_up!(env.lxr.got(Tok::LBrace));
|
||||
let mut fields: Vec<(Option<Arc<str>>, _)> = Vec::new();
|
||||
|
||||
|
|
|
@ -16,7 +16,7 @@ use yanais_syntax::none_up;
|
|||
pub mod parser {
|
||||
use yanais_syntax::lex as upslex;
|
||||
pub use yanais_syntax::{
|
||||
Error, ErrorCtx, ErrorKind, EvEqSourceSpan, FullError, MaybeParse, Parse, ParseDefaultCtx,
|
||||
Error, ErrorCtx, ErrorKind, EvEqSourceSpan, FullError, MaybeParse, Parse,
|
||||
Result,
|
||||
};
|
||||
|
||||
|
@ -83,11 +83,9 @@ pub struct SelIdent {
|
|||
pub dbidx: usize,
|
||||
}
|
||||
|
||||
impl ParseDefaultCtx for TaggedIdent {
|
||||
const DFL_CTX: PeCtx = PeCtx::Ident;
|
||||
}
|
||||
|
||||
impl MaybeParse<Kw> for TaggedIdent {
|
||||
const DFL_CTX: PeCtx = PeCtx::Ident;
|
||||
|
||||
fn maybe_parse(env: &mut ParseEnv<'_>) -> Pres<Option<Self>> {
|
||||
let lxrbak = env.lxr.clone();
|
||||
let Token { span, kind } = none_up!(env.lxr.next())?;
|
||||
|
@ -101,11 +99,9 @@ impl MaybeParse<Kw> for TaggedIdent {
|
|||
}
|
||||
}
|
||||
|
||||
impl ParseDefaultCtx for SelIdent {
|
||||
const DFL_CTX: PeCtx = PeCtx::Ident;
|
||||
}
|
||||
|
||||
impl MaybeParse<Kw> for SelIdent {
|
||||
const DFL_CTX: PeCtx = PeCtx::Ident;
|
||||
|
||||
fn maybe_parse(env: &mut ParseEnv<'_>) -> Pres<Option<Self>> {
|
||||
let TaggedIdent { span: _, name: _ } = none_up!(TaggedIdent::maybe_parse(env)?);
|
||||
let lxrbak = env.lxr.clone();
|
||||
|
|
Loading…
Reference in a new issue