metrics: add ast_metrics
authorJean Privat <jean@pryen.org>
Fri, 2 Aug 2013 06:47:39 +0000 (02:47 -0400)
committerJean Privat <jean@pryen.org>
Fri, 2 Aug 2013 07:01:17 +0000 (03:01 -0400)
Signed-off-by: Jean Privat <jean@pryen.org>

src/metrics/ast_metrics.nit [new file with mode: 0644]
src/metrics/metrics.nit
src/metrics/metrics_base.nit

diff --git a/src/metrics/ast_metrics.nit b/src/metrics/ast_metrics.nit
new file mode 100644 (file)
index 0000000..d66115e
--- /dev/null
@@ -0,0 +1,66 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2012 Jean Privat <jean@pryen.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.
+
+# Metrics about the nodes and identifiers in the AST
+module ast_metrics
+import modelbuilder
+private import metrics_base
+import frontend
+
+redef class ToolContext
+       var ast_metrics_phase = new AstMetricsPhase(self, null)
+end
+
+private class AstMetricsPhase
+       super Phase
+       var node_counter = new Counter[String]
+       var id_counter = new Counter[String]
+
+       redef fun process_mainmodule(mainmodule)
+       do
+               if not toolcontext.opt_ast.value and not toolcontext.opt_all.value then return
+               print "--- AST Metrics ---"
+               # Visit all the source code to collect data
+               var visitor = new AstMetricsVisitor(self)
+               for nmodule in toolcontext.modelbuilder.nmodules do
+                       visitor.enter_visit(nmodule)
+               end
+               print "## All nodes of the AST"
+               node_counter.print_summary
+               node_counter.print_elements(10)
+               print "## All identifiers of the AST"
+               id_counter.print_summary
+               id_counter.print_elements(10)
+       end
+end
+
+private class AstMetricsVisitor
+       super Visitor
+
+       var phase: AstMetricsPhase
+       init(phase: AstMetricsPhase) do self.phase = phase
+
+       redef fun visit(n)
+       do
+               if n == null then return
+               n.visit_all(self)
+               phase.node_counter.inc(n.class_name)
+               if n isa TId or n isa TAttrid or n isa TClassid then
+                       assert n isa Token
+                       phase.id_counter.inc(n.text)
+               end
+       end
+end
index 24936b1..c1f1830 100644 (file)
@@ -28,3 +28,4 @@ import rta_metrics
 import model_hyperdoc
 import tables_metrics
 import poset_metrics
+import ast_metrics
index cce151d..c68947b 100644 (file)
@@ -32,6 +32,8 @@ redef class ToolContext
        var opt_refinement = new OptionBool("Compute metrics about refinement usage", "--refinement")
        # --self
        var opt_self = new OptionBool("Compute metrics about the usage of explicit and implicit self", "--self")
+       # --ast
+       var opt_ast = new OptionBool("Compute metrics about the usage of nodes and identifiers in the AST", "--ast")
        # --nullables
        var opt_nullables = new OptionBool("Compute metrics on nullables send", "--nullables")
        # --static-types
@@ -57,6 +59,7 @@ redef class ToolContext
                self.option_context.add_option(opt_inheritance)
                self.option_context.add_option(opt_refinement)
                self.option_context.add_option(opt_self)
+               self.option_context.add_option(opt_ast)
                self.option_context.add_option(opt_nullables)
                self.option_context.add_option(opt_static_types)
                self.option_context.add_option(opt_tables)