Пример #1
0
        /// <summary>
        /// Initialization logic, called on the first instantiation of a Parser object.
        /// </summary>
        /// <param name="force">Force re-initialization</param>
        public static void Init(bool force = false)
        {
            if (hasRegistered && !force)
            {
                return;
            }

            Logger.Log(LogLevel.Info, Logger.REGISTRY, "Starting parselet registry.");
            PrefixParselets = new Dictionary <TokenType, IPrefixParselet>();

            Logger.Log(LogLevel.Debug, Logger.REGISTRY, "Registering prefix parselets.");
            PrefixParselets.Add(TokenTypes.Identifier, new NameParselet());
            PrefixParselets.Add(TokenTypes.Number, new NumberParselet());
            PrefixParselets.Add(TokenTypes.String, new StringParselet());
            PrefixParselets.Add(TokenTypes.Boolean, new BooleanParselet());

            PrefixParselets.Add(TokenTypes.ParenthesisIn, new ParenthesisParselet());

            PrefixParselets.Add(TokenTypes.BraceIn, new ListLiteralParselet());

            foreach (TokenType tc in UnaryPrefixRegistry.GetTokens())
            {
                RegisterPrefixOperator(tc);
            }

            if (PrefixLoading != null)
            {
                PrefixLoading(null, new PrefixLoadingEventArgs(PrefixParselets));
            }

            InfixParselets = new Dictionary <TokenType, IInfixParselet>();

            Logger.Log(LogLevel.Debug, Logger.REGISTRY, "Registering infix and postfix parselets.");
            InfixParselets.Add(TokenTypes.ParenthesisIn, new FunctionCallParselet());
            InfixParselets.Add(TokenTypes.BracketIn, new ListOrdinalParselet());
            InfixParselets.Add(TokenTypes.OperatorQuestion, new ConditionalParselet());

            foreach (TokenType tc in BinaryInfixRegistry.GetTokens())
            {
                BinaryInfixRegistry.RegItem reg = BinaryInfixRegistry.Get(tc);
                RegisterBinaryOperator(tc, reg.PrecedenceLevel, reg.IsRightAssociative);
            }

            foreach (TokenType tc in UnaryPostfixRegistry.GetTokens())
            {
                RegisterPostfixOperator(tc);
            }

            if (InfixLoading != null)
            {
                InfixLoading(null, new InfixLoadingEventArgs(InfixParselets));
            }

            hasRegistered = true;
        }
        /// <summary>
        /// Parses an expression with the parser through recursion
        /// </summary>
        /// <param name="parser">Parser to continue recursion</param>
        /// <param name="left">Already-parsed left side of operator</param>
        /// <param name="token">Identifying token for operator</param>
        /// <returns></returns>
        public NodeBase Parse(Parser parser, NodeBase left, Token token)
        {
            NodeBase right = parser.Parse(RightAssociative ? PrecedenceLevel - 1 : PrecedenceLevel);

            return(BinaryInfixRegistry.MakeNode(token.Type, left, right));
        }