Enhanced C#
Language of your choice: library documentation
Properties | Public Member Functions | Static Public Member Functions | Protected Member Functions | List of all members
Loyc.Syntax.Les.Les3PrettyPrinter Class Reference

A variant of Les3Printer that adds syntax highlighting in one of three ways: as console output, as HTML output, or as LesColorCode control codes. More...


Source file:
Inheritance diagram for Loyc.Syntax.Les.Les3PrettyPrinter:
Loyc.Syntax.Les.Les3Printer

Remarks

A variant of Les3Printer that adds syntax highlighting in one of three ways: as console output, as HTML output, or as LesColorCode control codes.

Create an instance by invoking the constructor, then call PrintToConsole for console output, PrintToHtml for HTML output, or Les3Printer.Print(IEnumerable<ILNode>) for just control codes.

Properties

string[] ColorCodesToCssClasses [get, set]
 The lookup table of strings for control codes (LesColorCode values) to HTML classes, used by PrintToHtml(IEnumerable<ILNode>, StringBuilder, bool). More...
 
- Properties inherited from Loyc.Syntax.Les.Les3Printer
StringBuilder SB [get, set]
 
IMessageSink MessageSink [get, set]
 Target for warning messages. More...
 
Les3PrinterOptions Options [get]
 

Public Member Functions

 Les3PrettyPrinter (IMessageSink sink=null, ILNodePrinterOptions options=null)
 Creates an instance of this class, which produces plain LES augmented with control codes. More...
 
 Les3PrettyPrinter (StringBuilder target, IMessageSink sink, ILNodePrinterOptions options)
 Creates an instance of this class, which produces plain LES augmented with control codes. More...
 
void PrintToConsole (ILNode node, bool endWithNewline=true)
 
void PrintToConsole (IEnumerable< ILNode > nodes, bool endWithNewline=true)
 
StringBuilder PrintToHtml (IEnumerable< ILNode > nodes, StringBuilder output=null, bool addPreCode=true)
 
StringBuilder PrintToHtml (ILNode node, StringBuilder output=null, bool addPreCode=true)
 
- Public Member Functions inherited from Loyc.Syntax.Les.Les3Printer
void SetOptions (ILNodePrinterOptions options)
 
StringBuilder Print (ILNode node, string suffix=null)
 
override string ToString ()
 
void VisitId (ILNode node)
 
void PrintIdCore (Symbol name, bool startToken, bool forceQuote=false)
 
void VisitLiteral (ILNode node)
 
void VisitCall (ILNode node)
 

Static Public Member Functions

static void PrintToConsoleCore (StringBuilder input, bool endWithNewline=true)
 
static string[] GetDefaultCssClassTable ()
 The lookup table of strings for control codes (LesColorCode values) to HTML classes. More...
 
static StringBuilder PrintToHtml (IEnumerable< ILNode > nodes, StringBuilder output=null, bool addPreCode=true, IMessageSink sink=null, ILNodePrinterOptions options=null)
 Prints an LNode as LESv3 with HTML syntax highlighting elements. More...
 
static StringBuilder PrintToHtmlCore (StringBuilder input, StringBuilder output=null, bool addPreCode=true, string newline="\n", string[] colorCodesToCssClasses=null)
 Converts a StringBuilder with LesColorCode control codes to HTML with Pygments CSS class codes. More...
 
- Static Public Member Functions inherited from Loyc.Syntax.Les.Les3Printer
static bool IsNormalIdentifier (Symbol name)
 
static bool IsOpPrefixChar (char c)
 

Protected Member Functions

override void StartToken (LesColorCode kind)
 
override LesColorCode ColorCodeForId (Symbol name)
 
- Protected Member Functions inherited from Loyc.Syntax.Les.Les3Printer
void Print (ILNode node, Precedence context, string suffix=null, NewlineContext nlContext=NewlineContext.AutoDetect)
 
void PrintCore (ILNode node, string suffix, bool avoidKwExprBraceAmbiguity=false)
 
void StartToken (LesColorCode kind, Chars charSet)
 
void StartToken (LesColorCode kind, Chars startSet, Chars endSet)
 
void WriteToken (char firstChar, LesColorCode kind, Chars tokenSet)
 
void WriteToken (string text, LesColorCode kind, Chars tokenSet)
 
void WriteToken (string text, LesColorCode kind, Chars startSet, Chars endSet)
 
void WriteOutsideToken (char space)
 
void Newline (bool avoidExtraNewline=false)
 
void Space (bool condition=true)
 

Additional Inherited Members

- Protected Types inherited from Loyc.Syntax.Les.Les3Printer
enum  Chars {
  Delimiter = 0, IdStart = 1, SingleQuote = 2,
  Id = IdStart | SingleQuote, Punc = 4, Dot = 16,
  NumberStart = IdStart | Dot, NumberEnd = Id | BQId | Dot, IdAndPunc = Id | Punc | Dot,
  DoubleQuote = 32, BQId = 64, StringStart = DoubleQuote | Id | BQId,
  At = 128, Space = 256, SLComment = 512
}
 Based on these flags, StartToken() and WriteToken() ensure that two adjacent tokens aren't treated like a single token when reparsed, by printing a space between them if necessary. More...
 
enum  NewlineContext {
  NewlineUnsafe = 0, NewlineSafeAfter = 1, NewlineSafeBefore = 2,
  StatementLevel = NewlineSafeBefore | NewlineSafeAfter, AutoDetect = 8
}
 Used to help keep track of where newline trivia (and single- line comments ending in a newline) are permitted, to avoid printing newline trivia where it would count as "end of expression". More...
 
- Protected fields inherited from Loyc.Syntax.Les.Les3Printer
PrinterState PS
 
ILNode _n
 
Precedence _context = Precedence.MinValue
 
NewlineContext _nlContext = NewlineContext.NewlineUnsafe
 
bool _inParensOrBracks = false
 
bool _isOneLiner = false
 Indicates whether the NodeStyle.OneLiner flag is present on the current node or any of its parents. It suppresses newlines within braced blocks. More...
 
Chars _curSet = 0
 
int HexNegativeExponentThreshold = -8
 
- Protected static fields inherited from Loyc.Syntax.Les.Les3Printer
static Symbol _s = GSymbol.Get("s")
 
static Symbol _F = GSymbol.Get("f")
 
static Symbol _D = GSymbol.Get("d")
 
static Symbol _M = GSymbol.Get("m")
 
static Symbol _U = GSymbol.Get("u")
 
static Symbol _L = GSymbol.Get("L")
 
static Symbol _UL = GSymbol.Get("uL")
 
static Symbol _Z = GSymbol.Get("z")
 
static Symbol _number = GSymbol.Get("n")
 

Constructor & Destructor Documentation

Loyc.Syntax.Les.Les3PrettyPrinter.Les3PrettyPrinter ( IMessageSink  sink = null,
ILNodePrinterOptions  options = null 
)
inline

Creates an instance of this class, which produces plain LES augmented with control codes.

Loyc.Syntax.Les.Les3PrettyPrinter.Les3PrettyPrinter ( StringBuilder  target,
IMessageSink  sink,
ILNodePrinterOptions  options 
)
inline

Creates an instance of this class, which produces plain LES augmented with control codes.

References Loyc.Syntax.LNode.IsSpecialName(), and Loyc.Symbol.Name.

Member Function Documentation

static string [] Loyc.Syntax.Les.Les3PrettyPrinter.GetDefaultCssClassTable ( )
inlinestatic

The lookup table of strings for control codes (LesColorCode values) to HTML classes.

For example, GetDefaultCssClassTable()[(int)LesColorCode.Number] indicates the default CSS class to use for numbers.

If the entry for a given code is null, no span element is emitted, which shortens the output.

The default class names are shared with the Pygments syntax highlighting system. A list of the CSS classes available in Pygments is available at this link. Only a small subset of these classes are used in this table.

Here is some suitable CSS:

.highlight { background-color: #f8f8f8; color: #111; }
.highlight .c  { color: #5A5; } /* Comment */
.highlight .n  { color: #111; } /* Name (omitted by default) */
.highlight .m  { color: #909; } /* Number */
.highlight .s  { color: #B44; } /* String */
.highlight .l  { color: #B04; } /* Literal (other) */
.highlight .kc { color: #41F; } /* Keyword.Constant */
.highlight .o  { color: #940; } /* Operator */
.highlight .p  { color: #111; } /* Punctuation (omitted by default) */
.highlight .kp { color: #33A; } /* Keyword.Pseudo () */
.highlight .nb { color: #007; } /* Name.Builtin (#specialId) */
.highlight .k  { color: #11F; } /* Keyword (.dotId) */
.highlight .x  { color: #D00; } /* Other */
.highlight .pi { color: #B50; } /* Parenthesis Inner (()) */

Note: LesTokenCode.Opener and LesTokenCode.Closer are handled specially. An opener and its matching closer (e.g. '(' and ')') are always given the same color, but nested parens/brackets are given alternating colors (CSS classes), with the entry for LesTokenCode.Opener used for outer parens and the entry for LesTokenCode.Closer used for inner parens. The default class name is "pi" for inner parentheses; no class name is used for outer parens. "pi" is not a standard name, so if you're using a standard Pygment stylesheet you should add an extra line, e.g.

.highlight .p { color: #111; } /* Punctuation (includes , ; { }) */
.highlight .pi { color: #B50; } /* Parenthesis Inner */
static StringBuilder Loyc.Syntax.Les.Les3PrettyPrinter.PrintToHtml ( IEnumerable< ILNode nodes,
StringBuilder  output = null,
bool  addPreCode = true,
IMessageSink  sink = null,
ILNodePrinterOptions  options = null 
)
inlinestatic

Prints an LNode as LESv3 with HTML syntax highlighting elements.

Parameters
nodesSyntax trees to print.
outputOutput StringBuilder for HTML code.
addPreCodeWhether to wrap the output in "&lt;pre class='highlight'>&lt;code>" tags.
optionsOptions to control the style for code printing.
Returns
The output StringBuilder
static StringBuilder Loyc.Syntax.Les.Les3PrettyPrinter.PrintToHtmlCore ( StringBuilder  input,
StringBuilder  output = null,
bool  addPreCode = true,
string  newline = "\n",
string[]  colorCodesToCssClasses = null 
)
inlinestatic

Converts a StringBuilder with LesColorCode control codes to HTML with Pygments CSS class codes.

Parameters
inputInput containing LesColorCode control characters.
outputOutput StringBuilder for HTML code. If null, a new one is created.
addPreCodeWhether to wrap the output in "&lt;pre class='highlight'>&lt;code>" tags.
newlineWhat to write to output when '
' is encountered.
colorCodesToCssClassesCSS class table for span tags, see GetDefaultCssClassTable.
Returns
The output StringBuilder.

Property Documentation

string [] Loyc.Syntax.Les.Les3PrettyPrinter.ColorCodesToCssClasses
getset

The lookup table of strings for control codes (LesColorCode values) to HTML classes, used by PrintToHtml(IEnumerable<ILNode>, StringBuilder, bool).

This property is null by default, which causes the default table to be used. See GetDefaultCssClassTable() for more information.