Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import in import #406

Merged
merged 3 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ private object StringLiteralParser {
Index ~
TokenParser.parseOrFail(Token.Quote) ~
Index ~
TextParser.parseOrFail(Token.ForwardSlash, Token.Quote).? ~
// TODO: Other keywords such as `Contract`, `Abstract` etc might also need stopping here.
// Add them only if LSP features require them.
TextParser.parseOrFail(Token.ForwardSlash, Token.Quote, Token.Import).? ~
path.rep ~
TokenParser.parse(Token.Quote) ~
Index
Expand Down Expand Up @@ -46,7 +48,7 @@ private object StringLiteralParser {
P {
Index ~
TokenParser.parseOrFail(Token.ForwardSlash) ~
TextParser.parse(Token.ForwardSlash, Token.Quote) ~
TextParser.parse(Token.ForwardSlash, Token.Quote, Token.Import) ~
Index
} map {
case (from, slash, text, to) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,133 @@ class ImportParserSpec extends AnyWordSpec with Matchers {
}
}

"stop" when {
"the head path is the `import` keyword" in {
val root =
parseSoft("import \"import")

// both `import` statements are parsed as individual `import` ASTs
root.parts should have size 2

root shouldBe
SoftAST.RootBlock(
index = indexOf(">>import \"import<<"),
parts = Seq(
SoftAST.Import(
index = indexOf(">>import \"<<import"),
importToken = Import(">>import<< \"import"),
postImportSpace = Some(Space("import>> <<\"import")),
string = Some(
SoftAST.StringLiteral(
index = indexOf("import >>\"<<import"),
startQuote = Quote("import >>\"<<import"),
head = None,
tail = Seq.empty,
endQuote = TokenExpected("import \">><<import", Token.Quote)
)
)
),
SoftAST.Import(
index = indexOf("import \">>import<<"),
importToken = Import("import \">>import<<"),
postImportSpace = None,
string = None
)
)
)
}

"the second path is the `import` keyword" when {
"without closing quote" in {
val root =
parseSoft("import \"nft/import")

// both `import` statements are parsed as individual `import` ASTs
root.parts should have size 2

root shouldBe
SoftAST.RootBlock(
index = indexOf(">>import \"nft/import<<"),
parts = Seq(
SoftAST.Import(
index = indexOf(">>import \"nft/<<import"),
importToken = Import(">>import<< \"nft/import"),
postImportSpace = Some(Space("import>> <<\"nft/import")),
string = Some(
SoftAST.StringLiteral(
index = indexOf("import >>\"nft/<<import"),
startQuote = Quote("import >>\"<<nft/import"),
head = Some(CodeString("import \">>nft<</import")),
tail = Seq(
SoftAST.Path(
index = indexOf("import \"nft>>/<<import"),
slash = ForwardSlash("import \"nft>>/<<import"),
text = CodeStringExpected("import \"nft/>><<import")
)
),
endQuote = TokenExpected("import \"nft/>><<import", Token.Quote)
)
)
),
SoftAST.Import(
index = indexOf("import \"nft/>>import<<"),
importToken = Import("import \"nft/>>import<<"),
postImportSpace = None,
string = None
)
)
)
}

"with closing quote" in {
val root =
parseSoft("import \"nft/import\"")

// both `import` statements are parsed as individual `import` ASTs
root.parts should have size 2

root shouldBe
SoftAST.RootBlock(
index = indexOf(">>import \"nft/import\"<<"),
parts = Seq(
SoftAST.Import(
index = indexOf(">>import \"nft/<<import\""),
importToken = Import(">>import<< \"nft/import\""),
postImportSpace = Some(Space("import>> <<\"nft/import\"")),
string = Some(
SoftAST.StringLiteral(
index = indexOf("import >>\"nft/<<import\""),
startQuote = Quote("import >>\"<<nft/import\""),
head = Some(CodeString("import \">>nft<</import\"")),
tail = Seq(
SoftAST.Path(
index = indexOf("import \"nft>>/<<import\""),
slash = ForwardSlash("import \"nft>>/<<import\""),
text = CodeStringExpected("import \"nft/>><<import\"")
)
),
endQuote = TokenExpected("import \"nft/>><<import\"", Token.Quote)
)
)
),
SoftAST.Import(
index = indexOf("import \"nft/>>import\"<<"),
importToken = Import("import \"nft/>>import<<\""),
postImportSpace = None,
string = Some(
SoftAST.StringLiteral(
index = indexOf("import \"nft/import>>\"<<"),
startQuote = Quote("import \"nft/import>>\"<<"),
head = None,
tail = Seq.empty,
endQuote = TokenExpected("import \"nft/import\">><<", Token.Quote)
)
)
)
)
)
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,9 @@ object TestSoftAST {
text = text
)

def CodeStringExpected(text: String): SoftAST.CodeStringExpected =
SoftAST.CodeStringExpected(indexOf(text))

def TokenDocumented[T <: Token](
index: SourceIndex,
token: T): SoftAST.TokenDocumented[T] =
Expand Down