实现int Scanner::process_identifier_table(int index);
This commit is contained in:
parent
b3da0e4487
commit
7eb6630248
@ -19,9 +19,7 @@ file(GLOB TEST_SOURCES ${CMAKE_SOURCE_DIR}/unit/*.cpp)
|
||||
|
||||
foreach(TEST_SOURCE ${TEST_SOURCES})
|
||||
get_filename_component(TEST_NAME ${TEST_SOURCE} NAME_WE)
|
||||
add_executable(${TEST_NAME} ${TEST_SOURCE} ${SOURCES}
|
||||
include/scanner.h
|
||||
include/Tbs.h)
|
||||
add_executable(${TEST_NAME} ${TEST_SOURCE} ${SOURCES})
|
||||
add_test(NAME ${TEST_NAME} COMMAND ${TEST_NAME})
|
||||
endforeach(TEST_SOURCE ${TEST_SOURCES})
|
||||
|
||||
|
@ -22,11 +22,13 @@ public:
|
||||
i += len - 1;
|
||||
len = 0;
|
||||
} else if (len = process_key_table(i)) {
|
||||
i += len - 1;
|
||||
i += len - 1;
|
||||
len = 0;
|
||||
} else if (len = process_punct_table(i)) {
|
||||
i += len - 1;
|
||||
len = 0;
|
||||
} else if (m_source_code[i] == ' ' || m_source_code[i] == '\t' || m_source_code[i] == '\n') {
|
||||
continue;
|
||||
} else {
|
||||
std::cerr << "Error: Tokenize" << std::endl;
|
||||
exit(0);
|
||||
@ -50,4 +52,7 @@ private:
|
||||
std::vector<Token> m_token_list;
|
||||
Tbs m_tables;
|
||||
int index;
|
||||
|
||||
// 记录标识符表的索引
|
||||
int identifier_index = 0;
|
||||
};
|
@ -1,15 +1,40 @@
|
||||
#include "Scanner.h"
|
||||
using std::cout;
|
||||
#include <sstream>
|
||||
|
||||
int Scanner::process_const_table(int index) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Scanner::process_identifier_table(int index) {
|
||||
std::stringstream buffer;
|
||||
int old_index = index;
|
||||
if (std::isalpha(m_source_code[index])) {
|
||||
buffer << m_source_code[index];
|
||||
index += 1;
|
||||
while(std::isalnum(m_source_code[index])) {
|
||||
buffer << m_source_code[index];
|
||||
index += 1;
|
||||
}
|
||||
|
||||
std::string identifier = buffer.str();
|
||||
for (const auto& key : m_tables.KeyTable) {
|
||||
if (identifier == key.second) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
m_tables.IdTable.insert({identifier_index, identifier});
|
||||
m_token_list.push_back(Token{identifier_index, ID_TABLE});
|
||||
identifier_index++;
|
||||
return index - old_index;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int Scanner::process_key_table(int index) {
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Scanner::process_punct_table(int index) {
|
||||
|
@ -6,6 +6,17 @@
|
||||
#include <vector>
|
||||
using std::string,std::vector;
|
||||
|
||||
TEST_CASE("Scanner test identifier table") {
|
||||
Tbs tables;
|
||||
std::string src = "abcvljl laadfs fafarwrw";
|
||||
Scanner scan(src, tables);
|
||||
scan.scan();
|
||||
|
||||
for (auto value_src: scan.get_token_list()) {
|
||||
std::cout << value_src.id << " " << value_src.type << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
TEST_CASE("Scanner test Punct table") {
|
||||
Tbs tables = {};
|
||||
|
Loading…
Reference in New Issue
Block a user