Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FOU-470] ANTLR Code Generation #25654

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions python_modules/dagster/.gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
dagster_tests/**/snapshots/* linguist-generated=true
dagster/_core/definitions/antlr_asset_selection/generated/* linguist-generated=true
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
grammar AssetSelection;
briantu marked this conversation as resolved.
Show resolved Hide resolved

start: expr EOF;

// Root rule for parsing expressions
expr
: assetExpr # AssetExpression
| attributeExpr # AttributeExpression
| traversal expr # UpTraversalExpression
| traversal expr traversal # UpAndDownTraversalExpression
| expr traversal # DownTraversalExpression
| NOT expr # NotExpression
| expr AND expr # AndExpression
| expr OR expr # OrExpression
| functionName LPAREN expr RPAREN # FunctionCallExpression
| LPAREN expr RPAREN # ParenthesizedExpression
;

// Traversal operators
traversal
: STAR
| PLUS+
;

// Function names as tokens
functionName
: SINKS
| ROOTS
;

// Attribute expressions for specific attributes
attributeExpr
: TAG COLON value (EQUAL value)? # TagAttributeExpr
| OWNER COLON value # OwnerAttributeExpr
| GROUP COLON value # GroupAttributeExpr
| KIND COLON value # KindAttributeExpr
| CODE_LOCATION COLON value # CodeLocationAttributeExpr
;

// Define the EQUAL token for tag:value=value syntax
EQUAL : '=';

// Value can be a quoted or unquoted string
value
: QUOTED_STRING
| UNQUOTED_STRING
;

// Asset expressions
assetExpr
: QUOTED_STRING # ExactMatchAsset
| UNQUOTED_STRING # PrefixMatchAsset
;

// Tokens for operators and keywords
AND : 'and';
OR : 'or';
NOT : 'not';

STAR : '*';
PLUS : '+';

COLON : ':';

LPAREN : '(';
RPAREN : ')';
COMMA : ',';

// Tokens for keys
OWNER : 'owner';
GROUP : 'group';
TAG : 'tag';
KIND : 'kind';
CODE_LOCATION : 'code_location';

// Tokens for function names
SINKS : 'sinks';
ROOTS : 'roots';

// Tokens for strings
QUOTED_STRING : '"' (~["\\\r\n])* '"' ;
UNQUOTED_STRING : [a-zA-Z_][a-zA-Z0-9_]*;

// Whitespace
WS : [ \t\r\n]+ -> skip ;
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# How to set up ANTLR

## Install Java

Using M1 MacBook and Homebrew.

Install java:

```bash
$ brew install openjdk
```

Verify it's installed:

```bash
$ $(brew --prefix openjdk)/bin/java --version
```

Verify it's for the arm64 hardware:

```bash
$ file $(brew --prefix openjdk)/bin/java
```

Create symlink:

```bash
$ sudo ln -sfn /opt/homebrew/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk
$ java -version
```

## Install ANTLR

https://github.com/antlr/antlr4/blob/master/doc/getting-started.md

1. Download:

```bash
$ cd /usr/local/lib
$ curl -O https://www.antlr.org/download/antlr-4.13.2-complete.jar
```

2. Add antlr-4.13.2-complete.jar to your `CLASSPATH`:

```bash
$ export CLASSPATH=".:/usr/local/lib/antlr-4.13.2-complete.jar:$CLASSPATH"
```

Also add to `.bash_profile` or whatever your startup script is.

3. Create alias for ANTLR:

```bash
$ alias antlr4='java -Xmx500M -cp "/usr/local/lib/antlr-4.13.2-complete.jar:$CLASSPATH" org.antlr.v4.Tool'
```

4. Install python runtime:

```bash
pip install antlr4-python3-runtime
```

## Generate Python Parser

Under the `python_modules/dagster/dagster/_core/definitions/asset_selection` directory, run

```bash
$ antlr4 -Dlanguage=Python3 -visitor AssetSelection.g4 -o generated
```

This will generate the following files from the grammar file `AssetSelection.g4` in a `generated` folder:

- `AssetSelection.interp`
- `AssetSelection.tokens`
- `AssetSelectionLexer.interp`
- `AssetSelectionLexer.py`
- `AssetSelectionLexer.tokens`
- `AssetSelectionListener.py`
- `AssetSelectionParser.py`
- `AssetSelectionVisitor.py`

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading