Example #1
0
        public Shell.Types.IShellReturnable ResultOf(string src, bool program)
        {
            logger.Verbose($"Source:\n{src.Trim()}");
            ICharStream charStream = CharStreams.fromstring(src);

            lexer = new ShellLexer(charStream);
            ITokenStream tokenStream = new CommonTokenStream(lexer);

            parser = new ShellParser(tokenStream)
            {
                BuildParseTree = true,
                ErrorHandler   = new BailErrorStrategy()
            };
            parser.RemoveErrorListeners();
            parser.AddErrorListener(new SyntaxErrorListener());
            IParseTree tree;

            // select the appropriate start rule
            if (program)
            {
                tree = parser.program();
            }
            else
            {
                tree = parser.statement();
            }

            logger.Debug($"Parse tree:\n{tree.ToStringTree(parser)}");
            visitor.tokenSource = lexer.InputStream;

            return(visitor.Visit(tree));
        }
Example #2
0
        public IShellSegment Parse(ShellParser parser, SyntaxToken <ShellTokenKind> token)
        {
            if (parser.MatchOne(ShellTokenKind.LeftParen))
            {
                parser.Take(ShellTokenKind.LeftParen);
                var segment = parser.WithCommandParsing(() => parser.Parse());
                parser.Take(ShellTokenKind.RightParen);

                return(new CommandInterpolationSegment(segment));
            }

            var varToken = parser.Take(ShellTokenKind.String);

            if (varToken.Text is null)
            {
                throw new ShellSyntaxException("Variable name cannot be a string interpolation", varToken.Span);
            }

            var varName = varToken.Text;

            if (!varName.Contains(":"))
            {
                return(new VariableSegment(null, varName));
            }

            var scopeEnd = varName.IndexOf(':');

            var scope = varName.Substring(0, scopeEnd);
            var name  = varName.Substring(scopeEnd + 1);

            return(new VariableSegment(scope, name));
        }
Example #3
0
        private static ShellParser MakeParser(string text)
        {
            AntlrInputStream  inputStream       = new AntlrInputStream(text);
            ShellLexer        shellLexer        = new ShellLexer(inputStream);
            CommonTokenStream commonTokenStream = new CommonTokenStream(shellLexer);
            ShellParser       shellParser       = new ShellParser(commonTokenStream);

            return(shellParser);
        }
Example #4
0
        public void ArgWordTest()
        {
            ShellParser  shellParser = MakeParser("wordarg");
            ArgContext   context     = shellParser.arg();
            ShellVisitor visitor     = new ShellVisitor();

            ParserResult result = visitor.Visit(context);

            result.IsArg.Should().BeTrue();
            result.ArgValue.Should().Be("wordarg");
        }
Example #5
0
        public void ArgStringPipeTest()
        {
            ShellParser  shellParser = MakeParser("\"12345 | echo asdf\"");
            ArgContext   context     = shellParser.arg();
            ShellVisitor visitor     = new ShellVisitor();

            ParserResult result = visitor.Visit(context);

            result.IsArg.Should().BeTrue();
            result.ArgValue.Should().Be("12345 | echo asdf");
        }
Example #6
0
        public void ArgStringSingleQuoteTest()
        {
            ShellParser  shellParser = MakeParser("'string arg'");
            ArgContext   context     = shellParser.arg();
            ShellVisitor visitor     = new ShellVisitor();

            ParserResult result = visitor.Visit(context);

            result.IsArg.Should().BeTrue();
            result.ArgValue.Should().Be("string arg");
        }
Example #7
0
        public void CmdTest()
        {
            ShellParser  shellParser = MakeParser("cmdword");
            CmdContext   context     = shellParser.cmd();
            ShellVisitor visitor     = new ShellVisitor();

            ParserResult result = visitor.Visit(context);

            result.IsCmd.Should().BeTrue();
            result.CmdValue.Should().Be("cmdword");
        }
