From 296040aa960a8b035392d2b66c7bf7faa2ae8ec3 Mon Sep 17 00:00:00 2001 From: peter klausler Date: Thu, 12 Jul 2018 16:23:25 -0700 Subject: [PATCH] [flang] Improve error recovery further, using a test case from Tim. Original-commit: flang-compiler/f18@9ef79fa9913804168d151a5b15588dbfaa02957a Reviewed-on: https://github.com/flang-compiler/f18/pull/123 --- flang/lib/parser/basic-parsers.h | 2 +- flang/lib/parser/grammar.h | 53 ++++++++++++++++++++-------------------- flang/lib/parser/type-parsers.h | 5 +++- 3 files changed, 32 insertions(+), 28 deletions(-) diff --git a/flang/lib/parser/basic-parsers.h b/flang/lib/parser/basic-parsers.h index c1abb08..78d39d7 100644 --- a/flang/lib/parser/basic-parsers.h +++ b/flang/lib/parser/basic-parsers.h @@ -208,7 +208,7 @@ public: private: const MessageFixedText text_; - const PA parser_; + const BacktrackingParser parser_; }; template diff --git a/flang/lib/parser/grammar.h b/flang/lib/parser/grammar.h index 37c3817..0c63853 100644 --- a/flang/lib/parser/grammar.h +++ b/flang/lib/parser/grammar.h @@ -54,27 +54,27 @@ constexpr auto execPartLookAhead{first(actionStmt >> ok, "ASSOCIATE ("_tok, "IF ("_tok, "WHERE ("_tok, "FORALL ("_tok)}; constexpr auto declErrorRecovery{ stmtErrorRecoveryStart >> !execPartLookAhead >> stmtErrorRecovery}; -TYPE_CONTEXT_PARSER("declaration construct"_en_US, - recovery( - withMessage("expected declaration construct"_err_en_US, +TYPE_PARSER(recovery( + withMessage("expected declaration construct"_err_en_US, + CONTEXT_PARSER("declaration construct"_en_US, first(construct(specificationConstruct), construct(statement(indirect(dataStmt))), construct( statement(indirect(formatStmt))), construct(statement(indirect(entryStmt))), construct( - statement(indirect(Parser{}))))), - construct(declErrorRecovery))) + statement(indirect(Parser{})))))), + construct(declErrorRecovery))) // R507 variant of declaration-construct for use in limitedSpecificationPart. -constexpr auto limitedDeclarationConstruct{inContext( - "declaration construct"_en_US, - recovery(withMessage("expected declaration construct"_err_en_US, - first(construct(specificationConstruct), - construct( - statement(indirect(dataStmt))))), - construct( - stmtErrorRecoveryStart >> stmtErrorRecovery)))}; +constexpr auto limitedDeclarationConstruct{recovery( + withMessage("expected declaration construct"_err_en_US, + inContext("declaration construct"_en_US, + first(construct(specificationConstruct), + construct( + statement(indirect(dataStmt)))))), + construct( + stmtErrorRecoveryStart >> stmtErrorRecovery))}; // R508 specification-construct -> // derived-type-def | enum-def | generic-stmt | interface-block | @@ -398,19 +398,20 @@ constexpr auto obsoleteExecutionPartConstruct{recovery(ignoredStatementPrefix >> parenthesized(nonemptyList(Parser{})) >> ok) >> construct()))}; -TYPE_CONTEXT_PARSER("execution part construct"_en_US, - recovery(withMessage("expected execution part construct"_err_en_US, - first(construct(executableConstruct), - construct( - statement(indirect(formatStmt))), - construct( - statement(indirect(entryStmt))), - construct( - statement(indirect(dataStmt))), - extension(construct(statement( - indirect(Parser{}))) || - obsoleteExecutionPartConstruct))), - construct(executionPartErrorRecovery))) +TYPE_PARSER(recovery( + withMessage("expected execution part construct"_err_en_US, + CONTEXT_PARSER("execution part construct"_en_US, + first(construct(executableConstruct), + construct( + statement(indirect(formatStmt))), + construct( + statement(indirect(entryStmt))), + construct( + statement(indirect(dataStmt))), + extension(construct( + statement(indirect(Parser{}))) || + obsoleteExecutionPartConstruct)))), + construct(executionPartErrorRecovery))) // R509 execution-part -> executable-construct [execution-part-construct]... TYPE_CONTEXT_PARSER("execution part"_en_US, diff --git a/flang/lib/parser/type-parsers.h b/flang/lib/parser/type-parsers.h index 428c683..fd3cae1 100644 --- a/flang/lib/parser/type-parsers.h +++ b/flang/lib/parser/type-parsers.h @@ -41,8 +41,11 @@ template struct Parser { return parser.Parse(state); \ } +#define CONTEXT_PARSER(contextText, pexpr) \ + instrumented((contextText), inContext((contextText), (pexpr))) + #define TYPE_CONTEXT_PARSER(contextText, pexpr) \ - TYPE_PARSER(instrumented((contextText), inContext((contextText), (pexpr)))) + TYPE_PARSER(CONTEXT_PARSER((contextText), (pexpr))) // Some specializations of Parser<> are used multiple times, or are // of some special importance, so we instantiate them once here and -- 2.7.4