CIRCT 23.0.0git
Loading...
Searching...
No Matches
FIRLexer.h
Go to the documentation of this file.
1//===- FIRLexer.h - .fir lexer and token definitions ------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Defines the a Lexer and Token interface for .fir files.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef FIRTOMLIR_FIRLEXER_H
14#define FIRTOMLIR_FIRLEXER_H
15
16#include "circt/Support/LLVM.h"
17#include "mlir/IR/BuiltinAttributes.h"
18#include "llvm/Support/SourceMgr.h"
19
20namespace mlir {
21class MLIRContext;
22class Location;
23} // namespace mlir
24
25namespace circt {
26namespace firrtl {
27
28/// This represents a specific token for .fir files.
29class FIRToken {
30public:
31 enum Kind {
32#define TOK_MARKER(NAME) NAME,
33#define TOK_IDENTIFIER(NAME) NAME,
34#define TOK_LITERAL(NAME) NAME,
35#define TOK_PUNCTUATION(NAME, SPELLING) NAME,
36#define TOK_KEYWORD(SPELLING) kw_##SPELLING,
37#define TOK_LPKEYWORD(SPELLING) lp_##SPELLING,
38#define TOK_LESSKEYWORD(SPELLING) langle_##SPELLING,
39#include "FIRTokenKinds.def"
40 };
41
43
44 // Return the bytes that make up this token.
45 StringRef getSpelling() const { return spelling; }
46
47 // Token classification.
48 Kind getKind() const { return kind; }
49 bool is(Kind K) const { return kind == K; }
50
51 bool isAny(Kind k1, Kind k2) const { return is(k1) || is(k2); }
52
53 /// Return true if this token is one of the specified kinds.
54 template <typename... T>
55 bool isAny(Kind k1, Kind k2, Kind k3, T... others) const {
56 if (is(k1))
57 return true;
58 return isAny(k2, k3, others...);
59 }
60
61 bool isNot(Kind k) const { return kind != k; }
62
63 /// Return true if this token isn't one of the specified kinds.
64 template <typename... T>
65 bool isNot(Kind k1, Kind k2, T... others) const {
66 return !isAny(k1, k2, others...);
67 }
68
69 /// Return true if this is one of the keyword token kinds (e.g. kw_wire).
70 bool isKeyword() const;
71
72 /// Given a token containing a string literal, return its value, including
73 /// removing the quote characters and unescaping the contents of the string.
74 /// The lexer has already verified that this token is valid.
75 std::string getStringValue() const;
76 static std::string getStringValue(StringRef spelling);
77
78 /// Given a token containing a verbatim string, return its value, including
79 /// removing the quote characters and unescaping the quotes of the string. The
80 /// lexer has already verified that this token is valid.
81 std::string getVerbatimStringValue() const;
82 static std::string getVerbatimStringValue(StringRef spelling);
83
84 // Location processing.
85 llvm::SMLoc getLoc() const;
86 llvm::SMLoc getEndLoc() const;
87 llvm::SMRange getLocRange() const;
88
89private:
90 /// Discriminator that indicates the sort of token this is.
92
93 /// A reference to the entire token contents; this is always a pointer into
94 /// a memory buffer owned by the source manager.
95 StringRef spelling;
96};
97
98class FIRLexerCursor;
99
100/// This implements a lexer for .fir files.
101class FIRLexer {
102public:
103 FIRLexer(const llvm::SourceMgr &sourceMgr, mlir::MLIRContext *context);
104
105 const llvm::SourceMgr &getSourceMgr() const { return sourceMgr; }
106
107 /// Move to the next valid token.
109
110 const FIRToken &getToken() const { return curToken; }
111
112 mlir::Location translateLocation(llvm::SMLoc loc);
113
114 /// Return the indentation level of the specified token or None if this token
115 /// is preceded by another token on the same line.
116 std::optional<unsigned> getIndentation(const FIRToken &tok) const;
117
118 /// Get an opaque pointer into the lexer state that can be restored later.
120
121private:
123
124 // Helpers.
125 FIRToken formToken(FIRToken::Kind kind, const char *tokStart) {
126 return FIRToken(kind, StringRef(tokStart, curPtr - tokStart));
127 }
128
129 FIRToken emitError(const char *loc, const Twine &message);
130
131 // Lexer implementation methods.
132 FIRToken lexFileInfo(const char *tokStart);
133 FIRToken lexInlineAnnotation(const char *tokStart);
134 FIRToken lexIdentifierOrKeyword(const char *tokStart);
135 FIRToken lexNumber(const char *tokStart);
136 void skipComment();
137 FIRToken lexString(const char *tokStart, bool isVerbatim);
138
139 const llvm::SourceMgr &sourceMgr;
140 const mlir::StringAttr bufferNameIdentifier;
141
142 StringRef curBuffer;
143 const char *curPtr;
144
145 /// This is the next token that hasn't been consumed yet.
147
148 FIRLexer(const FIRLexer &) = delete;
149 void operator=(const FIRLexer &) = delete;
150 friend class FIRLexerCursor;
151};
152
153/// This is the state captured for a lexer cursor.
155public:
157 : state(lexer.curPtr), curToken(lexer.getToken()) {}
158
159 void restore(FIRLexer &lexer) {
160 lexer.curPtr = state;
161 lexer.curToken = curToken;
162 }
163
164private:
165 const char *state;
167};
168
170 return FIRLexerCursor(*this);
171}
172
173} // namespace firrtl
174} // namespace circt
175
176#endif // FIRTOMLIR_FIRLEXER_H
static std::unique_ptr< Context > context
This is the state captured for a lexer cursor.
Definition FIRLexer.h:154
FIRLexerCursor(const FIRLexer &lexer)
Definition FIRLexer.h:156
void restore(FIRLexer &lexer)
Definition FIRLexer.h:159
This implements a lexer for .fir files.
Definition FIRLexer.h:101
FIRToken lexFileInfo(const char *tokStart)
Lex a file info specifier.
Definition FIRLexer.cpp:319
void lexToken()
Move to the next valid token.
Definition FIRLexer.h:108
const llvm::SourceMgr & getSourceMgr() const
Definition FIRLexer.h:105
FIRToken lexIdentifierOrKeyword(const char *tokStart)
Lex an identifier or keyword that starts with a letter.
Definition FIRLexer.cpp:391
const llvm::SourceMgr & sourceMgr
Definition FIRLexer.h:139
FIRToken curToken
This is the next token that hasn't been consumed yet.
Definition FIRLexer.h:146
FIRToken formToken(FIRToken::Kind kind, const char *tokStart)
Definition FIRLexer.h:125
FIRToken lexNumber(const char *tokStart)
Lex a number literal.
Definition FIRLexer.cpp:525
FIRToken lexString(const char *tokStart, bool isVerbatim)
StringLit ::= '"' UnquotedString? '"' VerbatimStringLit ::= '\'' UnquotedString? '\'' UnquotedString ...
Definition FIRLexer.cpp:476
friend class FIRLexerCursor
Definition FIRLexer.h:150
FIRLexer(const FIRLexer &)=delete
FIRLexerCursor getCursor() const
Get an opaque pointer into the lexer state that can be restored later.
Definition FIRLexer.h:169
const char * curPtr
Definition FIRLexer.h:143
std::optional< unsigned > getIndentation(const FIRToken &tok) const
Return the indentation level of the specified token or None if this token is preceded by another toke...
Definition FIRLexer.cpp:185
void skipComment()
Skip a comment line, starting with a ';' and going to end of line.
Definition FIRLexer.cpp:451
FIRToken emitError(const char *loc, const Twine &message)
Emit an error message and return a FIRToken::error token.
Definition FIRLexer.cpp:179
FIRLexer(const llvm::SourceMgr &sourceMgr, mlir::MLIRContext *context)
mlir::Location translateLocation(llvm::SMLoc loc)
Encode the specified source location information into a Location object for attachment to the IR or e...
Definition FIRLexer.cpp:170
void operator=(const FIRLexer &)=delete
FIRToken lexInlineAnnotation(const char *tokStart)
Lex a non-standard inline Annotation file.
Definition FIRLexer.cpp:350
const mlir::StringAttr bufferNameIdentifier
Definition FIRLexer.h:140
const FIRToken & getToken() const
Definition FIRLexer.h:110
This represents a specific token for .fir files.
Definition FIRLexer.h:29
bool isNot(Kind k) const
Definition FIRLexer.h:61
std::string getVerbatimStringValue() const
Given a token containing a verbatim string, return its value, including removing the quote characters...
Definition FIRLexer.cpp:117
StringRef getSpelling() const
Definition FIRLexer.h:45
bool isNot(Kind k1, Kind k2, T... others) const
Return true if this token isn't one of the specified kinds.
Definition FIRLexer.h:65
bool is(Kind K) const
Definition FIRLexer.h:49
FIRToken(Kind kind, StringRef spelling)
Definition FIRLexer.h:42
StringRef spelling
A reference to the entire token contents; this is always a pointer into a memory buffer owned by the ...
Definition FIRLexer.h:95
llvm::SMRange getLocRange() const
Definition FIRLexer.cpp:41
bool isAny(Kind k1, Kind k2, Kind k3, T... others) const
Return true if this token is one of the specified kinds.
Definition FIRLexer.h:55
bool isAny(Kind k1, Kind k2) const
Definition FIRLexer.h:51
std::string getStringValue() const
Given a token containing a string literal, return its value, including removing the quote characters ...
Definition FIRLexer.cpp:58
Kind kind
Discriminator that indicates the sort of token this is.
Definition FIRLexer.h:91
llvm::SMLoc getEndLoc() const
Definition FIRLexer.cpp:37
Kind getKind() const
Definition FIRLexer.h:48
llvm::SMLoc getLoc() const
Definition FIRLexer.cpp:33
bool isKeyword() const
Return true if this is one of the keyword token kinds (e.g. kw_wire).
Definition FIRLexer.cpp:44
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.