protected override void ParseInternal(string srcText, RTParsingContext context) { Debug.Assert(PeekMarker(srcText, SourceStartIndex) == MarkerType.StartMarker); int enteringDepth = context.MarkerDepth; context.MarkerDepth++; int index = this.SourceStartIndex + 2; while (index < srcText.Length) { RTExpression expr; if (Char.IsWhiteSpace(srcText[index])) { index++; continue; } MarkerType markerType = PeekMarker(srcText, index); if (markerType == MarkerType.StartMarker) { expr = new RTExpressionList(); } else if (markerType == MarkerType.None) { expr = new RTFunction(); } else //== EndMarker { context.MarkerDepth--; context.SourceEndIndex = index + 1; break; } expr.Parse(srcText, index, context); _items.Add(expr); index = context.SourceEndIndex + 1; } if (enteringDepth != context.MarkerDepth) { context.MarkerDepth = enteringDepth; //restore from error context.SourceEndIndex = index - 1; context.Errors.Add(new RTParsingError(this.SourceStartIndex, context.SourceEndIndex, RTErrorCode.EndMarkerMissing, ErrorMessages.Function_List_End_Marker_Missing)); } }
private void Init() { if (_options != null) { RTParsingContext context = new RTParsingContext(_factory); var exprList = new RTExpressionList(); exprList.Parse(_templateExpr, 0, context); //parse should not throw an exception _interpreter = exprList; _options = null; _templateExpr = null; if (!_interpreter.CanExecute) { _parsingException = new RTParsingException(context.Errors, ErrorMessages.Template_Parsing_Error); } } if (_parsingException != null) throw _parsingException; }
private void Init() { if (_mergeOptions != null) { RTParsingContext context = new RTParsingContext(_factory); var exprList = new RTExpressionList(); exprList.Parse(_mergeExpr, 0, context); _interpreter = exprList; _mergeOptions = null; //not needed anymore _mergeExpr = null; if (!_interpreter.CanExecute) { _parsingException = new RTParsingException(context.Errors, ErrorMessages.Post_Parsing_Error); } } if (_parsingException != null) throw _parsingException; }
protected override void ParseInternal(string srcText, RTParsingContext context) { bool flagExit = false; ParsingState state = ParsingState.Start; int index = SourceStartIndex; while (!flagExit) { int tokenStartIndex = index; TokenType token = ReadToken(srcText, ref index); if (token == TokenType.NONE) break; string tokenValue = srcText.Substring(tokenStartIndex, index - tokenStartIndex); switch (state) { case ParsingState.Start: if (token == TokenType.WHITESPACE) { //ignore } else if (token == TokenType.OPEN_BRACKET) { state = ParsingState.FuncName; } else { context.Errors.Add(new RTParsingError(tokenStartIndex, index - 1, RTErrorCode.OpenBracketMissing, ErrorMessages.Function_Open_Bracket_Missing)); flagExit = true; } break; case ParsingState.Invalid: if (token == TokenType.CLOSE_BRACKET) //exit at nearest closing bracket { flagExit = true; } break; case ParsingState.FuncName: if (token == TokenType.SYMBOL) { this.Name = tokenValue; state = ParsingState.FuncArg; } else if (token == TokenType.IDENTIFIER) { bool isvalid = CheckIdentifier(tokenValue); if (isvalid) { this.Name = tokenValue; state = ParsingState.FuncArg; } else { state = ParsingState.Invalid; context.Errors.Add(new RTParsingError(tokenStartIndex, index - 1, RTErrorCode.InvalidIdentifier, string.Format(ErrorMessages.Function_Invalid_Arg_Identifier, tokenValue))); } } else { state = ParsingState.Invalid; context.Errors.Add(new RTParsingError(tokenStartIndex, index - 1, RTErrorCode.InvalidFunctionName, string.Format(ErrorMessages.Function_Name_Missing, token))); if (token == TokenType.CLOSE_BRACKET) flagExit = false; } break; case ParsingState.FuncArgNegative: if (token == TokenType.WHITESPACE) { this.Args.Add(new RTVariable("-")); } else if (token == TokenType.NUMBER) { double temp; if (double.TryParse(tokenValue, out temp)) { this.Args.Add(-temp); } else { context.Errors.Add(new RTParsingError(tokenStartIndex, index - 1, RTErrorCode.InvalidNumber, string.Format(ErrorMessages.Function_Invalid_Arg_Number, tokenValue))); } } else { context.Errors.Add(new RTParsingError(tokenStartIndex, index - 1, RTErrorCode.InvalidToken, string.Format(ErrorMessages.Function_Invalid_Arg, tokenValue))); if (token == TokenType.CLOSE_BRACKET) flagExit = false; } state = ParsingState.FuncArg; break; case ParsingState.FuncArg: switch (token) { case TokenType.WHITESPACE://ignore break; case TokenType.CLOSE_BRACKET: flagExit = true; break; case TokenType.OPEN_BRACKET: RTFunction func = new RTFunction(); func.Parse(srcText, tokenStartIndex, context); index = context.SourceEndIndex + 1; this.Args.Add(func); break; case TokenType.TRUE: this.Args.Add(true); break; case TokenType.FALSE: this.Args.Add(false); break; case TokenType.NULL: this.Args.Add(null); break; case TokenType.INVALID: context.Errors.Add(new RTParsingError(tokenStartIndex, index - 1, RTErrorCode.InvalidToken, string.Format(ErrorMessages.Function_Invalid_Arg, tokenValue))); break; case TokenType.NUMBER: double temp; if (double.TryParse(tokenValue, out temp)) { this.Args.Add(temp); } else { context.Errors.Add(new RTParsingError(tokenStartIndex, index - 1, RTErrorCode.InvalidNumber, string.Format(ErrorMessages.Function_Invalid_Arg_Number, tokenValue))); } break; case TokenType.SYMBOL: if (tokenValue == "-") { state = ParsingState.FuncArgNegative; } else { this.Args.Add(new RTVariable(tokenValue)); } break; case TokenType.IDENTIFIER: bool isValid = CheckIdentifier(tokenValue); if (isValid) { this.Args.Add(new RTVariable(tokenValue)); } else { context.Errors.Add(new RTParsingError(tokenStartIndex, index - 1, RTErrorCode.InvalidIdentifier, string.Format(ErrorMessages.Function_Invalid_Arg_Identifier, tokenValue))); } break; case TokenType.EXPR_START: RTExpression expr = new RTExpressionList(); expr.Parse(srcText, tokenStartIndex, context); index = context.SourceEndIndex + 1; Args.Add(expr); break; default: throw new NotSupportedException("Unknown token type."); } break; default: throw new NotSupportedException("Unknow Function Parsing State."); } } if (!flagExit) { context.Errors.Add(new RTParsingError(this.SourceStartIndex, index - 1, RTErrorCode.EndBraketMissing, ErrorMessages.Function_End_Bracket_Missing)); } context.SourceEndIndex = index - 1; //resolve metadata this.Metadata = this.Name == null ? null : context.MetadataFactory.GetMetadata(this.Name); if (this.Metadata == null) { this.Metadata = context.MetadataFactory.GetMetadata(RTSysSymbols.APPLY); //the apply symbol this.Args.Insert(0, new RTVariable(this.Name)); this.Name = RTSysSymbols.APPLY; } this.Metadata.NormalizeArgs(this.Args); #if DEBUG _src = srcText.Substring(this.SourceStartIndex, context.SourceEndIndex - this.SourceStartIndex + 1); #endif }