contrib/jwrapper main: revamp docs
[nit.git] / contrib / jwrapper / src / jwrapper.nit
1 # This file is part of NIT (http://www.nitlanguage.org).
2 #
3 # Copyright 2014 Frédéric Vachon <fredvac@gmail.com>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # jwrapper wraps Java classes in extern Nit classes
18 #
19 # This tool takes class file, Jar archives and javap files as input,
20 # and it outputs a Nit source file.
21 # For further details on installation and usage, refer to the README file.
22 #
23 # Here's an overview of the project design :
24 # * Grammar and lexer : `grammar/javap.sablecc`
25 # * The `javap_visitor` implements the visitor that extracts data from the AST
26 # * The `code_generator` takes these data and converts it to Nit code via the `jtype_converter` module and generate the output Nit file.
27 # * The `model` contains data structures used to represent the data
28 # * The `jwrapper` module implements the user interface
29 module jwrapper
30
31 import opts
32 import javap_test_parser
33 import code_generator
34 import javap_visitor
35
36 var opts = new OptionContext
37
38 var opt_unknown = new OptionEnum(["comment", "stub", "ignore"], "How to deal with unknown types", 0, "-u")
39 var opt_verbose = new OptionCount("Verbosity", "-v")
40 var opt_output = new OptionString("Output file", "-o")
41 var opt_help = new OptionBool("Show this help message", "-h", "--help")
42
43 opts.add_option(opt_output, opt_unknown, opt_verbose, opt_help)
44 opts.parse args
45
46 if opts.errors.not_empty or opts.rest.is_empty or opt_help.value then
47 print """
48 Usage: jwrapper [options] file [other_file [...]]
49 Input files: bytecode Java class (.class), Jar archive (.jar) or javap output (.javap)
50 Options:"""
51 opts.usage
52
53 if opt_help.value then exit 0
54 exit 1
55 end
56
57 var out_file = opt_output.value
58 if out_file == null then out_file = "out.nit"
59
60 if not "javap".program_is_in_path then
61 print "Error: 'javap' must be in PATH"
62 exit 1
63 end
64
65 var javap = new ProcessReader("javap", "-public", dot_class)
66
67 var p = new TestParser_javap
68 var tree = p.work(javap.read_all)
69
70 var converter = new JavaTypeConverter
71
72 var visitor = new JavaVisitor(converter)
73 visitor.enter_visit(tree)
74
75 var generator = new CodeGenerator(out_file, visitor.java_class, opt_attr.value, opt_comment.value)
76 generator.generate