public long TryParse(string str) { long total = 0; long currentNum = 0; ParsePhase currentPhase = ParsePhase.pfNone; str += " "; for (int i = 0; i < str.Length; i++) { if (str[i] == ' ') { if (currentPhase != ParsePhase.pfNone) { throw new TimeIntervalException("Time interval parse error"); } } else if (str[i] >= '0' && str[i] <= '9') { currentNum *= 10; currentNum += Convert.ToInt16(str[i]) - Convert.ToInt16('0'); currentPhase = ParsePhase.pfDigit; } else { if (currentPhase != ParsePhase.pfDigit) { throw new TimeIntervalException("Interval modifier before number"); } switch (str[i]) { case 's': total += currentNum; break; case 'm': total += currentNum * secondsInMinute; break; case 'h': total += currentNum * secondsInMinute * minutesInHour; break; case 'd': total += currentNum * hoursInDay * secondsInMinute * minutesInHour; break; case 'w': total += currentNum * daysInWeek * hoursInDay * secondsInMinute * minutesInHour; break; case 'M': total += currentNum * weeksInMonth * daysInWeek * hoursInDay * secondsInMinute * minutesInHour; break; default: throw new TimeIntervalException("Unknown time interval modifier"); } currentNum = 0; currentPhase = ParsePhase.pfNone; } } return(total); }
private static string ParseFunc_Identify(string line, ref int i, ref ParsePhase phase, char c) { string identify = null; var endIndex = IndexOfIdentifyEnd(line, i + 1); if (i < endIndex) { identify = line.Substring(i, endIndex - i + 1); i = endIndex; } else { identify = c.ToString(); } return(identify); }
private static string ParseFunc_String(string line, ref int i, ref ParsePhase phase, char separator) { var begIndex = i + 1; var endIndex = IndexOfStringEnd(line, begIndex, separator); if (i < endIndex) { var str = line.Substring(begIndex, endIndex - i); i = endIndex; return(str); } else { var exception = new LexException(Error.StringNoEnd); exception.col = i + 1; throw exception; } }
/// <summary> /// Fill the SELECT field /// </summary> private Expression FillAggregate(ParsePhase phase, MethodCallExpression node) { if (phase != ParsePhase.SelectBinding || !_isSelectQuery) { throw new CqlLinqNotSupportedException(node, phase); } if (node.Arguments.Count == 2) { var cqlFunction = node.Method.Name == "Average" ? "AVG" : node.Method.Name; Visit(node.Arguments[1]); if (_selectFields.Count == 0) { // The selected field should be populated by now throw new CqlLinqNotSupportedException(node, phase); } var index = _selectFields.Count - 1; _selectFields[index] = cqlFunction.ToUpperInvariant() + "(" + _selectFields[index] + ")"; } else { _selectFields.Add("COUNT(*)"); } return(node); }
internal CqlLinqNotSupportedException(Expression expression, ParsePhase parsePhase) : base(string.Format("The expression {0} = [{1}] is not supported in {2} parse phase.", expression.NodeType, expression, parsePhase)) { Expression = expression; }
internal CqlLinqNotSupportedException(Expression expression, ParsePhase parsePhase) : base(string.Format("The expression '{0}' is not supported in '{1}' parse phase.", expression.NodeType.ToString(), parsePhase.ToString())) { Expression = expression; }
public static ParsePhase ParseLine <T>(string line, int lineNumber, ParsePhase phase, System.Action <Data, T> dataReceiver, T custom) { if (string.IsNullOrEmpty(line)) { return(phase); } int i = 0; if (ParsePhase.Note == phase) { var endIndex = line.IndexOf("*/"); if (0 > endIndex) { #if LOG_NOTE Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine(line); #endif return(phase); } #if LOG_NOTE Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine(line.Substring(0, endIndex + 2)); #endif phase = ParsePhase.New; i = endIndex + 2; } for (; i < line.Length; ++i) { var startI = i; Data data = null; var c = line[i]; ParseFunc func; if (StartCharMap.TryGetValue(c, out func)) { if (null != func) { data = func(line, ref i, ref phase); } } else if ('_' == c) { // identify var identify = ParseFunc_Identify(line, ref i, ref phase, c); data = new Data(Token.Identify, identify); } else if (char.IsLetter(c)) { var identify = ParseFunc_Identify(line, ref i, ref phase, c); Keyword k; if (Enum.TryParse(identify, true, out k) && ("API" == identify || identify.ToLower() == identify)) { // keyword data = new Data(Token.Keyword, k); } else { // identify data = new Data(Token.Identify, identify); } } else if (char.IsDigit(c)) { // constant data = ParseFunc_Number(line, ref i, ref phase, c); } else if (!char.IsWhiteSpace(c)) { var exception = new LexException(Error.InvalidCharactor); exception.col = i + 1; throw exception; } if (null != data) { data.line = line; data.row = lineNumber; data.col = startI + 1; dataReceiver(data, custom); } } return(phase); }
private static Data ParseFunc_Number(string line, ref int i, ref ParsePhase phase, char c) { /* * number phase * 0: scan number * 1: scan float or double * 2: scan end char * 3: check next char is not(letter,digit,_) */ Token token = Token.Int32; int numberPhase = 0; int j = i + 1; for (; j < line.Length; ++j) { var d = line[j]; switch (numberPhase) { case 0: switch (d) { case 't': numberPhase = 3; token = Token.Int8; continue; case 's': numberPhase = 3; token = Token.Int16; continue; case 'l': numberPhase = 3; token = Token.Int64; continue; case 'f': numberPhase = 3; token = Token.Float; continue; case 'u': numberPhase = 2; token = Token.UInt32; continue; case '.': numberPhase = 1; token = Token.Double; continue; } break; case 1: if ('f' == d) { numberPhase = 3; token = Token.Float; continue; } break; case 2: switch (d) { case 't': numberPhase = 3; token = Token.UInt8; continue; case 's': numberPhase = 3; token = Token.UInt16; continue; case 'l': numberPhase = 3; token = Token.UInt64; continue; } break; case 3: if ('_' == d || '.' == d || char.IsLetterOrDigit(d)) { var exception = new LexException(Error.InvalidCharactor); exception.col = j + 1; throw exception; } else { // end var data = new Data(token); data.number = TryParseNumber(token, line.Substring(i, j - i)); i = j - 1; return(data); } } if (!char.IsDigit(d)) { if ('_' == d || '.' == d || char.IsLetter(d)) { var exception = new LexException(Error.InvalidCharactor); exception.col = j + 1; throw exception; } else { // end var data = new Data(token); data.number = TryParseNumber(token, line.Substring(i, j - i)); i = j - 1; return(data); } } } return(null); }