#![deny(missing_docs)]
use crate::common::source::{Span, INVALID_SPAN};
use crate::common::score::{NodeRef, Result};
use crate::common::errors::*;
use crate::common::util::{HasSpan, HasDesc};
use crate::make_ctx::MakeContext;
use crate::score::ScoreContext;
use crate::score::ScopeRef;
#[derive(Copy, Clone)]
pub struct AddContext<'sbc, 'lazy: 'sbc, 'sb: 'lazy, 'ast: 'sb, 'ctx: 'sb> {
pub ctx: &'sbc ScoreContext<'lazy, 'sb, 'ast, 'ctx>,
pub scope: ScopeRef,
}
impl<'sbc, 'lazy, 'sb, 'ast, 'ctx> AddContext<'sbc, 'lazy, 'sb, 'ast, 'ctx> {
pub fn new(ctx: &'sbc ScoreContext<'lazy, 'sb, 'ast, 'ctx>, scope: ScopeRef) -> AddContext<'sbc, 'lazy, 'sb, 'ast, 'ctx> {
AddContext {
ctx: ctx,
scope: scope,
}
}
pub fn with_scope(&self, scope: ScopeRef) -> AddContext<'sbc, 'lazy, 'sb, 'ast, 'ctx> {
AddContext {
scope: scope,
..*self
}
}
pub fn make<I>(
&self,
span: Span
) -> (MakeContext<'sbc, 'lazy, 'sb, 'ast, 'ctx, I>, I, ScopeRef)
where I: NodeRef
{
assert!(span != INVALID_SPAN);
let id = I::alloc();
(MakeContext::new(self.ctx, span, id), id, self.scope)
}
pub fn unimp<T,R>(&self, node: &T) -> Result<R> where T: HasSpan + HasDesc {
self.emit(
DiagBuilder2::bug(format!("{} not implemented", node.desc()))
.span(node.human_span())
);
Err(())
}
pub fn add_optional<T,F,R>(&self, node: &'ast Option<T>, f: F) -> Result<Option<R>>
where F: FnOnce(&Self, &'ast T) -> Result<R>
{
Ok(match *node {
Some(ref ast) => Some(f(self, ast)?),
None => None,
})
}
}
impl<'sbc, 'lazy, 'sb, 'ast, 'ctx> DiagEmitter for AddContext<'sbc, 'lazy, 'sb, 'ast, 'ctx> {
fn emit(&self, diag: DiagBuilder2) {
self.ctx.emit(diag)
}
}