nitcc: tests script return non-zero on failure (print is not enough)
[nit.git] / src / neo.nit
index eebd408..3af570a 100644 (file)
 #
 # Note : All nodes described here are MEntities.
 #
-# `MProject`
+# `MPackage`
 #
-# * labels: `MProject`, `model_name` and `MEntity`.
-# * `(:MProject)-[:ROOT]->(:MGroup)`: root of the group tree.
+# * labels: `MPackage`, `model_name` and `MEntity`.
+# * `(:MPackage)-[:ROOT]->(:MGroup)`: root of the group tree.
 #
 # `MGroup`
 #
 # * labels: `MGroup`, `model_name` and `MEntity`.
-# * `full_name`: fully qualified name.
-# * `(:MGroup)-[:PROJECT]->(:MProject)`: associated project.
+# * `(:MGroup)-[:PROJECT]->(:MPackage)`: associated package.
 # * `(:MGroup)-[:PARENT]->(:MGroup)`: parent group. Does not exist for the root
 # group.
 # * `(:MGroup)-[:DECLARES]->(:MModule)`: modules that are direct children of
@@ -55,7 +54,6 @@
 # `MModule`
 #
 # * labels: `MModule`, `model_name` and `MEntity`.
-# * `full_name`: fully qualified name.
 # * `location`: origin of the definition. SEE: `Location.to_s`
 # * `(:MModule)-[:IMPORTS]->(:MModule)`: modules that are imported directly.
 # * `(:MModule)-[:INTRODUCES]->(:MClass)`: all by classes introduced by this
@@ -66,7 +64,6 @@
 # `MClass`
 #
 # * labels: `MClass`, `model_name` and `MEntity`.
-# * `full_name`: fully qualified name.
 # * `kind`: kind of the class (`interface`, `abstract class`, etc.)
 # * `visibility`: visibility of the class.
 # * `parameter_names`: JSON array listing the name of each formal generic
 # `MProperty`
 #
 # * labels: `MProperty`, `model_name` and `MEntity`. Must also have `MMethod`,
-# `MAttribute` or `MVirtualTypeProp`, depending on the class of the represented
-# entity.
-# * `full_name`: fully qualified name.
+# `MAttribute` `MVirtualTypeProp` or `MInnerClass`, depending on the class of
+# the represented entity.
 # * `visibility`: visibility of the property.
 # * `is_init`: Indicates if the property is a constructor. Exists only if the
 # node is a `MMethod`.
 # * `(:MProperty)-[:INTRO_CLASSDEF]->(:MClassDef)`: classdef that introduces
 # the property.
 #
+# Additional relationship for `MInnerClass`:
+#
+# * `(:MInnerClassDef)-[:NESTS]->(:MClass)`: actual inner class.
+#
 # `MPropDef`
 #
 # * labels: `MPropDef`, `model_name` and `MEntity`. Must also have `MMethodDef`,
-# `MAttributeDef` or `MVirtualTypeDef`, depending on the class of the
-# represented entity.
+# `MAttributeDef`, `MVirtualTypeDef` or `MInnerClassDef`, depending on the
+# class of the represented entity.
 # * `location`: origin of the definition. SEE: `Location.to_s`.
 # * `(:MPropDef)-[:DEFINES]->(:MProperty)`: associated property.
 #
 # is bound in this definition. Exists only if this definition bound the virtual
 # type to an effective type.
 #
+# Additional relationship for `MInnerClassDef`:
+#
+# * `(:MInnerClassDef)-[:NESTS]->(:MClassDef)`: actual inner class'
+# definition.
+#
 # `MType`
 #
 # * labels: `MType`, `model_name` and `MEntity`. Must also have `MClassType`,
@@ -228,9 +233,9 @@ class NeoModel
        fun load(model: Model): Model do
                var nodes: Array[NeoNode]
 
-               toolcontext.info("Loading project node...", 1)
-               nodes = client.nodes_with_labels([model_name, "MProject"])
-               for node in nodes do to_mproject(model, node)
+               toolcontext.info("Loading package node...", 1)
+               nodes = client.nodes_with_labels([model_name, "MPackage"])
+               for node in nodes do to_mpackage(model, node)
                toolcontext.info("Loading groups...", 1)
                nodes = client.nodes_with_labels([model_name, "MGroup"])
                for node in nodes do to_mgroup(model, node)
