Simple vavious processing on a AST

The analysis warns on :

  • superfluous parentheses
  • nested "once" expressions
  • use of "while true" instead of "loop"

Introduced classes

Redefined classes

redef class ADoExpr

nitc :: simple_misc_analysis $ ADoExpr

A do statement
redef abstract class AEscapeExpr

nitc :: simple_misc_analysis $ AEscapeExpr

A break or a continue
redef abstract class AExpr

nitc :: simple_misc_analysis $ AExpr

Expression and statements
redef class AForExpr

nitc :: simple_misc_analysis $ AForExpr

A for statement
redef class AForGroup

nitc :: simple_misc_analysis $ AForGroup

A collection iterated by a for, its automatic variables and its implicit iterator.
redef class AIfExpr

nitc :: simple_misc_analysis $ AIfExpr

A if statement
redef class AIfexprExpr

nitc :: simple_misc_analysis $ AIfexprExpr

A if expression (ternary conditional). eg. if true then 1 else 0
redef class ALoopExpr

nitc :: simple_misc_analysis $ ALoopExpr

A loop statement
redef class AModule

nitc :: simple_misc_analysis $ AModule

The main node of a Nit source-file
redef abstract class ANode

nitc :: simple_misc_analysis $ ANode

Root of the AST class-hierarchy
redef class AOnceExpr

nitc :: simple_misc_analysis $ AOnceExpr

A once expression. eg once x
redef class AParExpr

nitc :: simple_misc_analysis $ AParExpr

A simple parenthesis. eg (x)
redef class AParExprs

nitc :: simple_misc_analysis $ AParExprs

A list of expressions enclosed in parentheses
redef class AReturnExpr

nitc :: simple_misc_analysis $ AReturnExpr

A return statement. eg return x
redef class ASignature

nitc :: simple_misc_analysis $ ASignature

A signature in a method definition. eg (x,y:X,z:Z):T
redef class AWhileExpr

nitc :: simple_misc_analysis $ AWhileExpr

A while statement
redef class AWithExpr

nitc :: simple_misc_analysis $ AWithExpr

A with statement
redef class TSemi

nitc :: simple_misc_analysis $ TSemi

The symbol ;
redef abstract class Token

nitc :: simple_misc_analysis $ Token

Ancestor of all tokens
redef class ToolContext

nitc :: simple_misc_analysis $ ToolContext

Global context for tools

All class definitions

redef class ADoExpr

nitc :: simple_misc_analysis $ ADoExpr

A do statement
redef abstract class AEscapeExpr

nitc :: simple_misc_analysis $ AEscapeExpr

A break or a continue
redef abstract class AExpr

nitc :: simple_misc_analysis $ AExpr

Expression and statements
redef class AForExpr

nitc :: simple_misc_analysis $ AForExpr

A for statement
redef class AForGroup

nitc :: simple_misc_analysis $ AForGroup

A collection iterated by a for, its automatic variables and its implicit iterator.
redef class AIfExpr

nitc :: simple_misc_analysis $ AIfExpr

A if statement
redef class AIfexprExpr

nitc :: simple_misc_analysis $ AIfexprExpr

A if expression (ternary conditional). eg. if true then 1 else 0
redef class ALoopExpr

nitc :: simple_misc_analysis $ ALoopExpr

A loop statement
redef class AModule

nitc :: simple_misc_analysis $ AModule

The main node of a Nit source-file
redef abstract class ANode

nitc :: simple_misc_analysis $ ANode

Root of the AST class-hierarchy
redef class AOnceExpr

nitc :: simple_misc_analysis $ AOnceExpr

A once expression. eg once x
redef class AParExpr

nitc :: simple_misc_analysis $ AParExpr

A simple parenthesis. eg (x)
redef class AParExprs

nitc :: simple_misc_analysis $ AParExprs

A list of expressions enclosed in parentheses
redef class AReturnExpr

nitc :: simple_misc_analysis $ AReturnExpr

A return statement. eg return x
redef class ASignature

nitc :: simple_misc_analysis $ ASignature

A signature in a method definition. eg (x,y:X,z:Z):T
redef class AWhileExpr

nitc :: simple_misc_analysis $ AWhileExpr

A while statement
redef class AWithExpr

nitc :: simple_misc_analysis $ AWithExpr

A with statement
redef class TSemi

nitc :: simple_misc_analysis $ TSemi

The symbol ;
redef abstract class Token

nitc :: simple_misc_analysis $ Token

Ancestor of all tokens
redef class ToolContext

nitc :: simple_misc_analysis $ ToolContext

