public class

Parser

extends Scanner
implements Constants ParserActions
java.lang.Object
   ↳ sun.tools.java.Scanner
     ↳ sun.tools.java.Parser
Known Direct Subclasses

Class Overview

This class is used to parse Java statements and expressions. The result is a parse tree.

This class implements an operator precedence parser. Errors are reported to the Environment object, if the error can't be resolved immediately, a SyntaxError exception is thrown.

Error recovery is implemented by catching SyntaxError exceptions and discarding input tokens until an input token is reached that is possibly a legal continuation.

The parse tree that is constructed represents the input exactly (no rewrites to simpler forms). This is important if the resulting tree is to be used for code formatting in a programming environment. Currently only documentation comments are retained.

The parsing algorithm does NOT use any type information. Changes in the type system do not affect the structure of the parse tree. This restriction does introduce an ambiguity an expression of the form: (e1) e2 is assumed to be a cast if e2 does not start with an operator. That means that (a) - b is interpreted as subtract b from a and not cast negative b to type a. However, if a is a simple type (byte, int, ...) then it is assumed to be a cast.

WARNING: The contents of this source file are not part of any supported API. Code that depends on them does so at its own risk: they are subject to change or removal without notice.

Summary

[Expand]
Inherited Constants
From class sun.tools.java.Scanner
From interface sun.tools.java.Constants
From interface sun.tools.java.RuntimeConstants
Fields
protected int argIndex
protected Scanner scanner Usually this.scanner == (Scanner)this.
[Expand]
Inherited Fields
From class sun.tools.java.Scanner
From interface sun.tools.java.Constants
From interface sun.tools.java.RuntimeConstants
Public Constructors
Parser(Scanner scanner, ParserActions actions)
Create a parser, given a scanner and the semantic callback.
Protected Constructors
Parser(Environment env, InputStream in)
Create a parser
Parser(Scanner scanner)
Create a parser, given a scanner.
Public Methods
ClassDefinition beginClass(long off, String doc, int mod, IdentifierToken nm, IdentifierToken sup, IdentifierToken[] impl)
This method is deprecated. No replacement.
void defineField(long where, ClassDefinition c, String doc, int mod, Type t, IdentifierToken nm, IdentifierToken[] args, IdentifierToken[] exp, Node val)
This method is deprecated. No replacement.
void endClass(long off, ClassDefinition c)
This method is deprecated. No replacement.
void importClass(long off, IdentifierToken nm)
This method is deprecated. No replacement.
void importPackage(long off, IdentifierToken nm)
This method is deprecated. No replacement.
void match(int open, int close)
Scan to a matching '}', ']' or ')'.
void packageDeclaration(long off, IdentifierToken nm)
This method is deprecated. No replacement.
void parseFile()
Parse an Java file.
long scan()
Scan the next token.
Protected Methods
final void addArgument(Node n)
void beginClass(long off, String doc, int mod, Identifier nm, Identifier sup, Identifier[] impl)
This method is deprecated. Use the version with the IdentifierToken arguments.
void defineField(long where, String doc, int mod, Type t, Identifier nm, Identifier[] args, Identifier[] exp, Node val)
This method is deprecated. Use the version with the IdentifierToken arguments.
void endClass(long off, Identifier nm)
This method is deprecated. Use the version with the IdentifierToken arguments.
void expect(int t)
Expect a token, return its value, scan the next token or throw an exception.
final Expression[] exprArgs(int index)
ClassDefinition getCurrentClass()
Report the current class under construction.
void importClass(long off, Identifier nm)
This method is deprecated. Use the version with the IdentifierToken arguments.
void importPackage(long off, Identifier nm)
This method is deprecated. Use the version with the IdentifierToken arguments.
void packageDeclaration(long off, Identifier nm)
This method is deprecated. No replacement.
Type parseArrayBrackets(Type t)
Parse the tail of a type expression, which might be array brackets.
Expression parseBinaryExpression(Expression e)
Given a left-hand term, parse an operator and right-hand term.
Statement parseBlockStatement()
void parseClass()
Parse a top-level class or interface declaration.
ClassDefinition parseClassBody(IdentifierToken nm, int mod, int ctx, String doc, Vector ext, Vector impl, long p)
Parse the body of a class or interface declaration, starting at the left brace.
Statement parseDeclaration(long p, int mod, Expression type)
Parse declaration, called after the type expression has been parsed and the current token is IDENT.
Expression parseExpression()
Parse an expression.
void parseField()
Parse a field.
Identifier parseIdentifier(boolean star)
@deprecated
void parseInheritance(Vector ext, Vector impl)
Statement parseLocalClass(int mod)
Parse a block-local class or interface declaration.
Expression parseMethodExpression(Expression e, Identifier id)
Parse a method invocation.
int parseModifiers(int mask)
Parse a possibly-empty sequence of modifier keywords.
IdentifierToken parseName(boolean star)
Parse an identifier.
ClassDefinition parseNamedClass(int mod, int ctx, String doc)
Parse a named class or interface declaration, starting at "class" or "interface".
Expression parseNewInstanceExpression(long p, Expression outerArg, Expression type)
Parse a new instance expression.
Statement parseStatement()
Parse a statement.
Expression parseTerm()
Parse a primary expression.
Type parseType()
Parse a type expression, this results in a Type.
Expression parseTypeExpression()
Parse a type expression.
void recoverField(ClassDefinition newClass)
Recover after a syntax error in a field.
void recoverFile()
Recover after a syntax error in the file.
boolean recoverStatement()
Recover after a syntax error in a statement.
final Statement[] statArgs(int index)
void topLevelExpression(Expression e)
Check if an expression is a legal toplevel expression.
[Expand]
Inherited Methods
From class sun.tools.java.Scanner
From class java.lang.Object
From interface sun.tools.java.ParserActions

Fields

protected int argIndex

protected Scanner scanner

Usually this.scanner == (Scanner)this. However, a delegate scanner can produce tokens for this parser, in which case (Scanner)this is unused, except for this.token and this.pos instance variables which are filled from the real scanner by this.scan() and the constructor.

Public Constructors

public Parser (Scanner scanner, ParserActions actions)

Create a parser, given a scanner and the semantic callback.

Throws
IOException

Protected Constructors

protected Parser (Environment env, InputStream in)

Create a parser

Throws
IOException

protected Parser (Scanner scanner)

Create a parser, given a scanner.

Throws
IOException

Public Methods

public ClassDefinition beginClass (long off, String doc, int mod, IdentifierToken nm, IdentifierToken sup, IdentifierToken[] impl)

This method is deprecated.
No replacement.

Define class

Returns
  • a cookie for the class This cookie is used by the parser when calling defineField and endClass, and is not examined otherwise.

public void defineField (long where, ClassDefinition c, String doc, int mod, Type t, IdentifierToken nm, IdentifierToken[] args, IdentifierToken[] exp, Node val)

This method is deprecated.
No replacement.

Define a field

Parameters
c a cookie returned by the corresponding beginClass call

public void endClass (long off, ClassDefinition c)

This method is deprecated.
No replacement.

End class

Parameters
c a cookie returned by the corresponding beginClass call

public void importClass (long off, IdentifierToken nm)

This method is deprecated.
No replacement.

import class

public void importPackage (long off, IdentifierToken nm)

This method is deprecated.
No replacement.

import package

public void match (int open, int close)

Scan to a matching '}', ']' or ')'. The current token must be a '{', '[' or '(';

Throws
IOException

public void packageDeclaration (long off, IdentifierToken nm)

This method is deprecated.
No replacement.

package declaration

public void parseFile ()

Parse an Java file.

public long scan ()

Scan the next token.

Returns
  • the position of the previous token.
Throws
IOException

Protected Methods

protected final void addArgument (Node n)

protected void beginClass (long off, String doc, int mod, Identifier nm, Identifier sup, Identifier[] impl)

This method is deprecated.
Use the version with the IdentifierToken arguments.

protected void defineField (long where, String doc, int mod, Type t, Identifier nm, Identifier[] args, Identifier[] exp, Node val)

This method is deprecated.
Use the version with the IdentifierToken arguments.

protected void endClass (long off, Identifier nm)

This method is deprecated.
Use the version with the IdentifierToken arguments.

protected void expect (int t)

Expect a token, return its value, scan the next token or throw an exception.

protected final Expression[] exprArgs (int index)

protected ClassDefinition getCurrentClass ()

Report the current class under construction. By default, it's a no-op which returns null. It may only be called before the corresponding endClass().

protected void importClass (long off, Identifier nm)

This method is deprecated.
Use the version with the IdentifierToken arguments.

protected void importPackage (long off, Identifier nm)

This method is deprecated.
Use the version with the IdentifierToken arguments.

protected void packageDeclaration (long off, Identifier nm)

This method is deprecated.
No replacement.

protected Type parseArrayBrackets (Type t)

Parse the tail of a type expression, which might be array brackets. Return the given type, as possibly modified by the suffix.

protected Expression parseBinaryExpression (Expression e)

Given a left-hand term, parse an operator and right-hand term.

protected Statement parseBlockStatement ()

protected void parseClass ()

Parse a top-level class or interface declaration.

protected ClassDefinition parseClassBody (IdentifierToken nm, int mod, int ctx, String doc, Vector ext, Vector impl, long p)

Parse the body of a class or interface declaration, starting at the left brace.

protected Statement parseDeclaration (long p, int mod, Expression type)

Parse declaration, called after the type expression has been parsed and the current token is IDENT.

protected Expression parseExpression ()

Parse an expression.

protected void parseField ()

Parse a field.

protected Identifier parseIdentifier (boolean star)

@deprecated

protected void parseInheritance (Vector ext, Vector impl)

protected Statement parseLocalClass (int mod)

Parse a block-local class or interface declaration.

protected Expression parseMethodExpression (Expression e, Identifier id)

Parse a method invocation. Should be called when the current then is the '(' of the argument list.

protected int parseModifiers (int mask)

Parse a possibly-empty sequence of modifier keywords. Return the resulting bitmask. Diagnose repeated modifiers, but make no other checks. Only modifiers mentioned in the given bitmask are scanned; an unmatched modifier must be handled by the caller.

Throws
IOException

protected IdentifierToken parseName (boolean star)

Parse an identifier. ie: a.b.c returns "a.b.c" If star is true then "a.b.*" is allowed. The return value encodes both the identifier and its location.

protected ClassDefinition parseNamedClass (int mod, int ctx, String doc)

Parse a named class or interface declaration, starting at "class" or "interface".

protected Expression parseNewInstanceExpression (long p, Expression outerArg, Expression type)

Parse a new instance expression. Should be called when the current token is the '(' of the argument list.

protected Statement parseStatement ()

Parse a statement.

protected Expression parseTerm ()

Parse a primary expression.

protected Type parseType ()

Parse a type expression, this results in a Type. The parse includes trailing array brackets.

protected Expression parseTypeExpression ()

Parse a type expression. Does not parse the []'s.

protected void recoverField (ClassDefinition newClass)

Recover after a syntax error in a field. This involves discarding tokens until an EOF or a possible legal continuation is encountered.

protected void recoverFile ()

Recover after a syntax error in the file. This involves discarding tokens until an EOF or a possible legal continuation is encountered.

Throws
IOException

protected boolean recoverStatement ()

Recover after a syntax error in a statement. This involves discarding tokens until EOF or a possible continuation is encountered.

protected final Statement[] statArgs (int index)

protected void topLevelExpression (Expression e)

Check if an expression is a legal toplevel expression. Only method, inc, dec, and new expression are allowed.