Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %%{
- machine FQL;
- action markStart{
- tokenStartPos = p;
- }
- action OnParseError{
- throw new ParseErrorException("Parse error at index:" + p);
- }
- action OnPropertyEnd{
- currentOperatorPropertyName = data.Substring(tokenStartPos, p-tokenStartPos);
- //var bop = currentOperator as BinaryOperator;
- //if(bop!=null) bop.LeftOperand = new OperandProperty(token);
- //else ((InOperator)currentOperator).LeftOperand = new OperandProperty(token);
- }
- action OnOperatorEnd{
- //OnOperatorEnd
- if(rv==null) rv = currentOperator;
- else ((BinaryOperator)rv).RightOperand = currentOperator;
- currentOperator = null;
- }
- action OnInOperandFloatEnd{
- string token = data.Substring(tokenStartPos, p-tokenStartPos-1);
- ((InOperator)currentOperator).Operands.Add(new OperandValue(float.Parse(token, CultureInfo.InvariantCulture)));
- }
- action OnInOperandDecimalEnd{
- string token = data.Substring(tokenStartPos, p-tokenStartPos-1);
- ((InOperator)currentOperator).Operands.Add(new OperandValue(decimal.Parse(token, CultureInfo.InvariantCulture)));
- }
- action OnInOperandDoubleEnd{
- string token = data.Substring(tokenStartPos, p-tokenStartPos);
- ((InOperator)currentOperator).Operands.Add(new OperandValue(double.Parse(token, CultureInfo.InvariantCulture)));
- }
- action OnInOperandNumberEnd{
- string token = data.Substring(tokenStartPos, p-tokenStartPos);
- ((InOperator)currentOperator).Operands.Add(new OperandValue(int.Parse(token, CultureInfo.InvariantCulture)));
- }
- action OnInOperandQStringEnd{
- string token = data.Substring(tokenStartPos+1, p-tokenStartPos-2);
- ((InOperator)currentOperator).Operands.Add(new OperandValue(token));
- }
- action OnInOperandDateTimeEnd{
- string token = data.Substring(tokenStartPos+1, p-tokenStartPos-2);
- ((InOperator)currentOperator).Operands.Add(new OperandValue(DateTime.ParseExact(token, "o", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal)));
- }
- action OnInOperandGuidEnd{
- string token = data.Substring(tokenStartPos, p-tokenStartPos);
- ((InOperator)currentOperator).Operands.Add(new OperandValue(Guid.ParseExact(token, "B")));
- }
- action OnInQueryStart{
- var opIn = new InOperator();
- opIn.LeftOperand = new OperandProperty(currentOperatorPropertyName);
- currentOperator = opIn;
- }
- action OnBinaryQueryStart{
- var opBinary = new BinaryOperator();
- opBinary.LeftOperand = new OperandProperty(currentOperatorPropertyName);
- currentOperator = opBinary;
- }
- action OnBinaryOperatorAndEnd{
- rv = new BinaryOperator(rv, null, BinaryOperatorType.And);
- currentOperator = null;
- }
- action OnBinaryOperatorEqualEnd{
- ((BinaryOperator)currentOperator).OperatorType = BinaryOperatorType.Equal;
- }
- action OnBinaryOperatorNotEqualEnd{
- ((BinaryOperator)currentOperator).OperatorType = BinaryOperatorType.NotEqual;
- }
- action OnBinaryOperatorLessOrEqualEnd{
- ((BinaryOperator)currentOperator).OperatorType = BinaryOperatorType.LessOrEqual;
- }
- action OnBinaryOperatorGreaterOrEqualEnd{
- ((BinaryOperator)currentOperator).OperatorType = BinaryOperatorType.GreaterOrEqual;
- }
- action OnBinaryOperatorLessEnd{
- ((BinaryOperator)currentOperator).OperatorType = BinaryOperatorType.Less;
- }
- action OnBinaryOperatorGreaterEnd{
- ((BinaryOperator)currentOperator).OperatorType = BinaryOperatorType.Greater;
- }
- action OnBinaryOperandFloatEnd{
- string token = data.Substring(tokenStartPos, p-tokenStartPos-1);
- ((BinaryOperator)currentOperator).RightOperand = new OperandValue(float.Parse(token, CultureInfo.InvariantCulture));
- }
- action OnBinaryOperandDecimalEnd{
- string token = data.Substring(tokenStartPos, p-tokenStartPos-1);
- ((BinaryOperator)currentOperator).RightOperand = new OperandValue(decimal.Parse(token, CultureInfo.InvariantCulture));
- }
- action OnBinaryOperandDoubleEnd{
- string token = data.Substring(tokenStartPos, p-tokenStartPos);
- ((BinaryOperator)currentOperator).RightOperand = new OperandValue(double.Parse(token, CultureInfo.InvariantCulture));
- }
- action OnBinaryOperandNumberEnd{
- string token = data.Substring(tokenStartPos, p-tokenStartPos);
- ((BinaryOperator)currentOperator).RightOperand = new OperandValue(int.Parse(token, CultureInfo.InvariantCulture));
- }
- action OnBinaryOperandQStringEnd{
- string token = data.Substring(tokenStartPos+1, p-tokenStartPos-2);
- ((BinaryOperator)currentOperator).RightOperand = new OperandValue(token);
- }
- action OnBinaryOperandDateTimeEnd{
- string token = data.Substring(tokenStartPos+1, p-tokenStartPos-2);
- ((BinaryOperator)currentOperator).RightOperand = new OperandValue(DateTime.ParseExact(token, "o", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal));
- }
- action OnBinaryOperandGuidEnd{
- string token = data.Substring(tokenStartPos, p-tokenStartPos);
- ((BinaryOperator)currentOperator).RightOperand = new OperandValue(Guid.ParseExact(token, "B"));
- }
- IDENTIFIER = [a-zA-Z0-9_]+;
- K_AND = "and"i;
- K_IN = "in"i;
- EQ = "=";
- NOT_EQ = "!=";
- LESS_OR_EQUAL = "<=";
- GREATER_OR_EQUAL = ">=";
- LESS = "<";
- GREATER = ">";
- F = "f"i;
- M = "m"i;
- NUMBER = ( "+" | "-" )? digit+;
- FLOAT_NUMBER = ( "+" | "-" )? digit+ "." digit+ F;
- DOUBLE_NUMBER= ( "+" | "-" )? digit+ "." digit+;
- DECIMAL_NUMBER=( "+" | "-" )? digit+ "." digit+ M;
- DQSTRING = "\"" [^"\""]* "\"";
- QSTRING = "\'" [^"\'"]* "\'";
- DIGIT4 = digit{4};
- DIGIT2 = digit{2};
- DIGIT7 = digit{7};
- DATETIME = "#" DIGIT4 "-" DIGIT2 "-" DIGIT2 "T" DIGIT2 ":" DIGIT2 ":" DIGIT2 "." DIGIT7 ("Z" | (("+"|"-") DIGIT2 ":" DIGIT2)) "#";
- HEXDIG8 = xdigit{8};
- HEXDIG4 = xdigit{4};
- HEXDIG12 = xdigit{12};
- GUID = "{" HEXDIG8 "-" HEXDIG4 "-" HEXDIG4 "-" HEXDIG4 "-" HEXDIG12 "}";
- binary_queryProperty = (IDENTIFIER) >markStart %OnPropertyEnd;
- binary_queryOperator = (
- EQ %OnBinaryQueryStart %OnBinaryOperatorEqualEnd
- |NOT_EQ %OnBinaryQueryStart %OnBinaryOperatorNotEqualEnd
- |LESS_OR_EQUAL %OnBinaryQueryStart %OnBinaryOperatorLessOrEqualEnd
- |GREATER_OR_EQUAL %OnBinaryQueryStart %OnBinaryOperatorGreaterOrEqualEnd
- |LESS %OnBinaryQueryStart %OnBinaryOperatorLessEnd
- |GREATER %OnBinaryQueryStart %OnBinaryOperatorGreaterEnd
- ) >markStart;
- binary_queryOperand = (
- FLOAT_NUMBER %OnBinaryOperandFloatEnd
- |DECIMAL_NUMBER %OnBinaryOperandDecimalEnd
- |DOUBLE_NUMBER %OnBinaryOperandDoubleEnd
- |NUMBER %OnBinaryOperandNumberEnd
- |DQSTRING %OnBinaryOperandQStringEnd
- |QSTRING %OnBinaryOperandQStringEnd
- |DATETIME %OnBinaryOperandDateTimeEnd
- |GUID %OnBinaryOperandGuidEnd
- ) >markStart;
- binary_query = (binary_queryProperty space* binary_queryOperator space* binary_queryOperand) %OnOperatorEnd;
- in_queryProperty = (IDENTIFIER) >markStart %OnPropertyEnd;
- in_queryOperator = (
- K_IN %OnInQueryStart
- );
- in_queryOperand = (
- FLOAT_NUMBER %OnInOperandFloatEnd
- |DECIMAL_NUMBER %OnInOperandDecimalEnd
- |DOUBLE_NUMBER %OnInOperandDoubleEnd
- |NUMBER %OnInOperandNumberEnd
- |DQSTRING %OnInOperandQStringEnd
- |QSTRING %OnInOperandQStringEnd
- |DATETIME %OnInOperandDateTimeEnd
- |GUID %OnInOperandGuidEnd
- ) >markStart;
- in_query = (in_queryProperty space+ in_queryOperator space* "(" space* in_queryOperand (space* "," space* in_queryOperand)* ")") %OnOperatorEnd;
- query = binary_query | in_query;
- main := (query? (space+ (K_AND %OnBinaryOperatorAndEnd) space+ query)*) $err(OnParseError);
- }%%
- /*
- THIS FILE WAS AUTOGENERATED BY Ragel (http://www.complang.org/ragel/).
- See lang description in: FQL.rl file
- You can contact me if you have a problem: mailto:afanasiev.dmitry@gmail.com
- */
- #pragma warning disable 162
- namespace Efir.DataHub.Filtering.QL
- {
- using System;
- using System.Diagnostics;
- using System.Globalization;
- using System.Collections.Generic;
- using Efir.DataHub.Filtering;
- internal static class FQLParserStaticRagel
- {
- #region data
- %% write data;
- #endregion
- static FQLParserStaticRagel() {}
- public static CriteriaOperator Parse(string data)
- {
- if (string.IsNullOrEmpty(data)) return null;
- try
- {
- int tokenStartPos = 0;
- CriteriaOperator rv = null;
- CriteriaOperator currentOperator = null;
- string currentOperatorPropertyName = null;
- int cs;
- int p = 0;
- int pe = data.Length;
- int eof = data.Length;
- #region init
- %% write init;
- #endregion
- #region exec
- %% write exec;
- #endregion
- return rv;
- }
- catch(ParseErrorException) {throw;}
- catch(Exception ex) {throw new ParseErrorException(ex.ToString());}
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement