/// <summary> /// Creates a <see cref="IFunctionScope"/> inside this scope. /// Its name is automatically extracted from the header that may contain the /// opening curly brace '{' or not (in such case it is automatically appended). /// </summary> /// <param name="this">This scope.</param> /// <param name="header">The header of the function. Must not be null.</param> /// <returns>The new function scope.</returns> public static IFunctionScope CreateFunction(this IFunctionDefinerScope @this, string header) { if (header == null) { throw new ArgumentNullException(nameof(header)); } return(@this.CreateFunction(t => t.Append(header))); }
public void MergeWith(CodeWorkspaceImpl ws, IFunctionDefinerScope h, FunctionDefiner other) { foreach (var kv in other._funcs) { if (!_funcs.TryGetValue(kv.Key, out var my)) { my = new FunctionScopeImpl(ws, h); _funcs.Add(kv.Key, my); } my.MergeWith(kv.Value); } }
/// <summary> /// Finds or creates a function or a constructor inside this scope. /// There must not be any ': this( ... )' or ': base( ... )' clause or any start /// of the function body otherwise an <see cref="ArgumentException"/> is thrown. /// </summary> /// <param name="this">This scope.</param> /// <param name="declaration">The header of the function.</param> /// <returns>The new or existing function scope.</returns> public static IFunctionScope FindOrCreateFunction(this IFunctionDefinerScope @this, string declaration) { var fDef = FunctionDefinition.Parse(declaration, out string?bodyStart); if (fDef.ReturnType == null && fDef.ThisOrBaseConstructorCall != FunctionDefinition.CallConstructor.None) { throw new ArgumentException($"Constructor must not specify a ': this( ... )' or ': base( ... )' clause when using FindOrCreateFunction: {declaration}", nameof(declaration)); } if (bodyStart != null) { throw new ArgumentException($"Function must not specify the start of its body when using FindOrCreateFunction: {declaration}", nameof(declaration)); } return(@this.FindFunction(fDef.Key, false) ?? @this.CreateFunction(fDef)); }
public FunctionScopeImpl Create(CodeWorkspaceImpl ws, IFunctionDefinerScope h, Action <IFunctionScope> header) { if (header == null) { throw new ArgumentNullException(nameof(header)); } FunctionScopeImpl f = new FunctionScopeImpl(ws, h); header(f); f.Initialize(); _funcs.Add(f.Name, f); ws.OnFunctionCreated(f); return(f); }
public FunctionScopeImpl Create(CodeWorkspaceImpl ws, IFunctionDefinerScope h, FunctionDefinition def) { if (def == null) { throw new ArgumentNullException(nameof(def)); } if (_funcs.ContainsKey(def.Key)) { throw new ArgumentException($"Funcion or constructor with key {def.Key} already exists.", nameof(def)); } FunctionScopeImpl f = new FunctionScopeImpl(ws, h, def); _funcs.Add(f.Name, f); return(f); }