Skip to content

Commit

Permalink
[pp] Parse pp_token & text_line
Browse files Browse the repository at this point in the history
  • Loading branch information
LuisHsu committed Jan 21, 2025
1 parent cf7e28f commit bdecd79
Show file tree
Hide file tree
Showing 14 changed files with 248 additions and 211 deletions.
9 changes: 4 additions & 5 deletions src/exec/wasmvm-cc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,7 @@ int main(int argc, const char* argv[]){
// Preprocess
if(args["pp"]){
for(std::filesystem::path source_path : source_files){
std::ifstream fin(source_path);
PreProcessor pp(source_path, fin);
PreProcessor pp(source_path);
// if(args["output"]){
// std::ofstream output_file(std::get<std::string>(args["output"].value()));
// for(PreProcessor::PPToken tok = pp.get(); tok.has_value(); tok = pp.get()){
Expand Down Expand Up @@ -195,9 +194,9 @@ int main(int argc, const char* argv[]){
}
Linker linker(linker_config);

// }catch(Exception::Error &e){
// std::cerr << e.pos << ": " COLOR_Error ": " << e.what() << std::endl;
// return -1;
}catch(Exception::Error &e){
std::cerr << e.pos.file.filename().string() << ":" << e.pos.line << ":" << e.pos.col << ": " COLOR_Error ": " << e.what() << std::endl;
return -1;
}catch(Exception::Exception &e){
std::cerr << args.program.filename().string() << ": " COLOR_Error ": " << e.what() << std::endl;
return -1;
Expand Down
20 changes: 11 additions & 9 deletions src/include/Error.hpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
#ifndef WVMCC_Error_DEF
#define WVMCC_Error_DEF

#include <exception.hpp>
#include <exception>
#include <string>
#include <common.hpp>

namespace WasmVM {
namespace Exception {

// struct Error : public Exception {
// Error(SourcePos pos, std::string msg) : Exception(msg), pos(pos){}
// SourcePos pos;
// };
struct Error : public std::runtime_error {
Error(SourcePos pos, std::string msg) : std::runtime_error(msg), pos(pos){}
SourcePos pos;
};

// struct SyntaxError : public Error {
// SyntaxError(SourcePos pos, std::string msg) : Error(pos, "syntax error:" + msg){}
// };
struct SyntaxError : public Error {
SyntaxError(SourcePos pos, std::string msg) : Error(pos, "syntax error: " + msg){}
};

} // namespace WasmVM
} // namespace Exception
} // namespace WasmVM

#endif
27 changes: 19 additions & 8 deletions src/include/PreProcessor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,31 @@
#include <istream>
#include <sstream>
#include <filesystem>
#include <stack>
#include <deque>
#include <string>
#include <optional>
#include <common.hpp>

namespace WasmVM {

template <typename CharT, typename Traits = std::char_traits<CharT>>
struct PreProcessor : public std::basic_istream<CharT, Traits> {
struct PreProcessor {

PreProcessor(std::filesystem::path filepath, std::basic_istream<CharT, Traits>& stream)
: std::basic_istream<CharT, Traits>(&buf), filepath(filepath), stream(stream){}
struct Token {
enum {
HeaderName, Identifier, PPNumber, CharConst, StringLiteral, Punctuator, WhiteSpace, NewLine
} type;
std::string text;
SourcePos pos;
};

protected:
std::filesystem::path filepath;
std::basic_istream<CharT, Traits>& stream;
std::basic_stringbuf<CharT, Traits> buf;
PreProcessor(std::filesystem::path filepath);
std::optional<Token> get();

protected:
struct Visitor;
std::stack<std::shared_ptr<Visitor>> visitors;
std::deque<Token> buffer;
};

} // namespace WasmVM
Expand Down
144 changes: 0 additions & 144 deletions src/include/Token.hpp

This file was deleted.

16 changes: 16 additions & 0 deletions src/include/common.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef WVMCC_common_DEF
#define WVMCC_common_DEF

#include <filesystem>

namespace WasmVM {

struct SourcePos {
std::filesystem::path file;
size_t line;
size_t col;
};

} // namespace WasmVM

#endif
3 changes: 2 additions & 1 deletion src/lib/PreProcessor/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
find_package(Java 1.11 REQUIRED)
add_custom_command(OUTPUT PPLexer.cpp PPParser.cpp
COMMAND ${Java_JAVA_EXECUTABLE} -jar ${PROJECT_ROOT}/external/antlr4-4.13.3-dev-complete.jar -package WasmVM -no-visitor -no-listener -Xexact-output-dir -o ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_LIST_DIR}/PP.g4
COMMAND ${Java_JAVA_EXECUTABLE} -jar ${PROJECT_ROOT}/external/antlr4-4.13.3-dev-complete.jar -package WasmVM -visitor -no-listener -Xexact-output-dir -o ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_LIST_DIR}/PP.g4
DEPENDS ${CMAKE_CURRENT_LIST_DIR}/PPToken.g4 ${CMAKE_CURRENT_LIST_DIR}/PP.g4
)

Expand All @@ -14,6 +14,7 @@ add_library(pp
ErrorListener.cpp
PPLexer.cpp
PPParser.cpp
Visitor.cpp
)
target_link_libraries(pp antlr4-runtime)

Expand Down
6 changes: 2 additions & 4 deletions src/lib/PreProcessor/ErrorListener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@
#include <Error.hpp>
#include <PPParser.h>

void WasmVM::PPParseErrorListener::syntaxError(
void WasmVM::ParseErrorListener::syntaxError(
antlr4::Recognizer* recognizer,
antlr4::Token* offendingSymbol,
size_t line, size_t col,
const std::string &msg, std::exception_ptr
){
// Warning : not end with new_line
WasmVM::PPParser* parser = static_cast<WasmVM::PPParser*>(recognizer);
if((offendingSymbol->getType() == PPParser::EOF) && (parser->getRuleInvocationStack().front() == "text_line")){
Exception::Warning("source file should end with new-line");
}

}
2 changes: 1 addition & 1 deletion src/lib/PreProcessor/ErrorListener.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace WasmVM {

struct PPParseErrorListener : antlr4::BaseErrorListener {
struct ParseErrorListener : antlr4::BaseErrorListener {
void syntaxError(antlr4::Recognizer *recognizer, antlr4::Token * offendingSymbol, size_t line, size_t col, const std::string &msg, std::exception_ptr e) override;
};

Expand Down
2 changes: 1 addition & 1 deletion src/lib/PreProcessor/PP.g4
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ import PPToken;

group : text_line | EOF;

text_line : pp_token* NewLine;
text_line : pp_token* NewLine?;

pp_token : HeaderName | Identifier | PPNumber | CharConst | StringLiteral | Punctuator | BlockComment | LineComment | WhiteSpace;
21 changes: 21 additions & 0 deletions src/lib/PreProcessor/PreProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,25 @@

#include <PreProcessor.hpp>

#include "Visitor.hpp"

using namespace WasmVM;

PreProcessor::PreProcessor(std::filesystem::path filepath){
visitors.emplace(new Visitor(filepath, *this));
}

std::optional<PreProcessor::Token> PreProcessor::get(){
while(!visitors.empty() && buffer.empty()){
std::shared_ptr<Visitor> visitor = visitors.top();
if(!visitors.top()->fetch()){
visitors.pop();
}
}
if(visitors.empty() && buffer.empty()){
return std::nullopt;
}
Token token = buffer.front();
buffer.pop_front();
return token;
}
Loading

0 comments on commit bdecd79

Please sign in to comment.