diff options
| -rw-r--r-- | source/slang/parser.cpp | 38 | ||||
| -rw-r--r-- | tests/front-end/import-subdir.slang | 7 | ||||
| -rw-r--r-- | tests/front-end/subdir/import-subdir-a.slang | 5 |
3 files changed, 44 insertions, 6 deletions
diff --git a/source/slang/parser.cpp b/source/slang/parser.cpp index 1d843c821..3ffbc1263 100644 --- a/source/slang/parser.cpp +++ b/source/slang/parser.cpp @@ -794,15 +794,46 @@ namespace Slang } } + static TokenType peekTokenType(Parser* parser) + { + return parser->tokenReader.PeekTokenType(); + } + static RefPtr<Decl> parseImportDecl( Parser* parser) { parser->ReadToken("__import"); auto decl = new ImportDecl(); - decl->nameToken = parser->ReadToken(TokenType::Identifier); decl->scope = parser->currentScope; + if (peekTokenType(parser) == TokenType::StringLiterial) + { + auto nameToken = parser->ReadToken(TokenType::StringLiterial); + nameToken.Content = getStringLiteralTokenValue(nameToken); + decl->nameToken = nameToken; + } + else + { + auto nameToken = parser->ReadToken(TokenType::Identifier); + + // We allow a dotted format for the name, as sugar + if (peekTokenType(parser) == TokenType::Dot) + { + StringBuilder sb; + sb << nameToken.Content; + while (AdvanceIf(parser, TokenType::Dot)) + { + sb << "/"; + sb << parser->ReadToken(TokenType::Identifier).Content; + } + + nameToken.Content = sb.ProduceString(); + } + + decl->nameToken = nameToken; + } + parser->ReadToken(TokenType::Semicolon); return decl; @@ -3093,11 +3124,6 @@ namespace Slang #endif } - static TokenType peekTokenType(Parser* parser) - { - return parser->tokenReader.PeekTokenType(); - } - // We *might* be looking at an application of a generic to arguments, // but we need to disambiguate to make sure. static RefPtr<ExpressionSyntaxNode> maybeParseGenericApp( diff --git a/tests/front-end/import-subdir.slang b/tests/front-end/import-subdir.slang new file mode 100644 index 000000000..0d3931a82 --- /dev/null +++ b/tests/front-end/import-subdir.slang @@ -0,0 +1,7 @@ +//TEST:SIMPLE: + +// Confirming that we can use "dot notation" to import via a subdirectory + +__import subdir.import_subdir_a; + +float bar(float x) { return foo(x); }
\ No newline at end of file diff --git a/tests/front-end/subdir/import-subdir-a.slang b/tests/front-end/subdir/import-subdir-a.slang new file mode 100644 index 000000000..3962d4662 --- /dev/null +++ b/tests/front-end/subdir/import-subdir-a.slang @@ -0,0 +1,5 @@ +//TEST_IGNORE_FILE: + +// This is the imported code. + +float foo(float x) { return x; }
\ No newline at end of file |
