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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
//! Contain the error types ragarding the different tasks that QasmSim can
//! perform.

#[macro_use]
pub(crate) mod humanize;

use std::convert;
use std::error;
use std::fmt;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use self::humanize::humanize_error;
use crate::grammar::lexer::{self, Location, Tok};
pub use crate::interpreter::runtime::RuntimeError;
pub use crate::linker::LinkerError;
use crate::semantics::QasmType;
pub use crate::semantics::SemanticError;

/// Represent a parsing error.
pub type ParseError = lalrpop_util::ParseError<Location, lexer::Tok, lexer::LexicalError<Location>>;

/// An alias for a pair relating some source code with an error.
pub type SrcAndErr<'src, E> = (&'src str, E);

/// Types of errors in QasmSim. QasmSim errors contain information about
/// the error and the location in the source code where the error happens.
///
/// `QasmSimError` instances can be printed. They refer to the source code and
/// try to provide contextual information for fixing the problem.
///
/// Conversion between [`ParseError`], [`RuntimeError`] and [`LinkerError`] is
/// possible thanks to the trait `From` is defined for the pair
/// `(&'source str, T)` (see alias [`SrcAndErr`]) for all the errors listed
/// above.
///
/// # Examples
///
/// The error type of [`simulate()`] is [`RuntimeError`].
/// `RuntimeError` is a _sourceless_ error in the sense it does not relate with
/// the concrete source code beyond the location in the AST at which the error
/// happens.
///
/// You can use [`map_err`] for for capturing the error and converting it
/// into a `QasmSimError` from its pairing with the source.
///
/// ```
/// use qasmsim::{QasmSimError, parse_and_link, simulate};
///
/// let source = r#"
/// OPENQASM 2.0;
/// qreg q[2];
/// CX q[1], q[2]; // Notice we are indexing out of bounds here.
/// "#;
/// let program = parse_and_link(source)?;
/// let runtime_error = simulate(&program).expect_err("Index out of bounds");
/// let qasmsim_error = QasmSimError::from((source, runtime_error));
/// # Ok::<(), QasmSimError>(())
/// ```
///
/// [`ParseError`]: ./type.ParseError.html
/// [`RuntimeError`]: ./enum.RuntimeError.html
/// [`LinkerError`]: ../linker/enum.LinkerError.html
/// [`SrcAndErr`]: ./type.SrcAndErr.html
/// [`simulate()`]: ../fn.simulate.html
/// [`map_err`]: ../../std/result/enum.Result.html#method.map_err
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum QasmSimError<'src> {
    /// A generic unknown error.
    UnknownError(String),
    /// Found an invalid token at some position.
    InvalidToken {
        /// Line source.
        source: &'src str,
        /// Line number.
        lineno: usize,
        /// Position inside the line (0-based) where the invalid token starts.
        startpos: usize,
        /// Position inside the line (0-based) where the invalid token ends.
        endpos: Option<usize>,
        /// Token found.
        token: Option<Tok>,
        /// A list of expected tokens.
        expected: Vec<String>,
    },
    /// Found an unexpected end of file.
    UnexpectedEOF {
        /// Line source.
        source: &'src str,
        /// Line number.
        lineno: usize,
        /// Position inside the line (0-based) where the invalid token starts.
        startpos: usize,
        /// Position inside the line (0-based) where the invalid token ends.
        endpos: Option<usize>,
        /// Token found.
        token: Option<Tok>,
        /// A list of expected tokens.
        expected: Vec<String>,
    },
    /// Found an unexpected token.
    UnexpectedToken {
        /// Line source.
        source: &'src str,
        /// Line number.
        lineno: usize,
        /// Position inside the line (0-based) where the invalid token starts.
        startpos: usize,
        /// Position inside the line (0-based) where the invalid token ends.
        endpos: Option<usize>,
        /// Token found.
        token: Option<Tok>,
        /// A list of expected tokens.
        expected: Vec<String>,
    },
    /// Found a redefinition of a register.
    RedefinitionError {
        /// Line source.
        source: &'src str,
        /// Name of the register declared for second time.
        symbol_name: String,
        /// Line number.
        lineno: usize,
        /// Line number where the register was originally declared.
        previous_lineno: usize,
    },
    /// The unability of linking a library.
    LibraryNotFound {
        /// Line source.
        source: &'src str,
        /// Path to the library to be included.
        libpath: String,
        /// Line number.
        lineno: usize,
    },
    /// Use of register index that does not fit the register size.
    IndexOutOfBounds {
        /// Line source.
        source: &'src str,
        /// Line number.
        lineno: usize,
        /// Name of the register being indexed.
        symbol_name: String,
        /// Index tried to access.
        index: usize,
        /// Size of the register.
        size: usize,
    },
    /// Use of an unknown/undeclared symbol.
    SymbolNotFound {
        /// Line source.
        source: &'src str,
        /// Line number.
        lineno: usize,
        /// Name of the unknown symbol.
        symbol_name: String,
        /// The expected type.
        expected: QasmType,
    },
    /// The attempt of applying an operation passing the wrong number of
    /// parameters.
    WrongNumberOfParameters {
        /// Line source.
        source: &'src str,
        /// Line number.
        lineno: usize,
        /// Name of the operation.
        symbol_name: String,
        /// Indicate if the parameters are registers or real values.
        are_registers: bool,
        /// The number of passed parameters.
        given: usize,
        /// The number of expected parameters.
        expected: usize,
    },
    /// Use of a gate not previously defined.
    UndefinedGate {
        /// Line source.
        source: &'src str,
        /// Line number.
        lineno: usize,
        /// Name of the unknown gate.
        symbol_name: String,
    },
    /// Found an unexpected type of value.
    TypeMismatch {
        /// Line source.
        source: &'src str,
        /// Line number.
        lineno: usize,
        /// Name of the symbol with the incorrect type.
        symbol_name: String,
        /// Expected type.
        expected: QasmType,
    },
    /// Attempt of applying an operation to different sizes registers.
    RegisterSizeMismatch {
        /// Line source.
        source: &'src str,
        /// Line number.
        lineno: usize,
        /// Name of the operation.
        symbol_name: String,
        /// Sizes of the different registers involved.
        sizes: Vec<usize>,
    },
}

