Ejemplo n.º 1
0
        protected override void ParseInternal(string srcText, RTParsingContext context)
        {
            StringBuilder sb = new StringBuilder();
            int index = SourceStartIndex;
            ParsingState state = ParsingState.Neutral;

            while (index < srcText.Length)
            {
                char currentChar = srcText[index];
                switch (state)
                {
                    case ParsingState.Neutral:
                        if (currentChar == EXPR_MARKER) state = ParsingState.CheckingMarker;
                        else sb.Append(currentChar);
                        break;
                    case ParsingState.CheckingMarker:
                        if (currentChar == EXPR_START)
                        {
                            index--;
                            goto EXIT_LOOP;
                        }
                        else if (currentChar == EXPR_END)
                        {
                            if (context.MarkerDepth == 0)
                            {
                                sb.Append(EXPR_MARKER).Append(EXPR_END);
                                state = ParsingState.Neutral;
                            }
                            else
                            {
                                index--;
                                goto EXIT_LOOP;
                            }
                        }
                        else if (currentChar == EXPR_MARKER)
                        {
                            sb.Append(EXPR_MARKER);
                            state = ParsingState.Neutral;
                        }
                        else
                        {
                            sb.Append(EXPR_MARKER).Append(currentChar);
                            state = ParsingState.Neutral;
                        }
                        break;

                    default:
                        throw new NotSupportedException();
                }
                index++;
            }

            if (state == ParsingState.CheckingMarker)
            {
                sb.Append(EXPR_MARKER);
            }
            EXIT_LOOP:
            context.SourceEndIndex = index - 1;
            _value = sb.ToString();
        }
Ejemplo n.º 2
0
        public void Parse(string srcText, int srcStartIndex, RTParsingContext context)
        {
            Contract.Requires(srcStartIndex >= 0);
            Contract.Requires(srcStartIndex < srcText.Length);
            Contract.Requires(context != null);

            this.SourceStartIndex = srcStartIndex;
            var existingErrors = context.Errors.Count;
            ParseInternal(srcText, context);
            if (context.Errors.Count == existingErrors)
                this.CanExecute = true;
            this.SourceEndIndex = context.SourceEndIndex;
        }
Ejemplo n.º 3
0
        protected override void ParseInternal(string srcText, RTParsingContext context)
        {
            int enteringDepth = context.MarkerDepth;
            int index = this.SourceStartIndex;
            if (enteringDepth > 0)
            {
                context.MarkerDepth++;
                index += 2;
            }

            while (index < srcText.Length)
            {
                RTExpression expr;
                MarkerType markerType = PeekMarker(srcText, index);

                if (markerType == MarkerType.StartMarker)
                {
                    expr = new RTFunctionList();
                }
                else if (markerType == MarkerType.EndMarker)
                {
                    if (context.MarkerDepth > 0)
                    {
                        context.MarkerDepth--;
                        context.SourceEndIndex = index + 1;
                        break;
                    }
                    else
                    {
                        expr = new RTLiteral();
                    }
                }
                else
                {
                    expr = new RTLiteral();
                }

                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.Expression_List_End_Marker_Missing));
            }
        }
Ejemplo n.º 4
0
        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));
            }
        }
Ejemplo n.º 5
0
        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;
        }
Ejemplo n.º 6
0
        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;
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Need to return source end index with parsing context.
 /// Source start index is passed in through parsing context.
 /// </summary>
 /// <param name="srcText"></param>
 /// <param name="context"></param>
 protected abstract void ParseInternal(string srcText, RTParsingContext context);
Ejemplo n.º 8
0
 public void Parse(string srcText, RTParsingContext context)
 {
     Parse(srcText, 0, context);
 }
Ejemplo n.º 9
0
        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
        }