private IToken TryGetVariable()
        {
            // Consume first Character which must be a ?
            ConsumeCharacter();

            // Consume other valid Characters
            char next = Peek();

            while (Char.IsLetterOrDigit(next) || UnicodeSpecsHelper.IsLetterOrDigit(next) || next == '-' || next == '_')
            {
                ConsumeCharacter();
                next = Peek();
            }

            // Validate
            String value = Value;

            if (IsValidVarName(value))
            {
                return(new VariableToken(value, CurrentLine, StartPosition, EndPosition));
            }
            else
            {
                throw Error("The value '" + value + "' is not valid a Variable Name");
            }
        }
Beispiel #2
0
        private IToken TryGetVariable()
        {
            // Consume first Character which must be a ?/$
            ConsumeCharacter();

            // Consume other valid Characters
            char next = Peek();

            while (Char.IsLetterOrDigit(next) || UnicodeSpecsHelper.IsLetterOrDigit(next) || next == '-' || next == '_' || next == '\\')
            {
                if (next == '\\')
                {
                    // Check its a valid Escape
                    HandleEscapes(TokeniserEscapeMode.QName);
                }
                else
                {
                    ConsumeCharacter();
                }
                next = Peek();
            }

            // Validate
            String value = Value;

            if (value.EndsWith("."))
            {
                Backtrack();
                value = value.Substring(0, value.Length - 1);
            }

            if (SparqlSpecsHelper.IsValidVarName(value))
            {
                return(new VariableToken(value, CurrentLine, StartPosition, EndPosition));
            }
            else
            {
                throw Error("The value '" + value + "' is not valid a Variable Name");
            }
        }
        private bool IsValidVarName(String value)
        {
            if (_isValidVarName.IsMatch(value))
            {
                return(true);
            }
            else
            {
                // Have to validate Character by Character
                char[] cs    = value.ToCharArray();
                char   first = cs[0];

                // First character must be an underscore or letter
                if (first == '_' || Char.IsLetter(first) || UnicodeSpecsHelper.IsLetter(first))
                {
                    // Remaining Characters must be underscores, letters, numbers or hyphens
                    for (int i = 1; i < cs.Length; i++)
                    {
                        char c = cs[i];
                        if (c == '_' || c == '-' || Char.IsLetterOrDigit(c) || UnicodeSpecsHelper.IsLetterOrDigit(c))
                        {
                            // OK
                        }
                        else
                        {
                            // Invalid Character
                            return(false);
                        }
                    }

                    // If we get here it's all fine
                    return(true);
                }
                else
                {
                    // Invalid Start Character
                    return(false);
                }
            }
        }