Parse string commands to create DocQueries

Introduced properties

private var _allowed_commands: Array[String]

nitc :: CommandParser :: _allowed_commands

List of allowed command names for this parser
private var _catalog: nullable Catalog

nitc :: CommandParser :: _catalog

Catalog used for catalog commands
private var _commands_usage: Map[String, String]

nitc :: CommandParser :: _commands_usage

List of commands usage and documentation
private var _error: nullable CmdMessage

nitc :: CommandParser :: _error

Error or warning from last call to parse
private var _mainmodule: MModule

nitc :: CommandParser :: _mainmodule

Main module for linearization
private var _model: Model

nitc :: CommandParser :: _model

Model used to retrieve mentities
private var _modelbuilder: ModelBuilder

nitc :: CommandParser :: _modelbuilder

ModelBuilder used to retrieve AST nodes
fun allowed_commands: Array[String]

nitc :: CommandParser :: allowed_commands

List of allowed command names for this parser
fun allowed_commands=(allowed_commands: Array[String])

nitc :: CommandParser :: allowed_commands=

List of allowed command names for this parser
fun catalog: nullable Catalog

nitc :: CommandParser :: catalog

Catalog used for catalog commands
protected fun catalog=(catalog: nullable Catalog)

nitc :: CommandParser :: catalog=

Catalog used for catalog commands
fun commands_usage: Map[String, String]

nitc :: CommandParser :: commands_usage

List of commands usage and documentation
protected fun commands_usage=(commands_usage: Map[String, String])

nitc :: CommandParser :: commands_usage=

List of commands usage and documentation
init defaultinit(model: Model, mainmodule: MModule, modelbuilder: ModelBuilder, catalog: nullable Catalog)

nitc :: CommandParser :: defaultinit

fun error: nullable CmdMessage

nitc :: CommandParser :: error

Error or warning from last call to parse
protected fun error=(error: nullable CmdMessage)

nitc :: CommandParser :: error=

Error or warning from last call to parse
fun execute(query: String, no_color: nullable Bool)

nitc :: CommandParser :: execute

fun mainmodule: MModule

nitc :: CommandParser :: mainmodule

Main module for linearization
protected fun mainmodule=(mainmodule: MModule)

nitc :: CommandParser :: mainmodule=

Main module for linearization
fun model: Model

nitc :: CommandParser :: model

Model used to retrieve mentities
protected fun model=(model: Model)

nitc :: CommandParser :: model=

Model used to retrieve mentities
fun modelbuilder: ModelBuilder

nitc :: CommandParser :: modelbuilder

ModelBuilder used to retrieve AST nodes
protected fun modelbuilder=(modelbuilder: ModelBuilder)

nitc :: CommandParser :: modelbuilder=

ModelBuilder used to retrieve AST nodes
fun new_command(name: String): nullable DocCommand

nitc :: CommandParser :: new_command

Init a new DocCommand from its name
fun parse(string: String): nullable DocCommand

nitc :: CommandParser :: parse

Parse string as a DocCommand

Redefined properties

redef type SELF: CommandParser

nitc $ CommandParser :: SELF

Type of this instance, automatically specialized in every class

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
private var _allowed_commands: Array[String]

nitc :: CommandParser :: _allowed_commands

List of allowed command names for this parser
private var _catalog: nullable Catalog

nitc :: CommandParser :: _catalog

Catalog used for catalog commands
private var _commands_usage: Map[String, String]

nitc :: CommandParser :: _commands_usage

List of commands usage and documentation
private var _error: nullable CmdMessage

nitc :: CommandParser :: _error

Error or warning from last call to parse
private var _mainmodule: MModule

nitc :: CommandParser :: _mainmodule

Main module for linearization
private var _model: Model

nitc :: CommandParser :: _model

Model used to retrieve mentities
private var _modelbuilder: ModelBuilder

nitc :: CommandParser :: _modelbuilder

ModelBuilder used to retrieve AST nodes
fun allowed_commands: Array[String]

nitc :: CommandParser :: allowed_commands

List of allowed command names for this parser
fun allowed_commands=(allowed_commands: Array[String])

nitc :: CommandParser :: allowed_commands=

List of allowed command names for this parser
fun catalog: nullable Catalog

nitc :: CommandParser :: catalog

Catalog used for catalog commands
protected fun catalog=(catalog: nullable Catalog)

nitc :: CommandParser :: catalog=

Catalog used for catalog commands
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
fun commands_usage: Map[String, String]

nitc :: CommandParser :: commands_usage

List of commands usage and documentation
protected fun commands_usage=(commands_usage: Map[String, String])

nitc :: CommandParser :: commands_usage=

List of commands usage and documentation
init defaultinit(model: Model, mainmodule: MModule, modelbuilder: ModelBuilder, catalog: nullable Catalog)

nitc :: CommandParser :: defaultinit

fun error: nullable CmdMessage

nitc :: CommandParser :: error

Error or warning from last call to parse
protected fun error=(error: nullable CmdMessage)

nitc :: CommandParser :: error=

Error or warning from last call to parse
fun execute(query: String, no_color: nullable Bool)

nitc :: CommandParser :: execute

fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun hash: Int

core :: Object :: hash

The hash code of the object.
init init

core :: Object :: init

fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
fun mainmodule: MModule

nitc :: CommandParser :: mainmodule

Main module for linearization
protected fun mainmodule=(mainmodule: MModule)

nitc :: CommandParser :: mainmodule=

Main module for linearization
fun model: Model

nitc :: CommandParser :: model

Model used to retrieve mentities
protected fun model=(model: Model)

nitc :: CommandParser :: model=

Model used to retrieve mentities
fun modelbuilder: ModelBuilder

nitc :: CommandParser :: modelbuilder

ModelBuilder used to retrieve AST nodes
protected fun modelbuilder=(modelbuilder: ModelBuilder)

nitc :: CommandParser :: modelbuilder=

ModelBuilder used to retrieve AST nodes
private intern fun native_class_name: CString

core :: Object :: native_class_name

The class name of the object in CString format.
fun new_command(name: String): nullable DocCommand

nitc :: CommandParser :: new_command

Init a new DocCommand from its name
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
fun parse(string: String): nullable DocCommand

nitc :: CommandParser :: parse

Parse string as a DocCommand
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_s: String

core :: Object :: to_s

User readable representation of self.
package_diagram nitc::CommandParser CommandParser core::Object Object nitc::CommandParser->core::Object

Parents

interface Object

core :: Object

The root of the class hierarchy.

Class definitions

