internal static IProductionRuleSeries ExpandTemplates(this IProductionRuleSeries series, IList <IOilexerGrammarTokenEntry> availableStock, IOilexerGrammarProductionRuleEntry entry, OilexerGrammarFile file, ICompilerErrorCollection errors) { List <IProductionRule> result = new List <IProductionRule>(); foreach (IProductionRule rule in series.ToArray()) { if (!series.NeedsExpansion(entry)) { result.Add(rule); } else { result.Add(rule.ExpandTemplates(availableStock, entry, file, errors)); } } if (EachSubitemIsNamelessGroup(result)) { List <IProductionRule> minimizedResult = new List <IProductionRule>(); foreach (var rule in result) { foreach (IProductionRuleGroupItem item in rule) { foreach (var subRule in item) { minimizedResult.Add(subRule); } } } result = minimizedResult; } return(new ProductionRuleSeries(result)); }
public static IProductionRuleGroupItem Deliteralize(this IProductionRuleGroupItem ruleGroupItem, IOilexerGrammarProductionRuleEntry currentEntry, IList <IOilexerGrammarTokenEntry> availableStock, OilexerGrammarFile file, ICompilerErrorCollection errors) { if (((IProductionRuleSeries)(ruleGroupItem)).NeedsDeliteralized()) { IProductionRuleSeries series = ((IProductionRuleSeries)(ruleGroupItem)).Deliteralize(currentEntry, availableStock, file, errors); ProductionRuleGroupItem result = new ProductionRuleGroupItem(series.ToArray(), ruleGroupItem.Column, ruleGroupItem.Line, ruleGroupItem.Position); result.Name = ruleGroupItem.Name; result.RepeatOptions = ruleGroupItem.RepeatOptions; return(result); } else { return(ruleGroupItem); } }
public static IProductionRuleGroupItem FinalLink(this IProductionRuleGroupItem ruleGroupItem, IOilexerGrammarProductionRuleEntry currentEntry, OilexerGrammarFile file, ICompilerErrorCollection errors) { if (((IProductionRuleSeries)(ruleGroupItem)).NeedsFinalLinking()) { IProductionRuleSeries series = ((IProductionRuleSeries)(ruleGroupItem)).FinalLink(currentEntry, file, errors); if (series == null) { return(null); } ProductionRuleGroupItem result = new ProductionRuleGroupItem(series.ToArray(), ruleGroupItem.Column, ruleGroupItem.Line, ruleGroupItem.Position); result.Name = ruleGroupItem.Name; result.RepeatOptions = ruleGroupItem.RepeatOptions; return(result); } else { return(ruleGroupItem); } }
internal static IProductionRuleItem Expand(this IProductionRuleItem ruleItem, IOilexerGrammarProductionRuleEntry currentEntry, IList <IOilexerGrammarTokenEntry> availableStock, ProductionRuleTemplateArgumentSeries argumentLookup, IOilexerGrammarProductionRuleTemplateEntry entry, OilexerGrammarFile file, ICompilerErrorCollection errors) { if (ruleItem is IProductionRulePreprocessorDirective) { return(((IProductionRulePreprocessorDirective)(ruleItem)).Expand(currentEntry, availableStock, argumentLookup, entry, file, errors)); } else if (ruleItem is ITemplateParamReferenceProductionRuleItem) { TemplateParamReferenceProductionRuleItem trpri = (TemplateParamReferenceProductionRuleItem)ruleItem; if (argumentLookup.Lookup.ContainsKey(trpri.Reference)) { IProductionRuleSeries series = argumentLookup.Lookup[trpri.Reference].Replacement; /* * * Fix 4-29-2013 * * * Series null check, if an error is thrown by the user's template within a replacement * the result of the replacement is null, thus this is null. * */ if (series == null) { return(null); } if (series.Count == 1 && series[0].Count == 1) { IProductionRuleItem ipri = series[0][0].Clone(); trpri.CloneData(ipri); return(ipri); } else { ProductionRuleGroupItem result = new ProductionRuleGroupItem(series.ToArray(), trpri.Column, trpri.Line, trpri.Position); trpri.CloneData(result); return(result); } } else { return(trpri.Clone()); } } else if (ruleItem is ITemplateReferenceProductionRuleItem) { ITemplateReferenceProductionRuleItem rI = ruleItem as TemplateReferenceProductionRuleItem; List <IProductionRuleSeries> serii = new List <IProductionRuleSeries>(); foreach (IProductionRuleSeries series in rI) { var seriesCopy = series; /* * * Handle deliteralization here to expedite * phase 3. If a template yields 5000 literals * then deliteralizing that will waste a massive * chunk of processor cycles. * */ if (series.NeedsDeliteralized()) { seriesCopy = seriesCopy.Deliteralize(currentEntry, availableStock, file, errors); } serii.Add(seriesCopy.Expand(currentEntry, availableStock, argumentLookup, entry, file, errors)); } TemplateReferenceProductionRuleItem result = new TemplateReferenceProductionRuleItem(rI.Reference, serii, rI.Column, rI.Line, rI.Position); ((TemplateReferenceProductionRuleItem)ruleItem).CloneData(result); return(result.Expand(availableStock, entry, file, errors)); } else if (ruleItem is IProductionRuleGroupItem) { if (!((IProductionRuleSeries)(ruleItem)).HasExpansion()) { return(ruleItem.Clone()); } else { ProductionRuleGroupItem result = new ProductionRuleGroupItem(((IProductionRuleSeries)ruleItem).Expand(currentEntry, availableStock, argumentLookup, entry, file, errors).ToArray(), ruleItem.Column, ruleItem.Line, ruleItem.Position); result.RepeatOptions = ruleItem.RepeatOptions; result.Name = ruleItem.Name; return(result); } } else { return(ruleItem.Clone()); } }