示例#1
0
 public HlTokenReadEvent(HlTokenType expected, HlTokenType got) : base(
         $"Expected Token '{expected}' but got '{got}'",
         ErrorEventKeys.s_HlInvalidToken,
         false
         )
 {
 }
示例#2
0
 protected InvalidHlMemberModifiersEvent(HlTokenType a, HlTokenType b) : base(
         $"Token '{a}' can not be used together with '{b}'",
         ErrorEventKeys.s_HlInvalidMemberModifiers,
         false
         )
 {
 }
示例#3
0
        public static int ReadList(
            List <IHlToken> tokens,
            int start,
            HlTokenType item,
            HlTokenType separator,
            out IHlToken[] items)
        {
            List <IHlToken> list = new List <IHlToken>();
            int             i    = start;

            while (i < tokens.Count && tokens[i].Type == item)
            {
                list.Add(tokens[i]);

                i++;

                if (i >= tokens.Count || tokens[i].Type != separator)
                {
                    items = list.ToArray();

                    return(i - start);
                }

                i++;
            }

            EventManager <ErrorEvent> .SendEvent(
                new HlTokenReadEvent( tokens, item, tokens[i].Type, start )
                );

            items = list.ToArray();

            return(i - start);
        }
示例#4
0
 /// <summary>
 ///     Public Constructor
 /// </summary>
 /// <param name="context">XL Context</param>
 /// <param name="left">Left Side</param>
 /// <param name="operationType">Operation FunctionType</param>
 /// <param name="right">Right Side</param>
 public HlBinaryOp(
     HlExpression left,
     HlTokenType operationType,
     HlExpression right) : base(left.SourceIndex)
 {
     Left          = left;
     OperationType = operationType;
     Right         = right;
 }
示例#5
0
        /// <summary>
        ///     Reads Exactly one token of a specified type
        /// </summary>
        /// <param name="tokens">Token Stream</param>
        /// <param name="start">Start Index</param>
        /// <param name="type">Accepted FunctionType</param>
        /// <returns></returns>
        public static IHlToken ReadOne(List <IHlToken> tokens, int start, HlTokenType type)
        {
            if (!ReadOneOrNone(tokens, start, type, out IHlToken ret))
            {
                EventManager <ErrorEvent> .SendEvent(new HlTokenReadEvent( tokens, type, ret.Type, start ));
            }

            return(ret);
        }
示例#6
0
        /// <summary>
        ///     Creates an implemented expression
        /// </summary>
        /// <param name="parser">XLExpressionParser</param>
        /// <param name="currentNode">Current Expression Node</param>
        /// <returns></returns>
        public override HlExpression Create(HlExpressionParser parser, HlExpression currentNode)
        {
            HlTokenType type = parser.CurrentToken.Type;

            parser.Eat(type);

            HlExpression token = new HlUnaryOp(parser.ParseExpr(PrecedenceLevel), type);

            return(token);
        }
示例#7
0
 /// <summary>
 ///     Public Constructor
 /// </summary>
 /// <param name="tokenSequence">Token Sequence</param>
 /// <param name="expected">Expected Token</param>
 /// <param name="unmatched">Unmatched Token</param>
 /// <param name="start">Start index in source</param>
 public HlTokenReadEvent(
     IEnumerable <IHlToken> tokenSequence,
     HlTokenType expected,
     HlTokenType unmatched,
     int start) : this(
         tokenSequence,
         new[] { expected },
         unmatched,
         start
         )
 {
 }
示例#8
0
        /// <summary>
        ///     Creates an implemented expression
        /// </summary>
        /// <param name="parser">XLExpressionParser</param>
        /// <param name="currentNode">Current Expression Node</param>
        /// <returns></returns>
        public override HlExpression Create(HlExpressionParser parser, HlExpression currentNode)
        {
            HlTokenType tt = parser.CurrentToken.Type == HlTokenType.OpPlus
                                 ? HlTokenType.OpUnaryPostfixIncrement
                                 : HlTokenType.OpUnaryPostfixDecrement;

            parser.Eat(parser.CurrentToken.Type);
            parser.Eat(parser.CurrentToken.Type);

            HlExpression token =
                new HlUnaryOp(currentNode, tt);

            return(token);
        }
示例#9
0
 /// <summary>
 ///     Public Constructor
 /// </summary>
 /// <param name="tokenSequence">Token Sequence</param>
 /// <param name="expected">Expected Tokens</param>
 /// <param name="unmatched">Unmatched Token</param>
 /// <param name="start">Start index in source</param>
 public HlTokenReadEvent(
     IEnumerable <IHlToken> tokenSequence,
     HlTokenType[] expected,
     HlTokenType unmatched,
     int start) :
     base(
         $"Expected '{GetExpectedTokenString( expected )}' but got '{unmatched} at index {start}'",
         ErrorEventKeys.s_HlInvalidToken,
         false
         )
 {
     m_Sequence  = tokenSequence;
     m_Expected  = expected;
     m_Unmatched = unmatched;
 }
示例#10
0
 /// <summary>
 ///     Consumes the Specified Token
 ///     Throws an Error if a different token was found
 /// </summary>
 /// <param name="type">Expected Token FunctionType</param>
 public void Eat(HlTokenType type)
 {
     if (CurrentToken.Type == type)
     {
         CurrentToken = Reader.GetNext();
     }
     else
     {
         EventManager <ErrorEvent> .SendEvent(
             new HlTokenReadEvent(
                 Reader.Tokens,
                 type,
                 CurrentToken.Type,
                 CurrentToken.SourceIndex
                 )
             );
     }
 }
示例#11
0
        /// <summary>
        ///     reads one or none token of the accepted type
        /// </summary>
        /// <param name="tokens">Token Stream</param>
        /// <param name="start">Start index</param>
        /// <param name="type">Token FunctionType</param>
        /// <param name="result">Read Token</param>
        /// <returns>True if token was read.</returns>
        public static bool ReadOneOrNone(List <IHlToken> tokens, int start, HlTokenType type, out IHlToken result)
        {
            if (start >= 0 && tokens.Count > start)
            {
                result = tokens[start];

                if (tokens[start].Type == type)
                {
                    return(true);
                }

                return(false);
            }

            result = new EofToken();

            return(false);
        }
示例#12
0
        /// <summary>
        ///     Creates an implemented expression
        /// </summary>
        /// <param name="parser">XLExpressionParser</param>
        /// <param name="currentNode">Current Expression Node</param>
        /// <returns></returns>
        public override HlExpression Create(HlExpressionParser parser, HlExpression currentNode)
        {
            HlTokenType type = parser.CurrentToken.Type;

            parser.Eat(parser.CurrentToken.Type);
            HlExpression node = null;

            if (type == HlTokenType.OpLessThan)
            {
                if (parser.CurrentToken.Type == HlTokenType.OpEquality)
                {
                    parser.Eat(HlTokenType.OpEquality);

                    node = new HlBinaryOp(
                        currentNode,
                        HlTokenType.OpLessOrEqual,
                        parser.ParseExpr(PrecedenceLevel)
                        );
                }
                else
                {
                    node = new HlBinaryOp(currentNode, type, parser.ParseExpr(PrecedenceLevel));
                }
            }
            else if (type == HlTokenType.OpGreaterThan)
            {
                if (parser.CurrentToken.Type == HlTokenType.OpEquality)
                {
                    parser.Eat(HlTokenType.OpEquality);

                    node = new HlBinaryOp(
                        currentNode,
                        HlTokenType.OpGreaterOrEqual,
                        parser.ParseExpr(PrecedenceLevel)
                        );
                }
                else
                {
                    node = new HlBinaryOp(currentNode, type, parser.ParseExpr(PrecedenceLevel));
                }
            }

            return(node);
        }
示例#13
0
        /// <summary>
        ///     Creates an implemented expression
        /// </summary>
        /// <param name="parser">XLExpressionParser</param>
        /// <param name="currentNode">Current Expression Node</param>
        /// <returns></returns>
        public override HlExpression Create(HlExpressionParser parser, HlExpression currentNode)
        {
            HlTokenType type = parser.CurrentToken.Type;

            parser.Eat(type);
            parser.Eat(type);

            if (type == HlTokenType.OpLessThan)
            {
                type = HlTokenType.OpShiftLeft;
            }
            else if (type == HlTokenType.OpGreaterThan)
            {
                type = HlTokenType.OpShiftRight;
            }

            return(new HlBinaryOp(
                       currentNode,
                       type,
                       parser.ParseExpr(PrecedenceLevel)
                       ));
        }
示例#14
0
        /// <summary>
        ///     Creates a Value based on the Current State of the Expression Parser
        /// </summary>
        /// <param name="parser">The Parser</param>
        /// <returns>Parsed Expression</returns>
        public HlExpression CreateValue(HlExpressionParser parser, uint maxPrecedence)
        {
            if (parser.CurrentToken.Type == HlTokenType.OpBang ||
                parser.CurrentToken.Type == HlTokenType.OpTilde ||
                parser.CurrentToken.Type == HlTokenType.OpPlus ||
                parser.CurrentToken.Type == HlTokenType.OpMinus)
            {
                if (parser.Reader.PeekNext().Type == HlTokenType.OpPlus)
                {
                    parser.Eat(HlTokenType.OpPlus);
                    parser.Eat(HlTokenType.OpPlus);
                    HlTokenType  t     = HlTokenType.OpUnaryPrefixIncrement;
                    HlExpression token = new HlUnaryOp(parser.ParseExpr(), t);

                    return(token);
                }
                else if (parser.Reader.PeekNext().Type == HlTokenType.OpMinus)
                {
                    parser.Eat(HlTokenType.OpMinus);
                    parser.Eat(HlTokenType.OpMinus);
                    HlTokenType  t     = HlTokenType.OpUnaryPrefixDecrement;
                    HlExpression token = new HlUnaryOp(parser.ParseExpr(), t);

                    return(token);
                }
                else
                {
                    HlTokenType t = parser.CurrentToken.Type;
                    parser.Eat(t);
                    HlExpression token = new HlUnaryOp(parser.ParseExpr(), t);

                    return(token);
                }
            }

            if (parser.CurrentToken.Type == HlTokenType.OpAnd)
            {
                HlTokenType t = parser.CurrentToken.Type;
                parser.Eat(t);
                HlExpression token = new HlUnaryOp(parser.ParseExpr(2), HlTokenType.OpReference);

                return(token);
            }

            if (parser.CurrentToken.Type == HlTokenType.OpAsterisk)
            {
                HlTokenType t = parser.CurrentToken.Type;
                parser.Eat(t);
                HlExpression token = new HlUnaryOp(parser.ParseExpr(2), HlTokenType.OpDeReference);

                return(token);
            }

            if (parser.CurrentToken.Type == HlTokenType.OpNew && maxPrecedence >= 3)
            {
                parser.Eat(parser.CurrentToken.Type);
                HlExpression token = new HlUnaryOp(parser.ParseExpr(2), HlTokenType.OpNew);

                return(token);
            }

            if (parser.CurrentToken.Type == HlTokenType.OpReturn)
            {
                IHlToken rt = parser.CurrentToken;
                parser.Eat(HlTokenType.OpReturn);

                if (parser.CurrentToken.Type == HlTokenType.OpSemicolon)
                {
                    return(new HlReturnOp(null, rt.SourceIndex));
                }

                return(new HlReturnOp(parser.ParseExpr(), rt.SourceIndex));
            }

            if (parser.CurrentToken.Type == HlTokenType.OpContinue)
            {
                IHlToken ct = parser.CurrentToken;
                parser.Eat(HlTokenType.OpContinue);

                return(new HlContinueOp(ct.SourceIndex));
            }

            if (parser.CurrentToken.Type == HlTokenType.OpBreak)
            {
                IHlToken bt = parser.CurrentToken;
                parser.Eat(HlTokenType.OpBreak);

                return(new HlBreakOp(bt.SourceIndex));
            }

            if (parser.CurrentToken.Type == HlTokenType.OpIf)
            {
                return(HlSpecialOps.ReadIf(parser));
            }

            if (parser.CurrentToken.Type == HlTokenType.OpFor)
            {
                return(HlSpecialOps.ReadFor(parser));
            }

            if (parser.CurrentToken.Type == HlTokenType.OpWhile)
            {
                return(HlSpecialOps.ReadWhile(parser));
            }

            if (parser.CurrentToken.Type == HlTokenType.OpBracketOpen)
            {
                parser.Eat(HlTokenType.OpBracketOpen);
                HlExpression token = parser.ParseExpr();
                parser.Eat(HlTokenType.OpBracketClose);

                return(token);
            }

            if (parser.CurrentToken.Type == HlTokenType.OpThis ||
                parser.CurrentToken.Type == HlTokenType.OpBase)
            {
                HlExpression token = new HlVarOperand(
                    parser.CurrentToken,
                    parser.CurrentToken.SourceIndex
                    );

                parser.Eat(parser.CurrentToken.Type);

                return(token);
            }

            if (parser.CurrentToken.Type == HlTokenType.OpWord || parser.CurrentToken.Type == HlTokenType.OpNew)
            {
                IHlToken item = parser.CurrentToken;
                parser.Eat(parser.CurrentToken.Type);

                HlExpression token = new HlVarOperand(item, item.SourceIndex);

                return(token);
            }

            if (parser.CurrentToken.Type == HlTokenType.OpVariableDefinition)
            {
                VariableDefinitionToken vd = ( VariableDefinitionToken )parser.CurrentToken;
                HlVarDefOperand         token;

                if (vd.InitializerExpression != null && vd.InitializerExpression.Length != 0)
                {
                    HlExpressionParser p =
                        HlExpressionParser.Create(new HlExpressionReader(vd.InitializerExpression.ToList()));

                    token =
                        new HlVarDefOperand(vd, p.Parse());
                }
                else
                {
                    token =
                        new HlVarDefOperand(vd, new HlExpression[0]);
                }

                parser.Eat(HlTokenType.OpVariableDefinition);

                return(token);
            }

            if (parser.CurrentToken.Type == HlTokenType.OpFunctionDefinition)
            {
                FunctionDefinitionToken fToken = ( FunctionDefinitionToken )parser.CurrentToken;
                parser.Eat(HlTokenType.OpFunctionDefinition);

                HlExpression[] expressionBlock = null;

                if (fToken.Mods.All(x => x.Type != HlTokenType.OpAbstractMod))
                {
                    expressionBlock = HlExpressionParser.Create(new HlExpressionReader(fToken.Block.ToList())).
                                      Parse();
                }

                HlExpression token =
                    new HlFuncDefOperand(
                        fToken,
                        expressionBlock
                        );

                return(token);
            }

            if (parser.CurrentToken.Type == HlTokenType.OpNumber ||
                parser.CurrentToken.Type == HlTokenType.OpDecimalNumber ||
                parser.CurrentToken.Type == HlTokenType.OpStringLiteral ||
                parser.CurrentToken.Type == HlTokenType.OpCharLiteral)
            {
                HlExpression token = new HlValueOperand(parser.CurrentToken);
                parser.Eat(parser.CurrentToken.Type);

                return(token);
            }

            EventManager <ErrorEvent> .SendEvent(new HlTokenReadEvent( HlTokenType.Any, parser.CurrentToken.Type ));

            return(null);
        }
示例#15
0
 /// <summary>
 ///     Public Constructor
 /// </summary>
 /// <param name="type">Token FunctionType</param>
 /// <param name="value">Token Value</param>
 /// <param name="startIndex">Start index in the source stream</param>
 public HlTextToken(HlTokenType type, string value, int startIndex)
 {
     Type        = type;
     Value       = value;
     SourceIndex = startIndex;
 }
示例#16
0
        /// <summary>
        ///     Reads none or many of the specified token
        /// </summary>
        /// <param name="tokens">Token Stream</param>
        /// <param name="start">Start Index</param>
        /// <param name="step">Step per read token</param>
        /// <param name="type">Accepted FunctionType</param>
        /// <returns>Read Tokens</returns>
        public static IHlToken[] ReadNoneOrMany(List <IHlToken> tokens, int start, int step, HlTokenType type)
        {
            List <IHlToken> ret          = new List <IHlToken>();
            int             currentStart = start;

            while (ReadOneOrNone(tokens, currentStart, type, out IHlToken current))
            {
                currentStart += step;
                ret.Add(current);
            }

            return(ret.ToArray());
        }
示例#17
0
        /// <summary>
        ///     Reads one or many tokens of the specified type
        /// </summary>
        /// <param name="tokens">Token Stream</param>
        /// <param name="start">Start index</param>
        /// <param name="step">Step per read token</param>
        /// <param name="type">Accepted Token</param>
        /// <returns>Read Tokens</returns>
        public static IHlToken[] ReadOneOrMany(List <IHlToken> tokens, int start, int step, HlTokenType type)
        {
            List <IHlToken> ret = new List <IHlToken> {
                ReadOne(tokens, start, type)
            };

            ret.AddRange(ReadNoneOrMany(tokens, start + step, step, type));

            return(ret.ToArray());
        }
示例#18
0
 /// <summary>
 ///     Reads until end of stream or the specified type has been read.
 /// </summary>
 /// <param name="tokens">Token Stream</param>
 /// <param name="start">Start index</param>
 /// <param name="step">Step per read Token</param>
 /// <param name="type">End token type</param>
 /// <returns>Read Tokens</returns>
 public static IHlToken[] ReadUntil(List <IHlToken> tokens, int start, int step, HlTokenType type)
 {
     return(ReadUntilAny(tokens, start, step, new[] { type }));
 }
示例#19
0
 /// <summary>
 ///     Protected Constructor
 /// </summary>
 /// <param name="type">Token FunctionType</param>
 /// <param name="subtokens">Child Tokens</param>
 /// <param name="start">Start index in the source</param>
 protected CombinedToken(HlTokenType type, IHlToken[] subtokens, int start)
 {
     SubTokens   = subtokens;
     SourceIndex = start;
     Type        = type;
 }
示例#20
0
 /// <summary>
 ///     Public Constructor
 /// </summary>
 /// <param name="context">XL Context</param>
 /// <param name="left">Left Side</param>
 /// <param name="operationType">Operation FunctionType</param>
 public HlUnaryOp(HlExpression left, HlTokenType operationType) : base(left.SourceIndex)
 {
     Left          = left;
     OperationType = operationType;
 }