@@ -5,12 +5,12 @@ extern crate unic_emoji_char;
5
5
extern crate unicode_xid;
6
6
7
7
pub use super :: token:: Tok ;
8
+ use crate :: error:: { LexicalError , LexicalErrorType } ;
9
+ use crate :: location:: Location ;
8
10
use num_bigint:: BigInt ;
9
11
use num_traits:: Num ;
10
- use serde:: { Deserialize , Serialize } ;
11
12
use std:: cmp:: Ordering ;
12
13
use std:: collections:: HashMap ;
13
- use std:: fmt;
14
14
use std:: str:: FromStr ;
15
15
use unic_emoji_char:: is_emoji_presentation;
16
16
use unicode_xid:: UnicodeXID ;
@@ -60,61 +60,6 @@ pub struct Lexer<T: Iterator<Item = char>> {
60
60
keywords : HashMap < String , Tok > ,
61
61
}
62
62
63
- #[ derive( Debug , PartialEq ) ]
64
- pub struct LexicalError {
65
- pub error : LexicalErrorType ,
66
- pub location : Location ,
67
- }
68
-
69
- #[ derive( Debug , PartialEq ) ]
70
- pub enum LexicalErrorType {
71
- StringError ,
72
- UnicodeError ,
73
- NestingError ,
74
- UnrecognizedToken { tok : char } ,
75
- OtherError ( String ) ,
76
- }
77
-
78
- impl fmt:: Display for LexicalErrorType {
79
- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
80
- match * self {
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
- }
90
- }
91
-
92
- #[ derive( Clone , Debug , Default , PartialEq , Serialize , Deserialize ) ]
93
- pub struct Location {
94
- row : usize ,
95
- column : usize ,
96
- }
97
-
98
- impl fmt:: Display for Location {
99
- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
100
- write ! ( f, "line {} column {}" , self . row, self . column)
101
- }
102
- }
103
-
104
- impl Location {
105
- pub fn new ( row : usize , column : usize ) -> Self {
106
- Location { row, column }
107
- }
108
-
109
- pub fn row ( & self ) -> usize {
110
- self . row
111
- }
112
-
113
- pub fn column ( & self ) -> usize {
114
- self . column
115
- }
116
- }
117
-
118
63
pub fn get_keywords ( ) -> HashMap < String , Tok > {
119
64
let mut keywords: HashMap < String , Tok > = HashMap :: new ( ) ;
120
65
@@ -299,8 +244,7 @@ where
299
244
lxr. next_char ( ) ;
300
245
lxr. next_char ( ) ;
301
246
// Start at top row (=1) left column (=1)
302
- lxr. location . row = 1 ;
303
- lxr. location . column = 1 ;
247
+ lxr. location . reset ( ) ;
304
248
lxr
305
249
}
306
250
@@ -615,7 +559,10 @@ where
615
559
let tok = if is_bytes {
616
560
if string_content. is_ascii ( ) {
617
561
Tok :: Bytes {
618
- value : lex_byte ( string_content) ?,
562
+ value : lex_byte ( string_content) . map_err ( |error| LexicalError {
563
+ error,
564
+ location : self . get_pos ( ) ,
565
+ } ) ?,
619
566
}
620
567
} else {
621
568
return Err ( LexicalError {
@@ -684,7 +631,7 @@ where
684
631
let nxt = self . chars . next ( ) ;
685
632
self . chr0 = self . chr1 ;
686
633
self . chr1 = nxt;
687
- self . location . column += 1 ;
634
+ self . location . go_right ( ) ;
688
635
c
689
636
}
690
637
@@ -693,8 +640,7 @@ where
693
640
}
694
641
695
642
fn new_line ( & mut self ) {
696
- self . location . row += 1 ;
697
- self . location . column = 1 ;
643
+ self . location . newline ( ) ;
698
644
}
699
645
700
646
/// Given we are at the start of a line, count the number of spaces and/or tabs until the first character.
@@ -1252,7 +1198,7 @@ where
1252
1198
}
1253
1199
}
1254
1200
1255
- fn lex_byte ( s : String ) -> Result < Vec < u8 > , LexicalError > {
1201
+ fn lex_byte ( s : String ) -> Result < Vec < u8 > , LexicalErrorType > {
1256
1202
let mut res = vec ! [ ] ;
1257
1203
let mut escape = false ; //flag if previous was \
1258
1204
let mut hex_on = false ; // hex mode on or off
@@ -1271,10 +1217,7 @@ fn lex_byte(s: String) -> Result<Vec<u8>, LexicalError> {
1271
1217
hex_value. clear ( ) ;
1272
1218
}
1273
1219
} else {
1274
- return Err ( LexicalError {
1275
- error : LexicalErrorType :: StringError ,
1276
- location : Default :: default ( ) ,
1277
- } ) ;
1220
+ return Err ( LexicalErrorType :: StringError ) ;
1278
1221
}
1279
1222
} else {
1280
1223
match ( c, escape) {
0 commit comments