Skip to content

Commit

Permalink
feat: enhance parser with concurrency constructs (#16)
Browse files Browse the repository at this point in the history
* feat: enhance parser with concurrency constructs

* feat: add SelectStatement, GoStatement, and
SendStatement to parser

* feat: support for unary operator "<-"

* feat: address PR comments

* chore: fix erroneous grammar

* chore: make left of recvStmt optional as requirement
  • Loading branch information
alvynben authored Mar 30, 2024
1 parent 79506a5 commit 071ba6e
Showing 1 changed file with 46 additions and 2 deletions.
48 changes: 46 additions & 2 deletions src/go-slang/parser/go.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ TopLevelDeclaration
/ FunctionDeclaration

Statement
= Declaration
= SelectStatement
/ GoStatement
/ Declaration
/ SimpleStatement
/ ReturnStatement
/ IfStatement
Expand All @@ -60,7 +62,8 @@ Declaration
= VariableDeclaration

SimpleStatement
= ShortVariableDeclaration
= SendStatement
/ ShortVariableDeclaration
/ Assignment
/ ExpressionStatement

Expand Down Expand Up @@ -145,6 +148,7 @@ UnaryExpression
UnaryOperator
= "+"
/ "-"
/ "<-"

MultiplicativeExpression
= head: UnaryExpression
Expand Down Expand Up @@ -185,6 +189,46 @@ CallExpression
return makeNode({ type: "CallExpression", callee, args: args ?? [] })
}

/* Select Statement */

SelectStatement "select statement"
= SELECT_TOKEN _ "{" _ cases:CommClause* _ "}" EOS {
return makeNode({ type: "SelectStatement", cases })
}

CommClause
= pred:CommCase __ ":" _ statements:Statement* EOS {
return makeNode({ type: "CommClause", pred, statements })
}
CommCase
= CASE_TOKEN _ statement:(SendStatement / ReceiveStatement) EOS { return statement }
/ DEFAULT_TOKEN { return [] }

ReceiveStatement
= left:( ExpressionList __ "=" / IdentifierList __ ":=" )? __ right:ReceiveExpression EOS {
return makeNode({ type: "ReceiveStatement", left, right })
}

ReceiveExpression
= Expression

/* Go Statement */

GoStatement
= GO_TOKEN __ call:Expression EOS {
return makeNode({ type: "GoStatement", call })
}

/* Send Declaration */

SendStatement
= channel:Channel _ "<-" _ value:Expression EOS {
return makeNode({ type: "SendStatement", channel, value })
}

Channel
= Expression

/* Variable Declaration */

VariableDeclaration
Expand Down

0 comments on commit 071ba6e

Please sign in to comment.