User Guide
c11_lexer.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2023 Blue Brain Project, EPFL.
3  * See the top-level LICENSE file for details.
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #pragma once
9 
10 #include "parser/c/c11_parser.hpp"
11 
12 /**
13  * Flex expects the declaration of yylex to be defined in the macro YY_DECL
14  * and C++ parser class expects it to be declared.
15  */
16 #ifndef YY_DECL
17 #define YY_DECL nmodl::parser::CParser::symbol_type nmodl::parser::CLexer::next_token()
18 #endif
19 
20 /**
21  * For creating multiple (different) lexer classes, we can use `-P` flag
22  * (or prefix option) to rename each `NmodlFlexLexer` to some other name like
23  * `xxFlexLexer`. And then include <FlexLexer.h> in other sources once per
24  * lexer class, first renaming `yyFlexLexer` as shown below.
25  */
26 #ifndef __FLEX_LEXER_H
27 #define yyFlexLexer CFlexLexer
28 #include "FlexLexer.h"
29 #endif
30 
31 namespace nmodl {
32 namespace parser {
33 
34 /**
35  * @addtogroup lexer
36  * @{
37  */
38 
39 /**
40  * \class CLexer
41  * \brief Represent Lexer/Scanner class for C (11) language parsing
42  *
43  * Lexer defined to add some extra function to the scanner class from flex.
44  * Flex itself creates yyFlexLexer class, which we renamed using macros to
45  * NmodlFlexLexer. But we change the context of the generated yylex() function
46  * because the yylex() defined in NmodlFlexLexer has no parameters. Also, note
47  * that implementation of the member functions are in nmodl.l file due to use
48  * of macros.
49  */
50 class CLexer: public CFlexLexer {
51  public:
52  /** Reference to driver object which contains this lexer instance. This is
53  * used for error reporting and checking macro definitions. */
54 
55  /**
56  * \brief Reference to driver object where this lexer resides
57  *
58  * The driver object can be used from lexer to store/retrieve some
59  * global information. Currently this is used for storing all token
60  * encountered during lexing.
61  */
63 
64  /// location of the parsed token
65  location loc;
66 
67  /// \name Ctor & dtor
68  /// \{
69 
70  /**
71  * \brief CLexer constructor
72  *
73  * @param driver CDriver where this lexer resides
74  * @param in Input stream from where tokens will be read
75  * @param out Output stream where output will be sent
76  */
77  explicit CLexer(CDriver& driver, std::istream* in = nullptr, std::ostream* out = nullptr)
78  : CFlexLexer(in, out)
79  , driver(driver) {}
80 
81  ~CLexer() override = default;
82 
83  /// \}
84 
85  /**
86  * \brief Function for lexer to scan token (replacement for \c yylex())
87  *
88  * This is main lexing function generated by `flex` according to the macro
89  * declaration \c YY_DECL. The generated bison parser then calls this virtual
90  * function to fetch new tokens. Note that \c yylex() has different declaration
91  * and hence can't be used for new lexer.
92  *
93  * @return Symbol encapsulating parsed token
94  */
96 
97  /**
98  * \brief Get the type of token just parsed
99  *
100  * Check if parsed token is either \c enum, \c typedef or \c identifier.
101  *
102  * \attention Not needed anymore and could be removed?
103  *
104  * @return Symbol encapsulating parsed token
105  */
107 };
108 
109 /** @} */ // end of lexer
110 
111 } // namespace parser
112 } // namespace nmodl
nmodl::parser::CLexer
Represent Lexer/Scanner class for C (11) language parsing.
Definition: c11_lexer.hpp:50
nmodl::parser::CLexer::~CLexer
~CLexer() override=default
nmodl::parser::CLexer::CLexer
CLexer(CDriver &driver, std::istream *in=nullptr, std::ostream *out=nullptr)
CLexer constructor.
Definition: c11_lexer.hpp:77
nmodl::parser::CLexer::get_token_type
CParser::symbol_type get_token_type()
Get the type of token just parsed.
nmodl
encapsulates code generation backend implementations
Definition: ast_common.hpp:26
nmodl::parser::CLexer::loc
location loc
location of the parsed token
Definition: c11_lexer.hpp:65
nmodl::parser::CDriver
Class that binds all pieces together for parsing C verbatim blocks.
Definition: c11_driver.hpp:37
nmodl::parser::CLexer::driver
CDriver & driver
Reference to driver object which contains this lexer instance.
Definition: c11_lexer.hpp:62
symbol_type
void symbol_type(const std::string &name, T &value)
Definition: modtoken.cpp:32
nmodl::parser::CLexer::next_token
virtual CParser::symbol_type next_token()
Function for lexer to scan token (replacement for yylex())