Rename REAMDE to README.md
[nit.git] / src / neo.nit
index fe24e72..5e28695 100644 (file)
@@ -43,7 +43,6 @@
 # `MGroup`
 #
 # * labels: `MGroup`, `model_name` and `MEntity`.
-# * `full_name`: fully qualified name.
 # * `(:MGroup)-[:PROJECT]->(:MProject)`: associated project.
 # * `(:MGroup)-[:PARENT]->(:MGroup)`: parent group. Does not exist for the root
 # group.
@@ -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`,
-# `MNullableType`, `MVirtualType` or `MSignature`, depending on the class of
-# the represented entity.
+# `MNullableType`, `MVirtualType`, `MRawType` or `MSignature`, depending on the
+# class of the represented entity.
 #
 # Additional label and relationships for `MClassType`:
 #
 #
 # * `(:MNullableType)-[:TYPE]->(:MType)`: base type of the nullable type.
 #
+# Additional attribute and relationship for `MRawType`:
+#
+# * `text`: JSON array of the parts’ text.
+# * `(:MRawType)-[:LINK]->(:MTypePart)`: the parts that link to another entity.
+#
 # Additional attribute and relationships for `MSignature`:
 #
 # * `parameter_names`: JSON array representing the list of the parameter names.
 #
 # For example, if the source code contains:
 #
+# ~~~nitish
 #     fun foo(a: A, b: B, c: C)
+# ~~~
 #
 # The `MSignature` node will contain a property
 # `parameter_names = ["a", "b", "c"]` so the MSignature can be reconstructed
 #
 # MParameters are also ranked by their position in the corresponding signature.
 # Rank 0 for the first parameter, 1 for the next one and etc.
+#
+# `MTypePart`
+#
+# * labels: `MTypePart`, `model_name` and `MEntity`.
+# * `rank`: position in the `MRawType`.
+# * `(:MTypePart)-[:TARGET]->(:MEntity)`: the target of the link.
 module neo
 
-import model
+import doc::model_ext
 import neo4j
 import toolcontext
 
@@ -326,6 +344,20 @@ class NeoModel
                abort
        end
 
+       # 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("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)
+               if node.labels.has("MClassDef") then return to_mclassdef(model, node)
+               if node.labels.has("MProperty") then return to_mproperty(model, node)
+               if node.labels.has("MPropDef") then return to_mpropdef(model, node)
+               if node.labels.has("MType") then return to_mtype(model, node)
+               if node.labels.has("MParameter") then return to_mparameter(model, node)
+               abort
+       end
+
        # Make a new `NeoNode` based on `mentity`.
        private fun make_node(mentity: MEntity): NeoNode do
                var node = new NeoNode
@@ -333,7 +365,10 @@ 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 mentity.mdoc != null then
+                       node["mdoc"] = new JsonArray.from(mentity.mdoc.content)
+                       node["mdoc_location"] = mentity.mdoc.location.to_s
+               end
                return node
        end
 
@@ -367,7 +402,6 @@ class NeoModel
        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)))
                if parent != null then
@@ -406,7 +440,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)))
@@ -450,7 +483,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
@@ -532,7 +564,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"
@@ -541,6 +572,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
@@ -565,6 +599,9 @@ class NeoModel
                        mprop = new MAttribute(intro_mclassdef, name, visibility)
                else if node.labels.has("MVirtualTypeProp") then
                        mprop = new MVirtualTypeProp(intro_mclassdef, name, visibility)
+               else if node.labels.has("MInnerClass") then
+                       var inner = to_mclass(model, node.out_nodes("NESTS").first)
+                       mprop = new MInnerClass(intro_mclassdef, name, visibility, inner)
                end
                if mprop == null then
                        print "not yet implemented to_mproperty for {node.labels.join(",")}"
@@ -602,6 +639,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
@@ -635,6 +675,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(",")}"
@@ -673,15 +718,41 @@ class NeoModel
                        var rank = 0
                        for mparameter in mtype.mparameters do
                                names.add mparameter.name
-                               var pnode = to_node(mparameter)
+                               var pnode = mparameter_node(mparameter)
                                pnode["rank"] = rank
                                node.out_edges.add(new NeoEdge(node, "PARAMETER", pnode))
+                               rank += 1
                        end
                        if not names.is_empty then node["parameter_names"] = names
                        var return_mtype = mtype.return_mtype
                        if return_mtype != null then
                                node.out_edges.add(new NeoEdge(node, "RETURNTYPE", to_node(return_mtype)))
                        end
+               else if mtype isa MRawType then
+                       node.labels.add "MRawType"
+                       var text = new JsonArray
+                       var rank = 0
+                       for part in mtype.parts do
+                               text.add part.text
+                               if part.target != null then
+                                       var pnode = mtypepart_node(part)
+                                       pnode["rank"] = rank
+                                       node.out_edges.add(new NeoEdge(node, "LINK", pnode))
+                               end
+                               rank += 1
+                       end
+                       if not text.is_empty then node["text"] = text
+               end
+               return node
+       end
+
+       # Build a `NeoNode` representing `mtypepart`.
+       private fun mtypepart_node(mtypepart: MTypePart): NeoNode do
+               var node = make_node(mtypepart)
+               node.labels.add "MTypePart"
+               if mtypepart.target != null then
+                       var target_node = to_node(mtypepart.target.as(not null))
+                       node.out_edges.add(new NeoEdge(node, "TARGET", target_node))
                end
                return node
        end
@@ -744,6 +815,25 @@ class NeoModel
                        var mtype = new MSignature(mparameters, return_mtype)
                        mentities[node.id.as(Int)] = mtype
                        return mtype
+               else if node.labels.has("MRawType") then
+                       var mtype = new MRawType(model)
+                       var parts = node["text"]
+                       if parts isa JsonArray then
+                               for p in parts do
+                                       mtype.parts.add(new MTypePart(model, p.to_s, null))
+                               end
+                               for pnode in node.out_nodes("LINK") do
+                                       assert pnode.labels.has("MTypePart")
+                                       if not pnode.out_nodes("TARGET").is_empty then
+                                               var rank = pnode["rank"]
+                                               var target = to_mentity(model, pnode.out_nodes("TARGET").first)
+                                               assert rank isa Int
+                                               mtype.parts[rank] = mtype.parts[rank].link_to(target)
+                                       end
+                               end
+                       end
+                       mentities[node.id.as(Int)] = mtype
+                       return mtype
                end
                print "not yet implemented to_mtype for {node.labels.join(",")}"
                abort
@@ -755,6 +845,7 @@ class NeoModel
                node.labels.add "MParameter"
                node["name"] = mparameter.name
                node["is_vararg"] = mparameter.is_vararg
+               node["is_default"] = mparameter.is_default
                node.out_edges.add(new NeoEdge(node, "TYPE", to_node(mparameter.mtype)))
                return node
        end
@@ -770,25 +861,15 @@ class NeoModel
                var name = node["name"].to_s
                var mtype = to_mtype(model, node.out_nodes("TYPE").first)
                var is_vararg = node["is_vararg"].as(Bool)
-               var mparameter = new MParameter(name, mtype, is_vararg)
+               var is_default = node["is_default"].as(Bool)
+               var mparameter = new MParameter(name, mtype, is_vararg, is_default)
                mentities[node.id.as(Int)] = mparameter
                return mparameter
        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)
+               return new Location.from_string(loc)
        end
 
        # Get a `MVisibility` from its string representation.
@@ -801,6 +882,8 @@ class NeoModel
                        return protected_visibility
                else if vis == private_visibility.to_s then
                        return private_visibility
+               else if vis == package_visibility.to_s then
+                       return package_visibility
                else
                        return none_visibility
                end
@@ -818,8 +901,9 @@ class NeoModel
                        return enum_kind
                else if kind == extern_kind.to_s then
                        return extern_kind
+               else
+                       return raw_kind(kind)
                end
-               abort
        end
 
        # Extract the `MDoc` from `node` and link it to `mentity`.
@@ -829,7 +913,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