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

« back to all changes in this revision

Viewing changes to src/addins/AspNet/MonoDevelop.AspNet.Mvc/StateEngine/RazorStatementState.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
// RazorStatementState.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.Collections.Generic;
 
29
using System.Linq;
 
30
using MonoDevelop.Xml.StateEngine;
 
31
using System.Diagnostics;
 
32
using MonoDevelop.AspNet.Mvc.Parser;
 
33
 
 
34
namespace MonoDevelop.AspNet.Mvc.StateEngine
 
35
{
 
36
        public class RazorStatementState : RazorCodeFragmentState
 
37
        {
 
38
                protected const int POSSIBLE_CONTINUATION = MAXCONST + 1;
 
39
                protected const int ELSE = POSSIBLE_CONTINUATION + 1;
 
40
 
 
41
                IEnumerable<String> keys;
 
42
                RazorStatementState statementState;
 
43
 
 
44
                public RazorStatement CorrespondingStatement {
 
45
                        get { return CorrespondingBlock as RazorStatement; }
 
46
                }
 
47
 
 
48
                public override State PushChar (char c, IParseContext context, ref string rollback)
 
49
                {
 
50
                        if (context.CurrentStateLength == 1) {
 
51
                                bracketsBuilder.Clear ();
 
52
                                var stm = context.Nodes.FirstOrDefault (n => n is RazorStatement);
 
53
                                if (stm == null) {
 
54
                                        if (context.PreviousState is XmlClosingTagState && CorrespondingStatement != null)
 
55
                                                context.Nodes.Push (CorrespondingStatement);
 
56
                                        else {
 
57
                                                Debug.Fail ("Statement should be pushed before changing the state to StatementState");
 
58
                                                return Parent;
 
59
                                        }
 
60
                                } else
 
61
                                        CorrespondingBlock = stm as RazorCodeFragment;
 
62
                        }
 
63
 
 
64
                        if (context.StateTag == POSSIBLE_CONTINUATION) {
 
65
                                if (!Char.IsWhiteSpace (c))
 
66
                                        context.KeywordBuilder.Append (c);
 
67
 
 
68
                                string currentKey = context.KeywordBuilder.ToString ();
 
69
 
 
70
                                if (!keys.Any (s => s.StartsWith (currentKey))) {
 
71
                                        rollback = String.Empty;
 
72
                                        var p = Parent;
 
73
                                        while (!(p is RazorSpeculativeState))
 
74
                                                p = p.Parent;
 
75
                                        return p.Parent;
 
76
                                }
 
77
 
 
78
                                if (keys.Any (s => s == currentKey)) {
 
79
                                        if (currentKey != "else")
 
80
                                                return SwitchToContinuationStatement (context, currentKey);
 
81
                                        else
 
82
                                                context.StateTag = ELSE;
 
83
                                }
 
84
 
 
85
                                return null;
 
86
                        }
 
87
 
 
88
                        if (context.StateTag == ELSE) {
 
89
                                if (c != ' ') {
 
90
                                        string currentKey = context.KeywordBuilder.ToString ();
 
91
                                        if (c == 'i' && currentKey.Trim () == "else")
 
92
                                                currentKey = "else if";
 
93
                                        return SwitchToContinuationStatement (context, currentKey);
 
94
                                }
 
95
 
 
96
                                context.KeywordBuilder.Append (c);
 
97
                                return null;
 
98
                        }
 
99
 
 
100
                        switch (c) {
 
101
                        case '{':
 
102
                                if (context.StateTag != TRANSITION)
 
103
                                        return ParseOpeningBracket (c, context);
 
104
                                break;
 
105
 
 
106
                        case '}':
 
107
                                return ParseClosingBracket<RazorStatement> (c, context, Parent.Parent);
 
108
                        }
 
109
 
 
110
                        return base.PushChar (c, context, ref rollback);
 
111
                }
 
112
 
 
113
                State SwitchToContinuationStatement (IParseContext context, string key)
 
114
                {
 
115
                        string name = key.Trim ();
 
116
                        int length = key.Length;
 
117
                        if (name == "else if")
 
118
                                length = key.Length - 1;
 
119
                        else if (name == "else")
 
120
                                length = key.Length + 1;
 
121
                        var stm = new RazorStatement (context.LocationMinus (length)) { Name = name };
 
122
                        context.Nodes.Push (stm);
 
123
                        return EnsureSetAndAdopted<RazorStatementState> (ref statementState);
 
124
                }
 
125
 
 
126
                public override State ParseClosingBracket<T> (char c, IParseContext context, State stateToReturn)
 
127
                {
 
128
                        if (base.ParseClosingBracket<T> (c, context, stateToReturn) != null) {
 
129
                                if (RazorSymbols.CanBeContinued (CorrespondingBlock.Name)) {
 
130
                                        keys = RazorSymbols.PossibleKeywordsAfter (CorrespondingBlock.Name);
 
131
                                        context.StateTag = POSSIBLE_CONTINUATION;
 
132
                                } else
 
133
                                        return stateToReturn;
 
134
                        }
 
135
                        return null;
 
136
                }
 
137
        }
 
138
}