@@ -299,9 +304,9 @@ class NeoModel
 
        # Collect all nodes from the current `model`.
        private fun collect_model_nodes(model: Model): Collection[NeoNode] do
-               for mproject in model.mprojects do
-                       to_node(mproject)
-                       for mgroup in mproject.mgroups do to_node(mgroup)
+               for mpackage in model.mpackages do
+                       to_node(mpackage)
+                       for mgroup in mpackage.mgroups do to_node(mgroup)
                end
                return nodes.values
        end
@@ -327,7 +332,7 @@ class NeoModel
        # `mentities` are stored locally to avoid duplication.
        fun to_node(mentity: MEntity): NeoNode do
                if nodes.has_key(mentity) then return nodes[mentity]
-               if mentity isa MProject then return mproject_node(mentity)
+               if mentity isa MPackage then return mpackage_node(mentity)
                if mentity isa MGroup then return mgroup_node(mentity)
                if mentity isa MModule then return mmodule_node(mentity)
                if mentity isa MClass then return mclass_node(mentity)
@@ -341,7 +346,7 @@ class NeoModel
 
        # Get the `MEntity` associated with `node`.
        fun to_mentity(model: Model, node: NeoNode): MEntity do
-               if node.labels.has("MProject") then return to_mproject(model, node)
+               if node.labels.has("MPackage") then return to_mpackage(model, node)
                if node.labels.has("MGroup") then return to_mgroup(model, node)
                if node.labels.has("MModule") then return to_mmodule(model, node)
                if node.labels.has("MClass") then return to_mclass(model, node)
@@ -360,43 +365,51 @@ class NeoModel
                node.labels.add "MEntity"
                node.labels.add model_name
                node["name"] = mentity.name
-               if mentity.mdoc != null then node["mdoc"] = new JsonArray.from(mentity.mdoc.content)
+               if not mentity isa MSignature then
+                       #FIXME: MSignature is a MEntity, but has no model :/
+                       node["location"] = mentity.location.to_s
+               end
+               var mdoc = mentity.mdoc
+               if mdoc != null then
+                       node["mdoc"] = new JsonArray.from(mdoc.content)
+                       node["mdoc_location"] = mdoc.location.to_s
+               end
                return node
        end
 
-       # Build a `NeoNode` representing `mproject`.
-       private fun mproject_node(mproject: MProject): NeoNode do
-               var node = make_node(mproject)
-               node.labels.add "MProject"
-               var root = mproject.root
+       # Build a `NeoNode` representing `mpackage`.
+       private fun mpackage_node(mpackage: MPackage): NeoNode do
+               var node = make_node(mpackage)
+               node.labels.add "MPackage"
+               var root = mpackage.root
                if root != null then
                        node.out_edges.add(new NeoEdge(node, "ROOT", to_node(root)))
                end
                return node
        end
 
-       # Build a new `MProject` from a `node`.
+       # Build a new `MPackage` from a `node`.
        #
-       # REQUIRE `node.labels.has("MProject")`
-       private fun to_mproject(model: Model, node: NeoNode): MProject do
+       # REQUIRE `node.labels.has("MPackage")`
+       private fun to_mpackage(model: Model, node: NeoNode): MPackage do
                var m = mentities.get_or_null(node.id.as(Int))
-               if m isa MProject then return m
+               if m isa MPackage then return m
 
-               assert node.labels.has("MProject")
-               var mproject = new MProject(node["name"].to_s, model)
-               mentities[node.id.as(Int)] = mproject
-               set_doc(node, mproject)
-               mproject.root = to_mgroup(model, node.out_nodes("ROOT").first)
-               return mproject
+               assert node.labels.has("MPackage")
+               var location = to_location(node["location"].to_s)
+               var mpackage = new MPackage(node["name"].to_s, model, location)
+               mentities[node.id.as(Int)] = mpackage
+               set_doc(node, mpackage)
+               mpackage.root = to_mgroup(model, node.out_nodes("ROOT").first)
+               return mpackage
        end
 
        # Build a `NeoNode` representing `mgroup`.
        private fun mgroup_node(mgroup: MGroup): NeoNode do
                var node = make_node(mgroup)
                node.labels.add "MGroup"
