public void Star_Slash_No_Close_Tag_Returns_Comment_Token()
 {
     var lexer = new PhpLexer("<?/*");
     var tokens = lexer.GetTokens();
     CollectionAssert.AreEqual(new List<PhpToken>
     {
         new PhpToken(PhpTokenType.OpenTag, "<?", 0),
         new PhpToken(PhpTokenType.Comment, "/*", 2)
     }, tokens);
 }
 public static void IsValid(TokenPairs pairs)
 {
     var text = string.Join("", pairs.Select(pair => pair.Value).ToArray());
     var lexer = new PhpLexer(text);
     int index = 0;
     var expected = pairs.Select(pair =>
     {
         var token = new PhpToken(pair.Key, pair.Value, index);
         index += pair.Value.Length;
         return token;
     });
     List<PhpToken> actual = lexer.GetTokens();
     CollectionAssert.AreEqual(expected, actual);
 }
 public void One_Line_Html_Php_Mixed_With_Comment_Returns_Tokens()
 {
     var lexer = new PhpLexer("<h1>This is an <?php # echo 'simple';?> example</h1>");
     var tokens = lexer.GetTokens();
     CollectionAssert.AreEqual(new List<PhpToken>
     {
         new PhpToken(PhpTokenType.Html, "<h1>This is an ", 0),
         new PhpToken(PhpTokenType.OpenTag, "<?php", 15),
         new PhpToken(PhpTokenType.WhiteSpace, " ", 20),
         new PhpToken(PhpTokenType.Comment, "# echo 'simple';", 21),
         new PhpToken(PhpTokenType.CloseTag, "?>", 37),
         new PhpToken(PhpTokenType.Html, " example</h1>", 39)
     }, tokens);
 }
        private static PhpToken[] GetTokens(string text)
        {
            var lexer = new PhpLexer(text);

            return lexer.GetTokens().ToArray();
        }
 public static void IsEmpty(PhpLexer lexer)
 {
     CollectionAssert.AreEqual(new List<PhpToken>(), lexer.GetTokens());
 }
 public void Simple_Open_Tag_And_Echo_Returns_OpenTag_And_Echo_Tokens()
 {
     var lexer = new PhpLexer("<?echo(\"test\")");
     var tokens = lexer.GetTokens();
     CollectionAssert.AreEqual(new List<PhpToken>
     {
         new PhpToken(PhpTokenType.OpenTag, "<?", 0),
         new PhpToken(PhpTokenType.Identifier, "echo", 2),
         new PhpToken(PhpTokenType.LeftParenthesis, "(", 6),
         new PhpToken(PhpTokenType.String, "\"test\"", 7),
         new PhpToken(PhpTokenType.RightParenthesis, ")", 13)
     }, tokens);
 }
 public void LessThan_QuestionMark_phz_Returns_OpenTag_And_Unknown_Tokens()
 {
     var lexer = new PhpLexer("<?phz");
     var tokens = lexer.GetTokens();
     CollectionAssert.AreEqual(new List<PhpToken>
     {
         new PhpToken(PhpTokenType.OpenTag, "<?", 0),
         new PhpToken(PhpTokenType.Unknown, "phz", 2)
     }, tokens);
 }
 public void Empty_String_Returns_Empty_List()
 {
     var lexer = new PhpLexer("");
     LexerAssert.IsEmpty(lexer);
     Assert.IsNullOrEmpty(lexer.Text);
 }
 public void Default_Constructor_Returns_Empty_List()
 {
     var lexer = new PhpLexer();
     LexerAssert.IsEmpty(lexer);
     Assert.IsNullOrEmpty(lexer.Text);
 }
        public void Set(ref string PHP)
        {
            var lexer = new PhpLexer(PHP);
            var tokens = lexer.GetTokens().ToArray();
            var unk = tokens.Where(x => x.TokenType == PhpTokenType.Unknown);

            #if DEBUG
            if (unk.Any())
                throw new InvalidOperationException();
            #endif

            var funcTokens = StartDelimiter == '(' ?
                PhpParser.GetGlobalFunctionCallIds(tokens) :
                PhpParser.GetArrayAccesses(tokens);

            for (int i = funcTokens.Length - 1; i >= 0; i--)
            {
                if (funcTokens[i].Lexeme != FunctionName)
                    continue;

                if (StartDelimiter == '(')
                {
                    PHP = PHP.Remove(funcTokens[i].Index, funcTokens[i].Lexeme.Length);
                    PHP = PHP.Insert(funcTokens[i].Index, ReplacementName);
                }
                else
                {
                    var tokenIndex = Array.IndexOf(tokens, tokens.Single(x => x.Index == funcTokens[i].Index));
                    PhpToken leftBracket = new PhpToken(), rightBracket = new PhpToken();
                    for (int j = tokenIndex + 1; j < tokens.Length; j++)
                    {
                        if (tokens[j].TokenType == PhpTokenType.LeftBracket)
                            leftBracket = tokens[j];
                        else if (tokens[j].TokenType == PhpTokenType.RightBracket)
                        {
                            rightBracket = tokens[j];
                            break;
                        }
                    }
                    if (leftBracket.TokenType != PhpTokenType.LeftBracket)
                        continue;

                    PHP = PHP.Insert(rightBracket.Index, ")");
                    PHP = PHP.Insert(leftBracket.Index + 1, ReplacementName + "(");
                }
            }
        }
 public void Two_Slashes_Newline_Returns_Comment_Token()
 {
     var lexer = new PhpLexer("<?//\nfunct");
     var tokens = lexer.GetTokens();
     CollectionAssert.AreEqual(new List<PhpToken>
     {
         new PhpToken(PhpTokenType.OpenTag, "<?", 0),
         new PhpToken(PhpTokenType.Comment, "//", 2),
         new PhpToken(PhpTokenType.WhiteSpace, "\n", 4),
         new PhpToken(PhpTokenType.Unknown, "funct", 5)
     }, tokens);
 }