private Expression ParseExpression() { bool negative = false; Expression expression = null; if (CurrentToken.Text == "-") { negative = true; currentPosition++; } if (CurrentToken.TokenType == SelectorTokenType.Dimension || CurrentToken.Text == "n" || CurrentToken.Text == "-n") { string dimension = CurrentToken.Text; int dim; string negativeSecond = null; Match m = Regex.Match(CurrentToken.Text, @"(\d+n)-(\d+)"); if (m.Success) { dimension = m.Groups[1].Value; negativeSecond = m.Groups[2].Value; } if (int.TryParse(dimension.Substring(0, dimension.Length - 1), out dim) || CurrentToken.Text == "n" || CurrentToken.Text == "-n") { if (CurrentToken.Text == "n") { dim = 1; } else if (CurrentToken.Text == "-n") { dim = -1; } dim = negative ? dim * -1 : dim; currentPosition++; if (CurrentToken.TokenType == SelectorTokenType.Plus) { currentPosition++; if (CurrentToken.TokenType == SelectorTokenType.Number) { //xn+b int num; if (int.TryParse(CurrentToken.Text, out num)) { expression = new NumericExpression(dim, num); } else { //parse error } } } else if (negativeSecond != null) { //xn-b int num; if (int.TryParse(negativeSecond, out num)) { expression = new NumericExpression(dim, -num); currentPosition--; } else { //parse error } } else if (CurrentToken.Text == ")") { expression = new NumericExpression(dim, 0); currentPosition--; } } else { //parse error } } else if (CurrentToken.TokenType == SelectorTokenType.Number) { //b int num; if (int.TryParse(CurrentToken.Text, out num)) { expression = new NumericExpression(0, num * (negative ? -1 : 1)); } else { //parse error } } else if (CurrentToken.TokenType == SelectorTokenType.Ident) { //odd or even if (CurrentToken.Text == "odd") { expression = new OddExpression(); } else if (CurrentToken.Text == "even") { expression = new OddExpression(); } else { //parse error } } else if (CurrentToken.TokenType == SelectorTokenType.String) { string withoutQuotes = CurrentToken.Text.Substring(1, CurrentToken.Text.Length - 2); if (CurrentToken.Text == "odd") { expression = new OddExpression(); } else if (CurrentToken.Text == "even") { expression = new OddExpression(); } else { //parse error } } return expression; }
private Expression ParseExpression() { bool negative = false; Expression expression = null; if (CurrentToken.Text == "-") { negative = true; currentPosition++; } if (CurrentToken.TokenType == SelectorTokenType.Dimension || CurrentToken.Text == "n" || CurrentToken.Text == "-n") { string dimension = CurrentToken.Text; int dim; string negativeSecond = null; Match m = Regex.Match(CurrentToken.Text, @"(\d+n)-(\d+)"); if (m.Success) { dimension = m.Groups[1].Value; negativeSecond = m.Groups[2].Value; } if (int.TryParse(dimension.Substring(0, dimension.Length - 1), out dim) || CurrentToken.Text == "n" || CurrentToken.Text == "-n") { if (CurrentToken.Text == "n") { dim = 1; } else if (CurrentToken.Text == "-n") { dim = -1; } dim = negative ? dim * -1 : dim; currentPosition++; if (CurrentToken.TokenType == SelectorTokenType.Plus) { currentPosition++; if (CurrentToken.TokenType == SelectorTokenType.Number) { //xn+b int num; if (int.TryParse(CurrentToken.Text, out num)) { expression = new NumericExpression(dim, num); } else { //parse error } } } else if (negativeSecond != null) { //xn-b int num; if (int.TryParse(negativeSecond, out num)) { expression = new NumericExpression(dim, -num); currentPosition--; } else { //parse error } } else if (CurrentToken.Text == ")") { expression = new NumericExpression(dim, 0); currentPosition--; } } else { //parse error } } else if (CurrentToken.TokenType == SelectorTokenType.Number) { //b int num; if (int.TryParse(CurrentToken.Text, out num)) { expression = new NumericExpression(0, num * (negative ? -1 : 1)); } else { //parse error } } else if (CurrentToken.TokenType == SelectorTokenType.Ident) { //odd or even if (CurrentToken.Text == "odd") { expression = new OddExpression(); } else if (CurrentToken.Text == "even") { expression = new OddExpression(); } else { //parse error } } else if (CurrentToken.TokenType == SelectorTokenType.String) { string withoutQuotes = CurrentToken.Text.Substring(1, CurrentToken.Text.Length - 2); if (CurrentToken.Text == "odd") { expression = new OddExpression(); } else if (CurrentToken.Text == "even") { expression = new OddExpression(); } else { //parse error } } return(expression); }