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

DYN-7112: Fix DS parser to error early on finding import statements in code block nodes #15288

Merged
merged 17 commits into from
Nov 8, 2024
Merged
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ runtimeGeneratedExtension/
obj/
int/
packages/
bin/

#find -type d -name bin -exec git rm -r {} \;

Expand Down
85 changes: 41 additions & 44 deletions src/Engine/ProtoCore/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ public void AddDLLExtensionAppType(System.Type type)
/// </summary>
public bool IsParsingCodeBlockNode { get; set; }

internal bool IsParsingInTestMode { get; set; }

// This is the AST node list of default imported libraries needed for Graph Compiler
public CodeBlockNode ImportNodes { get; set; }

Expand Down Expand Up @@ -424,19 +426,54 @@ public void ResetForPrecompilation()
ForLoopBlockIndex = Constants.kInvalidIndex;
}

private void ResetAll(Options options)
// The unique subscript for SSA temporaries
// TODO Jun: Organize these variables in core into proper enums/classes/struct
public int SSASubscript { get; set; }
public Guid SSASubscript_GUID { get; set; }
public int SSAExprUID { get; set; }
public int SSAExpressionUID { get; set; }

/// <summary>
/// ExpressionUID is used as the unique id to identify an expression
/// It is incremented by 1 after mapping its current value to an expression
/// </summary>
public int ExpressionUID { get; set; }

private int tempVarId = 0;
private int tempLanguageId = 0;


// TODO Jun: Cleansify me - i dont need to be here
public AssociativeNode AssocNode { get; set; }
public int watchStartPC { get; set; }


//
// TODO Jun: This is the expression interpreters executable.
// It must be moved to its own core, whre each core is an instance of a compiler+interpreter
//
public Executable ExprInterpreterExe { get; set; }
public int watchFunctionScope { get; set; }
public int watchBaseOffset { get; set; }
public List<SymbolNode> watchSymbolList { get; set; }

public CodeGen assocCodegen { get; set; }

public TextWriter ExecutionLog { get; set; }

public Core(Options options)
{
Heap = new Heap();
//Rmem = new RuntimeMemory(Heap);
Configurations = new Dictionary<string, object>();
DllTypesToLoad = new List<System.Type>();

Options = options;

Compilers = new Dictionary<Language, Compiler>();
ClassIndex = Constants.kInvalidIndex;

FunctionTable = new FunctionTable();
FunctionTable = new FunctionTable();


watchFunctionScope = Constants.kInvalidIndex;
Expand Down Expand Up @@ -498,7 +535,7 @@ private void ResetAll(Options options)


ParsingMode = ParseMode.Normal;

IsParsingPreloadedAssembly = false;
IsParsingCodeBlockNode = false;
ImportHandler = null;
Expand All @@ -515,46 +552,6 @@ private void ResetAll(Options options)
newEntryPoint = Constants.kInvalidIndex;
}

// The unique subscript for SSA temporaries
// TODO Jun: Organize these variables in core into proper enums/classes/struct
public int SSASubscript { get; set; }
public Guid SSASubscript_GUID { get; set; }
public int SSAExprUID { get; set; }
public int SSAExpressionUID { get; set; }

/// <summary>
/// ExpressionUID is used as the unique id to identify an expression
/// It is incremented by 1 after mapping its current value to an expression
/// </summary>
public int ExpressionUID { get; set; }

private int tempVarId = 0;
private int tempLanguageId = 0;


// TODO Jun: Cleansify me - i dont need to be here
public AssociativeNode AssocNode { get; set; }
public int watchStartPC { get; set; }


//
// TODO Jun: This is the expression interpreters executable.
// It must be moved to its own core, whre each core is an instance of a compiler+interpreter
//
public Executable ExprInterpreterExe { get; set; }
public int watchFunctionScope { get; set; }
public int watchBaseOffset { get; set; }
public List<SymbolNode> watchSymbolList { get; set; }

public CodeGen assocCodegen { get; set; }

public TextWriter ExecutionLog { get; set; }

public Core(Options options)
{
ResetAll(options);
}

public SymbolNode GetFirstVisibleSymbol(string name, int classscope, int function, CodeBlock codeblock)
{
Validity.Assert(null != codeblock);
Expand Down
15 changes: 10 additions & 5 deletions src/Engine/ProtoCore/Parser/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
//#define ENABLE_INC_DEC_FIX
using System;
using System.Collections.Generic;
using System.IO;
using ProtoCore.AST;
using ProtoCore.AST.AssociativeAST;
using ProtoCore.DSASM;
using ProtoCore.Properties;
using ProtoCore.Utils;
using ProtoCore.Properties;

namespace ProtoCore.DesignScriptParser
{
namespace ProtoCore.DesignScriptParser {



public class Parser {
public class Parser {
public const int _EOF = 0;
public const int _ident = 1;
public const int _number = 2;
Expand Down Expand Up @@ -791,7 +791,7 @@ bool WeakSeparator(int n, int syFol, int repFol) {
void DesignScriptParser() {
Node node = null;
Hydrogen(out node);
if (!core.IsParsingPreloadedAssembly && !core.IsParsingCodeBlockNode && !builtinMethodsLoaded)
if (!core.IsParsingPreloadedAssembly && !core.IsParsingCodeBlockNode && !builtinMethodsLoaded && !core.IsParsingInTestMode)
{
CoreUtils.InsertPredefinedAndBuiltinMethods(core, node as CodeBlockNode);
root = node;
Expand Down Expand Up @@ -890,6 +890,11 @@ void Hydrogen(out Node codeBlockNode) {
}

void Import_Statement(out ProtoCore.AST.AssociativeAST.AssociativeNode node) {
if (core.IsParsingCodeBlockNode)
{
core.BuildStatus.LogSemanticError(Resources.ImportStatementNotSupported);
}

while (!(la.kind == 0 || la.kind == 34)) {SynErr(66); Get();}
string moduleName = "", typeName = "", alias = "";

Expand Down
7 changes: 3 additions & 4 deletions src/Engine/ProtoCore/Parser/Scanner.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@

using System;
using System.Collections;
using System.IO;
using System.Collections;

namespace ProtoCore.DesignScriptParser
{
namespace ProtoCore.DesignScriptParser {

public class Token {
public class Token {
public int kind; // token kind
public int pos; // token position in bytes in the source text (starting at 0)
public int charPos; // token position in characters in the source text (starting at 0)
Expand Down
11 changes: 7 additions & 4 deletions src/Engine/ProtoCore/Parser/atg/Associative.atg
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,13 @@ Hydrogen<out Node codeBlockNode>
.

Import_Statement<out ProtoCore.AST.AssociativeAST.AssociativeNode node>
= SYNC
= (.
if (core.IsParsingCodeBlockNode)
{
core.BuildStatus.LogSemanticError(Resources.ImportStatementNotSupported);
}
.)
SYNC
(.
string moduleName = "", typeName = "", alias = "";
.)
Expand Down Expand Up @@ -2028,9 +2034,6 @@ Associative_IdentifierList<out ProtoCore.AST.AssociativeAST.AssociativeNode node
}
else
{
string rhsName = null;
ProtoCore.AST.AssociativeAST.ExprListNode dimList = null;
int dim = 0;
if (rnode is ProtoCore.AST.AssociativeAST.FunctionCallNode)
{
ProtoCore.AST.AssociativeAST.FunctionCallNode rhsFNode = rnode as ProtoCore.AST.AssociativeAST.FunctionCallNode;
Expand Down
2 changes: 1 addition & 1 deletion src/Engine/ProtoCore/Parser/atg/Start.atg
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ DesignScriptParser
= (. Node node = null; .)
Hydrogen<out node>
(.
if (!core.IsParsingPreloadedAssembly && !core.IsParsingCodeBlockNode && !builtinMethodsLoaded)
if (!core.IsParsingPreloadedAssembly && !core.IsParsingCodeBlockNode && !builtinMethodsLoaded && !core.IsParsingInTestMode)
{
CoreUtils.InsertPredefinedAndBuiltinMethods(core, node as CodeBlockNode);
root = node;
Expand Down
1 change: 1 addition & 0 deletions src/Engine/ProtoCore/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@
[assembly: InternalsVisibleTo("ProtoScript")]
[assembly: InternalsVisibleTo("NodeDocumentationMarkdownGenerator")]
[assembly: InternalsVisibleTo("ProtoAssociative")]
[assembly: InternalsVisibleTo("IntegrationTests")]

10 changes: 3 additions & 7 deletions src/Engine/ProtoCore/Utils/CompilerUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ private static bool CompileCodeBlockAST(Core core, ParseParam parseParams, IDict


bool parsingPreloadFlag = core.IsParsingPreloadedAssembly;
bool parsingCbnFlag = core.IsParsingPreloadedAssembly;
bool parsingCbnFlag = core.IsParsingCodeBlockNode;
core.IsParsingPreloadedAssembly = false;
core.IsParsingCodeBlockNode = true;

Expand Down Expand Up @@ -383,7 +383,7 @@ private static void ParseUserCode(ProtoCore.Core core, string expression, string
}
catch
{
// For class declarations, import statements etc. that are currently ignored
// For class declarations etc. that are currently ignored
}
}

Expand Down Expand Up @@ -415,11 +415,7 @@ private static void ParseUserCodeCore(Core core, string expression, out List<Ass
// Append the temporaries only if it is not a function def or class decl
bool isFunctionOrClassDef = n is FunctionDefinitionNode;

if (n is ImportNode)
{
core.BuildStatus.LogSemanticError(Resources.ImportStatementNotSupported);
}
else if (n is ClassDeclNode)
if (n is ClassDeclNode)
{
core.BuildStatus.LogSemanticError(Resources.ClassDeclarationNotSupported);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Engine/ProtoCore/Utils/ParserUtils.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -140,8 +140,8 @@ private static Core CodeBlockParserCore()
var core = new Core(new Options())
{
ParsingMode = ParseMode.AllowNonAssignment,
IsParsingPreloadedAssembly = false,
IsParsingCodeBlockNode = true,
IsParsingPreloadedAssembly = false
};
return core;
}
Expand Down
17 changes: 17 additions & 0 deletions test/DynamoCoreTests/CodeBlockNodeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using CoreNodeModels;
using Dynamo.Engine.CodeCompletion;
using Dynamo.Graph;
Expand Down Expand Up @@ -1800,6 +1801,22 @@ public void TypedIdentifier_AssignedToDifferentType_ThrowsWarning()
ProtoCore.Properties.Resources.kConvertNonConvertibleTypes)));
}

[Test]
public void ImportStatementInCodeBlock_DoesNotLoadAssemblyIntoProcess()
{
var codeBlockNode = CreateCodeBlockNode();
var guid = codeBlockNode.GUID.ToString();

string assemblyName = "FFITarget";
UpdateCodeBlockNodeContent(codeBlockNode, $"import(\"{assemblyName}.dll\")");

var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();

var ffiTargetAsm = loadedAssemblies.Any(assembly => assembly.GetName().Name.Equals(assemblyName, StringComparison.OrdinalIgnoreCase));

Assert.False(ffiTargetAsm);
}

[Test]
public void TypedIdentifier_AssignedToDifferentType_ThrowsWarning2()
{
Expand Down
Loading
Loading