-               node["full_name"] = mgroup.full_name
                var parent = mgroup.parent
-               node.out_edges.add(new NeoEdge(node, "PROJECT", to_node(mgroup.mproject)))
+               node.out_edges.add(new NeoEdge(node, "PROJECT", to_node(mgroup.mpackage)))
                if parent != null then
                        node.out_edges.add(new NeoEdge(node, "PARENT", to_node(parent)))
                end
@@ -417,13 +430,14 @@ class NeoModel
                if m isa MGroup then return m
 
                assert node.labels.has("MGroup")
-               var mproject = to_mproject(model, node.out_nodes("PROJECT").first)
+               var location = to_location(node["location"].to_s)
+               var mpackage = to_mpackage(model, node.out_nodes("PROJECT").first)
                var parent: nullable MGroup = null
                var out = node.out_nodes("PARENT")
                if not out.is_empty then
                        parent = to_mgroup(model, out.first)
                end
-               var mgroup = new MGroup(node["name"].to_s, mproject, parent)
+               var mgroup = new MGroup(node["name"].to_s, location, mpackage, parent)
                mentities[node.id.as(Int)] = mgroup
                set_doc(node, mgroup)
                return mgroup
@@ -433,8 +447,6 @@ class NeoModel
        private fun mmodule_node(mmodule: MModule): NeoNode do
                var node = make_node(mmodule)
                node.labels.add "MModule"
-               node["full_name"] = mmodule.full_name
-               node["location"] = mmodule.location.to_s
                for parent in mmodule.in_importation.direct_greaters do
                        node.out_edges.add(new NeoEdge(node, "IMPORTS", to_node(parent)))
                end
@@ -477,7 +489,6 @@ class NeoModel
        private fun mclass_node(mclass: MClass): NeoNode do
                var node = make_node(mclass)
                node.labels.add "MClass"
-               node["full_name"] = mclass.full_name
                node["kind"] = mclass.kind.to_s
                node["visibility"] = mclass.visibility.to_s
                if not mclass.mparameters.is_empty then
@@ -499,6 +510,7 @@ class NeoModel
                assert node.labels.has("MClass")
                var mmodule = to_mmodule(model, node.in_nodes("INTRODUCES").first)
                var name = node["name"].to_s
+               var location = to_location(node["location"].to_s)
                var kind = to_kind(node["kind"].to_s)
                var visibility = to_visibility(node["visibility"].to_s)
                var parameter_names = new Array[String]
@@ -507,7 +519,7 @@ class NeoModel
                                parameter_names.add e.to_s
                        end
                end
-               var mclass = new MClass(mmodule, name, parameter_names, kind, visibility)
+               var mclass = new MClass(mmodule, name, location, parameter_names, kind, visibility)
                mentities[node.id.as(Int)] = mclass
                set_doc(node, mclass)
                return mclass
@@ -517,7 +529,6 @@ class NeoModel
        private fun mclassdef_node(mclassdef: MClassDef): NeoNode do
                var node = make_node(mclassdef)
                node.labels.add "MClassDef"
-               node["location"] = mclassdef.location.to_s
                node.out_edges.add(new NeoEdge(node, "BOUNDTYPE", to_node(mclassdef.bound_mtype)))
                node.out_edges.add(new NeoEdge(node, "MCLASS", to_node(mclassdef.mclass)))
                for mproperty in mclassdef.intro_mproperties do
@@ -559,7 +570,6 @@ class NeoModel
        private fun mproperty_node(mproperty: MProperty): NeoNode do
                var node = make_node(mproperty)
                node.labels.add "MProperty"
-               node["full_name"] = mproperty.full_name
                node["visibility"] = mproperty.visibility.to_s
                if mproperty isa MMethod then
                        node.labels.add "MMethod"
@@ -568,6 +578,9 @@ class NeoModel
                        node.labels.add "MAttribute"
                else if mproperty isa MVirtualTypeProp then
                        node.labels.add "MVirtualTypeProp"
+               else if mproperty isa MInnerClass then
+                       node.labels.add "MInnerClass"
+                       node.out_edges.add(new NeoEdge(node, "NESTS", to_node(mproperty.inner)))
                end
                node.out_edges.add(new NeoEdge(node, "INTRO_CLASSDEF", to_node(mproperty.intro_mclassdef)))
                return node
