public MacroInfo(Symbol @namespace, LexicalMacroAttribute a, LexicalMacro macro, bool deprecateAllNames = false) : base(a.Syntax, a.Description, a.Names) { DeprecatedNames = a.DeprecatedNames; DeprecationMessage = a.DeprecationMessage; if (Names.Length == 0 && (a.DeprecatedNames == null || a.DeprecatedNames.Length == 0) && (a.Mode & (MacroMode.MatchEveryCall | MacroMode.MatchEveryLiteral | MacroMode.MatchEveryIdentifier)) == 0) { Names = new string[1] { macro.Method.Name } } ; if (deprecateAllNames) { DeprecatedNames = (DeprecatedNames ?? EmptyArray <string> .Value).Union(Names).ToArray(); } CheckParam.IsNotNull("macro", macro); Namespace = @namespace; Macro = macro; Mode = a.Mode; }
public MacroInfo(Symbol @namespace, LexicalMacroAttribute a, LexicalMacro macro) : base(a.Syntax, a.Description, a.Names != null && a.Names.Length > 0 ? a.Names : new[] { macro.Method.Name }) { CheckParam.IsNotNull("macro", macro); Namespace = @namespace; Macro = macro; Mode = a.Mode; }
public MacroInfo(Symbol @namespace, Symbol name, LexicalMacro macro, LexicalMacroAttribute info) { NamespaceSym = @namespace; Name = name; Macro = macro; Info = info; Mode = info.Mode; if ((Mode & MacroMode.PriorityMask) == 0) { Mode |= MacroMode.NormalPriority; } }
private static LNode RegisterSimpleMacro(LNodeList attrs, LNode pattern, LNode body, IMacroContext context) { if (DecodeSubstitutionExpr(pattern, out _, out _, out _) != null) { return(Reject(context, pattern, "Defining a macro that could match everything is not allowed.")); } MacroMode modes = GetMacroMode(ref attrs, pattern); LNode macroName = pattern.Target ?? pattern; LNode replacement = body.AsList(S.Braces).AsLNode(S.Splice); if (pattern.IsCall) { WarnAboutMissingDollarSigns(pattern.Args, context, pattern, replacement); } // Note: we could fill out the macro's Syntax and Description with the // pattern and replacement converted to strings, but it's generally a // waste of CPU time as those strings are usually not requested. // Compromise: provide syntax pattern only var syntax = pattern.ToString(); var lma = new LexicalMacroAttribute(syntax, "User-defined macro at {0}".Localized(pattern.Range.Start), macroName.Name.Name) { Mode = modes }; if ((modes & (MacroMode.MatchEveryLiteral | MacroMode.MatchEveryCall | MacroMode.MatchEveryIdentifier)) != 0) { lma = new LexicalMacroAttribute(syntax, lma.Description) { Mode = modes } } ; var macroInfo = new MacroInfo(null, lma, UserDefinedMacro); macroInfo.Mode |= MacroMode.UseLogicalNameInErrorMessages; context.RegisterMacro(macroInfo); return(F.Splice()); // delete the `define` node from the output LNode UserDefinedMacro(LNode candidate, IMacroContext context2) { MMap <Symbol, LNode> captures = new MMap <Symbol, LNode>(); if (candidate.MatchesPattern(pattern, ref captures, out LNodeList unmatchedAttrs)) { LNode replacement2 = WithUniqueIdentifiers(replacement, context.IncrementTempCounter, out _); return(ReplaceCaptures(replacement2, captures).PlusAttrsBefore(unmatchedAttrs)); } return(null); } }
public static LNode replaceFn(LNode node, IMacroContext context1) { var retType = node.Args[0, LNode.Missing].Name; if (retType != _replace && retType != _define) { return(null); } LNode replaceKw, macroName, args, body; if (EcsValidators.MethodDefinitionKind(node, out replaceKw, out macroName, out args, out body, allowDelegate: false) != S.Fn || body == null) { return(null); } MacroMode mode, modes = 0; var leftoverAttrs = node.Attrs.SmartWhere(attr => { if (attr.IsId && Loyc.Compatibility.EnumStatic.TryParse(attr.Name.Name, out mode)) { modes |= mode; return(false); } return(true); }); LNode pattern = F.Call(macroName, args.Args).PlusAttrs(leftoverAttrs); LNode replacement = body.AsList(S.Braces).AsLNode(S.Splice).PlusAttrs(replaceKw.Attrs); replacement.Style &= ~NodeStyle.OneLiner; WarnAboutMissingDollarSigns(args, context1, pattern, replacement); // Note: we could fill out the macro's Syntax and Description with the // pattern and replacement converted to strings, but it's generally a // waste of CPU time as those strings are usually not requested. var lma = new LexicalMacroAttribute( string.Concat(macroName.Name, "(", args.Args.Count.ToString(), " args)"), "", macroName.Name.Name); var macroInfo = new MacroInfo(null, lma, (candidate, context2) => { MMap <Symbol, LNode> captures = new MMap <Symbol, LNode>(); VList <LNode> unmatchedAttrs; if (candidate.MatchesPattern(pattern, ref captures, out unmatchedAttrs)) { return(ReplaceCaptures(replacement, captures).PlusAttrsBefore(unmatchedAttrs)); } return(null); }) { Mode = modes }; context1.RegisterMacro(macroInfo); return(F.Splice()); }
public static LNode replaceFn(LNode node, IMacroContext context1) { var retType = node.Args[0, LNode.Missing].Name; if (retType != _replace && retType != _define) return null; LNode replaceKw, macroName, args, body; if (EcsValidators.MethodDefinitionKind(node, out replaceKw, out macroName, out args, out body, allowDelegate: false) != S.Fn || body == null) return null; MacroMode mode, modes = 0; var leftoverAttrs = node.Attrs.SmartWhere(attr => { if (attr.IsId && Loyc.Compatibility.EnumStatic.TryParse(attr.Name.Name, out mode)) { modes |= mode; return false; } return true; }); LNode pattern = F.Call(macroName, args.Args).PlusAttrs(leftoverAttrs); LNode replacement = body.AsList(S.Braces).AsLNode(S.Splice).PlusAttrs(replaceKw.Attrs); replacement.Style &= ~NodeStyle.OneLiner; WarnAboutMissingDollarSigns(args, context1, pattern, replacement); // Note: we could fill out the macro's Syntax and Description with the // pattern and replacement converted to strings, but it's generally a // waste of CPU time as those strings are usually not requested. var lma = new LexicalMacroAttribute( string.Concat(macroName.Name, "(", args.Args.Count.ToString(), " args)"), "", macroName.Name.Name); var macroInfo = new MacroInfo(null, lma, (candidate, context2) => { MMap<Symbol, LNode> captures = new MMap<Symbol, LNode>(); VList<LNode> unmatchedAttrs; if (candidate.MatchesPattern(pattern, ref captures, out unmatchedAttrs)) { return ReplaceCaptures(replacement, captures).PlusAttrsBefore(unmatchedAttrs); } return null; }) { Mode = modes }; context1.RegisterMacro(macroInfo); return F.Splice(); }