11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/ADT/SmallVector.h"
13 #include "llvm/ADT/StringRef.h"
15 using namespace circt;
20 enum class TokenKind {
39 Token formToken(TokenKind kind,
size_t n) {
40 auto s = spelling.take_front(n);
41 spelling = spelling.drop_front(n);
45 bool isIDChar(
char c) {
46 return c !=
' ' && c !=
'~' && c !=
'|' && c !=
':' && c !=
'>' &&
47 c !=
'/' && c !=
'[' && c !=
']' && c !=
'.';
52 auto size = spelling.size();
54 return formToken(TokenKind::Eof, pos);
55 auto current = spelling[pos];
58 return formToken(TokenKind::Tilde, pos + 1);
60 return formToken(TokenKind::Bar, pos + 1);
62 return formToken(TokenKind::Colon, pos + 1);
64 return formToken(TokenKind::Greater, pos + 1);
66 return formToken(TokenKind::Slash, pos + 1);
68 return formToken(TokenKind::LBrace, pos + 1);
70 return formToken(TokenKind::RBrace, pos + 1);
72 return formToken(TokenKind::Period, pos + 1);
78 while (pos != size && isIDChar(spelling[pos]))
80 return formToken(TokenKind::Id, pos);
83 ParseResult parseToken(TokenKind kind, StringRef &result) {
85 auto token = parseToken();
86 if (token.kind != kind)
87 return spelling = save, failure();
88 result = token.spelling;
92 ParseResult parseToken(TokenKind kind) {
94 return parseToken(kind, ignore);
99 if (succeeded(parseToken(TokenKind::Eof)))
101 SmallVector<PathElement> path;
105 if (parseToken(TokenKind::Id, module) || parseToken(TokenKind::Slash) ||
106 parseToken(TokenKind::Id, instance))
110 if (parseToken(TokenKind::Colon))
114 if (parseToken(TokenKind::Eof))
120 ParseResult
parsePath(PathAttr &pathAttr, StringAttr &moduleAttr,
121 StringAttr &refAttr, StringAttr &fieldAttr) {
122 SmallVector<PathElement> path;
125 if (parseToken(TokenKind::Id, module))
127 if (parseToken(TokenKind::Slash))
130 if (parseToken(TokenKind::Id, instance) || parseToken(TokenKind::Colon))
137 if (succeeded(parseToken(TokenKind::Greater))) {
139 if (parseToken(TokenKind::Id, ref))
143 SmallString<64> field;
145 if (succeeded(parseToken(TokenKind::LBrace))) {
147 if (parseToken(TokenKind::Id,
id) || parseToken(TokenKind::RBrace))
152 }
else if (succeeded(parseToken(TokenKind::Period))) {
154 if (parseToken(TokenKind::Id,
id))
167 if (parseToken(TokenKind::Eof))
172 MLIRContext *context;
179 return PathParser{context, spelling}.parseBasePath(path);
183 PathAttr &path, StringAttr &module,
184 StringAttr &ref, StringAttr &field) {
185 return PathParser{context, spelling}.parsePath(path, module, ref, field);
Direction get(bool isOutput)
Returns an output direction if isOutput is true, otherwise returns an input direction.
ParseResult parsePath(MLIRContext *context, StringRef spelling, PathAttr &path, StringAttr &module, StringAttr &ref, StringAttr &field)
Parse a target string in to a path.
ParseResult parseBasePath(MLIRContext *context, StringRef spelling, PathAttr &path)
Parse a target string of the form "Foo/bar:Bar/baz" in to a base path.
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.