pliron 0.15.0

Programming Languages Intermediate RepresentatiON
Documentation
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
//! Utilities for parsing.

use std::{num::ParseIntError, str::FromStr};

use crate::{
    arg_err,
    attribute::AttrObj,
    basic_block::BasicBlock,
    context::Ptr,
    debug_info::set_operation_result_name,
    identifier::Identifier,
    location::{Located, Location},
    operation::Operation,
    parsable::{IntoParseResult, Parsable, ParseResult, StateStream},
    result::Result,
    r#type::TypeObj,
    value::Value,
};
use combine::{
    Parser, Stream, any, between, many, many1, none_of,
    parser::char::{digit, spaces},
    sep_by, token,
};

/// Parse from `parser`, ignoring whitespace(s) before and after.
/// > **Warning**: Do not use this inside inside [combine::optional] or
/// >   inside repeating combiners, such as [combine::many].
/// >   After successfully parsing one instance, if spaces are consumed to parse
/// >   the next one, but the next one doesn't exist, it is treated as a failure
/// >   that consumed some input. This messes things up. So spaces must be consumed
/// >   after a successfull parse, and not prior to an upcoming one.
/// >   A possibly right way to, for example, parse a comma separated list of [Identifier]s:
///
///```
///     # use combine::{parser::char::spaces, Parser};
///     # use pliron::parsable::Parsable;
///     let ids = spaces().with
///               (combine::sep_by::<Vec<_>, _, _, _>
///                 (pliron::identifier::Identifier::parser(()).skip(spaces()),
///                 combine::token(',').skip(spaces())));
///```
/// >    Similarly, if you want to use this inside [combine::optional], you should
/// >    use [combine::attempt] to ensure that no input is consumed if the parser fails.
pub fn spaced<Input: Stream<Token = char>, Output>(
    parser: impl Parser<Input, Output = Output>,
) -> impl Parser<Input, Output = Output> {
    combine::between(spaces(), spaces(), parser)
}

/// A parser that returns the current [Location] and does nothing else
pub fn location<'a>() -> Box<dyn Parser<StateStream<'a>, Output = Location, PartialState = ()> + 'a>
{
    combine::parser(|parsable_state: &mut StateStream<'a>| {
        combine::ParseResult::PeekOk(parsable_state.loc()).into()
    })
    .boxed()
}

/// A parser combinator to parse [TypeId](crate::type::TypeId) followed by the type's contents.
pub fn type_parser<'a>()
-> Box<dyn Parser<StateStream<'a>, Output = Ptr<TypeObj>, PartialState = ()> + 'a> {
    Ptr::<TypeObj>::parser(())
}

/// A parser to parse any Rust integer type.
pub fn int_parse<'a, IntT>(state_stream: &mut StateStream<'a>, _arg: ()) -> ParseResult<'a, IntT>
where
    IntT: FromStr,
    IntT::Err: std::error::Error + Send + Sync + 'static,
{
    many1::<String, _, _>(digit())
        .and_then(|digits| digits.parse::<IntT>())
        .parse_stream(state_stream)
        .into()
}

/// Get a parser combinator that can parse any Rust integer type.
pub fn int_parser<'a, IntT>()
-> Box<dyn Parser<StateStream<'a>, Output = IntT, PartialState = ()> + 'a>
where
    IntT: FromStr,
    IntT::Err: std::error::Error + Send + Sync + 'static,
{
    combine::parser(move |parsable_state: &mut StateStream<'a>| int_parse(parsable_state, ()))
        .boxed()
}

/// A trait for parsing an integer from a string with a given radix.
pub trait FromStrRadix: Sized {
    fn from_str_radix(src: &str, radix: u32) -> std::result::Result<Self, ParseIntError>;
}

macro_rules! impl_from_str_radix_for_int {
    ($($ty:ty),*) => {
        $(
            impl FromStrRadix for $ty {
                fn from_str_radix(src: &str, radix: u32) -> std::result::Result<Self, ParseIntError> {
                    <$ty>::from_str_radix(src, radix)
                }
            }
        )*
    };
}
impl_from_str_radix_for_int!(u8, u16, u32, u64, usize, i8, i16, i32, i64, isize);

/// Parser to parse a hexadecimal integer, which is a sequence of hexadecimal digits prefixed with `0x`.
pub fn hex_int_parse<'a, IntT>(
    state_stream: &mut StateStream<'a>,
    _arg: (),
) -> ParseResult<'a, IntT>
where
    IntT: FromStrRadix,
{
    combine::parser::char::string("0x")
        .with(many1::<String, _, _>(combine::parser::char::hex_digit()))
        .and_then(|digits| IntT::from_str_radix(&digits, 16))
        .parse_stream(state_stream)
        .into()
}

