forked from glayzzle/php-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
116 lines (106 loc) · 2.96 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
* Copyright (C) 2014 Glayzzle (BSD3 License)
* @authors https://github.com/glayzzle/php-parser/graphs/contributors
* @url http://glayzzle.com
*/
var extend = require('util')._extend;
// helper to clone an object
var deepCopy = function(origin, add) {
if (!add || typeof add !== 'object') return origin;
var keys = Object.keys(add);
var i = keys.length;
while (i--) {
var assign = add[keys[i]];
if (typeof assign === 'object' && !Array.isArray(assign)) {
if (!origin.hasOwnProperty(keys[i])) {
origin[keys[i]] = {};
}
origin[keys[i]] = deepCopy(origin[keys[i]], assign);
} else {
origin[keys[i]] = assign;
}
}
return origin;
};
var engine = {
// creates a new instance (for multiple parsing cases)
create: function(options) {
var result = deepCopy({}, this);
if (options) {
if (options.hasOwnProperty('lexer')) {
result.lexer = deepCopy(result.lexer, options.lexer);
}
if (options.hasOwnProperty('parser')) {
result.parser = deepCopy(result.parser, options.parser);
}
result.parser.lexer = result.lexer;
}
return result;
},
// parsing eval string as '$x = 1;'
parseEval: function(buffer, options) {
var lexer = this.lexer;
var parser = this.parser;
if (options) {
if (options.hasOwnProperty('lexer')) {
lexer = extend(lexer, options.lexer);
}
if (options.hasOwnProperty('parser')) {
parser = extend(parser, options.parser);
}
parser.lexer = lexer;
}
lexer.mode_eval = true;
lexer.all_tokens = false;
return parser.parse(buffer);
}
// parse php code with '<?php $x = 1;'
,parseCode: function(buffer, options) {
var lexer = this.lexer;
var parser = this.parser;
if (options) {
if (options.hasOwnProperty('lexer')) {
lexer = extend(lexer, options.lexer);
}
if (options.hasOwnProperty('parser')) {
parser = extend(parser, options.parser);
}
parser.lexer = lexer;
}
lexer.mode_eval = false;
lexer.all_tokens = false;
return parser.parse(buffer);
}
// split the buffer into tokens
,tokenGetAll: function(buffer, options) {
var lexer = this.lexer;
lexer.mode_eval = false;
lexer.all_tokens = true;
if (options) {
lexer = extend(lexer, options);
}
var EOF = lexer.EOF;
var names = this.tokens.values;
lexer.setInput(buffer);
var token = lexer.lex() || EOF;
var result = [];
while(token != EOF) {
var entry = lexer.yytext;
if (names.hasOwnProperty(token)) {
entry = [names[token], entry, lexer.yylloc.first_line];
}
result.push(entry);
token = lexer.lex() || EOF;
}
return result;
}
,parser: null
,lexer: null
// tokens dictionnary
,tokens: require('./src/tokens')
};
// lexer instance
engine.lexer = require('./src/lexer')(engine);
// parser instance
engine.parser = require('./src/parser')(engine);
module.exports = engine;