Package org.openquark.cal.compiler.SourceModel

Examples of org.openquark.cal.compiler.SourceModel.SourceElement


     * null if there is no inner definition containing the given position.
     * @return the innermost definition containing the given position or null if there is no such definition.
     */
    private static Pair<SourceElement, SourceRange> findInnermostDef(LocalDefn.Function.Definition def, SourcePosition position){
        SourceRange next = null;
        SourceElement element = null;
        if (def.getDefiningExpr() instanceof SourceModel.Expr.Let){
            SourceModel.Expr.Let let = (SourceModel.Expr.Let) def.getDefiningExpr();
            for(LocalDefn localDefn : let.getLocalDefinitions()){
                SourceRange testSourcePosition = localDefn.getSourceRange();
                if (testSourcePosition.containsPosition(position)){
View Full Code Here


     * source range of the element. This may be null.
     */
    static Pair<SourceElement, SourceRange> findContainingSourceElement(SourceModel.ModuleDefn moduleDefn, SourcePosition position){
        final int nTopLevelDefns = moduleDefn.getNTopLevelDefns();
        SourceRange next = null;
        SourceElement element = null;
        for (int i = 0; i < nTopLevelDefns; i++) {
            TopLevelSourceElement nthTopLevelDefn = moduleDefn.getNthTopLevelDefn(i);
            // Check for matching data constructor
            if (nthTopLevelDefn instanceof SourceModel.TypeConstructorDefn.AlgebraicType){
                SourceModel.TypeConstructorDefn.AlgebraicType algebraicType = (AlgebraicType) nthTopLevelDefn;
                for(DataConsDefn dataConsDefn : algebraicType.getDataConstructors()){
                    SourceRange testSourcePosition = dataConsDefn.getSourceRangeOfDefn();
                    if (testSourcePosition.containsPosition(position)){
                        if (next == null || next.contains(testSourcePosition)){
                            next = testSourcePosition;
                            element = dataConsDefn;
                            return new Pair<SourceElement, SourceRange>(element, element.getSourceRange());
                        }
                    }
                }
            }
            // check for matching class method
            else if (nthTopLevelDefn instanceof SourceModel.TypeClassDefn){
                SourceModel.TypeClassDefn typeClassDefn = (SourceModel.TypeClassDefn) nthTopLevelDefn;
                for(ClassMethodDefn classMethodDefn : typeClassDefn.getClassMethodDefns()){
                    SourceRange testSourcePosition = classMethodDefn.getSourceRangeOfClassDefn();
                    if (testSourcePosition.containsPosition(position)){
                        if (next == null || next.contains(testSourcePosition)){
                            next = testSourcePosition;
                            element = classMethodDefn;
                            return new Pair<SourceElement, SourceRange>(element, element.getSourceRange());
                        }
                    }
                }
            }
            // check for let expressions
            else if (nthTopLevelDefn instanceof SourceModel.FunctionDefn.Algebraic){
                SourceModel.FunctionDefn.Algebraic algebraic = (SourceModel.FunctionDefn.Algebraic) nthTopLevelDefn;
                if (algebraic.getDefiningExpr() instanceof SourceModel.Expr.Let){
                    SourceModel.Expr.Let let = (SourceModel.Expr.Let) algebraic.getDefiningExpr();
                    for(LocalDefn localDefn : let.getLocalDefinitions()){
                        SourceRange testSourcePosition = localDefn.getSourceRange();
                        if (testSourcePosition.containsPosition(position)){
                            if (next == null || next.contains(testSourcePosition)){
                                next = testSourcePosition;
                                element = localDefn;

                                if (localDefn instanceof LocalDefn.Function){
                                    SourceModel.LocalDefn.Function localFunction = (SourceModel.LocalDefn.Function) localDefn;
                                   
                                    // if there is a type declaration then use that to put the
                                    // cal doc in front of
                                    for(LocalDefn localTypeDefn : let.getLocalDefinitions()){                                   
                                        if (localTypeDefn instanceof SourceModel.LocalDefn.Function.TypeDeclaration){
                                            SourceModel.LocalDefn.Function.TypeDeclaration type = (SourceModel.LocalDefn.Function.TypeDeclaration) localTypeDefn;
                                            if (type.getName().equals(localFunction.getName())){
                                                element = type;                                               
                                            }
                                        }
                                    }
                                }
                                if (localDefn instanceof LocalDefn.Function.Definition){
                                    LocalDefn.Function.Definition def = (Definition) localDefn;
                                    Pair<SourceElement, SourceRange> innerMatch = findInnermostDef(def, position);
                                    if (innerMatch != null){
                                        element = innerMatch.fst();
                                        next = innerMatch.snd();
                                    }
                                }
                            }
                        }
                    }
                    if (element != null){
                        break;
                    }
                }
               
            }

            // check the current element
            SourceRange testSourcePosition = nthTopLevelDefn.getSourceRangeOfDefn();
            if (testSourcePosition.containsPosition(position)){
                if (next == null || next.contains(testSourcePosition)){
                    next = testSourcePosition;
                    element = nthTopLevelDefn;
                }
            }
        }

        if (element != null){
            // Find type declaration if any
            if (element instanceof SourceModel.FunctionDefn){
                SourceModel.FunctionDefn functionDefn = (FunctionDefn) element;
                String name = functionDefn.getName();
                for (int i = 0; i < nTopLevelDefns; i++) {
                    TopLevelSourceElement nthTopLevelDefn = moduleDefn.getNthTopLevelDefn(i);
                    if (nthTopLevelDefn instanceof FunctionTypeDeclaration){
                        FunctionTypeDeclaration type = (FunctionTypeDeclaration) nthTopLevelDefn;
                        if (type.getFunctionName().equals(name)){
                            element = type;
                            break;
                        }
                    }
                }
            }
           
            return new Pair<SourceElement, SourceRange>(element, element.getSourceRange());
        }
        else{
            if (moduleDefn.getSourceRange().containsPosition(position)){
                return new Pair<SourceElement, SourceRange>(moduleDefn, moduleDefn.getSourceRange());
            }
View Full Code Here

    //
   
    public void testSimpleCodeGem() {
        CodeGem gem = createCodeGem("x + y");
       
        SourceElement element = createFunctionDefn("x y", "x + y");
       
        assertFunctionTextEquals(gem, element);
    }
View Full Code Here

   
    public void testSimpleCodeGem_ArgReordering() {
        CodeGem gem = createCodeGem("x + y");
        reorderArgs(gem, new int[] {1, 0});
       
        SourceElement element = createFunctionDefn("y x", "x + y");
       
        assertFunctionTextEquals(gem, element);
    }
View Full Code Here

            .replaceAll("%x", "x").replaceAll("%y", "y"));
       
        renameArg(gem, 0, "x", "a");
        renameArg(gem, 1, "y", "b");
       
        SourceElement element = createFunctionDefn("a b", code
            .replaceAll("%x", "a").replaceAll("%y", "b"));
       
        assertFunctionTextEquals(gem, element);
    }
View Full Code Here

       
        renameArg(gem, 0, "x", "a");
        renameArg(gem, 1, "y", "b");
        reorderArgs(gem, new int[] {1, 0});
       
        SourceElement element = createFunctionDefn("b a", code
            .replaceAll("%x", "a").replaceAll("%y", "b"));
       
        assertFunctionTextEquals(gem, element);
    }
View Full Code Here

   
    public void testSimpleCodeGem_BurntInput1() {
        CodeGem gem = createCodeGem("x + y");
        burnArg(gem, 1, "y");
       
        SourceElement element = createFunctionDefn("x", "\\y -> x + y");
       
        assertFunctionTextEquals(gem, element);
    }
View Full Code Here

    public void testSimpleCodeGem_BurntInput0() {
        CodeGem gem = createCodeGem("x + y");
        burnArg(gem, 0, "x");
       
        SourceElement element = createFunctionDefn("y", "\\x -> x + y");
       
        assertFunctionTextEquals(gem, element);
    }
View Full Code Here

    public void testSimpleCodeGem_BurntInput0_ArgReordering() {
        CodeGem gem = createCodeGem("x + y");
        burnArg(gem, 0, "x");
        reorderArgs(gem, new int[] {1, 0});
       
        SourceElement element = createFunctionDefn("y", "\\x -> x + y");
       
        assertFunctionTextEquals(gem, element);
    }
View Full Code Here

    public void testSimpleCodeGem_BurntAllInputs() {
        CodeGem gem = createCodeGem("x + y");
        burnArg(gem, 0, "x");
        burnArg(gem, 1, "y");
       
        SourceElement element = createFunctionDefn("", "\\x y -> x + y");
       
        assertFunctionTextEquals(gem, element);
    }
View Full Code Here

TOP

Related Classes of org.openquark.cal.compiler.SourceModel.SourceElement

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.