Skip to content

Commit

Permalink
Merge pull request #41683 from lochana-chathura/worker_new
Browse files Browse the repository at this point in the history
Add parsing support for new worker related syntax
  • Loading branch information
rdulmina authored Nov 17, 2023
2 parents a3371aa + a5d501c commit 7fd8965
Show file tree
Hide file tree
Showing 32 changed files with 4,441 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9978,9 +9978,9 @@ private STNode parseNamespacePrefix() {
* Parse named worker declaration.
* <p>
* <code>named-worker-decl := [annots] [transactional] worker worker-name return-type-descriptor { sequence-stmt }
* </code>
* [on-fail-clause]</code>
*
* @param annots Annotations attached to the worker decl
* @param annots Annotations attached to the worker decl
* @param qualifiers Preceding transactional keyword in a list
* @return Parsed node
*/
Expand All @@ -9992,8 +9992,9 @@ private STNode parseNamedWorkerDeclaration(STNode annots, List<STNode> qualifier
STNode returnTypeDesc = parseReturnTypeDescriptor();
STNode workerBody = parseBlockNode();
endContext();
STNode onFailClause = parseOptionalOnFailClause();
return STNodeFactory.createNamedWorkerDeclarationNode(annots, transactionalKeyword, workerKeyword, workerName,
returnTypeDesc, workerBody);
returnTypeDesc, workerBody, onFailClause);
}

private STNode getTransactionalKeyword(List<STNode> qualifierList) {
Expand Down Expand Up @@ -13068,7 +13069,14 @@ private STNode parseSyncSendToken() {
/**
* Parse receive action.
* <p>
* <code>receive-action := single-receive-action | multiple-receive-action</code>
* <code>receive-action := single-receive-action | multiple-receive-action | alternate-receive-action</code>
* <p><code>
* single-receive-action := <- peer-worker
* <br></br>
* multiple-receive-action := <- { receive-field (, receive-field)* }
* <br></br>
* alternate-receive-action := <- peer-worker (| peer-worker)*
* </code>
*
* @return Receive action
*/
Expand All @@ -13082,7 +13090,7 @@ private STNode parseReceiveWorkers() {
switch (peek().kind) {
case FUNCTION_KEYWORD:
case IDENTIFIER_TOKEN:
return parsePeerWorkerName();
return parseSingleOrAlternateReceiveWorkers();
case OPEN_BRACE_TOKEN:
return parseMultipleReceiveWorkers();
default:
Expand All @@ -13091,6 +13099,32 @@ private STNode parseReceiveWorkers() {
}
}

private STNode parseSingleOrAlternateReceiveWorkers() {
startContext(ParserRuleContext.SINGLE_OR_ALTERNATE_WORKER);
List<STNode> workers = new ArrayList<>();
// Parse first peer worker name, that has no leading comma
STNode peerWorker = parsePeerWorkerName();
workers.add(peerWorker);

STToken nextToken = peek();
if (nextToken.kind != SyntaxKind.PIPE_TOKEN) {
endContext();
return peerWorker;
}

// Parse the remaining peer worker names
while (nextToken.kind == SyntaxKind.PIPE_TOKEN) {
STNode pipeToken = consume();
workers.add(pipeToken);
peerWorker = parsePeerWorkerName();
workers.add(peerWorker);
nextToken = peek();
}

endContext();
return STNodeFactory.createAlternateReceiveWorkerNode(STNodeFactory.createNodeList(workers));
}

/**
* Parse multiple worker receivers.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,10 @@ public class BallerinaParserErrorHandler extends AbstractParserErrorHandler {
{ ParserRuleContext.ARG_LIST_OPEN_PAREN, ParserRuleContext.SEMICOLON };

private static final ParserRuleContext[] RECEIVE_WORKERS =
{ ParserRuleContext.PEER_WORKER_NAME, ParserRuleContext.MULTI_RECEIVE_WORKERS };
{ ParserRuleContext.SINGLE_OR_ALTERNATE_WORKER, ParserRuleContext.MULTI_RECEIVE_WORKERS };

private static final ParserRuleContext[] SINGLE_OR_ALTERNATE_WORKER_SEPARATOR =
{ ParserRuleContext.SINGLE_OR_ALTERNATE_WORKER_END, ParserRuleContext.PIPE };

private static final ParserRuleContext[] RECEIVE_FIELD =
{ ParserRuleContext.PEER_WORKER_NAME, ParserRuleContext.RECEIVE_FIELD_NAME };
Expand Down Expand Up @@ -1607,6 +1610,7 @@ protected boolean hasAlternativePaths(ParserRuleContext currentCtx) {
case GROUPING_KEY_LIST_ELEMENT:
case GROUPING_KEY_LIST_ELEMENT_END:
case RESULT_CLAUSE:
case SINGLE_OR_ALTERNATE_WORKER_SEPARATOR:
return true;
default:
return false;
Expand Down Expand Up @@ -2075,6 +2079,8 @@ protected ParserRuleContext getShortestAlternative(ParserRuleContext currentCtx)
return ParserRuleContext.TYPE_DESC_IN_TUPLE;
case RESULT_CLAUSE:
return ParserRuleContext.SELECT_CLAUSE;
case SINGLE_OR_ALTERNATE_WORKER_SEPARATOR:
return ParserRuleContext.SINGLE_OR_ALTERNATE_WORKER_END;
default:
throw new IllegalStateException("Alternative path entry not found");
}
Expand Down Expand Up @@ -2845,6 +2851,9 @@ private Result seekMatchInExprRelatedAlternativePaths(ParserRuleContext currentC
case ERROR_CONSTRUCTOR_RHS:
alternativeRules = ERROR_CONSTRUCTOR_RHS;
break;
case SINGLE_OR_ALTERNATE_WORKER_SEPARATOR:
alternativeRules = SINGLE_OR_ALTERNATE_WORKER_SEPARATOR;
break;
default:
throw new IllegalStateException("seekMatchInExprRelatedAlternativePaths found: " + currentCtx);
}
Expand Down Expand Up @@ -3481,6 +3490,11 @@ protected ParserRuleContext getNextRule(ParserRuleContext currentCtx, int nextLo
return getNextRuleForBindingPattern();
case TUPLE_MEMBERS:
return ParserRuleContext.TUPLE_MEMBER;
case SINGLE_OR_ALTERNATE_WORKER:
return ParserRuleContext.PEER_WORKER_NAME;
case SINGLE_OR_ALTERNATE_WORKER_END:
endContext(); // end single-or-alternate-worker
return ParserRuleContext.EXPRESSION_RHS;
default:
return getNextRuleInternal(currentCtx, nextLookahead);
}
Expand All @@ -3504,6 +3518,8 @@ private ParserRuleContext getNextRuleInternal(ParserRuleContext currentCtx, int
return ParserRuleContext.XML_ATOMIC_NAME_PATTERN;
} else if (parentCtx == ParserRuleContext.MATCH_PATTERN) {
return ParserRuleContext.MATCH_PATTERN_START;
} else if (parentCtx == ParserRuleContext.SINGLE_OR_ALTERNATE_WORKER) {
return ParserRuleContext.PEER_WORKER_NAME;
}
return ParserRuleContext.TYPE_DESCRIPTOR;
case TABLE_CONSTRUCTOR:
Expand Down Expand Up @@ -4091,8 +4107,11 @@ private ParserRuleContext getNextRuleForKeywords(ParserRuleContext currentCtx, i
case FLUSH_KEYWORD:
return ParserRuleContext.OPTIONAL_PEER_WORKER;
case PEER_WORKER_NAME:
if (getParentContext() == ParserRuleContext.MULTI_RECEIVE_WORKERS) {
parentCtx = getParentContext();
if (parentCtx == ParserRuleContext.MULTI_RECEIVE_WORKERS) {
return ParserRuleContext.RECEIVE_FIELD_END;
} else if (parentCtx == ParserRuleContext.SINGLE_OR_ALTERNATE_WORKER) {
return ParserRuleContext.SINGLE_OR_ALTERNATE_WORKER_SEPARATOR;
}
return ParserRuleContext.EXPRESSION_RHS;
case WAIT_KEYWORD:
Expand Down Expand Up @@ -4269,6 +4288,7 @@ private void startContextIfRequired(ParserRuleContext currentCtx) {
case BRACED_EXPRESSION:
case CLIENT_RESOURCE_ACCESS_ACTION:
case TUPLE_MEMBERS:
case SINGLE_OR_ALTERNATE_WORKER:

// Contexts that expect a type
case TYPE_DESC_IN_ANNOTATION_DECL:
Expand Down Expand Up @@ -4824,10 +4844,10 @@ private ParserRuleContext getNextRuleForCloseBrace(int nextLookahead) {
case CLOSE_BRACE_TOKEN:
return ParserRuleContext.CLOSE_BRACE;
default:
return ParserRuleContext.STATEMENT;
return ParserRuleContext.REGULAR_COMPOUND_STMT_RHS;
}
} else {
return ParserRuleContext.STATEMENT;
return ParserRuleContext.REGULAR_COMPOUND_STMT_RHS;
}
case MATCH_BODY:
return ParserRuleContext.MATCH_PATTERN;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,9 @@ public enum ParserRuleContext {
OPTIONAL_TOP_LEVEL_SEMICOLON("optional-top-level-semicolon"),
TUPLE_MEMBERS("tuple-members"),
TUPLE_MEMBER("tuple-member"),
SINGLE_OR_ALTERNATE_WORKER("single-or-alternate-worker"),
SINGLE_OR_ALTERNATE_WORKER_SEPARATOR("single-or-alternate-worker-separator"),
SINGLE_OR_ALTERNATE_WORKER_END("single-or-alternate-worker-end"),
;

private String value;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. 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 io.ballerina.compiler.internal.parser.tree;

import io.ballerina.compiler.syntax.tree.AlternateReceiveWorkerNode;
import io.ballerina.compiler.syntax.tree.Node;
import io.ballerina.compiler.syntax.tree.NonTerminalNode;
import io.ballerina.compiler.syntax.tree.SyntaxKind;

import java.util.Collection;
import java.util.Collections;

/**
* This is a generated internal syntax tree node.
*
* @since 2201.9.0
*/
public class STAlternateReceiveWorkerNode extends STNode {
public final STNode workers;

STAlternateReceiveWorkerNode(
STNode workers) {
this(
workers,
Collections.emptyList());
}

STAlternateReceiveWorkerNode(
STNode workers,
Collection<STNodeDiagnostic> diagnostics) {
super(SyntaxKind.ALTERNATE_RECEIVE_WORKER, diagnostics);
this.workers = workers;

addChildren(
workers);
}

public STNode modifyWith(Collection<STNodeDiagnostic> diagnostics) {
return new STAlternateReceiveWorkerNode(
this.workers,
diagnostics);
}

public STAlternateReceiveWorkerNode modify(
STNode workers) {
if (checkForReferenceEquality(
workers)) {
return this;
}

return new STAlternateReceiveWorkerNode(
workers,
diagnostics);
}

public Node createFacade(int position, NonTerminalNode parent) {
return new AlternateReceiveWorkerNode(this, position, parent);
}

@Override
public void accept(STNodeVisitor visitor) {
visitor.visit(this);
}

@Override
public <T> T apply(STNodeTransformer<T> transformer) {
return transformer.transform(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,24 @@ public class STNamedWorkerDeclarationNode extends STNode {
public final STNode workerName;
public final STNode returnTypeDesc;
public final STNode workerBody;
public final STNode onFailClause;

STNamedWorkerDeclarationNode(
STNode annotations,
STNode transactionalKeyword,
STNode workerKeyword,
STNode workerName,
STNode returnTypeDesc,
STNode workerBody) {
STNode workerBody,
STNode onFailClause) {
this(
annotations,
transactionalKeyword,
workerKeyword,
workerName,
returnTypeDesc,
workerBody,
onFailClause,
Collections.emptyList());
}

Expand All @@ -62,6 +65,7 @@ public class STNamedWorkerDeclarationNode extends STNode {
STNode workerName,
STNode returnTypeDesc,
STNode workerBody,
STNode onFailClause,
Collection<STNodeDiagnostic> diagnostics) {
super(SyntaxKind.NAMED_WORKER_DECLARATION, diagnostics);
this.annotations = annotations;
Expand All @@ -70,14 +74,16 @@ public class STNamedWorkerDeclarationNode extends STNode {
this.workerName = workerName;
this.returnTypeDesc = returnTypeDesc;
this.workerBody = workerBody;
this.onFailClause = onFailClause;

addChildren(
annotations,
transactionalKeyword,
workerKeyword,
workerName,
returnTypeDesc,
workerBody);
workerBody,
onFailClause);
}

public STNode modifyWith(Collection<STNodeDiagnostic> diagnostics) {
Expand All @@ -88,6 +94,7 @@ public STNode modifyWith(Collection<STNodeDiagnostic> diagnostics) {
this.workerName,
this.returnTypeDesc,
this.workerBody,
this.onFailClause,
diagnostics);
}

Expand All @@ -97,14 +104,16 @@ public STNamedWorkerDeclarationNode modify(
STNode workerKeyword,
STNode workerName,
STNode returnTypeDesc,
STNode workerBody) {
STNode workerBody,
STNode onFailClause) {
if (checkForReferenceEquality(
annotations,
transactionalKeyword,
workerKeyword,
workerName,
returnTypeDesc,
workerBody)) {
workerBody,
onFailClause)) {
return this;
}

Expand All @@ -115,6 +124,7 @@ public STNamedWorkerDeclarationNode modify(
workerName,
returnTypeDesc,
workerBody,
onFailClause,
diagnostics);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -979,15 +979,17 @@ public static STNode createNamedWorkerDeclarationNode(
STNode workerKeyword,
STNode workerName,
STNode returnTypeDesc,
STNode workerBody) {
STNode workerBody,
STNode onFailClause) {

return new STNamedWorkerDeclarationNode(
annotations,
transactionalKeyword,
workerKeyword,
workerName,
returnTypeDesc,
workerBody);
workerBody,
onFailClause);
}

public static STNode createNamedWorkerDeclarator(
Expand Down Expand Up @@ -1824,6 +1826,13 @@ public static STNode createReceiveFieldsNode(
closeBrace);
}

public static STNode createAlternateReceiveWorkerNode(
STNode workers) {

return new STAlternateReceiveWorkerNode(
workers);
}

public static STNode createRestDescriptorNode(
STNode typeDescriptor,
STNode ellipsisToken) {
Expand Down
Loading

0 comments on commit 7fd8965

Please sign in to comment.