private static bool BadKeyPredicate(FallbackNode node, IToken token, IResultAccumulator resultAccumulator) { return (node.Name?.ToLowerInvariant() == "bad-key-fallback" && token is TextToken textToken && textToken.Text.StartsWith("-")); }
protected override bool AcceptsTokenImpl(IToken token, IResultAccumulator resultAccumulator) { if (token is TextToken textToken) { var text = textToken.Text; if (!this.IsCaseSensitive) { text = text.ToLowerInvariant(); } if (string.Equals(text, this.ExactText)) { var textTokenClass = textToken.Class; if ( this.TextClassesImpl.Contains(textTokenClass) || this.TextClassesImpl.Any(x => string.Equals(text, x.TryConvertFrom(text, textTokenClass))) ) { return(true); } } } return(false); }
public static T GetLastResult <T>(this IResultAccumulator accumulator) { if (accumulator == null) { throw new ArgumentNullException(nameof(accumulator)); } if (accumulator.Count == 0) { throw new InvalidOperationException("Result accumulator is empty."); } var index = accumulator.Count - 1; var result = accumulator[index]; if (result == null) { throw new NullReferenceException($"Last result is null."); } if (result.GetType() != typeof(T)) { throw new InvalidCastException( $"Last result expected to be of type '{typeof(T).FullName}', but is of type '{result.GetType().FullName}'."); } return((T)result); }
protected override bool AcceptsTokenImpl(IToken token, IResultAccumulator resultAccumulator) { var result = token is EnumToken<TEnum> enumToken && enumToken.Value.Equals(this.Value); return result; }
private void ValueAction(ActionNode node, IToken token, IResultAccumulator resultAccumulator) { var command = resultAccumulator.GetLastResult <CliCommand>(); var entry = command.Entries.Last(); var textToken = (TextToken)token; entry.Value = textToken.Text; }
protected override bool AcceptsTokenImpl(IToken token, IResultAccumulator resultAccumulator) { var result = token is PunctuationToken punctuationToken && punctuationToken.Value.Equals(this.Value); return(result); }
private bool BadOptionFallback(FallbackNode node, IToken token, IResultAccumulator resultAccumulator) { if (token is TextToken textToken) { return(textToken.Class is KeyTextClass); } return(false); }
protected override bool AcceptsTokenImpl(IToken token, IResultAccumulator resultAccumulator) { if (this.TokenAcceptPredicate == null) { throw new InvalidOperationException($"'{nameof(TokenAcceptPredicate)}' is null."); } return(this.TokenAcceptPredicate(token, resultAccumulator)); }
private void KeyAction(ActionNode node, IToken token, IResultAccumulator resultAccumulator) { var command = resultAccumulator.GetLastResult <CliCommand>(); var entry = new CliCommandEntry { Alias = node.Properties["alias"], }; command.Entries.Add(entry); }
private void ArgumentAction(ActionNode node, IToken token, IResultAccumulator resultAccumulator) { var command = resultAccumulator.EnsureExecutorCommand(); var alias = node.Properties["alias"]; var argument = TokenToArgument(token); var entry = CliCommandEntry.CreateArgument(alias, argument); command.Entries.Add(entry); }
public FallbackNodeAcceptedTokenException( FallbackNode fallbackNode, IToken token, IResultAccumulator resultAccumulator) : base("Fallback node accepted token.") { this.FallbackNode = fallbackNode ?? throw new ArgumentNullException(nameof(fallbackNode)); this.Token = token ?? throw new ArgumentNullException(nameof(token)); this.ResultAccumulator = resultAccumulator ?? throw new ArgumentNullException(nameof(resultAccumulator)); }
private void KeyAction(ActionNode node, IToken token, IResultAccumulator resultAccumulator) { var command = resultAccumulator.EnsureExecutorCommand(); var alias = node.Properties["alias"]; var key = TokenToKey(token); var entry = CliCommandEntry.CreateKeyValuePair(alias, key); command.Entries.Add(entry); }
private void OptionAction(ActionNode node, IToken token, IResultAccumulator resultAccumulator) { var command = resultAccumulator.GetLastResult <CliCommand>(); var alias = node.Properties["alias"]; var key = TokenToKey(token); var entry = CliCommandEntry.CreateOption(alias, key); command.Entries.Add(entry); }
public void Act(IToken token, IResultAccumulator resultAccumulator) { if (token == null) { throw new ArgumentNullException(nameof(token)); } if (resultAccumulator == null) { throw new ArgumentNullException(nameof(resultAccumulator)); } this.ActImpl(token, resultAccumulator); }
private void WorkerAction(ActionNode node, IToken token, IResultAccumulator resultAccumulator) { if (resultAccumulator.Count == 0) { var command = new CliCommand { WorkerName = node.Properties["worker-name"], }; resultAccumulator.AddResult(command); } else { var command = resultAccumulator.GetLastResult <CliCommand>(); command.WorkerName = node.Properties["worker-name"]; } }
protected override bool AcceptsTokenImpl(IToken token, IResultAccumulator resultAccumulator) { if (token is TextToken textToken) { var text = textToken.Text; var textTokenClass = textToken.Class; if ( this.TextClassesImpl.Contains(textTokenClass) || this.TextClassesImpl.Any(x => x.TryConvertFrom(text, textTokenClass) != null) ) { return(true); } } return(false); }
public static CliCommand AddAddInCommand(this IResultAccumulator resultAccumulator, string addInName) { if (resultAccumulator == null) { throw new ArgumentNullException(nameof(resultAccumulator)); } if (resultAccumulator.Count != 0) { throw new CliException("Internal error: result accumulator must be empty."); } var command = CliCommand.CreateAddInCommand(addInName); resultAccumulator.AddResult(command); return(command); }
public static CliCommand EnsureExecutorCommand(this IResultAccumulator resultAccumulator) { if (resultAccumulator == null) { throw new ArgumentNullException(nameof(resultAccumulator)); } if (resultAccumulator.Count == 0) { var command = CliCommand.CreateNamelessExecutorCommand(); resultAccumulator.AddResult(command); return(command); } else { var command = resultAccumulator.GetLastResult <CliCommand>(); return(command); } }
public bool AcceptsToken(IToken token, IResultAccumulator resultAccumulator) { if (token == null) { throw new ArgumentNullException(nameof(token)); } if (resultAccumulator == null) { throw new ArgumentNullException(nameof(resultAccumulator)); } var basicResult = this.AcceptsTokenImpl(token, resultAccumulator); if (!basicResult) { return(false); } var additionalCheck = this.AdditionalChecker?.Invoke(token, resultAccumulator) ?? true; return(additionalCheck); }
protected abstract void ActImpl(IToken token, IResultAccumulator resultAccumulator);
protected abstract bool AcceptsTokenImpl(IToken token, IResultAccumulator resultAccumulator);
protected override void ActImpl(IToken token, IResultAccumulator resultAccumulator) { throw new InvalidOperationException($"Cannot call '{nameof(Act)}' for end node."); }
protected override void ActImpl(IToken token, IResultAccumulator resultAccumulator) { throw new InvalidOperationException($"'{nameof(Act)}' should not be called for '{typeof(IdleNode).Name}'."); }
protected override bool AcceptsTokenImpl(IToken token, IResultAccumulator resultAccumulator) { return(this.FallbackPredicate(this, token, resultAccumulator)); }
protected override void ActImpl(IToken token, IResultAccumulator resultAccumulator) { throw new FallbackNodeAcceptedTokenException(this, token, resultAccumulator); }
private void ArgumentAction(ActionNode node, IToken token, IResultAccumulator resultAccumulator) { throw new NotSupportedException(); // will implement in real CLI. }
protected override void ActImpl(IToken token, IResultAccumulator resultAccumulator) { this.Action?.Invoke(this, token, resultAccumulator); resultAccumulator.Modify(); }
private void ExecutorAction(ActionNode node, IToken token, IResultAccumulator resultAccumulator) { resultAccumulator.EnsureExecutorCommand(node.Properties["executor-name"]); }
protected override bool AcceptsTokenImpl(IToken token, IResultAccumulator resultAccumulator) { var result = token is EnumToken <TEnum>; return(result); }
protected override bool AcceptsTokenImpl(IToken token, IResultAccumulator resultAccumulator) { return(token is IntegerToken); }