115 lines · c
1//===-- lib/Parser/stmt-parser.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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef FORTRAN_PARSER_STMT_PARSER_H_10#define FORTRAN_PARSER_STMT_PARSER_H_11 12// Basic parsing of statements.13 14#include "basic-parsers.h"15#include "token-parsers.h"16 17namespace Fortran::parser {18 19// statement(p) parses Statement<P> for some statement type P that is the20// result type of the argument parser p, while also handling labels and21// end-of-statement markers.22 23// R611 label -> digit [digit]...24constexpr auto label{space >> digitString64 / spaceCheck};25 26template <typename PA>27inline constexpr auto unterminatedStatement(const PA &p) {28 return skipStuffBeforeStatement >>29 sourced(construct<Statement<typename PA::resultType>>(30 maybe(label / space), p));31}32 33constexpr auto atEndOfStmt{space >>34 withMessage("expected end of statement"_err_en_US, lookAhead(";\n"_ch))};35constexpr auto checkEndOfKnownStmt{recovery(atEndOfStmt, SkipTo<'\n'>{})};36 37constexpr auto endOfLine{consumedAllInput ||38 withMessage("expected end of line"_err_en_US, "\n"_ch >> ok)};39 40constexpr auto semicolons{";"_ch >> skipMany(";"_tok) / space / maybe("\n"_ch)};41constexpr auto endOfStmt{42 space >> withMessage("expected end of statement"_err_en_US,43 semicolons || endOfLine)};44constexpr auto skipToNextLineIfAny{consumedAllInput || SkipPast<'\n'>{}};45constexpr auto forceEndOfStmt{recovery(endOfStmt, skipToNextLineIfAny)};46 47template <typename PA> inline constexpr auto statement(const PA &p) {48 return unterminatedStatement(p) / endOfStmt;49}50 51// unlabeledStatement() is basically statement() for those few situations52// in Fortran where a statement cannot have a label.53template <typename PA> inline constexpr auto unlabeledStatement(const PA &p) {54 return space >>55 sourced(construct<UnlabeledStatement<typename PA::resultType>>(p));56}57 58// This unambiguousStatement() variant of statement() provides better error59// recovery for contexts containing statements that might have trailing60// garbage, but it must be used only when no instance of the statement in61// question could also be a legal prefix of some other statement that might62// be valid at that point. It only makes sense to use this within "some()"63// or "many()" so as to not end the list of statements.64template <typename PA> inline constexpr auto unambiguousStatement(const PA &p) {65 return unterminatedStatement(p) / forceEndOfStmt;66}67 68constexpr auto ignoredStatementPrefix{69 skipStuffBeforeStatement >> maybe(label) >> maybe(name / ":") >> space};70 71// Error recovery within a statement() call: skip *to* the end of the line,72// unless at an END or CONTAINS statement.73constexpr auto inStmtErrorRecovery{!"END"_tok >> !"CONTAINS"_tok >>74 (consumedAllInput || SkipTo<'\n'>{}) >> construct<ErrorRecovery>()};75 76// Error recovery within statement sequences: skip *past* the end of the line,77// but not over an END or CONTAINS statement.78constexpr auto skipStmtErrorRecovery{!"END"_tok >> !"CONTAINS"_tok >>79 (consumedAllInput || SkipPast<'\n'>{}) >> construct<ErrorRecovery>()};80 81// Error recovery across statements: skip the line, unless it looks82// like it might end the containing construct.83constexpr auto stmtErrorRecoveryStart{ignoredStatementPrefix};84constexpr auto skipBadLine{skipToNextLineIfAny >> construct<ErrorRecovery>()};85constexpr auto executionPartErrorRecovery{stmtErrorRecoveryStart >>86 !"END"_tok >> !"CONTAINS"_tok >> !"ELSE"_tok >> !"CASE"_tok >>87 !"TYPE IS"_tok >> !"CLASS"_tok >> !"RANK"_tok >>88 !("!$ACC "_sptok >> "END"_tok) >>89 !("!$OMP "_sptok >> ("END"_tok || "SECTION"_id)) >> skipBadLine};90 91// END statement error recovery92constexpr auto missingOptionalName{pure<std::optional<Name>>()};93constexpr auto noNameEnd{"END" >> missingOptionalName};94 95// For unrecognizable construct END statements. Be sure to not consume96// a program unit's END statement.97constexpr auto progUnitEndStmt{consumedAllInput ||98 "END" >> (lookAhead("\n"_ch) || "SUBROUTINE"_tok || "FUNCTION"_tok ||99 "PROCEDURE"_tok || "MODULE"_tok || "SUBMODULE"_tok ||100 "PROGRAM"_tok || "BLOCK DATA"_tok)};101constexpr auto constructEndStmtErrorRecovery{102 !progUnitEndStmt >> ("END"_tok >> SkipTo<'\n'>{} || ok)};103constexpr auto namedConstructEndStmtErrorRecovery{104 constructEndStmtErrorRecovery >> missingOptionalName};105 106constexpr auto progUnitEndStmtErrorRecovery{107 many(!"END"_tok >> SkipPast<'\n'>{}) >>108 maybe("END"_tok >> SkipTo<'\n'>{}) >> missingOptionalName};109 110constexpr auto beginDirective{skipStuffBeforeStatement >> "!"_ch};111constexpr auto endDirective{space >> endOfLine};112 113} // namespace Fortran::parser114#endif // FORTRAN_PARSER_STMT_PARSER_H_115