Skip to main content

flux_syntax/
lib.rs

1#![feature(deref_patterns, rustc_private)]
2
3extern crate rustc_ast;
4extern crate rustc_data_structures;
5extern crate rustc_errors;
6extern crate rustc_span;
7
8pub mod lexer;
9mod parser;
10pub mod surface;
11pub mod symbols;
12mod token;
13use lexer::Cursor;
14use rustc_ast::tokenstream::TokenStream;
15use rustc_span::{
16    BytePos, Ident, Span, Symbol, SyntaxContext, def_id::LocalDefId, edition::Edition,
17};
18use surface::NodeId;
19
20use crate::parser::lookahead::Expected;
21
22#[derive(Default)]
23pub struct ParseSess {
24    next_node_id: usize,
25}
26
27impl ParseSess {
28    fn cx<'a>(&'a mut self, tokens: &'a TokenStream, span: Span) -> ParseCtxt<'a> {
29        ParseCtxt::new(self, tokens, span)
30    }
31
32    pub fn parse_refined_by(
33        &mut self,
34        tokens: &TokenStream,
35        span: Span,
36    ) -> ParseResult<surface::RefineParams> {
37        parser::parse_refined_by(&mut self.cx(tokens, span))
38    }
39
40    pub fn parse_type_alias(
41        &mut self,
42        tokens: &TokenStream,
43        span: Span,
44    ) -> ParseResult<surface::TyAlias> {
45        parser::parse_type_alias(&mut self.cx(tokens, span))
46    }
47
48    pub fn parse_fn_sig(
49        &mut self,
50        tokens: &TokenStream,
51        span: Span,
52    ) -> ParseResult<surface::FnSig> {
53        parser::parse_fn_sig(&mut self.cx(tokens, span), token::Eof)
54    }
55
56    pub fn parse_trait_assoc_reft(
57        &mut self,
58        tokens: &TokenStream,
59        span: Span,
60    ) -> ParseResult<Vec<surface::TraitAssocReft>> {
61        parser::parse_trait_assoc_refts(&mut self.cx(tokens, span))
62    }
63
64    pub fn parse_impl_assoc_reft(
65        &mut self,
66        tokens: &TokenStream,
67        span: Span,
68    ) -> ParseResult<Vec<surface::ImplAssocReft>> {
69        parser::parse_impl_assoc_refts(&mut self.cx(tokens, span))
70    }
71
72    pub fn parse_ident_list(
73        &mut self,
74        tokens: &TokenStream,
75        span: Span,
76    ) -> ParseResult<Vec<Ident>> {
77        parser::parse_ident_list(&mut self.cx(tokens, span))
78    }
79
80    pub fn parse_path_list(
81        &mut self,
82        tokens: &TokenStream,
83        span: Span,
84    ) -> ParseResult<Vec<surface::ExprPath>> {
85        parser::parse_path_list(&mut self.cx(tokens, span))
86    }
87
88    pub fn parse_flux_item(
89        &mut self,
90        tokens: &TokenStream,
91        span: Span,
92    ) -> ParseResult<Vec<surface::FluxItem>> {
93        parser::parse_flux_items(&mut self.cx(tokens, span))
94    }
95
96    pub fn parse_type(&mut self, tokens: &TokenStream, span: Span) -> ParseResult<surface::Ty> {
97        parser::parse_type(&mut self.cx(tokens, span))
98    }
99
100    pub fn parse_variant(
101        &mut self,
102        tokens: &TokenStream,
103        span: Span,
104    ) -> ParseResult<surface::VariantDef> {
105        parser::parse_variant(&mut self.cx(tokens, span), false)
106    }
107
108    pub fn parse_expr(&mut self, tokens: &TokenStream, span: Span) -> ParseResult<surface::Expr> {
109        parser::parse_expr(&mut self.cx(tokens, span), true)
110    }
111
112    pub fn parse_static_info(
113        &mut self,
114        tokens: &TokenStream,
115        span: Span,
116    ) -> ParseResult<surface::StaticInfo> {
117        let ty = parser::parse_type(&mut self.cx(tokens, span))?;
118        Ok(surface::StaticInfo { ty })
119    }
120
121    pub fn parse_constant_info(
122        &mut self,
123        tokens: &TokenStream,
124        span: Span,
125    ) -> ParseResult<surface::ConstantInfo> {
126        let expr = parser::parse_expr(&mut self.cx(tokens, span), true)?;
127        Ok(surface::ConstantInfo { expr: Some(expr) })
128    }
129
130    pub fn parse_yes_or_no_with_reason(
131        &mut self,
132        tokens: &TokenStream,
133        span: Span,
134    ) -> ParseResult<bool> {
135        parser::parse_yes_or_no_with_reason(&mut self.cx(tokens, span))
136    }
137
138    pub fn next_node_id(&mut self) -> NodeId {
139        let id = NodeId(self.next_node_id);
140        self.next_node_id += 1;
141        id
142    }
143
144    pub fn parse_detached_specs(
145        &mut self,
146        tokens: &TokenStream,
147        span: Span,
148    ) -> ParseResult<surface::DetachedSpecs> {
149        parser::parse_detached_specs(&mut self.cx(tokens, span))
150    }
151}
152
153struct ParseCtxt<'a> {
154    sess: &'a mut ParseSess,
155    ctx: SyntaxContext,
156    parent: Option<LocalDefId>,
157    edition: Edition,
158    tokens: Cursor<'a>,
159}
160
161impl<'a> ParseCtxt<'a> {
162    fn new(sess: &'a mut ParseSess, tokens: &'a TokenStream, span: Span) -> Self {
163        Self {
164            sess,
165            ctx: span.ctxt(),
166            parent: span.parent(),
167            edition: span.edition(),
168            tokens: Cursor::new(tokens, span.lo()),
169        }
170    }
171
172    fn next_node_id(&mut self) -> NodeId {
173        self.sess.next_node_id()
174    }
175
176    fn mk_span(&self, lo: BytePos, hi: BytePos) -> Span {
177        Span::new(lo, hi, self.ctx, self.parent)
178    }
179
180    fn lo(&self) -> BytePos {
181        self.tokens.lo()
182    }
183
184    fn hi(&self) -> BytePos {
185        self.tokens.hi()
186    }
187
188    fn is_reserved(&self, sym: Symbol) -> bool {
189        symbols::is_reserved(sym, self.edition)
190    }
191
192    fn unexpected_token(&mut self, expected: Vec<Expected>) -> ParseError {
193        let tok = self.tokens.at(0);
194        let kind = if tok.kind == token::Eof {
195            ParseErrorKind::UnexpectedEof
196        } else {
197            ParseErrorKind::UnexpectedToken { expected }
198        };
199        ParseError { kind, span: self.mk_span(tok.lo, tok.hi) }
200    }
201
202    fn cannot_be_chained(&self, lo: BytePos, hi: BytePos) -> ParseError {
203        ParseError { kind: ParseErrorKind::CannotBeChained, span: self.mk_span(lo, hi) }
204    }
205}
206
207pub type ParseResult<T = ()> = Result<T, ParseError>;
208
209pub struct ParseError {
210    pub kind: ParseErrorKind,
211    pub span: Span,
212}
213
214#[derive(Debug)]
215pub enum ParseErrorKind {
216    UnexpectedToken { expected: Vec<Expected> },
217    UnexpectedEof,
218    CannotBeChained,
219    InvalidBinding,
220    InvalidSort,
221    InvalidDetachedSpec,
222}