1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 04:37:44 +00:00

JSSpecCompiler: Provide an adequate command line interface

This commit is contained in:
Dan Klishch 2023-10-01 23:26:56 -04:00 committed by Andrew Kaster
parent 867ce0df52
commit 6ed069ea8d
10 changed files with 226 additions and 33 deletions

View file

@ -4,8 +4,10 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "Parser/CppASTConverter.h"
#include <LibCore/File.h>
#include "Function.h"
#include "Parser/CppASTConverter.h"
#include "Parser/SpecParser.h"
namespace JSSpecCompiler {
@ -219,4 +221,32 @@ Tree CppASTConverter::as_possibly_empty_tree(Cpp::Statement const* statement)
return make_ref_counted<TreeList>(Vector<Tree> {});
}
CppParsingStep::CppParsingStep()
: CompilationStep("parser"sv)
{
}
CppParsingStep::~CppParsingStep() = default;
void CppParsingStep::run(TranslationUnitRef translation_unit)
{
auto filename = translation_unit->filename;
auto file = Core::File::open_file_or_standard_stream(filename, Core::File::OpenMode::Read).release_value_but_fixme_should_propagate_errors();
m_input = file->read_until_eof().release_value_but_fixme_should_propagate_errors();
Cpp::Preprocessor preprocessor { filename, m_input };
m_parser = adopt_own_if_nonnull(new Cpp::Parser { preprocessor.process_and_lex(), filename });
auto cpp_translation_unit = m_parser->parse();
VERIFY(m_parser->errors().is_empty());
for (auto const& declaration : cpp_translation_unit->declarations()) {
if (declaration->is_function()) {
auto const* cpp_function = AK::verify_cast<Cpp::FunctionDeclaration>(declaration.ptr());
translation_unit->adopt_function(CppASTConverter(cpp_function).convert());
}
}
}
}

View file

@ -6,9 +6,11 @@
#pragma once
#include <AK/OwnPtr.h>
#include <LibCpp/AST.h>
#include <LibCpp/Parser.h>
#include "Forward.h"
#include "CompilationPipeline.h"
namespace JSSpecCompiler {
@ -31,4 +33,16 @@ private:
RefPtr<Cpp::FunctionDeclaration> m_function;
};
class CppParsingStep : public CompilationStep {
public:
CppParsingStep();
~CppParsingStep();
void run(TranslationUnitRef translation_unit) override;
private:
OwnPtr<Cpp::Parser> m_parser;
ByteBuffer m_input;
};
}

View file

@ -5,7 +5,10 @@
*/
#include <AK/NonnullOwnPtr.h>
#include <LibCore/File.h>
#include <LibXML/Parser/Parser.h>
#include "Function.h"
#include "Parser/Lexer.h"
#include "Parser/SpecParser.h"
#include "Parser/TextParser.h"
@ -173,4 +176,31 @@ ParseErrorOr<void> SpecFunction::parse_definition(XML::Node const* element)
return {};
}
SpecParsingStep::SpecParsingStep()
: CompilationStep("parser"sv)
{
}
SpecParsingStep::~SpecParsingStep() = default;
void SpecParsingStep::run(TranslationUnitRef translation_unit)
{
auto filename = translation_unit->filename;
auto file = Core::File::open_file_or_standard_stream(filename, Core::File::OpenMode::Read).release_value_but_fixme_should_propagate_errors();
m_input = file->read_until_eof().release_value_but_fixme_should_propagate_errors();
XML::Parser parser { m_input };
auto document = parser.parse().release_value_but_fixme_should_propagate_errors();
m_document = AK::adopt_own_if_nonnull(new XML::Document(move(document)));
auto spec_function = SpecFunction::create(&m_document->root()).release_value_but_fixme_should_propagate_errors();
auto* function = translation_unit->adopt_function(
make_ref_counted<FunctionDefinition>(spec_function.m_name, spec_function.m_algorithm.m_tree));
for (auto const& argument : spec_function.m_arguments)
function->m_local_variables.set(argument.name, make_ref_counted<VariableDeclaration>(argument.name));
}
}

View file

@ -6,7 +6,10 @@
#pragma once
#include <AK/OwnPtr.h>
#include "AST/AST.h"
#include "CompilationPipeline.h"
#include "Forward.h"
#include "Parser/ParseError.h"
#include "Parser/Token.h"
@ -58,4 +61,16 @@ public:
Algorithm m_algorithm;
};
class SpecParsingStep : public CompilationStep {
public:
SpecParsingStep();
~SpecParsingStep();
void run(TranslationUnitRef translation_unit) override;
private:
OwnPtr<XML::Document> m_document;
ByteBuffer m_input;
};
}