Skip to content

Commit

Permalink
#25 - Add minimal support for .ml4 files: use the same parser than fo…
Browse files Browse the repository at this point in the history
…r Ocaml
  • Loading branch information
giraud committed Jan 11, 2018
1 parent 6712fb4 commit c82275c
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 0 deletions.
6 changes: 6 additions & 0 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@
<!-- Ocaml -->
<fileTypeFactory implementation="com.reason.ide.files.OclInterfaceFileTypeFactory"/>
<fileTypeFactory implementation="com.reason.ide.files.OclFileTypeFactory"/>
<!-- OcamlP4 -->
<fileTypeFactory implementation="com.reason.ide.files.Ml4FileTypeFactory"/>

<!--
| Parsers
Expand All @@ -105,6 +107,10 @@
<lang.syntaxHighlighterFactory language="Ocaml"
implementationClass="com.reason.ide.highlight.OclSyntaxHighlighterFactory"/>

<lang.parserDefinition language="OcamlP4" implementationClass="com.reason.lang.ocaml.OclP4ParserDefinition"/>
<lang.syntaxHighlighterFactory language="OcamlP4"
implementationClass="com.reason.ide.highlight.OclSyntaxHighlighterFactory"/>

<!--
| Structure / folding / comments
-->
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/com/reason/ide/files/Ml4File.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.reason.ide.files;

import com.intellij.openapi.fileTypes.FileType;
import com.intellij.psi.FileViewProvider;
import com.reason.lang.ocaml.OclLanguage;
import org.jetbrains.annotations.NotNull;

public class Ml4File extends FileBase {
public Ml4File(@NotNull FileViewProvider viewProvider) {
super(viewProvider, OclLanguage.INSTANCE);
}

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

@Override
public String toString() {
return "OcamlP4 File";
}
}
41 changes: 41 additions & 0 deletions src/main/java/com/reason/ide/files/Ml4FileType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.reason.ide.files;

import com.intellij.openapi.fileTypes.LanguageFileType;
import com.reason.icons.Icons;
import com.reason.lang.ocaml.OclLanguage;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

public class Ml4FileType extends LanguageFileType {
public static final Ml4FileType INSTANCE = new Ml4FileType();

private Ml4FileType() {
super(OclLanguage.INSTANCE);
}

@NotNull
@Override
public String getName() {
return "OcamlP4 file";
}

@NotNull
@Override
public String getDescription() {
return "Ocaml preprocessor file";
}

@NotNull
@Override
public String getDefaultExtension() {
return "ml4";
}

@Nullable
@Override
public Icon getIcon() {
return Icons.OCL_FILE;
}
}
12 changes: 12 additions & 0 deletions src/main/java/com/reason/ide/files/Ml4FileTypeFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.reason.ide.files;

import com.intellij.openapi.fileTypes.FileTypeConsumer;
import com.intellij.openapi.fileTypes.FileTypeFactory;
import org.jetbrains.annotations.NotNull;

public class Ml4FileTypeFactory extends FileTypeFactory {
@Override
public void createFileTypes(@NotNull FileTypeConsumer fileTypeConsumer) {
fileTypeConsumer.consume(Ml4FileType.INSTANCE, "ml4");
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/reason/lang/ocaml/OclP4Language.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.reason.lang.ocaml;

import com.intellij.lang.Language;

public class OclP4Language extends Language {
public static final OclP4Language INSTANCE = new OclP4Language();

private OclP4Language() {
super("Ml4");
}
}
70 changes: 70 additions & 0 deletions src/main/java/com/reason/lang/ocaml/OclP4ParserDefinition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.reason.lang.ocaml;

import com.intellij.lang.ASTNode;
import com.intellij.lang.Language;
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.TokenType;
import com.intellij.psi.tree.IFileElementType;
import com.intellij.psi.tree.TokenSet;
import com.reason.ide.files.Ml4File;
import com.reason.lang.LexerAdapter;
import com.reason.lang.PsiElementFactory;
import org.jetbrains.annotations.NotNull;

public class OclP4ParserDefinition implements ParserDefinition {
private static final TokenSet WHITE_SPACES = TokenSet.create(TokenType.WHITE_SPACE);
private static final TokenSet COMMENTS = TokenSet.create(OclTypes.INSTANCE.COMMENT);
private static final TokenSet STRINGS = TokenSet.create(OclTypes.INSTANCE.STRING);

private static final IFileElementType FILE = new IFileElementType(Language.findInstance(OclP4Language.class));

@NotNull
@Override
public Lexer createLexer(Project project) {
return new LexerAdapter(OclTypes.INSTANCE);
}

@NotNull
public TokenSet getWhitespaceTokens() {
return WHITE_SPACES;
}

@NotNull
public TokenSet getCommentTokens() {
return COMMENTS;
}

@NotNull
public TokenSet getStringLiteralElements() {
return STRINGS;
}

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

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

public PsiFile createFile(FileViewProvider viewProvider) {
return new Ml4File(viewProvider);
}

public ParserDefinition.SpaceRequirements spaceExistanceTypeBetweenTokens(ASTNode left, ASTNode right) {
return ParserDefinition.SpaceRequirements.MAY;
}

@NotNull
public PsiElement createElement(ASTNode node) {
return PsiElementFactory.createElement(OclTypes.INSTANCE, node);
}
}

0 comments on commit c82275c

Please sign in to comment.