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 #ifndef __FLEX_LEXER_H
32 #define yyFlexLexer SpicyFlexLexer
33 #include <FlexLexer.h>
34 
35 #undef yyFlexLexer
36 #endif
37 
38 namespace spicy {
39 
40 namespace logging::debug {
41 inline const hilti::logging::DebugStream Parser("parser");
42 } // namespace logging::debug
43 
44 namespace detail {
45 namespace parser {
46 
47 class Parser;
48 class Scanner;
49 
51 class Driver {
52 public:
53  Driver() : _preprocessor(spicy::configuration().preprocessor_constants) {}
54 
55  hilti::Result<hilti::Node> parse(std::istream& in, const std::string& filename);
56  hilti::Result<hilti::Node> parseExpression(const std::string& expression, const Meta& m = Meta());
57 
58  Scanner* scanner() const { return _scanner; }
59  Parser* parser() const { return _parser; }
60 
61  // Methods for the parser.
62 
63  std::string* currentFile() { return &_filename; }
64  int currentLine() { return _line; }
65  void error(const std::string& msg, const Meta& m);
66  void enablePatternMode();
67  void disablePatternMode();
68  void enableExpressionMode();
69  void disableExpressionMode();
70  void enableDottedIDMode();
71  void disableDottedIDMode();
72  void enableHookIDMode();
73  void disableHookIDMode();
74  void setDestinationModule(Module m) { _module = std::move(m); }
75  void setDestinationExpression(Expression e) { _expression = std::move(e); }
76  int nextToken();
77  void processPreprocessorLine(const std::string_view& directive, const std::string_view& expression, const Meta& m);
78 
79 private:
80  Module _module;
81  Expression _expression;
82  std::string _filename;
83  int _line{};
84  Parser* _parser = nullptr;
85  Scanner* _scanner = nullptr;
86  int _next_token = 0;
88 };
89 
90 } // namespace parser
91 } // namespace detail
92 } // namespace spicy
Definition: driver.h:51
Definition: preprocessor.h:22
Definition: meta.h:18
Definition: module.h:21
Definition: scanner.h:17
Definition: result.h:67
Definition: logger.h:28