Skip to content

Commit

Permalink
Implemented a simple parser for Hamlet to enable language injections.
Browse files Browse the repository at this point in the history
  • Loading branch information
carymrobbins committed Jan 9, 2015
1 parent 385fe24 commit 761ee81
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 22 deletions.
1 change: 1 addition & 0 deletions META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@
<!-- Hamlet support -->
<fileTypeFactory implementation="com.haskforce.yesod.shakespeare.hamlet.HamletFileTypeFactory"/>
<lang.syntaxHighlighterFactory key="Hamlet" implementationClass="com.haskforce.yesod.shakespeare.hamlet.highlighting.HamletSyntaxHighlighterFactory"/>
<lang.parserDefinition language="Hamlet" implementationClass="com.haskforce.yesod.shakespeare.hamlet.HamletParserDefinition" />

<!-- External builder support -->
<!-- Note that the classpath is needed for plugin deployment to export a zip archive. -->
Expand Down
21 changes: 2 additions & 19 deletions src/com/haskforce/cabal/CabalParser.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,6 @@
package com.haskforce.cabal;

import com.intellij.lang.ASTNode;
import com.intellij.lang.PsiBuilder;
import com.intellij.lang.PsiParser;
import com.intellij.psi.tree.IElementType;
import org.jetbrains.annotations.NotNull;
import com.haskforce.utils.parser.SimplePsiParser;

/**
* Simple parser which basically returns the result from the syntax highlighting lexer.
*/
public class CabalParser implements PsiParser {
@NotNull
@Override
public ASTNode parse(IElementType root, PsiBuilder builder) {
PsiBuilder.Marker marker = builder.mark();
while (!builder.eof()) {
builder.advanceLexer();
}
marker.done(root);
return builder.getTreeBuilt();
}
public class CabalParser extends SimplePsiParser {
}
23 changes: 23 additions & 0 deletions src/com/haskforce/utils/parser/SimplePsiParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.haskforce.utils.parser;

import com.intellij.lang.ASTNode;
import com.intellij.lang.PsiBuilder;
import com.intellij.lang.PsiParser;
import com.intellij.psi.tree.IElementType;
import org.jetbrains.annotations.NotNull;

/**
* Simple parser which basically returns the result from the syntax highlighting lexer.
*/
public class SimplePsiParser implements PsiParser {
@NotNull
@Override
public ASTNode parse(IElementType root, PsiBuilder builder) {
PsiBuilder.Marker marker = builder.mark();
while (!builder.eof()) {
builder.advanceLexer();
}
marker.done(root);
return builder.getTreeBuilt();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.haskforce.yesod.shakespeare.hamlet;

import com.intellij.icons.AllIcons;
import com.intellij.openapi.fileTypes.LanguageFileType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -35,6 +34,6 @@ public String getDefaultExtension() {
@Nullable
@Override
public Icon getIcon() {
return AllIcons.FileTypes.Html;
return HamletIcons.FILE;
}
}
9 changes: 9 additions & 0 deletions src/com/haskforce/yesod/shakespeare/hamlet/HamletIcons.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.haskforce.yesod.shakespeare.hamlet;

import com.intellij.icons.AllIcons;

import javax.swing.*;

public class HamletIcons {
public static final Icon FILE = AllIcons.FileTypes.Html;
}
6 changes: 6 additions & 0 deletions src/com/haskforce/yesod/shakespeare/hamlet/HamletParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.haskforce.yesod.shakespeare.hamlet;

import com.haskforce.utils.parser.SimplePsiParser;

public class HamletParser extends SimplePsiParser {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.haskforce.yesod.shakespeare.hamlet;

import com.haskforce.yesod.shakespeare.hamlet.highlighting.HamletSyntaxHighlightingLexer;
import com.haskforce.yesod.shakespeare.hamlet.psi.HamletFile;
import com.haskforce.yesod.shakespeare.hamlet.psi.HamletTypes;
import com.intellij.extapi.psi.ASTWrapperPsiElement;
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 org.jetbrains.annotations.NotNull;

public class HamletParserDefinition implements ParserDefinition {
public static final IFileElementType FILE = new IFileElementType(Language.findInstance(HamletLanguage.class));

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

@Override
public PsiParser createParser(Project project) {
return new HamletParser();
}

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

@NotNull
@Override
public TokenSet getWhitespaceTokens() {
return TokenSet.create(TokenType.WHITE_SPACE);
}

@NotNull
@Override
public TokenSet getCommentTokens() {
return TokenSet.create(HamletTypes.LINE_COMMENT);
}

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

@NotNull
@Override
public PsiElement createElement(ASTNode node) {
return new ASTWrapperPsiElement(node);
}

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

@Override
public SpaceRequirements spaceExistanceTypeBetweenTokens(ASTNode left, ASTNode right) {
return SpaceRequirements.MAY;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
public class HamletSyntaxHighlightingLexer extends MergingLexerAdapter {
public static final TokenSet mergeMe = TokenSet.create(HamletTypes.HASKELL_CODE);

HamletSyntaxHighlightingLexer() {
public HamletSyntaxHighlightingLexer() {
super(new FlexAdapter(new _HamletSyntaxHighlightingLexer()), mergeMe);
}
}
35 changes: 35 additions & 0 deletions src/com/haskforce/yesod/shakespeare/hamlet/psi/HamletFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.haskforce.yesod.shakespeare.hamlet.psi;

import com.haskforce.yesod.shakespeare.hamlet.HamletFileType;
import com.haskforce.yesod.shakespeare.hamlet.HamletIcons;
import com.haskforce.yesod.shakespeare.hamlet.HamletLanguage;
import com.intellij.extapi.psi.PsiFileBase;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.psi.FileViewProvider;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

public class HamletFile extends PsiFileBase {
public HamletFile(@NotNull FileViewProvider viewProvider) {
super(viewProvider, HamletLanguage.INSTANCE);
}

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

@Override
public String toString() {
return "Hamlet File";
}

@Nullable
@Override
public Icon getIcon(int flags) {
return HamletIcons.FILE;
}
}

0 comments on commit 761ee81

Please sign in to comment.