Simple visitor framework for Nit models.

This module provides the ModelVisitor abstract class and refines the classes of the MEntity hierarchy to add visiting related methods.

A standard approach on complex visitor is to dispatch the ModelVisitor::visit on the entities to do specific things.

class FooVisitor
  super ModelVisitor
  redef fun visit(e) do e.foo_visit(self)
end

redef class MEntity
do
  fun foo_vist(v: FooVisitor) do visit_all(v)
end

redef class MClass
do
  redef fun foo_visit(v) do
     print self
     super
  end
end

Introduced classes

abstract class ModelVisitor

nitc :: ModelVisitor

The abstract model visitor template.

Redefined classes

redef class MClassDef

nitc :: model_visitor $ MClassDef

A definition (an introduction or a refinement) of a class in a module
redef abstract class MEntity

nitc :: model_visitor $ MEntity

A named and possibly documented entity in the model.
redef class MGroup

nitc :: model_visitor $ MGroup

A group of modules in a package
redef class MModule

nitc :: model_visitor $ MModule

A Nit module is usually associated with a Nit source file.
redef class MPackage

nitc :: model_visitor $ MPackage

A Nit package, that encompass a product
redef class Model

nitc :: model_visitor $ Model

The container class of a Nit object-oriented model.

All class definitions

redef class MClassDef

nitc :: model_visitor $ MClassDef

A definition (an introduction or a refinement) of a class in a module
redef abstract class MEntity

nitc :: model_visitor $ MEntity

A named and possibly documented entity in the model.
redef class MGroup

nitc :: model_visitor $ MGroup

A group of modules in a package
redef class MModule

nitc :: model_visitor $ MModule

A Nit module is usually associated with a Nit source file.
redef class MPackage

nitc :: model_visitor $ MPackage

A Nit package, that encompass a product
redef class Model

nitc :: model_visitor $ Model

The container class of a Nit object-oriented model.
abstract class ModelVisitor

nitc $ ModelVisitor

The abstract model visitor template.
package_diagram nitc::model_visitor model_visitor nitc::model_filters model_filters nitc::model_visitor->nitc::model_filters nitc::model_examples model_examples nitc::model_filters->nitc::model_examples nitc::parse_annotations parse_annotations nitc::model_filters->nitc::parse_annotations ...nitc::model_examples ... ...nitc::model_examples->nitc::model_examples ...nitc::parse_annotations ... ...nitc::parse_annotations->nitc::parse_annotations nitc::test_model_visitor test_model_visitor nitc::test_model_visitor->nitc::model_visitor a_star-m a_star-m a_star-m->nitc::test_model_visitor a_star-m... ... a_star-m...->a_star-m

Ancestors

module abstract_collection

core :: abstract_collection

Abstract collection classes and services.
module abstract_text

core :: abstract_text

Abstract class for manipulation of sequences of characters
module annotation

nitc :: annotation

Management and utilities on annotations
module array

core :: array

This module introduces the standard array structure.
module bitset

core :: bitset

Services to handle BitSet
module bytes

core :: bytes

Services for byte streams and arrays
module caching

serialization :: caching

Services for caching serialization engines
module circular_array

core :: circular_array

Efficient data structure to access both end of the sequence.
module codec_base

core :: codec_base

Base for codecs to use with streams
module codecs

core :: codecs

Group module for all codec-related manipulations
module collection

core :: collection

This module define several collection classes.
module console

console :: console

Defines some ANSI Terminal Control Escape Sequences.
module core

core :: core

Standard classes and methods used by default by Nit programs and libraries.
module digraph

graph :: digraph

Implementation of directed graphs, also called digraphs.
module engine_tools

serialization :: engine_tools

Advanced services for serialization engines
module environ

core :: environ

Access to the environment variables of the process
module error

core :: error

Standard error-management infrastructure.
module exec

core :: exec

Invocation and management of operating system sub-processes.
module file

core :: file

File manipulations (create, read, write, etc.)
module fixed_ints

core :: fixed_ints

