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 { ...@@ -78,11 +78,11 @@ namespace {
typedef p::Span<uint32_t> Location; typedef p::Span<uint32_t> Location;
Token::Body::Builder initTok(Orphan<Token>& t, const Location& loc) { Token::Builder initTok(Orphan<Token>& t, const Location& loc) {
auto tb = t.get(); auto builder = t.get();
tb.setStartByte(loc.begin()); builder.setStartByte(loc.begin());
tb.setEndByte(loc.end()); builder.setEndByte(loc.end());
return tb.getBody(); return builder;
} }
void buildTokenSequenceList(List<List<Token>>::Builder builder, void buildTokenSequenceList(List<List<Token>>::Builder builder,
......
...@@ -28,18 +28,18 @@ using Cxx = import "/capnp/c++.capnp"; ...@@ -28,18 +28,18 @@ using Cxx = import "/capnp/c++.capnp";
$Cxx.namespace("capnp::compiler"); $Cxx.namespace("capnp::compiler");
struct Token { struct Token {
body @0 union { union {
identifier @1 :Text; identifier @0 :Text;
stringLiteral @2 :Text; stringLiteral @1 :Text;
integerLiteral @3 :UInt64; integerLiteral @2 :UInt64;
floatLiteral @4 :Float64; floatLiteral @3 :Float64;
operator @5 :Text; operator @4 :Text;
parenthesizedList @6 :List(List(Token)); parenthesizedList @5 :List(List(Token));
bracketedList @7 :List(List(Token)); bracketedList @6 :List(List(Token));
} }
startByte @8 :UInt32; startByte @7 :UInt32;
endByte @9 :UInt32; endByte @8 :UInt32;
} }
struct Statement { struct Statement {
......
This diff is collapsed.
This diff is collapsed.
...@@ -186,12 +186,11 @@ struct Located { ...@@ -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 { struct MatchTokenType {
kj::Maybe<Located<T>> operator()(Token::Reader token) const { kj::Maybe<Located<T>> operator()(Token::Reader token) const {
auto body = token.getBody(); if (token.which() == type) {
if (body.which() == type) { return Located<T>((token.*get)(), token.getStartByte(), token.getEndByte());
return Located<T>((body.*get)(), token.getStartByte(), token.getEndByte());
} else { } else {
return nullptr; return nullptr;
} }
...@@ -200,7 +199,7 @@ struct MatchTokenType { ...@@ -200,7 +199,7 @@ struct MatchTokenType {
#define TOKEN_TYPE_PARSER(type, discrim, getter) \ #define TOKEN_TYPE_PARSER(type, discrim, getter) \
p::transformOrReject(p::any, \ 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 identifier = TOKEN_TYPE_PARSER(Text::Reader, IDENTIFIER, getIdentifier);
constexpr auto stringLiteral = TOKEN_TYPE_PARSER(Text::Reader, STRING_LITERAL, getStringLiteral); 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