11use alloc:: { collections:: BTreeMap , string:: String , sync:: Arc , vec:: Vec } ;
22
33use nom:: {
4- IResult ,
4+ IResult , Parser ,
55 bytes:: complete:: take,
66 combinator:: { flat_map, map, map_res} ,
77 multi:: length_count,
88 number:: complete:: { be_u16, be_u32} ,
9- sequence:: tuple,
109} ;
11- use nom_derive:: { NomBE , Parse } ;
1210
1311use crate :: { ConstantPoolReference , constant_pool:: ConstantPoolItem , opcode:: Opcode } ;
1412
@@ -21,7 +19,7 @@ pub struct CodeAttributeExceptionTable {
2119
2220impl CodeAttributeExceptionTable {
2321 pub fn parse < ' a > ( data : & ' a [ u8 ] , constant_pool : & BTreeMap < u16 , ConstantPoolItem > ) -> IResult < & ' a [ u8 ] , Self > {
24- map ( tuple ( ( be_u16, be_u16, be_u16, be_u16) ) , |( start_pc, end_pc, handler_pc, catch_type) | {
22+ map ( ( be_u16, be_u16, be_u16, be_u16) , |( start_pc, end_pc, handler_pc, catch_type) | {
2523 let catch_type = if catch_type != 0 {
2624 let index = constant_pool. get ( & catch_type) . unwrap ( ) . class_name_index ( ) ;
2725 Some ( constant_pool. get ( & index) . unwrap ( ) . utf8 ( ) )
@@ -35,7 +33,8 @@ impl CodeAttributeExceptionTable {
3533 handler_pc,
3634 catch_type,
3735 }
38- } ) ( data)
36+ } )
37+ . parse ( data)
3938 }
4039}
4140
@@ -50,21 +49,22 @@ pub struct AttributeInfoCode {
5049impl AttributeInfoCode {
5150 pub fn parse < ' a > ( data : & ' a [ u8 ] , constant_pool : & BTreeMap < u16 , ConstantPoolItem > ) -> IResult < & ' a [ u8 ] , Self > {
5251 map (
53- tuple ( (
52+ (
5453 be_u16,
5554 be_u16,
5655 map ( flat_map ( be_u32, take) , |x : & [ u8 ] | Self :: parse_code ( x, constant_pool) ) ,
5756 length_count ( be_u16, |x| CodeAttributeExceptionTable :: parse ( x, constant_pool) ) ,
5857 length_count ( be_u16, |x| AttributeInfo :: parse ( x, constant_pool) ) ,
59- ) ) ,
58+ ) ,
6059 |( max_stack, max_locals, code, exception_table, attributes) | Self {
6160 max_stack,
6261 max_locals,
6362 code,
6463 exception_table,
6564 attributes,
6665 } ,
67- ) ( data)
66+ )
67+ . parse ( data)
6868 }
6969
7070 fn parse_code ( code : & [ u8 ] , constant_pool : & BTreeMap < u16 , ConstantPoolItem > ) -> BTreeMap < u32 , Opcode > {
@@ -86,12 +86,20 @@ impl AttributeInfoCode {
8686 }
8787}
8888
89- #[ derive( NomBE ) ]
9089pub struct AttributeInfoLineNumberTableEntry {
9190 pub start_pc : u16 ,
9291 pub line_number : u16 ,
9392}
9493
94+ impl AttributeInfoLineNumberTableEntry {
95+ pub fn parse ( data : & [ u8 ] ) -> IResult < & [ u8 ] , Self > {
96+ let ( data, start_pc) = be_u16 ( data) ?;
97+ let ( data, line_number) = be_u16 ( data) ?;
98+
99+ Ok ( ( data, Self { start_pc, line_number } ) )
100+ }
101+ }
102+
95103pub struct LocalVariableTableEntry {
96104 pub start_pc : u16 ,
97105 pub length : u16 ,
@@ -103,21 +111,22 @@ pub struct LocalVariableTableEntry {
103111impl LocalVariableTableEntry {
104112 pub fn parse < ' a > ( data : & ' a [ u8 ] , constant_pool : & BTreeMap < u16 , ConstantPoolItem > ) -> IResult < & ' a [ u8 ] , Self > {
105113 map (
106- tuple ( (
114+ (
107115 be_u16,
108116 be_u16,
109117 map ( be_u16, |x| constant_pool. get ( & x) . unwrap ( ) . utf8 ( ) ) ,
110118 map ( be_u16, |x| constant_pool. get ( & x) . unwrap ( ) . utf8 ( ) ) ,
111119 be_u16,
112- ) ) ,
120+ ) ,
113121 |( start_pc, length, name, descriptor, index) | Self {
114122 start_pc,
115123 length,
116124 name,
117125 descriptor,
118126 index,
119127 } ,
120- ) ( data)
128+ )
129+ . parse ( data)
121130 }
122131}
123132
@@ -142,12 +151,14 @@ pub enum AttributeInfo {
142151impl AttributeInfo {
143152 pub fn parse < ' a > ( data : & ' a [ u8 ] , constant_pool : & BTreeMap < u16 , ConstantPoolItem > ) -> IResult < & ' a [ u8 ] , Self > {
144153 map_res (
145- tuple ( ( map ( be_u16, |x| constant_pool. get ( & x) . unwrap ( ) . utf8 ( ) ) , flat_map ( be_u32, take) ) ) ,
154+ ( map ( be_u16, |x| constant_pool. get ( & x) . unwrap ( ) . utf8 ( ) ) , flat_map ( be_u32, take) ) ,
146155 |( name, info) : ( _ , & [ u8 ] ) | {
147156 Ok :: < _ , nom:: Err < _ > > ( match name. as_str ( ) {
148157 "ConstantValue" => AttributeInfo :: ConstantValue ( Self :: parse_constant_value ( info, constant_pool) ?. 1 ) ,
149158 "Code" => AttributeInfo :: Code ( AttributeInfoCode :: parse ( info, constant_pool) ?. 1 ) ,
150- "LineNumberTable" => AttributeInfo :: LineNumberTable ( length_count ( be_u16, AttributeInfoLineNumberTableEntry :: parse) ( info) ?. 1 ) ,
159+ "LineNumberTable" => {
160+ AttributeInfo :: LineNumberTable ( length_count ( be_u16, AttributeInfoLineNumberTableEntry :: parse) . parse ( info) ?. 1 )
161+ }
151162 "SourceFile" => AttributeInfo :: SourceFile ( Self :: parse_source_file ( info, constant_pool) ?. 1 ) ,
152163 "LocalVariableTable" => AttributeInfo :: LocalVariableTable ( Self :: parse_local_variable_table ( info, constant_pool) ?. 1 ) ,
153164 "StackMap" => AttributeInfo :: StackMap ( info. to_vec ( ) ) ,
@@ -162,21 +173,22 @@ impl AttributeInfo {
162173 _ => return Err ( nom:: Err :: Error ( nom:: error_position!( info, nom:: error:: ErrorKind :: Switch ) ) ) ,
163174 } )
164175 } ,
165- ) ( data)
176+ )
177+ . parse ( data)
166178 }
167179
168180 fn parse_source_file < ' a > ( data : & ' a [ u8 ] , constant_pool : & BTreeMap < u16 , ConstantPoolItem > ) -> IResult < & ' a [ u8 ] , Arc < String > > {
169- map ( be_u16, |x| constant_pool. get ( & x) . unwrap ( ) . utf8 ( ) ) ( data)
181+ map ( be_u16, |x| constant_pool. get ( & x) . unwrap ( ) . utf8 ( ) ) . parse ( data)
170182 }
171183
172184 fn parse_constant_value < ' a > ( data : & ' a [ u8 ] , constant_pool : & BTreeMap < u16 , ConstantPoolItem > ) -> IResult < & ' a [ u8 ] , ConstantPoolReference > {
173- map ( be_u16, |x| ConstantPoolReference :: from_constant_pool ( constant_pool, x as _ ) ) ( data)
185+ map ( be_u16, |x| ConstantPoolReference :: from_constant_pool ( constant_pool, x as _ ) ) . parse ( data)
174186 }
175187
176188 fn parse_local_variable_table < ' a > (
177189 data : & ' a [ u8 ] ,
178190 constant_pool : & BTreeMap < u16 , ConstantPoolItem > ,
179191 ) -> IResult < & ' a [ u8 ] , Vec < LocalVariableTableEntry > > {
180- length_count ( be_u16, |x| LocalVariableTableEntry :: parse ( x, constant_pool) ) ( data)
192+ length_count ( be_u16, |x| LocalVariableTableEntry :: parse ( x, constant_pool) ) . parse ( data)
181193 }
182194}
0 commit comments