File indexing completed on 2024-03-24 15:28:49

0001 /*
0002     This file is part of the kholidays library.
0003 
0004     SPDX-FileCopyrightText: 2010 John Layt <john@layt.net>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #ifndef KHOLIDAYS_HOLIDAYPARSERDRIVERPLAN_P_H
0010 #define KHOLIDAYS_HOLIDAYPARSERDRIVERPLAN_P_H
0011 
0012 #include "../holidayparserdriver_p.h"
0013 
0014 #include <QByteArray>
0015 #include <QStringList>
0016 
0017 #include <string>
0018 
0019 namespace KHolidays
0020 {
0021 class HolidayScannerPlan;
0022 class HolidayParserPlan;
0023 class location;
0024 
0025 /**
0026  * HolidayParserDriverPlan implementation class
0027  *
0028  * Implements the standard driver interface for parsing Plan holiday files as
0029  * implemented in KDE SC 4.5 onwards.  This includes extensions to the file
0030  * format to support non-Gregorian calendar systems, metadata, and other new
0031  * features.
0032  *
0033  * Implemented using Bison/Flex and the Bison C++ skeleton v2.1a, see README.
0034  * While almost all code is new, the structure of the parser is based on the
0035  * original Plan parser.
0036  *
0037  * @internal Private, for internal use only
0038  */
0039 class HolidayParserDriverPlan : public HolidayParserDriver
0040 {
0041 public:
0042     /**
0043      * Constructor of Plan file parser driver
0044      *
0045      * @param filePath full path to holiday file
0046      */
0047     explicit HolidayParserDriverPlan(const QString &planFilename);
0048 
0049     /**
0050      * Destructor.
0051      */
0052     ~HolidayParserDriverPlan() override;
0053 
0054     /**
0055      * Bison C++ skeleton error message handling
0056      *
0057      * @param errorMessage error message to log
0058      */
0059     void error(const KHolidays::location &errorLocation, const QString &errorMessage);
0060 
0061     /**
0062      * Standard error message handling
0063      *
0064      * @param errorMessage error message to log
0065      */
0066     void error(const QString &errorMessage) override;
0067 
0068 protected:
0069     /**
0070      * Actually parse the file, new plan format implementation
0071      */
0072     void parse() override;
0073 
0074     /**
0075      * Parse the file for metadata only and populate the metadata variables
0076      */
0077     void parseMetadata() override;
0078 
0079     /**
0080      * Set the calendar system to use
0081      *
0082      * @param calendar The QCalendarSystem calendar system to use
0083      */
0084     void setParseCalendar(QCalendarSystem::CalendarSystem calendar) override;
0085 
0086     // Bison C++ skeleton required friend for Bison parser class implementation
0087     friend class HolidayParserPlan;
0088 
0089     // Accessor methods for parser to talk to driver
0090     QString filePath();
0091     std::string *fileToParse() const;
0092 
0093     // Calendar and date calculation utilities
0094     int adjustedMonthNumber(int month);
0095     bool isLeapYear(int year);
0096     int parseYear();
0097 
0098     // Utilities for parser to calculate interim Julian Day Number during parsing
0099     int julianDayFromEventName(const QString &eventName);
0100     int julianDayFromEaster();
0101     int julianDayFromPascha();
0102     int julianDayFromMonthDay(int month, int day);
0103     int julianDayFromRelativeWeekday(int occurrence, int weekday, int jd);
0104     int julianDayFromWeekdayInMonth(int occurrence, int weekday, int month);
0105 
0106     // Utilities for parser to set variables during parsing
0107     void setParseCalendar(const QString &calendarType);
0108     void setFileCountryCode(const QString &countryCode);
0109     void setFileLanguageCode(const QString &languageCode);
0110     void setFileName(const QString &ame);
0111     void setFileDescription(const QString &description);
0112     void setEventName(const QString &eventName);
0113     void setEventCategory(const QString &category);
0114     void setEventCalendarType(const QString &calendarType = QStringLiteral("gregorian"));
0115     void setEventDate(int eventYear, int eventMonth, int eventDay);
0116     void setEventDate(int jd);
0117 
0118     // Terminal functions for parser to create holidays from parsed variables
0119     void setFromEaster(int offset, int duration);
0120     void setFromPascha(int offset, int duration);
0121     void setFromDate(int offset, int condition, int duration);
0122     void setFromWeekdayInMonth(int occurrence, int weekday, int month, int offset, int duration);
0123     void setFromRelativeWeekday(int occurrence, int weekday, int offset, int duration);
0124     void setEvent(int event_jd, int observe_offset, int duration);
0125 
0126 private:
0127     // Calendar and date calculation utilities
0128     int julianDay(int year, int month, int day);
0129     void julianDayToDate(int jd, int *year, int *month, int *day);
0130     QDate easter(int year);
0131     QDate pascha(int year);
0132     QCalendarSystem::CalendarSystem typeToSystem(const QString &calendarType);
0133     QString systemToType(QCalendarSystem::CalendarSystem calendar);
0134 
0135     int conditionalOffset(int year, int month, int day, int condition);
0136 
0137     void addHoliday(const QDate &date, int duration);
0138 
0139     QByteArray m_scanData; // Holiday file stored as a string
0140 
0141     QStringList m_fileCalendarTypes; // List of all Calendar Types used in file
0142     QString m_parseCalendarType; // Calendar Type being parsed
0143 
0144     bool m_traceParsing; // Bison C++ skeleton enable tracing in Bison parser class
0145     HolidayParserPlan *m_parser; // Bison C++ skeleton Bison parser class implementation
0146 
0147     bool m_traceScanning; // Flex C++ enable tracing in Flex scanner class
0148     HolidayScannerPlan *m_scanner; // Flex C++ scanner class implementation
0149 
0150     bool m_parseMetadataOnly; // Only parse file for metadata
0151     QDate m_parseYearStart; // First day of year being parsed
0152     QDate m_parseYearEaster; // Easter in the parse year, Gregorian only
0153     QDate m_parseYearPascha; // Orthodox Easter in the parse year, Gregorian only
0154 
0155     QStringList m_eventCategories; // Event categories
0156     QString m_eventCalendarType; // Calendar Type for event rule
0157     QString m_eventName; // Event name text
0158     int m_eventYear; // Event date fields
0159     int m_eventMonth; // Event date fields
0160     int m_eventDay; // Event date fields
0161 
0162     std::string *m_fileToParse = nullptr;
0163 };
0164 
0165 }
0166 
0167 #endif // KHOLIDAYS_HOLIDAYPARSERDRIVERPLAN_P_H