/// <summary> /// Abstract factory /// </summary> public static IStatement CreateStatement(ParseContext context, IAstNode parent) { RToken currentToken = context.Tokens.CurrentToken; string keyword = context.TextProvider.GetText(currentToken); IStatement statement = null; switch (keyword) { case "if": statement = new If(); break; case "for": statement = new For(); break; case "while": statement = new KeywordExpressionScopeStatement(); break; case "repeat": statement = new KeywordScopeStatement(allowsSimpleScope: false); break; case "break": case "next": statement = new KeywordStatement(); break; case "function": statement = new FunctionStatement(); break; default: context.AddError(new ParseError(ParseErrorType.UnexpectedToken, ErrorLocation.Token, currentToken)); break; } return(statement); }
/// <summary> /// Abstract factory creating statements depending on current /// token and the following token sequence /// </summary> /// <returns></returns> public static IStatement Create(ParseContext context, IAstNode parent, string terminatingKeyword) { TokenStream <RToken> tokens = context.Tokens; RToken currentToken = tokens.CurrentToken; IStatement statement = null; switch (currentToken.TokenType) { case RTokenType.Keyword: // If statement starts with a keyword, it is not an assignment // hence we should always try keyword based statements first. // Some of the statements may be R-values like typeof() but // in case of the statement appearing on its own return value // will be simply ignored. IDE may choose to show a warning. if (currentToken.SubType == RTokenSubType.BuiltinFunction && tokens.NextToken.TokenType != RTokenType.OpenBrace) { // 'return <- x + y' is allowed statement = new ExpressionStatement(terminatingKeyword); } else { statement = KeywordStatement.CreateStatement(context, parent); } break; case RTokenType.Semicolon: statement = new EmptyStatement(); break; default: // Possible L-value in a left-hand assignment, // a function call or R-value in a right hand assignment. statement = new ExpressionStatement(terminatingKeyword); break; } return(statement); }
/// <summary> /// Abstract factory /// </summary> public static IStatement CreateStatement(ParseContext context, IAstNode parent) { RToken currentToken = context.Tokens.CurrentToken; string keyword = context.TextProvider.GetText(currentToken); IStatement statement = null; switch (keyword) { case "if": statement = new If(); break; case "for": statement = new For(); break; case "while": statement = new KeywordExpressionScopeStatement(); break; case "repeat": statement = new KeywordScopeStatement(allowsSimpleScope: false); break; case "break": case "next": statement = new KeywordStatement(); break; case "function": statement = new FunctionStatement(); break; default: context.AddError(new ParseError(ParseErrorType.UnexpectedToken, ErrorLocation.Token, currentToken)); break; } return statement; }