Skip to content

face-orm/face-parser

Repository files navigation

Face Parser

Latest Stable Version Build Status Test Coverage

Tool to help to create a parser with php. Originally built to parse FQL queries but might be used for anything else.

Usage

    use Face\Parser\RegexpLexer as Lexer;
    use Face\Parser\TokenNavigation;
    
    $lexer = new Lexer();
    
    $lexer->setTokens([
    
        "function"                  => "T_FUNCTION",
        "class"                     => "T_CLASS",
        "[a-zA-Z_][a-zA-Z0-9_]*"    => "T_IDENTIFIER",
        "\\{"                       => "T_L_BRACKET",
        "\\}"                       => "T_R_BRACKET",
        "\\("                       => "T_L_PARENTHESIS",
        "\\)"                       => "T_R_PARENTHESIS",
        ";"                         => "T_SEMICOLON",
        "\\s+"                      => "T_WHITESPACE"
    
    ]);
    
    $string = 'class foo{
        function bar(){}
    }';
    
    $tokenArray = $lexer->tokenize($string);
    
    $tokens = new TokenNavigation($tokenArray);
    
    // Check if the first token is a "class" keyword or throws an exception
    $tokens->expectToBe("T_CLASS");
    
    $tokens->next();
    $tokens->expectToBe("T_IDENTIFIER");
    
    $className = $tokens->current()->getTokenValue();
    
    // while next token is a function keyword
    while($tokens->hasNext() && $tokens->look(1)->is("T_FUNCTION")){
        // Parse the function body
        someFunctionThatParsesTheFunctionAndJumpsToTheNextToken($tokens);
    }
    

About

PHP helper to create lexer/parser

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published