You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: bin/peggy.js
+23
Original file line number
Diff line number
Diff line change
@@ -3014,6 +3014,11 @@ const cliOptions = program
3014
3014
"-m, --source-map [mapfile]",
3015
3015
"Generate a source map. If name is not specified, the source map will be named \"<input_file>.map\" if input is a file and \"source.map\" if input is a standard input. This option conflicts with the `-t/--test` and `-T/--test-file` options unless `-o/--output` is also specified"
3016
3016
)
3017
+
.option(
3018
+
"-a, --ast",
3019
+
"Output a grammar AST instead of a parser code",
3020
+
false
3021
+
)
3017
3022
.option(
3018
3023
"-t, --test <text>",
3019
3024
"Test the parser with the given text, outputting the result of running the parser instead of the parser itself. If the input to be tested is not parsed, the CLI will exit with code 2"
@@ -3057,6 +3062,7 @@ const PARSER_DEFAULTS = {
3057
3062
3058
3063
// Default values for CLI arguments
3059
3064
constPROG_DEFAULTS={
3065
+
ast: false,
3060
3066
input: undefined,
3061
3067
output: undefined,
3062
3068
sourceMap: undefined,
@@ -3148,6 +3154,16 @@ if (options.exportVar !== undefined) {
3148
3154
}
3149
3155
}
3150
3156
3157
+
if(progOptions.ast&&progOptions.sourceMap){
3158
+
abort("Can't use the -a/--ast option with the -m/--source-map option.");
3159
+
}
3160
+
if(progOptions.ast&&progOptions.test){
3161
+
abort("Can't use the -a/--ast option with the -t/--test option.");
3162
+
}
3163
+
if(progOptions.ast&&progOptions.testFile){
3164
+
abort("Can't use the -a/--ast option with the -T/--test-file option.");
3165
+
}
3166
+
3151
3167
verbose=progOptions.verbose;
3152
3168
letinputFile=progOptions.input;
3153
3169
if(verbose){
@@ -3184,6 +3200,9 @@ if (progOptions.test && progOptions.testFile) {
3184
3200
abort("The -t/--test and -T/--test-file options are mutually exclusive.");
3185
3201
}
3186
3202
3203
+
if(progOptions.ast){
3204
+
options.output="ast";
3205
+
}else
3187
3206
// If CLI parameter was defined, enable source map generation
Copy file name to clipboardExpand all lines: bin/peggy.mjs
+23
Original file line number
Diff line number
Diff line change
@@ -132,6 +132,11 @@ const cliOptions = program
132
132
"-m, --source-map [mapfile]",
133
133
"Generate a source map. If name is not specified, the source map will be named \"<input_file>.map\" if input is a file and \"source.map\" if input is a standard input. This option conflicts with the `-t/--test` and `-T/--test-file` options unless `-o/--output` is also specified"
134
134
)
135
+
.option(
136
+
"-a, --ast",
137
+
"Output a grammar AST instead of a parser code",
138
+
false
139
+
)
135
140
.option(
136
141
"-t, --test <text>",
137
142
"Test the parser with the given text, outputting the result of running the parser instead of the parser itself. If the input to be tested is not parsed, the CLI will exit with code 2"
@@ -175,6 +180,7 @@ const PARSER_DEFAULTS = {
175
180
176
181
// Default values for CLI arguments
177
182
constPROG_DEFAULTS={
183
+
ast: false,
178
184
input: undefined,
179
185
output: undefined,
180
186
sourceMap: undefined,
@@ -266,6 +272,16 @@ if (options.exportVar !== undefined) {
266
272
}
267
273
}
268
274
275
+
if(progOptions.ast&&progOptions.sourceMap){
276
+
abort("Can't use the -a/--ast option with the -m/--source-map option.");
277
+
}
278
+
if(progOptions.ast&&progOptions.test){
279
+
abort("Can't use the -a/--ast option with the -t/--test option.");
280
+
}
281
+
if(progOptions.ast&&progOptions.testFile){
282
+
abort("Can't use the -a/--ast option with the -T/--test-file option.");
283
+
}
284
+
269
285
verbose=progOptions.verbose;
270
286
letinputFile=progOptions.input;
271
287
if(verbose){
@@ -302,6 +318,9 @@ if (progOptions.test && progOptions.testFile) {
302
318
abort("The -t/--test and -T/--test-file options are mutually exclusive.");
303
319
}
304
320
321
+
if(progOptions.ast){
322
+
options.output="ast";
323
+
}else
305
324
// If CLI parameter was defined, enable source map generation
If set to <code>"source-and-map"</code>, it will return a <ahref="https://github.com/mozilla/source-map#sourcenode"><code>SourceNode</code></a> object; you can
360
366
get source code by calling <code>toString()</code> method or source code and mapping by
361
-
calling <code>toStringWithSourceMap()</code> method, see the <ahref="https://github.com/mozilla/source-map#sourcenode"><code>SourceNode</code></a> documentation
367
+
calling <code>toStringWithSourceMap()</code> method, see the <ahref="https://github.com/mozilla/source-map#sourcenode"><code>SourceNode</code></a> documentation.
368
+
If set to <code>"ast"</code> then an internal grammar AST representation will be returned
362
369
(default: <code>"parser"</code>)</p>
363
370
<blockquote>
364
371
<p><strong>Note</strong>: because of bug <ahref="https://github.com/mozilla/source-map/issues/444">source-map/444</a> you should also set <code>grammarSource</code> to
0 commit comments