Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

Commit 348374f

Browse files
Hugues Valoisjakebailey
authored andcommitted
Add back implementation of Parser.ParseInteractiveCode. (#1148)
1 parent 92bd44a commit 348374f

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

src/Parsing/Impl/Parser.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,56 @@ public static Parser CreateParser(Stream stream, PythonLanguageVersion version,
146146
//file_input: (Newline | stmt)* ENDMARKER
147147
public PythonAst ParseFile(Uri module = null) => ParseFileWorker(module);
148148

149+
//[stmt_list] Newline | compound_stmt Newline
150+
//stmt_list ::= simple_stmt (";" simple_stmt)* [";"]
151+
//compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
152+
//Returns a simple or coumpound_stmt or null if input is incomplete
153+
/// <summary>
154+
/// Parse one or more lines of interactive input
155+
/// </summary>
156+
/// <returns>null if input is not yet valid but could be with more lines</returns>
157+
public PythonAst ParseInteractiveCode(Uri module, out ParseResult properties) {
158+
bool parsingMultiLineCmpdStmt;
159+
160+
properties = ParseResult.Complete;
161+
162+
StartParsing();
163+
Statement ret = InternalParseInteractiveInput(out parsingMultiLineCmpdStmt, out bool isEmptyStmt);
164+
165+
if (ErrorCode == 0) {
166+
if (isEmptyStmt) {
167+
properties = ParseResult.Empty;
168+
} else if (parsingMultiLineCmpdStmt) {
169+
properties = ParseResult.IncompleteStatement;
170+
}
171+
172+
if (isEmptyStmt) {
173+
return null;
174+
}
175+
176+
return CreateAst(module, ret);
177+
} else {
178+
if ((ErrorCode & ErrorCodes.IncompleteMask) != 0) {
179+
if ((ErrorCode & ErrorCodes.IncompleteToken) != 0) {
180+
properties = ParseResult.IncompleteToken;
181+
return null;
182+
}
183+
184+
if ((ErrorCode & ErrorCodes.IncompleteStatement) != 0) {
185+
if (parsingMultiLineCmpdStmt) {
186+
properties = ParseResult.IncompleteStatement;
187+
} else {
188+
properties = ParseResult.IncompleteToken;
189+
}
190+
return null;
191+
}
192+
}
193+
194+
properties = ParseResult.Invalid;
195+
return null;
196+
}
197+
}
198+
149199
public Expression ParseFStrSubExpr() {
150200
_alwaysAllowContextDependentSyntax = true;
151201
StartParsing();

src/Parsing/Test/ParserTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,26 @@ public void FStringErrors() {
128128
);
129129
}
130130

131+
[DataRow("True", ParseResult.Complete)]
132+
[DataRow("if True:", ParseResult.IncompleteStatement)]
133+
[DataRow("if True", ParseResult.Invalid)]
134+
[DataRow("", ParseResult.Empty)]
135+
[DataTestMethod, Priority(0)]
136+
public void InteractiveCode(string code, ParseResult expectedResult) {
137+
foreach (var version in AllVersions) {
138+
var module = new Uri("file:///interactive");
139+
var parser = Parser.CreateParser(new StringReader(code), version);
140+
var ast = parser.ParseInteractiveCode(module, out var result);
141+
result.Should().Be(expectedResult);
142+
if (expectedResult == ParseResult.Complete) {
143+
ast.Should().NotBeNull();
144+
ast.Module.Should().BeEquivalentTo(module);
145+
} else {
146+
ast.Should().BeNull();
147+
}
148+
}
149+
}
150+
131151
[TestMethod, Priority(0)]
132152
public void FStrings() {
133153
foreach (var version in V36AndUp) {

0 commit comments

Comments
 (0)