Example #8
0
        public override IShellReturnable Execute(State state, List <Tuple <int, string, Shell.Types.IShellData> > args)
        {
            var name         = (Shell.Types.String)args.First().Item3;
            var namespacedid = (Shell.Types.String)args.Skip(1).First().Item3;

            // test the name to ensure its valid
            var match = Regex.Match(name.contents, @"(\w|_)(\w|\d|_)*");

            if (!match.Success && match.Length != name.contents.Length)
            {
                Console.WriteLine("Aliasing failed! The given name cannot be set as an identifier.");
                return(new Shell.Types.None());
            }

            if (state.Names.Exists(name.contents))
            {
                Console.WriteLine("Aliasing failed! The given name is already defined.");
                return(new Shell.Types.None());
            }

            // parse the namespaced id
            var charStream  = CharStreams.fromstring(namespacedid.contents);
            var lexer       = new ShellLexer(charStream);
            var tokenStream = new CommonTokenStream(lexer);
            var parser      = new ShellParser(tokenStream)
            {
                ErrorHandler = new BailErrorStrategy()
            };

            ShellParser.Identifier_namespacedContext context;

            try {
                context = parser.identifier_namespaced();
            } catch (Exception e) {
                Console.WriteLine($"Aliasing failed! Could not parse the given namespaced identifier: {e.Message}");
                return(new Shell.Types.None());
            }

            var result = Utility.GetNamespacedIdentifier(context, state);

            if (!(result is Shell.Types.Function))
            {
                Console.WriteLine("Aliasing failed! The given namespaced id does not refer to a function.");
                return(new Shell.Types.None());
            }

            // set the alias in the calling context
            var tup = state.contexts.SkipLast(1).Last();

            tup.Item2.Set(name.contents, (Shell.Types.Function)result);

            return(new Shell.Types.None());
        }
Example #9
0
        public void SimpleCommandNoArgsTest()
        {
            ShellParser          shellParser = MakeParser("git");
            SimpleCommandContext context     = shellParser.simpleCommand();
            ShellVisitor         visitor     = new ShellVisitor();

            ParserResult  result        = visitor.Visit(context);
            SimpleCommand actualCommand = result.SimpleCommandValue;

            result.IsSimpleCommand.Should().BeTrue();
            actualCommand.Command.Should().Be("git");
            actualCommand.Arguments.Should().BeEmpty();
        }
Example #10
0
        public void ArgListMixedArgsTest()
        {
            ShellParser  shellParser = MakeParser("these are 'some args'");
            ArgsContext  context     = shellParser.args();
            ShellVisitor visitor     = new ShellVisitor();

            ParserResult result = visitor.Visit(context);

            result.IsArgList.Should().BeTrue();
            result.ArgListValue.Should().BeEquivalentTo(new List <string> {
                "these", "are", "some args"
            }, opts => opts.WithStrictOrdering());
        }
Example #11
0
        public void PipeListOneCommandTest()
        {
            ShellParser     shellParser = MakeParser("git reset --hard");
            PipeListContext context     = shellParser.pipeList();
            ShellVisitor    visitor     = new ShellVisitor();

            ParserResult         result         = visitor.Visit(context);
            List <SimpleCommand> actualPipeList = result.PipeListValue;

            result.IsPipeList.Should().BeTrue();
            actualPipeList.Should().HaveCount(1);
            actualPipeList[0].ToString().Should().Be("git reset --hard");
        }
Example #12
0
        public void ShellCommandOnlyPipesTest()
        {
            ShellParser         shellParser = MakeParser("git reset --hard | echo");
            ShellCommandContext context     = shellParser.shellCommand();
            ShellVisitor        visitor     = new ShellVisitor();

            ParserResult result       = visitor.Visit(context);
            ShellCommand shellCommand = result.ShellCommandValue;

            result.IsShellCommand.Should().BeTrue();
            shellCommand.IsBackground.Should().BeFalse();
            shellCommand.CommandList.Should().HaveCount(2);
        }
Example #13
0
        public void ArgListOneWordTest()
        {
            ShellParser  shellParser = MakeParser("wordarg");
            ArgsContext  context     = shellParser.args();
            ShellVisitor visitor     = new ShellVisitor();

            ParserResult result = visitor.Visit(context);

            result.IsArgList.Should().BeTrue();
            result.ArgListValue.Should().BeEquivalentTo(new List <string> {
                "wordarg"
            }, opts => opts.WithStrictOrdering());
        }
Example #14
0
        private static ShellCommand ParseCommand(string command)
        {
            AntlrInputStream  inputStream       = new AntlrInputStream(command);
            ShellLexer        shellLexer        = new ShellLexer(inputStream);
            CommonTokenStream commonTokenStream = new CommonTokenStream(shellLexer);
            ShellParser       shellParser       = new ShellParser(commonTokenStream);

            ShellParser.ShellCommandContext context = shellParser.shellCommand();
            ShellVisitor visitor = new ShellVisitor();
            ParserResult result  = visitor.Visit(context);

            return(result.ShellCommandValue);
        }
Example #15
0
        public void ShellCommandSingleCommandTest()
        {
            ShellParser         shellParser = MakeParser("git reset --hard");
            ShellCommandContext context     = shellParser.shellCommand();
            ShellVisitor        visitor     = new ShellVisitor();

            ParserResult result       = visitor.Visit(context);
            ShellCommand shellCommand = result.ShellCommandValue;

            result.IsShellCommand.Should().BeTrue();
            shellCommand.IsBackground.Should().BeFalse();
            shellCommand.CommandList.Should().HaveCount(1);
            shellCommand.CommandList[0].ToString().Should().Be("git reset --hard");
        }
Example #16
0
        public void SimpleCommandWithArgsTest()
        {
            ShellParser          shellParser = MakeParser("git reset --hard");
            SimpleCommandContext context     = shellParser.simpleCommand();
            ShellVisitor         visitor     = new ShellVisitor();

            ParserResult  result        = visitor.Visit(context);
            SimpleCommand actualCommand = result.SimpleCommandValue;

            result.IsSimpleCommand.Should().BeTrue();
            actualCommand.Command.Should().Be("git");
            actualCommand.Arguments.Should().BeEquivalentTo(new List <string> {
                "reset", "--hard"
            }, opt => opt.WithStrictOrdering());
        }
Example #17
0
        static void Main(string[] args)
        {
            Logger logger = new Logger("AddTasks", "AddTasks");

            logger.V("Staring app");
            logger.I("Values " + string.Join(" ", args));

            SCHElement element = null;

            try
            {
                element = ShellParser.Parse(args, SCHEnvironment.LibDir);
                logger.I("Parse has been ended");
            } catch (Exception e)
            {
                logger.E("Parsing error");
                logger.E(e.ToString());

                Error(e.ToString());
                return;
            }

            using (SchedulerClient client = new SchedulerClient())
            {
                if (!client.Connect())
                {
                    Error("Unable to connect to server!");

                    logger.E("Unable to connect to server!");
                    logger.E(client.ExMsg);

                    Error(client.ExMsg);
                    return;
                }

                if (!client.SendElement(element))
                {
                    logger.E("Unable to send data");
                    logger.E(client.ExMsg);

                    Error("Unable to send data!");
                    Error(client.ExMsg);
                    return;
                }
                logger.I("OK!");
            }
        }
Example #18
0
        /// <summary>
        /// Loads a namespace definition from file
        /// </summary>
        /// <remark>
        /// Pass rename="" to load namespace with original name
        /// </remark>
        public static Shell.Types.Namespace LoadNamespace(string path, string rename, Visitor visitor)
        {
            var pathFull = Path.GetFullPath(path);
            var pathDir  = Path.GetDirectoryName(pathFull);
            // Read and prepare file
            var src = Encoding.ASCII.GetString(File.ReadAllBytes(path));

            if (src.Last() != '\n')
            {
                src += '\n';
            }

            var charStream  = CharStreams.fromstring(src);
            var lexer       = new ShellLexer(charStream);
            var tokenStream = new CommonTokenStream(lexer);
            var parser      = new ShellParser(tokenStream);
            var tree        = parser.namespace_declaration();

            return(new Shell.Types.Namespace(rename, pathDir, tree, visitor));
        }
Example #19
0
        private static IShellSegment ParseCommandLine(
            string input, out IEnumerable <SyntaxToken <ShellTokenKind> > tokens
            )
        {
            try
            {
                var lexer  = new ShellLexer(input);
                var parser = new ShellParser(tokens = lexer.Tokenize());
                return(parser.ParseAll());
            }
            catch (ShellSyntaxException ex) when(!Debugger.IsAttached)
            {
                Console.WriteLine();
                ConsoleEx.WriteError($"{ex.Message} at position {ex.Span.Start.Index}");
                Console.WriteLine();

                const int    PaddingSize    = 10;
                const string PrefixEllipsis = "... ";

                var trim    = ex.Span.Start.Index > PaddingSize && input.Length > Console.BufferWidth;
                var section = trim ? $"{PrefixEllipsis}{input.Substring( ex.Span.Start.Index - PaddingSize )}" : input;

                if (section.Length > Console.BufferWidth)
                {
                    const string TrailingEllipsis = " ...";
                    section =
                        $"{section.Substring( 0, Console.BufferWidth - TrailingEllipsis.Length )}{TrailingEllipsis}";
                }

                var len        = trim ? PaddingSize + PrefixEllipsis.Length : ex.Span.Start.Index;
                var whitespace = new string( ' ', len );
                var line       = new string( '─', len );

                Console.WriteLine(section);
                Console.WriteLine($"{whitespace}^", Program.Config.ColorScheme.ErrorColor);
                Console.WriteLine($"{line}┘", Program.Config.ColorScheme.ErrorColor);
            }

            tokens = null;
            return(null);
        }
Example #20
0
        public IShellSegment ParseString(ShellParser parser, SyntaxToken <ShellTokenKind> token)
        {
            /* whatif: the top-level token's Data property is a list of tokens,
             *         but any child tokens in that list have their Data property
             *         set to an array of tokens due to how the lexer processes interpolations.
             *         Potentially confusing?
             */
            if (token.Text != null || !(token.Data is List <SyntaxToken <ShellTokenKind> > pieces))
            {
                return(new TextSegment(token.Text));
            }

            var segments = new List <IShellSegment>();

            foreach (var piece in pieces)
            {
                // ReSharper disable once SwitchStatementMissingSomeCases
                switch (piece.Kind)
                {
                case ShellTokenKind.String:
                    segments.Add(new TextSegment(piece.Text));
                    break;

                case ShellTokenKind.StringInterpolation:
                    var interpolationParser = new ShellParser(
                        (SyntaxToken <ShellTokenKind>[])piece.Data
                        );

                    segments.Add(interpolationParser.ParseAll());
                    break;

                default:
                    throw new NotSupportedException();
                }
            }

            return(new StringInterpolationSegment(segments));
        }
Example #21
0
        public IShellSegment Parse(ShellParser parser, SyntaxToken <ShellTokenKind> token)
        {
            var first = this.ParseString(parser, token);

            if (parser.CommandParsingDisabled)
            {
                return(first);
            }

            if (!parser.CommandParsingDisabled && !parser.HasSegment())
            {
                return(new CommandSegment(first));
            }

            var args = new List <IShellSegment>();

            while (parser.HasSegment())
            {
                args.Add(parser.WithoutCommandParsing(() => parser.Parse(Precedence.Command)));
            }

            return(new CommandSegment(first, args));
        }
Example #22
0
        Task execContextAsync(HttpListenerContext context, int idx)
        {
            return(Task.Run(() =>
            {
                try
                {
                    var prefix = string.Format("{0:000}: ", idx);
                    var req = context.Request;
                    var requestProxy = new RequestProxy(context);

                    log.info(prefix + "--------------------------------------------------------------------------------");

                    log.info(prefix + DateTime.Now, ConsoleColor.DarkGray);
                    log.info(prefix + "===== Request =====", ConsoleColor.DarkGreen);
                    log.info(prefix + req.HttpMethod + " " + req.Url.LocalPath, ConsoleColor.White);
                    log.headers(prefix, "[Headers] ", req.Headers);
                    if (req.HasEntityBody)
                    {
                        log.formatted(prefix, "[Body   ] ", requestProxy.getBody());
                    }

                    log.info(prefix + "===== Response =====", ConsoleColor.DarkGreen);

                    var pathInfo = findPath(requestProxy, context);
                    if (!pathInfo.HasValue)
                    {
                        pathInfo = new YamlPathInfo {
                            response = (YamlResponseInfo)settings.notfound
                        };
                    }
                    var p = pathInfo.Value;

                    Task taskWait = Task.Delay(p.wait > 0 ? p.wait : 0);

                    if (p.command != null)
                    {
                        log.info(prefix + "command: " + p.command);

                        List <string> commands = ShellParser.split(p.command);
                        var process = new Process();
                        var startInfo = new ProcessStartInfo
                        {
                            WindowStyle = ProcessWindowStyle.Hidden,
                            FileName = commands[0],
                            Arguments = string.Join(" ", commands.GetRange(1, commands.Count - 1))
                        };
                        process.StartInfo = startInfo;
                        process.Start();
                        process.WaitForExit();
                    }

                    var yamlres = p.response;
                    var defres = settings.defaultResponse;

                    int statudCode = int.Parse(yamlres.status ?? defres.status);

                    var respHeaders = new Dictionary <string, string>();
                    var headers = yamlres.headers ?? defres.headers;
                    if (headers != null)
                    {
                        foreach (var e in headers)
                        {
                            respHeaders[e.Key] = e.Value;
                        }
                    }

                    string body = "";
                    if (yamlres.bodytext != null || yamlres.bodyfile != null)
                    {
                        body += yamlres.bodytext ?? "";
                        body += readFile(yamlres.bodyfile) ?? "";
                    }
                    else
                    {
                        body += defres.bodytext ?? "";
                        body += readFile(defres.bodyfile) ?? "";
                    }

                    taskWait.Wait();

                    writeResponse(context, prefix, statudCode, body, respHeaders);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("message: " + ex.Message);
                    Console.WriteLine("stack trace: ");
                    Console.WriteLine(ex.StackTrace);
                    Console.WriteLine();
                }
            }));
        }
Example #23
0
        public IShellSegment Parse(ShellParser parser, IShellSegment left, SyntaxToken <ShellTokenKind> token)
        {
            var right = parser.Parse(this.Precedence);

            return(new PipeSegment(left, right));
        }
        public async Task <string> ReMux(string inputfile, string inputs, string subtileargs, Format formats,
                                         string audiolanguagecode, string audiolanguage, double initialPercent, double percentIncrement,
                                         DownloadInfo dinfo, IProgress <DownloadInfo> progress, CancellationToken token)
        {
            int formatcnt = 0;

            foreach (Format fol in Enum.GetValues(typeof(Format)))
            {
                if ((fol & formats) == fol)
                {
                    formatcnt++;
                }
            }

            percentIncrement /= (2 * formatcnt);
            string size             = string.Empty;
            string intermediatefile = dinfo.FullPath + ".tm2";

            foreach (Format fol in Enum.GetValues(typeof(Format)))
            {
                if ((fol & formats) == fol)
                {
                    string ffmpegargs = string.Format(LibSet[FFMPEGArgsS], inputfile, inputs, subtileargs, intermediatefile, fol == Format.Mkv ? string.Empty : "-c:s mov_text ", audiolanguagecode, audiolanguage, fol == Format.Mkv ? "-f matroska" : "-f mp4");
                    token.ThrowIfCancellationRequested();
                    dinfo.Percent   = initialPercent;
                    initialPercent += percentIncrement;
                    dinfo.Status    = "Muxing Video";
                    progress.Report(dinfo);
                    ShellParser ffm = new ShellParser();
                    await ffm.Start(LibSet[FFMPEGEXES], ffmpegargs, token);

                    dinfo.Percent   = initialPercent;
                    initialPercent += percentIncrement;
                    dinfo.Status    = "Unique Hashing";
                    progress.Report(dinfo);

                    if (fol == Format.Mkv)
                    {
                        Matroska.Matroska.MatroskaHash(intermediatefile, dinfo.FullPath + "." + fol.ToExtension());
                    }
                    else
                    {
                        Mp4.Mp4.Mp4Hash(intermediatefile, dinfo.FullPath + "." + fol.ToExtension());
                    }
                    try
                    {
                        File.Delete(intermediatefile);
                    }
                    catch (Exception)
                    {
                        // ignored
                    }
                    FileInfo f = new FileInfo(dinfo.FullPath + "." + fol.ToExtension());
                    if (formatcnt == 1)
                    {
                        size = f.Length.ToString();
                    }
                    else
                    {
                        size += f.Length.ToString() + " (" + fol.ToExtension() + "), ";
                    }
                }
            }
            try
            {
                File.Delete(inputfile);
            }
            catch (Exception)
            {
                // ignored
            }
            if (formatcnt > 1)
            {
                size = size.Substring(0, size.Length - 2);
            }
            return(size);
        }