diff options
| author | Tim Foley <tfoley@nvidia.com> | 2017-06-27 14:22:01 -0700 |
|---|---|---|
| committer | Tim Foley <tfoley@nvidia.com> | 2017-06-27 14:22:01 -0700 |
| commit | 9eee1175d9c3089ba53ebed12171bf57a40e80e3 (patch) | |
| tree | 4f3f1e8b03a1ea3a515c955d3b7e5f6a6b6283f0 /source/slang/parser.cpp | |
| parent | 97f253189af41f117e2de17093904cbb45202d46 (diff) | |
Allow "dotted" import paths
The code:
__import foo.bar;
will try to import from a file matching "foo/bar.slang".
I also went ahead and allowed a raw string literal in and import:
__import "foo/bar";
(In the latter case, an explicit `/` must be used instead of `.`)
Diffstat (limited to 'source/slang/parser.cpp')
| -rw-r--r-- | source/slang/parser.cpp | 38 |
1 files changed, 32 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( |
