flux_syntax/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#![feature(rustc_private, box_patterns)]

extern crate rustc_ast;
extern crate rustc_span;

pub mod lexer;
pub mod surface;

use lalrpop_util::lalrpop_mod;
use lexer::{Cursor, Location, Token};
use rustc_ast::tokenstream::TokenStream;
use rustc_span::{def_id::LocalDefId, BytePos, Span, SyntaxContext};
use surface::NodeId;

lalrpop_mod!(
    #[allow(warnings)]
    #[allow(clippy::all)]
    grammar
);

#[derive(Default)]
pub struct ParseSess {
    next_node_id: usize,
}

macro_rules! parse {
    ($sess:expr, $parser:path, $tokens:expr, $span:expr) => {{
        let mut cx = ParseCtxt::new($sess, $span);
        <$parser>::new()
            .parse(&mut cx, Cursor::new($tokens, $span.lo()))
            .map_err(|err| cx.map_err(err))
    }};
}

impl ParseSess {
    pub fn parse_refined_by(
        &mut self,
        tokens: &TokenStream,
        span: Span,
    ) -> ParseResult<surface::RefineParams> {
        parse!(self, grammar::RefinedByParser, tokens, span)
    }

    pub fn parse_generics(
        &mut self,
        tokens: &TokenStream,
        span: Span,
    ) -> ParseResult<surface::Generics> {
        parse!(self, grammar::GenericsParser, tokens, span)
    }

    pub fn parse_type_alias(
        &mut self,
        tokens: &TokenStream,
        span: Span,
    ) -> ParseResult<surface::TyAlias> {
        parse!(self, grammar::TyAliasParser, tokens, span)
    }

    pub fn parse_fn_sig(
        &mut self,
        tokens: &TokenStream,
        span: Span,
    ) -> ParseResult<surface::FnSig> {
        parse!(self, grammar::FnSigParser, tokens, span)
    }

    pub fn parse_trait_assoc_reft(
        &mut self,
        tokens: &TokenStream,
        span: Span,
    ) -> ParseResult<surface::TraitAssocReft> {
        parse!(self, grammar::TraitAssocReftParser, tokens, span)
    }

    pub fn parse_impl_assoc_reft(
        &mut self,
        tokens: &TokenStream,
        span: Span,
    ) -> ParseResult<surface::ImplAssocReft> {
        parse!(self, grammar::ImplAssocReftParser, tokens, span)
    }

    pub fn parse_qual_names(
        &mut self,
        tokens: &TokenStream,
        span: Span,
    ) -> ParseResult<surface::QualNames> {
        parse!(self, grammar::QualNamesParser, tokens, span)
    }

    pub fn parse_flux_item(
        &mut self,
        tokens: &TokenStream,
        span: Span,
    ) -> ParseResult<Vec<surface::Item>> {
        parse!(self, grammar::ItemsParser, tokens, span)
    }

    pub fn parse_type(&mut self, tokens: &TokenStream, span: Span) -> ParseResult<surface::Ty> {
        parse!(self, grammar::TyParser, tokens, span)
    }

    pub fn parse_variant(
        &mut self,
        tokens: &TokenStream,
        span: Span,
    ) -> ParseResult<surface::VariantDef> {
        parse!(self, grammar::VariantParser, tokens, span)
    }

    pub fn parse_expr(&mut self, tokens: &TokenStream, span: Span) -> ParseResult<surface::Expr> {
        parse!(self, grammar::ExprParser, tokens, span)
    }

    pub fn next_node_id(&mut self) -> NodeId {
        let id = NodeId(self.next_node_id);
        self.next_node_id += 1;
        id
    }
}

struct ParseCtxt<'a> {
    offset: BytePos,
    ctx: SyntaxContext,
    parent: Option<LocalDefId>,
    sess: &'a mut ParseSess,
}

impl<'a> ParseCtxt<'a> {
    fn new(sess: &'a mut ParseSess, span: Span) -> Self {
        Self { sess, offset: span.lo(), ctx: span.ctxt(), parent: span.parent() }
    }

    fn next_node_id(&mut self) -> NodeId {
        self.sess.next_node_id()
    }

    fn map_span(&self, lo: Location, hi: Location) -> Span {
        Span::new(lo.0 + self.offset, hi.0 + self.offset, self.ctx, self.parent)
    }

    fn map_err(&self, err: LalrpopError) -> ParseError {
        match err {
            LalrpopError::InvalidToken { .. } => unreachable!(),
            LalrpopError::User { error: UserParseError::UnexpectedToken(lo, hi) } => {
                ParseErrorKind::UnexpectedToken.into_error(self.map_span(lo, hi))
            }
            LalrpopError::UnrecognizedEof { location, expected: _ } => {
                ParseErrorKind::UnexpectedEof.into_error(self.map_span(location, location))
            }
            LalrpopError::UnrecognizedToken { token: (start, _, end), expected: _ }
            | LalrpopError::ExtraToken { token: (start, _, end) } => {
                ParseErrorKind::UnexpectedToken.into_error(self.map_span(start, end))
            }
        }
    }
}

pub enum UserParseError {
    UnexpectedToken(Location, Location),
}

type LalrpopError = lalrpop_util::ParseError<Location, Token, UserParseError>;

pub type ParseResult<T> = Result<T, ParseError>;

pub struct ParseError {
    pub kind: ParseErrorKind,
    pub span: Span,
}

#[derive(Debug)]
pub enum ParseErrorKind {
    UnexpectedEof,
    UnexpectedToken,
    IntTooLarge,
}

impl ParseErrorKind {
    fn into_error(self, span: Span) -> ParseError {
        ParseError { kind: self, span }
    }
}