/// Get a parser combinator to parse a hexadecimal integer, which is a sequence of hexadecimal digits prefixed with `0x`.
pub fn hex_int_parser<'a, IntT>()
-> Box<dyn Parser<StateStream<'a>, Output = IntT, PartialState = ()> + 'a>
where
    IntT: FromStrRadix,
{
    combine::parser(move |parsable_state: &mut StateStream<'a>| hex_int_parse(parsable_state, ()))
        .boxed()
}

/// Parse a quoted string, which is a double-quoted string that may contain escaped characters.
pub fn quoted_string_parse<'a>(
    state_stream: &mut StateStream<'a>,
    _arg: (),
) -> ParseResult<'a, String> {
    // An escaped charater is one that is preceded by a backslash.
    let escaped_char = combine::parser(move |parsable_state: &mut StateStream<'a>| {
        // This combine::parser() is so that we can get a location before the parsing begins.
        let loc = parsable_state.loc();
        let mut escaped_char = token('\\').with(any()).then(move |c: char| {
            let loc = loc.clone();
            // This combine::parser() is so that we can return an error of the right type.
            // I can't get the right error type with `and_then`
            combine::parser(move |_parsable_state: &mut StateStream<'a>| {
                // Filter out the escaped characters that we handle.
                let result = match c {
                    '\\' => Ok('\\'),
                    '\"' => Ok('\"'),
                    _ => arg_err!(loc.clone(), "Unexpected escaped character \\{}", c),
                };
                result.into_parse_result()
            })
        });
        escaped_char.parse_stream(parsable_state).into()
    });

    // We want to scan a double quote deliminted string with possibly escaped characters in between.
    let quoted_string = between(
        token('"'),
        token('"'),
        many(escaped_char.or(none_of("\"".chars()))),
    );

    quoted_string
        .map(|chars: Vec<char>| {
            // Convert the characters to a string.
            chars.into_iter().collect::<String>()
        })
        .parse_stream(state_stream)
        .into()
}

/// A parser combinator to parse a quoted string, which is a double-quoted string that may contain escaped characters.
pub fn quoted_string_parser<'a>()
-> Box<dyn Parser<StateStream<'a>, Output = String, PartialState = ()> + 'a> {
    combine::parser(move |parsable_state: &mut StateStream<'a>| {
        quoted_string_parse(parsable_state, ())
    })
    .boxed()
}

/// A parser combinator to parse [AttrId](crate::attribute::AttrId) followed by the attribute's contents.
pub fn attr_parser<'a>()
-> Box<dyn Parser<StateStream<'a>, Output = AttrObj, PartialState = ()> + 'a> {
    AttrObj::parser(())
}

/// Parse a delimitted list of objects.
pub fn delimited_list_parser<Input: Stream<Token = char>, Output>(
    open: char,
    close: char,
    sep: char,
    parser: impl Parser<Input, Output = Output>,
) -> impl Parser<Input, Output = Vec<Output>> {
    between(
        token(open).skip(spaces()),
        spaces().with(token(close)),
        list_parser(sep, parser),
    )
}

/// Parse a list of objects.
pub fn list_parser<Input: Stream<Token = char>, Output>(
    sep: char,
    parser: impl Parser<Input, Output = Output>,
) -> impl Parser<Input, Output = Vec<Output>> {
    sep_by::<Vec<_>, _, _, _>(parser.skip(spaces()), token(sep).skip(spaces()))
}

/// Parse zero-or-more occurrences (ignoring spaces) of `parser`.
pub fn zero_or_more_parser<Input: Stream<Token = char>, Output>(
    parser: impl Parser<Input, Output = Output>,
) -> impl Parser<Input, Output = Vec<Output>> {
    many::<Vec<_>, _, _>(spaces().with(parser.skip(spaces())))
}

/// Parse an identifier into an SSA [Value]. Typically called to parse
/// the SSA operands of an [Operation]. If the SSA value hasn't been defined yet,
/// a [forward reference](crate::builtin::ops::ForwardRefOp) is returned.
pub fn ssa_opd_parse<'a>(state_stream: &mut StateStream<'a>, _arg: ()) -> ParseResult<'a, Value> {
    Identifier::parser(())
        .parse_stream(state_stream)
        .map(|opd| {
            state_stream
                .state
                .name_tracker
                .ssa_use(state_stream.state.ctx, &opd)
        })
        .into()
}

/// A parser to parse an identifier into an SSA [Value]. Typically called to parse
/// the SSA operands of an [Operation]. If the SSA value hasn't been defined yet,
/// a [forward reference](crate::builtin::ops::ForwardRefOp) is returned.
pub fn ssa_opd_parser<'a>()
-> Box<dyn Parser<StateStream<'a>, Output = Value, PartialState = ()> + 'a> {
    combine::parser(move |parsable_state: &mut StateStream<'a>| ssa_opd_parse(parsable_state, ()))
        .boxed()
}

/// Parse a block label into a [`Ptr<BasicBlock>`]. Typically called to parse
/// the block operands of an [Operation]. If the block doesn't exist, it's created,
pub fn block_opd_parse<'a>(
    state_stream: &mut StateStream<'a>,
    _arg: (),
) -> ParseResult<'a, Ptr<BasicBlock>> {
    token('^')
        .with(Identifier::parser(()))
        .parse_stream(state_stream)
        .map(|opd| {
            state_stream
                .state
                .name_tracker
                .block_use(state_stream.state.ctx, &opd)
        })
        .into()
}

