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-04-21 00:19:20 +02:00
|
|
|
#ifndef FORTRAN_PARSER_STMT_PARSER_H_
|
|
|
|
#define FORTRAN_PARSER_STMT_PARSER_H_
|
|
|
|
|
|
|
|
// Basic parsing of statements.
|
|
|
|
|
|
|
|
#include "basic-parsers.h"
|
|
|
|
#include "token-parsers.h"
|
|
|
|
|
2018-05-02 22:48:12 +02:00
|
|
|
namespace Fortran::parser {
|
2018-04-21 00:19:20 +02:00
|
|
|
|
|
|
|
// statement(p) parses Statement<P> for some statement type P that is the
|
|
|
|
// result type of the argument parser p, while also handling labels and
|
|
|
|
// end-of-statement markers.
|
|
|
|
|
|
|
|
// R611 label -> digit [digit]...
|
2018-09-13 20:29:10 +02:00
|
|
|
constexpr auto label{space >> digitString / spaceCheck};
|
2018-04-21 00:19:20 +02:00
|
|
|
|
|
|
|
template<typename PA> inline constexpr auto unterminatedStatement(const PA &p) {
|
2018-07-12 23:46:23 +02:00
|
|
|
return skipStuffBeforeStatement >>
|
2018-04-24 01:53:16 +02:00
|
|
|
sourced(construct<Statement<typename PA::resultType>>(
|
|
|
|
maybe(label), space >> p));
|
2018-04-21 00:19:20 +02:00
|
|
|
}
|
|
|
|
|
2018-07-12 23:46:23 +02:00
|
|
|
constexpr auto endOfLine{
|
|
|
|
"\n"_ch >> ok || fail("expected end of line"_err_en_US)};
|
2018-04-21 00:19:20 +02:00
|
|
|
|
2018-07-31 21:32:09 +02:00
|
|
|
constexpr auto semicolons{";"_ch >> skipMany(";"_tok) / space / maybe("\n"_ch)};
|
2018-07-11 02:09:07 +02:00
|
|
|
constexpr auto endOfStmt{
|
2018-07-31 21:32:09 +02:00
|
|
|
space >> withMessage("expected end of statement"_err_en_US,
|
|
|
|
semicolons || endOfLine)};
|
|
|
|
constexpr auto forceEndOfStmt{recovery(endOfStmt, SkipPast<'\n'>{})};
|
2018-04-21 00:19:20 +02:00
|
|
|
|
|
|
|
template<typename PA> inline constexpr auto statement(const PA &p) {
|
|
|
|
return unterminatedStatement(p) / endOfStmt;
|
|
|
|
}
|
|
|
|
|
2018-07-17 21:34:07 +02:00
|
|
|
// This unambiguousStatement() variant of statement() provides better error
|
|
|
|
// recovery for contexts containing statements that might have trailing
|
|
|
|
// garbage, but it must be used only when no instance of the statement in
|
|
|
|
// question could also be a legal prefix of some other statement that might
|
|
|
|
// be valid at that point. It only makes sense to use this within "some()"
|
|
|
|
// or "many()" so as to not end the list of statements.
|
|
|
|
template<typename PA> inline constexpr auto unambiguousStatement(const PA &p) {
|
2018-07-31 21:32:09 +02:00
|
|
|
return unterminatedStatement(p) / forceEndOfStmt;
|
2018-07-17 21:34:07 +02:00
|
|
|
}
|
|
|
|
|
2018-07-11 02:09:07 +02:00
|
|
|
constexpr auto ignoredStatementPrefix{
|
2018-07-12 23:46:23 +02:00
|
|
|
skipStuffBeforeStatement >> maybe(label) >> maybe(name / ":") >> space};
|
2018-04-21 00:19:20 +02:00
|
|
|
|
2018-09-10 23:19:37 +02:00
|
|
|
// Error recovery within a statement() call: skip *to* the end of the line,
|
|
|
|
// unless at an END or CONTAINS statement.
|
|
|
|
constexpr auto inStmtErrorRecovery{!"END"_tok >> !"CONTAINS"_tok >>
|
|
|
|
SkipTo<'\n'>{} >> construct<ErrorRecovery>()};
|
|
|
|
|
|
|
|
// Error recovery within statement sequences: skip *past* the end of the line,
|
2018-04-21 00:19:20 +02:00
|
|
|
// but not over an END or CONTAINS statement.
|
2018-09-10 23:19:37 +02:00
|
|
|
constexpr auto skipStmtErrorRecovery{!"END"_tok >> !"CONTAINS"_tok >>
|
2018-07-12 23:46:23 +02:00
|
|
|
SkipPast<'\n'>{} >> construct<ErrorRecovery>()};
|
2018-04-21 00:19:20 +02:00
|
|
|
|
|
|
|
// Error recovery across statements: skip the line, unless it looks
|
|
|
|
// like it might end the containing construct.
|
2018-07-12 23:46:23 +02:00
|
|
|
constexpr auto stmtErrorRecoveryStart{ignoredStatementPrefix};
|
|
|
|
constexpr auto skipBadLine{SkipPast<'\n'>{} >> construct<ErrorRecovery>()};
|
|
|
|
constexpr auto executionPartErrorRecovery{stmtErrorRecoveryStart >>
|
|
|
|
!"END"_tok >> !"CONTAINS"_tok >> !"ELSE"_tok >> !"CASE"_tok >>
|
2018-07-21 00:49:19 +02:00
|
|
|
!"TYPE IS"_tok >> !"CLASS"_tok >> !"RANK"_tok >>
|
|
|
|
!("!$OMP "_sptok >> "END"_tok) >> skipBadLine};
|
2018-04-21 00:19:20 +02:00
|
|
|
|
2018-07-31 00:31:06 +02:00
|
|
|
// END statement error recovery
|
|
|
|
constexpr auto missingOptionalName{defaulted(cut >> maybe(name))};
|
|
|
|
constexpr auto noNameEnd{"END" >> missingOptionalName};
|
2018-07-31 21:32:09 +02:00
|
|
|
constexpr auto atEndOfStmt{space >>
|
|
|
|
withMessage("expected end of statement"_err_en_US, lookAhead(";\n"_ch))};
|
|
|
|
constexpr auto bareEnd{noNameEnd / recovery(atEndOfStmt, SkipTo<'\n'>{})};
|
|
|
|
|
2018-07-31 00:31:06 +02:00
|
|
|
constexpr auto endStmtErrorRecovery{
|
2018-09-04 20:42:10 +02:00
|
|
|
("END"_tok >> SkipTo<'\n'>{} || ok) >> missingOptionalName};
|
|
|
|
|
2018-07-31 01:33:55 +02:00
|
|
|
constexpr auto progUnitEndStmtErrorRecovery{
|
|
|
|
(many(!"END"_tok >> SkipPast<'\n'>{}) >>
|
|
|
|
("END"_tok >> SkipTo<'\n'>{} || consumedAllInput)) >>
|
|
|
|
missingOptionalName};
|
2018-07-31 00:31:06 +02:00
|
|
|
|
2018-05-02 22:48:12 +02:00
|
|
|
} // namespace Fortran::parser
|
2018-04-21 00:19:20 +02:00
|
|
|
#endif // FORTRAN_PARSER_STMT_PARSER_H_
|