2
2
#![ allow( missing_docs) ]
3
3
4
4
use enum_assoc:: Assoc ;
5
- use nom:: {
6
- branch:: alt,
7
- sequence:: preceded,
8
- } ;
5
+ use nom:: { branch:: alt, sequence:: preceded} ;
9
6
10
7
use crate :: parser:: {
11
8
ast:: {
@@ -16,7 +13,7 @@ use crate::parser::{
16
13
span:: Span ,
17
14
ParserInput , ParserResult ,
18
15
} ;
19
- use num:: { BigInt , Num } ;
16
+ use num:: { BigInt , Num } ;
20
17
21
18
#[ derive( Assoc , Debug , Clone , Copy , PartialEq , Eq ) ]
22
19
#[ func( pub fn token( token: & TokenKind ) -> Option <Self >) ]
@@ -44,7 +41,7 @@ impl Encoding {
44
41
}
45
42
46
43
#[ derive( Debug ) ]
47
- pub struct EncodedNumber < ' a > {
44
+ pub struct EncodedNumber < ' a > {
48
45
/// [Span] associated with this node
49
46
span : Span < ' a > ,
50
47
/// The prefix of the encoded number
@@ -53,7 +50,6 @@ pub struct EncodedNumber<'a>{
53
50
suffix : Token < ' a > ,
54
51
}
55
52
56
-
57
53
/// Value of [EncodedNumber]
58
54
#[ derive( Debug ) ]
59
55
pub enum NumberValue {
@@ -64,52 +60,42 @@ pub enum NumberValue {
64
60
}
65
61
66
62
impl < ' a > EncodedNumber < ' a > {
67
-
68
63
/// Removes the binary prefix (0b) and returns the binary suffix
69
64
fn parse_binary ( input : ParserInput < ' a > ) -> ParserResult < ' a , ( Encoding , Token < ' a > ) > {
70
- preceded ( Token :: binary_prefix, Token :: bin_number) ( input) . map (
71
- |( remaining, bin_digits) | {
72
- ( remaining, ( Encoding :: Binary , bin_digits) )
73
- } )
65
+ preceded ( Token :: binary_prefix, Token :: bin_number) ( input)
66
+ . map ( |( remaining, bin_digits) | ( remaining, ( Encoding :: Binary , bin_digits) ) )
74
67
}
75
68
76
-
77
69
/// Removes the octal prefix (0o) and returns the octal suffix
78
70
fn parse_octal ( input : ParserInput < ' a > ) -> ParserResult < ' a , ( Encoding , Token < ' a > ) > {
79
- preceded ( Token :: octal_prefix, Token :: oct_number) ( input) . map (
80
- |( remaining, oct_digits) | {
81
- ( remaining, ( Encoding :: Octal , oct_digits) )
82
- } )
71
+ preceded ( Token :: octal_prefix, Token :: oct_number) ( input)
72
+ . map ( |( remaining, oct_digits) | ( remaining, ( Encoding :: Octal , oct_digits) ) )
83
73
}
84
74
85
75
/// Removes the hex prefix (0x) and returns the hex suffix
86
76
fn parse_hex ( input : ParserInput < ' a > ) -> ParserResult < ' a , ( Encoding , Token < ' a > ) > {
87
- preceded ( Token :: hex_prefix, Token :: hex_number) ( input) . map (
88
- |( remaining, hex_chars) | {
89
- ( remaining, ( Encoding :: Hexadecimal , hex_chars) )
90
- } )
77
+ preceded ( Token :: hex_prefix, Token :: hex_number) ( input)
78
+ . map ( |( remaining, hex_chars) | ( remaining, ( Encoding :: Hexadecimal , hex_chars) ) )
91
79
}
92
80
93
-
94
81
/// Return the value of this number, represented as a [NumberValue].
95
82
pub fn value ( & self ) -> NumberValue {
96
- let string = format ! ( "{}{}" , self . prefix. print( ) , self . suffix) ;
83
+ let string = format ! ( "{}{}" , self . prefix. print( ) , self . suffix) ;
97
84
98
85
// Retrieves the base of encoded number based on [Encoding]
99
86
let nr_encoding = self . prefix . radix ( ) ;
100
87
let span = & self . suffix . span ( ) ;
101
88
let suffix = span. fragment ( ) ;
102
-
89
+
103
90
// Returns decoded number as <i64> if it is not too big
104
- // Otherwise, return string representation of the decoded number
91
+ // Otherwise, return string representation of the decoded number
105
92
if let Ok ( integer) = <i64 >:: from_str_radix ( suffix, nr_encoding) {
106
93
return NumberValue :: Integer ( integer) ;
107
- } else if let Ok ( bigint) = <BigInt as Num >:: from_str_radix ( suffix, nr_encoding) {
94
+ } else if let Ok ( bigint) = <BigInt as Num >:: from_str_radix ( suffix, nr_encoding) {
108
95
return NumberValue :: Large ( bigint. to_string ( ) ) ;
109
96
}
110
97
NumberValue :: Large ( string)
111
98
}
112
-
113
99
}
114
100
115
101
const CONTEXT : ParserContext = ParserContext :: EncodedNumber ;
@@ -131,26 +117,20 @@ impl<'a> ProgramAST<'a> for EncodedNumber<'a> {
131
117
132
118
context (
133
119
CONTEXT ,
134
- alt ( (
135
- Self :: parse_binary,
136
- Self :: parse_octal,
137
- Self :: parse_hex,
138
- ) ) ,
120
+ alt ( ( Self :: parse_binary, Self :: parse_octal, Self :: parse_hex) ) ,
139
121
) ( input)
140
- . map (
141
- |( rest, ( prefix, suffix) ) | {
142
- let rest_span = rest. span ;
143
-
144
- (
145
- rest,
146
- EncodedNumber {
147
- span : input_span. until_rest ( & rest_span) ,
148
- prefix,
149
- suffix,
150
- } ,
151
- )
152
- } ,
153
- )
122
+ . map ( |( rest, ( prefix, suffix) ) | {
123
+ let rest_span = rest. span ;
124
+
125
+ (
126
+ rest,
127
+ EncodedNumber {
128
+ span : input_span. until_rest ( & rest_span) ,
129
+ prefix,
130
+ suffix,
131
+ } ,
132
+ )
133
+ } )
154
134
}
155
135
156
136
fn context ( & self ) -> ParserContext {
@@ -163,32 +143,35 @@ mod test {
163
143
use nom:: combinator:: all_consuming;
164
144
165
145
use crate :: parser:: {
166
- ast:: { expression:: basic:: enc_number:: { EncodedNumber , NumberValue } , ProgramAST } ,
146
+ ast:: {
147
+ expression:: basic:: enc_number:: { EncodedNumber , NumberValue } ,
148
+ ProgramAST ,
149
+ } ,
167
150
ParserInput , ParserState ,
168
151
} ;
169
152
170
153
#[ test]
171
154
fn parse_numbers ( ) {
172
155
let valid_numbers = vec ! [
173
- ( "0x11" , 17 ) ,
174
- ( "0o11" , 9 ) ,
175
- ( "0b11" , 3 ) ,
176
- ( "0xAB2CE" , 701134 ) ,
177
- ( "0xab2ce" , 701134 ) ,
178
- ( "0o777" , 511 ) ,
179
- ( "0b01010101" , 85 ) ,
156
+ ( "0x11" , 17 ) ,
157
+ ( "0o11" , 9 ) ,
158
+ ( "0b11" , 3 ) ,
159
+ ( "0xAB2CE" , 701134 ) ,
160
+ ( "0xab2ce" , 701134 ) ,
161
+ ( "0o777" , 511 ) ,
162
+ ( "0b01010101" , 85 ) ,
180
163
] ;
181
164
182
- let invalid_numbers = vec ! [ "0xG" , "0o8" , "0b2" , "0x" , "0b" , "0o" ] ;
165
+ let invalid_numbers = vec ! [ "0xG" , "0o8" , "0b2" , "0x" , "0b" , "0o" ] ;
183
166
184
167
for ( valid, exp_value) in valid_numbers {
185
168
let input = ParserInput :: new ( valid, ParserState :: default ( ) ) ;
186
169
let result = all_consuming ( EncodedNumber :: parse) ( input) ;
187
170
assert ! ( result. is_ok( ) ) ;
188
171
189
172
if let Ok ( ( _, ast_node) ) = result {
190
- if let NumberValue :: Integer ( integer) = ast_node. value ( ) {
191
- assert_eq ! ( integer, exp_value) ;
173
+ if let NumberValue :: Integer ( integer) = ast_node. value ( ) {
174
+ assert_eq ! ( integer, exp_value) ;
192
175
}
193
176
} ;
194
177
}
0 commit comments