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

« back to all changes in this revision

Viewing changes to src/addins/AspNet/MonoDevelop.AspNet.Mvc/RazorGenerator/PreprocessedCSharpRazorCodeParser.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
// Based on MvcCSharpRazorCodeParser.cs
 
3
//     Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
 
4
//     Licensed under the Microsoft Public License (MS-PL)
 
5
//
 
6
// Changes:
 
7
//     Author: Michael Hutchinson <mhutch@xamarin.com>
 
8
//     Copyright (c) 2012 Xamarin Inc (http://xamarin.com)
 
9
//     Licensed under the Microsoft Public License (MS-PL)
 
10
//
 
11
 
 
12
using System.Globalization;
 
13
using System.Web.Razor.Generator;
 
14
using System.Web.Razor.Parser;
 
15
using System.Web.Razor.Text;
 
16
using System.Collections.Generic;
 
17
using System.Web.Razor.Parser.SyntaxTree;
 
18
using System.Web.Razor.Tokenizer.Symbols;
 
19
using System;
 
20
using System.CodeDom;
 
21
 
 
22
namespace MonoDevelop.RazorGenerator
 
23
{
 
24
        class PreprocessedCSharpRazorCodeParser : CSharpCodeParser
 
25
        {
 
26
                public const string ModelKeyword = "model";
 
27
                public const string PropertyKeyword = "__property";
 
28
                public const string ClassKeyword = "__class";
 
29
 
 
30
                HashSet<string> directives = new HashSet<string> ();
 
31
 
 
32
                public PreprocessedCSharpRazorCodeParser()
 
33
                {
 
34
                        MapDirectives (ModelDirective, ModelKeyword);
 
35
                        MapDirectives (PropertyDirective, PropertyKeyword);
 
36
                        MapDirectives (ClassDirective, ClassKeyword);
 
37
                }
 
38
 
 
39
                void ModelDirective ()
 
40
                {
 
41
                        ValueDirective (ModelKeyword, true, (s, l) => {
 
42
                                var split = GetArgumentWords (s);
 
43
                                if (split.Length == 1) {
 
44
                                        return new PropertyCodeGenerator (split[0], "Model");
 
45
                                }
 
46
                                Context.OnError (l, string.Format ("The '{0}' directive requires exactly one argument", ModelKeyword));
 
47
                                return null;
 
48
                        });
 
49
                }
 
50
 
 
51
                void PropertyDirective ()
 
52
                {
 
53
                        ValueDirective (PropertyKeyword, true, (s, l) => {
 
54
                                var split = GetArgumentWords (s);
 
55
                                if (split.Length == 2) {
 
56
                                        return new PropertyCodeGenerator (split[0], split[1]);
 
57
                                }
 
58
                                Context.OnError (l, string.Format ("The '{0}' directive requires exactly two arguments", PropertyKeyword));
 
59
                                return null;
 
60
                        });
 
61
                }
 
62
 
 
63
                void ClassDirective ()
 
64
                {
 
65
                        ValueDirective (ClassKeyword, true, (s, l) => {
 
66
                                var split = GetArgumentWords (s);
 
67
                                if (split.Length != 1 && split.Length != 2) {
 
68
                                        Context.OnError (l, string.Format ("The '{0}' directive requires one or two arguments", ClassKeyword));
 
69
                                        return null;
 
70
                                }
 
71
                                string name = null, access = null;
 
72
                                if (split[0] == "public" || split[0] == "internal") {
 
73
                                        access = split[0];
 
74
                                } else {
 
75
                                        name = split[0];
 
76
                                }
 
77
                                if (split.Length == 2) {
 
78
                                        if (access == null) {
 
79
                                                string err = "If '{0}' directive has two arguments, the first must be 'public' or 'internal'.";
 
80
                                                Context.OnError (l, string.Format (err, ClassKeyword));
 
81
                                                return null;
 
82
                                        }
 
83
                                        name = split[1];
 
84
                                }
 
85
                                return new ClassNameCodeGenerator (access, name);
 
86
                        });
 
87
                }
 
88
 
 
89
                static char[] wordSplitChars = new[] { ' ', '\t'};
 
90
                static string[] GetArgumentWords (string value)
 
91
                {
 
92
                        return value.Split (wordSplitChars, StringSplitOptions.RemoveEmptyEntries);
 
93
                }
 
94
                
 
95
                void ValueDirective (string keyword, bool checkOne, Func<string,SourceLocation,SpanCodeGenerator> valueParsed)
 
96
                {
 
97
                        AssertDirective (ClassKeyword);
 
98
                        AcceptAndMoveNext ();
 
99
                        if (checkOne && !directives.Add (keyword)) {
 
100
                                Context.OnError (
 
101
                                        CurrentLocation,
 
102
                                        string.Format ("Only one '{0}' directive is permitted", keyword)
 
103
                                        );
 
104
                        }
 
105
                        SourceLocation location = CurrentLocation;
 
106
                        BaseTypeDirective (
 
107
                                string.Format ("The '{0}' directive must have a value", keyword),
 
108
                                s => {
 
109
                                        if (s != null)
 
110
                                                return valueParsed (s, location);
 
111
                                        return null;
 
112
                                }
 
113
                        );
 
114
                }
 
115
 
 
116
                class PropertyCodeGenerator : SpanCodeGenerator
 
117
                {
 
118
                        public PropertyCodeGenerator (string type, string name)
 
119
                        {
 
120
                                this.Type = type;
 
121
                                this.Name = name;
 
122
                        }
 
123
 
 
124
                        public string Type {get; private set; }
 
125
                        public string Name { get; private set; }
 
126
 
 
127
                        public override void GenerateCode (Span target, CodeGeneratorContext context)
 
128
                        {
 
129
                                var text = string.Format ("public {0} {1} {{ get; set; }}\n", Type, Name);
 
130
                                var prop = new CodeSnippetTypeMember (text);
 
131
                                prop.LinePragma = new CodeLinePragma (context.SourceFile, target.Start.LineIndex + 1);
 
132
                                context.GeneratedClass.Members.Add (prop);
 
133
                        }
 
134
                }
 
135
 
 
136
                class ClassNameCodeGenerator : SpanCodeGenerator
 
137
                {
 
138
                        public ClassNameCodeGenerator (string access, string name)
 
139
                        {
 
140
                                this.Access = access;
 
141
                                this.Name = name;
 
142
                        }
 
143
 
 
144
                        public string Name {get; private set; }
 
145
                        public string Access {get; private set; }
 
146
 
 
147
                        public override void GenerateCode (Span target, CodeGeneratorContext context)
 
148
                        {
 
149
                                if (Name != null) {
 
150
                                        var idx = Name.LastIndexOf ('.');
 
151
                                        if (idx > 0) {
 
152
                                                context.Namespace.Name = Name.Substring (0, idx);
 
153
                                                context.GeneratedClass.Name = Name.Substring (idx + 1);
 
154
                                        } else {
 
155
                                                context.GeneratedClass.Name = Name;
 
156
                                        }
 
157
                                }
 
158
 
 
159
                                if (Access == "public") {
 
160
                                        context.GeneratedClass.TypeAttributes |= System.Reflection.TypeAttributes.Public;
 
161
                                } else if (Access == "internal") {
 
162
                                        context.GeneratedClass.TypeAttributes &= ~System.Reflection.TypeAttributes.Public;
 
163
                                }
 
164
                        }
 
165
                }
 
166
        }
 
167
}
 
 
b'\\ No newline at end of file'