Search in sources :

Example 1 with Expression

use of japa.parser.ast.expr.Expression in project japid42 by branaway.

the class JavaSyntaxTool method parseNamedArgs.

/**
	 * 
	 * @param line
	 * @return list of named args if all the args are named; empty list if none
	 *         is named; or an exception is thrown if the arg list is not valid
	 *         or named and un-named are mixed
	 * 
	 */
public static List<NamedArg> parseNamedArgs(String line) {
    final List<NamedArg> ret = new ArrayList<NamedArg>();
    if (line == null || line.trim().length() == 0)
        return ret;
    line = line.trim();
    //		if (line.startsWith("(")) {
    //			if (line.endsWith(")"))
    //				line = line.substring(1, line.length() - 1);
    //			else
    //				throw new RuntimeException("no closing ')' in arg expression: "
    //						+ line);
    //		}
    String cl = String.format(classTempForArgs, line);
    final String finalLine = line;
    try {
        CompilationUnit cu = parse(cl);
        VoidVisitorAdapter visitor = new VoidVisitorAdapter() {

            boolean hasNamed = false;

            boolean hasUnNamed = false;

            @Override
            public void visit(MethodCallExpr n, Object arg) {
                List<Expression> args = n.getArgs();
                // api issue: args can be null in case of empty arg list
                if (args != null)
                    for (Expression expr : args) {
                        if (expr instanceof AssignExpr) {
                            if (hasUnNamed)
                                throw new RuntimeException("the line has mixed named and un-named arg list. It's not valid in Japid tag invocation. It must be all-or-none.: " + finalLine);
                            hasNamed = true;
                            AssignExpr ae = (AssignExpr) expr;
                            NamedArg na = new NamedArg(ae.getTarget(), ae.getValue());
                            ret.add(na);
                        } else {
                            if (hasNamed)
                                throw new RuntimeException("the line has mixed named and un-named arg list. It's not valid in Japid tag invocation. It must be all-or-none.: " + finalLine);
                            hasUnNamed = true;
                        }
                    }
            }
        };
        cu.accept(visitor, null);
    } catch (ParseException e) {
        throw new RuntimeException("the line does not seem to be a valid arg list: " + line + ". ");
    }
    return ret;
}
Also used : CompilationUnit(japa.parser.ast.CompilationUnit) ArrayList(java.util.ArrayList) AssignExpr(japa.parser.ast.expr.AssignExpr) Expression(japa.parser.ast.expr.Expression) ParseException(japa.parser.ParseException) VoidVisitorAdapter(japa.parser.ast.visitor.VoidVisitorAdapter) MethodCallExpr(japa.parser.ast.expr.MethodCallExpr)

Example 2 with Expression

use of japa.parser.ast.expr.Expression in project japid42 by branaway.

the class JavaSyntaxTool method parseArgs.

// XXX this method does not properly parse thingsl like A<t> a
// it does not detect the error
@SuppressWarnings("unchecked")
public static List<String> parseArgs(String line) {
    final List<String> ret = new ArrayList<String>();
    if (line == null || line.trim().length() == 0)
        return ret;
    @SuppressWarnings("rawtypes") VoidVisitorAdapter visitor = new VoidVisitorAdapter() {

        @Override
        public void visit(MethodCallExpr n, Object arg) {
            List<Expression> args = n.getArgs();
            // api issue: args can be null in case of empty arg list
            if (args != null)
                for (Expression e : args) {
                    ret.add(e.toString());
                }
        }
    };
    line = line.trim();
    if (line.startsWith("(")) {
        // perhaps it's in the form of (...)
        String cl = String.format(classTempForArgsNoParenthesis, line);
        try {
            CompilationUnit cu = parse(cl);
            cu.accept(visitor, null);
            return ret;
        } catch (ParseException e) {
        // perhaps not really (...). fall through
        }
    }
    String cl = String.format(classTempForArgs, line);
    try {
        CompilationUnit cu = parse(cl);
        cu.accept(visitor, null);
    } catch (ParseException e) {
        throw new RuntimeException("the line does not seem to be a valid arg list: " + line);
    }
    return ret;
}
Also used : CompilationUnit(japa.parser.ast.CompilationUnit) Expression(japa.parser.ast.expr.Expression) ArrayList(java.util.ArrayList) ParseException(japa.parser.ParseException) VoidVisitorAdapter(japa.parser.ast.visitor.VoidVisitorAdapter) MethodCallExpr(japa.parser.ast.expr.MethodCallExpr)

Example 3 with Expression

use of japa.parser.ast.expr.Expression in project Japid by branaway.

the class JavaSyntaxTool method parseArgs.

// XXX this method does not properly parse thingsl like A<t> a
// it does not detect the error
@SuppressWarnings("unchecked")
public static List<String> parseArgs(String line) {
    final List<String> ret = new ArrayList<String>();
    if (line == null || line.trim().length() == 0)
        return ret;
    @SuppressWarnings("rawtypes") VoidVisitorAdapter visitor = new VoidVisitorAdapter() {

        @Override
        public void visit(MethodCallExpr n, Object arg) {
            List<Expression> args = n.getArgs();
            // api issue: args can be null in case of empty arg list
            if (args != null)
                for (Expression e : args) {
                    ret.add(e.toString());
                }
        }
    };
    line = line.trim();
    if (line.startsWith("(")) {
        // perhaps it's in the form of (...)
        String cl = String.format(classTempForArgsNoParenthesis, line);
        try {
            CompilationUnit cu = parse(cl);
            cu.accept(visitor, null);
            return ret;
        } catch (ParseException e) {
        // perhaps not really (...). fall through
        }
    }
    String cl = String.format(classTempForArgs, line);
    try {
        CompilationUnit cu = parse(cl);
        cu.accept(visitor, null);
    } catch (ParseException e) {
        throw new RuntimeException("the line does not seem to be a valid arg list: " + line);
    }
    return ret;
}
Also used : CompilationUnit(japa.parser.ast.CompilationUnit) Expression(japa.parser.ast.expr.Expression) ArrayList(java.util.ArrayList) ParseException(japa.parser.ParseException) VoidVisitorAdapter(japa.parser.ast.visitor.VoidVisitorAdapter) MethodCallExpr(japa.parser.ast.expr.MethodCallExpr)

Example 4 with Expression

use of japa.parser.ast.expr.Expression in project Japid by branaway.

the class JavaSyntaxTool method parseNamedArgs.

/**
	 * 
	 * @param line
	 * @return list of named args if all the args are named; empty list if none
	 *         is named; or an exception is thrown if the arg list is not valid
	 *         or named and un-named are mixed
	 * 
	 */
public static List<NamedArg> parseNamedArgs(String line) {
    final List<NamedArg> ret = new ArrayList<NamedArg>();
    if (line == null || line.trim().length() == 0)
        return ret;
    line = line.trim();
    //		if (line.startsWith("(")) {
    //			if (line.endsWith(")"))
    //				line = line.substring(1, line.length() - 1);
    //			else
    //				throw new RuntimeException("no closing ')' in arg expression: "
    //						+ line);
    //		}
    String cl = String.format(classTempForArgs, line);
    final String finalLine = line;
    try {
        CompilationUnit cu = parse(cl);
        VoidVisitorAdapter visitor = new VoidVisitorAdapter() {

            boolean hasNamed = false;

            boolean hasUnNamed = false;

            @Override
            public void visit(MethodCallExpr n, Object arg) {
                List<Expression> args = n.getArgs();
                // api issue: args can be null in case of empty arg list
                if (args != null)
                    for (Expression expr : args) {
                        if (expr instanceof AssignExpr) {
                            if (hasUnNamed)
                                throw new RuntimeException("the line has mixed named and un-named arg list. It's not valid in Japid tag invocation. It must be all-or-none.: " + finalLine);
                            hasNamed = true;
                            AssignExpr ae = (AssignExpr) expr;
                            NamedArg na = new NamedArg(ae.getTarget(), ae.getValue());
                            ret.add(na);
                        } else {
                            if (hasNamed)
                                throw new RuntimeException("the line has mixed named and un-named arg list. It's not valid in Japid tag invocation. It must be all-or-none.: " + finalLine);
                            hasUnNamed = true;
                        }
                    }
            }
        };
        cu.accept(visitor, null);
    } catch (ParseException e) {
        throw new RuntimeException("the line does not seem to be a valid arg list: " + line + ". ");
    }
    return ret;
}
Also used : CompilationUnit(japa.parser.ast.CompilationUnit) ArrayList(java.util.ArrayList) AssignExpr(japa.parser.ast.expr.AssignExpr) Expression(japa.parser.ast.expr.Expression) ParseException(japa.parser.ParseException) VoidVisitorAdapter(japa.parser.ast.visitor.VoidVisitorAdapter) MethodCallExpr(japa.parser.ast.expr.MethodCallExpr)

Example 5 with Expression

use of japa.parser.ast.expr.Expression in project enumerable by hraberg.

the class LambdaExpressionTrees method parseExpression.

public static Expression parseExpression(String expression) {
    try {
        Class<?> parserClass = Class.forName("japa.parser.ASTParser");
        Constructor<?> ctor = parserClass.getConstructor(Reader.class);
        ctor.setAccessible(true);
        Object parser = ctor.newInstance(new StringReader(expression));
        Method method = parserClass.getMethod("Expression");
        method.setAccessible(true);
        return (Expression) method.invoke(parser);
    } catch (Exception e) {
        throw uncheck(e);
    }
}
Also used : Expression(japa.parser.ast.expr.Expression) StringReader(java.io.StringReader) Method(java.lang.reflect.Method) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

Expression (japa.parser.ast.expr.Expression)6 ParseException (japa.parser.ParseException)4 CompilationUnit (japa.parser.ast.CompilationUnit)4 MethodCallExpr (japa.parser.ast.expr.MethodCallExpr)4 VoidVisitorAdapter (japa.parser.ast.visitor.VoidVisitorAdapter)4 ArrayList (java.util.ArrayList)4 AssignExpr (japa.parser.ast.expr.AssignExpr)2 Method (java.lang.reflect.Method)2 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1