nitc/examples: inaugurate the directory with `get_mclasses`
authorJean Privat <jean@pryen.org>
Tue, 3 May 2016 15:44:54 +0000 (11:44 -0400)
committerJean Privat <jean@pryen.org>
Tue, 3 May 2016 19:51:15 +0000 (15:51 -0400)
Signed-off-by: Jean Privat <jean@pryen.org>

src/examples/README.md [new file with mode: 0644]
src/examples/get_mclasses.nit [new file with mode: 0644]

diff --git a/src/examples/README.md b/src/examples/README.md
new file mode 100644 (file)
index 0000000..17a6638
--- /dev/null
@@ -0,0 +1,6 @@
+# Example programs on the Nit model and compiler
+
+This directory contains small sample programs that use the various services of `nitc`.
+
+Most of these programs should be short, simple and focuses on a few concerns.
+Successful ones might move to the parent directory and become a full stand-alone tool (with a manpage!)
diff --git a/src/examples/get_mclasses.nit b/src/examples/get_mclasses.nit
new file mode 100644 (file)
index 0000000..79e22ba
--- /dev/null
@@ -0,0 +1,86 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Sample program that search classes in a model
+#
+# eg.
+#
+# ~~~raw
+# get_mclasses ../../lib core::Array Array care::Arrow Fail
+# ~~~
+module get_mclasses
+
+import parser_util
+import modelbuilder
+import modelize
+import more_collections
+
+# Manage the logistic of the tool
+var tc = new ToolContext
+tc.tooldescription = "Usage: get_mclasses file qualified_class_name..."
+tc.process_options(args)
+
+# Parse the first argument to fill a model
+var model = new Model
+var mb = new ModelBuilder(model, tc)
+mb.parse_full([args.shift])
+mb.run_phases
+
+# Query remaining arguments
+for arg in args do
+       # Ask the parser to parse the argument. It's its job!
+       var n = tc.parse_something(arg)
+       if n isa AType then
+               # Only the class qualified identifier is useful
+               # We just ignore `nullable` or generic arguments.
+               var qid = n.n_qid
+               var full_name = qid.full_name
+               print "search: {full_name}"
+
+               # Iterate on all classes of the model to find one that matches the qualified name.
+               # Because we are efficient, we iterate only on the classes with the same short name.
+               var short_name = qid.n_id.text
+               var classes = model.get_mclasses_by_name(short_name)
+               var found = false
+               if classes != null then for c in classes do
+                       if qid.accept(c) then
+                               print " * {c.full_name}"
+                               found = true
+                       end
+               end
+
+               if not found then
+                       # We are unlucky, maybe a misspell?
+                       # Look for all classes with a similar name
+                       var bests = new BestDistance[MClass](full_name.length)
+                       for c in model.mclasses do
+                               # Check with both the full_name and the short_name
+                               var lev = full_name.levenshtein_distance(c.full_name)
+                               bests.update(lev, c)
+                               lev = full_name.levenshtein_distance(c.name)
+                               bests.update(lev, c)
+                       end
+                       if bests.best_items.is_empty then
+                               print " Found nothing :("
+                       else
+                               print " Did you mean?"
+                               for c in bests.best_items do
+                                       print " * {c.full_name}"
+                               end
+                       end
+               end
+       else
+               print "`{arg}` is {n}, wanted a class name :("
+       end
+end