-
Notifications
You must be signed in to change notification settings - Fork 0
/
Inline18.cs
196 lines (179 loc) · 6.98 KB
/
Inline18.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Text.RegularExpressions;
namespace Inline2018
{
class Inline18
{
Dictionary<string, string> variables = new Dictionary<string, string>();
List<string> list=new List<string>();
List<string> output = new List<string>();
Syntax syntax = new Syntax();
MyParser expPars = new MyParser();
bool IsVarDeclared(string var)
{
return variables.ContainsKey(var);
}
string VarReturn(string input)
{
string oldStr, newStr,varName;
Regex varRex = new Regex(@"\[(?'var'[A-z]+[0-9]*[_\.\$#]*?)\]");
try
{
while (varRex.IsMatch(input))
{
oldStr = varRex.Match(input).Value.ToString();
varName = varRex.Match(input).Groups["var"].ToString();
newStr = variables[varName].ToString();
if (IsVarDeclared(varName))
input = input.Replace(oldStr, newStr);
else
{
Console.WriteLine("Variable " + varName + " not declared"); return "";
}
}
}catch(Exception e)
{ Console.WriteLine("Error"); Console.WriteLine(e.Message); }
return input;
}
string VarOutput(string input)
{
string oldStr,newStr,varName;
Regex varRex = new Regex(@"=\[(?'var'.+?)\]");
try
{
while (varRex.IsMatch(input))
{
oldStr = varRex.Match(input).Value.ToString();
varName = varRex.Match(input).Groups["var"].ToString();
newStr = variables[varRex.Match(input).Groups["var"].ToString()].ToString();
if (IsVarDeclared(varName))
input = input.Replace(oldStr, newStr);
else
{ Console.WriteLine("Variable " + varName + " not declared"); return ""; }
}
}
catch (Exception e) { Console.WriteLine("Error"); Console.WriteLine(e.Message); }
input = input.Replace("\"", "");
return input;
}
string DeclareVar(string input)
{
Regex decRex = new Regex(@"^\[(?'var'.*)\] *= *(?'rest'[^\}]*)");
string varName = decRex.Match(input).Groups["var"].ToString();
string restLine = decRex.Match(input).Groups["rest"].ToString();
string temp = restLine;
restLine = VarReturn(restLine);
if (restLine == "") return restLine;
if (!IsVarDeclared(varName))
{
variables.Add(varName, expPars.parseString(restLine));
}
else
{
string t = expPars.parseString(restLine);
if (t.Contains("\"") == variables[varName].Contains("\""))
{
variables[varName] = t;
}
else
{
Console.WriteLine("<< Error variable " + varName + " declared with different type >>");
}
}
input = input.Replace(temp, "");
input = input.TrimEnd('=');
return input;
}
string InlineBlock(string input)
{
Regex inRex = new Regex(@"@\{(?'inline'.*)\}\s?");
string oldStr,newStr;
while(inRex.IsMatch(input))
{
oldStr = inRex.Match(input).Value.ToString();
newStr = inRex.Match(input).Groups["inline"].ToString();
input = input.Replace(oldStr, "");
DeclareVar(newStr);
}
return input;
}
void IncludeFile(string input)
{
Regex incRex = new Regex(@"include (?'path'.+\.txt)");
string oldStr, newStr;
while(incRex.IsMatch(input))
{
oldStr = incRex.Match(input).Value.ToString();
newStr = Path.GetFullPath(incRex.Match(input).Groups["path"].ToString());
try
{
if (!File.Exists(newStr))
Console.WriteLine("The file " + newStr + " doesn't exist");
Run(newStr);
input = input.Replace(oldStr, "");
}
catch (Exception e)
{
Console.WriteLine("The file "+newStr+" could not be read");
Console.WriteLine(e.Message);
}
}
}
string Tags(string input)
{
Regex tagRex = new Regex(@"<all_caps>+(?'tag'.*)</all_caps>+");
string oldStr, newStr;
while(tagRex.IsMatch(input))
{
oldStr = tagRex.Match(input).Value.ToString();
newStr = tagRex.Match(input).Groups["tag"].ToString();
input = input.Replace(oldStr, newStr.ToUpper());
}
return input;
}
public void Output()
{
PrintList("OUTPUT", list);
}
void PrintList(string message,List<string> list)
{
Console.WriteLine(message+":");
foreach (var x in list)
Console.WriteLine(x);
Console.WriteLine("END OF " + message+".");
}
public bool Run(string putanja )
{
using (StreamReader sr = new StreamReader(putanja))
{
while (sr.Peek() >= 0)
{
string line = sr.ReadLine();//works
// Console.WriteLine(line);
if (!syntax.IsValidLine(line))//works
return false;
if (syntax.IsValidPath(line))//works
IncludeFile(line);
if(syntax.IsDeclaration(line))
DeclareVar(line);
if (!syntax.IsDeclaration(line) && !syntax.IsValidPath(line))
{
string x = Tags(VarOutput(InlineBlock(line)));//works
if (syntax.IsValidOutputCharacters(x))
list.Add(x);
else
list.Add("<<< Invalid character used >>>");
}
}
}
// printList("OUTPUT", list);
//printList("OUTPUT",output);
return true;
}
}
}