2
// RazorStatementState.cs
5
// Piotr Dowgiallo <sparekd@gmail.com>
7
// Copyright (c) 2012 Piotr Dowgiallo
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:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
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
28
using System.Collections.Generic;
30
using MonoDevelop.Xml.StateEngine;
31
using System.Diagnostics;
32
using MonoDevelop.AspNet.Mvc.Parser;
34
namespace MonoDevelop.AspNet.Mvc.StateEngine
36
public class RazorStatementState : RazorCodeFragmentState
38
protected const int POSSIBLE_CONTINUATION = MAXCONST + 1;
39
protected const int ELSE = POSSIBLE_CONTINUATION + 1;
41
IEnumerable<String> keys;
42
RazorStatementState statementState;
44
public RazorStatement CorrespondingStatement {
45
get { return CorrespondingBlock as RazorStatement; }
48
public override State PushChar (char c, IParseContext context, ref string rollback)
50
if (context.CurrentStateLength == 1) {
51
bracketsBuilder.Clear ();
52
var stm = context.Nodes.FirstOrDefault (n => n is RazorStatement);
54
if (context.PreviousState is XmlClosingTagState && CorrespondingStatement != null)
55
context.Nodes.Push (CorrespondingStatement);
57
Debug.Fail ("Statement should be pushed before changing the state to StatementState");
61
CorrespondingBlock = stm as RazorCodeFragment;
64
if (context.StateTag == POSSIBLE_CONTINUATION) {
65
if (!Char.IsWhiteSpace (c))
66
context.KeywordBuilder.Append (c);
68
string currentKey = context.KeywordBuilder.ToString ();
70
if (!keys.Any (s => s.StartsWith (currentKey))) {
71
rollback = String.Empty;
73
while (!(p is RazorSpeculativeState))
78
if (keys.Any (s => s == currentKey)) {
79
if (currentKey != "else")
80
return SwitchToContinuationStatement (context, currentKey);
82
context.StateTag = ELSE;
88
if (context.StateTag == ELSE) {
90
string currentKey = context.KeywordBuilder.ToString ();
91
if (c == 'i' && currentKey.Trim () == "else")
92
currentKey = "else if";
93
return SwitchToContinuationStatement (context, currentKey);
96
context.KeywordBuilder.Append (c);
102
if (context.StateTag != TRANSITION)
103
return ParseOpeningBracket (c, context);
107
return ParseClosingBracket<RazorStatement> (c, context, Parent.Parent);
110
return base.PushChar (c, context, ref rollback);
113
State SwitchToContinuationStatement (IParseContext context, string key)
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);
126
public override State ParseClosingBracket<T> (char c, IParseContext context, State stateToReturn)
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;
133
return stateToReturn;