Skip to content

Commit

Permalink
Merge pull request #27 from antlr/highlighter-refacto
Browse files Browse the repository at this point in the history
Highlighter refactoring
  • Loading branch information
bjansen authored Jun 20, 2020
2 parents 1e4b5bb + 403b925 commit 0b5b12a
Show file tree
Hide file tree
Showing 64 changed files with 1,307 additions and 2,001 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ language: java
jdk:
- openjdk8
env:
- IDEA_VERSION=IC-15.0.6
- IDEA_VERSION=IC-2016.3.8
- IDEA_VERSION=IC-2017.3.6
- IDEA_VERSION=IC-2018.3.3
- IDEA_VERSION=IC-2019.3
- IDEA_VERSION=IC-2020.1

Expand Down
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
}

plugins {
id "org.jetbrains.intellij" version "0.4.15"
id "org.jetbrains.intellij" version "0.4.21"
}

apply plugin: 'idea'
Expand Down Expand Up @@ -44,7 +44,8 @@ dependencies {
exclude group:'com.ibm.icu', module:'icu4j'
}
implementation "org.antlr:antlr4-intellij-adaptor:0.1"
testImplementation group: 'junit', name: 'junit', version: '4.11'
testImplementation group: 'junit', name: 'junit', version: '4.13'
testImplementation group: 'org.mockito', name: 'mockito-core', version: '3.3.3'
}

generateGrammarSource {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ pluginVersion=0.8-SNAPSHOT
ideaVersion=2020.1

# The version of ANTLR v4 that will be used to generate the parser
antlr4Version=4.7.2
antlr4Version=4.8-1
9 changes: 7 additions & 2 deletions src/main/antlr/STGLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ WRAP : 'wrap' ;
ANCHOR : 'anchor' ;
SEPARATOR : 'separator' ;

ID : NameStartChar NameChar* ;
ID : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'-'|'_')* ;

// -----------------------------------
// Grammar specific fragments
Expand All @@ -111,23 +111,28 @@ fragment RPct : '%>' ;
fragment LDAngle : LShift ;
fragment RDAngle : RShift ;

ERRCHAR : . -> channel (HIDDEN) ;

mode STRING_MODE;

STRING_ESC : '\\"' {setText(getText()+"\"");} -> more ;
STRING_NL : '\n' -> more ; // match but it should be an error
STRING : '"' -> popMode ;
TRING_TEXT : . -> more;
EOF_STRING : EOF -> popMode ;
STRING_TEXT : . -> more;

mode BIGSTRING_MODE;

BIGSTRING_ESC : '\\>' -> more ;
BIGSTRING : '>>' -> popMode ;
EOF_BIGSTRING : EOF -> popMode ;
BIGSTRING_TEXT : . -> more;

mode BIGSTRING_NO_NL_MODE;

BIGSTRING_NO_NL_ESC : '\\%' -> more;
BIGSTRING_NO_NL : '%>' -> popMode ;
EOF_BIGSTRING_NO_NL : EOF -> popMode ;
BIGSTRING_NO_NL_TEXT: . -> more;


12 changes: 7 additions & 5 deletions src/main/antlr/STGParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,16 @@ template
: ( AT ID DOT ID LPAREN RPAREN
| ID LPAREN formalArgs? RPAREN
)
TMPL_ASSIGN
( STRING // "..."
| BIGSTRING // <<...>>
| BIGSTRING_NO_NL // <%...%>
)
TMPL_ASSIGN templateContent
| ID TMPL_ASSIGN ID // alias one template to another
;

templateContent
: STRING // "..."
| BIGSTRING // <<...>>
| BIGSTRING_NO_NL // <%...%>
;

formalArgs
: formalArg ( COMMA formalArg )*
;
Expand Down
129 changes: 129 additions & 0 deletions src/main/antlr/STLexer.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/* [The "BSD license"]
* Copyright (c) 2011-2014 Terence Parr
* Copyright (c) 2015 Gerald Rosenberg
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/* Antlr grammar for StringTemplate v4.
*
* Modified 2015.06.16 gbr
* -- update for compatibility with Antlr v4.5
* -- use imported standard fragments
*/

lexer grammar STLexer;

options {
superClass = LexerAdaptor ;
}

import LexBasic; // Standard set of fragments

channels {
OFF_CHANNEL // non-default channel for whitespace and comments
}

// -----------------------------------
// default mode = Outside

TMPL_COMMENT : TmplComment -> channel(OFF_CHANNEL) ;

HORZ_WS : Hws+ -> channel(OFF_CHANNEL) ;
VERT_WS : Vws+ -> channel(OFF_CHANNEL) ;

ESCAPE : . { isLDelim() }? EscSeq . { isRDelim() }? ; // self contained
LDELIM : . { isLDelim() }? -> mode(Inside) ; // switch mode to inside
RBRACE : RBrace { endsSubTemplate() }? ; // conditional switch to inside

TEXT : . { adjText(); } ; // have to handle weird terminals


// -----------------------------------
mode Inside ;

INS_HORZ_WS : Hws+ -> type(HORZ_WS), channel(OFF_CHANNEL) ;
INS_VERT_WS : Vws+ -> type(VERT_WS), channel(OFF_CHANNEL) ;

LBRACE : LBrace { startSubTemplate(); } ;
RDELIM : . { isRDelim() }? -> mode(DEFAULT_MODE) ;

