2019-05-11 01:04:10 +02:00
|
|
|
// Copyright (c) 2018-2019, NVIDIA CORPORATION. All rights reserved.
|
2018-05-01 21:50:34 +02:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-02-16 20:42:17 +01:00
|
|
|
#ifndef FORTRAN_PARSER_TOKEN_SEQUENCE_H_
|
|
|
|
#define FORTRAN_PARSER_TOKEN_SEQUENCE_H_
|
2018-02-13 21:50:47 +01:00
|
|
|
|
|
|
|
// A buffer class capable of holding a contiguous sequence of characters
|
2018-03-23 23:14:52 +01:00
|
|
|
// and a partitioning thereof into preprocessing tokens, along with their
|
2018-03-20 18:59:07 +01:00
|
|
|
// associated provenances.
|
2018-02-13 21:50:47 +01:00
|
|
|
|
2018-03-20 18:59:07 +01:00
|
|
|
#include "char-block.h"
|
2018-02-13 21:50:47 +01:00
|
|
|
#include "provenance.h"
|
2018-03-20 18:59:07 +01:00
|
|
|
#include <cstddef>
|
2018-02-13 21:50:47 +01:00
|
|
|
#include <cstring>
|
2018-05-11 20:15:20 +02:00
|
|
|
#include <ostream>
|
2018-02-13 21:50:47 +01:00
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
2018-05-02 22:48:12 +02:00
|
|
|
namespace Fortran::parser {
|
2018-02-13 21:50:47 +01:00
|
|
|
|
|
|
|
// Buffers a contiguous sequence of characters that has been partitioned into
|
|
|
|
// a sequence of preprocessing tokens with provenances.
|
|
|
|
class TokenSequence {
|
|
|
|
public:
|
|
|
|
TokenSequence() {}
|
|
|
|
TokenSequence(const TokenSequence &that) { Put(that); }
|
2018-03-20 18:59:07 +01:00
|
|
|
TokenSequence(
|
|
|
|
const TokenSequence &that, std::size_t at, std::size_t count = 1) {
|
2018-02-13 21:50:47 +01:00
|
|
|
Put(that, at, count);
|
|
|
|
}
|
|
|
|
TokenSequence(TokenSequence &&that)
|
|
|
|
: start_{std::move(that.start_)}, nextStart_{that.nextStart_},
|
|
|
|
char_{std::move(that.char_)}, provenances_{std::move(that.provenances_)} {
|
|
|
|
}
|
|
|
|
TokenSequence(const std::string &s, Provenance p) { Put(s, p); }
|
|
|
|
|
|
|
|
TokenSequence &operator=(const TokenSequence &that) {
|
|
|
|
clear();
|
|
|
|
Put(that);
|
|
|
|
return *this;
|
|
|
|
}
|
2018-05-11 20:32:10 +02:00
|
|
|
TokenSequence &operator=(TokenSequence &&that);
|
2018-02-13 21:50:47 +01:00
|
|
|
bool empty() const { return start_.empty(); }
|
|
|
|
void clear();
|
|
|
|
void pop_back();
|
|
|
|
void shrink_to_fit();
|
2018-05-11 20:32:10 +02:00
|
|
|
void swap(TokenSequence &);
|
2018-02-13 21:50:47 +01:00
|
|
|
|
2018-03-23 23:14:52 +01:00
|
|
|
std::size_t SizeInTokens() const { return start_.size(); }
|
|
|
|
std::size_t SizeInChars() const { return char_.size(); }
|
|
|
|
|
|
|
|
CharBlock ToCharBlock() const { return {&char_[0], char_.size()}; }
|
|
|
|
std::string ToString() const { return ToCharBlock().ToString(); }
|
|
|
|
|
|
|
|
CharBlock TokenAt(std::size_t token) const {
|
|
|
|
return {&char_[start_.at(token)], TokenBytes(token)};
|
|
|
|
}
|
|
|
|
char CharAt(std::size_t j) const { return char_.at(j); }
|
|
|
|
|
2018-05-11 21:54:49 +02:00
|
|
|
std::size_t SkipBlanks(std::size_t) const;
|
|
|
|
|
2018-02-13 21:50:47 +01:00
|
|
|
void PutNextTokenChar(char ch, Provenance provenance) {
|
|
|
|
char_.emplace_back(ch);
|
|
|
|
provenances_.Put({provenance, 1});
|
|
|
|
}
|
|
|
|
|
|
|
|
void CloseToken() {
|
|
|
|
start_.emplace_back(nextStart_);
|
|
|
|
nextStart_ = char_.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReopenLastToken() {
|
|
|
|
nextStart_ = start_.back();
|
|
|
|
start_.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Put(const TokenSequence &);
|
2018-02-15 22:13:28 +01:00
|
|
|
void Put(const TokenSequence &, ProvenanceRange);
|
2018-03-20 18:59:07 +01:00
|
|
|
void Put(const TokenSequence &, std::size_t at, std::size_t tokens = 1);
|
|
|
|
void Put(const char *, std::size_t, Provenance);
|
|
|
|
void Put(const CharBlock &, Provenance);
|
2018-02-13 21:50:47 +01:00
|
|
|
void Put(const std::string &, Provenance);
|
|
|
|
void Put(const std::stringstream &, Provenance);
|
2019-05-11 01:04:10 +02:00
|
|
|
|
2018-03-20 18:59:07 +01:00
|
|
|
Provenance GetTokenProvenance(
|
|
|
|
std::size_t token, std::size_t offset = 0) const;
|
2018-02-15 22:13:28 +01:00
|
|
|
ProvenanceRange GetTokenProvenanceRange(
|
2018-03-20 18:59:07 +01:00
|
|
|
std::size_t token, std::size_t offset = 0) const;
|
2018-02-15 22:13:28 +01:00
|
|
|
ProvenanceRange GetIntervalProvenanceRange(
|
2018-03-20 18:59:07 +01:00
|
|
|
std::size_t token, std::size_t tokens = 1) const;
|
2018-02-15 22:13:28 +01:00
|
|
|
ProvenanceRange GetProvenanceRange() const;
|
2018-02-13 21:50:47 +01:00
|
|
|
|
2018-03-23 23:14:52 +01:00
|
|
|
char *GetMutableCharData() { return &char_[0]; }
|
|
|
|
TokenSequence &ToLowerCase();
|
2018-05-11 21:44:56 +02:00
|
|
|
bool HasBlanks(std::size_t firstChar = 0) const;
|
|
|
|
bool HasRedundantBlanks(std::size_t firstChar = 0) const;
|
|
|
|
TokenSequence &RemoveBlanks(std::size_t firstChar = 0);
|
|
|
|
TokenSequence &RemoveRedundantBlanks(std::size_t firstChar = 0);
|
2019-05-11 01:04:10 +02:00
|
|
|
TokenSequence &ClipComment();
|
2018-05-11 20:15:20 +02:00
|
|
|
void Emit(CookedSource &) const;
|
|
|
|
void Dump(std::ostream &) const;
|
2018-03-20 18:59:07 +01:00
|
|
|
|
2018-02-13 21:50:47 +01:00
|
|
|
private:
|
2018-03-20 18:59:07 +01:00
|
|
|
std::size_t TokenBytes(std::size_t token) const {
|
2018-02-13 21:50:47 +01:00
|
|
|
return (token + 1 >= start_.size() ? char_.size() : start_[token + 1]) -
|
|
|
|
start_[token];
|
|
|
|
}
|
|
|
|
|
2018-03-20 18:59:07 +01:00
|
|
|
std::vector<std::size_t> start_;
|
|
|
|
std::size_t nextStart_{0};
|
2018-02-13 21:50:47 +01:00
|
|
|
std::vector<char> char_;
|
|
|
|
OffsetToProvenanceMappings provenances_;
|
|
|
|
};
|
2018-10-25 14:55:23 +02:00
|
|
|
}
|
2018-02-16 20:42:17 +01:00
|
|
|
#endif // FORTRAN_PARSER_TOKEN_SEQUENCE_H_
|