private bool DoHasUnreachableAlternative(string ruleName, ChoiceExpression choice) { Debug.Assert(choice != null); bool has = false; for (int i = 0; i < choice.Expressions.Length - 1 && !has; ++i) { // 'x' / . / 'z' if (DoAlwaysSucceeds(ruleName, choice.Expressions[i])) { has = true; } // 'xx' / 'xxY' else if (DoHasBadLiteralPrefix(choice)) { has = true; } // [e] / [f] where e is a superset of the chars in f else if (DoHasBadRangePrefix(choice)) { has = true; } } return(has); }
private bool DoHasBadLiteralPrefix(ChoiceExpression choice) { Debug.Assert(choice != null); bool bad = false; for (int i = 0; i < choice.Expressions.Length - 1 && !bad; ++i) { LiteralExpression literal1 = choice.Expressions[i] as LiteralExpression; if (literal1 != null) { for (int j = i + 1; j < choice.Expressions.Length && !bad; ++j) { LiteralExpression literal2 = choice.Expressions[j] as LiteralExpression; if (literal2 != null) { if (m_settings["ignore-case"] == "true") { bad = literal2.Literal.ToLower().StartsWith(literal1.Literal.ToLower()); } else { bad = literal2.Literal.StartsWith(literal1.Literal); } } } } } return(bad); }
private bool DoHasBadRangePrefix(ChoiceExpression choice) { Debug.Assert(choice != null); bool bad = false; for (int i = 0; i < choice.Expressions.Length - 1 && !bad; ++i) { var range1 = choice.Expressions[i] as RangeExpression; if (range1 != null) { var chars1 = new CharSet(range1); for (int j = i + 1; j < choice.Expressions.Length && !bad; ++j) { var range2 = choice.Expressions[j] as RangeExpression; if (range2 != null) { var chars2 = new CharSet(range2); bad = chars1.IsSuperSetOf(chars2); } } } } return(bad); }
protected virtual void WalkChoiceExpression(ChoiceExpression choiceExpression) { foreach (var expression in choiceExpression.Choices) { this.WalkExpression(expression); } }
private static Expression Choice(INode node) { if (node.Count > 1) { var expressions = node.Children.Select(Expression); return(ChoiceExpression.From(node, expressions)); } return(Expression(node[0])); }
protected override void WalkChoiceExpression(ChoiceExpression choiceExpression) { var namesCopy = new HashSet <string>(this.currentNames); foreach (var expression in choiceExpression.Choices) { this.WalkExpression(expression); this.currentNames.IntersectWith(namesCopy); } }
protected override void VisitChoiceExpression(ChoiceExpression expression) { var start = parsers.Count; base.VisitChoiceExpression(expression); var parser = new Choice(Pop(start)); parsers.Add(parser); }
protected override void VisitChoiceExpression(ChoiceExpression expression) { base.VisitChoiceExpression(expression); foreach (var choice in expression.Choices) { if (choice.Attr.IsNullable) { SetNullable(expression, true); } } }
private Expression Expression(INode node) { Debug.Assert(node.Name == "choice"); if (node.Count > 1) { var choices = node.Children.Select(Sequence); return(ChoiceExpression.From(node, choices)); } return(Sequence(node[0])); }
private bool DoAlwaysSucceeds(string ruleName, Expression expr) { bool succeeds = false; do { ChoiceExpression choice = expr as ChoiceExpression; if (choice != null) { // Note that we only need to check the last alternative because we'll // get another error if an interior one always succeeds. succeeds = DoAlwaysSucceeds(ruleName, choice.Expressions[choice.Expressions.Length - 1]); break; } RangeExpression range = expr as RangeExpression; if (range != null) { succeeds = range.ToString() == "."; break; } RepetitionExpression rep = expr as RepetitionExpression; if (rep != null) { succeeds = rep.Min == 0; break; } SequenceExpression seq = expr as SequenceExpression; if (seq != null) { succeeds = seq.Expressions.All(e => DoAlwaysSucceeds(ruleName, e)); break; } RuleExpression rule2 = expr as RuleExpression; if (rule2 != null && rule2.Name != ruleName) { succeeds = DoAlwaysSucceeds(rule2.Name); break; } }while (false); return(succeeds); }
protected override void VisitChoiceExpression(ChoiceExpression expression) { CS.IndentInOut( "ChoiceExpression", () => CheckChoices(expression.Choices.ToList(), 0)); void CheckChoices(IReadOnlyList <Expression> exprs, int index) { if (index < exprs.Count) { VisitExpression(exprs[index]); index += 1; if (index < exprs.Count) { CS.If( $"!{Locals.Result}.IsSuccess", () => CheckChoices(exprs, index)); } } } }
protected override void WalkChoiceExpression(ChoiceExpression choiceExpression) { base.WalkChoiceExpression(choiceExpression); this.Set(choiceExpression, choiceExpression.Choices[0]); }
protected override void WalkChoiceExpression(ChoiceExpression choiceExpression) => this.RenderChoiceExpression(choiceExpression, this.writer, this.currentIndentation);
private Expression DoChoice(List<Result> results) { Expression value = null; if (results.Count == 1) value = results[0].Value; else if (results.Count > 1) value = new ChoiceExpression((from r in results where r.Value != null select r.Value).ToArray()); return value; }