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.Text.RegularExpressions;
class Program
{
static void Main(string[] args)
{
string text =
"(values (value1 40) (value2 0) (value3 6) (value4 80) \r\n" +
"(value5 70) (value6 10 20))\r\n" +
"(values (value1 70) (value2 0) (value3 6) (value4 80) \r\n" +
"(value5 70) (value6 10 20 30 40))\r\n" +
"(values (value1 80) (value2 0) (value3 6) (value4 80) \r\n" +
"(value5 70) (value6 10 20 30)) ; testkommentar.. :)\r\n" +
"(values (value1 90) (value2 0) (value3 6) (value4 40) \r\n" +
"(value5 70) (value6 10 20 30 20))\r\n" +
"(values (value1 10) (value2 0) (value3 6) (value4 80) ; noch ein kommentar... ;)\r\n" +
"(value5 70) (value6 10 20 30 50))\r\n" +
"(values (values1 22) (values2 (550 40 111) (1 2 3)))\r\n";
var result = Parse(text);
for (int i = 0; i < result.Count; i++)
{
Console.WriteLine("\r\n -- Zeile {0}", i);
foreach (string key in result[i].Keys)
{
Console.WriteLine("Name: {0}", key);
for (int j = 0; j < result[i][key].Length; j++)
Console.Write("{0}, ", result[i][key][j]);
Console.WriteLine();
}
}
Console.ReadLine();
}
private static List<Dictionary<string, string[]>> Parse(string text)
{
var data = new List<Dictionary<string, string[]>>();
bool outer = false;
bool inner = false;
bool tuple = false;
bool first = false;
string key = null;
string tupledata = null;
List<string> values = null;
Dictionary<string, string[]> currentData = new Dictionary<string, string[]>();
foreach (string token in Tokenize(text))
{
switch (token)
{
case "(":
if (inner)
{
tuple = true;
tupledata = "";
}
else if (outer)
{
inner = true;
first = true;
values = new List<string>();
}
else
outer = true;
break;
case ")":
if (tuple)
{
tuple = false;
values.Add(tupledata);
tupledata = null;
break;
}
if (inner)
{
inner = false;
string[] vals = values.ToArray();
currentData.Add(key, vals);
}
else if (outer)
{
outer = false;
if (currentData != null)
data.Add(currentData);
currentData = new Dictionary<string, string[]>();
}
else
throw new Exception("Unexpected token ')'");
break;
default:
if (token[0] == ';') // kommentar.. ;)
break;
if (tuple)
{
tupledata += token + " ";
break;
}
if (!inner)
break;
if (first)
{
key = token;
first = false;
}
else
{
values.Add(token);
}
break;
}
}
return data;
}
private static IEnumerable<string> Tokenize(string text)
{
string token = "";
bool isCommentToken = false;
foreach (char c in text)
{
switch (c)
{
case '\n':
case '\r':
if (isCommentToken)
{
isCommentToken = false;
yield return token;
token = "";
}
goto case ' ';
case ' ':
if (isCommentToken)
{
token += c;
break;
}
if (token.Length > 0)
yield return token;
token = "";
break;
case '(':
case ')':
if (isCommentToken)
{
token += c;
break;
}
if (token.Length > 0)
yield return token;
yield return c.ToString();
token = "";
break;
case ';':
if (isCommentToken)
{
token += c;
break;
}
if (token.Length > 0)
yield return token;
isCommentToken = true;
token = c.ToString();
break;
default:
token += c;
break;
}
}
if (token.Length > 0)
yield return token;
}
} |