jeops.compiler
Class Main

java.lang.Object
  |
  +--jeops.compiler.Main

public class Main
extends java.lang.Object
implements TokenConstants

Main class in the Jeops phase of converting rule files into Java classes.

When invoked, the method convert() will create a java file in the same directory as the rule file (with the extension changed from .rules to .java). In this way, rule files can be grouped into packages in the same way as java classes.

Version:
0.02 14.01.2000 Grouping of the conditions according to the appearance of the declaration.
Author:
Carlos Figueira Filho (csff@cin.ufpe.br)

Field Summary
private  java.util.Vector allDeclForLocalDecl
          The index of all declared identifiers used by some rule local declaration.
private  java.util.Vector convertedRuleDeclarations
          The converted declarations of the current rule.
private  java.util.Vector convertedRuleLocalDeclarations
          The converted local declarations of the current rule.
private  java.util.Vector declaredClassNames
          The classes of the identifiers generated in the new java file.
private  ImportList importList
          The import list of this rule base.
private  java.lang.String javaFileName
          The name of the generated java file.
private  java.util.Vector lastDeclForLocalDecl
          The index of the last declared identifier used by some rule local declaration.
private  java.util.Vector localDeclClasses
          Vector (of vectors) of the classes of the local declarations.
private  java.util.Vector numberOfConditions
          The number of conditions in each rule.
private  java.util.Vector numberOfDeclarations
          The number of declarations in each rule.
private  java.util.Vector numberOfLocalDeclarations
          The number of local declarations in each rule.
private  java.lang.String ruleBaseName
          The name of this rule base.
private  java.util.Vector ruleDeclarations
          The declarations of the current rule.
private  java.lang.String ruleFileName
          The name of the rules file.
private  java.util.Vector ruleLocalDeclarations
          The local declarations of the current rule.
private  java.lang.String ruleName
          The name of the rule being converted.
private  java.util.Vector ruleNames
          The names of the rules.
private  Scanner scanner
          The scanner used to read from the input file.
private  boolean thereIsRuleBase
          Flag that indicates whether a rule base is declared in the file.
private  Token token
          The current token.
private  java.util.Vector variableIdents
          The list of the identifiers of the instance variables of the generated java file.
private  java.util.Vector variables
          The list of declaration of instance variables of the generated java file.
private  java.io.PrintWriter writer
          The writer used to create the java file.
 
Fields inherited from interface jeops.compiler.parser.TokenConstants
ACTIONS, ASTERISK, CLOSE_CURLY_BRACKET, COMMA, COMMENT, CONDITIONS, DECLARATIONS, DOT, EOF, EQUALS, ERROR, EXTENDS, IDENT, IMPLEMENTS, IMPORT, LOCALDECL, NONE_ABOVE, OPEN_CURLY_BRACKET, PACKAGE, PUBLIC, RULE, RULE_BASE, SEMICOLON, STRING, WHITE_SPACE
 
Constructor Summary
Main(java.lang.String ruleFileName)
          Class constructor.
 
Method Summary
private  void checkToken(Token token, int[] tokenTypes, java.lang.String message)
          Checks whether a token is one of several given types, throwing an exception if it's not.
private  void checkToken(Token token, int tokenType, java.lang.String message)
          Checks whether a token is of a given type, throwing an exception if it's not.
private  boolean compileSourceFile(java.lang.String className)
          Attempts to find a source file and compile it into a .class file.
 void convert()
          Creates a java source file that behaves as defined in the rules file.
private  void convertRule(java.io.PrintWriter writer)
          Converts a rule into a set of java methods.
private  void convertRuleActions(java.io.PrintWriter writer)
          Processes the actions part of a rule.
private  void convertRuleConditions(java.io.PrintWriter writer)
          Processes the conditions part of a rule.
private  void convertRuleDeclarations(java.io.PrintWriter writer)
          Processes the declaration part of a rule.
private  void convertRuleLocalDeclarations(java.io.PrintWriter writer)
          Processes the local declarations part of a rule.
static void main(java.lang.String[] args)
          Test method for this class.
private  void processEndOfRuleBase(java.io.PrintWriter writer)
          Processes the end of the rule base, adding control information.
private  void processImport(java.io.PrintWriter writer)
          Processes an import statement of a rule base.
private  void processPackage(java.io.PrintWriter writer)
          Processes the package statement of a rule base.
