Initial work on parsing.
This commit is contained in:
@@ -12,7 +12,7 @@ namespace MalachScript::Parser {
|
||||
auto* last = first;
|
||||
while (true) {
|
||||
auto* next = LexNext();
|
||||
last->_next = std::unique_ptr<const LexToken>(next);
|
||||
last->SetNext(next);
|
||||
last = next;
|
||||
if (next->GetKind() == LexTokenKind::EndOfFile) {
|
||||
break;
|
||||
@@ -459,8 +459,21 @@ namespace MalachScript::Parser {
|
||||
}
|
||||
|
||||
static uint32_t constexpr Hash(const char8_t* input) {
|
||||
return *input != 0U ? static_cast<uint32_t>(*input) + 33 * Hash(input + 1) : 5381;
|
||||
if (*input != 0U) {
|
||||
return static_cast<uint32_t>(*input) + 33 * Hash(input + 1);
|
||||
} else {
|
||||
return 5381;
|
||||
}
|
||||
};
|
||||
static uint32_t HashStringView(const std::u8string_view& sv){
|
||||
auto init = 5381;
|
||||
for (auto it = sv.rbegin(); it != sv.rend(); ++it)
|
||||
{
|
||||
init *= 33;
|
||||
init += static_cast<uint32_t>(*it);
|
||||
}
|
||||
return init;
|
||||
}
|
||||
|
||||
LexToken* Lexer::LexKeywordOrIdentifier() {
|
||||
auto start = _position;
|
||||
@@ -468,9 +481,9 @@ namespace MalachScript::Parser {
|
||||
while (IsAlphaNumericalOrUnderscore(Peek(offset))) {
|
||||
offset++;
|
||||
}
|
||||
auto str = std::u8string(_script.substr(start, offset));
|
||||
auto str = _script.substr(start, offset);
|
||||
Progress(offset - 1);
|
||||
switch (Hash(str.c_str())) {
|
||||
switch (HashStringView(str)) {
|
||||
case Hash(u8"and"): return Create<LexTokenImpl<LexTokenKind::AndKeyword>>(TextSpan(start, _position));
|
||||
case Hash(u8"abstract"):
|
||||
return Create<LexTokenImpl<LexTokenKind::AbstractKeyword>>(TextSpan(start, _position));
|
||||
|
||||
Reference in New Issue
Block a user