nitc $ CommandParser
# Parse string commands to create DocQueries
class CommandParser

	# Model used to retrieve mentities
	var model: Model

	# Main module for linearization
	var mainmodule: MModule

	# ModelBuilder used to retrieve AST nodes
	var modelbuilder: ModelBuilder

	# Catalog used for catalog commands
	var catalog: nullable Catalog

	# List of allowed command names for this parser
	var allowed_commands: Array[String] = [
	"link", "doc", "code", "lin", "uml", "graph", "search",
	"parents", "ancestors", "children", "descendants",
	"param", "return", "new", "call", "defs", "list", "random",
	"ini-desc", "ini-git", "ini-issues", "ini-maintainer", "ini-contributors", "ini-license",
	"license-file", "contrib-file", "license-content", "contrib-content", "git-clone",
	"mains", "main-compile", "main-run", "main-opts", "testing",
	"catalog", "stats", "tags", "tag", "person", "contrib", "maintain"] is writable

	# List of commands usage and documentation
	var commands_usage: Map[String, String] do
		var usage = new ArrayMap[String, String]
		usage["search: <string>"] = "list entities matching `string`"
		usage["link: <name>"] = "display the link to `name`"
		usage["doc: <name>"] = "display the documentation for `name`"
		usage["defs: <name>"] = "list all definitions for `name`"
		usage["code: <name>"] = "display the code for `name`"
		usage["lin: <name>"] = "display the linearization for `name`"
		usage["uml: <name>"] = "display the UML diagram for `name`"
		usage["graph: <name>"] = "display the inheritance graph for `name`"
		usage["parents: <name>"] = "list the direct parents of `name`"
		usage["ancestors: <name>"] = "list all ancestors of `name`"
		usage["children: <name>"] = "list direct children of `name`"
		usage["descendants: <name>"] = "list all descendants of `name`"
		usage["param: <type>"] = "list all methods accepting `type` as parameter"
		usage["return: <type>"] = "list all methods returning `type`"
		usage["new: <class>"] = "list all methods initializing `class`"
		usage["call: <property>"] = "list all methods calling `property`"
		usage["list: <kind>"] = "list all entities of `kind` from the model"
		usage["random: <kind>"] = "list random entities of `kind` from the model"
		usage["catalog:"] = "list packages from catalog"
		usage["stats:"] = "display catalog statistics"
		usage["tags:"] = "list all tabs from catalog"
		usage["tag: <tag>"] = "list all packages with `tag`"
		usage["maintain: <person>"] = "list all packages maintained by `person`"
		usage["contrib: <person>"] = "list all packages contributed by `person`"
		# Ini commands
		usage["ini-desc: <package>"] = "display the description from the `package` ini file"
		usage["ini-git: <package>"] = "display the git url from the `package` ini file"
		usage["ini-issues: <package>"] = "display the issues url from the `package` ini file"
		usage["ini-license: <package>"] = "display the license from the `package` ini file"
		usage["ini-maintainer: <package>"] = "display the maintainer from the `package` ini file"
		usage["ini-contributors: <package>"] = "display the contributors from the `package` ini file"
		usage["license-file: <package>"] = "display the license file for the `package`"
		usage["license-content: <package>"] = "display the license file content for the `package`"
		usage["contrib-file: <package>"] = "display the contrib file for the `package`"
		usage["contrib-content: <package>"] = "display the contrib file content for the `package`"
		usage["git-clone: <package>"] = "display the git clone command for the `package`"
		# Main
		usage["mains: <name>"] = "display the list of main methods for `name`"
		usage["main-compile: <name>"] = "display the nitc command to compile `name`"
		usage["main-run: <name>"] = "display the command to run `name`"
		usage["main-opts: <name>"] = "display the command options for `name`"
		usage["testing: <name>"] = "display the nitunit command to test `name`"
		return usage
	end

	# Parse `string` as a DocCommand
	#
	# Returns `null` if the string cannot be parsed.
	# See `error` for the error messages produced by both the parser and the commands.
	fun parse(string: String): nullable DocCommand do
		var pos = 0
		var tmp = new FlatBuffer
		error = null

		# Parse command name
		pos = string.read_until(tmp, pos, ':', '|')
		var name = tmp.write_to_string.trim

		# Check allowed commands
		if name.is_empty then
			error = new CmdParserError("Empty command name", 0)
			return null
		end
		# If the command name contains two consecutive colons or there is no colon in the name,
		# we certainly have a wiki link to a mentity
		var is_short_link = false
		if (pos < string.length - 2 and string[pos] == ':' and string[pos + 1] == ':') or
		   pos == string.length then
			is_short_link = true
		else if pos < string.length - 1 and string[pos] == '|' then
			is_short_link = true
			pos -= 1
		else if not allowed_commands.has(name) then
			error = new CmdParserError("Unknown command name `{name}`", 0)
			return null
		end

		# Parse the argument
		tmp.clear
		pos = string.read_until(tmp, pos + 1, '|')
		var arg = tmp.write_to_string.trim
		if is_short_link and not arg.is_empty then
			arg = "{name}:{arg}"
		else if is_short_link then
			arg = name
		end

		# Parse command options
		var opts = new CmdOptions
		while pos < string.length do
			# Parse option name
			tmp.clear
			pos = string.read_until(tmp, pos + 1, ':', ',')
			var oname = tmp.write_to_string.trim
			var oval = ""
			if oname.is_empty then break
			# Parse option value
			if pos < string.length and string[pos] == ':' then
				tmp.clear
				pos = string.read_until(tmp, pos + 1, ',')
				oval = tmp.write_to_string.trim
			end
			opts[oname] = oval
		end

		# Build the command
		var command
		if is_short_link then
			command = new CmdEntityLink(model)
		else
			command = new_command(name)
		end
		if command == null then
			error = new CmdParserError("Unknown command name `{name}`", 0)
			return null
		end

		# Initialize command from string options
		var status = command.parser_init(arg, opts)
		if not status isa CmdSuccess then error = status

		return command
	end

	# Init a new DocCommand from its `name`
	#
	# You must redefine this method to add new custom commands.
	fun new_command(name: String): nullable DocCommand do
		# CmdEntity
		if name == "link" then return new CmdEntityLink(model)
		if name == "doc" then return new CmdComment(model)
		if name == "code" then return new CmdEntityCode(model, modelbuilder)
		if name == "lin" then return new CmdLinearization(model, mainmodule)
		if name == "defs" then return new CmdFeatures(model)
		if name == "parents" then return new CmdParents(model, mainmodule)
		if name == "ancestors" then return new CmdAncestors(model, mainmodule)
		if name == "children" then return new CmdChildren(model, mainmodule)
		if name == "descendants" then return new CmdDescendants(model, mainmodule)
		if name == "param" then return new CmdParam(model)
		if name == "return" then return new CmdReturn(model)
		if name == "new" then return new CmdNew(model, modelbuilder)
		if name == "call" then return new CmdCall(model, modelbuilder)
		# CmdGraph
		if name == "uml" then return new CmdUML(model, mainmodule)
		if name == "graph" then return new CmdInheritanceGraph(model, mainmodule)
		# CmdModel
		if name == "list" then return new CmdModelEntities(model)
		if name == "random" then return new CmdRandomEntities(model)
		# Ini
		if name == "ini-desc" then return new CmdIniDescription(model)
		if name == "ini-git" then return new CmdIniGitUrl(model)
		if name == "ini-issues" then return new CmdIniIssuesUrl(model)
		if name == "ini-license" then return new CmdIniLicense(model)
		if name == "ini-maintainer" then return new CmdIniMaintainer(model)
		if name == "ini-contributors" then return new CmdIniContributors(model)
		if name == "license-file" then return new CmdLicenseFile(model)
		if name == "license-content" then return new CmdLicenseFileContent(model)
		if name == "contrib-file" then return new CmdContribFile(model)
		if name == "contrib-content" then return new CmdContribFileContent(model)
		if name == "git-clone" then return new CmdIniCloneCommand(model)
		# CmdMain
		if name == "mains" then return new CmdMains(model)
		if name == "main-compile" then return new CmdMainCompile(model)
		if name == "main-run" then return new CmdManSynopsis(model)
		if name == "main-opts" then return new CmdManOptions(model)
		if name == "testing" then return new CmdTesting(model)
		# CmdCatalog
		var catalog = self.catalog
		if catalog != null then
			if name == "catalog" then return new CmdCatalogPackages(model, catalog)
			if name == "stats" then return new CmdCatalogStats(model, catalog)
			if name == "tags" then return new CmdCatalogTags(model, catalog)
			if name == "tag" then return new CmdCatalogTag(model, catalog)
			if name == "person" then return new CmdCatalogPerson(model, catalog)
			if name == "contrib" then return new CmdCatalogContributing(model, catalog)
			if name == "maintain" then return new CmdCatalogMaintaining(model, catalog)
			if name == "search" then return new CmdCatalogSearch(model, catalog)
		else
			if name == "search" then return new CmdSearch(model)
		end
		return null
	end

	# Error or warning from last call to `parse`
	var error: nullable CmdMessage = null
end
src/doc/commands/commands_parser.nit:26,1--239,3

nitc :: term $ CommandParser
redef class CommandParser

	fun execute(query: String, no_color: nullable Bool) do
		var cmd = self.parse(query)
		var error = self.error

		# Translate links to doc commands
		if cmd isa CmdEntityLink then
			cmd = new CmdComment(model, mentity_name = query)
			var opts = new CmdOptions
			var status = cmd.parser_init(query, opts)
			if not status isa CmdSuccess then error = status
		end

		if error isa CmdError or error isa CmdWarning then
			error.print_message(no_color)
			print ""
		end
		if cmd == null then return
		cmd.execute(no_color)
	end
end
src/doc/term/term.nit:21,1--42,3