1 //===-- lib/Parser/token-sequence.h -----------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef FORTRAN_PARSER_TOKEN_SEQUENCE_H_
10 #define FORTRAN_PARSER_TOKEN_SEQUENCE_H_
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.
16 #include "flang/Parser/char-block.h"
17 #include "flang/Parser/provenance.h"
28 namespace Fortran::parser
{
32 // Buffers a contiguous sequence of characters that has been partitioned into
33 // a sequence of preprocessing tokens with provenances.
37 TokenSequence(const TokenSequence
&that
) { Put(that
); }
39 const TokenSequence
&that
, std::size_t at
, std::size_t count
= 1) {
42 TokenSequence(TokenSequence
&&that
)
43 : start_
{std::move(that
.start_
)}, nextStart_
{that
.nextStart_
},
44 char_
{std::move(that
.char_
)}, provenances_
{
45 std::move(that
.provenances_
)} {}
46 TokenSequence(const std::string
&s
, Provenance p
) { Put(s
, p
); }
48 TokenSequence
&operator=(const TokenSequence
&that
) {
53 TokenSequence
&operator=(TokenSequence
&&that
);
54 bool empty() const { return start_
.empty(); }
58 void swap(TokenSequence
&);
60 std::size_t SizeInTokens() const { return start_
.size(); }
61 std::size_t SizeInChars() const { return char_
.size(); }
63 CharBlock
ToCharBlock() const { return {&char_
[0], char_
.size()}; }
64 std::string
ToString() const { return ToCharBlock().ToString(); }
66 CharBlock
TokenAt(std::size_t token
) const {
67 return {&char_
[start_
.at(token
)], TokenBytes(token
)};
69 char CharAt(std::size_t j
) const { return char_
.at(j
); }
70 CharBlock
CurrentOpenToken() const {
71 return {&char_
[nextStart_
], char_
.size() - nextStart_
};
74 std::size_t SkipBlanks(std::size_t) const;
76 // True if anything remains in the sequence at & after the given offset
77 // except blanks and line-ending C++ and Fortran free-form comments.
78 bool IsAnythingLeft(std::size_t) const;
80 void PutNextTokenChar(char ch
, Provenance provenance
) {
81 char_
.emplace_back(ch
);
82 provenances_
.Put({provenance
, 1});
86 start_
.emplace_back(nextStart_
);
87 nextStart_
= char_
.size();
90 void ReopenLastToken() {
91 nextStart_
= start_
.back();
95 void Put(const TokenSequence
&);
96 void Put(const TokenSequence
&, ProvenanceRange
);
97 void Put(const TokenSequence
&, std::size_t at
, std::size_t tokens
= 1);
98 void Put(const char *, std::size_t, Provenance
);
99 void Put(const CharBlock
&, Provenance
);
100 void Put(const std::string
&, Provenance
);
101 void Put(llvm::raw_string_ostream
&, Provenance
);
103 Provenance
GetCharProvenance(std::size_t) const;
104 Provenance
GetTokenProvenance(
105 std::size_t token
, std::size_t offset
= 0) const;
106 ProvenanceRange
GetTokenProvenanceRange(
107 std::size_t token
, std::size_t offset
= 0) const;
108 ProvenanceRange
GetIntervalProvenanceRange(
109 std::size_t token
, std::size_t tokens
= 1) const;
110 ProvenanceRange
GetProvenanceRange() const;
112 char *GetMutableCharData() { return &char_
[0]; }
113 TokenSequence
&ToLowerCase();
114 bool HasBlanks(std::size_t firstChar
= 0) const;
115 bool HasRedundantBlanks(std::size_t firstChar
= 0) const;
116 TokenSequence
&RemoveBlanks(std::size_t firstChar
= 0);
117 TokenSequence
&RemoveRedundantBlanks(std::size_t firstChar
= 0);
118 TokenSequence
&ClipComment(bool skipFirst
= false);
119 const TokenSequence
&CheckBadFortranCharacters(Messages
&) const;
120 const TokenSequence
&CheckBadParentheses(Messages
&) const;
121 void Emit(CookedSource
&) const;
122 llvm::raw_ostream
&Dump(llvm::raw_ostream
&) const;
125 std::size_t TokenBytes(std::size_t token
) const {
126 return (token
+ 1 >= start_
.size() ? char_
.size() : start_
[token
+ 1]) -
130 std::vector
<std::size_t> start_
;
131 std::size_t nextStart_
{0};
132 std::vector
<char> char_
;
133 OffsetToProvenanceMappings provenances_
;
135 } // namespace Fortran::parser
136 #endif // FORTRAN_PARSER_TOKEN_SEQUENCE_H_