private  Token readNextToken()
          Reads the next token from the scanner, throwing an exception in case of an error.
private  void skipWhiteSpace(java.io.PrintWriter writer, boolean outputComments)
          Auxiliar method used to advance through white space and comments.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

writer

private java.io.PrintWriter writer
The writer used to create the java file.

ruleFileName

private java.lang.String ruleFileName
The name of the rules file.

javaFileName

private java.lang.String javaFileName
The name of the generated java file.

ruleBaseName

private java.lang.String ruleBaseName
The name of this rule base.

scanner

private Scanner scanner
The scanner used to read from the input file.

token

private Token token
The current token.

variables

private java.util.Vector variables
The list of declaration of instance variables of the generated java file.

variableIdents

private java.util.Vector variableIdents
The list of the identifiers of the instance variables of the generated java file.

declaredClassNames

private java.util.Vector declaredClassNames
The classes of the identifiers generated in the new java file.

ruleName

private java.lang.String ruleName
The name of the rule being converted.

numberOfDeclarations

private java.util.Vector numberOfDeclarations
The number of declarations in each rule.

numberOfLocalDeclarations

private java.util.Vector numberOfLocalDeclarations
The number of local declarations in each rule.

numberOfConditions

private java.util.Vector numberOfConditions
The number of conditions in each rule.

ruleNames

private java.util.Vector ruleNames
The names of the rules.

importList

private ImportList importList
The import list of this rule base.

ruleDeclarations

private java.util.Vector ruleDeclarations
The declarations of the current rule.

convertedRuleDeclarations

private java.util.Vector convertedRuleDeclarations
The converted declarations of the current rule.

ruleLocalDeclarations

private java.util.Vector ruleLocalDeclarations
The local declarations of the current rule.

convertedRuleLocalDeclarations

private java.util.Vector convertedRuleLocalDeclarations
The converted local declarations of the current rule.

lastDeclForLocalDecl

private java.util.Vector lastDeclForLocalDecl
The index of the last declared identifier used by some rule local declaration.

allDeclForLocalDecl

private java.util.Vector allDeclForLocalDecl
The index of all declared identifiers used by some rule local declaration. It's a vector of Integer Vectors.

thereIsRuleBase

private boolean thereIsRuleBase
Flag that indicates whether a rule base is declared in the file.

localDeclClasses

private java.util.Vector localDeclClasses
Vector (of vectors) of the classes of the local declarations.
Constructor Detail

Main

public Main(java.lang.String ruleFileName)
     throws java.io.IOException
Class constructor.
Parameters:
ruleFileName - the name of the rules file.
Throws:
java.io.IOException - if some IO error occurs.
Method Detail

checkToken

private void checkToken(Token token,
                        int[] tokenTypes,
                        java.lang.String message)
                 throws JeopsException
Checks whether a token is one of several given types, throwing an exception if it's not.
Parameters:
token - the token to be checked.
types - the desired token types.
message - the message in the exception.
Throws:
JeopsException - if the token is of an unexpected type.

checkToken

private void checkToken(Token token,
                        int tokenType,
                        java.lang.String message)
                 throws JeopsException
Checks whether a token is of a given type, throwing an exception if it's not.
Parameters:
token - the token to be checked.
type - the desired token type.
message - the message in the exception.
Throws:
JeopsException - if the token is of an unexpected type.

compileSourceFile

private boolean compileSourceFile(java.lang.String className)
                           throws JeopsException
Attempts to find a source file and compile it into a .class file.
Parameters:
className - the name of the class to be compiled.
Returns:
true if the compilation could be performed; false otherwise.
Throws:
JeopsException - if some error occurred during the compilation.

convert

public void convert()
             throws java.io.IOException,
                    JeopsException
Creates a java source file that behaves as defined in the rules file.
Throws:
java.io.IOException - if some IO error occurs.
JeopsException - if some error occurs while converting the rule.

convertRule

private void convertRule(java.io.PrintWriter writer)
                  throws java.io.IOException,
                         JeopsException
Converts a rule into a set of java methods.
Parameters:
writer - the print writer used to write to the generated file.
Throws:
java.io.IOException - if some IO error occurs.
JeopsException - if some error occurs while converting the rule.

convertRuleActions

private void convertRuleActions(java.io.PrintWriter writer)
                         throws java.io.IOException,
                                JeopsException
