Permalink
Browse files

ParseOptions class

  • Loading branch information...
1 parent 30a01c1 commit 691c6c7cd9c8df4db083ec01e80651c9974b90a8 @langpavel langpavel committed Jun 25, 2012
@@ -1,71 +1,7 @@
{
- var options = this.options || arguments[2] || {};
-
- options.STRING_HEX_ESCAPE = !!options.STRING_HEX_ESCAPE;
- options.STRING_UNICODE_ESCAPE = !!options.STRING_UNICODE_ESCAPE;
- options.STRING_INVALID_ESCAPE_STRIP_BACKSLASH = !!options.STRING_INVALID_ESCAPE_STRIP_BACKSLASH;
- options.STRING_STRICT_ESCAPE = !!options.STRING_STRICT_ESCAPE;
-
- options.ALLOW_OCTAL_NOTATION = !!options.ALLOW_OCTAL_NOTATION;
- options.OCTAL_AS_DECIMAL = !!options.OCTAL_AS_DECIMAL;
-
- if(typeof options.BOOLEAN_VALUE_TRUE === 'undefined')
- options.BOOLEAN_VALUE_TRUE = true;
-
- if(typeof options.BOOLEAN_VALUE_FALSE === 'undefined')
- options.BOOLEAN_VALUE_FALSE = false;
-
- if(typeof options.NULL_VALUE === 'undefined') {
- options.NULL_VALUE = {
- isNull: true,
- valueOf: function() { return null; },
- toString: function() { return "NULL"; }
- }
- }
-
- function defineExpressionEvent(name) {
- if(options[name]) return;
- options[name] = function(expr) { return expr; }
- }
-
- defineExpressionEvent('onExpression');
- defineExpressionEvent('onOrExpression');
- defineExpressionEvent('onXorExpression');
- defineExpressionEvent('onAndExpression');
-
- // handles both, NOT and '!' operators
- // note that this does not same priority in grammar
- defineExpressionEvent('onNotExpression');
-
- defineExpressionEvent('onComparisonExpression');
- defineExpressionEvent('onBitwiseOrExpression');
- defineExpressionEvent('onBitwiseAndExpression');
-
- // handles << and >> operators
- defineExpressionEvent('onBitShiftExpression');
-
- // handles binary + (addition) and - (subtraction)
- defineExpressionEvent('onAddExpression');
-
- // handles binary operators:
- // - '*' - multiplication
- // - '/' - division
- // - 'DIV' - integer division
- // - '%' or 'MOD' - modulo - integer division remainder
- defineExpressionEvent('onMulDivExpression');
-
- defineExpressionEvent('onBitwiseXorExpression');
- defineExpressionEvent('onUnaryExpression');
-
- // hadles IS [NOT] (NULL | TRUE | FALSE | UNKNOWN)
- defineExpressionEvent('onIsExpression');
-
- // handles COLLATION 'name'
- defineExpressionEvent('onCollateExpression');
-
- // handles BINARY <expr> modifier
- defineExpressionEvent('onModifierBinaryExpression');
-
+ var options = arguments[2];
+ if(!options.isParseOptions)
+ throw new Error("Parser require ParseOptions instance as 3rd argument");
function safeMergeObject(target, src, resolveCallback) {
for(var name in src) {
@@ -5,53 +5,70 @@
// 12.3.1. Operator Precedence
// http://dev.mysql.com/doc/refman/5.6/en/operator-precedence.html
+// this is actually an extension
+CONSTANT_EXPRESSION "constant expression"
+ = expr:LOGICALOR_EXPR {
+ return options.resolveConstantExpression(expr);
+ }
+
+
EXPRESSION
- = _ expr:ASSIGN_EXPR { return options.onExpression(expr); }
+ = _ expr:ASSIGN_EXPR { return options.expression(expr); }
ASSIGN_EXPR // := assignment, can be simly '=' in some cases but here i avoid it
= left:LOGICALOR_EXPR tail:( ':=' _ expr:LOGICALOR_EXPR { return expr; } )+ {
tail.unshift(left);
return {
- operator: "ASSIGN",
+ operator: ":=",
expressions: tail
};
}
/ LOGICALOR_EXPR
LOGICALOR_EXPR // ||, OR
- = left:LOGICALXOR_EXPR tail:( ('||'/'OR'i) _ expr:LOGICALXOR_EXPR { return expr; } )+ {
- tail.unshift(left);
- return options.onOrExpression({
- operator: "OR",
- expressions: tail
+ = left:LOGICALXOR_EXPR
+ tail:( op:('||'/'OR'i) _ expr:LOGICALXOR_EXPR { return [op, expr]; } )+
+ {
+ var exprs = [left];
+ var operators = [];
+ tail.forEach(function(val){
+ operators.push(val[0]); // operator
+ exprs.push(val[1]); // expression
+ });
+ return options.orExpression({
+ operators: operators,
+ expressions: exprs
});
}
/ LOGICALXOR_EXPR
LOGICALXOR_EXPR // XOR
- = left:LOGICALAND_EXPR tail:( 'XOR'i _ expr:LOGICALXOR_EXPR { return expr; } )+ {
+ = left:LOGICALAND_EXPR tail:( 'XOR'i _ expr:LOGICALXOR_EXPR { return expr; } )+
+ {
tail.unshift(left);
- return options.onXorExpression({
+ return options.xorExpression({
operator: "XOR",
expressions: tail
});
}
/ LOGICALAND_EXPR
LOGICALAND_EXPR // &&, AND
- = left:LOGICALNOT_EXPR tail:( ('&&'/'AND'i) _ expr:LOGICALNOT_EXPR { return expr; } )+ {
+ = left:LOGICALNOT_EXPR tail:( ('&&'/'AND'i) _ expr:LOGICALNOT_EXPR { return expr; } )+
+ {
tail.unshift(left);
- return options.onAndExpression({
+ return options.andExpression({
operator: "AND",
expressions: tail
});
}
/ LOGICALNOT_EXPR
LOGICALNOT_EXPR // NOT
- = "NOT"i __ expr:COND_EXPR {
- return options.onNotExpression({
- operator: "NOT",
+ = "NOT"i __ expr:COND_EXPR
+ {
+ return options.notExpression({
+ unary: "NOT",
expression: expr
});
}
@@ -66,7 +83,7 @@ COMPARISON_EXPR // = (comparison), <=>, >=, >, <=, <, <>, !=, IS, LIKE, REGEXP,
not:"NOT"i? _
val:("TRUE"i / "FALSE"i / "UNKNOWN"i / "NULL"i) _
{
- return options.onIsExpression({
+ return options.isExpression({
unary: 'IS',
not: !!not,
value: val.toUpperCase(),
@@ -83,7 +100,7 @@ COMPARISON_EXPR // = (comparison), <=>, >=, >, <=, <, <>, !=, IS, LIKE, REGEXP,
operators.push(val[0]); // operator
exprs.push(val[1]); // expression
});
- return options.onComparisonExpression({
+ return options.comparisonExpression({
operators: operators,
expressions: exprs
});
@@ -93,7 +110,7 @@ COMPARISON_EXPR // = (comparison), <=>, >=, >, <=, <, <>, !=, IS, LIKE, REGEXP,
BITOR_EXPR // |
= left:BITAND_EXPR tail:( '|' _ expr:BITAND_EXPR { return expr; } )+ {
tail.unshift(left);
- return options.onBitwiseOrExpression({
+ return options.bitwiseOrExpression({
operator: "|",
expressions: tail
});
@@ -103,7 +120,7 @@ BITOR_EXPR // |
BITAND_EXPR // &
= left:BITSHIFT_EXPR tail:( '&' _ expr:BITSHIFT_EXPR { return expr; } )+ {
tail.unshift(left);
- return options.onBitwiseAndExpression({
+ return options.bitwiseAndExpression({
operator: "&",
expressions: tail
});
@@ -118,7 +135,7 @@ BITSHIFT_EXPR // <<, >>
operators.push(val[0]); // operator
exprs.push(val[1]); // expression
});
- return options.onBitShiftExpression({
+ return options.bitShiftExpression({
operators: operators,
expressions: exprs
});
@@ -133,7 +150,7 @@ ADD_EXPR // +, -
operators.push(val[0]); // operator
exprs.push(val[1]); // expression
});
- return options.onAddExpression({
+ return options.addExpression({
operators: operators,
expressions: exprs
});
@@ -151,7 +168,7 @@ MULT_EXPR // *, /, DIV, %, MOD
operators.push(val[0]); // operator
exprs.push(val[1]); // expression
});
- return options.onMulDivExpression({
+ return options.mulDivExpression({
operators: operators,
expressions: exprs
});
@@ -161,7 +178,7 @@ MULT_EXPR // *, /, DIV, %, MOD
BITXOR_EXPR // ^
= left:UNARY_EXPR tail:( '^' _ expr:UNARY_EXPR { return expr; } )+ {
tail.unshift(left);
- return options.onBitwiseXorExpression({
+ return options.bitwiseXorExpression({
operator: "^",
expressions: tail
});
@@ -170,7 +187,7 @@ BITXOR_EXPR // ^
UNARY_EXPR // - (unary minus), ~ (unary bit inversion), + (unary plus)
= op:('~' / '+' / '-') _ expr:UNARY_EXPR {
- return options.onUnaryExpression({
+ return options.unaryExpression({
unary: op,
expression: expr
});
@@ -179,7 +196,7 @@ UNARY_EXPR // - (unary minus), ~ (unary bit inversion), + (unary plus)
HIGH_NOT_EXPR // !
= '!' _ expr:HIGH_NOT_EXPR {
- return options.onNotExpression({
+ return options.notExpression({
unary: '!',
expression: expr
});
@@ -188,7 +205,7 @@ HIGH_NOT_EXPR // !
STRING_COLLATE_EXPR // COLLATE
= expr:STRING_BINARY_EXPR "COLLATE"i _ collation:COLLATION_NAME {
- return options.onCollateExpression({
+ return options.collateExpression({
unary: 'COLLATE',
collation: collation,
expression: expr
@@ -204,7 +221,7 @@ COLLATION_NAME "collation name"
STRING_BINARY_EXPR // BINARY MODIFIER
= "BINARY"i __ expr:INTERVAL_EXPR {
- return options.onModifierBinaryExpression({
+ return options.modifierBinaryExpression({
unary: 'BINARY',
expression: expr
});
@@ -220,8 +237,3 @@ PRIMARY_EXPR
/ "(" expr:EXPRESSION ")" { return expr; }
-CONSTANT_EXPRESSION "constant expression"
- // TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO
- // this is helper for evaluating expressions from constant values to constant value
- = CONSTANT_VALUE
-
@@ -13,7 +13,8 @@ CREATE_TABLE
{
safeMergeObject(props, props1);
safeMergeObject(props, props2);
- props.statement = "CREATE TABLE";
+ props.statement = "CREATE";
+ props.what = "TABLE";
props.schema = table.schema;
props.table = table.table;
if(temp) props.temporary = true;
@@ -120,7 +121,7 @@ COLUMN_TYPE_PROPERTIES "Column type properties"
props.type = type.toUpperCase();
return props;
}
- / _ "NOT"i _ "NULL"i props:COLUMN_TYPE_PROPERTIES {
+ / _ "NOT"i _ "NULL"i _ props:COLUMN_TYPE_PROPERTIES {
if(typeof props.notNull !== 'undefined')
throw new Error('NULL or NOT NULL?');
props.notNull=true;
@@ -132,15 +133,15 @@ COLUMN_TYPE_PROPERTIES "Column type properties"
props.notNull=false;
return props;
}
- / _ "PRIMARY"i (_ "KEY"i)? props:COLUMN_TYPE_PROPERTIES {
+ / _ "PRIMARY"i (_ "KEY"i)? _ props:COLUMN_TYPE_PROPERTIES {
props.primaryKey=true;
return props;
}
- / _ "UNIQ"i "UE"i? props:COLUMN_TYPE_PROPERTIES {
+ / _ "UNIQ"i "UE"i? _ props:COLUMN_TYPE_PROPERTIES {
props.unique=true;
return props;
}
- / _ "AUTO"i ( "_" / _ )"INC"i "REMENT"i? props:COLUMN_TYPE_PROPERTIES {
+ / _ "AUTO"i ( "_" / _ )"INC"i "REMENT"i? _ props:COLUMN_TYPE_PROPERTIES {
props.autoIncrement=true;
return props;
}
@@ -152,7 +153,7 @@ COLUMN_TYPE_PROPERTIES "Column type properties"
props.default = 'CURRENT_TIMESTAMP';
return props;
}
- / _ "COMMENT"i ( _ "=" _ / __ ) comment:STRING props:COLUMN_TYPE_PROPERTIES {
+ / _ "COMMENT"i ( _ "=" _ / __ ) comment:STRING props:COLUMN_TYPE_PROPERTIES {
props.comment = comment;
return props;
}
Oops, something went wrong.

0 comments on commit 691c6c7

Please sign in to comment.