FLANG
token-sequence.h
1//===-- lib/Parser/token-sequence.h -----------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef FORTRAN_PARSER_TOKEN_SEQUENCE_H_
10#define FORTRAN_PARSER_TOKEN_SEQUENCE_H_
11
12// A buffer class capable of holding a contiguous sequence of characters
13// and a partitioning thereof into preprocessing tokens, along with their
14// associated provenances.
15
16#include "flang/Parser/char-block.h"
17#include "flang/Parser/provenance.h"
18#include <cstddef>
19#include <string>
20#include <utility>
21#include <vector>
22
23namespace llvm {
24class raw_ostream;
25}
26
27namespace Fortran::parser {
28
29class Messages;
30class Prescanner;
31
32// Buffers a contiguous sequence of characters that has been partitioned into
33// a sequence of preprocessing tokens with provenances.
34class TokenSequence {
35public:
36 TokenSequence() {}
37 TokenSequence(const TokenSequence &that) { CopyAll(that); }
38 TokenSequence(
39 const TokenSequence &that, std::size_t at, std::size_t count = 1) {
40 AppendRange(that, at, count);
41 }
42 TokenSequence(TokenSequence &&that)
43 : start_{std::move(that.start_)}, nextStart_{that.nextStart_},
44 char_{std::move(that.char_)},
45 provenances_{std::move(that.provenances_)} {}
46 TokenSequence(const std::string &s, Provenance p) { Put(s, p); }
47
48 TokenSequence &operator=(const TokenSequence &that) {
49 clear();
50 CopyAll(that);
51 return *this;
52 }
53 TokenSequence &operator=(TokenSequence &&that);
54 bool empty() const { return start_.empty(); }
55 void clear();
56 void pop_back();
57 void shrink_to_fit();
58 void swap(TokenSequence &);
59
60 std::size_t SizeInTokens() const { return start_.size(); }
61 std::size_t SizeInChars() const { return char_.size(); }
62
63 CharBlock ToCharBlock() const {
64 return char_.empty() ? CharBlock{} : CharBlock{&char_[0], char_.size()};
65 }
66 std::string ToString() const { return ToCharBlock().ToString(); }
67
68 CharBlock TokenAt(std::size_t token) const {
69 if (auto bytes{TokenBytes(token)}) {
70 return {&char_[start_.at(token)], bytes};
71 } else { // char_ could be empty
72 return {};
73 }
74 }
75 char CharAt(std::size_t j) const { return char_.at(j); }
76 CharBlock CurrentOpenToken() const {
77 return {&char_[nextStart_], char_.size() - nextStart_};
78 }
79
80 std::size_t SkipBlanks(std::size_t) const;
81 std::optional<std::size_t> SkipBlanksBackwards(std::size_t) const;
82
83 // True if anything remains in the sequence at & after the given offset
84 // except blanks and line-ending C++ and Fortran free-form comments.
85 bool IsAnythingLeft(std::size_t) const;
86
87 void PutNextTokenChar(char ch, Provenance provenance) {
88 char_.emplace_back(ch);
89 provenances_.Put({provenance, 1});
90 }
91
92 void CloseToken() {
93 start_.emplace_back(nextStart_);
94 nextStart_ = char_.size();
95 }
96
97 void ReopenLastToken() {
98 nextStart_ = start_.back();
99 start_.pop_back();
100 }
101
102 // These append characters with provenance and then close the token.
103 // When the last token of this sequence remains open beforehand,
104 // the new characters are appended to it.
105 void Put(const char *, std::size_t, Provenance);
106 void Put(const CharBlock &, Provenance);
107 void Put(const std::string &, Provenance);
108 void Put(llvm::raw_string_ostream &, Provenance);
109
110 // Appends a full copy of another sequence. When the last token of this
111 // sequence remains open beforehand, it is closed before the new text
112 // is appended.
113 void CopyAll(const TokenSequence &);
114 // Copies a range of tokens from another sequence. If the last token of
115 // this sequence remains open, the first token of the copied range will be
116 // appended to it.
117 void AppendRange(
118 const TokenSequence &, std::size_t at, std::size_t tokens = 1);
119 // Copies tokens (via Put above) with new provenance.
120 void CopyWithProvenance(const TokenSequence &, ProvenanceRange);
121
122 Provenance GetCharProvenance(std::size_t) const;
123 Provenance GetTokenProvenance(
124 std::size_t token, std::size_t offset = 0) const;
125 ProvenanceRange GetTokenProvenanceRange(
126 std::size_t token, std::size_t offset = 0) const;
127 ProvenanceRange GetIntervalProvenanceRange(
128 std::size_t token, std::size_t tokens = 1) const;
129 ProvenanceRange GetProvenanceRange() const;
130
131 char *GetMutableCharData() { return &char_[0]; }
132 TokenSequence &ToLowerCase();
133 bool HasBlanks(std::size_t firstChar = 0) const;
134 bool HasRedundantBlanks(std::size_t firstChar = 0) const;
135 TokenSequence &RemoveBlanks(std::size_t firstChar = 0);
136 TokenSequence &RemoveRedundantBlanks(std::size_t firstChar = 0);
137 TokenSequence &ClipComment(const Prescanner &, bool skipFirst = false);
138 const TokenSequence &CheckBadFortranCharacters(
139 Messages &, const Prescanner &, bool preprocessingOnly) const;
140 bool BadlyNestedParentheses() const;
141 const TokenSequence &CheckBadParentheses(Messages &) const;
142 void Emit(CookedSource &) const;
143 llvm::raw_ostream &Dump(llvm::raw_ostream &) const;
144
145private:
146 std::size_t TokenBytes(std::size_t token) const {
147 return (token + 1 >= start_.size() ? char_.size() : start_[token + 1]) -
148 start_[token];
149 }
150
151 std::vector<std::size_t> start_;
152 std::size_t nextStart_{0};
153 std::vector<char> char_;
154 OffsetToProvenanceMappings provenances_;
155};
156} // namespace Fortran::parser
157#endif // FORTRAN_PARSER_TOKEN_SEQUENCE_H_
Definition char-block.h:26
Definition provenance.h:233
Definition message.h:332
Definition prescan.h:34
Definition provenance.h:52
Definition check-expression.h:19