Property definitions

nitc $ CommandParser :: defaultinit
# 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