📄 java1.1nola.jj
字号:
/* * Copyright © 2002 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, * California 95054, U.S.A. All rights reserved. Sun Microsystems, Inc. has * intellectual property rights relating to technology embodied in the product * that is described in this document. In particular, and without limitation, * these intellectual property rights may include one or more of the U.S. * patents listed at http://www.sun.com/patents and one or more additional * patents or pending patent applications in the U.S. and in other countries. * U.S. Government Rights - Commercial software. Government users are subject * to the Sun Microsystems, Inc. standard license agreement and applicable * provisions of the FAR and its supplements. Use is subject to license terms. * Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered * trademarks of Sun Microsystems, Inc. in the U.S. and other countries. This * product is covered and controlled by U.S. Export Control laws and may be * subject to the export or import laws in other countries. Nuclear, missile, * chemical biological weapons or nuclear maritime end uses or end users, * whether direct or indirect, are strictly prohibited. Export or reexport * to countries subject to U.S. embargo or to entities identified on U.S. * export exclusion lists, including, but not limited to, the denied persons * and specially designated nationals lists is strictly prohibited. */options { JAVA_UNICODE_ESCAPE = true;}PARSER_BEGIN(JavaParser)public class JavaParser { public static void main(String args[]) { JavaParser parser; if (args.length == 0) { System.out.println("Java Parser Version 1.1: Reading from standard input . . ."); parser = new JavaParser(System.in); } else if (args.length == 1) { System.out.println("Java Parser Version 1.1: Reading from file " + args[0] + " . . ."); try { parser = new JavaParser(new java.io.FileInputStream(args[0])); } catch (java.io.FileNotFoundException e) { System.out.println("Java Parser Version 1.1: File " + args[0] + " not found."); return; } } else { System.out.println("Java Parser Version 1.1: Usage is one of:"); System.out.println(" java JavaParser < inputfile"); System.out.println("OR"); System.out.println(" java JavaParser inputfile"); return; } try { parser.CompilationUnit(); System.out.println("Java Parser Version 1.1: Java program parsed successfully."); } catch (ParseException e) { System.out.println(e.getMessage()); System.out.println("Java Parser Version 1.1: Encountered errors during parse."); } } // Semantic lookahead rules follow. // implements: LOOKAHEAD( ( "abstract" | "final" | "public" )* "class" ) static boolean isClassDeclaration() { int curTok = 1; Token tok; while (true) { tok = getToken(curTok); switch (tok.kind) { case ABSTRACT: case FINAL: case PUBLIC: curTok++; break; case CLASS: return true; default: return false; } } } // implements: LOOKAHEAD(2) for Initializer() static boolean isInitializer() { Token tok = getToken(1); if (tok.kind == LBRACE) return true; if (tok.kind == STATIC) { tok = getToken(2); if (tok.kind == LBRACE) return true; } return false; } // implements: LOOKAHEAD( ( "static" | "abstract" | "final" | "public" | "protected" | "private" )* "class" ) static boolean isNestedClassDeclaration() { int curTok = 1; Token tok; while (true) { tok = getToken(curTok); switch (tok.kind) { case STATIC: case ABSTRACT: case FINAL: case PUBLIC: case PROTECTED: case PRIVATE: curTok++; break; case CLASS: return true; default: return false; } } } // implements: LOOKAHEAD( ( "static" | "abstract" | "final" | "public" | "protected" | "private" )* "interface" ) static boolean isNestedInterfaceDeclaration() { int curTok = 1; Token tok; while (true) { tok = getToken(curTok); switch (tok.kind) { case STATIC: case ABSTRACT: case FINAL: case PUBLIC: case PROTECTED: case PRIVATE: curTok++; break; case INTERFACE: return true; default: return false; } } } // implements: LOOKAHEAD( [ "public" | "protected" | "private" ] Name() "(" ) static boolean isConstructorDeclaration() { int curTok = 1; Token tok; tok = getToken(1); switch (tok.kind) { case PUBLIC: case PROTECTED: case PRIVATE: curTok = 2; } tok = getToken(curTok++); if (tok.kind != IDENTIFIER) return false; while (true) { tok = getToken(curTok++); if (tok.kind == LPAREN) return true; if (tok.kind != DOT) return false; tok = getToken(curTok++); if (tok.kind != IDENTIFIER) return false; } } // Returns true if the next set of tokens cannot be a field declaration. // Returns false if the next set of tokens cannot be a method declaration. // Note how this method is used in the grammar. We don't have to do a // comprehensive check like we have done in the lookahead methods above. // This show (therefore) another way you can implement lookahead methods. // The way we do it is to see if we can find a "(" before either a "=" or // a ";", in which case we return true. static boolean isMethodDeclaration() { int curTok = 1; Token tok; while (true) { tok = getToken(curTok++); switch (tok.kind) { case LPAREN: return true; case ASSIGN: case SEMICOLON: case EOF: return false; } } } // Checks that there is a "," and then there is no "}" following that. static boolean moreVariableInitializers() { return getToken(1).kind == COMMA && getToken(2).kind != RBRACE; } // Checks that this is a constructor invocation as opposed to a block // statement. static boolean isConstructorInvocation() { int curTok = 1; Token tok = getToken(1); switch (tok.kind) { case THIS: case SUPER: // We are assuming here that if the statement starts with "this" // or "super", and the next token is "(", then it has to be a // constructor invocation. return getToken(2).kind == LPAREN; case STRING_LITERAL: case LPAREN: case NEW: case IDENTIFIER: // Now move across tokens until the end of the statement - the // first semicolon not nested within any kind of parentheses. // If a "super(" is matched also not nested and before this // semicolon, we return true. Otherwise return false. int nestingLevel = 0; while (true) { tok = getToken(curTok++); switch (tok.kind) { case SEMICOLON: if (nestingLevel == 0) { return false; } break; case SUPER: if (nestingLevel == 0) { return getToken(curTok).kind == LPAREN; } break; case LPAREN: case LBRACE: case LBRACKET: nestingLevel++; break; case RPAREN: case RBRACE: case RBRACKET: nestingLevel--; break; case EOF: return false; } } default: return false; } } // Returns true if this is a primitive type (or an array of primitive // type) cast. static boolean isPrimitiveTypeCast() { if (getToken(1).kind != LPAREN) return false; Token tok = getToken(2); switch (tok.kind) { case BOOLEAN: case CHAR: case BYTE: case SHORT: case INT: case LONG: case FLOAT: case DOUBLE: return true; } return false; } // Returns true if this is a type cast. static boolean isTypeCast() { if (isPrimitiveTypeCast()) return true; if (getToken(1).kind != LPAREN) return false; int curTok = 2; Token tok = getToken(curTok++); if (tok.kind != IDENTIFIER) return false; while (true) { tok = getToken(curTok++); if (tok.kind != DOT) break; tok = getToken(curTok++); if (tok.kind != IDENTIFIER) return false; } if (tok.kind == RPAREN) { tok = getToken(curTok); switch (tok.kind) { case TILDE: case BANG: case LPAREN: case IDENTIFIER: case THIS: case SUPER: case NEW: case INTEGER_LITERAL: case FLOATING_POINT_LITERAL: case CHARACTER_LITERAL: case STRING_LITERAL: case TRUE: case FALSE: case NULL: return true; } return false; } else { if (tok.kind != LBRACKET) return false; tok = getToken(curTok); if (tok.kind != RBRACKET) return false; return true; } } // Distinguishes between ClassSelector and Name. static boolean isClassSelector() { int curTok = 1; Token tok = getToken(curTok++); if (tok.kind != IDENTIFIER) return true; while (true) { tok = getToken(curTok++); while (tok.kind == LBRACKET) { // A simple loop to accept "[]"s. We are a little sloppy // in that we don't require it to be at the end, but then // this is only a lookahead check. tok = getToken(curTok++); if (tok.kind != RBRACKET) return false; tok = getToken(curTok++); } if (tok.kind != DOT) return false; tok = getToken(curTok++); if (tok.kind == CLASS) return true; if (tok.kind != IDENTIFIER) return false; } } // implements: LOOKAHEAD([ "final" ] Type() <IDENTIFIER>) static boolean isLocalVariableDeclaration() { int curTok = 1; Token tok = getToken(curTok++); if (tok.kind == FINAL) tok = getToken(curTok++); switch (tok.kind) { case BOOLEAN: case CHAR: case BYTE: case SHORT: case INT: case LONG: case FLOAT: case DOUBLE: tok = getToken(curTok++); break; case IDENTIFIER: while (true) { tok = getToken(curTok++); if (tok.kind != DOT) break; tok = getToken(curTok++); if (tok.kind != IDENTIFIER) return false; } break; default: return false; } while (tok.kind == LBRACKET) { tok = getToken(curTok++); if (tok.kind != RBRACKET) return false; tok = getToken(curTok++); } return tok.kind == IDENTIFIER; } static boolean isPrimarySuffix() { Token tok = getToken(1); if (tok.kind == LPAREN || tok.kind == LBRACKET) return true; if (tok.kind == DOT) { tok = getToken(2); switch (tok.kind) { case THIS: case NEW: case IDENTIFIER: return true; } } return false; }}PARSER_END(JavaParser)/* WHITE SPACE */SKIP :{ " "| "\t"| "\n"| "\r"| "\f"}/* COMMENTS */MORE :{ "//" : IN_SINGLE_LINE_COMMENT| <"/**" ~["/"]> { input_stream.backup(1); } : IN_FORMAL_COMMENT| "/*" : IN_MULTI_LINE_COMMENT}<IN_SINGLE_LINE_COMMENT>SPECIAL_TOKEN :{ <SINGLE_LINE_COMMENT: "\n" | "\r" | "\r\n" > : DEFAULT}<IN_FORMAL_COMMENT>SPECIAL_TOKEN :{ <FORMAL_COMMENT: "*/" > : DEFAULT}<IN_MULTI_LINE_COMMENT>SPECIAL_TOKEN :{ <MULTI_LINE_COMMENT: "*/" > : DEFAULT}<IN_SINGLE_LINE_COMMENT,IN_FORMAL_COMMENT,IN_MULTI_LINE_COMMENT>MORE :{ < ~[] >}/* RESERVED WORDS AND LITERALS */TOKEN :{ < ABSTRACT: "abstract" >| < BOOLEAN: "boolean" >| < BREAK: "break" >| < BYTE: "byte" >| < CASE: "case" >| < CATCH: "catch" >| < CHAR: "char" >| < CLASS: "class" >| < CONST: "const" >| < CONTINUE: "continue" >| < _DEFAULT: "default" >| < DO: "do" >| < DOUBLE: "double" >| < ELSE: "else" >| < EXTENDS: "extends" >| < FALSE: "false" >| < FINAL: "final" >| < FINALLY: "finally" >| < FLOAT: "float" >| < FOR: "for" >| < GOTO: "goto" >| < IF: "if" >| < IMPLEMENTS: "implements" >| < IMPORT: "import" >| < INSTANCEOF: "instanceof" >| < INT: "int" >| < INTERFACE: "interface" >| < LONG: "long" >| < NATIVE: "native" >| < NEW: "new" >| < NULL: "null" >| < PACKAGE: "package">| < PRIVATE: "private" >| < PROTECTED: "protected" >| < PUBLIC: "public" >| < RETURN: "return" >| < SHORT: "short" >| < STATIC: "static" >| < SUPER: "super" >| < SWITCH: "switch" >| < SYNCHRONIZED: "synchronized" >| < THIS: "this" >| < THROW: "throw" >| < THROWS: "throws" >| < TRANSIENT: "transient" >| < TRUE: "true" >| < TRY: "try" >| < VOID: "void" >| < VOLATILE: "volatile" >| < WHILE: "while" >}/* LITERALS */TOKEN :{ < INTEGER_LITERAL: <DECIMAL_LITERAL> (["l","L"])? | <HEX_LITERAL> (["l","L"])? | <OCTAL_LITERAL> (["l","L"])? >| < #DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* >| < #HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ >| < #OCTAL_LITERAL: "0" (["0"-"7"])* >| < FLOATING_POINT_LITERAL: (["0"-"9"])+ "." (["0"-"9"])* (<EXPONENT>)? (["f","F","d","D"])? | "." (["0"-"9"])+ (<EXPONENT>)? (["f","F","d","D"])? | (["0"-"9"])+ <EXPONENT> (["f","F","d","D"])? | (["0"-"9"])+ (<EXPONENT>)? ["f","F","d","D"] >| < #EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >| < CHARACTER_LITERAL: "'" ( (~["'","\\","\n","\r"]) | ("\\" ( ["n","t","b","r","f","\\","'","\""] | ["0"-"7"] ( ["0"-"7"] )?
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -