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::parser {
45 
46 class Parser;
47 class Scanner;
48 
50 class Driver {
51 public:
52  Driver() : _preprocessor(spicy::configuration().preprocessor_constants) {}
53 
54  hilti::Result<hilti::Node> parse(std::istream& in, const std::string& filename);
55  hilti::Result<hilti::Node> parseExpression(const std::string& expression, const Meta& m = Meta());
56 
57  Scanner* scanner() const { return _scanner; }
58  Parser* parser() const { return _parser; }
59 
60  // Methods for the parser.
61 
62  std::string* currentFile() { return &_filename; }
63  int currentLine() { return _line; }
64  void error(const std::string& msg, const Meta& m);
65  void enablePatternMode();
66  void disablePatternMode();
67  void enableExpressionMode();
68  void disableExpressionMode();
69  void enableDottedIDMode();
70  void disableDottedIDMode();
71  void enableHookIDMode();
72  void disableHookIDMode();
73  void setDestinationModule(Module m) { _module = std::move(m); }
74  void setDestinationExpression(Expression e) { _expression = std::move(e); }
75  int nextToken();
76  void processPreprocessorLine(const std::string_view& directive, const std::string_view& expression, const Meta& m);
77 
78  void docSummary(const std::string& s);
79  void docText(const std::string& s);
80  void docField(const std::string& s);
81  const DocString& docGet() const;
82  DocString&& docGetAndClear();
83  void docClear();
84 
85 private:
86  DocString _doc;
87  Module _module;
88  Expression _expression;
89  std::string _filename;
90  int _line{};
91  Parser* _parser = nullptr;
92  Scanner* _scanner = nullptr;
93  int _next_token = 0;
95 };
96 
97 } // namespace detail::parser
98 } // namespace spicy
Definition: driver.h:50
Definition: doc-string.h:15
Definition: preprocessor.h:23
Definition: meta.h:19
Definition: module.h:21
Definition: scanner.h:17
Definition: result.h:67
Definition: logger.h:28