-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
61 lines (50 loc) · 1.36 KB
/
app.py
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
import sys
import traceback
from flask import (
Flask,
jsonify,
render_template,
request
)
from parser import parse_string
from util import to_json, type_name
from visitor import NodeVisitor
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/parse', methods=['POST'])
def parse():
source = request.form.get('source', "")
if not source.endswith("\n"):
source += "\n"
return jsonify(tree=to_json(parse_string(source)))
@app.route('/transform', methods=['POST'])
def transform():
source = request.form.get('source', "")
if not source.endswith("\n"):
source += "\n"
node = parse_string(source)
visitor_source = request.form.get('visitor', "")
globals_ = {
"NodeVisitor": NodeVisitor,
"type_name": type_name
}
output = None
error = None
try:
exec(visitor_source, globals_, None)
visitor = globals_["Visitor"]()
visitor.visit(node)
output = str(node)
except Exception as e:
exc_type, exc_value, exc_traceback = sys.exc_info()
parts = traceback.extract_tb(exc_traceback)
_, lineno, _, _ = parts[-1]
error = {
"lineno": lineno,
"message": str(exc_value)
}
return jsonify(output=output, error=error)
if __name__ == "__main__":
app.run()