User Guide
modtoken.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 <iomanip>
11 #include <iostream>
12 #include <sstream>
13 #include <string>
14 
15 #include "parser/nmodl/location.hh"
16 
17 namespace nmodl {
18 
19 /**
20  * @defgroup token Token Implementation
21  * @brief All token related implementation
22  *
23  * @defgroup token_modtoken Token Classes
24  * @ingroup token
25  * @brief Represent token classes for various scanners
26  * @{
27  */
28 
29 /**
30  * \class ModToken
31  * \brief Represent token returned by scanner
32  *
33  * Every token returned by lexer is represented by ModToken. Some tokens are
34  * also externally defined names like \c dt, \c t. These names are defined in
35  * NEURON and hence we set external property to true. Also, location class
36  * represent the position of the token in nmodl file. By default location is
37  * initialized to <c>line,column</c> as <c>1,1</c>. Some tokens are explicitly
38  * added during compiler passes. Hence we set the position to <c>0,0</c> so that we can
39  * distinguish them from other tokens produced by lexer.
40  *
41  * \todo
42  * - \ref LocationType object is copyable except if we specify the stream name.
43  * It would be good to track filename when we go for multi-channel optimization
44  * and code generation.
45  *
46  * \see
47  * - @ref token_test for how ModToken can be used
48  */
49 
50 class ModToken {
51  using LocationType = nmodl::parser::location;
52 
53  private:
54  /// name of the token
55  std::string name;
56 
57  /// token value returned by lexer
58  int token = -1;
59 
60  /// position of token in the mod file
62 
63  /// true if token is externally defined variable (e.g. \c t, \c dt in NEURON)
64  bool external = false;
65 
66  public:
67  /// \name Ctor & dtor
68  /// \{
69 
71  : pos(nullptr, 0){};
72 
73  explicit ModToken(bool ext)
74  : pos(nullptr, 0)
75  , external(ext) {}
76 
77  ModToken(std::string name, int token, LocationType& pos)
78  : name(std::move(name))
79  , token(token)
80  , pos(pos) {}
81 
82  /// \}
83 
84  /// Return a new instance of token
85  ModToken* clone() const {
86  return new ModToken(*this);
87  }
88 
89  /// Return token text from mod file
90  std::string text() const {
91  return name;
92  }
93 
94  /// Return token type from lexer
95  int type() const {
96  return token;
97  }
98 
99  /// Return line number where token started in the mod file
100  int start_line() const {
101  return pos.begin.line;
102  }
103 
104  /// Return start of column number where token appear in the mod file
105  int start_column() const {
106  return pos.begin.column;
107  }
108 
109  /**
110  * Return position of the token as string
111  *
112  * This is used used by other passes like scope checker to print the location
113  * of token in mod files :
114  * \li external token position : `[EXTERNAL]`
115  * \li token with unknown position : `[UNKNOWN]`
116  * \li token with known position : `[line_num.start_column-end_column]`
117  */
118  std::string position() const;
119 
120  /**
121  * Overload \c << operator to print object
122  *
123  * Overload ostream operator to print token in the form :
124  *
125  * \code
126  * token at [line.start_column-end_column] type token
127  * \endcode
128  *
129  * For example:
130  *
131  * \code
132  * v at [118.9-14] type 376
133  * \endcode
134  */
135  friend std::ostream& operator<<(std::ostream& stream, const ModToken& mt);
136 
137  /**
138  * Overload \c + operator to create an object whose position starts
139  * from the start line and column of the first adder and finishes
140  * at the end line and column of the second adder
141  *
142  *
143  * For example:
144  *
145  * \code
146  * a at [118.9-14]
147  * b at [121.4-5]
148  * \endcode
149  *
150  * Output:
151  *
152  * \code
153  * (a + b) at [118.9-121.5]
154  * \endcode
155  */
156  friend ModToken operator+(ModToken const& adder1, ModToken const& adder2);
157 };
158 
159 /** @} */ // end of token_modtoken
160 
161 } // namespace nmodl
nmodl::ModToken::ModToken
ModToken()
Definition: modtoken.hpp:70
nmodl::ModToken::operator<<
friend std::ostream & operator<<(std::ostream &stream, const ModToken &mt)
Overload << operator to print object.
Definition: modtoken.cpp:26
nmodl::ModToken::token
int token
token value returned by lexer
Definition: modtoken.hpp:58
nmodl::ModToken::type
int type() const
Return token type from lexer.
Definition: modtoken.hpp:95
nmodl
encapsulates code generation backend implementations
Definition: ast_common.hpp:26
nmodl::ModToken::operator+
friend ModToken operator+(ModToken const &adder1, ModToken const &adder2)
Overload + operator to create an object whose position starts from the start line and column of the f...
Definition: modtoken.cpp:32
nmodl::ModToken::name
std::string name
name of the token
Definition: modtoken.hpp:55
nmodl::ModToken::ModToken
ModToken(bool ext)
Definition: modtoken.hpp:73
nmodl::ModToken::position
std::string position() const
Return position of the token as string.
Definition: modtoken.cpp:14
nmodl::ModToken::ModToken
ModToken(std::string name, int token, LocationType &pos)
Definition: modtoken.hpp:77
nmodl::ModToken::external
bool external
true if token is externally defined variable (e.g. t, dt in NEURON)
Definition: modtoken.hpp:64
nmodl::ModToken::pos
LocationType pos
position of token in the mod file
Definition: modtoken.hpp:61
nmodl::ModToken::text
std::string text() const
Return token text from mod file.
Definition: modtoken.hpp:90
nmodl::ModToken::start_line
int start_line() const
Return line number where token started in the mod file.
Definition: modtoken.hpp:100
nmodl::ModToken::clone
ModToken * clone() const
Return a new instance of token.
Definition: modtoken.hpp:85
nmodl::ModToken::start_column
int start_column() const
Return start of column number where token appear in the mod file.
Definition: modtoken.hpp:105
nmodl::ModToken
Represent token returned by scanner.
Definition: modtoken.hpp:50
nmodl::ModToken::LocationType
nmodl::parser::location LocationType
Definition: modtoken.hpp:51