File: FixUpCollection.cs

package info (click to toggle)
mono 4.6.2.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (727 lines) | stat: -rw-r--r-- 26,308 bytes parent folder | download
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
//---------------------------------------------------------------------
// <copyright file="FixUpCollection.cs" company="Microsoft">
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//
// @owner       [....]
// @backupOwner [....]
//---------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Data;
using System.Data.EntityModel.SchemaObjectModel;
using System.Data.Entity.Design;

namespace System.Data.EntityModel.Emitters
{
    internal sealed class FixUpCollection : List<FixUp>
    {
        #region Private Types
        private enum CSDeclType
        {
            Method,
            Property,
            Other,
        }
        public enum VBStatementType
        {
            BeginClass,
            EndClass,
            BeginProperty,
            EndProperty,
            BeginMethod,
            EndMethod,
            BeginPropertyGetter,
            EndPropertyGetter,
            BeginPropertySetter,
            EndPropertySetter,
            Other,
        }
        #endregion

        #region Instance Fields
        private Dictionary<string,List<FixUp>> _classFixUps = null;
        private LanguageOption _language;
        #endregion

        #region Static Fields
        static readonly char[] _CSEndOfClassDelimiters = new char[] { ' ',':' };
        const string _CSClassKeyWord = " class ";
        static readonly char[] _CSFieldMarkers = new char[] { '=',';' };
        static readonly char[] _VBEndOfClassDelimiters = new char[] { ' ', '(' };
        static readonly char[] _VBNonDeclMarkers = new char[] { '=', '"', '\'' };
        #endregion

        #region Public Methods
        /// <summary>
        /// 
        /// </summary>
        public FixUpCollection()
        {
        }

