1+ //! A parser for refinement type and formula annotations.
2+ //!
3+ //! This module provides a parser for `#[thrust::...]` attributes in Thrust. The parser is
4+ //! parameterized by the [`Resolver`] trait, which abstracts over the resolution of variable
5+ //! names. This allows the parser to be used in different contexts with different sets of
6+ //! available variables.
7+ //!
8+ //! The main entry point is [`AnnotParser`], which parses a [`TokenStream`] into a
9+ //! [`rty::RefinedType`] or a [`chc::Formula`].
10+
111use rustc_ast:: token:: { BinOpToken , Delimiter , LitKind , Token , TokenKind } ;
212use rustc_ast:: tokenstream:: { RefTokenTreeCursor , Spacing , TokenStream , TokenTree } ;
313use rustc_index:: IndexVec ;
@@ -7,6 +17,9 @@ use crate::chc;
717use crate :: pretty:: PrettyDisplayExt as _;
818use crate :: rty;
919
20+ /// A formula in an annotation.
21+ ///
22+ /// This can be either a logical formula or the special value `auto` which tells Thrust to infer it.
1023#[ derive( Debug , Clone ) ]
1124pub enum AnnotFormula < T > {
1225 Auto ,
@@ -19,6 +32,7 @@ impl<T> AnnotFormula<T> {
1932 }
2033}
2134
35+ /// A trait for resolving variables in annotations to their logical representation and their sorts.
2236pub trait Resolver {
2337 type Output ;
2438 fn resolve ( & self , ident : Ident ) -> Option < ( Self :: Output , chc:: Sort ) > ;
@@ -56,6 +70,7 @@ pub trait ResolverExt: Resolver {
5670
5771impl < T > ResolverExt for T where T : Resolver { }
5872
73+ /// An error that can occur when parsing an attribute.
5974#[ derive( Debug , Clone , thiserror:: Error ) ]
6075pub enum ParseAttrError {
6176 #[ error( "unexpected end of input (expected {expected:?})" ) ]
@@ -96,6 +111,7 @@ impl ParseAttrError {
96111
97112type Result < T > = std:: result:: Result < T , ParseAttrError > ;
98113
114+ /// A parser for refinement type annotations and formula annotations.
99115struct Parser < ' a , T > {
100116 resolver : T ,
101117 cursor : RefTokenTreeCursor < ' a > ,
@@ -610,6 +626,7 @@ where
610626 }
611627}
612628
629+ /// A [`Resolver`] implementation for resolving specific variable as [`rty::RefinedTypeVar::Value`].
613630struct RefinementResolver < ' a , T > {
614631 resolver : Box < dyn Resolver < Output = T > + ' a > ,
615632 self_ : Option < ( Ident , chc:: Sort ) > ,
@@ -643,6 +660,7 @@ impl<'a, T> RefinementResolver<'a, T> {
643660 }
644661}
645662
663+ /// A [`Resolver`] that maps the output of another [`Resolver`].
646664pub struct MappedResolver < ' a , T , F > {
647665 resolver : Box < dyn Resolver < Output = T > + ' a > ,
648666 map : F ,
@@ -669,6 +687,9 @@ impl<'a, T, F> MappedResolver<'a, T, F> {
669687 }
670688}
671689
690+ /// A [`Resolver`] that stacks multiple [`Resolver`]s.
691+ ///
692+ /// This resolver tries to resolve an identifier by querying a list of resolvers in order.
672693pub struct StackedResolver < ' a , T > {
673694 resolvers : Vec < Box < dyn Resolver < Output = T > + ' a > > ,
674695}
@@ -698,6 +719,7 @@ impl<'a, T> StackedResolver<'a, T> {
698719 }
699720}
700721
722+ /// A parser for annotations.
701723#[ derive( Debug , Clone ) ]
702724pub struct AnnotParser < T > {
703725 resolver : T ,
0 commit comments