Ejemplo n.º 1
0
        private static GlobNode ParseNode(GlobParserContext context)
        {
            if (context.CurrentToken?.Kind == GlobTokenKind.Wildcard)
            {
                var next = context.Peek();
                if (next != null && next.Kind == GlobTokenKind.Wildcard)
                {
                    context.Accept(GlobTokenKind.Wildcard);
                    context.Accept(GlobTokenKind.Wildcard);
                    return(new RecursiveWildcardNode());
                }
            }
            else if (context.CurrentToken?.Kind == GlobTokenKind.Parent)
            {
                context.Accept(GlobTokenKind.Parent);
                return(new ParentDirectoryNode());
            }
            else if (context.CurrentToken?.Kind == GlobTokenKind.Current)
            {
                context.Accept(GlobTokenKind.Current);
                return(new CurrentDirectoryNode());
            }

            var items = new List <PathSegment>();

            while (true)
            {
                switch (context.CurrentToken?.Kind)
                {
                case GlobTokenKind.Text:
                case GlobTokenKind.CharacterWildcard:
                case GlobTokenKind.Wildcard:
                case GlobTokenKind.BracketWildcard:
                case GlobTokenKind.BraceExpansion:
                    items.Add(ParsePathSegment(context));
                    continue;
                }

                break;
            }

            return(new PathNode(items, context.Options));
        }
Ejemplo n.º 2
0
        private GlobNode ParseRoot(GlobParserContext context)
        {
            if (context.CurrentToken == null)
            {
                return(new RelativeRootNode());
            }

            if (_environment.Platform.IsUnix())
            {
                // Starts with a separator?
                if (context.CurrentToken.Kind == GlobTokenKind.PathSeparator)
                {
                    return(new UnixRootNode());
                }
            }
            else
            {
                // Starts with a separator?
                if (context.CurrentToken.Kind == GlobTokenKind.PathSeparator)
                {
                    if (context.Peek()?.Kind == GlobTokenKind.PathSeparator)
                    {
                        context.Accept();
                        return(new UncRootNode(string.Empty));
                    }

                    // Get the drive from the working directory.
                    var workingDirectory = _environment.WorkingDirectory;
                    var root             = workingDirectory.FullPath.Split(':').First();
                    return(new WindowsRootNode(root));
                }

                // Is this a drive?
                if (context.CurrentToken.Kind == GlobTokenKind.Text &&
                    context.CurrentToken.Value.Length == 1 &&
                    context.Peek()?.Kind == GlobTokenKind.WindowsRoot)
                {
                    var identifier = ParseText(context);
                    context.Accept(GlobTokenKind.WindowsRoot);
                    return(new WindowsRootNode(identifier.Value));
                }
            }

            // Starts with an identifier?
            if (context.CurrentToken.Kind == GlobTokenKind.Text)
            {
                // Is the identifier indicating a current directory?
                if (context.CurrentToken.Value == ".")
                {
                    context.Accept(GlobTokenKind.Text);
                    if (context.CurrentToken.Kind != GlobTokenKind.PathSeparator)
                    {
                        throw new InvalidOperationException();
                    }

                    context.Accept(GlobTokenKind.PathSeparator);
                }
            }

            return(new RelativeRootNode());
        }