-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_class.rs
More file actions
80 lines (72 loc) · 2.78 KB
/
parse_class.rs
File metadata and controls
80 lines (72 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//! Example showing how to parse a VB6 class file from raw bytes.
//! This example uses a hardcoded byte array, but in a real application,
//! you would typically read the bytes from a `.cls` file on disk.
//!
use vb6parse::files::ClassFile;
use vb6parse::io::SourceFile;
fn main() {
// Hardcoded example of a VB6 class file content in bytes.
let input = b"VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = \"Something\"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
' Some comment";
// Decode the source file from the byte array.
// The filename is provided for reference in error messages.
// In a real application, use the actual filename.
// Decode with replacement to handle any invalid characters gracefully.
let result = SourceFile::decode_with_replacement("class_parse.cls", input);
// Handle potential decoding errors.
let source_file = match result {
Ok(source_file) => source_file,
Err(e) => panic!("Failed to decode source file 'class_parse.cls': {e:?}"),
};
// Parse the class file from the decoded source file.
let class = ClassFile::parse(&source_file).unwrap_or_fail();
// Print out some information about the parsed class file.
println!("Class Version Major: {}", class.header.version.major);
println!("Class Version Minor: {}", class.header.version.minor);
println!("Class Properties:");
println!("\tMulti Use: {:?}", class.header.properties.multi_use);
println!("\tPersistable: {:?}", class.header.properties.persistable);
println!(
"\tData Binding Behavior: {:?}",
class.header.properties.data_binding_behavior
);
println!(
"\tData Source Behavior: {:?}",
class.header.properties.data_source_behavior
);
println!(
"\tMTS Transaction Mode: {:?}",
class.header.properties.mts_transaction_mode
);
println!("Class Attributes:");
println!("\tName: {:?}", class.header.attributes.name);
println!(
"\tGlobal Name Space: {:?}",
class.header.attributes.global_name_space
);
println!("\tCreatable: {:?}", class.header.attributes.creatable);
println!(
"\tPredeclared ID: {:?}",
class.header.attributes.predeclared_id
);
println!("\tExposed: {:?}", class.header.attributes.exposed);
println!("Ext Attributes:");
for ext in &class.header.attributes.ext_key {
println!("\t{} = {}", ext.0, ext.1);
}
// Print the concrete syntax tree (CST) of the parsed class file.
println!("CST:");
println!("{}", class.cst.debug_tree());
}