Skip to content

Commit 2dd0b7f

Browse files
authored
Merge pull request #406 from alephium/import_in_import
`Import` in `import`
2 parents c7cd4ab + d394025 commit 2dd0b7f

File tree

3 files changed

+136
-2
lines changed

3 files changed

+136
-2
lines changed

compiler-access/src/main/scala/org/alephium/ralph/lsp/access/compiler/parser/soft/StringLiteralParser.scala

+4-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ private object StringLiteralParser {
1818
Index ~
1919
TokenParser.parseOrFail(Token.Quote) ~
2020
Index ~
21-
TextParser.parseOrFail(Token.ForwardSlash, Token.Quote).? ~
21+
// TODO: Other keywords such as `Contract`, `Abstract` etc might also need stopping here.
22+
// Add them only if LSP features require them.
23+
TextParser.parseOrFail(Token.ForwardSlash, Token.Quote, Token.Import).? ~
2224
path.rep ~
2325
TokenParser.parse(Token.Quote) ~
2426
Index
@@ -46,7 +48,7 @@ private object StringLiteralParser {
4648
P {
4749
Index ~
4850
TokenParser.parseOrFail(Token.ForwardSlash) ~
49-
TextParser.parse(Token.ForwardSlash, Token.Quote) ~
51+
TextParser.parse(Token.ForwardSlash, Token.Quote, Token.Import) ~
5052
Index
5153
} map {
5254
case (from, slash, text, to) =>

compiler-access/src/test/scala/org/alephium/ralph/lsp/access/compiler/parser/soft/ImportParserSpec.scala

+129
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,133 @@ class ImportParserSpec extends AnyWordSpec with Matchers {
9494
}
9595
}
9696

97+
"stop" when {
98+
"the head path is the `import` keyword" in {
99+
val root =
100+
parseSoft("import \"import")
101+
102+
// both `import` statements are parsed as individual `import` ASTs
103+
root.parts should have size 2
104+
105+
root shouldBe
106+
SoftAST.RootBlock(
107+
index = indexOf(">>import \"import<<"),
108+
parts = Seq(
109+
SoftAST.Import(
110+
index = indexOf(">>import \"<<import"),
111+
importToken = Import(">>import<< \"import"),
112+
postImportSpace = Some(Space("import>> <<\"import")),
113+
string = Some(
114+
SoftAST.StringLiteral(
115+
index = indexOf("import >>\"<<import"),
116+
startQuote = Quote("import >>\"<<import"),
117+
head = None,
118+
tail = Seq.empty,
119+
endQuote = TokenExpected("import \">><<import", Token.Quote)
120+
)
121+
)
122+
),
123+
SoftAST.Import(
124+
index = indexOf("import \">>import<<"),
125+
importToken = Import("import \">>import<<"),
126+
postImportSpace = None,
127+
string = None
128+
)
129+
)
130+
)
131+
}
132+
133+
"the second path is the `import` keyword" when {
134+
"without closing quote" in {
135+
val root =
136+
parseSoft("import \"nft/import")
137+
138+
// both `import` statements are parsed as individual `import` ASTs
139+
root.parts should have size 2
140+
141+
root shouldBe
142+
SoftAST.RootBlock(
143+
index = indexOf(">>import \"nft/import<<"),
144+
parts = Seq(
145+
SoftAST.Import(
146+
index = indexOf(">>import \"nft/<<import"),
147+
importToken = Import(">>import<< \"nft/import"),
148+
postImportSpace = Some(Space("import>> <<\"nft/import")),
149+
string = Some(
150+
SoftAST.StringLiteral(
151+
index = indexOf("import >>\"nft/<<import"),
152+
startQuote = Quote("import >>\"<<nft/import"),
153+
head = Some(CodeString("import \">>nft<</import")),
154+
tail = Seq(
155+
SoftAST.Path(
156+
index = indexOf("import \"nft>>/<<import"),
157+
slash = ForwardSlash("import \"nft>>/<<import"),
158+
text = CodeStringExpected("import \"nft/>><<import")
159+
)
160+
),
161+
endQuote = TokenExpected("import \"nft/>><<import", Token.Quote)
162+
)
163+
)
164+
),
165+
SoftAST.Import(
166+
index = indexOf("import \"nft/>>import<<"),
167+
importToken = Import("import \"nft/>>import<<"),
168+
postImportSpace = None,
169+
string = None
170+
)
171+
)
172+
)
173+
}
174+
175+
"with closing quote" in {
176+
val root =
177+
parseSoft("import \"nft/import\"")
178+
179+
// both `import` statements are parsed as individual `import` ASTs
180+
root.parts should have size 2
181+
182+
root shouldBe
183+
SoftAST.RootBlock(
184+
index = indexOf(">>import \"nft/import\"<<"),
185+
parts = Seq(
186+
SoftAST.Import(
187+
index = indexOf(">>import \"nft/<<import\""),
188+
importToken = Import(">>import<< \"nft/import\""),
189+
postImportSpace = Some(Space("import>> <<\"nft/import\"")),
190+
string = Some(
191+
SoftAST.StringLiteral(
192+
index = indexOf("import >>\"nft/<<import\""),
193+
startQuote = Quote("import >>\"<<nft/import\""),
194+
head = Some(CodeString("import \">>nft<</import\"")),
195+
tail = Seq(
196+
SoftAST.Path(
197+
index = indexOf("import \"nft>>/<<import\""),
198+
slash = ForwardSlash("import \"nft>>/<<import\""),
199+
text = CodeStringExpected("import \"nft/>><<import\"")
200+
)
201+
),
202+
endQuote = TokenExpected("import \"nft/>><<import\"", Token.Quote)
203+
)
204+
)
205+
),
206+
SoftAST.Import(
207+
index = indexOf("import \"nft/>>import\"<<"),
208+
importToken = Import("import \"nft/>>import<<\""),
209+
postImportSpace = None,
210+
string = Some(
211+
SoftAST.StringLiteral(
212+
index = indexOf("import \"nft/import>>\"<<"),
213+
startQuote = Quote("import \"nft/import>>\"<<"),
214+
head = None,
215+
tail = Seq.empty,
216+
endQuote = TokenExpected("import \"nft/import\">><<", Token.Quote)
217+
)
218+
)
219+
)
220+
)
221+
)
222+
}
223+
}
224+
}
225+
97226
}

compiler-access/src/test/scala/org/alephium/ralph/lsp/access/compiler/parser/soft/ast/TestSoftAST.scala

+3
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,9 @@ object TestSoftAST {
813813
text = text
814814
)
815815

816+
def CodeStringExpected(text: String): SoftAST.CodeStringExpected =
817+
SoftAST.CodeStringExpected(indexOf(text))
818+
816819
def TokenDocumented[T <: Token](
817820
index: SourceIndex,
818821
token: T): SoftAST.TokenDocumented[T] =

0 commit comments

Comments
 (0)