Skip to content

Commit

Permalink
fixed grammar, prepared for argslist
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Vanusanik committed Feb 3, 2023
1 parent de256af commit 2165225
Show file tree
Hide file tree
Showing 11 changed files with 403 additions and 172 deletions.
338 changes: 177 additions & 161 deletions src/main/gen/com/en_circle/slt/plugin/lisp/LispLexer.java

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions src/main/gen/com/en_circle/slt/plugin/lisp/LispParser.java

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public class SltStaticHighlighter extends SyntaxHighlighterBase {
SyntaxHighlighterBase.fillMap(colors, SltHighlighterColors.SUGAR,
LispTypes.FUNCTION, LispTypes.UNINTERN, LispTypes.REFERENCE_SET, LispTypes.REFERENCE_LABEL,
LispTypes.TEST_SUCCESS, LispTypes.TEST_FALURE, LispTypes.EVAL_VALUE, LispTypes.ARRAY_START,
LispTypes.PATHNAME_INDICATOR, LispTypes.STRUCTURE_TOKEN);
LispTypes.PATHNAME_INDICATOR, LispTypes.STRUCTURE_TOKEN, LispTypes.UNQUOTE, LispTypes.UNQUOTE_SPLICE,
LispTypes.BACKQUOTE, LispTypes.QUOTE);
}

