Error out on lexer failure

This commit is contained in:
Hackerpilot 2020-03-06 13:09:14 -08:00
parent 399041c84f
commit 4db5ba44b2
2 changed files with 30 additions and 10 deletions

View File

@ -16,7 +16,17 @@ import dfmt.tokens;
import dfmt.wrapping; import dfmt.wrapping;
import std.array; 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) Config* formatterConfig)
{ {
LexerConfig config; LexerConfig config;
@ -33,11 +43,18 @@ void format(OutputRange)(string source_desc, ubyte[] buffer, OutputRange output,
auto visitor = new FormatVisitor(&astInformation); auto visitor = new FormatVisitor(&astInformation);
visitor.visit(mod); visitor.visit(mod);
astInformation.cleanup(); 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 depths = generateDepthInfo(tokens);
auto tokenFormatter = TokenFormatter!OutputRange(buffer, tokens, depths, auto tokenFormatter = TokenFormatter!OutputRange(buffer, tokens, depths,
output, &astInformation, formatterConfig); output, &astInformation, formatterConfig);
tokenFormatter.format(); tokenFormatter.format();
return true;
} }
immutable(short[]) generateDepthInfo(const Token[] tokens) pure nothrow @trusted immutable(short[]) generateDepthInfo(const Token[] tokens) pure nothrow @trusted

View File

@ -19,14 +19,14 @@ static immutable VERSION = () {
version (built_with_dub) version (built_with_dub)
{ {
enum DFMT_VERSION = import("dubhash.txt").strip; enum DFMT_VERSION = import("dubhash.txt").strip;
} }
else else
{ {
/** /**
* Current build's Git commit hash * Current build's Git commit hash
*/ */
enum DFMT_VERSION = import("githash.txt").strip; enum DFMT_VERSION = import("githash.txt").strip;
} }
return DFMT_VERSION ~ DEBUG_SUFFIX; return DFMT_VERSION ~ DEBUG_SUFFIX;
@ -211,7 +211,7 @@ else
else else
break; break;
} }
format("stdin", buffer, output.lockingTextWriter(), &config); return format("stdin", buffer, output.lockingTextWriter(), &config);
} }
else else
{ {
@ -219,6 +219,7 @@ else
if (args.length >= 2) if (args.length >= 2)
inplace = true; inplace = true;
int retVal;
while (args.length > 0) while (args.length > 0)
{ {
const path = args.front; const path = args.front;
@ -253,11 +254,13 @@ else
f.rawRead(buffer); f.rawRead(buffer);
if (inplace) if (inplace)
output = File(path, "wb"); 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;
} }
} }