From e398a47fc9ef8ded356c56b5739808ce1192e3d2 Mon Sep 17 00:00:00 2001
From: Ferenc Szontágh <szf@fsociety.hu>
Date: Fri, 18 Apr 2025 05:37:07 +0000
Subject: [PATCH] fix syntax, add expected to errors
---
src/Parser/Parser.cpp | 3 ++-
test_scripts/test1.vs | 3 ---
src/Parser/Parser.hpp | 18 ++++++++++++------
test_scripts/test2.vs | 2 --
README.md | 2 +-
5 files changed, 15 insertions(+), 13 deletions(-)
diff --git a/README.md b/README.md
index d9a4cc3..3651d73 100644
--- a/README.md
+++ b/README.md
@@ -30,7 +30,7 @@
string name = "VoidScript";
// Functions
-function $add(int $a, double $b, string $help) {
+function add = (int $a, double $b, string $help) {
int $result = $a + $b;
print("The sum is: ", $result, "\n");
print("Help: ", $help, "\n");
diff --git a/src/Parser/Parser.cpp b/src/Parser/Parser.cpp
index 37c2081..38ff418 100644
--- a/src/Parser/Parser.cpp
+++ b/src/Parser/Parser.cpp
@@ -175,7 +175,8 @@
if (startIt != tokens_.end() && endIt != tokens_.end() && startIt < endIt) {
filtered_tokens = std::vector<Lexer::Tokens::Token>(startIt + 1, endIt);
}
- std::string_view input_string = input_str_view_.substr(opening_brace.end_pos, closing_brace.end_pos);
+ auto len = closing_brace.start_pos - opening_brace.end_pos;
+ std::string_view input_string = input_str_view_.substr(opening_brace.end_pos, len);
current_token_index_ = tokenIndex;
expect(Lexer::Tokens::Type::PUNCTUATION, "}");
diff --git a/src/Parser/Parser.hpp b/src/Parser/Parser.hpp
index 9956e7d..92461bb 100644
--- a/src/Parser/Parser.hpp
+++ b/src/Parser/Parser.hpp
@@ -22,15 +22,21 @@
public:
using BaseException::BaseException;
- Exception(const std::string & msg, const Lexer::Tokens::Token & token) {
+ Exception(const std::string & msg, const std::string & expected, const Lexer::Tokens::Token & token) {
rawMessage_ = msg + ": " + token.dump();
context_ =
" at line: " + std::to_string(token.line_number) + ", column: " + std::to_string(token.column_number);
+ if (expected.empty() == false) {
+ rawMessage_ += " (expected: " + expected + ")";
+ }
formattedMessage_ = formatMessage();
}
- Exception(const std::string & msg, int line, int col) {
- rawMessage_ = msg;
+ Exception(const std::string & msg, std::string & expected, int line, int col) {
+ rawMessage_ = msg;
+ if (expected.empty() == false) {
+ rawMessage_ += " (expected: " + expected + ")";
+ }
context_ = " at line: " + std::to_string(line) + ", column: " + std::to_string(col);
formattedMessage_ = formatMessage();
}
@@ -146,13 +152,13 @@
(current_token_index_ == tokens_.size() - 1 && tokens_.back().type == Lexer::Tokens::Type::END_OF_FILE);
}
- [[noreturn]] void reportError(const std::string & message) {
+ [[noreturn]] void reportError(const std::string & message, const std::string expected = "") {
if (current_token_index_ < tokens_.size()) {
- throw Exception(message, tokens_[current_token_index_]);
+ throw Exception(message, expected, tokens_[current_token_index_]);
}
int line = tokens_.empty() ? 0 : tokens_.back().line_number;
int col = tokens_.empty() ? 0 : tokens_.back().column_number;
- throw Exception(message, line, col);
+ throw Exception(message, expected, line, col);
}
// parseStatement (változatlan)
diff --git a/test_scripts/test1.vs b/test_scripts/test1.vs
index ca2de5f..e6cefac 100644
--- a/test_scripts/test1.vs
+++ b/test_scripts/test1.vs
@@ -1,4 +1,3 @@
-<?void
string $name = "World 😀"; # world test
string $greeting = "Hello ";
string $smiley = "😀 = \\U0001F600 = \U0001F600\n";
@@ -10,5 +9,3 @@
print("The number2: ", $number2, "\n");
print("Unicode: \u00E9 \U0001F600, hex: \x41, newline:\nEnd\t",$greeting, $name, "\n\nSmiley test: ", $smiley);
-
-?>
\ No newline at end of file
diff --git a/test_scripts/test2.vs b/test_scripts/test2.vs
index 6e43991..ab60178 100644
--- a/test_scripts/test2.vs
+++ b/test_scripts/test2.vs
@@ -1,5 +1,3 @@
-<?void
-
int $num = 123;
double $double = 12.3;
string $variable = "This is a string content with a number: 123";
--
Gitblit v1.9.3