Skip to content

Commit

Permalink
Finish VersionReq
Browse files Browse the repository at this point in the history
  • Loading branch information
ken-matsui committed Jan 1, 2024
1 parent 070e0bf commit 8cc9169
Show file tree
Hide file tree
Showing 3 changed files with 292 additions and 29 deletions.
32 changes: 16 additions & 16 deletions src/Semver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ std::ostream& operator<<(std::ostream& os, const VersionToken& tok) noexcept {
os << '+';
break;
case VersionToken::Eof:
case VersionToken::Unknown:
break;
}
return os;
Expand Down Expand Up @@ -55,6 +56,7 @@ bool operator==(const VersionToken& lhs, const VersionToken& rhs) noexcept {
case VersionToken::Hyphen:
case VersionToken::Plus:
case VersionToken::Eof:
case VersionToken::Unknown:
return true;
}
return false;
Expand All @@ -71,10 +73,12 @@ bool operator>(const VersionToken& lhs, const VersionToken& rhs) {
}

static String carets(const VersionToken& tok) noexcept {
if (tok.kind != VersionToken::Eof) {
return String(tok.size(), '^');
} else {
return "^";
switch (tok.kind) {
case VersionToken::Eof:
case VersionToken::Unknown:
return "^";
default:
return String(tok.size(), '^');
}
}

Expand Down Expand Up @@ -255,17 +259,14 @@ bool VersionLexer::isEof() const noexcept {
return pos >= s.size();
}

void VersionLexer::step() {
if (isEof()) {
throw SemverException(s, '\n', String(pos, ' '), "^ unexpected eof");
}
void VersionLexer::step() noexcept {
++pos;
}

VersionToken VersionLexer::consumeIdent() {
VersionToken VersionLexer::consumeIdent() noexcept {
usize len = 0;
while (pos < s.size() && (std::isalnum(s[pos]) || s[pos] == '-')) {
++pos;
step();
++len;
}
return {VersionToken::Ident, StringRef(s.data() + pos - len, len)};
Expand All @@ -290,7 +291,7 @@ VersionToken VersionLexer::consumeNum() {
}

value = value * 10 + digit;
++pos;
step();
++len;
}
return {VersionToken::Num, value};
Expand All @@ -304,7 +305,7 @@ VersionToken VersionLexer::consumeNumOrIdent() {
if (!std::isdigit(s[pos])) {
isIdent = true;
}
++pos;
step();
}

pos = oldPos;
Expand Down Expand Up @@ -335,9 +336,8 @@ VersionToken VersionLexer::next() {
step();
return VersionToken{VersionToken::Plus};
} else {
throw SemverException(
s, '\n', String(pos, ' '), "^ unexpected character: `", c, '`'
);
step();
return VersionToken{VersionToken::Unknown};
}
}

Expand Down Expand Up @@ -481,7 +481,7 @@ void test_parse() {
Version::parse(" "), SemverException,
"invalid semver:\n"
" \n"
"^ unexpected character: ` `"
"^ expected number"
);
ASSERT_EXCEPTION(
Version::parse("1"), SemverException,
Expand Down
5 changes: 3 additions & 2 deletions src/Semver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ struct VersionToken {
Hyphen, // -
Plus, // +
Eof,
Unknown,
};

Kind kind;
Expand Down Expand Up @@ -86,8 +87,8 @@ struct VersionLexer {
explicit VersionLexer(StringRef s) : s(s), pos(0) {}

bool isEof() const noexcept;
void step();
VersionToken consumeIdent();
void step() noexcept;
VersionToken consumeIdent() noexcept;
VersionToken consumeNum();
VersionToken consumeNumOrIdent();
VersionToken next();
Expand Down
Loading

0 comments on commit 8cc9169

Please sign in to comment.