|
16 | 16 | */
|
17 | 17 | package com.inet.sass.function;
|
18 | 18 |
|
| 19 | +import static com.inet.sass.parser.SCSSLexicalUnit.SAC_CENTIMETER; |
| 20 | +import static com.inet.sass.parser.SCSSLexicalUnit.SAC_DEGREE; |
| 21 | +import static com.inet.sass.parser.SCSSLexicalUnit.SAC_DIMENSION; |
| 22 | +import static com.inet.sass.parser.SCSSLexicalUnit.SAC_EM; |
| 23 | +import static com.inet.sass.parser.SCSSLexicalUnit.SAC_EX; |
| 24 | +import static com.inet.sass.parser.SCSSLexicalUnit.SAC_FUNCTION; |
| 25 | +import static com.inet.sass.parser.SCSSLexicalUnit.SAC_GRADIAN; |
| 26 | +import static com.inet.sass.parser.SCSSLexicalUnit.SAC_HERTZ; |
| 27 | +import static com.inet.sass.parser.SCSSLexicalUnit.SAC_IDENT; |
| 28 | +import static com.inet.sass.parser.SCSSLexicalUnit.SAC_INCH; |
| 29 | +import static com.inet.sass.parser.SCSSLexicalUnit.SAC_INTEGER; |
| 30 | +import static com.inet.sass.parser.SCSSLexicalUnit.SAC_KILOHERTZ; |
| 31 | +import static com.inet.sass.parser.SCSSLexicalUnit.SAC_LEM; |
| 32 | +import static com.inet.sass.parser.SCSSLexicalUnit.SAC_MILLIMETER; |
| 33 | +import static com.inet.sass.parser.SCSSLexicalUnit.SAC_MILLISECOND; |
| 34 | +import static com.inet.sass.parser.SCSSLexicalUnit.SAC_PERCENTAGE; |
| 35 | +import static com.inet.sass.parser.SCSSLexicalUnit.SAC_PICA; |
| 36 | +import static com.inet.sass.parser.SCSSLexicalUnit.SAC_PIXEL; |
| 37 | +import static com.inet.sass.parser.SCSSLexicalUnit.SAC_POINT; |
| 38 | +import static com.inet.sass.parser.SCSSLexicalUnit.SAC_RADIAN; |
| 39 | +import static com.inet.sass.parser.SCSSLexicalUnit.SAC_REAL; |
| 40 | +import static com.inet.sass.parser.SCSSLexicalUnit.SAC_REM; |
| 41 | +import static com.inet.sass.parser.SCSSLexicalUnit.SAC_RGBCOLOR; |
| 42 | +import static com.inet.sass.parser.SCSSLexicalUnit.SAC_SECOND; |
| 43 | +import static com.inet.sass.parser.SCSSLexicalUnit.SCSS_LIST; |
| 44 | +import static com.inet.sass.parser.SCSSLexicalUnit.SCSS_NULL; |
| 45 | + |
19 | 46 | import com.inet.sass.ScssContext;
|
20 | 47 | import com.inet.sass.parser.FormalArgumentList;
|
21 | 48 | import com.inet.sass.parser.LexicalUnitImpl;
|
22 | 49 | import com.inet.sass.parser.SassList;
|
23 |
| -import com.inet.sass.parser.SassListItem; |
24 | 50 | import com.inet.sass.parser.SassList.Separator;
|
| 51 | +import com.inet.sass.parser.SassListItem; |
25 | 52 | import com.inet.sass.util.ColorUtil;
|
26 | 53 |
|
27 | 54 | class TypeOfFunctionGenerator extends AbstractFunctionGenerator {
|
28 | 55 |
|
29 | 56 | private static String[] argumentNames = { "value" };
|
30 | 57 |
|
31 | 58 | TypeOfFunctionGenerator() {
|
32 |
| - super(createArgumentList(argumentNames, false), "type-of"); |
| 59 | + super( createArgumentList( argumentNames, false ), "type-of" ); |
33 | 60 | }
|
34 | 61 |
|
35 | 62 | @Override
|
36 |
| - protected SassListItem computeForArgumentList(ScssContext context, |
37 |
| - LexicalUnitImpl function, FormalArgumentList actualArguments) { |
38 |
| - SassListItem param = getParam(actualArguments, 0); |
| 63 | + protected SassListItem computeForArgumentList( ScssContext context, LexicalUnitImpl function, FormalArgumentList actualArguments ) { |
| 64 | + SassListItem param = getParam( actualArguments, 0 ); |
39 | 65 | String type = "string";
|
40 | 66 |
|
41 |
| - if (param instanceof SassList) { |
42 |
| - type = "list"; |
43 |
| - SassList list = (SassList)param; |
44 |
| - if( list.size() > 0 ) { |
45 |
| - // we need only to check the first entry, the other has the parser already checked |
46 |
| - SassListItem first = list.get( 0 ); |
47 |
| - if( first.getClass() == SassList.class && ((SassList)first).getSeparator() == Separator.COLON ) { |
48 |
| - type = "map"; |
| 67 | + switch( param.getItemType() ) { |
| 68 | + case SCSS_LIST: |
| 69 | + type = "list"; |
| 70 | + SassList list = (SassList)param; |
| 71 | + if( list.size() > 0 ) { |
| 72 | + // we need only to check the first entry, the other has the parser already checked |
| 73 | + SassListItem first = list.get( 0 ); |
| 74 | + if( first.getClass() == SassList.class && ((SassList)first).getSeparator() == Separator.COLON ) { |
| 75 | + type = "map"; |
| 76 | + } |
49 | 77 | }
|
50 |
| - } |
51 |
| - } else if (param instanceof LexicalUnitImpl) { |
52 |
| - LexicalUnitImpl unit = (LexicalUnitImpl) param; |
53 |
| - if (unit.getItemType() == LexicalUnitImpl.SCSS_NULL) { |
| 78 | + break; |
| 79 | + case SCSS_NULL: |
54 | 80 | type = "null";
|
55 |
| - } else if (isNumber(unit)) { |
| 81 | + break; |
| 82 | + case SAC_INTEGER: |
| 83 | + case SAC_REAL: |
| 84 | + case SAC_EM: |
| 85 | + case SAC_LEM: |
| 86 | + case SAC_REM: |
| 87 | + case SAC_EX: |
| 88 | + case SAC_PIXEL: |
| 89 | + case SAC_INCH: |
| 90 | + case SAC_CENTIMETER: |
| 91 | + case SAC_MILLIMETER: |
| 92 | + case SAC_POINT: |
| 93 | + case SAC_PICA: |
| 94 | + case SAC_PERCENTAGE: |
| 95 | + case SAC_DEGREE: |
| 96 | + case SAC_GRADIAN: |
| 97 | + case SAC_RADIAN: |
| 98 | + case SAC_MILLISECOND: |
| 99 | + case SAC_SECOND: |
| 100 | + case SAC_HERTZ: |
| 101 | + case SAC_KILOHERTZ: |
| 102 | + case SAC_DIMENSION: |
56 | 103 | type = "number";
|
57 |
| - } else if (isBoolean(unit)) { |
58 |
| - type = "bool"; |
59 |
| - } else if (unit.getItemType() == LexicalUnitImpl.SAC_RGBCOLOR) { |
60 |
| - type = "color"; |
61 |
| - } else if( ColorUtil.isColorName( unit ) ) { |
62 |
| - type = "color"; |
63 |
| - } else if( ColorUtil.isHexColor( unit ) ) { |
64 |
| - type = "color"; |
65 |
| - } else if (unit.getItemType() == LexicalUnitImpl.SAC_FUNCTION) { |
66 |
| - if ("rgb".equals(unit.getFunctionName()) |
67 |
| - || "rgba".equals(unit.getFunctionName()) |
68 |
| - || "hsl".equals(unit.getFunctionName()) |
69 |
| - || "hsla".equals(unit.getFunctionName())) { |
| 104 | + break; |
| 105 | + case SAC_IDENT: |
| 106 | + String str = ((LexicalUnitImpl)param).getStringValue(); |
| 107 | + if( "true".equals( str ) || "false".equals( str ) ) { |
| 108 | + type = "bool"; |
| 109 | + break; |
| 110 | + } |
| 111 | + if( ColorUtil.isColorName( str ) || ColorUtil.isHexColor( str ) ) { |
70 | 112 | type = "color";
|
| 113 | + break; |
71 | 114 | }
|
72 |
| - } |
73 |
| - } |
74 |
| - |
75 |
| - return createIdent(function, type); |
76 |
| - } |
77 |
| - |
78 |
| - private boolean isBoolean(LexicalUnitImpl unit) { |
79 |
| - if (unit.getItemType() != LexicalUnitImpl.SAC_IDENT) { |
80 |
| - return false; |
| 115 | + break; |
| 116 | + case SAC_RGBCOLOR: |
| 117 | + type = "color"; |
| 118 | + break; |
| 119 | + case SAC_FUNCTION: |
| 120 | + str = ((LexicalUnitImpl)param).getFunctionName(); |
| 121 | + switch( str ) { |
| 122 | + case "rgb": |
| 123 | + case "rgba": |
| 124 | + case "hsl": |
| 125 | + case "hsla": |
| 126 | + type = "color"; |
| 127 | + break; |
| 128 | + } |
| 129 | + break; |
81 | 130 | }
|
82 |
| - return "true".equals(unit.getStringValue()) |
83 |
| - || "false".equals(unit.getStringValue()); |
84 |
| - } |
85 |
| - |
86 |
| - private boolean isNumber(LexicalUnitImpl unit) { |
87 |
| - return unit.isNumber(); |
88 |
| - } |
89 | 131 |
|
90 |
| - private LexicalUnitImpl createIdent(LexicalUnitImpl function, |
91 |
| - String paramType) { |
92 |
| - return LexicalUnitImpl.createRawIdent( function.getUri(), function.getLineNumber(), function.getColumnNumber(), paramType ); |
| 132 | + return LexicalUnitImpl.createRawIdent( function.getUri(), function.getLineNumber(), function.getColumnNumber(), type ); |
93 | 133 | }
|
94 |
| - |
95 | 134 | }
|
0 commit comments