impl fmt::Display for QasmSimError<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut buffer = String::new();
        humanize_error(&mut buffer, self)?;
        write!(f, "{}", buffer)
    }
}

impl error::Error for QasmSimError<'_> {}

impl convert::From<String> for QasmSimError<'_> {
    fn from(err: String) -> Self {
        QasmSimError::UnknownError(err)
    }
}

impl<'src> From<SrcAndErr<'src, ParseError>> for QasmSimError<'src> {
    fn from(src_and_err: SrcAndErr<'src, ParseError>) -> Self {
        let (input, error) = src_and_err;
        match error {
            ParseError::InvalidToken { location } => {
                let (source, lineno, startpos, endpos) = extract_line(location.0, None, input);
                QasmSimError::InvalidToken {
                    source,
                    lineno,
                    startpos,
                    endpos,
                    token: None,
                    expected: Vec::new(),
                }
            }
            ParseError::UnrecognizedEOF { location, expected } => {
                let (source, lineno, startpos, endpos) = extract_line(location.0, None, input);
                QasmSimError::UnexpectedEOF {
                    source,
                    lineno,
                    startpos,
                    endpos,
                    token: None,
                    expected,
                }
            }
            ParseError::UnrecognizedToken { token, expected } => {
                let location = token.0;
                let endlocation = token.2;
                let (source, lineno, startpos, endpos) =
                    extract_line(location.0, Some(endlocation.0), input);
                QasmSimError::UnexpectedToken {
                    source,
                    lineno,
                    startpos,
                    endpos,
                    token: Some(token.1),
                    expected,
                }
            }
            ParseError::ExtraToken { token } => {
                let location = token.0;
                let endlocation = token.2;
                let (source, lineno, startpos, endpos) =
                    extract_line(location.0, Some(endlocation.0), input);
                QasmSimError::UnexpectedToken {
                    source,
                    lineno,
                    startpos,
                    endpos,
                    token: Some(token.1),
                    expected: Vec::new(),
                }
            }
            ParseError::User { error: lexer_error } => {
                let location = lexer_error.location;
                let (source, lineno, startpos, endpos) = extract_line(location.0, None, input);
                QasmSimError::InvalidToken {
                    // XXX: Actually, this should be "InvalidInput"
                    source,
                    lineno,
                    startpos,
                    endpos,
                    token: None,
                    expected: Vec::new(),
                }
            }
        }
    }
}

