-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathphp2json.py
More file actions
executable file
·29 lines (25 loc) · 813 Bytes
/
Copy pathphp2json.py
File metadata and controls
executable file
·29 lines (25 loc) · 813 Bytes
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
#!/usr/bin/env python
# php2json.py - Converts PHP to a JSON-based abstract syntax tree
# Usage: php2json.py < input.php > output.json
import sys
from phply.phplex import lexer
from phply.phpparse import make_parser
import json
input = sys.stdin
output = sys.stdout
with_lineno = True
def export(items):
result = []
if items:
for item in items:
if hasattr(item, 'generic'):
item = item.generic(with_lineno=with_lineno)
result.append(item)
return result
def php2json(input_file, output_file):
parser = make_parser()
json.dump(export(parser.parse(input_file,
lexer=lexer,
tracking=with_lineno)),
output_file, indent=2)
output_file.write('\n')