From ec543fe7cbe7fe7c91474821ae82605c3b0d585b Mon Sep 17 00:00:00 2001 From: kavindu Date: Sun, 2 Aug 2020 21:37:55 +0530 Subject: [PATCH 01/20] Create the AST --- .../model/clauses/MatchClauseNode.java | 49 +++++++++ .../tree/bindingpattern/BindingPattern.java | 7 ++ .../bindingpattern/CaptureBindingPattern.java | 19 ++++ .../bindingpattern/ListBindingPattern.java | 11 ++ .../model/tree/expressions/MatchGuard.java | 24 ++++ .../tree/matchpatterns/ConstPattern.java | 11 ++ .../tree/matchpatterns/MatchPattern.java | 7 ++ .../VarBindingPatternMatchPattern.java | 11 ++ .../matchpatterns/WildCardMatchPattern.java | 7 ++ .../tree/statements/MatchStatementNode.java | 33 ++++++ .../bindingpatterns/BLangBindingPattern.java | 8 ++ .../BLangCaptureBindingPattern.java | 45 ++++++++ .../BLangListBindingPattern.java | 33 ++++++ .../tree/clauses/BLangMatchClause.java | 104 ++++++++++++++++++ .../tree/expressions/BLangMatchGuard.java | 31 ++++++ .../tree/matchpatterns/BLangConstPattern.java | 32 ++++++ .../tree/matchpatterns/BLangMatchPattern.java | 12 ++ .../BLangVarBindingPatternMatchPattern.java | 33 ++++++ .../BLangWildCardMatchPattern.java | 20 ++++ .../tree/statements/BLangMatchStatement.java | 64 +++++++++++ 20 files changed, 561 insertions(+) create mode 100644 compiler/ballerina-lang/src/main/java/org/ballerinalang/model/clauses/MatchClauseNode.java create mode 100644 compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/BindingPattern.java create mode 100644 compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/CaptureBindingPattern.java create mode 100644 compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/ListBindingPattern.java create mode 100644 compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/expressions/MatchGuard.java create mode 100644 compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/ConstPattern.java create mode 100644 compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/MatchPattern.java create mode 100644 compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/VarBindingPatternMatchPattern.java create mode 100644 compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/WildCardMatchPattern.java create mode 100644 compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/statements/MatchStatementNode.java create mode 100644 compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangBindingPattern.java create mode 100644 compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangCaptureBindingPattern.java create mode 100644 compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangListBindingPattern.java create mode 100644 compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/clauses/BLangMatchClause.java create mode 100644 compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/expressions/BLangMatchGuard.java create mode 100644 compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangConstPattern.java create mode 100644 compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangMatchPattern.java create mode 100644 compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangVarBindingPatternMatchPattern.java create mode 100644 compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangWildCardMatchPattern.java create mode 100644 compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/statements/BLangMatchStatement.java diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/clauses/MatchClauseNode.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/clauses/MatchClauseNode.java new file mode 100644 index 000000000000..382dac4e0865 --- /dev/null +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/clauses/MatchClauseNode.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.ballerinalang.model.clauses; + +import org.ballerinalang.model.tree.expressions.ExpressionNode; +import org.ballerinalang.model.tree.matchpatterns.MatchPattern; +import org.ballerinalang.model.tree.Node; +import org.ballerinalang.model.tree.expressions.MatchGuard; +import org.ballerinalang.model.tree.statements.BlockStatementNode; + +import java.util.List; + +public interface MatchClauseNode extends Node { + // TODO : add getters and setters from match-pattern-list + MatchGuard getMatchGuard(); + + void setMatchGuard(MatchGuard matchGuard); + + BlockStatementNode getBLockStatement(); + + void setBlockStatement(BlockStatementNode blockStatement); + + List getMatchPatterns(); + + void addMatchPattern(MatchPattern matchPattern); + + boolean isLastClause(); + + void setLastClause(boolean isLastClause); + + ExpressionNode getExpression(); + + void setExpression(ExpressionNode expression); +} diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/BindingPattern.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/BindingPattern.java new file mode 100644 index 000000000000..099294566805 --- /dev/null +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/BindingPattern.java @@ -0,0 +1,7 @@ +package org.ballerinalang.model.tree.bindingpattern; + +import org.ballerinalang.model.tree.Node; + +public interface BindingPattern extends Node { + +} diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/CaptureBindingPattern.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/CaptureBindingPattern.java new file mode 100644 index 000000000000..0c8ef1a5a517 --- /dev/null +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/CaptureBindingPattern.java @@ -0,0 +1,19 @@ +package org.ballerinalang.model.tree.bindingpattern; + +import org.ballerinalang.model.symbols.Symbol; +import org.ballerinalang.model.symbols.VariableSymbol; +import org.ballerinalang.model.tree.IdentifierNode; +import org.ballerinalang.model.tree.Node; +import org.ballerinalang.model.tree.expressions.ExpressionNode; + +public interface CaptureBindingPattern extends Node { + + IdentifierNode getIdentifier(); + + void setIdentifier(IdentifierNode variableName); + + VariableSymbol getSymbol(); + + void setSymbol(VariableSymbol symbol); + +} diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/ListBindingPattern.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/ListBindingPattern.java new file mode 100644 index 000000000000..d1ffdbc65b1e --- /dev/null +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/ListBindingPattern.java @@ -0,0 +1,11 @@ +package org.ballerinalang.model.tree.bindingpattern; + +import org.ballerinalang.model.tree.Node; + +import java.util.List; + +public interface ListBindingPattern extends Node { + List getBindingPatterns(); + + void addBindingPattern(BindingPattern bindingPattern); +} diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/expressions/MatchGuard.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/expressions/MatchGuard.java new file mode 100644 index 000000000000..d18a1e634a46 --- /dev/null +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/expressions/MatchGuard.java @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.ballerinalang.model.tree.expressions; + +public interface MatchGuard { + ExpressionNode getExpression(); + + void setExpression(ExpressionNode expression); +} diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/ConstPattern.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/ConstPattern.java new file mode 100644 index 000000000000..6a6ba202b4bc --- /dev/null +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/ConstPattern.java @@ -0,0 +1,11 @@ +package org.ballerinalang.model.tree.matchpatterns; + +import org.ballerinalang.model.tree.Node; +import org.ballerinalang.model.tree.expressions.ExpressionNode; + +public interface ConstPattern extends Node { + + ExpressionNode getExpresion(); + + void setExpression(ExpressionNode expression); +} diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/MatchPattern.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/MatchPattern.java new file mode 100644 index 000000000000..fb08452af36d --- /dev/null +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/MatchPattern.java @@ -0,0 +1,7 @@ +package org.ballerinalang.model.tree.matchpatterns; + +import org.ballerinalang.model.tree.Node; + +public interface MatchPattern extends Node { + +} diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/VarBindingPatternMatchPattern.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/VarBindingPatternMatchPattern.java new file mode 100644 index 000000000000..0ed0fb3d0f01 --- /dev/null +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/VarBindingPatternMatchPattern.java @@ -0,0 +1,11 @@ +package org.ballerinalang.model.tree.matchpatterns; + +import org.ballerinalang.model.tree.Node; +import org.ballerinalang.model.tree.bindingpattern.BindingPattern; + +public interface VarBindingPatternMatchPattern extends Node { + + BindingPattern getBindingPattern(); + + void setBindingPattern(BindingPattern bindingPattern); +} diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/WildCardMatchPattern.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/WildCardMatchPattern.java new file mode 100644 index 000000000000..9f8d455aec2a --- /dev/null +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/WildCardMatchPattern.java @@ -0,0 +1,7 @@ +package org.ballerinalang.model.tree.matchpatterns; + +import org.ballerinalang.model.tree.Node; + +public interface WildCardMatchPattern extends Node { + +} diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/statements/MatchStatementNode.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/statements/MatchStatementNode.java new file mode 100644 index 000000000000..fbcd2410ac3b --- /dev/null +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/statements/MatchStatementNode.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.ballerinalang.model.tree.statements; + +import org.ballerinalang.model.clauses.MatchClauseNode; +import org.ballerinalang.model.tree.expressions.ExpressionNode; + +import java.util.List; + +public interface MatchStatementNode extends StatementNode { + ExpressionNode getExpression(); + + void setExpression(ExpressionNode expression); + + List getMatchClauses(); + + void addMatchClause(MatchClauseNode matchClauseNode); +} diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangBindingPattern.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangBindingPattern.java new file mode 100644 index 000000000000..b542f2f1e50b --- /dev/null +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangBindingPattern.java @@ -0,0 +1,8 @@ +package org.wso2.ballerinalang.compiler.tree.bindingpatterns; + +import org.ballerinalang.model.tree.bindingpattern.BindingPattern; +import org.wso2.ballerinalang.compiler.tree.BLangNode; + +public abstract class BLangBindingPattern extends BLangNode implements BindingPattern { + +} diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangCaptureBindingPattern.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangCaptureBindingPattern.java new file mode 100644 index 000000000000..438277f90737 --- /dev/null +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangCaptureBindingPattern.java @@ -0,0 +1,45 @@ +package org.wso2.ballerinalang.compiler.tree.bindingpatterns; + +import org.ballerinalang.model.symbols.VariableSymbol; +import org.ballerinalang.model.tree.IdentifierNode; +import org.ballerinalang.model.tree.NodeKind; +import org.ballerinalang.model.tree.bindingpattern.CaptureBindingPattern; +import org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol; +import org.wso2.ballerinalang.compiler.tree.BLangIdentifier; +import org.wso2.ballerinalang.compiler.tree.BLangNodeVisitor; + +public class BLangCaptureBindingPattern extends BLangBindingPattern implements CaptureBindingPattern { + + BLangIdentifier identifier; + public BVarSymbol symbol; + + @Override + public void accept(BLangNodeVisitor visitor) { + visitor.visit(this); + } + + @Override + public NodeKind getKind() { + return NodeKind.CAPTURE_BINDING_PATTERN; + } + + @Override + public IdentifierNode getIdentifier() { + return identifier; + } + + @Override + public void setIdentifier(IdentifierNode identifier) { + this.identifier = (BLangIdentifier) identifier; + } + + @Override + public VariableSymbol getSymbol() { + return symbol; + } + + @Override + public void setSymbol(VariableSymbol symbol) { + this.symbol = (BVarSymbol) symbol; + } +} diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangListBindingPattern.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangListBindingPattern.java new file mode 100644 index 000000000000..ee7cd402fbaa --- /dev/null +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangListBindingPattern.java @@ -0,0 +1,33 @@ +package org.wso2.ballerinalang.compiler.tree.bindingpatterns; + +import org.ballerinalang.model.tree.NodeKind; +import org.ballerinalang.model.tree.bindingpattern.BindingPattern; +import org.ballerinalang.model.tree.bindingpattern.ListBindingPattern; +import org.wso2.ballerinalang.compiler.tree.BLangNodeVisitor; + +import java.util.ArrayList; +import java.util.List; + +public class BLangListBindingPattern extends BLangBindingPattern implements ListBindingPattern { + public List bindingPatterns = new ArrayList<>(); + + @Override + public List getBindingPatterns() { + return bindingPatterns; + } + + @Override + public void addBindingPattern(BindingPattern bindingPattern) { + bindingPatterns.add((BLangBindingPattern) bindingPattern); + } + + @Override + public void accept(BLangNodeVisitor visitor) { + + } + + @Override + public NodeKind getKind() { + return NodeKind.LIST_BINDING_PATTERN; + } +} diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/clauses/BLangMatchClause.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/clauses/BLangMatchClause.java new file mode 100644 index 000000000000..ee4656d6f6bd --- /dev/null +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/clauses/BLangMatchClause.java @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.wso2.ballerinalang.compiler.tree.clauses; + +import org.ballerinalang.model.clauses.MatchClauseNode; +import org.ballerinalang.model.tree.expressions.ExpressionNode; +import org.ballerinalang.model.tree.matchpatterns.MatchPattern; +import org.ballerinalang.model.tree.NodeKind; +import org.ballerinalang.model.tree.expressions.MatchGuard; +import org.ballerinalang.model.tree.statements.BlockStatementNode; +import org.wso2.ballerinalang.compiler.semantics.model.types.BType; +import org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangMatchPattern; +import org.wso2.ballerinalang.compiler.tree.BLangNode; +import org.wso2.ballerinalang.compiler.tree.BLangNodeVisitor; +import org.wso2.ballerinalang.compiler.tree.expressions.BLangMatchGuard; +import org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt; + +import java.util.ArrayList; +import java.util.List; + +public class BLangMatchClause extends BLangNode implements MatchClauseNode { + + public List matchPatterns = new ArrayList<>(); + public BLangMatchGuard matchGuard; + public BLangBlockStmt blockStmt; + public boolean isLastClause; + public BLangExpression expr; // This is used to keep the expression of match statement. + + @Override + public void accept(BLangNodeVisitor visitor) { + visitor.visit(this); + } + + @Override + public NodeKind getKind() { + return NodeKind.MATCH_CLAUSE; + } + + @Override + public BLangMatchGuard getMatchGuard() { + return matchGuard; + } + + @Override + public void setMatchGuard(MatchGuard matchGuard) { + this.matchGuard = (BLangMatchGuard) matchGuard; + } + + @Override + public BlockStatementNode getBLockStatement() { + return blockStmt; + } + + @Override + public void setBlockStatement(BlockStatementNode blockStatement) { + this.blockStmt = (BLangBlockStmt) blockStatement; + } + + @Override + public List getMatchPatterns() { + return matchPatterns; + } + + @Override + public void addMatchPattern(MatchPattern matchPattern) { + matchPatterns.add((BLangMatchPattern) matchPattern); + } + + @Override + public boolean isLastClause() { + return isLastClause; + } + + @Override + public void setLastClause(boolean isLastClause) { + this.isLastClause = isLastClause; + } + + @Override + public ExpressionNode getExpression() { + return expr; + } + + @Override + public void setExpression(ExpressionNode expression) { + this.expr = (BLangExpression) expression; + } +} diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/expressions/BLangMatchGuard.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/expressions/BLangMatchGuard.java new file mode 100644 index 000000000000..841db94189d1 --- /dev/null +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/expressions/BLangMatchGuard.java @@ -0,0 +1,31 @@ +package org.wso2.ballerinalang.compiler.tree.expressions; + +import org.ballerinalang.model.tree.NodeKind; +import org.ballerinalang.model.tree.expressions.ExpressionNode; +import org.ballerinalang.model.tree.expressions.MatchGuard; +import org.wso2.ballerinalang.compiler.tree.BLangNodeVisitor; + +public class BLangMatchGuard extends BLangExpression implements MatchGuard { + + public BLangExpression expr; + + @Override + public ExpressionNode getExpression() { + return expr; + } + + @Override + public void setExpression(ExpressionNode expression) { + this.expr = (BLangExpression) expression; + } + + @Override + public void accept(BLangNodeVisitor visitor) { + visitor.visit(this); + } + + @Override + public NodeKind getKind() { + return null; + } +} diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangConstPattern.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangConstPattern.java new file mode 100644 index 000000000000..07f8c157130b --- /dev/null +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangConstPattern.java @@ -0,0 +1,32 @@ +package org.wso2.ballerinalang.compiler.tree.matchpatterns; + +import org.ballerinalang.model.tree.NodeKind; +import org.ballerinalang.model.tree.expressions.ExpressionNode; +import org.ballerinalang.model.tree.matchpatterns.ConstPattern; +import org.wso2.ballerinalang.compiler.tree.BLangNodeVisitor; +import org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression; + +public class BLangConstPattern extends BLangMatchPattern implements ConstPattern { + + public BLangExpression expr; + + @Override + public void accept(BLangNodeVisitor visitor) { + visitor.visit(this); + } + + @Override + public NodeKind getKind() { + return NodeKind.CONST_MATCH_PATTERN; + } + + @Override + public ExpressionNode getExpresion() { + return expr; + } + + @Override + public void setExpression(ExpressionNode expression) { + this.expr = (BLangExpression) expression; + } +} diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangMatchPattern.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangMatchPattern.java new file mode 100644 index 000000000000..cf62aa4c5644 --- /dev/null +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangMatchPattern.java @@ -0,0 +1,12 @@ +package org.wso2.ballerinalang.compiler.tree.matchpatterns; + +import org.ballerinalang.model.tree.matchpatterns.MatchPattern; +import org.wso2.ballerinalang.compiler.semantics.model.types.BType; +import org.wso2.ballerinalang.compiler.tree.BLangNode; +import org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression; + +public abstract class BLangMatchPattern extends BLangNode implements MatchPattern { + public BLangExpression matchExpr; // TODO : should changed as action or expr + + public boolean matchGuardIsAvailable; +} diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangVarBindingPatternMatchPattern.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangVarBindingPatternMatchPattern.java new file mode 100644 index 000000000000..1e16a3d7fce3 --- /dev/null +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangVarBindingPatternMatchPattern.java @@ -0,0 +1,33 @@ +package org.wso2.ballerinalang.compiler.tree.matchpatterns; + +import org.ballerinalang.model.tree.NodeKind; +import org.ballerinalang.model.tree.bindingpattern.BindingPattern; +import org.ballerinalang.model.tree.matchpatterns.VarBindingPatternMatchPattern; +import org.wso2.ballerinalang.compiler.tree.BLangNodeVisitor; +import org.wso2.ballerinalang.compiler.tree.bindingpatterns.BLangBindingPattern; + +public class BLangVarBindingPatternMatchPattern extends BLangMatchPattern implements VarBindingPatternMatchPattern { + + BLangBindingPattern bindingPattern; + + @Override + public void accept(BLangNodeVisitor visitor) { + visitor.visit(this); + } + + @Override + public NodeKind getKind() { + + return NodeKind.VAR_BINDING_PATTERN_MATCH_PATTERN; + } + + @Override + public BLangBindingPattern getBindingPattern() { + return bindingPattern; + } + + @Override + public void setBindingPattern(BindingPattern bindingPattern) { + this.bindingPattern = (BLangBindingPattern) bindingPattern; + } +} diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangWildCardMatchPattern.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangWildCardMatchPattern.java new file mode 100644 index 000000000000..ea1b0e18d884 --- /dev/null +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangWildCardMatchPattern.java @@ -0,0 +1,20 @@ +package org.wso2.ballerinalang.compiler.tree.matchpatterns; + +import org.ballerinalang.model.tree.NodeKind; +import org.ballerinalang.model.tree.matchpatterns.WildCardMatchPattern; +import org.wso2.ballerinalang.compiler.tree.BLangNodeVisitor; + +public class BLangWildCardMatchPattern extends BLangMatchPattern implements WildCardMatchPattern { + + public boolean matchesAll; + + @Override + public void accept(BLangNodeVisitor visitor) { + visitor.visit(this); + } + + @Override + public NodeKind getKind() { + return NodeKind.WILDCARD_MATCH_PATTERN; + } +} diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/statements/BLangMatchStatement.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/statements/BLangMatchStatement.java new file mode 100644 index 000000000000..1a1df530722c --- /dev/null +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/statements/BLangMatchStatement.java @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.wso2.ballerinalang.compiler.tree.statements; + +import org.ballerinalang.model.clauses.MatchClauseNode; +import org.ballerinalang.model.tree.NodeKind; +import org.ballerinalang.model.tree.expressions.ExpressionNode; +import org.ballerinalang.model.tree.statements.MatchStatementNode; +import org.wso2.ballerinalang.compiler.tree.BLangNodeVisitor; +import org.wso2.ballerinalang.compiler.tree.clauses.BLangMatchClause; +import org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression; + +import java.util.ArrayList; +import java.util.List; + +public class BLangMatchStatement extends BLangStatement implements MatchStatementNode { + public BLangExpression expr; // TODO : replace with new node `action|expression` + public List matchClauses = new ArrayList<>(); + + @Override + public ExpressionNode getExpression() { + return expr; + } + + @Override + public void setExpression(ExpressionNode expression) { + this.expr = (BLangExpression) expression; + } + + @Override + public List getMatchClauses() { + return matchClauses; + } + + @Override + public void addMatchClause(MatchClauseNode matchClauseNode) { + matchClauses.add((BLangMatchClause) matchClauseNode); + } + + @Override + public void accept(BLangNodeVisitor visitor) { + visitor.visit(this); + } + + @Override + public NodeKind getKind() { + return NodeKind.MATCH_STATEMENT; + } +} From b3128f68b608a55dc05c718f0b085e33ae6fccde Mon Sep 17 00:00:00 2001 From: kavindu Date: Sun, 2 Aug 2020 21:39:46 +0530 Subject: [PATCH 02/20] Add the transformation --- .../org/ballerinalang/model/TreeBuilder.java | 50 ++++++++ .../ballerinalang/model/tree/NodeKind.java | 11 ++ .../compiler/parser/BLangNodeTransformer.java | 120 +++++++++++++----- .../compiler/parser/NodeCloner.java | 64 ++++++++++ .../compiler/tree/BLangNodeVisitor.java | 40 ++++++ 5 files changed, 254 insertions(+), 31 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/TreeBuilder.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/TreeBuilder.java index 0df70a51e8b5..79f24ff86f03 100644 --- a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/TreeBuilder.java +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/TreeBuilder.java @@ -21,6 +21,7 @@ import org.ballerinalang.model.clauses.InputClauseNode; import org.ballerinalang.model.clauses.LetClauseNode; import org.ballerinalang.model.clauses.LimitClauseNode; +import org.ballerinalang.model.clauses.MatchClauseNode; import org.ballerinalang.model.clauses.OnClauseNode; import org.ballerinalang.model.clauses.OnConflictClauseNode; import org.ballerinalang.model.clauses.OnFailClauseNode; @@ -51,6 +52,8 @@ import org.ballerinalang.model.tree.TupleVariableNode; import org.ballerinalang.model.tree.TypeDefinition; import org.ballerinalang.model.tree.XMLNSDeclarationNode; +import org.ballerinalang.model.tree.bindingpattern.CaptureBindingPattern; +import org.ballerinalang.model.tree.bindingpattern.ListBindingPattern; import org.ballerinalang.model.tree.expressions.AnnotAccessNode; import org.ballerinalang.model.tree.expressions.ArrowFunctionNode; import org.ballerinalang.model.tree.expressions.BinaryExpressionNode; @@ -58,6 +61,8 @@ import org.ballerinalang.model.tree.expressions.CheckedExpressionNode; import org.ballerinalang.model.tree.expressions.ElvisExpressionNode; import org.ballerinalang.model.tree.expressions.ErrorVariableReferenceNode; +import org.ballerinalang.model.tree.expressions.ExpressionNode; +import org.ballerinalang.model.tree.expressions.FailExpressionNode; import org.ballerinalang.model.tree.expressions.FieldBasedAccessNode; import org.ballerinalang.model.tree.expressions.GroupExpressionNode; import org.ballerinalang.model.tree.expressions.IndexBasedAccessNode; @@ -102,6 +107,9 @@ import org.ballerinalang.model.tree.expressions.XMLQNameNode; import org.ballerinalang.model.tree.expressions.XMLQuotedStringNode; import org.ballerinalang.model.tree.expressions.XMLTextLiteralNode; +import org.ballerinalang.model.tree.matchpatterns.ConstPattern; +import org.ballerinalang.model.tree.matchpatterns.VarBindingPatternMatchPattern; +import org.ballerinalang.model.tree.matchpatterns.WildCardMatchPattern; import org.ballerinalang.model.tree.statements.AssignmentNode; import org.ballerinalang.model.tree.statements.BlockStatementNode; import org.ballerinalang.model.tree.statements.BreakNode; @@ -121,6 +129,7 @@ import org.ballerinalang.model.tree.statements.MatchNode.MatchStaticBindingPatternNode; import org.ballerinalang.model.tree.statements.MatchNode.MatchStructuredBindingPatternNode; import org.ballerinalang.model.tree.statements.MatchNode.MatchTypedBindingPatternNode; +import org.ballerinalang.model.tree.statements.MatchStatementNode; import org.ballerinalang.model.tree.statements.PanicNode; import org.ballerinalang.model.tree.statements.QueryActionNode; import org.ballerinalang.model.tree.statements.RecordDestructureNode; @@ -156,6 +165,11 @@ import org.wso2.ballerinalang.compiler.tree.BLangBlockFunctionBody; import org.wso2.ballerinalang.compiler.tree.BLangClassDefinition; import org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit; +import org.wso2.ballerinalang.compiler.tree.bindingpatterns.BLangCaptureBindingPattern; +import org.wso2.ballerinalang.compiler.tree.bindingpatterns.BLangListBindingPattern; +import org.wso2.ballerinalang.compiler.tree.expressions.BLangMatchGuard; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangConstPattern; +import org.wso2.ballerinalang.compiler.tree.BLangEndpoint; import org.wso2.ballerinalang.compiler.tree.BLangErrorVariable; import org.wso2.ballerinalang.compiler.tree.BLangExprFunctionBody; import org.wso2.ballerinalang.compiler.tree.BLangExternalFunctionBody; @@ -174,12 +188,15 @@ import org.wso2.ballerinalang.compiler.tree.BLangTestablePackage; import org.wso2.ballerinalang.compiler.tree.BLangTupleVariable; import org.wso2.ballerinalang.compiler.tree.BLangTypeDefinition; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangVarBindingPatternMatchPattern; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangWildCardMatchPattern; import org.wso2.ballerinalang.compiler.tree.BLangXMLNS; import org.wso2.ballerinalang.compiler.tree.clauses.BLangDoClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangFromClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangJoinClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangLetClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangLimitClause; +import org.wso2.ballerinalang.compiler.tree.clauses.BLangMatchClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangOnClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangOnConflictClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangOnFailClause; @@ -270,6 +287,7 @@ import org.wso2.ballerinalang.compiler.tree.statements.BLangMatch.BLangMatchStaticBindingPatternClause; import org.wso2.ballerinalang.compiler.tree.statements.BLangMatch.BLangMatchStructuredBindingPatternClause; import org.wso2.ballerinalang.compiler.tree.statements.BLangMatch.BLangMatchTypedBindingPatternClause; +import org.wso2.ballerinalang.compiler.tree.statements.BLangMatchStatement; import org.wso2.ballerinalang.compiler.tree.statements.BLangPanic; import org.wso2.ballerinalang.compiler.tree.statements.BLangRecordDestructure; import org.wso2.ballerinalang.compiler.tree.statements.BLangRecordVariableDef; @@ -731,6 +749,38 @@ public static MatchNode createMatchStatement() { return new BLangMatch(); } + public static MatchStatementNode createMatchStatementNode() { + return new BLangMatchStatement(); + } + + public static WildCardMatchPattern createWildCardMatchPattern() { + return new BLangWildCardMatchPattern(); + } + + public static ConstPattern createConstMatchPattern() { + return new BLangConstPattern(); + } + + public static VarBindingPatternMatchPattern createVarBindingPattern() { + return new BLangVarBindingPatternMatchPattern(); + } + + public static MatchClauseNode createMatchClause() { + return new BLangMatchClause(); + } + + public static ExpressionNode createMatchGuard() { + return new BLangMatchGuard(); + } + + public static CaptureBindingPattern createCaptureBindingPattern() { + return new BLangCaptureBindingPattern(); + } + + public static ListBindingPattern createListBindingPattern() { + return new BLangListBindingPattern(); + } + public static MatchTypedBindingPatternNode createMatchStatementSimpleBindingPattern() { return new BLangMatchTypedBindingPatternClause(); } diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/NodeKind.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/NodeKind.java index 50191b08f447..e6cf3d54afd6 100644 --- a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/NodeKind.java +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/NodeKind.java @@ -145,6 +145,7 @@ public enum NodeKind { FORK_JOIN, IF, MATCH, + MATCH_STATEMENT, MATCH_TYPED_PATTERN_CLAUSE, MATCH_STATIC_PATTERN_CLAUSE, MATCH_STRUCTURED_PATTERN_CLAUSE, @@ -188,6 +189,16 @@ public enum NodeKind { ORDER_BY, ORDER_KEY, ON_FAIL, + MATCH_CLAUSE, + + /* Match patterns */ + CONST_MATCH_PATTERN, + WILDCARD_MATCH_PATTERN, + VAR_BINDING_PATTERN_MATCH_PATTERN, + + /* Binding patterns*/ + CAPTURE_BINDING_PATTERN, + LIST_BINDING_PATTERN, /* Types */ ARRAY_TYPE, diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/BLangNodeTransformer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/BLangNodeTransformer.java index 572d180899cb..8132acaecff8 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/BLangNodeTransformer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/BLangNodeTransformer.java @@ -232,6 +232,7 @@ import org.ballerinalang.model.tree.SimpleVariableNode; import org.ballerinalang.model.tree.TopLevelNode; import org.ballerinalang.model.tree.VariableNode; +import org.ballerinalang.model.tree.bindingpattern.BindingPattern; import org.ballerinalang.model.tree.expressions.ExpressionNode; import org.ballerinalang.model.tree.expressions.XMLNavigationAccess; import org.ballerinalang.model.tree.statements.VariableDefinitionNode; @@ -243,6 +244,10 @@ import org.wso2.ballerinalang.compiler.tree.BLangBlockFunctionBody; import org.wso2.ballerinalang.compiler.tree.BLangClassDefinition; import org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit; +import org.wso2.ballerinalang.compiler.tree.bindingpatterns.BLangCaptureBindingPattern; +import org.wso2.ballerinalang.compiler.tree.bindingpatterns.BLangListBindingPattern; +import org.wso2.ballerinalang.compiler.tree.expressions.BLangMatchGuard; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangConstPattern; import org.wso2.ballerinalang.compiler.tree.BLangErrorVariable; import org.wso2.ballerinalang.compiler.tree.BLangExprFunctionBody; import org.wso2.ballerinalang.compiler.tree.BLangExternalFunctionBody; @@ -264,12 +269,15 @@ import org.wso2.ballerinalang.compiler.tree.BLangTupleVariable; import org.wso2.ballerinalang.compiler.tree.BLangTypeDefinition; import org.wso2.ballerinalang.compiler.tree.BLangVariable; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangVarBindingPatternMatchPattern; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangWildCardMatchPattern; import org.wso2.ballerinalang.compiler.tree.BLangXMLNS; import org.wso2.ballerinalang.compiler.tree.clauses.BLangDoClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangFromClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangJoinClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangLetClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangLimitClause; +import org.wso2.ballerinalang.compiler.tree.clauses.BLangMatchClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangOnClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangOnConflictClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangOnFailClause; @@ -359,7 +367,7 @@ import org.wso2.ballerinalang.compiler.tree.statements.BLangForkJoin; import org.wso2.ballerinalang.compiler.tree.statements.BLangIf; import org.wso2.ballerinalang.compiler.tree.statements.BLangLock; -import org.wso2.ballerinalang.compiler.tree.statements.BLangMatch; +import org.wso2.ballerinalang.compiler.tree.statements.BLangMatchStatement; import org.wso2.ballerinalang.compiler.tree.statements.BLangPanic; import org.wso2.ballerinalang.compiler.tree.statements.BLangRecordDestructure; import org.wso2.ballerinalang.compiler.tree.statements.BLangRecordVariableDef; @@ -3738,46 +3746,96 @@ public BLangNode transform(XMLStepExpressionNode xmlStepExpressionNode) { @Override public BLangNode transform(MatchStatementNode matchStatementNode) { - BLangMatch bLangMatch = (BLangMatch) TreeBuilder.createMatchStatement(); - bLangMatch.expr = createExpression(matchStatementNode.condition()); - - for (Node matchClauseNode : matchStatementNode.matchClauses()) { - MatchClauseNode matchClause = (MatchClauseNode) matchClauseNode; - BLangMatch.BLangMatchStaticBindingPatternClause patternClause = - (BLangMatch.BLangMatchStaticBindingPatternClause) - TreeBuilder.createMatchStatementStaticBindingPattern(); - bLangMatch.patternClauses.add(patternClause); - - SeparatedNodeList matchPatterns = matchClause.matchPatterns(); - Node firstPattern = matchPatterns.get(0); - patternClause.pos = getPosition(firstPattern); - - if (matchPatterns.size() == 1) { - patternClause.literal = createLiteralOrReference(firstPattern); - } else { // spec says atleast one match pattern so this else will mean more than one - BLangBinaryExpr expr = (BLangBinaryExpr) TreeBuilder.createBinaryExpressionNode(); - expr.lhsExpr = createLiteralOrReference(firstPattern); - for (int i = 1; i < matchPatterns.size(); i++) { - if (expr.rhsExpr != null) { - BLangBinaryExpr tempExpr = ((BLangBinaryExpr) TreeBuilder.createBinaryExpressionNode()); - tempExpr.lhsExpr = expr; - expr = tempExpr; + BLangMatchStatement matchStatement = (BLangMatchStatement) TreeBuilder.createMatchStatementNode(); + BLangExpression matchStmtExpr = createExpression(matchStatementNode.condition()); + matchStatement.setExpression(matchStmtExpr); + + for (MatchClauseNode matchClauseNode : matchStatementNode.matchClauses()) { + BLangMatchClause bLangMatchClause = (BLangMatchClause) TreeBuilder.createMatchClause(); + bLangMatchClause.pos = getPosition(matchClauseNode); + bLangMatchClause.expr = matchStmtExpr; + boolean matchGuardAvailable = false; + + if (matchClauseNode.matchGuard().isPresent()) { + matchGuardAvailable = true; + BLangMatchGuard bLangMatchGuard = (BLangMatchGuard) TreeBuilder.createMatchGuard(); + bLangMatchGuard.expr = createExpression(matchClauseNode.matchGuard().get().expression()); + bLangMatchClause.setMatchGuard(bLangMatchGuard); + } + + for (Node matchPattern : matchClauseNode.matchPatterns()) { + DiagnosticPos matchPatternPos = getPosition(matchPattern); + if (matchPattern.kind() == SyntaxKind.SIMPLE_NAME_REFERENCE && + ((SimpleNameReferenceNode) matchPattern).name().text().equals("_")) { + // wildcard match + BLangWildCardMatchPattern bLangWildCardMatchPattern = + (BLangWildCardMatchPattern) TreeBuilder.createWildCardMatchPattern(); + bLangWildCardMatchPattern.matchExpr = matchStmtExpr; + bLangWildCardMatchPattern.pos = matchPatternPos; + + bLangMatchClause.addMatchPattern(bLangWildCardMatchPattern); + } else if (matchPattern.kind() == SyntaxKind.DECIMAL_INTEGER_LITERAL || + matchPattern.kind() == SyntaxKind.DECIMAL_FLOATING_POINT_LITERAL || + matchPattern.kind() == SyntaxKind.STRING_LITERAL || + matchPattern.kind() == SyntaxKind.SIMPLE_NAME_REFERENCE || + matchPattern.kind() == SyntaxKind.TRUE_KEYWORD || + matchPattern.kind() == SyntaxKind.FALSE_KEYWORD) { + BLangConstPattern bLangConstMatchPattern = + (BLangConstPattern) TreeBuilder.createConstMatchPattern(); + bLangConstMatchPattern.setExpression(createExpression(matchPattern)); + bLangConstMatchPattern.matchExpr = matchStmtExpr; + bLangConstMatchPattern.pos = matchPatternPos; + + bLangMatchClause.addMatchPattern(bLangConstMatchPattern); + } else if (matchPattern.kind() == SyntaxKind.TYPED_BINDING_PATTERN) { // var a + TypedBindingPatternNode typedBindingPatternNode = (TypedBindingPatternNode) matchPattern; + BLangVarBindingPatternMatchPattern bLangVarBindingPattern = + (BLangVarBindingPatternMatchPattern) TreeBuilder.createVarBindingPattern(); + bLangVarBindingPattern.matchExpr = matchStmtExpr; + bLangVarBindingPattern.pos = matchPatternPos; + bLangVarBindingPattern.matchGuardIsAvailable = matchGuardAvailable; + + SyntaxKind patternKind = typedBindingPatternNode.bindingPattern().kind(); + switch (patternKind) { + case CAPTURE_BINDING_PATTERN: + // TODO : check whether why cant we call the existing transform method + CaptureBindingPatternNode captureBindingPattern = + (CaptureBindingPatternNode) typedBindingPatternNode.bindingPattern(); + BLangCaptureBindingPattern bLangCaptureBindingPattern = + createCaptureBindingPattern(captureBindingPattern); + bLangVarBindingPattern.setBindingPattern(bLangCaptureBindingPattern); + break; + default: + // TODO : Remove this after all binding patterns are implemented + dlog.error(matchPatternPos, DiagnosticCode.MATCH_PATTERN_NOT_SUPPORTED); } - expr.opKind = OperatorKind.BITWISE_OR; - expr.rhsExpr = createLiteralOrReference(matchPatterns.get(i)); + bLangMatchClause.addMatchPattern(bLangVarBindingPattern); + } else { + // TODO : Remove this after all binding patterns are implemented + dlog.error(matchPatternPos, DiagnosticCode.MATCH_PATTERN_NOT_SUPPORTED); } - patternClause.literal = expr; } - patternClause.body = (BLangBlockStmt) transform(((MatchClauseNode) matchClause).blockStatement()); + bLangMatchClause.setBlockStatement((BLangBlockStmt) transform(matchClauseNode.blockStatement())); + matchStatement.addMatchClause(bLangMatchClause); } matchStatementNode.onFailClause().ifPresent(onFailClauseNode -> { - bLangMatch.setOnFailClause( + matchStatement.setOnFailClause( (org.ballerinalang.model.clauses.OnFailClauseNode) (onFailClauseNode.apply(this))); }); - return bLangMatch; + return matchStatement; + + } + + private BLangCaptureBindingPattern createCaptureBindingPattern(CaptureBindingPatternNode captureBindingPatternNode) { + BLangCaptureBindingPattern bLangCaptureBindingPattern = + (BLangCaptureBindingPattern) TreeBuilder.createCaptureBindingPattern(); + bLangCaptureBindingPattern.setIdentifier(createIdentifier(captureBindingPatternNode + .variableName())); + bLangCaptureBindingPattern.pos = getPosition(captureBindingPatternNode); + return bLangCaptureBindingPattern; } public BLangExpression createLiteralOrReference(Node node) { diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/NodeCloner.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/NodeCloner.java index 8880222b6bd4..30d80a99baf4 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/NodeCloner.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/NodeCloner.java @@ -29,6 +29,9 @@ import org.wso2.ballerinalang.compiler.tree.BLangBlockFunctionBody; import org.wso2.ballerinalang.compiler.tree.BLangClassDefinition; import org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit; +import org.wso2.ballerinalang.compiler.tree.bindingpatterns.BLangCaptureBindingPattern; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangConstPattern; +import org.wso2.ballerinalang.compiler.tree.BLangEndpoint; import org.wso2.ballerinalang.compiler.tree.BLangErrorVariable; import org.wso2.ballerinalang.compiler.tree.BLangErrorVariable.BLangErrorDetailEntry; import org.wso2.ballerinalang.compiler.tree.BLangExprFunctionBody; @@ -54,6 +57,8 @@ import org.wso2.ballerinalang.compiler.tree.BLangTupleVariable; import org.wso2.ballerinalang.compiler.tree.BLangTypeDefinition; import org.wso2.ballerinalang.compiler.tree.BLangVariable; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangVarBindingPatternMatchPattern; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangWildCardMatchPattern; import org.wso2.ballerinalang.compiler.tree.BLangWorker; import org.wso2.ballerinalang.compiler.tree.BLangXMLNS; import org.wso2.ballerinalang.compiler.tree.clauses.BLangDoClause; @@ -61,6 +66,7 @@ import org.wso2.ballerinalang.compiler.tree.clauses.BLangJoinClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangLetClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangLimitClause; +import org.wso2.ballerinalang.compiler.tree.clauses.BLangMatchClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangOnClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangOnConflictClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangOnFailClause; @@ -97,6 +103,7 @@ import org.wso2.ballerinalang.compiler.tree.expressions.BLangMarkdownParameterDocumentation; import org.wso2.ballerinalang.compiler.tree.expressions.BLangMarkdownReturnParameterDocumentation; import org.wso2.ballerinalang.compiler.tree.expressions.BLangMatchExpression; +import org.wso2.ballerinalang.compiler.tree.expressions.BLangMatchGuard; import org.wso2.ballerinalang.compiler.tree.expressions.BLangNamedArgsExpression; import org.wso2.ballerinalang.compiler.tree.expressions.BLangNumericLiteral; import org.wso2.ballerinalang.compiler.tree.expressions.BLangObjectConstructorExpression; @@ -163,6 +170,7 @@ import org.wso2.ballerinalang.compiler.tree.statements.BLangMatch.BLangMatchStaticBindingPatternClause; import org.wso2.ballerinalang.compiler.tree.statements.BLangMatch.BLangMatchStructuredBindingPatternClause; import org.wso2.ballerinalang.compiler.tree.statements.BLangMatch.BLangMatchTypedBindingPatternClause; +import org.wso2.ballerinalang.compiler.tree.statements.BLangMatchStatement; import org.wso2.ballerinalang.compiler.tree.statements.BLangPanic; import org.wso2.ballerinalang.compiler.tree.statements.BLangRecordDestructure; import org.wso2.ballerinalang.compiler.tree.statements.BLangRecordVariableDef; @@ -672,6 +680,62 @@ public void visit(BLangMatch source) { clone.onFailClause = clone(source.onFailClause); } + @Override + public void visit(BLangMatchStatement source) { + BLangMatchStatement clone = new BLangMatchStatement(); + source.cloneRef = clone; + clone.setExpression(source.getExpression()); + clone.matchClauses = cloneList(source.matchClauses); + } + + @Override + public void visit(BLangMatchClause source) { + BLangMatchClause clone = new BLangMatchClause(); + source.cloneRef = clone; + clone.matchPatterns = cloneList(source.matchPatterns); + clone.setMatchGuard(source.getMatchGuard()); + clone.setBlockStatement(source.getBLockStatement()); + clone.expr = source.expr; + } + + @Override + public void visit(BLangMatchGuard source) { + BLangMatchGuard clone = new BLangMatchGuard(); + source.cloneRef = clone; + clone.setExpression(source.getExpression()); + } + + @Override + public void visit(BLangWildCardMatchPattern source) { + BLangWildCardMatchPattern clone = new BLangWildCardMatchPattern(); + clone.matchExpr = source.matchExpr; + source.cloneRef = clone; + } + + @Override + public void visit(BLangConstPattern source) { + BLangConstPattern clone = new BLangConstPattern(); + source.cloneRef = clone; + clone.matchExpr = source.matchExpr; + clone.setExpression(source.getExpresion()); + } + + @Override + public void visit(BLangVarBindingPatternMatchPattern source) { + BLangVarBindingPatternMatchPattern clone = new BLangVarBindingPatternMatchPattern(); + source.cloneRef = clone; + clone.matchExpr = source.matchExpr; + clone.setBindingPattern(source.getBindingPattern()); + } + + @Override + public void visit(BLangCaptureBindingPattern source) { + BLangCaptureBindingPattern clone = new BLangCaptureBindingPattern(); + source.cloneRef = clone; + clone.setIdentifier(source.getIdentifier()); + clone.setSymbol(source.getSymbol()); + } + @Override public void visit(BLangMatchTypedBindingPatternClause source) { diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/BLangNodeVisitor.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/BLangNodeVisitor.java index 1f32728282a0..bbc5a34a740c 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/BLangNodeVisitor.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/BLangNodeVisitor.java @@ -19,11 +19,14 @@ import org.wso2.ballerinalang.compiler.tree.BLangXMLNS.BLangLocalXMLNS; import org.wso2.ballerinalang.compiler.tree.BLangXMLNS.BLangPackageXMLNS; +import org.wso2.ballerinalang.compiler.tree.bindingpatterns.BLangCaptureBindingPattern; +import org.wso2.ballerinalang.compiler.tree.bindingpatterns.BLangListBindingPattern; import org.wso2.ballerinalang.compiler.tree.clauses.BLangDoClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangFromClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangJoinClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangLetClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangLimitClause; +import org.wso2.ballerinalang.compiler.tree.clauses.BLangMatchClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangOnClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangOnConflictClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangOnFailClause; @@ -73,6 +76,7 @@ import org.wso2.ballerinalang.compiler.tree.expressions.BLangMarkdownReturnParameterDocumentation; import org.wso2.ballerinalang.compiler.tree.expressions.BLangMatchExpression; import org.wso2.ballerinalang.compiler.tree.expressions.BLangMatchExpression.BLangMatchExprPatternClause; +import org.wso2.ballerinalang.compiler.tree.expressions.BLangMatchGuard; import org.wso2.ballerinalang.compiler.tree.expressions.BLangNamedArgsExpression; import org.wso2.ballerinalang.compiler.tree.expressions.BLangNumericLiteral; import org.wso2.ballerinalang.compiler.tree.expressions.BLangObjectConstructorExpression; @@ -122,6 +126,9 @@ import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString; import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLSequenceLiteral; import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLTextLiteral; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangConstPattern; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangVarBindingPatternMatchPattern; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangWildCardMatchPattern; import org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment; import org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt; import org.wso2.ballerinalang.compiler.tree.statements.BLangBreak; @@ -142,6 +149,7 @@ import org.wso2.ballerinalang.compiler.tree.statements.BLangMatch; import org.wso2.ballerinalang.compiler.tree.statements.BLangMatch.BLangMatchStaticBindingPatternClause; import org.wso2.ballerinalang.compiler.tree.statements.BLangMatch.BLangMatchStructuredBindingPatternClause; +import org.wso2.ballerinalang.compiler.tree.statements.BLangMatchStatement; import org.wso2.ballerinalang.compiler.tree.statements.BLangPanic; import org.wso2.ballerinalang.compiler.tree.statements.BLangRecordDestructure; import org.wso2.ballerinalang.compiler.tree.statements.BLangRecordVariableDef; @@ -337,6 +345,34 @@ public void visit(BLangMatch matchNode) { throw new AssertionError(); } + public void visit(BLangMatchStatement matchStatementNode) { + throw new AssertionError(); + } + + public void visit(BLangMatchGuard matchGuard) { + throw new AssertionError(); + } + + public void visit(BLangConstPattern constMatchPattern) { + throw new AssertionError(); + } + + public void visit(BLangWildCardMatchPattern wildCardMatchPattern) { + throw new AssertionError(); + } + + public void visit(BLangVarBindingPatternMatchPattern varBindingPattern) { + throw new AssertionError(); + } + + public void visit(BLangCaptureBindingPattern captureBindingPattern) { + throw new AssertionError(); + } + + public void visit(BLangListBindingPattern listBindingPattern) { + throw new AssertionError(); + } + public void visit(BLangMatch.BLangMatchTypedBindingPatternClause patternClauseNode) { throw new AssertionError(); } @@ -401,6 +437,10 @@ public void visit(BLangLimitClause limitClause) { throw new AssertionError(); } + public void visit(BLangMatchClause matchClause) { + throw new AssertionError(); + } + public void visit(BLangWhile whileNode) { throw new AssertionError(); } From 8a0706d970036ef434b71d940ec5a3d8b47cde36 Mon Sep 17 00:00:00 2001 From: kavindu Date: Sun, 2 Aug 2020 21:40:46 +0530 Subject: [PATCH 03/20] Change the compiler phases --- .../compiler/desugar/ConstantPropagation.java | 38 ++++++ .../compiler/desugar/Desugar.java | 125 ++++++++++++++++++ .../semantics/analyzer/CodeAnalyzer.java | 87 +++++++++++- .../semantics/analyzer/DataflowAnalyzer.java | 37 ++++++ .../semantics/analyzer/SemanticAnalyzer.java | 64 +++++++++ .../semantics/analyzer/SymbolEnter.java | 1 + .../semantics/analyzer/TaintAnalyzer.java | 18 +++ .../compiler/semantics/analyzer/Types.java | 65 +++++++++ 8 files changed, 428 insertions(+), 7 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/ConstantPropagation.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/ConstantPropagation.java index e07c9d125789..a95a5f1cc95d 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/ConstantPropagation.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/ConstantPropagation.java @@ -32,9 +32,11 @@ import org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment; import org.wso2.ballerinalang.compiler.tree.BLangBlockFunctionBody; import org.wso2.ballerinalang.compiler.tree.BLangClassDefinition; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangConstPattern; import org.wso2.ballerinalang.compiler.tree.BLangExprFunctionBody; import org.wso2.ballerinalang.compiler.tree.BLangExternalFunctionBody; import org.wso2.ballerinalang.compiler.tree.BLangFunction; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangMatchPattern; import org.wso2.ballerinalang.compiler.tree.BLangNode; import org.wso2.ballerinalang.compiler.tree.BLangNodeVisitor; import org.wso2.ballerinalang.compiler.tree.BLangPackage; @@ -42,12 +44,15 @@ import org.wso2.ballerinalang.compiler.tree.BLangSimpleVariable; import org.wso2.ballerinalang.compiler.tree.BLangTupleVariable; import org.wso2.ballerinalang.compiler.tree.BLangTypeDefinition; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangVarBindingPatternMatchPattern; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangWildCardMatchPattern; import org.wso2.ballerinalang.compiler.tree.BLangXMLNS; import org.wso2.ballerinalang.compiler.tree.clauses.BLangDoClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangFromClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangJoinClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangLetClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangLimitClause; +import org.wso2.ballerinalang.compiler.tree.clauses.BLangMatchClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangOnClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangOnConflictClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangOnFailClause; @@ -127,6 +132,7 @@ import org.wso2.ballerinalang.compiler.tree.statements.BLangIf; import org.wso2.ballerinalang.compiler.tree.statements.BLangLock; import org.wso2.ballerinalang.compiler.tree.statements.BLangMatch; +import org.wso2.ballerinalang.compiler.tree.statements.BLangMatchStatement; import org.wso2.ballerinalang.compiler.tree.statements.BLangPanic; import org.wso2.ballerinalang.compiler.tree.statements.BLangRecordDestructure; import org.wso2.ballerinalang.compiler.tree.statements.BLangRecordVariableDef; @@ -453,6 +459,38 @@ public void visit(BLangCompoundAssignment compoundAssignNode) { result = compoundAssignNode; } + @Override + public void visit(BLangMatchStatement matchStatement) { + matchStatement.expr = rewrite(matchStatement.expr); + rewrite(matchStatement.matchClauses); + result = matchStatement; + } + + @Override + public void visit(BLangMatchClause matchClause) { + for (BLangMatchPattern matchPattern : matchClause.matchPatterns) { + rewrite(matchPattern); + } + matchClause.blockStmt = rewrite(matchClause.blockStmt); + result = matchClause; + } + + @Override + public void visit(BLangConstPattern constMatchPattern) { + constMatchPattern.expr = rewrite(constMatchPattern.expr); + result = constMatchPattern; + } + + @Override + public void visit(BLangWildCardMatchPattern wildCardMatchPattern) { + result = wildCardMatchPattern; + } + + @Override + public void visit(BLangVarBindingPatternMatchPattern varBindingPattern) { + result = varBindingPattern; + } + @Override public void visit(BLangMatch matchNode) { matchNode.expr = rewrite(matchNode.expr); diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java index 6d34e79b66fa..37d136a75754 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java @@ -75,6 +75,9 @@ import org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment; import org.wso2.ballerinalang.compiler.tree.BLangBlockFunctionBody; import org.wso2.ballerinalang.compiler.tree.BLangClassDefinition; +import org.wso2.ballerinalang.compiler.tree.bindingpatterns.BLangBindingPattern; +import org.wso2.ballerinalang.compiler.tree.bindingpatterns.BLangCaptureBindingPattern; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangConstPattern; import org.wso2.ballerinalang.compiler.tree.BLangErrorVariable; import org.wso2.ballerinalang.compiler.tree.BLangExprFunctionBody; import org.wso2.ballerinalang.compiler.tree.BLangExternalFunctionBody; @@ -83,6 +86,7 @@ import org.wso2.ballerinalang.compiler.tree.BLangIdentifier; import org.wso2.ballerinalang.compiler.tree.BLangImportPackage; import org.wso2.ballerinalang.compiler.tree.BLangInvokableNode; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangMatchPattern; import org.wso2.ballerinalang.compiler.tree.BLangNode; import org.wso2.ballerinalang.compiler.tree.BLangNodeVisitor; import org.wso2.ballerinalang.compiler.tree.BLangPackage; @@ -99,6 +103,7 @@ import org.wso2.ballerinalang.compiler.tree.BLangXMLNS; import org.wso2.ballerinalang.compiler.tree.BLangXMLNS.BLangLocalXMLNS; import org.wso2.ballerinalang.compiler.tree.BLangXMLNS.BLangPackageXMLNS; +import org.wso2.ballerinalang.compiler.tree.clauses.BLangMatchClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangOnFailClause; import org.wso2.ballerinalang.compiler.tree.expressions.BLangAccessExpression; import org.wso2.ballerinalang.compiler.tree.expressions.BLangAnnotAccessExpr; @@ -187,6 +192,8 @@ import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName; import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString; import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLTextLiteral; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangVarBindingPatternMatchPattern; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangWildCardMatchPattern; import org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment; import org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt; import org.wso2.ballerinalang.compiler.tree.statements.BLangBreak; @@ -208,6 +215,7 @@ import org.wso2.ballerinalang.compiler.tree.statements.BLangMatch.BLangMatchStaticBindingPatternClause; import org.wso2.ballerinalang.compiler.tree.statements.BLangMatch.BLangMatchStructuredBindingPatternClause; import org.wso2.ballerinalang.compiler.tree.statements.BLangMatch.BLangMatchTypedBindingPatternClause; +import org.wso2.ballerinalang.compiler.tree.statements.BLangMatchStatement; import org.wso2.ballerinalang.compiler.tree.statements.BLangPanic; import org.wso2.ballerinalang.compiler.tree.statements.BLangRecordDestructure; import org.wso2.ballerinalang.compiler.tree.statements.BLangRecordVariableDef; @@ -2914,6 +2922,123 @@ public void visit(BLangMatch matchStmt) { this.onFailCallFuncDef = currentOnFailCallDef; } + @Override + public void visit(BLangMatchStatement matchStatement) { + BLangBlockStmt matchBlockStmt = (BLangBlockStmt) TreeBuilder.createBlockNode(); + matchBlockStmt.pos = matchStatement.pos; + + String matchExprVarName = GEN_VAR_PREFIX.value; + + BLangExpression matchExpr = matchStatement.expr; + BLangSimpleVariable matchExprVar = ASTBuilderUtil.createVariable(matchExpr.pos, + matchExprVarName, matchExpr.type, matchExpr, new BVarSymbol(0, + names.fromString(matchExprVarName), + this.env.scope.owner.pkgID, matchExpr.type, this.env.scope.owner)); + + BLangSimpleVariableDef matchExprVarDef = ASTBuilderUtil.createVariableDef(matchBlockStmt.pos, matchExprVar); + matchBlockStmt.stmts.add(matchExprVarDef); + matchBlockStmt.stmts.add(convertMatchClausesToIfElseStmt(matchStatement.matchClauses, matchExprVar)); + rewrite(matchBlockStmt, this.env); + + result = matchBlockStmt; + } + + private BLangStatement convertMatchClausesToIfElseStmt(List matchClauses, + BLangSimpleVariable matchExprVar) { + BLangIf parentIfNode = convertMatchClauseToIfStmt(matchClauses.get(0), matchExprVar); + BLangIf currentIfNode = parentIfNode; + for (int i = 1; i < matchClauses.size(); i++) { + currentIfNode.elseStmt = convertMatchClauseToIfStmt(matchClauses.get(i), matchExprVar); + currentIfNode = (BLangIf) currentIfNode.elseStmt; + } + + return parentIfNode; + } + + private BLangIf convertMatchClauseToIfStmt(BLangMatchClause matchClause, BLangSimpleVariable matchExprVar) { + BLangExpression ifCondition = createConditionFromMatchPatterns(matchClause.matchPatterns, matchExprVar, + matchClause.pos); + if (matchClause.matchGuard != null) { + ifCondition = ASTBuilderUtil.createBinaryExpr(matchClause.pos, ifCondition, matchClause.matchGuard.expr, + symTable.booleanType, OperatorKind.AND, (BOperatorSymbol) symResolver + .resolveBinaryOperator(OperatorKind.AND, symTable.booleanType, symTable.booleanType)); + } + return ASTBuilderUtil.createIfElseStmt(matchClause.pos, ifCondition, matchClause.blockStmt, null); + } + + private BLangExpression createConditionFromMatchPatterns(List matchPatterns, + BLangSimpleVariable matchExprVar, DiagnosticPos pos) { + BLangSimpleVariableDef resultVarDef = createVarDef("$result$", symTable.booleanType, null, pos); + BLangSimpleVarRef resultVarRef = ASTBuilderUtil.createVariableRef(pos, resultVarDef.var.symbol); + // $result$ = true + BLangBlockStmt successBody = ASTBuilderUtil.createBlockStmt(pos); + BLangAssignment successAssignment = + ASTBuilderUtil.createAssignmentStmt(pos, resultVarRef, getBooleanLiteral(true)); + successBody.addStatement(successAssignment); + // $result$ = false + BLangBlockStmt failureBody = ASTBuilderUtil.createBlockStmt(pos); + BLangAssignment failureAssignment = + ASTBuilderUtil.createAssignmentStmt(pos, resultVarRef, getBooleanLiteral(false)); + failureBody.addStatement(failureAssignment); + + BLangIf parentIfElse = createIfElseStmtFromMatchPattern(matchPatterns.get(0), matchExprVar, successBody, pos); + BLangIf currentIfElse = parentIfElse; + + for (int i = 1; i < matchPatterns.size(); i++) { + currentIfElse.elseStmt = createIfElseStmtFromMatchPattern(matchPatterns.get(i), matchExprVar, successBody, + matchPatterns.get(i).pos); + currentIfElse = (BLangIf) currentIfElse.elseStmt; + } + currentIfElse.elseStmt = failureBody; + + BLangBlockStmt blockStmt = ASTBuilderUtil.createBlockStmt(pos, Lists.of(resultVarDef, parentIfElse)); + BLangStatementExpression stmtExpr = createStatementExpression(blockStmt, resultVarRef); + + return rewriteExpr(stmtExpr); + } + + private BLangIf createIfElseStmtFromMatchPattern(BLangMatchPattern matchPattern, + BLangSimpleVariable matchExprVar, + BLangBlockStmt successBody, + DiagnosticPos pos) { + NodeKind patternKind = matchPattern.getKind(); + BLangSimpleVarRef matchExprVarRef = ASTBuilderUtil.createVariableRef(matchExprVar.pos, matchExprVar.symbol); + + switch (patternKind) { + case WILDCARD_MATCH_PATTERN: + return ASTBuilderUtil.createIfElseStmt(pos, ASTBuilderUtil.createLiteral(pos, + symTable.booleanType, ((BLangWildCardMatchPattern) matchPattern).matchesAll), successBody, null); + case CONST_MATCH_PATTERN: + BLangConstPattern constMatchPattern = (BLangConstPattern) matchPattern; + return ASTBuilderUtil.createIfElseStmt(pos, createBinaryExpression(constMatchPattern.pos, matchExprVarRef, + constMatchPattern.expr), successBody, null); + case VAR_BINDING_PATTERN_MATCH_PATTERN: + // var [a, b] or var a + BLangVarBindingPatternMatchPattern varBindingPattern = (BLangVarBindingPatternMatchPattern) matchPattern; + BLangBindingPattern bindingPattern = varBindingPattern.getBindingPattern(); + + switch (bindingPattern.getKind()) { + case CAPTURE_BINDING_PATTERN: + // always matches a value + BLangCaptureBindingPattern captureBindingPattern = + (BLangCaptureBindingPattern) varBindingPattern.getBindingPattern(); + + BLangSimpleVariable captureBindingPatternVar = ASTBuilderUtil.createVariable(pos, + captureBindingPattern.getIdentifier().getValue(), matchExprVar.type, matchExprVarRef, + captureBindingPattern.symbol); + BLangSimpleVariableDef captureBindingPatternVarDef = + ASTBuilderUtil.createVariableDef(pos, captureBindingPatternVar); + + successBody.addStatement(captureBindingPatternVarDef); + return ASTBuilderUtil.createIfElseStmt(pos, ASTBuilderUtil.createLiteral(pos, + symTable.booleanType, true), successBody, null); + } + } + // If some patterns are not implemented, those should be detected before this phase + // TODO : Remove this after all patterns are implemented + return null; + } + @Override public void visit(BLangForeach foreach) { BLangOnFailClause currentOnFailClause = this.onFailClause; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/CodeAnalyzer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/CodeAnalyzer.java index bb977bfad7eb..e44674ef2053 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/CodeAnalyzer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/CodeAnalyzer.java @@ -58,6 +58,9 @@ import org.wso2.ballerinalang.compiler.tree.BLangBlockFunctionBody; import org.wso2.ballerinalang.compiler.tree.BLangClassDefinition; import org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit; +import org.wso2.ballerinalang.compiler.tree.bindingpatterns.BLangBindingPattern; +import org.wso2.ballerinalang.compiler.tree.bindingpatterns.BLangCaptureBindingPattern; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangConstPattern; import org.wso2.ballerinalang.compiler.tree.BLangErrorVariable; import org.wso2.ballerinalang.compiler.tree.BLangExprFunctionBody; import org.wso2.ballerinalang.compiler.tree.BLangExternalFunctionBody; @@ -65,6 +68,7 @@ import org.wso2.ballerinalang.compiler.tree.BLangIdentifier; import org.wso2.ballerinalang.compiler.tree.BLangImportPackage; import org.wso2.ballerinalang.compiler.tree.BLangInvokableNode; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangMatchPattern; import org.wso2.ballerinalang.compiler.tree.BLangNode; import org.wso2.ballerinalang.compiler.tree.BLangNodeVisitor; import org.wso2.ballerinalang.compiler.tree.BLangPackage; @@ -77,6 +81,8 @@ import org.wso2.ballerinalang.compiler.tree.BLangTupleVariable; import org.wso2.ballerinalang.compiler.tree.BLangTypeDefinition; import org.wso2.ballerinalang.compiler.tree.BLangVariable; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangVarBindingPatternMatchPattern; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangWildCardMatchPattern; import org.wso2.ballerinalang.compiler.tree.BLangWorker; import org.wso2.ballerinalang.compiler.tree.BLangXMLNS; import org.wso2.ballerinalang.compiler.tree.clauses.BLangDoClause; @@ -84,6 +90,7 @@ import org.wso2.ballerinalang.compiler.tree.clauses.BLangJoinClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangLetClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangLimitClause; +import org.wso2.ballerinalang.compiler.tree.clauses.BLangMatchClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangOnClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangOnConflictClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangOnFailClause; @@ -167,6 +174,7 @@ import org.wso2.ballerinalang.compiler.tree.statements.BLangMatch.BLangMatchBindingPatternClause; import org.wso2.ballerinalang.compiler.tree.statements.BLangMatch.BLangMatchStaticBindingPatternClause; import org.wso2.ballerinalang.compiler.tree.statements.BLangMatch.BLangMatchStructuredBindingPatternClause; +import org.wso2.ballerinalang.compiler.tree.statements.BLangMatchStatement; import org.wso2.ballerinalang.compiler.tree.statements.BLangPanic; import org.wso2.ballerinalang.compiler.tree.statements.BLangRecordDestructure; import org.wso2.ballerinalang.compiler.tree.statements.BLangRecordVariableDef; @@ -209,6 +217,7 @@ import org.wso2.ballerinalang.compiler.util.diagnotic.BLangDiagnosticLogHelper; import org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos; import org.wso2.ballerinalang.util.Flags; +import org.wso2.ballerinalang.util.Lists; import java.util.ArrayList; import java.util.Comparator; @@ -248,7 +257,9 @@ public class CodeAnalyzer extends BLangNodeVisitor { private int transactionCount; private boolean statementReturns; private boolean failureHandled; + private boolean matchClauseReturns; private boolean lastStatement; + private boolean hasLastPattern = false; private boolean withinLockBlock; private SymbolTable symTable; private Types types; @@ -277,6 +288,8 @@ public class CodeAnalyzer extends BLangNodeVisitor { private int commitCountWithinBlock; private int rollbackCountWithinBlock; private boolean queryToTableWithKey; + private List matchExprTypes; + private BType matchExprType; public static CodeAnalyzer getInstance(CompilerContext context) { CodeAnalyzer codeGenerator = context.get(CODE_ANALYZER_KEY); @@ -730,7 +743,12 @@ public void visit(BLangReturn returnStmt) { this.returnWithinLambdaWrappingCheckStack.pop(); this.returnWithinLambdaWrappingCheckStack.push(true); } - this.statementReturns = true; + + if (returnStmt.parent != null && returnStmt.parent.parent.getKind() == NodeKind.MATCH_CLAUSE) { + this.matchClauseReturns = true; + } else { + this.statementReturns = true; + } analyzeExpr(returnStmt.expr); this.returnTypes.peek().add(returnStmt.expr.type); } @@ -769,6 +787,61 @@ public void visit(BLangIf ifStmt) { analyzeExpr(ifStmt.expr); } + @Override + public void visit(BLangMatchStatement matchStatement) { + analyzeExpr(matchStatement.expr); + matchExprType = matchStatement.expr.type; + + boolean matchStmtReturns = true; + for (BLangMatchClause matchClause : matchStatement.matchClauses) { + analyzeNode(matchClause, env); + matchStmtReturns = this.matchClauseReturns && matchClause.matchGuard == null && hasLastPattern; + } + this.statementReturns = matchStmtReturns; + hasLastPattern = false; + } + + @Override + public void visit(BLangMatchClause matchClause) { + for (BLangMatchPattern matchPattern : matchClause.matchPatterns) { + if (hasLastPattern && matchClause.matchGuard == null) { + dlog.error(matchPattern.pos, DiagnosticCode.MATCH_STMT_UNREACHABLE_PATTERN_AVAILABLE); + } + if (matchPattern.type == symTable.noType) { + dlog.error(matchClause.pos, DiagnosticCode.MATCH_STMT_UNMATCHED_PATTERN); + } + + this.isJSONContext = types.isJSONContext(matchExprType); + analyzeNode(matchPattern, env); + } + + analyzeNode(matchClause.blockStmt, env); + resetStatementReturns(); + } + + @Override + public void visit(BLangConstPattern constMatchPattern) { + analyzeNode(constMatchPattern.expr, env); + } + + @Override + public void visit(BLangWildCardMatchPattern wildCardMatchPattern) { + hasLastPattern = wildCardMatchPattern.matchesAll = types.isAssignable(wildCardMatchPattern.matchExpr.type, + symTable.anyType); + } + + @Override + public void visit(BLangVarBindingPatternMatchPattern varBindingPattern) { + BLangBindingPattern bindingPattern = varBindingPattern.getBindingPattern(); + analyzeNode(bindingPattern, env); + hasLastPattern = hasLastPattern && !varBindingPattern.matchGuardIsAvailable; + } + + @Override + public void visit(BLangCaptureBindingPattern captureBindingPattern) { + hasLastPattern = true; + } + @Override public void visit(BLangMatch matchStmt) { this.errorTypes.push(new LinkedHashSet<>()); @@ -906,23 +979,23 @@ private boolean analyzeStaticMatchPatterns(BLangMatch matchStmt) { return false; } List matchedPatterns = new ArrayList<>(); - for (BLangMatchStaticBindingPatternClause pattern : matchStmt.getStaticPatternClauses()) { - analyzeNode(pattern, env); + for (BLangMatchStaticBindingPatternClause patternClause : matchStmt.getStaticPatternClauses()) { + analyzeNode(patternClause, env); List matchedExpTypes = matchStmt.exprTypes .stream() - .filter(exprType -> isValidStaticMatchPattern(exprType, pattern.literal)) + .filter(exprType -> isValidStaticMatchPattern(exprType, patternClause.literal)) .collect(Collectors.toList()); if (matchedExpTypes.isEmpty()) { // log error if a pattern will not match to any of the expected types - dlog.error(pattern.pos, DiagnosticCode.MATCH_STMT_UNMATCHED_PATTERN); + dlog.error(patternClause.pos, DiagnosticCode.MATCH_STMT_UNMATCHED_PATTERN); continue; } this.isJSONContext = types.isJSONContext(matchStmt.expr.type); - analyzeNode(pattern.literal, env); - matchedPatterns.add(pattern); + analyzeNode(patternClause.literal, env); + matchedPatterns.add(patternClause); } if (matchedPatterns.isEmpty()) { diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/DataflowAnalyzer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/DataflowAnalyzer.java index aa61b9380050..f5a7aa0c8cf5 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/DataflowAnalyzer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/DataflowAnalyzer.java @@ -65,6 +65,7 @@ import org.wso2.ballerinalang.compiler.tree.clauses.BLangJoinClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangLetClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangLimitClause; +import org.wso2.ballerinalang.compiler.tree.clauses.BLangMatchClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangOnClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangOnConflictClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangOnFailClause; @@ -155,6 +156,7 @@ import org.wso2.ballerinalang.compiler.tree.statements.BLangMatch; import org.wso2.ballerinalang.compiler.tree.statements.BLangMatch.BLangMatchStaticBindingPatternClause; import org.wso2.ballerinalang.compiler.tree.statements.BLangMatch.BLangMatchStructuredBindingPatternClause; +import org.wso2.ballerinalang.compiler.tree.statements.BLangMatchStatement; import org.wso2.ballerinalang.compiler.tree.statements.BLangPanic; import org.wso2.ballerinalang.compiler.tree.statements.BLangRecordDestructure; import org.wso2.ballerinalang.compiler.tree.statements.BLangRecordVariableDef; @@ -566,6 +568,36 @@ public void visit(BLangIf ifNode) { this.uninitializedVars = mergeUninitializedVars(ifResult.uninitializedVars, elseResult.uninitializedVars); } + @Override + public void visit(BLangMatchStatement matchStatement) { + analyzeNode(matchStatement.expr, env); + + Map unInitVars = new HashMap<>(); + BranchResult lastClauseResult = null; + for (BLangMatchClause matchClause : matchStatement.matchClauses) { + if (matchClause.isLastClause) { + lastClauseResult = analyzeBranch(matchClause, env); + } else { + BranchResult result = analyzeBranch(matchClause, env); + // If the flow was terminated within the block, then that branch should not be considered for + // analyzing the data-flow for the downstream code. + if (result.flowTerminated) { + continue; + } + unInitVars = mergeUninitializedVars(unInitVars, result.uninitializedVars); + } + } + + if (lastClauseResult != null) { + // only if last pattern is present, uninitializedVars should be updated + unInitVars = mergeUninitializedVars(unInitVars, lastClauseResult.uninitializedVars); + this.uninitializedVars = unInitVars; + return; + } + unInitVars = mergeUninitializedVars(new HashMap<>(), this.uninitializedVars); + this.uninitializedVars = unInitVars; + } + @Override public void visit(BLangMatch match) { analyzeNode(match.expr, env); @@ -1619,6 +1651,11 @@ public void visit(BLangMatchStructuredBindingPatternClause bLangMatchStructuredB analyzeNode(bLangMatchStructuredBindingPatternClause.body, env); } + @Override + public void visit(BLangMatchClause matchClause) { + analyzeNode(matchClause.blockStmt, env); + } + private void addUninitializedVar(BLangVariable variable) { if (!this.uninitializedVars.containsKey(variable.symbol)) { this.uninitializedVars.put(variable.symbol, InitStatus.UN_INIT); diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java index 4f27f5579e8a..aa230378f120 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java @@ -60,6 +60,8 @@ import org.wso2.ballerinalang.compiler.tree.BLangAnnotation; import org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment; import org.wso2.ballerinalang.compiler.tree.BLangBlockFunctionBody; +import org.wso2.ballerinalang.compiler.tree.bindingpatterns.BLangCaptureBindingPattern; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangConstPattern; import org.wso2.ballerinalang.compiler.tree.BLangClassDefinition; import org.wso2.ballerinalang.compiler.tree.BLangErrorVariable; import org.wso2.ballerinalang.compiler.tree.BLangExprFunctionBody; @@ -78,8 +80,11 @@ import org.wso2.ballerinalang.compiler.tree.BLangTupleVariable; import org.wso2.ballerinalang.compiler.tree.BLangTypeDefinition; import org.wso2.ballerinalang.compiler.tree.BLangVariable; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangVarBindingPatternMatchPattern; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangWildCardMatchPattern; import org.wso2.ballerinalang.compiler.tree.BLangWorker; import org.wso2.ballerinalang.compiler.tree.BLangXMLNS; +import org.wso2.ballerinalang.compiler.tree.clauses.BLangMatchClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangOnFailClause; import org.wso2.ballerinalang.compiler.tree.expressions.BLangAccessExpression; import org.wso2.ballerinalang.compiler.tree.expressions.BLangBinaryExpr; @@ -92,6 +97,7 @@ import org.wso2.ballerinalang.compiler.tree.expressions.BLangLambdaFunction; import org.wso2.ballerinalang.compiler.tree.expressions.BLangListConstructorExpr; import org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral; +import org.wso2.ballerinalang.compiler.tree.expressions.BLangMatchGuard; import org.wso2.ballerinalang.compiler.tree.expressions.BLangNamedArgsExpression; import org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral; import org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordVarRef; @@ -119,6 +125,7 @@ import org.wso2.ballerinalang.compiler.tree.statements.BLangMatch; import org.wso2.ballerinalang.compiler.tree.statements.BLangMatch.BLangMatchStaticBindingPatternClause; import org.wso2.ballerinalang.compiler.tree.statements.BLangMatch.BLangMatchStructuredBindingPatternClause; +import org.wso2.ballerinalang.compiler.tree.statements.BLangMatchStatement; import org.wso2.ballerinalang.compiler.tree.statements.BLangPanic; import org.wso2.ballerinalang.compiler.tree.statements.BLangRecordDestructure; import org.wso2.ballerinalang.compiler.tree.statements.BLangRecordVariableDef; @@ -2292,12 +2299,69 @@ public void visit(BLangMatch matchNode) { matchNode.exprTypes = exprTypes; } + @Override + public void visit(BLangMatchStatement matchStatement) { + typeChecker.checkExpr(matchStatement.expr, env, symTable.noType); + matchStatement.matchClauses.forEach(matchClause -> matchClause.accept(this)); + } + + @Override + public void visit(BLangMatchClause matchClause) { + SymbolEnv blockEnv = SymbolEnv.createBlockEnv(matchClause.blockStmt, env); + + SymbolEnv finalBlockEnv = blockEnv; + matchClause.matchPatterns.forEach(matchPattern -> analyzeNode(matchPattern, finalBlockEnv)); + + if (matchClause.matchGuard != null) { + typeChecker.checkExpr(matchClause.matchGuard.expr, blockEnv); + blockEnv = typeNarrower.evaluateTruth(matchClause.matchGuard.expr, matchClause.blockStmt, blockEnv); + } + analyzeStmt(matchClause.blockStmt, blockEnv); + } + + @Override + public void visit(BLangMatchGuard matchGuard) { + matchGuard.expr.accept(this); + } + + @Override + public void visit(BLangVarBindingPatternMatchPattern varBindingPattern) { + NodeKind patternKind = varBindingPattern.getBindingPattern().getKind(); + BType matchExprType = varBindingPattern.matchExpr.type; + + switch (patternKind) { + case CAPTURE_BINDING_PATTERN: + varBindingPattern.type = matchExprType; + break; + } + analyzeNode(varBindingPattern.getBindingPattern(), env); + } + + @Override + public void visit(BLangConstPattern constMatchPattern) { + BLangExpression constPatternExpr = constMatchPattern.expr; + typeChecker.checkExpr(constPatternExpr, env); + constMatchPattern.type = types.resolvePatternTypeFromMatchExpr(constMatchPattern.matchExpr.type, + constMatchPattern); + } + + @Override + public void visit(BLangWildCardMatchPattern wildCardMatchPattern) { + } + @Override public void visit(BLangMatchStaticBindingPatternClause patternClause) { checkStaticMatchPatternLiteralType(patternClause.literal); analyzeStmt(patternClause.body, this.env); } + @Override + public void visit(BLangCaptureBindingPattern captureBindingPattern) { + captureBindingPattern.symbol = new BVarSymbol(0, new Name(captureBindingPattern.getIdentifier().getValue()), + env.enclPkg.packageID, symTable.anyType, env.scope.owner); + symbolEnter.defineSymbol(captureBindingPattern.pos, captureBindingPattern.symbol, env); + } + private BType checkStaticMatchPatternLiteralType(BLangExpression expression) { switch (expression.getKind()) { diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SymbolEnter.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SymbolEnter.java index 2d21ea4e8825..1c41a415ecc7 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SymbolEnter.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SymbolEnter.java @@ -99,6 +99,7 @@ import org.wso2.ballerinalang.compiler.tree.BLangTypeDefinition; import org.wso2.ballerinalang.compiler.tree.BLangWorker; import org.wso2.ballerinalang.compiler.tree.BLangXMLNS; +import org.wso2.ballerinalang.compiler.tree.bindingpatterns.BLangCaptureBindingPattern; import org.wso2.ballerinalang.compiler.tree.expressions.BLangConstant; import org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression; import org.wso2.ballerinalang.compiler.tree.expressions.BLangLambdaFunction; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TaintAnalyzer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TaintAnalyzer.java index 6ebde709bd65..56da07f36aa6 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TaintAnalyzer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TaintAnalyzer.java @@ -62,6 +62,7 @@ import org.wso2.ballerinalang.compiler.tree.BLangWorker; import org.wso2.ballerinalang.compiler.tree.BLangXMLNS; import org.wso2.ballerinalang.compiler.tree.clauses.BLangInputClause; +import org.wso2.ballerinalang.compiler.tree.clauses.BLangMatchClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangOnFailClause; import org.wso2.ballerinalang.compiler.tree.expressions.BLangAccessExpression; import org.wso2.ballerinalang.compiler.tree.expressions.BLangAnnotAccessExpr; @@ -137,6 +138,7 @@ import org.wso2.ballerinalang.compiler.tree.statements.BLangIf; import org.wso2.ballerinalang.compiler.tree.statements.BLangLock; import org.wso2.ballerinalang.compiler.tree.statements.BLangMatch; +import org.wso2.ballerinalang.compiler.tree.statements.BLangMatchStatement; import org.wso2.ballerinalang.compiler.tree.statements.BLangPanic; import org.wso2.ballerinalang.compiler.tree.statements.BLangRecordDestructure; import org.wso2.ballerinalang.compiler.tree.statements.BLangRecordVariableDef; @@ -805,6 +807,22 @@ public void visit(BLangIf ifNode) { overridingAnalysis = true; } + @Override + public void visit(BLangMatchStatement matchStatement) { + matchStatement.expr.accept(this); + TaintedStatus observedTaintedStatusOfMatchExpr = getCurrentAnalysisState().taintedStatus; + + for (BLangMatchClause matchClause : matchStatement.matchClauses) { + getCurrentAnalysisState().taintedStatus = observedTaintedStatusOfMatchExpr; + matchClause.accept(this); + } + } + + @Override + public void visit(BLangMatchClause matchClause) { + matchClause.blockStmt.accept(this); + } + @Override public void visit(BLangMatch matchStmt) { matchStmt.expr.accept(this); diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/Types.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/Types.java index e9233aeaad80..9c085d8c6fc6 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/Types.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/Types.java @@ -65,7 +65,9 @@ import org.wso2.ballerinalang.compiler.tree.clauses.BLangInputClause; import org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression; import org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral; +import org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef; import org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeConversionExpr; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangConstPattern; import org.wso2.ballerinalang.compiler.tree.statements.BLangForeach; import org.wso2.ballerinalang.compiler.util.BArrayState; import org.wso2.ballerinalang.compiler.util.CompilerContext; @@ -283,6 +285,69 @@ public boolean isSubTypeOfList(BType type) { return ((BUnionType) type).getMemberTypes().stream().allMatch(this::isSubTypeOfList); } + public BType resolvePatternTypeFromMatchExpr(BType matchExprType, BLangConstPattern constMatchPattern) { + BLangExpression constPatternExpr = constMatchPattern.expr; + BType constMatchPatternExprType = constPatternExpr.type; + + if (constPatternExpr.getKind() == NodeKind.SIMPLE_VARIABLE_REF) { + BType varRefSymbolType = ((BLangSimpleVarRef) constPatternExpr).symbol.type; + if (isAssignable(varRefSymbolType, matchExprType)) { + return varRefSymbolType; + } + return symTable.noType; + } + + // After the above check, according to spec all other const-patterns should be literals. + BLangLiteral constPatternLiteral = (BLangLiteral) constPatternExpr; + + if (containsAnyType(constMatchPatternExprType)) { + return matchExprType; + } else if (containsAnyType(matchExprType)) { + return constMatchPatternExprType; + } + + // This should handle specially + if (matchExprType.tag == TypeTags.BYTE && constMatchPatternExprType.tag == TypeTags.INT) { + return matchExprType; + } + + if (isAssignable(constMatchPatternExprType, matchExprType)) { + return constMatchPatternExprType; + } + + if (matchExprType.tag == TypeTags.UNION) { + for (BType memberType : ((BUnionType) matchExprType).getMemberTypes()) { + if (memberType.tag == TypeTags.FINITE) { + if (isAssignableToFiniteType(memberType, constPatternLiteral)) { + return memberType; + } + } else { + if (isAssignable(constMatchPatternExprType, matchExprType)) { + return constMatchPatternExprType; + } + } + } + } else if (matchExprType.tag == TypeTags.FINITE) { + if (isAssignableToFiniteType(matchExprType, constPatternLiteral)) { + return matchExprType; + } + } + return symTable.noType; + } + + public boolean containsAnyType(BType type) { + if (type.tag != TypeTags.UNION) { + return type.tag == TypeTags.ANY; + } + + for (BType memberTypes : ((BUnionType) type).getMemberTypes()) { + if (memberTypes.tag == TypeTags.ANY) { + return true; + } + } + return false; + } + public boolean isSubTypeOfMapping(BType type) { if (type.tag != TypeTags.UNION) { return isSubTypeOfBaseType(type, TypeTags.MAP) || isSubTypeOfBaseType(type, TypeTags.RECORD); From 574e34294ce40316a06879c299eb77d27a409047 Mon Sep 17 00:00:00 2001 From: kavindu Date: Sun, 2 Aug 2020 21:42:15 +0530 Subject: [PATCH 04/20] Handle the errors --- .../org/ballerinalang/util/diagnostic/DiagnosticCode.java | 2 ++ .../ballerina-lang/src/main/resources/compiler.properties | 6 ++++++ .../org/ballerinalang/langserver/common/LSNodeVisitor.java | 6 ++++++ tests/jballerina-unit-test/src/test/resources/testng.xml | 2 ++ 4 files changed, 16 insertions(+) diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/util/diagnostic/DiagnosticCode.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/util/diagnostic/DiagnosticCode.java index 5f3defcb28dd..f27f9085bbbd 100644 --- a/compiler/ballerina-lang/src/main/java/org/ballerinalang/util/diagnostic/DiagnosticCode.java +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/util/diagnostic/DiagnosticCode.java @@ -400,6 +400,8 @@ public enum DiagnosticCode { MATCH_STMT_UNREACHABLE_PATTERN("match.stmt.unreachable.pattern"), MATCH_STMT_UNMATCHED_PATTERN("match.stmt.unmatched.pattern"), MATCH_STMT_PATTERN_ALWAYS_MATCHES("match.stmt.pattern.always.matches"), + MATCH_STMT_UNREACHABLE_PATTERN_AVAILABLE("match.stmt.unreachable.pattern.available"), + MATCH_PATTERN_NOT_SUPPORTED("match.pattern.not.supported"), MATCH_STMT_CONTAINS_TWO_DEFAULT_PATTERNS("match.stmt.contains.two.default.patterns"), THROW_STMT_NOT_SUPPORTED("throw.stmt.not.supported"), diff --git a/compiler/ballerina-lang/src/main/resources/compiler.properties b/compiler/ballerina-lang/src/main/resources/compiler.properties index 3be9b5333996..afd98187ef04 100644 --- a/compiler/ballerina-lang/src/main/resources/compiler.properties +++ b/compiler/ballerina-lang/src/main/resources/compiler.properties @@ -953,6 +953,12 @@ error.match.stmt.unmatched.pattern=\ error.match.stmt.pattern.always.matches=\ pattern will always be matched +error.match.stmt.unreachable.pattern.available=\ + unreachable pattern + +error.match.pattern.not.supported=\ + unsupported match pattern + error.match.stmt.contains.two.default.patterns=\ match statement has a 'static value' default pattern and a 'binding value' default pattern diff --git a/language-server/modules/langserver-core/src/main/java/org/ballerinalang/langserver/common/LSNodeVisitor.java b/language-server/modules/langserver-core/src/main/java/org/ballerinalang/langserver/common/LSNodeVisitor.java index ee21b6ee6581..1c70b0a673a9 100644 --- a/language-server/modules/langserver-core/src/main/java/org/ballerinalang/langserver/common/LSNodeVisitor.java +++ b/language-server/modules/langserver-core/src/main/java/org/ballerinalang/langserver/common/LSNodeVisitor.java @@ -159,6 +159,7 @@ import org.wso2.ballerinalang.compiler.tree.statements.BLangIf; import org.wso2.ballerinalang.compiler.tree.statements.BLangLock; import org.wso2.ballerinalang.compiler.tree.statements.BLangMatch; +import org.wso2.ballerinalang.compiler.tree.statements.BLangMatchStatement; import org.wso2.ballerinalang.compiler.tree.statements.BLangPanic; import org.wso2.ballerinalang.compiler.tree.statements.BLangRecordDestructure; import org.wso2.ballerinalang.compiler.tree.statements.BLangRecordVariableDef; @@ -412,6 +413,11 @@ public void visit(BLangMatch matchNode) { // No implementation } + @Override + public void visit(BLangMatchStatement matchStatement) { + // No implementation + } + @Override public void visit(BLangMatch.BLangMatchTypedBindingPatternClause patternClauseNode) { // No implementation diff --git a/tests/jballerina-unit-test/src/test/resources/testng.xml b/tests/jballerina-unit-test/src/test/resources/testng.xml index 846aa419c60c..c6b59536aab4 100644 --- a/tests/jballerina-unit-test/src/test/resources/testng.xml +++ b/tests/jballerina-unit-test/src/test/resources/testng.xml @@ -194,6 +194,8 @@ + + From c63f5618722597f59e04f777fe15c08ea6c3467a Mon Sep 17 00:00:00 2001 From: kavindu Date: Sun, 2 Aug 2020 21:43:50 +0530 Subject: [PATCH 05/20] Add the tests --- .../matchstmt/MatchStmtConstPattern.java | 105 +++++++ .../CaptureBindingPattern.java | 44 +++ .../matchstmt/const-pattern-negative.bal | 97 ++++++ .../statements/matchstmt/const-pattern.bal | 296 ++++++++++++++++++ .../capture-binding-pattern-negative.bal | 26 ++ .../capture-binding-pattern.bal | 83 +++++ 6 files changed, 651 insertions(+) create mode 100644 tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/MatchStmtConstPattern.java create mode 100644 tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/varbindingpatternmatchpattern/CaptureBindingPattern.java create mode 100644 tests/jballerina-unit-test/src/test/resources/test-src/statements/matchstmt/const-pattern-negative.bal create mode 100644 tests/jballerina-unit-test/src/test/resources/test-src/statements/matchstmt/const-pattern.bal create mode 100644 tests/jballerina-unit-test/src/test/resources/test-src/statements/matchstmt/varbindingpatternmatchpattern/capture-binding-pattern-negative.bal create mode 100644 tests/jballerina-unit-test/src/test/resources/test-src/statements/matchstmt/varbindingpatternmatchpattern/capture-binding-pattern.bal diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/MatchStmtConstPattern.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/MatchStmtConstPattern.java new file mode 100644 index 000000000000..b071cb0223d9 --- /dev/null +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/MatchStmtConstPattern.java @@ -0,0 +1,105 @@ +// Copyright (c) 2020 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +// +// WSO2 Inc. licenses this file to you under the Apache License, +// Version 2.0 (the "License"); you may not use this file except +// in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.ballerinalang.test.statements.matchstmt; + +import org.ballerinalang.test.util.BAssertUtil; +import org.ballerinalang.test.util.BCompileUtil; +import org.ballerinalang.test.util.BRunUtil; +import org.ballerinalang.test.util.CompileResult; +import org.testng.Assert; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +public class MatchStmtConstPattern { + + private CompileResult result, resultNegative; + + @BeforeClass + public void setup() { + result = BCompileUtil.compile("test-src/statements/matchstmt/const-pattern.bal"); + resultNegative = BCompileUtil.compile("test-src/statements/matchstmt/const-pattern-negative.bal"); + } + + @Test + public void testConstPattern1() { + BRunUtil.invoke(result, "testConstPattern1"); + } + + @Test + public void testConstPattern2() { + BRunUtil.invoke(result, "testConstPattern2"); + } + + @Test + public void testConstPattern3() { + BRunUtil.invoke(result, "testConstPattern3"); + } + + @Test + public void testConstPattern4() { + BRunUtil.invoke(result, "testConstPattern4"); + } + + @Test + public void testConstPattern5() { + BRunUtil.invoke(result, "testConstPattern5"); + } + + @Test + public void testConstPattern6() { + BRunUtil.invoke(result, "testConstPattern6"); + } + + @Test + public void testConstPattern7() { + BRunUtil.invoke(result, "testConstPattern7"); + } + + @Test(description = "Test pattern will not be matched") + public void testConstPatternNegative() { + Assert.assertEquals(resultNegative.getErrorCount(), 26); + int i = -1; + String patternNotMatched = "pattern will not be matched"; + + BAssertUtil.validateError(resultNegative, ++i, patternNotMatched, 27, 9); + BAssertUtil.validateError(resultNegative, ++i, patternNotMatched, 28, 9); + BAssertUtil.validateError(resultNegative, ++i, patternNotMatched, 29, 9); + BAssertUtil.validateError(resultNegative, ++i, patternNotMatched, 34, 9); + BAssertUtil.validateError(resultNegative, ++i, patternNotMatched, 36, 9); + BAssertUtil.validateError(resultNegative, ++i, patternNotMatched, 37, 9); + BAssertUtil.validateError(resultNegative, ++i, patternNotMatched, 38, 9); + BAssertUtil.validateError(resultNegative, ++i, patternNotMatched, 43, 9); + BAssertUtil.validateError(resultNegative, ++i, patternNotMatched, 44, 9); + BAssertUtil.validateError(resultNegative, ++i, patternNotMatched, 45, 9); + BAssertUtil.validateError(resultNegative, ++i, patternNotMatched, 46, 9); + BAssertUtil.validateError(resultNegative, ++i, patternNotMatched, 52, 9); + BAssertUtil.validateError(resultNegative, ++i, patternNotMatched, 53, 9); + BAssertUtil.validateError(resultNegative, ++i, patternNotMatched, 54, 9); + BAssertUtil.validateError(resultNegative, ++i, patternNotMatched, 56, 9); + BAssertUtil.validateError(resultNegative, ++i, patternNotMatched, 62, 9); + BAssertUtil.validateError(resultNegative, ++i, patternNotMatched, 64, 9); + BAssertUtil.validateError(resultNegative, ++i, patternNotMatched, 65, 9); + BAssertUtil.validateError(resultNegative, ++i, patternNotMatched, 70, 9); + BAssertUtil.validateError(resultNegative, ++i, patternNotMatched, 71, 9); + BAssertUtil.validateError(resultNegative, ++i, patternNotMatched, 72, 9); + BAssertUtil.validateError(resultNegative, ++i, patternNotMatched, 73, 9); + BAssertUtil.validateError(resultNegative, ++i, patternNotMatched, 78, 9); + BAssertUtil.validateError(resultNegative, ++i, patternNotMatched, 79, 9); + BAssertUtil.validateError(resultNegative, ++i, patternNotMatched, 80, 9); + BAssertUtil.validateError(resultNegative, ++i, patternNotMatched, 94, 9); + } +} diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/varbindingpatternmatchpattern/CaptureBindingPattern.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/varbindingpatternmatchpattern/CaptureBindingPattern.java new file mode 100644 index 000000000000..d2db09d5ec2f --- /dev/null +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/varbindingpatternmatchpattern/CaptureBindingPattern.java @@ -0,0 +1,44 @@ +package org.ballerinalang.test.statements.matchstmt.varbindingpatternmatchpattern; + +import org.ballerinalang.test.util.BAssertUtil; +import org.ballerinalang.test.util.BCompileUtil; +import org.ballerinalang.test.util.BRunUtil; +import org.ballerinalang.test.util.CompileResult; +import org.testng.Assert; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +public class CaptureBindingPattern { + private CompileResult result, resultNegative; + + @BeforeClass + public void setup() { + result = BCompileUtil.compile("test-src/statements/matchstmt/varbindingpatternmatchpattern/capture-binding" + + "-pattern.bal"); + resultNegative = BCompileUtil.compile("test-src/statements/matchstmt/varbindingpatternmatchpattern/capture" + + "-binding-pattern-negative.bal"); + } + + @Test + public void testCaptureBindingPattern1() { + BRunUtil.invoke(result, "testCaptureBindingPattern1"); + } + + @Test + public void testCaptureBindingPattern2() { + BRunUtil.invoke(result, "testCaptureBindingPattern2"); + } + + @Test + public void testCaptureBindingPattern3() { + BRunUtil.invoke(result, "testCaptureBindingPattern3"); + } + + @Test + public void testCaptureBindingPatternNegative1() { + Assert.assertEquals(resultNegative.getErrorCount(), 1); + + int i = -1; + BAssertUtil.validateError(resultNegative, ++i, "unreachable pattern", 22, 9); + } +} diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/statements/matchstmt/const-pattern-negative.bal b/tests/jballerina-unit-test/src/test/resources/test-src/statements/matchstmt/const-pattern-negative.bal new file mode 100644 index 000000000000..d1cb3b2a6672 --- /dev/null +++ b/tests/jballerina-unit-test/src/test/resources/test-src/statements/matchstmt/const-pattern-negative.bal @@ -0,0 +1,97 @@ +// Copyright (c) 2020 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +// +// WSO2 Inc. licenses this file to you under the Apache License, +// Version 2.0 (the "License"); you may not use this file except +// in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +type Finite "A" | "B" | true | 15; + +type Rec1 record {| + int | float a; +|}; + +function simpleTypes() returns string { + int x1 = 10; + match x1 { + 20 => {return "20";} + false => {return "false";} // pattern will not be matched + "Ballerina" => {return "Ballerina";} // pattern will not be matched + 10.4 => {return "10.4";} // pattern will not be matched + } + + boolean x2 = true; + match x2 { + 20 => {return "20";} // pattern will not be matched + false => {return "false";} + 21 => {return "21";} // pattern will not be matched + "Ballerina" => {return "Ballerina";} // pattern will not be matched + 10.4 => {return "10.4";} // pattern will not be matched + } + + float x3 = 10.1; + match x3 { + 20 => {return "20";} // pattern will not be matched + false => {return "false";} // pattern will not be matched + 21 => {return "21";} // pattern will not be matched + "Ballerina" => {return "Ballerina";} // pattern will not be matched + 10.4 => {return "10.4";} + } + + string x4 = "Ballerina"; + match x4 { + 20 => {return "20"; } // pattern will not be matched + false => {return "false";} // pattern will not be matched + 21 => {return "21";} // pattern will not be matched + "Ballerina" => {return "Ballerina";} + 10.4 => {return "10.4";} // pattern will not be matched + } + + byte x5 = 200; + match x5 { + 20 => {return "20";} + false => {return "false";} // pattern will not be matched + 21 => {return "21";} + "Ballerina" => {return "Ballerina";} // pattern will not be matched + 10.4 => {return "10.4";} // pattern will not be matched + } + + Rec1 r1 = {a: 200}; + match r1 { + 20 => {return "20"; }// pattern will not be matched + false => {return "false";} // pattern will not be matched + "Ballerina" => {return "Ballerina";} // pattern will not be matched + 10.4 => {return "10.4";} // pattern will not be matched + } + + Finite f = "B"; + match f { + 16 => {return "a";} // pattern will not be matched + false => {return "a";} // pattern will not be matched + "C" => {return "a";} // pattern will not be matched + } + + return "Fail"; +} + +const CONST_1 = "A"; +const CONST_2 = "B"; +const CONST_3 = "C"; + +function invalidConstTypes(CONST_1|CONST_2 a) returns string { + match a { + CONST_1 => {return "A";} + CONST_2 => {return "B";} + CONST_3 => {return "C";} // pattern will not be matched + } + return "Default"; +} diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/statements/matchstmt/const-pattern.bal b/tests/jballerina-unit-test/src/test/resources/test-src/statements/matchstmt/const-pattern.bal new file mode 100644 index 000000000000..238d37a78915 --- /dev/null +++ b/tests/jballerina-unit-test/src/test/resources/test-src/statements/matchstmt/const-pattern.bal @@ -0,0 +1,296 @@ +// Copyright (c) 2020 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +// +// WSO2 Inc. licenses this file to you under the Apache License, +// Version 2.0 (the "License"); you may not use this file except +// in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +import ballerina/io; + +function constPattern1(string | int | boolean a) returns string { + match a { + 12 => { + return "12"; + } + "Hello" => { + return "Hello"; + } + 15 => { + return "15"; + } + true => { + return "true"; + } + false => { + return "false"; + } + "HelloAgain" => { + return "HelloAgain"; + } + } + return "Default"; +} + +function testConstPattern1() { + string | int | boolean a1 = 12; + string | int | boolean a2 = "Hello"; + string | int | boolean a3 = true; + string | int | boolean a4 = 15; + string | int | boolean a5 = "HelloAgain"; + string | int | boolean a6 = false; + string | int | boolean a7 = "NothingToMatch"; + + assertEquals("12", constPattern1(a1)); + assertEquals("Hello", constPattern1(a2)); + assertEquals("true", constPattern1(a3)); + assertEquals("15", constPattern1(a4)); + assertEquals("HelloAgain", constPattern1(a5)); + assertEquals("false", constPattern1(a6)); + assertEquals("Default", constPattern1(a7)); +} + +function constPattern2(string | int | boolean a, string | int | boolean b) returns string { + match a { + 12 => { + return "Value is '12'"; + } + "Hello" => { + return "Value is 'Hello'"; + } + 15 => { + match b { + 34 => { + return "Value is '15 & 34'"; + } + "HelloWorld" => { + return "Value is '15 & HelloWorld'"; + } + } + } + "HelloAgain" => { + match b { + 34 => { + return "Value is 'HelloAgain & 34'"; + } + "HelloWorld" => { + return "Value is 'HelloAgain & HelloWorld'"; + } + } + } + true => { + return "Value is 'true'"; + } + } + + return "Value is 'Default'"; +} + +function testConstPattern2() { + string | int | boolean a1 = 12; + string | int | boolean a2 = "Hello"; + + string | int | boolean a3 = 15; + string | int | boolean a4 = "HelloWorld"; + + string | int | boolean a5 = "HelloAgain"; + string | int | boolean a6 = 34; + + string | int | boolean a7 = "NothingToMatch"; + string | int | boolean a8 = false; + + string | int | boolean a9 = 15; + string | int | boolean a10 = 34; + + string | int | boolean a11 = true; + string | int | boolean a12 = false; + + assertEquals("Value is '12'", constPattern2(a1, a2)); + assertEquals("Value is '15 & HelloWorld'", constPattern2(a3, a4)); + assertEquals("Value is 'HelloAgain & 34'", constPattern2(a5, a6)); + assertEquals("Value is 'Default'", constPattern2(a7, a8)); + assertEquals("Value is '15 & 34'", constPattern2(a9, a10)); + assertEquals("Value is 'true'", constPattern2(a11, a12)); +} + +function constPattern3(string | int | boolean a) returns string { + match a { + 12 | 13 | 14 => { + return "Value is : " + io:sprintf("%s", a); + } + "Hello" | "World" => { + return "Value is : " + io:sprintf("%s", a); + } + 15 | "Test" => { + return "Value is : " + io:sprintf("%s", a); + } + true | false => { + return "Value is : " + io:sprintf("%s", a); + } + "HelloAgain" => { + return "Value is : " + io:sprintf("%s", a); + } + } + + return "Default value is : " + io:sprintf("%s", a); +} + +function testConstPattern3(){ + string | int | boolean a1 = 12; + string | int | boolean a2 = "Hello"; + string | int | boolean a3 = true; + + string | int | boolean a4 = 15; + string | int | boolean a5 = "HelloAgain"; + string | int | boolean a6 = false; + + string | int | boolean a7 = "NothingToMatch"; + string | int | boolean a8 = 13; + string | int | boolean a9 = 14; + string | int | boolean a10 = "World"; + string | int | boolean a11 = "Test"; + + assertEquals("Value is : 12", constPattern3(a1)); + assertEquals("Value is : Hello", constPattern3(a2)); + assertEquals("Value is : true", constPattern3(a3)); + assertEquals("Value is : 15", constPattern3(a4)); + assertEquals("Value is : HelloAgain", constPattern3(a5)); + assertEquals("Value is : false", constPattern3(a6)); + assertEquals("Default value is : NothingToMatch", constPattern3(a7)); + assertEquals("Value is : 13", constPattern3(a8)); + assertEquals("Value is : 14", constPattern3(a9)); + assertEquals("Value is : World", constPattern3(a10)); + assertEquals("Value is : Test", constPattern3(a11)); +} + +function testConstPattern4() { + string result = ""; + any|error a = 12; + match a { + 1 | 2 => {result = "1|2";} + 3 | 4 => {result = "3|4";} + 11 | 12 => {result = "11|12";} + _ => {result = "Default";} + } + + assertEquals("11|12", result); +} + +const CONST1 = "Ballerina"; +const CONST2 = 200; +const NIL = (); + +function constPattern5(any a) returns string { + match a { + 100 => { + return "100"; + } + CONST2 => { + return "200"; + } + "Bal" => { + return "Bal"; + } + NIL => { + return "Nil"; + } + CONST1 => { + return "Ballerina"; + } + _ => { + return "Default"; + } + } +} + +function testConstPattern5() { + string a1 = "Bal"; + string a2 = "Ballerina"; + int a3 = 100; + int a4 = 200; + () a5 = (); + float a6 = 100.0; + + assertEquals("Bal", constPattern5(a1)); + assertEquals("Ballerina", constPattern5(a2)); + assertEquals("100", constPattern5(a3)); + assertEquals("200", constPattern5(a4)); + assertEquals("Nil", constPattern5(a5)); + assertEquals("Default", constPattern5(a6)); +} + +const CONST_1 = "A"; +const CONST_2 = "B"; +const CONST_3 = 10; +const CONST_4 = true; + +function constPattern6(CONST_1 | CONST_2 | CONST_3 | CONST_4 a) returns string { + string results = ""; + + match a { + CONST_2 => { + results += "B"; + } + CONST_1 => { + results += "A"; + } + true => { + results += "true"; + } + CONST_3 => { + results += "10"; + } + } + + match a { + 10 => { + results += "10"; + } + "A" => { + results += "A"; + } + CONST_4 => { + results += "true"; + } + "B" => { + results += "B"; + } + } + + return results; +} + +function testConstPattern6() { + assertEquals("AA", constPattern6(CONST_1)); + assertEquals("BB", constPattern6(CONST_2)); + assertEquals("1010", constPattern6(CONST_3)); + assertEquals("truetrue", constPattern6(CONST_4)); +} + +function testConstPattern7() { + any|error v = error("{UserGenError}Error"); + string result = "no-match"; + + match v { + 0 => { result = "zero"; } + _ => { result = "other"; } + } + + assertEquals("no-match", result); +} + +function assertEquals(anydata expected, anydata actual) { + if expected == actual { + return; + } + + panic error("expected '" + expected.toString() + "', found '" + actual.toString () + "'"); +} diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/statements/matchstmt/varbindingpatternmatchpattern/capture-binding-pattern-negative.bal b/tests/jballerina-unit-test/src/test/resources/test-src/statements/matchstmt/varbindingpatternmatchpattern/capture-binding-pattern-negative.bal new file mode 100644 index 000000000000..a4d15d128760 --- /dev/null +++ b/tests/jballerina-unit-test/src/test/resources/test-src/statements/matchstmt/varbindingpatternmatchpattern/capture-binding-pattern-negative.bal @@ -0,0 +1,26 @@ +// Copyright (c) 2020 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +// +// WSO2 Inc. licenses this file to you under the Apache License, +// Version 2.0 (the "License"); you may not use this file except +// in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +function captureBindingPatternNegative1(any v) returns string { + match v { + var a => { + return "Matched"; + } + _ => { + return "Not matched"; + } + } +} diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/statements/matchstmt/varbindingpatternmatchpattern/capture-binding-pattern.bal b/tests/jballerina-unit-test/src/test/resources/test-src/statements/matchstmt/varbindingpatternmatchpattern/capture-binding-pattern.bal new file mode 100644 index 000000000000..9f70cfcd1477 --- /dev/null +++ b/tests/jballerina-unit-test/src/test/resources/test-src/statements/matchstmt/varbindingpatternmatchpattern/capture-binding-pattern.bal @@ -0,0 +1,83 @@ +// Copyright (c) 2020 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +// +// WSO2 Inc. licenses this file to you under the Apache License, +// Version 2.0 (the "License"); you may not use this file except +// in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +function captureBindingPattern1(any v) returns string { + match v { + var a => { + return "Matched"; + } + } +} + +function testCaptureBindingPattern1() { + assertEquals(captureBindingPattern1(2), "Matched"); + assertEquals(captureBindingPattern1("str"), "Matched"); + assertEquals(captureBindingPattern1(true), "Matched"); +} + +function captureBindingPattern2(any v) returns anydata { + match v { + var a if a is int => { + return a; + } + var a if a is string => { + return a; + } + var a if a is boolean => { + return a; + } + } + + return "Not matched."; +} + +function testCaptureBindingPattern2() { + assertEquals(captureBindingPattern2(2), 2); + assertEquals(captureBindingPattern2("str"), "str"); + assertEquals(captureBindingPattern2(true), true); + assertEquals(captureBindingPattern2([]), "Not matched."); +} + +function captureBindingPattern3(any v) returns string { + match v { + var a if a is int && a == 2 => { + return "a == 2"; + } + var a if a is string && a == "str" => { + return "a == str"; + } + var a if a is boolean && a == true => { + return "a == true"; + } + } + + return "Not matched."; +} + +function testCaptureBindingPattern3() { + assertEquals(captureBindingPattern3(2), "a == 2"); + assertEquals(captureBindingPattern3("str"), "a == str"); + assertEquals(captureBindingPattern3(true), "a == true"); + assertEquals(captureBindingPattern3("Not matched"), "Not matched."); +} + +function assertEquals(anydata expected, anydata actual) { + if expected == actual { + return; + } + + panic error("expected '" + expected.toString() + "', found '" + actual.toString () + "'"); +} From 72ca615b43bff902db919201077af19f349b1348 Mon Sep 17 00:00:00 2001 From: kavindu Date: Sun, 2 Aug 2020 22:13:52 +0530 Subject: [PATCH 06/20] Add license headers --- .../model/clauses/MatchClauseNode.java | 2 +- .../tree/bindingpattern/BindingPattern.java | 17 ++++++++++++++++ .../bindingpattern/CaptureBindingPattern.java | 20 ++++++++++++++++--- .../bindingpattern/ListBindingPattern.java | 17 ++++++++++++++++ .../tree/matchpatterns/ConstPattern.java | 17 ++++++++++++++++ .../tree/matchpatterns/MatchPattern.java | 17 ++++++++++++++++ .../VarBindingPatternMatchPattern.java | 17 ++++++++++++++++ .../matchpatterns/WildCardMatchPattern.java | 17 ++++++++++++++++ .../bindingpatterns/BLangBindingPattern.java | 17 ++++++++++++++++ .../BLangCaptureBindingPattern.java | 17 ++++++++++++++++ .../BLangListBindingPattern.java | 17 ++++++++++++++++ .../tree/expressions/BLangMatchGuard.java | 17 ++++++++++++++++ .../tree/matchpatterns/BLangConstPattern.java | 17 ++++++++++++++++ .../tree/matchpatterns/BLangMatchPattern.java | 17 ++++++++++++++++ .../BLangVarBindingPatternMatchPattern.java | 17 ++++++++++++++++ .../BLangWildCardMatchPattern.java | 17 ++++++++++++++++ ...rn.java => MatchStmtConstPatternTest.java} | 2 +- ...rn.java => CaptureBindingPatternTest.java} | 20 ++++++++++++++++++- .../src/test/resources/testng.xml | 4 ++-- 19 files changed, 278 insertions(+), 8 deletions(-) rename tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/{MatchStmtConstPattern.java => MatchStmtConstPatternTest.java} (99%) rename tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/varbindingpatternmatchpattern/{CaptureBindingPattern.java => CaptureBindingPatternTest.java} (65%) diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/clauses/MatchClauseNode.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/clauses/MatchClauseNode.java index 382dac4e0865..0ab57d979b0c 100644 --- a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/clauses/MatchClauseNode.java +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/clauses/MatchClauseNode.java @@ -26,7 +26,7 @@ import java.util.List; public interface MatchClauseNode extends Node { - // TODO : add getters and setters from match-pattern-list + MatchGuard getMatchGuard(); void setMatchGuard(MatchGuard matchGuard); diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/BindingPattern.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/BindingPattern.java index 099294566805..e196d3120e49 100644 --- a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/BindingPattern.java +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/BindingPattern.java @@ -1,3 +1,20 @@ +/* + * Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ package org.ballerinalang.model.tree.bindingpattern; import org.ballerinalang.model.tree.Node; diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/CaptureBindingPattern.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/CaptureBindingPattern.java index 0c8ef1a5a517..176f15e688d6 100644 --- a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/CaptureBindingPattern.java +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/CaptureBindingPattern.java @@ -1,10 +1,25 @@ +/* + * Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ package org.ballerinalang.model.tree.bindingpattern; -import org.ballerinalang.model.symbols.Symbol; import org.ballerinalang.model.symbols.VariableSymbol; import org.ballerinalang.model.tree.IdentifierNode; import org.ballerinalang.model.tree.Node; -import org.ballerinalang.model.tree.expressions.ExpressionNode; public interface CaptureBindingPattern extends Node { @@ -15,5 +30,4 @@ public interface CaptureBindingPattern extends Node { VariableSymbol getSymbol(); void setSymbol(VariableSymbol symbol); - } diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/ListBindingPattern.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/ListBindingPattern.java index d1ffdbc65b1e..cab038ae8e84 100644 --- a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/ListBindingPattern.java +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/ListBindingPattern.java @@ -1,3 +1,20 @@ +/* + * Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ package org.ballerinalang.model.tree.bindingpattern; import org.ballerinalang.model.tree.Node; diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/ConstPattern.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/ConstPattern.java index 6a6ba202b4bc..ac134c282620 100644 --- a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/ConstPattern.java +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/ConstPattern.java @@ -1,3 +1,20 @@ +/* + * Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ package org.ballerinalang.model.tree.matchpatterns; import org.ballerinalang.model.tree.Node; diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/MatchPattern.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/MatchPattern.java index fb08452af36d..75338665e7dd 100644 --- a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/MatchPattern.java +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/MatchPattern.java @@ -1,3 +1,20 @@ +/* + * Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ package org.ballerinalang.model.tree.matchpatterns; import org.ballerinalang.model.tree.Node; diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/VarBindingPatternMatchPattern.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/VarBindingPatternMatchPattern.java index 0ed0fb3d0f01..6803254f29b1 100644 --- a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/VarBindingPatternMatchPattern.java +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/VarBindingPatternMatchPattern.java @@ -1,3 +1,20 @@ +/* + * Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ package org.ballerinalang.model.tree.matchpatterns; import org.ballerinalang.model.tree.Node; diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/WildCardMatchPattern.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/WildCardMatchPattern.java index 9f8d455aec2a..e35bd32e3d5d 100644 --- a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/WildCardMatchPattern.java +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/WildCardMatchPattern.java @@ -1,3 +1,20 @@ +/* + * Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ package org.ballerinalang.model.tree.matchpatterns; import org.ballerinalang.model.tree.Node; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangBindingPattern.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangBindingPattern.java index b542f2f1e50b..388f7da4a1f8 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangBindingPattern.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangBindingPattern.java @@ -1,3 +1,20 @@ +/* + * Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ package org.wso2.ballerinalang.compiler.tree.bindingpatterns; import org.ballerinalang.model.tree.bindingpattern.BindingPattern; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangCaptureBindingPattern.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangCaptureBindingPattern.java index 438277f90737..c56508b6fa21 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangCaptureBindingPattern.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangCaptureBindingPattern.java @@ -1,3 +1,20 @@ +/* + * Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ package org.wso2.ballerinalang.compiler.tree.bindingpatterns; import org.ballerinalang.model.symbols.VariableSymbol; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangListBindingPattern.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangListBindingPattern.java index ee7cd402fbaa..4f5c05c6ffc6 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangListBindingPattern.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangListBindingPattern.java @@ -1,3 +1,20 @@ +/* + * Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ package org.wso2.ballerinalang.compiler.tree.bindingpatterns; import org.ballerinalang.model.tree.NodeKind; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/expressions/BLangMatchGuard.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/expressions/BLangMatchGuard.java index 841db94189d1..264a1f9d941b 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/expressions/BLangMatchGuard.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/expressions/BLangMatchGuard.java @@ -1,3 +1,20 @@ +/* + * Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ package org.wso2.ballerinalang.compiler.tree.expressions; import org.ballerinalang.model.tree.NodeKind; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangConstPattern.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangConstPattern.java index 07f8c157130b..726c1c442b12 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangConstPattern.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangConstPattern.java @@ -1,3 +1,20 @@ +/* + * Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ package org.wso2.ballerinalang.compiler.tree.matchpatterns; import org.ballerinalang.model.tree.NodeKind; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangMatchPattern.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangMatchPattern.java index cf62aa4c5644..a494a3cd63ae 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangMatchPattern.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangMatchPattern.java @@ -1,3 +1,20 @@ +/* + * Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ package org.wso2.ballerinalang.compiler.tree.matchpatterns; import org.ballerinalang.model.tree.matchpatterns.MatchPattern; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangVarBindingPatternMatchPattern.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangVarBindingPatternMatchPattern.java index 1e16a3d7fce3..1a7977c50e64 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangVarBindingPatternMatchPattern.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangVarBindingPatternMatchPattern.java @@ -1,3 +1,20 @@ +/* + * Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ package org.wso2.ballerinalang.compiler.tree.matchpatterns; import org.ballerinalang.model.tree.NodeKind; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangWildCardMatchPattern.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangWildCardMatchPattern.java index ea1b0e18d884..ad5f5612d4fc 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangWildCardMatchPattern.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangWildCardMatchPattern.java @@ -1,3 +1,20 @@ +/* + * Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ package org.wso2.ballerinalang.compiler.tree.matchpatterns; import org.ballerinalang.model.tree.NodeKind; diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/MatchStmtConstPattern.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/MatchStmtConstPatternTest.java similarity index 99% rename from tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/MatchStmtConstPattern.java rename to tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/MatchStmtConstPatternTest.java index b071cb0223d9..12a544e4f265 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/MatchStmtConstPattern.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/MatchStmtConstPatternTest.java @@ -24,7 +24,7 @@ import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; -public class MatchStmtConstPattern { +public class MatchStmtConstPatternTest { private CompileResult result, resultNegative; diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/varbindingpatternmatchpattern/CaptureBindingPattern.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/varbindingpatternmatchpattern/CaptureBindingPatternTest.java similarity index 65% rename from tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/varbindingpatternmatchpattern/CaptureBindingPattern.java rename to tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/varbindingpatternmatchpattern/CaptureBindingPatternTest.java index d2db09d5ec2f..3449f0f797a0 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/varbindingpatternmatchpattern/CaptureBindingPattern.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/varbindingpatternmatchpattern/CaptureBindingPatternTest.java @@ -1,3 +1,20 @@ +/* + * Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ package org.ballerinalang.test.statements.matchstmt.varbindingpatternmatchpattern; import org.ballerinalang.test.util.BAssertUtil; @@ -8,7 +25,8 @@ import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; -public class CaptureBindingPattern { +@Test(groups = { "disableOnOldParser" }) +public class CaptureBindingPatternTest { private CompileResult result, resultNegative; @BeforeClass diff --git a/tests/jballerina-unit-test/src/test/resources/testng.xml b/tests/jballerina-unit-test/src/test/resources/testng.xml index c6b59536aab4..5f36d64dd681 100644 --- a/tests/jballerina-unit-test/src/test/resources/testng.xml +++ b/tests/jballerina-unit-test/src/test/resources/testng.xml @@ -194,8 +194,8 @@ - - + + From 42d74421896e089de79b87b80644c824e4093ad2 Mon Sep 17 00:00:00 2001 From: kavindu Date: Tue, 4 Aug 2020 10:08:48 +0530 Subject: [PATCH 07/20] Fix checkstyle --- .../org/ballerinalang/model/TreeBuilder.java | 24 +++++++++---------- .../model/clauses/MatchClauseNode.java | 13 ++++++---- ...ngPattern.java => BindingPatternNode.java} | 7 +++++- .../bindingpattern/CaptureBindingPattern.java | 5 ++++ .../bindingpattern/ListBindingPattern.java | 9 +++++-- .../model/tree/expressions/MatchGuard.java | 5 ++++ ...onstPattern.java => ConstPatternNode.java} | 7 +++++- ...atchPattern.java => MatchPatternNode.java} | 7 +++++- ...=> VarBindingPatternMatchPatternNode.java} | 13 ++++++---- ...ern.java => WildCardMatchPatternNode.java} | 8 +++++-- .../tree/statements/MatchStatementNode.java | 5 ++++ .../compiler/desugar/ConstantPropagation.java | 7 +++--- .../compiler/desugar/Desugar.java | 15 ++++++++---- .../compiler/parser/BLangNodeTransformer.java | 22 ++++++----------- .../compiler/parser/NodeCloner.java | 8 +++---- .../semantics/analyzer/CodeAnalyzer.java | 9 ++++--- .../semantics/analyzer/SemanticAnalyzer.java | 6 +++-- .../semantics/analyzer/SymbolEnter.java | 1 - .../bindingpatterns/BLangBindingPattern.java | 7 ++++-- .../BLangCaptureBindingPattern.java | 3 +++ .../BLangListBindingPattern.java | 9 ++++--- .../tree/clauses/BLangMatchClause.java | 16 +++++++------ .../tree/expressions/BLangMatchGuard.java | 3 +++ .../tree/matchpatterns/BLangConstPattern.java | 7 ++++-- .../tree/matchpatterns/BLangMatchPattern.java | 8 ++++--- .../BLangVarBindingPatternMatchPattern.java | 11 +++++---- .../BLangWildCardMatchPattern.java | 7 ++++-- .../tree/statements/BLangMatchStatement.java | 3 +++ 28 files changed, 162 insertions(+), 83 deletions(-) rename compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/{BindingPattern.java => BindingPatternNode.java} (84%) rename compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/{ConstPattern.java => ConstPatternNode.java} (87%) rename compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/{MatchPattern.java => MatchPatternNode.java} (84%) rename compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/{VarBindingPatternMatchPattern.java => VarBindingPatternMatchPatternNode.java} (74%) rename compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/{WildCardMatchPattern.java => WildCardMatchPatternNode.java} (83%) diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/TreeBuilder.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/TreeBuilder.java index 79f24ff86f03..921568a609a5 100644 --- a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/TreeBuilder.java +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/TreeBuilder.java @@ -107,9 +107,9 @@ import org.ballerinalang.model.tree.expressions.XMLQNameNode; import org.ballerinalang.model.tree.expressions.XMLQuotedStringNode; import org.ballerinalang.model.tree.expressions.XMLTextLiteralNode; -import org.ballerinalang.model.tree.matchpatterns.ConstPattern; -import org.ballerinalang.model.tree.matchpatterns.VarBindingPatternMatchPattern; -import org.ballerinalang.model.tree.matchpatterns.WildCardMatchPattern; +import org.ballerinalang.model.tree.matchpatterns.ConstPatternNode; +import org.ballerinalang.model.tree.matchpatterns.VarBindingPatternMatchPatternNode; +import org.ballerinalang.model.tree.matchpatterns.WildCardMatchPatternNode; import org.ballerinalang.model.tree.statements.AssignmentNode; import org.ballerinalang.model.tree.statements.BlockStatementNode; import org.ballerinalang.model.tree.statements.BreakNode; @@ -165,10 +165,6 @@ import org.wso2.ballerinalang.compiler.tree.BLangBlockFunctionBody; import org.wso2.ballerinalang.compiler.tree.BLangClassDefinition; import org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit; -import org.wso2.ballerinalang.compiler.tree.bindingpatterns.BLangCaptureBindingPattern; -import org.wso2.ballerinalang.compiler.tree.bindingpatterns.BLangListBindingPattern; -import org.wso2.ballerinalang.compiler.tree.expressions.BLangMatchGuard; -import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangConstPattern; import org.wso2.ballerinalang.compiler.tree.BLangEndpoint; import org.wso2.ballerinalang.compiler.tree.BLangErrorVariable; import org.wso2.ballerinalang.compiler.tree.BLangExprFunctionBody; @@ -188,9 +184,9 @@ import org.wso2.ballerinalang.compiler.tree.BLangTestablePackage; import org.wso2.ballerinalang.compiler.tree.BLangTupleVariable; import org.wso2.ballerinalang.compiler.tree.BLangTypeDefinition; -import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangVarBindingPatternMatchPattern; -import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangWildCardMatchPattern; import org.wso2.ballerinalang.compiler.tree.BLangXMLNS; +import org.wso2.ballerinalang.compiler.tree.bindingpatterns.BLangCaptureBindingPattern; +import org.wso2.ballerinalang.compiler.tree.bindingpatterns.BLangListBindingPattern; import org.wso2.ballerinalang.compiler.tree.clauses.BLangDoClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangFromClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangJoinClause; @@ -231,6 +227,7 @@ import org.wso2.ballerinalang.compiler.tree.expressions.BLangMarkdownReturnParameterDocumentation; import org.wso2.ballerinalang.compiler.tree.expressions.BLangMatchExpression; import org.wso2.ballerinalang.compiler.tree.expressions.BLangMatchExpression.BLangMatchExprPatternClause; +import org.wso2.ballerinalang.compiler.tree.expressions.BLangMatchGuard; import org.wso2.ballerinalang.compiler.tree.expressions.BLangNamedArgsExpression; import org.wso2.ballerinalang.compiler.tree.expressions.BLangNumericLiteral; import org.wso2.ballerinalang.compiler.tree.expressions.BLangObjectConstructorExpression; @@ -268,6 +265,9 @@ import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName; import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString; import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLTextLiteral; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangConstPattern; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangVarBindingPatternMatchPattern; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangWildCardMatchPattern; import org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment; import org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt; import org.wso2.ballerinalang.compiler.tree.statements.BLangBreak; @@ -753,15 +753,15 @@ public static MatchStatementNode createMatchStatementNode() { return new BLangMatchStatement(); } - public static WildCardMatchPattern createWildCardMatchPattern() { + public static WildCardMatchPatternNode createWildCardMatchPattern() { return new BLangWildCardMatchPattern(); } - public static ConstPattern createConstMatchPattern() { + public static ConstPatternNode createConstMatchPattern() { return new BLangConstPattern(); } - public static VarBindingPatternMatchPattern createVarBindingPattern() { + public static VarBindingPatternMatchPatternNode createVarBindingPattern() { return new BLangVarBindingPatternMatchPattern(); } diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/clauses/MatchClauseNode.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/clauses/MatchClauseNode.java index 0ab57d979b0c..f3504851c695 100644 --- a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/clauses/MatchClauseNode.java +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/clauses/MatchClauseNode.java @@ -17,14 +17,19 @@ */ package org.ballerinalang.model.clauses; -import org.ballerinalang.model.tree.expressions.ExpressionNode; -import org.ballerinalang.model.tree.matchpatterns.MatchPattern; import org.ballerinalang.model.tree.Node; +import org.ballerinalang.model.tree.expressions.ExpressionNode; import org.ballerinalang.model.tree.expressions.MatchGuard; +import org.ballerinalang.model.tree.matchpatterns.MatchPatternNode; import org.ballerinalang.model.tree.statements.BlockStatementNode; import java.util.List; +/** + * The interface with the APIs to implement the match-clause. + * + * @since 2.0.0 + */ public interface MatchClauseNode extends Node { MatchGuard getMatchGuard(); @@ -35,9 +40,9 @@ public interface MatchClauseNode extends Node { void setBlockStatement(BlockStatementNode blockStatement); - List getMatchPatterns(); + List getMatchPatterns(); - void addMatchPattern(MatchPattern matchPattern); + void addMatchPattern(MatchPatternNode matchPattern); boolean isLastClause(); diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/BindingPattern.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/BindingPatternNode.java similarity index 84% rename from compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/BindingPattern.java rename to compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/BindingPatternNode.java index e196d3120e49..0549b0ea03b8 100644 --- a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/BindingPattern.java +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/BindingPatternNode.java @@ -19,6 +19,11 @@ import org.ballerinalang.model.tree.Node; -public interface BindingPattern extends Node { +/** + * The interface with the APIs to implement the binding-pattern. + * + * @since 2.0.0 + */ +public interface BindingPatternNode extends Node { } diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/CaptureBindingPattern.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/CaptureBindingPattern.java index 176f15e688d6..51d57fb1fdd5 100644 --- a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/CaptureBindingPattern.java +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/CaptureBindingPattern.java @@ -21,6 +21,11 @@ import org.ballerinalang.model.tree.IdentifierNode; import org.ballerinalang.model.tree.Node; +/** + * The interface with the APIs to implement the capture-binding-pattern. + * + * @since 2.0.0 + */ public interface CaptureBindingPattern extends Node { IdentifierNode getIdentifier(); diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/ListBindingPattern.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/ListBindingPattern.java index cab038ae8e84..1ecbc5b30ada 100644 --- a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/ListBindingPattern.java +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/ListBindingPattern.java @@ -21,8 +21,13 @@ import java.util.List; +/** + * The interface with the APIs to implement the list-binding-pattern. + * + * @since 2.0.0 + */ public interface ListBindingPattern extends Node { - List getBindingPatterns(); + List getBindingPatterns(); - void addBindingPattern(BindingPattern bindingPattern); + void addBindingPattern(BindingPatternNode bindingPattern); } diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/expressions/MatchGuard.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/expressions/MatchGuard.java index d18a1e634a46..81cf3a6c602b 100644 --- a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/expressions/MatchGuard.java +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/expressions/MatchGuard.java @@ -17,6 +17,11 @@ */ package org.ballerinalang.model.tree.expressions; +/** + * The interface with the APIs to implement the match-guard. + * + * @since 2.0.0 + */ public interface MatchGuard { ExpressionNode getExpression(); diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/ConstPattern.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/ConstPatternNode.java similarity index 87% rename from compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/ConstPattern.java rename to compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/ConstPatternNode.java index ac134c282620..0d882bb83fce 100644 --- a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/ConstPattern.java +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/ConstPatternNode.java @@ -20,7 +20,12 @@ import org.ballerinalang.model.tree.Node; import org.ballerinalang.model.tree.expressions.ExpressionNode; -public interface ConstPattern extends Node { +/** + * The interface with the APIs to implement the const-pattern. + * + * @since 2.0.0 + */ +public interface ConstPatternNode extends Node { ExpressionNode getExpresion(); diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/MatchPattern.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/MatchPatternNode.java similarity index 84% rename from compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/MatchPattern.java rename to compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/MatchPatternNode.java index 75338665e7dd..f3e396b31c78 100644 --- a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/MatchPattern.java +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/MatchPatternNode.java @@ -19,6 +19,11 @@ import org.ballerinalang.model.tree.Node; -public interface MatchPattern extends Node { +/** + * The interface with the APIs to implement the match-pattern. + * + * @since 2.0.0 + */ +public interface MatchPatternNode extends Node { } diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/VarBindingPatternMatchPattern.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/VarBindingPatternMatchPatternNode.java similarity index 74% rename from compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/VarBindingPatternMatchPattern.java rename to compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/VarBindingPatternMatchPatternNode.java index 6803254f29b1..4f22979a5e09 100644 --- a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/VarBindingPatternMatchPattern.java +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/VarBindingPatternMatchPatternNode.java @@ -18,11 +18,16 @@ package org.ballerinalang.model.tree.matchpatterns; import org.ballerinalang.model.tree.Node; -import org.ballerinalang.model.tree.bindingpattern.BindingPattern; +import org.ballerinalang.model.tree.bindingpattern.BindingPatternNode; -public interface VarBindingPatternMatchPattern extends Node { +/** + * The interface with the APIs to implement the var-binding-pattern match-pattern. + * + * @since 2.0.0 + */ +public interface VarBindingPatternMatchPatternNode extends Node { - BindingPattern getBindingPattern(); + BindingPatternNode getBindingPattern(); - void setBindingPattern(BindingPattern bindingPattern); + void setBindingPattern(BindingPatternNode bindingPattern); } diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/WildCardMatchPattern.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/WildCardMatchPatternNode.java similarity index 83% rename from compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/WildCardMatchPattern.java rename to compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/WildCardMatchPatternNode.java index e35bd32e3d5d..abd75e330669 100644 --- a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/WildCardMatchPattern.java +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/WildCardMatchPatternNode.java @@ -19,6 +19,10 @@ import org.ballerinalang.model.tree.Node; -public interface WildCardMatchPattern extends Node { - +/** + * The interface with the APIs to implement the wild-card-binding-pattern. + * + * @since 2.0.0 + */ +public interface WildCardMatchPatternNode extends Node { } diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/statements/MatchStatementNode.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/statements/MatchStatementNode.java index fbcd2410ac3b..b53b56ba5b98 100644 --- a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/statements/MatchStatementNode.java +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/statements/MatchStatementNode.java @@ -22,6 +22,11 @@ import java.util.List; +/** + * The interface with the APIs to implement the match-stmt. + * + * @since 2.0.0 + */ public interface MatchStatementNode extends StatementNode { ExpressionNode getExpression(); diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/ConstantPropagation.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/ConstantPropagation.java index a95a5f1cc95d..83d0aaef2b0d 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/ConstantPropagation.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/ConstantPropagation.java @@ -36,7 +36,6 @@ import org.wso2.ballerinalang.compiler.tree.BLangExprFunctionBody; import org.wso2.ballerinalang.compiler.tree.BLangExternalFunctionBody; import org.wso2.ballerinalang.compiler.tree.BLangFunction; -import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangMatchPattern; import org.wso2.ballerinalang.compiler.tree.BLangNode; import org.wso2.ballerinalang.compiler.tree.BLangNodeVisitor; import org.wso2.ballerinalang.compiler.tree.BLangPackage; @@ -44,8 +43,6 @@ import org.wso2.ballerinalang.compiler.tree.BLangSimpleVariable; import org.wso2.ballerinalang.compiler.tree.BLangTupleVariable; import org.wso2.ballerinalang.compiler.tree.BLangTypeDefinition; -import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangVarBindingPatternMatchPattern; -import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangWildCardMatchPattern; import org.wso2.ballerinalang.compiler.tree.BLangXMLNS; import org.wso2.ballerinalang.compiler.tree.clauses.BLangDoClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangFromClause; @@ -117,6 +114,10 @@ import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLNavigationAccess; import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLProcInsLiteral; import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLTextLiteral; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangConstPattern; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangMatchPattern; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangVarBindingPatternMatchPattern; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangWildCardMatchPattern; import org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment; import org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt; import org.wso2.ballerinalang.compiler.tree.statements.BLangBreak; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java index 37d136a75754..3bbddad3c7a0 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java @@ -86,7 +86,6 @@ import org.wso2.ballerinalang.compiler.tree.BLangIdentifier; import org.wso2.ballerinalang.compiler.tree.BLangImportPackage; import org.wso2.ballerinalang.compiler.tree.BLangInvokableNode; -import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangMatchPattern; import org.wso2.ballerinalang.compiler.tree.BLangNode; import org.wso2.ballerinalang.compiler.tree.BLangNodeVisitor; import org.wso2.ballerinalang.compiler.tree.BLangPackage; @@ -103,6 +102,8 @@ import org.wso2.ballerinalang.compiler.tree.BLangXMLNS; import org.wso2.ballerinalang.compiler.tree.BLangXMLNS.BLangLocalXMLNS; import org.wso2.ballerinalang.compiler.tree.BLangXMLNS.BLangPackageXMLNS; +import org.wso2.ballerinalang.compiler.tree.bindingpatterns.BLangBindingPattern; +import org.wso2.ballerinalang.compiler.tree.bindingpatterns.BLangCaptureBindingPattern; import org.wso2.ballerinalang.compiler.tree.clauses.BLangMatchClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangOnFailClause; import org.wso2.ballerinalang.compiler.tree.expressions.BLangAccessExpression; @@ -192,6 +193,8 @@ import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName; import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString; import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLTextLiteral; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangConstPattern; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangMatchPattern; import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangVarBindingPatternMatchPattern; import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangWildCardMatchPattern; import org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment; @@ -3007,14 +3010,16 @@ private BLangIf createIfElseStmtFromMatchPattern(BLangMatchPattern matchPattern, switch (patternKind) { case WILDCARD_MATCH_PATTERN: return ASTBuilderUtil.createIfElseStmt(pos, ASTBuilderUtil.createLiteral(pos, - symTable.booleanType, ((BLangWildCardMatchPattern) matchPattern).matchesAll), successBody, null); + symTable.booleanType, ((BLangWildCardMatchPattern) matchPattern).matchesAll), successBody, + null); case CONST_MATCH_PATTERN: BLangConstPattern constMatchPattern = (BLangConstPattern) matchPattern; - return ASTBuilderUtil.createIfElseStmt(pos, createBinaryExpression(constMatchPattern.pos, matchExprVarRef, - constMatchPattern.expr), successBody, null); + return ASTBuilderUtil.createIfElseStmt(pos, createBinaryExpression(constMatchPattern.pos, + matchExprVarRef, constMatchPattern.expr), successBody, null); case VAR_BINDING_PATTERN_MATCH_PATTERN: // var [a, b] or var a - BLangVarBindingPatternMatchPattern varBindingPattern = (BLangVarBindingPatternMatchPattern) matchPattern; + BLangVarBindingPatternMatchPattern varBindingPattern = + (BLangVarBindingPatternMatchPattern) matchPattern; BLangBindingPattern bindingPattern = varBindingPattern.getBindingPattern(); switch (bindingPattern.getKind()) { diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/BLangNodeTransformer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/BLangNodeTransformer.java index 8132acaecff8..bcbccbb97217 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/BLangNodeTransformer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/BLangNodeTransformer.java @@ -232,7 +232,6 @@ import org.ballerinalang.model.tree.SimpleVariableNode; import org.ballerinalang.model.tree.TopLevelNode; import org.ballerinalang.model.tree.VariableNode; -import org.ballerinalang.model.tree.bindingpattern.BindingPattern; import org.ballerinalang.model.tree.expressions.ExpressionNode; import org.ballerinalang.model.tree.expressions.XMLNavigationAccess; import org.ballerinalang.model.tree.statements.VariableDefinitionNode; @@ -244,10 +243,6 @@ import org.wso2.ballerinalang.compiler.tree.BLangBlockFunctionBody; import org.wso2.ballerinalang.compiler.tree.BLangClassDefinition; import org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit; -import org.wso2.ballerinalang.compiler.tree.bindingpatterns.BLangCaptureBindingPattern; -import org.wso2.ballerinalang.compiler.tree.bindingpatterns.BLangListBindingPattern; -import org.wso2.ballerinalang.compiler.tree.expressions.BLangMatchGuard; -import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangConstPattern; import org.wso2.ballerinalang.compiler.tree.BLangErrorVariable; import org.wso2.ballerinalang.compiler.tree.BLangExprFunctionBody; import org.wso2.ballerinalang.compiler.tree.BLangExternalFunctionBody; @@ -269,9 +264,8 @@ import org.wso2.ballerinalang.compiler.tree.BLangTupleVariable; import org.wso2.ballerinalang.compiler.tree.BLangTypeDefinition; import org.wso2.ballerinalang.compiler.tree.BLangVariable; -import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangVarBindingPatternMatchPattern; -import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangWildCardMatchPattern; import org.wso2.ballerinalang.compiler.tree.BLangXMLNS; +import org.wso2.ballerinalang.compiler.tree.bindingpatterns.BLangCaptureBindingPattern; import org.wso2.ballerinalang.compiler.tree.clauses.BLangDoClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangFromClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangJoinClause; @@ -310,6 +304,7 @@ import org.wso2.ballerinalang.compiler.tree.expressions.BLangMarkdownDocumentationLine; import org.wso2.ballerinalang.compiler.tree.expressions.BLangMarkdownParameterDocumentation; import org.wso2.ballerinalang.compiler.tree.expressions.BLangMarkdownReturnParameterDocumentation; +import org.wso2.ballerinalang.compiler.tree.expressions.BLangMatchGuard; import org.wso2.ballerinalang.compiler.tree.expressions.BLangNamedArgsExpression; import org.wso2.ballerinalang.compiler.tree.expressions.BLangNumericLiteral; import org.wso2.ballerinalang.compiler.tree.expressions.BLangObjectConstructorExpression; @@ -353,6 +348,9 @@ import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName; import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString; import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLTextLiteral; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangConstPattern; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangVarBindingPatternMatchPattern; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangWildCardMatchPattern; import org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment; import org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt; import org.wso2.ballerinalang.compiler.tree.statements.BLangBreak; @@ -3829,7 +3827,8 @@ public BLangNode transform(MatchStatementNode matchStatementNode) { } - private BLangCaptureBindingPattern createCaptureBindingPattern(CaptureBindingPatternNode captureBindingPatternNode) { + private BLangCaptureBindingPattern createCaptureBindingPattern(CaptureBindingPatternNode + captureBindingPatternNode) { BLangCaptureBindingPattern bLangCaptureBindingPattern = (BLangCaptureBindingPattern) TreeBuilder.createCaptureBindingPattern(); bLangCaptureBindingPattern.setIdentifier(createIdentifier(captureBindingPatternNode @@ -3838,13 +3837,6 @@ private BLangCaptureBindingPattern createCaptureBindingPattern(CaptureBindingPat return bLangCaptureBindingPattern; } - public BLangExpression createLiteralOrReference(Node node) { - if (node.kind() == SyntaxKind.SIMPLE_NAME_REFERENCE) { - return createExpression(node); - } - return createSimpleLiteral(node); - } - private BLangXMLElementFilter createXMLElementFilter(Node node) { String ns = ""; String elementName = "*"; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/NodeCloner.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/NodeCloner.java index 30d80a99baf4..5eb8110fe73f 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/NodeCloner.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/NodeCloner.java @@ -29,8 +29,6 @@ import org.wso2.ballerinalang.compiler.tree.BLangBlockFunctionBody; import org.wso2.ballerinalang.compiler.tree.BLangClassDefinition; import org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit; -import org.wso2.ballerinalang.compiler.tree.bindingpatterns.BLangCaptureBindingPattern; -import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangConstPattern; import org.wso2.ballerinalang.compiler.tree.BLangEndpoint; import org.wso2.ballerinalang.compiler.tree.BLangErrorVariable; import org.wso2.ballerinalang.compiler.tree.BLangErrorVariable.BLangErrorDetailEntry; @@ -57,10 +55,9 @@ import org.wso2.ballerinalang.compiler.tree.BLangTupleVariable; import org.wso2.ballerinalang.compiler.tree.BLangTypeDefinition; import org.wso2.ballerinalang.compiler.tree.BLangVariable; -import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangVarBindingPatternMatchPattern; -import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangWildCardMatchPattern; import org.wso2.ballerinalang.compiler.tree.BLangWorker; import org.wso2.ballerinalang.compiler.tree.BLangXMLNS; +import org.wso2.ballerinalang.compiler.tree.bindingpatterns.BLangCaptureBindingPattern; import org.wso2.ballerinalang.compiler.tree.clauses.BLangDoClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangFromClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangJoinClause; @@ -151,6 +148,9 @@ import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString; import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLSequenceLiteral; import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLTextLiteral; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangConstPattern; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangVarBindingPatternMatchPattern; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangWildCardMatchPattern; import org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment; import org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt; import org.wso2.ballerinalang.compiler.tree.statements.BLangBreak; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/CodeAnalyzer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/CodeAnalyzer.java index e44674ef2053..74e69c6f3f18 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/CodeAnalyzer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/CodeAnalyzer.java @@ -81,10 +81,10 @@ import org.wso2.ballerinalang.compiler.tree.BLangTupleVariable; import org.wso2.ballerinalang.compiler.tree.BLangTypeDefinition; import org.wso2.ballerinalang.compiler.tree.BLangVariable; -import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangVarBindingPatternMatchPattern; -import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangWildCardMatchPattern; import org.wso2.ballerinalang.compiler.tree.BLangWorker; import org.wso2.ballerinalang.compiler.tree.BLangXMLNS; +import org.wso2.ballerinalang.compiler.tree.bindingpatterns.BLangBindingPattern; +import org.wso2.ballerinalang.compiler.tree.bindingpatterns.BLangCaptureBindingPattern; import org.wso2.ballerinalang.compiler.tree.clauses.BLangDoClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangFromClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangJoinClause; @@ -155,6 +155,10 @@ import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName; import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString; import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLTextLiteral; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangConstPattern; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangMatchPattern; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangVarBindingPatternMatchPattern; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangWildCardMatchPattern; import org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment; import org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt; import org.wso2.ballerinalang.compiler.tree.statements.BLangBreak; @@ -217,7 +221,6 @@ import org.wso2.ballerinalang.compiler.util.diagnotic.BLangDiagnosticLogHelper; import org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos; import org.wso2.ballerinalang.util.Flags; -import org.wso2.ballerinalang.util.Lists; import java.util.ArrayList; import java.util.Comparator; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java index aa230378f120..2b90accac979 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java @@ -80,10 +80,9 @@ import org.wso2.ballerinalang.compiler.tree.BLangTupleVariable; import org.wso2.ballerinalang.compiler.tree.BLangTypeDefinition; import org.wso2.ballerinalang.compiler.tree.BLangVariable; -import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangVarBindingPatternMatchPattern; -import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangWildCardMatchPattern; import org.wso2.ballerinalang.compiler.tree.BLangWorker; import org.wso2.ballerinalang.compiler.tree.BLangXMLNS; +import org.wso2.ballerinalang.compiler.tree.bindingpatterns.BLangCaptureBindingPattern; import org.wso2.ballerinalang.compiler.tree.clauses.BLangMatchClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangOnFailClause; import org.wso2.ballerinalang.compiler.tree.expressions.BLangAccessExpression; @@ -107,6 +106,9 @@ import org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeConversionExpr; import org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeInit; import org.wso2.ballerinalang.compiler.tree.expressions.BLangVariableReference; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangConstPattern; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangVarBindingPatternMatchPattern; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangWildCardMatchPattern; import org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment; import org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt; import org.wso2.ballerinalang.compiler.tree.statements.BLangBreak; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SymbolEnter.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SymbolEnter.java index 1c41a415ecc7..2d21ea4e8825 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SymbolEnter.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SymbolEnter.java @@ -99,7 +99,6 @@ import org.wso2.ballerinalang.compiler.tree.BLangTypeDefinition; import org.wso2.ballerinalang.compiler.tree.BLangWorker; import org.wso2.ballerinalang.compiler.tree.BLangXMLNS; -import org.wso2.ballerinalang.compiler.tree.bindingpatterns.BLangCaptureBindingPattern; import org.wso2.ballerinalang.compiler.tree.expressions.BLangConstant; import org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression; import org.wso2.ballerinalang.compiler.tree.expressions.BLangLambdaFunction; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangBindingPattern.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangBindingPattern.java index 388f7da4a1f8..36c97f748458 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangBindingPattern.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangBindingPattern.java @@ -17,9 +17,12 @@ */ package org.wso2.ballerinalang.compiler.tree.bindingpatterns; -import org.ballerinalang.model.tree.bindingpattern.BindingPattern; +import org.ballerinalang.model.tree.bindingpattern.BindingPatternNode; import org.wso2.ballerinalang.compiler.tree.BLangNode; -public abstract class BLangBindingPattern extends BLangNode implements BindingPattern { +/** + * @since 2.0.0 + */ +public abstract class BLangBindingPattern extends BLangNode implements BindingPatternNode { } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangCaptureBindingPattern.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangCaptureBindingPattern.java index c56508b6fa21..94983c827159 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangCaptureBindingPattern.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangCaptureBindingPattern.java @@ -25,6 +25,9 @@ import org.wso2.ballerinalang.compiler.tree.BLangIdentifier; import org.wso2.ballerinalang.compiler.tree.BLangNodeVisitor; +/** + * @since 2.0.0 + */ public class BLangCaptureBindingPattern extends BLangBindingPattern implements CaptureBindingPattern { BLangIdentifier identifier; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangListBindingPattern.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangListBindingPattern.java index 4f5c05c6ffc6..190dce2df0c1 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangListBindingPattern.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangListBindingPattern.java @@ -18,23 +18,26 @@ package org.wso2.ballerinalang.compiler.tree.bindingpatterns; import org.ballerinalang.model.tree.NodeKind; -import org.ballerinalang.model.tree.bindingpattern.BindingPattern; +import org.ballerinalang.model.tree.bindingpattern.BindingPatternNode; import org.ballerinalang.model.tree.bindingpattern.ListBindingPattern; import org.wso2.ballerinalang.compiler.tree.BLangNodeVisitor; import java.util.ArrayList; import java.util.List; +/** + * @since 2.0.0 + */ public class BLangListBindingPattern extends BLangBindingPattern implements ListBindingPattern { public List bindingPatterns = new ArrayList<>(); @Override - public List getBindingPatterns() { + public List getBindingPatterns() { return bindingPatterns; } @Override - public void addBindingPattern(BindingPattern bindingPattern) { + public void addBindingPattern(BindingPatternNode bindingPattern) { bindingPatterns.add((BLangBindingPattern) bindingPattern); } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/clauses/BLangMatchClause.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/clauses/BLangMatchClause.java index ee4656d6f6bd..9850445963ed 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/clauses/BLangMatchClause.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/clauses/BLangMatchClause.java @@ -18,22 +18,24 @@ package org.wso2.ballerinalang.compiler.tree.clauses; import org.ballerinalang.model.clauses.MatchClauseNode; -import org.ballerinalang.model.tree.expressions.ExpressionNode; -import org.ballerinalang.model.tree.matchpatterns.MatchPattern; import org.ballerinalang.model.tree.NodeKind; +import org.ballerinalang.model.tree.expressions.ExpressionNode; import org.ballerinalang.model.tree.expressions.MatchGuard; +import org.ballerinalang.model.tree.matchpatterns.MatchPatternNode; import org.ballerinalang.model.tree.statements.BlockStatementNode; -import org.wso2.ballerinalang.compiler.semantics.model.types.BType; -import org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression; -import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangMatchPattern; import org.wso2.ballerinalang.compiler.tree.BLangNode; import org.wso2.ballerinalang.compiler.tree.BLangNodeVisitor; +import org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression; import org.wso2.ballerinalang.compiler.tree.expressions.BLangMatchGuard; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangMatchPattern; import org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt; import java.util.ArrayList; import java.util.List; +/** + * @since 2.0.0 + */ public class BLangMatchClause extends BLangNode implements MatchClauseNode { public List matchPatterns = new ArrayList<>(); @@ -73,12 +75,12 @@ public void setBlockStatement(BlockStatementNode blockStatement) { } @Override - public List getMatchPatterns() { + public List getMatchPatterns() { return matchPatterns; } @Override - public void addMatchPattern(MatchPattern matchPattern) { + public void addMatchPattern(MatchPatternNode matchPattern) { matchPatterns.add((BLangMatchPattern) matchPattern); } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/expressions/BLangMatchGuard.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/expressions/BLangMatchGuard.java index 264a1f9d941b..c9ec29140235 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/expressions/BLangMatchGuard.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/expressions/BLangMatchGuard.java @@ -22,6 +22,9 @@ import org.ballerinalang.model.tree.expressions.MatchGuard; import org.wso2.ballerinalang.compiler.tree.BLangNodeVisitor; +/** + * @since 2.0.0 + */ public class BLangMatchGuard extends BLangExpression implements MatchGuard { public BLangExpression expr; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangConstPattern.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangConstPattern.java index 726c1c442b12..aecffe23850f 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangConstPattern.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangConstPattern.java @@ -19,11 +19,14 @@ import org.ballerinalang.model.tree.NodeKind; import org.ballerinalang.model.tree.expressions.ExpressionNode; -import org.ballerinalang.model.tree.matchpatterns.ConstPattern; +import org.ballerinalang.model.tree.matchpatterns.ConstPatternNode; import org.wso2.ballerinalang.compiler.tree.BLangNodeVisitor; import org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression; -public class BLangConstPattern extends BLangMatchPattern implements ConstPattern { +/** + * @since 2.0.0 + */ +public class BLangConstPattern extends BLangMatchPattern implements ConstPatternNode { public BLangExpression expr; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangMatchPattern.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangMatchPattern.java index a494a3cd63ae..882549b4c532 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangMatchPattern.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangMatchPattern.java @@ -17,12 +17,14 @@ */ package org.wso2.ballerinalang.compiler.tree.matchpatterns; -import org.ballerinalang.model.tree.matchpatterns.MatchPattern; -import org.wso2.ballerinalang.compiler.semantics.model.types.BType; +import org.ballerinalang.model.tree.matchpatterns.MatchPatternNode; import org.wso2.ballerinalang.compiler.tree.BLangNode; import org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression; -public abstract class BLangMatchPattern extends BLangNode implements MatchPattern { +/** + * @since 2.0.0 + */ +public abstract class BLangMatchPattern extends BLangNode implements MatchPatternNode { public BLangExpression matchExpr; // TODO : should changed as action or expr public boolean matchGuardIsAvailable; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangVarBindingPatternMatchPattern.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangVarBindingPatternMatchPattern.java index 1a7977c50e64..04b98fa14a33 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangVarBindingPatternMatchPattern.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangVarBindingPatternMatchPattern.java @@ -18,12 +18,15 @@ package org.wso2.ballerinalang.compiler.tree.matchpatterns; import org.ballerinalang.model.tree.NodeKind; -import org.ballerinalang.model.tree.bindingpattern.BindingPattern; -import org.ballerinalang.model.tree.matchpatterns.VarBindingPatternMatchPattern; +import org.ballerinalang.model.tree.bindingpattern.BindingPatternNode; +import org.ballerinalang.model.tree.matchpatterns.VarBindingPatternMatchPatternNode; import org.wso2.ballerinalang.compiler.tree.BLangNodeVisitor; import org.wso2.ballerinalang.compiler.tree.bindingpatterns.BLangBindingPattern; -public class BLangVarBindingPatternMatchPattern extends BLangMatchPattern implements VarBindingPatternMatchPattern { +/** + * @since 2.0.0 + */ +public class BLangVarBindingPatternMatchPattern extends BLangMatchPattern implements VarBindingPatternMatchPatternNode { BLangBindingPattern bindingPattern; @@ -44,7 +47,7 @@ public BLangBindingPattern getBindingPattern() { } @Override - public void setBindingPattern(BindingPattern bindingPattern) { + public void setBindingPattern(BindingPatternNode bindingPattern) { this.bindingPattern = (BLangBindingPattern) bindingPattern; } } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangWildCardMatchPattern.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangWildCardMatchPattern.java index ad5f5612d4fc..e2047c96cab1 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangWildCardMatchPattern.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangWildCardMatchPattern.java @@ -18,10 +18,13 @@ package org.wso2.ballerinalang.compiler.tree.matchpatterns; import org.ballerinalang.model.tree.NodeKind; -import org.ballerinalang.model.tree.matchpatterns.WildCardMatchPattern; +import org.ballerinalang.model.tree.matchpatterns.WildCardMatchPatternNode; import org.wso2.ballerinalang.compiler.tree.BLangNodeVisitor; -public class BLangWildCardMatchPattern extends BLangMatchPattern implements WildCardMatchPattern { +/** + * @since 2.0.0 + */ +public class BLangWildCardMatchPattern extends BLangMatchPattern implements WildCardMatchPatternNode { public boolean matchesAll; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/statements/BLangMatchStatement.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/statements/BLangMatchStatement.java index 1a1df530722c..7ae77e868f1a 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/statements/BLangMatchStatement.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/statements/BLangMatchStatement.java @@ -28,6 +28,9 @@ import java.util.ArrayList; import java.util.List; +/** + * @since 2.0.0 + */ public class BLangMatchStatement extends BLangStatement implements MatchStatementNode { public BLangExpression expr; // TODO : replace with new node `action|expression` public List matchClauses = new ArrayList<>(); From 93af60582061bbc5d1c6681efc5677a724a3f46c Mon Sep 17 00:00:00 2001 From: kavindu Date: Tue, 4 Aug 2020 17:21:36 +0530 Subject: [PATCH 08/20] Fix bugs --- compiler/ballerina-lang/spotbugs-exclude.xml | 6 ++++++ .../model/tree/bindingpattern/CaptureBindingPattern.java | 2 -- .../org/wso2/ballerinalang/compiler/parser/NodeCloner.java | 3 ++- .../tree/bindingpatterns/BLangCaptureBindingPattern.java | 5 ----- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/compiler/ballerina-lang/spotbugs-exclude.xml b/compiler/ballerina-lang/spotbugs-exclude.xml index f5d8f71da021..8eb044d71d9a 100644 --- a/compiler/ballerina-lang/spotbugs-exclude.xml +++ b/compiler/ballerina-lang/spotbugs-exclude.xml @@ -40,6 +40,12 @@ + + + + + + diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/CaptureBindingPattern.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/CaptureBindingPattern.java index 51d57fb1fdd5..48eb0bf444fc 100644 --- a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/CaptureBindingPattern.java +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/CaptureBindingPattern.java @@ -33,6 +33,4 @@ public interface CaptureBindingPattern extends Node { void setIdentifier(IdentifierNode variableName); VariableSymbol getSymbol(); - - void setSymbol(VariableSymbol symbol); } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/NodeCloner.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/NodeCloner.java index 5eb8110fe73f..85f223e785fa 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/NodeCloner.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/NodeCloner.java @@ -24,6 +24,7 @@ import org.ballerinalang.model.tree.TopLevelNode; import org.ballerinalang.model.tree.expressions.ExpressionNode; import org.ballerinalang.model.tree.statements.VariableDefinitionNode; +import org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol; import org.wso2.ballerinalang.compiler.tree.BLangAnnotation; import org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment; import org.wso2.ballerinalang.compiler.tree.BLangBlockFunctionBody; @@ -733,7 +734,7 @@ public void visit(BLangCaptureBindingPattern source) { BLangCaptureBindingPattern clone = new BLangCaptureBindingPattern(); source.cloneRef = clone; clone.setIdentifier(source.getIdentifier()); - clone.setSymbol(source.getSymbol()); + clone.symbol = (BVarSymbol) source.getSymbol(); } @Override diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangCaptureBindingPattern.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangCaptureBindingPattern.java index 94983c827159..cd68e5b544bf 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangCaptureBindingPattern.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangCaptureBindingPattern.java @@ -57,9 +57,4 @@ public void setIdentifier(IdentifierNode identifier) { public VariableSymbol getSymbol() { return symbol; } - - @Override - public void setSymbol(VariableSymbol symbol) { - this.symbol = (BVarSymbol) symbol; - } } From 5440c99deba010c7f2f01201418085192e1c85ed Mon Sep 17 00:00:00 2001 From: kavindu Date: Tue, 4 Aug 2020 21:08:35 +0530 Subject: [PATCH 09/20] Add missing docs --- .../test/statements/matchstmt/MatchStmtConstPatternTest.java | 5 +++++ .../CaptureBindingPatternTest.java | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/MatchStmtConstPatternTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/MatchStmtConstPatternTest.java index 12a544e4f265..27d791134a14 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/MatchStmtConstPatternTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/MatchStmtConstPatternTest.java @@ -24,6 +24,11 @@ import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; +/** + * Test cases to verify the behaviour of the const-pattern. + * + * @since 2.0.0 + */ public class MatchStmtConstPatternTest { private CompileResult result, resultNegative; diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/varbindingpatternmatchpattern/CaptureBindingPatternTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/varbindingpatternmatchpattern/CaptureBindingPatternTest.java index 3449f0f797a0..d719c2133463 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/varbindingpatternmatchpattern/CaptureBindingPatternTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/varbindingpatternmatchpattern/CaptureBindingPatternTest.java @@ -25,6 +25,11 @@ import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; +/** + * Test cases to verify the behaviour of the var binding-pattern capture-pattern. + * + * @since 2.0.0 + */ @Test(groups = { "disableOnOldParser" }) public class CaptureBindingPatternTest { private CompileResult result, resultNegative; From 78bd96195c6cf9c9c1149a39ef13bc4613544cf0 Mon Sep 17 00:00:00 2001 From: kavindu Date: Wed, 5 Aug 2020 11:33:47 +0530 Subject: [PATCH 10/20] Change the transformer --- .../compiler/parser/BLangNodeTransformer.java | 104 +++++++++--------- 1 file changed, 55 insertions(+), 49 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/BLangNodeTransformer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/BLangNodeTransformer.java index bcbccbb97217..9599386de8e4 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/BLangNodeTransformer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/BLangNodeTransformer.java @@ -349,6 +349,7 @@ import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString; import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLTextLiteral; import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangConstPattern; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangMatchPattern; import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangVarBindingPatternMatchPattern; import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangWildCardMatchPattern; import org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment; @@ -3763,57 +3764,15 @@ public BLangNode transform(MatchStatementNode matchStatementNode) { for (Node matchPattern : matchClauseNode.matchPatterns()) { DiagnosticPos matchPatternPos = getPosition(matchPattern); - if (matchPattern.kind() == SyntaxKind.SIMPLE_NAME_REFERENCE && - ((SimpleNameReferenceNode) matchPattern).name().text().equals("_")) { - // wildcard match - BLangWildCardMatchPattern bLangWildCardMatchPattern = - (BLangWildCardMatchPattern) TreeBuilder.createWildCardMatchPattern(); - bLangWildCardMatchPattern.matchExpr = matchStmtExpr; - bLangWildCardMatchPattern.pos = matchPatternPos; - - bLangMatchClause.addMatchPattern(bLangWildCardMatchPattern); - } else if (matchPattern.kind() == SyntaxKind.DECIMAL_INTEGER_LITERAL || - matchPattern.kind() == SyntaxKind.DECIMAL_FLOATING_POINT_LITERAL || - matchPattern.kind() == SyntaxKind.STRING_LITERAL || - matchPattern.kind() == SyntaxKind.SIMPLE_NAME_REFERENCE || - matchPattern.kind() == SyntaxKind.TRUE_KEYWORD || - matchPattern.kind() == SyntaxKind.FALSE_KEYWORD) { - BLangConstPattern bLangConstMatchPattern = - (BLangConstPattern) TreeBuilder.createConstMatchPattern(); - bLangConstMatchPattern.setExpression(createExpression(matchPattern)); - bLangConstMatchPattern.matchExpr = matchStmtExpr; - bLangConstMatchPattern.pos = matchPatternPos; - - bLangMatchClause.addMatchPattern(bLangConstMatchPattern); - } else if (matchPattern.kind() == SyntaxKind.TYPED_BINDING_PATTERN) { // var a - TypedBindingPatternNode typedBindingPatternNode = (TypedBindingPatternNode) matchPattern; - BLangVarBindingPatternMatchPattern bLangVarBindingPattern = - (BLangVarBindingPatternMatchPattern) TreeBuilder.createVarBindingPattern(); - bLangVarBindingPattern.matchExpr = matchStmtExpr; - bLangVarBindingPattern.pos = matchPatternPos; - bLangVarBindingPattern.matchGuardIsAvailable = matchGuardAvailable; - - SyntaxKind patternKind = typedBindingPatternNode.bindingPattern().kind(); - switch (patternKind) { - case CAPTURE_BINDING_PATTERN: - // TODO : check whether why cant we call the existing transform method - CaptureBindingPatternNode captureBindingPattern = - (CaptureBindingPatternNode) typedBindingPatternNode.bindingPattern(); - BLangCaptureBindingPattern bLangCaptureBindingPattern = - createCaptureBindingPattern(captureBindingPattern); - bLangVarBindingPattern.setBindingPattern(bLangCaptureBindingPattern); - break; - default: - // TODO : Remove this after all binding patterns are implemented - dlog.error(matchPatternPos, DiagnosticCode.MATCH_PATTERN_NOT_SUPPORTED); - } - bLangMatchClause.addMatchPattern(bLangVarBindingPattern); - } else { - // TODO : Remove this after all binding patterns are implemented - dlog.error(matchPatternPos, DiagnosticCode.MATCH_PATTERN_NOT_SUPPORTED); + BLangMatchPattern bLangMatchPattern = transformMatchPattern(matchPattern, matchPatternPos); + // TODO : Remove this check after all binding patterns are implemented + if (bLangMatchPattern != null) { + bLangMatchPattern.matchExpr = matchStmtExpr; + bLangMatchPattern.matchGuardIsAvailable = matchGuardAvailable; + bLangMatchClause.addMatchPattern(bLangMatchPattern); } } - + bLangMatchClause.setBlockStatement((BLangBlockStmt) transform(matchClauseNode.blockStatement())); matchStatement.addMatchClause(bLangMatchClause); } @@ -3824,7 +3783,54 @@ public BLangNode transform(MatchStatementNode matchStatementNode) { }); return matchStatement; + } + private BLangMatchPattern transformMatchPattern(Node matchPattern, DiagnosticPos matchPatternPos) { + + if (matchPattern.kind() == SyntaxKind.SIMPLE_NAME_REFERENCE && + ((SimpleNameReferenceNode) matchPattern).name().text().equals("_")) { + // wildcard match + BLangWildCardMatchPattern bLangWildCardMatchPattern = + (BLangWildCardMatchPattern) TreeBuilder.createWildCardMatchPattern(); + bLangWildCardMatchPattern.pos = matchPatternPos; + return bLangWildCardMatchPattern; + } else if (matchPattern.kind() == SyntaxKind.DECIMAL_INTEGER_LITERAL || + matchPattern.kind() == SyntaxKind.DECIMAL_FLOATING_POINT_LITERAL || + matchPattern.kind() == SyntaxKind.STRING_LITERAL || + matchPattern.kind() == SyntaxKind.SIMPLE_NAME_REFERENCE || + matchPattern.kind() == SyntaxKind.TRUE_KEYWORD || + matchPattern.kind() == SyntaxKind.FALSE_KEYWORD) { + BLangConstPattern bLangConstMatchPattern = + (BLangConstPattern) TreeBuilder.createConstMatchPattern(); + bLangConstMatchPattern.setExpression(createExpression(matchPattern)); + bLangConstMatchPattern.pos = matchPatternPos; + return bLangConstMatchPattern; + } else if (matchPattern.kind() == SyntaxKind.TYPED_BINDING_PATTERN) { // var a + TypedBindingPatternNode typedBindingPatternNode = (TypedBindingPatternNode) matchPattern; + BLangVarBindingPatternMatchPattern bLangVarBindingPattern = + (BLangVarBindingPatternMatchPattern) TreeBuilder.createVarBindingPattern(); + bLangVarBindingPattern.pos = matchPatternPos; + + SyntaxKind patternKind = typedBindingPatternNode.bindingPattern().kind(); + switch (patternKind) { + case CAPTURE_BINDING_PATTERN: + // TODO : check whether why cant we call the existing transform method + CaptureBindingPatternNode captureBindingPattern = + (CaptureBindingPatternNode) typedBindingPatternNode.bindingPattern(); + BLangCaptureBindingPattern bLangCaptureBindingPattern = + createCaptureBindingPattern(captureBindingPattern); + bLangVarBindingPattern.setBindingPattern(bLangCaptureBindingPattern); + break; + default: + // TODO : Remove this after all binding patterns are implemented + dlog.error(matchPatternPos, DiagnosticCode.MATCH_PATTERN_NOT_SUPPORTED); + } + return bLangVarBindingPattern; + } else { + // TODO : Remove this after all binding patterns are implemented + dlog.error(matchPatternPos, DiagnosticCode.MATCH_PATTERN_NOT_SUPPORTED); + return null; + } } private BLangCaptureBindingPattern createCaptureBindingPattern(CaptureBindingPatternNode From d805c3e13abfa286abd7756ff40dbf5314dc59eb Mon Sep 17 00:00:00 2001 From: kavindu Date: Wed, 5 Aug 2020 13:02:45 +0530 Subject: [PATCH 11/20] Change the Nodecloner --- .../java/org/wso2/ballerinalang/compiler/parser/NodeCloner.java | 1 + 1 file changed, 1 insertion(+) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/NodeCloner.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/NodeCloner.java index 85f223e785fa..48a44d605bb1 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/NodeCloner.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/NodeCloner.java @@ -726,6 +726,7 @@ public void visit(BLangVarBindingPatternMatchPattern source) { BLangVarBindingPatternMatchPattern clone = new BLangVarBindingPatternMatchPattern(); source.cloneRef = clone; clone.matchExpr = source.matchExpr; + clone.matchGuardIsAvailable = source.matchGuardIsAvailable; clone.setBindingPattern(source.getBindingPattern()); } From e9a2d0a0dfb3594af071ec4b3602967c19389d2a Mon Sep 17 00:00:00 2001 From: kavindu Date: Thu, 6 Aug 2020 09:35:45 +0530 Subject: [PATCH 12/20] Disable lang server tests --- .../ballerinalang/langserver/definition/DefinitionTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/language-server/modules/langserver-core/src/test/java/org/ballerinalang/langserver/definition/DefinitionTest.java b/language-server/modules/langserver-core/src/test/java/org/ballerinalang/langserver/definition/DefinitionTest.java index 7b7507f0428f..67fb608ecd6e 100644 --- a/language-server/modules/langserver-core/src/test/java/org/ballerinalang/langserver/definition/DefinitionTest.java +++ b/language-server/modules/langserver-core/src/test/java/org/ballerinalang/langserver/definition/DefinitionTest.java @@ -403,8 +403,8 @@ private Object[][] testDataProvider() throws IOException { // {"defMatchStmt6.json", "matchstmt"}, // {"defMatchStmt7.json", "matchstmt"}, // Covers the Match statement - Constant pattern - {"defMatchStmt8.json", "matchstmt"}, - {"defMatchStmt9.json", "matchstmt"}, +// {"defMatchStmt8.json", "matchstmt"}, +// {"defMatchStmt9.json", "matchstmt"}, // Covers the Match Statement - List Pattern // {"defMatchStmt10.json", "matchstmt"}, // Covers the Match Statement - Mapping Pattern From 8130601d3d24bdca5132f3a5f30bdb55f8cf8bd3 Mon Sep 17 00:00:00 2001 From: kavindu Date: Thu, 6 Aug 2020 16:58:19 +0530 Subject: [PATCH 13/20] Handle NPE in match expr --- .../compiler/semantics/analyzer/Types.java | 10 +++++++--- .../matchstmt/MatchStmtConstPatternTest.java | 1 + 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/Types.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/Types.java index 9c085d8c6fc6..dc1e1bd0dbe1 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/Types.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/Types.java @@ -290,9 +290,13 @@ public BType resolvePatternTypeFromMatchExpr(BType matchExprType, BLangConstPatt BType constMatchPatternExprType = constPatternExpr.type; if (constPatternExpr.getKind() == NodeKind.SIMPLE_VARIABLE_REF) { - BType varRefSymbolType = ((BLangSimpleVarRef) constPatternExpr).symbol.type; - if (isAssignable(varRefSymbolType, matchExprType)) { - return varRefSymbolType; + BLangSimpleVarRef constVarRef = (BLangSimpleVarRef) constPatternExpr; + if (constVarRef.symbol == null) { + return symTable.noType; + } + BType constVarRefSymbolType = constVarRef.symbol.type; + if (isAssignable(constVarRefSymbolType, matchExprType)) { + return constVarRefSymbolType; } return symTable.noType; } diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/MatchStmtConstPatternTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/MatchStmtConstPatternTest.java index 27d791134a14..13dfbb95d75c 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/MatchStmtConstPatternTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/MatchStmtConstPatternTest.java @@ -29,6 +29,7 @@ * * @since 2.0.0 */ +@Test(groups = { "disableOnOldParser" }) public class MatchStmtConstPatternTest { private CompileResult result, resultNegative; From f24bb7deb09296c94c305d866e1dc947e73302d1 Mon Sep 17 00:00:00 2001 From: kavindu Date: Fri, 7 Aug 2020 09:36:35 +0530 Subject: [PATCH 14/20] Add nill literla support --- .../compiler/parser/BLangNodeTransformer.java | 1 + .../matchstmt/MatchStmtConstPatternTest.java | 5 +++++ .../test-src/statements/matchstmt/const-pattern.bal | 12 ++++++++++++ 3 files changed, 18 insertions(+) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/BLangNodeTransformer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/BLangNodeTransformer.java index 9599386de8e4..17545f3057f3 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/BLangNodeTransformer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/BLangNodeTransformer.java @@ -3798,6 +3798,7 @@ private BLangMatchPattern transformMatchPattern(Node matchPattern, DiagnosticPos matchPattern.kind() == SyntaxKind.DECIMAL_FLOATING_POINT_LITERAL || matchPattern.kind() == SyntaxKind.STRING_LITERAL || matchPattern.kind() == SyntaxKind.SIMPLE_NAME_REFERENCE || + matchPattern.kind() == SyntaxKind.NULL_KEYWORD || matchPattern.kind() == SyntaxKind.TRUE_KEYWORD || matchPattern.kind() == SyntaxKind.FALSE_KEYWORD) { BLangConstPattern bLangConstMatchPattern = diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/MatchStmtConstPatternTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/MatchStmtConstPatternTest.java index 13dfbb95d75c..b3046a8efc8b 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/MatchStmtConstPatternTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/MatchStmtConstPatternTest.java @@ -75,6 +75,11 @@ public void testConstPattern7() { BRunUtil.invoke(result, "testConstPattern7"); } + @Test + public void testConstPattern8() { + BRunUtil.invoke(result, "testConstPattern8"); + } + @Test(description = "Test pattern will not be matched") public void testConstPatternNegative() { Assert.assertEquals(resultNegative.getErrorCount(), 26); diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/statements/matchstmt/const-pattern.bal b/tests/jballerina-unit-test/src/test/resources/test-src/statements/matchstmt/const-pattern.bal index 238d37a78915..2c8edca61fbe 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/statements/matchstmt/const-pattern.bal +++ b/tests/jballerina-unit-test/src/test/resources/test-src/statements/matchstmt/const-pattern.bal @@ -287,6 +287,18 @@ function testConstPattern7() { assertEquals("no-match", result); } +function testConstPattern8() { + json j = null; + string result = ""; + + match j { + 0 => {result = "0";} + null => {result = "null";} + } + + assertEquals("null", result); +} + function assertEquals(anydata expected, anydata actual) { if expected == actual { return; From b7ff4934ba022c79fdd963746742c8b4aee07201 Mon Sep 17 00:00:00 2001 From: kavindu Date: Mon, 10 Aug 2020 09:37:56 +0530 Subject: [PATCH 15/20] Fix failure tests --- .../org/ballerinalang/util/diagnostic/DiagnosticCode.java | 2 +- .../ballerinalang/compiler/desugar/ConstantPropagation.java | 5 +---- .../ballerinalang/compiler/parser/BLangNodeTransformer.java | 1 + .../compiler/semantics/analyzer/CodeAnalyzer.java | 2 +- 4 files changed, 4 insertions(+), 6 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/util/diagnostic/DiagnosticCode.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/util/diagnostic/DiagnosticCode.java index f27f9085bbbd..fd99b18e9ccc 100644 --- a/compiler/ballerina-lang/src/main/java/org/ballerinalang/util/diagnostic/DiagnosticCode.java +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/util/diagnostic/DiagnosticCode.java @@ -400,7 +400,7 @@ public enum DiagnosticCode { MATCH_STMT_UNREACHABLE_PATTERN("match.stmt.unreachable.pattern"), MATCH_STMT_UNMATCHED_PATTERN("match.stmt.unmatched.pattern"), MATCH_STMT_PATTERN_ALWAYS_MATCHES("match.stmt.pattern.always.matches"), - MATCH_STMT_UNREACHABLE_PATTERN_AVAILABLE("match.stmt.unreachable.pattern.available"), + MATCH_STMT_PATTERN_UNREACHABLE("match.stmt.unreachable.pattern.available"), MATCH_PATTERN_NOT_SUPPORTED("match.pattern.not.supported"), MATCH_STMT_CONTAINS_TWO_DEFAULT_PATTERNS("match.stmt.contains.two.default.patterns"), diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/ConstantPropagation.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/ConstantPropagation.java index 83d0aaef2b0d..f265657984f0 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/ConstantPropagation.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/ConstantPropagation.java @@ -115,7 +115,6 @@ import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLProcInsLiteral; import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLTextLiteral; import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangConstPattern; -import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangMatchPattern; import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangVarBindingPatternMatchPattern; import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangWildCardMatchPattern; import org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment; @@ -469,9 +468,7 @@ public void visit(BLangMatchStatement matchStatement) { @Override public void visit(BLangMatchClause matchClause) { - for (BLangMatchPattern matchPattern : matchClause.matchPatterns) { - rewrite(matchPattern); - } + rewrite(matchClause.matchPatterns); matchClause.blockStmt = rewrite(matchClause.blockStmt); result = matchClause; } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/BLangNodeTransformer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/BLangNodeTransformer.java index 17545f3057f3..952a38bdcedc 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/BLangNodeTransformer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/BLangNodeTransformer.java @@ -3799,6 +3799,7 @@ private BLangMatchPattern transformMatchPattern(Node matchPattern, DiagnosticPos matchPattern.kind() == SyntaxKind.STRING_LITERAL || matchPattern.kind() == SyntaxKind.SIMPLE_NAME_REFERENCE || matchPattern.kind() == SyntaxKind.NULL_KEYWORD || + matchPattern.kind() == SyntaxKind.NIL_LITERAL || matchPattern.kind() == SyntaxKind.TRUE_KEYWORD || matchPattern.kind() == SyntaxKind.FALSE_KEYWORD) { BLangConstPattern bLangConstMatchPattern = diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/CodeAnalyzer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/CodeAnalyzer.java index 74e69c6f3f18..54eb305099ba 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/CodeAnalyzer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/CodeAnalyzer.java @@ -808,7 +808,7 @@ public void visit(BLangMatchStatement matchStatement) { public void visit(BLangMatchClause matchClause) { for (BLangMatchPattern matchPattern : matchClause.matchPatterns) { if (hasLastPattern && matchClause.matchGuard == null) { - dlog.error(matchPattern.pos, DiagnosticCode.MATCH_STMT_UNREACHABLE_PATTERN_AVAILABLE); + dlog.error(matchPattern.pos, DiagnosticCode.MATCH_STMT_PATTERN_UNREACHABLE); } if (matchPattern.type == symTable.noType) { dlog.error(matchClause.pos, DiagnosticCode.MATCH_STMT_UNMATCHED_PATTERN); From 864c6921360f521a4b7fcc6d4318b07b26e1af6e Mon Sep 17 00:00:00 2001 From: kavindu Date: Mon, 17 Aug 2020 10:50:38 +0530 Subject: [PATCH 16/20] Test syntax errors --- .../semantics/analyzer/CodeAnalyzer.java | 2 +- .../MatchStatementSyntaxErrorsTest.java | 56 +++++++++++++++++++ .../matchstmt/match-stmt-syntax-errors.bal | 17 ++++++ .../src/test/resources/testng.xml | 1 + 4 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/MatchStatementSyntaxErrorsTest.java create mode 100644 tests/jballerina-unit-test/src/test/resources/test-src/statements/matchstmt/match-stmt-syntax-errors.bal diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/CodeAnalyzer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/CodeAnalyzer.java index 54eb305099ba..d28139b06217 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/CodeAnalyzer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/CodeAnalyzer.java @@ -795,7 +795,7 @@ public void visit(BLangMatchStatement matchStatement) { analyzeExpr(matchStatement.expr); matchExprType = matchStatement.expr.type; - boolean matchStmtReturns = true; + boolean matchStmtReturns = false; for (BLangMatchClause matchClause : matchStatement.matchClauses) { analyzeNode(matchClause, env); matchStmtReturns = this.matchClauseReturns && matchClause.matchGuard == null && hasLastPattern; diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/MatchStatementSyntaxErrorsTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/MatchStatementSyntaxErrorsTest.java new file mode 100644 index 000000000000..602371a92644 --- /dev/null +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/MatchStatementSyntaxErrorsTest.java @@ -0,0 +1,56 @@ +// Copyright (c) 2020 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +// +// WSO2 Inc. licenses this file to you under the Apache License, +// Version 2.0 (the "License"); you may not use this file except +// in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.ballerinalang.test.statements.matchstmt; + +import org.ballerinalang.test.util.BAssertUtil; +import org.ballerinalang.test.util.BCompileUtil; +import org.ballerinalang.test.util.CompileResult; +import org.testng.Assert; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +/** + * Test cases to verify the behaviour of the const-pattern. + * + * @since 2.0.0 + */ +@Test(groups = { "disableOnOldParser" }) +public class MatchStatementSyntaxErrorsTest { + + private CompileResult result; + + @BeforeClass + public void setup() { + result = BCompileUtil.compile("test-src/statements/matchstmt/match-stmt-syntax-errors.bal"); + } + + @Test + public void testSyntaxErrors() { + Assert.assertEquals(result.getErrorCount(), 9); + + int i = -1; + BAssertUtil.validateError(result, ++i, "undefined symbol 'v'", 5, 9); + BAssertUtil.validateError(result, ++i, "missing open brace token", 6, 1); + BAssertUtil.validateError(result, ++i, "missing right double arrow token", 6, 1); + BAssertUtil.validateError(result, ++i, "missing close brace token", 8, 1); + BAssertUtil.validateError(result, ++i, "missing open brace token", 12, 1); + BAssertUtil.validateError(result, ++i, "missing close brace token", 13, 1); + BAssertUtil.validateError(result, ++i, "missing identifier", 17, 1); + BAssertUtil.validateError(result, ++i, "missing open brace token", 17, 1); + BAssertUtil.validateError(result, ++i, "missing close brace token", 18, 1); + } +} diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/statements/matchstmt/match-stmt-syntax-errors.bal b/tests/jballerina-unit-test/src/test/resources/test-src/statements/matchstmt/match-stmt-syntax-errors.bal new file mode 100644 index 000000000000..44753d795c51 --- /dev/null +++ b/tests/jballerina-unit-test/src/test/resources/test-src/statements/matchstmt/match-stmt-syntax-errors.bal @@ -0,0 +1,17 @@ +// syntax errors +function func1() { + string var1 = ""; + match var1 { + v + } +} + +function func2() { + string v = ""; + match v +} + +function func3() { + string v = ""; + match +} diff --git a/tests/jballerina-unit-test/src/test/resources/testng.xml b/tests/jballerina-unit-test/src/test/resources/testng.xml index 5f36d64dd681..72cbea97379f 100644 --- a/tests/jballerina-unit-test/src/test/resources/testng.xml +++ b/tests/jballerina-unit-test/src/test/resources/testng.xml @@ -195,6 +195,7 @@ + From 1fcada5f7d7de3f10b7a0f25363bbf9c3f93e374 Mon Sep 17 00:00:00 2001 From: kavindu Date: Wed, 2 Sep 2020 08:51:48 +0530 Subject: [PATCH 17/20] Fix suggestions --- .../model/clauses/MatchClauseNode.java | 4 +-- .../bindingpattern/BindingPatternNode.java | 2 +- .../bindingpattern/CaptureBindingPattern.java | 2 +- .../bindingpattern/ListBindingPattern.java | 2 +- .../model/tree/expressions/MatchGuard.java | 2 +- .../tree/matchpatterns/ConstPatternNode.java | 2 +- .../tree/matchpatterns/MatchPatternNode.java | 2 +- .../VarBindingPatternMatchPatternNode.java | 2 +- .../WildCardMatchPatternNode.java | 2 +- .../tree/statements/MatchStatementNode.java | 2 +- .../compiler/parser/BLangNodeTransformer.java | 8 ++--- .../semantics/analyzer/CodeAnalyzer.java | 10 +++--- .../semantics/analyzer/DataflowAnalyzer.java | 31 ------------------- .../semantics/analyzer/SemanticAnalyzer.java | 4 ++- .../bindingpatterns/BLangBindingPattern.java | 2 +- .../BLangCaptureBindingPattern.java | 2 +- .../BLangListBindingPattern.java | 2 +- .../tree/clauses/BLangMatchClause.java | 6 ++-- .../tree/expressions/BLangMatchGuard.java | 2 +- .../tree/matchpatterns/BLangConstPattern.java | 2 +- .../tree/matchpatterns/BLangMatchPattern.java | 2 +- .../BLangVarBindingPatternMatchPattern.java | 2 +- .../BLangWildCardMatchPattern.java | 2 +- .../tree/statements/BLangMatchStatement.java | 2 +- .../MatchStatementSyntaxErrorsTest.java | 2 +- .../matchstmt/MatchStmtConstPatternTest.java | 2 +- .../CaptureBindingPatternTest.java | 2 +- .../statements/matchstmt/const-pattern.bal | 22 ++++++------- 28 files changed, 48 insertions(+), 79 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/clauses/MatchClauseNode.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/clauses/MatchClauseNode.java index f3504851c695..96d88d9e8609 100644 --- a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/clauses/MatchClauseNode.java +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/clauses/MatchClauseNode.java @@ -28,7 +28,7 @@ /** * The interface with the APIs to implement the match-clause. * - * @since 2.0.0 + * @since Swan Lake */ public interface MatchClauseNode extends Node { @@ -46,7 +46,7 @@ public interface MatchClauseNode extends Node { boolean isLastClause(); - void setLastClause(boolean isLastClause); + void setLastClause(); ExpressionNode getExpression(); diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/BindingPatternNode.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/BindingPatternNode.java index 0549b0ea03b8..e7e6da0c3907 100644 --- a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/BindingPatternNode.java +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/BindingPatternNode.java @@ -22,7 +22,7 @@ /** * The interface with the APIs to implement the binding-pattern. * - * @since 2.0.0 + * @since Swan Lake */ public interface BindingPatternNode extends Node { diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/CaptureBindingPattern.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/CaptureBindingPattern.java index 48eb0bf444fc..874cf215c962 100644 --- a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/CaptureBindingPattern.java +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/CaptureBindingPattern.java @@ -24,7 +24,7 @@ /** * The interface with the APIs to implement the capture-binding-pattern. * - * @since 2.0.0 + * @since Swan Lake */ public interface CaptureBindingPattern extends Node { diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/ListBindingPattern.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/ListBindingPattern.java index 1ecbc5b30ada..b60ac5f3e9e8 100644 --- a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/ListBindingPattern.java +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/bindingpattern/ListBindingPattern.java @@ -24,7 +24,7 @@ /** * The interface with the APIs to implement the list-binding-pattern. * - * @since 2.0.0 + * @since Swan Lake */ public interface ListBindingPattern extends Node { List getBindingPatterns(); diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/expressions/MatchGuard.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/expressions/MatchGuard.java index 81cf3a6c602b..3e89fc1a6997 100644 --- a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/expressions/MatchGuard.java +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/expressions/MatchGuard.java @@ -20,7 +20,7 @@ /** * The interface with the APIs to implement the match-guard. * - * @since 2.0.0 + * @since Swan Lake */ public interface MatchGuard { ExpressionNode getExpression(); diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/ConstPatternNode.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/ConstPatternNode.java index 0d882bb83fce..cc3e20bd0d05 100644 --- a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/ConstPatternNode.java +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/ConstPatternNode.java @@ -23,7 +23,7 @@ /** * The interface with the APIs to implement the const-pattern. * - * @since 2.0.0 + * @since Swan Lake */ public interface ConstPatternNode extends Node { diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/MatchPatternNode.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/MatchPatternNode.java index f3e396b31c78..16c889d11917 100644 --- a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/MatchPatternNode.java +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/MatchPatternNode.java @@ -22,7 +22,7 @@ /** * The interface with the APIs to implement the match-pattern. * - * @since 2.0.0 + * @since Swan Lake */ public interface MatchPatternNode extends Node { diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/VarBindingPatternMatchPatternNode.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/VarBindingPatternMatchPatternNode.java index 4f22979a5e09..97d49de13846 100644 --- a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/VarBindingPatternMatchPatternNode.java +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/VarBindingPatternMatchPatternNode.java @@ -23,7 +23,7 @@ /** * The interface with the APIs to implement the var-binding-pattern match-pattern. * - * @since 2.0.0 + * @since Swan Lake */ public interface VarBindingPatternMatchPatternNode extends Node { diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/WildCardMatchPatternNode.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/WildCardMatchPatternNode.java index abd75e330669..c13ef1fd71cb 100644 --- a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/WildCardMatchPatternNode.java +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/matchpatterns/WildCardMatchPatternNode.java @@ -22,7 +22,7 @@ /** * The interface with the APIs to implement the wild-card-binding-pattern. * - * @since 2.0.0 + * @since Swan Lake */ public interface WildCardMatchPatternNode extends Node { } diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/statements/MatchStatementNode.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/statements/MatchStatementNode.java index b53b56ba5b98..d6717bcb6f97 100644 --- a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/statements/MatchStatementNode.java +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/statements/MatchStatementNode.java @@ -25,7 +25,7 @@ /** * The interface with the APIs to implement the match-stmt. * - * @since 2.0.0 + * @since Swan Lake */ public interface MatchStatementNode extends StatementNode { ExpressionNode getExpression(); diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/BLangNodeTransformer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/BLangNodeTransformer.java index 952a38bdcedc..caabac1d251e 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/BLangNodeTransformer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/BLangNodeTransformer.java @@ -3794,14 +3794,12 @@ private BLangMatchPattern transformMatchPattern(Node matchPattern, DiagnosticPos (BLangWildCardMatchPattern) TreeBuilder.createWildCardMatchPattern(); bLangWildCardMatchPattern.pos = matchPatternPos; return bLangWildCardMatchPattern; - } else if (matchPattern.kind() == SyntaxKind.DECIMAL_INTEGER_LITERAL || - matchPattern.kind() == SyntaxKind.DECIMAL_FLOATING_POINT_LITERAL || + } else if (matchPattern.kind() == SyntaxKind.NUMERIC_LITERAL || matchPattern.kind() == SyntaxKind.STRING_LITERAL || matchPattern.kind() == SyntaxKind.SIMPLE_NAME_REFERENCE || - matchPattern.kind() == SyntaxKind.NULL_KEYWORD || + matchPattern.kind() == SyntaxKind.NULL_LITERAL || matchPattern.kind() == SyntaxKind.NIL_LITERAL || - matchPattern.kind() == SyntaxKind.TRUE_KEYWORD || - matchPattern.kind() == SyntaxKind.FALSE_KEYWORD) { + matchPattern.kind() == SyntaxKind.BOOLEAN_LITERAL) { BLangConstPattern bLangConstMatchPattern = (BLangConstPattern) TreeBuilder.createConstMatchPattern(); bLangConstMatchPattern.setExpression(createExpression(matchPattern)); diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/CodeAnalyzer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/CodeAnalyzer.java index d28139b06217..79eeac4bfcd4 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/CodeAnalyzer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/CodeAnalyzer.java @@ -985,10 +985,12 @@ private boolean analyzeStaticMatchPatterns(BLangMatch matchStmt) { for (BLangMatchStaticBindingPatternClause patternClause : matchStmt.getStaticPatternClauses()) { analyzeNode(patternClause, env); - List matchedExpTypes = matchStmt.exprTypes - .stream() - .filter(exprType -> isValidStaticMatchPattern(exprType, patternClause.literal)) - .collect(Collectors.toList()); + List matchedExpTypes = new ArrayList<>(); + for (BType exprType : matchStmt.exprTypes) { + if (isValidStaticMatchPattern(exprType, patternClause.literal)) { + matchedExpTypes.add(exprType); + } + } if (matchedExpTypes.isEmpty()) { // log error if a pattern will not match to any of the expected types diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/DataflowAnalyzer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/DataflowAnalyzer.java index f5a7aa0c8cf5..8baed453a921 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/DataflowAnalyzer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/DataflowAnalyzer.java @@ -65,7 +65,6 @@ import org.wso2.ballerinalang.compiler.tree.clauses.BLangJoinClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangLetClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangLimitClause; -import org.wso2.ballerinalang.compiler.tree.clauses.BLangMatchClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangOnClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangOnConflictClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangOnFailClause; @@ -570,32 +569,7 @@ public void visit(BLangIf ifNode) { @Override public void visit(BLangMatchStatement matchStatement) { - analyzeNode(matchStatement.expr, env); - Map unInitVars = new HashMap<>(); - BranchResult lastClauseResult = null; - for (BLangMatchClause matchClause : matchStatement.matchClauses) { - if (matchClause.isLastClause) { - lastClauseResult = analyzeBranch(matchClause, env); - } else { - BranchResult result = analyzeBranch(matchClause, env); - // If the flow was terminated within the block, then that branch should not be considered for - // analyzing the data-flow for the downstream code. - if (result.flowTerminated) { - continue; - } - unInitVars = mergeUninitializedVars(unInitVars, result.uninitializedVars); - } - } - - if (lastClauseResult != null) { - // only if last pattern is present, uninitializedVars should be updated - unInitVars = mergeUninitializedVars(unInitVars, lastClauseResult.uninitializedVars); - this.uninitializedVars = unInitVars; - return; - } - unInitVars = mergeUninitializedVars(new HashMap<>(), this.uninitializedVars); - this.uninitializedVars = unInitVars; } @Override @@ -1651,11 +1625,6 @@ public void visit(BLangMatchStructuredBindingPatternClause bLangMatchStructuredB analyzeNode(bLangMatchStructuredBindingPatternClause.body, env); } - @Override - public void visit(BLangMatchClause matchClause) { - analyzeNode(matchClause.blockStmt, env); - } - private void addUninitializedVar(BLangVariable variable) { if (!this.uninitializedVars.containsKey(variable.symbol)) { this.uninitializedVars.put(variable.symbol, InitStatus.UN_INIT); diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java index 2b90accac979..cd0bcd0a5c83 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java @@ -2304,7 +2304,9 @@ public void visit(BLangMatch matchNode) { @Override public void visit(BLangMatchStatement matchStatement) { typeChecker.checkExpr(matchStatement.expr, env, symTable.noType); - matchStatement.matchClauses.forEach(matchClause -> matchClause.accept(this)); + for (BLangMatchClause matchClause : matchStatement.matchClauses) { + analyzeNode(matchClause, env); + } } @Override diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangBindingPattern.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangBindingPattern.java index 36c97f748458..ba821d466367 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangBindingPattern.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangBindingPattern.java @@ -21,7 +21,7 @@ import org.wso2.ballerinalang.compiler.tree.BLangNode; /** - * @since 2.0.0 + * @since Swan Lake */ public abstract class BLangBindingPattern extends BLangNode implements BindingPatternNode { diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangCaptureBindingPattern.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangCaptureBindingPattern.java index cd68e5b544bf..06a22133d8ee 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangCaptureBindingPattern.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangCaptureBindingPattern.java @@ -26,7 +26,7 @@ import org.wso2.ballerinalang.compiler.tree.BLangNodeVisitor; /** - * @since 2.0.0 + * @since Swan Lake */ public class BLangCaptureBindingPattern extends BLangBindingPattern implements CaptureBindingPattern { diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangListBindingPattern.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangListBindingPattern.java index 190dce2df0c1..b8cf70170367 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangListBindingPattern.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/bindingpatterns/BLangListBindingPattern.java @@ -26,7 +26,7 @@ import java.util.List; /** - * @since 2.0.0 + * @since Swan Lake */ public class BLangListBindingPattern extends BLangBindingPattern implements ListBindingPattern { public List bindingPatterns = new ArrayList<>(); diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/clauses/BLangMatchClause.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/clauses/BLangMatchClause.java index 9850445963ed..bfc7ec4f427d 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/clauses/BLangMatchClause.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/clauses/BLangMatchClause.java @@ -34,7 +34,7 @@ import java.util.List; /** - * @since 2.0.0 + * @since Swan Lake */ public class BLangMatchClause extends BLangNode implements MatchClauseNode { @@ -90,8 +90,8 @@ public boolean isLastClause() { } @Override - public void setLastClause(boolean isLastClause) { - this.isLastClause = isLastClause; + public void setLastClause() { + this.isLastClause = true; } @Override diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/expressions/BLangMatchGuard.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/expressions/BLangMatchGuard.java index c9ec29140235..26d3e3e8f351 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/expressions/BLangMatchGuard.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/expressions/BLangMatchGuard.java @@ -23,7 +23,7 @@ import org.wso2.ballerinalang.compiler.tree.BLangNodeVisitor; /** - * @since 2.0.0 + * @since Swan Lake */ public class BLangMatchGuard extends BLangExpression implements MatchGuard { diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangConstPattern.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangConstPattern.java index aecffe23850f..353d221dc3ab 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangConstPattern.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangConstPattern.java @@ -24,7 +24,7 @@ import org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression; /** - * @since 2.0.0 + * @since Swan Lake */ public class BLangConstPattern extends BLangMatchPattern implements ConstPatternNode { diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangMatchPattern.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangMatchPattern.java index 882549b4c532..290184546e4f 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangMatchPattern.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangMatchPattern.java @@ -22,7 +22,7 @@ import org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression; /** - * @since 2.0.0 + * @since Swan Lake */ public abstract class BLangMatchPattern extends BLangNode implements MatchPatternNode { public BLangExpression matchExpr; // TODO : should changed as action or expr diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangVarBindingPatternMatchPattern.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangVarBindingPatternMatchPattern.java index 04b98fa14a33..4be9a787678b 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangVarBindingPatternMatchPattern.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangVarBindingPatternMatchPattern.java @@ -24,7 +24,7 @@ import org.wso2.ballerinalang.compiler.tree.bindingpatterns.BLangBindingPattern; /** - * @since 2.0.0 + * @since Swan Lake */ public class BLangVarBindingPatternMatchPattern extends BLangMatchPattern implements VarBindingPatternMatchPatternNode { diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangWildCardMatchPattern.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangWildCardMatchPattern.java index e2047c96cab1..9b412eba7ce0 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangWildCardMatchPattern.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/matchpatterns/BLangWildCardMatchPattern.java @@ -22,7 +22,7 @@ import org.wso2.ballerinalang.compiler.tree.BLangNodeVisitor; /** - * @since 2.0.0 + * @since Swan Lake */ public class BLangWildCardMatchPattern extends BLangMatchPattern implements WildCardMatchPatternNode { diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/statements/BLangMatchStatement.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/statements/BLangMatchStatement.java index 7ae77e868f1a..aab0a423ba0b 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/statements/BLangMatchStatement.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/statements/BLangMatchStatement.java @@ -29,7 +29,7 @@ import java.util.List; /** - * @since 2.0.0 + * @since Swan Lake */ public class BLangMatchStatement extends BLangStatement implements MatchStatementNode { public BLangExpression expr; // TODO : replace with new node `action|expression` diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/MatchStatementSyntaxErrorsTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/MatchStatementSyntaxErrorsTest.java index 602371a92644..cb71a85ca0fa 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/MatchStatementSyntaxErrorsTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/MatchStatementSyntaxErrorsTest.java @@ -26,7 +26,7 @@ /** * Test cases to verify the behaviour of the const-pattern. * - * @since 2.0.0 + * @since Swan Lake */ @Test(groups = { "disableOnOldParser" }) public class MatchStatementSyntaxErrorsTest { diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/MatchStmtConstPatternTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/MatchStmtConstPatternTest.java index b3046a8efc8b..26a59ed14623 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/MatchStmtConstPatternTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/MatchStmtConstPatternTest.java @@ -27,7 +27,7 @@ /** * Test cases to verify the behaviour of the const-pattern. * - * @since 2.0.0 + * @since Swan Lake */ @Test(groups = { "disableOnOldParser" }) public class MatchStmtConstPatternTest { diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/varbindingpatternmatchpattern/CaptureBindingPatternTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/varbindingpatternmatchpattern/CaptureBindingPatternTest.java index d719c2133463..c69e6b97e4b3 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/varbindingpatternmatchpattern/CaptureBindingPatternTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/matchstmt/varbindingpatternmatchpattern/CaptureBindingPatternTest.java @@ -28,7 +28,7 @@ /** * Test cases to verify the behaviour of the var binding-pattern capture-pattern. * - * @since 2.0.0 + * @since Swan Lake */ @Test(groups = { "disableOnOldParser" }) public class CaptureBindingPatternTest { diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/statements/matchstmt/const-pattern.bal b/tests/jballerina-unit-test/src/test/resources/test-src/statements/matchstmt/const-pattern.bal index 2c8edca61fbe..3cfe9c216535 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/statements/matchstmt/const-pattern.bal +++ b/tests/jballerina-unit-test/src/test/resources/test-src/statements/matchstmt/const-pattern.bal @@ -14,9 +14,7 @@ // specific language governing permissions and limitations // under the License. -import ballerina/io; - -function constPattern1(string | int | boolean a) returns string { +function constPattern1(string|int|boolean a) returns string { match a { 12 => { return "12"; @@ -58,7 +56,7 @@ function testConstPattern1() { assertEquals("Default", constPattern1(a7)); } -function constPattern2(string | int | boolean a, string | int | boolean b) returns string { +function constPattern2(string|int|boolean a, string|int|boolean b) returns string { match a { 12 => { return "Value is '12'"; @@ -121,26 +119,26 @@ function testConstPattern2() { assertEquals("Value is 'true'", constPattern2(a11, a12)); } -function constPattern3(string | int | boolean a) returns string { +function constPattern3(string|int|boolean a) returns string { match a { 12 | 13 | 14 => { - return "Value is : " + io:sprintf("%s", a); + return "Value is : " + a.toString(); } "Hello" | "World" => { - return "Value is : " + io:sprintf("%s", a); + return "Value is : " + a.toString(); } 15 | "Test" => { - return "Value is : " + io:sprintf("%s", a); + return "Value is : " + a.toString(); } true | false => { - return "Value is : " + io:sprintf("%s", a); + return "Value is : " + a.toString(); } "HelloAgain" => { - return "Value is : " + io:sprintf("%s", a); + return "Value is : " + a.toString(); } } - return "Default value is : " + io:sprintf("%s", a); + return "Default value is : " + a.toString(); } function testConstPattern3(){ @@ -232,7 +230,7 @@ const CONST_2 = "B"; const CONST_3 = 10; const CONST_4 = true; -function constPattern6(CONST_1 | CONST_2 | CONST_3 | CONST_4 a) returns string { +function constPattern6(CONST_1|CONST_2|CONST_3|CONST_4 a) returns string { string results = ""; match a { From a3bfce4619761bc0f9e2bc7b9f5357d15e792cd4 Mon Sep 17 00:00:00 2001 From: kavindu Date: Thu, 10 Sep 2020 19:56:34 +0530 Subject: [PATCH 18/20] Fix build failure --- .../src/main/java/org/ballerinalang/model/TreeBuilder.java | 1 - .../compiler/desugar/ConstantPropagation.java | 1 - .../org/wso2/ballerinalang/compiler/desugar/Desugar.java | 7 ++----- .../org/wso2/ballerinalang/compiler/parser/NodeCloner.java | 1 - .../compiler/semantics/analyzer/SemanticAnalyzer.java | 2 +- 5 files changed, 3 insertions(+), 9 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/TreeBuilder.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/TreeBuilder.java index 921568a609a5..ddfc41e5c693 100644 --- a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/TreeBuilder.java +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/TreeBuilder.java @@ -165,7 +165,6 @@ import org.wso2.ballerinalang.compiler.tree.BLangBlockFunctionBody; import org.wso2.ballerinalang.compiler.tree.BLangClassDefinition; import org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit; -import org.wso2.ballerinalang.compiler.tree.BLangEndpoint; import org.wso2.ballerinalang.compiler.tree.BLangErrorVariable; import org.wso2.ballerinalang.compiler.tree.BLangExprFunctionBody; import org.wso2.ballerinalang.compiler.tree.BLangExternalFunctionBody; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/ConstantPropagation.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/ConstantPropagation.java index f265657984f0..585ae243fec4 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/ConstantPropagation.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/ConstantPropagation.java @@ -32,7 +32,6 @@ import org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment; import org.wso2.ballerinalang.compiler.tree.BLangBlockFunctionBody; import org.wso2.ballerinalang.compiler.tree.BLangClassDefinition; -import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangConstPattern; import org.wso2.ballerinalang.compiler.tree.BLangExprFunctionBody; import org.wso2.ballerinalang.compiler.tree.BLangExternalFunctionBody; import org.wso2.ballerinalang.compiler.tree.BLangFunction; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java index 3bbddad3c7a0..d45bc913d7da 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java @@ -102,8 +102,6 @@ import org.wso2.ballerinalang.compiler.tree.BLangXMLNS; import org.wso2.ballerinalang.compiler.tree.BLangXMLNS.BLangLocalXMLNS; import org.wso2.ballerinalang.compiler.tree.BLangXMLNS.BLangPackageXMLNS; -import org.wso2.ballerinalang.compiler.tree.bindingpatterns.BLangBindingPattern; -import org.wso2.ballerinalang.compiler.tree.bindingpatterns.BLangCaptureBindingPattern; import org.wso2.ballerinalang.compiler.tree.clauses.BLangMatchClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangOnFailClause; import org.wso2.ballerinalang.compiler.tree.expressions.BLangAccessExpression; @@ -193,7 +191,6 @@ import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName; import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString; import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLTextLiteral; -import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangConstPattern; import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangMatchPattern; import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangVarBindingPatternMatchPattern; import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangWildCardMatchPattern; @@ -2935,8 +2932,8 @@ public void visit(BLangMatchStatement matchStatement) { BLangExpression matchExpr = matchStatement.expr; BLangSimpleVariable matchExprVar = ASTBuilderUtil.createVariable(matchExpr.pos, matchExprVarName, matchExpr.type, matchExpr, new BVarSymbol(0, - names.fromString(matchExprVarName), - this.env.scope.owner.pkgID, matchExpr.type, this.env.scope.owner)); + names.fromString(matchExprVarName), this.env.scope.owner.pkgID, matchExpr.type, + this.env.scope.owner, matchExpr.pos, VIRTUAL)); BLangSimpleVariableDef matchExprVarDef = ASTBuilderUtil.createVariableDef(matchBlockStmt.pos, matchExprVar); matchBlockStmt.stmts.add(matchExprVarDef); diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/NodeCloner.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/NodeCloner.java index 48a44d605bb1..5d53a22eaf2b 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/NodeCloner.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/NodeCloner.java @@ -30,7 +30,6 @@ import org.wso2.ballerinalang.compiler.tree.BLangBlockFunctionBody; import org.wso2.ballerinalang.compiler.tree.BLangClassDefinition; import org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit; -import org.wso2.ballerinalang.compiler.tree.BLangEndpoint; import org.wso2.ballerinalang.compiler.tree.BLangErrorVariable; import org.wso2.ballerinalang.compiler.tree.BLangErrorVariable.BLangErrorDetailEntry; import org.wso2.ballerinalang.compiler.tree.BLangExprFunctionBody; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java index cd0bcd0a5c83..d1493b3f94f6 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java @@ -2362,7 +2362,7 @@ public void visit(BLangMatchStaticBindingPatternClause patternClause) { @Override public void visit(BLangCaptureBindingPattern captureBindingPattern) { captureBindingPattern.symbol = new BVarSymbol(0, new Name(captureBindingPattern.getIdentifier().getValue()), - env.enclPkg.packageID, symTable.anyType, env.scope.owner); + env.enclPkg.packageID, symTable.anyType, env.scope.owner, captureBindingPattern.pos, SOURCE); symbolEnter.defineSymbol(captureBindingPattern.pos, captureBindingPattern.symbol, env); } From d42b7d6233713dacecc3dedee9d3eb0dce278591 Mon Sep 17 00:00:00 2001 From: kavindu Date: Sat, 12 Sep 2020 09:00:31 +0530 Subject: [PATCH 19/20] Fix import errors --- .../compiler/desugar/Desugar.java | 6 +- .../semantics/analyzer/SemanticAnalyzer.java | 2 - .../src/test/resources/testng-new-parser.xml | 231 ++++++++++++++++++ 3 files changed, 234 insertions(+), 5 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java index d45bc913d7da..dd22b9c9b5fa 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java @@ -75,9 +75,6 @@ import org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment; import org.wso2.ballerinalang.compiler.tree.BLangBlockFunctionBody; import org.wso2.ballerinalang.compiler.tree.BLangClassDefinition; -import org.wso2.ballerinalang.compiler.tree.bindingpatterns.BLangBindingPattern; -import org.wso2.ballerinalang.compiler.tree.bindingpatterns.BLangCaptureBindingPattern; -import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangConstPattern; import org.wso2.ballerinalang.compiler.tree.BLangErrorVariable; import org.wso2.ballerinalang.compiler.tree.BLangExprFunctionBody; import org.wso2.ballerinalang.compiler.tree.BLangExternalFunctionBody; @@ -102,6 +99,8 @@ import org.wso2.ballerinalang.compiler.tree.BLangXMLNS; import org.wso2.ballerinalang.compiler.tree.BLangXMLNS.BLangLocalXMLNS; import org.wso2.ballerinalang.compiler.tree.BLangXMLNS.BLangPackageXMLNS; +import org.wso2.ballerinalang.compiler.tree.bindingpatterns.BLangBindingPattern; +import org.wso2.ballerinalang.compiler.tree.bindingpatterns.BLangCaptureBindingPattern; import org.wso2.ballerinalang.compiler.tree.clauses.BLangMatchClause; import org.wso2.ballerinalang.compiler.tree.clauses.BLangOnFailClause; import org.wso2.ballerinalang.compiler.tree.expressions.BLangAccessExpression; @@ -191,6 +190,7 @@ import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName; import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString; import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLTextLiteral; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangConstPattern; import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangMatchPattern; import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangVarBindingPatternMatchPattern; import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangWildCardMatchPattern; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java index d1493b3f94f6..cd526d925a66 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java @@ -60,8 +60,6 @@ import org.wso2.ballerinalang.compiler.tree.BLangAnnotation; import org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment; import org.wso2.ballerinalang.compiler.tree.BLangBlockFunctionBody; -import org.wso2.ballerinalang.compiler.tree.bindingpatterns.BLangCaptureBindingPattern; -import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangConstPattern; import org.wso2.ballerinalang.compiler.tree.BLangClassDefinition; import org.wso2.ballerinalang.compiler.tree.BLangErrorVariable; import org.wso2.ballerinalang.compiler.tree.BLangExprFunctionBody; diff --git a/tests/jballerina-unit-test/src/test/resources/testng-new-parser.xml b/tests/jballerina-unit-test/src/test/resources/testng-new-parser.xml index e69de29bb2d1..19a84d04070b 100644 --- a/tests/jballerina-unit-test/src/test/resources/testng-new-parser.xml +++ b/tests/jballerina-unit-test/src/test/resources/testng-new-parser.xml @@ -0,0 +1,231 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From d58689d16b99307dd2d7790310f34af41ff9c52d Mon Sep 17 00:00:00 2001 From: kavindu Date: Tue, 15 Sep 2020 10:46:31 +0530 Subject: [PATCH 20/20] Rebase with master --- .../java/org/ballerinalang/model/TreeBuilder.java | 1 - .../model/tree/statements/MatchStatementNode.java | 5 +++++ .../compiler/desugar/ConstantPropagation.java | 1 + .../ballerinalang/compiler/parser/NodeCloner.java | 1 + .../compiler/semantics/analyzer/CodeAnalyzer.java | 8 ++++---- .../semantics/analyzer/DataflowAnalyzer.java | 4 +++- .../semantics/analyzer/SemanticAnalyzer.java | 4 ++++ .../compiler/semantics/analyzer/TaintAnalyzer.java | 4 ++++ .../tree/statements/BLangMatchStatement.java | 13 +++++++++++++ 9 files changed, 35 insertions(+), 6 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/TreeBuilder.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/TreeBuilder.java index ddfc41e5c693..59b875b52378 100644 --- a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/TreeBuilder.java +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/TreeBuilder.java @@ -62,7 +62,6 @@ import org.ballerinalang.model.tree.expressions.ElvisExpressionNode; import org.ballerinalang.model.tree.expressions.ErrorVariableReferenceNode; import org.ballerinalang.model.tree.expressions.ExpressionNode; -import org.ballerinalang.model.tree.expressions.FailExpressionNode; import org.ballerinalang.model.tree.expressions.FieldBasedAccessNode; import org.ballerinalang.model.tree.expressions.GroupExpressionNode; import org.ballerinalang.model.tree.expressions.IndexBasedAccessNode; diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/statements/MatchStatementNode.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/statements/MatchStatementNode.java index d6717bcb6f97..5224958ef4df 100644 --- a/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/statements/MatchStatementNode.java +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/model/tree/statements/MatchStatementNode.java @@ -18,6 +18,7 @@ package org.ballerinalang.model.tree.statements; import org.ballerinalang.model.clauses.MatchClauseNode; +import org.ballerinalang.model.clauses.OnFailClauseNode; import org.ballerinalang.model.tree.expressions.ExpressionNode; import java.util.List; @@ -35,4 +36,8 @@ public interface MatchStatementNode extends StatementNode { List getMatchClauses(); void addMatchClause(MatchClauseNode matchClauseNode); + + OnFailClauseNode getOnFailClause(); + + void setOnFailClause(OnFailClauseNode onFailClause); } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/ConstantPropagation.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/ConstantPropagation.java index 585ae243fec4..1701b9185142 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/ConstantPropagation.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/ConstantPropagation.java @@ -462,6 +462,7 @@ public void visit(BLangCompoundAssignment compoundAssignNode) { public void visit(BLangMatchStatement matchStatement) { matchStatement.expr = rewrite(matchStatement.expr); rewrite(matchStatement.matchClauses); + matchStatement.onFailClause = rewrite(matchStatement.onFailClause); result = matchStatement; } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/NodeCloner.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/NodeCloner.java index 5d53a22eaf2b..cd87e54deb05 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/NodeCloner.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/NodeCloner.java @@ -686,6 +686,7 @@ public void visit(BLangMatchStatement source) { source.cloneRef = clone; clone.setExpression(source.getExpression()); clone.matchClauses = cloneList(source.matchClauses); + clone.onFailClause = source.onFailClause; } @Override diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/CodeAnalyzer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/CodeAnalyzer.java index 79eeac4bfcd4..52df72c25941 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/CodeAnalyzer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/CodeAnalyzer.java @@ -58,9 +58,6 @@ import org.wso2.ballerinalang.compiler.tree.BLangBlockFunctionBody; import org.wso2.ballerinalang.compiler.tree.BLangClassDefinition; import org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit; -import org.wso2.ballerinalang.compiler.tree.bindingpatterns.BLangBindingPattern; -import org.wso2.ballerinalang.compiler.tree.bindingpatterns.BLangCaptureBindingPattern; -import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangConstPattern; import org.wso2.ballerinalang.compiler.tree.BLangErrorVariable; import org.wso2.ballerinalang.compiler.tree.BLangExprFunctionBody; import org.wso2.ballerinalang.compiler.tree.BLangExternalFunctionBody; @@ -68,7 +65,6 @@ import org.wso2.ballerinalang.compiler.tree.BLangIdentifier; import org.wso2.ballerinalang.compiler.tree.BLangImportPackage; import org.wso2.ballerinalang.compiler.tree.BLangInvokableNode; -import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangMatchPattern; import org.wso2.ballerinalang.compiler.tree.BLangNode; import org.wso2.ballerinalang.compiler.tree.BLangNodeVisitor; import org.wso2.ballerinalang.compiler.tree.BLangPackage; @@ -793,6 +789,9 @@ public void visit(BLangIf ifStmt) { @Override public void visit(BLangMatchStatement matchStatement) { analyzeExpr(matchStatement.expr); + if (!this.failureHandled) { + this.failureHandled = matchStatement.onFailClause != null; + } matchExprType = matchStatement.expr.type; boolean matchStmtReturns = false; @@ -802,6 +801,7 @@ public void visit(BLangMatchStatement matchStatement) { } this.statementReturns = matchStmtReturns; hasLastPattern = false; + analyzeOnFailClause(matchStatement.onFailClause); } @Override diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/DataflowAnalyzer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/DataflowAnalyzer.java index 8baed453a921..f56d45842807 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/DataflowAnalyzer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/DataflowAnalyzer.java @@ -569,7 +569,9 @@ public void visit(BLangIf ifNode) { @Override public void visit(BLangMatchStatement matchStatement) { - + if (matchStatement.onFailClause != null) { + analyzeNode(matchStatement.onFailClause, env); + } } @Override diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java index cd526d925a66..1397586a352b 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java @@ -2305,6 +2305,10 @@ public void visit(BLangMatchStatement matchStatement) { for (BLangMatchClause matchClause : matchStatement.matchClauses) { analyzeNode(matchClause, env); } + + if (matchStatement.onFailClause != null) { + this.analyzeNode(matchStatement.onFailClause, env); + } } @Override diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TaintAnalyzer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TaintAnalyzer.java index 56da07f36aa6..519bf8dc8bb5 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TaintAnalyzer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TaintAnalyzer.java @@ -816,6 +816,10 @@ public void visit(BLangMatchStatement matchStatement) { getCurrentAnalysisState().taintedStatus = observedTaintedStatusOfMatchExpr; matchClause.accept(this); } + + if (matchStatement.onFailClause != null) { + analyzeNode(matchStatement.onFailClause, env); + } } @Override diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/statements/BLangMatchStatement.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/statements/BLangMatchStatement.java index aab0a423ba0b..6c987a07bd44 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/statements/BLangMatchStatement.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/tree/statements/BLangMatchStatement.java @@ -18,11 +18,13 @@ package org.wso2.ballerinalang.compiler.tree.statements; import org.ballerinalang.model.clauses.MatchClauseNode; +import org.ballerinalang.model.clauses.OnFailClauseNode; import org.ballerinalang.model.tree.NodeKind; import org.ballerinalang.model.tree.expressions.ExpressionNode; import org.ballerinalang.model.tree.statements.MatchStatementNode; import org.wso2.ballerinalang.compiler.tree.BLangNodeVisitor; import org.wso2.ballerinalang.compiler.tree.clauses.BLangMatchClause; +import org.wso2.ballerinalang.compiler.tree.clauses.BLangOnFailClause; import org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression; import java.util.ArrayList; @@ -34,6 +36,7 @@ public class BLangMatchStatement extends BLangStatement implements MatchStatementNode { public BLangExpression expr; // TODO : replace with new node `action|expression` public List matchClauses = new ArrayList<>(); + public BLangOnFailClause onFailClause; @Override public ExpressionNode getExpression() { @@ -55,6 +58,16 @@ public void addMatchClause(MatchClauseNode matchClauseNode) { matchClauses.add((BLangMatchClause) matchClauseNode); } + @Override + public OnFailClauseNode getOnFailClause() { + return this.onFailClause; + } + + @Override + public void setOnFailClause(OnFailClauseNode onFailClause) { + this.onFailClause = (BLangOnFailClause) onFailClause; + } + @Override public void accept(BLangNodeVisitor visitor) { visitor.visit(this);