Skip to content

Commit 8972928

Browse files
committed
handle errors with a SyntaxError error
1 parent 4f94f6d commit 8972928

File tree

5 files changed

+108
-16
lines changed

5 files changed

+108
-16
lines changed

docs/README.md

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,30 @@ Returns **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refere
2727

2828
## parseCode
2929

30-
parse php code with '<?php $x = 1;'
30+
Function that parse a php code with open/close tags
31+
32+
Sample code :
33+
34+
```php
35+
<?php $x = 1;
36+
```
37+
38+
Usage :
39+
40+
```js
41+
var parser = require('php-parser');
42+
var phpParser = new parser({
43+
// some options
44+
});
45+
var ast = phpParser.parseCode('...php code...', 'foo.php');
46+
```
3147

3248
**Parameters**
3349

34-
- `buffer`
50+
- `buffer` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** The code to be parsed
51+
- `filename` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** Filename
52+
53+
Returns **Program**
3554

3655
## tokenGetAll
3756

@@ -62,18 +81,36 @@ Evaluate the buffer
6281

6382
## parseCode
6483

65-
parse php code with '&lt;?php $x = 1;'
84+
Static function that parse a php code with open/close tags
85+
86+
Sample code :
87+
88+
```php
89+
<?php $x = 1;
90+
```
91+
92+
Usage :
93+
94+
```js
95+
var parser = require('php-parser');
96+
var ast = parser.parseCode('...php code...', 'foo.php');
97+
```
6698

6799
**Parameters**
68100

69-
- `buffer`
70-
- `options`
101+
- `buffer` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** The code to be parsed
102+
- `filename` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** Filename
103+
- `options` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** Optional options
104+
105+
Returns **Program**
71106

72107
## tokenGetAll
73108

74-
split the buffer into tokens
109+
Split the buffer into tokens
75110

76111
**Parameters**
77112

78113
- `buffer`
79114
- `options`
115+
116+
Returns **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)>**

docs/parser.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ main entry point : converts a source code to AST
3232
**Parameters**
3333

3434
- `code`
35+
- `filename`
3536

3637
## raiseError
3738

src/index.js

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,28 +78,66 @@ engine.parseEval = function(buffer, options) {
7878
engine.prototype.parseEval = function(buffer) {
7979
this.lexer.mode_eval = true;
8080
this.lexer.all_tokens = false;
81-
return this.parser.parse(buffer);
81+
return this.parser.parse(buffer, 'eval');
8282
};
8383

8484
/**
85-
* parse php code with '<?php $x = 1;'
85+
* Static function that parse a php code with open/close tags
86+
*
87+
* Sample code :
88+
* ```php
89+
* <?php $x = 1;
90+
* ```
91+
*
92+
* Usage :
93+
* ```js
94+
* var parser = require('php-parser');
95+
* var ast = parser.parseCode('...php code...', 'foo.php');
96+
* ```
97+
* @param {String} buffer - The code to be parsed
98+
* @param {String} filename - Filename
99+
* @param {Object} options - Optional options
100+
* @return {Program}
86101
*/
87-
engine.parseCode = function(buffer, options) {
102+
engine.parseCode = function(buffer, filename, options) {
103+
if (typeof filename === 'object') {
104+
// retro-compatibility
105+
options = filename;
106+
filename = 'unknown';
107+
}
88108
var self = new engine(options);
89-
return self.parseCode(buffer);
109+
return self.parseCode(buffer, filename);
90110
};
91111

92112
/**
93-
* parse php code with '<?php $x = 1;'
113+
* Function that parse a php code with open/close tags
114+
*
115+
* Sample code :
116+
* ```php
117+
* <?php $x = 1;
118+
* ```
119+
*
120+
* Usage :
121+
* ```js
122+
* var parser = require('php-parser');
123+
* var phpParser = new parser({
124+
* // some options
125+
* });
126+
* var ast = phpParser.parseCode('...php code...', 'foo.php');
127+
* ```
128+
* @param {String} buffer - The code to be parsed
129+
* @param {String} filename - Filename
130+
* @return {Program}
94131
*/
95-
engine.prototype.parseCode = function(buffer) {
132+
engine.prototype.parseCode = function(buffer, filename) {
96133
this.lexer.mode_eval = false;
97134
this.lexer.all_tokens = false;
98-
return this.parser.parse(buffer);
135+
return this.parser.parse(buffer, filename);
99136
};
100137

101138
/**
102-
* split the buffer into tokens
139+
* Split the buffer into tokens
140+
* @return {String[]}
103141
*/
104142
engine.tokenGetAll = function(buffer, options) {
105143
var self = new engine(options);

src/parser.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,9 @@ parser.prototype.getTokenName = function(token) {
156156
/**
157157
* main entry point : converts a source code to AST
158158
*/
159-
parser.prototype.parse = function(code) {
159+
parser.prototype.parse = function(code, filename) {
160160
this._errors = [];
161+
this.filename = filename || 'eval';
161162
this.currentNamespace = [''];
162163
this.lexer.setInput(code);
163164
this.lexer.comment_tokens = this.extractDoc;
@@ -185,7 +186,11 @@ parser.prototype.parse = function(code) {
185186
parser.prototype.raiseError = function(message, msgExpect, expect, token) {
186187
message += ' on line ' + this.lexer.yylloc.first_line;
187188
if (!this.suppressErrors) {
188-
throw new Error(message);
189+
var err = new SyntaxError(
190+
message, this.filename, this.lexer.yylloc.first_line
191+
);
192+
err.columnNumber = this.lexer.yylloc.first_column
193+
throw err;
189194
}
190195
// Error node :
191196
var node = this.ast.prepare('error', this)(

test/astTests.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@ describe('Test AST structure', function() {
99
ast.children.length.should.be.exactly(0);
1010
});
1111

12+
13+
it('test syntax error', function() {
14+
(function(){
15+
var ast = parser.parseCode([
16+
'<?php',
17+
' $a = 1',
18+
' $b = 2' // <-- unexpected $b expecting a ';'
19+
].join('\n'));
20+
}).should.throw(/line\s3/);
21+
});
22+
1223
it('test inline', function() {
1324
var ast = parser.parseCode('Hello <?php echo "World"; ?> !');
1425
ast.children[0].kind.should.be.exactly('inline');

0 commit comments

Comments
 (0)