⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parser.java

📁 java编译器gjc源码 java编译环境
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
      */
    VarDef variableDeclaratorId(long flags, Tree type) {
        int pos = S.pos;
        Name name = ident();
        type = bracketsOpt(type);
        return F.at(pos).VarDef(flags, name, type, null);
    }

    /**
      * CompilationUnit = [PACKAGE Qualident ";"] {ImportDeclaration} {TypeDeclaration}
      */
    public Tree.TopLevel compilationUnit() {
        int pos = S.pos;
        Tree pid = null;
        String dc = S.docComment;
        if (S.token == PACKAGE) {
            S.nextToken();
            pid = qualident();
            accept(SEMI);
        }
        ListBuffer defs = new ListBuffer();
        while (S.token == IMPORT)
            defs.append(importDeclaration());
        while (S.token != EOF)
            defs.append(typeDeclaration());
        Tree.TopLevel toplevel = F.at(pos).TopLevel(pid, defs.toList());
        attach(toplevel, dc);
        if (keepDocComments)
            toplevel.docComments = docComments;
        if (genEndPos)
            toplevel.endPositions = endPositions;
        return toplevel;
    }

    /**
      * ImportDeclaration = IMPORT Ident { "." Ident } [ "." "*" ] ";"
      */
    Tree importDeclaration() {
        int pos = S.pos;
        S.nextToken();
        Tree pid = F.at(S.pos).Ident(ident());
        do {
            accept(DOT);
            if (S.token == STAR) {
                pid = F.at(S.pos).Select(pid, names.asterisk);
                S.nextToken();
                break;
            } else {
                pid = F.at(S.pos).Select(pid, ident());
            }
        } while (S.token == DOT)
            ;
        accept(SEMI);
        return F.at(pos).Import(pid);
    }

    /**
      * TypeDeclaration = ClassOrInterfaceDeclaration
      *                  | ";"
      */
    Tree typeDeclaration() {
        long flags = 0;
        if (S.pos == S.errPos) {
            flags = modifiersOpt();
            while (S.token != CLASS && S.token != INTERFACE && S.token != EOF) {
                S.nextToken();
                flags = modifiersOpt();
            }
        }
        int pos = S.pos;
        if (S.token == SEMI) {
            S.nextToken();
            return F.at(pos).Block(0, Tree.emptyList);
        } else {
            String dc = S.docComment;
            flags = flags | modifiersOpt();
            return classOrInterfaceDeclaration(flags, dc);
        }
    }

    /**
      * ClassOrInterfaceDeclaration = ModifiersOpt
      *                                (ClassDeclaration | InterfaceDeclaration)
      *  @param flags    Any modifiers starting the class or interface declaration
      *                  which are not in ModifiersOpt
      *  @param dc       The documentation comment for the class, or null.
      */
    Tree classOrInterfaceDeclaration(long flags, String dc) {
        flags = flags | modifiersOpt();
        if (S.token == CLASS)
            return classDeclaration(flags, dc);
        else if (S.token == INTERFACE)
            return interfaceDeclaration(flags, dc);
        else
            return syntaxError("class.or.intf.expected");
    }

    /**
      * ClassDeclaration = CLASS Ident TypeParametersOpt [EXTENDS Type]
      *                     [IMPLEMENTS TypeList] ClassBody
      *  @param flags    The modifiers starting the class declaration
      *  @param dc       The documentation comment for the class, or null.
      */
    Tree classDeclaration(long flags, String dc) {
        int pos = S.pos;
        accept(CLASS);
        Name name = ident();
        List typarams = TypeParameter.emptyList;
        Tree extending = null;
        if (S.token == EXTENDS) {
            S.nextToken();
            extending = type();
        }
        List implementing = Tree.emptyList;
        if (S.token == IMPLEMENTS) {
            S.nextToken();
            implementing = typeList();
        }
        List defs = classOrInterfaceBody(name, false);
        Tree result = F.at(pos).ClassDef(flags, name, typarams, extending,
                implementing, defs);
        attach(result, dc);
        return result;
    }

    /**
      * InterfaceDeclaration = INTERFACE Ident TypeParametersOpt
      *                         [EXTENDS TypeList] InterfaceBody
      *  @param flags    The modifiers starting the interface declaration
      *  @param dc       The documentation comment for the interface, or null.
      */
    Tree interfaceDeclaration(long flags, String dc) {
        int pos = S.pos;
        accept(INTERFACE);
        Name name = ident();
        List typarams = TypeParameter.emptyList;
        List extending = Tree.emptyList;
        if (S.token == EXTENDS) {
            S.nextToken();
            extending = typeList();
        }
        List defs = classOrInterfaceBody(name, true);
        Tree result = F.at(pos).ClassDef(flags | Flags.INTERFACE, name, typarams,
                null, extending, defs);
        attach(result, dc);
        return result;
    }

    /**
      * TypeList = Type {"," Type}
      */
    List typeList() {
        ListBuffer ts = new ListBuffer();
        ts.append(type());
        while (S.token == COMMA) {
            S.nextToken();
            ts.append(type());
        }
        return ts.toList();
    }

    /**
      * ClassBody     = "{" {ClassBodyDeclaration} "}"
      *  InterfaceBody = "{" {InterfaceBodyDeclaration} "}"
      */
    List classOrInterfaceBody(Name className, boolean isInterface) {
        int pos = S.pos;
        accept(LBRACE);
        ListBuffer defs = new ListBuffer();
        while (S.token != RBRACE && S.token != EOF) {
            defs.appendList(classOrInterfaceBodyDeclaration(className, isInterface));
        }
        accept(RBRACE);
        return defs.toList();
    }

    /**
      * ClassBodyDeclaration =
      *      ";"
      *    | [STATIC] Block
      *    | ModifiersOpt
      *      ( Type Ident
      *        ( VariableDeclaratorsRest ";" | MethodDeclaratorRest )
      *      | VOID Ident MethodDeclaratorRest
      *      | TypeParameters (Type | VOID) Ident MethodDeclaratorRest
      *      | Ident ConstructorDeclaratorRest
      *      | TypeParameters Ident ConstructorDeclaratorRest
      *      | ClassOrInterfaceDeclaration
      *      )
      *  InterfaceBodyDeclaration =
      *      ";"
      *    | ModifiersOpt Type Ident
      *      ( ConstantDeclaratorsRest | InterfaceMethodDeclaratorRest ";" )
      */
    List classOrInterfaceBodyDeclaration(Name className, boolean isInterface) {
        int pos = S.pos;
        if (S.token == SEMI) {
            S.nextToken();
            return Tree.emptyList.prepend(F.at(pos).Block(0, Tree.emptyList));
        } else {
            String dc = S.docComment;
            long flags = modifiersOpt();
            if (S.token == CLASS || S.token == INTERFACE) {
                return Tree.emptyList.prepend(
                        classOrInterfaceDeclaration(flags, dc));
            } else if (S.token == LBRACE && !isInterface &&
                    (flags & Flags.StandardFlags & ~Flags.STATIC) == 0) {
                return Tree.emptyList.prepend(block(flags));
            } else {
                List typarams = TypeParameter.emptyList;
                int token = S.token;
                Name name = S.name;
                pos = S.pos;
                Tree type;
                boolean isVoid = S.token == VOID;
                if (isVoid) {
                    type = F.at(pos).TypeIdent(Type.VOID);
                    S.nextToken();
                } else {
                    type = type();
                }
                if (S.token == LPAREN && !isInterface && type.tag == Tree.IDENT) {
                    if (isInterface || name != className)
                        log.error(pos, "invalid.meth.decl.ret.type.req");
                    return Tree.emptyList.prepend(
                            methodDeclaratorRest(pos, flags, null, names.init,
                            typarams, isInterface, true, dc));
                } else {
                    pos = S.pos;
                    name = ident();
                    if (S.token == LPAREN) {
                        return Tree.emptyList.prepend(
                                methodDeclaratorRest(pos, flags, type, name,
                                typarams, isInterface, isVoid, dc));
                    } else if (!isVoid && typarams.isEmpty()) {
                        List defs = variableDeclaratorsRest(pos, flags, type, name,
                                isInterface, dc);
                        accept(SEMI);
                        return defs;
                    } else {
                        syntaxError(S.pos, "expected", keywords.token2string(LPAREN));
                        return Tree.emptyList;
                    }
                }
            }
        }
    }

    /**
      * MethodDeclaratorRest =
      *      FormalParameters BracketsOpt [Throws TypeList] ( MethodBody | ";")
      *  VoidMethodDeclaratorRest =
      *      FormalParameters [Throws TypeList] ( MethodBody | ";")
      *  InterfaceMethodDeclaratorRest =
      *      FormalParameters BracketsOpt [THROWS TypeList] ";"
      *  VoidInterfaceMethodDeclaratorRest =
      *      FormalParameters [THROWS TypeList] ";"
      *  ConstructorDeclaratorRest =
      *      "(" FormalParameterListOpt ")" [THROWS TypeList] MethodBody
      */
    Tree methodDeclaratorRest(int pos, long flags, Tree type, Name name,
            List typarams, boolean isInterface, boolean isVoid, String dc) {
        List params = formalParameters();
        if (!isVoid)
            type = bracketsOpt(type);
        List thrown = Tree.emptyList;
        if (S.token == THROWS) {
            S.nextToken();
            thrown = qualidentList();
        }
        Block body;
        if (S.token == LBRACE) {
            body = block();
        } else {
            accept(SEMI);
            body = null;
        }
        Tree result = F.at(pos).MethodDef(flags, name, type, typarams, params, thrown,
                body);
        attach(result, dc);
        return result;
    }

    /**
      * QualidentList = Qualident {"," Qualident}
      */
    List qualidentList() {
        ListBuffer ts = new ListBuffer();
        ts.append(qualident());
        while (S.token == COMMA) {
            S.nextToken();
            ts.append(qualident());
        }
        return ts.toList();
    }

    /**
      * FormalParameters = "(" [FormalParameter {"," FormalParameter}] ")"
      */
    List formalParameters() {
        ListBuffer params = new ListBuffer();
        accept(LPAREN);
        if (S.token != RPAREN) {
            params.append(formalParameter());
            while (S.token == COMMA) {
                S.nextToken();
                params.append(formalParameter());
            }
        }
        accept(RPAREN);
        return params.toList();
    }

    int optFinal() {
        if (S.token == FINAL) {
            S.nextToken();
            return Flags.FINAL;
        } else {
            return 0;
        }
    }

    /**
      * FormalParameter = [FINAL] Type VariableDeclaratorId
      */
    VarDef formalParameter() {
        return variableDeclaratorId(optFinal() | Flags.PARAMETER, type());
    }

    /**
      * Share the terminator when making lists of trees.
      *  This is am optimized implementation of List.make(a, b).
      */
    private List makeList(Tree a, Tree b) {
        return new List(a, new List(b, Tree.emptyList));
    }

    /**
      * Share the terminator when making lists of trees.
      *  This is am optimized implementation of List.make(a).
      */
    private List makeList(Tree a) {
        return new List(a, Tree.emptyList);
    }

    /**
      * Check that given tree is a legal expression statement.
      */
    Tree checkExprStat(Tree t) {
        switch (t.tag) {
        case Tree.PREINC:

        case Tree.PREDEC:

        case Tree.POSTINC:

        case Tree.POSTDEC:

        case Tree.ASSIGN:

        case Tree.BITOR_ASG:

        case Tree.BITXOR_ASG:

        case Tree.BITAND_ASG:

        case Tree.SL_ASG:

        case Tree.SR_ASG:

        case Tree.USR_ASG:

        case Tree.PLUS_ASG:

        case Tree.MINUS_ASG:

        case Tree.MUL_ASG:

        case Tree.DIV_ASG:

        case Tree.MOD_ASG:

        case Tree.APPLY:

        case Tree.NEWCLASS:

        case Tree.ERRONEOUS:
            return t;

        default:
            log.error(t.pos, "not.stmt");
            return errorTree;

        }
    }

    /**
      * Return precedence of operator represented by token,
      *  -1 if token is not a binary operator. @see TreeInfo.opPrec
      */
    static int prec(int token) {
        int oc = optag(token);
        return (oc >= 0) ? TreeInfo.opPrec(oc) : -1;
    }

    /**
      * Return operation tag of binary operator represented by token,
      *  -1 if token is not a binary operator.
      */
    static int optag(int token) {
        switch (token) {
        case BARBAR:
            return Tree.OR;

        case AMPAMP:
            return Tree.AND;

        case BAR:
            return Tree.BITOR;

        case BAREQ:
            return Tree.BITOR_ASG;

        case CARET:
            return Tree.BITXOR;

        case CARETEQ:
            return Tree.BITXOR_ASG;

        case AMP:
            return Tree.BITAND;

        case AMPEQ:
            return Tree.BITAND_ASG;

        case EQ

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -