Skip to content

Commit

Permalink
Add GString support for invocation parameters in SimpleGroovyParser
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-wyluda committed Sep 6, 2013
1 parent 30e4eb0 commit 265a3ce
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class GradleSourceUtil
public static String setProjectName(String source, String projectPath, String newName)
{
source = SourceUtil.addNewLineAtEnd(source);
source += String.format("project(\"%s\").name = \"%s\"\n", projectPath, newName);
source += String.format("project('%s').name = '%s'\n", projectPath, newName);

return source;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,15 @@ private void processMethodCallExpression(Expression expression, PreInvocationWit

Expression argumentsExpression = ((MethodCallExpression) expression).getArguments();

// In case argument expression is string constant or closure
// In case argument expression is a (G)String constant or closure
if (argumentsExpression instanceof ArgumentListExpression &&
((ArgumentListExpression) argumentsExpression).getExpressions().size() == 1)
{
processArgumentListExpression((ArgumentListExpression) argumentsExpression, node,
methodName, lineNumber, columnNumber, lastLineNumber, lastColumnNumber);
}

// If argument expression is TupleExpression then it may be a map
// If argument expression is a TupleExpression then it may be a map
else if (argumentsExpression instanceof TupleExpression &&
((TupleExpression) argumentsExpression).getExpressions().size() == 1)
{
Expand All @@ -226,10 +226,10 @@ private void processArgumentListExpression(ArgumentListExpression argumentsExpre
{
Expression argumentExpression = ((ArgumentListExpression) argumentsExpression).getExpressions().get(0);

// If argument is string constant
if (argumentExpression instanceof ConstantExpression)
// If argument is a string constant
if (isStringOrGString(argumentExpression))
{
String string = ((ConstantExpression) argumentExpression).getValue().toString();
String string = valueFromStringOrGString(argumentExpression);

String code = source.substring(SourceUtil.positionInSource(source, lineNumber, columnNumber),
SourceUtil.positionInSource(source, lastLineNumber, lastColumnNumber));
Expand All @@ -238,7 +238,7 @@ private void processArgumentListExpression(ArgumentListExpression argumentsExpre
node.invocationWithStringList.add(invocation);
}

// If argument is closure
// If argument is a closure
else if (argumentExpression instanceof ClosureExpression)
{
BlockStatement blockStatement = (BlockStatement) ((ClosureExpression) argumentExpression).getCode();
Expand Down Expand Up @@ -280,10 +280,10 @@ private void processNamedArgumentListExpression(NamedArgumentListExpression argu
Expression keyExpression = mapEntryExpression.getKeyExpression();
Expression valueExpression = mapEntryExpression.getValueExpression();
if (keyExpression instanceof ConstantExpression &&
valueExpression instanceof ConstantExpression)
isStringOrGString(valueExpression))
{
String key = ((ConstantExpression) keyExpression).getValue().toString();
String value = ((ConstantExpression) valueExpression).getValue().toString();
String value = valueFromStringOrGString(valueExpression);
parameters.put(key, value);
}
}
Expand All @@ -297,30 +297,17 @@ private void processNamedArgumentListExpression(NamedArgumentListExpression argu

private void processBinaryExpression(BinaryExpression expression, PreInvocationWithClosure node)
{
boolean rightExpressionIsAString = expression.getRightExpression() instanceof ConstantExpression &&
((ConstantExpression) expression.getRightExpression()).getValue() instanceof String;
boolean rightExpressionIsAGString = expression.getRightExpression() instanceof GStringExpression;

// This condition must be true to be string variable assignment
// but not new variable declaration
if (!(expression instanceof DeclarationExpression) &&
(expression.getLeftExpression() instanceof VariableExpression ||
expression.getLeftExpression() instanceof PropertyExpression) &&
expression.getOperation().getText().toString().equals("=") &&
(rightExpressionIsAString || rightExpressionIsAGString))
isStringOrGString(expression.getRightExpression()))
{
String variable = expression.getLeftExpression().getText();
String value = null;
String value = valueFromStringOrGString(expression.getRightExpression());

if (rightExpressionIsAString)
{
value = (String) ((ConstantExpression) expression.getRightExpression()).getValue();
}
else if (rightExpressionIsAGString)
{
value = (String) ((GStringExpression) expression.getRightExpression()).getText();
}

int lineNumber = expression.getLineNumber();
int columnNumber = expression.getColumnNumber();
int lastLineNumber = expression.getLastLineNumber();
Expand All @@ -334,4 +321,33 @@ else if (rightExpressionIsAGString)
node.variableAssignmentList.add(variableAssignment);
}
}

private String valueFromStringOrGString(Expression expression)
{
if (isString(expression))
{
return (String) ((ConstantExpression) expression).getValue();
}
else if (isGString(expression))
{
return (String) ((GStringExpression) expression).getText();
}
throw new IllegalArgumentException("Given expression is neither String nor GString expression");
}

private boolean isStringOrGString(Expression expression)
{
return isString(expression) || isGString(expression);
}

private boolean isString(Expression expression)
{
return expression instanceof ConstantExpression &&
((ConstantExpression) expression).getValue() instanceof String;
}

private boolean isGString(Expression expression)
{
return expression instanceof GStringExpression;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void testFlatSource()
Map.Entry<String, String> entry = parameters.entrySet().iterator().next();
assertEquals("plugin", entry.getKey());
assertEquals("java", entry.getValue());

VariableAssignment assignment = parser.getVariableAssignments().get(0);
assertEquals("x", assignment.getVariable());
assertEquals("y", assignment.getValue());
Expand Down Expand Up @@ -91,21 +91,21 @@ public void testNestedSource()
InvocationWithString compile = dependencies.getInvocationsWithString().get(0);
assertEquals("compile", compile.getMethodName());
assertEquals("group:artifact:1.0.0", compile.getString());

VariableAssignment assignment = subprojects.getVariableAssignments().get(0);
assertEquals("abc", assignment.getVariable());
assertEquals("def", assignment.getValue());

VariableAssignment assignment2 = subprojects.getVariableAssignments().get(1);
assertEquals("a.b.c", assignment2.getVariable());
assertEquals("d.e.f", assignment2.getValue());

VariableAssignment assignment3 = subprojects.getVariableAssignments().get(2);
// Seems that Groovy core parser changes getX() to this.getX()
assertEquals("this.getX().y", assignment3.getVariable());
assertEquals("xyz", assignment3.getValue());
}

@Test
public void testInvocationWithClosureByName()
{
Expand All @@ -120,33 +120,33 @@ public void testInvocationWithClosureByName()
"}\n";

SimpleGroovyParser parser = SimpleGroovyParser.fromSource(source);

InvocationWithClosure subprojects = parser.invocationWithClosureByName("subprojects").get();
assertEquals("subprojects", subprojects.getMethodName());

InvocationWithMap applyPlugin = subprojects.invocationWithMapByName("apply").get();
assertEquals("apply", applyPlugin.getMethodName());

InvocationWithClosure dependencies = subprojects.invocationWithClosureByName("dependencies").get();
assertEquals("dependencies", dependencies.getMethodName());

InvocationWithString compile = dependencies.invocationWithStringByName("compile").get();
assertEquals("compile", compile.getMethodName());
}

@Test
public void testInvocationWithClosureByNameAbsent()
{
String sauce = "" +
"x {\n" +
" y {\n" +
" }\n" +
"}\n";
"x {\n" +
" y {\n" +
" }\n" +
"}\n";

Optional<InvocationWithClosure> optional = SimpleGroovyParser.fromSource(sauce).invocationWithClosureByName("z");
assertFalse(optional.isPresent());
}

@Test
public void testAllInvocationAtPath()
{
Expand All @@ -166,19 +166,19 @@ public void testAllInvocationAtPath()
"}\n" +
"";
SimpleGroovyParser parser = SimpleGroovyParser.fromSource(source);

List<InvocationWithClosure> list = parser.allInvocationsAtPath("subprojects", "dependencies");
assertEquals(2, list.size());

InvocationWithClosure invocation = list.get(0);
assertEquals(1, invocation.getInvocationsWithString().size());
assertEquals("compile", invocation.getInvocationsWithString().get(0).getMethodName());

invocation = list.get(1);
assertEquals(1, invocation.getInvocationsWithString().size());
assertEquals("testRuntime", invocation.getInvocationsWithString().get(0).getMethodName());
}

@Test
public void testLineColumnNumbers()
{
Expand All @@ -188,30 +188,30 @@ public void testLineColumnNumbers()
"clojure {\n" +
"}\n";
SimpleGroovyParser parser = SimpleGroovyParser.fromSource(source);

assertEquals(1, parser.getInvocationsWithString().size());
assertEquals(1, parser.getInvocationsWithMap().size());
assertEquals(1, parser.getInvocationsWithClosure().size());

InvocationWithString strInv = parser.getInvocationsWithString().get(0);
assertEquals(1, strInv.getLineNumber());
assertEquals(1, strInv.getColumnNumber());
assertEquals(1, strInv.getLastLineNumber());
assertEquals(10, strInv.getLastColumnNumber());

InvocationWithMap mapInv = parser.getInvocationsWithMap().get(0);
assertEquals(2, mapInv.getLineNumber());
assertEquals(1, mapInv.getColumnNumber());
assertEquals(2, mapInv.getLastLineNumber());
assertEquals(18, mapInv.getLastColumnNumber());

InvocationWithClosure closureInv = parser.getInvocationsWithClosure().get(0);
assertEquals(3, closureInv.getLineNumber());
assertEquals(1, closureInv.getColumnNumber());
assertEquals(4, closureInv.getLastLineNumber());
assertEquals(2, closureInv.getLastColumnNumber());
}

@Test
public void testGetCode()
{
Expand All @@ -221,21 +221,21 @@ public void testGetCode()
"clojure {\n" +
"}\n";
SimpleGroovyParser parser = SimpleGroovyParser.fromSource(source);

assertEquals(1, parser.getInvocationsWithString().size());
assertEquals(1, parser.getInvocationsWithMap().size());
assertEquals(1, parser.getInvocationsWithClosure().size());

InvocationWithString strInv = parser.getInvocationsWithString().get(0);
assertEquals("abc 'def'", strInv.getCode());

InvocationWithMap mapInv = parser.getInvocationsWithMap().get(0);
assertEquals("alpha beta: gamma", mapInv.getCode());

InvocationWithClosure closureInv = parser.getInvocationsWithClosure().get(0);
assertEquals("clojure {\n}", closureInv.getCode());
}

@Test
public void testGStringVariableAssignment()
{
Expand All @@ -249,4 +249,19 @@ public void testGStringVariableAssignment()
assertEquals("variable", variableAssignment.getVariable());
assertEquals("2 + 2 = $(2 + 2)", variableAssignment.getValue());
}

@Test
public void testGStringInvocations()
{
String source = "" +
"strInv \"abc $xyz\"\n" +
"mapInv a: \"b$c:x\"\n";
SimpleGroovyParser parser = SimpleGroovyParser.fromSource(source);

assertEquals(1, parser.getInvocationsWithString().size());
assertEquals(1, parser.getInvocationsWithMap().size());

assertEquals("abc $xyz", parser.getInvocationsWithString().get(0).getString());
assertEquals("b$c:x", parser.getInvocationsWithMap().get(0).getParameters().get("a"));
}
}

0 comments on commit 265a3ce

Please sign in to comment.