This repository has been archived by the owner on Jul 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprettyPrint.js
145 lines (124 loc) · 3.64 KB
/
prettyPrint.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* eslint-env node */
'use strict';
var fs = require('fs');
var ohm = require('..');
/*
Usage: prettyPrint.js <filename>
Pretty prints the Ohm grammar in the file indicated by <filename>.
*/
// Helpers
// -------
function indentLines(arr, depth) {
var padding = new Array(depth + 1).join(' ');
return arr.join('\n' + padding);
}
function printRule(name, formals, desc, op, body) {
var ans = ' ' + name.sourceString + formals.prettyPrint() + ' ';
var indentation;
if (desc.children.length > 0) {
ans += desc.prettyPrint() + '\n ';
indentation = 4;
} else {
indentation = ans.length;
}
ans += op.sourceString.trim() + ' ';
var bodyLines = body.prettyPrint().split('\n');
return ans + indentLines(bodyLines, indentation);
}
function printPostfixOp(e, op) {
return e.prettyPrint() + op.sourceString;
}
function printPrefixOp(op, e) {
return op.sourceString + e.prettyPrint();
}
function printParams(open, paramList, close) {
var params = paramList.asIteration().children.map(function(c) {
return c.sourceString;
});
return params.length === 0 ? '' : '<' + params.join(', ') + '>';
}
// Semantics
// ---------
var semantics = ohm.ohmGrammar.createSemantics();
semantics.addOperation('prettyPrint()', {
Grammar: function(name, superGrammar, open, rules, close) {
var decl = name.sourceString + superGrammar.prettyPrint();
return decl + ' {\n' + rules.prettyPrint().join('\n') + '\n}';
},
SuperGrammar: function(_, ident) {
return ' <: ' + ident.sourceString;
},
Rule_define: printRule,
Rule_override: function(name, formals, op, body) {
return printRule(name, formals, null, op, body);
},
Rule_extend: function(name, formals, op, body) {
return printRule(name, formals, null, op, body);
},
RuleBody: function(_, termList) {
return termList.asIteration().prettyPrint().join('\n| ');
},
Formals: printParams,
Params: printParams,
TopLevelTerm_inline: function(seq, caseName) {
return seq.prettyPrint() + ' ' + caseName.prettyPrint();
},
Alt: function(list) {
return list.asIteration().prettyPrint().join(' | ');
},
Seq: function(iter) {
return iter.prettyPrint().join(' ');
},
Iter_star: printPostfixOp,
Iter_plus: printPostfixOp,
Iter_opt: printPostfixOp,
Pred_not: printPrefixOp,
Pred_lookahead: printPrefixOp,
Lex_lex: printPrefixOp,
Base_application: function(id, params) {
return id.sourceString + params.prettyPrint();
},
Base_range: function(t1, _, t2) {
return t1.prettyPrint() + '..' + t2.prettyPrint();
},
Base_paren: function(open, alt, close) {
return '(' + alt.prettyPrint() + ')';
},
caseName: function(_, leading, name, trailing, end) {
return '-- ' + name.sourceString;
},
ruleDescr: function(open, text, close) {
return '(' + text.sourceString.trim() + ')';
},
terminal: function(open, _, close) {
return this.sourceString;
},
oneCharTerminal: function(open, c, close) {
return this.sourceString;
}
});
// Exports
// -------
var prettyPrint = module.exports = function(source) {
var matchResult = ohm.ohmGrammar.match(source, 'Grammar');
if (matchResult.failed()) {
return matchResult;
}
return semantics(matchResult).prettyPrint();
};
// Main
// ----
if (require.main === module) {
var filename = process.argv[2];
var source = fs.readFileSync(filename).toString();
var result = prettyPrint(source, filename);
/* eslint-disable no-console, no-process-exit */
if (typeof result === 'string') {
console.log(result);
} else {
console.error('Not an Ohm grammar: ' + filename);
console.error(result.message);
process.exit(1);
}
/* eslint-enable no-console */
}