22
22
23
23
``` js
24
24
// initialize the php parser factory class
25
+ var fs = require (' fs' );
26
+ var path = require (' path' );
25
27
var engine = require (' php-parser' );
28
+
26
29
// initialize a new parser instance
27
30
var parser = new engine ({
28
31
// some options :
@@ -35,12 +38,19 @@ var parser = new engine({
35
38
});
36
39
37
40
// Retrieve the AST from the specified source
38
- var AST = parser .parseEval (' echo "Hello World";' );
39
- // AST.kind === 'program';
40
- // AST.children[0].kind === 'echo';
41
+ var eval = parser .parseEval (' echo "Hello World";' );
41
42
42
43
// Retrieve an array of tokens (same as php function token_get_all)
43
44
var tokens = parser .tokenGetAll (' <?php echo "Hello World";' );
45
+
46
+ // Load a static file (Note: this file should exist on your computer)
47
+ var phpFile = fs .readFileSync ( ' ./example.php' );
48
+
49
+ // Log out results
50
+ console .log ( ' Eval parse:' , eval );
51
+ console .log ( ' Tokens parse:' , tokens );
52
+ console .log ( ' File parse:' , parser .parseCode (phpFile) );
53
+
44
54
```
45
55
46
56
Sample AST output
@@ -72,9 +82,9 @@ API Overview
72
82
73
83
The main API exposes a class with the following methods :
74
84
75
- - ** parseEval** (String buffer ) : parse a PHP code in eval style mode (without php open tags)
76
- - ** parseCode** (String buffer , String filename) : parse a PHP code by using php open tags.
77
- - ** tokenGetAll** (String buffer ) : retrieves a list of all tokens from the specified input.
85
+ - ** parseEval** (String|Buffer ) : parse a PHP code in eval style mode (without php open tags)
86
+ - ** parseCode** (String|Buffer , String filename) : parse a PHP code by using php open tags.
87
+ - ** tokenGetAll** (String|Buffer ) : retrieves a list of all tokens from the specified input.
78
88
79
89
You can also [ pass options] ( https://github.com/glayzzle/php-parser/wiki/Options ) that change the behavior of the parser/lexer.
80
90
0 commit comments