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

« back to all changes in this revision

Viewing changes to src/addins/AspNet/MonoDevelop.AspNet.Mvc/StateEngine/RazorCodeFragmentState.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
ļ»æ//
 
2
// RazorCodeFragmentState.cs
 
3
//
 
4
// Author:
 
5
//              Piotr Dowgiallo <sparekd@gmail.com>
 
6
//
 
7
// Copyright (c) 2012 Piotr Dowgiallo
 
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
using System.Text;
 
29
using MonoDevelop.Xml.StateEngine;
 
30
using MonoDevelop.AspNet.StateEngine;
 
31
using MonoDevelop.AspNet.Mvc.Parser;
 
32
 
 
33
namespace MonoDevelop.AspNet.Mvc.StateEngine
 
34
{
 
35
        public abstract class RazorCodeFragmentState : RazorState
 
36
        {
 
37
                protected const int NONE = 0;
 
38
                protected const int BRACKET = 1;
 
39
                protected const int SOLIDUS = 2;
 
40
                protected const int INSIDE_PARENTHESES = 3;
 
41
                protected const int TRANSITION = 4;
 
42
                protected const int MAXCONST = TRANSITION;
 
43
 
 
44
                char previousChar;
 
45
                protected StringBuilder bracketsBuilder;
 
46
 
 
47
                HtmlTagState htmlTagState;
 
48
                HtmlClosingTagState htmlClosingTagState;
 
49
                RazorCommentState razorCommentState;
 
50
                RazorSpeculativeState speculativeState;
 
51
                RazorExpressionState expressionState;
 
52
                RazorCodeBlockState childBlockState;
 
53
 
 
54
                public RazorCodeFragment CorrespondingBlock { get; set; }
 
55
                public bool IsInsideParentheses { get; protected set; }
 
56
                public bool IsInsideGenerics { get; protected set; }
 
57
 
 
58
                public RazorCodeFragmentState ()
 
59
                        : this (new HtmlTagState (true), new HtmlClosingTagState (true),
 
60
                        new RazorCommentState (), new RazorExpressionState (), new RazorSpeculativeState ())
 
61
                {
 
62
                }
 
63
 
 
64
                public RazorCodeFragmentState (HtmlTagState html, HtmlClosingTagState htmlClosing,
 
65
                        RazorCommentState comment, RazorExpressionState expression, RazorSpeculativeState speculative)
 
66
                {
 
67
                        htmlTagState = html;
 
68
                        htmlClosingTagState = htmlClosing;
 
69
                        razorCommentState = comment;
 
70
                        expressionState = expression;
 
71
                        speculativeState = speculative;
 
72
 
 
73
                        Adopt (htmlTagState);
 
74
                        Adopt (htmlClosingTagState);
 
75
                        Adopt (razorCommentState);
 
76
                        Adopt (expressionState);
 
77
                        Adopt (speculativeState);
 
78
 
 
79
                        bracketsBuilder = new StringBuilder ();
 
80
                }
 
81
 
 
82
                public override State PushChar (char c, IParseContext context, ref string rollback)
 
83
                {
 
84
                        switch (context.StateTag) {
 
85
                        case SOLIDUS:
 
86
                                if (Char.IsLetter (c)) {
 
87
                                        rollback = String.Empty;
 
88
                                        return htmlClosingTagState;
 
89
                                }
 
90
                                context.StateTag = NONE;
 
91
                                break;
 
92
 
 
93
                        case BRACKET:
 
94
                                if (Char.IsLetter (c)) {
 
95
                                        rollback = String.Empty;
 
96
                                        return htmlTagState;
 
97
                                }
 
98
                                else if (c == '/')
 
99
                                        context.StateTag = SOLIDUS;
 
100
                                else
 
101
                                        context.StateTag = NONE;
 
102
                                break;
 
103
 
 
104
                        case TRANSITION:
 
105
                                rollback = String.Empty;
 
106
                                switch (c) {
 
107
                                case '{':
 
108
                                        return EnsureSetAndAdopted<RazorCodeBlockState> (ref childBlockState);
 
109
                                case '*':
 
110
                                        return razorCommentState;
 
111
                                case '(':
 
112
                                        return expressionState;
 
113
                                default:
 
114
                                        if (context.CurrentStateLength <= 2 || (!Char.IsLetterOrDigit (previousChar) 
 
115
                                                        && (Char.IsLetter (c) || c == '_')))
 
116
                                                return speculativeState;
 
117
                                        else
 
118
                                                context.StateTag = NONE;
 
119
                                        break;
 
120
                                }
 
121
 
 
122
                                break;
 
123
 
 
124
                        case INSIDE_PARENTHESES:
 
125
                                if (c == '(')
 
126
                                        context.KeywordBuilder.Append (c);
 
127
                                else if (c == ')') {
 
128
                                        if (context.KeywordBuilder.Length > 0)
 
129
                                                context.KeywordBuilder.Remove (0, 1);
 
130
                                        if (context.KeywordBuilder.Length == 0) {
 
131
                                                context.StateTag = NONE;
 
132
                                                IsInsideParentheses = false;
 
133
                                        }
 
134
                                }
 
135
                                break;
 
136
 
 
137
                        case NONE:
 
138
                                switch (c) {
 
139
                                case '(':
 
140
                                        context.KeywordBuilder.Append (c);
 
141
                                        context.StateTag = INSIDE_PARENTHESES;
 
142
                                        IsInsideParentheses = true;
 
143
                                        break;
 
144
 
 
145
                                case '<':
 
146
                                        if (context.Nodes.Peek () is XElement || !Char.IsLetterOrDigit (previousChar)) {
 
147
                                                context.StateTag = BRACKET;
 
148
                                                IsInsideGenerics = false;
 
149
                                        } else
 
150
                                                IsInsideGenerics = true;
 
151
                                        break;
 
152
 
 
153
                                case '>':
 
154
                                        IsInsideGenerics = false;
 
155
                                        break;
 
156
 
 
157
                                case '@':
 
158
                                        context.StateTag = TRANSITION;
 
159
                                        break;
 
160
 
 
161
                                default:
 
162
                                        previousChar = c;
 
163
                                        break;
 
164
                                }
 
165
 
 
166
                                break;
 
167
                        }
 
168
 
 
169
                        return null;
 
170
                }
 
171
 
 
172
                // ParseOpeningBracket and ParseClosingBracket can use simplified method of tracking brackets.
 
173
                // It's fast, and works correctly when parsing char by char, but sometimes may incorrectly determine
 
174
                // the end of a block in nested subblocks when we click inside the block from another one,
 
175
                // because the parent block isn't parsed from end to end then.
 
176
                // The simplified version is used mostly for testing. In real environment finding matching brackets
 
177
                // precisely is necessary for code completion.
 
178
 
 
179
                public virtual State ParseOpeningBracket (char c, IParseContext context)
 
180
                {
 
181
                        var rootState = RootState as RazorFreeState;
 
182
                        if (!rootState.UseSimplifiedBracketTracker && !CorrespondingBlock.FirstBracket.HasValue)
 
183
                                CorrespondingBlock.FindFirstBracket (context.Location);
 
184
                        else if (rootState.UseSimplifiedBracketTracker)
 
185
                                bracketsBuilder.Append (c);
 
186
                        return null;
 
187
                }
 
188
 
 
189
                public virtual State ParseClosingBracket<T> (char c, IParseContext context, State stateToReturn) where T : XNode
 
190
                {
 
191
                        bool isEnding = false;
 
192
                        var rootState = RootState as RazorFreeState;
 
193
                        if (rootState.UseSimplifiedBracketTracker) {
 
194
                                if (bracketsBuilder.Length > 0)
 
195
                                        bracketsBuilder.Remove (0, 1);
 
196
                                if (bracketsBuilder.Length == 0)
 
197
                                        isEnding = true;
 
198
                        }
 
199
                        else if (!rootState.UseSimplifiedBracketTracker && CorrespondingBlock.IsEndingBracket (context.LocationMinus (1)))
 
200
                                isEnding = true;
 
201
 
 
202
                        if (isEnding) {
 
203
                                StateEngineService.EndCodeFragment<T> (context);
 
204
                                return stateToReturn;
 
205
                        }
 
206
 
 
207
                        return null;
 
208
                }
 
209
        }
 
210
}