Browse Source

Fixed 2 bugs

- Duplicate labels in a PROC now fail
- Bad token line indication when the token concludes a line
master
Ludovic 'Archivist' Lagouardette 1 year ago
parent
commit
85e7c18bf2
3 changed files with 17 additions and 8 deletions
  1. +7
    -0
      include/molasses/errors.h
  2. +4
    -4
      src/molasses/lexer.cpp
  3. +6
    -4
      src/molasses/parser_primitives.cpp

+ 7
- 0
include/molasses/errors.h View File

@ -61,6 +61,13 @@ namespace molasses {
"\tExpected ", expected, "\n", "\tFound ", found, "\n" "\tExpected ", expected, "\n", "\tFound ", found, "\n"
)) {} )) {}
}; };
struct duplicate_label_error : public parser_error {
duplicate_label_error(const symbol& sym, const std::string& found)
: parser_error(details::concatenate_builder(
"Unexpected label duplication\n", "\tAt ", sym.file_name, ":", sym.line, ":", sym.column, "\n",
"\tFound ", found, " duplicated label\n"
)) {}
};
struct expecting_token_error : public parser_error { struct expecting_token_error : public parser_error {
expecting_token_error(const std::string& expected, const std::string& context) expecting_token_error(const std::string& expected, const std::string& context)
: parser_error(details::concatenate_builder( : parser_error(details::concatenate_builder(

+ 4
- 4
src/molasses/lexer.cpp View File

@ -44,10 +44,6 @@ namespace molasses {
}; };
for(auto& character : source) { for(auto& character : source) {
if(character == '\n') {
line++;
column = 0;
}
column++; column++;
if(state == state_machine_t::string_escape) { if(state == state_machine_t::string_escape) {
switch(character) { switch(character) {
@ -79,6 +75,10 @@ namespace molasses {
} else { } else {
builder << character; builder << character;
} }
if(character == '\n') {
line++;
column = 0;
}
} else { } else {
if(state == state_machine_t::string_end) { if(state == state_machine_t::string_end) {
std::stringstream quoted; std::stringstream quoted;

+ 6
- 4
src/molasses/parser_primitives.cpp View File

@ -176,12 +176,11 @@ namespace molasses {
throw expecting_token_error(expected, context); \ throw expecting_token_error(expected, context); \
}}while(false) }}while(false)
decltype(it) last_valid;
decltype(it) last_valid = it;
if(*it != PROC_KW) { if(*it != PROC_KW) {
throw unexpected_token_error(*it, tokens.dictionary[*it],tokens.dictionary[PROC_KW]); throw unexpected_token_error(*it, tokens.dictionary[*it],tokens.dictionary[PROC_KW]);
} }
last_valid = it;
++it; ++it;
check_for_unexpected_stream_end("Procedure-Name", details::concatenate_builder("In top level, file ",last_valid->file_name, ":", last_valid->line, ":", last_valid->column)); check_for_unexpected_stream_end("Procedure-Name", details::concatenate_builder("In top level, file ",last_valid->file_name, ":", last_valid->line, ":", last_valid->column));
@ -227,8 +226,11 @@ namespace molasses {
if(*ahead == GOTO_KW) { if(*ahead == GOTO_KW) {
found_gotos[tokens.dictionary[*it]] = body.size(); found_gotos[tokens.dictionary[*it]] = body.size();
} else if(*ahead == LABEL_KW) { } else if(*ahead == LABEL_KW) {
found_labels[tokens.dictionary[*it]] = body.size();
// TODO: Handle duplicate labels
auto label_value = tokens.dictionary[*it];
if(found_labels.contains(label_value)) {
throw duplicate_label_error(*it, label_value);
}
found_labels[label_value] = body.size();
} }
body.emplace_back(*it); body.emplace_back(*it);
body.emplace_back(*ahead); body.emplace_back(*ahead);

Loading…
Cancel
Save