Global context for tools
package_diagram nitc::simple_misc_analysis simple_misc_analysis nitc::phase phase nitc::simple_misc_analysis->nitc::phase nitc::toolcontext toolcontext nitc::phase->nitc::toolcontext nitc\>parser\> parser nitc::phase->nitc\>parser\> ...nitc::toolcontext ... ...nitc::toolcontext->nitc::toolcontext ...nitc\>parser\> ... ...nitc\>parser\>->nitc\>parser\> nitc::frontend frontend nitc::frontend->nitc::simple_misc_analysis nitc::frontend... ... nitc::frontend...->nitc::frontend

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 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 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 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 location

nitc :: location

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

core :: math

Mathematical operations
module meta

meta :: meta

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

more_collections :: more_collections

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

core :: native

Native structures for text and bytes
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 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 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

module phase

nitc :: phase

Phases of the processing of nit programs

Children

module frontend

nitc :: frontend

Collect and orchestration of main frontend phases

Descendants

module a_star-m

a_star-m

module api

nitc :: api

Components required to build a web server about the nit model.
module api_auth

nitc :: api_auth

module api_base

nitc :: api_base

Base classes used by nitweb.
module api_docdown

nitc :: api_docdown

Nitdoc specific Markdown format handling for Nitweb
module api_feedback

nitc :: api_feedback

Feedback related features
module api_light

nitc :: api_light

Highlight and collect messages from a piece of code
module api_model

nitc :: api_model

module code_gen

nitc :: code_gen

Main frontend phases plus code generation phases
module commands_docdown

nitc :: commands_docdown

Doc down related queries
module highlight

nitc :: highlight

Highlighting of Nit AST
module html_commands

nitc :: html_commands

Render commands results as HTML
module html_model

nitc :: html_model

Translate mentities to html blocks.
module htmlight

nitc :: htmlight

Highlighting of Nit AST with HTML
module java_compiler

nitc :: java_compiler

Compile Nit code to Java code
module json_commands

nitc :: json_commands

Translate command results to json
module json_model

nitc :: json_model

Make model entities Serializable.
module md_commands

nitc :: md_commands

Render commands results as Markdown
module nit

nitc :: nit

A naive Nit interpreter
module nitc

nitc :: nitc

A Nit compiler
module nitcatalog

nitc :: nitcatalog

Basic catalog generator for Nit packages
module nitdoc

nitc :: nitdoc

Generator of static API documentation for the Nit language
module nitj

nitc :: nitj

Compile Nit into Java code runnable on the Java Virtual Machine.
module nitlight

nitc :: nitlight

Tool that produces highlighting for Nit programs
module nitmetrics

nitc :: nitmetrics

A program that collects various metrics on nit programs and libraries
module nitpackage

nitc :: nitpackage

Helpful features about packages
module nitpick

nitc :: nitpick

A program that collect potential style and code issues
module nitrestful

nitc :: nitrestful

Tool generating boilerplate code linking RESTful actions to Nit methods
module nitsaf

nitc :: nitsaf

Nit Static Analysis Framework client example.
module nitserial

nitc :: nitserial

Serialization support compiler, a tool to support deserialization of live generic types
module nitsmells

nitc :: nitsmells

module nituml

nitc :: nituml

UML generator in dot format.
module nitunit

nitc :: nitunit

Testing tool.
module nitvm

nitc :: nitvm

The Nit virtual machine launcher
module nitweb

nitc :: nitweb

Runs a webserver based on nitcorn that render things from model.
module nitx

nitc :: nitx

nitx, a command tool that displays useful data about Nit code
module static

nitc :: static

Nitdoc generation framework
module static_base

nitc :: static_base

Base entities shared by all the nitdoc code
module static_cards

nitc :: static_cards

Cards templates for the static documentation
module static_html

nitc :: static_html

Render documentation pages as HTML
module static_index

nitc :: static_index

Manage indexing of Nit model for Nitdoc QuickSearch.
module static_structure

nitc :: static_structure

Composes the pages of the static documentation
module term

nitc :: term

module test_astbuilder

nitc :: test_astbuilder

Program used to test the clone method of the astbuilder tool
module test_highlight

nitc :: test_highlight

Program used to test the Nit highlighter
module test_model_visitor

nitc :: test_model_visitor

Example of model_visitor
module test_neo

nitc :: test_neo

Test for neo model saving and loading.
module test_test_phase

nitc :: test_test_phase

Example of simple module that aims to do some specific work on nit programs.
# Simple vavious processing on a AST
# The analysis warns on :
#  * superfluous parentheses
#  * nested "once" expressions
#  * use of "while true" instead of "loop"
module simple_misc_analysis

import phase

redef class ToolContext
	# Execute `AModule::do_simple_misc_analysis` on each module.
	var simple_misc_analysis_phase: Phase = new SimpleMiscAnalysisPhase(self, null)
