forked from dglo/java2go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
215 lines (179 loc) · 4.51 KB
/
main.go
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
package main
import (
"flag"
"fmt"
"go/format"
goparser "go/parser"
"go/token"
"io/ioutil"
"log"
"os"
"path"
"runtime/debug"
"strings"
"java2go/dumper"
"java2go/grammar"
"java2go/parser"
)
const sep = "------------"
func analyze(jp *grammar.JProgramFile, path string, config *parser.Config,
rules []parser.TransformFunc, print_report, verbose bool) *parser.GoProgram {
if print_report {
fmt.Println(sep + " CONVERT " + sep)
log.Printf("/** Convert %s **/\n", path)
}
gp := parser.NewGoProgram(convertPathToGo(path), config, verbose)
gp.Analyze(jp)
for _, rule := range rules {
gp.RunTransform(rule, gp, nil, nil)
}
return gp
}
func convertPathToGo(path string) string {
i := strings.LastIndex(path, "/")
var name string
if i < 0 {
name = path
} else {
name = path[i+1:]
}
if strings.HasSuffix(strings.ToLower(name), ".java") {
name = name[:len(name)-5]
}
return name + ".go"
}
func dump(gp *parser.GoProgram) {
}
func parseDirectory(dir string, cfg *parser.Config, outPath string,
debugLex bool, printReport bool, verbose bool) {
if flist, err := ioutil.ReadDir(dir); err != nil {
log.Printf("Cannot read \"%v\": %v\n", dir, err)
} else {
for _, finfo := range flist {
fullpath := path.Join(dir, finfo.Name())
if finfo.IsDir() {
parseDirectory(fullpath, cfg, outPath, debugLex, printReport,
verbose)
} else {
parseFile(fullpath, finfo, cfg, outPath, debugLex, printReport,
verbose, false)
}
}
}
}
func parseFile(path string, finfo os.FileInfo, cfg *parser.Config,
outPath string, debugLex bool, printReport bool, verbose bool,
logUnknownFile bool) {
if strings.HasSuffix(path, ".java") {
parseJava(path, cfg, outPath, debugLex, printReport, verbose)
} else if strings.HasSuffix(path, ".go") {
parseGo(path)
} else if logUnknownFile {
log.Printf("Ignoring unknown file \"%v\"", path)
}
}
func parseGo(path string) {
fset := token.NewFileSet() // positions are relative to fset
fmt.Printf("Parsing %s\n", path)
// Parse the file
f, err := goparser.ParseFile(fset, path, nil, 0)
if err != nil {
fmt.Println(err)
return
}
dumper.Dump("Parser", f)
format.Node(os.Stdout, fset, f)
}
func parseJava(path string, cfg *parser.Config, dirPath string,
debugLex, print_report, verbose bool) {
if verbose {
fmt.Printf("// %s\n", path)
}
l := grammar.NewFileLexer(path, debugLex)
if l != nil {
defer func() {
if r := recover(); r != nil {
err, ok := r.(error)
if !ok {
fmt.Fprintf(os.Stderr, "panic in %v: ??%v??<%T>\n",
path, r, r)
} else {
fmt.Fprintf(os.Stderr, "panic in %v: %v\n", path, err)
}
debug.PrintStack()
}
}()
prtn := grammar.JulyParse(l)
if prtn != 0 {
fmt.Fprintf(os.Stderr, "parser returned %d\n", prtn)
}
if l.JavaProgram() == nil {
fmt.Println("No code found")
} else {
rules := parser.StandardRules
gp := analyze(l.JavaProgram(), path, cfg, rules, print_report,
verbose)
if print_report {
fmt.Println(sep + " GODUMP " + sep)
gp.WriteString(os.Stdout)
fmt.Println()
fmt.Println(sep + " PARSE TREE " + sep)
gp.DumpTree()
fmt.Println(sep + " GO " + sep)
gp.Dump(os.Stdout)
}
if dirPath != "" {
if err := gp.Write(dirPath); err != nil {
fmt.Fprintf(os.Stderr, "Cannot write %v: %v\n", gp.Name(),
err)
}
} else if !print_report {
gp.Dump(os.Stdout)
}
}
l.Close()
}
}
func main1() {
var configPath string
flag.StringVar(&configPath, "config", "", "Config file")
var debugFlag bool
flag.BoolVar(&debugFlag, "debug", false, "Enable parser debugging")
var debugLexFlag bool
flag.BoolVar(&debugLexFlag, "debugLex", false, "Enable lexer debugging")
var dirPath string
flag.StringVar(&dirPath, "dir", "", "Directory where Go code is written")
var reportFlag bool
flag.BoolVar(&reportFlag, "report", false,
"Do not print dumps/translations to stdout")
var verboseFlag bool
flag.BoolVar(&verboseFlag, "verbose", false, "Print more stuff")
flag.Parse()
if debugFlag {
grammar.JulyDebug = 9
}
log.SetFlags(0)
var cfg *parser.Config
for _, f := range flag.Args() {
finfo, err := os.Stat(f)
if err != nil {
log.Printf("Bad file %v: %v\n", f, err)
continue
}
if cfg == nil {
if configPath != "" {
cfg = parser.ReadConfig(configPath)
}
if cfg == nil {
cfg = &parser.Config{}
}
}
if finfo.IsDir() {
parseDirectory(f, cfg, dirPath, debugLexFlag, reportFlag,
verboseFlag)
} else {
parseFile(f, finfo, cfg, dirPath, debugLexFlag, reportFlag,
verboseFlag, true)
}
}
}