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

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
//
// RazorCodeFragmentState.cs
//
// Author:
//		Piotr Dowgiallo <sparekd@gmail.com>
//
// Copyright (c) 2012 Piotr Dowgiallo
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Text;
using MonoDevelop.Xml.StateEngine;
using MonoDevelop.AspNet.StateEngine;
using MonoDevelop.AspNet.Mvc.Parser;

namespace MonoDevelop.AspNet.Mvc.StateEngine
{
	public abstract class RazorCodeFragmentState : RazorState
	{
		protected const int NONE = 0;
		protected const int BRACKET = 1;
		protected const int SOLIDUS = 2;
		protected const int INSIDE_PARENTHESES = 3;
		protected const int TRANSITION = 4;
		protected const int MAXCONST = TRANSITION;

		char previousChar;
		protected StringBuilder bracketsBuilder;

		HtmlTagState htmlTagState;
		HtmlClosingTagState htmlClosingTagState;
		RazorCommentState razorCommentState;
		RazorSpeculativeState speculativeState;
		RazorExpressionState expressionState;
		RazorCodeBlockState childBlockState;

		public RazorCodeFragment CorrespondingBlock { get; set; }
		public bool IsInsideParentheses { get; protected set; }
		public bool IsInsideGenerics { get; protected set; }

		public RazorCodeFragmentState ()
			: this (new HtmlTagState (true), new HtmlClosingTagState (true),
			new RazorCommentState (), new RazorExpressionState (), new RazorSpeculativeState ())
		{
		}

		public RazorCodeFragmentState (HtmlTagState html, HtmlClosingTagState htmlClosing,
			RazorCommentState comment, RazorExpressionState expression, RazorSpeculativeState speculative)
		{
			htmlTagState = html;
			htmlClosingTagState = htmlClosing;
			razorCommentState = comment;
			expressionState = expression;
			speculativeState = speculative;

			Adopt (htmlTagState);
			Adopt (htmlClosingTagState);
			Adopt (razorCommentState);
			Adopt (expressionState);
			Adopt (speculativeState);

			bracketsBuilder = new StringBuilder ();
		}

		public override State PushChar (char c, IParseContext context, ref string rollback)
		{
			switch (context.StateTag) {
			case SOLIDUS:
				if (Char.IsLetter (c)) {
					rollback = String.Empty;
					return htmlClosingTagState;
				}
				context.StateTag = NONE;
				break;

			case BRACKET:
				if (Char.IsLetter (c)) {
					rollback = String.Empty;
					return htmlTagState;
				}
				else if (c == '/')
					context.StateTag = SOLIDUS;
				else
					context.StateTag = NONE;
				break;

			case TRANSITION:
				rollback = String.Empty;
				switch (c) {
				case '{':
					return EnsureSetAndAdopted<RazorCodeBlockState> (ref childBlockState);
				case '*':
					return razorCommentState;
				case '(':
					return expressionState;
				default:
					if (context.CurrentStateLength <= 2 || (!Char.IsLetterOrDigit (previousChar) 
							&& (Char.IsLetter (c) || c == '_')))
						return speculativeState;
					else
						context.StateTag = NONE;
					break;
				}

				break;

			case INSIDE_PARENTHESES:
				if (c == '(')
					context.KeywordBuilder.Append (c);
				else if (c == ')') {
					if (context.KeywordBuilder.Length > 0)
						context.KeywordBuilder.Remove (0, 1);
					if (context.KeywordBuilder.Length == 0) {
						context.StateTag = NONE;
						IsInsideParentheses = false;
					}
				}
				break;

			case NONE:
				switch (c) {
				case '(':
					context.KeywordBuilder.Append (c);
					context.StateTag = INSIDE_PARENTHESES;
					IsInsideParentheses = true;
					break;

				case '<':
					if (context.Nodes.Peek () is XElement || !Char.IsLetterOrDigit (previousChar)) {
						context.StateTag = BRACKET;
						IsInsideGenerics = false;
					} else
						IsInsideGenerics = true;
					break;

				case '>':
					IsInsideGenerics = false;
					break;

				case '@':
					context.StateTag = TRANSITION;
					break;

				default:
					previousChar = c;
					break;
				}

				break;
			}

			return null;
		}

		// ParseOpeningBracket and ParseClosingBracket can use simplified method of tracking brackets.
		// It's fast, and works correctly when parsing char by char, but sometimes may incorrectly determine
		// the end of a block in nested subblocks when we click inside the block from another one,
		// because the parent block isn't parsed from end to end then.
		// The simplified version is used mostly for testing. In real environment finding matching brackets
		// precisely is necessary for code completion.

		public virtual State ParseOpeningBracket (char c, IParseContext context)
		{
			var rootState = RootState as RazorFreeState;
			if (!rootState.UseSimplifiedBracketTracker && !CorrespondingBlock.FirstBracket.HasValue)
				CorrespondingBlock.FindFirstBracket (context.Location);
			else if (rootState.UseSimplifiedBracketTracker)
				bracketsBuilder.Append (c);
			return null;
		}

		public virtual State ParseClosingBracket<T> (char c, IParseContext context, State stateToReturn) where T : XNode
		{
			bool isEnding = false;
			var rootState = RootState as RazorFreeState;
			if (rootState.UseSimplifiedBracketTracker) {
				if (bracketsBuilder.Length > 0)
					bracketsBuilder.Remove (0, 1);
				if (bracketsBuilder.Length == 0)
					isEnding = true;
			}
			else if (!rootState.UseSimplifiedBracketTracker && CorrespondingBlock.IsEndingBracket (context.LocationMinus (1)))
				isEnding = true;

			if (isEnding) {
				StateEngineService.EndCodeFragment<T> (context);
				return stateToReturn;
			}

			return null;
		}
	}
}