2022-02-07 20:08:43 +00:00
|
|
|
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
|
|
|
#include <optional>
|
|
|
|
|
|
|
|
#include "Luau/Common.h"
|
|
|
|
#include "Luau/Ast.h"
|
2022-08-04 23:35:33 +01:00
|
|
|
#include "Luau/AstJsonEncoder.h"
|
2022-02-07 20:08:43 +00:00
|
|
|
#include "Luau/Parser.h"
|
|
|
|
#include "Luau/ParseOptions.h"
|
2022-12-09 19:57:01 +00:00
|
|
|
#include "Luau/ToString.h"
|
2022-02-07 20:08:43 +00:00
|
|
|
|
|
|
|
#include "FileUtils.h"
|
|
|
|
|
|
|
|
static void displayHelp(const char* argv0)
|
|
|
|
{
|
|
|
|
printf("Usage: %s [file]\n", argv0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int assertionHandler(const char* expr, const char* file, int line, const char* function)
|
|
|
|
{
|
|
|
|
printf("%s(%d): ASSERTION FAILED: %s\n", file, line, expr);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
Luau::assertHandler() = assertionHandler;
|
|
|
|
|
|
|
|
for (Luau::FValue<bool>* flag = Luau::FValue<bool>::list; flag; flag = flag->next)
|
|
|
|
if (strncmp(flag->name, "Luau", 4) == 0)
|
|
|
|
flag->value = true;
|
|
|
|
|
|
|
|
if (argc >= 2 && strcmp(argv[1], "--help") == 0)
|
|
|
|
{
|
|
|
|
displayHelp(argv[0]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (argc < 2)
|
|
|
|
{
|
|
|
|
displayHelp(argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* name = argv[1];
|
|
|
|
std::optional<std::string> maybeSource = std::nullopt;
|
|
|
|
if (strcmp(name, "-") == 0)
|
|
|
|
{
|
|
|
|
maybeSource = readStdin();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
maybeSource = readFile(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!maybeSource)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Couldn't read source %s\n", name);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string source = *maybeSource;
|
|
|
|
|
|
|
|
Luau::Allocator allocator;
|
|
|
|
Luau::AstNameTable names(allocator);
|
|
|
|
|
|
|
|
Luau::ParseOptions options;
|
2022-07-21 22:16:54 +01:00
|
|
|
options.captureComments = true;
|
2022-02-07 20:08:43 +00:00
|
|
|
options.allowDeclarationSyntax = true;
|
|
|
|
|
|
|
|
Luau::ParseResult parseResult = Luau::Parser::parse(source.data(), source.size(), names, allocator, options);
|
|
|
|
|
|
|
|
if (parseResult.errors.size() > 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Parse errors were encountered:\n");
|
|
|
|
for (const Luau::ParseError& error : parseResult.errors)
|
|
|
|
{
|
|
|
|
fprintf(stderr, " %s - %s\n", toString(error.getLocation()).c_str(), error.getMessage().c_str());
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
|
2022-07-21 22:16:54 +01:00
|
|
|
printf("%s", Luau::toJson(parseResult.root, parseResult.commentLocations).c_str());
|
2022-02-07 20:08:43 +00:00
|
|
|
|
|
|
|
return parseResult.errors.size() > 0 ? 1 : 0;
|
|
|
|
}
|