~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/Libraries/NRefactory/Project/Src/Lexer/Special/PreProcessingDirective.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
 
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
 
3
 
 
4
using System;
 
5
using System.Collections.Generic;
 
6
 
 
7
namespace ICSharpCode.NRefactory
 
8
{
 
9
        public class PreprocessingDirective : AbstractSpecial
 
10
        {
 
11
                #region Conversion C# <-> VB
 
12
                public static void VBToCSharp(IList<ISpecial> list)
 
13
                {
 
14
                        for (int i = 0; i < list.Count; ++i) {
 
15
                                if (list[i] is PreprocessingDirective)
 
16
                                        list[i] = VBToCSharp((PreprocessingDirective)list[i]);
 
17
                        }
 
18
                }
 
19
                
 
20
                public static PreprocessingDirective VBToCSharp(PreprocessingDirective dir)
 
21
                {
 
22
                        string cmd = dir.Cmd;
 
23
                        string arg = dir.Arg;
 
24
                        if (cmd.Equals("#End", StringComparison.InvariantCultureIgnoreCase)) {
 
25
                                if (arg.ToLowerInvariant().StartsWith("region")) {
 
26
                                        cmd = "#endregion";
 
27
                                        arg = "";
 
28
                                } else if ("if".Equals(arg, StringComparison.InvariantCultureIgnoreCase)) {
 
29
                                        cmd = "#endif";
 
30
                                        arg = "";
 
31
                                }
 
32
                        } else if (cmd.Equals("#Region", StringComparison.InvariantCultureIgnoreCase)) {
 
33
                                cmd = "#region";
 
34
                        } else if (cmd.Equals("#If", StringComparison.InvariantCultureIgnoreCase)) {
 
35
                                cmd = "#if";
 
36
                                if (arg.ToLowerInvariant().EndsWith(" then"))
 
37
                                        arg = arg.Substring(0, arg.Length - 5);
 
38
                        } else if (cmd.Equals("#Else", StringComparison.InvariantCultureIgnoreCase)) {
 
39
                                if (dir.Expression != null)
 
40
                                        cmd = "#elif";
 
41
                                else
 
42
                                        cmd = "#else";
 
43
                        } else if (cmd.Equals("#ElseIf", StringComparison.InvariantCultureIgnoreCase)) {
 
44
                                cmd = "#elif";
 
45
                        }
 
46
                        return new PreprocessingDirective(cmd, arg, dir.StartPosition, dir.EndPosition) {
 
47
                                Expression = dir.Expression
 
48
                        };
 
49
                }
 
50
                
 
51
                public static void CSharpToVB(List<ISpecial> list)
 
52
                {
 
53
                        for (int i = 0; i < list.Count; ++i) {
 
54
                                if (list[i] is PreprocessingDirective)
 
55
                                        list[i] = CSharpToVB((PreprocessingDirective)list[i]);
 
56
                        }
 
57
                }
 
58
                
 
59
                public static PreprocessingDirective CSharpToVB(PreprocessingDirective dir)
 
60
                {
 
61
                        string cmd = dir.Cmd;
 
62
                        string arg = dir.Arg;
 
63
                        switch (cmd) {
 
64
                                case "#region":
 
65
                                        cmd = "#Region";
 
66
                                        if (!arg.StartsWith("\"")) {
 
67
                                                arg = "\"" + arg.Trim() + "\"";
 
68
                                        }
 
69
                                        break;
 
70
                                case "#endregion":
 
71
                                        cmd = "#End";
 
72
                                        arg = "Region";
 
73
                                        break;
 
74
                                case "#endif":
 
75
                                        cmd = "#End";
 
76
                                        arg = "If";
 
77
                                        break;
 
78
                                case "#if":
 
79
                                        arg += " Then";
 
80
                                        break;
 
81
                        }
 
82
                        if (cmd.Length > 1) {
 
83
                                cmd = cmd.Substring(0, 2).ToUpperInvariant() + cmd.Substring(2);
 
84
                        }
 
85
                        return new PreprocessingDirective(cmd, arg, dir.StartPosition, dir.EndPosition) {
 
86
                                Expression = dir.Expression
 
87
                        };
 
88
                }
 
89
                #endregion
 
90
                
 
91
                string cmd;
 
92
                string arg;
 
93
                Ast.Expression expression = Ast.Expression.Null;
 
94
                
 
95
                /// <summary>
 
96
                /// Gets the directive name, including '#'.
 
97
                /// </summary>
 
98
                public string Cmd {
 
99
                        get {
 
100
                                return cmd;
 
101
                        }
 
102
                        set {
 
103
                                cmd = value ?? string.Empty;
 
104
                        }
 
105
                }
 
106
                
 
107
                /// <summary>
 
108
                /// Gets the directive argument.
 
109
                /// </summary>
 
110
                public string Arg {
 
111
                        get {
 
112
                                return arg;
 
113
                        }
 
114
                        set {
 
115
                                arg = value ?? string.Empty;
 
116
                        }
 
117
                }
 
118
                
 
119
                /// <summary>
 
120
                /// Gets/sets the expression (for directives that take an expression, e.g. #if and #elif).
 
121
                /// </summary>
 
122
                public Ast.Expression Expression {
 
123
                        get { return expression; }
 
124
                        set { expression = value ?? Ast.Expression.Null; }
 
125
                }
 
126
                
 
127
                /// <value>
 
128
                /// The end position of the pre processor directive line.
 
129
                /// May be != EndPosition.
 
130
                /// </value>
 
131
                public Location LastLineEnd {
 
132
                        get;
 
133
                        set;
 
134
                }
 
135
                
 
136
                                
 
137
                public override string ToString()
 
138
                {
 
139
                        return String.Format("[PreProcessingDirective: Cmd = {0}, Arg = {1}]",
 
140
                                             Cmd,
 
141
                                             Arg);
 
142
                }
 
143
                
 
144
                public PreprocessingDirective(string cmd, string arg, Location start, Location end)
 
145
                        : base(start, end)
 
146
                {
 
147
                        this.Cmd = cmd;
 
148
                        this.Arg = arg;
 
149
                }
 
150
                
 
151
                public override object AcceptVisitor(ISpecialVisitor visitor, object data)
 
152
                {
 
153
                        return visitor.Visit(this, data);
 
154
                }
 
155
        }
 
156
}