|
7 | 7 | //! # Usage
|
8 | 8 | //!
|
9 | 9 | //! ```
|
| 10 | +//! use std::fmt; |
| 11 | +//! use std::fmt::Formatter; |
| 12 | +//! use itertools::Itertools; |
| 13 | +//! use partiql_common::syntax::location::LineAndColumn; |
10 | 14 | //! use partiql_parser::{Parser, ParserError, ParserResult};
|
11 | 15 | //!
|
12 | 16 | //! let parser = Parser::default();
|
|
15 | 19 | //!
|
16 | 20 | //! let errs: ParserError = parser.parse("SELECT").expect_err("expected error");
|
17 | 21 | //!
|
| 22 | +//! // Print out messages with byte offsets |
18 | 23 | //! let errs_at: ParserError =
|
19 | 24 | //! parser.parse("SELECT * FROM a AY a CROSS JOIN c AS c AT q").unwrap_err();
|
20 | 25 | //! assert_eq!(errs_at.errors[0].to_string(), "Unexpected token `<a:UNQUOTED_IDENT>` at `(b19..b20)`");
|
| 26 | +//! |
| 27 | +//! // Print out messages with line:column offsets |
| 28 | +//! let errs_at_nice: ParserError = |
| 29 | +//! parser.parse("SELECT * FROM a AY a CROSS JOIN c AS c AT q").unwrap_err(); |
| 30 | +//! let offsets = &errs_at_nice.offsets; |
| 31 | +//! let source = &errs_at_nice.text; |
| 32 | +//! let err_msg = errs_at_nice.errors.iter().map(|e| |
| 33 | +//! e.clone().map_loc(|loc| LineAndColumn::from(offsets.at(source, loc).unwrap()).to_string())).join("\n"); |
| 34 | +//! assert_eq!(err_msg, "Unexpected token `<a:UNQUOTED_IDENT>` at `(1:20..1:21)`"); |
| 35 | +//! |
| 36 | +//! |
| 37 | +//! |
| 38 | +//! // Print out messages with custom line:column offsets |
| 39 | +//! #[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash)] |
| 40 | +//! pub struct VerboseLineAndColumn(LineAndColumn); |
| 41 | +//! |
| 42 | +//! impl fmt::Display for VerboseLineAndColumn { |
| 43 | +//! fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { |
| 44 | +//! write!(f, "Line {}, Offset {}", self.0.line, self.0.column) |
| 45 | +//! } |
| 46 | +//! } |
| 47 | +//! |
| 48 | +//! let err_msg = errs_at_nice.errors.iter().map(|e| |
| 49 | +//! e.clone().map_loc(|loc| VerboseLineAndColumn(LineAndColumn::from(offsets.at(source, loc).unwrap())).to_string())).join("\n"); |
| 50 | +//! assert_eq!(err_msg, "Unexpected token `<a:UNQUOTED_IDENT>` at `(Line 1, Offset 20..Line 1, Offset 21)`"); |
21 | 51 | //! ```
|
22 | 52 | //!
|
23 | 53 | //! [partiql]: https://partiql.org
|
|
0 commit comments