This page demonstrates how to use FSharp.CodeFormat.dll to tokenize
F# source code, obtain information about the source code (mainly tooltips
from the type-checker) and how to turn the code into a nicely formatted HTML.

If you want to process multiple snippets, it is a good idea to keep the
formatting agent around if possible. The agent needs to load the F# compiler
(which needs to load various files itself) and so this takes a long time. As the above
example shows, you can specify which version of FSharp.Compiler.dll to use.

The formatting agent provides a ParseSource method (together with an asynchronous
version for use from F# and also a version that returns a .NET Task for C#).
To call the method, we define a simple F# code as a string:

When calling the method, you need to specify a file name and the actual content
of the script file. The file does not have to physically exist. It is used by the
F# compiler to resolve relative references (e.g. #r) and to automatically name
the module including all code in the file.

You can also specify additional parameters, such as *.dll references, by passing
a third argument with compiler options (e.g. "-r:Foo.dll -r:Bar.dll").

This operation might take some time, so it is a good idea to use an asynchronous
variant of the method. It returns two arrays - the first contains F# snippets
in the source code and the second contains any errors reported by the compiler.
A single source file can include multiple snippets using the same formatting tags
as those used on fssnip.net as documented in the
about page.

The TokenSpan.Token is the most important kind of token. It consists of a kind
(identifier, keyword, etc.), the original F# code and tool tip information.
The tool tip is further formatted using a simple document format, but we simply
print the value using the F# pretty printing, so the result looks as follows:

The Omitted token is generated if you use the special (*[omit:...]*) command.
The Output token is generated if you use the // [fsi:...] command to format
output returned by F# interactive. The Error command wraps code that should be
underlined with a red squiggle if the code contains an error.

Finally, the CodeFormat type also includes a method FormatHtml that can be used
to generate nice HTML output from an F# snippet. This is used, for example, on
F# Snippets. The following example shows how to call it:

1: 2: 3: 4: 5: 6: 7: 8: 9:

letprefix="fst"lethtml=CodeFormat.FormatHtml(snippets, prefix)
// Print all snippets, in case there is more of themforsnipinhtml.Snippetsdoprintfn"%s"snip.Content// Print HTML code that is generated for ToolTipsprintfn"%s"html.ToolTip

If the input contains multiple snippets separated using the //[snippet:...] comment, e.g.: