//===========================================================================
//
// Parsing Expression Grammar for Java 1.6 as a parboiled parser.
// Based on Chapters 3 and 18 of Java Language Specification, Third Edition (JLS)
// at http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html.
//
//---------------------------------------------------------------------------
//
// Copyright (C) 2010 by Mathias Doenitz
// Based on the Mouse 1.3 grammar for Java 1.6, which is
// Copyright (C) 2006, 2009, 2010, 2011 by Roman R Redziejowski (www.romanredz.se).
//
// The author gives unlimited permission to copy and distribute
// this file, with or without modifications, as long as this notice
// is preserved, and any changes are properly documented.
//
// This file is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
//---------------------------------------------------------------------------
//
// Change log
// 2006-12-06 Posted on Internet.
// 2009-04-04 Modified to conform to Mouse syntax:
// Underscore removed from names
// \f in Space replaced by Unicode for FormFeed.
// 2009-07-10 Unused rule THREADSAFE removed.
// 2009-07-10 Copying and distribution conditions relaxed by the author.
// 2010-01-28 Transcribed to parboiled
// 2010-02-01 Fixed problem in rule "FormalParameterDecls"
// 2010-03-29 Fixed problem in "annotation"
// 2010-03-31 Fixed problem in unicode escapes, String literals and line comments
// (Thanks to Reinier Zwitserloot for the finds)
// 2010-07-26 Fixed problem in LocalVariableDeclarationStatement (accept annotations),
// HexFloat (HexSignificant) and AnnotationTypeDeclaration (bug in the JLS!)
// 2010-10-07 Added full support of Unicode Identifiers as set forth in the JLS
// (Thanks for Ville Peurala for the patch)
// 2011-07-23 Transcribed all missing fixes from Romans Mouse grammar (http://www.romanredz.se/papers/Java.1.6.peg)
//
//===========================================================================
package org.parboiled.examples.java;
import org.parboiled.BaseParser;
import org.parboiled.Rule;
import org.parboiled.annotations.*;
@SuppressWarnings({"InfiniteRecursion"})
@BuildParseTree
public class JavaParser extends BaseParser<Object> {
//-------------------------------------------------------------------------
// Compilation Unit
//-------------------------------------------------------------------------
public Rule CompilationUnit() {
return Sequence(
Spacing(),
Optional(PackageDeclaration()),
ZeroOrMore(ImportDeclaration()),
ZeroOrMore(TypeDeclaration()),
EOI
);
}
Rule PackageDeclaration() {
return Sequence(ZeroOrMore(Annotation()), Sequence(PACKAGE, QualifiedIdentifier(), SEMI));
}
Rule ImportDeclaration() {
return Sequence(
IMPORT,
Optional(STATIC),
QualifiedIdentifier(),
Optional(DOT, STAR),
SEMI
);
}
Rule TypeDeclaration() {
return FirstOf(
Sequence(
ZeroOrMore(Modifier()),
FirstOf(
ClassDeclaration(),
EnumDeclaration(),
InterfaceDeclaration(),
AnnotationTypeDeclaration()
)
),
SEMI
);
}
//-------------------------------------------------------------------------
// Class Declaration
//-------------------------------------------------------------------------
Rule ClassDeclaration() {
return Sequence(
CLASS,
Identifier(),
Optional(TypeParameters()),
Optional(EXTENDS, ClassType()),
Optional(IMPLEMENTS, ClassTypeList()),
ClassBody()
);
}
Rule ClassBody() {
return Sequence(LWING, ZeroOrMore(ClassBodyDeclaration()), RWING);
}
Rule ClassBodyDeclaration() {
return FirstOf(
SEMI,
Sequence(Optional(STATIC), Block()),
Sequence(ZeroOrMore(Modifier()), MemberDecl())
);
}
Rule MemberDecl() {
return FirstOf(
Sequence(TypeParameters(), GenericMethodOrConstructorRest()),
Sequence(Type(), Identifier(), MethodDeclaratorRest()),
Sequence(Type(), VariableDeclarators(), SEMI),
Sequence(VOID, Identifier(), VoidMethodDeclaratorRest()),
Sequence(Identifier(), ConstructorDeclaratorRest()),
InterfaceDeclaration(),
ClassDeclaration(),
EnumDeclaration(),
AnnotationTypeDeclaration()
);
}
Rule GenericMethodOrConstructorRest() {
return FirstOf(
Sequence(FirstOf(Type(), VOID), Identifier(), MethodDeclaratorRest()),
Sequence(Identifier(), ConstructorDeclaratorRest())
);
}
Rule MethodDeclaratorRest() {
return Sequence(
FormalParameters(),
ZeroOrMore(Dim()),
Optional(THROWS, ClassTypeList()),
FirstOf(MethodBody(), SEMI)
);
}
Rule VoidMethodDeclaratorRest() {
return Sequence(
FormalParameters(),
Optional(THROWS, ClassTypeList()),
FirstOf(MethodBody(), SEMI)
);
}
Rule ConstructorDeclaratorRest() {
return Sequence(FormalParameters(), Optional(THROWS, ClassTypeList()), MethodBody());
}
Rule MethodBody() {
return Block();
}
//-------------------------------------------------------------------------
// Interface Declaration
//-------------------------------------------------------------------------
Rule InterfaceDeclaration() {
return Sequence(
INTERFACE,
Identifier(),
Optional(TypeParameters()),
Optional(EXTENDS, ClassTypeList()),
InterfaceBody()
);
}
Rule InterfaceBody() {
return Sequence(LWING, ZeroOrMore(InterfaceBodyDeclaration()), RWING);
}
Rule InterfaceBodyDeclaration() {
return FirstOf(
Sequence(ZeroOrMore(Modifier()), InterfaceMemberDecl()),
SEMI
);
}
Rule InterfaceMemberDecl() {
return FirstOf(
InterfaceMethodOrFieldDecl(),
InterfaceGenericMethodDecl(),
Sequence(VOID, Identifier(), VoidInterfaceMethodDeclaratorsRest()),
InterfaceDeclaration(),
AnnotationTypeDeclaration(),
ClassDeclaration(),
EnumDeclaration()
);
}
Rule InterfaceMethodOrFieldDecl() {
return Sequence(Sequence(Type(), Identifier()), InterfaceMethodOrFieldRest());
}
Rule InterfaceMethodOrFieldRest() {
return FirstOf(
Sequence(ConstantDeclaratorsRest(), SEMI),
InterfaceMethodDeclaratorRest()
);
}
Rule InterfaceMethodDeclaratorRest() {
return Sequence(
FormalParameters(),
ZeroOrMore(Dim()),
Optional(THROWS, ClassTypeList()),
SEMI
);
}
Rule InterfaceGenericMethodDecl() {
return Sequence(TypeParameters(), FirstOf(Type(), VOID), Identifier(), InterfaceMethodDeclaratorRest());
}
Rule VoidInterfaceMethodDeclaratorsRest() {
return Sequence(FormalParameters(), Optional(THROWS, ClassTypeList()), S
评论1