parse files

This commit is contained in:
z7zmey 2017-12-01 18:04:31 +02:00
parent e41b97a09d
commit 880749fbda
8 changed files with 572 additions and 518 deletions

View File

@ -10,7 +10,7 @@ all: parser.go scanner.go
go build go build
run: all run: all
./php-parser ./php-parser example.php
scanner.go: scanner.l scanner.go: scanner.l
golex -o $@ $< golex -o $@ $<

16
example.php Normal file
View File

@ -0,0 +1,16 @@
<?php
namespace z7zmey\Example;
use \Exception;
use z7zmey\Foo\{Bar, function Baz};
abstract class Foo extends Bar implements Buz, Buzz {
use \z7zmey\_Trait;
public const CC = 0;
public function &test(bool $a, string $b = null): ?void {
}
}

View File

@ -20,7 +20,7 @@ type lexer struct {
stateStack []int stateStack []int
} }
func newLexer(src io.Reader, dst io.Writer, fName string) *lexer { func newLexer(src io.Reader, fName string) *lexer {
file := token.NewFileSet().AddFile(fName, -1, 1<<31-1) file := token.NewFileSet().AddFile(fName, -1, 1<<31-1)
lx, err := lex.New(file, bufio.NewReader(src), lex.RuneClass(rune2Class)) lx, err := lex.New(file, bufio.NewReader(src), lex.RuneClass(rune2Class))
if err != nil { if err != nil {

48
main.go
View File

@ -1,41 +1,33 @@
package main package main
import ( import (
"bytes" "flag"
"fmt"
"log"
"os" "os"
"unicode"
"github.com/yookoala/realpath"
) )
const src = `
<?php
namespace Test;
/**
* Class foo
*/
class foo
{
}
`
func main() { func main() {
yyDebug = 0 yyDebug = 0
yyErrorVerbose = true yyErrorVerbose = true
l := newLexer(bytes.NewBufferString(src), os.Stdout, "file.name")
yyParse(l) flag.Parse()
for _, path := range flag.Args() {
real, err := realpath.Realpath(path)
checkErr(err)
fmt.Printf("\n==> %s", real)
src, _ := os.Open(string(real))
rn := parse(src, real)
fmt.Println(rn)
}
} }
func rune2Class(r rune) int { func checkErr(err error) {
if r >= 0 && r < 0x80 { // Keep ASCII as it is. if err != nil {
return int(r) log.Fatal(err)
} }
if unicode.IsLetter(r) {
return classUnicodeLeter
}
if unicode.IsDigit(r) {
return classUnicodeDigit
}
// return classOther
return -1
} }

980
parser.go

File diff suppressed because it is too large Load Diff

View File

@ -2,9 +2,17 @@
package main package main
import ( import (
"fmt" "io"
) )
var rootNode = Node("Root")
func parse(src io.Reader, fName string) node {
rootNode = Node("Root") //reset
yyParse(newLexer(src, fName))
return rootNode
}
%} %}
%union{ %union{
@ -194,7 +202,7 @@ import (
///////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////
start: start:
top_statement_list { fmt.Println($1) } top_statement_list { rootNode = $1; }
; ;
reserved_non_modifiers: reserved_non_modifiers:

View File

@ -11,6 +11,7 @@ package main
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"unicode"
) )
const ( const (
@ -29,6 +30,20 @@ const (
var heredocLabel []byte var heredocLabel []byte
func rune2Class(r rune) int {
if r >= 0 && r < 0x80 { // Keep ASCII as it is.
return int(r)
}
if unicode.IsLetter(r) {
return classUnicodeLeter
}
if unicode.IsDigit(r) {
return classUnicodeDigit
}
// return classOther
return -1
}
func (l *lexer) Lex(lval *yySymType) int { // Lex(lval *yySymType) func (l *lexer) Lex(lval *yySymType) int { // Lex(lval *yySymType)
c := l.Enter() c := l.Enter()

View File

@ -10,6 +10,7 @@ package main
import ( import (
"fmt" "fmt"
"bytes" "bytes"
"unicode"
) )
const ( const (
@ -28,6 +29,20 @@ const (
var heredocLabel []byte var heredocLabel []byte
func rune2Class(r rune) int {
if r >= 0 && r < 0x80 { // Keep ASCII as it is.
return int(r)
}
if unicode.IsLetter(r) {
return classUnicodeLeter
}
if unicode.IsDigit(r) {
return classUnicodeDigit
}
// return classOther
return -1
}
func (l *lexer) Lex(lval *yySymType) int { // Lex(lval *yySymType) func (l *lexer) Lex(lval *yySymType) int { // Lex(lval *yySymType)
c := l.Enter() c := l.Enter()