Skip to content

Commit

Permalink
Partial fix that breaks calc
Browse files Browse the repository at this point in the history
  • Loading branch information
Mangara committed Mar 23, 2018
1 parent 0783371 commit f1880c3
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 41 deletions.
14 changes: 6 additions & 8 deletions src/main/antlr4/cz/vutbr/web/csskit/antlr4/CSSParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -344,19 +344,17 @@ funct_args
}

funct_argument
: ( IDENT
| PLUS
| MINUS
: ( MINUS? IDENT
| ASTERISK
| SLASH
| LPAREN
| RPAREN
| NUMBER
| PERCENTAGE
| DIMENSION
| (PLUS | MINUS)? NUMBER
| (PLUS | MINUS)? PERCENTAGE
| (PLUS | MINUS)? DIMENSION
| HASH
| string
| funct
| MINUS? funct
| COMMA
| CLASSKEYWORD //invalid
| UNIRANGE //invalid
Expand All @@ -371,7 +369,7 @@ funct_argument
) S*
;
catch [RecognitionException re] {
log.error("Recognition exception | valuepart");
log.error("Recognition exception | funct_argument");
IntervalSet intervalSet = new IntervalSet(RCURLY, SEMICOLON);
getCSSErrorHandler().consumeUntil(this, intervalSet, CSSLexerState.RecoveryMode.BALANCED, null);
_localctx.addErrorNode(this.getTokenFactory().create(INVALID_STATEMENT,""));
Expand Down
47 changes: 24 additions & 23 deletions src/main/java/cz/vutbr/web/csskit/antlr4/CSSParserVisitorImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1071,57 +1071,58 @@ public Object visitFunct_argument(CSSParser.Funct_argumentContext ctx)
declaration_stack.peek().invalid = true;
return null;
}
if (ctx.PLUS() != null) {
log.debug("VP - plus");
funct_args_stack.peek().term = tf.createOperator('+');
} else if (ctx.MINUS() != null) {
log.debug("VP - minus");
funct_args_stack.peek().term = tf.createOperator('-');
} else if (ctx.ASTERISK() != null) {
log.debug("VP - *");
int unary = 1;
if (ctx.MINUS() != null) {
unary = -1;
}
if (ctx.ASTERISK() != null) {
log.debug("FA - *");
funct_args_stack.peek().term = tf.createOperator('*');
} else if (ctx.SLASH() != null) {
log.debug("VP - /");
log.debug("FA - /");
funct_args_stack.peek().term = tf.createOperator('/');
} else if (ctx.LPAREN() != null) {
log.debug("VP - (");
log.debug("FA - (");
funct_args_stack.peek().term = tf.createOperator('(');
} else if (ctx.RPAREN() != null) {
log.debug("VP - )");
log.debug("FA - )");
funct_args_stack.peek().term = tf.createOperator(')');
} else if (ctx.COMMA() != null) {
log.debug("VP - comma");
log.debug("FA - comma");
funct_args_stack.peek().term = tf.createOperator(',');
} else if (ctx.string() != null) {
log.debug("VP - string");
log.debug("FA - string");
funct_args_stack.peek().term = tf.createString(extractTextUnescaped(ctx.string().getText()));
} else if (ctx.IDENT() != null) {
log.debug("VP - ident");
funct_args_stack.peek().term = tf.createIdent(extractTextUnescaped(ctx.IDENT().getText()));
log.debug("FA - ident");
funct_args_stack.peek().term = tf.createIdent(extractTextUnescaped((unary == -1 ? "-" : "") + ctx.IDENT().getText()));
} else if (ctx.PERCENTAGE() != null) {
log.debug("VP - percentage");
funct_args_stack.peek().term = tf.createPercent(ctx.PERCENTAGE().getText(), 1);
log.debug("FA - percentage");
funct_args_stack.peek().term = tf.createPercent(ctx.PERCENTAGE().getText(), unary);
} else if (ctx.DIMENSION() != null) {
log.debug("VP - dimension");
log.debug("FA - dimension");
String dim = ctx.DIMENSION().getText();
funct_args_stack.peek().term = tf.createDimension(dim, 1);
funct_args_stack.peek().term = tf.createDimension(dim, unary);
if (funct_args_stack.peek().term == null) {
log.info("Unable to create dimension from {}, unary {}", dim, 1);
log.info("Unable to create dimension from {}, unary {}", dim, unary);
declaration_stack.peek().invalid = true;
}
} else if (ctx.HASH() != null) {
log.debug("VP - hash");
log.debug("FA - hash");
funct_args_stack.peek().term = tf.createColor(ctx.HASH().getText());
if (funct_args_stack.peek().term == null) {
declaration_stack.peek().invalid = true;
}
} else if (ctx.NUMBER() != null) {
log.debug("VP - number");
funct_args_stack.peek().term = tf.createNumeric(ctx.NUMBER().getText(), 1);
log.debug("FA - number");
funct_args_stack.peek().term = tf.createNumeric(ctx.NUMBER().getText(), unary);
} else if (ctx.funct() != null) {
funct_args_stack.peek().term = null;
Term<?> fnterm = (Term<?>) visitFunct(ctx.funct());
if (fnterm != null) {
if (unary == -1 && fnterm instanceof TermFunction) {
((TermFunction) fnterm).setFunctionName('-' + ((TermFunction) fnterm).getFunctionName());
}
funct_args_stack.peek().term = fnterm;
} else {
declaration_stack.peek().invalid = true;
Expand Down
17 changes: 8 additions & 9 deletions src/test/java/test/FunctionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,14 @@ public class FunctionsTest {
public static final String TEST_RECT3 = "p { clip: rect(1px, 10em, 3rem, auto); color: red; }";

/* calc() length expressions (all should evaluate to 60.0) */
public static final String TEST_CALC_L[];
static {
TEST_CALC_L = new String[5];
TEST_CALC_L[0] = "p { width: calc(60px); color: red; }";
TEST_CALC_L[1] = "p { width: calc(1em + 0.5em); color: red; }";
TEST_CALC_L[2] = "p { width: calc(1em + (10% * 2)); color: red; }";
TEST_CALC_L[3] = "p { width: calc(-3em + 4.5em); color: red; }";
TEST_CALC_L[4] = "p { width: calc(3em + (-1.5em)); color: red; }";
}
public static final String TEST_CALC_L[] = new String[] {
"p { width: calc(60px); color: red; }",
"p { width: calc(1em + 0.5em); color: red; }",
"p { width: calc(1em + (10% * 2)); color: red; }",
"p { width: calc(-3em + 4.5em); color: red; }",
"p { width: calc(3em + (-1.5em)); color: red; }",
"p { width: calc(3em - 1.5em); color: red; }"
};

/* calc() angle expressions (all should evaluate to 33) */
public static final String TEST_CALC_A[];
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/test/GradientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public void testGradientAngles() throws IOException, CSSException {
ss = CSSFactory.parseString(TEST_LINEAR_GRADIENT_ANGLE_NEGATIVE, null);
separatedArguments = ((TermFunction) ((RuleSet) ss.get(0)).get(0).get(0)).getSeparatedArgs(tf.createOperator(','));
assertEquals("There are 3 comma-separated arguments ", 3, separatedArguments.size());
assertEquals("The first argument is -10deg ", Arrays.asList(tf.createOperator('-'), tf.createAngle("10deg", TermNumeric.Unit.deg, 1)), separatedArguments.get(0));
assertEquals("The first argument is -10deg ", Arrays.asList(tf.createAngle("10deg", TermNumeric.Unit.deg, -1)), separatedArguments.get(0));
}

@Test
Expand Down

0 comments on commit f1880c3

Please sign in to comment.