-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented a simple parser for Hamlet to enable language injections.
- Loading branch information
1 parent
385fe24
commit 761ee81
Showing
9 changed files
with
151 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
73 changes: 73 additions & 0 deletions
73
src/com/haskforce/yesod/shakespeare/hamlet/HamletParserDefinition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/com/haskforce/yesod/shakespeare/hamlet/psi/HamletFile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |