d85f5a4816
Example from issue: ```php <?php # # Comment # $a = 100; ``` The problem with the example from the issue is that `#` is immediately followed by a line break. And since the rule in the lexer for such comments was changed, this case was handled incorrectly. ``` (('#' ^'[') | '//') any_line* when is_not_comment_end => { lex.ungetStr("?>") lex.addFreeFloatingToken(tkn, token.T_COMMENT, lex.ts, lex.te) }; ``` This rule has one problem, it checks two characters at once, first for the match `#`, and then for the mismatch `[`, which leads to the fact that in the case of an empty comment, the first matcher will capture `#`, and the second line break (`\n`), which will lead to the fact that `any_line` matcher will not work and will not increase the line number. The next rule added is specifically for this case. ``` '#' newline when is_not_comment_end => { lex.ungetStr("?>") lex.addFreeFloatingToken(tkn, token.T_COMMENT, lex.ts, lex.te) }; ``` |
||
---|---|---|
.github | ||
cmd/php-parser | ||
internal | ||
pkg | ||
.gitattributes | ||
.gitignore | ||
.travis.yml | ||
CHANGELOG.md | ||
CODE_OF_CONDUCT.md | ||
CONTRIBUTING.md | ||
go.mod | ||
go.sum | ||
LICENSE | ||
Makefile | ||
parser.jpg | ||
README.md |
This is a fork of the z7zmey parser that adds PHP 8 support.
PHP Parser written in Go
This project uses goyacc and ragel tools to create PHP parser. It parses source code into AST. It can be used to write static analysis, refactoring, metrics, code style formatting tools.
Features
- Fully support PHP 5, PHP 7 and PHP 8.0 syntax
- Abstract syntax tree (AST) representation
- Traversing AST
- Resolving namespace names
- Parsing syntax-invalid PHP files
- Saving and printing free-floating comments and whitespaces
Who Uses
- VKCOM/noverify — pretty fast linter for PHP
- VKCOM/nocolor — architecture validation tool for PHP based on the concept of colored functions
- quasilyte/phpgrep — tool for syntax-aware PHP code search
Usage example
package main
import (
"log"
"os"
"github.com/VKCOM/php-parser/pkg/conf"
"github.com/VKCOM/php-parser/pkg/errors"
"github.com/VKCOM/php-parser/pkg/parser"
"github.com/VKCOM/php-parser/pkg/version"
"github.com/VKCOM/php-parser/pkg/visitor/dumper"
)
func main() {
src := []byte(`<?php echo "Hello world";`)
// Error handler
var parserErrors []*errors.Error
errorHandler := func(e *errors.Error) {
parserErrors = append(parserErrors, e)
}
// Parse
rootNode, err := parser.Parse(src, conf.Config{
Version: &version.Version{Major: 8, Minor: 0},
ErrorHandlerFunc: errorHandler,
})
if err != nil {
log.Fatal("Error:" + err.Error())
}
if len(parserErrors) > 0 {
for _, e := range parserErrors {
log.Println(e.String())
}
os.Exit(1)
}
// Dump
goDumper := dumper.NewDumper(os.Stdout).
WithTokens().
WithPositions()
rootNode.Accept(goDumper)
}
Install
go get github.com/VKCOM/php-parser/cmd/php-parser
CLI
php-parser [flags] <path> ...
flag | type | description |
---|---|---|
--p |
bool |
Print file paths |
--e |
bool |
Print errors |
--d |
bool |
Dump AST in Golang format |
--r |
bool |
Resolve names |
--prof |
string |
Start profiler: [cpu, mem, trace] |
--phpver |
string |
PHP version (default: 8.0) |
Namespace resolver
Namespace resolver is a visitor that resolves nodes fully qualified name and saves into map[node.Node]string
structure
- For
Class
,Interface
,Trait
,Function
,Constant
nodes it saves name with current namespace. - For
Name
,Relative
,FullyQualified
nodes it resolvesuse
aliases and saves a fully qualified name.