e81a0af20a4ea26965ddaef8486c68af8288376a
[nit.git] / src / examples / test_loader.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Sample program that scan and load things.
16 #
17 # It shows the difference betwenn the various services of the `loader`.
18 module test_loader
19
20 import loader
21
22 # Display current loaded/known things.
23 fun stats(mb: ModelBuilder)
24 do
25 var m = mb.model
26 print " model: mpackages={m.mpackages.length} mmodules={m.mmodules.length}"
27 print " mb: identified modules={mb.identified_modules.length}; parsed modules={mb.parsed_modules.length}"
28 end
29
30
31 # Create a tool context to handle options and paths
32 var toolcontext = new ToolContext
33 toolcontext.keep_going = true
34
35 # We do not add other options, so process them now!
36 toolcontext.process_options(args)
37 var arguments = toolcontext.option_context.rest
38
39 # We need a model to collect stuff
40 var model = new Model
41 # And a model builder to parse files
42 var modelbuilder = new ModelBuilder(model, toolcontext)
43
44 # Identify each argument independently
45 for a in arguments do
46 var x
47
48 print "{a}: module?"
49 x = modelbuilder.identify_module(a)
50 if x != null then
51 print "\tmodule {x.full_name} at {x.filepath or else "?"}"
52 else
53 var le = modelbuilder.last_loader_error
54 if le != null then
55 print "\t{le}"
56 else
57 print "\tnothing"
58 end
59 end
60
61 print "{a}: group?"
62 x = modelbuilder.identify_group(a)
63 if x != null then
64 print "\tgroup {x.full_name} at {x.filepath or else "?"}"
65 else
66 var le = modelbuilder.last_loader_error
67 if le != null then
68 print "\t{le}"
69 else
70 print "\tnothing"
71 end
72 end
73 toolcontext.check_errors
74 stats(modelbuilder)
75 end
76
77 # Scan everything (including subdirectories)
78 var mm = modelbuilder.scan_full(arguments)
79 print "scan_full found {mm.length} modules"
80 stats(modelbuilder)
81
82 # Parse specific modules only
83 mm = modelbuilder.parse(arguments)
84 print "parse found {mm.length} modules"
85 stats(modelbuilder)
86
87 # Parse everything (including modules in subdirectories)
88 mm = modelbuilder.parse_full(arguments)
89 print "parse_full found {mm.length} modules"
90 stats(modelbuilder)