More working tests.
[castle.git] / MonoRail / Castle.MonoRail.Views.Brail / ReplaceUknownWithParameters.cs
blob2010369a367db6ea9c7f8e868d6a8b9a16013e93
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.MonoRail.Views.Brail
17 using Boo.Lang.Compiler.Ast;
18 using Boo.Lang.Compiler.Steps;
19 using Boo.Lang.Compiler.TypeSystem;
21 ///<summary>
22 /// Replace any uknown identifier with a call to GetParameter('unknown')
23 /// this mean that unknonw identifier in scripts will only fail in run time if they
24 /// were not defined by the controller.
25 /// </summary>
26 public class ReplaceUknownWithParameters : ProcessMethodBodiesWithDuckTyping
28 private IMethod getParam;
29 private IMethod tryGetParam;
31 public override void OnReferenceExpression(ReferenceExpression node)
33 IEntity entity = NameResolutionService.Resolve(node.Name);
34 if (entity != null)
36 base.OnReferenceExpression(node);
38 else
40 MethodInvocationExpression mie = CodeBuilder.CreateMethodInvocation(
41 CodeBuilder.CreateSelfReference(_currentMethod.DeclaringType),
42 GetMethod(node.Name));
43 mie.Arguments.Add(GetNameLiteral(node.Name));
44 node.ParentNode.Replace(node, mie);
48 protected override void InitializeMemberCache()
50 base.InitializeMemberCache();
51 getParam = TypeSystemServices.Map(typeof(BrailBase).GetMethod("GetParameter"));
52 tryGetParam = TypeSystemServices.Map(typeof(BrailBase).GetMethod("TryGetParameter"));
55 public IMethod GetMethod(string name)
57 if (name[0] == '?')
58 return tryGetParam;
59 else
60 return getParam;
63 public StringLiteralExpression GetNameLiteral(string name)
65 if (name[0] == '?')
66 return CodeBuilder.CreateStringLiteral(name.Substring(1));
67 else
68 return CodeBuilder.CreateStringLiteral(name);