Skip to content

Instantly share code, notes, and snippets.

@seanmcelroy
Created May 3, 2016 02:39
Show Gist options
  • Save seanmcelroy/c25cfb57c9a32b140c2469ba148712d2 to your computer and use it in GitHub Desktop.
Save seanmcelroy/c25cfb57c9a32b140c2469ba148712d2 to your computer and use it in GitHub Desktop.
WhiteSpace: [\s\n\t ]+;
Comment: '/*' (!'*/' .)* '*/';
(?<Literal>): "'" [^']* "'";
S: (WhiteSpace / Comment)*;Integer: [0-9]+;
Digit: Integer('.'Integer)?;
Identifier: [a-zA-Z][a-zA-Z0-9]*;
(?<Operator>): ('+' / '-' / '*' / '/' / '%');
(?<AliasName>): Identifier;
Alias: '['? AliasName ']'?;
(?<SelectQuantifier>): ( 'ALL'\i / 'DISTINCT'\i );
(?<TopAmount>): '('? ( ( Digit S 'PERCENT'\i ) / Digit ) ')'?;
Top: 'TOP'\i S TopAmount (S 'WITH TIES'\i)?;
AliasPrefix: '['? Alias ']'? S '.';
(?<Aggregator>): ( 'AVG'\i / 'CAST'\i / 'COUNT'\i / 'STDEV'\i / 'SUM'\i);
(?<ColumnIdentity>): '$IDENTITY'\i;
(?<ColumnName>): Identifier;
(?<ColumnRowGUID>): '$ROWGUID'\i;
(?<ColumnWild>): '*';
Value: (
ColumnIdentity /
ColumnRowGUID /
ColumnWild /
AliasPrefix ColumnWild /
AliasPrefix '['? ColumnName ']'? /
'['? ColumnName ']'? /
Digit /
Literal);
(?<Aggregation>): Aggregator S '(' S Value S ')';
ValueExpression: ( Aggregation / Value ) (S Operator S ValueExpression)?;
(?<ColumnExpression>): ( ValueExpression S 'AS'\i S Alias / ValueExpression );
SelectClause: 'SELECT'\i (S SelectQuantifier)? (S Top)? (S ColumnExpression) (S ',' S ColumnExpression)*;
(?<ServerName>): Identifier;
Server: '['? ServerName ']'? '.';
(?<DatabaseName>): Identifier;
Database: '['? DatabaseName ']'? '.';
(?<SchemaName>): Identifier;
Schema: '['? SchemaName ']'? '.';
(?<ObjectName>): Identifier;
Object: '['? ObjectName ']'?;
ObjectReference: (
Server Database Schema Object S ('AS'\i S)? Alias /
Server Database Schema Object /
Server Database '.' Object S ('AS'\i S)? Alias /
Server Database '.' Object /
Server '.' Schema Object S ('AS'\i S)? Alias /
Server '.' Schema Object /
Server '..' Object S ('AS'\i S)? Alias /
Server '..' Object /
Database Schema Object S ('AS'\i S)? Alias /
Database Schema Object /
Database '.' Object S ('AS'\i S)? Alias /
Database '.' Object /
Schema Object S ('AS'\i S)? Alias /
Schema Object /
Object S ('AS'\i S)? Alias /
Object
);
(?<LogicalComparison>): ('=' / '!=' / '<>' / '>' / '>=' / '<' / '<=');
(?<FromClause>): 'FROM'\i S ObjectReference;
(?<JoinLValue>): ValueExpression;
(?<JoinRValue>): ValueExpression;
(?<Not>): ( 'NOT'\i S);
AndKeyword: 'AND'\i;
(?<BetweenKeyword>): 'BETWEEN'\i;
(?<IsKeyword>): 'IS'\i;
(?<LikeKeyword>): 'LIKE'\i;
(?<NullKeyword>): 'NULL'\i;
(?<LogicalBoolean>): ( AndKeyword / 'OR'\i );
JoinEval: '('? S Not? JoinLValue S LogicalComparison S Not? JoinRValue S ')'?;
JoinCondition: '('? JoinEval ( S ( AndKeyword / 'OR'\i ) S JoinEval )* ')'?;
(?<JoinPredicate>): 'ON'\i S JoinCondition;
(?<JoinType>): ( 'CROSS'\i / 'INNER'\i / ( 'LEFT'\i / 'RIGHT'\i / 'FULL'\i ) (S 'OUTER')? );
(?<JoinClause>): JoinType S 'JOIN'\i S ObjectReference S JoinPredicate? S;
(?<PredicateExpression>): ValueExpression;
(?<StringExpression>): Literal;
(?<BetweenLValue>): PredicateExpression;
(?<BetweenRValue>): PredicateExpression;
(?<Predicate>): (
PredicateExpression S LogicalComparison S PredicateExpression /
PredicateExpression S Not? LikeKeyword S StringExpression /
PredicateExpression S Not? BetweenKeyword S BetweenLValue S AndKeyword S BetweenRValue /
PredicateExpression S IsKeyword S Not? NullKeyword
);
(?<SearchCondition>): Not? ( Predicate / '(' S SearchCondition S ')' ) ( S LogicalBoolean S Not? ( Predicate / '(' S SearchCondition S ')' ) )*;
(?<WhereClause>): 'WHERE'\i S SearchCondition;
(?<SelectQuery>):
SelectClause S
FromClause (S ',' S FromClause)* S
JoinClause*
WhereClause?;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment