Beispiel #1
0
 private static object ResolveToken(TokenRef tokenRef, IReadOnlyList <Func <object> > values)
 {
     return(values[(int)(tokenRef.Family & ~TokenFamily.Reference) - ReservedTokenCount]());
 }
Beispiel #2
0
        public static bool CppStyleEvaluator(IProcessorState processor, ref int bufferLength, ref int currentBufferPosition)
        {
            TokenTrie trie = new TokenTrie();

            //Logic
            trie.AddToken(processor.Encoding.GetBytes("&&"), 0);
            trie.AddToken(processor.Encoding.GetBytes("||"), 1);
            trie.AddToken(processor.Encoding.GetBytes("^"), 2);
            trie.AddToken(processor.Encoding.GetBytes("!"), 3);
            trie.AddToken(processor.Encoding.GetBytes(">"), 4);
            trie.AddToken(processor.Encoding.GetBytes(">="), 5);
            trie.AddToken(processor.Encoding.GetBytes("<"), 6);
            trie.AddToken(processor.Encoding.GetBytes("<="), 7);
            trie.AddToken(processor.Encoding.GetBytes("=="), 8);
            trie.AddToken(processor.Encoding.GetBytes("="), 9);
            trie.AddToken(processor.Encoding.GetBytes("!="), 10);

            //Bitwise
            trie.AddToken(processor.Encoding.GetBytes("&"), 11);
            trie.AddToken(processor.Encoding.GetBytes("|"), 12);
            trie.AddToken(processor.Encoding.GetBytes("<<"), 13);
            trie.AddToken(processor.Encoding.GetBytes(">>"), 14);

            //Braces
            trie.AddToken(processor.Encoding.GetBytes("("), 15);
            trie.AddToken(processor.Encoding.GetBytes(")"), 16);

            //Whitespace
            trie.AddToken(processor.Encoding.GetBytes(" "), 17);
            trie.AddToken(processor.Encoding.GetBytes("\t"), 18);

            //EOLs
            trie.AddToken(processor.Encoding.GetBytes("\r\n"), 19);
            trie.AddToken(processor.Encoding.GetBytes("\n"), 20);
            trie.AddToken(processor.Encoding.GetBytes("\r"), 21);

            //Tokens
            trie.Append(processor.EncodingConfig.Variables);

            //Run forward to EOL and collect args
            TokenFamily     currentTokenFamily;
            List <byte>     currentTokenBytes = new List <byte>();
            List <TokenRef> tokens            = new List <TokenRef>();
            int             token;

            if (!trie.GetOperation(processor.CurrentBuffer, bufferLength, ref currentBufferPosition, out token))
            {
                currentTokenFamily = TokenFamily.Literal;
                currentTokenBytes.Add(processor.CurrentBuffer[currentBufferPosition++]);
            }
            else if (token > ReservedTokenMaxIndex)
            {
                currentTokenFamily = TokenFamily.Reference | (TokenFamily)token;
                tokens.Add(new TokenRef
                {
                    Family = currentTokenFamily
                });
            }
            else
            {
                currentTokenFamily = (TokenFamily)token;

                if (currentTokenFamily != TokenFamily.WindowsEOL && currentTokenFamily != TokenFamily.LegacyMacEOL && currentTokenFamily != TokenFamily.UnixEOL)
                {
                    tokens.Add(new TokenRef
                    {
                        Family = currentTokenFamily
                    });
                }
                else
                {
                    return(EvaluateCondition(tokens, processor.EncodingConfig.VariableValues));
                }
            }

            int braceDepth = 0;

            if (tokens[0].Family == TokenFamily.OpenBrace)
            {
                ++braceDepth;
            }

            bool first = true;

            while ((first || braceDepth > 0) && bufferLength > 0)
            {
                int targetLen = Math.Min(bufferLength, trie.MaxLength);
                for (; currentBufferPosition < bufferLength - targetLen + 1;)
                {
                    int oldBufferPos = currentBufferPosition;
                    if (trie.GetOperation(processor.CurrentBuffer, bufferLength, ref currentBufferPosition, out token))
                    {
                        if (braceDepth == 0)
                        {
                            switch (tokens[tokens.Count - 1].Family)
                            {
                            case TokenFamily.Whitespace:
                            case TokenFamily.Tab:
                            case TokenFamily.CloseBrace:
                            case TokenFamily.WindowsEOL:
                            case TokenFamily.UnixEOL:
                            case TokenFamily.LegacyMacEOL:
                                TokenFamily thisFamily = (TokenFamily)token;
                                if (thisFamily == TokenFamily.WindowsEOL || thisFamily == TokenFamily.UnixEOL || thisFamily == TokenFamily.LegacyMacEOL)
                                {
                                    currentBufferPosition = oldBufferPos;
                                }

                                break;

                            default:
                                currentBufferPosition = oldBufferPos;
                                first = false;
                                break;
                            }

                            if (!first)
                            {
                                break;
                            }
                        }

                        //We matched an item, so whatever this is, it's not a literal, end the current literal if that's
                        //  what we currently have
                        if (currentTokenFamily == TokenFamily.Literal)
                        {
                            string literal = processor.Encoding.GetString(currentTokenBytes.ToArray());
                            tokens.Add(new TokenRef
                            {
                                Family  = TokenFamily.Literal,
                                Literal = literal
                            });
                            currentTokenBytes.Clear();
                        }

                        //If we have a token from the args...
                        if (token > ReservedTokenMaxIndex)
                        {
                            if (currentTokenFamily == TokenFamily.Literal)
                            {
                                TokenRef previous = tokens[tokens.Count - 1];
                                previous.Literal += processor.Encoding.GetString(currentTokenBytes.ToArray());
                                currentTokenBytes = processor.Encoding.GetBytes(previous.Literal).ToList();
                                tokens.RemoveAt(tokens.Count - 1);
                            }
                            else
                            {
                                currentTokenFamily = TokenFamily.Reference | (TokenFamily)token;
                                tokens.Add(new TokenRef
                                {
                                    Family = currentTokenFamily
                                });
                            }
                        }
                        //If we have a normal token...
                        else
                        {
                            currentTokenFamily = (TokenFamily)token;
                            if (currentTokenFamily != TokenFamily.WindowsEOL && currentTokenFamily != TokenFamily.LegacyMacEOL && currentTokenFamily != TokenFamily.UnixEOL)
                            {
                                if (currentTokenFamily == TokenFamily.OpenBrace)
                                {
                                    ++braceDepth;
                                }
                                else if (currentTokenFamily == TokenFamily.CloseBrace)
                                {
                                    --braceDepth;
                                }

                                tokens.Add(new TokenRef
                                {
                                    Family = currentTokenFamily
                                });
                            }
                            else
                            {
                                return(EvaluateCondition(tokens, processor.EncodingConfig.VariableValues));
                            }
                        }
                    }
                    else if (braceDepth > 0)
                    {
                        currentTokenFamily = TokenFamily.Literal;
                        currentTokenBytes.Add(processor.CurrentBuffer[currentBufferPosition++]);
                    }
                    else
                    {
                        first = false;
                        break;
                    }
                }

                processor.AdvanceBuffer(currentBufferPosition);
                currentBufferPosition = processor.CurrentBufferPosition;
                bufferLength          = processor.CurrentBufferLength;
            }

            return(EvaluateCondition(tokens, processor.EncodingConfig.VariableValues));
        }