Skip to content
This repository has been archived by the owner on May 13, 2024. It is now read-only.

Commit

Permalink
parser and lexer
Browse files Browse the repository at this point in the history
  • Loading branch information
le-yams authored and yann-disanto committed Sep 1, 2023
1 parent dc2818e commit 98e9ac6
Show file tree
Hide file tree
Showing 14 changed files with 266 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ build/
*.iml
*.ipr
out/
**/src/main/**/gen/
**/src/generated/
!**/src/main/**/out/
!**/src/test/**/out/

Expand Down
37 changes: 35 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@

plugins {
id("java")
id("org.jetbrains.kotlin.jvm") version "1.9.0"
id("org.jetbrains.intellij") version "1.15.0"
id("org.jetbrains.grammarkit") version "2022.3.1"
}

group = "com.example"
group = "com.github.le_yams"
version = "1.0-SNAPSHOT"

repositories {
mavenCentral()
}

sourceSets["main"].java.srcDirs("src/main/gen")
sourceSets["main"].java.srcDirs("src/generated/java", "src/main/java")


// Configure Gradle IntelliJ Plugin
// Read more: https://plugins.jetbrains.com/docs/intellij/tools-gradle-intellij-plugin.html
Expand All @@ -22,7 +25,22 @@ intellij {
plugins.set(listOf(/* Plugin Dependencies */))
}

grammarKit {
jflexRelease.set("1.7.0-1")
grammarKitRelease.set("2021.1.2")
intellijRelease.set("203.7717.81")
}

