* follow-up #17837: add `Console` for interactive sessions * fix Latex
This commit is contained in:
parent
706562f661
commit
436af88d8c
14 changed files with 252 additions and 155 deletions
|
|
@ -137,6 +137,8 @@ bottomline=false}
|
||||||
\newcommand{\spanReference}[1]{#1}
|
\newcommand{\spanReference}[1]{#1}
|
||||||
\newcommand{\spanOther}[1]{#1}
|
\newcommand{\spanOther}[1]{#1}
|
||||||
\newcommand{\spantok}[1]{\frame{#1}}
|
\newcommand{\spantok}[1]{\frame{#1}}
|
||||||
|
\newcommand{\spanPrompt}[1]{\textcolor{red}{\textbf{#1}}}
|
||||||
|
\newcommand{\spanProgramOutput}[1]{\textcolor{gray}{\textbf{#1}}}
|
||||||
\newcommand{\spanprogram}[1]{\textbf{\underline{#1}}}
|
\newcommand{\spanprogram}[1]{\textbf{\underline{#1}}}
|
||||||
\newcommand{\spanoption}[1]{\textbf{#1}}
|
\newcommand{\spanoption}[1]{\textbf{#1}}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,3 @@
|
||||||
.. default-role:: code
|
|
||||||
|
|
||||||
==================================
|
==================================
|
||||||
Nim Destructors and Move Semantics
|
Nim Destructors and Move Semantics
|
||||||
==================================
|
==================================
|
||||||
|
|
@ -7,6 +5,8 @@ Nim Destructors and Move Semantics
|
||||||
:Authors: Andreas Rumpf
|
:Authors: Andreas Rumpf
|
||||||
:Version: |nimversion|
|
:Version: |nimversion|
|
||||||
|
|
||||||
|
.. include:: rstcommon.rst
|
||||||
|
.. default-role:: nim
|
||||||
.. contents::
|
.. contents::
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -273,7 +273,7 @@ Sink parameter inference
|
||||||
========================
|
========================
|
||||||
|
|
||||||
The current implementation can do a limited form of sink parameter
|
The current implementation can do a limited form of sink parameter
|
||||||
inference. But it has to be enabled via `--sinkInference:on`, either
|
inference. But it has to be enabled via `--sinkInference:on`:option:, either
|
||||||
on the command line or via a `push` pragma.
|
on the command line or via a `push` pragma.
|
||||||
|
|
||||||
To enable it for a section of code, one can
|
To enable it for a section of code, one can
|
||||||
|
|
@ -496,10 +496,11 @@ for expressions of type `lent T` or of type `var T`.
|
||||||
The .cursor annotation
|
The .cursor annotation
|
||||||
======================
|
======================
|
||||||
|
|
||||||
Under the `--gc:arc|orc` modes Nim's `ref` type is implemented via the same runtime
|
Under the `--gc:arc|orc`:option: modes Nim's `ref` type is implemented
|
||||||
"hooks" and thus via reference counting. This means that cyclic structures cannot be freed
|
via the same runtime "hooks" and thus via reference counting.
|
||||||
immediately (`--gc:orc` ships with a cycle collector). With the `.cursor` annotation
|
This means that cyclic structures cannot be freed
|
||||||
one can break up cycles declaratively:
|
immediately (`--gc:orc`:option: ships with a cycle collector).
|
||||||
|
With the `.cursor` annotation one can break up cycles declaratively:
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
|
|
||||||
|
|
|
||||||
145
doc/docgen.rst
145
doc/docgen.rst
|
|
@ -1,5 +1,3 @@
|
||||||
.. default-role:: code
|
|
||||||
|
|
||||||
===================================
|
===================================
|
||||||
Nim DocGen Tools Guide
|
Nim DocGen Tools Guide
|
||||||
===================================
|
===================================
|
||||||
|
|
@ -7,6 +5,8 @@
|
||||||
:Author: Erik O'Leary
|
:Author: Erik O'Leary
|
||||||
:Version: |nimversion|
|
:Version: |nimversion|
|
||||||
|
|
||||||
|
.. include:: rstcommon.rst
|
||||||
|
.. default-role:: Nim
|
||||||
.. contents::
|
.. contents::
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -17,20 +17,22 @@ This document describes the `documentation generation tools`:idx: built into
|
||||||
the `Nim compiler <nimc.html>`_, which can generate HTML and JSON output
|
the `Nim compiler <nimc.html>`_, which can generate HTML and JSON output
|
||||||
from input .nim files and projects, as well as HTML and LaTeX from input RST
|
from input .nim files and projects, as well as HTML and LaTeX from input RST
|
||||||
(reStructuredText) files. The output documentation will include the module
|
(reStructuredText) files. The output documentation will include the module
|
||||||
dependencies (`import`), any top-level documentation comments (##), and
|
dependencies (`import`), any top-level documentation comments (`##`), and
|
||||||
exported symbols (*), including procedures, types, and variables.
|
exported symbols (`*`), including procedures, types, and variables.
|
||||||
|
|
||||||
Quick start
|
Quick start
|
||||||
-----------
|
-----------
|
||||||
|
|
||||||
Generate HTML documentation for a file:
|
Generate HTML documentation for a file:
|
||||||
|
|
||||||
::
|
.. code:: cmd
|
||||||
|
|
||||||
nim doc <filename>.nim
|
nim doc <filename>.nim
|
||||||
|
|
||||||
Generate HTML documentation for a whole project:
|
Generate HTML documentation for a whole project:
|
||||||
|
|
||||||
::
|
.. code:: cmd
|
||||||
|
|
||||||
# delete any htmldocs/*.idx file before starting
|
# delete any htmldocs/*.idx file before starting
|
||||||
nim doc --project --index:on --git.url:<url> --git.commit:<tag> --outdir:htmldocs <main_filename>.nim
|
nim doc --project --index:on --git.url:<url> --git.commit:<tag> --outdir:htmldocs <main_filename>.nim
|
||||||
# this will generate html files, a theindex.html index, css and js under `htmldocs`
|
# this will generate html files, a theindex.html index, css and js under `htmldocs`
|
||||||
|
|
@ -39,15 +41,15 @@ Generate HTML documentation for a whole project:
|
||||||
# CORS will prevent opening file:// urls; this works:
|
# CORS will prevent opening file:// urls; this works:
|
||||||
python3 -m http.server 7029 --directory htmldocs
|
python3 -m http.server 7029 --directory htmldocs
|
||||||
# When --outdir is omitted it defaults to $projectPath/htmldocs,
|
# When --outdir is omitted it defaults to $projectPath/htmldocs,
|
||||||
or `$nimcache/htmldocs` with `--usenimcache` which avoids clobbering your sources;
|
# or `$nimcache/htmldocs` with `--usenimcache` which avoids clobbering your sources;
|
||||||
and likewise without `--project`.
|
# and likewise without `--project`.
|
||||||
Adding `-r` will open in a browser directly.
|
# Adding `-r` will open in a browser directly.
|
||||||
|
|
||||||
|
|
||||||
Documentation Comments
|
Documentation Comments
|
||||||
----------------------
|
----------------------
|
||||||
|
|
||||||
Any comments which are preceded by a double-hash (##), are interpreted as
|
Any comments which are preceded by a double-hash (`##`), are interpreted as
|
||||||
documentation. Comments are parsed as RST (see `reference
|
documentation. Comments are parsed as RST (see `reference
|
||||||
<http://docutils.sourceforge.net/docs/user/rst/quickref.html>`_), providing
|
<http://docutils.sourceforge.net/docs/user/rst/quickref.html>`_), providing
|
||||||
Nim module authors the ability to easily generate richly formatted
|
Nim module authors the ability to easily generate richly formatted
|
||||||
|
|
@ -66,7 +68,7 @@ Outputs::
|
||||||
name: string
|
name: string
|
||||||
age: int
|
age: int
|
||||||
|
|
||||||
This type contains a description of a person
|
This type contains a description of a person
|
||||||
|
|
||||||
Field documentation comments can be added to fields like so:
|
Field documentation comments can be added to fields like so:
|
||||||
|
|
||||||
|
|
@ -127,12 +129,15 @@ Document Types
|
||||||
HTML
|
HTML
|
||||||
----
|
----
|
||||||
|
|
||||||
The generation of HTML documents is done via the `doc` command. This command
|
The generation of HTML documents is done via the `doc`:option: command. This command
|
||||||
takes either a single .nim file, outputting a single .html file with the same
|
takes either a single ``.nim`` file, outputting a single ``.html`` file with the same
|
||||||
base filename, or multiple .nim files, outputting multiple .html files and,
|
base filename, or multiple ``.nim`` files, outputting multiple ``.html`` files and,
|
||||||
optionally, an index file.
|
optionally, an index file.
|
||||||
|
|
||||||
The `doc` command::
|
The `doc`:option: command:
|
||||||
|
|
||||||
|
.. code:: cmd
|
||||||
|
|
||||||
nim doc sample
|
nim doc sample
|
||||||
|
|
||||||
Partial Output::
|
Partial Output::
|
||||||
|
|
@ -148,12 +153,16 @@ compiler.
|
||||||
JSON
|
JSON
|
||||||
----
|
----
|
||||||
|
|
||||||
The generation of JSON documents is done via the `jsondoc` command. This command
|
The generation of JSON documents is done via the `jsondoc`:option: command.
|
||||||
takes in a .nim file and outputs a .json file with the same base filename. Note
|
This command takes in a ``.nim`` file and outputs a ``.json`` file with
|
||||||
that this tool is built off of the `doc` command (previously `doc2`), and
|
the same base filename.
|
||||||
contains the same information.
|
Note that this tool is built off of the `doc`:option: command
|
||||||
|
(previously `doc2`:option:), and contains the same information.
|
||||||
|
|
||||||
|
The `jsondoc`:option: command:
|
||||||
|
|
||||||
|
.. code:: cmd
|
||||||
|
|
||||||
The `jsondoc` command::
|
|
||||||
nim jsondoc sample
|
nim jsondoc sample
|
||||||
|
|
||||||
Output::
|
Output::
|
||||||
|
|
@ -173,10 +182,13 @@ Output::
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
Similarly to the old `doc` command, the old `jsondoc` command has been
|
Similarly to the old `doc`:option: command, the old `jsondoc`:option: command has been
|
||||||
renamed to `jsondoc0`.
|
renamed to `jsondoc0`:option:.
|
||||||
|
|
||||||
|
The `jsondoc0`:option: command:
|
||||||
|
|
||||||
|
.. code:: cmd
|
||||||
|
|
||||||
The `jsondoc0` command::
|
|
||||||
nim jsondoc0 sample
|
nim jsondoc0 sample
|
||||||
|
|
||||||
Output::
|
Output::
|
||||||
|
|
@ -192,8 +204,8 @@ Output::
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
Note that the `jsondoc` command outputs it's JSON without pretty-printing it,
|
Note that the `jsondoc`:option: command outputs it's JSON without pretty-printing it,
|
||||||
while `jsondoc0` outputs pretty-printed JSON.
|
while `jsondoc0`:option: outputs pretty-printed JSON.
|
||||||
|
|
||||||
Related Options
|
Related Options
|
||||||
===============
|
===============
|
||||||
|
|
@ -201,22 +213,24 @@ Related Options
|
||||||
Project switch
|
Project switch
|
||||||
--------------
|
--------------
|
||||||
|
|
||||||
::
|
.. code:: cmd
|
||||||
|
|
||||||
nim doc --project filename.nim
|
nim doc --project filename.nim
|
||||||
|
|
||||||
This will recursively generate documentation of all nim modules imported
|
This will recursively generate documentation of all nim modules imported
|
||||||
into the input module that belong to the Nimble package that `filename.nim`
|
into the input module that belong to the Nimble package that ``filename.nim``
|
||||||
belongs to.
|
belongs to.
|
||||||
|
|
||||||
|
|
||||||
Index switch
|
Index switch
|
||||||
------------
|
------------
|
||||||
|
|
||||||
::
|
.. code:: cmd
|
||||||
|
|
||||||
nim doc --index:on filename.nim
|
nim doc --index:on filename.nim
|
||||||
|
|
||||||
This will generate an index of all the exported symbols in the input Nim
|
This will generate an index of all the exported symbols in the input Nim
|
||||||
module, and put it into a neighboring file with the extension of `.idx`. The
|
module, and put it into a neighboring file with the extension of ``.idx``. The
|
||||||
index file is line-oriented (newlines have to be escaped). Each line
|
index file is line-oriented (newlines have to be escaped). Each line
|
||||||
represents a tab-separated record of several columns, the first two mandatory,
|
represents a tab-separated record of several columns, the first two mandatory,
|
||||||
the rest optional. See the `Index (idx) file format`_ section for details.
|
the rest optional. See the `Index (idx) file format`_ section for details.
|
||||||
|
|
@ -229,31 +243,37 @@ file.
|
||||||
See source switch
|
See source switch
|
||||||
-----------------
|
-----------------
|
||||||
|
|
||||||
::
|
.. code:: cmd
|
||||||
|
|
||||||
nim doc --git.url:<url> filename.nim
|
nim doc --git.url:<url> filename.nim
|
||||||
|
|
||||||
With the `git.url` switch the *See source* hyperlink will appear below each
|
With the `git.url`:option: switch the *See source* hyperlink will appear below each
|
||||||
documented item in your source code pointing to the implementation of that
|
documented item in your source code pointing to the implementation of that
|
||||||
item on a GitHub repository.
|
item on a GitHub repository.
|
||||||
You can click the link to see the implementation of the item.
|
You can click the link to see the implementation of the item.
|
||||||
|
|
||||||
The `git.commit` switch overrides the hardcoded `devel` branch in config/nimdoc.cfg.
|
The `git.commit`:option: switch overrides the hardcoded `devel` branch in config/nimdoc.cfg.
|
||||||
This is useful to link to a different branch e.g. `--git.commit:master`,
|
This is useful to link to a different branch e.g. `--git.commit:master`:option:,
|
||||||
or to a tag e.g. `--git.commit:1.2.3` or a commit.
|
or to a tag e.g. `--git.commit:1.2.3`:option: or a commit.
|
||||||
|
|
||||||
Source URLs are generated as `href="${url}/tree/${commit}/${path}#L${line}"` by default and this compatible with GitHub but not with GitLab.
|
Source URLs are generated as ``href="${url}/tree/${commit}/${path}#L${line}"``
|
||||||
|
by default and this compatible with GitHub but not with GitLab.
|
||||||
|
|
||||||
Similarly, `git.devel` switch overrides the hardcoded `devel` branch for the `Edit` link which is also useful if you have a different working branch than `devel` e.g. `--git.devel:master`.
|
Similarly, `git.devel`:option: switch overrides the hardcoded `devel` branch
|
||||||
|
for the `Edit` link which is also useful if you have a different working
|
||||||
|
branch than `devel` e.g. `--git.devel:master`:option:.
|
||||||
|
|
||||||
Edit URLs are generated as `href="${url}/tree/${devel}/${path}#L${line}"` by default.
|
Edit URLs are generated as ``href="${url}/tree/${devel}/${path}#L${line}"``
|
||||||
|
by default.
|
||||||
|
|
||||||
You can edit `config/nimdoc.cfg` and modify the `doc.item.seesrc` value with a hyperlink to your own code repository.
|
You can edit ``config/nimdoc.cfg`` and modify the ``doc.item.seesrc`` value
|
||||||
|
with a hyperlink to your own code repository.
|
||||||
|
|
||||||
In the case of Nim's own documentation, the `commit` value is just a commit
|
In the case of Nim's own documentation, the `commit` value is just a commit
|
||||||
hash to append to a formatted URL to https://github.com/nim-lang/Nim. The
|
hash to append to a formatted URL to https://github.com/nim-lang/Nim. The
|
||||||
`tools/nimweb.nim` helper queries the current git commit hash during the doc
|
``tools/nimweb.nim`` helper queries the current git commit hash during the doc
|
||||||
generation, but since you might be working on an unpublished repository, it
|
generation, but since you might be working on an unpublished repository, it
|
||||||
also allows specifying a `githash` value in `web/website.ini` to force a
|
also allows specifying a `githash` value in ``web/website.ini`` to force a
|
||||||
specific commit in the output.
|
specific commit in the output.
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -261,28 +281,31 @@ Other Input Formats
|
||||||
===================
|
===================
|
||||||
|
|
||||||
The *Nim compiler* also has support for RST (reStructuredText) files with
|
The *Nim compiler* also has support for RST (reStructuredText) files with
|
||||||
the `rst2html` and `rst2tex` commands. Documents like this one are
|
the `rst2html`:option: and `rst2tex`:option: commands. Documents like this one are
|
||||||
initially written in a dialect of RST which adds support for nim source code
|
initially written in a dialect of RST which adds support for nim source code
|
||||||
highlighting with the `.. code-block:: nim` prefix. `code-block` also
|
highlighting with the ``.. code-block:: nim`` prefix. ``code-block`` also
|
||||||
supports highlighting of C++ and some other c-like languages.
|
supports highlighting of C++ and some other c-like languages.
|
||||||
|
|
||||||
Usage::
|
Usage:
|
||||||
nim rst2html docgen.txt
|
|
||||||
|
.. code:: cmd
|
||||||
|
|
||||||
|
nim rst2html docgen.rst
|
||||||
|
|
||||||
Output::
|
Output::
|
||||||
You're reading it!
|
You're reading it!
|
||||||
|
|
||||||
The `rst2tex` command is invoked identically to `rst2html`, but outputs
|
The `rst2tex`:option: command is invoked identically to `rst2html`:option:,
|
||||||
a .tex file instead of .html.
|
but outputs a ``.tex`` file instead of ``.html``.
|
||||||
|
|
||||||
|
|
||||||
HTML anchor generation
|
HTML anchor generation
|
||||||
======================
|
======================
|
||||||
|
|
||||||
When you run the `rst2html` command, all sections in the RST document will
|
When you run the `rst2html`:option: command, all sections in the RST document will
|
||||||
get an anchor you can hyperlink to. Usually, you can guess the anchor lower
|
get an anchor you can hyperlink to. Usually, you can guess the anchor lower
|
||||||
casing the section title and replacing spaces with dashes, and in any case, you
|
casing the section title and replacing spaces with dashes, and in any case, you
|
||||||
can get it from the table of contents. But when you run the `doc`
|
can get it from the table of contents. But when you run the `doc`:option:
|
||||||
command to generate API documentation, some symbol get one or two anchors at
|
command to generate API documentation, some symbol get one or two anchors at
|
||||||
the same time: a numerical identifier, or a plain name plus a complex name.
|
the same time: a numerical identifier, or a plain name plus a complex name.
|
||||||
|
|
||||||
|
|
@ -314,15 +337,15 @@ suffix may be added depending on the type of the callable:
|
||||||
Callable type Suffix
|
Callable type Suffix
|
||||||
------------- --------------
|
------------- --------------
|
||||||
proc *empty string*
|
proc *empty string*
|
||||||
macro `.m`
|
macro ``.m``
|
||||||
method `.e`
|
method ``.e``
|
||||||
iterator `.i`
|
iterator ``.i``
|
||||||
template `.t`
|
template ``.t``
|
||||||
converter `.c`
|
converter ``.c``
|
||||||
------------- --------------
|
------------- --------------
|
||||||
|
|
||||||
The relationship of type to suffix is made by the proc `complexName` in the
|
The relationship of type to suffix is made by the proc `complexName` in the
|
||||||
`compiler/docgen.nim` file. Here are some examples of complex names for
|
``compiler/docgen.nim`` file. Here are some examples of complex names for
|
||||||
symbols in the `system module <system.html>`_.
|
symbols in the `system module <system.html>`_.
|
||||||
|
|
||||||
* `type SomeSignedInt = int | int8 | int16 | int32 | int64` **=>**
|
* `type SomeSignedInt = int | int8 | int16 | int32 | int64` **=>**
|
||||||
|
|
@ -346,7 +369,7 @@ symbols in the `system module <system.html>`_.
|
||||||
Index (idx) file format
|
Index (idx) file format
|
||||||
=======================
|
=======================
|
||||||
|
|
||||||
Files with the `.idx` extension are generated when you use the `Index
|
Files with the ``.idx`` extension are generated when you use the `Index
|
||||||
switch <#related-options-index-switch>`_ along with commands to generate
|
switch <#related-options-index-switch>`_ along with commands to generate
|
||||||
documentation from source or text files. You can programmatically generate
|
documentation from source or text files. You can programmatically generate
|
||||||
indices with the `setIndexTerm()
|
indices with the `setIndexTerm()
|
||||||
|
|
@ -364,7 +387,7 @@ columns is:
|
||||||
|
|
||||||
1. Mandatory term being indexed. Terms can include quoting according to
|
1. Mandatory term being indexed. Terms can include quoting according to
|
||||||
Nim's rules (e.g. \`^\`).
|
Nim's rules (e.g. \`^\`).
|
||||||
2. Base filename plus anchor hyperlink (e.g. `algorithm.html#*,int,SortOrder`).
|
2. Base filename plus anchor hyperlink (e.g. ``algorithm.html#*,int,SortOrder``).
|
||||||
3. Optional human-readable string to display as a hyperlink. If the value is not
|
3. Optional human-readable string to display as a hyperlink. If the value is not
|
||||||
present or is the empty string, the hyperlink will be rendered
|
present or is the empty string, the hyperlink will be rendered
|
||||||
using the term. Prefix whitespace indicates that this entry is
|
using the term. Prefix whitespace indicates that this entry is
|
||||||
|
|
@ -373,8 +396,8 @@ columns is:
|
||||||
this as a tooltip after hovering a moment over the hyperlink.
|
this as a tooltip after hovering a moment over the hyperlink.
|
||||||
|
|
||||||
The index generation tools try to differentiate between documentation
|
The index generation tools try to differentiate between documentation
|
||||||
generated from `.nim` files and documentation generated from `.txt` or
|
generated from ``.nim`` files and documentation generated from ``.txt`` or
|
||||||
`.rst` files. The former are always closely related to source code and
|
``.rst`` files. The former are always closely related to source code and
|
||||||
consist mainly of API entries. The latter are generic documents meant for
|
consist mainly of API entries. The latter are generic documents meant for
|
||||||
human reading.
|
human reading.
|
||||||
|
|
||||||
|
|
@ -393,7 +416,7 @@ the index file with their third column having as much prefix spaces as their
|
||||||
level is in the TOC (at least 1 character). The prefix whitespace helps to
|
level is in the TOC (at least 1 character). The prefix whitespace helps to
|
||||||
filter TOC entries from API or text symbols. This is important because the
|
filter TOC entries from API or text symbols. This is important because the
|
||||||
amount of spaces is used to replicate the hierarchy for document TOCs in the
|
amount of spaces is used to replicate the hierarchy for document TOCs in the
|
||||||
final index, and TOC entries found in `.nim` files are discarded.
|
final index, and TOC entries found in ``.nim`` files are discarded.
|
||||||
|
|
||||||
|
|
||||||
Additional resources
|
Additional resources
|
||||||
|
|
@ -404,8 +427,8 @@ Additional resources
|
||||||
`RST Quick Reference
|
`RST Quick Reference
|
||||||
<http://docutils.sourceforge.net/docs/user/rst/quickref.html>`_
|
<http://docutils.sourceforge.net/docs/user/rst/quickref.html>`_
|
||||||
|
|
||||||
The output for HTML and LaTeX comes from the `config/nimdoc.cfg` and
|
The output for HTML and LaTeX comes from the ``config/nimdoc.cfg`` and
|
||||||
`config/nimdoc.tex.cfg` configuration files. You can add and modify these
|
``config/nimdoc.tex.cfg`` configuration files. You can add and modify these
|
||||||
files to your project to change the look of the docgen output.
|
files to your project to change the look of the docgen output.
|
||||||
|
|
||||||
You can import the `packages/docutils/rstgen module <rstgen.html>`_ in your
|
You can import the `packages/docutils/rstgen module <rstgen.html>`_ in your
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,3 @@
|
||||||
.. default-role:: code
|
|
||||||
|
|
||||||
===================================
|
===================================
|
||||||
DrNim User Guide
|
DrNim User Guide
|
||||||
===================================
|
===================================
|
||||||
|
|
@ -7,6 +5,8 @@
|
||||||
:Author: Andreas Rumpf
|
:Author: Andreas Rumpf
|
||||||
:Version: |nimversion|
|
:Version: |nimversion|
|
||||||
|
|
||||||
|
.. include:: rstcommon.rst
|
||||||
|
.. default-role:: Nim
|
||||||
.. contents::
|
.. contents::
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -43,7 +43,8 @@ your code.
|
||||||
Installation
|
Installation
|
||||||
============
|
============
|
||||||
|
|
||||||
Run `koch drnim`, the executable will afterwards be in `$nim/bin/drnim`.
|
Run `koch drnim`:option:, the executable will afterwards be
|
||||||
|
in ``$nim/bin/drnim``.
|
||||||
|
|
||||||
|
|
||||||
Motivating Example
|
Motivating Example
|
||||||
|
|
|
||||||
22
doc/estp.rst
22
doc/estp.rst
|
|
@ -1,9 +1,10 @@
|
||||||
.. default-role:: code
|
|
||||||
|
|
||||||
===================================================
|
===================================================
|
||||||
Embedded Stack Trace Profiler (ESTP) User Guide
|
Embedded Stack Trace Profiler (ESTP) User Guide
|
||||||
===================================================
|
===================================================
|
||||||
|
|
||||||
|
.. include:: rstcommon.rst
|
||||||
|
.. default-role:: Nim
|
||||||
|
|
||||||
:Author: Andreas Rumpf
|
:Author: Andreas Rumpf
|
||||||
:Version: |nimversion|
|
:Version: |nimversion|
|
||||||
|
|
||||||
|
|
@ -12,7 +13,7 @@ Nim comes with a platform independent profiler -
|
||||||
the Embedded Stack Trace Profiler (ESTP). The profiler
|
the Embedded Stack Trace Profiler (ESTP). The profiler
|
||||||
is *embedded* into your executable. To activate the profiler you need to do:
|
is *embedded* into your executable. To activate the profiler you need to do:
|
||||||
|
|
||||||
* compile your program with the `--profiler:on --stackTrace:on` command
|
* compile your program with the `--profiler:on --stackTrace:on`:option: command
|
||||||
line options
|
line options
|
||||||
* import the `nimprof` module
|
* import the `nimprof` module
|
||||||
* run your program as usual.
|
* run your program as usual.
|
||||||
|
|
@ -20,10 +21,10 @@ is *embedded* into your executable. To activate the profiler you need to do:
|
||||||
You can in fact look at `nimprof`'s source code to see how to implement
|
You can in fact look at `nimprof`'s source code to see how to implement
|
||||||
your own profiler.
|
your own profiler.
|
||||||
|
|
||||||
The setting `--profiler:on` defines the conditional symbol `profiler`.
|
The setting `--profiler:on`:option: defines the conditional symbol `profiler`.
|
||||||
You can use `when compileOption("profiler")` to make the switch seamless.
|
You can use `when compileOption("profiler")` to make the switch seamless.
|
||||||
If `profiler` is `off`, your program runs normally. Otherwise your program
|
If `profiler`:option: is `off`:option:, your program runs normally.
|
||||||
is profiled.
|
Otherwise your program is profiled.
|
||||||
|
|
||||||
```nim
|
```nim
|
||||||
when compileOption("profiler"):
|
when compileOption("profiler"):
|
||||||
|
|
@ -31,10 +32,10 @@ when compileOption("profiler"):
|
||||||
```
|
```
|
||||||
|
|
||||||
After your program has finished the profiler will create a
|
After your program has finished the profiler will create a
|
||||||
file `profile_results.txt` containing the profiling results.
|
file ``profile_results.txt`` containing the profiling results.
|
||||||
|
|
||||||
Since the profiler works by examining stack traces, it's essential that
|
Since the profiler works by examining stack traces, it's essential that
|
||||||
the option `--stackTrace:on` is active! Unfortunately this means that a
|
the option `--stackTrace:on`:option: is active! Unfortunately this means that a
|
||||||
profiling build is much slower than a release build.
|
profiling build is much slower than a release build.
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -45,8 +46,9 @@ You can also use ESTP as a memory profiler to see which stack traces allocate
|
||||||
the most memory and thus create the most GC pressure. It may also help to
|
the most memory and thus create the most GC pressure. It may also help to
|
||||||
find memory leaks. To activate the memory profiler you need to do:
|
find memory leaks. To activate the memory profiler you need to do:
|
||||||
|
|
||||||
* compile your program with the `--profiler:off --stackTrace:on -d:memProfiler`
|
* compile your program with the
|
||||||
command line options. Yes it's `--profiler:off`.
|
`--profiler:off --stackTrace:on -d:memProfiler`:option:
|
||||||
|
command line options. Yes it's `--profiler:off`:option:.
|
||||||
* import the `nimprof` module
|
* import the `nimprof` module
|
||||||
* run your program as usual.
|
* run your program as usual.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ Short description of Nim's modules
|
||||||
Module Description
|
Module Description
|
||||||
============== ==========================================================
|
============== ==========================================================
|
||||||
nim main module: parses the command line and calls
|
nim main module: parses the command line and calls
|
||||||
``main.MainCommand``
|
`main.MainCommand`
|
||||||
main implements the top-level command dispatching
|
main implements the top-level command dispatching
|
||||||
nimconf implements the config file reader
|
nimconf implements the config file reader
|
||||||
syntaxes dispatcher for the different parsers and filters
|
syntaxes dispatcher for the different parsers and filters
|
||||||
|
|
@ -36,7 +36,7 @@ evals contains an AST interpreter for compile time evaluation
|
||||||
pragmas semantic checking of pragmas
|
pragmas semantic checking of pragmas
|
||||||
|
|
||||||
idents implements a general mapping from identifiers to an internal
|
idents implements a general mapping from identifiers to an internal
|
||||||
representation (``PIdent``) that is used so that a simple
|
representation (`PIdent`) that is used so that a simple
|
||||||
id-comparison suffices to establish whether two Nim
|
id-comparison suffices to establish whether two Nim
|
||||||
identifiers are equivalent
|
identifiers are equivalent
|
||||||
ropes implements long strings represented as trees for
|
ropes implements long strings represented as trees for
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
.. default-role:: code
|
|
||||||
|
|
||||||
===================
|
===================
|
||||||
Source Code Filters
|
Source Code Filters
|
||||||
===================
|
===================
|
||||||
|
|
||||||
|
.. include:: rstcommon.rst
|
||||||
|
.. default-role:: code
|
||||||
.. contents::
|
.. contents::
|
||||||
|
|
||||||
A `Source Code Filter (SCF)` transforms the input character stream to an in-memory
|
A `Source Code Filter (SCF)` transforms the input character stream to an in-memory
|
||||||
|
|
@ -25,8 +25,8 @@ just like an ordinary procedure call with named or positional arguments. The
|
||||||
available parameters depend on the invoked filter. Before version 0.12.0 of
|
available parameters depend on the invoked filter. Before version 0.12.0 of
|
||||||
the language `#!` was used instead of `#?`.
|
the language `#!` was used instead of `#?`.
|
||||||
|
|
||||||
**Hint:** With `--hint[codeBegin]:on` or `--verbosity:2`
|
**Hint:** With `--hint[codeBegin]:on`:option: or `--verbosity:2`:option:
|
||||||
(or higher) while compiling or `nim check`, Nim lists the processed code after
|
(or higher) while compiling or `nim check`:cmd:, Nim lists the processed code after
|
||||||
each filter application.
|
each filter application.
|
||||||
|
|
||||||
Usage
|
Usage
|
||||||
|
|
@ -70,10 +70,10 @@ The replace filter replaces substrings in each line.
|
||||||
|
|
||||||
Parameters and their defaults:
|
Parameters and their defaults:
|
||||||
|
|
||||||
`sub: string = ""`
|
* `sub: string = ""`
|
||||||
the substring that is searched for
|
the substring that is searched for
|
||||||
|
|
||||||
`by: string = ""`
|
* `by: string = ""`
|
||||||
the string the substring is replaced with
|
the string the substring is replaced with
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -85,14 +85,14 @@ each line.
|
||||||
|
|
||||||
Parameters and their defaults:
|
Parameters and their defaults:
|
||||||
|
|
||||||
`startswith: string = ""`
|
* `startswith: string = ""`
|
||||||
strip only the lines that start with *startswith* (ignoring leading
|
strip only the lines that start with *startswith* (ignoring leading
|
||||||
whitespace). If empty every line is stripped.
|
whitespace). If empty every line is stripped.
|
||||||
|
|
||||||
`leading: bool = true`
|
* `leading: bool = true`
|
||||||
strip leading whitespace
|
strip leading whitespace
|
||||||
|
|
||||||
`trailing: bool = true`
|
* `trailing: bool = true`
|
||||||
strip trailing whitespace
|
strip trailing whitespace
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -107,19 +107,19 @@ statements need `end X` delimiters.
|
||||||
|
|
||||||
Parameters and their defaults:
|
Parameters and their defaults:
|
||||||
|
|
||||||
`metaChar: char = '#'`
|
* `metaChar: char = '#'`
|
||||||
prefix for a line that contains Nim code
|
prefix for a line that contains Nim code
|
||||||
|
|
||||||
`subsChar: char = '$'`
|
* `subsChar: char = '$'`
|
||||||
prefix for a Nim expression within a template line
|
prefix for a Nim expression within a template line
|
||||||
|
|
||||||
`conc: string = " & "`
|
* `conc: string = " & "`
|
||||||
the operation for concatenation
|
the operation for concatenation
|
||||||
|
|
||||||
`emit: string = "result.add"`
|
* `emit: string = "result.add"`
|
||||||
the operation to emit a string literal
|
the operation to emit a string literal
|
||||||
|
|
||||||
`toString: string = "$"`
|
* `toString: string = "$"`
|
||||||
the operation that is applied to each expression
|
the operation that is applied to each expression
|
||||||
|
|
||||||
Example::
|
Example::
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,3 @@
|
||||||
.. default-role:: code
|
|
||||||
|
|
||||||
=========================================
|
=========================================
|
||||||
Internals of the Nim Compiler
|
Internals of the Nim Compiler
|
||||||
=========================================
|
=========================================
|
||||||
|
|
@ -8,6 +6,8 @@
|
||||||
:Author: Andreas Rumpf
|
:Author: Andreas Rumpf
|
||||||
:Version: |nimversion|
|
:Version: |nimversion|
|
||||||
|
|
||||||
|
.. include:: rstcommon.rst
|
||||||
|
.. default-role:: Nim
|
||||||
.. contents::
|
.. contents::
|
||||||
|
|
||||||
"Abstraction is layering ignorance on top of reality." -- Richard Gabriel
|
"Abstraction is layering ignorance on top of reality." -- Richard Gabriel
|
||||||
|
|
@ -38,25 +38,31 @@ Path Purpose
|
||||||
Bootstrapping the compiler
|
Bootstrapping the compiler
|
||||||
==========================
|
==========================
|
||||||
|
|
||||||
**Note**: Add ``.`` to your PATH so that `koch` can be used without the `./`.
|
**Note**: Add ``.`` to your PATH so that `koch`:cmd: can be used without the ``./``.
|
||||||
|
|
||||||
Compiling the compiler is a simple matter of running::
|
Compiling the compiler is a simple matter of running:
|
||||||
|
|
||||||
|
.. code:: cmd
|
||||||
|
|
||||||
nim c koch.nim
|
nim c koch.nim
|
||||||
koch boot -d:release
|
koch boot -d:release
|
||||||
|
|
||||||
For a debug version use::
|
For a debug version use:
|
||||||
|
|
||||||
|
.. code:: cmd
|
||||||
|
|
||||||
nim c koch.nim
|
nim c koch.nim
|
||||||
koch boot
|
koch boot
|
||||||
|
|
||||||
|
|
||||||
And for a debug version compatible with GDB::
|
And for a debug version compatible with GDB:
|
||||||
|
|
||||||
|
.. code:: cmd
|
||||||
|
|
||||||
nim c koch.nim
|
nim c koch.nim
|
||||||
koch boot --debuginfo --linedir:on
|
koch boot --debuginfo --linedir:on
|
||||||
|
|
||||||
The `koch` program is Nim's maintenance script. It is a replacement for
|
The `koch`:cmd: program is Nim's maintenance script. It is a replacement for
|
||||||
make and shell scripting with the advantage that it is much more portable.
|
make and shell scripting with the advantage that it is much more portable.
|
||||||
More information about its options can be found in the `koch <koch.html>`_
|
More information about its options can be found in the `koch <koch.html>`_
|
||||||
documentation.
|
documentation.
|
||||||
|
|
@ -65,15 +71,17 @@ documentation.
|
||||||
Developing the compiler
|
Developing the compiler
|
||||||
=======================
|
=======================
|
||||||
|
|
||||||
To create a new compiler for each run, use `koch temp`::
|
To create a new compiler for each run, use `koch temp`:cmd:\:
|
||||||
|
|
||||||
|
.. code:: cmd
|
||||||
|
|
||||||
koch temp c test.nim
|
koch temp c test.nim
|
||||||
|
|
||||||
`koch temp` creates a debug build of the compiler, which is useful
|
`koch temp`:cmd: creates a debug build of the compiler, which is useful
|
||||||
to create stacktraces for compiler debugging.
|
to create stacktraces for compiler debugging.
|
||||||
|
|
||||||
You can of course use GDB or Visual Studio to debug the
|
You can of course use GDB or Visual Studio to debug the
|
||||||
compiler (via `--debuginfo --lineDir:on`). However, there
|
compiler (via `--debuginfo --lineDir:on`:option:). However, there
|
||||||
are also lots of procs that aid in debugging:
|
are also lots of procs that aid in debugging:
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -136,16 +144,18 @@ examples how the AST represents each syntactic structure.
|
||||||
Bisecting for regressions
|
Bisecting for regressions
|
||||||
=========================
|
=========================
|
||||||
|
|
||||||
`koch temp` returns 125 as the exit code in case the compiler
|
`koch temp`:cmd: returns 125 as the exit code in case the compiler
|
||||||
compilation fails. This exit code tells `git bisect` to skip the
|
compilation fails. This exit code tells `git bisect`:cmd: to skip the
|
||||||
current commit.::
|
current commit:
|
||||||
|
|
||||||
|
.. code:: cmd
|
||||||
|
|
||||||
git bisect start bad-commit good-commit
|
git bisect start bad-commit good-commit
|
||||||
git bisect run ./koch temp -r c test-source.nim
|
git bisect run ./koch temp -r c test-source.nim
|
||||||
|
|
||||||
You can also bisect using custom options to build the compiler, for example if
|
You can also bisect using custom options to build the compiler, for example if
|
||||||
you don't need a debug version of the compiler (which runs slower), you can replace
|
you don't need a debug version of the compiler (which runs slower), you can replace
|
||||||
`./koch temp` by explicit compilation command, see `Rebuilding the compiler`_.
|
`./koch temp`:cmd: by explicit compilation command, see `Rebuilding the compiler`_.
|
||||||
|
|
||||||
|
|
||||||
Runtimes
|
Runtimes
|
||||||
|
|
@ -182,7 +192,7 @@ check that the OS, System modules work and recompile Nim.
|
||||||
|
|
||||||
The only case where things aren't as easy is when old runtime's garbage
|
The only case where things aren't as easy is when old runtime's garbage
|
||||||
collectors need some assembler tweaking to work. The default
|
collectors need some assembler tweaking to work. The default
|
||||||
implementation uses C's `setjmp` function to store all registers
|
implementation uses C's `setjmp`:c: function to store all registers
|
||||||
on the hardware stack. It may be necessary that the new platform needs to
|
on the hardware stack. It may be necessary that the new platform needs to
|
||||||
replace this generic code by some assembler code.
|
replace this generic code by some assembler code.
|
||||||
|
|
||||||
|
|
@ -207,7 +217,7 @@ Complex assignments
|
||||||
|
|
||||||
We already know the type information as a graph in the compiler.
|
We already know the type information as a graph in the compiler.
|
||||||
Thus we need to serialize this graph as RTTI for C code generation.
|
Thus we need to serialize this graph as RTTI for C code generation.
|
||||||
Look at the file `lib/system/hti.nim` for more information.
|
Look at the file ``lib/system/hti.nim`` for more information.
|
||||||
|
|
||||||
|
|
||||||
Magics and compilerProcs
|
Magics and compilerProcs
|
||||||
|
|
@ -368,7 +378,7 @@ pass generates code to setup the environment and to pass it around. However,
|
||||||
this pass does not change the types! So we have some kind of mismatch here; on
|
this pass does not change the types! So we have some kind of mismatch here; on
|
||||||
the one hand the proc expression becomes an explicit tuple, on the other hand
|
the one hand the proc expression becomes an explicit tuple, on the other hand
|
||||||
the tyProc(ccClosure) type is not changed. For C code generation it's also
|
the tyProc(ccClosure) type is not changed. For C code generation it's also
|
||||||
important the hidden formal param is `void*` and not something more
|
important the hidden formal param is `void*`:c: and not something more
|
||||||
specialized. However the more specialized env type needs to passed to the
|
specialized. However the more specialized env type needs to passed to the
|
||||||
backend somehow. We deal with this by modifying `s.ast[paramPos]` to contain
|
backend somehow. We deal with this by modifying `s.ast[paramPos]` to contain
|
||||||
the formal hidden parameter, but not `s.typ`!
|
the formal hidden parameter, but not `s.typ`!
|
||||||
|
|
|
||||||
|
|
@ -938,6 +938,14 @@ span.option {
|
||||||
color: var(--option);
|
color: var(--option);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
span.Prompt {
|
||||||
|
font-weight: bold;
|
||||||
|
color: red; }
|
||||||
|
|
||||||
|
span.ProgramOutput {
|
||||||
|
font-weight: bold;
|
||||||
|
color: #808080; }
|
||||||
|
|
||||||
span.program {
|
span.program {
|
||||||
font-weight: bold;
|
font-weight: bold;
|
||||||
color: var(--program);
|
color: var(--program);
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,10 @@
|
||||||
.. default-role:: code
|
===========
|
||||||
|
Testament
|
||||||
|
===========
|
||||||
|
|
||||||
|
.. include:: rstcommon.rst
|
||||||
|
.. default-role:: nim
|
||||||
|
.. contents::
|
||||||
|
|
||||||
Testament is an advanced automatic unittests runner for Nim tests, is used for the development of Nim itself,
|
Testament is an advanced automatic unittests runner for Nim tests, is used for the development of Nim itself,
|
||||||
offers process isolation for your tests, it can generate statistics about test cases,
|
offers process isolation for your tests, it can generate statistics about test cases,
|
||||||
|
|
@ -11,29 +17,34 @@ so can be useful to run your tests, even the most complex ones.
|
||||||
Test files location
|
Test files location
|
||||||
===================
|
===================
|
||||||
|
|
||||||
By default Testament looks for test files on `"./tests/*.nim"`.
|
By default Testament looks for test files on ``"./tests/*.nim"``.
|
||||||
You can overwrite this pattern glob using `pattern <glob>`.
|
You can overwrite this pattern glob using `pattern <glob>`:option:.
|
||||||
The default working directory path can be changed using
|
The default working directory path can be changed using
|
||||||
`--directory:"folder/subfolder/"`.
|
`--directory:"folder/subfolder/"`:option:.
|
||||||
|
|
||||||
Testament uses the `nim` compiler on `PATH`.
|
Testament uses the `nim`:cmd: compiler on `PATH`.
|
||||||
You can change that using `--nim:"folder/subfolder/nim"`.
|
You can change that using `--nim:"folder/subfolder/nim"`:option:.
|
||||||
Running JavaScript tests with `--targets:"js"` requires a working NodeJS on
|
Running JavaScript tests with `--targets:"js"`:option: requires
|
||||||
`PATH`.
|
a working NodeJS on `PATH`.
|
||||||
|
|
||||||
|
|
||||||
Options
|
Options
|
||||||
=======
|
=======
|
||||||
|
|
||||||
* `--print` Also print results to the console
|
--print Also print results to the console
|
||||||
* `--simulate` See what tests would be run but don't run them (for debugging)
|
--simulate See what tests would be run but don't run them
|
||||||
* `--failing` Only show failing/ignored tests
|
(for debugging)
|
||||||
* `--targets:"c cpp js objc"` Run tests for specified targets (default: all)
|
--failing Only show failing/ignored tests
|
||||||
* `--nim:path` Use a particular nim executable (default: `$PATH/nim`)
|
--targets:"c cpp js objc"
|
||||||
* `--directory:dir` Change to directory dir before reading the tests or doing anything else.
|
Run tests for specified targets (default: all)
|
||||||
* `--colors:on|off` Turn messages coloring on|off.
|
--nim:path Use a particular nim executable (default: $PATH/nim)
|
||||||
* `--backendLogging:on|off` Disable or enable backend logging. By default turned on.
|
--directory:dir Change to directory dir before reading the tests
|
||||||
* `--skipFrom:file` Read tests to skip from `file` - one test per line, # comments ignored
|
or doing anything else.
|
||||||
|
--colors:on|off Turn messages coloring on|off.
|
||||||
|
--backendLogging:on|off Disable or enable backend logging.
|
||||||
|
By default turned on.
|
||||||
|
--skipFrom:file Read tests to skip from ``file`` - one test per
|
||||||
|
line, # comments ignored
|
||||||
|
|
||||||
|
|
||||||
Running a single test
|
Running a single test
|
||||||
|
|
@ -42,27 +53,26 @@ Running a single test
|
||||||
This is a minimal example to understand the basics,
|
This is a minimal example to understand the basics,
|
||||||
not very useful for production, but easy to understand:
|
not very useful for production, but easy to understand:
|
||||||
|
|
||||||
.. code::
|
.. code:: console
|
||||||
|
|
||||||
$ mkdir tests
|
$ mkdir tests
|
||||||
$ echo "assert 42 == 42" > tests/test0.nim
|
$ echo "assert 42 == 42" > tests/test0.nim
|
||||||
$ testament run test0.nim
|
$ testament run test0.nim
|
||||||
PASS: tests/test0.nim C ( 0.2 sec)
|
PASS: tests/test0.nim C ( 0.2 sec)
|
||||||
|
|
||||||
$ testament r test0
|
$ testament r test0
|
||||||
PASS: tests/test0.nim C ( 0.2 sec)
|
PASS: tests/test0.nim C ( 0.2 sec)
|
||||||
|
|
||||||
|
|
||||||
Running all tests from a directory
|
Running all tests from a directory
|
||||||
==================================
|
==================================
|
||||||
|
|
||||||
.. code::
|
.. code:: console
|
||||||
|
|
||||||
$ testament pattern "tests/*.nim"
|
$ testament pattern "tests/*.nim"
|
||||||
|
|
||||||
To search for tests deeper in a directory, use
|
To search for tests deeper in a directory, use
|
||||||
|
|
||||||
.. code::
|
.. code:: console
|
||||||
|
|
||||||
$ testament pattern "tests/**/*.nim" # one level deeper
|
$ testament pattern "tests/**/*.nim" # one level deeper
|
||||||
$ testament pattern "tests/**/**/*.nim" # two levels deeper
|
$ testament pattern "tests/**/**/*.nim" # two levels deeper
|
||||||
|
|
@ -70,10 +80,10 @@ To search for tests deeper in a directory, use
|
||||||
HTML Reports
|
HTML Reports
|
||||||
============
|
============
|
||||||
|
|
||||||
Generate HTML Reports `testresults.html` from unittests,
|
Generate HTML Reports ``testresults.html`` from unittests,
|
||||||
you have to run at least 1 test *before* generating a report:
|
you have to run at least 1 test *before* generating a report:
|
||||||
|
|
||||||
.. code::
|
.. code:: console
|
||||||
|
|
||||||
$ testament html
|
$ testament html
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -49,6 +49,11 @@
|
||||||
## `nimgrep --ext:'nim|nims' file.name`:cmd: shows how to input ``|``.
|
## `nimgrep --ext:'nim|nims' file.name`:cmd: shows how to input ``|``.
|
||||||
## Any argument that contains ``.`` or ``/`` or ``\`` will be treated
|
## Any argument that contains ``.`` or ``/`` or ``\`` will be treated
|
||||||
## as a file or directory.
|
## as a file or directory.
|
||||||
|
##
|
||||||
|
## In addition to `Cmd` there is also `Console` language for
|
||||||
|
## displaying interactive sessions.
|
||||||
|
## Lines with a command should start with ``$``, other lines are considered
|
||||||
|
## as program output.
|
||||||
|
|
||||||
import
|
import
|
||||||
strutils
|
strutils
|
||||||
|
|
@ -57,7 +62,7 @@ from algorithm import binarySearch
|
||||||
type
|
type
|
||||||
SourceLanguage* = enum
|
SourceLanguage* = enum
|
||||||
langNone, langNim, langCpp, langCsharp, langC, langJava,
|
langNone, langNim, langCpp, langCsharp, langC, langJava,
|
||||||
langYaml, langPython, langCmd
|
langYaml, langPython, langCmd, langConsole
|
||||||
TokenClass* = enum
|
TokenClass* = enum
|
||||||
gtEof, gtNone, gtWhitespace, gtDecNumber, gtBinNumber, gtHexNumber,
|
gtEof, gtNone, gtWhitespace, gtDecNumber, gtBinNumber, gtHexNumber,
|
||||||
gtOctNumber, gtFloatNumber, gtIdentifier, gtKeyword, gtStringLit,
|
gtOctNumber, gtFloatNumber, gtIdentifier, gtKeyword, gtStringLit,
|
||||||
|
|
@ -65,7 +70,7 @@ type
|
||||||
gtOperator, gtPunctuation, gtComment, gtLongComment, gtRegularExpression,
|
gtOperator, gtPunctuation, gtComment, gtLongComment, gtRegularExpression,
|
||||||
gtTagStart, gtTagEnd, gtKey, gtValue, gtRawData, gtAssembler,
|
gtTagStart, gtTagEnd, gtKey, gtValue, gtRawData, gtAssembler,
|
||||||
gtPreprocessor, gtDirective, gtCommand, gtRule, gtHyperlink, gtLabel,
|
gtPreprocessor, gtDirective, gtCommand, gtRule, gtHyperlink, gtLabel,
|
||||||
gtReference, gtProgram, gtOption, gtOther
|
gtReference, gtPrompt, gtProgramOutput, gtProgram, gtOption, gtOther
|
||||||
GeneralTokenizer* = object of RootObj
|
GeneralTokenizer* = object of RootObj
|
||||||
kind*: TokenClass
|
kind*: TokenClass
|
||||||
start*, length*: int
|
start*, length*: int
|
||||||
|
|
@ -76,14 +81,17 @@ type
|
||||||
|
|
||||||
const
|
const
|
||||||
sourceLanguageToStr*: array[SourceLanguage, string] = ["none",
|
sourceLanguageToStr*: array[SourceLanguage, string] = ["none",
|
||||||
"Nim", "C++", "C#", "C", "Java", "Yaml", "Python", "Cmd"]
|
"Nim", "C++", "C#", "C", "Java", "Yaml", "Python", "Cmd", "Console"]
|
||||||
|
sourceLanguageToAlpha*: array[SourceLanguage, string] = ["none",
|
||||||
|
"Nim", "cpp", "csharp", "C", "Java", "Yaml", "Python", "Cmd", "Console"]
|
||||||
|
## list of languages spelled with alpabetic characters
|
||||||
tokenClassToStr*: array[TokenClass, string] = ["Eof", "None", "Whitespace",
|
tokenClassToStr*: array[TokenClass, string] = ["Eof", "None", "Whitespace",
|
||||||
"DecNumber", "BinNumber", "HexNumber", "OctNumber", "FloatNumber",
|
"DecNumber", "BinNumber", "HexNumber", "OctNumber", "FloatNumber",
|
||||||
"Identifier", "Keyword", "StringLit", "LongStringLit", "CharLit",
|
"Identifier", "Keyword", "StringLit", "LongStringLit", "CharLit",
|
||||||
"EscapeSequence", "Operator", "Punctuation", "Comment", "LongComment",
|
"EscapeSequence", "Operator", "Punctuation", "Comment", "LongComment",
|
||||||
"RegularExpression", "TagStart", "TagEnd", "Key", "Value", "RawData",
|
"RegularExpression", "TagStart", "TagEnd", "Key", "Value", "RawData",
|
||||||
"Assembler", "Preprocessor", "Directive", "Command", "Rule", "Hyperlink",
|
"Assembler", "Preprocessor", "Directive", "Command", "Rule", "Hyperlink",
|
||||||
"Label", "Reference",
|
"Label", "Reference", "Prompt", "ProgramOutput",
|
||||||
# start from lower-case if there is a corresponding RST role (see rst.nim)
|
# start from lower-case if there is a corresponding RST role (see rst.nim)
|
||||||
"program", "option",
|
"program", "option",
|
||||||
"Other"]
|
"Other"]
|
||||||
|
|
@ -103,9 +111,11 @@ const
|
||||||
"xor", "yield"]
|
"xor", "yield"]
|
||||||
|
|
||||||
proc getSourceLanguage*(name: string): SourceLanguage =
|
proc getSourceLanguage*(name: string): SourceLanguage =
|
||||||
for i in countup(succ(low(SourceLanguage)), high(SourceLanguage)):
|
for i in succ(low(SourceLanguage)) .. high(SourceLanguage):
|
||||||
if cmpIgnoreStyle(name, sourceLanguageToStr[i]) == 0:
|
if cmpIgnoreStyle(name, sourceLanguageToStr[i]) == 0:
|
||||||
return i
|
return i
|
||||||
|
if cmpIgnoreStyle(name, sourceLanguageToAlpha[i]) == 0:
|
||||||
|
return i
|
||||||
result = langNone
|
result = langNone
|
||||||
|
|
||||||
proc initGeneralTokenizer*(g: var GeneralTokenizer, buf: cstring) =
|
proc initGeneralTokenizer*(g: var GeneralTokenizer, buf: cstring) =
|
||||||
|
|
@ -915,17 +925,17 @@ proc pythonNextToken(g: var GeneralTokenizer) =
|
||||||
"with", "yield"]
|
"with", "yield"]
|
||||||
nimNextToken(g, keywords)
|
nimNextToken(g, keywords)
|
||||||
|
|
||||||
proc cmdNextToken(g: var GeneralTokenizer) =
|
proc cmdNextToken(g: var GeneralTokenizer, dollarPrompt = false) =
|
||||||
var pos = g.pos
|
var pos = g.pos
|
||||||
g.start = g.pos
|
g.start = g.pos
|
||||||
if g.state == low(TokenClass):
|
if g.state == low(TokenClass):
|
||||||
g.state = gtProgram
|
g.state = if dollarPrompt: gtPrompt else: gtProgram
|
||||||
case g.buf[pos]
|
case g.buf[pos]
|
||||||
of ' ', '\t'..'\r':
|
of ' ', '\t'..'\r':
|
||||||
g.kind = gtWhitespace
|
g.kind = gtWhitespace
|
||||||
while g.buf[pos] in {' ', '\t'..'\r'}:
|
while g.buf[pos] in {' ', '\t'..'\r'}:
|
||||||
if g.buf[pos] == '\n':
|
if g.buf[pos] == '\n':
|
||||||
g.state = gtProgram
|
g.state = if dollarPrompt: gtPrompt else: gtProgram
|
||||||
inc(pos)
|
inc(pos)
|
||||||
of '\'', '"':
|
of '\'', '"':
|
||||||
g.kind = gtOption
|
g.kind = gtOption
|
||||||
|
|
@ -955,6 +965,15 @@ proc cmdNextToken(g: var GeneralTokenizer) =
|
||||||
g.kind = gtOperator
|
g.kind = gtOperator
|
||||||
inc(pos)
|
inc(pos)
|
||||||
of '\0': g.kind = gtEof
|
of '\0': g.kind = gtEof
|
||||||
|
elif dollarPrompt and g.state == gtPrompt:
|
||||||
|
if g.buf[pos] == '$' and g.buf[pos+1] in {' ', '\t'}:
|
||||||
|
g.kind = gtPrompt
|
||||||
|
inc pos, 2
|
||||||
|
g.state = gtProgram
|
||||||
|
else:
|
||||||
|
g.kind = gtProgramOutput
|
||||||
|
while g.buf[pos] notin {'\n', '\0'}:
|
||||||
|
inc(pos)
|
||||||
else:
|
else:
|
||||||
if g.state == gtProgram:
|
if g.state == gtProgram:
|
||||||
g.kind = gtProgram
|
g.kind = gtProgram
|
||||||
|
|
@ -986,6 +1005,7 @@ proc getNextToken*(g: var GeneralTokenizer, lang: SourceLanguage) =
|
||||||
of langYaml: yamlNextToken(g)
|
of langYaml: yamlNextToken(g)
|
||||||
of langPython: pythonNextToken(g)
|
of langPython: pythonNextToken(g)
|
||||||
of langCmd: cmdNextToken(g)
|
of langCmd: cmdNextToken(g)
|
||||||
|
of langConsole: cmdNextToken(g, dollarPrompt=true)
|
||||||
|
|
||||||
proc tokenize*(text: string, lang: SourceLanguage): seq[(string, TokenClass)] =
|
proc tokenize*(text: string, lang: SourceLanguage): seq[(string, TokenClass)] =
|
||||||
var g: GeneralTokenizer
|
var g: GeneralTokenizer
|
||||||
|
|
|
||||||
|
|
@ -89,6 +89,8 @@
|
||||||
##
|
##
|
||||||
## - generic command line highlighting roles:
|
## - generic command line highlighting roles:
|
||||||
## - ``:cmd:`` for commands and common shells syntax
|
## - ``:cmd:`` for commands and common shells syntax
|
||||||
|
## - ``:console:`` the same for interactive sessions
|
||||||
|
## (commands should be prepended by ``$``)
|
||||||
## - ``:program:`` for executable names [cmp:Sphinx]_
|
## - ``:program:`` for executable names [cmp:Sphinx]_
|
||||||
## (one can just use ``:cmd:`` on single word)
|
## (one can just use ``:cmd:`` on single word)
|
||||||
## - ``:option:`` for command line options [cmp:Sphinx]_
|
## - ``:option:`` for command line options [cmp:Sphinx]_
|
||||||
|
|
@ -168,6 +170,7 @@
|
||||||
import
|
import
|
||||||
os, strutils, rstast, std/enumutils, algorithm, lists, sequtils,
|
os, strutils, rstast, std/enumutils, algorithm, lists, sequtils,
|
||||||
std/private/miscdollars
|
std/private/miscdollars
|
||||||
|
from highlite import SourceLanguage, getSourceLanguage
|
||||||
|
|
||||||
type
|
type
|
||||||
RstParseOption* = enum ## options for the RST parser
|
RstParseOption* = enum ## options for the RST parser
|
||||||
|
|
@ -549,10 +552,6 @@ proc defaultFindFile*(filename: string): string =
|
||||||
proc defaultRole(options: RstParseOptions): string =
|
proc defaultRole(options: RstParseOptions): string =
|
||||||
if roNimFile in options: "nim" else: "literal"
|
if roNimFile in options: "nim" else: "literal"
|
||||||
|
|
||||||
# mirror highlite.nim sourceLanguageToStr with substitutions c++ cpp, c# csharp
|
|
||||||
const supportedLanguages = ["nim", "yaml", "python", "java", "c",
|
|
||||||
"cpp", "csharp", "cmd"]
|
|
||||||
|
|
||||||
proc whichRoleAux(sym: string): RstNodeKind =
|
proc whichRoleAux(sym: string): RstNodeKind =
|
||||||
let r = sym.toLowerAscii
|
let r = sym.toLowerAscii
|
||||||
case r
|
case r
|
||||||
|
|
@ -566,7 +565,7 @@ proc whichRoleAux(sym: string): RstNodeKind =
|
||||||
of "code": result = rnInlineLiteral
|
of "code": result = rnInlineLiteral
|
||||||
of "program", "option", "tok": result = rnCodeFragment
|
of "program", "option", "tok": result = rnCodeFragment
|
||||||
# c++ currently can be spelled only as cpp, c# only as csharp
|
# c++ currently can be spelled only as cpp, c# only as csharp
|
||||||
elif r in supportedLanguages:
|
elif getSourceLanguage(r) != langNone:
|
||||||
result = rnInlineCode
|
result = rnInlineCode
|
||||||
else: # unknown role
|
else: # unknown role
|
||||||
result = rnUnknownRole
|
result = rnUnknownRole
|
||||||
|
|
@ -2614,7 +2613,7 @@ proc dirRole(p: var RstParser): PRstNode =
|
||||||
result = parseDirective(p, rnDirective, {hasArg, hasOptions}, nil)
|
result = parseDirective(p, rnDirective, {hasArg, hasOptions}, nil)
|
||||||
# just check that language is supported, TODO: real role association
|
# just check that language is supported, TODO: real role association
|
||||||
let lang = getFieldValue(result, "language").strip
|
let lang = getFieldValue(result, "language").strip
|
||||||
if lang != "" and lang notin supportedLanguages:
|
if lang != "" and getSourceLanguage(lang) == langNone:
|
||||||
rstMessage(p, mwUnsupportedLanguage, lang)
|
rstMessage(p, mwUnsupportedLanguage, lang)
|
||||||
|
|
||||||
proc dirRawAux(p: var RstParser, result: var PRstNode, kind: RstNodeKind,
|
proc dirRawAux(p: var RstParser, result: var PRstNode, kind: RstNodeKind,
|
||||||
|
|
|
||||||
|
|
@ -938,6 +938,14 @@ span.option {
|
||||||
color: var(--option);
|
color: var(--option);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
span.Prompt {
|
||||||
|
font-weight: bold;
|
||||||
|
color: red; }
|
||||||
|
|
||||||
|
span.ProgramOutput {
|
||||||
|
font-weight: bold;
|
||||||
|
color: #808080; }
|
||||||
|
|
||||||
span.program {
|
span.program {
|
||||||
font-weight: bold;
|
font-weight: bold;
|
||||||
color: var(--program);
|
color: var(--program);
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
|
|
||||||
import unittest
|
import unittest, strutils
|
||||||
import ../../lib/packages/docutils/highlite
|
import ../../lib/packages/docutils/highlite
|
||||||
|
|
||||||
block: # Nim tokenizing"
|
block: # Nim tokenizing
|
||||||
test "string literals and escape seq":
|
test "string literals and escape seq":
|
||||||
check("\"ok1\\nok2\\nok3\"".tokenize(langNim) ==
|
check("\"ok1\\nok2\\nok3\"".tokenize(langNim) ==
|
||||||
@[("\"ok1", gtStringLit), ("\\n", gtEscapeSequence), ("ok2", gtStringLit),
|
@[("\"ok1", gtStringLit), ("\\n", gtEscapeSequence), ("ok2", gtStringLit),
|
||||||
|
|
@ -11,3 +11,16 @@ block: # Nim tokenizing"
|
||||||
check("\"\"\"ok1\\nok2\\nok3\"\"\"".tokenize(langNim) ==
|
check("\"\"\"ok1\\nok2\\nok3\"\"\"".tokenize(langNim) ==
|
||||||
@[("\"\"\"ok1\\nok2\\nok3\"\"\"", gtLongStringLit)
|
@[("\"\"\"ok1\\nok2\\nok3\"\"\"", gtLongStringLit)
|
||||||
])
|
])
|
||||||
|
|
||||||
|
block: # Cmd (shell) tokenizing
|
||||||
|
test "cmd with dollar and output":
|
||||||
|
check(
|
||||||
|
dedent"""
|
||||||
|
$ nim c file.nim
|
||||||
|
out: file [SuccessX]"""
|
||||||
|
.tokenize(langConsole) ==
|
||||||
|
@[("$ ", gtPrompt), ("nim", gtProgram),
|
||||||
|
(" ", gtWhitespace), ("c", gtOption), (" ", gtWhitespace),
|
||||||
|
("file.nim", gtIdentifier), ("\n", gtWhitespace),
|
||||||
|
("out: file [SuccessX]", gtProgramOutput)
|
||||||
|
])
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue