-
Notifications
You must be signed in to change notification settings - Fork 0
/
AS2MXMLParser.py
92 lines (86 loc) · 3.3 KB
/
AS2MXMLParser.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
#coding=utf-8
import ASModule
import codecs
import FileProcessor
from ASParserState import ASPaserState
from ASParserState import StartState
from UIComponentDescriptor import uiComponentDescripter2XML
from UIComponentDescriptor import UIComponentDescriptor
from StringProcessor import StringProcessor
def fromFileToFile(readPath, typePrefixDict = {}, defaultPrefix = '', writePath = ''):
'''
read file descriptor from readPath, and write XML to writePath
'''
if writePath == '':
writePath = readPath + '.xml'
s = FileProcessor.readAllFromFile(readPath)
s = ASModule.removeComments(s)
uicd = UIComponentDescriptor.parserDescriptorFromString(s)
doc = uiComponentDescripter2XML(uicd, typePrefixDict, defaultPrefix)
FileProcessor.writeAllToFile(doc.toprettyxml(' '), writePath)
def configParser(configPath = './config.cfg'):
'''
read config.cfg in the dir of this script
return (readWriteDict:dict, typePrefixDict:dict, defaultPrefix:str)
'''
readWriteDict = {}
typePrefixDict = {}
defaultPrefix = ''
config = open(configPath, 'rb')
lines = FileProcessor.readAllFromFile(configPath).splitlines()
for line in lines:
processor = StringProcessor(line)
tempChar = processor.skipSpace().readChar()
tempKey = processor.skipSpace().readStringWithSameLayerBracket('<')
tempKey = StringProcessor.cutHeadAndTail(tempKey)
tempVal = processor.skipSpace().readStringWithSameLayerBracket('<')
tempVal = StringProcessor.cutHeadAndTail(tempVal)
if tempChar == '#':
continue
elif tempChar == '@':
readWriteDict[tempKey] = tempVal
elif tempChar == '$':
typePrefixDict[tempKey] = tempVal
elif tempChar == '&':
defaultPrefix = tempKey
else:
if not (line.isspace() or line == ''):
raise RuntimeError('illegal config head')
return (readWriteDict, typePrefixDict, defaultPrefix)
def parseASString(s):
processor = StringProcessor(s)
state = StartState
words = ['', '', '', '', '']
while not state.isEnd():
tempWord = processor.skipSpace().readWord()
words = words[1:]
words.append(tempWord)
state = state.nextState(tempWord)
if state.isError():
ws = ''
for w in words:
ws += w
raise RuntimeError('error state, last 5 words are ' + ws)
return ASPaserState._currentClass
def parseConstructor(asClass):
pass
def fromASToMXML(asClass):
print('class name is : ' + asClass.getName())
for name, var in asClass.getVariablesIter():\
print('variable : ' + var.getName())
for name, fun in asClass.getFunctionsIter():
print('function : ' + fun.getName())
for name, getter in asClass.getGettersIter():
print('getter : ' + getter.getName())
for name, setter in asClass.getFunctionsIter():
print('setter : ' + setter.getName())
if __name__ == '__main__':
(readWriteDict, tagPrefixDict, defPrefix) = configParser()
for (k, v) in readWriteDict.items():
fromFileToFile(k, tagPrefixDict, defPrefix, v)
'''
asString = FileProcessor.readAllFromFile('D:\\teststate.as')
asString = ASModule.removeComments(asString)
asClass = parseASString(asString)
doc = fromASToMXML(asClass)
'''