tasks {

compileJava {

dependsOn(
generateLexer,
generateParser,
)
}

// Set the JVM compatibility versions
withType<JavaCompile> {
sourceCompatibility = "17"
Expand All @@ -32,6 +50,21 @@ tasks {
kotlinOptions.jvmTarget = "17"
}

generateLexer {
sourceFile.set(file("src/main/java/com/github/le_yams/openfga4intellij/parsing/OpenFGALexer.flex"))
targetDir.set("src/generated/java/com/github/le_yams/openfga4intellij/parsing")
targetClass.set("OpenFGALexer")
purgeOldFiles.set(true)
}

generateParser {
sourceFile.set(file("src/main/java/com/github/le_yams/openfga4intellij/parsing/openfga.bnf"))
targetRoot.set("src/generated/java")
pathToParser.set("com/github/le_yams/openfga4intellij/parsing/RustParser.java")
pathToPsiRoot.set("com/github/le_yams/openfga4intellij/psi")
purgeOldFiles.set(true)
}

patchPluginXml {
sinceBuild.set("222")
untilBuild.set("232.*")
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/github/le_yams/openfga4intellij/OpenFGAFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.github.le_yams.openfga4intellij;

import com.intellij.extapi.psi.PsiFileBase;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.psi.FileViewProvider;
import org.jetbrains.annotations.NotNull;

public class OpenFGAFile extends PsiFileBase {

public OpenFGAFile(@NotNull FileViewProvider viewProvider) {
super(viewProvider, OpenFGALanguage.INSTANCE);
}

@NotNull
@Override
public FileType getFileType() {
return OpenFGAFileType.INSTANCE;
}

@Override
public String toString() {
return getFileType().getName();
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.openfga4intellij;
package com.github.le_yams.openfga4intellij;

import com.intellij.openapi.fileTypes.LanguageFileType;
import org.jetbrains.annotations.NotNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.openfga4intellij;
package com.github.le_yams.openfga4intellij;

import com.intellij.openapi.util.IconLoader;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.openfga4intellij;
package com.github.le_yams.openfga4intellij;

import com.intellij.lang.Language;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.github.le_yams.openfga4intellij;

import com.github.le_yams.openfga4intellij.parsing.OpenFGALexer;
import com.intellij.lexer.FlexAdapter;

public class OpenFGALexerAdapter extends FlexAdapter {

public OpenFGALexerAdapter() {
super(new OpenFGALexer(null));
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.example.openfga4intellij.psi;
package com.github.le_yams.openfga4intellij.parsing;

import com.example.openfga4intellij.OpenFGALanguage;
import com.github.le_yams.openfga4intellij.OpenFGALanguage;
import com.intellij.psi.tree.IElementType;
import org.jetbrains.annotations.NonNls;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.github.le_yams.openfga4intellij.parsing;

import com.intellij.lexer.FlexLexer;
import com.intellij.psi.tree.IElementType;

import static com.intellij.psi.TokenType.BAD_CHARACTER;
import static com.intellij.psi.TokenType.WHITE_SPACE;
import static com.github.le_yams.openfga4intellij.psi.OpenFGATypes.*;

%%

%{
public OpenFGALexer() {
this((java.io.Reader)null);
}
%}

%public
%class OpenFGALexer
%implements FlexLexer
%function advance
%type IElementType
%unicode

ALPHA_NUMERIC=[a-zA-Z0-9_-]+
END_OF_LINE=(\r\n)|\n
WHITESPACE=[\ \t]
IDENT1=((\ {2})|\t)
IDENT2=((\ {4})|(\t{2}))
SINGLE_LINE_COMMENT=[ \t]*#.*

%%
<YYINITIAL> {
" " { return WHITE_SPACE; }

"model" { return MODEL; }
"schema" { return SCHEMA; }
"1.1" { return SCHEMA_VERSION_V1_1; }
"type" { return TYPE; }
"relations" { return RELATIONS; }
"define" { return DEFINE; }
"#" { return HASH; }
":" { return COLON; }
"*" { return WILDCARD; }
"[" { return L_SQUARE; }
"]" { return R_SQUARE; }
"," { return COMMA; }
"and" { return AND; }
"or" { return OR; }
"but not" { return BUT_NOT; }
"from" { return FROM; }
"EOF" { return EOF; }

{ALPHA_NUMERIC} { return ALPHA_NUMERIC; }
{END_OF_LINE} { return END_OF_LINE; }
{WHITESPACE} { return WHITESPACE; }
{IDENT1} { return IDENT1; }
{IDENT2} { return IDENT2; }
{SINGLE_LINE_COMMENT} { return SINGLE_LINE_COMMENT; }

}

[^] { return BAD_CHARACTER; }
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.github.le_yams.openfga4intellij.parsing;

import com.github.le_yams.openfga4intellij.OpenFGAFile;
import com.github.le_yams.openfga4intellij.OpenFGALanguage;
import com.github.le_yams.openfga4intellij.OpenFGALexerAdapter;
import com.github.le_yams.openfga4intellij.psi.OpenFGATypes;
import com.intellij.lang.ASTNode;
import com.intellij.lang.ParserDefinition;
import com.intellij.lang.PsiParser;
import com.intellij.lexer.Lexer;
import com.intellij.openapi.project.Project;
import com.intellij.psi.FileViewProvider;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.tree.IFileElementType;
import com.intellij.psi.tree.TokenSet;
import org.jetbrains.annotations.NotNull;

public class OpenFGAParserDefinition implements ParserDefinition {

public static final IFileElementType FILE = new IFileElementType(OpenFGALanguage.INSTANCE);

@NotNull
@Override
public Lexer createLexer(Project project) {
return new OpenFGALexerAdapter();
}

@NotNull
@Override
public TokenSet getCommentTokens() {
return OpenFGATokenSets.SINGLE_LINE_COMMENT;
}

@NotNull
@Override
public TokenSet getStringLiteralElements() {
return TokenSet.EMPTY;
}

// @NotNull
// @Override
// public TokenSet getWhitespaceTokens() {
// return OpenFGATokenSets.WHITE_SPACE;
// }

@NotNull
@Override
public PsiParser createParser(final Project project) {
return new OpenFGAParser();
}

@NotNull
@Override
public IFileElementType getFileNodeType() {
return FILE;
}

@NotNull
@Override
public PsiFile createFile(@NotNull FileViewProvider viewProvider) {
return new OpenFGAFile(viewProvider);
}

@NotNull
@Override
public PsiElement createElement(ASTNode node) {
return OpenFGATypes.Factory.createElement(node);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.github.le_yams.openfga4intellij.parsing;

import com.github.le_yams.openfga4intellij.psi.OpenFGATypes;
import com.intellij.psi.tree.TokenSet;

public interface OpenFGATokenSets {

TokenSet KEYWORDS = TokenSet.create(
OpenFGATypes.MODEL,
OpenFGATypes.SCHEMA,
OpenFGATypes.TYPE,
OpenFGATypes.RELATIONS,
OpenFGATypes.DEFINE,
OpenFGATypes.OR,
OpenFGATypes.FROM
);

TokenSet RELATION_NAME = TokenSet.create(OpenFGATypes.RELATION_NAME);
TokenSet TYPE_IDENTIFIER = TokenSet.create(OpenFGATypes.TYPE_IDENTIFIER);
TokenSet SINGLE_LINE_COMMENT = TokenSet.create(OpenFGATypes.COMMENT);
// TokenSet DUMMY_WHITE_SPACE = TokenSet.create(OpenFGATypes.DUMMY_WHITE_SPACE);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.example.openfga4intellij.psi;
package com.github.le_yams.openfga4intellij.parsing;

import com.example.openfga4intellij.OpenFGALanguage;
import com.github.le_yams.openfga4intellij.OpenFGALanguage;
import com.intellij.psi.tree.IElementType;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
Expand Down
Loading

0 comments on commit 98e9ac6

Please sign in to comment.