forked from vincenthEE/DotEditor
/
DotScriptEditor.py
253 lines (197 loc) · 8.24 KB
/
DotScriptEditor.py
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# coding=utf8
'''
Copyright (R) 2015 Vincent.H <forever.h@gmail.com>
Published under Apache 2.0 License (http://www.apache.org/licenses/LICENSE-2.0.html).
-------------------------------------------------------------------------------------
This module realized a simple dot language editor.
'''
import wx, os
from UIClass import DialogScript
import ExtParser
import ExtGraph
import ply.lex as lex
def get_lexer():
'''Generate a very lexer to parse the dot script.'''
### All tokens.
tokens = (
'QUOTED_STRING',
'COMMENT',
'KEYWORD',
'EDGE_LINK',
'PAREN',
)
t_QUOTED_STRING = r'(\"(\\"|[^"])*?\")'
t_COMMENT = r'(/\*(.|\n)*?\*/)|(//.*)'
t_KEYWORD = r'(?i)(strict|digraph|graph|node|edge|subgraph)(?=[,;\n\.\ \{\[])'
t_PAREN = r'\[|\]|\{|\}'
t_EDGE_LINK = r'--|-\>'
def t_error(t):
#print("Illegal character '%s'" % t.value[0])
t.lexer.skip(1)
lexer = lex.lex()
return lexer
def parse_dot(script):
'''Parse dot script, return list of (token, begin_pos, end_pos).'''
lexer = get_lexer()
lexer.input(script)
result = []
while True:
tok = lexer.token()
if not tok:
break
r = tok.lexpos, tok.lexpos+len(tok.value), tok.type
result.append(r)
return result
def _is_same_color(textAtt1, textAtt2):
if textAtt1.GetTextColour().GetRGB() == textAtt2.GetTextColour().GetRGB():
return True
else:
return False
class DS(DialogScript):
'''Dialog to edit dot script.'''
font_dict = {'PLAIN':wx.TextAttr('#000000')}
def __init__(self, parent):
DialogScript.__init__(self, parent)
colors = [wx.Colour(31, 120, 180), \
wx.Colour(51, 160, 44), \
wx.Colour(227, 26, 28), \
wx.Colour(255, 127, 0), \
]
self.font_dict.update( {
'QUOTED_STRING': wx.TextAttr(colors[0]),
'COMMENT': wx.TextAttr(colors[1]),
'STRICT': wx.TextAttr(colors[3]),
'KEYWORD': wx.TextAttr(colors[3]),
'EDGE_LINK': wx.TextAttr(colors[3]),
'PAREN': wx.TextAttr(colors[2]),
} )
ft = wx.Font(11, wx.FONTFAMILY_DEFAULT,
wx.FONTSTYLE_NORMAL,
wx.FONTWEIGHT_NORMAL,
faceName='Monospace')
### Try find best programming-font.
try:
if os.sys.platform == 'win32':
ft = wx.Font(11, wx.FONTFAMILY_DEFAULT,
wx.FONTSTYLE_NORMAL,
wx.FONTWEIGHT_NORMAL,
faceName='Consolas')
elif os.sys.platform == 'darwin':
ft = wx.Font(11, wx.FONTFAMILY_DEFAULT,
wx.FONTSTYLE_NORMAL,
wx.FONTWEIGHT_NORMAL,
faceName='Monaco')
except:
pass
self.m_text_script.SetDefaultStyle(wx.TextAttr(font=ft))
self.m_text_script.SetFont(ft)
self.m_text_script.Bind(wx.EVT_KEY_DOWN, self.onTab)
def onTab(self, event):
keycode = event.GetKeyCode()
if keycode == wx.WXK_TAB:
self.m_text_script.WriteText(' '*4)
#event.EventObject.Navigate()
else:
event.Skip()
def light_script_block(self, pos_begin, length):
'''Syntax highlight the specified line.'''
l_begin = pos_begin
l_end = pos_begin + length
script = self.m_text_script.GetValue()
ttable = parse_dot(script)
### Fill 'PLAIN' token to blank in ttable ;)
ttable.sort()
new_table = []
for x in range(len(ttable)):
t = ttable[x]
if x > 0:
t_pre = ttable[x-1]
if t[0] > t_pre[1]+1: # Find a gap.
new_table.append((t_pre[1]+1, t[0], 'PLAIN'))
new_table.append(t)
for t in new_table:
if l_begin <= t[0] <= l_end or l_begin <= t[1] <= l_end:
self.m_text_script.SetStyle(t[0], t[1], self.font_dict[t[2]])
if t[1] < l_end:
self.m_text_script.SetStyle(t[1], l_end, self.font_dict['PLAIN'])
elif t[0] < l_begin and l_end < t[1]:
self.m_text_script.SetStyle(l_begin, l_end, self.font_dict[t[2]])
return
def light_script_all(self):
'''Syntax highlight all script.'''
script = self.m_text_script.GetValue()
ttable = parse_dot(script)
for t in ttable:
self.m_text_script.SetStyle(t[0], t[1], self.font_dict[t[2]])
# A lock var to disable onText function when SetScript working.
highlight_lock = False
def SetScript(self, text):
self.m_text_script.SetValue(text)
self.highlight_lock = True
self.light_script_all()
self.highlight_lock = False
def onText(self, event):
if self.highlight_lock:
return
pos = self.m_text_script.GetInsertionPoint()
x, _ = self.m_text_script.PositionToXY(pos)
self.light_script_block(pos-x, x)
return
def onCheck(self, event):
'''Do check on script, format script if correct.'''
script = self.m_text_script.GetValue().strip()
### Get strict status here.
strict_status = False
if script[:6].lower() == 'strict':
strict_status = True
try:
g = ExtParser.parse_string(script.encode('utf8'))
### Hack the strcit status cause bug of pydot.
g.set_strict(strict_status)
g = ExtGraph.ExtGraph(obj_dict=g.obj_dict)
except ExtParser.ParseException, err:
pos = self.m_text_script.XYToPosition(0, err.lineno-1)
w = self.m_text_script.GetLineLength(err.lineno-1)
self.m_text_script.SetFocus()
self.m_text_script.SetSelection(pos, pos+w)
wx.MessageBox("Parse Script Error.\n"+20*"-"+"\n%s"%err, 'Parse script error')
return
self.SetScript(g.EG_to_string().decode('utf8'))
return
def onOK(self, event):
'''
If script is ok, store script in graph objcet and return to main window.
Else highlight the error line in editor.
'''
script = self.m_text_script.GetValue().strip()
### Get strict status here.
strict_status = False
if script[:6].lower() == 'strict':
strict_status = True
try:
g = ExtParser.parse_string(script.encode('utf8'))
### Hack the strcit status cause bug of pydot.
g.set_strict(strict_status)
self.graph = ExtGraph.ExtGraph(obj_dict=g.obj_dict)
self.EndModal(wx.ID_OK)
except ExtParser.ParseException, err:
pos = self.m_text_script.XYToPosition(0, err.lineno-1)
w = self.m_text_script.GetLineLength(err.lineno-1)
self.m_text_script.SetFocus()
self.m_text_script.SetSelection(pos, pos+w)
wx.MessageBox("Parse Script Error.\n"+20*"-"+"\n%s"%err, 'Parse script error')
return
def __test_lexer():
script = '''
digraph G {
rankdir=LR;
penwidth=0.1;
node [fontname="serif", fontsize=13, colorscheme="brbg3", color=1, comment="Wildcard node"];
edge [fontname="sans-serif", fontsize=10, colorscheme="brbg3", color=3, fontcolor=3, comment="Wildcard edge"];
"n1" [label="Welcome to DotEditor :)\n\n <--- select 'n1' and click minus to delete me."];
n1 -> n2
}
'''
parse_dot(script)
if __name__ == '__main__':
__test_lexer()