private IReadOnlyList <IMacroConfig> ProduceMacroConfig(List <IMacroConfig> computedMacroConfigs) { List <IMacroConfig> generatedMacroConfigs = new List <IMacroConfig>(); if (Guids != null) { int guidCount = 0; foreach (Guid guid in Guids) { int id = guidCount++; string replacementId = "guid" + id; generatedMacroConfigs.Add(new GuidMacroConfig(replacementId, null)); _guidToGuidPrefixMap[guid] = replacementId; } } if (Symbols != null) { foreach (KeyValuePair <string, ISymbolModel> symbol in Symbols) { if (symbol.Value.Binding == "safe_name") { _safeNameName = symbol.Key; // save for later, to generate the safe_name regex macro } if (symbol.Value.Type == "computed") { string value = ((ComputedSymbol)symbol.Value).Value; string evaluator = ((ComputedSymbol)symbol.Value).Evaluator; computedMacroConfigs.Add(new EvaluateMacroConfig(symbol.Key, value, evaluator)); } else if (symbol.Value.Type == "generated") { GeneratedSymbol symbolInfo = (GeneratedSymbol)symbol.Value; string type = symbolInfo.Generator; string variableName = symbol.Key; Dictionary <string, JToken> configParams = new Dictionary <string, JToken>(); foreach (KeyValuePair <string, JToken> parameter in symbolInfo.Parameters) { configParams.Add(parameter.Key, parameter.Value); } generatedMacroConfigs.Add(new GeneratedSymbolDeferredMacroConfig(type, variableName, configParams)); } } } if (_safeNameName == null) { IList <KeyValuePair <string, string> > steps = new List <KeyValuePair <string, string> >(); steps.Add(new KeyValuePair <string, string>(@"\W", "_")); generatedMacroConfigs.Add(new RegexMacroConfig("safe_name", NameParameter.Name, steps)); _safeNameName = "safe_name"; } return(generatedMacroConfigs); }
public static ISymbolModel FromJObject(JObject jObject) { GeneratedSymbol sym = new GeneratedSymbol { Binding = jObject.ToString(nameof(Binding)), Generator = jObject.ToString(nameof(Generator)), Parameters = jObject.ToJTokenDictionary(StringComparer.Ordinal, nameof(Parameters)), Type = jObject.ToString(nameof(Type)), Replaces = jObject.ToString(nameof(Replaces)) }; return(sym); }
public static GeneratedSymbol FromJObject(JObject jObject) { GeneratedSymbol sym = new GeneratedSymbol { Binding = jObject.ToString(nameof(Binding)), Generator = jObject.ToString(nameof(Generator)), DataType = jObject.ToString(nameof(DataType)), Parameters = jObject.ToJTokenDictionary(StringComparer.Ordinal, nameof(Parameters)), Type = jObject.ToString(nameof(Type)), Replaces = jObject.ToString(nameof(Replaces)), ReplacementContexts = SymbolModelConverter.ReadReplacementContexts(jObject) }; return(sym); }
public static ISymbolModel GetModelForObject(JObject jObject) { switch (jObject.ToString(nameof(ISymbolModel.Type))) { case "parameter": return(ParameterSymbol.FromJObject(jObject)); case "computed": return(ComputedSymbol.FromJObject(jObject)); case "generated": return(GeneratedSymbol.FromJObject(jObject)); default: return(null); } }
// Note: Only ParameterSymbol has a Description property, this it's the only one that gets localization // TODO: change how localization gets merged in, don't do it here. public static ISymbolModel GetModelForObject(JObject jObject, IParameterSymbolLocalizationModel localization, string defaultOverride) { switch (jObject.ToString(nameof(ISymbolModel.Type))) { case ParameterSymbol.TypeName: return(ParameterSymbol.FromJObject(jObject, localization, defaultOverride)); case ComputedSymbol.TypeName: return(ComputedSymbol.FromJObject(jObject)); case BindSymbolTypeName: case GeneratedSymbol.TypeName: return(GeneratedSymbol.FromJObject(jObject)); default: return(null); } }
//TODO: Restructure this to take language idioms into account while allowing // the expanded conditional syntax and operation toggling to work private Dictionary <string, JObject> ProduceConfig(string switchPrefix, string conditionalPrefix, string actionableConditionalPrefix, string commentToken, bool generateMacros) { Dictionary <string, JObject> cfg = new Dictionary <string, JObject> { { "flags", JObject.Parse($@"{{ ""conditionals"": {{ ""on"": ""{switchPrefix}+:cnd"", ""off"": ""{switchPrefix}-:cnd"" }}, ""replacements"": {{ ""on"": ""{switchPrefix}+:replacements"", ""off"": ""{switchPrefix}-:replacements"" }}, ""expandVariables"": {{ ""on"": ""{switchPrefix}+:vars"", ""off"": ""{switchPrefix}-:vars"" }}, ""flags"": {{ ""off"": ""{switchPrefix}-:flags"" }}, ""include"": {{ ""on"": ""{switchPrefix}+:include"", ""off"": ""{switchPrefix}-:include"" }} }}") }, { "conditionals", JObject.Parse($@"{{ ""if"": [ ""{conditionalPrefix}if"" ], ""else"": [ ""{conditionalPrefix}else"" ], ""elseif"": [ ""{conditionalPrefix}elseif"" ], ""endif"": [ ""{conditionalPrefix}endif"" ], ""evaluator"": ""C++"", ""wholeLine"": true, ""trim"": true }}") }, { "variables", JObject.Parse(@"{ ""sources"": { ""environment"": ""env_{0}"", ""user"": ""usr_{0}"", }, ""order"": [ ""environment"", ""user"" ], ""fallbackFormat"": ""{0}"", ""expand"": false }") }, { "replacements", JObject.Parse($@"{{ ""{commentToken}"": {{ ""replaceWith"": """", ""id"": ""stripComments"" }}, ""{commentToken}{commentToken}"": {{ ""replaceWith"": ""{commentToken}"", ""id"": ""preserveDoubleComment"" }} }}") } }; if (!string.IsNullOrEmpty(actionableConditionalPrefix)) { JObject cndDef = cfg["conditionals"]; cndDef["actionableIf"] = new JArray { $"{actionableConditionalPrefix}if" }; cndDef["actionableElse"] = new JArray { $"{actionableConditionalPrefix}else" }; cndDef["actionableElseif"] = new JArray { $"{actionableConditionalPrefix}elseif" }; cndDef["actions"] = new JArray { "stripComments", "preserveDoubleComment" }; } if (generateMacros) { cfg["macros"] = new JObject(); } if (generateMacros && Guids != null) { int guidCount = 0; foreach (Guid guid in Guids) { JObject macros = cfg["macros"]; JObject macro = new JObject(); int id = guidCount++; macros["guid" + id] = macro; macro["type"] = "guid"; macro["action"] = "new"; _guidToGuidPrefixMap[guid] = "guid" + id; } } foreach (KeyValuePair <Guid, string> map in _guidToGuidPrefixMap) { foreach (char fmt in "ndbpxNDPBX") { string rplc = char.IsUpper(fmt) ? map.Key.ToString(fmt.ToString()).ToUpperInvariant() : map.Key.ToString(fmt.ToString()).ToLowerInvariant(); cfg["replacements"][rplc] = map.Value + "-" + fmt; } } if (Symbols != null) { foreach (KeyValuePair <string, ISymbolModel> symbol in Symbols) { if (symbol.Value.Binding == "safe_name") { _safeNameName = symbol.Key; } if (symbol.Value.Type == "computed" && generateMacros) { JObject cmp = new JObject(); cfg["macros"][symbol.Key] = cmp; cmp["type"] = "evaluate"; cmp["action"] = ((ComputedSymbol)symbol.Value).Value; cmp["evaluator"] = "C++"; } else if (symbol.Value.Type == "generated" && generateMacros) { JObject gen = new JObject(); cfg["macros"][symbol.Key] = gen; GeneratedSymbol gs = (GeneratedSymbol)symbol.Value; gen["type"] = gs.Generator; foreach (KeyValuePair <string, string> parameter in gs.Parameters) { gen[parameter.Key] = parameter.Value; } } if (symbol.Value.Replaces != null) { cfg["replacements"][symbol.Value.Replaces] = symbol.Key; } } } if (_safeNameName == null && generateMacros) { cfg["macros"]["safe_name"] = JObject.Parse(@"{ ""type"": ""regex"", ""action"": ""replace"", ""source"": """ + NameParameter.Name + @""", ""steps"": [{ ""regex"": ""\\W"", ""replacement"": ""_"" }] }"); _safeNameName = "safe_name"; } cfg["replacements"][SourceName] = _safeNameName; return(cfg); }