Skip to content

Commit

Permalink
tools/parse --with-spans
Browse files Browse the repository at this point in the history
  • Loading branch information
stasm committed Jan 23, 2018
1 parent 4588bec commit 0b8ba5f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion fluent-syntax/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ fixtures: $(STRUCTURE_AST)

.PHONY: $(STRUCTURE_AST)
$(STRUCTURE_AST): test/fixtures_structure/%.json: test/fixtures_structure/%.ftl
@../tools/parse.js -s $< > $@
@../tools/parse.js --silent --with-spans $< > $@
@echo -e " $(OK) $@"
2 changes: 1 addition & 1 deletion fluent/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ fixtures: $(STRUCTURE_JSON)

.PHONY: $(STRUCTURE_JSON)
$(STRUCTURE_JSON): test/fixtures_structure/%.json: ../fluent-syntax/test/fixtures_structure/%.ftl
@../tools/parse.js -rs $< > $@
@../tools/parse.js --runtime --silent $< > $@
@echo -e " $(OK) $@"
11 changes: 9 additions & 2 deletions tools/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ program
.usage('[options] [file]')
.option('-r, --runtime', 'Use the runtime parser')
.option('-s, --silent', 'Silence syntax errors')
.option('--with-spans', 'Compute spans of AST nodes')
.parse(process.argv);

if (program.args.length) {
Expand Down Expand Up @@ -47,12 +48,18 @@ function printRuntime(data) {
}

function printResource(data) {
const withSpans = !!program.withSpans;
const source = data.toString();
const res = FluentSyntax.parse(source);
const res = FluentSyntax.parse(source, {withSpans});
console.log(JSON.stringify(res, null, 2));

if (!program.silent) {
res.body.map(entry => printAnnotations(source, entry));
// Spans are required to pretty-print the annotations. If needed, re-parse
// the source.
const {body} = withSpans
? res
: FluentSyntax.parse(source, {withSpans: true});
body.map(entry => printAnnotations(source, entry));
}
}

Expand Down

0 comments on commit 0b8ba5f

Please sign in to comment.