end

private class SimpleMiscAnalysisPhase
	super Phase
	redef fun process_nmodule(nmodule) do nmodule.do_simple_misc_analysis(toolcontext)
end

redef class AModule
	# Visit the module to detect easy warnings that does not need the metamodel or the importation
	# Warnings are displayed on the toolcontext
	fun do_simple_misc_analysis(toolcontext: ToolContext)
	do
		var v = new SimpleMiscVisitor(toolcontext)
		v.enter_visit(self)

		var t = location.file.first_token
		while t != null do
			t.accept_simple_misc_token(v)
			t = t.next_token
		end
	end
end

private class SimpleMiscVisitor
	super Visitor
	redef fun visit(n)
	do
		n.accept_simple_misc(self)
	end

	# Number of nested once
	var once_count: Int = 0

	var toolcontext: ToolContext

	fun warning(node: ANode, tag, msg: String)
	do
		toolcontext.warning(node.hot_location, tag, msg)
	end

	# Issue a warning if `sub` is a standalone `do` block.
	fun check_do_expr(sub: nullable AExpr)
	do
		if sub isa ADoExpr then
			warning(sub, "useless-do", "Warning: superfluous `do` block.")
		end
	end
end


###############################################################################

redef class ANode
	private fun accept_simple_misc(v: SimpleMiscVisitor)
	do
		visit_all(v)
		after_simple_misc(v)
	end
	private fun after_simple_misc(v: SimpleMiscVisitor) do end
end

redef class Token
	private fun accept_simple_misc_token(v: SimpleMiscVisitor)
	do
	end
end

redef class ASignature
	redef fun after_simple_misc(v)
	do
		if self.n_opar != null and self.n_params.is_empty then
			v.warning(self, "parentheses", "Warning: superfluous parentheses.")
		end
	end
end

redef class AExpr
	# Warn in case of superfluous parentheses
	private fun warn_parentheses(v: SimpleMiscVisitor) do end
end

redef class AParExpr
	redef fun warn_parentheses(v)
	do
		v.warning(self, "parentheses", "Warning: superfluous parentheses.")
	end
end

redef class AParExprs
	redef fun after_simple_misc(v)
	do
		if n_exprs.is_empty then
			v.warning(self, "parentheses", "Warning: superfluous parentheses.")
		end
	end
end

redef class AReturnExpr
	redef fun after_simple_misc(v)
	do
		var e = n_expr
		if e != null then
			e.warn_parentheses(v)
		end
	end
end

redef class AEscapeExpr
	redef fun after_simple_misc(v)
	do
		var e = n_expr
		if e != null then
			e.warn_parentheses(v)
		end
	end
end

redef class AWhileExpr
	redef fun after_simple_misc(v)
	do
		if n_expr isa ATrueExpr then
			v.warning(self, "loop", "Warning: use `loop` instead of `while true do`.")
		else
			n_expr.warn_parentheses(v)
		end
		v.check_do_expr(n_block)
	end
end

redef class ADoExpr
	redef fun after_simple_misc(v)
	do
		v.check_do_expr(n_block)
	end
end

redef class ALoopExpr
	redef fun after_simple_misc(v)
	do
		v.check_do_expr(n_block)
	end
end

redef class AForExpr
	redef fun after_simple_misc(v)
	do
		v.check_do_expr(n_block)
	end
end

redef class AForGroup
	redef fun after_simple_misc(v)
	do
		n_expr.warn_parentheses(v)
	end
end

redef class AWithExpr
	redef fun after_simple_misc(v)
	do
		v.check_do_expr(n_block)
	end
end

redef class AIfExpr
	redef fun after_simple_misc(v)
	do
		n_expr.warn_parentheses(v)
	end
end

redef class AIfexprExpr
	redef fun after_simple_misc(v)
	do
		n_expr.warn_parentheses(v)
	end
end

redef class AOnceExpr
	redef fun accept_simple_misc(v)
	do
		if v.once_count > 0 then
			v.warning(self, "nested-once", "Warning: useless once in a once expression.")
		end
		v.once_count = v.once_count + 1

		super

		v.once_count = v.once_count - 1
	end
end

redef class TSemi
	redef fun accept_simple_misc_token(v)
	do
		var n = next_token
		var p = prev_token
		if
			n == null or
			n isa TEol or
			n isa EOF or
			n isa TComment or
			p == null or
			p isa TEol or
			p isa EOF or
			p isa TComment or
			p isa TSemi
		then
			v.warning(self, "semi", "Warning: superfluous `;`.")
			return
		end
	end
end
src/frontend/simple_misc_analysis.nit:17,1--241,3