org.overturetool.vdmj.syntax
Class SyntaxReader

java.lang.Object
  extended by org.overturetool.vdmj.syntax.SyntaxReader
Direct Known Subclasses:
BindReader, ClassReader, DefinitionReader, ExpressionReader, ModuleReader, OvertureReader, PatternReader, StatementReader, TypeReader

public abstract class SyntaxReader
extends java.lang.Object

The parent class of all syntax readers.


Field Summary
protected  BindReader bindReader
          A BindReader, if created.
protected  ClassReader classReader
          A ClassReader, if created.
protected  DefinitionReader definitionReader
          A DefinitionReader, if created.
protected  Dialect dialect
          The dialect of VDM that we're parsing.
protected  ExpressionReader expressionReader
          An ExpressionReader, if created.
protected  PatternReader patternReader
          A PatternReader, if created.
protected  LexTokenReader reader
          The lexical analyser.
protected  StatementReader statementReader
          A StatementReader, if created.
protected  TypeReader typeReader
          A TypeReader, if created.
 
Constructor Summary
protected SyntaxReader()
           
protected SyntaxReader(LexTokenReader reader)
          Create a reader with the given lexical analyser and VDM++ flag.
 
Method Summary
protected  void checkFor(Token tok, int number, java.lang.String message)
          If the last token is as expected, advance, else raise an error.
 void close()
           
protected  BindReader getBindReader()
           
protected  ClassReader getClassReader()
           
 java.lang.String getCurrentModule()
           
protected  DefinitionReader getDefinitionReader()
           
 int getErrorCount()
           
 java.util.List<VDMError> getErrors()
           
protected  ExpressionReader getExpressionReader()
           
protected  PatternReader getPatternReader()
           
protected  StatementReader getStatementReader()
           
protected  TypeReader getTypeReader()
           
 int getWarningCount()
           
 java.util.List<VDMWarning> getWarnings()
           
protected  LexNameToken idToName(LexIdentifierToken id)
          Convert an identifier into a name.
protected  boolean ignore(Token tok)
          If the last token is the one passed, advance by one, else do nothing.
protected  LexIdentifierToken lastIdToken()
          Return the last token, converted to a LexIdentifierToken.
protected  LexNameToken lastNameToken()
          Return the last token, converted to a LexNameToken.
protected  LexToken lastToken()
          Return the last token read by the lexical analyser without advancing.
protected  LexToken nextToken()
          Read the next token from the lexical analyser, and advance by one token.
 void printErrors(java.io.PrintWriter out)
          Print errors and warnings to the PrintWriter passed.
 void printWarnings(java.io.PrintWriter out)
           
protected  LexIdentifierToken readIdToken(java.lang.String message)
          Return the last token as an identifier, and advance by one token.
protected  LexNameToken readNameToken(java.lang.String message)
          Return the last token as a name, and advance by one token.
protected  LexToken readToken()
          Return the last token read, and also advance by one token.
protected  void report(LocatedException error, Token[] after, Token[] upto)
          Raise a syntax error and attempt to recover.
 void setCurrentModule(java.lang.String module)
          Set the name of the current module or class.
protected  void throwMessage(int number, java.lang.String message)
          Raise a ParserException at the current location.
protected  void throwMessage(int number, java.lang.String message, LexToken token)
          Raise a ParserException at the location of the token passed in.
 java.lang.String toString()
           
protected  void warning(int no, java.lang.String msg, LexLocation location)
          Report a warning.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

reader

protected final LexTokenReader reader
The lexical analyser.


dialect

protected final Dialect dialect
The dialect of VDM that we're parsing.


definitionReader

protected DefinitionReader definitionReader
A DefinitionReader, if created.


expressionReader

protected ExpressionReader expressionReader
An ExpressionReader, if created.


patternReader

protected PatternReader patternReader
A PatternReader, if created.


typeReader

protected TypeReader typeReader
A TypeReader, if created.


bindReader

protected BindReader bindReader
A BindReader, if created.


statementReader

protected StatementReader statementReader
A StatementReader, if created.


classReader

protected ClassReader classReader
A ClassReader, if created.

Constructor Detail

SyntaxReader

protected SyntaxReader(LexTokenReader reader)
Create a reader with the given lexical analyser and VDM++ flag.


SyntaxReader

protected SyntaxReader()
Method Detail

nextToken

protected LexToken nextToken()
                      throws LexException
Read the next token from the lexical analyser, and advance by one token.

Returns:
The next token.
Throws:
LexException

lastToken

protected LexToken lastToken()
                      throws LexException
Return the last token read by the lexical analyser without advancing. Repeated calls to this method will return the same result.

Returns:
The last token again.
Throws:
LexException

readToken

protected LexToken readToken()
                      throws LexException
Return the last token read, and also advance by one token. This is equivalent to calling lastToken() followed by nextToken(), but returning the result of lastToken.

Returns:
The last token.
Throws:
LexException

setCurrentModule

public void setCurrentModule(java.lang.String module)
Set the name of the current module or class. Unqualified symbol names use this as their module/class name. See idToName(org.overturetool.vdmj.lex.LexIdentifierToken).

