Skip to content

Commit

Permalink
[ narc (#7) ] Finish lexer, initial parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
ice1000 committed Oct 9, 2019
1 parent 2a2adcb commit 2ad82fb
Show file tree
Hide file tree
Showing 16 changed files with 172 additions and 33 deletions.
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ charset = utf-8
end_of_line = lf
insert_final_newline = false
indent_style = tab
indent_size = tab
tab_width = 2

[gradle.properties]
Expand Down
31 changes: 19 additions & 12 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ val commitHash = kotlin.run {
output.trim()
}

val pluginComingVersion = "0.7.3"
val pluginComingVersion = "0.8.0"
val pluginVersion = if (isCI) "$pluginComingVersion-$commitHash" else pluginComingVersion
val packageName = "org.ice1000.tt"

Expand All @@ -36,7 +36,7 @@ plugins {
}

grammarKit {
grammarKitRelease = "1206783ca03dc80ce7806504e056f37378c331d3"
grammarKitRelease = "d0dbcb89a2e5cd90b9bdd517b2a69ab131f5fbf7"
}

fun fromToolbox(root: String, ide: String) = file(root)
Expand All @@ -60,6 +60,7 @@ allprojects { apply { plugin("org.jetbrains.grammarkit") } }
intellij {
updateSinceUntilBuild = false
instrumentCode = true
// downloadSources = true
val user = System.getProperty("user.name")
val os = System.getProperty("os.name")
val root = when {
Expand Down Expand Up @@ -173,15 +174,16 @@ fun grammar(name: String): Pair<GenerateParser, GenerateLexer> {
}
}

val (genMiniTTParser, genMiniTTLexer) = grammar("MiniTT")
val (genACoreParser, genACoreLexer) = grammar("ACore")
val (genMLPolyRParser, genMLPolyRLexer) = grammar("MLPolyR")
val (genRedPrlParser, genRedPrlLexer) = grammar("RedPrl")
val (genAgdaParser, genAgdaLexer) = grammar("Agda")
val (genCubicalTTParser, genCubicalTTLexer) = grammar("CubicalTT")
val (genYaccTTParser, genYaccTTLexer) = grammar("YaccTT")
val (genVoileParser, genVoileLexer) = grammar("Voile")
val (genMlangParser, genMlangLexer) = grammar("Mlang")
grammar("MiniTT")
grammar("ACore")
grammar("MLPolyR")
grammar("RedPrl")
grammar("Agda")
grammar("CubicalTT")
grammar("YaccTT")
grammar("Voile")
grammar("Narc")
grammar("Mlang")

fun utilities(name: String, job: LanguageUtilityGenerationTask.() -> Unit) = task<LanguageUtilityGenerationTask>(name) {
this.job()
Expand Down Expand Up @@ -234,6 +236,12 @@ utilities("genNarcUtility") {
languageName = "Narc"
exeName = "narc"
trimVersion = """version.removePrefix("narc").trim()"""
supportsParsing = true
highlightTokenPairs = listOf(
kw, id, paren, semi, brace, lc, fn,
"UNRESOLVED" to "IDENTIFIER",
"OPERATOR" to "OPERATION_SIGN",
"INACCESS" to "BRACES")
}

utilities("genVoileUtility") {
Expand All @@ -244,7 +252,6 @@ utilities("genVoileUtility") {
highlightTokenPairs = listOf(
kw, id, comma, paren, semi, brace, lc, fn,
"CONSTRUCTOR" to "FUNCTION_DECLARATION",
"VARIANT" to "FUNCTION_DECLARATION",
"UNRESOLVED" to "IDENTIFIER",
"OPERATOR" to "OPERATION_SIGN",
"BRACE2" to "BRACES")
Expand Down
5 changes: 1 addition & 4 deletions grammar/narc.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,9 @@ private declaration ::= clause | definition | data | codata

private identifier ::= IDENTIFIER
nameDecl ::= identifier {
implements=['com.intellij.psi.PsiNameIdentifierOwner']
mixin='org.ice1000.tt.psi.narc.NarcNameDeclMixin'
}
nameUsage ::= identifier {
extends=expr
mixin='org.ice1000.tt.psi.narc.NarcNameUsageMixin'
}

expr ::=
Expand All @@ -54,7 +51,7 @@ telescopicExpr ::= LPAREN nameDecl+ COLON expr RPAREN {
pin=3
}
dollarExpr ::= expr DOLLAR expr
litExpr ::= META | KW_TYPE
litExpr ::= META IDENTIFIER | KW_TYPE
appExpr ::= expr expr+
parenExpr ::= LPAREN expr RPAREN

Expand Down
61 changes: 61 additions & 0 deletions grammar/narc.flex
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.ice1000.tt.psi.narc;

import com.intellij.lexer.FlexLexer;
import com.intellij.psi.tree.IElementType;
import static org.ice1000.tt.psi.narc.NarcTokenType.*;
import static org.ice1000.tt.psi.narc.NarcTypes.*;

import static com.intellij.psi.TokenType.BAD_CHARACTER;
import static com.intellij.psi.TokenType.WHITE_SPACE;

%%

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

private int commentStart = 0;
private int commentDepth = 0;
%}

%public
%class NarcLexer
%implements FlexLexer
%function advance
%type IElementType
%unicode
%eof{ return;
%eof}

WHITE_SPACE=[\ \t\f\r\n]+
UNIVERSE = Type
IDENTIFIER=[a-zA-Z][a-zA-Z0-9\-'\\/]*
COMMENTS = \/\/[^\n\r]*

%%

"->" { return ARROW; }
= { return EQ; }
_ { return META; }
; { return SEMI; }
: { return COLON; }
\. { return DOT; }
\( { return LPAREN; }
\) { return RPAREN; }
\{ { return LBRACE; }
\} { return RBRACE; }
\$ { return DOLLAR; }
\|_ { return LINACCESS; }
_\| { return RINACCESS; }

data { return KW_DATA; }
clause { return KW_CLAUSE; }
codata { return KW_CODATA; }
definition { return KW_DEFINITION; }
projection { return KW_PROJECTION; }
constructor { return KW_CONSTRUCTOR; }
{UNIVERSE} { return KW_TYPE; }
{COMMENTS} { return LINE_COMMENT; }
{IDENTIFIER} { return IDENTIFIER; }
{WHITE_SPACE} { return WHITE_SPACE; }

[^] { return BAD_CHARACTER; }
2 changes: 1 addition & 1 deletion grammar/voile.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ lamExpr ::= LAM nameDecl+ DOT expr {
implements=['com.intellij.psi.PsiNameIdentifierOwner']
mixin='org.ice1000.tt.psi.voile.VoileLocalDeclMixin'
}
litExpr ::= META | VARIANT | CONS | BOT | KW_TYPE | KW_NOCASES
litExpr ::= META | CONS | KW_TYPE | KW_NOCASES
appExpr ::= expr expr+
parenExpr ::= LPAREN expr RPAREN

Expand Down
5 changes: 0 additions & 5 deletions grammar/voile.flex
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ _ { return META; }
, { return COMMA; }
\^ { return UP; }
\. { return DOT; }
\! { return BOT; }
\* { return SIG; }
\( { return LPAREN; }
\) { return RPAREN; }
Expand All @@ -68,10 +67,6 @@ whatever { return KW_NOCASES; }
{COMMENTS} { return LINE_COMMENT; }
{IDENTIFIER} { return IDENTIFIER; }
@{IDENTIFIER} { return CONS; }
'{IDENTIFIER} { return VARIANT; }
#{IDENTIFIER} { return IMPLICIT_IDENTIFIER; }
{WHITE_SPACE} { return WHITE_SPACE; }

[^] { return BAD_CHARACTER; }


3 changes: 2 additions & 1 deletion res/META-INF/change-notes.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
0.7.3<br/>
0.8.0<br/>
<ul>
<li>Parse and syntax highlight Narc files</li>
</ul>
0.7.2<br/>
<ul>
Expand Down
1 change: 1 addition & 0 deletions res/META-INF/description.html
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ <h3>Features (listed for each supported language):</h3><br/>
<li><a href="https://docs.rs/nar">Narc</a>:<br/>
<ul>
<li>Executable (narc) management</li>
<li>Commenter, brace matcher</li>
</ul>
</li>
</ul>
5 changes: 4 additions & 1 deletion res/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<!--Voile-->
<configurationType implementation="org.ice1000.tt.execution.VoileRunConfigurationType"/>
<runConfigurationProducer implementation="org.ice1000.tt.execution.VoileRunConfigurationProducer"/>
<lang.commenter language="Voile" implementationClass="org.ice1000.tt.editing.voile.VoileCommenter"/>
<lang.commenter language="Voile" implementationClass="org.ice1000.tt.editing.CxxLineCommenter"/>
<lang.parserDefinition language="Voile" implementationClass="org.ice1000.tt.psi.voile.VoileGeneratedParserDefinition"/>
<lang.syntaxHighlighterFactory language="Voile" implementationClass="org.ice1000.tt.editing.voile.VoileHighlighterFactory"/>
<stubElementTypeHolder class="org.ice1000.tt.psi.voile.VoileTypes"/>
Expand Down Expand Up @@ -191,6 +191,9 @@
<!--Narc-->
<configurationType implementation="org.ice1000.tt.execution.NarcRunConfigurationType"/>
<runConfigurationProducer implementation="org.ice1000.tt.execution.NarcRunConfigurationProducer"/>
<lang.commenter language="Narc" implementationClass="org.ice1000.tt.editing.CxxLineCommenter"/>
<lang.braceMatcher language="Narc" implementationClass="org.ice1000.tt.editing.narc.NarcBraceMatcher"/>
<lang.syntaxHighlighterFactory language="Narc" implementationClass="org.ice1000.tt.editing.narc.NarcHighlighterFactory"/>

<projectConfigurable
groupId="language"
Expand Down
7 changes: 5 additions & 2 deletions src/org/ice1000/tt/editing/editing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,17 @@ abstract class TTCommenter : Commenter {
override fun getBlockCommentSuffix(): String? = null
}

open class CxxLineCommenter : TTCommenter() {
override fun getLineCommentPrefix() = "// "
}

class HaskellCommenter : TTCommenter() {
override fun getBlockCommentPrefix() = "{-"
override fun getBlockCommentSuffix() = "-}"
override fun getLineCommentPrefix() = "--"
}

class CxxCommenter : TTCommenter() {
override fun getLineCommentPrefix() = "// "
class CxxCommenter : CxxLineCommenter() {
override fun getBlockCommentPrefix() = "/*"
override fun getBlockCommentSuffix() = "*/"
}
Expand Down
16 changes: 16 additions & 0 deletions src/org/ice1000/tt/editing/narc/editing.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.ice1000.tt.editing.narc

import com.intellij.lang.BracePair
import org.ice1000.tt.editing.TTBraceMatcher
import org.ice1000.tt.psi.narc.NarcTypes

class NarcBraceMatcher : TTBraceMatcher() {
private companion object Pairs {
private val PAIRS = arrayOf(
BracePair(NarcTypes.LBRACE, NarcTypes.RBRACE, false),
BracePair(NarcTypes.LINACCESS, NarcTypes.RINACCESS, false),
BracePair(NarcTypes.LPAREN, NarcTypes.RPAREN, false))
}

override fun getPairs() = PAIRS
}
37 changes: 37 additions & 0 deletions src/org/ice1000/tt/editing/narc/lex-highlight.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.ice1000.tt.editing.narc

import com.intellij.openapi.editor.colors.TextAttributesKey
import com.intellij.psi.tree.IElementType
import org.ice1000.tt.psi.narc.NarcTokenType
import org.ice1000.tt.psi.narc.NarcTypes

object NarcHighlighter : NarcGeneratedSyntaxHighlighter() {
@JvmField
val KEYWORDS = listOf(
NarcTypes.KW_CLAUSE,
NarcTypes.KW_DEFINITION,
NarcTypes.KW_CODATA,
NarcTypes.KW_DATA,
NarcTypes.KW_CONSTRUCTOR,
NarcTypes.KW_PROJECTION,
NarcTypes.KW_TYPE
)
@JvmField
val OPERATORS = listOf(
NarcTypes.DOLLAR,
NarcTypes.ARROW,
NarcTypes.DOT
)

override fun getTokenHighlights(type: IElementType?): Array<TextAttributesKey> = when (type) {
NarcTypes.SEMI -> SEMICOLON_KEY
NarcTypes.IDENTIFIER -> IDENTIFIER_KEY
NarcTokenType.LINE_COMMENT -> LINE_COMMENT_KEY
NarcTypes.LPAREN, NarcTypes.RPAREN -> PAREN_KEY
NarcTypes.LBRACE, NarcTypes.RBRACE -> BRACE_KEY
NarcTypes.LINACCESS, NarcTypes.RINACCESS -> INACCESS_KEY
in OPERATORS -> OPERATOR_KEY
in KEYWORDS -> KEYWORD_KEY
else -> emptyArray()
}
}
9 changes: 4 additions & 5 deletions src/org/ice1000/tt/editing/voile/editing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ import com.intellij.patterns.PlatformPatterns
import com.intellij.psi.PsiElement
import com.intellij.psi.tree.TokenSet
import icons.TTIcons
import org.ice1000.tt.editing.*
import org.ice1000.tt.editing.SimpleProvider
import org.ice1000.tt.editing.TTBraceMatcher
import org.ice1000.tt.editing.TTFindUsagesProvider
import org.ice1000.tt.editing.makeKeywordsCompletion
import org.ice1000.tt.psi.redprl.redPrlLexer
import org.ice1000.tt.psi.voile.*

class VoileCommenter : TTCommenter() {
override fun getLineCommentPrefix() = "// "
}

class VoileBraceMatcher : TTBraceMatcher() {
private companion object Pairs {
private val PAIRS = arrayOf(
Expand Down
2 changes: 1 addition & 1 deletion src/org/ice1000/tt/editing/voile/lex-highlight.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ object VoileHighlighter : VoileGeneratedSyntaxHighlighter() {

override fun getTokenHighlights(type: IElementType?): Array<TextAttributesKey> = when (type) {
VoileTypes.CONS -> CONSTRUCTOR_KEY
VoileTypes.VARIANT -> VARIANT_KEY
VoileTypes.SEMI -> SEMICOLON_KEY
VoileTypes.COMMA -> COMMA_KEY
VoileTypes.IDENTIFIER -> IDENTIFIER_KEY
Expand All @@ -41,6 +40,7 @@ object VoileHighlighter : VoileGeneratedSyntaxHighlighter() {
else -> emptyArray()
}
}

class VoileColorSettingsPage : VoileGeneratedColorSettingsPage() {
private companion object DescriptorHolder {
private val DESCRIPTORS = arrayOf(
Expand Down
19 changes: 19 additions & 0 deletions src/org/ice1000/tt/psi/narc/parser.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.ice1000.tt.psi.narc

import com.intellij.openapi.project.Project
import com.intellij.psi.PsiFileFactory
import com.intellij.psi.tree.IElementType
import com.intellij.psi.tree.TokenSet
import org.ice1000.tt.NarcLanguage

class NarcTokenType(debugName: String) : IElementType(debugName, NarcLanguage.INSTANCE) {
companion object Builtin {
@JvmField val LINE_COMMENT = NarcTokenType("line comment")
@JvmField val COMMENTS = TokenSet.create(LINE_COMMENT)
@JvmField val IDENTIFIERS = TokenSet.create(NarcTypes.IDENTIFIER)

fun fromText(text: String, project: Project) = PsiFileFactory.getInstance(project).createFileFromText(NarcLanguage.INSTANCE, text).firstChild
fun createDef(text: String, project: Project) = fromText(text, project) as? NarcDefinition
}
}

1 change: 0 additions & 1 deletion src/org/ice1000/tt/psi/voile/parser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,3 @@ class VoileTokenType(debugName: String) : IElementType(debugName, VoileLanguage.
fun createNameUsage(text: String, project: Project) = createExpr(text, project) as? VoileNameUsage
}
}

0 comments on commit 2ad82fb

Please sign in to comment.