Skip to content

Commit

Permalink
LocalStack's Amazon States Language (ASL) Grammar (antlr#3981)
Browse files Browse the repository at this point in the history
* Add LocalStack AmazonStatesLanguage grammars and examples.

* build configuration

* devide modules into asl and intrinsic functions

* start productions with eof
  • Loading branch information
MEPalma authored Mar 3, 2024
1 parent dcbcab9 commit a7363ce
Show file tree
Hide file tree
Showing 39 changed files with 1,430 additions and 0 deletions.
85 changes: 85 additions & 0 deletions amazon-states-language-intrinsic-functions/ASLIntrinsicLexer.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* Copyright (c) 2017+ LocalStack contributors
* Copyright (c) 2016 Atlassian Pty Ltd
* Licensed 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.
*/

/**
* Amazon States Lanaguage (ASL) Grammar for ANTLR v4
*
* Based on:
* http://github.com/localstack/localstack
* and
* https://states-language.net/spec.html
*/

// $antlr-format alignTrailingComments true, columnLimit 150, maxEmptyLinesToKeep 1, reflowComments false, useTab false
// $antlr-format allowShortRulesOnASingleLine true, allowShortBlocksOnASingleLine true, minEmptyLines 0, alignSemicolons ownLine
// $antlr-format alignColons trailing, singleLineOverrulesHangingColon true, alignLexerCommands true, alignLabels true, alignTrailers true

lexer grammar ASLIntrinsicLexer;

CONTEXT_PATH_STRING: DOLLAR DOLLAR JSON_PATH_BODY;

JSON_PATH_STRING: DOLLAR JSON_PATH_BODY;

fragment JSON_PATH_BODY: JSON_PATH_BRACK? (DOT IDENTIFIER? JSON_PATH_BRACK?)*;

fragment JSON_PATH_BRACK: '[' (JSON_PATH_BRACK | ~[\]])* ']';

DOLLAR : '$';
LPAREN : '(';
RPAREN : ')';
COMMA : ',';
DOT : '.';

TRUE : 'true';
FALSE : 'false';

States : 'States';
Format : 'Format';
StringToJson : 'StringToJson';
JsonToString : 'JsonToString';
Array : 'Array';
ArrayPartition : 'ArrayPartition';
ArrayContains : 'ArrayContains';
ArrayRange : 'ArrayRange';
ArrayGetItem : 'ArrayGetItem';
ArrayLength : 'ArrayLength';
ArrayUnique : 'ArrayUnique';
Base64Encode : 'Base64Encode';
Base64Decode : 'Base64Decode';
Hash : 'Hash';
JsonMerge : 'JsonMerge';
MathRandom : 'MathRandom';
MathAdd : 'MathAdd';
StringSplit : 'StringSplit';
UUID : 'UUID';

STRING: '\'' (ESC | SAFECODEPOINT)*? '\'';

fragment ESC : '\\' (UNICODE | .);
fragment UNICODE : 'u' HEX HEX HEX HEX;
fragment HEX : [0-9a-fA-F];
fragment SAFECODEPOINT : ~ ['\\\u0000-\u001F];
INT: '-'? ('0' | [1-9] [0-9]*);
NUMBER: '-'? INT ('.' [0-9]+)? EXP?;
fragment EXP: [Ee] [+\-]? INT;
IDENTIFIER: ([0-9a-zA-Z_] | UNICODE)+;
WS: [ \t\n]+ -> skip;
72 changes: 72 additions & 0 deletions amazon-states-language-intrinsic-functions/ASLIntrinsicParser.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* Copyright (c) 2017+ LocalStack contributors
* Copyright (c) 2016 Atlassian Pty Ltd
* Licensed 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.
*/

/**
* Amazon States Lanaguage (ASL) Grammar for ANTLR v4
*
* Based on:
* http://github.com/localstack/localstack
* and
* https://states-language.net/spec.html
*/

// $antlr-format alignTrailingComments true, columnLimit 150, maxEmptyLinesToKeep 1, reflowComments false, useTab false
// $antlr-format allowShortRulesOnASingleLine true, allowShortBlocksOnASingleLine true, minEmptyLines 0, alignSemicolons ownLine
// $antlr-format alignColons trailing, singleLineOverrulesHangingColon true, alignLexerCommands true, alignLabels true, alignTrailers true

parser grammar ASLIntrinsicParser;

options {
tokenVocab = ASLIntrinsicLexer;
}

intrinsic_function: states_func_decl EOF;

states_func_decl: States DOT state_fun_name func_arg_list;

state_fun_name:
Format
| StringToJson
| JsonToString
| Array
| ArrayPartition
| ArrayContains
| ArrayRange
| ArrayGetItem
| ArrayLength
| ArrayUnique
| Base64Encode
| Base64Decode
| Hash
| JsonMerge
| MathRandom
| MathAdd
| StringSplit
| UUID
;

func_arg_list: LPAREN func_arg (COMMA func_arg)* RPAREN | LPAREN RPAREN;

func_arg:
STRING # func_arg_string
| INT # func_arg_int
| NUMBER # func_arg_float
| (TRUE | FALSE) # func_arg_bool
| CONTEXT_PATH_STRING # func_arg_context_path
| JSON_PATH_STRING # func_arg_json_path
| states_func_decl # func_arg_func_decl
;
39 changes: 39 additions & 0 deletions amazon-states-language-intrinsic-functions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Amazon States Language Intrinsic Functions ANTLR4 Grammar

The ANTLR4 grammar for the Intrinsic Functions of Amazon States Language (ASL), initially developed by
[LocalStack](github.com/localstack/localstack).

The module includes grammars (`ASLIntrinsicLexer.g4` and `ASLIntrinsicParser.g4`) for parsing intrinsic
functions within ASL derivations.

## License
```txt
Copyright (c) 2017+ LocalStack contributors
Copyright (c) 2016 Atlassian Pty Ltd
Licensed 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.
```

## Links

* [LocalStack](https://github.com/localstack/localstack) - LocalStack, the project
that developed the grammar and an Amazon States Language interpreter.
* [Amazon Language Specification](https://states-language.net/spec.html) - The original
language specification for Amazon States Language provided by Amazon.


## Examples
The `examples/` directory contains examples illustrating the utilisation of intrinsic functions.

## Original source
<https://github.com/localstack/localstack>
4 changes: 4 additions & 0 deletions amazon-states-language-intrinsic-functions/desc.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" ?>
<desc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../_scripts/desc.xsd">
<targets>CSharp;Cpp;Dart;Go;Java;JavaScript;PHP;Python3;TypeScript</targets>
</desc>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
States.Array($.FunctionInput.fst, $.FunctionInput.snd)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
States.ArrayContains($.FunctionInput.fst, $.FunctionInput.snd)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
States.ArrayGetItem($.FunctionInput.fst, $.FunctionInput.snd)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
States.ArrayLength($.FunctionInput)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
States.ArrayPartition($.FunctionInput.fst, $.FunctionInput.snd)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
States.ArrayRange($.FunctionInput.fst, $.FunctionInput.snd, $.FunctionInput.trd)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
States.ArrayUnique($.FunctionInput)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
States.Base64Decode($.FunctionInput)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
States.Base64Encode($.FunctionInput)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
States.Format('Formatting arguments fst={} and snd={}.', $.FunctionInput.fst, $.FunctionInput.snd)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
States.Hash($.FunctionInput.fst, $.FunctionInput.snd)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
States.JsonMerge($.FunctionInput.fst, $.FunctionInput.snd, false)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
States.JsonToString($.FunctionInput)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
States.MathAdd($.FunctionInput.fst, $.FunctionInput.snd)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
States.MathRandom($.FunctionInput.fst, $.FunctionInput.snd, $.FunctionInput.trd)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
States.ArrayGetItem(States.StringSplit($$.StateMachine.Name, '-'), States.MathAdd(States.ArrayLength(States.StringSplit($$.StateMachine.Name, '-')), -1))
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
States.StringSplit('Hello\nWorld', '\n')
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
States.StringToJson($.FunctionInput)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
States.UUID()
57 changes: 57 additions & 0 deletions amazon-states-language-intrinsic-functions/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>amazon-states-language-intrinsic-functions</artifactId>
<packaging>jar</packaging>
<name>localstack.cloud amazon-states-language intrinsic-functions</name>
<parent>
<groupId>org.antlr.grammars</groupId>
<artifactId>grammarsv4</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<build>
<plugins>
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>${antlr.version}</version>
<configuration>
<sourceDirectory>${basedir}</sourceDirectory>
<includes>
<include>ASLIntrinsicLexer.g4</include>
<include>ASLIntrinsicParser.g4</include>
</includes>
<visitor>true</visitor>
<listener>true</listener>
</configuration>
<executions>
<execution>
<goals>
<goal>antlr4</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.khubla.antlr</groupId>
<artifactId>antlr4test-maven-plugin</artifactId>
<version>${antlr4test-maven-plugin.version}</version>
<configuration>
<verbose>false</verbose>
<showTree>false</showTree>
<entryPoint>intrinsic_function</entryPoint>
<grammarName>ASLIntrinsic</grammarName>
<packageName></packageName>
<exampleFiles>examples/</exampleFiles>
</configuration>
<executions>
<execution>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Loading

0 comments on commit a7363ce

Please sign in to comment.