Processes the actions part of a rule. This method creates one void method for the whole body of actions of the rule. The method will have the same name of the rule.

In order to be correctly invoked, the current token type must be ACTIONS. After successful completion, the token type is CLOSE_CURLY_BRACKET, indicating the end of the rule.

Parameters:
writer - the print writer used to write to the generated file.
Throws:
java.io.IOException - if some IO error occurs.
JeopsException - if some error occurs while converting the rule.

convertRuleConditions

private void convertRuleConditions(java.io.PrintWriter writer)
                            throws java.io.IOException,
                                   JeopsException
Processes the conditions part of a rule. This method creates one method (returning a boolean value) for every condition of the rule. Each method will be named as the name of the rule followed by "_cond_", where n is the index of the condition. Additionally, another method will be created, named as the name of the rule followed only by "_cond", that will receive an integer used to check the correct condition.

In order to be correctly invoked, the current token type must be CONDITIONS. After successful completion, the token type is ACTIONS.

Parameters:
writer - the print writer used to write to the generated file.
Throws:
java.io.IOException - if some IO error occurs.
JeopsException - if some error occurs while converting the rule.

convertRuleDeclarations

private void convertRuleDeclarations(java.io.PrintWriter writer)
                              throws java.io.IOException,
                                     JeopsException
Processes the declaration part of a rule. This method fills the ruleDeclaration and convertedRuleDeclaration fields with the declared variables and their counterpart in the generated java file. It will also fill the condGrouping field.

This method also deals with the optional section of local declarations of the rule, so that it guarantees that after this method is successfully invoked, the token type is CONDITIONS

. In order to be correctly invoked, the current token type must be DECLARATIONS. After successful completion, the token type is CONDITIONS.

Parameters:
writer - the print writer used to write to the generated file.
Throws:
java.io.IOException - if some IO error occurs.
JeopsException - if some error occurs while converting the rule.

convertRuleLocalDeclarations

private void convertRuleLocalDeclarations(java.io.PrintWriter writer)
                                   throws java.io.IOException,
                                          JeopsException
Processes the local declarations part of a rule. This method continues filling the ruleLocalDeclaration and convertedRuleLocalDeclaration fields with the locally declared variables and their counterpart in the generated java file.

In order to be correctly invoked, the current token type must be LOCALDECL. After successful completion, the token type is CONDITIONS.

Parameters:
writer - the print writer used to write to the generated file.
Throws:
java.io.IOException - if some IO error occurs.
JeopsException - if some error occurs while converting the rule.

main

public static void main(java.lang.String[] args)
Test method for this class.
Parameters:
args - command-line arguments. None is needed, but one can pass the rule file name for the converting.

processEndOfRuleBase

private void processEndOfRuleBase(java.io.PrintWriter writer)
                           throws java.io.IOException
Processes the end of the rule base, adding control information.
Parameters:
writer - the print writer used to write to the generated file.
Throws:
java.io.IOException - if some IO error occurs.

processImport

private void processImport(java.io.PrintWriter writer)
                    throws java.io.IOException,
                           JeopsException
Processes an import statement of a rule base.
Parameters:
writer - the print writer used to write to the generated file.
Throws:
java.io.IOException - if some IO error occurs.
JeopsException - if some error occurs while converting the rule.

processPackage

private void processPackage(java.io.PrintWriter writer)
                     throws java.io.IOException,
                            JeopsException
Processes the package statement of a rule base.
Parameters:
writer - the print writer used to write to the generated file.
Throws:
java.io.IOException - if some IO error occurs.
JeopsException - if some error occurs while converting the rule.

readNextToken

private Token readNextToken()
                     throws java.io.IOException,
                            JeopsException
Reads the next token from the scanner, throwing an exception in case of an error.
Returns:
the token read from the scanner.
Throws:
java.io.IOException - if some IO error occurs.
JeopsException - is the token read is an error.

skipWhiteSpace

private void skipWhiteSpace(java.io.PrintWriter writer,
                            boolean outputComments)
                     throws java.io.IOException
Auxiliar method used to advance through white space and comments. The caller must indicate whether the comments should be written to the output file. This method throws an error if EOF is found.
Parameters:
writer - the print writer used to write to the generated file.
outputComments - if the white space and comments should be written to the output file
Throws:
java.io.IOException - if some IO error occurs.