impl<'src> From<SrcAndErr<'src, RuntimeError>> for QasmSimError<'src> {
    fn from(source_and_error: SrcAndErr<'src, RuntimeError>) -> Self {
        let (input, error) = source_and_error;
        match error {
            RuntimeError::Other => QasmSimError::UnknownError(format!("{:?}", error)),
            RuntimeError::RegisterSizeMismatch {
                location,
                symbol_name,
                sizes,
            } => {
                let (source, lineno, _, _) = extract_line(location.0, None, input);
                QasmSimError::RegisterSizeMismatch {
                    source,
                    lineno,
                    symbol_name,
                    sizes,
                }
            }
            RuntimeError::TypeMismatch {
                location,
                symbol_name,
                expected,
            } => {
                let (source, lineno, _, _) = extract_line(location.0, None, input);
                QasmSimError::TypeMismatch {
                    source,
                    lineno,
                    symbol_name,
                    expected,
                }
            }
            RuntimeError::UndefinedGate {
                location,
                symbol_name,
            } => {
                let (source, lineno, _, _) = extract_line(location.0, None, input);
                QasmSimError::UndefinedGate {
                    source,
                    lineno,
                    symbol_name,
                }
            }
            RuntimeError::WrongNumberOfParameters {
                are_registers,
                location,
                symbol_name,
                given,
                expected,
            } => {
                let (source, lineno, _, _) = extract_line(location.0, None, input);
                QasmSimError::WrongNumberOfParameters {
                    are_registers,
                    source,
                    symbol_name,
                    lineno,
                    expected,
                    given,
                }
            }
            RuntimeError::SymbolNotFound {
                location,
                symbol_name,
                expected,
            } => {
                let (source, lineno, _, _) = extract_line(location.0, None, input);
                QasmSimError::SymbolNotFound {
                    source,
                    symbol_name,
                    lineno,
                    expected,
                }
            }
            RuntimeError::IndexOutOfBounds {
                location,
                symbol_name,
                index,
                size,
            } => {
                let (source, lineno, _, _) = extract_line(location.0, None, input);
                QasmSimError::IndexOutOfBounds {
                    source,
                    symbol_name,
                    lineno,
                    size,
                    index,
                }
            }
            RuntimeError::SemanticError(semantic_error) => match semantic_error {
                SemanticError::RedefinitionError {
                    symbol_name,
                    location,
                    previous_location,
                } => {
                    let (source, lineno, _, _) = extract_line(location.0, None, input);
                    let (_, previous_lineno, _, _) = extract_line(previous_location.0, None, input);
                    QasmSimError::RedefinitionError {
                        source,
                        symbol_name,
                        lineno,
                        previous_lineno,
                    }
                }
            },
        }
    }
}

impl<'src> From<SrcAndErr<'src, LinkerError>> for QasmSimError<'src> {
    fn from(source_and_error: SrcAndErr<'src, LinkerError>) -> Self {
        let (input, error) = source_and_error;
        match error {
            LinkerError::LibraryNotFound { location, libpath } => {
                let (source, lineno, _, _) = extract_line(location.0, None, input);
                QasmSimError::LibraryNotFound {
                    source,
                    libpath,
                    lineno,
                }
            }
        }
    }
}

fn extract_line(
    offset: usize,
    endoffset: Option<usize>,
    doc: &str,
) -> (&str, usize, usize, Option<usize>) {
    assert!(
        offset <= doc.len(),
        "linestart={} must in the range 0..=doc.len()={}",
        offset,
        doc.len()
    );

    let mut linecount = 1;
    let mut start = 0;
    let mut end = start + 1;
    for (idx, character) in doc.chars().enumerate() {
        if idx < offset && character == '\n' {
            start = idx + 1;
            linecount += 1;
        }
        if idx >= offset {
            end = idx + 1;
            if character == '\n' {
                break;
            }
        }
    }

    if end < start {
        end = doc.len();
    }

    let startpos = offset - start;
    let endpos = endoffset.map(|endoffset| endoffset - start);

    (&doc[start..end], linecount, startpos, endpos)
}

#[cfg(test)]
mod test_into_doc_coords {
    use indoc::indoc;

    use super::extract_line;

    macro_rules! test_get_line_src {
    ($source:expr, $( $name:ident: $offset:expr, $offsetend:expr => $expected:expr ),*) => {
      $(
        #[test]
        fn $name() {
          assert_eq!(extract_line($offset, $offsetend, &$source), $expected);
        }
      )*
    };
  }

    test_get_line_src!(indoc!("
      line 1
      line 2
      line 3"
      ),
      test_beginning_of_source: 0, None => ("line 1\n", 1, 0, None),
      test_middle_of_source: 7, None => ("line 2\n", 2, 0, None),
      test_last_character: 20, None => ("line 3", 3, 6, None)
    );
}