In the Ulipad code Editor to do a support Golang plugin, this file is plugin required mclassbrowser.py file part of the
golangparse.py
ImportRedefparsefile (filename): Dict= {'Import':[],'struct':{},'func': []} in_import=False Lines=open (filename). ReadLines () forLineninchRange (0, Len (lines)): Line=Lines[linen].lstrip () match= Re.match (r'import\s+ (\w*\s*) "(\w+)"', line)ifMatch! =none:dict['Import'].append ((Match.Groups () [1], linen))elifRe.match (R'import\s+\ (', line)! =None:in_import=TrueelifIn_import:match= Re.match (r'(\w*\s*) "(\w+)"', line)ifMatch! =none:dict['Import'].append ((Match.Groups () [1], linen))elifRe.match (R'\)', line): In_import=FalseElse: Match= Re.match (r'type\s+ (\w+) \s+struct', line)ifMatch! =none:dict['struct'][match.groups () [-1]] = {'Linen': Linen,'Method':[]} Else: Match= Re.match (r'func\s+ (\w+) \ (. *\)', line)ifMatch! =none:dict['func'].append ((Match.Groups () [1], linen))Else: Match= Re.match (r'func\s+\ (\w+\s+\* (\w+) \) \s+ (\w+) \ (. *\)', line)ifMatch! =none:dict['struct'][match.groups () [0]]['Method'].append ((Match.Groups () [1], linen))returnDictif __name__=="__main__": Dict= Parsefile ('Test.go') Print '-----Import-----' forName, Lineninchdict['Import']: Printname, LinenPrint '-----struct-----' forNameinchdict['struct']: PrintName, dict['struct'][name]['Linen'] Print '\ t','-----Method-----' forName, Lineninchdict['struct'][name]['Method']: Print '\ t', name, linenPrint '------func------' forName, Lineninchdict['func']: PrintName, Linen
Parsed file Test.go
Package MainImport "FMT"Import "Sex"Import ( "Log" " Time"type person struct {Name string age uint sex sex. Sex}func ( Self*Person ) Sayhi () {fmt. Printf ("Hi from%t\n", *Self )} Func ( Self*Person ) SayHello () {fmt. Printf ("Hello from%t\n", *Self )} Type Student struct {person School string}func ( self*Student) SayHello () {fmt. Printf ("Hello from%t\n", *Self )} Func main () {lucy:= Student{person:person{name:"Lucy", age:15, Sex:sex. Female}, School:"Beijing Primary School"} fmt. Printf ("Age of %s:%v\n", Lucy. Name, Lucy. Age) Fmt. Printf ("Sex of%s:%v\n", Lucy. Name, Lucy. Sex) fmt. Printf ("School of%s:%v\n", Lucy. Name, Lucy. School) Lucy. Sayhi () Lucy. SayHello () Lucy. Person.sayhello ()}
Output results
Parsing Golang source files with Python