11pub mod version;
2+ use std:: { marker:: PhantomData , path:: PathBuf } ;
3+
24pub use version:: * ;
35
46pub ( crate ) mod parol_macro;
@@ -9,9 +11,56 @@ pub(crate) mod parol_macro;
911///
1012/// - `Parser`: The type that distinguishes the parser. This is used to
1113/// differentiate between different parsing strategies or contexts.
12- pub trait ParseOps < Format , Parser > : Sized {
14+ pub trait ParseOps < Format : InputFormat , Parser > : Sized {
1315 type Context ;
1416 type Error ;
1517
1618 fn parse ( from : Format , context : Self :: Context ) -> Result < Self , Self :: Error > ;
1719}
20+
21+ /// A marker trait for input formats.
22+ #[ diagnostic:: on_unimplemented(
23+ message = "the type `{Self}` is not a valid input format" ,
24+ label = "this type does not implement the `InputFormat` trait"
25+ ) ]
26+ pub trait InputFormat { }
27+
28+ /// An input structure for the parser.
29+ pub struct ParserInput < ' a > {
30+ /// The string content to be parsed.
31+ pub content : & ' a str ,
32+
33+ /// The name of the file from which the content was read.
34+ ///
35+ /// FIXME: This is specific to V1 and not used for accessing the content.
36+ pub file_name : PathBuf ,
37+ }
38+
39+ impl < ' a > ParserInput < ' a > {
40+ /// Assumes that the input is in the format of the inferred grammar version.
41+ pub fn assume_inferred < Version : GrammarVersion > ( self ) -> Versioned < Version , Self > {
42+ Versioned :: assume_inferred ( self )
43+ }
44+ }
45+
46+ /// A wrapper type for a value that is assumed to be in the specific format.
47+ pub struct Versioned < Version : GrammarVersion , T > ( pub T , PhantomData < Version > ) ;
48+ impl < Version : GrammarVersion , T > InputFormat for Versioned < Version , T > { }
49+
50+ impl < Version : GrammarVersion , T > Versioned < Version , T > {
51+ /// Creates a [`Versioned`] instance with the given value.
52+ ///
53+ /// This is useful when the type system already knows the version of the value.
54+ pub fn assume_inferred ( value : T ) -> Self {
55+ Self ( value, PhantomData )
56+ }
57+
58+ /// Creates a [`Versioned`] instance with an assumed version and the given value.
59+ ///
60+ /// This is useful when you want to explicitly specify the version at the point of parsing, not just at the type level.
61+ /// If the type system already knows the version, you can use [`Versioned::assume_inferred`].
62+ pub fn assume_version ( version : Version , value : T ) -> Self {
63+ let _ = version;
64+ Self ( value, PhantomData )
65+ }
66+ }
0 commit comments