Skip to content

Commit

Permalink
css3
Browse files Browse the repository at this point in the history
  • Loading branch information
teverett committed Apr 14, 2017
1 parent 0943bd9 commit bde53d1
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 0 deletions.
120 changes: 120 additions & 0 deletions css3/css3.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
grammar css3;

stylesheet
: importRule* (nested | ruleset)+
;

importRule
: ('@import' | '@include') STRING
;

nested
: '@' nest '{' properties? nested* '}'
;

nest
: IDENT IDENT* pseudo*
;

ruleset
: selectors '{' properties? '}'
;

selectors
: selector (',' selector)*
;

selector
: elem selectorOperation* attrib* pseudo?
;

selectorOperation
: selectop? elem
;

selectop
: '>'
| '+'
;

properties
: declaration (';' declaration?)*
;

elem
: IDENT
| '#' IDENT
| '.' IDENT
;

pseudo
: (':'|'::') IDENT
| (':'|'::') function
;

attrib
: '[' IDENT (attribRelate (STRING | IDENT))? ']'
;

attribRelate
: '='
| '~='
| '|='
;

declaration
: IDENT ':' args
;

args
: expr (','? expr)*
;

expr
: (NUM unit?)
| IDENT
| COLOR
| STRING
| function
;

unit
: ('%'|'px'|'cm'|'mm'|'in'|'pt'|'pc'|'em'|'ex'|'deg'|'rad'|'grad'|'ms'|'s'|'hz'|'khz')
;

function
: IDENT '(' args? ')'
;

IDENT
: ('_' | 'a'..'z'| 'A'..'Z' | '\u0100'..'\ufffe' )
('_' | '-' | 'a'..'z'| 'A'..'Z' | '\u0100'..'\ufffe' | '0'..'9')*
| '-' ('_' | 'a'..'z'| 'A'..'Z' | '\u0100'..'\ufffe' )
('_' | '-' | 'a'..'z'| 'A'..'Z' | '\u0100'..'\ufffe' | '0'..'9')*
;

STRING
: '"' (~('"'|'\n'|'\r'))* '"'
| '\'' (~('\''|'\n'|'\r'))* '\''
;

NUM
: '-' (('0'..'9')* '.')? ('0'..'9')+
| (('0'..'9')* '.')? ('0'..'9')+
;

COLOR
: '#' ('0'..'9'|'a'..'f'|'A'..'F')+
;

SL_COMMENT
: '//' (~('\n'|'\r'))* ('\n'|'\r'('\n')?) ->skip
;

COMMENT
: '/*' .* '*/' ->skip
;

WS : ( ' ' | '\t' | '\r' | '\n' | '\f' )+ ->skip
;

9 changes: 9 additions & 0 deletions css3/examples/example1.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
body {
color: #000000;
}
h1 {
color: #ff0000;
}
p {
color: #00ff00;
}
7 changes: 7 additions & 0 deletions css3/examples/example2.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
body {
font-family: Arial, Helvetica, sans-serif;
}
h1 {
font-family: "Times New Roman", Times, serif;
}

55 changes: 55 additions & 0 deletions css3/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>CSS3</artifactId>
<packaging>jar</packaging>
<name>CSS3 grammar</name>
<parent>
<groupId>com.antlr.grammarsv4</groupId>
<artifactId>grammarsv4</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<build>
<plugins>
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>${antlr.version}</version>
<configuration>
<sourceDirectory>${basedir}</sourceDirectory>
<grammars>css3.g4</grammars>
<visitor>true</visitor>
<listener>true</listener>
</configuration>
<executions>
<execution>
<goals>
<goal>antlr4</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.khubla.antlr</groupId>
<artifactId>antlr4test-maven-plugin</artifactId>
<version>${antlr4test-maven-plugin.version}</version>
<configuration>
<verbose>false</verbose>
<showTree>false</showTree>
<entryPoint>stylesheet</entryPoint>
<grammarName>css3</grammarName>
<packageName></packageName>
<exampleFiles>examples/</exampleFiles>
</configuration>
<executions>
<execution>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
5 changes: 5 additions & 0 deletions css3/readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
trihus Tue Jul 1, 2008 13:43
This CSS parser handles valid CSS syntax. It will also accept property values not defined by CSS without giving an error. It has been debugged with ANTLRWorks and tested on a number of CSS files. (You must comment out language=CSharp for it to work with ANTLRWorks). I don't think it handles the full syntax of @import or @include.

Forward ported from Antlr3 to Antlr4 by Tom Everett. April 2017.

1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<module>cpp</module>
<module>creole</module>
<!-- C# runtime is not supported. <module>csharp</module> -->
<module>css3</module>
<module>csv</module>
<module>datetime</module>
<module>dcm</module>
Expand Down

0 comments on commit bde53d1

Please sign in to comment.