22//!
33//! See [`lex_file`] for more information.
44
5- #[ allow( clippy:: enum_glob_use) ]
6- use LexingState :: * ;
7-
85use super :: state:: api:: {
9- CommentState , EscapeState , LexingState , SymbolState , end_current, handle_escape
6+ CommentState , EscapeState , LexingState as LS , SymbolState , end_current, handle_escape
107} ;
118use super :: types:: api:: { LexingData , Token } ;
129use crate :: errors:: api:: { Location , Res } ;
1310
1411/// Function to manage one character.
1512///
16- /// This function updates the [`LexingState `] automaton, and executes the right
13+ /// This function updates the [`LS `] automaton, and executes the right
1714/// handlers.
1815#[ expect( clippy:: too_many_lines) ]
1916fn lex_char (
2017 ch : char ,
2118 location : & Location ,
2219 lex_data : & mut LexingData ,
23- lex_state : & mut LexingState ,
20+ lex_state : & mut LS ,
2421 escape_state : & mut EscapeState ,
2522 eol : bool ,
2623) {
2724 match ( ch, lex_state, escape_state) {
28- ( _, StartOfLine , _) if ch. is_whitespace ( ) => ( ) ,
25+ ( _, LS :: StartOfLine , _) if ch. is_whitespace ( ) => ( ) ,
2926 /* Inside comment */
30- ( '/' , state @ Comment ( CommentState :: Star ) , _) => {
31- * state = Comment ( CommentState :: False ) ;
27+ ( '/' , state @ LS :: Comment ( CommentState :: Star ) , _) => {
28+ * state = LS :: Comment ( CommentState :: False ) ;
3229 }
33- ( '*' , state @ Comment ( CommentState :: True ) , _) => {
34- * state = Comment ( CommentState :: Star ) ;
30+ ( '*' , state @ LS :: Comment ( CommentState :: True ) , _) => {
31+ * state = LS :: Comment ( CommentState :: Star ) ;
3532 }
36- ( _, Comment ( CommentState :: True ) , _) => ( ) ,
37- ( _, state @ Comment ( CommentState :: Star ) , _) => {
38- * state = Comment ( CommentState :: True ) ;
33+ ( _, LS :: Comment ( CommentState :: True ) , _) => ( ) ,
34+ ( _, state @ LS :: Comment ( CommentState :: Star ) , _) => {
35+ * state = LS :: Comment ( CommentState :: True ) ;
3936 }
4037 /* Escaped character */
4138 (
4239 _,
43- state @ ( Char ( None ) | Str ( _) ) ,
40+ state @ ( LS :: Char ( None ) | LS :: Str ( _) ) ,
4441 escape @ ( EscapeState :: Single | EscapeState :: Sequence ( _) ) ,
4542 ) => {
4643 if let Some ( escaped) = handle_escape ( ch, lex_data, escape, location) {
4744 * escape = EscapeState :: False ;
4845 #[ expect( clippy:: wildcard_enum_match_arm) ]
4946 match state {
50- Char ( None ) => * state = Char ( Some ( escaped) ) ,
51- Str ( val) => val. push ( escaped) ,
47+ LS :: Char ( None ) => * state = LS :: Char ( Some ( escaped) ) ,
48+ LS :: Str ( val) => val. push ( escaped) ,
5249 _ => panic ! ( "this can't happen, see match above" ) ,
5350 }
5451 }
@@ -61,11 +58,11 @@ fn lex_char(
6158 ( '*' , state, _) if state. symbol ( ) . and_then ( SymbolState :: last) == Some ( '/' ) => {
6259 state. clear_last_symbol ( ) ;
6360 end_current ( state, lex_data, location) ;
64- * state = Comment ( CommentState :: True ) ;
61+ * state = LS :: Comment ( CommentState :: True ) ;
6562 }
6663
6764 /* Escape character */
68- ( '\\' , Char ( None ) | Str ( _) , escape) => * escape = EscapeState :: Single ,
65+ ( '\\' , LS :: Char ( None ) | LS :: Str ( _) , escape) => * escape = EscapeState :: Single ,
6966 ( '\\' , _, escape) if eol => * escape = EscapeState :: Single ,
7067 ( '\\' , state, _) => lex_data. push_err ( location. to_error ( format ! (
7168 "Escape characters are only authorised in strings or chars, not in '{}' context." ,
@@ -74,39 +71,39 @@ fn lex_char(
7471
7572 /* Static strings and chars */
7673 // open/close
77- ( '\'' , Symbols ( symbol_state) , _) if symbol_state. is_trigraph ( ) => {
74+ ( '\'' , LS :: Symbols ( symbol_state) , _) if symbol_state. is_trigraph ( ) => {
7875 if let Some ( ( size, symbol) ) = symbol_state. push ( ch, lex_data, location) {
7976 lex_data. push_token ( Token :: from_symbol ( symbol, size, location) ) ;
8077 }
8178 }
82- ( '\'' , state @ Char ( _) , _) => end_current ( state, lex_data, location) ,
83- ( '\'' , state, _) if !matches ! ( state, Str ( _) ) => {
79+ ( '\'' , state @ LS :: Char ( _) , _) => end_current ( state, lex_data, location) ,
80+ ( '\'' , state, _) if !matches ! ( state, LS :: Str ( _) ) => {
8481 end_current ( state, lex_data, location) ;
85- * state = LexingState :: Char ( None ) ;
82+ * state = LS :: Char ( None ) ;
8683 }
87- ( '\"' , state @ Str ( _) , _) => {
84+ ( '\"' , state @ LS :: Str ( _) , _) => {
8885 end_current ( state, lex_data, location) ;
8986 }
90- ( '\"' , state, _) if !matches ! ( state, Char ( _) ) => {
87+ ( '\"' , state, _) if !matches ! ( state, LS :: Char ( _) ) => {
9188 end_current ( state, lex_data, location) ;
92- * state = LexingState :: Str ( String :: new ( ) ) ;
89+ * state = LS :: Str ( String :: new ( ) ) ;
9390 }
9491 // middle
95- ( _, Char ( Some ( _) ) , _) => lex_data
92+ ( _, LS :: Char ( Some ( _) ) , _) => lex_data
9693 . push_err ( location. to_error ( "A char must contain only one character." . to_owned ( ) ) ) ,
97- ( _, state @ Char ( None ) , _) => * state = Char ( Some ( ch) ) ,
98- ( _, Str ( val) , _) => val. push ( ch) ,
94+ ( _, state @ LS :: Char ( None ) , _) => * state = LS :: Char ( Some ( ch) ) ,
95+ ( _, LS :: Str ( val) , _) => val. push ( ch) ,
9996
10097 /* Operator symbols */
10198 ( '/' , state, _) if state. symbol ( ) . and_then ( SymbolState :: last) == Some ( '/' ) => {
10299 state. clear_last_symbol ( ) ;
103100 end_current ( state, lex_data, location) ;
104101 lex_data. set_end_line ( ) ;
105102 }
106- ( '.' , Ident ( ident) , _) if !ident. contains ( '.' ) && ident. is_number ( ) => {
103+ ( '.' , LS :: Ident ( ident) , _) if !ident. contains ( '.' ) && ident. is_number ( ) => {
107104 ident. push ( '.' ) ;
108105 }
109- ( '+' | '-' , Ident ( ident) , _)
106+ ( '+' | '-' , LS :: Ident ( ident) , _)
110107 if !ident. contains ( '-' ) && !ident. contains ( '+' ) && ident. last_is_exp ( ) =>
111108 {
112109 ident. push ( ch) ;
@@ -117,13 +114,13 @@ fn lex_char(
117114 state,
118115 _,
119116 ) => {
120- if let Symbols ( symbol_state) = state {
117+ if let LS :: Symbols ( symbol_state) = state {
121118 if let Some ( ( size, symbol) ) = symbol_state. push ( ch, lex_data, location) {
122119 lex_data. push_token ( Token :: from_symbol ( symbol, size, location) ) ;
123120 }
124121 } else {
125122 end_current ( state, lex_data, location) ;
126- * state = LexingState :: Symbols ( SymbolState :: from ( ch) ) ;
123+ * state = LS :: Symbols ( SymbolState :: from ( ch) ) ;
127124 }
128125 }
129126
@@ -133,13 +130,13 @@ fn lex_char(
133130 }
134131
135132 // Whitespace: end of everyone
136- ( _, Ident ( val) , _) if ch. is_alphanumeric ( ) || matches ! ( ch, '_' | '.' | '+' | '-' ) => {
133+ ( _, LS :: Ident ( val) , _) if ch. is_alphanumeric ( ) || matches ! ( ch, '_' | '.' | '+' | '-' ) => {
137134 // dbg!("here", &val, ch);
138135 val. push ( ch) ;
139136 // dbg!("there", &val);
140137 }
141138 ( _, state, _) if ch. is_alphanumeric ( ) || matches ! ( ch, '_' ) => {
142- if let Symbols ( symbol) = state
139+ if let LS :: Symbols ( symbol) = state
143140 && symbol. last ( ) == Some ( '.' )
144141 && ch. is_ascii_digit ( )
145142 {
@@ -166,7 +163,7 @@ fn lex_char(
166163#[ inline]
167164pub fn lex_file ( content : & str , location : & mut Location ) -> Res < Vec < Token > > {
168165 let mut lex_data = LexingData :: default ( ) ;
169- let mut lex_state = LexingState :: default ( ) ;
166+ let mut lex_state = LS :: default ( ) ;
170167
171168 for line in content. lines ( ) {
172169 lex_line ( line, location, & mut lex_data, & mut lex_state) ;
@@ -180,12 +177,7 @@ pub fn lex_file(content: &str, location: &mut Location) -> Res<Vec<Token>> {
180177///
181178/// It stops at the first erroneous character, or at the end of the line if
182179/// everything was ok.
183- fn lex_line (
184- line : & str ,
185- location : & mut Location ,
186- lex_data : & mut LexingData ,
187- lex_state : & mut LexingState ,
188- ) {
180+ fn lex_line ( line : & str , location : & mut Location , lex_data : & mut LexingData , lex_state : & mut LS ) {
189181 lex_data. newline ( ) ;
190182 let mut escape_state = EscapeState :: False ;
191183 let trimmed = line. trim_end ( ) ;
@@ -217,6 +209,6 @@ fn lex_line(
217209 ) ) ;
218210 }
219211 } else {
220- * lex_state = LexingState :: default ( ) ;
212+ * lex_state = LS :: default ( ) ;
221213 }
222214}
0 commit comments