Commit d5eba005 authored by Kenton Varda's avatar Kenton Varda

Refactor lexer.capnp to use unnamed unions.

parent 7b8bbc87
This diff is collapsed.
......@@ -78,11 +78,11 @@ namespace {
typedef p::Span<uint32_t> Location;
Token::Body::Builder initTok(Orphan<Token>& t, const Location& loc) {
auto tb = t.get();
tb.setStartByte(loc.begin());
tb.setEndByte(loc.end());
return tb.getBody();
Token::Builder initTok(Orphan<Token>& t, const Location& loc) {
auto builder = t.get();
builder.setStartByte(loc.begin());
builder.setEndByte(loc.end());
return builder;
}
void buildTokenSequenceList(List<List<Token>>::Builder builder,
......
......@@ -28,18 +28,18 @@ using Cxx = import "/capnp/c++.capnp";
$Cxx.namespace("capnp::compiler");
struct Token {
body @0 union {
identifier @1 :Text;
stringLiteral @2 :Text;
integerLiteral @3 :UInt64;
floatLiteral @4 :Float64;
operator @5 :Text;
parenthesizedList @6 :List(List(Token));
bracketedList @7 :List(List(Token));
union {
identifier @0 :Text;
stringLiteral @1 :Text;
integerLiteral @2 :UInt64;
floatLiteral @3 :Float64;
operator @4 :Text;
parenthesizedList @5 :List(List(Token));
bracketedList @6 :List(List(Token));
}
startByte @8 :UInt32;
endByte @9 :UInt32;
startByte @7 :UInt32;
endByte @8 :UInt32;
}
struct Statement {
......
This diff is collapsed.
This diff is collapsed.
......@@ -186,12 +186,11 @@ struct Located {
// =======================================================================================
template <typename T, Token::Body::Which type, T (Token::Body::Reader::*get)() const>
template <typename T, Token::Which type, T (Token::Reader::*get)() const>
struct MatchTokenType {
kj::Maybe<Located<T>> operator()(Token::Reader token) const {
auto body = token.getBody();
if (body.which() == type) {
return Located<T>((body.*get)(), token.getStartByte(), token.getEndByte());
if (token.which() == type) {
return Located<T>((token.*get)(), token.getStartByte(), token.getEndByte());
} else {
return nullptr;
}
......@@ -200,7 +199,7 @@ struct MatchTokenType {
#define TOKEN_TYPE_PARSER(type, discrim, getter) \
p::transformOrReject(p::any, \
MatchTokenType<type, Token::Body::discrim, &Token::Body::Reader::getter>())
MatchTokenType<type, Token::discrim, &Token::Reader::getter>())
constexpr auto identifier = TOKEN_TYPE_PARSER(Text::Reader, IDENTIFIER, getIdentifier);
constexpr auto stringLiteral = TOKEN_TYPE_PARSER(Text::Reader, STRING_LITERAL, getStringLiteral);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment