-
Notifications
You must be signed in to change notification settings - Fork 0
/
Generator.cs
160 lines (142 loc) · 4.92 KB
/
Generator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;
namespace Maussoft.Mvc.ViewGen
{
public class Token
{
public Token(string type, string value)
{
Type = type;
Value = value;
}
public string Type;
public string Value;
}
public class Statement
{
public Statement(string type, Token[] tokens)
{
Type = type;
Tokens = tokens;
}
public string Type;
public Token[] Tokens;
}
public class Generator
{
public virtual void ConvertFile(string filename)
{
}
protected void AddTokenListAsStatement(List<Statement> statements, List<Token> statement)
{
if (statement.Count > 0)
{
statements.Add(new Statement(statement[0].Type, statement.ToArray()));
}
}
protected void ParsePropertiesInHead(Dictionary<string, string> properties, Token head)
{
// simple parser
string regex = @"([a-zA-Z]+)(\s*=\s*""([^""]+?)"")?";
foreach (Match match in Regex.Matches(head.Value, regex, RegexOptions.IgnoreCase))
{
string key = match.Groups[1].Value;
if (match.Groups[2].Value.Length > 0)
{
string value = match.Groups[3].Value;
properties[key] = value;
}
else
{
properties["Type"] = key;
}
}
}
protected List<Statement> Parse(List<Token> input, Dictionary<string, string> properties)
{
List<Statement> statements = new List<Statement>();
List<Token> statement = new List<Token>();
for (int i = 0; i < input.Count; i++)
{
if (input[i].Type == "Head")
{
AddTokenListAsStatement(statements, statement);
ParsePropertiesInHead(properties, input[i]);
statement = new List<Token>();
}
else if (input[i].Type == "Code")
{
AddTokenListAsStatement(statements, statement);
statements.Add(new Statement("Code", new Token[] { input[i] }));
statement = new List<Token>();
}
else if (input[i].Type == "NewLine")
{
if (statement.Count > 0)
{
if (statement[0].Type == "Text")
{
statement[0].Type = "TextLine";
}
}
AddTokenListAsStatement(statements, statement);
statements.Add(new Statement("NewLine", new Token[] { input[i] }));
statement = new List<Token>();
}
else
{
statement.Add(input[i]);
}
}
AddTokenListAsStatement(statements, statement);
/*for (int i = 0; i < statements.Count; i++) {
Token[] tokens = statements[i].Tokens;
for (int j = 0; j < tokens.Length; j++) {
Token token = tokens [j];
Console.Write (token.Type+"("+token.Value+")");
}
Console.WriteLine ();
}*/
return statements;
}
protected void AddTokens(List<Token> tokens, string type, string value)
{
string[] lines = value.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
for (int i = 0; i < lines.Length; i++)
{
tokens.Add(new Token(type, lines[i]));
if (i < lines.Length - 1)
{
tokens.Add(new Token("NewLine", ""));
}
}
}
protected List<Token> Tokenize(string input)
{
int start, end = 0;
List<Token> tokens = new List<Token>();
while ((start = input.IndexOf("<%", end)) >= 0)
{
AddTokens(tokens, "Text", input.Substring(end, start - end));
end = input.IndexOf("%>", start);
if (end < 0) break;
if (input[start + 2] == '=')
{
AddTokens(tokens, "Expr", input.Substring(start + 3, end - (start + 3)));
}
else if (input[start + 2] == '@')
{
AddTokens(tokens, "Head", input.Substring(start + 3, end - (start + 3)));
}
else
{
AddTokens(tokens, "Code", input.Substring(start + 3, end - (start + 3)));
}
end = end + 2;
}
AddTokens(tokens, "Text", input.Substring(end));
return tokens;
}
}
}