-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.html
More file actions
91 lines (78 loc) · 2.53 KB
/
parser.html
File metadata and controls
91 lines (78 loc) · 2.53 KB
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
<html>
<head>
<title>Cadence to JS translator test page</title>
<!--script src="../js/dummyconsole.js"></script-->
<script src="./js/lib/json2.js"></script>
<script src="./js/lib/beautify.js"></script>
<script>Cadence = { };</script>
<script src="./js/language/lang.js"></script>
<script src="./js/core/lex.js"></script>
<script src="./js/core/parser.js"></script>
<script src="./js/core/errors.js"></script>
<script src="./js/core/ast.js"></script>
<script src="./js/core/core.js"></script>
</head>
<body>
<h2>Test the parser</h2>
<textarea id="parser-input" rows="8" cols="80"></textarea>
<p>
<button id="parse-btn">Parse</button>
<button id="link-btn">Link to example</button>
</p>
<h2>Generated Javascript</h2>
<pre id="parser-output2"></pre>
<h2>Generated AST</h2>
<pre id="parser-output"></pre>
<script>
function getParameterByName(name) {
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if (results == null)
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
function getCadenceSource() {
return document.getElementById('parser-input').value;
}
function translate() {
var cadenceSource = getCadenceSource();
try {
window.localStorage.setItem('cadenceSource', cadenceSource);
} catch (e) {
// allow localStorage to fail
}
var parserAST = new Cadence.Parser(cadenceSource);
parserAST.parse();
if (parserAST.errors.length == 0) {
var genout = parserAST.generate();
document.getElementById('parser-output2').innerHTML = genout;
eval(genout);
}
var parserOutput = parserAST.prettyPrint();
document.getElementById('parser-output').innerHTML = parserOutput;
}
var previousSource = getParameterByName("s");
if (!previousSource) {
try {
previousSource = window.localStorage.getItem('cadenceSource');
} catch (e) {
// allow localStorage to fail
}
}
if (previousSource) {
document.getElementById('parser-input').value = previousSource;
translate();
}
document.getElementById('parse-btn').addEventListener('click', function () {
translate();
});
document.getElementById('link-btn').addEventListener('click', function () {
window.location.href = window.location.origin + window.location.pathname +
'?s=' + encodeURIComponent(getCadenceSource());
});
</script>
</body>
</html>