Browse Source

change assira.ui: split CSS grammar

Ranides Atterwim 1 năm trước cách đây
mục cha
commit
349d41d38c

+ 51 - 0
assira.ui/src/main/antlr4/imports/CssBorder.g4

@@ -0,0 +1,51 @@
+grammar CssBorder;
+
+import Primitives, CssColor, CssUnit;
+
+borderCSS
+    : borderCSS_Style WS color WS borderSize
+    | borderCSS_Style (WS borderSize)? (WS color)?
+    | color WS borderCSS_Style WS borderSize
+    | color (WS borderSize)? (WS borderCSS_Style)?
+    | borderSize WS borderCSS_Style WS color
+    | borderSize (WS color)? (WS borderCSS_Style)?
+    ;
+
+borderCSS_Style
+    : CSS2_none
+    | CSS2_BORDER_solid
+    | CSS2_BORDER_dotted
+    | CSS2_BORDER_dashed
+    | CSS2_BORDER_double
+    | CSS2_BORDER_groove
+    | CSS2_BORDER_ridge
+    | CSS2_BORDER_inset
+    | CSS2_BORDER_outset
+    ;
+
+borderSize
+    : borderSizeComponent #borderSize1
+    | borderSizeComponent WS borderSizeComponent #borderSize2
+    | borderSizeComponent WS borderSizeComponent WS borderSizeComponent WS borderSizeComponent #borderSize4
+    ;
+
+borderSizeComponent
+    : numberUnit
+    | CSS2_BORDER_thin
+    | CSS2_BORDER_medium
+    | CSS2_BORDER_thick
+    ;
+
+CSS2_none             : 'none';
+CSS2_BORDER_solid     : 'solid';
+CSS2_BORDER_dotted    : 'dotted';
+CSS2_BORDER_dashed    : 'dashed';
+CSS2_BORDER_double    : 'double';
+CSS2_BORDER_groove    : 'groove';
+CSS2_BORDER_ridge     : 'ridge';
+CSS2_BORDER_inset     : 'inset';
+CSS2_BORDER_outset    : 'outset';
+
+CSS2_BORDER_thin      : 'thin';
+CSS2_BORDER_medium    : 'medium';
+CSS2_BORDER_thick     : 'thick';

+ 57 - 0
assira.ui/src/main/antlr4/imports/CssColor.g4

@@ -0,0 +1,57 @@
+grammar CssColor;
+
+import Primitives;
+
+color
+    : colorHSL      #color_hsl
+    | colorHSLA     #color_hsla
+    | colorRGB      #color_rgb
+    | colorRGBA     #color_rgba
+    | colorHEX      #color_hex
+    ;
+
+colorHSL
+    : PREFIX_HSL LP h=componentHue CS s=componentSaturation CS l=componentLightness RP
+    ;
+
+colorHSLA
+    : (PREFIX_HSL|PREFIX_HSLA) LP h=componentHue CS s=componentSaturation CS l=componentLightness CS alpha=componentAlpha RP
+    ;
+
+colorRGB
+    : (PREFIX_RGB|PREFIX_RGBA) LP r=componentRGB CS g=componentRGB CS b=componentRGB RP
+    ;
+
+colorRGBA
+    : PREFIX_RGBA LP r=componentRGB CS g=componentRGB CS b=componentRGB CS alpha=componentAlpha RP
+    ;
+
+colorHEX
+    : codeHex
+    ;
+
+componentHue
+    : numberInt
+    ;
+
+componentSaturation
+    : numberPct
+    ;
+
+componentLightness
+    : numberPct
+    ;
+
+componentRGB
+    : numberInt
+    ;
+
+componentAlpha
+    : numberReal
+    | numberInt
+    ;
+
+PREFIX_RGB   : 'rgb'  CHAR_WS*;
+PREFIX_RGBA  : 'rgba' CHAR_WS*;
+PREFIX_HSL   : 'hsl'  CHAR_WS*;
+PREFIX_HSLA  : 'hsla' CHAR_WS*;

+ 65 - 0
assira.ui/src/main/antlr4/imports/CssUnit.g4

@@ -0,0 +1,65 @@
+grammar CssUnit;
+
+import Primitives;
+
+numberUnit:
+    number unit
+    ;
+
+unit
+    : unitAbs
+    | unitRel
+    ;
+
+unitAbs
+    : CSS2_UNIT_PX
+    | CSS2_UNIT_PT
+    | CSS2_UNIT_CM
+    | CSS2_UNIT_MM
+    | CSS2_UNIT_Q
+    | CSS2_UNIT_IN
+    | CSS2_UNIT_PC
+    | CSS2_UNIT_EM
+    | CSS2_UNIT_REM
+    ;
+
+unitRel
+    : CSS2_UNIT_EX
+    | CSS2_UNIT_LH
+    | CSS2_UNIT_RLH
+    | CSS2_UNIT_VB
+    | CSS2_UNIT_VI
+    | CSS2_UNIT_VW
+    | CSS2_UNIT_VH
+    | CSS2_UNIT_SVW
+    | CSS2_UNIT_SVH
+    | CSS2_UNIT_LVW
+    | CSS2_UNIT_LVH
+    | CSS2_UNIT_BVW
+    | CSS2_UNIT_BVH
+    ;
+
+CSS2_UNIT_PX : 'px';
+CSS2_UNIT_PT : 'pt';
+CSS2_UNIT_CM : 'cm';
+CSS2_UNIT_MM : 'mm';
+CSS2_UNIT_Q  : 'Q';
+CSS2_UNIT_IN : 'in';
+CSS2_UNIT_PC : 'pc';
+
+CSS2_UNIT_EM : 'em';
+CSS2_UNIT_REM: 'rem';
+CSS2_UNIT_EX : 'ex';
+CSS2_UNIT_LH : 'lh';
+CSS2_UNIT_RLH: 'rlh';
+CSS2_UNIT_VB : 'vb';
+CSS2_UNIT_VI : 'vi';
+CSS2_UNIT_VW : 'vw';
+CSS2_UNIT_VH : 'vh';
+CSS2_UNIT_SVW: 'svw';
+CSS2_UNIT_SVH: 'svh';
+CSS2_UNIT_LVW: 'lvw';
+CSS2_UNIT_LVH: 'lvh';
+CSS2_UNIT_BVW: 'bvw';
+CSS2_UNIT_BVH: 'bvh';
+

+ 1 - 165
assira.ui/src/main/antlr4/net/ranides/grammars/generated/CssGrammar.g4

@@ -1,6 +1,6 @@
 grammar CssGrammar;
 
-import Primitives;
+import Primitives, CssColor, CssUnit, CssBorder;
 
 root_colors
     : color (WS color)+ EOF    #color_list
@@ -19,27 +19,6 @@ border
     | borderSwingSimple
     ;
 
-borderCSS
-    : borderCSS_Style WS color WS borderSize
-    | borderCSS_Style (WS borderSize)? (WS color)?
-    | color WS borderCSS_Style WS borderSize
-    | color (WS borderSize)? (WS borderCSS_Style)?
-    | borderSize WS borderCSS_Style WS color
-    | borderSize (WS color)? (WS borderCSS_Style)?
-    ;
-
-borderCSS_Style
-    : CSS2_none
-    | CSS2_BORDER_solid
-    | CSS2_BORDER_dotted
-    | CSS2_BORDER_dashed
-    | CSS2_BORDER_double
-    | CSS2_BORDER_groove
-    | CSS2_BORDER_ridge
-    | CSS2_BORDER_inset
-    | CSS2_BORDER_outset
-    ;
-
 borderSwingSimple
     : borderSwingSimple_Style WS borderSwingSimple_Type (WS color)? (WS color)?
     | borderSwingSimple_Style WS borderSwingSimple_Type (WS color)? (WS color)?
@@ -61,146 +40,3 @@ SWING_BORDER_bevel: 'bevel';
 SWING_BORDER_bevel_soft: 'soft-bevel';
 SWING_BORDER_raised: 'raised';
 SWING_BORDER_lowered: 'lowered';
-
-borderSize
-    : borderSizeComponent #borderSize1
-    | borderSizeComponent WS borderSizeComponent #borderSize2
-    | borderSizeComponent WS borderSizeComponent WS borderSizeComponent WS borderSizeComponent #borderSize4
-    ;
-
-borderSizeComponent
-    : numberUnit
-    | CSS2_BORDER_thin
-    | CSS2_BORDER_medium
-    | CSS2_BORDER_thick
-    ;
-
-numberUnit:
-    number unit
-    ;
-
-unit
-    : unitAbs
-    | unitRel
-    ;
-
-unitAbs
-    : CSS2_UNIT_PX
-    | CSS2_UNIT_PT
-    | CSS2_UNIT_CM
-    | CSS2_UNIT_MM
-    | CSS2_UNIT_Q
-    | CSS2_UNIT_IN
-    | CSS2_UNIT_PC
-    | CSS2_UNIT_EM
-    | CSS2_UNIT_REM
-    ;
-
-unitRel
-    : CSS2_UNIT_EX
-    | CSS2_UNIT_LH
-    | CSS2_UNIT_RLH
-    | CSS2_UNIT_VB
-    | CSS2_UNIT_VI
-    | CSS2_UNIT_VW
-    | CSS2_UNIT_VH
-    | CSS2_UNIT_SVW
-    | CSS2_UNIT_SVH
-    | CSS2_UNIT_LVW
-    | CSS2_UNIT_LVH
-    | CSS2_UNIT_BVW
-    | CSS2_UNIT_BVH
-    ;
-
-color
-    : colorHSL      #color_hsl
-    | colorHSLA     #color_hsla
-    | colorRGB      #color_rgb
-    | colorRGBA     #color_rgba
-    | colorHEX      #color_hex
-    ;
-
-colorHSL
-    : PREFIX_HSL LP h=componentHue CS s=componentSaturation CS l=componentLightness RP
-    ;
-
-colorHSLA
-    : (PREFIX_HSL|PREFIX_HSLA) LP h=componentHue CS s=componentSaturation CS l=componentLightness CS alpha=componentAlpha RP
-    ;
-
-colorRGB
-    : (PREFIX_RGB|PREFIX_RGBA) LP r=componentRGB CS g=componentRGB CS b=componentRGB RP
-    ;
-
-colorRGBA
-    : PREFIX_RGBA LP r=componentRGB CS g=componentRGB CS b=componentRGB CS alpha=componentAlpha RP
-    ;
-
-colorHEX
-    : codeHex
-    ;
-
-componentHue
-    : numberInt
-    ;
-
-componentSaturation
-    : numberPct
-    ;
-
-componentLightness
-    : numberPct
-    ;
-
-componentRGB
-    : numberInt
-    ;
-
-componentAlpha
-    : numberReal
-    | numberInt
-    ;
-
-CSS2_none             : 'none';
-CSS2_BORDER_solid     : 'solid';
-CSS2_BORDER_dotted    : 'dotted';
-CSS2_BORDER_dashed    : 'dashed';
-CSS2_BORDER_double    : 'double';
-CSS2_BORDER_groove    : 'groove';
-CSS2_BORDER_ridge     : 'ridge';
-CSS2_BORDER_inset     : 'inset';
-CSS2_BORDER_outset    : 'outset';
-
-CSS2_BORDER_thin      : 'thin';
-CSS2_BORDER_medium    : 'medium';
-CSS2_BORDER_thick     : 'thick';
-
-CSS2_UNIT_PX : 'px';
-CSS2_UNIT_PT : 'pt';
-CSS2_UNIT_CM : 'cm';
-CSS2_UNIT_MM : 'mm';
-CSS2_UNIT_Q  : 'Q';
-CSS2_UNIT_IN : 'in';
-CSS2_UNIT_PC : 'pc';
-
-CSS2_UNIT_EM : 'em';
-CSS2_UNIT_REM: 'rem';
-CSS2_UNIT_EX : 'ex';
-CSS2_UNIT_LH : 'lh';
-CSS2_UNIT_RLH: 'rlh';
-CSS2_UNIT_VB : 'vb';
-CSS2_UNIT_VI : 'vi';
-CSS2_UNIT_VW : 'vw';
-CSS2_UNIT_VH : 'vh';
-CSS2_UNIT_SVW: 'svw';
-CSS2_UNIT_SVH: 'svh';
-CSS2_UNIT_LVW: 'lvw';
-CSS2_UNIT_LVH: 'lvh';
-CSS2_UNIT_BVW: 'bvw';
-CSS2_UNIT_BVH: 'bvh';
-
-
-PREFIX_RGB   : 'rgb'  CHAR_WS*;
-PREFIX_RGBA  : 'rgba' CHAR_WS*;
-PREFIX_HSL   : 'hsl'  CHAR_WS*;
-PREFIX_HSLA  : 'hsla' CHAR_WS*;

+ 16 - 2
assira.ui/src/test/java/net/ranides/assira/ui/primitives/CssParserTest.java

@@ -53,11 +53,25 @@ public class CssParserTest {
         .prepare();
 
     @Test
-    public void grammarInspection() throws IOException {
+    public void grammarInspectionGenerated() throws IOException {
         G4Parser gp = G4Parser.builder()
             .lexer(CssGrammarLexer.class)
             .parser(CssGrammarParser.class)
             .build();
+        grammarInspection(gp);
+    }
+
+    @Test
+    public void grammarInspectionInterpreted() throws IOException {
+        G4Parser gp = G4Parser.builder()
+            .sourceFile("src\\main\\antlr4\\net\\ranides\\grammars\\generated\\CssGrammar.g4")
+            .imports("src\\main\\antlr4\\imports")
+            .handler((message, error) -> System.out.println(message))
+            .build();
+        grammarInspection(gp);
+    }
+
+    public void grammarInspection(G4Parser gp) throws IOException {
 
         G4Node out = gp.parse("color", "rgba(15,20,30,0.75)");
 
@@ -73,7 +87,7 @@ public class CssParserTest {
             out.find("colorRGBA/@unknown").path();
         });
 
-        assertThrows(e -> e.getMessage().contains("There is no path 'unknown' inside node 'color/colorRGBA$0=rgba(15,20,30,0.75)'"), () -> {
+        assertThrows(e -> e.getMessage().contains("There is no path 'unknown' inside node 'color/colorRGBA$1=rgba(15,20,30,0.75)'"), () -> {
             out.child("colorRGBA").find("unknown").path();
         });
     }

+ 9 - 9
assira.ui/src/test/resources/grammars/CssColors.tree.txt

@@ -1,20 +1,20 @@
 color#color_rgba = 'rgba(15,20,30,0.75)'
-   colorRGBA$0 = 'rgba(15,20,30,0.75)'
+   colorRGBA$1 = 'rgba(15,20,30,0.75)'
       PREFIX_RGBA = 'rgba'
       LP = '('
-      componentRGB$0 = '15'
-         numberInt$0 = '15'
+      componentRGB$1 = '15'
+         numberInt$1 = '15'
             NUMBER_INT = '15'
       CS = ','
-      componentRGB$0 = '20'
-         numberInt$0 = '20'
+      componentRGB$1 = '20'
+         numberInt$1 = '20'
             NUMBER_INT = '20'
       CS = ','
-      componentRGB$0 = '30'
-         numberInt$0 = '30'
+      componentRGB$1 = '30'
+         numberInt$1 = '30'
             NUMBER_INT = '30'
       CS = ','
-      componentAlpha$0 = '0.75'
-         numberReal$0 = '0.75'
+      componentAlpha$1 = '0.75'
+         numberReal$1 = '0.75'
             NUMBER_REAL = '0.75'
       RP = ')'