Parameters:
module -

getCurrentModule

public java.lang.String getCurrentModule()
Returns:
The current module/class name.

idToName

protected LexNameToken idToName(LexIdentifierToken id)
Convert an identifier into a name. A name is an identifier that has a module name qualifier, so this method uses the current module to convert the identifier passed in.

Parameters:
id - The identifier to convert
Returns:
The corresponding name.

lastIdToken

protected LexIdentifierToken lastIdToken()
                                  throws ParserException,
                                         LexException
Return the last token, converted to a LexIdentifierToken. If the last token is not an identifier token, an exception is thrown with the message passed in.

Returns:
The last token as a LexIdentifierToken.
Throws:
LexException
ParserException

lastNameToken

protected LexNameToken lastNameToken()
                              throws LexException,
                                     ParserException
Return the last token, converted to a LexNameToken. If the last token is not a name token, or an identifier token that can be converted to a name, an exception is thrown with the message passed in.

Returns:
The last token as a LexIdentifierToken.
Throws:
LexException
ParserException

readIdToken

protected LexIdentifierToken readIdToken(java.lang.String message)
                                  throws LexException,
                                         ParserException
Return the last token as an identifier, and advance by one token. This is similar to calling lastIdToken() followed by nextToken, and returning the result of the lastIdToken.

Parameters:
message - The message to throw if the last token is not an id.
Returns:
The last token as a LexIdentifierToken.
Throws:
LexException
ParserException

readNameToken

protected LexNameToken readNameToken(java.lang.String message)
                              throws LexException,
                                     ParserException
Return the last token as a name, and advance by one token. This is similar to calling lastNameToken() followed by nextToken, and returning the result of the lastNameToken.

Parameters:
message - The message to throw if the last token is not a name.
Returns:
The last token as a LexNameToken.
Throws:
LexException
ParserException

getDefinitionReader

protected DefinitionReader getDefinitionReader()
Returns:
A new DefinitionReader.

getExpressionReader

protected ExpressionReader getExpressionReader()
Returns:
A new DefinitionReader.

getPatternReader

protected PatternReader getPatternReader()
Returns:
A new PatternReader.

getTypeReader

protected TypeReader getTypeReader()
Returns:
A new TypeReader.

getBindReader

protected BindReader getBindReader()
Returns:
A new BindReader.

getStatementReader

protected StatementReader getStatementReader()
Returns:
A new StatementReader.

getClassReader

protected ClassReader getClassReader()
Returns:
A new ClassReader.

close

public void close()
           throws java.io.IOException
Throws:
java.io.IOException

checkFor

protected void checkFor(Token tok,
                        int number,
                        java.lang.String message)
                 throws LexException,
                        ParserException
If the last token is as expected, advance, else raise an error.

Parameters:
tok - The token type to check for.
number - The error number.
message - The error message to raise if the token is not as expected.
Throws:
LexException
ParserException

ignore

protected boolean ignore(Token tok)
                  throws LexException
If the last token is the one passed, advance by one, else do nothing.

Parameters:
tok - The token type to check for.
Returns:
True if the token was skipped.
Throws:
LexException

throwMessage

protected void throwMessage(int number,
                            java.lang.String message)
                     throws ParserException,
                            LexException
Raise a ParserException at the current location.

Parameters:
number - The error number.
message - The error message.
Throws:
ParserException
LexException

throwMessage

protected void throwMessage(int number,
                            java.lang.String message,
                            LexToken token)
                     throws ParserException
Raise a ParserException at the location of the token passed in.

Parameters:
number - The error number.
message - The error message.
token - The location of the error.
Throws:
ParserException

report

protected void report(LocatedException error,
                      Token[] after,
                      Token[] upto)
Raise a syntax error and attempt to recover. The error is added to the errors list, and if this exceeds 100 errors the parser is aborted. The "after" and "upto" lists of token types are then used to control the advance of the parser to skip beyond the error. Tokens are read until one occurs in either list or EOF is reached. If the token is in the "after" list, one more token is read before returning; if it is in the "upto" list, the last token is left pointing to the token before returning. If EOF is reached, the method returns.

Parameters:
error - The exception that caused the error.
after - A list of tokens to recover to, and step one beyond.
upto - A list of tokens to recover to.

warning

protected void warning(int no,
                       java.lang.String msg,
                       LexLocation location)
Report a warning. Unlike errors, this does no token recovery.


getErrorCount

public int getErrorCount()
Returns:
The error count from all readers that can raise errors.

getErrors

public java.util.List<VDMError> getErrors()
Returns:
The errors from all readers that can raise errors.

getWarningCount

public int getWarningCount()
Returns:
The warning count from all readers that can raise warnings.

getWarnings

public java.util.List<VDMWarning> getWarnings()
Returns:
The warnings from all readers that can raise warnings.

printErrors

public void printErrors(java.io.PrintWriter out)
Print errors and warnings to the PrintWriter passed.


printWarnings

public void printWarnings(java.io.PrintWriter out)

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object


Copyright © 2009. All Rights Reserved.