Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitef1d543

Browse files
committed
Pass more information in user defined parse error
1 parent22c0b08 commitef1d543

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

‎compiler/src/error.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ impl fmt::Display for CompileError {
5656
}?;
5757

5858
// Print line number:
59-
write!(f," at line {:?}",self.location.row())
59+
match&self.error{
60+
CompileErrorType::Parse(..) =>Ok(()),
61+
_ =>write!(f," at line {:?}",self.location.row()),
62+
}
6063
}
6164
}
6265

‎parser/src/error.rs‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub enum ParseError {
2424
/// Parser encountered an unexpected token
2525
UnrecognizedToken(TokSpan,Vec<String>),
2626
/// Maps to `User` type from `lalrpop-util`
27-
Other,
27+
Other(LexicalError),
2828
}
2929

3030
/// Convert `lalrpop_util::ParseError` to our internal type
@@ -34,8 +34,7 @@ impl From<InnerError<Location, Tok, LexicalError>> for ParseError {
3434
// TODO: Are there cases where this isn't an EOF?
3535
InnerError::InvalidToken{ location} =>ParseError::EOF(Some(location)),
3636
InnerError::ExtraToken{ token} =>ParseError::ExtraToken(token),
37-
// Inner field is a unit-like enum `LexicalError::StringError` with no useful info
38-
InnerError::User{ ..} =>ParseError::Other,
37+
InnerError::User{ error} =>ParseError::Other(error),
3938
InnerError::UnrecognizedToken{ token, expected} =>{
4039
match token{
4140
Some(tok) =>ParseError::UnrecognizedToken(tok, expected),
@@ -66,8 +65,7 @@ impl fmt::Display for ParseError {
6665
ParseError::UnrecognizedToken(ref t_span, _) =>{
6766
write!(f,"Got unexpected token: {:?} at {:?}", t_span.1, t_span.0)
6867
}
69-
// This is user defined, it probably means a more useful error should have been given upstream.
70-
ParseError::Other =>write!(f,"Got unsupported token(s)"),
68+
ParseError::Other(ref error) =>write!(f,"{}", error),
7169
}
7270
}
7371
}

‎parser/src/lexer.rs‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use num_traits::Num;
1010
use serde::{Deserialize,Serialize};
1111
use std::cmp::Ordering;
1212
use std::collections::HashMap;
13+
use std::fmt;
1314
use std::str::FromStr;
1415
use unic_emoji_char::is_emoji_presentation;
1516
use unicode_xid::UnicodeXID;
@@ -59,13 +60,13 @@ pub struct Lexer<T: Iterator<Item = char>> {
5960
keywords:HashMap<String,Tok>,
6061
}
6162

62-
#[derive(Debug)]
63+
#[derive(Debug,PartialEq)]
6364
pubstructLexicalError{
6465
puberror:LexicalErrorType,
6566
publocation:Location,
6667
}
6768

68-
#[derive(Debug)]
69+
#[derive(Debug,PartialEq)]
6970
pubenumLexicalErrorType{
7071
StringError,
7172
UnicodeError,
@@ -74,6 +75,26 @@ pub enum LexicalErrorType {
7475
OtherError(String),
7576
}
7677

78+
impl fmt::DisplayforLexicalError{
79+
fnfmt(&self,f:&mut fmt::Formatter) -> fmt::Result{
80+
matchself.error{
81+
LexicalErrorType::StringError =>write!(f,"Got unexpected string"),
82+
LexicalErrorType::UnicodeError =>write!(f,"Got unexpected unicode"),
83+
LexicalErrorType::NestingError =>write!(f,"Got unexpected nesting"),
84+
LexicalErrorType::UnrecognizedToken{ tok} =>{
85+
write!(f,"Got unexpected token {}", tok)
86+
}
87+
LexicalErrorType::OtherError(ref msg) =>write!(f,"{}", msg),
88+
}?;
89+
write!(
90+
f,
91+
" at line {} column {}",
92+
self.location.row(),
93+
self.location.column()
94+
)
95+
}
96+
}
97+
7798
#[derive(Clone,Debug,Default,PartialEq,Serialize,Deserialize)]
7899
pubstructLocation{
79100
row:usize,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp