JavaCC Parser Generation
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Vijendra_Singh
Posted On:   Tuesday, March 7, 2006 01:15 AM

New Page 1 Can anyone help me out regarding this. I have to modify this file to do one specific thing. My aim is to ignore certain statement: i.e. create view (...........after that whole statement) If it is possible by modifying this SqlParser.jj file pleasegivemesoe hint or way to achive the target. Itgenerates all 7 files when gets executed by command Javacc SqlParser.jj so if anyone can do something plase let me know. Thanks Vijendra File is here Code: /** * Copyright (C) 2000 Ampersand Corporation *    More>>
New Page 1

Can anyone help me out regarding this.
I have to modify this file to do one specific thing.
My aim is to ignore certain statement:

i.e. create view (...........after that whole statement)

If it is possible by modifying this SqlParser.jj file pleasegivemesoe hint or way to achive the target.
Itgenerates all 7 files when gets executed by command
Javacc SqlParser.jj

so if anyone can do something plase let me know.

Thanks
Vijendra


File is here Code:


















/**

* Copyright (C) 2000 Ampersand Corporation

*

* Author: M. A. Sridhar

*

* Creation date: July 19th, 2000

*

* This is the JavaCC grammar definition file for the DDL dialect used by

* ZeroCode.

*/





// options {

// DEBUG_PARSER = true;

// }





PARSER_BEGIN(SqlParser)



package zerocode.sqlParser;



import com.sun.java.util.collections.*;

import java.io.*;

import java.util.Enumeration;



import zerocode.dbSupport.*;

import zerocode.core.*;







/**

* The SqlParser class encapsulates parsing of SQL DDL statements to enable

* zeroCode to construct the schema-related data structures. Most of its

* implementation is generated using the

* JavaCC

* application.

*/

