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

« back to all changes in this revision

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