public void TestCommasAndCurlyBraces() { var expression = "{0,1} + {0.5*2, (0.5 + 0.5)}"; var expected = new Token[] { new Token { Type = TokenType.Paren, Text = "{" }, new Token { Type = TokenType.Number, Text = "0" }, new Token { Type = TokenType.Comma, Text = "," }, new Token { Type = TokenType.Number, Text = "1" }, new Token { Type = TokenType.Paren, Text = "}" }, new Token { Type = TokenType.Operator, Text = "+" }, new Token { Type = TokenType.Paren, Text = "{" }, new Token { Type = TokenType.Number, Text = "0.5" }, new Token { Type = TokenType.Operator, Text = "*" }, new Token { Type = TokenType.Number, Text = "2" }, new Token { Type = TokenType.Comma, Text = "," }, new Token { Type = TokenType.Paren, Text = "(" }, new Token { Type = TokenType.Number, Text = "0.5" }, new Token { Type = TokenType.Operator, Text = "+" }, new Token { Type = TokenType.Number, Text = "0.5" }, new Token { Type = TokenType.Paren, Text = ")" }, new Token { Type = TokenType.Paren, Text = "}" }, }; var tokens = Parser.Parse(expression).ToArray(); Assert.AreEqual(expected, tokens); }
public void TestBasicExpression() { var expression = "a + b / c * d"; var expected = new Token[] { new Token { Type = TokenType.Identifier, Text = "a" }, new Token { Type = TokenType.Operator, Text = "+" }, new Token { Type = TokenType.Identifier, Text = "b" }, new Token { Type = TokenType.Operator, Text = "/" }, new Token { Type = TokenType.Identifier, Text = "c" }, new Token { Type = TokenType.Operator, Text = "*" }, new Token { Type = TokenType.Identifier, Text = "d" } }; var tokens = Parser.Parse(expression).ToArray(); Assert.AreEqual(expected, tokens); }
public void TestMultiCharacterOperators() { var expression = "a >= b != c"; var expected = new Token[] { new Token { Type = TokenType.Identifier, Text = "a" }, new Token { Type = TokenType.Operator, Text = ">=" }, new Token { Type = TokenType.Identifier, Text = "b" }, new Token { Type = TokenType.Operator, Text = "!=" }, new Token { Type = TokenType.Identifier, Text = "c" } }; var tokens = Parser.Parse(expression).ToArray(); Assert.AreEqual(expected, tokens); }
public void TestComplexExpression() { var expression = "(obj.field + (4 * 2)) > 0.5"; var expected = new Token[] { new Token { Type = TokenType.Paren, Text = "(" }, new Token { Type = TokenType.Identifier, Text = "obj" }, new Token { Type = TokenType.Operator, Text = "." }, new Token { Type = TokenType.Identifier, Text = "field" }, new Token { Type = TokenType.Operator, Text = "+" }, new Token { Type = TokenType.Paren, Text = "(" }, new Token { Type = TokenType.Number, Text = "4" }, new Token { Type = TokenType.Operator, Text = "*" }, new Token { Type = TokenType.Number, Text = "2" }, new Token { Type = TokenType.Paren, Text = ")" }, new Token { Type = TokenType.Paren, Text = ")" }, new Token { Type = TokenType.Operator, Text = ">" }, new Token { Type = TokenType.Number, Text = "0.5" } }; var tokens = Parser.Parse(expression).ToArray(); Assert.AreEqual(expected, tokens); }
public void TestNumbers() { var expression = "1 + 2.503 + -37 - -15.6"; var expected = new Token[] { new Token { Type = TokenType.Number, Text = "1" }, new Token { Type = TokenType.Operator, Text = "+" }, new Token { Type = TokenType.Number, Text = "2.503" }, new Token { Type = TokenType.Operator, Text = "+" }, new Token { Type = TokenType.Number, Text = "-37" }, new Token { Type = TokenType.Operator, Text = "-" }, new Token { Type = TokenType.Number, Text = "-15.6" } }; var tokens = Parser.Parse(expression).ToArray(); Assert.AreEqual(expected, tokens); }
public IEnumerable<Token> Parse (string expression) { var current = new Token(); var matches = _Regex.Matches(expression); foreach (var match in matches.Cast<Match>()) { current.Text = match.ToString(); if (match.Groups[_Group_Number].Success) current.Type = TokenType.Number; else if (match.Groups[_Group_Identifier].Success) current.Type = TokenType.Identifier; else if (match.Groups[_Group_Operator].Success) current.Type = TokenType.Operator; else if (match.Groups[_Group_Paren].Success) current.Type = TokenType.Paren; else if (match.Groups[_Group_Comma].Success) current.Type = TokenType.Comma; yield return current; } }