2018-05-01 21:50:34 +02:00
|
|
|
// Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
|
|
|
|
//
|
|
|
|
// 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_SOURCE_H_
|
|
|
|
#define FORTRAN_PARSER_SOURCE_H_
|
2018-01-30 20:55:03 +01:00
|
|
|
|
|
|
|
// Source file content is lightly normalized when the file is read.
|
|
|
|
// - Line ending markers are converted to single newline characters
|
|
|
|
// - A newline character is added to the last line of the file if one is needed
|
|
|
|
|
2018-03-20 18:59:07 +01:00
|
|
|
#include <cstddef>
|
2018-01-30 20:55:03 +01:00
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
2018-02-09 23:04:11 +01:00
|
|
|
#include <utility>
|
2018-02-07 22:18:36 +01:00
|
|
|
#include <vector>
|
2018-01-30 20:55:03 +01:00
|
|
|
|
2018-05-02 22:48:12 +02:00
|
|
|
namespace Fortran::parser {
|
2018-01-30 20:55:03 +01:00
|
|
|
|
2018-02-14 00:24:43 +01:00
|
|
|
std::string DirectoryName(std::string path);
|
|
|
|
std::string LocateSourceFile(
|
|
|
|
std::string name, const std::vector<std::string> &searchPath);
|
|
|
|
|
2018-01-30 20:55:03 +01:00
|
|
|
class SourceFile {
|
2018-02-05 21:54:36 +01:00
|
|
|
public:
|
2018-01-30 20:55:03 +01:00
|
|
|
SourceFile() {}
|
|
|
|
~SourceFile();
|
2018-02-02 00:01:23 +01:00
|
|
|
std::string path() const { return path_; }
|
2018-01-30 20:55:03 +01:00
|
|
|
const char *content() const { return content_; }
|
2018-03-20 18:59:07 +01:00
|
|
|
std::size_t bytes() const { return bytes_; }
|
|
|
|
std::size_t lines() const { return lineStart_.size(); }
|
2018-02-14 00:24:43 +01:00
|
|
|
|
|
|
|
bool Open(std::string path, std::stringstream *error);
|
2018-04-06 19:34:59 +02:00
|
|
|
bool ReadStandardInput(std::stringstream *error);
|
2018-02-14 00:24:43 +01:00
|
|
|
void Close();
|
2018-03-20 18:59:07 +01:00
|
|
|
std::pair<int, int> FindOffsetLineAndColumn(std::size_t) const;
|
|
|
|
std::size_t GetLineStartOffset(int lineNumber) const {
|
2018-02-15 22:13:28 +01:00
|
|
|
return lineStart_.at(lineNumber - 1);
|
|
|
|
}
|
2018-02-05 23:29:26 +01:00
|
|
|
|
2018-02-05 21:54:36 +01:00
|
|
|
private:
|
2018-04-06 19:34:59 +02:00
|
|
|
bool ReadFile(std::string errorPath, std::stringstream *error);
|
|
|
|
|
2018-01-30 20:55:03 +01:00
|
|
|
std::string path_;
|
|
|
|
int fileDescriptor_{-1};
|
|
|
|
bool isMemoryMapped_{false};
|
|
|
|
const char *content_{nullptr};
|
2018-03-20 18:59:07 +01:00
|
|
|
std::size_t bytes_{0};
|
|
|
|
std::vector<std::size_t> lineStart_;
|
2018-01-30 20:55:03 +01:00
|
|
|
};
|
2018-05-02 22:48:12 +02:00
|
|
|
|
|
|
|
} // namespace Fortran::parser
|
2018-02-16 20:42:17 +01:00
|
|
|
#endif // FORTRAN_PARSER_SOURCE_H_
|