Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…ina-lang into issue-37750
  • Loading branch information
SasinduDilshara committed Jun 15, 2023
2 parents 6bd984d + 4509b09 commit a6f7948
Show file tree
Hide file tree
Showing 413 changed files with 20,011 additions and 3,362 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/nightly_publish_timestamped_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
fail-fast: false
max-parallel: 4
matrix:
branch: ['master', '2201.2.x', '2201.3.x', '2201.4.x']
branch: ['master', '2201.4.x', '2201.5.x', '2201.6.x']
timeout-minutes: 240
if: github.repository_owner == 'ballerina-platform'
steps:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
*
* @since 2.0.0
*/
class EvaluatorImpl extends Evaluator {
public class EvaluatorImpl extends Evaluator {
protected EvaluatorImpl(Preprocessor preprocessor, TreeParser treeParser,
SnippetFactory snippetFactory, ShellSnippetsInvoker invoker) {
super(preprocessor, treeParser, snippetFactory, invoker);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import io.ballerina.compiler.syntax.tree.AnnotationDeclarationNode;
import io.ballerina.compiler.syntax.tree.AssignmentStatementNode;
import io.ballerina.compiler.syntax.tree.BindingPatternNode;
import io.ballerina.compiler.syntax.tree.BlockStatementNode;
import io.ballerina.compiler.syntax.tree.BreakStatementNode;
import io.ballerina.compiler.syntax.tree.ClassDefinitionNode;
Expand Down Expand Up @@ -47,6 +48,7 @@
import io.ballerina.compiler.syntax.tree.Node;
import io.ballerina.compiler.syntax.tree.NodeFactory;
import io.ballerina.compiler.syntax.tree.NodeList;
import io.ballerina.compiler.syntax.tree.NodeParser;
import io.ballerina.compiler.syntax.tree.PanicStatementNode;
import io.ballerina.compiler.syntax.tree.RetryStatementNode;
import io.ballerina.compiler.syntax.tree.ReturnStatementNode;
Expand All @@ -57,6 +59,8 @@
import io.ballerina.compiler.syntax.tree.Token;
import io.ballerina.compiler.syntax.tree.TransactionStatementNode;
import io.ballerina.compiler.syntax.tree.TypeDefinitionNode;
import io.ballerina.compiler.syntax.tree.TypeDescriptorNode;
import io.ballerina.compiler.syntax.tree.TypedBindingPatternNode;
import io.ballerina.compiler.syntax.tree.VariableDeclarationNode;
import io.ballerina.compiler.syntax.tree.WhileStatementNode;
import io.ballerina.compiler.syntax.tree.XMLNamespaceDeclarationNode;
Expand All @@ -68,6 +72,8 @@
import io.ballerina.shell.snippet.types.StatementSnippet;
import io.ballerina.shell.snippet.types.VariableDeclarationSnippet;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
Expand All @@ -78,6 +84,7 @@
public class BasicSnippetFactory extends SnippetFactory {
// Create a caches of Syntax kind -> Variable types/Sub snippets that are known.
// These will be used when identifying the variable type/snippet type.
private int varFunctionCount = 0;
protected static final Map<Class<?>, SnippetSubKind> MODULE_MEM_DCLNS = Map.ofEntries(
Map.entry(FunctionDefinitionNode.class, SnippetSubKind.FUNCTION_DEFINITION),
Map.entry(ListenerDeclarationNode.class, SnippetSubKind.LISTENER_DECLARATION),
Expand Down Expand Up @@ -125,8 +132,10 @@ public ImportDeclarationSnippet createImportSnippet(Node node) {
}

@Override
public VariableDeclarationSnippet createVariableDeclarationSnippet(Node node) {
public List<VariableDeclarationSnippet> createVariableDeclarationSnippets(Node node) {
ModuleVariableDeclarationNode dclnNode;
ModuleVariableDeclarationNode newDclnNode = null;
List<VariableDeclarationSnippet> snippets = new ArrayList<>();
if (containsIsolated(node)) {
addErrorDiagnostic("Isolation not allowed in the Ballerina shell");
return null;
Expand All @@ -136,18 +145,44 @@ public VariableDeclarationSnippet createVariableDeclarationSnippet(Node node) {
dclnNode = (ModuleVariableDeclarationNode) node;
} else if (node instanceof VariableDeclarationNode) {
VariableDeclarationNode varNode = (VariableDeclarationNode) node;
VariableDeclarationNode newNode = null;
NodeList<Token> qualifiers = NodeFactory.createEmptyNodeList();
// Only final qualifier is transferred.
// It is the only possible qualifier that can be transferred.
if (varNode.finalKeyword().isPresent()) {
qualifiers = NodeFactory.createNodeList(varNode.finalKeyword().get());
}

if (((VariableDeclarationNode) node).initializer().get().kind() == SyntaxKind.QUERY_ACTION) {
TypedBindingPatternNode typedBindingPatternNode = varNode.typedBindingPattern();
TypeDescriptorNode typeDescriptorNode = typedBindingPatternNode.typeDescriptor();
BindingPatternNode bindingPatternNode = typedBindingPatternNode.bindingPattern();
String functionPart = "function() returns ";
String functionBody = varNode.initializer().get().toString();
String functionString = "var " + "f_" + varFunctionCount + " = " + functionPart
+ typeDescriptorNode.toString() + "{" + typeDescriptorNode + bindingPatternNode.toString()
+ " = " + functionBody + ";" + "return " + bindingPatternNode + ";};";
varNode = (VariableDeclarationNode) NodeParser.parseStatement(functionString);
newNode = (VariableDeclarationNode) NodeParser
.parseStatement(typeDescriptorNode + " " + bindingPatternNode
+ " = f_" + varFunctionCount + "();");
}

varFunctionCount += 1;
dclnNode = NodeFactory.createModuleVariableDeclarationNode(
NodeFactory.createMetadataNode(null, varNode.annotations()), null,
qualifiers, varNode.typedBindingPattern(),
varNode.equalsToken().orElse(null), varNode.initializer().orElse(null),
varNode.semicolonToken()
);
if (newNode != null) {
newDclnNode = NodeFactory.createModuleVariableDeclarationNode(
NodeFactory.createMetadataNode(null, newNode.annotations()), null,
qualifiers, newNode.typedBindingPattern(),
newNode.equalsToken().orElse(null), newNode.initializer().orElse(null),
newNode.semicolonToken()
);
}
} else {
return null;
}
Expand All @@ -158,7 +193,13 @@ public VariableDeclarationSnippet createVariableDeclarationSnippet(Node node) {
"Give an initial value for your variable.");
return null;
}
return new VariableDeclarationSnippet(dclnNode);

snippets.add(new VariableDeclarationSnippet(dclnNode));
if (newDclnNode != null) {
snippets.add(new VariableDeclarationSnippet(newDclnNode));
}

return snippets;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package io.ballerina.shell.snippet.factory;

import io.ballerina.compiler.syntax.tree.Node;
import io.ballerina.compiler.syntax.tree.SyntaxKind;
import io.ballerina.shell.DiagnosticReporter;
import io.ballerina.shell.exceptions.SnippetException;
import io.ballerina.shell.snippet.Snippet;
Expand All @@ -40,43 +41,60 @@
*/
public abstract class SnippetFactory extends DiagnosticReporter {
/**
* Creates a snippet from the given node.
* Creates snippets from the given node.
* This will throw and error if the resultant snippet is an erroneous snippet.
*
* @param nodes Root node to create snippet from.
* @return Snippet that contains the node.
* @return Snippets that contains the node.
* @throws SnippetException If couldn't identify the snippet.
*/
public Collection<Snippet> createSnippets(Collection<Node> nodes) throws SnippetException {
List<Snippet> snippets = new ArrayList<>();
for (Node node : nodes) {
snippets.add(createSnippet(node));
snippets.addAll(createSnippets(node));
}
return snippets;
}

/**
* Creates a snippet from the given node.
* Creates snippets from the given node.
* This will throw and error if the resultant snippet is an erroneous snippet.
*
* @param node Root node to create snippet from.
* @return Snippet that contains the node.
* @return Snippets that contains the node.
* @throws SnippetException If couldn't identify the snippet.
*/
public Snippet createSnippet(Node node) throws SnippetException {
public Collection<Snippet> createSnippets(Node node) throws SnippetException {
List<SnippetCreator> functions = new ArrayList<>();
functions.add(this::createImportSnippet);
functions.add(this::createVariableDeclarationSnippet);
functions.add(this::createModuleMemberDeclarationSnippet);
functions.add(this::createStatementSnippet);
functions.add(this::createExpressionSnippet);
Snippet snippet;
List<Snippet> snippetList = new ArrayList<>();

if (node.kind() == SyntaxKind.LOCAL_VAR_DECL || node.kind() == SyntaxKind.MODULE_VAR_DECL) {
List<VariableDeclarationSnippet> variableDeclarationSnippets = createVariableDeclarationSnippets(node);
if (variableDeclarationSnippets != null) {
for (Snippet varSnippets : variableDeclarationSnippets) {
if (varSnippets != null) {
String message = String.format("Node identified as a %s snippet.", varSnippets.getKind());
addDebugDiagnostic(message);
snippetList.add(varSnippets);
}
}

return snippetList;
}
}

for (SnippetCreator function : functions) {
snippet = function.create(node);
if (snippet != null) {
String message = String.format("Node identified as a %s snippet.", snippet.getKind());
addDebugDiagnostic(message);
return snippet;
snippetList.add(snippet);
return snippetList;
}
}

Expand All @@ -94,13 +112,13 @@ public Snippet createSnippet(Node node) throws SnippetException {
public abstract ImportDeclarationSnippet createImportSnippet(Node node);

/**
* Create a variable declaration snippet from the given node.
* Create variable declaration snippets from the given node.
* Returns null if snippet cannot be created.
*
* @param node Root node to create snippet from.
* @return Snippet that contains the node.
* @return Snippets that contains the node.
*/
public abstract VariableDeclarationSnippet createVariableDeclarationSnippet(Node node);
public abstract List<VariableDeclarationSnippet> createVariableDeclarationSnippets(Node node);

/**
* Create a module member declaration snippet from the given node.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class QueryEvaluatorTest extends AbstractEvaluatorTest {
private static final String QUERY_EXPR_EVALUATOR_TESTCASE = "testcases/evaluator/query.expr.json";
private static final String QUERY_JOIN_EVALUATOR_TESTCASE = "testcases/evaluator/query.join.json";

private static final String QUERY_ACTION_EVALUATOR_TESTCASE = "testcases/evaluator/query.action.json";

@Test
public void testEvaluateQueryExpr() throws BallerinaShellException {
// TODO: (#28389) Compiler crashes for module-level query expression which uses var
Expand All @@ -41,4 +43,9 @@ public void testEvaluateQueryJoin() throws BallerinaShellException {
// TODO: (#28390) Module-level query expressions don't work
testEvaluate(QUERY_JOIN_EVALUATOR_TESTCASE);
}

@Test
public void testEvaluateQueryAction() throws BallerinaShellException {
testEvaluate(QUERY_ACTION_EVALUATOR_TESTCASE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private void testProcess(String fileName, SnippetKind kind) {
try {
Collection<Node> nodes = treeParser.parse(testCase.getInput());
for (Node node : nodes) {
Snippet snippet = snippetFactory.createSnippet(node);
Snippet snippet = snippetFactory.createSnippets(node).stream().findFirst().orElse(null);
Assert.assertNotNull(snippet, testCase.getName());
Assert.assertTrue(testCase.isAccepted(), testCase.getName());
Assert.assertEquals(snippet.getKind(), kind, testCase.getName());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[
{
"description": "Define nums list.",
"code": "int[] nums = [1, 2, 3, 4];"
},
{
"description": "Define numsTimes10 list.",
"code": "int[] numsTimes10 = [];"
},
{
"description": "Query action evaluation.",
"code": "error? e = from var i in nums do { numsTimes10.push(i * 10); };"
},
{
"description": "Evaluate list.",
"code": "numsTimes10",
"expr": "[10,20,30,40]"
},
{
"description": "Define numsTimes20 list.",
"code": "int[] numsTimes20 = [];"
},
{
"description": "Query action evaluation.",
"code": "error? e = from var i in nums do { numsTimes20.push(i * 20); };"
},
{
"description": "Evaluate list.",
"code": "numsTimes20",
"expr": "[20,40,60,80]"
},
{
"description": "Check error vaue.",
"code": "e is error",
"expr": "false"
}
]
Loading

0 comments on commit a6f7948

Please sign in to comment.