Sen API
Sen Libraries
Loading...
Searching...
No Matches
stl_parser.h
Go to the documentation of this file.
1// === stl_parser.h ====================================================================================================
2// Sen Infrastructure
3// Released under the Apache License v2.0 (SPDX-License-Identifier Apache-2.0).
4// See the LICENSE.txt file for more information.
5// © Airbus SAS, Airbus Helicopters, and Airbus Defence and Space SAU/GmbH/SAS.
6// =====================================================================================================================
7
8#ifndef SEN_CORE_LANG_STL_PARSER_H
9#define SEN_CORE_LANG_STL_PARSER_H
10
11// sen
15
16// std
17#include <optional>
18#include <stdexcept>
19#include <string>
20#include <vector>
21
22namespace sen::lang
23{
24
27
30{
31public:
33 explicit StlParser(const StlTokenList& tokens);
34
35 struct ParseError: public std::runtime_error
36 {
37 explicit ParseError(const std::string& msg): std::runtime_error(msg) {}
38 };
39
41 [[nodiscard]] std::vector<StlStatement> parse();
42
44 [[nodiscard]] QueryStatement parseQuery();
45
46private: // declarations
47 [[nodiscard]] StlStatement declaration();
48 [[nodiscard]] StlImportStatement importDeclaration();
49 [[nodiscard]] StlPackageStatement packageDeclaration();
50 [[nodiscard]] StlTypeNameStatement typeNameStatement(const std::vector<std::string>& messages);
51 [[nodiscard]] StlStructStatement structDeclaration();
52 [[nodiscard]] StlStructFieldStatement structFieldStatement();
53 [[nodiscard]] StlEnumStatement enumDeclaration();
54 [[nodiscard]] StlEnumeratorStatement enumeratorDeclaration();
55 [[nodiscard]] StlVariantStatement variantDeclaration();
56 [[nodiscard]] StlVariantElement variantElementDeclaration();
57 [[nodiscard]] StlSequenceStatement sequenceStatement();
58 [[nodiscard]] StlArrayStatement arrayStatement();
59 [[nodiscard]] StlQuantityStatement quantityStatement();
60 [[nodiscard]] StlTypeAliasStatement aliasStatement();
61 [[nodiscard]] StlOptionalTypeStatement optionalTypeStatement();
62 [[nodiscard]] StlClassStatement classStatement(bool isAbstract);
63 [[nodiscard]] StlInterfaceStatement interfaceStatement();
64 [[nodiscard]] StlVarStatement varStatement();
65 [[nodiscard]] StlFunctionStatement functionStatement();
66 [[nodiscard]] StlEventStatement eventStatement();
67 [[nodiscard]] StlArgStatement argStatement();
68 [[nodiscard]] std::optional<StlAttributeList> maybeAttributeList(const std::string& subjectName);
69 [[nodiscard]] StlAttributeList attributeList(const std::string& subjectName);
70 [[nodiscard]] BusNameStatement busNameStatement();
71 [[nodiscard]] std::shared_ptr<StlExpr> expression();
72 [[nodiscard]] std::shared_ptr<StlExpr> logicOr();
73 [[nodiscard]] std::shared_ptr<StlExpr> logicAnd();
74 [[nodiscard]] std::shared_ptr<StlExpr> equality();
75 [[nodiscard]] std::shared_ptr<StlExpr> comparison();
76 [[nodiscard]] std::shared_ptr<StlExpr> term();
77 [[nodiscard]] std::shared_ptr<StlExpr> factor();
78 [[nodiscard]] std::shared_ptr<StlExpr> unary();
79 [[nodiscard]] std::shared_ptr<StlExpr> primary();
80 [[nodiscard]] std::shared_ptr<StlExpr> variable();
81
82private: // expressions
83 [[nodiscard]] StlToken literal();
84
85private: // utility
86 [[nodiscard]] bool match(const std::vector<StlTokenType>& types);
87 [[nodiscard]] bool check(StlTokenType type) const;
88 [[nodiscard]] const StlToken& peek() const;
89 [[nodiscard]] StlToken peekNext() const;
90 [[nodiscard]] const StlToken& previous() const;
91 [[nodiscard]] bool isAtEnd() const noexcept;
92 [[nodiscard]] const StlToken& advance();
93 [[nodiscard]] const StlToken& consume(StlTokenType type, const std::vector<std::string>& message);
94 [[nodiscard]] ParseError error(const StlToken& token, const std::string& message) const;
95 void checkNotReserved(const StlToken& token);
96
97private: // others
98 void classOrInterfaceMembers(StlInterfaceStatement& statement);
99 void classParents(StlClassStatement& statement);
100 void validateComments(const StlToken& functionOrEventIdentifier, const std::vector<StlArgStatement>& arguments);
101 [[nodiscard]] bool containsNewLine(size_t startOffset, size_t endOffset) const;
102
103private:
104 const StlTokenList& tokens_;
105 std::size_t current_ = 0U;
106 std::vector<StlToken> previousComment_;
107};
108
110
111} // namespace sen::lang
112
113#endif // SEN_CORE_LANG_STL_PARSER_H
StlParser(const StlTokenList &tokens)
Stores the tokens for eventual parsing.
std::vector< StlStatement > parse()
Parse the tokens given in the constructor into a set of statements.
QueryStatement parseQuery()
Parse the tokens as a query statement.
std::variant< std::monostate, StlImportStatement, StlPackageStatement, StlStructStatement, StlEnumStatement, StlVariantStatement, StlSequenceStatement, StlArrayStatement, StlQuantityStatement, StlTypeAliasStatement, StlOptionalTypeStatement, StlClassStatement, StlInterfaceStatement > StlStatement
Definition stl_statement.h:183
std::vector< StlToken > StlTokenList
Definition stl_token.h:121
std::vector< StlAttribute > StlAttributeList
Definition stl_statement.h:41
StlTokenType
Supported tokens.
Definition stl_token.h:28
Definition stl_statement.h:198
Definition stl_statement.h:204
Definition stl_statement.h:134
Definition stl_statement.h:111
Definition stl_statement.h:176
Definition stl_statement.h:72
Definition stl_statement.h:65
Definition stl_statement.h:159
Definition stl_statement.h:150
Definition stl_statement.h:26
Definition stl_statement.h:167
Definition stl_statement.h:127
Definition stl_statement.h:31
Definition stl_statement.h:93
Definition stl_statement.h:102
Definition stl_statement.h:50
Definition stl_statement.h:57
Definition stl_statement.h:120
Definition stl_statement.h:44
Definition stl_statement.h:141
Definition stl_statement.h:80
Definition stl_statement.h:86
Definition code_location.h:14
STL namespace.
Definition stl_parser.h:36
ParseError(const std::string &msg)
Definition stl_parser.h:37
Definition stl_token.h:98