/// <summary> /// Parser for EachStatement /// </summary> /// <returns>Parsed EachStatement</returns> public EachStatement ParseEachStatement() { EachStatement eachStatement = new EachStatement(); //Skip each token NextToken("each", "each (identifier : expression) Statement", "each"); //Skip ( token NextToken("(", "each (identifier : expression) Statement", '('); //Parse identifier CurrentToken = TokenStream.NextToken(); eachStatement.SetIdentifier(CurrentToken.GetValue().ToString()); //Skip : token NextToken(":", "each (identifier : expression) Statement", ':'); //Parse expression eachStatement.SetExpression(expressionParser.ParseExpression()); //Skip ) token NextToken(")", "each (identifier : expression) Statement", ')'); //Parse statement eachStatement.SetStatement(ParseStatement()); return(eachStatement); }
/// <summary> /// Parser for ExpressionArgument /// </summary> /// <returns>ExpressionArgument</returns> public Argument ParseExpressionArgument() { ExpressionArgument argument = new ExpressionArgument(); //Parse expression argument.SetExpression(expressionParser.ParseExpression()); return(argument); }
public void ParseCatExpressionTest() { //Create parser and parse tokens ExpressionParser expressionParser = new ExpressionParser(Init("expression+'symbol")); Expression expression = expressionParser.ParseExpression(); //Check type of expression Assert.AreEqual(typeof(CatExpression), expression.GetType()); CatExpression parsedCatExpression = (CatExpression)expression; //Check expressions in catexpression Assert.AreEqual(typeof(VarExpression), parsedCatExpression.GetLeftExpression().GetType()); Assert.AreEqual(typeof(SymExpression), parsedCatExpression.GetRightExpression().GetType()); VarExpression left = (VarExpression) parsedCatExpression.GetLeftExpression(); SymExpression right = (SymExpression)parsedCatExpression.GetRightExpression(); Assert.AreEqual("expression", left.GetVariableIdentifier()); Assert.AreEqual("symbol", right.GetSym()); }
/// <summary> /// Parser for Predicate /// </summary> /// <returns>Parsed Predicate</returns> public Predicate ParsePredicate() { Predicate predicate = null; //Empty predicate if (TokenStream.HasNext() && TokenStream.Peek(1).GetValue().ToString() == "!") { //Not Predicate NotPredicate notPredicate = new NotPredicate(); //Skip ! token NextToken("!", "! predicate", '!'); //Parse predicate after ! notPredicate.SetPredicate(ParsePredicate()); predicate = notPredicate; } else { //Other predicates exists out of an expression Expression expression = expressionParser.ParseExpression(); if (TokenStream.HasNext() && TokenStream.Peek(1).GetValue().ToString() == ".") { //IsPredicate predicate = ParseIsPredicate(expression); } else { //ExpressionPredicate predicate = ParseExpressionPredicate(expression); } } //Check for && or || predicates if (TokenStream.HasNext() && TokenStream.Peek(1).GetValue().ToString() == "&" && TokenStream.Peek(2).GetValue().ToString() == "&") { //AndPredicate return(ParseAndPredicate(predicate)); } else if (TokenStream.HasNext() && TokenStream.Peek(1).GetValue().ToString() == "|" && TokenStream.Peek(2).GetValue().ToString() == "|") { //OrPredicate return(ParseOrPredicate(predicate)); } return(predicate); }
/// <summary> /// Parser for Embed /// </summary> /// <returns>Parsed Embed</returns> public Embed ParseEmbed() { Embed embed = null; NodeList markupList = new NodeList(); //Set up MarkupParser markupParser = new MarkupParser(EmbeddingTokenStream); //Parse Markup* //EmbeddingTokenStream.HasNext(2) && !(EmbeddingTokenStream.Peek(2).GetValue().ToString() == ">" while (NextTokenIsMarkup()) { markupList.Add(markupParser.ParseMarkup()); } //Check if an expression is remaining, otherwise embed is MarkupEmbedding if (IsExpressionRemaining()) { //ExpressionEmbedding ExpressionEmbed expressionEmbed = new ExpressionEmbed(); //Add already parsed markups to expressionEmbed expressionEmbed.SetMarkups(markupList); //Set up expressionparser expressionParser = new ExpressionParser(EmbeddingTokenStream); //Parse expression expressionEmbed.SetExpression(expressionParser.ParseExpression()); embed = expressionEmbed; } else { //MarkupEmbedding //Get last item from markupList and add it as Markup if (markupList.Count != 0) { Markup markup = (Markup)markupList.Get(markupList.Count - 1); markupList.Remove(markupList.Count - 1); MarkupEmbed markupEmbed = new MarkupEmbed(); markupEmbed.SetMarkups(markupList); markupEmbed.SetMarkup(markup); embed = markupEmbed; } else { //Markup* Markup must contain at least one markup element throw new UnexpectedToken("Markup expected, but found:", CurrentToken.GetValue().ToString(), CurrentToken.GetLine()); } } /*//Determine if remaining embed is Markup or Expression * if (NextTokenIsMarkup()) * { //MarkupEmbedding * MarkupEmbed markupEmbed = new MarkupEmbed(); * * //Add already parsed markups to markupEmbed * markupEmbed.SetMarkups(markupList); * * //Parse latest markup * markupEmbed.SetMarkup(markupParser.ParseMarkup()); * * embed = markupEmbed; * } * else * { //ExpressionEmbedding * ExpressionEmbed expressionEmbed = new ExpressionEmbed(); * * //Add already parsed markups to expressionEmbed * expressionEmbed.SetMarkups(markupList); * * //Set up expressionparser * expressionParser = new ExpressionParser(EmbeddingTokenStream); * * //Parse expression * expressionEmbed.SetExpression(expressionParser.ParseExpression()); * * embed = expressionEmbed; * } */ return(embed); }
public void ParseIsPredicateTest() { //Create parser PredicateParser predicateParser = new PredicateParser(Init(".string?")); ExpressionParser expressionParser = new ExpressionParser(Init("test")); Expression parsedExpression = expressionParser.ParseExpression(); IsPredicate parsedIsPredicate = predicateParser.ParseIsPredicate(parsedExpression); //Check expression Assert.AreEqual(typeof(VarExpression), parsedIsPredicate.GetExpression().GetType()); Assert.AreEqual("test", parsedIsPredicate.GetExpression().ToString()); //Check type Assert.AreEqual(typeof(StringType), parsedIsPredicate.GetType().GetType()); }
public void ParseFieldExpressionTest() { //Create parser and parse tokens ExpressionParser expressionParser = new ExpressionParser(Init("expressie.identifier")); Expression expression = expressionParser.ParseExpression(); //Test type of expression Assert.AreEqual(typeof(FieldExpression), expression.GetType()); FieldExpression parsedFieldExpression = (FieldExpression)expression; //Test field members Assert.AreEqual("expressie", parsedFieldExpression.GetExpression().ToString()); Assert.AreEqual(typeof(VarExpression), parsedFieldExpression.GetExpression().GetType()); Assert.AreEqual("identifier", parsedFieldExpression.GetIdentifier()); }
public void ParseExpressionTest() { //Create parser and parse tokens ExpressionParser expressionParser = new ExpressionParser(Init("expressie.identifier")); Expression expression = expressionParser.ParseExpression(); //Test type of expression Assert.AreEqual(typeof(FieldExpression), expression.GetType()); }
/// <summary> /// Parser for Embed /// </summary> /// <returns>Parsed Embed</returns> public Embed ParseEmbed() { Embed embed = null; NodeList markupList = new NodeList(); //Set up MarkupParser markupParser = new MarkupParser(EmbeddingTokenStream); //Parse Markup* //EmbeddingTokenStream.HasNext(2) && !(EmbeddingTokenStream.Peek(2).GetValue().ToString() == ">" while(NextTokenIsMarkup()) { markupList.Add(markupParser.ParseMarkup()); } //Check if an expression is remaining, otherwise embed is MarkupEmbedding if (IsExpressionRemaining()) { //ExpressionEmbedding ExpressionEmbed expressionEmbed = new ExpressionEmbed(); //Add already parsed markups to expressionEmbed expressionEmbed.SetMarkups(markupList); //Set up expressionparser expressionParser = new ExpressionParser(EmbeddingTokenStream); //Parse expression expressionEmbed.SetExpression(expressionParser.ParseExpression()); embed = expressionEmbed; } else { //MarkupEmbedding //Get last item from markupList and add it as Markup if (markupList.Count != 0) { Markup markup = (Markup)markupList.Get(markupList.Count - 1); markupList.Remove(markupList.Count - 1); MarkupEmbed markupEmbed = new MarkupEmbed(); markupEmbed.SetMarkups(markupList); markupEmbed.SetMarkup(markup); embed = markupEmbed; } else { //Markup* Markup must contain at least one markup element throw new UnexpectedToken("Markup expected, but found:", CurrentToken.GetValue().ToString(), CurrentToken.GetLine()); } } /*//Determine if remaining embed is Markup or Expression if (NextTokenIsMarkup()) { //MarkupEmbedding MarkupEmbed markupEmbed = new MarkupEmbed(); //Add already parsed markups to markupEmbed markupEmbed.SetMarkups(markupList); //Parse latest markup markupEmbed.SetMarkup(markupParser.ParseMarkup()); embed = markupEmbed; } else { //ExpressionEmbedding ExpressionEmbed expressionEmbed = new ExpressionEmbed(); //Add already parsed markups to expressionEmbed expressionEmbed.SetMarkups(markupList); //Set up expressionparser expressionParser = new ExpressionParser(EmbeddingTokenStream); //Parse expression expressionEmbed.SetExpression(expressionParser.ParseExpression()); embed = expressionEmbed; } */ return embed; }