STRING : DQuoteLiteral ;

IF : 'if' ;
ELSEIF : 'elseif' ;
ELSE : 'else' ;
ENDIF : 'endif' ;
SUPER : 'super' ;
END : '@end' ;

TRUE : True ;
FALSE : False ;

INS_ID : NameStartChar NameChar* -> type(ID);

AT : At ;
ELLIPSIS : Ellipsis ;
DOT : Dot ;
COMMA : Comma ;
COLON : Colon ;
SEMI : Semi ;
AND : And ;
OR : Or ;
LPAREN : LParen ;
RPAREN : RParen ;
LBRACK : LBrack ;
RBRACK : RBrack ;
EQUALS : Equal ;
BANG : Bang ;

INS_EOF : EOF;

// -----------------------------------
// Unknown content in mode Inside
INS_ERR_CHAR : . -> channel(HIDDEN) ;


// -----------------------------------
mode SubTemplate ;

SUB_HORZ_WS : Hws+ -> type(HORZ_WS), channel(OFF_CHANNEL) ;
SUB_VERT_WS : Vws+ -> type(VERT_WS), channel(OFF_CHANNEL) ;

ID : NameStartChar NameChar* ;
SUB_COMMA : Comma -> type(COMMA) ;
PIPE : Pipe -> mode(DEFAULT_MODE) ;


// -----------------------------------
// Grammar specific fragments

fragment TmplComment : LTmplMark .*? RTmplMark ;

fragment LTmplMark : . { isLTmplComment() }? Bang ;
fragment RTmplMark : Bang . { isRTmplComment() }? ;

SUB_EOF : EOF;

SUB_ERR_CHAR : . -> channel(HIDDEN) ;
58 changes: 0 additions & 58 deletions src/main/antlr/STLexer.tokens

This file was deleted.

41 changes: 25 additions & 16 deletions src/main/antlr/STParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ element

singleElement
: exprTag
| TEXT+
| (TEXT | ESCAPE)+
;

compoundElement
Expand All @@ -63,7 +63,7 @@ compoundElement
;

exprTag
: LDELIM expr ( SEMI exprOptions )? RDELIM
: LDELIM mapExpr ( SEMI exprOptions )? RDELIM
;

region
Expand All @@ -73,7 +73,7 @@ region
;

subtemplate
: LCURLY ( ID ( COMMA ID )* PIPE )? elements RCURLY
: LBRACE ( ID ( COMMA ID )* PIPE )? elements RBRACE
;

ifstat
Expand All @@ -94,12 +94,12 @@ andConditional

notConditional
: BANG notConditional
| expr
| memberExpr
;

notConditionalExpr
: ID ( DOT ID
| DOT LPAREN expr RPAREN
| DOT LPAREN mapExpr RPAREN
)*
;

Expand All @@ -111,14 +111,22 @@ option
: ID ( EQUALS expr )?
;

expr: expr COLON mapTemplateRef // chain template applications together
| expr DOT ID
| expr DOT LPAREN expr RPAREN
| includeExpr
| expr (COMMA expr)* COLON mapTemplateRef // template application
| expr COLON mapTemplateRef // alternating templates
( COMMA mapTemplateRef )*
| primary
expr
: memberExpr ( COLON mapTemplateRef )?
;

// more complicated than necessary to avoid backtracking,
// which ruins error handling
mapExpr
: memberExpr ( ( COMMA memberExpr )+ COLON mapTemplateRef )?
( COLON mapTemplateRef ( COMMA mapTemplateRef )* )*
;

memberExpr
: includeExpr
( DOT ID
| DOT LPAREN mapExpr RPAREN
)*
;

// expr:template(args) apply template to expr
Expand All @@ -127,15 +135,16 @@ expr: expr COLON mapTemplateRef // chain template applications together
mapTemplateRef
: ID LPAREN args? RPAREN
| subtemplate
| LPAREN expr RPAREN LPAREN argExprList? RPAREN
| LPAREN mapExpr RPAREN LPAREN argExprList? RPAREN
;

includeExpr
: ID LPAREN expr? RPAREN
: ID LPAREN mapExpr? RPAREN
| SUPER DOT ID LPAREN args? RPAREN
| ID LPAREN args? RPAREN
| AT SUPER DOT ID LPAREN RPAREN
| AT ID LPAREN RPAREN
| primary
;

primary
Expand All @@ -146,7 +155,7 @@ primary
| subtemplate
| list
| LPAREN conditional RPAREN
| LPAREN expr RPAREN ( LPAREN argExprList? RPAREN )?
| LPAREN mapExpr RPAREN ( LPAREN argExprList? RPAREN )?
;

list
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/antlr/jetbrains/st4plugin/Icons.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@

public class Icons {
public static final Icon STG_FILE = IconLoader.getIcon("/icons/st.png");
public static final Icon BIGSTRING = IconLoader.getIcon("/icons/bigstring.png");
public static final Icon BIGSTRING_NONL = IconLoader.getIcon("/icons/bigstring-nonl.png");
public static final Icon STRING = IconLoader.getIcon("/icons/string.png");
public static final Icon DICT = IconLoader.getIcon("/icons/dict.png");
}
Loading

0 comments on commit 0b5b12a

Please sign in to comment.