Basic integers of fixed-precision
module fixed_ints_text

core :: fixed_ints_text

Text services to complement fixed_ints
module flat

core :: flat

All the array-based text representations
module gc

core :: gc

Access to the Nit internal garbage collection mechanism
module hash_collection

core :: hash_collection

Introduce HashMap and HashSet.
module ini

ini :: ini

Read and write INI configuration files
module inspect

serialization :: inspect

Refine Serializable::inspect to show more useful information
module iso8859_1

core :: iso8859_1

Codec for ISO8859-1 I/O
module kernel

core :: kernel

Most basic classes and methods.
module lexer

nitc :: lexer

Lexer and its tokens.
module lexer_work

nitc :: lexer_work

Internal algorithm and data structures for the Nit lexer
module list

core :: list

This module handle double linked lists
module literal

nitc :: literal

Parsing of literal values in the abstract syntax tree.
module loader

nitc :: loader

Loading of Nit source files
module location

nitc :: location

Nit source-file and locations in source-file
module math

core :: math

Mathematical operations
module mdoc

nitc :: mdoc

Documentation of model entities
module meta

meta :: meta

Simple user-defined meta-level to manipulate types of instances as object.
module mmodule

nitc :: mmodule

modules and module hierarchies in the metamodel
module mmodule_data

nitc :: mmodule_data

Define and retrieve data in modules
module model

nitc :: model

Classes, types and properties
module model_base

nitc :: model_base

The abstract concept of model and related common things
module model_examples

nitc :: model_examples

Examples for Model entities
module modelbuilder_base

nitc :: modelbuilder_base

Load nit source files and build the associated model
module modelize_class

nitc :: modelize_class

Analysis and verification of class definitions to instantiate model element
module modelize_property

nitc :: modelize_property

Analysis and verification of property definitions to instantiate model element
module more_collections

more_collections :: more_collections

Highly specific, but useful, collections-related classes.
module mpackage

nitc :: mpackage

Modelisation of a Nit package
module native

core :: native

Native structures for text and bytes
module nitpm_shared

nitc :: nitpm_shared

Services related to the Nit package manager
module numeric

core :: numeric

Advanced services for Numeric types
module opts

opts :: opts

Management of options on the command line
module ordered_tree

ordered_tree :: ordered_tree

Manipulation and presentation of ordered trees.
module parse_annotations

nitc :: parse_annotations

Simple annotation parsing
module parser

nitc :: parser

Parser.
module parser_nodes

nitc :: parser_nodes

AST nodes of the Nit language
module parser_prod

nitc :: parser_prod

Production AST nodes full definition.
module parser_work

nitc :: parser_work

Internal algorithm and data structures for the Nit parser
module phase

nitc :: phase

Phases of the processing of nit programs
module poset

poset :: poset

Pre order sets and partial order set (ie hierarchies)
module protocol

core :: protocol

module queue

core :: queue

Queuing data structures and wrappers
module range

core :: range

Module for range of discrete objects.
module re

core :: re

Regular expression support for all services based on Pattern
module ropes

core :: ropes

Tree-based representation of a String.
module serialization

serialization :: serialization

General serialization services
module serialization_core

serialization :: serialization_core

Abstract services to serialize Nit objects to different formats
module sorter

core :: sorter

This module contains classes used to compare things and sorts arrays.
module stream

core :: stream

Input and output streams of characters
module tables

nitc :: tables

Module that interfaces the parsing tables.
module template

template :: template

Basic template system
module text

core :: text

All the classes and methods related to the manipulation of text entities
module time

core :: time

Management of time and dates
module toolcontext

nitc :: toolcontext

Common command-line tool infrastructure than handle options and error messages
module union_find

core :: union_find

union–find algorithm using an efficient disjoint-set data structure
module utf8

core :: utf8

Codec for UTF-8 I/O
module version

nitc :: version

This file was generated by git-gen-version.sh

Parents

Children

module test_model_visitor

nitc :: test_model_visitor

Example of model_visitor

