parse files
This commit is contained in:
		
							parent
							
								
									e41b97a09d
								
							
						
					
					
						commit
						880749fbda
					
				
							
								
								
									
										2
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								Makefile
									
									
									
									
									
								
							| @ -10,7 +10,7 @@ all: parser.go scanner.go | ||||
| 	go build | ||||
| 
 | ||||
| run: all | ||||
| 	./php-parser | ||||
| 	./php-parser example.php | ||||
| 
 | ||||
| scanner.go: scanner.l | ||||
| 	golex -o $@ $< | ||||
|  | ||||
							
								
								
									
										16
									
								
								example.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								example.php
									
									
									
									
									
										Normal 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 { | ||||
|          | ||||
|     } | ||||
| } | ||||
							
								
								
									
										2
									
								
								lexer.go
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								lexer.go
									
									
									
									
									
								
							| @ -20,7 +20,7 @@ type lexer struct { | ||||
| 	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) | ||||
| 	lx, err := lex.New(file, bufio.NewReader(src), lex.RuneClass(rune2Class)) | ||||
| 	if err != nil { | ||||
|  | ||||
							
								
								
									
										48
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										48
									
								
								main.go
									
									
									
									
									
								
							| @ -1,41 +1,33 @@ | ||||
| package main | ||||
| 
 | ||||
| import ( | ||||
| 	"bytes" | ||||
| 	"flag" | ||||
| 	"fmt" | ||||
| 	"log" | ||||
| 	"os" | ||||
| 	"unicode" | ||||
| 
 | ||||
| 	"github.com/yookoala/realpath" | ||||
| ) | ||||
| 
 | ||||
| const src = ` | ||||
| <?php | ||||
| namespace Test; | ||||
| 
 | ||||
| /** | ||||
|  * Class foo | ||||
|  */ | ||||
| class foo | ||||
| { | ||||
|      | ||||
| } | ||||
| ` | ||||
| 
 | ||||
| func main() { | ||||
| 	yyDebug = 0 | ||||
| 	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 { | ||||
| 	if r >= 0 && r < 0x80 { // Keep ASCII as it is. | ||||
| 		return int(r) | ||||
| func checkErr(err error) { | ||||
| 	if err != nil { | ||||
| 		log.Fatal(err) | ||||
| 	} | ||||
| 	if unicode.IsLetter(r) { | ||||
| 		return classUnicodeLeter | ||||
| 	} | ||||
| 	if unicode.IsDigit(r) { | ||||
| 		return classUnicodeDigit | ||||
| 	} | ||||
| 	// return classOther | ||||
| 	return -1 | ||||
| } | ||||
|  | ||||
							
								
								
									
										12
									
								
								parser.y
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								parser.y
									
									
									
									
									
								
							| @ -2,9 +2,17 @@ | ||||
| package main | ||||
| 
 | ||||
| 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{ | ||||
| @ -194,7 +202,7 @@ import ( | ||||
| ///////////////////////////////////////////////////////////////////////// | ||||
| 
 | ||||
| start: | ||||
|     top_statement_list                                  { fmt.Println($1) } | ||||
|     top_statement_list                                  { rootNode = $1; } | ||||
| ; | ||||
| 
 | ||||
| reserved_non_modifiers: | ||||
|  | ||||
							
								
								
									
										15
									
								
								scanner.go
									
									
									
									
									
								
							
							
						
						
									
										15
									
								
								scanner.go
									
									
									
									
									
								
							| @ -11,6 +11,7 @@ package main | ||||
| import ( | ||||
| 	"bytes" | ||||
| 	"fmt" | ||||
| 	"unicode" | ||||
| ) | ||||
| 
 | ||||
| const ( | ||||
| @ -29,6 +30,20 @@ const ( | ||||
| 
 | ||||
| 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) | ||||
| 	c := l.Enter() | ||||
| 
 | ||||
|  | ||||
							
								
								
									
										15
									
								
								scanner.l
									
									
									
									
									
								
							
							
						
						
									
										15
									
								
								scanner.l
									
									
									
									
									
								
							| @ -10,6 +10,7 @@ package main | ||||
| import ( | ||||
|     "fmt" | ||||
|     "bytes" | ||||
| 	"unicode" | ||||
| ) | ||||
| 
 | ||||
| const ( | ||||
| @ -28,6 +29,20 @@ const ( | ||||
| 
 | ||||
| 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) | ||||
|   c := l.Enter() | ||||
| 
 | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user