/// A parser to parse a block label into a [`Ptr<BasicBlock>`]. Typically called to parse
/// the block operands of an [Operation]. If the block doesn't exist, it's created,
pub fn block_opd_parser<'a>()
-> Box<dyn Parser<StateStream<'a>, Output = Ptr<BasicBlock>, PartialState = ()> + 'a> {
    combine::parser(move |parsable_state: &mut StateStream<'a>| block_opd_parse(parsable_state, ()))
        .boxed()
}

/// After an [Operation] is fully parsed, for each result,
/// set its name and register it as an SSA definition.
pub fn process_parsed_ssa_defs(
    state_stream: &mut StateStream,
    results: &[(Identifier, Location)],
    op: Ptr<Operation>,
) -> Result<()> {
    let ctx = &mut state_stream.state.ctx;
    assert!(
        results.len() == op.deref(ctx).get_num_results(),
        "Error processing parsed SSA definitions. Result count mismatch"
    );

    let name_tracker = &mut state_stream.state.name_tracker;
    for (idx, name_loc) in results.iter().enumerate() {
        let res = op.deref(ctx).get_result(idx);
        name_tracker.ssa_def(ctx, name_loc, res)?;
        set_operation_result_name(ctx, op, idx, Some(name_loc.0.clone()));
    }
    Ok(())
}

#[cfg(test)]
mod test {
    use super::*;

    use expect_test::expect;

    use crate::{
        context::Context,
        location,
        parsable::{self, state_stream_from_iterator},
        printable::Printable,
    };

    #[test]
    fn test_parse_type() {
        let mut ctx = Context::new();

        let state_stream = state_stream_from_iterator(
            "builtin.some".chars(),
            parsable::State::new(&mut ctx, location::Source::InMemory),
        );

        let res = type_parser().parse(state_stream);
        let err_msg = format!("{}", res.err().unwrap());

        let expected_err_msg = expect![[r#"
            Parse error at line: 1, column: 1
            Unregistered type builtin.some
        "#]];
        expected_err_msg.assert_eq(&err_msg);

        let state_stream = state_stream_from_iterator(
            "builtin.integer a".chars(),
            parsable::State::new(&mut ctx, location::Source::InMemory),
        );

        let res = type_parser().parse(state_stream);
        let err_msg = format!("{}", res.err().unwrap());

        let expected_err_msg = expect![[r#"
            Parse error at line: 1, column: 17
            Unexpected `a`
            Expected whitespaces, si, ui or i
        "#]];
        expected_err_msg.assert_eq(&err_msg);

        let state_stream = state_stream_from_iterator(
            "builtin.integer si32".chars(),
            parsable::State::new(&mut ctx, location::Source::InMemory),
        );

        let parsed = type_parser().parse(state_stream).unwrap().0;
        assert_eq!(parsed.disp(&ctx).to_string(), "builtin.integer si32");
    }

    #[test]
    fn test_hex_int_parser() {
        use crate::{
            context::Context,
            location,
            parsable::{self, state_stream_from_iterator},
        };

        let mut ctx = Context::new();

        // Valid hex integer
        let state_stream = state_stream_from_iterator(
            "0xff".chars(),
            parsable::State::new(&mut ctx, location::Source::InMemory),
        );
        let parsed: u64 = hex_int_parser().parse(state_stream).unwrap().0;
        assert_eq!(parsed, 0xff);

        // Valid hex integer with uppercase digits
        let state_stream = state_stream_from_iterator(
            "0xDEAD".chars(),
            parsable::State::new(&mut ctx, location::Source::InMemory),
        );
        let parsed: u64 = hex_int_parser().parse(state_stream).unwrap().0;
        assert_eq!(parsed, 0xDEAD);

        // u32 type
        let state_stream = state_stream_from_iterator(
            "0xCAFE".chars(),
            parsable::State::new(&mut ctx, location::Source::InMemory),
        );
        let parsed: u32 = hex_int_parser().parse(state_stream).unwrap().0;
        assert_eq!(parsed, 0xCAFEu32);

        // u8 type
        let state_stream = state_stream_from_iterator(
            "0x7f".chars(),
            parsable::State::new(&mut ctx, location::Source::InMemory),
        );
        let parsed: u8 = hex_int_parser().parse(state_stream).unwrap().0;
        assert_eq!(parsed, 0x7fu8);

        // i64 type
        let state_stream = state_stream_from_iterator(
            "0x1234".chars(),
            parsable::State::new(&mut ctx, location::Source::InMemory),
        );
        let parsed: i64 = hex_int_parser().parse(state_stream).unwrap().0;
        assert_eq!(parsed, 0x1234i64);

        // usize type
        let state_stream = state_stream_from_iterator(
            "0xABCDEF".chars(),
            parsable::State::new(&mut ctx, location::Source::InMemory),
        );
        let parsed: usize = hex_int_parser().parse(state_stream).unwrap().0;
        assert_eq!(parsed, 0xABCDEFusize);

        // Value too large for u8 (0x100 = 256) should fail
        {
            let state_stream = state_stream_from_iterator(
                "0x100".chars(),
                parsable::State::new(&mut ctx, location::Source::InMemory),
            );
            let res = hex_int_parser::<u8>().parse(state_stream);
            assert!(res.is_err());
        }

        // Value too large for u16 (0x10000 = 65536) should fail
        {
            let state_stream = state_stream_from_iterator(
                "0x10000".chars(),
                parsable::State::new(&mut ctx, location::Source::InMemory),
            );
            let res = hex_int_parser::<u16>().parse(state_stream);
            assert!(res.is_err());
        }

        // Missing 0x prefix should fail
        {
            let state_stream = state_stream_from_iterator(
                "ff".chars(),
                parsable::State::new(&mut ctx, location::Source::InMemory),
            );
            let res = hex_int_parser::<u64>().parse(state_stream);
            assert!(res.is_err());
        }

        // No digits after 0x should fail
        {
            let state_stream = state_stream_from_iterator(
                "0x".chars(),
                parsable::State::new(&mut ctx, location::Source::InMemory),
            );
            let res = hex_int_parser::<u64>().parse(state_stream);
            assert!(res.is_err());
        }
    }
}