Descendants

module a_star-m

a_star-m

# Simple visitor framework for Nit models.
#
# This module provides the `ModelVisitor` abstract class and
# refines the classes of the `MEntity` hierarchy to add visiting related methods.
#
# A standard approach on complex visitor is to dispatch the `ModelVisitor::visit` on the
# entities to do specific things.
#
# ~~~nitish
# class FooVisitor
#   super ModelVisitor
#   redef fun visit(e) do e.foo_visit(self)
# end
#
# redef class MEntity
# do
#   fun foo_vist(v: FooVisitor) do visit_all(v)
# end
#
# redef class MClass
# do
#   redef fun foo_visit(v) do
#      print self
#      super
#   end
# end
# ~~~
module model_visitor

import model_filters

# The abstract model visitor template.
#
# Specific visitor must implement the `visit` method to perform the work.
abstract class ModelVisitor

	# Visit the entity `e`.
	#
	# This method setups `current_entity` and call `visit`.
	# If `e` is null, nothing is done.
	fun enter_visit(e: nullable MEntity) do
		if e == null then return
		if not accept_mentity(e) then return
		var old_entity = current_entity
		current_entity = e
		visit(e)
		current_entity = old_entity
	end

	# The current visited entity
	var current_entity: nullable MEntity = null

	# Method to define in specific visitor.
	#
	# It should not be called directly but used by `enter_visit`
	protected fun visit(e: MEntity) is abstract

	# Filters to apply when visiting the model.
	#
	# See ModelFilters for configuration.
	var filter: ModelFilter is lazy, writable, optional do
		return new ModelFilter(
			min_visibility = protected_visibility,
			accept_fictive = false,
			accept_test = false,
			accept_example = false,
			accept_redef = true,
			accept_extern = true,
			accept_attribute = true,
			accept_empty_doc = true
		)
	end

	# Should we accept this `mentity` from the view?
	#
	# If no `override_filter` is passed then use `self.filter`.
	fun accept_mentity(mentity: MEntity, override_filter: nullable ModelFilter): Bool do
		if override_filter != null then
			return override_filter.accept_mentity(mentity)
		end
		return filter.accept_mentity(mentity)
	end
end

redef class MEntity
	# Call `v.enter_visit` on all nested entities.
	#
	# See the specific implementation in the subclasses.
	fun visit_all(v: ModelVisitor) do end
end

redef class Model
	# Visit all the packages of the model.
	redef fun visit_all(v) do
		for x in mpackages do v.enter_visit(x)
	end
end

redef class MPackage
	# Visit the root group of the package.
	redef fun visit_all(v) do
		v.enter_visit(root)
	end
end

redef class MGroup
	# Visit all the subgroups and modules of the group.
	redef fun visit_all(v) do
		for x in in_nesting.direct_smallers do v.enter_visit(x)
		for x in mmodules do v.enter_visit(x)
	end
end

redef class MModule
	# Visit all the classes and class definitions of the module.
	#
	# On class introduction, the `MClass` then the `MClassDef` are visited.
	# On class refinement, only the `MClassDef` is visited (the `MClass` is visited in an imported module).
	# On class importation, nothing is visited (the `MClass` and the `MClassDef` are visited in imported modules).
	redef fun visit_all(v) do
		for x in mclassdefs do
			if x.is_intro then v.enter_visit(x.mclass)
			v.enter_visit(x)
		end
	end
end

redef class MClassDef
	# Visit all the classes and class definitions of the module.
	#
	# On property introduction, the `MProperty` then the `MPropDef` are visited.
	# On property redefinition, only the `MPropDef` is visited (the `MProperty` is visited in an inherited class).
	# On property inheritance, nothing is visited (the `MProperty` and the `MPropDef` are visited in inherited classes).
	redef fun visit_all(v) do
		for x in mpropdefs do
			if x.is_intro then v.enter_visit(x.mproperty)
			v.enter_visit(x)
		end
	end
end
src/model/model_visitor.nit:15,1--154,3