From 4db5ba44b202b2d1890576ab5bd7fd14425ef429 Mon Sep 17 00:00:00 2001 From: Hackerpilot Date: Fri, 6 Mar 2020 13:09:14 -0800 Subject: [PATCH] Error out on lexer failure --- src/dfmt/formatter.d | 21 +++++++++++++++++++-- src/dfmt/main.d | 19 +++++++++++-------- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/dfmt/formatter.d b/src/dfmt/formatter.d index 354401d..9dd1103 100644 --- a/src/dfmt/formatter.d +++ b/src/dfmt/formatter.d @@ -16,7 +16,17 @@ import dfmt.tokens; import dfmt.wrapping; import std.array; -void format(OutputRange)(string source_desc, ubyte[] buffer, OutputRange output, +/** + * Formats the code contained in `buffer` into `output`. + * Params: + * source_desc = A description of where `buffer` came from. Usually a file name. + * buffer = The raw source code. + * output = The output range that will have the formatted code written to it. + * formatterConfig = Formatter configuration. + * Returns: `true` if the formatting succeeded, `false` of a lexing error. This + * function can return `true` if parsing failed. + */ +bool format(OutputRange)(string source_desc, ubyte[] buffer, OutputRange output, Config* formatterConfig) { LexerConfig config; @@ -33,11 +43,18 @@ void format(OutputRange)(string source_desc, ubyte[] buffer, OutputRange output, auto visitor = new FormatVisitor(&astInformation); visitor.visit(mod); astInformation.cleanup(); - auto tokens = byToken(buffer, config, &cache).array(); + auto tokenRange = byToken(buffer, config, &cache); + auto app = appender!(Token[])(); + for (; !tokenRange.empty(); tokenRange.popFront()) + app.put(tokenRange.front()); + auto tokens = app.data; + if (!tokenRange.messages.empty) + return false; auto depths = generateDepthInfo(tokens); auto tokenFormatter = TokenFormatter!OutputRange(buffer, tokens, depths, output, &astInformation, formatterConfig); tokenFormatter.format(); + return true; } immutable(short[]) generateDepthInfo(const Token[] tokens) pure nothrow @trusted diff --git a/src/dfmt/main.d b/src/dfmt/main.d index 6844cfc..ad45279 100644 --- a/src/dfmt/main.d +++ b/src/dfmt/main.d @@ -19,14 +19,14 @@ static immutable VERSION = () { version (built_with_dub) { - enum DFMT_VERSION = import("dubhash.txt").strip; + enum DFMT_VERSION = import("dubhash.txt").strip; } else { - /** - * Current build's Git commit hash - */ - enum DFMT_VERSION = import("githash.txt").strip; + /** + * Current build's Git commit hash + */ + enum DFMT_VERSION = import("githash.txt").strip; } return DFMT_VERSION ~ DEBUG_SUFFIX; @@ -211,7 +211,7 @@ else else break; } - format("stdin", buffer, output.lockingTextWriter(), &config); + return format("stdin", buffer, output.lockingTextWriter(), &config); } else { @@ -219,6 +219,7 @@ else if (args.length >= 2) inplace = true; + int retVal; while (args.length > 0) { const path = args.front; @@ -253,11 +254,13 @@ else f.rawRead(buffer); if (inplace) output = File(path, "wb"); - format(path, buffer, output.lockingTextWriter(), &config); + bool formatResult = format(path, buffer, output.lockingTextWriter(), &config); + if (!formatResult) + retVal = 1; } } + return retVal; } - return 0; } }