Spicy
driver.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #ifdef yylex
6 #undef yylex
7 // Work-around for bison messing up the function name by adding the local namespace.
8 #define yylex lex
9 #endif
10 
11 #include <memory.h>
12 
13 #include <iostream>
14 #include <string>
15 #include <utility>
16 #include <vector>
17 
18 #include <hilti/ast/all.h>
19 #include <hilti/base/preprocessor.h>
20 
21 #include <spicy/ast/all.h>
22 #include <spicy/autogen/config.h>
23 
24 #undef YY_DECL
25 #define YY_DECL \
26  spicy::detail::parser::Parser::token_type \
27  spicy::detail::parser::Scanner::lex(spicy::detail::parser::Parser::semantic_type* yylval, \
28  spicy::detail::parser::location* yylloc, \
29  spicy::detail::parser::Driver* driver)
30 
31 #define YYSTYPE yystype_spicy
32 
33 #ifndef __FLEX_LEXER_H
34 #define yyFlexLexer SpicyFlexLexer
35 #include <FlexLexer.h>
36 
37 #undef yyFlexLexer
38 #endif
39 
41 struct yystype_spicy {
42  bool bool_ = false;
43  double real = 0.0;
44  uint64_t uint = 0;
45  int64_t sint = 0;
46  std::string str;
47 
48  hilti::ID id;
49  hilti::Declaration declaration;
50  hilti::Type type;
51  hilti::Ctor ctor;
52  hilti::Expression expression;
53  hilti::Statement statement;
54  hilti::Attribute attribute;
55  hilti::Function function;
56 
57  std::optional<hilti::Expression> opt_expression;
58  std::optional<hilti::Statement> opt_statement;
59  std::optional<hilti::AttributeSet> opt_attributes;
60 
61  hilti::declaration::Linkage linkage;
62  hilti::declaration::parameter::Kind function_parameter_kind;
63  hilti::function::CallingConvention function_calling_convention;
64  hilti::type::function::Parameter function_parameter;
65  hilti::type::function::Result function_result;
66  hilti::type::function::Flavor function_flavor;
68 
69  std::vector<std::string> strings;
70  std::vector<hilti::Declaration> declarations;
71  std::vector<hilti::Expression> expressions;
72  std::vector<hilti::Statement> statements;
73  std::vector<hilti::type::function::Parameter> function_parameters;
74  std::vector<hilti::statement::switch_::Case> switch_cases;
75 
76  std::pair<hilti::ID, hilti::Type> tuple_type_elem;
77  std::vector<std::pair<hilti::ID, hilti::Type>> tuple_type_elems;
78 
79  hilti::type::struct_::Field struct_field;
80  hilti::ctor::struct_::Field struct_elem;
81  std::vector<hilti::type::struct_::Field> struct_fields;
82  std::vector<hilti::ctor::struct_::Field> struct_elems;
83 
84  hilti::ctor::Map::Element map_elem;
85  std::vector<hilti::ctor::Map::Element> map_elems;
86 
87  hilti::type::enum_::Label enum_label;
88  std::vector<hilti::type::enum_::Label> enum_labels;
89 
90  spicy::type::bitfield::Bits bitfield_bits_spec;
91  std::vector<spicy::type::bitfield::Bits> bitfield_bits;
92 
93  std::pair<std::vector<hilti::Declaration>, std::vector<hilti::Statement>> decls_and_stmts;
94 
95  // Spicy-only
96  std::optional<hilti::ID> opt_id;
97  std::vector<spicy::type::unit::Item> unit_items;
98  spicy::type::unit::Item unit_item;
99  spicy::Engine engine;
100  std::vector<spicy::Hook> hooks;
101  spicy::Hook hook;
102 
104  std::vector<spicy::type::unit::item::switch_::Case> unit_switch_cases;
105 };
106 
107 namespace spicy {
108 
109 namespace logging::debug {
110 inline const hilti::logging::DebugStream Parser("parser");
111 } // namespace logging::debug
112 
113 namespace detail {
114 namespace parser {
115 
116 class Parser;
117 class Scanner;
118 
120 class Driver {
121 public:
122  Driver() : _preprocessor(spicy::configuration().preprocessor_constants) {}
123 
124  hilti::Result<hilti::Node> parse(std::istream& in, const std::string& filename);
125  hilti::Result<hilti::Node> parseExpression(const std::string& expression, const Meta& m = Meta());
126 
127  Scanner* scanner() const { return _scanner; }
128  Parser* parser() const { return _parser; }
129 
130  // Methods for the parser.
131 
132  std::string* currentFile() { return &_filename; }
133  int currentLine() { return _line; }
134  void error(const std::string& msg, const Meta& m);
135  void enablePatternMode();
136  void disablePatternMode();
137  void enableExpressionMode();
138  void disableExpressionMode();
139  void enableDottedIDMode();
140  void disableDottedIDMode();
141  void enableHookIDMode();
142  void disableHookIDMode();
143  void setDestinationModule(Module m) { _module = std::move(m); }
144  void setDestinationExpression(Expression e) { _expression = std::move(e); }
145  int nextToken();
146  void processPreprocessorLine(const std::string_view& directive, const std::string_view& expression, const Meta& m);
147 
148 private:
149  Module _module;
150  Expression _expression;
151  std::string _filename;
152  int _line{};
153  Parser* _parser = nullptr;
154  Scanner* _scanner = nullptr;
155  int _next_token = 0;
157 };
158 
159 } // namespace parser
160 } // namespace detail
161 } // namespace spicy
Definition: driver.h:120
Definition: bitfield.h:25
Definition: function.h:47
Definition: struct.h:23
Definition: function.h:44
Definition: hook.h:17
Definition: preprocessor.h:22
Definition: meta.h:18
Definition: driver.h:41
Definition: attribute.h:26
Definition: parameter.h:45
Definition: module.h:20
Definition: scanner.h:17
Definition: enum.h:17
Definition: switch.h:34
Definition: id.h:18
Definition: result.h:67
Definition: logger.h:28