Enhanced C#
Language of your choice: library documentation
Static Public Member Functions | List of all members
LeMP.MacroContext Class Reference

Standard extension methods for IMacroContext. More...


Source file:

Remarks

Standard extension methods for IMacroContext.

Static Public Member Functions

static LNode CurrentNode (this IMacroContext ctx)
 
static Pair< LNodeList, LNodeListGetArgsAndBody (this IMacroContext ctx, bool orRemainingNodes)
 Splits the current node into a pair of "argument" and "body" lists, potentially treating ctx.RemainingNodes as the "body" list. More...
 
static IEnumerable< KeyValuePair< LNode, LNode > > GetOptions (LNodeList optionList)
 Transforms an option list in the format option1(v1), option2(v2) or option1: v1, option2: v2 into a sequence of (key, value) pairs. If the format of a given node is invalid, this function yields (node, null). More...
 

Member Function Documentation

◆ GetArgsAndBody()

static Pair<LNodeList, LNodeList> LeMP.MacroContext.GetArgsAndBody ( this IMacroContext  ctx,
bool  orRemainingNodes 
)
inlinestatic

Splits the current node into a pair of "argument" and "body" lists, potentially treating ctx.RemainingNodes as the "body" list.

Parameters
ctxContext of the current macro.
orRemainingNodesWhether to use ctx.RemainingNodes as the second list if there is no {braces node} at the end of ctx.CurrentNode().Args.
Returns
A pair where the first item is "arguments" and the second is the "body". If no body was detected then the second list is empty and the first list is simply ctx.CurrentNode().Args.

EC# supports a syntax specially designed for macro calls:

macroName(args) { stmts; }

This is stored as a call node with a body, in braces, as its final parameter, i.e. it is equivalent to

macroName(args, { stmts; });

A similar, but more general feature called "superexpressions" exists in LES.

Some macros would additionally like to apply themselves to all remaining nodes in the current list of statements or expressions, i.e.

macroName(args); stmts;

LeMP supports this through the IMacroContext.DropRemainingNodes and IMacroContext.RemainingNodes APIs. If your macro wants to apply itself to all remaining statements or expressions in the current sequence of nodes, it can set the DropRemainingNodes property to true and then simply incorporate RemainingNodes into its own output (if you need to return multiple statements from your macro, use list.AsLNode(CodeSymbols.Splice) to convert a VList{LNode} to an LNode.)

This extension method helps you by detecting whether the current node has a body in braces or not. If the braces are present, the returned pair consists of the args shortened by one (i.e. ctx.CurrentNode().Args.WithoutLast(1)) and the Args of the "{}" braces node. Otherwise, ctx.CurrentNode().Args is the first item in the pair.

In the latter case, if orRemainingNodes then this method sets ctx.DropRemainingNodes to true and uses ctx.RemainingNodes as the second list. Otherwise the second list is left blank.

References Loyc.Syntax.LNode.Args, Loyc.Syntax.CodeSymbols.Braces, LeMP.IMacroContext.DropRemainingNodes, and LeMP.IMacroContext.RemainingNodes.

◆ GetOptions()

static IEnumerable<KeyValuePair<LNode, LNode> > LeMP.MacroContext.GetOptions ( LNodeList  optionList)
inlinestatic

Transforms an option list in the format option1(v1), option2(v2) or option1: v1, option2: v2 into a sequence of (key, value) pairs. If the format of a given node is invalid, this function yields (node, null).

option1: v1, option2: v2 is parsed into &lsquo;’::=(option1, v1), @'::=(option2, v2)</c> in EC# or <c>@':(option1, v1), @':`(option2, v2) in LES.

References Loyc.Syntax.LNode.Args, and Loyc.Syntax.CodeSymbols.NamedArg.

Referenced by Loyc.LLPG.Macros.LLLPG_lexer(), and Loyc.LLPG.Macros.LLLPG_parser().