Skip to content
This repository has been archived by the owner on May 11, 2021. It is now read-only.

Commit

Permalink
bnf/flex rules tweaks, working folding builder
Browse files Browse the repository at this point in the history
  • Loading branch information
hsz committed Jan 27, 2015
1 parent 5e778d6 commit d7a8d04
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 78 deletions.
53 changes: 24 additions & 29 deletions resources/bnf/Latex.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@
COMMA = ","
COLON = ":"
ASTERISK = "*"
SLASH = '\'
LINE_BREAK = '\\'

IDENTIFIER_END = '\end'
IDENTIFIER_BEGIN = '\begin'
IDENTIFIER = "regexp:\\\p{Alpha}+"

INSTRUCTION = "regexp:\\[a-zA-Z]+"
IDENTIFIER = "regexp:[a-zA-Z]+"
ARGUMENT = "regexp:\b[\p{L}\w ]+\b|\\\W"
ARGUMENT = "regexp:\b([^\]\}\),\\])+\b"
COMMENT = "regexp:%.*"

SPECIAL = "regexp:[\S]"
SPECIAL = "regexp:[\S]|\\."
CRLF = "regexp:[\s\r\n]+"
]

Expand All @@ -42,33 +42,28 @@


latexFile ::= item_ *
private item_ ::= !<<eof>> expr *

private expr ::= text_expr
| group_expr +
| argument_expr
| instruction_expr
| special_expr
private item_ ::= !<<eof>> expr * { pin = 1 }
private expr ::= section
| instruction
| TEXT
| SPECIAL
| CRLF
private expr_inside ::= argument_expr
| group_expr +
| instruction_expr
| special_expr
private instruction_ ::= INSTRUCTION_BEGIN
| INSTRUCTION_END
| INSTRUCTION
| LINE_BREAK

section ::= instruction_begin expr * instruction_end { pin = 1 }
instruction ::= IDENTIFIER ASTERISK ? argument_group * { pin = 1 }
instruction_begin ::= IDENTIFIER_BEGIN expr_list_brace
instruction_end ::= IDENTIFIER_END expr_list_brace
argument_group ::= expr_list_bracket | expr_list_brace | expr_list_paren

private argument_expr ::= ( special_expr * ( ARGUMENT | instruction ) special_expr * ) * { pin(".*") = 2 }

INSTRUCTION_BEGIN ::= "\\begin"
INSTRUCTION_END ::= "\\end"
TEXT ::= ( ARGUMENT special_expr ? ) +

private argument_expr ::= (ARGUMENT | IDENTIFIER) (special_expr + ( ARGUMENT | IDENTIFIER | instruction_expr | CRLF ) ) * { pin(".*") = 1 }
private group_expr ::= expr_list_bracket | expr_list_brace | expr_list_paren
instruction_expr ::= instruction_ ASTERISK ? group_expr *
private text_expr ::= ( special_expr * ARGUMENT special_expr * CRLF * ) +
private special_expr ::= SPECIAL | COLON | COMMA | LINE_BREAK
private special_expr ::= SPECIAL | COLON | COMMA | LINE_BREAK | CRLF

