示例#1
0
 /// <summary>
 /// Determines whether a particular character is valid within an atom.
 /// </summary>
 public static bool IsValidIdentifierAtomChar(char value)
 {
     return(SymbolCharacters.IsValidChar(value));
 }
示例#2
0
 /// <summary>
 /// Determines whether a particular character is valid within a relative identifier.
 /// </summary>
 public static bool IsValidRelativeIdChar(char value)
 {
     return(SymbolCharacters.IsValidChar(value));
 }
示例#3
0
        /// <summary>
        /// Try to create a PartialSymbol from a string.
        /// </summary>
        /// <returns>Return the parser result indicating success, or what was wrong with the parsing.</returns>
        public static ParseResult TryCreate <T>(StringTable table, T partialSymbol, out PartialSymbol result, out int characterWithError)
            where T : struct, ICharSpan <T>
        {
            Contract.RequiresNotNull(table);

            using (var wrap = Pools.GetStringIdList())
            {
                List <StringId> components = wrap.Instance;

                int index = 0;
                int start = 0;
                int last  = partialSymbol.Length - 1;
                while (index < partialSymbol.Length)
                {
                    var ch = partialSymbol[index];

                    // trivial reject of invalid characters
                    if (!SymbolCharacters.IsValidDottedIdentifierChar(ch))
                    {
                        characterWithError = index;
                        result             = Invalid;
                        return(ParseResult.FailureDueToInvalidCharacter);
                    }

                    if (ch == SymbolCharacters.DottedIdentifierSeparatorChar)
                    {
                        // found a component separator
                        if (index == start || index == last)
                        {
                            characterWithError = index;
                            result             = Invalid;
                            return(ParseResult.LeadingOrTrailingDot);
                        }
                        else if (index > start)
                        {
                            // make a identifier atom out of [start..index]
                            SymbolAtom             atom;
                            int                    charError;
                            SymbolAtom.ParseResult papr = SymbolAtom.TryCreate(
                                table,
                                partialSymbol.Subsegment(start, index - start),
                                out atom,
                                out charError);

                            if (papr != SymbolAtom.ParseResult.Success)
                            {
                                characterWithError = index + charError;
                                result             = Invalid;
                                return(ParseResult.FailureDueToInvalidCharacter);
                            }

                            components.Add(atom.StringId);
                        }

                        // skip over the dot
                        index++;
                        start = index;
                        continue;
                    }

                    index++;
                }

                if (index > start)
                {
                    // make a identifier atom out of [start..index]
                    SymbolAtom             atom;
                    int                    charError;
                    SymbolAtom.ParseResult papr = SymbolAtom.TryCreate(
                        table,
                        partialSymbol.Subsegment(start, index - start),
                        out atom,
                        out charError);

                    if (papr != SymbolAtom.ParseResult.Success)
                    {
                        characterWithError = index + charError;
                        result             = Invalid;
                        return(ParseResult.FailureDueToInvalidCharacter);
                    }

                    components.Add(atom.StringId);
                }

                result = new PartialSymbol(components.ToArray());

                characterWithError = -1;
                return(ParseResult.Success);
            }
        }
示例#4
0
        private static ParseResult TryParseIdentifier <TChars>(
            ExpandedTokenData <TChars> token,
            PipExecutionContext context,
            ref int position,
            out TChars identifier,
            bool logErrors = true)
            where TChars : struct, ICharSpan <TChars>
        {
            Contract.Requires(token.IsValid);
            Contract.RequiresNotNull(context);

            var pathTable = context.PathTable;

            var text = token.Text;

            if (text.Length == 0 || position == text.Length)
            {
                var updateToken = token.UpdateLineInformationForPosition(position);
                identifier = default(TChars);
                return(new ParseResult()
                {
                    Status = ParseStatus.UnexpectedEmptyIdentifier,
                    Path = updateToken.Path.ToString(pathTable),
                    Line = updateToken.Line,
                    Position = updateToken.Position,
                });
            }

            int firstPosition = position;

            char firstChar = text[position];

            if (!SymbolCharacters.IsValidStartChar(firstChar))
            {
                var updateToken = token.UpdateLineInformationForPosition(position);
                identifier = default(TChars);
                return(new ParseResult()
                {
                    Status = ParseStatus.UnexpectedCharacterAtStartOfIdentifier,
                    Path = updateToken.Path.ToString(pathTable),
                    Line = updateToken.Line,
                    Position = updateToken.Position,
                    Text = firstChar.ToString(),
                });
            }

            position++;

            for (; position < text.Length; position++)
            {
                char ch = text[position];
                if (!SymbolCharacters.IsValidChar(ch))
                {
                    break;
                }
            }

            identifier = text.Subsegment(firstPosition, position - firstPosition);
            return(new ParseResult {
                Status = ParseStatus.Success
            });
        }