/// <summary>
 ///     Initializes a new instance of the <see cref="ParseErrorException" /> class.
 /// </summary>
 /// <param name="error">The error message.</param>
 /// <param name="expression">The expression.</param>
 /// <param name="location">The error location.</param>
 /// <param name="innerException">The inner exception.</param>
 public ParseErrorException(string error, string expression, Location location, Exception innerException)
     : base(location.BuildParseError(error, expression), innerException)
 {
     Error = error;
     Expression = expression;
     Location = location.Clone();
 }
 /// <summary>
 ///     Initializes a new instance of the <see cref="ParseErrorException" /> class.
 /// </summary>
 /// <param name="error">The error message.</param>
 /// <param name="expression">The expression.</param>
 /// <param name="location">The error location.</param>
 /// <param name="innerException">The inner exception.</param>
 public ParseErrorException(string error, string expression, Location location, Exception innerException)
     : base(location.BuildParseError(error, expression), innerException)
 {
     Error      = error;
     Expression = expression;
     Location   = location.Clone();
 }
        private bool Next()
        {
            int line, column;

            RemainingExpression = RemainingExpression.TrimStart(out line, out column);
            Location.Line      += line;
            Location.Column     = line > 0 ? column : Location.Column + column;

            if (RemainingExpression.Length == 0)
            {
                return(false);
            }

            foreach (var kvp in RegexMap)
            {
                var regex = kvp.Value;
                var match = regex.Match(RemainingExpression);
                if (!match.Success)
                {
                    continue;
                }

                var value = match.Value;
                Token = new Token(kvp.Key, ConvertTokenValue(kvp.Key, value), Location.Clone());

                RemainingExpression = RemainingExpression.Substring(value.Length, out line, out column);
                Location.Line      += line;
                Location.Column     = line > 0 ? column : Location.Column + column;
                return(true);
            }
            throw new ParseErrorException("Invalid token.", Expr, Location);
        }
        /// <summary>
        ///     Analyzes a specified logical expression and extracts a sequence of tokens.
        /// </summary>
        /// <param name="expression">The logical expression.</param>
        /// <returns>
        ///     A sequence of extracted tokens.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">expression;Expression not provided.</exception>
        /// <exception cref="ParseErrorException"></exception>
        public IEnumerable <Token> Analyze(string expression)
        {
            if (expression == null)
            {
                throw new ArgumentNullException(nameof(expression), "Expression not provided.");
            }

            lock (_locker)
            {
                Location = new Location(1, 1);
                Expr     = RemainingExpression = expression;

                var tokens = new List <Token>();
                while (Next())
                {
                    tokens.Add(Token);
                }

                // once we've reached the end of the string, EOF token is returned - thus, parser's lookahead does not have to worry about running out of tokens
                tokens.Add(new Token(TokenType.EOF, string.Empty, Location.Clone()));
                return(tokens);
            }
        }
Example #5
0
        /// <summary>
        ///     Analyzes a specified logical expression and extracts a sequence of tokens.
        /// </summary>
        /// <param name="expression">The logical expression.</param>
        /// <returns>
        ///     A sequence of extracted tokens.
        /// </returns>
        /// <exception cref="System.ArgumentNullException"><c>expression</c> is null</exception>
        /// <exception cref="ParseErrorException"></exception>
        public IEnumerable<Token> Analyze(string expression)
        {
            if (expression == null)
                throw new ArgumentNullException(nameof(expression), "Expression not provided.");

            lock (_locker)
            {
                Location = new Location(1, 1);
                ExprString = RemainingExprString = expression;

                var tokens = new List<Token>();
                while (Next())
                {
                    tokens.Add(Token);
                }

                // once we've reached the end of the string, EOF token is returned - thus, parser's lookahead does not have to worry about running out of tokens
                tokens.Add(new Token(TokenType.EOF, string.Empty, string.Empty, Location.Clone()));
                return tokens;
            }
        }