jwrapper: deal with the optional "Compiled from" line in the grammar
[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 is a Nit extern class generator `in "Java"`.
18 # It takes a .class file and output a Nit file. For further details on installation and usage, refer to the README file.
19 #
20 # Here's an overview of the project design :
21 # * Grammar and lexer : `grammar/javap.sablecc`
22 # * The `javap_visitor` implements the visitor that extracts data from the AST
23 # * The `code_generator` takes these data and converts it to Nit code via the `jtype_converter` module and generate the output Nit file.
24 # * The `types` contains data structures used to represent the data
25 # * The `jwrapper` module implements the user interface
26 module jwrapper
27
28 import opts
29 import javap_test_parser
30 import code_generator
31 import javap_visitor
32
33 var opts = new OptionContext
34
35 var opt_attr = new OptionBool("Generate attributes", "-a", "--with-attributes")
36 var opt_comment = new OptionBool("Comment methods/attributes containing unknown types", "-c", "--comment")
37 var opt_wrap = new OptionBool("Create extern classes wrapping unknown types (Default)", "-w", "--wrap")
38 var opt_help = new OptionBool("Show this help message", "-h", "--help")
39
40 opts.add_option(opt_attr, opt_comment, opt_wrap, opt_help)
41
42 opts.parse(args)
43
44 if opt_wrap.value and opt_comment.value then
45 print "Error: Can't use both '-c' and '-w'"
46 exit 1
47 end
48
49 if not opts.errors.is_empty or opts.rest.length != 2 or opt_help.value then
50 print "USAGE: jwrapper [OPTIONS] class_file nit_file"
51 print "Options:"
52 opts.usage
53
54 if opt_help.value then exit 0
55 exit 1
56 end
57
58 var dot_class = opts.rest[0]
59 var out_file = opts.rest[1]
60
61 if not "javap".program_is_in_path then
62 print "ERROR: 'javap' not found."
63 exit 1
64 end
65
66 var javap = new IProcess("javap", "-public", dot_class)
67
68 var p = new TestParser_javap
69 var tree = p.work(javap.read_all)
70
71 var converter = new JavaTypeConverter
72
73 var visitor = new JavaVisitor(converter)
74 visitor.enter_visit(tree)
75
76 var generator = new CodeGenerator(out_file, visitor.java_class, opt_attr.value, opt_comment.value)
77 generator.generate