@@ -583,15 +596,19 @@ class NeoModel
                assert node.labels.has("MProperty")
                var intro_mclassdef = to_mclassdef(model, node.out_nodes("INTRO_CLASSDEF").first)
                var name = node["name"].to_s
+               var location = to_location(node["location"].to_s)
                var visibility = to_visibility(node["visibility"].to_s)
                var mprop: nullable MProperty = null
                if node.labels.has("MMethod") then
-                       mprop = new MMethod(intro_mclassdef, name, visibility)
+                       mprop = new MMethod(intro_mclassdef, name, location, visibility)
                        mprop.is_init = node["is_init"].as(Bool)
                else if node.labels.has("MAttribute") then
-                       mprop = new MAttribute(intro_mclassdef, name, visibility)
+                       mprop = new MAttribute(intro_mclassdef, name, location, visibility)
                else if node.labels.has("MVirtualTypeProp") then
-                       mprop = new MVirtualTypeProp(intro_mclassdef, name, visibility)
+                       mprop = new MVirtualTypeProp(intro_mclassdef, name, location, visibility)
+               else if node.labels.has("MInnerClass") then
+                       var inner = to_mclass(model, node.out_nodes("NESTS").first)
+                       mprop = new MInnerClass(intro_mclassdef, name, location, visibility, inner)
                end
                if mprop == null then
                        print "not yet implemented to_mproperty for {node.labels.join(",")}"
@@ -606,7 +623,6 @@ class NeoModel
        private fun mpropdef_node(mpropdef: MPropDef): NeoNode do
                var node = make_node(mpropdef)
                node.labels.add "MPropDef"
-               node["location"] = mpropdef.location.to_s
                node.out_edges.add(new NeoEdge(node, "DEFINES", to_node(mpropdef.mproperty)))
                if mpropdef isa MMethodDef then
                        node.labels.add "MMethodDef"
@@ -629,6 +645,9 @@ class NeoModel
                        if bound != null then
                                node.out_edges.add(new NeoEdge(node, "BOUND", to_node(bound)))
                        end
+               else if mpropdef isa MInnerClassDef then
+                       node.labels.add "MInnerClassDef"
+                       node.out_edges.add(new NeoEdge(node, "NESTS", to_node(mpropdef.inner)))
                end
                return node
        end
@@ -662,6 +681,11 @@ class NeoModel
                        mentities[node.id.as(Int)] = mpropdef
                        var bound = node.out_nodes("BOUND")
                        if not bound.is_empty then mpropdef.bound = to_mtype(model, bound.first)
+               else if node.labels.has("MInnerClassDef") then
+                       var inner = to_mclassdef(model, node.out_nodes("NESTS").first)
+                       mpropdef = new MInnerClassDef(mclassdef,
+                                       mproperty.as(MInnerClass), location, inner)
+                       mentities[node.id.as(Int)] = mpropdef
                end
                if mpropdef == null then
                        print "not yet implemented to_mpropdef for {node.labels.join(",")}"
@@ -848,19 +872,8 @@ class NeoModel
        end
 
        # Get a `Location` from its string representation.
-       private fun to_location(loc: String): Location do
-               #TODO filepath
-               var parts = loc.split_with(":")
-               var file = new SourceFile.from_string(parts[0], "")
-               var pos = parts[1].split_with("--")
-               var pos1 = pos[0].split_with(",")
-               var pos2 = pos[1].split_with(",")
-               var line_s = pos1[0].to_i
-               var line_e = pos2[0].to_i
-               var column_s = pos1[1].to_i
-               var column_e = 0
-               if pos2.length == 2 then pos2[1].to_i
-               return new Location(file, line_s, line_e, column_s, column_e)
+       private fun to_location(loc: String): nitc::Location do
+               return new nitc::Location.from_string(loc)
        end
 
        # Get a `MVisibility` from its string representation.
@@ -904,7 +917,8 @@ class NeoModel
                        for e in node["mdoc"].as(JsonArray) do
                                lines.add e.to_s#.replace("\n", "\\n")
                        end
-                       var mdoc = new MDoc
+                       var location = to_location(node["mdoc_location"].to_s)
+                       var mdoc = new MDoc(location)
                        mdoc.content.add_all(lines)
                        mdoc.original_mentity = mentity
                        mentity.mdoc = mdoc