1 | // |
---|
2 | // PrimitiveExpression.cs |
---|
3 | // |
---|
4 | // Author: |
---|
5 | // Mike Krüger <mkrueger@novell.com> |
---|
6 | // |
---|
7 | // Copyright (c) 2009 Novell, Inc (http://www.novell.com) |
---|
8 | // |
---|
9 | // Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
10 | // of this software and associated documentation files (the "Software"), to deal |
---|
11 | // in the Software without restriction, including without limitation the rights |
---|
12 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
13 | // copies of the Software, and to permit persons to whom the Software is |
---|
14 | // furnished to do so, subject to the following conditions: |
---|
15 | // |
---|
16 | // The above copyright notice and this permission notice shall be included in |
---|
17 | // all copies or substantial portions of the Software. |
---|
18 | // |
---|
19 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
20 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
21 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
22 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
23 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
24 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
25 | // THE SOFTWARE. |
---|
26 | |
---|
27 | using System; |
---|
28 | |
---|
29 | namespace ICSharpCode.NRefactory.CSharp |
---|
30 | { |
---|
31 | /// <summary> |
---|
32 | /// Represents a literal value. |
---|
33 | /// </summary> |
---|
34 | public class PrimitiveExpression : Expression |
---|
35 | { |
---|
36 | public static readonly object AnyValue = new object(); |
---|
37 | |
---|
38 | TextLocation startLocation; |
---|
39 | public override TextLocation StartLocation { |
---|
40 | get { |
---|
41 | return startLocation; |
---|
42 | } |
---|
43 | } |
---|
44 | |
---|
45 | internal void SetStartLocation(TextLocation value) |
---|
46 | { |
---|
47 | ThrowIfFrozen(); |
---|
48 | this.startLocation = value; |
---|
49 | this.endLocation = null; |
---|
50 | } |
---|
51 | |
---|
52 | string literalValue; |
---|
53 | TextLocation? endLocation; |
---|
54 | public override TextLocation EndLocation { |
---|
55 | get { |
---|
56 | if (!endLocation.HasValue) { |
---|
57 | endLocation = value is string ? AdvanceLocation (StartLocation, literalValue ?? "") : |
---|
58 | new TextLocation (StartLocation.Line, StartLocation.Column + (literalValue ?? "").Length); |
---|
59 | } |
---|
60 | return endLocation.Value; |
---|
61 | } |
---|
62 | } |
---|
63 | |
---|
64 | object value; |
---|
65 | |
---|
66 | public object Value { |
---|
67 | get { return this.value; } |
---|
68 | set { |
---|
69 | ThrowIfFrozen(); |
---|
70 | this.value = value; |
---|
71 | literalValue = null; |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | /// <remarks>Never returns null.</remarks> |
---|
76 | public string LiteralValue { |
---|
77 | get { return literalValue ?? ""; } |
---|
78 | } |
---|
79 | |
---|
80 | /// <remarks>Can be null.</remarks> |
---|
81 | public string UnsafeLiteralValue { |
---|
82 | get { return literalValue; } |
---|
83 | } |
---|
84 | |
---|
85 | public void SetValue(object value, string literalValue) |
---|
86 | { |
---|
87 | if (value == null) |
---|
88 | throw new ArgumentNullException(); |
---|
89 | ThrowIfFrozen(); |
---|
90 | this.value = value; |
---|
91 | this.literalValue = literalValue; |
---|
92 | } |
---|
93 | |
---|
94 | public PrimitiveExpression (object value) |
---|
95 | { |
---|
96 | this.Value = value; |
---|
97 | this.literalValue = null; |
---|
98 | } |
---|
99 | |
---|
100 | public PrimitiveExpression (object value, string literalValue) |
---|
101 | { |
---|
102 | this.Value = value; |
---|
103 | this.literalValue = literalValue; |
---|
104 | } |
---|
105 | |
---|
106 | public PrimitiveExpression (object value, TextLocation startLocation, string literalValue) |
---|
107 | { |
---|
108 | this.Value = value; |
---|
109 | this.startLocation = startLocation; |
---|
110 | this.literalValue = literalValue; |
---|
111 | } |
---|
112 | |
---|
113 | public override void AcceptVisitor (IAstVisitor visitor) |
---|
114 | { |
---|
115 | visitor.VisitPrimitiveExpression (this); |
---|
116 | } |
---|
117 | |
---|
118 | public override T AcceptVisitor<T> (IAstVisitor<T> visitor) |
---|
119 | { |
---|
120 | return visitor.VisitPrimitiveExpression (this); |
---|
121 | } |
---|
122 | |
---|
123 | public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data) |
---|
124 | { |
---|
125 | return visitor.VisitPrimitiveExpression (this, data); |
---|
126 | } |
---|
127 | |
---|
128 | unsafe static TextLocation AdvanceLocation(TextLocation startLocation, string str) |
---|
129 | { |
---|
130 | int line = startLocation.Line; |
---|
131 | int col = startLocation.Column; |
---|
132 | fixed (char* start = str) { |
---|
133 | char* p = start; |
---|
134 | char* endPtr = start + str.Length; |
---|
135 | while (p < endPtr) { |
---|
136 | var nl = NewLine.GetDelimiterLength(*p, () => { |
---|
137 | char* nextp = p + 1; |
---|
138 | if (nextp < endPtr) |
---|
139 | return *nextp; |
---|
140 | return '\0'; |
---|
141 | }); |
---|
142 | if (nl > 0) { |
---|
143 | line++; |
---|
144 | col = 1; |
---|
145 | if (nl == 2) |
---|
146 | p++; |
---|
147 | } else { |
---|
148 | col++; |
---|
149 | } |
---|
150 | p++; |
---|
151 | } |
---|
152 | } |
---|
153 | return new TextLocation (line, col); |
---|
154 | } |
---|
155 | |
---|
156 | protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) |
---|
157 | { |
---|
158 | PrimitiveExpression o = other as PrimitiveExpression; |
---|
159 | return o != null && (this.Value == AnyValue || object.Equals(this.Value, o.Value)); |
---|
160 | } |
---|
161 | } |
---|
162 | } |
---|