~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/monomac/src/parse.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
98
98
 
99
99
class Declaration {
100
100
        public string selector, retval, parameters;
101
 
        public bool is_abstract, is_static;
 
101
        public bool is_abstract, is_static, appearance;
102
102
        
103
 
        public Declaration (string selector, string retval, string parameters, bool is_abstract, bool is_static)
 
103
        public Declaration (string selector, string retval, string parameters, bool is_abstract, bool is_static, bool appearance)
104
104
        {
105
105
                this.selector = selector;
106
106
                this.retval = retval;
107
107
                this.parameters = parameters;
108
108
                this.is_abstract = is_abstract;
109
109
                this.is_static = is_static;
 
110
                this.appearance = appearance;
110
111
        }
111
112
        
112
113
}
113
114
 
114
115
class Declarations {
115
116
        List<Declaration> decls = new List<Declaration> ();
116
 
        StreamWriter gencs;
 
117
        TextWriter gencs;
117
118
        
118
 
        public Declarations (StreamWriter gencs)
 
119
        public Declarations (TextWriter gencs)
119
120
        {
120
121
                this.gencs = gencs;
121
122
        }
163
164
 
164
165
        List<string> ignore = new List<string> ();
165
166
        
166
 
        public void Generate ()
 
167
        public void Generate (string extraAttribute)
167
168
        {
168
169
                var copy = decls;
169
170
                var properties = (from d in copy
180
181
                        if (ignore.Contains (d.selector) || properties.Contains (d.selector))
181
182
                                continue;
182
183
 
 
184
                        if (extraAttribute != null)
 
185
                                gencs.WriteLine ("\t\t[{0}]", extraAttribute);
183
186
                        if (d.is_abstract)
184
187
                                gencs.WriteLine ("\t\t[Abstract]");
185
188
                        if (d.is_static)
186
189
                                gencs.WriteLine ("\t\t[Static]");
 
190
                        if (d.appearance)
 
191
                                gencs.WriteLine ("\t\t[Appearance]");
187
192
                        gencs.WriteLine ("\t\t[Export (\"{0}\")]", d.selector);
188
193
                        gencs.WriteLine ("\t\t{0} {1} ({2});", d.retval, TrivialParser.AsMethod (TrivialParser.CleanSelector (d.selector)), d.parameters);
189
194
                        gencs.WriteLine ();
210
215
}
211
216
 
212
217
class TrivialParser {
213
 
        StreamWriter gencs, other;
 
218
        TextWriter gencs, other;
214
219
        StreamReader r;
215
220
 
216
221
        // Used to limit which APIs to include in the binding
217
222
        string limit;
 
223
        string extraAttribute;
218
224
        OptionSet options;
219
225
        
220
226
        ArrayList types = new ArrayList ();
221
227
        
222
 
        void ProcessProperty (string line)
 
228
        void ProcessProperty (string line, bool appearance)
223
229
        {
224
230
                bool ro = false;
225
231
                string getter = null;
268
274
                                break;
269
275
                        selector.Append (c);
270
276
                }
 
277
                if (extraAttribute != null)
 
278
                        gencs.WriteLine ("\t\t[{0}]", extraAttribute);
 
279
                if (appearance)
 
280
                        gencs.WriteLine ("\t\t[Appearance]");
271
281
                gencs.WriteLine ("\t\t[Export (\"{0}\")]", selector);
 
282
                        
272
283
                gencs.WriteLine ("\t\t{0} {1} {{ {2} {3} }}",
273
284
                                 RemapType (type.ToString ()), AsMethod (selector.ToString ()),
274
285
                                 getter != null ? "[Bind (\"" + getter + "\")] get;" : "get;",
278
289
 
279
290
        public static string AsMethod (string msg)
280
291
        {
 
292
                if (msg.Length == 0)
 
293
                        return msg;
 
294
                
281
295
                return Char.ToUpper (msg [0]) + msg.Substring (1);
282
296
        }
283
297
        
296
310
                                for (; i < sig.Length; i++){
297
311
                                        c = sig [i];
298
312
                                        if (c == ')'){
 
313
                                                for (++i; i < sig.Length && Char.IsWhiteSpace (sig [i]); i++)
 
314
                                                        ;
 
315
 
299
316
                                                for (++i; i < sig.Length; i++){
300
317
                                                        if (!Char.IsLetterOrDigit (sig [i]))
301
318
                                                                break;
312
329
        enum State {
313
330
                SkipToType,
314
331
                EndOfType,
 
332
                SkipToParameter,
315
333
                Parameter,
316
 
                        
317
334
        }
318
335
        
319
336
        string MakeParameters (string sig)
320
337
        {
321
 
                //Console.WriteLine ("Making Parameters: [{0}]", sig);
322
338
                int colon = sig.IndexOf (':');
323
339
                if (colon == -1)
324
340
                        return "";
340
356
                                break;
341
357
                        case State.EndOfType:
342
358
                                if (c == ')'){
343
 
                                        state = State.Parameter;
 
359
                                        state = State.SkipToParameter;
344
360
                                        sb.Append (RemapType (tsb.ToString ()));
345
361
                                        sb.Append (' ');
346
362
                                } else {
348
364
                                                tsb.Append (c);
349
365
                                }
350
366
                                break;
351
 
                                
 
367
 
 
368
                        case State.SkipToParameter:
 
369
                                if (!Char.IsWhiteSpace (c)){
 
370
                                        state = State.Parameter;
 
371
                                        sb.Append (c);
 
372
                                }
 
373
                                break;
352
374
                        case State.Parameter:
353
375
                                if (Char.IsWhiteSpace (c)){
354
376
                                        state = State.SkipToType;
393
415
                        return "uint";
394
416
                case "NSUInteger":
395
417
                        return "uint";
 
418
                case "instancetype":
396
419
                case "id":
397
420
                        return "NSObject";
398
421
                case "BOOL":
404
427
                        return "NSUrl";
405
428
                case "NSTimeInterval":
406
429
                        return "double";
 
430
                case "dispatch_queue_t":
 
431
                        return "DispatchQueue";
 
432
                case "SCNVector4":
 
433
                        return "Vector4";
 
434
                case "SCNVector3":
 
435
                        return "Vector3";
407
436
                }
408
437
                return type;
409
438
        }
410
439
        
411
 
        Regex rx = new Regex ("(NS_AVAILABLE\\(.*\\)|NS_AVAILABLE_MAC\\([0-9_]+\\))");
 
440
        Regex rx = new Regex ("(NS_AVAILABLE\\(.*\\)|NS_AVAILABLE_IOS\\([0-9_]+\\)|NS_AVAILABLE_MAC\\([0-9_]+\\)|__OSX_AVAILABLE_STARTING\\([_A-Z0-9,]+\\))");
412
441
        Regex rx2 = new Regex ("AVAILABLE_MAC_OS_X_VERSION[_A-Z0-9]*");
413
442
        Regex rx3 = new Regex ("AVAILABLE_MAC_OS_X_VERSION[_A-Z0-9]*");
 
443
        Regex rx4 = new Regex ("UI_APPEARANCE_SELECTOR");
414
444
        
415
445
        string CleanDeclaration (string line)
416
446
        {
417
 
                return rx3.Replace (rx2.Replace (rx.Replace (line, ""), ""), "");
 
447
                return rx4.Replace (rx3.Replace (rx2.Replace (rx.Replace (line, ""), ""), ""), "");
418
448
        }
419
449
 
420
450
        public static string CleanSelector (string selector)
429
459
        
430
460
        Declaration ProcessDeclaration (bool isProtocol, string line, bool is_optional)
431
461
        {
 
462
                bool debug = false;
 
463
                if (line.IndexOf ("6_0") != -1)
 
464
                        debug = true;
 
465
 
432
466
                if (limit != null){
433
467
                        if (!HasLimitKeyword (line))
434
468
                                return null;
436
470
                        if (line.IndexOf (limit) == -1)
437
471
                                return null;
438
472
                }
439
 
                
 
473
 
 
474
                var appearance = (line.IndexOf ("UI_APPEARANCE_SELECTOR") != -1);
440
475
                line = CleanDeclaration (line);
441
476
                if (line.Length == 0)
442
477
                        return null;
447
482
                        if (is_abstract)
448
483
                                gencs.WriteLine ("\t\t[Abstract]");
449
484
 
450
 
                        ProcessProperty (line);
 
485
                        ProcessProperty (line, appearance);
451
486
                        return null;
452
487
                }
453
488
                //Console.WriteLine ("PROCESSING: {0}", line);
460
495
                //Console.WriteLine ("->{0}\np={1} q-p={2}", line, p, q-p);
461
496
                string retval = RemapType (line.Substring (p+1, q-p-1));
462
497
                p = line.IndexOf (';');
463
 
                string signature = line.Substring (q+1, p-q);
 
498
                string signature = line.Substring (q+1, p-q).Trim (new char [] { ' ', ';' });
 
499
                //Console.WriteLine ("SIG: {0} {1}", line, p);
464
500
                string selector = MakeSelector (signature);
465
501
                string parameters = MakeParameters (signature);
466
502
 
467
503
                //Console.WriteLine ("signature: {0}", signature);
468
504
                //Console.WriteLine ("selector: {0}", selector);
469
 
                return new Declaration (selector, retval, parameters, is_abstract, is_static);
 
505
                return new Declaration (selector, retval, parameters, is_abstract, is_static, appearance);
470
506
        }
471
507
        
472
508
        void ProcessInterface (string iface)
477
513
 
478
514
                //Console.WriteLine ("**** {0} ", iface);
479
515
                types.Add (cols [1]);
 
516
                if (extraAttribute != null)
 
517
                        gencs.Write ("\n\t[{0}]", extraAttribute);
480
518
                if (cols.Length >= 4)
481
519
                        gencs.WriteLine ("\n\t[BaseType (typeof ({0}))]", cols [3]);
482
520
                gencs.WriteLine ("\t{0}interface {1} {{", limit == null ? "" : "public partial ", cols [1]);
483
 
                
484
 
                while ((line = r.ReadLine ()) != null && (need_close && !line.StartsWith ("}"))){
485
 
                        if (line == "{")
486
 
                                need_close = true;
487
 
                }
488
 
                        
 
521
 
 
522
                //while ((line = r.ReadLine ()) != null && (need_close && !line.StartsWith ("}"))){
 
523
                //      if (line == "{")
 
524
                //              need_close = true;
 
525
                //}
 
526
                line = r.ReadLine ();
 
527
                if (line == "{")
 
528
                        need_close = true;
 
529
                while (line != null && (need_close && line != "}"))
 
530
                        line = r.ReadLine ();
 
531
 
489
532
                var decl = new Declarations (gencs);
490
533
                while ((line = r.ReadLine ()) != null && !line.StartsWith ("@end")){
491
534
                        string full = "";
500
543
                        }
501
544
                        break;
502
545
                }
503
 
                decl.Generate ();
 
546
                decl.Generate (extraAttribute);
504
547
                gencs.WriteLine ("\t}");
505
548
        }
506
549
 
510
553
                string line;
511
554
 
512
555
                types.Add (d [1]);
 
556
                if (extraAttribute != null)
 
557
                        gencs.WriteLine ("\n\t[{0}]", extraAttribute);
513
558
                gencs.WriteLine ("\n\t[BaseType (typeof ({0}))]", d.Length > 2 ? d [2] : "NSObject");
514
559
                gencs.WriteLine ("\t[Model]");
515
560
                gencs.WriteLine ("\tinterface {0} {{", d [1]);
532
577
                        if (line.StartsWith ("@end"))
533
578
                                break;
534
579
                }
535
 
                decl.Generate ();
 
580
                decl.Generate (extraAttribute);
536
581
                gencs.WriteLine ("\t}");
537
582
        }
538
583
 
542
587
                
543
588
                Environment.Exit (0);
544
589
        }
 
590
 
 
591
        string Clean (string prefix, string line)
 
592
        {
 
593
                return line.Substring (line.IndexOf (prefix));
 
594
        }
545
595
        
546
596
        TrivialParser ()
547
597
        {
 
598
#if false
548
599
                try {
549
600
                        gencs = File.CreateText ("gen.cs");
550
601
                } catch {
556
607
                } catch {
557
608
                        other = File.CreateText ("/tmp/other.c");
558
609
                }
559
 
                
 
610
#endif
 
611
                gencs = Console.Out;
 
612
                other = new StringWriter ();
 
613
 
560
614
                options = new OptionSet () {
561
615
                        { "limit=", "Limit methods to methods for the specific API level (ex: 5_0)", arg => limit = arg },
 
616
                        { "extra=", "Extra attribute to add, for example: 'Since(6,0)'", arg => extraAttribute = arg },
562
617
                        { "help", "Shows the help", a => ShowHelp () }
563
618
                };
564
619
        }
597
652
                                                line = line.Substring (p);
598
653
                                        }
599
654
                                        
600
 
                                        if (line.StartsWith ("@interface"))
601
 
                                                ProcessInterface (line);
602
 
                                        if (line.StartsWith ("@protocol") && !line.EndsWith (";")) // && line.IndexOf ("<") != -1)
603
 
                                                ProcessProtocol (line);
 
655
                                        if (line.IndexOf ("@interface") != -1)
 
656
                                                ProcessInterface (Clean ("@interface", line));
 
657
                                        if (line.IndexOf ("@protocol") != -1 && !line.EndsWith (";")) // && line.IndexOf ("<") != -1)
 
658
                                                ProcessProtocol (Clean ("@protocol", line));
604
659
                                        
605
660
                                        other.WriteLine (line);
606
661
                                }
607
662
                        }
608
663
                }
609
 
                foreach (string s in types){
610
 
                        Console.WriteLine ("\t\ttypeof ({0}),", s);
611
 
                }
612
664
                gencs.Close ();
613
665
                other.Close ();
614
666
        }