private meta expr_list_bracket ::= LBRACKET <<sequence expr_inside special_expr>> ? RBRACKET { pin(".*") = 1 }
private meta expr_list_brace ::= LBRACE <<sequence expr_inside special_expr>> ? RBRACE { pin(".*") = 1 }
private meta expr_list_paren ::= LPAREN <<sequence expr_inside special_expr>> ? RPAREN { pin(".*") = 1 }
private meta sequence ::= <<p>> + (<<q>> <<p>>) * { pin(".*") = 1 }
private meta expr_list_bracket ::= LBRACKET <<sequence argument_expr special_expr>> ? RBRACKET { pin(".*") = 1 }
private meta expr_list_brace ::= LBRACE <<sequence argument_expr special_expr>> ? RBRACE { pin(".*") = 1 }
private meta expr_list_paren ::= LPAREN <<sequence argument_expr special_expr>> ? RPAREN { pin(".*") = 1 }
private meta sequence ::= <<p>> (<<q>> <<p>>) * { pin(".*") = 1 }
14 changes: 3 additions & 11 deletions src/mobi/hsz/idea/latex/lang/LatexFoldingBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import com.intellij.lang.folding.FoldingBuilderEx;
import com.intellij.lang.folding.FoldingDescriptor;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.intellij.psi.search.PsiElementProcessor;
import com.intellij.psi.tree.IElementType;
Expand Down Expand Up @@ -59,20 +58,13 @@ public FoldingDescriptor[] buildFoldRegions(@NotNull PsiElement root, @NotNull D

final List<FoldingDescriptor> result = ContainerUtil.newArrayList();

final List<PsiElement> begins = ContainerUtil.newArrayList();

if (!quick) {
PsiTreeUtil.processElements(file, new PsiElementProcessor() {
@Override
public boolean execute(@NotNull PsiElement element) {
IElementType type = element.getNode().getElementType();
if (type.equals(LatexTypes.INSTRUCTION_BEGIN)) {
begins.add(element);
} else if (type.equals(LatexTypes.INSTRUCTION_END)) {
PsiElement last = begins.get(begins.size() - 1);
TextRange range = new TextRange(last.getTextRange().getStartOffset(), element.getTextRange().getEndOffset());
result.add(new FoldingDescriptor(last, range));
begins.remove(last);
if (type.equals(LatexTypes.SECTION)) {
result.add(new FoldingDescriptor(element.getNode(), element.getNode().getTextRange()));
}
return true;
}
Expand All @@ -85,7 +77,7 @@ public boolean execute(@NotNull PsiElement element) {
@Nullable
@Override
public String getPlaceholderText(@NotNull ASTNode node) {
return node.getTreeParent().getText() + LatexBundle.message("folding.placeholder");
return node.getFirstChildNode().getText() + LatexBundle.message("folding.placeholder");
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/mobi/hsz/idea/latex/lang/LatexParserDefinition.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class LatexParserDefinition implements ParserDefinition {

/** Latex instruction started with \ */
public static final TokenSet INSTRUCTIONS = TokenSet.create(
LatexTypes.INSTRUCTION, LatexTypes.INSTRUCTION_BEGIN, LatexTypes.INSTRUCTION_END
LatexTypes.IDENTIFIER, LatexTypes.IDENTIFIER_BEGIN, LatexTypes.IDENTIFIER_END
);

/** Latex instruction's argument */
Expand Down
10 changes: 5 additions & 5 deletions src/mobi/hsz/idea/latex/lexer/Latex.flex
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ EOL = "\r"|"\n"|"\r\n"
LINE_WS = [\ \t\f]
WHITE_SPACE = ({LINE_WS}|{EOL})+

INSTRUCTION = \\[a-zA-Z]+
IDENTIFIER = \\[a-zA-Z]+
COMMENT = %.*
ARGUMENT = [^\(\)\{\}\[\]\\,]
TEXT = [^\(\)\{\}\[\]\\\%\ \t\f\r\n]|"\\\%"|("\\"{SPECIAL})
Expand Down Expand Up @@ -59,8 +59,8 @@ SPECIAL = "$"|"&"|"#"|"_"|"~"|"^"|"\\"


<YYINITIAL> {WHITE_SPACE}+ { return WHITE_SPACE; }
<YYINITIAL> "\\begin" { return INSTRUCTION_BEGIN; }
<YYINITIAL> "\\end" { return INSTRUCTION_END; }
<YYINITIAL> {INSTRUCTION} { return INSTRUCTION; }
<YYINITIAL> "\\begin" { return IDENTIFIER_BEGIN; }
<YYINITIAL> "\\end" { return IDENTIFIER_END; }
<YYINITIAL> {IDENTIFIER} { return IDENTIFIER; }
<YYINITIAL> {COMMENT} { return COMMENT; }
<YYINITIAL> {TEXT}+ { return TEXT; }
<YYINITIAL> {TEXT}+ { return ARGUMENT; }
60 changes: 28 additions & 32 deletions src/mobi/hsz/idea/latex/lexer/LatexLexer.java

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

0 comments on commit d7a8d04

Please sign in to comment.