Spicy
driver.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <utility>
6 #include <vector>
7 #ifdef yylex
8 #undef yylex
9 // Work-around for bison messing up the function name by adding the local namespace.
10 #define yylex lex
11 #endif
12 
13 #include <memory.h>
14 
15 #include <iostream>
16 #include <string>
17 
18 #include <hilti/ast/all.h>
19 #include <hilti/base/result.h>
20 
21 #undef YY_DECL
22 #define YY_DECL \
23  hilti::detail::parser::Parser::token_type \
24  hilti::detail::parser::Scanner::lex(hilti::detail::parser::Parser::semantic_type* yylval, \
25  hilti::detail::parser::location* yylloc, \
26  hilti::detail::parser::Driver* driver)
27 
28 #define YYSTYPE yystype_hilti
29 
30 #ifndef __FLEX_LEXER_H
31 #define yyFlexLexer HiltiFlexLexer
32 #include <FlexLexer.h>
33 
34 #undef yyFlexLexer
35 #endif
36 
38 struct yystype_hilti {
39  bool bool_ = false;
40  double real = 0.0;
41  uint64_t uint = 0;
42  int64_t sint = 0;
43  std::string str;
44 
45  hilti::ID id;
46  hilti::Declaration declaration;
47  hilti::Type type;
48  hilti::Ctor ctor;
49  hilti::Expression expression;
50  hilti::Statement statement;
51  hilti::Attribute attribute;
52  hilti::Function function;
53  hilti::type::Flags type_flags;
54 
55  std::optional<hilti::Expression> opt_expression;
56  std::optional<hilti::Statement> opt_statement;
57  std::optional<hilti::AttributeSet> opt_attributes;
58 
59  hilti::declaration::Linkage linkage;
60  hilti::declaration::parameter::Kind function_parameter_kind;
61  hilti::function::CallingConvention function_calling_convention;
62  hilti::type::function::Parameter function_parameter;
63  hilti::type::function::Result function_result;
64  hilti::type::function::Flavor function_flavor;
67 
68  std::vector<std::string> strings;
69  std::vector<hilti::Declaration> declarations;
70  std::vector<hilti::Expression> expressions;
71  std::vector<hilti::Statement> statements;
72  std::vector<hilti::type::function::Parameter> function_parameters;
73  std::vector<hilti::statement::switch_::Case> switch_cases;
74  std::vector<hilti::statement::try_::Catch> try_catches;
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::type::union_::Field union_field;
85  std::vector<hilti::type::union_::Field> union_fields;
86 
87  hilti::ctor::Map::Element map_elem;
88  std::vector<hilti::ctor::Map::Element> map_elems;
89 
90  hilti::type::enum_::Label enum_label;
91  std::vector<hilti::type::enum_::Label> enum_labels;
92 
93  std::pair<std::vector<hilti::Declaration>, std::vector<hilti::Statement>> decls_and_stmts;
94 };
95 
96 namespace hilti {
97 
98 namespace logging::debug {
99 inline const DebugStream Parser("parser");
100 } // namespace logging::debug
101 
102 namespace detail {
103 namespace parser {
104 
105 class Parser;
106 class Scanner;
107 
109 class Driver {
110 public:
111  Result<hilti::Node> parse(std::istream& in, const std::string& filename);
112 
113  Scanner* scanner() const { return _scanner; }
114  Parser* parser() const { return _parser; }
115 
116  // Methods for the parser.
117 
118  std::string* currentFile() { return &_filename; }
119  void error(const std::string& msg, const Meta& m);
120  void enablePatternMode();
121  void disablePatternMode();
122  void enableExpressionMode();
123  void disableExpressionMode();
124  void enableDottedIDMode();
125  void disableDottedIDMode();
126  void setDestinationModule(Module&& m) { _module = std::move(m); }
127 
128 private:
129  Module _module;
130  std::string _filename;
131  Parser* _parser = nullptr;
132  Scanner* _scanner = nullptr;
133  int _expression_mode = 0;
134 };
135 
136 } // namespace parser
137 } // namespace detail
138 } // namespace hilti
Definition: try.h:20
Definition: union.h:21
Definition: function.h:47
Definition: struct.h:23
Definition: function.h:44
Definition: meta.h:18
Definition: scanner.h:17
Definition: attribute.h:26
Definition: parameter.h:45
Definition: type.h:54
Definition: driver.h:38
Definition: module.h:20
Definition: enum.h:17
Definition: switch.h:34
Definition: id.h:18
Definition: driver.h:109
Definition: result.h:67