Enhanced C#
Language of your choice: library documentation
Public fields | Public Member Functions | List of all members
Loyc.Syntax.LiteralHandlerTable Class Reference

A central class for keeping track of literal parsers and literal printers. More...


Source file:
Inheritance diagram for Loyc.Syntax.LiteralHandlerTable:
Loyc.Syntax.ILiteralParser Loyc.Syntax.ILiteralPrinter Loyc.Syntax.StandardLiteralHandlers

Remarks

A central class for keeping track of literal parsers and literal printers.

See also
StandardLiteralHandlers

Public fields

IReadOnlyDictionary< Symbol, Func< UString, Symbol, Either< object, LogMessage > > > Parsers => _parsers
 A table of parsers indexed by type marker Symbol. The AddParser method is used to add an item to this collection. More...
 
IReadOnlyDictionary< object, Func< ILNode, StringBuilder, Either< Symbol, LogMessage > > > Printers => _printers
 A table of printers indexed by Type or by type marker Symbol. The AddPrinter methods are used to add an item to this collection. More...
 

Public Member Functions

bool AddParser (bool replaceExisting, Symbol typeMarker, Func< UString, Symbol, Either< object, LogMessage >> parser)
 Adds a parser to the Parsers collection. More...
 
bool AddPrinter (bool replaceExisting, Symbol type, Func< ILNode, StringBuilder, Either< Symbol, LogMessage >> printer)
 Adds a printer to the Printers collection. More...
 
bool AddPrinter (bool replaceExisting, Type type, Func< ILNode, StringBuilder, Either< Symbol, LogMessage >> printer)
 
bool CanParse (Symbol typeMarker)
 Returns true if there is a parser function for the given type marker. Never throws. More...
 
bool CanPrint (Symbol typeMarker)
 Returns true if there is a printer function for the given type marker. Never throws. More...
 
bool CanPrint (Type type, bool searchBases=true)
 Returns true if there is a printer function for the given type. Never throws. More...
 
Either< object, ILogMessageTryParse (UString textValue, Symbol typeMarker)
 Attempts to parse a string with a given type marker.
 
Either< Symbol, ILogMessageTryPrint (ILNode literal, StringBuilder sb)
 Searches Printers for a printer for the value and uses it to convert the value to a string. When a printer can be found both by type marker Symbol and by Type, the printer for the matching type marker is used (takes priority). The complete search order is (1) type marker (if any), (2) exact type, (3) base class and base interfaces, in that order, recursively, breadth-first. More...
 

Member Function Documentation

◆ AddParser()

bool Loyc.Syntax.LiteralHandlerTable.AddParser ( bool  replaceExisting,
Symbol  typeMarker,
Func< UString, Symbol, Either< object, LogMessage >>  parser 
)
inline

Adds a parser to the Parsers collection.

Parameters
replaceExistingIf the specified type already has a printer assigned to it, it will be replaced only if this flag is true.
typeMarkerThe parser will be invoked by TryParse when this type marker Symbol matches the literal being parsed.
parserA function that converts a UString to a value (object), or returns a LogMessage if an error occurs. The type marker is also provided to the parser.
Returns
true if the printer was installed (if replaceExisting is true, the method will return true unless the type is null.)

◆ AddPrinter()

bool Loyc.Syntax.LiteralHandlerTable.AddPrinter ( bool  replaceExisting,
Symbol  type,
Func< ILNode, StringBuilder, Either< Symbol, LogMessage >>  printer 
)

Adds a printer to the Printers collection.

Parameters
replaceExistingIf the specified type already has a printer assigned to it, it will be replaced only if this flag is true.
typeThe printer will be invoked by TryPrint(ILNode) when this Type or type marker Symbol matches the literal being printed.
printerA printer function that prints into the provided StringBuilder and returns null on success, or a description of the error that occurred.
Returns
true if the printer was installed (if replaceExisting is true, the method will return true unless the type is null.)

◆ CanParse()

bool Loyc.Syntax.LiteralHandlerTable.CanParse ( Symbol  typeMarker)
inline

Returns true if there is a parser function for the given type marker. Never throws.

Implements Loyc.Syntax.ILiteralParser.

References Loyc.Syntax.LiteralHandlerTable.Parsers.

◆ CanPrint() [1/2]

bool Loyc.Syntax.LiteralHandlerTable.CanPrint ( Symbol  typeMarker)
inline

Returns true if there is a printer function for the given type marker. Never throws.

Implements Loyc.Syntax.ILiteralPrinter.

References Loyc.Syntax.LiteralHandlerTable.Printers.

◆ CanPrint() [2/2]

bool Loyc.Syntax.LiteralHandlerTable.CanPrint ( Type  type,
bool  searchBases = true 
)
inline

Returns true if there is a printer function for the given type. Never throws.

Parameters
searchBasesWhether to search for printers among the base types of the given type.
Returns
True if type is not null and if there is a printer for that type.

References Loyc.Syntax.LiteralHandlerTable.Printers.

◆ TryPrint()

Either<Symbol, ILogMessage> Loyc.Syntax.LiteralHandlerTable.TryPrint ( ILNode  literal,
StringBuilder  sb 
)
inline

Searches Printers for a printer for the value and uses it to convert the value to a string. When a printer can be found both by type marker Symbol and by Type, the printer for the matching type marker is used (takes priority). The complete search order is (1) type marker (if any), (2) exact type, (3) base class and base interfaces, in that order, recursively, breadth-first.

Parameters
literalA literal that you want to convert to a string.
Returns
Either the type marker for the literal, or an error message. On return, the string form of the literal is appended to the StringBuilder. If an error occurs, it is possible that some kind of output was added to the StringBuilder anyway.

If a printer returns an error, this method tries to find other printers that might be able to print the value. If no printer succeeds, the first error that occurred is returned.

When the literal is null and there is no printer associated with literal.TypeMarker, this funtion produces no output and returns literal.TypeMarker.

On success, the return value indicates which type marker is recommended based on the data type of the literal. This is not guaranteed to match the TypeMarker originally stored in the literal. It is recommended that language printers use the type marker stored in the literal (regardless of what this method returns) unless literal.TypeMarker == null.

Implements Loyc.Syntax.ILiteralPrinter.

References Loyc.Syntax.IUninterpretedLiteral.TypeMarker.

Member Data Documentation

◆ Parsers

IReadOnlyDictionary<Symbol, Func<UString, Symbol, Either<object, LogMessage> > > Loyc.Syntax.LiteralHandlerTable.Parsers => _parsers

A table of parsers indexed by type marker Symbol. The AddParser method is used to add an item to this collection.

Referenced by Loyc.Syntax.LiteralHandlerTable.CanParse(), and Loyc.Syntax.LiteralHandlerTable.TryParse().

◆ Printers

IReadOnlyDictionary<object, Func<ILNode, StringBuilder, Either<Symbol, LogMessage> > > Loyc.Syntax.LiteralHandlerTable.Printers => _printers

A table of printers indexed by Type or by type marker Symbol. The AddPrinter methods are used to add an item to this collection.

Referenced by Loyc.Syntax.LiteralHandlerTable.CanPrint().