blob: 3d7e96224c2a930b1c399da170044f54923c5af1 [file] [log] [blame]
// Copyright 2017 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stdint.h>
#include <string_view>
#include "source_span.h"
namespace fidl {
// A Token represents a typed view into a source buffer. That is, it
// has a TokenKind, and it has a buffer representing the data
// corresponding to the token. No processing is done on the data:
// string or numeric literals aren't further parsed, identifiers
// uniqued, and so on.
class Token {
enum Kind : uint8_t {
#define TOKEN(Name) k##Name,
#include "fidl/"
#undef TOKEN
// Each identifier token is assigned a subkind, which are defined below, or a sentinel value if
// the token does not match one of the subkinds, to make it simple for the parser to either
// consume a generic identifier or one that matches a specific subkind. This design makes it so
// that there are no "reserved keywords" and any identifier can be defined by the user.
enum Subkind : uint8_t {
kNone = 0,
#define TOKEN_SUBKIND(Name, Spelling) k##Name,
#include "fidl/"
class KindAndSubkind {
constexpr KindAndSubkind(Kind kind, Subkind subkind) : kind_(kind), subkind_(subkind) {}
constexpr Kind kind() const { return kind_; }
constexpr Subkind subkind() const { return subkind_; }
constexpr uint16_t combined() const { return uint16_t(kind_) | uint16_t(subkind_ << 8); }
Kind kind_;
Subkind subkind_;
Token(SourceSpan previous_end, SourceSpan span, Kind kind, Subkind subkind)
: previous_end_(previous_end),
kind_and_subkind_(KindAndSubkind(kind, subkind)) {}
Token() : Token(SourceSpan(), SourceSpan(), Token::Kind::kNotAToken, Token::Subkind::kNone) {}
static const char* Name(KindAndSubkind kind_and_subkind) {
switch (kind_and_subkind.combined()) {
#define TOKEN(Name) \
case Token::Kind::k##Name: \
return #Name;
#include "fidl/"
#undef TOKEN
#define TOKEN_SUBKIND(Name, Spelling) \
case Token::KindAndSubkind(Token::Kind::kIdentifier, Token::Subkind::k##Name).combined(): \
return #Spelling;
#include "fidl/"
return "<unknown token>";
std::string_view data() const { return; }
const SourceSpan& span() const { return span_; }
void set_previous_end(SourceSpan span) { previous_end_ = span; }
SourceSpan previous_end() const { return previous_end_; }
Kind kind() const { return kind_and_subkind_.kind(); }
Subkind subkind() const { return kind_and_subkind_.subkind(); }
KindAndSubkind kind_and_subkind() const { return kind_and_subkind_; }
// The end of the previous token. Everything between this and span_ is
// somehow uninteresting to the parser (whitespace, comments, discarded
// braces, etc).
SourceSpan previous_end_;
SourceSpan span_;
KindAndSubkind kind_and_subkind_;
} // namespace fidl