Ejemplo n.º 1
0
        public MacroDefinition Resolve(string macroName)
        {
            MacroDefinition macroDefinition;

            var key = macroName.Replace("-", "").Replace("_", "_");

            if (!_macros.TryGetValue(key, out macroDefinition))
            {
                var name = key.Substring(0, 1).ToUpper() + key.Substring(1).ToLower();
                macroDefinition = new MacroDefinition()
                {
                    Name = name
                };
                _macros.Add(macroDefinition.Name, macroDefinition);
            }

            return(macroDefinition);
        }
Ejemplo n.º 2
0
 public void PushMacro(MacroDefinition macroDefinition, string hookName)
 {
     Push(CodeContextTypes.MethodCall, macroDefinition.Name, new Bag <string>().Set <MacroDefinition>("definition", macroDefinition).Set <string>("hook", hookName));
 }
Ejemplo n.º 3
0
        private void ProcessMacroMethod(MacroAttribute[] macroAttributes, MethodInfo method)
        {
            var parameters = method.GetParameters();

            bool requiresHook = macroAttributes.FirstOrDefault(m => m.RequiresHook)?.RequiresHook ?? false;

            bool requiresState = macroAttributes.FirstOrDefault(m => m.RequiresState)?.RequiresState ?? false;

            int maxAttributes = parameters.Length;

            int minAttributes   = 0;
            int parameterNumber = 1;

            foreach (var parameter in parameters)
            {
                if (!parameter.IsOptional)
                {
                    if (parameter.DefaultValue == null)
                    {
                        minAttributes++;
                    }
                }
                if (parameter.ParameterType.Name == "IState`1")
                {
                    requiresState = true;
                    if (parameterNumber != 1)
                    {
                        throw new InvalidOperationException($"Only the first parameter of macro method {method.Name} can be used to pass template state IState<>");
                    }
                }
                if (parameter.ParameterType.Name == "HookDelegate")
                {
                    requiresHook = true;
                    if (parameterNumber != maxAttributes)
                    {
                        throw new InvalidOperationException($"Only the last parameter of macro method {method.Name} can be used to pass the hook");
                    }
                }
                parameterNumber++;
            }

            MacroDefinition definition = new MacroDefinition()
            {
                Name          = method.Name,
                MinArguments  = minAttributes,
                MaxArguments  = maxAttributes,
                BranchOf      = macroAttributes.FirstOrDefault(m => m.BranchOf != null)?.BranchOf,
                RequiresHook  = requiresHook,
                RequiresState = requiresState,
                Defined       = true
            };

            _macros.Add(definition.Name, definition);
            foreach (var macroAttribute in macroAttributes)
            {
                if (macroAttribute.Alias != null)
                {
                    MacroDefinition aliasedDefinition;
                    if (_macros.TryGetValue(macroAttribute.Alias, out aliasedDefinition))
                    {
                        if (definition == aliasedDefinition)
                        {
                            throw new InvalidOperationException($"Template base {_templateType.FullName} macro {method.Name} is attempting to redefine the macro alias \"{macroAttribute.Alias}\" which is already used by the macro {aliasedDefinition.Name}");
                        }
                        else
                        {
                            throw new InvalidOperationException($"Template base {_templateType.FullName} macro {method.Name} has a duplicate macro alias \"{macroAttribute.Alias}\"");
                        }
                    }
                    else
                    {
                        _macros.Add(macroAttribute.Alias, definition);
                    }
                }
            }
            if (requiresState)
            {
                definition.MinArguments--;
                definition.MaxArguments--;
            }
            if (requiresHook)
            {
                definition.MinArguments--;
                definition.MaxArguments--;
            }
        }