        public static bool IsLanguageSupported(LanguageOption language)
        {
            switch ( language )
            {
                case LanguageOption.GenerateVBCode:
                case LanguageOption.GenerateCSharpCode:
                    return true;
            }
            return false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="writer"></param>
        /// <param name="language"></param>
        public void Do(System.IO.TextReader reader, System.IO.TextWriter writer, LanguageOption language, bool hasNamespace)
        {
            Language = language;

            // set up the fix ups for each class.
            foreach ( FixUp fixUp in this )
            {
                List<FixUp> fixUps = null;
                if ( ClassFixUps.ContainsKey(fixUp.Class) )
                {
                    fixUps = ClassFixUps[fixUp.Class];
                }
                else
                {
                    fixUps = new List<FixUp>();
                    ClassFixUps.Add(fixUp.Class,fixUps);
                }
                fixUps.Add(fixUp);
            }

            switch ( Language )
            {
                case LanguageOption.GenerateVBCode:
                    DoFixUpsForVB(reader, writer);
                    break;
                case LanguageOption.GenerateCSharpCode:
                    DoFixUpsForCS(reader, writer, hasNamespace);
                    break;
                default:
                    Debug.Assert(false,"Unexpected language value: "+Language.ToString());
                    CopyFile(reader,writer);
                    break;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="writer"></param>
        private static void CopyFile(System.IO.TextReader reader, System.IO.TextWriter writer)
        {
            string line;
            while ( (line=reader.ReadLine()) != null )
                writer.WriteLine(line);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="writer"></param>
        private void DoFixUpsForCS(System.IO.TextReader reader, System.IO.TextWriter writer, bool hasNamespace)
        {
            int braceCount = 0;
            string line;
            string trimmedLine;
            string currentOuterClass = null;
            string className;
            bool classWanted = false;
            FixUp getterFixUp = null;
            FixUp setterFixUp = null;
            int nameSpaceLevel = hasNamespace ? 1 : 0;
            while ( (line=reader.ReadLine()) != null )
            {
                trimmedLine = line.Trim();
                if ( trimmedLine == "{" )
                    ++braceCount;
                else if ( trimmedLine == "}" )
                {
                    --braceCount;
                    if (braceCount < nameSpaceLevel + 2)
                    {
                        setterFixUp = null;
                        if (braceCount < nameSpaceLevel + 1)
                        {
                            currentOuterClass = null;
                            classWanted = false;
                        }
                    }
                }
                else if ( string.IsNullOrEmpty(trimmedLine) || trimmedLine.StartsWith("//",StringComparison.Ordinal) )
                {
                    // comment, just emit as is....
                }
                else if ( IsCSClassDefinition(line,out className) )
                {
                    if (braceCount == nameSpaceLevel)
                    {
                        currentOuterClass = className;
                        className = null;
                        classWanted = IsClassWanted(currentOuterClass);
                        if ( classWanted )
                            line = FixUpClassDecl(currentOuterClass,line);
                    }
                }
                else if ( classWanted )
                {
                    //we only care about methods/properties in top level classes
                    if (braceCount == nameSpaceLevel + 1)
                    {
                        string name;
                        switch ( GetCSDeclType(trimmedLine,out name) )
                        {
                            case CSDeclType.Method:
                                line = FixUpMethodDecl(currentOuterClass,name,line);
                                break;
                            case CSDeclType.Property:
                                setterFixUp = FixUpSetter(currentOuterClass, name);
                                getterFixUp = FixUpGetter(currentOuterClass, name);
                                break;
                        }
                    }
                    else if (braceCount == nameSpaceLevel + 2)
                    {
                        if (trimmedLine == "set" && setterFixUp != null)
                        {
                            line = setterFixUp.Fix(LanguageOption.GenerateCSharpCode, line);
                            setterFixUp = null;
                        }
                        else if (trimmedLine == "get" && getterFixUp != null)
                        {
                            line = getterFixUp.Fix(LanguageOption.GenerateCSharpCode, line);
                            getterFixUp = null;
                        }
                    }
                }
                writer.WriteLine(line);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="writer"></param>
        public void DoFixUpsForVB(System.IO.TextReader reader, System.IO.TextWriter writer)
        {
            Language = LanguageOption.GenerateVBCode;

            string line;
            Stack<VBStatementType> context = new Stack<VBStatementType>();
            int classDepth = 0;
            string currentOuterClass = null;
            bool classWanted = false;
            FixUp getterFixUp = null;
            FixUp setterFixUp = null;
            while ( (line=reader.ReadLine()) != null )
            {
                if ( line == null || line.Length == 0 || line[0] == '\'' )
                {
                    // empty line or comment, ouput as is
                }
                else
                {
                    string name;
                    switch ( GetVBStatementType(context, line, out name) )
                    {
                        case VBStatementType.BeginClass:
                            ++classDepth;
                            setterFixUp = null;
                            if ( classDepth == 1 )
                            {
                                currentOuterClass = name;
                                classWanted = IsClassWanted(name);
                                if ( classWanted )
                                    line = FixUpClassDecl(currentOuterClass, line);
                            }
                            break;

                        case VBStatementType.EndClass:
                            --classDepth;
                            if (classDepth == 0)
                            {
                                currentOuterClass = null;
                            }
                            break;

                        case VBStatementType.BeginProperty:
                            if (classWanted)
                            {
                                getterFixUp = FixUpGetter(currentOuterClass, name);
                                setterFixUp = FixUpSetter(currentOuterClass, name);
                            }
                            else
                            {
                                getterFixUp = null;
                                setterFixUp = null;
                            }
                            break;

                        case VBStatementType.EndProperty:
                            getterFixUp = null;
                            setterFixUp = null;
                            break;

                        case VBStatementType.BeginMethod:
                            if (classWanted)
                            {
                                line = FixUpMethodDecl(currentOuterClass, name, line);
                            }
                            break;

                        case VBStatementType.BeginPropertySetter:
                            if (setterFixUp != null)
                            {
                                line = setterFixUp.Fix(Language, line);
                            }
                            setterFixUp = null;
                            break;

                        case VBStatementType.BeginPropertyGetter:
                            if (getterFixUp != null)
                            {
                                line = getterFixUp.Fix(Language, line);
                            }
                            getterFixUp = null;
                            break;
                    }
                }
                writer.WriteLine(line);
            }
        }

        #endregion

        #region Private Methods
        /// <summary>
        /// 
        /// </summary>
        /// <param name="className"></param>
        /// <returns></returns>
        private bool IsClassWanted(string className)
        {
            return ClassFixUps.ContainsKey(className);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="line"></param>
        /// <param name="className"></param>
        /// <returns></returns>
        private static bool IsCSClassDefinition(string line,out string className)
        {
            int index = line.IndexOf(_CSClassKeyWord,StringComparison.Ordinal);
            if ( index < 0 )
            {
                className = null;
                return false;
            }
            index += _CSClassKeyWord.Length;
            int end = line.IndexOfAny(_CSEndOfClassDelimiters, index);
            if ( end < 0 )
                className = line.Substring(index);
            else
                className = line.Substring(index,end-index);


            if (className.StartsWith("@", StringComparison.Ordinal))
            {
                // remove the escaping mechanisim for C# keywords
                className = className.Substring(1);
            }
            
            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="className"></param>
        /// <param name="line"></param>
        /// <returns></returns>
        private string FixUpClassDecl(string className,string line)
        {
            IList<FixUp> fixUps = ClassFixUps[className];
            foreach ( FixUp fixUp in fixUps )
            {
                if ( fixUp.Type == FixUpType.MarkClassAsStatic )
                {
                    return fixUp.Fix(Language,line);
                }
            }
            return line;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="line"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        private static CSDeclType GetCSDeclType(string line, out string name)
        {
            // we know we're at the class member level.
            // things we could encounter are (limited to things we actually emit):
            //    nested classes (already identified)
            //    attributes
            //    fields
            //    methods
            //    properties

            name = null;

            //Attributes
            if (line[0] == '[')
                return CSDeclType.Other;

            // Methods have ( and ) without a =
            int parIdx1 = line.IndexOf('(');
            int parIdx2 = line.IndexOf(')');
            int equIdx = line.IndexOf('='); //return -1 for absent equal sign.

            if (equIdx == -1 && parIdx1 >= 0 && parIdx2 > parIdx1)
            {
                line = line.Substring(0, parIdx1).TrimEnd(null);
                name = line.Substring(line.LastIndexOf(' ') + 1);
                return CSDeclType.Method;
            }

            //we assume fields have = or ;
            if (line.IndexOfAny(_CSFieldMarkers, 0) >= 0)
                return CSDeclType.Other;

            //Properties
            CSDeclType declType = CSDeclType.Property;
            name = line.Substring(line.LastIndexOf(' ') + 1);
            return declType;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="className"></param>
        /// <param name="methodName"></param>
        /// <param name="line"></param>
        /// <returns></returns>
        private string FixUpMethodDecl(string className,string methodName,string line)
        {
            IList<FixUp> fixUps = ClassFixUps[className];
            foreach ( FixUp fixUp in fixUps )
            {
                if ( fixUp.Method == methodName && 
                    (fixUp.Type == FixUpType.MarkOverrideMethodAsSealed || fixUp.Type == FixUpType.MarkAbstractMethodAsPartial) )
                {
                    return fixUp.Fix(Language,line);
                }
            }
            return line;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="className"></param>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        private FixUp FixUpSetter(string className,string propertyName)
        {
            IList<FixUp> fixUps = ClassFixUps[className];
            foreach ( FixUp fixUp in fixUps )
            {
                if (fixUp.Property == propertyName &&
                    (fixUp.Type == FixUpType.MarkPropertySetAsPrivate ||
                     fixUp.Type == FixUpType.MarkPropertySetAsInternal ||
                     fixUp.Type == FixUpType.MarkPropertySetAsPublic ||
                     fixUp.Type == FixUpType.MarkPropertySetAsProtected))
                {
                    return fixUp;
                }
            }
            return null;
        }

        private FixUp FixUpGetter(string className, string propertyName)
        {
            IList<FixUp> fixUps = ClassFixUps[className];
            foreach (FixUp fixUp in fixUps)
            {
                if (fixUp.Property == propertyName && 
                    (fixUp.Type == FixUpType.MarkPropertyGetAsPrivate ||
                     fixUp.Type == FixUpType.MarkPropertyGetAsInternal ||
                     fixUp.Type == FixUpType.MarkPropertyGetAsPublic ||
                     fixUp.Type == FixUpType.MarkPropertyGetAsProtected))
                {
                    return fixUp;
                }
            }
            return null;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="line"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        private static VBStatementType GetVBStatementType(Stack<VBStatementType> context, string line, out string name)
        {
            name = null;
            VBStatementType current = VBStatementType.Other;

            // if the statement constains ", =, or... then it's not a statement type we care about
            if ( line.IndexOfAny(_VBNonDeclMarkers) >= 0 )
                return current;

            
            string normalizedLine = NormalizeForVB(line);

            if ( context.Count <= 0 )
            {
                // without context we only accept BeginClass
                if ( LineIsVBBeginClassMethodProperty(normalizedLine, "Class", ref name) )
                {
                    current = VBStatementType.BeginClass;
                    context.Push(current);
                }
            }
            else
            {
                // we only look for things based on context:
                switch ( context.Peek() )
                {
                    // at BeginClass we only accept 
                    //    BeginClass
                    //    EndClass
                    //    BeginProperty
                    //    BeginMethod
                    case VBStatementType.BeginClass:
                        if ( normalizedLine == "End Class" )
                        {
                            current = VBStatementType.EndClass;
                            context.Pop();
                        }
                        else
                        {
                            if ( LineIsVBBeginClassMethodProperty(normalizedLine, "Class", ref name) )
                            {
                                current = VBStatementType.BeginClass;
                                context.Push(current);
                            }
                            else if ( LineIsVBBeginClassMethodProperty(normalizedLine, "MustOverride Sub", ref name) )
                            {
                                // Abstract methods do not have an "End Sub", this don't push the context.
                                current = VBStatementType.BeginMethod;
                            }
                            else if ( LineIsVBBeginClassMethodProperty(normalizedLine, "Function", ref name) 
                                || LineIsVBBeginClassMethodProperty(normalizedLine, "Sub", ref name) )
                            {
                                current = VBStatementType.BeginMethod;
                                context.Push(current);
                            }
                            else if ( LineIsVBBeginClassMethodProperty(normalizedLine, "Property", ref name) )
                            {
                                current = VBStatementType.BeginProperty;
                                context.Push(current);
                            }
                        }
                        break;

                    // at BeginProperty we only accept
                    //    EndProperty
                    //    BeginPropertyGetter
                    //    BeginPropertySetter
                    case VBStatementType.BeginProperty:
                        if ( normalizedLine == "End Property" )
                        {
                            current = VBStatementType.EndProperty;
                            context.Pop();
                        }
                        else
                        {
                            if ( LineIsVBBeginSetterGetter(normalizedLine, "Get") )
                            {
                                current = VBStatementType.BeginPropertyGetter;
                                context.Push(current);
                            }
                            else if ( LineIsVBBeginSetterGetter(normalizedLine, "Set") )
                            {
                                current = VBStatementType.BeginPropertySetter;
                                context.Push(current);
                            }
                        }
                        break;

                    // at BeginMethod we only accept
                    //    EndMethod
                    case VBStatementType.BeginMethod:
                        if ( normalizedLine == "End Sub" || normalizedLine == "End Function" )
                        {
                            current = VBStatementType.EndMethod;
                            context.Pop();
                        }
                        break;

                    // at BeginPropertyGetter we only accept
                    //    EndPropertyGetter
                    case VBStatementType.BeginPropertyGetter:
                        if ( normalizedLine == "End Get" )
                        {
                            current = VBStatementType.EndPropertyGetter;
                            context.Pop();
                        }
                        break;

                    // at BeginPropertySetter we only accept
                    //    EndPropertySetter
                    case VBStatementType.BeginPropertySetter:
                        if ( normalizedLine == "End Set" )
                        {
                            current = VBStatementType.EndPropertySetter;
                            context.Pop();
                        }
                        break;
                }
            }

            return current;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="line"></param>
        /// <returns></returns>
        private static string NormalizeForVB(string line)
        {
            // no leading or trailing spaces and tabs are replaced with spaces
            line = line.Replace('\t', ' ').Trim();

            // consecutuve spaces are replaced with single spaces...
            // (we don't care about hammering strings; we just use the normalized line for statment identification...
            while ( line.IndexOf("  ", 0,StringComparison.Ordinal) >= 0 )
                line = line.Replace("  ", " ");

            return line;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="line"></param>
        /// <param name="keyword"></param>
        /// <returns></returns>
        private static bool LineIsVBBeginSetterGetter(string line, string keyword)
        {
            return IndexOfKeyword(line, keyword) >= 0;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="line"></param>
        /// <param name="keyword"></param>
        /// <returns></returns>
        private static int IndexOfKeyword(string line, string keyword)
        {
            int index = line.IndexOf(keyword,StringComparison.Ordinal);
            if ( index < 0 )
                return index;

            char ch;
            int indexAfter = index+keyword.Length;
            if ( (index == 0 || char.IsWhiteSpace(line, index-1)) && (indexAfter == line.Length || (ch=line[indexAfter]) == '(' || char.IsWhiteSpace(ch)) )
                return index;

            return -1;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="line"></param>
        /// <param name="keyword"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        private static bool LineIsVBBeginClassMethodProperty(string line, string keyword, ref string name)
        {
            // line must contain the keyword
            int index = IndexOfKeyword(line, keyword);
            if ( index < 0 )
                return false;

            // after the keyword we expact a space and the name
            index += keyword.Length;
            if ( index >= line.Length || !char.IsWhiteSpace(line, index) )
                return false;
            ++index;
            if ( index >= line.Length )
                return false;

            // after the name we expect a EOL or a delimiter...
            int end = line.IndexOfAny(_VBEndOfClassDelimiters, index);
            if ( end < 0 )
                end = line.Length;

            name = line.Substring(index, end-index).Trim();

            if (name.StartsWith("[", StringComparison.Ordinal) && name.EndsWith("]", StringComparison.Ordinal))
            {
                // remove the vb keyword escaping mechanisim
                name = name.Substring(1, name.Length - 2);
            }
            
            return true;
        }
        #endregion

        #region Private Properties
        /// <summary>
        /// 
        /// </summary>
        private LanguageOption Language
        {
            get
            {
                return _language;
            }
            set
            {
                _language = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <value></value>
        private Dictionary<string,List<FixUp>> ClassFixUps
        {
            get
            {
                if ( _classFixUps == null )
                {
                    _classFixUps = new Dictionary<string,List<FixUp>>();
                }

                return _classFixUps;
            }
        }
        #endregion
    }
}