Skip to content

Commit c19d1ce

Browse files
committed
Started working on StringTemplateCSS.
1 parent cdf7406 commit c19d1ce

7 files changed

Lines changed: 892 additions & 4 deletions

File tree

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*
2+
* [LICENSE]
3+
*/
4+
grammar StringTemplateCSS;
5+
6+
css
7+
: selectorCombination '{' property+ '}' EOF
8+
;
9+
10+
selectorCombination
11+
: selector+
12+
;
13+
14+
selector
15+
: ('.' | '#')? (Identifier | StringLiteral) (COLONCOLON ('before' | 'after'))?
16+
;
17+
18+
property
19+
: Identifier ':' StringLiteral ';'
20+
;
21+
22+
StringLiteral
23+
: '"' StringCharacters? '"' | '\'' StringCharacters? '\''
24+
;
25+
26+
fragment
27+
StringCharacters
28+
: StringCharacter+
29+
;
30+
31+
fragment
32+
StringCharacter
33+
: ~["\\]
34+
| EscapeSequence
35+
;
36+
37+
// §3.10.6 Escape Sequences for Character and String Literals
38+
39+
fragment
40+
EscapeSequence
41+
: '\\' [btnfr"'\\]
42+
| OctalEscape
43+
| UnicodeEscape // This is not in the spec but prevents having to preprocess the input
44+
;
45+
46+
fragment
47+
OctalEscape
48+
: '\\' OctalDigit
49+
| '\\' OctalDigit OctalDigit
50+
| '\\' ZeroToThree OctalDigit OctalDigit
51+
;
52+
53+
fragment
54+
OctalDigit
55+
: [0-7]
56+
;
57+
58+
fragment
59+
ZeroToThree
60+
: [0-3]
61+
;
62+
63+
// This is not in the spec but prevents having to preprocess the input
64+
fragment
65+
UnicodeEscape
66+
: '\\' 'u' HexDigit HexDigit HexDigit HexDigit
67+
;
68+
69+
fragment
70+
HexDigit
71+
: [0-9a-fA-F]
72+
;
73+
74+
LBRACE : '{';
75+
RBRACE : '}';
76+
LBRACK : '[';
77+
RBRACK : ']';
78+
SEMI : ';';
79+
COMMA : ',';
80+
DOT : '.';
81+
82+
GT : '>';
83+
LT : '<';
84+
TILDE : '~';
85+
COLON : ':';
86+
MUL : '*';
87+
COLONCOLON : '::';
88+
89+
// §3.8 Identifiers (must appear after all keywords in the grammar)
90+
91+
Identifier
92+
: JavaLetter JavaLetterOrDigit*
93+
;
94+
95+
fragment
96+
JavaLetter
97+
: [a-zA-Z$_] // these are the "java letters" below 0xFF
98+
| // covers all characters above 0xFF which are not a surrogate
99+
~[\u0000-\u00FF\uD800-\uDBFF]
100+
{Character.isJavaIdentifierStart(_input.LA(-1))}?
101+
| // covers UTF-16 surrogate pairs encodings for U+10000 to U+10FFFF
102+
[\uD800-\uDBFF] [\uDC00-\uDFFF]
103+
{Character.isJavaIdentifierStart(Character.toCodePoint((char)_input.LA(-2), (char)_input.LA(-1)))}?
104+
;
105+
106+
fragment
107+
JavaLetterOrDigit
108+
: [a-zA-Z0-9$_] // these are the "java letters or digits" below 0xFF
109+
| // covers all characters above 0xFF which are not a surrogate
110+
~[\u0000-\u00FF\uD800-\uDBFF]
111+
{Character.isJavaIdentifierPart(_input.LA(-1))}?
112+
| // covers UTF-16 surrogate pairs encodings for U+10000 to U+10FFFF
113+
[\uD800-\uDBFF] [\uDC00-\uDFFF]
114+
{Character.isJavaIdentifierPart(Character.toCodePoint((char)_input.LA(-2), (char)_input.LA(-1)))}?
115+
;
116+
117+
//
118+
// Additional symbols not defined in the lexical specification
119+
//
120+
121+
AT : '@';
122+
ELLIPSIS : '...';
123+
124+
//
125+
// Whitespace and comments
126+
//
127+
128+
WS : [ \t\r\n\u000C]+ -> skip
129+
;
130+
131+
COMMENT
132+
: '/*' .*? '*/' -> skip
133+
;
134+
135+
LINE_COMMENT
136+
: '//' ~[\r\n]* -> skip
137+
;
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
devdel
3+
4+
Copyright (C) 2002-today Jose San Leandro Armendariz
5+
chous@acm-sl.org
6+
7+
This library is free software; you can redistribute it and/or
8+
modify it under the terms of the GNU General Public
9+
License as published by the Free Software Foundation; either
10+
version 2 of the License, or any later version.
11+
12+
This library is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
General Public License for more details.
16+
17+
You should have received a copy of the GNU General Public
18+
License along with this library; if not, write to the Free Software
19+
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20+
21+
Thanks to ACM S.L. for distributing this library under the GPL license.
22+
Contact info: jose.sanleandro@acm-sl.com
23+
24+
******************************************************************************
25+
*
26+
* Filename: StringTemplateCSSHelper.java
27+
*
28+
* Author: Jose San Leandro Armendariz
29+
*
30+
* Description:
31+
*
32+
* Date: 2015/03/02
33+
* Time: 17:13
34+
*
35+
*/
36+
package org.acmsl.javacss.css;
37+
38+
/*
39+
* Importing JetBrains annotations.
40+
*/
41+
import org.acmsl.javacss.css.parser.StringTemplateCSSLexer;
42+
import org.acmsl.javacss.css.parser.StringTemplateCSSParser;
43+
import org.antlr.v4.runtime.CommonTokenStream;
44+
import org.antlr.v4.runtime.ANTLRInputStream;
45+
import org.antlr.v4.runtime.tree.ParseTree;
46+
import org.antlr.v4.runtime.tree.xpath.XPath;
47+
48+
/*
49+
* Importing checkthread.org annotations.
50+
*/
51+
import org.checkthread.annotations.ThreadSafe;
52+
53+
import java.util.ArrayList;
54+
import java.util.Collection;
55+
import java.util.List;
56+
57+
/**
58+
*
59+
* @author <a href="mailto:queryj@acm-sl.org">Jose San Leandro</a>
60+
* @since 3.0
61+
* Created: 2015/03/02 17:13
62+
*/
63+
@ThreadSafe
64+
public class StringTemplateCSSHelper
65+
{
66+
private final String input;
67+
private List<String> selectors;
68+
69+
public StringTemplateCSSHelper(final String input)
70+
{
71+
this.input = input;
72+
}
73+
74+
public List<String> getSelectors()
75+
{
76+
if (this.selectors == null)
77+
{
78+
initialize(this.input);
79+
}
80+
81+
return this.selectors;
82+
}
83+
84+
protected void initialize(String input)
85+
{
86+
StringTemplateCSSLexer lexer = new StringTemplateCSSLexer(new ANTLRInputStream(input));
87+
88+
CommonTokenStream tokens = new CommonTokenStream(lexer);
89+
90+
StringTemplateCSSParser parser = new StringTemplateCSSParser(tokens);
91+
92+
ParseTree tree = parser.css();
93+
94+
Collection<ParseTree> selectorEntries = XPath.findAll(tree, "//selectorCombination", parser);
95+
96+
this.selectors = new ArrayList<String>(selectorEntries.size());
97+
98+
for (ParseTree selectorEntry : selectorEntries)
99+
{
100+
this.selectors.add(selectorEntry.getText());
101+
}
102+
}
103+
}

src/main/java/org/acmsl/javacss/java8/parser/ASTHelper.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,7 @@
4040
*/
4141
import org.acmsl.javacss.java8.parser.Java8Parser.CompilationUnitContext;
4242
import org.acmsl.javacss.java8.parser.Java8Parser.ImportDeclarationContext;
43-
import org.acmsl.javacss.java8.parser.Java8Parser.SingleTypeImportDeclarationContext;
44-
import org.acmsl.javacss.java8.parser.Java8Parser.TypeNameContext;
4543
import org.antlr.v4.runtime.ANTLRInputStream;
46-
import org.antlr.v4.runtime.CommonToken;
4744
import org.antlr.v4.runtime.CommonTokenStream;
4845
import org.antlr.v4.runtime.tree.ParseTree;
4946

0 commit comments

Comments
 (0)