-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copia di MainForm.cs
177 lines (169 loc) · 4.15 KB
/
Copia di MainForm.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
/*
* Creato da SharpDevelop.
* Utente: Family Rose
* Data: 02/03/2009
* Ora: 20.56
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Collections.Specialized;
namespace testtext
{
public class PyParser
{
private StreamReader _t;
private PySection _main;
public PyParser()
{
}
public void Render(StringCollection box)
{
RenderSection(box, _main);
}
private void InsertToPos(StringCollection coll, int pos, string str)
{
if (pos >= coll.Count)
{
coll.Add(str);
} else {
coll.Insert(pos, str);
}
}
public void RenderSection(StringCollection box, PySection section)
{
if (section.Name != "")
{
InsertToPos(box, section.Begin, "# --- " + section.Name + " : begin ---\n");
}
if (section.Children.Count == 0)
{
int cnt = 0;
foreach (string it in section.Lines)
{
InsertToPos(box, section.Begin + cnt, it + "\n");
cnt++;
}
} else {
int cnt = 0;
foreach (string it in section.Lines)
{
InsertToPos(box, section.Begin + cnt, it + "\n");
cnt++;
}
foreach (PySection sec in section.Children)
{
RenderSection(box, sec);
}
}
if (section.Name != "")
{
InsertToPos(box, section.Begin, "# --- " + section.Name + " : end ---\n");
}
}
public void ParseFile(string filename)
{
_t = File.OpenText(filename);
StringCollection coll = new StringCollection();
_main = new PySection("");
while (!_t.EndOfStream)
{
coll.Add(_t.ReadLine());
}
ParseFileSection(coll, _main, 0);
}
public int ParseFileSection(StringCollection sec, PySection section, int row)
{
// sec collection, doesn't contain # --- begin / # --- end
int count = row;
bool insection = false;
StringCollection tempc = new StringCollection();
foreach (string item in sec)
{
if (insection == true)
{
if (item.Contains("# ---") && item.Contains("end"))
{
insection = false;
string nn = item.Substring(6, item.IndexOf(':') - 6);
section.Children.Add(new PySection(nn.Trim()));
PySection child = (PySection)section.Children[section.Children.Count - 1];
child.Begin = count;
int cnt = ParseFileSection(tempc, child, count);
child.Size = cnt - child.Begin;
} else {
string debugstr = sec[count];
tempc.Add(debugstr);
}
}
else if (item.Contains("# ---") && item.Contains("begin"))
{
// begin a new section
insection = true;
tempc.Clear();
}
else if (item.Contains("# ---") && item.Contains("end"))
{
// something to do here?
insection = false;
}
else
{
section.Lines.Add(item);
}
count++;
}
return row;
}
}
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
StringCollection c = new StringCollection();
PyParser parser = new PyParser();
parser.ParseFile("../../app_struct.py");
parser.Render(c);
foreach (string item in c)
{
richTextBox1.AppendText(item);
// i++;
}
/*
PyFileEditor ed = new PyFileEditor();
ed.InsertSingleLineToSection(-1, PyFileSection.PY_EVENTDECL_SECTION, "Ciao\n");
ed.InsertAfterSection(PyFileSection.PY_EVENTIMPL_SECTION, "Ecco dopo\n");
ed.InsertAfterSection(PyFileSection.PY_EVENTIMPL_SECTION, "Ecco dopo\n");
ed.InsertBeforeSection(PyFileSection.PY_EVENTIMPL_SECTION, "Ecco prima!\n");
ed.InsertBeforeSection(PyFileSection.PY_EVENTIMPL_SECTION, "Ecco prima!\n");
string[] s = new string[ed.PyStream.Count];
int i=0;
foreach (string item in ed.PyStream)
{
s[i++] = item;
}
for (i=0; i<8; i++)
{
PySection sec = ed._py_sections[i];
string str = s[sec.Begin];
str = str.Substring(0, str.Length - 1);
s[sec.Begin] = str + " : " + sec.Begin.ToString() + "\n";
}
i=0;
foreach (string item in s)
{
richTextBox1.AppendText(i.ToString() + " :: " + item);
i++;
}
*/
}
}
}