objr_procmacro 1.0.0

procmacro crate for objr
Documentation
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
//SPDX-License-Identifier: MIT OR Apache-2.0

//! Implements declaration parsing for objc headers
//!
//! Primarily used in subclassing.

#[derive(Debug)]
struct Type(String);
#[derive(Debug)]
struct SelectorPart(String);
#[derive(Debug)]
struct ArgumentName(String);

///Taken from https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html#//apple_ref/doc/uid/TP40008048-CH100
#[derive(Debug)]
//ignore the fact that some of these cases are unused
#[allow(dead_code)]
enum ParsedType {
    Char,
    Int,
    Short,
    Long,
    LongLong,
    UChar,
    UInt,
    UShort,
    ULong,
    ULongLong,
    Float,
    Double,
    Bool,
    Void,
    CharStar,
    Object,
    Sel,
    //These types are included but may not be correctly parsed
    Array,
    Structure,
    Union,
    Bitfield,
    Pointer(Box<ParsedType>),
    Class,
    Unknown,
    //"Special" types, not part of the standard, but implemented for convenience
    CGRect,
    CGSize,
}

impl ParsedType {
    fn type_encoding(&self) -> String {
        //https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html#//apple_ref/doc/uid/TP40008048-CH100
        match self {
            ParsedType::Char => "c".to_owned(),
            ParsedType::Int => "i".to_owned(),
            ParsedType::Short => "s".to_owned(),
            ParsedType::Long => "l".to_owned(),
            ParsedType::LongLong => "q".to_owned(),
            ParsedType::UChar => "C".to_owned(),
            ParsedType::UInt => "I".to_owned(),
            ParsedType::UShort => "S".to_owned(),
            ParsedType::ULong => "L".to_owned(),
            ParsedType::ULongLong => "Q".to_owned(),
            ParsedType::Float => "f".to_owned(),
            ParsedType::Double => "d".to_owned(),
            ParsedType::Bool => "B".to_owned(),
            ParsedType::Void => "v".to_owned(),
            ParsedType::CharStar => "*".to_owned(),
            ParsedType::Object => "@".to_owned(),
            ParsedType::Sel => ":".to_owned(),
            ParsedType::Array => "[v]".to_owned(), //treated as array to void
            ParsedType::Structure => "{n=v}".to_owned(), //treated as struct of void
            ParsedType::Union => "(n=v)".to_owned(), //union of void
            ParsedType::Bitfield => "b0".to_owned(), //0 bits
            ParsedType::Pointer(t) => {
                let mut s = "^".to_owned();
                s.push_str(&t.type_encoding());
                s
            }
            ParsedType::Class => "@".to_owned(),
            ParsedType::Unknown => "?".to_owned(),
            ParsedType::CGRect => "{CGRect={CGPoint=dd}{CGSize=dd}}".to_owned(),
            ParsedType::CGSize => "{CGSize=dd}".to_owned(),
        }
    }

