public Parser(Token[] tokens, List<Option> options) { this.tokens = new Tokens(tokens); this.options = options; Parameters = new List<string>(); ParsedOption = new List<Option>(); }
void ParseOption(Token token) { lastOption = GetOptionByToken(token); Token nextToken = tokens.NextToken(); if (nextToken == null) { CheckSaturation(lastOption); lastOption.AddArgumentValue("true"); ParsedOption.Add(lastOption); } else if (nextToken is OptionToken) { CheckSaturation(lastOption); lastOption.AddArgumentValue("true"); ParsedOption.Add(lastOption); ParseOption(nextToken); } else if (nextToken is ArgumentToken) { if (IsFullSaturated(lastOption)) { ParseOptionArguments(nextToken); } else { lastOption.AddArgumentValue("true"); ParsedOption.Add(lastOption); ParseParameters(nextToken); } } else if (nextToken is TreatAsArgumentToken) { CheckSaturation(lastOption); lastOption.AddArgumentValue("true"); ParsedOption.Add(lastOption); ParseParameters(tokens.NextToken()); } }
Option GetOptionByToken(Token token) { return options.Where(opt => opt.Switches.Contains(token.Value)).FirstOrDefault(); }
public Tokens(Token[] tokens) { this.tokenNum = 0; this.tokens = tokens; }
void ParseParameters(Token token) { Parameters.Add(token.Value); // determine next way Token nextToken = tokens.NextToken(); if (nextToken == null) { return; } else if (nextToken is ArgumentToken) { ParseParameters(nextToken); } else if (nextToken is TreatAsArgumentToken) { ParseParameters(tokens.NextToken()); } else if (nextToken is OptionToken) { ParseOption(nextToken); } }
void ParseOptionArguments(Token token) { if (!lastOption.ValueType.Validate(token.Value)) { throw new ArgumentValidityException(); } lastOption.AddArgumentValue(token.Value); Token nextToken = tokens.NextToken(); if (nextToken == null) { CheckSaturation(lastOption); ParsedOption.Add(lastOption); } else if (nextToken is OptionToken) { CheckSaturation(lastOption); ParsedOption.Add(lastOption); ParseOption(nextToken); } else if (nextToken is ArgumentToken) { if (IsFullSaturated(lastOption)) { ParseOptionArguments(nextToken); } else { ParsedOption.Add(lastOption); ParseParameters(nextToken); } } else if (nextToken is TreatAsArgumentToken) { CheckSaturation(lastOption); ParsedOption.Add(lastOption); ParseParameters(tokens.NextToken()); } }