User Guide
unit_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/unit/unit_parser.hpp"
11 
12 /**
13  * Flex expects the declaration of yylex to be defined in the macro YY_DECL
14  * and Unit parser class expects it to be declared.
15  */
16 #ifndef YY_DECL
17 #define YY_DECL nmodl::parser::UnitParser::symbol_type nmodl::parser::UnitLexer::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 yyFlexLexer 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 UnitFlexLexer
28 #include "FlexLexer.h"
29 #endif
30 
31 namespace nmodl {
32 namespace parser {
33 
34 /**
35  * @addtogroup lexer
36  * @addtogroup units
37  * @{
38  */
39 
40 /**
41  * \class UnitLexer
42  * \brief Represent Lexer/Scanner class for Units parsing
43  *
44  * Lexer defined to add some extra function to the scanner class from flex.
45  * Flex itself creates yyFlexLexer class, which we renamed using macros to
46  * UnitFlexLexer. But we change the context of the generated yylex() function
47  * because the yylex() defined in UnitFlexLexer has no parameters.
48  */
49 class UnitLexer: public UnitFlexLexer {
50  public:
51  /// location of the parsed token
52  location loc;
53 
54  /// \name Ctor & dtor
55  /// \{
56 
57  /**
58  * \brief UnitLexer constructor
59  *
60  * @param driver UnitDriver where this lexer resides
61  * @param in Input stream from where tokens will be read
62  * @param out Output stream where output will be sent
63  */
64  explicit UnitLexer(UnitDriver& /* driver */,
65  std::istream* in = nullptr,
66  std::ostream* out = nullptr)
67  : UnitFlexLexer(in, out) {}
68 
69  ~UnitLexer() override = default;
70 
71  /// \}
72 
73  /**
74  * \brief Function for lexer to scan token (replacement for \c yylex())
75  *
76  * This is main lexing function generated by `flex` according to the macro
77  * declaration \c YY_DECL. The generated bison parser then calls this virtual
78  * function to fetch new tokens. Note that \c yylex() has different declaration
79  * and hence can't be used for new lexer.
80  *
81  * @return Symbol encapsulating parsed token
82  */
84 };
85 
86 } // namespace parser
87 } // namespace nmodl
nmodl
encapsulates code generation backend implementations
Definition: ast_common.hpp:26
nmodl::parser::UnitLexer::loc
location loc
location of the parsed token
Definition: unit_lexer.hpp:52
nmodl::parser::UnitLexer::next_token
virtual UnitParser::symbol_type next_token()
Function for lexer to scan token (replacement for yylex())
nmodl::parser::UnitLexer::UnitLexer
UnitLexer(UnitDriver &, std::istream *in=nullptr, std::ostream *out=nullptr)
UnitLexer constructor.
Definition: unit_lexer.hpp:64
symbol_type
void symbol_type(const std::string &name, T &value)
Definition: modtoken.cpp:32
nmodl::parser::UnitLexer::~UnitLexer
~UnitLexer() override=default
nmodl::parser::UnitLexer
Represent Lexer/Scanner class for Units parsing.
Definition: unit_lexer.hpp:49
nmodl::parser::UnitDriver
Class that binds all pieces together for parsing C units.
Definition: unit_driver.hpp:39