    //This is declared as `Result` so we can make it a const fn.  However,
    //the err is basically a panic.
    const fn magic_size(&self) -> Result<u8,()> {

        /*On x64 anyway this appears to be the size of the type in bytes, rounded up to the nearest word.

        e.g. char is 1 byte, but we round up to 4
        int is 4 bytes and also rounded up to 4 etc.

        I assume this is some alignment or memory thing either part of C or objc, not sure which.

        Not handling the incomplete types since it seems like more work than it's worth.
         */
        match self {
            ParsedType::Char => Ok(4),
            ParsedType::Int => Ok(4),
            ParsedType::Short => Ok(4),
            ParsedType::Long => Ok(8),
            ParsedType::LongLong => Ok(8),
            ParsedType::UChar => Ok(4),
            ParsedType::UInt => Ok(4),
            ParsedType::UShort => Ok(4),
            ParsedType::ULong => Ok(8),
            ParsedType::ULongLong => Ok(8),
            ParsedType::Float => Ok(4),
            ParsedType::Double => Ok(8),
            ParsedType::Bool => Ok(4),
            ParsedType::Void => Err(()),
            ParsedType::CharStar => Ok(8),
            ParsedType::Object => Ok(8),
            ParsedType::Sel => Ok(8),
            ParsedType::Array => Err(()),
            ParsedType::Structure => Err(()),
            ParsedType::Union => Err(()),
            ParsedType::Bitfield => Err(()),
            ParsedType::Pointer(_) => Ok(8),
            ParsedType::Class => Ok(8),
            ParsedType::Unknown => Err(()),
            ParsedType::CGRect => Ok(32),
            ParsedType::CGSize => Ok(16),
        }
    }
    fn parse(str: &str) -> Self {
        match str {
            "CGSize" => ParsedType::CGSize,
            "NSSize" => ParsedType::CGSize,
            "CGRect" => ParsedType::CGRect,
            "NSRect" => ParsedType::CGRect,
            "id" => ParsedType::Object,
            "char" => ParsedType::Char,
            "int" => ParsedType::Int,
            "short" => ParsedType::Short,
            "long" => ParsedType::Long,
            "long long" => ParsedType::LongLong,
            "unsigned char" => ParsedType::UChar,
            "unsigned int" => ParsedType::UInt,
            "unsigned short" => ParsedType::UShort,
            "unsigned long"=>ParsedType::ULong,
            "unsigned long long"=>ParsedType::ULongLong,
            "float"=>ParsedType::Float,
            "double"=>ParsedType::Double,
            "bool"=>ParsedType::Bool,
            "BOOL"=>ParsedType::Bool,
            "void"=>ParsedType::Void,
            "char*"=>ParsedType::CharStar,
            "char *"=>ParsedType::CharStar,
            str if str.ends_with("*") => {
                //parse the part before the pointer
                let prior_to_ptr = str.split_at(str.len() - 2);
                let final_result = match ParsedType::parse(prior_to_ptr.0) {
                    ParsedType::Unknown => ParsedType::Object,
                    //valid types are pointers to the type
                    other => ParsedType::Pointer(Box::new(other))
                };
                final_result
            },
            "SEL"=>ParsedType::Sel,
            _ => ParsedType::Unknown
        }
    }
}

///This parses expressions such as `[-/+](ReturnType) selectorPart:(ArgumentType) ArgumentName`
#[derive(Debug)]
enum DeclarationParserState {
    Initial,
    ///e.g., `-(void)` is `void`
    ReturnType(Type),
    SelectorPart(SelectorPart),
    ArgumentType(Type),
    ArgumentName(ArgumentName) //loops back to SelectorPart
}

struct PartialDeclaration {
    selector_part: SelectorPart,
    argument_type: Type,
}

enum PartType {
    LoneSelector(SelectorPart), //something like `-(void) a`;
    Argument(PartialDeclaration) //something like `-(void) a:(int) foo`
}


struct ParsedDeclaration {
    //todo: methodkind
    return_type: Type,
    //All methods are required to have at least 1 part.
    //To model this in the typesystem, we store the first part inline
    first_part: PartType,
    //subsequent parts go in here
    next_parts: Vec<PartialDeclaration>

}

impl ParsedDeclaration {
    fn selector(&self) -> String {
        let mut s = String::new();
        match &self.first_part {
            PartType::LoneSelector(sel) => {
                s.push_str(&sel.0);
            }
            PartType::Argument(a) => {
                s.push_str(&a.selector_part.0);
                s.push(':');
            }
        }
        for part in &self.next_parts {
            s.push_str(&part.selector_part.0);
            s.push(':')
        }
        s
    }

    fn type_str(&self) -> String {
        let mut user_args = Vec::new();
        match &self.first_part {
            PartType::LoneSelector(_) => {}
            PartType::Argument(arg) => {
                user_args.push(ParsedType::parse(&arg.argument_type.0));
            }
        }
        for arg in &self.next_parts {
            user_args.push(ParsedType::parse(&arg.argument_type.0));
        }
        let return_type = ParsedType::parse(&self.return_type.0);
        //output starts with return type
        let mut output = return_type.type_encoding();
        //Next phrase is the entire size
        //calculate the arg size
        let user_arg_size = user_args.iter().fold(0, |a, b| a + b.magic_size().expect("magic_size"));
        let entire_size = user_arg_size
            + ParsedType::Object.magic_size().expect("magic_size") //implicit self arg
        + ParsedType::Sel.magic_size().expect("magic_size"); //implicit SEL arg
        //return type seems not to be included in this value.

        //this consists of
        //0.  entire_size
        //1. @0 => seems to indicate the self arg goes into some 0 slot
        //2. :{} => sel goes into slot 8
        output.push_str(&format!("{}@0:{}",entire_size,ParsedType::Object.magic_size().expect("magic_size")));

        let mut slot = ParsedType::Object.magic_size().expect("magic_size") + ParsedType::Sel.magic_size().expect("magic_size");
        for arg in user_args {
            output.push_str(&arg.type_encoding());
            output.push_str(&format!("{}",slot));
            //advance slot for next time?
            slot += arg.magic_size().expect("magic_size");
        }
        output
    }
}

const DEBUG_PARSER: bool = false;

impl ParsedDeclaration {

    fn from_str(str: &str) -> Result<Self,String> {
        let mut state = DeclarationParserState::Initial;
        let mut string_iter = str.chars();
        let mut return_type = None;

        let mut current_partial_argument_type = None;
        let mut current_partial_selector_part = None;

        let mut parsed_partials = Vec::new();

        while let Some(char) = string_iter.next(){

            //I thought about parsing in wider blocks than by characters but I think
            //it would complicate the tokenization (whitespace removal) somewhat.
            match state { //state is moved here.  After this point we need to reassign it.
                DeclarationParserState::Initial => {
                    if char == ' ' {
                        state = DeclarationParserState::Initial; //continue
                    }
                    else if char == '-' {
                        state = DeclarationParserState::ReturnType(Type(String::with_capacity(10)));
                    }
                    else {
                        return Err(format!("expected `-``near {:?}",char));
                    }
                }
                DeclarationParserState::ReturnType(partial_type) => {
                    if char == ' ' && partial_type.0.len() == 0 {
                        //ignore leading space
                        state = DeclarationParserState::ReturnType(partial_type);
                    }
                    else if char == ' ' {
                        return Err("Expected return type near ' '".to_owned());
                    }
                    else if char == '(' {
                        //ignore
                        state = DeclarationParserState::ReturnType(partial_type);
                    }
                    else if char == ')' {
                        //section complete
                        if DEBUG_PARSER {
                            println!("Parsed return type {:?}",partial_type);
                        }
                        return_type = Some(partial_type);
                        state = DeclarationParserState::SelectorPart(SelectorPart(String::with_capacity(20)));
                    }
                    else if char == '(' || char == ' ' {
                        //ignore
                        state = DeclarationParserState::ReturnType(partial_type);
                    }
                    else {
                        //extend type
                        let mut extended_type = partial_type.0;
                        extended_type.push(char);
                        state = DeclarationParserState::ReturnType(Type(extended_type));
                    }
                }
                DeclarationParserState::SelectorPart(partial_selector) => {
                    if char == ' ' && partial_selector.0.len() == 0 {
                        //ignore leading space
                        state = DeclarationParserState::SelectorPart(partial_selector);
                    }
                    else if char == ' ' {
                        return Err(format!("Expected `selector:` near {:?}", partial_selector))
                    }
                    else if char == ':' {
                        //section complete
                        if DEBUG_PARSER {
                            println!("Parsed {:?}",partial_selector);
                        }
                        current_partial_selector_part = Some(partial_selector);

                        state = DeclarationParserState::ArgumentType(Type(String::with_capacity(10)));
                    }
                    else {
                        //extend type
                        let mut partial_string = partial_selector.0;
                        partial_string.push(char);
                        state = DeclarationParserState::SelectorPart(SelectorPart(partial_string));
                    }
                }
                DeclarationParserState::ArgumentType(partial_type) => {
                    if char == ' ' && partial_type.0.len() == 0 {
                        //ignore leading whitespace
                        state = DeclarationParserState::ArgumentType(partial_type)
                    }
                    else if char == ' ' {
                        return Err(format!("Expected argument type near whitespace after {:?}",partial_type));
                    }
                    else if char == '(' { //ignore this token
                        state = DeclarationParserState::ArgumentType(partial_type)
                    }
                    else if char == ')' {
                        //section complete
                        if DEBUG_PARSER {
                            println!("Parsed argument type {:?}",partial_type);
                        }
                        current_partial_argument_type = Some(partial_type);
                        state = DeclarationParserState::ArgumentName(ArgumentName(String::with_capacity(10)));
                    }
                    else { //extend type
                        let mut new= partial_type.0;
                        new.push(char);
                        state = DeclarationParserState::ArgumentType(Type(new));
                    }
                }
                DeclarationParserState::ArgumentName(partial_name) => {
                    if char == ' ' && partial_name.0.len() == 0 {
                        //ignore leading whitespace
                        state = DeclarationParserState::ArgumentName(partial_name)
                    }
                    else if char == ' ' { //end of argument name
                        if DEBUG_PARSER {
                            println!("Parsed {:?}",partial_name);
                        }
                        let new_part = PartialDeclaration {
                            argument_type: current_partial_argument_type.take().unwrap(),
                            selector_part: current_partial_selector_part.take().unwrap()
                        };
                        parsed_partials.push(new_part);
                        state = DeclarationParserState::SelectorPart(SelectorPart(String::with_capacity(20)));
                    }
                    else {
                        let mut new = partial_name.0;
                        new.push(char);
                        state = DeclarationParserState::ArgumentName(ArgumentName(new));
                    }
                }
            }
        } //end of chars

        //at this point, the question is, did we stop at an OK location?
        let expected: Option<&'static str> = match state {
            DeclarationParserState::Initial => Some("-"),
            DeclarationParserState::ReturnType(_) => Some(")"),
            DeclarationParserState::SelectorPart(_) => None, //ok to stop here
            DeclarationParserState::ArgumentType(_) => Some(")"),
            DeclarationParserState::ArgumentName(_) => None, //ok to stop here
        };
        if let Some(expected) = expected {
            return Err(format!("Expected `{}` after {}",expected,str));
        }

