blob: 0b1a7c1441120f290b12b255a38ca01eb05a7ceb [file] [log] [blame]
options {
STATIC = false;
}
PARSER_BEGIN(AQLParser)
package edu.uci.ics.asterix.aql.parser;
import java.io.*;
import java.util.List;
import java.util.ArrayList;
import java.util.Stack;
import java.util.Map;
import java.util.HashMap;
import java.util.LinkedHashMap;
import edu.uci.ics.asterix.aql.literal.FloatLiteral;
import edu.uci.ics.asterix.aql.literal.DoubleLiteral;
import edu.uci.ics.asterix.aql.literal.FalseLiteral;
import edu.uci.ics.asterix.aql.base.Literal;
import edu.uci.ics.asterix.aql.literal.IntegerLiteral;
import edu.uci.ics.asterix.aql.literal.LongIntegerLiteral;
import edu.uci.ics.asterix.aql.literal.NullLiteral;
import edu.uci.ics.asterix.aql.literal.StringLiteral;
import edu.uci.ics.asterix.aql.literal.TrueLiteral;
import edu.uci.ics.asterix.metadata.bootstrap.MetadataConstants;
import edu.uci.ics.asterix.aql.base.*;
import edu.uci.ics.asterix.aql.expression.*;
import edu.uci.ics.asterix.common.config.DatasetConfig.DatasetType;
import edu.uci.ics.asterix.common.config.DatasetConfig.IndexType;
import edu.uci.ics.asterix.aql.expression.visitor.AQLPrintVisitor;
import edu.uci.ics.asterix.aql.expression.UnaryExpr.Sign;
import edu.uci.ics.asterix.aql.base.Statement.Kind;
import edu.uci.ics.asterix.aql.context.Scope;
import edu.uci.ics.asterix.aql.context.RootScopeFactory;
import edu.uci.ics.asterix.common.annotations.*;
import edu.uci.ics.asterix.common.exceptions.AsterixException;
import edu.uci.ics.asterix.om.functions.AsterixFunction;
import edu.uci.ics.asterix.common.functions.FunctionSignature;
import edu.uci.ics.hyracks.algebricks.core.algebra.expressions.IExpressionAnnotation;
import edu.uci.ics.hyracks.algebricks.core.algebra.expressions.IndexedNLJoinExpressionAnnotation;
import edu.uci.ics.hyracks.algebricks.core.algebra.functions.FunctionIdentifier;
import edu.uci.ics.hyracks.algebricks.common.utils.Pair;
import edu.uci.ics.hyracks.algebricks.common.utils.Triple;
public class AQLParser extends ScopeChecker {
// optimizer hints
private static final String HASH_GROUP_BY_HINT = "hash";
private static final String BROADCAST_JOIN_HINT = "bcast";
private static final String INDEXED_NESTED_LOOP_JOIN_HINT = "indexnl";
private static final String INMEMORY_HINT = "inmem";
private static final String VAL_FILE_HINT = "val-files";
private static final String VAL_FILE_SAME_INDEX_HINT = "val-file-same-idx";
private static final String INTERVAL_HINT = "interval";
private static final String COMPOSE_VAL_FILES_HINT = "compose-val-files";
private static final String INSERT_RAND_INT_HINT = "insert-rand-int";
private static final String LIST_VAL_FILE_HINT = "list-val-file";
private static final String LIST_HINT = "list";
private static final String DATETIME_BETWEEN_YEARS_HINT = "datetime-between-years";
private static final String DATE_BETWEEN_YEARS_HINT = "date-between-years";
private static final String DATETIME_ADD_RAND_HOURS_HINT = "datetime-add-rand-hours";
private static final String AUTO_HINT = "auto";
private static final String GEN_FIELDS_HINT = "gen-fields";
// data generator hints
private static final String DGEN_HINT = "dgen";
private static class IndexParams {
public IndexType type;
public int gramLength;
public IndexParams(IndexType type, int gramLength) {
this.type = type;
this.gramLength = gramLength;
}
};
private static class FunctionName {
public String dataverse = null;
public String library = null;
public String function = null;
}
private static String getHint(Token t) {
if (t.specialToken == null) {
return null;
}
String s = t.specialToken.image;
int n = s.length();
if (n < 2) {
return null;
}
return s.substring(1).trim();
}
public AQLParser(String s){
this(new StringReader(s));
super.setInput(s);
}
public static void main(String args[]) throws ParseException, TokenMgrError, IOException, FileNotFoundException, AsterixException {
File file = new File(args[0]);
Reader fis = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
AQLParser parser = new AQLParser(fis);
List<Statement> st = parser.Statement();
//st.accept(new AQLPrintVisitor(), 0);
}
}
PARSER_END(AQLParser)
List<Statement> Statement() throws ParseException:
{
scopeStack.push(RootScopeFactory.createRootScope(this));
List<Statement> decls = new ArrayList<Statement>();
Statement stmt = null;
}
{
( stmt = SingleStatement() (";") ?
{
decls.add(stmt);
}
)*
<EOF>
{
return decls;
}
}
Statement SingleStatement() throws ParseException:
{
Statement stmt = null;
}
{
(
stmt = DataverseDeclaration()
| stmt = FunctionDeclaration()
| stmt = CreateStatement()
| stmt = LoadStatement()
| stmt = DropStatement()
| stmt = WriteStatement()
| stmt = SetStatement()
| stmt = InsertStatement()
| stmt = DeleteStatement()
| stmt = UpdateStatement()
| stmt = FeedStatement()
| stmt = Query()
)
{
return stmt;
}
}
DataverseDecl DataverseDeclaration() throws ParseException:
{
String dvName = null;
}
{
"use" "dataverse" dvName = Identifier()
{
defaultDataverse = dvName;
return new DataverseDecl(new Identifier(dvName));
}
}
Statement CreateStatement() throws ParseException:
{
String hint = null;
boolean dgen = false;
Statement stmt = null;
}
{
"create"
(
{
hint = getHint(token);
if (hint != null && hint.startsWith(DGEN_HINT)) {
dgen = true;
}
}
stmt = TypeSpecification(hint, dgen)
| stmt = NodegroupSpecification()
| stmt = DatasetSpecification()
| stmt = IndexSpecification()
| stmt = DataverseSpecification()
| stmt = FunctionSpecification()
)
{
return stmt;
}
}
TypeDecl TypeSpecification(String hint, boolean dgen) throws ParseException:
{
Pair<Identifier,Identifier> nameComponents = null;
boolean ifNotExists = false;
TypeExpression typeExpr = null;
}
{
"type" nameComponents = TypeName() ifNotExists = IfNotExists()
"as" typeExpr = TypeExpr()
{
long numValues = -1;
String filename = null;
if (dgen) {
String splits[] = hint.split(" +");
if (splits.length != 3) {
throw new ParseException("Expecting /*+ dgen <filename> <numberOfItems> */");
}
filename = splits[1];
numValues = Long.parseLong(splits[2]);
}
TypeDataGen tddg = new TypeDataGen(dgen, filename, numValues);
return new TypeDecl(nameComponents.first, nameComponents.second, typeExpr, tddg, ifNotExists);
}
}
NodegroupDecl NodegroupSpecification() throws ParseException:
{
String name = null;
String tmp = null;
boolean ifNotExists = false;
List<Identifier>ncNames = null;
}
{
"nodegroup" name = Identifier()
ifNotExists = IfNotExists() "on" tmp = Identifier()
{
ncNames = new ArrayList<Identifier>();
ncNames.add(new Identifier(tmp));
}
( "," tmp = Identifier()
{
ncNames.add(new Identifier(tmp));
}
)*
{
return new NodegroupDecl(new Identifier(name), ncNames, ifNotExists);
}
}
DatasetDecl DatasetSpecification() throws ParseException:
{
Pair<Identifier,Identifier> nameComponents = null;
boolean ifNotExists = false;
String typeName = null;
String adapterName = null;
Map<String,String> properties = null;
FunctionSignature appliedFunction = null;
List<String> primaryKeyFields = null;
String nodeGroupName = null;
Map<String,String> hints = new HashMap<String,String>();
DatasetDecl dsetDecl = null;
}
{
(
"external" <DATASET> nameComponents = QualifiedName()
<LEFTPAREN> typeName = Identifier() <RIGHTPAREN>
ifNotExists = IfNotExists()
"using" adapterName = AdapterName() properties = Configuration()
( "hints" hints = Properties() )?
{
ExternalDetailsDecl edd = new ExternalDetailsDecl();
edd.setAdapter(adapterName);
edd.setProperties(properties);
dsetDecl = new DatasetDecl(nameComponents.first,
nameComponents.second,
new Identifier(typeName),
hints,
DatasetType.EXTERNAL,
edd,
ifNotExists);
}
| "feed" <DATASET> nameComponents = QualifiedName()
<LEFTPAREN> typeName = Identifier() <RIGHTPAREN>
ifNotExists = IfNotExists()
"using" adapterName = AdapterName() properties = Configuration()
(appliedFunction = ApplyFunction())? primaryKeyFields = PrimaryKey()
( "on" nodeGroupName = Identifier() )?
( "hints" hints = Properties() )?
{
FeedDetailsDecl fdd = new FeedDetailsDecl(adapterName,
properties,
appliedFunction,
nodeGroupName != null
? new Identifier(nodeGroupName)
: null,
primaryKeyFields);
dsetDecl = new DatasetDecl(nameComponents.first,
nameComponents.second,
new Identifier(typeName),
hints,
DatasetType.FEED,
fdd,
ifNotExists);
}
| ("internal")? <DATASET> nameComponents = QualifiedName()
<LEFTPAREN> typeName = Identifier() <RIGHTPAREN>
ifNotExists = IfNotExists()
primaryKeyFields = PrimaryKey() ("on" nodeGroupName = Identifier() )?
( "hints" hints = Properties() )?
{
InternalDetailsDecl idd = new InternalDetailsDecl(nodeGroupName != null
? new Identifier(nodeGroupName)
: null,
primaryKeyFields);
dsetDecl = new DatasetDecl(nameComponents.first,
nameComponents.second,
new Identifier(typeName),
hints,
DatasetType.INTERNAL,
idd,
ifNotExists);
}
)
{
return dsetDecl;
}
}
CreateIndexStatement IndexSpecification() throws ParseException:
{
CreateIndexStatement cis = new CreateIndexStatement();
String indexName = null;
String fieldExpr = null;
boolean ifNotExists = false;
Pair<Identifier,Identifier> nameComponents = null;
IndexParams indexType = null;
}
{
"index" indexName = Identifier()
ifNotExists = IfNotExists()
"on" nameComponents = QualifiedName()
<LEFTPAREN> ( fieldExpr = Identifier()
{
cis.addFieldExpr(fieldExpr);
}
) ("," fieldExpr = Identifier()
{
cis.addFieldExpr(fieldExpr);
}
)* <RIGHTPAREN> ( "type" indexType = IndexType() )?
{
cis.setIndexName(new Identifier(indexName));
cis.setIfNotExists(ifNotExists);
cis.setDataverseName(nameComponents.first);
cis.setDatasetName(nameComponents.second);
if (indexType != null) {
cis.setIndexType(indexType.type);
cis.setGramLength(indexType.gramLength);
}
return cis;
}
}
IndexParams IndexType() throws ParseException:
{
IndexType type = null;
int gramLength = 0;
}
{
("btree"
{
type = IndexType.BTREE;
}
| "rtree"
{
type = IndexType.RTREE;
}
| "keyword"
{
type = IndexType.FUZZY_WORD_INVIX;
}
| "ngram" <LEFTPAREN> <INTEGER_LITERAL>
{
type = IndexType.FUZZY_NGRAM_INVIX;
gramLength = Integer.valueOf(token.image);
}
<RIGHTPAREN>)
{
return new IndexParams(type, gramLength);
}
}
CreateDataverseStatement DataverseSpecification() throws ParseException :
{
String dvName = null;
boolean ifNotExists = false;
String format = null;
}
{
"dataverse" dvName = Identifier()
ifNotExists = IfNotExists()
( "with format" format = StringLiteral() )?
{
return new CreateDataverseStatement(new Identifier(dvName), format, ifNotExists);
}
}
CreateFunctionStatement FunctionSpecification() throws ParseException:
{
FunctionSignature signature;
boolean ifNotExists = false;
List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
String functionBody;
VarIdentifier var = null;
Expression functionBodyExpr;
Token beginPos;
Token endPos;
FunctionName fctName = null;
createNewScope();
}
{
"function" fctName = FunctionName()
ifNotExists = IfNotExists()
<LEFTPAREN> (<VARIABLE>
{
var = new VarIdentifier();
var.setValue(token.image);
paramList.add(var);
getCurrentScope().addNewVarSymbolToScope(var);
}
("," <VARIABLE>
{
var = new VarIdentifier();
var.setValue(token.image);
paramList.add(var);
getCurrentScope().addNewVarSymbolToScope(var);
}
)*)? <RIGHTPAREN> "{"
{
beginPos = token;
}
functionBodyExpr = Expression() "}"
{
endPos = token;
functionBody = extractFragment(beginPos.beginLine, beginPos.beginColumn, endPos.beginLine, endPos.beginColumn);
// TODO use fctName.library
signature = new FunctionSignature(fctName.dataverse, fctName.function, paramList.size());
getCurrentScope().addFunctionDescriptor(signature, false);
return new CreateFunctionStatement(signature, paramList, functionBody, ifNotExists);
}
}
List<VarIdentifier> ParameterList() throws ParseException:
{
List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
VarIdentifier var = null;
}
{
<LEFTPAREN> (<VARIABLE>
{
var = new VarIdentifier();
var.setValue(token.image);
paramList.add(var);
getCurrentScope().addNewVarSymbolToScope(var);
}
("," <VARIABLE>
{
var = new VarIdentifier();
var.setValue(token.image);
paramList.add(var);
getCurrentScope().addNewVarSymbolToScope(var);
}
)*)? <RIGHTPAREN>
{
return paramList;
}
}
boolean IfNotExists() throws ParseException:
{
}
{
( "if not exists"
{
return true;
}
)?
{
return false;
}
}
FunctionSignature ApplyFunction() throws ParseException:
{
FunctionSignature funcSig = null;
}
{
"apply" "function" funcSig = FunctionSignature()
{
return funcSig;
}
}
String GetPolicy() throws ParseException:
{
String policy = null;
}
{
"using" "policy" policy = Identifier()
{
return policy;
}
}
FunctionSignature FunctionSignature() throws ParseException:
{
FunctionName fctName = null;
int arity = 0;
}
{
fctName = FunctionName() "@" <INTEGER_LITERAL>
{
arity = new Integer(token.image);
if (arity < 0 && arity != FunctionIdentifier.VARARGS) {
throw new ParseException(" invalid arity:" + arity);
}
// TODO use fctName.library
return new FunctionSignature(fctName.dataverse, fctName.function, arity);
}
}
List<String> PrimaryKey() throws ParseException:
{
String tmp = null;
List<String> primaryKeyFields = new ArrayList<String>();
}
{
"primary" "key" tmp = Identifier()
{
primaryKeyFields.add(tmp);
}
( "," tmp = Identifier()
{
primaryKeyFields.add(tmp);
}
)*
{
return primaryKeyFields;
}
}
Statement DropStatement() throws ParseException:
{
String id = null;
Pair<Identifier,Identifier> pairId = null;
Triple<Identifier,Identifier,Identifier> tripleId = null;
FunctionSignature funcSig = null;
boolean ifExists = false;
Statement stmt = null;
}
{
"drop"
(
<DATASET> pairId = QualifiedName() ifExists = IfExists()
{
stmt = new DropStatement(pairId.first, pairId.second, ifExists);
}
| "index" tripleId = DoubleQualifiedName() ifExists = IfExists()
{
stmt = new IndexDropStatement(tripleId.first, tripleId.second, tripleId.third, ifExists);
}
| "nodegroup" id = Identifier() ifExists = IfExists()
{
stmt = new NodeGroupDropStatement(new Identifier(id), ifExists);
}
| "type" pairId = TypeName() ifExists = IfExists()
{
stmt = new TypeDropStatement(pairId.first, pairId.second, ifExists);
}
| "dataverse" id = Identifier() ifExists = IfExists()
{
stmt = new DataverseDropStatement(new Identifier(id), ifExists);
}
| "function" funcSig = FunctionSignature() ifExists = IfExists()
{
stmt = new FunctionDropStatement(funcSig, ifExists);
}
)
{
return stmt;
}
}
boolean IfExists() throws ParseException :
{
}
{
( "if" "exists"
{
return true;
}
)?
{
return false;
}
}
InsertStatement InsertStatement() throws ParseException:
{
Pair<Identifier,Identifier> nameComponents = null;
Query query;
}
{
"insert" "into" <DATASET> nameComponents = QualifiedName() query = Query()
{
return new InsertStatement(nameComponents.first, nameComponents.second, query, getVarCounter());
}
}
DeleteStatement DeleteStatement() throws ParseException:
{
VariableExpr var = null;
Expression condition = null;
Pair<Identifier, Identifier> nameComponents;
}
{
"delete" var = Variable()
{
getCurrentScope().addNewVarSymbolToScope(var.getVar());
}
"from" <DATASET> nameComponents = QualifiedName()
("where" condition = Expression())?
{
return new DeleteStatement(var, nameComponents.first, nameComponents.second, condition, getVarCounter());
}
}
UpdateStatement UpdateStatement() throws ParseException:
{
VariableExpr vars;
Expression target;
Expression condition;
UpdateClause uc;
List<UpdateClause> ucs = new ArrayList<UpdateClause>();
}
{
"update" vars = Variable() "in" target = Expression()
"where" condition = Expression()
<LEFTPAREN> (uc = UpdateClause()
{
ucs.add(uc);
}
("," uc = UpdateClause()
{
ucs.add(uc);
}
)*) <RIGHTPAREN>
{
return new UpdateStatement(vars, target, condition, ucs);
}
}
UpdateClause UpdateClause() throws ParseException:
{
Expression target = null;
Expression value = null ;
InsertStatement is = null;
DeleteStatement ds = null;
UpdateStatement us = null;
Expression condition = null;
UpdateClause ifbranch = null;
UpdateClause elsebranch = null;
}
{
"set" target = Expression() ":=" value = Expression()
| is = InsertStatement()
| ds = DeleteStatement()
| us = UpdateStatement()
| "if" <LEFTPAREN> condition = Expression() <RIGHTPAREN>
"then" ifbranch = UpdateClause()
[LOOKAHEAD(1) "else" elsebranch = UpdateClause()]
{
return new UpdateClause(target, value, is, ds, us, condition, ifbranch, elsebranch);
}
}
Statement SetStatement() throws ParseException:
{
String pn = null;
String pv = null;
}
{
"set" pn = Identifier() pv = StringLiteral()
{
return new SetStatement(pn, pv);
}
}
Statement WriteStatement() throws ParseException:
{
String nodeName = null;
String fileName = null;
Statement stmt = null;
Query query;
String writerClass = null;
Pair<Identifier,Identifier> nameComponents = null;
}
{
"write" ((
"output" "to" nodeName = Identifier() ":" fileName = StringLiteral()
( "using" writerClass = StringLiteral() )?
{
stmt = new WriteStatement(new Identifier(nodeName), fileName, writerClass);
}
) | (
"into" <DATASET>
{
nameComponents = QualifiedName();
}
<LEFTPAREN> query = Query() <RIGHTPAREN>
{
stmt = new WriteFromQueryResultStatement(nameComponents.first, nameComponents.second, query, getVarCounter());
}
))
{
return stmt;
}
}
LoadFromFileStatement LoadStatement() throws ParseException:
{
Identifier dataverseName = null;
Identifier datasetName = null;
boolean alreadySorted = false;
String adapterName;
Map<String,String> properties;
Pair<Identifier,Identifier> nameComponents = null;
}
{
"load" <DATASET> nameComponents = QualifiedName()
{
dataverseName = nameComponents.first;
datasetName = nameComponents.second;
}
"using" adapterName = AdapterName() properties = Configuration()
("pre-sorted"
{
alreadySorted = true;
}
)?
{
return new LoadFromFileStatement(dataverseName, datasetName, adapterName, properties, alreadySorted);
}
}
String AdapterName() throws ParseException :
{
String adapterName = null;
}
{
adapterName = Identifier()
{
return adapterName;
}
}
Statement FeedStatement() throws ParseException:
{
Pair<Identifier,Identifier> nameComponents = null;
Map<String,String> configuration = null;
Statement stmt = null;
String policy = null;
}
{
(
"begin" "feed" nameComponents = QualifiedName() (policy = GetPolicy())?
{
stmt = new BeginFeedStatement(nameComponents.first, nameComponents.second, policy, getVarCounter());
}
| "suspend" "feed" nameComponents = QualifiedName()
{
stmt = new ControlFeedStatement(ControlFeedStatement.OperationType.SUSPEND, nameComponents.first, nameComponents.second);
}
| "resume" "feed" nameComponents = QualifiedName()
{
stmt = new ControlFeedStatement(ControlFeedStatement.OperationType.RESUME, nameComponents.first, nameComponents.second);
}
| "end" "feed" nameComponents = QualifiedName()
{
stmt = new ControlFeedStatement(ControlFeedStatement.OperationType.END, nameComponents.first, nameComponents.second);
}
| "alter" "feed" nameComponents = QualifiedName() "set" configuration = Configuration()
{
stmt = new ControlFeedStatement(ControlFeedStatement.OperationType.ALTER, nameComponents.first, nameComponents.second, configuration);
}
)
{
return stmt;
}
}
Map<String,String> Configuration() throws ParseException :
{
Map<String,String> configuration = new LinkedHashMap<String,String>();
Pair<String, String> keyValuePair = null;
}
{
<LEFTPAREN> ( keyValuePair = KeyValuePair()
{
configuration.put(keyValuePair.first, keyValuePair.second);
}
( "," keyValuePair = KeyValuePair()
{
configuration.put(keyValuePair.first, keyValuePair.second);
}
)* )? <RIGHTPAREN>
{
return configuration;
}
}
Pair<String, String> KeyValuePair() throws ParseException:
{
String key;
String value;
}
{
<LEFTPAREN> key = StringLiteral() "=" value = StringLiteral() <RIGHTPAREN>
{
return new Pair<String, String>(key, value);
}
}
Map<String,String> Properties() throws ParseException:
{
Map<String,String> properties = new HashMap<String,String>();
Pair<String, String> property;
}
{
( <LEFTPAREN> property = Property()
{
properties.put(property.first, property.second);
}
( "," property = Property()
{
properties.put(property.first, property.second);
}
)* <RIGHTPAREN> )?
{
return properties;
}
}
Pair<String, String> Property() throws ParseException:
{
String key;
String value;
}
{
key = Identifier() "=" ( value = StringLiteral() | <INTEGER_LITERAL>
{
try {
value = "" + Long.valueOf(token.image);
} catch (NumberFormatException nfe) {
throw new ParseException("inapproriate value: " + token.image);
}
}
)
{
return new Pair<String, String>(key.toUpperCase(), value);
}
}
TypeExpression TypeExpr() throws ParseException:
{
TypeExpression typeExpr = null;
}
{
(
typeExpr = RecordTypeDef()
| typeExpr = TypeReference()
| typeExpr = OrderedListTypeDef()
| typeExpr = UnorderedListTypeDef()
)
{
return typeExpr;
}
}
RecordTypeDefinition RecordTypeDef() throws ParseException:
{
RecordTypeDefinition recType = new RecordTypeDefinition();
RecordTypeDefinition.RecordKind recordKind = null;
}
{
( "closed" { recordKind = RecordTypeDefinition.RecordKind.CLOSED; }
| "open" { recordKind = RecordTypeDefinition.RecordKind.OPEN; } )?
"{"
{
String hint = getHint(token);
if (hint != null) {
String splits[] = hint.split(" +");
if (splits[0].equals(GEN_FIELDS_HINT)) {
if (splits.length != 5) {
throw new ParseException("Expecting: /*+ gen-fields <type> <min> <max> <prefix>*/");
}
if (!splits[1].equals("int")) {
throw new ParseException("The only supported type for gen-fields is int.");
}
UndeclaredFieldsDataGen ufdg = new UndeclaredFieldsDataGen(UndeclaredFieldsDataGen.Type.INT,
Integer.parseInt(splits[2]), Integer.parseInt(splits[3]), splits[4]);
recType.setUndeclaredFieldsDataGen(ufdg);
}
}
}
(
RecordField(recType)
( "," RecordField(recType) )*
)?
"}"
{
if (recordKind == null) {
recordKind = RecordTypeDefinition.RecordKind.OPEN;
}
recType.setRecordKind(recordKind);
return recType;
}
}
void RecordField(RecordTypeDefinition recType) throws ParseException:
{
String fieldName;
TypeExpression type = null;
boolean nullable = false;
}
{
fieldName = Identifier()
{
fieldName = token.image;
String hint = getHint(token);
IRecordFieldDataGen rfdg = null;
if (hint != null) {
String splits[] = hint.split(" +");
if (splits[0].equals(VAL_FILE_HINT)) {
File[] valFiles = new File[splits.length - 1];
for (int k=1; k<splits.length; k++) {
valFiles[k-1] = new File(splits[k]);
}
rfdg = new FieldValFileDataGen(valFiles);
} else if (splits[0].equals(VAL_FILE_SAME_INDEX_HINT)) {
rfdg = new FieldValFileSameIndexDataGen(new File(splits[1]), splits[2]);
} else if (splits[0].equals(LIST_VAL_FILE_HINT)) {
rfdg = new ListValFileDataGen(new File(splits[1]), Integer.parseInt(splits[2]), Integer.parseInt(splits[3]));
} else if (splits[0].equals(LIST_HINT)) {
rfdg = new ListDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
} else if (splits[0].equals(INTERVAL_HINT)) {
FieldIntervalDataGen.ValueType vt;
if (splits[1].equals("int")) {
vt = FieldIntervalDataGen.ValueType.INT;
} else if (splits[1].equals("long")) {
vt = FieldIntervalDataGen.ValueType.LONG;
} else if (splits[1].equals("float")) {
vt = FieldIntervalDataGen.ValueType.FLOAT;
} else if (splits[1].equals("double")) {
vt = FieldIntervalDataGen.ValueType.DOUBLE;
} else {
throw new ParseException("Unknown type for interval data gen: " + splits[1]);
}
rfdg = new FieldIntervalDataGen(vt, splits[2], splits[3]);
} else if (splits[0].equals(INSERT_RAND_INT_HINT)) {
rfdg = new InsertRandIntDataGen(splits[1], splits[2]);
} else if (splits[0].equals(DATE_BETWEEN_YEARS_HINT)) {
rfdg = new DateBetweenYearsDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
} else if (splits[0].equals(DATETIME_BETWEEN_YEARS_HINT)) {
rfdg = new DatetimeBetweenYearsDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
} else if (splits[0].equals(DATETIME_ADD_RAND_HOURS_HINT)) {
rfdg = new DatetimeAddRandHoursDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]), splits[3]);
} else if (splits[0].equals(AUTO_HINT)) {
rfdg = new AutoDataGen(splits[1]);
}
}
}
":"
( type = TypeExpr() )
("?" { nullable = true; } )?
{
recType.addField(fieldName, type, nullable, rfdg);
}
}
TypeReferenceExpression TypeReference() throws ParseException:
{
String id = null;
}
{
id = Identifier()
{
return new TypeReferenceExpression(new Identifier(id));
}
}
OrderedListTypeDefinition OrderedListTypeDef() throws ParseException:
{
TypeExpression type = null;
}
{
"["
( type = TypeExpr() )
"]"
{
return new OrderedListTypeDefinition(type);
}
}
UnorderedListTypeDefinition UnorderedListTypeDef() throws ParseException:
{
TypeExpression type = null;
}
{
"{{"
( type = TypeExpr() )
"}}"
{
return new UnorderedListTypeDefinition(type);
}
}
FunctionName FunctionName() throws ParseException:
{
String first = null;
String second = null;
String third = null;
boolean secondAfterDot = false;
}
{
first = Identifier() ( "." second = Identifier()
{
secondAfterDot = true;
}
("#" third = Identifier())? | "#" second = Identifier() )?
{
FunctionName result = new FunctionName();
if (second == null) {
result.dataverse = defaultDataverse;
result.library = null;
result.function = first;
} else if (third == null) {
if (secondAfterDot) {
result.dataverse = first;
result.library = null;
result.function = second;
} else {
result.dataverse = defaultDataverse;
result.library = first;
result.function = second;
}
} else {
result.dataverse = first;
result.library = second;
result.function = third;
}
return result;
}
}
Pair<Identifier,Identifier> TypeName() throws ParseException:
{
Pair<Identifier,Identifier> name = null;
}
{
name = QualifiedName()
{
if (name.first == null) {
name.first = new Identifier(defaultDataverse);
}
return name;
}
}
String Identifier() throws ParseException:
{
String lit = null;
}
{
<IDENTIFIER>
{
return token.image;
}
| lit = StringLiteral()
{
return lit;
}
}
String StringLiteral() throws ParseException:
{
}
{
<STRING_LITERAL>
{
return removeQuotesAndEscapes(token.image);
}
}
Pair<Identifier,Identifier> QualifiedName() throws ParseException:
{
String first = null;
String second = null;
}
{
first = Identifier() ("." second = Identifier())?
{
Identifier id1 = null;
Identifier id2 = null;
if (second == null) {
id2 = new Identifier(first);
} else
{
id1 = new Identifier(first);
id2 = new Identifier(second);
}
return new Pair<Identifier,Identifier>(id1, id2);
}
}
Triple<Identifier,Identifier,Identifier> DoubleQualifiedName() throws ParseException:
{
String first = null;
String second = null;
String third = null;
}
{
first = Identifier() "." second = Identifier() ("." third = Identifier())?
{
Identifier id1 = null;
Identifier id2 = null;
Identifier id3 = null;
if (third == null) {
id2 = new Identifier(first);
id3 = new Identifier(second);
} else {
id1 = new Identifier(first);
id2 = new Identifier(second);
id3 = new Identifier(third);
}
return new Triple<Identifier,Identifier,Identifier>(id1, id2, id3);
}
}
FunctionDecl FunctionDeclaration() throws ParseException:
{
FunctionDecl funcDecl;
FunctionSignature signature;
String functionName;
List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
Expression funcBody;
createNewScope();
}
{
"declare" "function" functionName = Identifier()
paramList = ParameterList()
"{" funcBody = Expression() "}"
{
signature = new FunctionSignature(defaultDataverse, functionName, paramList.size());
getCurrentScope().addFunctionDescriptor(signature, false);
funcDecl = new FunctionDecl(signature, paramList, funcBody);
removeCurrentScope();
return funcDecl;
}
}
Query Query() throws ParseException:
{
Query query = new Query();
Expression expr;
}
{
expr = Expression()
{
query.setBody(expr);
query.setVarCounter(getVarCounter());
return query;
}
}
Expression Expression():
{
Expression expr = null;
Expression exprP = null;
}
{
(
//OperatorExpr | IfThenElse | FLWOGRExpression | QuantifiedExpression
expr = OperatorExpr()
| expr = IfThenElse()
| expr = FLWOGR()
| expr = QuantifiedExpression()
)
{
return (exprP==null) ? expr : exprP;
}
}
Expression OperatorExpr()throws ParseException:
{
OperatorExpr op = null;
Expression operand = null;
}
{
operand = AndExpr()
(
"or"
{
if (op == null) {
op = new OperatorExpr();
op.addOperand(operand);
op.setCurrentop(true);
}
op.addOperator(token.image);
}
operand = AndExpr()
{
op.addOperand(operand);
}
)*
{
return op==null? operand: op;
}
}
Expression AndExpr()throws ParseException:
{
OperatorExpr op = null;
Expression operand = null;
}
{
operand = RelExpr()
(
"and"
{
if (op == null) {
op = new OperatorExpr();
op.addOperand(operand);
op.setCurrentop(true);
}
op.addOperator(token.image);
}
operand = RelExpr()
{
op.addOperand(operand);
}
)*
{
return op==null? operand: op;
}
}
Expression RelExpr()throws ParseException:
{
OperatorExpr op = null;
Expression operand = null;
boolean broadcast = false;
IExpressionAnnotation annotation = null;
}
{
operand = AddExpr()
{
if (operand instanceof VariableExpr) {
String hint = getHint(token);
if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
broadcast = true;
}
}
}
(
LOOKAHEAD(2)( "<" | ">" | "<=" | ">=" | "=" | "!=" |"~=")
{
String mhint = getHint(token);
if (mhint != null && mhint.equals(INDEXED_NESTED_LOOP_JOIN_HINT)) {
annotation = IndexedNLJoinExpressionAnnotation.INSTANCE;
}
if (op == null) {
op = new OperatorExpr();
op.addOperand(operand, broadcast);
op.setCurrentop(true);
broadcast = false;
}
op.addOperator(token.image);
}
operand = AddExpr()
{
broadcast = false;
if (operand instanceof VariableExpr) {
String hint = getHint(token);
if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
broadcast = true;
}
}
op.addOperand(operand, broadcast);
}
)?
{
if (annotation != null) {
op.addHint(annotation);
}
return op==null? operand: op;
}
}
Expression AddExpr()throws ParseException:
{
OperatorExpr op = null;
Expression operand = null;
}
{
operand = MultExpr()
( ("+" | "-")
{
if (op == null) {
op = new OperatorExpr();
op.addOperand(operand);
op.setCurrentop(true);
}
((OperatorExpr)op).addOperator(token.image);
}
operand = MultExpr()
{
op.addOperand(operand);
}
)*
{
return op==null? operand: op;
}
}
Expression MultExpr()throws ParseException:
{
OperatorExpr op = null;
Expression operand = null;
}
{
operand = UnionExpr()
(( "*" | "/" | "%" | <CARET> | "idiv")
{
if (op == null) {
op = new OperatorExpr();
op.addOperand(operand);
op.setCurrentop(true);
}
op.addOperator(token.image);
}
operand = UnionExpr()
{
op.addOperand(operand);
}
)*
{
return op==null?operand:op;
}
}
Expression UnionExpr() throws ParseException:
{
UnionExpr union = null;
Expression operand1 = null;
Expression operand2 = null;
}
{
operand1 = UnaryExpr()
("union"
(operand2 = UnaryExpr()) {
if (union == null) {
union = new UnionExpr();
union.addExpr(operand1);
}
union.addExpr(operand2);
} )*
{
return (union == null)? operand1: union;
}
}
Expression UnaryExpr() throws ParseException:
{
Expression uexpr = null;
Expression expr = null;
}
{
(( "+"|"-")
{
uexpr = new UnaryExpr();
if("+".equals(token.image))
((UnaryExpr)uexpr).setSign(Sign.POSITIVE);
else if("-".equals(token.image))
((UnaryExpr)uexpr).setSign(Sign.NEGATIVE);
else
throw new ParseException();
}
)?
expr = ValueExpr()
{
if(uexpr!=null){
((UnaryExpr)uexpr).setExpr(expr);
return uexpr;
}
else{
return expr;
}
}
}
Expression ValueExpr()throws ParseException:
{
Expression expr = null;
Identifier ident = null;
AbstractAccessor fa = null;
int index;
}
{
expr = PrimaryExpr() ( ident = Field()
{
fa = (fa == null ? new FieldAccessor(expr, ident)
: new FieldAccessor(fa, ident));
}
| index = Index()
{
fa = (fa == null ? new IndexAccessor(expr, index)
: new IndexAccessor(fa, index));
}
)*
{
return fa == null ? expr : fa;
}
}
Identifier Field() throws ParseException:
{
String ident = null;
}
{
"." ident = Identifier()
{
return new Identifier(ident);
}
}
int Index() throws ParseException:
{
Expression expr = null;
int idx = -2;
}
{
"[" ( expr = Expression()
{
if(expr.getKind() == Expression.Kind.LITERAL_EXPRESSION)
{
Literal lit = ((LiteralExpr)expr).getValue();
if(lit.getLiteralType() == Literal.Type.INTEGER ||
lit.getLiteralType() == Literal.Type.LONG) {
idx = Integer.valueOf(lit.getStringValue());
}
else {
throw new ParseException("Index should be an INTEGER");
}
}
}
| "?"
{
idx = IndexAccessor.ANY;
// ANY
}
)
"]"
{
return idx;
}
}
Expression PrimaryExpr()throws ParseException:
{
Expression expr = null;
}
{
( LOOKAHEAD(2)
expr = FunctionCallExpr()
| expr = Literal()
| expr = DatasetAccessExpression()
| expr = VariableRef()
{
if(((VariableExpr)expr).getIsNewVar() == true)
throw new ParseException("can't find variable " + ((VariableExpr)expr).getVar());
}
| expr = ListConstructor()
| expr = RecordConstructor()
| expr = ParenthesizedExpression()
)
{
return expr;
}
}
Expression Literal() throws ParseException:
{
LiteralExpr lit = new LiteralExpr();
String str = null;
}
{
( str = StringLiteral()
{
lit.setValue(new StringLiteral(str));
}
| <INTEGER_LITERAL>
{
try {
lit.setValue(new IntegerLiteral(new Integer(token.image)));
} catch(NumberFormatException ex) {
lit.setValue(new LongIntegerLiteral(new Long(token.image)));
}
}
| <FLOAT_LITERAL>
{
lit.setValue(new FloatLiteral(new Float(token.image)));
}
| <DOUBLE_LITERAL>
{
lit.setValue(new DoubleLiteral(new Double(token.image)));
}
| <NULL>
{
lit.setValue(NullLiteral.INSTANCE);
}
| <TRUE>
{
lit.setValue(TrueLiteral.INSTANCE);
}
| <FALSE>
{
lit.setValue(FalseLiteral.INSTANCE);
}
)
{
return lit;
}
}
VariableExpr VariableRef() throws ParseException:
{
VariableExpr varExp = new VariableExpr();
VarIdentifier var = new VarIdentifier();
}
{
<VARIABLE>
{
String varName = token.image;
Identifier ident = lookupSymbol(varName);
if (isInForbiddenScopes(varName)) {
throw new ParseException("Inside limit clauses, it is disallowed to reference a variable having the same name as any variable bound in the same scope as the limit clause.");
}
if(ident != null) { // exist such ident
varExp.setIsNewVar(false);
varExp.setVar((VarIdentifier)ident);
} else {
varExp.setVar(var);
}
var.setValue(varName);
return varExp;
}
}
VariableExpr Variable() throws ParseException:
{
VariableExpr varExp = new VariableExpr();
VarIdentifier var = new VarIdentifier();
}
{
<VARIABLE>
{
Identifier ident = lookupSymbol(token.image);
if(ident != null) { // exist such ident
varExp.setIsNewVar(false);
}
varExp.setVar(var);
var.setValue(token.image);
return varExp;
}
}
Expression ListConstructor() throws ParseException:
{
Expression expr = null;
}
{
(
expr = OrderedListConstructor() | expr = UnorderedListConstructor()
)
{
return expr;
}
}
ListConstructor OrderedListConstructor() throws ParseException:
{
ListConstructor expr = new ListConstructor();
Expression tmp = null;
List<Expression> exprList = new ArrayList<Expression>();
expr.setType(ListConstructor.Type.ORDERED_LIST_CONSTRUCTOR);
}
{
"["
( tmp = Expression()
{
exprList.add(tmp);
}
("," tmp = Expression() { exprList.add(tmp); })*
)?
"]"
{
expr.setExprList(exprList);
return expr;
}
}
ListConstructor UnorderedListConstructor() throws ParseException:
{
ListConstructor expr = new ListConstructor();
Expression tmp = null;
List<Expression> exprList = new ArrayList<Expression>();
expr.setType(ListConstructor.Type.UNORDERED_LIST_CONSTRUCTOR);
}
{
"{{" ( tmp = Expression()
{
exprList.add(tmp);
}
("," tmp = Expression() { exprList.add(tmp); })*)? "}}"
{
expr.setExprList(exprList);
return expr;
}
}
RecordConstructor RecordConstructor() throws ParseException:
{
RecordConstructor expr = new RecordConstructor();
FieldBinding tmp = null;
List<FieldBinding> fbList = new ArrayList<FieldBinding>();
}
{
"{" (tmp = FieldBinding()
{
fbList.add(tmp);
}
("," tmp = FieldBinding() { fbList.add(tmp); })*)? "}"
{
expr.setFbList(fbList);
return expr;
}
}
FieldBinding FieldBinding() throws ParseException:
{
FieldBinding fb = new FieldBinding();
Expression left, right;
}
{
left = Expression() ":" right = Expression()
{
fb.setLeftExpr(left);
fb.setRightExpr(right);
return fb;
}
}
Expression FunctionCallExpr() throws ParseException:
{
CallExpr callExpr;
List<Expression> argList = new ArrayList<Expression>();
Expression tmp;
int arity = 0;
FunctionName funcName = null;
String hint = null;
}
{
funcName = FunctionName()
{
hint = getHint(token);
}
<LEFTPAREN> (tmp = Expression()
{
argList.add(tmp);
arity ++;
}
("," tmp = Expression()
{
argList.add(tmp);
arity++;
}
)*)? <RIGHTPAREN>
{
// TODO use funcName.library
String fqFunctionName = funcName.library == null ? funcName.function : funcName.library + "#" + funcName.function;
FunctionSignature signature
= lookupFunctionSignature(funcName.dataverse, fqFunctionName, arity);
if (signature == null) {
signature = new FunctionSignature(funcName.dataverse, fqFunctionName, arity);
}
callExpr = new CallExpr(signature,argList);
if (hint != null && hint.startsWith(INDEXED_NESTED_LOOP_JOIN_HINT)) {
callExpr.addHint(IndexedNLJoinExpressionAnnotation.INSTANCE);
}
return callExpr;
}
}
Expression DatasetAccessExpression() throws ParseException:
{
String funcName;
String arg1 = null;
String arg2 = null;
Expression nameArg;
}
{
<DATASET>
{
funcName = token.image;
}
( ( arg1 = Identifier() ( "." arg2 = Identifier() )? )
{
String name = arg2 == null ? arg1 : arg1 + "." + arg2;
LiteralExpr ds = new LiteralExpr();
ds.setValue( new StringLiteral(name) );
nameArg = ds;
}
| ( <LEFTPAREN> nameArg = Expression() <RIGHTPAREN> ) )
{
String dataverse = MetadataConstants.METADATA_DATAVERSE_NAME;
FunctionSignature signature = lookupFunctionSignature(dataverse, funcName, 1);
if (signature == null) {
signature = new FunctionSignature(dataverse, funcName, 1);
}
List<Expression> argList = new ArrayList<Expression>();
argList.add(nameArg);
return new CallExpr(signature, argList);
}
}
Expression ParenthesizedExpression() throws ParseException:
{
Expression expr;
}
{
<LEFTPAREN> expr = Expression() <RIGHTPAREN>
{
return expr;
}
}
Expression IfThenElse() throws ParseException:
{
Expression condExpr;
Expression thenExpr;
Expression elseExpr;
IfExpr ifExpr = new IfExpr();
}
{
"if" <LEFTPAREN> condExpr = Expression() <RIGHTPAREN> "then" thenExpr = Expression() "else" elseExpr = Expression()
{
ifExpr.setCondExpr(condExpr);
ifExpr.setThenExpr(thenExpr);
ifExpr.setElseExpr(elseExpr);
return ifExpr;
}
}
Expression FLWOGR() throws ParseException:
{
FLWOGRExpression flworg = new FLWOGRExpression();
List<Clause> clauseList = new ArrayList<Clause>();
Expression returnExpr;
Clause tmp;
createNewScope();
}
{
(tmp = ForClause() {clauseList.add(tmp);} | tmp = LetClause() {clauseList.add(tmp);})
(tmp = Clause() {clauseList.add(tmp);})* "return" returnExpr = Expression()
{
flworg.setClauseList(clauseList);
flworg.setReturnExpr(returnExpr);
removeCurrentScope();
return flworg;
}
}
Clause Clause()throws ParseException :
{
Clause clause;
}
{
(
clause = ForClause()
| clause = LetClause()
| clause = WhereClause()
| clause = OrderbyClause()
| clause = GroupClause()
| clause = LimitClause()
| clause = DistinctClause()
)
{
return clause;
}
}
Clause ForClause()throws ParseException :
{
ForClause fc = new ForClause();
VariableExpr varExp;
VariableExpr varPos = null;
Expression inExp;
extendCurrentScope();
}
{
"for" varExp = Variable() ("at" varPos = Variable())? "in" ( inExp = Expression() )
{
fc.setVarExpr(varExp);
getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
fc.setInExpr(inExp);
if (varPos != null) {
fc.setPosExpr(varPos);
getCurrentScope().addNewVarSymbolToScope(varPos.getVar());
}
return fc;
}
}
Clause LetClause() throws ParseException:
{
LetClause lc = new LetClause();
VariableExpr varExp;
Expression beExp;
extendCurrentScope();
}
{
"let" varExp = Variable() ":=" beExp = Expression()
{
getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
lc.setVarExpr(varExp);
lc.setBeExpr(beExp);
return lc;
}
}
Clause WhereClause()throws ParseException :
{
WhereClause wc = new WhereClause();
Expression whereExpr;
}
{
"where" whereExpr = Expression()
{
wc.setWhereExpr(whereExpr);
return wc;
}
}
Clause OrderbyClause()throws ParseException :
{
OrderbyClause oc = new OrderbyClause();
Expression orderbyExpr;
List<Expression> orderbyList = new ArrayList<Expression>();
List<OrderbyClause.OrderModifier> modifierList = new ArrayList<OrderbyClause.OrderModifier >();
int numOfOrderby = 0;
}
{
(
"order"
{
String hint = getHint(token);
if (hint != null && hint.startsWith(INMEMORY_HINT)) {
String splits[] = hint.split(" +");
int numFrames = Integer.parseInt(splits[1]);
int numTuples = Integer.parseInt(splits[2]);
oc.setNumFrames(numFrames);
oc.setNumTuples(numTuples);
}
}
"by" orderbyExpr = Expression()
{
orderbyList.add(orderbyExpr);
OrderbyClause.OrderModifier modif = OrderbyClause.OrderModifier.ASC;
}
( ("asc" { modif = OrderbyClause.OrderModifier.ASC; })
| ("desc" { modif = OrderbyClause.OrderModifier.DESC; }))?
{
modifierList.add(modif);
}
("," orderbyExpr = Expression()
{
orderbyList.add(orderbyExpr);
modif = OrderbyClause.OrderModifier.ASC;
}
( ("asc" { modif = OrderbyClause.OrderModifier.ASC; })
| ("desc" { modif = OrderbyClause.OrderModifier.DESC; }))?
{
modifierList.add(modif);
}
)*
)
{
oc.setModifierList(modifierList);
oc.setOrderbyList(orderbyList);
return oc;
}
}
Clause GroupClause()throws ParseException :
{
GroupbyClause gbc = new GroupbyClause();
// GbyVariableExpressionPair pair = new GbyVariableExpressionPair();
List<GbyVariableExpressionPair> vePairList = new ArrayList<GbyVariableExpressionPair>();
List<GbyVariableExpressionPair> decorPairList = new ArrayList<GbyVariableExpressionPair>();
List<VariableExpr> withVarList= new ArrayList<VariableExpr>();
VariableExpr var = null;
VariableExpr withVar = null;
Expression expr = null;
VariableExpr decorVar = null;
Expression decorExpr = null;
}
{
{
Scope newScope = extendCurrentScopeNoPush(true);
// extendCurrentScope(true);
}
"group"
{
String hint = getHint(token);
if (hint != null && hint.equals(HASH_GROUP_BY_HINT)) {
gbc.setHashGroupByHint(true);
}
}
"by" (LOOKAHEAD(2) var = Variable()
{
newScope.addNewVarSymbolToScope(var.getVar());
} ":=")?
expr = Expression()
{
GbyVariableExpressionPair pair1 = new GbyVariableExpressionPair(var, expr);
vePairList.add(pair1);
}
("," ( LOOKAHEAD(2) var = Variable()
{
newScope.addNewVarSymbolToScope(var.getVar());
} ":=")?
expr = Expression()
{
GbyVariableExpressionPair pair2 = new GbyVariableExpressionPair(var, expr);
vePairList.add(pair2);
}
)*
("decor" decorVar = Variable() ":=" decorExpr = Expression()
{
newScope.addNewVarSymbolToScope(decorVar.getVar());
GbyVariableExpressionPair pair3 = new GbyVariableExpressionPair(decorVar, decorExpr);
decorPairList.add(pair3);
}
("," "decor" decorVar = Variable() ":=" decorExpr = Expression()
{
newScope.addNewVarSymbolToScope(decorVar.getVar());
GbyVariableExpressionPair pair4 = new GbyVariableExpressionPair(decorVar, decorExpr);
decorPairList.add(pair4);
}
)*
)?
"with" withVar = VariableRef()
{
if(withVar.getIsNewVar()==true)
throw new ParseException("can't find variable " + withVar.getVar());
withVarList.add(withVar);
newScope.addNewVarSymbolToScope(withVar.getVar());
}
("," withVar = VariableRef()
{
if(withVar.getIsNewVar()==true)
throw new ParseException("can't find variable " + withVar.getVar());
withVarList.add(withVar);
newScope.addNewVarSymbolToScope(withVar.getVar());
})*
{
gbc.setGbyPairList(vePairList);
gbc.setDecorPairList(decorPairList);
gbc.setWithVarList(withVarList);
replaceCurrentScope(newScope);
return gbc;
}
}
LimitClause LimitClause() throws ParseException:
{
LimitClause lc = new LimitClause();
Expression expr;
pushForbiddenScope(getCurrentScope());
}
{
"limit" expr = Expression() { lc.setLimitExpr(expr); }
("offset" expr = Expression() { lc.setOffset(expr); })?
{
popForbiddenScope();
return lc;
}
}
DistinctClause DistinctClause() throws ParseException:
{
List<Expression> exprs = new ArrayList<Expression>();
Expression expr;
}
{
"distinct" "by" expr = Expression()
{
exprs.add(expr);
}
("," expr = Expression()
{
exprs.add(expr);
}
)*
{
return new DistinctClause(exprs);
}
}
QuantifiedExpression QuantifiedExpression()throws ParseException:
{
QuantifiedExpression qc = new QuantifiedExpression();
List<QuantifiedPair> quantifiedList = new ArrayList<QuantifiedPair>();
Expression satisfiesExpr;
VariableExpr var;
Expression inExpr;
QuantifiedPair pair;
}
{
{
createNewScope();
}
( ("some" { qc.setQuantifier(QuantifiedExpression.Quantifier.SOME); })
| ("every" { qc.setQuantifier(QuantifiedExpression.Quantifier.EVERY); }))
var = Variable() "in" inExpr = Expression()
{
pair = new QuantifiedPair(var, inExpr);
getCurrentScope().addNewVarSymbolToScope(var.getVar());
quantifiedList.add(pair);
}
(
"," var = Variable() "in" inExpr = Expression()
{
pair = new QuantifiedPair(var, inExpr);
getCurrentScope().addNewVarSymbolToScope(var.getVar());
quantifiedList.add(pair);
}
)*
"satisfies" satisfiesExpr = Expression()
{
qc.setSatisfiesExpr(satisfiesExpr);
qc.setQuantifiedList(quantifiedList);
removeCurrentScope();
return qc;
}
}
TOKEN_MGR_DECLS:
{
public int commentDepth = 0;
}
<DEFAULT>
TOKEN :
{
<CARET : "^" >
}
<DEFAULT>
TOKEN :
{
<DATASET : "dataset" >
}
<DEFAULT>
TOKEN :
{
<LEFTPAREN : "(" >
}
<DEFAULT>
TOKEN :
{
<RIGHTPAREN : ")" >
}
<DEFAULT>
TOKEN :
{
<INTEGER_LITERAL : (<DIGIT>)+ >
}
<DEFAULT>
TOKEN :
{
<NULL : "null">
}
<DEFAULT>
TOKEN :
{
<TRUE : "true">
}
<DEFAULT>
TOKEN :
{
<FALSE : "false">
}
<DEFAULT>
TOKEN :
{
<#DIGIT : ["0" - "9"]>
}
TOKEN:
{
< DOUBLE_LITERAL: <INTEGER>
| <INTEGER> ( "." <INTEGER> )?
| "." <INTEGER>
>
|
< FLOAT_LITERAL: <INTEGER> ( "f" | "F" )
| <INTEGER> ( "." <INTEGER> ( "f" | "F" ) )?
| "." <INTEGER> ( "f" | "F" )
>
|
<INTEGER : (<DIGIT>)+ >
}
<DEFAULT>
TOKEN :
{
<#LETTER : ["A" - "Z", "a" - "z"]>
}
<DEFAULT>
TOKEN :
{
<SPECIALCHARS : ["$", "_", "-"] >
}
<DEFAULT>
TOKEN :
{
<STRING_LITERAL : ("\"" (<EscapeQuot> | ~["\""])* "\"") | ("\'"(<EscapeApos> | ~["\'"])* "\'")>
|
< #EscapeQuot: "\\\"" >
|
< #EscapeApos: "\\\'" >
}
<DEFAULT>
TOKEN :
{
<IDENTIFIER : (<LETTER>)+ (<LETTER> | <DIGIT> | <SPECIALCHARS>)*>
}
<DEFAULT>
TOKEN :
{
<VARIABLE : "$" (<LETTER>)+ (<LETTER> | <DIGIT> | "_")* >
}
SKIP:
{
" "
| "\t"
| "\r"
| "\n"
}
SKIP:
{
<"//" (~["\n"])* "\n">
}
SKIP:
{
<"//" (~["\n","\r"])* ("\n"|"\r"|"\r\n")?>
}
SKIP:
{
<"/*"> {commentDepth=1;}: INSIDE_COMMENT
}
<INSIDE_COMMENT>
SPECIAL_TOKEN:
{
<"+"(" ")*(~["*"])*>
}
<INSIDE_COMMENT>
SKIP:
{
<"/*"> {commentDepth++;}
}
<INSIDE_COMMENT>
SKIP:
{
<"*/"> {commentDepth--; if (commentDepth == 0) SwitchTo(DEFAULT);}
| <~[]>
}