C# Class LeMP.MacroProcessor

Encapsulates the LeMP engine, a simple LISP-style macro processor, suitable for running LLLPG and other lexical macros.
MacroProcessor itself only cares about to #import/#importMacros/#unimportMacros statements, and { braces } (for scoping the #import statements). The macro processor should be configured with any needed macros like this: var MP = new MacroProcessor(prelude, sink); MP.AddMacros(typeof(LeMP.Prelude.Macros).Assembly); MP.PreOpenedNamespaces.Add(GSymbol.Get("LeMP.Prelude")); In order for the input code to have access to macros, two steps are necessary: you have to add the macro classes with AddMacros and then you have to import the namespace that contains the class(es). Higher-level code (e.g. Compiler) can define "always-open" namespaces by adding entries to PreOpenedNamespaces, and the code being processed can open additional namespaces with a #importMacros(Namespace) statement (in LES, "import macros Namespace" can be used as a synonym if PreOpenedNamespaces contains LeMP.Prelude). MacroProcessor is not aware of any distinction between "statements" and "expressions"; it will run macros no matter where they are located, whether as standalone statements, attributes, or arguments to functions. MacroProcessor's main responsibilities are to keep track of a table of registered macros (call AddMacros to register more), to keep track of which namespaces are open (namespaces can be imported by #import, or by import which is defined in the LES prelude); to scan the input for macros to call; and to control the printout of messages. This class processes a batch of files at once. Call either ProcessSynchronously or ProcessParallel. Parallelizing on a file-by-file basis is easy; each source file is completely independent, since no semantic analysis is being done. TODO: add method for processing an LNode instead of a list of source files.
Show file Open project: qwertie/ecsharp Class Usage Examples

Public Properties

Property Type Description
DefaultScopedProperties object>.MMap
MaxExpansions int

Private Properties

Property Type Description
AddMacro void
AsDelegate LexicalMacro
GetMacros IEnumerable

Public Methods

Method Description
AddMacros ( Assembly assembly, bool writeToSink = true ) : bool
AddMacros ( Type type ) : bool
IncrementTempCounter ( ) : int

Gets the next number to use as a suffix for temporary variables, then increments it.

MacroProcessor currently starts this counter at 10 to avoid collisions with names like tmp_2 and tmp_3 that might be names chosen by a developer; tmp_10 is much less likely to collide.

MacroProcessor ( IMessageSink sink ) : System

Initializes MacroProcessor with default prelude.

MacroProcessor ( IMessageSink sink, Type prelude ) : System

Initializes MacroProcessor.

ProcessAsync ( IReadOnlyList sourceFiles, Action onProcessed = null ) : Task>[]

Processes source files in parallel using .NET Tasks. The method returns immediately.

ProcessParallel ( IReadOnlyList sourceFiles, Action onProcessed = null ) : void
ProcessSynchronously ( VList stmts ) : VList

Processes a list of nodes directly on the current thread.

Note: AbortTimeout doesn't work when using this overload.

ProcessSynchronously ( IReadOnlyList sourceFiles, Action onProcessed = null ) : void

Processes source files one at a time (may be easier for debugging).

Private Methods

Method Description
AddMacro ( MMap macros, MacroInfo info ) : void
AsDelegate ( MethodInfo method, IMessageSink sink, object instance ) : LexicalMacro
GetMacros ( Type type, Symbol @namespace, IMessageSink sink, object instance = null ) : IEnumerable

Method Details

AddMacros() public method

public AddMacros ( Assembly assembly, bool writeToSink = true ) : bool
assembly System.Reflection.Assembly
writeToSink bool
return bool

AddMacros() public method

public AddMacros ( Type type ) : bool
type System.Type
return bool

IncrementTempCounter() public static method

Gets the next number to use as a suffix for temporary variables, then increments it.
MacroProcessor currently starts this counter at 10 to avoid collisions with names like tmp_2 and tmp_3 that might be names chosen by a developer; tmp_10 is much less likely to collide.
public static IncrementTempCounter ( ) : int
return int

MacroProcessor() public method

Initializes MacroProcessor with default prelude.
public MacroProcessor ( IMessageSink sink ) : System
sink IMessageSink
return System

MacroProcessor() public method

Initializes MacroProcessor.
public MacroProcessor ( IMessageSink sink, Type prelude ) : System
sink IMessageSink The destination for warning and error messages. NOTE: /// this class can process files in parallel. Consider using a thread-safe /// implementation of .
prelude System.Type An initial type from which to add macros. /// Omit this parameter to use typeof(LeMP.Prelude.BuiltinMacros).
return System

ProcessAsync() public method

Processes source files in parallel using .NET Tasks. The method returns immediately.
public ProcessAsync ( IReadOnlyList sourceFiles, Action onProcessed = null ) : Task>[]
sourceFiles IReadOnlyList
onProcessed Action
return Task>[]

ProcessParallel() public method

public ProcessParallel ( IReadOnlyList sourceFiles, Action onProcessed = null ) : void
sourceFiles IReadOnlyList
onProcessed Action
return void

ProcessSynchronously() public method

Processes a list of nodes directly on the current thread.
Note: AbortTimeout doesn't work when using this overload.
public ProcessSynchronously ( VList stmts ) : VList
stmts VList
return VList

ProcessSynchronously() public method

Processes source files one at a time (may be easier for debugging).
public ProcessSynchronously ( IReadOnlyList sourceFiles, Action onProcessed = null ) : void
sourceFiles IReadOnlyList
onProcessed Action
return void

Property Details

DefaultScopedProperties public property

Default values of scoped properties.
Code being processed can look up a scoped property named "N" with #getScopedProperty("N") in LESv2 or EC#. This map is empty by default. Scoped properties are "scoped" in the sense that setting a property with #setScopedProperty(keyLiteral, valueLiteral) takes effect only until the end of the braced block in which it appears. The @@#inputFolder and @@#inputFileName properties (note: @@ is EC# syntax for Symbol) are not normally stored in this collection; when you use ProcessSynchronously or ProcessParallel, @@#inputFolder and @@#inputFileName are set according to the folder and filename in InputOutput.FileName. However, @@#inputFolder is not set if the filename has no folder component, so this collection could be used to provide a value of @@#inputFolder in that case.
public MMap DefaultScopedProperties
return object>.MMap

MaxExpansions public property

public int MaxExpansions
return int