public class SqlParser {





public interface ConstraintType {



public static final int UNIQUE = 1;



public static final int PRIMARY_KEY = 2;



}





public static void main (String args[]) throws Exception {

Schema dbSchema = buildSchema (new InputStreamReader (System.in));

System.out.println (dbSchema.toString());

}







public static synchronized Schema buildSchema (Reader stream)

throws ZcException {

_fkConstraints = new ArrayList();

_errorList = new Vector();

_dbSchema = new Schema ("ZeroCodeSchema");

if (_parser == null)

_parser = new SqlParser (stream);

else

_parser.ReInit (stream);

try {

_parser.ddlSequence ();

} catch (ParseException e) {

throw new ZcException

(ErrorCode.INVALID_SCHEMA, "SQL syntax error: " + e.getMessage());

}

_dbSchema.setConstraints (_fkConstraints);





if (_errorList.size() > 0)

throw new ZcException

(ErrorCode.INVALID_SCHEMA,

"Schema errors:
" + StringUtils.join (_errorList, "
"));

return _dbSchema;

}







private static void addTable (DbTable table) {

_dbSchema.addTable (table);

}







private static void addSequence (String sequenceName) {

_dbSchema.addSequence (sequenceName);

}







private static int convertToken (String message)

throws ZcException {

try {

int val = Integer.parseInt (token.toString());

return val;

} catch (NumberFormatException e) {

throw new ZcException

(ErrorCode.INVALID_SCHEMA,

message + "token '" + token.toString() + "' line " +

token.beginLine + " column " + token.beginColumn);

}

}



private static void setConstraint (DbTable table, String constraintName,

int constraintType,

StringSet columnNameSet) {

if (constraintType == ConstraintType.PRIMARY_KEY) {

int nCols = columnNameSet.size();

if (nCols > 1) {

// _errorList.add ("Table " + table.name() + ": must have " +

// "exactly one column as primary key.");

// We set the pri key anyway, to inhibit the subsequent

// "No primary key defined" error message.

table.setPrimaryKey (columnNameSet);

} else if (nCols <= 0) {

// _errorList.add ("Table " + table.name() + ": must have " +

// "a primary key.");

} else {

String colName = (String) columnNameSet.asArray()[0];

DbColumn col = table.columnWithName (colName);

if (col == null)

_errorList.add ("Table " + table.name() + ": Column '" +

colName + "', specified in primary key " +

"constraint, is not a declared column.");

else

table.setPrimaryKey (col.name());

}

} else {

// It should be a UNIQUE constraint

table.addUniqueConstraint

(new UniqueConstraint (constraintName, table, columnNameSet));

}

}





private static void addForeignKeyConstraint (String constraintName,

DbTable table,

StringSet columnNameSet,

String toTableName,

String toColName) {

if (columnNameSet.size() != 1) {

// We now silently ignore this problem -- MAS 1/7/2003

// _errorList.add ("Table " + table.name() + ": must have " +

// "exactly one column in foreign key.");

return;

}

String fkColName = columnNameSet.asArray()[0];

DbColumn fkCol = table.columnWithName (fkColName);

if (fkCol == null) {

_errorList.add ("Table " + table.name() +

": Cannot add foreign key constraint: no " +

"foreign key column named '" + fkColName + "'");

return;

}

DbTable toTable = _dbSchema.tableWithName (toTableName);

if (toTable == null) {

_errorList.add ("Table '" + toTableName + "' not found in schema.");

return;

}

DbColumn toColumn = toTable.columnWithName (toColName);

if (toColumn == null) {

_errorList.add ("Attempting to add foreign key from " +

table.name() + "(" +

columnNameSet.joinString(",") + ") to table '" +

toTableName + "': the latter has no column " +

"named '" + toColName + "'");

} else {

String constraintId = constraintName != null

? constraintName

: ("zcFkConstraint" + (++zcFkCount));

ForeignKeyConstraint constraint = new ForeignKeyConstraint

(constraintId, fkCol, toColumn, _fkConstraints.size());

_fkConstraints.add (constraint);

}

}



private static Schema _dbSchema;

private static Vector _errorList;

private static List _fkConstraints;

private static SqlParser _parser;



private static int zcFkCount = 0;

}



PARSER_END(SqlParser)



SKIP :

{

" "

|

" "

|

"
"

|

"
"

|

"--" : IN_LINE_COMMENT

|

"/*" : IN_COMMENT



}



SKIP:

{

"
" : DEFAULT

}



MORE:

{

< ~[] >

}



SKIP:

{

"*/" : DEFAULT

}



MORE:

{

< ~[] >

}







TOKEN [IGNORE_CASE] :

{

< ADD: "add" >

| < ALL: "all" >

| < ALTER: "alter" >

| < AND: "and" >

| < ANY: "any" >

| < ASC: "asc" >

| < BY: "by" >

| < CACHE: "cache" >

| < CASCADE: "cascade" >

| < CHECK: "check" >

| < COLUMN: "column" >

| < COMMENT: "comment" >

| < CONSTRAINT: "constraint" >

| < CONSTRAINTS: "constraints" >

| < CREATE: "create" >

| < CYCLE: "cycle" >

| < DEFAULT_TOK: "default" >

| < DELETE: "delete" >

| < DESC: "desc" >

| < DROP: "drop" >

| < FOREIGN: "foreign" >

| < IDENTITY: "identity" >

| < IN: "in" >

| < INCREMENT: "increment" >

| < INDEX: "index" >

| < IS: "is" >

| < KEY: "key" >

| < MAXVALUE: "maxvalue" >

| < MINVALUE: "minvalue" >

| < NOCACHE: "nocache" >

| < NOCYCLE: "nocycle" >

| < NOMAXVALUE: "nomaxvalue" >

| < NOMINVALUE: "nominvalue" >

| < NOORDER: "noorder" >

| < NOT: "not" >

| < NULL: "null" >

| < ON: "on" >

| < OR: "or" >

| < ORDER: "order" >

| < PRIMARY: "primary" >

| < REFERENCES: "references" >

| < SEQUENCE: "sequence" >

| < SOME: "some" >

| < START: "start" >

| < TABLE: "table" >

| < UNIQUE : "unique" >

| < UPDATE : "update" >

| < WITH: "with" >





| < COMMA: "," >

| < DOT: "." >

| < IDENTIFIER: ["a"-"z","A"-"Z", "_"] (["a"-"z", "_", "0"-"9", "$", "#"])* >

| < LPAREN: "(" >

| < NUMBER: (["0"-"9"])+ >

| < RPAREN: ")" >

| < SEMICOLON: ";" >

| < SLASH: "/" >

| < STRING_LITERAL:

(

"'"

(~["'","","
","
"])*

"'"

)+

>

}





String allowedColumnName () :

{}

{

(

LOOKAHEAD(2)

< IDENTIFIER >

| < ANY >

| < ASC >

| < BY >

| < CACHE >

| < CASCADE >

| < CHECK >

| < COLUMN >

| < COMMENT >

| < CONSTRAINTS >

| < CREATE >

| < CYCLE >

| < DEFAULT_TOK >

| < DELETE >

| < DESC >

| < DROP >

| < FOREIGN >

| < IDENTITY >

| < IN >

| < INCREMENT >

| < INDEX >

| < IS >

| < KEY >

| < MAXVALUE >

| < MINVALUE >

| < NOCACHE >

| < NOCYCLE >

| < NOMAXVALUE >

| < NOMINVALUE >

| < NOORDER >

| < NOT >

| < NULL >

| < ON >

| < OR >

| < ORDER >

| < PRIMARY >

| < REFERENCES >

| < SEQUENCE >

| < SOME >

| < START >

| < TABLE >

| < UNIQUE >

| < UPDATE >

| < WITH >

) { return token.toString(); }

}





void ddlSequence () throws ZcException :

{

DbTable table;

String sequenceName;

}



{

(

(

comment()

|

(



(

table = tableDefinition () {

addTable (table);

}



|

indexDefinition ()

|

sequenceName = sequenceDefinition ()

{

addSequence (sequenceName);

}

)

)

|

alterTable ()

|

dropTable()



)

(



|



)+

)+



}













void comment () :

{ }

{





(

(





)

|

(









)

)





}









DbTable tableDefinition () throws ZcException :

{

DbTable table;

DbColumn col;

}

{

{

table = new DbTable

(token.toString(), _dbSchema);

}





col = columnDefinition (table)

(



(

LOOKAHEAD(2)

col = columnDefinition (table)

|

constraint (table)

)



)*





{return table;}

}









DbColumn columnDefinition (DbTable table)

throws ZcException :

{

String columnName, columnType;

int size = 0, precision = 0;

boolean nullable = true;

DbColumn col;

}

{

(

columnName = allowedColumnName()

{columnType = token.toString();}

( {

columnType += " " + token.toString();

}

)?

(



{

size = convertToken ("Integer size expected");

}



(



{

precision = convertToken

("Integer precision expected");

}

)?



)? {

col = new DbColumn

(columnName, columnType, size, precision,

table.columnCount(), nullable, table);

table.addColumn (col);

}

(

columnConstraint(col)

)*

)

{return col;}

}







void columnConstraint (DbColumn col) throws ZcException:

{

boolean nullable = true;

String toTableName, toColumnName, constraintName;

int constraintType;

StringSet colNameSet = null;

}

{

(



{

constraintName = token.toString();

}

(

(



{

constraintType = ConstraintType.UNIQUE;

}

)

|



(



{

constraintType = ConstraintType.PRIMARY_KEY;

col.table().setPrimaryKey (col.name());

}

)



|



(

(





(



)? {

constraintName = token.toString();

}

)?



{

toTableName = token.toString();

DbTable toTable = _dbSchema.tableWithName

(toTableName);

if (toTable == null)

throw new ZcException

(ErrorCode.INTERNAL_ERROR,

"Foreign key reference to nonexistent " +

"table '" + toTableName + "': line " +

token.beginLine + ", column " + token.beginColumn);

toColumnName = toTable.primaryKeyName();

if (toColumnName == null)

throw new ZcException

(ErrorCode.INTERNAL_ERROR,

"Foreign key reference to table '" +

toTableName + "' with no primary key: line " +

token.beginLine + ", column " + token.beginColumn);

}

(



colNameSet = columnNameList()



)? {

DbTable table = col.table();

if (colNameSet != null && colNameSet.size() > 0){

if (colNameSet.size() > 1)

throw new ZcException

(ErrorCode.INVALID_SCHEMA,

"Column " + col + " refers to " +

"more than one column: line " +

token.beginLine + ", column " +

token.beginColumn);

toColumnName = colNameSet.asArray()[0];

}

StringSet colNames = new StringSet();

colNames.add (col.name());

addForeignKeyConstraint

(constraintName, table, colNames, toTableName, toColumnName);

}

)

)

)

|

(





checkCondition()



)

|

(

(

{

col.setNullable (false);

}

)?



)

|

(

defaultClause()

)

|

(



(











)?

{

col.setIdentity ();

}

)

}









void defaultClause () :

{}

{



defaultExpr()

}





void constraint (DbTable table) throws ZcException :

{

String constraintName = null;

int constraintType = 0;

StringSet colNameSet = null, refColNameSet = null;

String toTableName = null;

String toColumn = null;

}

{



(



{

constraintName = token.toString();

}

)?

(

(



(

{

constraintType = ConstraintType.UNIQUE;

}



|



(



{

constraintType = ConstraintType.PRIMARY_KEY;

}

)

)



colNameSet = columnNameList ()

{

setConstraint (table, constraintName,

constraintType, colNameSet);

}

)



|



(





(

{

constraintName = token.toString();

}

)?



colNameSet = columnNameList()





{

toTableName = token.toString();

DbTable toTable = _dbSchema.tableWithName (toTableName);

if (toTable == null)

throw new ZcException

(ErrorCode.INTERNAL_ERROR,

"Foreign key reference to nonexistent " +

"table '" + toTableName + "': line " +

token.beginLine + ", column " + token.beginColumn);

toColumn = toTable.primaryKeyName();

}

(



refColNameSet = columnNameList ()



)? {

if (refColNameSet != null && refColNameSet.size() > 0) {

toColumn = refColNameSet.asArray()[0];

}

addForeignKeyConstraint

(constraintName, table, colNameSet, toTableName, toColumn);

}

)

|

(





checkCondition()



)

)

}











StringSet columnNameList () :

{

StringSet columns = new StringSet();

}

{



{

columns.add (token.toString());

}

(



{

columns.add (token.toString());

}

)*



{return columns;}





}









void alterTable () throws ZcException :

{

String tableName = null;

DbTable table = null;

}

{





{

tableName = token.toString();

table = _dbSchema.tableWithName (tableName);

if (table == null)

throw new ZcException

(ErrorCode.INTERNAL_ERROR,

"Invalid ALTER TABLE: " +

"No table with name '" + tableName + "': line " +

token.beginLine + ", column " + token.beginColumn);

}



(

constraintClause (table)

(



constraintClause (table)

)*

|

(



constraintClause (table)



)

)

}







void constraintClause (DbTable table) throws ZcException:

{

}

{

constraint (table)

(



( | )



)?

}









void dropTable () :

{}

{







(





)?

}











void indexDefinition () :

{}

{

(



)?













(



|



)?

(





(



|



)?

)*



}





String sequenceDefinition () :

{

String name;

}

{



{ name = token.toString(); }

(

(



)

|

(



)

|

(



)

|



|



|



|



|

(



)

|



|

(



)

|



|



)*

{return name;}

}





void defaultExpr () :

{}

{



|



|

(



(



defaultExpr()

(



defaultExpr()

)*



)?

)

|

(



defaultExpr()



)

}





void checkCondition() :

{}

{

(



checkCondition()



)

|

(



checkCondition2()

)

|

(

checkCondition2()

(

(



|



)

checkCondition()

)?

)

}





void checkCondition2() :

{}

{

expr()

(

(

(

"="

|

"!="

|

">"

|

">="

|

" <"

|

" <="

)

(

expr()

|

(



|



|



)

exprList()

)

)

|

(



exprList()

)

)?

}





void expr () :

{}

{

(



|



|



)

(

(

"+"

|

"-"

|

"*"

|



)

expr()

)?

}



void exprList () :

{}

{



expr()

(



expr()

)*



}











 





   <<Less
About | Sitemap | Contact