ConfigCommandMethod GetImplicitCommand(string methodName, ConfigRule rule) { var method = new ConfigCommandMethod() { Name = new ConfigToken(ConfigTokenType.Label, rule.FromPos, rule.ToPos, methodName), }; for (var i = 0; i < rule.Segments.Count; i++) { var segment = rule.Segments[i]; var token = segment.Token; if (segment.Modifier == null || segment.Modifier.Type != ConfigTokenType.Bang) { ConfigCommand argument = new ConfigCommandArg() { Value = new ConfigToken(ConfigTokenType.ArgumentValue, token.FromPos, token.ToPos, i.ToString(CultureInfo.InvariantCulture)), }; SetCommandSegment(argument, segment.Segment); method.Arguments.Add(argument); } } return(method); }
protected override ConfigProduction Reduce_RuleList_2(ConfigRule rule) { var production = new ConfigProduction(); production.Rules.Add(rule); return(production); }
void PopulateCommand(ConfigProduction production, ConfigRule rule, int ruleNum) { var command = rule.Command; if (command == null) { var targetName = production.Target.Text; var methodName = string.Format(CultureInfo.InvariantCulture, "Reduce_{0}_{1}", targetName, ruleNum); rule.Command = command = GetImplicitCommand(methodName, rule); } else { var validator = new CommandValidator(_reporter); validator.Validate(command, rule.Production); } SetCommandSegment(command, production.Segment); }
void DecorateRule(ConfigProduction cProduction, ConfigRule cRule, int ruleNum) { var cSegments = cRule.Segments; ImmutableArray <Segment> segments; if (cSegments.Count == 0) { cRule.FromPos = cProduction.Target.FromPos; cRule.ToPos = cProduction.Target.ToPos; segments = ImmutableArray <Segment> .Empty; } else { var builder = ImmutableArray.CreateBuilder <Segment>(cSegments.Count); cRule.FromPos = cSegments[0].Token.FromPos; cRule.ToPos = cSegments[cSegments.Count - 1].Token.ToPos; for (var i = 0; i < cSegments.Count; i++) { var cSegment = cSegments[i]; var segment = GetSegment(cSegment.Token, cSegment.Modifier); cSegment.Segment = segment; builder.Add(segment); } segments = builder.MoveToImmutable(); } var production = new Production(cProduction.Segment, segments); cRule.Production = production; PopulateCommand(cProduction, cRule, ruleNum); if (!_config.RuleLookup.ContainsKey(production)) { _config.RuleLookup.Add(production, cRule); } else { ReporterHelper.AddWarning(_reporter, cRule, "The production '{0}' has already been defined.", production); } }
static void ReportReduceReduceConflict(Config config, List <Production> productions, IErrorReporter reporter) { var rules = new ConfigRule[productions.Count]; var starts = new int[productions.Count]; for (var i = 0; i < rules.Length; i++) { var production = productions[i]; if (production.Target.IsInitial) { starts[i] = -1; } else { var rule = config.RuleLookup[production]; rules[i] = rule; starts[i] = rule.FromPos.Index; } } Array.Sort(starts, rules); string format; string val; if (rules[0] == null) { format = "{0} causes a reduce-accept conflict."; val = null; } else { format = "{0} causes a reduce-reduce conflict with {1}."; val = rules[0].Production.ToString(); } for (var i = 1; i < rules.Length; i++) { ReporterHelper.AddError(reporter, rules[i], format, rules[i].Production, val); } }
Segment GetOptionalProduction(ConfigToken token) { var subSegment = GetSegment(token); if (!_optionalProductions.TryGetValue(subSegment, out var masterSegment)) { ConfigUsing cUsing; masterSegment = subSegment.GetOptional(); if (subSegment.IsTerminal) { if ((cUsing = _config.TerminalType) != null) { _ntTypes.Add(masterSegment, cUsing); } } else if (_ntTypes.TryGetValue(subSegment, out cUsing)) { _ntTypes.Add(masterSegment, cUsing); } var rule1 = new ConfigRule() { Segments = { new ConfigSegment() { Segment = subSegment, Token = token, }, }, Production = new Production(masterSegment, ImmutableArray.Create(subSegment)), Command = new ConfigCommandArg() { Value = new ConfigToken(ConfigTokenType.ArgumentValue, token.FromPos, token.ToPos, "0"), Segment = subSegment, Using = cUsing, }, FromPos = token.FromPos, ToPos = token.ToPos, }; var rule2 = new ConfigRule() { Segments = { }, Production = new Production(masterSegment, ImmutableArray <Segment> .Empty), Command = new ConfigCommandNull() { Value = new ConfigToken(ConfigTokenType.Null, token.FromPos, token.ToPos, "null"), Segment = subSegment, Using = cUsing, }, FromPos = token.FromPos, ToPos = token.ToPos, }; var cProduction = new ConfigProduction() { Segment = masterSegment, Target = new ConfigToken(ConfigTokenType.NonTerminal, token.FromPos, token.ToPos, masterSegment.Name), Using = cUsing, Rules = { rule1, rule2, }, }; _config.Productions.Add(cProduction); _config.RuleLookup.Add(rule1.Production, rule1); _config.RuleLookup.Add(rule2.Production, rule2); _optionalProductions.Add(subSegment, masterSegment); } return(masterSegment); }
protected override ConfigRule Reduce_SegmentList_1(ConfigRule segmentListSeg, ConfigSegment segmentSeg) { segmentListSeg.Segments.Add(segmentSeg); return(segmentListSeg); }
protected override ConfigRule Reduce_Rule_2(ConfigRule segmentListSeg, ConfigCommand commandSeg) { segmentListSeg.Command = commandSeg; return(segmentListSeg); }
protected override ConfigProduction Reduce_RuleList_1(ConfigProduction ruleListSeg, ConfigRule rule) { ruleListSeg.Rules.Add(rule); return(ruleListSeg); }