        //Finish all our final states
        //If we were parsing an argument, finish the partial
        if let Some(t) = current_partial_argument_type.take() {
            parsed_partials.push(PartialDeclaration {
                argument_type: t,
                selector_part: current_partial_selector_part.take().expect("current_partial_selector_part")
            });
        }

        let first_part: PartType;
        match state {
            DeclarationParserState::SelectorPart(part) if parsed_partials.len() == 0 => {
                //In this case, we may have parsed a bit of a selector, but did not see a `:`
                //ex `-(void) foo;`
                //here we want this to be a lone selector
                first_part = PartType::LoneSelector(part);
            }
            _ => {
                //otherwise the first part is the removed first element
                first_part = PartType::Argument(parsed_partials.remove(0));
            }
        }


        Ok(ParsedDeclaration {
            return_type: return_type.expect("return_type"),
            first_part,
            next_parts: parsed_partials
        })
    }
}

///Uses the above typesystem to parse a declaration into a selector
pub fn parse_to_selector(declaration: &str) -> Result<String,String> {
    let decl = ParsedDeclaration::from_str(declaration);
    decl.map(|f| f.selector())
}


pub fn parse_to_type_encoding(declaration: &str) -> Result<String,String> {
    let decl = ParsedDeclaration::from_str(declaration);
    decl.map(|f| f.type_str())
}



#[test]
fn parse_declaration_1() {
    let parse_1 = ParsedDeclaration::from_str("-(void) bar");
    assert!(parse_1.is_ok());
    let t = parse_1.unwrap();
    assert_eq!(t.selector(), "bar");

    assert_eq!(t.type_str(), "v16@0:8");
}

#[test]
fn parse_declaration_2() {
    let parse_2 = ParsedDeclaration::from_str("-(void) a:(int) arg b: (float) arg2");
    assert!(parse_2.is_ok(),"{:?}",parse_2.err().unwrap());
    let p = parse_2.unwrap();
    assert_eq!(p.selector(), "a:b:");
    assert_eq!(p.type_str(), "v24@0:8i16f20");
}

#[test] fn parse_declaration_3() {
    let parse = ParsedDeclaration::from_str("-(id) initWithFrame:(CGRect) frame");
    assert!(parse.is_ok());
    let p = parse.unwrap();
    assert_eq!(p.selector(), "initWithFrame:");
    assert_eq!(p.type_str(), "@48@0:8{CGRect={CGPoint=dd}{CGSize=dd}}16");
}