@Override
Expand All @@ -37,4 +38,5 @@ public class SltStaticHighlighter extends SyntaxHighlighterBase {
public TextAttributesKey @NotNull [] getTokenHighlights(IElementType tokenType) {
return SyntaxHighlighterBase.pack(colors.get(tokenType));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.en_circle.slt.plugin.highlights.SltHighlighterColors;
import com.en_circle.slt.plugin.highlights.annotators.SymbolAnnotator.AnnotationResult;
import com.en_circle.slt.plugin.lisp.LispParserUtil;
import com.en_circle.slt.plugin.lisp.LispParserUtil.QuoteState;
import com.en_circle.slt.plugin.lisp.psi.LispSymbol;
import com.en_circle.slt.plugin.lisp.psi.LispVisitor;
import com.en_circle.slt.plugin.services.lisp.LispEnvironmentService;
Expand Down Expand Up @@ -58,10 +59,13 @@ public void apply(@NotNull PsiFile file, AnnotationResult annotationResult, @Not

@Override
public void visitSymbol(@NotNull LispSymbol element) {
String text = element.getText();
String packageName = LispParserUtil.getPackage(element);
SymbolState state = LispEnvironmentService.getInstance(element.getProject()).refreshSymbolFromServer(packageName, text);
setHighlight(element, text, holder, state);
QuoteState quoteState = LispParserUtil.getQuoteState(element);
if (quoteState == QuoteState.NO_STATE || quoteState == QuoteState.ERROR_STATE) {
String text = element.getText();
String packageName = LispParserUtil.getPackage(element);
SymbolState state = LispEnvironmentService.getInstance(element.getProject()).refreshSymbolFromServer(packageName, text);
setHighlight(element, text, holder, state);
}
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/en_circle/slt/plugin/lisp/Lisp.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ sexpr ::= (enhancement* datum) | comment

comment ::= LINE_COMMENT | BLOCK_COMMENT

enhancement ::= REFERENCE_SET | TEST_SUCCESS | COMMA | BACKQUOTE | QUOTE | FUNCTION
enhancement ::= REFERENCE_SET | TEST_SUCCESS | UNQUOTE | UNQUOTE_SPLICE | BACKQUOTE | QUOTE | FUNCTION

datum ::= tested | evaled | pathname | UNDEFINED_SEQUENCE | BIT_ARRAY | CHARACTER | REFERENCE_LABEL
| number | real_pair
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/com/en_circle/slt/plugin/lisp/Lisp.flex
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ IElementType processBuffer(boolean unget) {

%}

%state UNQUOTE_STATE
%state LINE_COMMENT
%state STRING
%state STRING_ESCAPE
Expand Down Expand Up @@ -61,7 +62,7 @@ TERMINATING_MACRO_CHAR=[\"'\(\),;`]
<YYINITIAL> [;] { yybegin(LINE_COMMENT); }
<YYINITIAL> [\"] { yybegin(STRING); }
<YYINITIAL> [`] { yybegin(YYINITIAL); return LispTypes.BACKQUOTE; }
<YYINITIAL> [,] { yybegin(YYINITIAL); return LispTypes.COMMA; }
<YYINITIAL> [,] { yybegin(UNQUOTE_STATE); }
<YYINITIAL> [#] { yybegin(SHARPSIGN); }

<YYINITIAL> {WHITESPACE_CHARACTER}+ { yybegin(YYINITIAL); return TokenType.WHITE_SPACE; }
Expand All @@ -71,6 +72,12 @@ TERMINATING_MACRO_CHAR=[\"'\(\),;`]
<YYINITIAL> <<EOF>> { return null; }
<YYINITIAL> [^] { yybegin(YYINITIAL); return TokenType.ERROR_ELEMENT; }

<UNQUOTE_STATE> {
[@] { yybegin(YYINITIAL); return LispTypes.UNQUOTE_SPLICE; }
<<EOF>> { yybegin(YYINITIAL); return LispTypes.UNQUOTE; }
[^] { yybegin(YYINITIAL); yypushback(1); return LispTypes.UNQUOTE; }
}

<LINE_COMMENT> {
[\r\n] { yybegin(YYINITIAL); return LispTypes.LINE_COMMENT; }
<<EOF>> { yybegin(YYINITIAL); return LispTypes.LINE_COMMENT; }
Expand Down
190 changes: 190 additions & 0 deletions src/main/java/com/en_circle/slt/plugin/lisp/LispParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiWhiteSpace;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.containers.Stack;

import java.util.List;
import java.util.function.Function;
Expand Down Expand Up @@ -139,4 +141,192 @@ public static LispList getIfHead(PsiElement element) {
}
return null;
}

public static QuoteState getQuoteState(PsiElement o) {
if (o instanceof LispList list) {
return getQuoteState(list);
} else {
QuoteState quoteState = QuoteState.NO_STATE;

LispSexpr parent = PsiTreeUtil.getParentOfType(o, LispSexpr.class);
if (parent == null) {
return quoteState;
}

LispList plist = PsiTreeUtil.getParentOfType(o, LispList.class);

if (plist == null) {
return quoteState;
}

quoteState = getQuoteState(plist);
if (o instanceof PsiWhiteSpace) {
return quoteState;
}
return getQuoteState(parent, quoteState);
}
}

public static QuoteState getQuoteState(LispList o) {
QuoteState quoteState = QuoteState.NO_STATE;
LispSexpr sexpr = PsiTreeUtil.getParentOfType(o, LispSexpr.class);
if (sexpr == null) {
return quoteState;
}
PsiElement parent = sexpr.getParent();
if (parent == null) {
return quoteState;
} else {
if (parent instanceof LispList list) {
QuoteState parentState = getQuoteState(list);
quoteState = combineQuoteStates(quoteState, parentState);
}
}

return getQuoteState(sexpr, quoteState);
}

private static QuoteState getQuoteState(LispSexpr self, QuoteState quoteState) {
LispSexpr superExpr = PsiTreeUtil.getParentOfType(self, LispSexpr.class);
if (superExpr != null) {
LispDatum datum = superExpr.getDatum();
if (datum != null) {
LispList plist = datum.getList();
if (plist != null) {
LispSexpr first = plist.getSexprList().get(0);
if (first != self) {
if (first.getDatum() != null && first.getDatum().getCompoundSymbol() != null) {
LispSymbol symbol = first.getDatum().getCompoundSymbol().getSymbol();
QuoteState symbolState = getQuoteStateForSymbol(symbol.getName());
if (symbolState != QuoteState.NO_STATE) {
boolean checked = false;
for (int i=1; i<plist.getSexprList().size(); i++) {
LispSexpr other = plist.getSexprList().get(i);
if (other.getDatum() != null) {
if (other == self) {
if (!checked) {
checked = true;
}
}
}
}
if (checked) {
quoteState = combineQuoteStates(symbolState, quoteState);
} else {
quoteState = QuoteState.ERROR_STATE;
}
}
}
}
}
}
}

for (LispEnhancement enhancement : self.getEnhancementList()) {
String text = enhancement.getText();
if (text.equals("`")) {
quoteState = combineQuoteStates(QuoteState.BACKQUOTE, quoteState);
} else if (text.equals(",")) {
quoteState = combineQuoteStates(QuoteState.UNQUOTE, quoteState);
} else if (text.equals(",@")) {
quoteState = combineQuoteStates(QuoteState.UNQUOTE_SPLICE, quoteState);
}
}
return quoteState;
}

private static QuoteState getQuoteStateForSymbol(String name) {
if (name == null)
return QuoteState.NO_STATE;

if (name.equalsIgnoreCase("quote"))
return QuoteState.QUOTE;
if (name.equalsIgnoreCase("unquote"))
return QuoteState.UNQUOTE;
if (name.equalsIgnoreCase("unquote-splice"))
return QuoteState.UNQUOTE_SPLICE;
if (name.equalsIgnoreCase("backquote"))
return QuoteState.BACKQUOTE;

return QuoteState.NO_STATE;
}

private static QuoteState combineQuoteStates(QuoteState quoteState, QuoteState parentState) {
if (parentState == QuoteState.ERROR_STATE)
return QuoteState.ERROR_STATE;
if (parentState == QuoteState.QUOTE)
return QuoteState.QUOTE;
if (parentState == QuoteState.BACKQUOTE) {
if (quoteState == QuoteState.NO_STATE)
return QuoteState.BACKQUOTE;
if (quoteState == QuoteState.UNQUOTE || quoteState == QuoteState.UNQUOTE_SPLICE)
return QuoteState.NO_STATE;
}
if (parentState == QuoteState.NO_STATE) {
if (quoteState == QuoteState.UNQUOTE || quoteState == QuoteState.UNQUOTE_SPLICE)
return QuoteState.ERROR_STATE;
if (quoteState == QuoteState.BACKQUOTE)
return QuoteState.BACKQUOTE;
if (quoteState == QuoteState.QUOTE)
return QuoteState.QUOTE;
}
if (quoteState == QuoteState.UNQUOTE || quoteState == QuoteState.UNQUOTE_SPLICE)
return QuoteState.NO_STATE;
return QuoteState.ERROR_STATE;
}

public static QuoteState getQuoteStateUnfinished(PsiElement o) {
assert (o.getParent() instanceof PsiFile);

Stack<PsiElement> elementStack = new Stack<>();
PsiElement e = o;
int parenthesis = 0;

while (e.getPrevSibling() != null && !(e.getPrevSibling() instanceof LispToplevel)) {
ASTNode node = e.getNode();
if (parenthesis == 0) {
if (node.getElementType() == LispTypes.RPAREN) {
++parenthesis;
} else {
elementStack.push(e);
}
} else {
if (node.getElementType() == LispTypes.LPAREN) {
--parenthesis;
}
}

e = e.getPrevSibling();
}

QuoteState quoteState = QuoteState.NO_STATE;
boolean firstElementCheck = false;
for (PsiElement element : elementStack) {
ASTNode node = element.getNode();
if (node.getElementType() == LispTypes.QUOTE) {
quoteState = combineQuoteStates(QuoteState.QUOTE, quoteState);
} else if (node.getElementType() == LispTypes.UNQUOTE) {
quoteState = combineQuoteStates(QuoteState.UNQUOTE, quoteState);
} else if (node.getElementType() == LispTypes.UNQUOTE_SPLICE) {
quoteState = combineQuoteStates(QuoteState.UNQUOTE_SPLICE, quoteState);
} else if (node.getElementType() == LispTypes.BACKQUOTE) {
quoteState = combineQuoteStates(QuoteState.BACKQUOTE, quoteState);
} else {
if (node.getElementType() == LispTypes.LPAREN) {
firstElementCheck = true;
}

if (node.getElementType() == LispTypes.SYMBOL_TOKEN && firstElementCheck && node != o.getNode()) {
firstElementCheck = false;
QuoteState state = getQuoteStateForSymbol(node.getText());
quoteState = combineQuoteStates(state, quoteState);
}
}
}
return quoteState;
}

public enum QuoteState {
BACKQUOTE, QUOTE, UNQUOTE, UNQUOTE_SPLICE, NO_STATE, ERROR_STATE
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.en_circle.slt.plugin.swank.SlimeRequest;
import com.en_circle.slt.plugin.swank.SwankClient;
import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer;
import com.intellij.codeInsight.hints.ParameterHintsPassFactory;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
Expand Down Expand Up @@ -165,6 +166,7 @@ private boolean doStart() throws Exception {
listener.onPostStart();
}

ParameterHintsPassFactory.forceHintsUpdateOnNextPass();
DaemonCodeAnalyzer.getInstance(project).restart();
} finally {
starting = false;
Expand Down
9 changes: 8 additions & 1 deletion src/main/lisp/swank/swank.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ format suitable for Emacs."
(when references
(sort references #'string< :key (lambda (x) (first x))))))

(defslimefun operator-arglist-list (name package)
(ignore-errors
(let ((args (arglist (parse-symbol name (guess-buffer-package package)))))
(cond ((eq args :not-available) nil)
(t args)))))

(export 'slt-eval)
(export 'compile-string-region-slt)
(export 'find-reference-prefix)
(export 'find-reference-prefix)
(export 'operator-arglist-list)
1 change: 1 addition & 0 deletions src/main/resources/templates/en_US/initscript.cl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
(:use :cl)
(:export +slt-interpret+))

(in-package :slt)
(defconstant +slt-interpret+ ~interpret~
"Defines current slt interpret. For SBCL the value is :sbcl")

Expand Down

0 comments on commit 2165225

Please sign in to comment.