blob: 9db686cea4ceb58b8210ddcd78854dfa9e8f9aba [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_location.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
enum Subkind : uint8_t {
kNone = 0,
#define KEYWORD(Name, Spelling) k##Name,
#include "fidl/"
#undef KEYWORD
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(SourceLocation previous_end, SourceLocation location, Kind kind, Subkind subkind)
: previous_end_(previous_end), location_(location), kind_and_subkind_(KindAndSubkind(kind, subkind)) {}
: Token(SourceLocation(), SourceLocation(), 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 KEYWORD(Name, Spelling) \
case Token::KindAndSubkind(Token::Kind::kIdentifier, Token::Subkind::k##Name).combined(): \
return #Spelling;
#include "fidl/"
#undef KEYWORD
return "<unknown token>";
std::string_view data() const { return; }
const SourceLocation& location() const { return location_; }
void set_previous_end(SourceLocation location) { previous_end_ = location; }
SourceLocation 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 location_ is
// somehow uninteresting to the parser (whitespace, comments, discarded
// braces, etc).
SourceLocation previous_end_;
SourceLocation location_;
KindAndSubkind kind_and_subkind_;
} // namespace fidl