public Scanner(SourceReader source, ErrorHandler errorHandler)
 {
     Source = source;
     ErrorHandler = errorHandler;
     SymbolTokens.Add("(", Token.Types.LParen);
     SymbolTokens.Add(")", Token.Types.RParen);
     SymbolTokens.Add("+", Token.Types.OpPlus);
     SymbolTokens.Add("-", Token.Types.OpMinus);
     SymbolTokens.Add("/", Token.Types.OpDivide);
     SymbolTokens.Add("*", Token.Types.OpMultiply);
     SymbolTokens.Add("<", Token.Types.OpLess);
     SymbolTokens.Add("=", Token.Types.OpEquals);
     SymbolTokens.Add("&", Token.Types.OpAnd);
     SymbolTokens.Add("!", Token.Types.OpNot);
     SymbolTokens.Add(";", Token.Types.LineTerm);
     SymbolTokens.Add(":", Token.Types.Colon);
     SymbolTokens.Add(":=", Token.Types.OpAssignment);
     SymbolTokens.Add("..", Token.Types.OpRange);
     KeywordTokens.Add("var", Token.Types.KwVar);
     KeywordTokens.Add("int", Token.Types.KwInt);
     KeywordTokens.Add("string", Token.Types.KwString);
     KeywordTokens.Add("bool", Token.Types.KwBool);
     KeywordTokens.Add("for", Token.Types.KwFor);
     KeywordTokens.Add("end", Token.Types.KwEnd);
     KeywordTokens.Add("in", Token.Types.KwIn);
     KeywordTokens.Add("do", Token.Types.KwDo);
     KeywordTokens.Add("read", Token.Types.KwRead);
     KeywordTokens.Add("print", Token.Types.KwPrint);
     KeywordTokens.Add("assert", Token.Types.KwAssert);
     EscapeCharacters.Add('"', '"');
     EscapeCharacters.Add('\'', '\'');
     EscapeCharacters.Add('n', '\n');
     EscapeCharacters.Add('t', '\t');
     EscapeCharacters.Add('\\', '\\');
 }
 public SourceReaderSample(SourceReader reader, SourceStream stream, SourceReaderSampleFlags flags, long timestamp, long duration, Sample sample, int count)
 {
     this.Reader = reader;
     this.Stream = stream;
     this.Flags = flags;
     this.Timestamp = timestamp;
     this.Duration = duration;
     this.Count = count;
     this.Sample = sample;
 }
Exemple #3
0
        public static CssValue Parse(string text)
        {
            using (var reader = new SourceReader(new StringReader(text)))
            {
                var tokenizer = new CssTokenizer(reader, LexicalMode.Value);

                var parser = new CssParser(tokenizer);

                return parser.ReadValueList();
            }
        }
Exemple #4
0
 /// <summary>
 /// Gets or sets the source stream. See remarks.
 /// </summary>
 /// <value>The source.</value>
 /// <remarks>
 /// The source must be set before calling <see cref="GetSamples()"/>
 /// </remarks>
 public void SetSourceStream(Stream value)
 {
     lock (sourceReaderLock)
     {
         // If the nextSourceReader is not null
         if (nextSourceReader != null)
             nextSourceReader.Dispose();
         nextSourceReader = new SourceReader(value);
         Initialize(nextSourceReader);
     }
 }
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("Usage: interpreter filename [-p]\n\n-p: Print the parsed abstract syntax tree.");
                return;
            }
            string filename = args[0];
            StreamReader sourceFile;
            try
            {
                sourceFile = new StreamReader(filename);
            }
            catch (Exception)
            {
                Console.WriteLine("File " + filename + " not found.");
                return;
            }

            SourceReader source = new SourceReader(sourceFile);
            ErrorHandler errors = new ErrorHandler();
            Scanner lexer = new Scanner(source, errors);
            Parser parser = new Parser(lexer, errors);
            StmtList ast = parser.Parse();
            TypeCheckerVisitor checker = new TypeCheckerVisitor(errors, ast);
            SymbolTable SymbolTable = checker.TypeCheck();
            if (errors.HasErrors)
            {
                Console.WriteLine("Given program contains following errors:");
                foreach (Error error in errors.GetErrors())
                {
                    Console.WriteLine(error);
                }
                return;
            }
            if (args.Length > 1 && args[1] == "-p")
            {
                Console.WriteLine("Parsed abstact syntax tree:");
                AstPrinterVisitor printer = new AstPrinterVisitor(ast);
                printer.Print();
                Console.WriteLine();
                Console.WriteLine("Program output:");
            }
            ExecutorVisitor executor = new ExecutorVisitor(SymbolTable, ast);
            try
            {
                executor.Execute();
            }
            catch (RuntimeException e)
            {
                Console.WriteLine("Runtime exception: " + e.Message);
            }
        }
        public ForkableScanner Create(SourceReader source)
        {
            CodeContract.RequiresArgumentNotNull(source, "source");

            Scanner masterScanner = new Scanner(ScannerInfo);
            masterScanner.SetSource(source);
            masterScanner.SetTriviaTokens(m_triviaTokens);
            masterScanner.ErrorManager = ErrorManager;
            masterScanner.RecoverErrors = RecoverErrors;
            masterScanner.LexicalErrorId = LexicalErrorId;

            return ForkableScanner.Create(masterScanner);
        }
        public ForkableScanner Create(SourceReader source)
        {
            CodeContract.RequiresArgumentNotNull(source, "source");

            Scanner masterScanner = new Scanner(ScannerInfo);
            masterScanner.SetSource(source);
            masterScanner.SetTriviaTokens(m_triviaTokens);
            masterScanner.ErrorList = ErrorList;
            masterScanner.RecoverErrors = RecoverErrors;
            masterScanner.LexicalErrorId = LexicalErrorId;
            masterScanner.ThrowAtReadingAfterEndOfStream = ThrowAtReadingAfterEndOfStream;

            return ForkableScanner.Create(masterScanner);
        }
		public void TestSourceReader()
		{
			SourceReader reader = new SourceReader(new StringReader("1234567890"));
			Assert.AreEqual('1', reader.Read());
			Assert.AreEqual('2', reader.Read());
			Assert.AreEqual("12", reader.ReadedBlock());
			Assert.AreEqual(true, reader.Unget());
			Assert.AreEqual(true, reader.Unget());
			Assert.AreEqual(false, reader.Unget());
			Assert.AreEqual('2', reader.Read(1));
			Assert.AreEqual("12", reader.ReadedBlock());
			Assert.AreEqual(true, reader.Unget());
			Assert.AreEqual("1", reader.ReadedBlock());
			Assert.AreEqual('3', reader.Read(1));
			Assert.AreEqual("123", reader.ReadedBlock());
			Assert.AreEqual("123", reader.Accept());
			Assert.AreEqual("", reader.ReadedBlock());
			Assert.AreEqual("", reader.Accept());
			Assert.AreEqual(false, reader.Unget());
			Assert.AreEqual('6', reader.Read(2));
			Assert.AreEqual("456", reader.ReadedBlock());
		}
Exemple #9
0
        public static MediaFoundationStreamingSources CreateFromFile(DXGIDeviceManager dxgiDeviceManager, VariablePath ファイルパス, WaveFormat soundDeviceFormat)
        {
            var sources = new MediaFoundationStreamingSources();

            #region " ファイルから SourceReaderEx を生成する。"
            //----------------
            using (var ビデオ属性 = new MediaAttributes())
            {
                // DXVAに対応しているGPUの場合には、それをデコードに利用するよう指定する。
                ビデオ属性.Set(SourceReaderAttributeKeys.D3DManager, dxgiDeviceManager);

                // 追加のビデオプロセッシングを有効にする。
                ビデオ属性.Set(SourceReaderAttributeKeys.EnableAdvancedVideoProcessing, true);    // 真偽値が bool だったり

                // 追加のビデオプロセッシングを有効にしたら、こちらは無効に。
                ビデオ属性.Set(SinkWriterAttributeKeys.ReadwriteDisableConverters, 0);             // int だったり

                // 属性を使って、SourceReaderEx を生成。
                using (var sourceReader = new SourceReader(ファイルパス.数なしパス, ビデオ属性))        // パスは URI 扱い
                    sources._SourceReaderEx = sourceReader.QueryInterface <SourceReaderEx>();
            }
            //----------------
            #endregion

            #region " WaveFormat を生成。"
            //----------------
            sources._Audioのフォーマット = new WaveFormat(
                soundDeviceFormat.SampleRate,
                32,
                soundDeviceFormat.Channels,
                AudioEncoding.IeeeFloat);
            //----------------
            #endregion

            sources._SourceReaderEx生成後の初期化();

            return(sources);
        }
Exemple #10
0
        public void TestSourceReader()
        {
            var reader = new SourceReader(new StringReader("1234567890"));

            Assert.AreEqual('1', reader.Read());
            Assert.AreEqual('2', reader.Read());
            Assert.AreEqual("12", reader.ReadedBlock());
            Assert.AreEqual(true, reader.Unget());
            Assert.AreEqual(true, reader.Unget());
            Assert.AreEqual(false, reader.Unget());
            Assert.AreEqual('2', reader.Read(1));
            Assert.AreEqual("12", reader.ReadedBlock());
            Assert.AreEqual(true, reader.Unget());
            Assert.AreEqual("1", reader.ReadedBlock());
            Assert.AreEqual('3', reader.Read(1));
            Assert.AreEqual("123", reader.ReadedBlock());
            Assert.AreEqual("123", reader.Accept());
            Assert.AreEqual("", reader.ReadedBlock());
            Assert.AreEqual("", reader.Accept());
            Assert.AreEqual(false, reader.Unget());
            Assert.AreEqual('6', reader.Read(2));
            Assert.AreEqual("456", reader.ReadedBlock());
        }
Exemple #11
0
        private long GetLength(SourceReader reader)
        {
            lock (_lockObj)
            {
                try
                {
                    if (reader == null)
                    {
                        return(0);
                    }

                    var value =
                        reader.GetPresentationAttribute(SourceReaderIndex.MediaSource,
                                                        MediaFoundationAttributes.MF_PD_DURATION);
                    //bug: still, depending on the decoder, this returns imprecise values.
                    return(NanoSecond100UnitsToBytes((long)(ulong)value.Value));
                }
                catch (Exception)
                {
                    return(0);
                }
            }
        }
Exemple #12
0
        SourceStream SetVideoMediaType(SourceReader sourceReader)
        {
            try
            {
                var sourceStream = sourceReader.Streams.FirstOrDefault(s => s.IsSelected && s.NativeMediaType.IsVideo);

                if (sourceStream.IsNull)
                {
                    throw new Exception("Unable to find video track within file.");
                }

                sourceStream.CurrentMediaType = new MediaType()
                {
                    MajorType = MFMediaType.Video, SubType = MFMediaType.RGB32
                };

                return(sourceStream);
            }
            catch (Exception e)
            {
                throw new Exception(string.Format("Unable to decode video stream. {0}", e.Message), e);
            }
        }
        private static ScannerResult ScanWhitespaceToken(SourceReader reader)
        {
            var thisReader  = reader;
            var sourceChar  = default(SourceChar);
            var sourceChars = new List <SourceChar>();

            // read the first whitespace character
            (sourceChar, thisReader) = thisReader.Read(ArmStringValidator.IsWhitespace);
            sourceChars.Add(sourceChar);
            // read the remaining whitespace
            while (!thisReader.Eof() && thisReader.Peek(ArmStringValidator.IsWhitespace))
            {
                (sourceChar, thisReader) = thisReader.Read();
                sourceChars.Add(sourceChar);
            }
            // return the result
            var extent = SourceExtent.From(sourceChars);
            var value  = extent.ToString();

            return(new ScannerResult(
                       new WhitespaceToken(extent, value), thisReader
                       ));
        }
            public static void EnumValueArrayWithQualifiedEnumValuesAndQuirksDisabledShouldThrow()
            {
                var sourceMof =
                    "instance of GOLF_Date\r\n" +
                    "{\r\n" +
                    "\tMonth = {MonthEnums.July};\r\n" +
                    "};";
                var tokens    = Lexing.Lexer.Lex(SourceReader.From(sourceMof));
                var tokensMof = TokenMofGenerator.ConvertToMof(tokens);
                var ex        = Assert.Throws <UnexpectedTokenException>(
                    () =>
                {
                    var astNodes = Parser.Parse(tokens);
                }
                    );

                Assert.AreEqual(
                    "Unexpected token found at Position 46, Line Number 3, Column Number 21.\r\n" +
                    "Token Type: 'DotOperatorToken'\r\n" +
                    "Token Text: '.'",
                    ex.Message
                    );
            }
        private void ReadHtml(TagHelperContent output, StringBuilder builder, SourceReader reader)
        {
            while (reader.Current != SourceReader.NullChar)
            {
                if (reader.Current == '$' && reader.IsNextNonWhiteSpace('{'))
                {//代码块
                    Queue(output, builder);
                    ReadInlineCode(output, reader);
                }

                if (reader.Current == '>')
                {//标签结束
                    builder.Append(reader.Current);
                    Queue(output, builder);
                    reader.Skip();
                    reader.MoveNext();
                    return;
                }

                builder.Append(reader.Current);
                reader.Skip();
            }
        }
Exemple #16
0
        public void Test(SourceReader sr)
        {
            Console.WriteLine("=============== Error Recovery Parser Combinators =======");

            SetUpScanner();
            var production = SetUpParser();

            ForkableScannerBuilder fsb = new ForkableScannerBuilder(m_scannerInfo);

            fsb.SetTriviaTokens(SPACE.Index);

            var scanner = fsb.Create(sr);

            var runner = new ParserRunner <int>(production);

            List <SyntaxError> errors = new List <SyntaxError>();
            var result = runner.Execute(scanner, errors);

            if (errors.Count == 0)
            {
                Console.WriteLine("Result: {0}", result);
            }
            else
            {
                Console.WriteLine("Parse Errors:");
                foreach (var err in errors)
                {
                    Console.WriteLine("{0}:{1} {2}",
                                      err.Code,
                                      err.Description,
                                      err.Location.StartLocation);
                }
            }

            Console.WriteLine();
            Console.WriteLine();
        }
        private void Process(TagHelperContent output, string source)
        {
            var reader  = new SourceReader(source);
            var builder = new StringBuilder();

            while (reader.MoveNext())
            {
                if (reader.Current == '<' && !reader.IsNext(' '))
                {//读取html标签
                    ReadHtml(output, builder, reader);
                    continue;
                }

                if (reader.Current == '$' && reader.IsNextNonWhiteSpace('{'))
                {//代码块
                    Queue(output, builder);
                    ReadInlineCode(output, reader);
                    continue;
                }

                if (reader.Current == '`')
                {//字符串
                    ReadQuote(output, builder, reader);
                    continue;
                }

                var code = reader.ReadUntil(new[] { '`', '<' }).Trim();
                foreach (var s in code.Split(new[] { "\n" }, StringSplitOptions.RemoveEmptyEntries))
                {
                    if (string.IsNullOrWhiteSpace(s))
                    {
                        continue;
                    }
                    output.AppendHtml(s.Trim());
                }
            }
        }
            public static void InvalidStructureFeatureShouldThrow()
            {
                var sourceMof =
                    "structure Sponsor\r\n" +
                    "{\r\n" +
                    "\t100\r\n" +
                    "};";
                var tokens    = Lexing.Lexer.Lex(SourceReader.From(sourceMof));
                var tokensMof = TokenMofGenerator.ConvertToMof(tokens);

                Assert.AreEqual(sourceMof, tokensMof);
                var ex = Assert.Throws <UnexpectedTokenException>(
                    () => {
                    var astNodes = Parser.Parse(tokens);
                }
                    );

                Assert.AreEqual(
                    "Unexpected token found at Position 23, Line Number 3, Column Number 2.\r\n" +
                    "Token Type: 'IntegerLiteralToken'\r\n" +
                    "Token Text: '100'",
                    ex.Message
                    );
            }
        public static void ParseMofFileInstances(string filename)
        {
            // read the text from the mof file
            var sourceText = File.ReadAllText(filename);

            // turn the text into a stream of characters for lexing
            var reader = SourceReader.From(sourceText);

            // lex the characters into a sequence of tokens
            var tokens = Lexer.Lex(reader);

            // parse the tokens into an ast tree
            var ast = Parser.Parse(tokens);

            // scan the ast for any "instance" definitions and convert them

            /*            var instances = ((MofSpecificationAst)ast).Productions
             *                                                    .Where(p => (p is InstanceValueDeclarationAst))
             *                                                    .Cast<InstanceValueDeclarationAst>()
             *                                                    .Select(Instance.FromAstNode)
             *                                                    .ToList();*/

            var classes = ((MofSpecificationAst)ast).Productions
                          .Where(p => (p is ClassDeclarationAst))
                          .Cast <ClassDeclarationAst>()
                          .ToList();

            foreach (var cc in classes)
            {
                System.Console.WriteLine(cc.ClassName);
            }


            // return the result
            //return instances.ToList();
        }
Exemple #20
0
        public static List <Instance> ParseMofFileInstances(string filename)
        {
            // read the text from the mof file
            var sourceText = File.ReadAllText(filename);

            // turn the text into a stream of characters for lexing
            var reader = SourceReader.From(sourceText);

            // lex the characters into a sequence of tokens
            var tokens = Lexer.Lex(reader);

            // parse the tokens into an ast tree
            var ast = Parser.Parse(tokens);

            // scan the ast for any "instance" definitions and convert them
            var instances = ((MofSpecificationAst)ast).Productions
                            .Where(p => (p is InstanceValueDeclarationAst))
                            .Cast <InstanceValueDeclarationAst>()
                            .Select(Instance.FromAstNode)
                            .ToList();

            // return the result
            return(instances.ToList());
        }
Exemple #21
0
        static void Main(string[] args)
        {
            BasicConfigurator.Configure();

            var directory = "Files";

            foreach (var filePath in Directory.EnumerateFiles(directory))
            {
                var descriptor = new SourceDescriptor(filePath);
                var reader = new SourceReader();
                var delay = 10;
                var inputSource = new StockQuotesStream(reader);
                var rocIndex = new RocIndex(delay);

                inputSource.Inputs.Subscribe(rocIndex);
                var pocket = new Pocket(inputSource.Inputs.Skip(delay), rocIndex.Values.Select(x => x > 0 ? true : false), 1000);

                decimal finalValue = 0;
                pocket.Money.Subscribe(Console.WriteLine);

                inputSource.Start(descriptor);
                Console.WriteLine(String.Format("{0}: {1}", Path.GetFileName(filePath), finalValue));
            }
        }
Exemple #22
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        /// <remarks>
        ///
        /// See https://www.dmtf.org/sites/default/files/standards/documents/DSP0221_3.0.1.pdf
        ///
        /// 7.7.1 Names
        ///
        /// MOF names are identifiers with the format defined by the IDENTIFIER rule.
        ///
        /// No whitespace is allowed between the elements of the rules in this ABNF section.
        ///
        ///     IDENTIFIER          = firstIdentifierChar *( nextIdentifierChar )
        ///     firstIdentifierChar = UPPERALPHA / LOWERALPHA / UNDERSCORE
        ///     nextIdentifierChar  = firstIdentifierChar / decimalDigit
        ///     elementName         = localName / schemaQualifiedName
        ///     localName           = IDENTIFIER
        ///
        /// </remarks>
        public static (IdentifierToken, Lexer) ReadIdentifierToken(SourceReader reader)
        {
            var thisReader  = reader;
            var sourceChar  = default(SourceChar);
            var sourceChars = new List <SourceChar>();
            var nameChars   = new StringBuilder();

            // firstIdentifierChar
            (sourceChar, thisReader) = thisReader.Read(StringValidator.IsFirstIdentifierChar);
            sourceChars.Add(sourceChar);
            nameChars.Append(sourceChar.Value);
            // *( nextIdentifierChar )
            while (!thisReader.Eof() && thisReader.Peek(StringValidator.IsNextIdentifierChar))
            {
                (sourceChar, thisReader) = thisReader.Read();
                sourceChars.Add(sourceChar);
                nameChars.Append(sourceChar.Value);
            }
            // return the result
            var extent = SourceExtent.From(sourceChars);
            var name   = nameChars.ToString();

            return(new IdentifierToken(extent, name), new Lexer(thisReader));
        }
Exemple #23
0
        public void Close()
        {
            logger.Debug("VideoCaptureSource::Close()");

            if (mediaSource != null)
            {
                //mediaSource?.Shutdown();

                mediaSource.Dispose();
                mediaSource = null;
            }

            if (sourceReader != null)
            {
                sourceReader.Dispose();
                sourceReader = null;
            }

            if (OutputMediaType != null)
            {
                OutputMediaType.Dispose();
                OutputMediaType = null;
            }
        }
		/// <summary>
		/// 使用指定原文件读取器和选项初始化 <see cref="RegexParser"/> 类的新实例。
		/// </summary>
		/// <param name="reader">正则表达式的源文件读取器。</param>
		/// <param name="option">正则表达式的选项。</param>
		/// <param name="regexDef">正则表达式的定义。</param>
		private RegexParser(SourceReader reader, RegexOptions option, IDictionary<string, Regex> regexDef)
			: this(option, regexDef)
		{
			this.pattern = null;
			this.reader = reader;
		}
		/// <summary>
		/// 根据给定的源文件解析正则表达式。
		/// </summary>
		/// <param name="reader">正则表达式的源文件读取器。</param>
		/// <param name="option">正则表达式的选项。</param>
		/// <param name="regexDef">正则表达式的定义。</param>
		/// <returns>解析得到的正则表达式。</returns>
		internal static Regex ParseRegex(SourceReader reader, RegexOptions option, IDictionary<string, Regex> regexDef)
		{
			RegexParser parser = new RegexParser(reader, option, regexDef);
			return parser.StartScanRegex();
		}
Exemple #26
0
        public void SetSource(SourceReader source)
        {
            CodeContract.RequiresArgumentNotNull(source, "source");
            m_source = source;

            Initialize();
        }
Exemple #27
0
        // 生成と終了


        public MediaFoundationFileVideoSource(VariablePath ファイルパス, double 再生速度 = 1.0)
        {
            using var _ = new LogBlock(Log.現在のメソッド名);

            this.再生速度 = Math.Max(0.01, Math.Min(10.0, 再生速度));

            #region " フレームキューを生成。"
            //----------------
            // キューサイズは3フレームとする。
            this._FrameQueue = new BlockingQueue <VideoFrame>(3);
            //----------------
            #endregion

            #region " ファイルから SourceReaderEx を生成する。"
            //----------------
            using (var ビデオ属性 = new MediaAttributes())
            {
                // DXVAに対応しているGPUの場合には、それをデコードに利用するよう指定する。
                ビデオ属性.Set(SourceReaderAttributeKeys.D3DManager, Global.MFDXGIDeviceManager);

                // 追加のビデオプロセッシングを有効にする。
                ビデオ属性.Set(SourceReaderAttributeKeys.EnableAdvancedVideoProcessing, true);    // 真偽値が bool だったり

                // 追加のビデオプロセッシングを有効にしたら、こちらは無効に。
                ビデオ属性.Set(SinkWriterAttributeKeys.ReadwriteDisableConverters, 0);             // int だったり

                // 属性を使って、SourceReaderEx を生成。
                using var sourceReader = new SourceReader(ファイルパス.数なしパス, ビデオ属性);       // パスは URI 扱い
                this._SourceReaderEx   = sourceReader.QueryInterface <SourceReaderEx>();
            }

            // 最初のビデオストリームだけを選択。
            this._SourceReaderEx.SetStreamSelection(SourceReaderIndex.AllStreams, false);
            this._SourceReaderEx.SetStreamSelection(SourceReaderIndex.FirstVideoStream, true);
            //----------------
            #endregion

            #region " ビデオの長さを取得する。"
            //----------------
            this.総演奏時間sec = (long)((this._SourceReaderEx.GetPresentationAttribute(SourceReaderIndex.MediaSource, PresentationDescriptionAttributeKeys.Duration) / this.再生速度) / 10_000_000.0);
            //----------------
            #endregion

            #region " デコーダを選択し、完全メディアタイプを取得する。"
            //----------------
            // 部分メディアタイプを設定する。
            using (var 部分MediaType = new MediaType())
            {
                // フォーマットは ARGB32 で固定とする。(SourceReaderEx を使わない場合、H264 では ARGB32 が選べないので注意。)
                部分MediaType.Set(MediaTypeAttributeKeys.MajorType, MediaTypeGuids.Video);
                部分MediaType.Set(MediaTypeAttributeKeys.Subtype, VideoFormatGuids.Argb32);

                // 部分メディアタイプを SourceReaderEx にセットする。SourceReaderEx は、必要なデコーダをロードするだろう。
                this._SourceReaderEx.SetCurrentMediaType(SourceReaderIndex.FirstVideoStream, 部分MediaType);
            }

            // 完成されたメディアタイプを取得する。
            this._MediaType = this._SourceReaderEx.GetCurrentMediaType(SourceReaderIndex.FirstVideoStream);
            //----------------
            #endregion

            #region " ビデオのフレームサイズを取得する。(動画の途中でのサイズ変更は考慮しない。)"
            //----------------
            long packedFrameSize = this._MediaType.Get(MediaTypeAttributeKeys.FrameSize);
            this.フレームサイズ = new Size2F((packedFrameSize >> 32) & 0xFFFFFFFF, (packedFrameSize) & 0xFFFFFFFF);
            //----------------
            #endregion

            this._デコードキャンセル  = new CancellationTokenSource();
            this._デコード起動完了通知 = new ManualResetEventSlim(false);
            this._一時停止解除通知   = new ManualResetEventSlim(true);
        }
Exemple #28
0
		/// <summary>
		/// 根据给定的源文件读取器解析正则表达式。
		/// </summary>
		/// <param name="reader">正则表达式的源文件读取器。</param>
		/// <param name="option">正则表达式的选项。</param>
		/// <param name="regexDef">正则表达式的定义。</param>
		/// <returns>解析得到的正则表达式。</returns>
		public static Regex Parse(SourceReader reader, RegexOptions option, IDictionary<string, Regex> regexDef)
		{
			return RegexParser.ParseRegex(reader, option, regexDef);
		}
Exemple #29
0
		/// <summary>
		/// 根据给定的源文件读取器解析正则表达式。
		/// </summary>
		/// <param name="reader">正则表达式的源文件读取器。</param>
		/// <returns>解析得到的正则表达式。</returns>
		public static Regex Parse(SourceReader reader)
		{
			return RegexParser.ParseRegex(reader, RegexOptions.None, null);
		}
            public static void ParsePropetyValueArrayWithLiteralStrings()
            {
                var tokens = Lexing.Lexer.Lex(
                    SourceReader.From(
                        "instance of myType as $Alias00000070\r\n" +
                        "{\r\n" +
                        "    ServerURLs = { \"https://URL1\", \"https://URL2\" };\r\n" +
                        "};"
                        )
                    );
                var actualAst   = Parser.Parse(tokens);
                var expectedAst = new MofSpecificationAst(
                    new ReadOnlyCollection <MofProductionAst>(
                        new List <MofProductionAst>
                {
                    new InstanceValueDeclarationAst(
                        new IdentifierToken(
                            new SourceExtent(
                                new SourcePosition(0, 1, 1),
                                new SourcePosition(7, 1, 8),
                                "instance"
                                ),
                            "instance"
                            ),
                        new IdentifierToken(
                            new SourceExtent(
                                new SourcePosition(9, 1, 10),
                                new SourcePosition(10, 1, 11),
                                "of"
                                ),
                            "of"
                            ),
                        new IdentifierToken(
                            new SourceExtent(
                                new SourcePosition(12, 1, 13),
                                new SourcePosition(17, 1, 18),
                                "myType"
                                ),
                            "myType"
                            ),
                        new IdentifierToken(
                            new SourceExtent(
                                new SourcePosition(19, 1, 20),
                                new SourcePosition(20, 1, 21),
                                "as"
                                ),
                            "as"
                            ),
                        new AliasIdentifierToken(
                            new SourceExtent(
                                new SourcePosition(22, 1, 23),
                                new SourcePosition(35, 1, 36),
                                "$Alias00000070"
                                ),
                            "Alias00000070"
                            ),
                        new PropertyValueListAst(
                            new ReadOnlyDictionary <string, PropertyValueAst>(
                                new Dictionary <string, PropertyValueAst> {
                        { "ServerURLs", new LiteralValueArrayAst(
                              new ReadOnlyCollection <LiteralValueAst>(
                                  new List <LiteralValueAst> {
                                new StringValueAst.Builder {
                                    StringLiteralValues = new List <StringLiteralToken> {
                                        new StringLiteralToken(
                                            new SourceExtent(
                                                new SourcePosition(60, 3, 20),
                                                new SourcePosition(73, 3, 33),
                                                "\"https://URL1\""
                                                ),
                                            "https://URL1"
                                            )
                                    },
                                    Value = "https://URL1"
                                }.Build(),
                                new StringValueAst.Builder {
                                    StringLiteralValues = new List <StringLiteralToken> {
                                        new StringLiteralToken(
                                            new SourceExtent(
                                                new SourcePosition(76, 3, 36),
                                                new SourcePosition(89, 3, 49),
                                                "\"https://URL2\""
                                                ),
                                            "https://URL2"
                                            )
                                    },
                                    Value = "https://URL2"
                                }.Build()
                            }
                                  )
                              ) }
                    }
                                )
                            ),
                        new StatementEndToken(
                            new SourceExtent(
                                new SourcePosition(96, 4, 2),
                                new SourcePosition(96, 4, 2),
                                ";"
                                )
                            )
                        )
                }
                        )
                    );
                var actualJson   = TestUtils.ConvertToJson(actualAst);
                var expectedJson = TestUtils.ConvertToJson(expectedAst);

                Assert.AreEqual(expectedJson, actualJson);
            }
Exemple #31
0
        public MediaFoundationFileVideoSource(VariablePath ファイルパス, double 再生速度 = 1.0)
        {
            this.再生速度 = Math.Max(0.01, Math.Min(10.0, 再生速度));

            #region " フレームキューを生成。"
            //----------------
            // キューサイズは3フレームとする。
            this._FrameQueue = new BlockingQueue <VideoFrame>(3);
            //----------------
            #endregion

            #region " ファイルから SourceReaderEx を生成する。"
            //----------------
            using (var ビデオ属性 = new MediaAttributes())
            {
                // DXVAに対応しているGPUの場合には、それをデコードに利用するよう指定する。
                ビデオ属性.Set(SourceReaderAttributeKeys.D3DManager, グラフィックデバイス.Instance.DXGIDeviceManager);

                // 追加のビデオプロセッシングを有効にする。
                ビデオ属性.Set(SourceReaderAttributeKeys.EnableAdvancedVideoProcessing, true);    // 真偽値が bool だったり

                // 追加のビデオプロセッシングを有効にしたら、こちらは無効に。
                ビデオ属性.Set(SinkWriterAttributeKeys.ReadwriteDisableConverters, 0);             // int だったり

                // 属性を使って、SourceReaderEx を生成。
                using (var sourceReader = new SourceReader(ファイルパス.数なしパス, ビデオ属性))        // パスは URI 扱い
                    this._SourceReaderEx = sourceReader.QueryInterface <SourceReaderEx>();
            }

            // 最初のビデオストリームだけを選択。
            this._SourceReaderEx.SetStreamSelection(SourceReaderIndex.AllStreams, false);
            this._SourceReaderEx.SetStreamSelection(SourceReaderIndex.FirstVideoStream, true);
            //----------------
            #endregion

            #region " ビデオの長さを取得する。"
            //----------------
            this.総演奏時間sec =
                FDKUtilities.換_100ns単位からsec単位へ(
                    this._SourceReaderEx.GetPresentationAttribute(SourceReaderIndex.MediaSource, PresentationDescriptionAttributeKeys.Duration)) /
                this.再生速度;
            //----------------
            #endregion

            #region " デコーダを選択し、完全メディアタイプを取得する。"
            //----------------
            // 部分メディアタイプを設定する。
            using (var videoMediaType = new MediaType())
            {
                // フォーマットは ARGB32 で固定とする。(SourceReaderEx を使わない場合、H264 では ARGB32 が選べないので注意。)
                videoMediaType.Set(MediaTypeAttributeKeys.MajorType, MediaTypeGuids.Video);
                videoMediaType.Set(MediaTypeAttributeKeys.Subtype, VideoFormatGuids.Argb32);

                // 部分メディアタイプを SourceReaderEx にセットする。SourceReaderEx は、必要なデコーダをロードするだろう。
                this._SourceReaderEx.SetCurrentMediaType(SourceReaderIndex.FirstVideoStream, videoMediaType);
            }

            // 完成されたメディアタイプを取得する。
            this._MediaType = this._SourceReaderEx.GetCurrentMediaType(SourceReaderIndex.FirstVideoStream);
            //----------------
            #endregion

            #region " ビデオのフレームサイズを取得する。"
            //----------------
            long packedFrameSize = this._MediaType.Get(MediaTypeAttributeKeys.FrameSize);   // 動画の途中でのサイズ変更には対応しない。
            this.フレームサイズ = new Size2F((packedFrameSize >> 32) & 0xFFFFFFFF, (packedFrameSize) & 0xFFFFFFFF);
            //----------------
            #endregion
        }
Exemple #32
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        /// <remarks>
        ///
        /// See https://www.dmtf.org/sites/default/files/standards/documents/DSP0221_3.0.1.pdf
        ///
        /// 7.6.1.1 Integer value
        ///
        /// No whitespace is allowed between the elements of the rules in this ABNF section.
        ///
        ///     integerValue         = binaryValue / octalValue / hexValue / decimalValue
        ///
        ///     binaryValue          = [ "+" / "-" ] 1*binaryDigit ( "b" / "B" )
        ///     binaryDigit          = "0" / "1"
        ///
        ///     octalValue           = [ "+" / "-" ] unsignedOctalValue
        ///     unsignedOctalValue   = "0" 1*octalDigit
        ///     octalDigit           = "0" / "1" / "2" / "3" / "4" / "5" / "6" / "7"
        ///
        ///     hexValue             = [ "+" / "-" ] ( "0x" / "0X" ) 1*hexDigit
        ///     hexDigit             = decimalDigit / "a" / "A" / "b" / "B" / "c" / "C" /
        ///                                           "d" / "D" / "e" / "E" / "f" / "F"
        ///
        ///     decimalValue         = [ "+" / "-" ] unsignedDecimalValue
        ///     unsignedDecimalValue = "0" / positiveDecimalDigit *decimalDigit
        ///
        ///     decimalDigit         = "0" / positiveDecimalDigit
        ///     positiveDecimalDigit = "1"..."9"
        ///
        /// 7.6.1.2 Real value
        ///
        /// No whitespace is allowed between the elements of the rules in this ABNF section.
        ///
        ///     realValue            = [ "+" / "-" ] *decimalDigit "." 1*decimalDigit
        ///                            [ ( "e" / "E" ) [ "+" / "-" ] 1*decimalDigit ]
        ///
        ///     decimalDigit         = "0" / positiveDecimalDigit
        ///     positiveDecimalDigit = "1"..."9"
        ///
        /// </remarks>
        public static (Token, Lexer) ReadNumericLiteralToken(SourceReader reader)
        {
            int ParseBinaryValueDigits(IEnumerable <SourceChar> binaryChars, SourceChar sign)
            {
                return(ParseIntegerValueDigits(new Dictionary <char, int>
                {
                    { '0', 0 }, { '1', 1 }
                }, 2, binaryChars, sign));
            }

            int ParseOctalValueDigits(IEnumerable <SourceChar> octalChars, SourceChar sign)
            {
                return(ParseIntegerValueDigits(new Dictionary <char, int>
                {
                    { '0', 0 }, { '1', 1 }, { '2', 2 }, { '3', 3 }, { '4', 4 }, { '5', 5 }, { '6', 6 }, { '7', 7 }
                }, 8, octalChars, sign));
            }

            int ParseHexValueDigits(IEnumerable <SourceChar> hexChars, SourceChar sign)
            {
                return(ParseIntegerValueDigits(new Dictionary <char, int>
                {
                    { '0', 0 }, { '1', 1 }, { '2', 2 }, { '3', 3 }, { '4', 4 }, { '5', 5 }, { '6', 6 }, { '7', 7 }, { '8', 8 }, { '9', 9 },
                    { 'a', 10 }, { 'b', 11 }, { 'c', 12 }, { 'd', 13 }, { 'e', 14 }, { 'f', 15 },
                    { 'A', 10 }, { 'B', 11 }, { 'C', 12 }, { 'D', 13 }, { 'E', 14 }, { 'F', 15 }
                }, 16, hexChars, sign));
            }

            int ParseDecimalValueDigits(IEnumerable <SourceChar> decimalChars, SourceChar sign)
            {
                return(ParseIntegerValueDigits(new Dictionary <char, int>
                {
                    { '0', 0 }, { '1', 1 }, { '2', 2 }, { '3', 3 }, { '4', 4 }, { '5', 5 }, { '6', 6 }, { '7', 7 }, { '8', 8 }, { '9', 9 }
                }, 10, decimalChars, sign));
            }

            int ParseIntegerValueDigits(Dictionary <char, int> alphabet, int radix, IEnumerable <SourceChar> chars, SourceChar sign)
            {
                var literalValue = 0;

                foreach (var digit in chars)
                {
                    var digitValue = alphabet[digit.Value];
                    literalValue = (literalValue * radix) + digitValue;
                }
                if (sign?.Value == '-')
                {
                    literalValue = -literalValue;
                }
                return(literalValue);
            }

            const int stateLeadingSign         = 1;
            const int stateFirstDigitBlock     = 2;
            const int stateOctalOrDecimalValue = 3;
            const int stateBinaryValue         = 4;
            const int stateOctalValue          = 5;
            const int stateHexValue            = 6;
            const int stateDecimalValue        = 7;
            const int stateRealValue           = 8;
            const int stateRealValueFraction   = 9;
            const int stateRealValueExponent   = 10;
            const int stateDone = 99;

            var thisReader  = reader;
            var sourceChar  = default(SourceChar);
            var sourceChars = new List <SourceChar>();

            var token           = default(Token);
            var signChar        = default(SourceChar);
            var firstDigitBlock = new List <SourceChar>();

            var currentState = stateLeadingSign;

            while (currentState != stateDone)
            {
                switch (currentState)
                {
                case stateLeadingSign:
                    // we're reading the initial optional leading sign
                    // [ "+" / "-" ]
                    sourceChar = thisReader.Peek();
                    switch (sourceChar.Value)
                    {
                    case '+':
                    case '-':
                        (signChar, thisReader) = thisReader.Read();
                        sourceChars.Add(signChar);
                        break;
                    }
                    currentState = stateFirstDigitBlock;
                    break;

                case stateFirstDigitBlock:
                    // we're reading the first block of digits in the value,
                    // but we won't necessarily know which type we're reading
                    // until we've consumed more of the input stream
                    //
                    //     binaryValue  => 1*binaryDigit
                    //     octalValue   => "0" 1*octalDigit
                    //     hexValue     => ( "0x" / "0X" )
                    //     decimalValue => positiveDecimalDigit *decimalDigit
                    //     realValue    => *decimalDigit
                    //
                    if (thisReader.Peek('.'))
                    {
                        // we're reading a realValue with no "*decimalDigit" characters before the "."
                        // e.g. ".45", "+.45", "-.45", so consume the decimal point
                        (sourceChar, thisReader) = thisReader.Read();
                        sourceChars.Add(sourceChar);
                        // and go to the next state
                        currentState = stateRealValueFraction;
                        break;
                    }
                    // we don't know which base the value is in yet, but if it's hexadecimal them
                    // we should be reading the "0x" part here, so restrict digits to decimal in
                    // all cases
                    (firstDigitBlock, thisReader) = thisReader.ReadWhile(StringValidator.IsDecimalDigit);
                    sourceChars.AddRange(firstDigitBlock);
                    // now we can do some validation
                    if (firstDigitBlock.Count == 0)
                    {
                        // only realValue allows no digits in the first block, and
                        // we've already handled that at the start of this case
                        throw new UnexpectedCharacterException(sourceChar);
                    }
                    // if we've reached the end of the stream then there's no suffix
                    // (e.g. b, B, x, X, .) so this must be an octalValue or decimalValue
                    if (thisReader.Eof())
                    {
                        currentState = stateOctalOrDecimalValue;
                        break;
                    }
                    // check the next character to see if it tells us anything
                    // about which type of literal we're reading
                    sourceChar = thisReader.Peek();
                    switch (sourceChar.Value)
                    {
                    case 'b':
                    case 'B':
                        // binaryValue
                        currentState = stateBinaryValue;
                        break;

                    case 'x':
                    case 'X':
                        // hexValue
                        currentState = stateHexValue;
                        break;

                    case '.':
                        // realValue
                        currentState = stateRealValue;
                        break;

                    default:
                        // by elmination, this must be an octalValue or decimalValue
                        currentState = stateOctalOrDecimalValue;
                        break;
                    }
                    break;

                case stateOctalOrDecimalValue:
                    // we're reading an octalValue or decimalValue, but we're not sure which yet...
                    if ((firstDigitBlock.First().Value == '0') && (firstDigitBlock.Count > 1))
                    {
                        currentState = stateOctalValue;
                    }
                    else
                    {
                        currentState = stateDecimalValue;
                    }
                    break;

                case stateBinaryValue:
                    // we're trying to read a binaryValue, so check all the characters in the digit block are valid,
                    // i.e. 1*binaryDigit
                    if (firstDigitBlock.Any(c => !StringValidator.IsBinaryDigit(c.Value)))
                    {
                        throw new UnexpectedCharacterException(sourceChar);
                    }
                    // all the characters are valid, so consume the suffix
                    (sourceChar, thisReader) = thisReader.Read(c => (c == 'b') || (c == 'B'));
                    sourceChars.Add(sourceChar);
                    // now build the return value
                    var binaryValue = ParseBinaryValueDigits(firstDigitBlock, signChar);
                    token = new IntegerLiteralToken(SourceExtent.From(sourceChars), IntegerKind.BinaryValue, binaryValue);
                    // and we're done
                    currentState = stateDone;
                    break;

                case stateOctalValue:
                    // we're trying to read an octalValue (since decimalValue can't start with a
                    // leading '0') so check all the characters in the digit block are valid,
                    // i.e. "0" 1*octalDigit
                    if ((firstDigitBlock.Count < 2) || (firstDigitBlock.First().Value != '0'))
                    {
                        throw new UnexpectedCharacterException(sourceChar);
                    }
                    if (firstDigitBlock.Skip(1).Any(c => !StringValidator.IsOctalDigit(c.Value)))
                    {
                        throw new UnexpectedCharacterException(sourceChar);
                    }
                    // now build the return value
                    var octalValue = ParseOctalValueDigits(firstDigitBlock, signChar);
                    token = new IntegerLiteralToken(SourceExtent.From(sourceChars), IntegerKind.OctalValue, octalValue);
                    // and we're done
                    currentState = stateDone;
                    break;

                case stateHexValue:
                    // we're trying to read a hexValue, so we should have just read a leading zero
                    if ((firstDigitBlock.Count != 1) || (firstDigitBlock.First().Value != '0'))
                    {
                        throw new UnexpectedCharacterException(sourceChar);
                    }
                    // all the characters are valid, so consume the suffix
                    (sourceChar, thisReader) = thisReader.Read(c => (c == 'x') || (c == 'X'));
                    sourceChars.Add(sourceChar);
                    // 1*hexDigit
                    var hexDigits = default(List <SourceChar>);
                    (hexDigits, thisReader) = thisReader.ReadWhile(StringValidator.IsHexDigit);
                    if (hexDigits.Count == 0)
                    {
                        throw new UnexpectedCharacterException(thisReader.Peek());
                    }
                    sourceChars.AddRange(hexDigits);
                    // build the return value
                    var hexValue = ParseHexValueDigits(hexDigits, signChar);
                    token = new IntegerLiteralToken(SourceExtent.From(sourceChars), IntegerKind.HexValue, hexValue);
                    // and we're done
                    currentState = stateDone;
                    break;

                case stateDecimalValue:
                    // we're trying to read a decimalValue (since that's the only remaining option),
                    // so check all the characters in the digit block are valid,
                    // i.e. "0" / positiveDecimalDigit *decimalDigit
                    if ((firstDigitBlock.Count == 1) && (firstDigitBlock.First().Value == '0'))
                    {
                        // "0"
                    }
                    else if (!StringValidator.IsPositiveDecimalDigit(firstDigitBlock.First().Value))
                    {
                        throw new UnexpectedCharacterException(sourceChar);
                    }
                    else if (firstDigitBlock.Skip(1).Any(c => !StringValidator.IsDecimalDigit(c.Value)))
                    {
                        throw new UnexpectedCharacterException(sourceChar);
                    }
                    // build the return value
                    var decimalValue = ParseDecimalValueDigits(firstDigitBlock, signChar);
                    token = new IntegerLiteralToken(SourceExtent.From(sourceChars), IntegerKind.DecimalValue, decimalValue);
                    // and we're done
                    currentState = stateDone;
                    break;

                case stateRealValue:
                    // we're trying to read a realValue, so check all the characters in the digit block are valid,
                    // i.e. *decimalDigit
                    if (firstDigitBlock.Any(c => !StringValidator.IsDecimalDigit(c.Value)))
                    {
                        throw new UnexpectedCharacterException(sourceChar);
                    }
                    // all the characters are valid, so consume the decimal point
                    (sourceChar, thisReader) = thisReader.Read('.');
                    sourceChars.Add(sourceChar);
                    // and go to the next state
                    currentState = stateRealValueFraction;
                    break;

                case stateRealValueFraction:
                    // 1*decimalDigit
                    var realFractionDigits = default(List <SourceChar>);
                    (realFractionDigits, thisReader) = thisReader.ReadWhile(StringValidator.IsHexDigit);
                    if (realFractionDigits.Count == 0)
                    {
                        throw new UnexpectedCharacterException(thisReader.Peek());
                    }
                    sourceChars.AddRange(realFractionDigits);
                    // ( "e" / "E" )
                    if (!thisReader.Eof())
                    {
                        sourceChar = thisReader.Peek();
                        if ((sourceChar.Value == 'e') || (sourceChar.Value == 'E'))
                        {
                            currentState = stateRealValueExponent;
                            break;
                        }
                    }
                    // build the return value
                    var realIntegerValue  = ParseDecimalValueDigits(firstDigitBlock, signChar);
                    var realFractionValue = (double)ParseDecimalValueDigits(realFractionDigits, signChar);
                    if (realFractionDigits.Any())
                    {
                        realFractionValue = realFractionValue / Math.Pow(10, realFractionDigits.Count);
                    }
                    token = new RealLiteralToken(
                        SourceExtent.From(sourceChars),
                        realIntegerValue + realFractionValue
                        );
                    // and we're done
                    currentState = stateDone;
                    break;

                case stateRealValueExponent:
                    throw new InvalidOperationException();

                case stateDone:
                    // the main while loop should exit before we ever get here
                    throw new InvalidOperationException();

                default:
                    throw new InvalidOperationException();
                }
            }

            return(token, new Lexer(thisReader));
        }
Exemple #33
0
 public SourceStream(SourceReader sourceReader, int index)
 {
     this.sourceReader = sourceReader;
     this.index = index;
 }
Exemple #34
0
        private void Initialize(SourceReader reader)
        {
            // Invalidate selection for all streams
            reader.SetStreamSelection(SourceReaderIndex.AllStreams, false);

            // Select only audio stream
            reader.SetStreamSelection(SourceReaderIndex.FirstAudioStream, true);

            // Get the media type for the current stream.
            using (var mediaType = reader.GetNativeMediaType(SourceReaderIndex.FirstAudioStream, 0))
            {
                var majorType = mediaType.Get(MediaTypeAttributeKeys.MajorType);
                if (majorType != MediaTypeGuids.Audio)
                    throw new InvalidOperationException("Input stream doesn't contain an audio stream.");
            }

            // Set the type on the source reader to use PCM
            using (var partialType = new MediaType())
            {
                partialType.Set(MediaTypeAttributeKeys.MajorType, MediaTypeGuids.Audio);
                partialType.Set(MediaTypeAttributeKeys.Subtype, AudioFormatGuids.Pcm);
                reader.SetCurrentMediaType(SourceReaderIndex.FirstAudioStream, partialType);
            }

            // Retrieve back the real media type
            using (var realMediaType = reader.GetCurrentMediaType(SourceReaderIndex.FirstAudioStream))
            {
                int sizeRef;
                WaveFormat = realMediaType.ExtracttWaveFormat(out sizeRef);
            }

            Duration = new TimeSpan(reader.GetPresentationAttribute(SourceReaderIndex.MediaSource, PresentationDescriptionAttributeKeys.Duration));
        }
Exemple #35
0
        protected override void Dispose(bool disposeManagedResources)
        {
            base.Dispose(disposeManagedResources);

            CleanupAndDispose();

            if (nextSourceReader != null)
            {
                nextSourceReader.Dispose();
                nextSourceReader = null;
            }

            if (sourceReader != null)
            {
                sourceReader.Dispose();
                sourceReader = null;
            }
        }
Exemple #36
0
        /// <summary>
        /// Gets the decoded PCM samples. See remarks.
        /// </summary>
        /// <param name="startingPositionInSeconds">The starting position in seconds.</param>
        /// <returns>An enumerator of pointer to PCM decoded data with the same format as returned by <see cref="WaveFormat"/>.</returns>
        /// <remarks>
        /// This method is only working as a single enumerator at a time.
        /// The <see cref="SetSourceStream(System.IO.Stream)"/> must be set before calling <see cref="GetSamples()"/>
        /// </remarks>
        public IEnumerable<DataPointer> GetSamples(TimeSpan startingPositionInSeconds)
        {
            // A new reader is setup, so initialize it.
            lock (sourceReaderLock)
            {
                // If the reader was changed
                if (nextSourceReader != null)
                {
                    if (sourceReader != null)
                        sourceReader.Dispose();

                    sourceReader = nextSourceReader;
                    nextSourceReader = null;
                }
            }

            // Make sure that any prior call 
            CleanupAndDispose();

            CheckIfDisposed();

            // Set the position
            sourceReader.SetCurrentPosition((long)(startingPositionInSeconds.TotalSeconds * 1e7));

            while (true)
            {
                int streamIndex;
                SourceReaderFlags flags;
                long time;

                CheckIfDisposed();

                using (currentSample = sourceReader.ReadSample(SourceReaderIndex.FirstAudioStream, SourceReaderControlFlags.None, out streamIndex, out flags, out time))
                {
                    if ((flags & SourceReaderFlags.Endofstream) != 0)
                        break;

                    CheckIfDisposed();

                    using (currentBuffer = currentSample.ConvertToContiguousBuffer())
                    {
                        int bufferMaxLength;
                        int bufferCurrentLength;

                        CheckIfDisposed();
                        
                        var ptr = currentBuffer.Lock(out bufferMaxLength, out bufferCurrentLength);

                        yield return new DataPointer(ptr, bufferCurrentLength);

                        // Warning, because the yield could never return here, currentBuffer and currentSample should be disposed when disposing this object or when
                        // calling it again on the GetSamples method.

                        // In case a Dispose occured while decoding
                        if (currentBuffer == null)
                            break;

                        currentBuffer.Unlock();
                    }
                }
            }

            // They have been disposed, so we can just clear them
            currentBuffer = null;
            currentSample = null;
        }
Exemple #37
0
        public void SetSource(SourceReader source)
        {
            if (m_lookAheadQueue.Count > 0)
            {
                throw new InvalidOperationException("The source is not allowed to be set when the look ahead queue not empty");
            }

            m_masterScanner.SetSource(source);
        }
		public void CanReadFromStream()
		{
			var sourceReader = new SourceReader(_csv, Encoding.UTF8, 1);
			Assert.Equal(11, sourceReader.ReadFromStream().Count());
		}
        public List <SyntaxToken> Lex(string sourceText)
        {
            var reader = SourceReader.From(sourceText);

            return(this.ReadToEnd(reader).ToList());
        }
Exemple #40
0
        /// <summary>
        /// Called by the ctor to configure the media playback component.
        /// </summary>
        private void InitializeMediaPipeline()
        {
            MediaManager.Startup(false);
            MediaAttributes sourceReaderAttributes = new MediaAttributes();

            sourceReaderAttributes.Set(SourceReaderAttributeKeys.EnableAdvancedVideoProcessing, true);
            this.sourceReader = new SourceReader(this.filename, sourceReaderAttributes);
            this.sourceReader.SetStreamSelection(SourceReaderIndex.AllStreams, false);

            int  streamIndex     = 0;
            bool doneEnumerating = false;

            while (!doneEnumerating)
            {
                try
                {
                    MediaType mediaType = this.sourceReader.GetCurrentMediaType(streamIndex);
                    var       subType   = mediaType.Get(MediaTypeAttributeKeys.Subtype);
                    DumpMediaType(mediaType);

                    if (mediaType.MajorType == MediaTypeGuids.Video && this.imageStreamIndex == -1)
                    {
                        this.imageStreamIndex = streamIndex;

                        // get the image size
                        long frameSize = mediaType.Get(MediaTypeAttributeKeys.FrameSize);
                        this.videoHeight = (short)frameSize;
                        this.videoWidth  = (short)(frameSize >> 32);

                        // enable the stream and set the current media type
                        this.sourceReader.SetStreamSelection(this.imageStreamIndex, true);
                        mediaType = new MediaType();
                        mediaType.Set(MediaTypeAttributeKeys.MajorType, MediaTypeGuids.Video);
                        mediaType.Set(MediaTypeAttributeKeys.Subtype, VideoFormatGuids.Rgb24);
                        mediaType.Set(MediaTypeAttributeKeys.FrameSize, frameSize);
                        this.sourceReader.SetCurrentMediaType(this.imageStreamIndex, mediaType);
                    }
                    else if (mediaType.MajorType == MediaTypeGuids.Audio && this.audioStreamIndex == -1)
                    {
                        this.audioStreamIndex = streamIndex;

                        // enable the stream and set the current media type to PCM
                        this.sourceReader.SetStreamSelection(this.audioStreamIndex, true);
                        mediaType = new MediaType();
                        mediaType.Set(MediaTypeAttributeKeys.MajorType, MediaTypeGuids.Audio);
                        mediaType.Set(MediaTypeAttributeKeys.Subtype, AudioFormatGuids.Pcm);
                        this.sourceReader.SetCurrentMediaType(this.audioStreamIndex, mediaType);

                        // get back all the media type details
                        mediaType = this.sourceReader.GetCurrentMediaType(this.audioStreamIndex);
                        int numberOfChannels = mediaType.Get(MediaTypeAttributeKeys.AudioNumChannels);
                        int sampleRate       = mediaType.Get(MediaTypeAttributeKeys.AudioSamplesPerSecond);
                        int bitsPerSample    = mediaType.Get(MediaTypeAttributeKeys.AudioBitsPerSample);

                        // post our output audio format
                        this.waveFormat = WaveFormat.CreatePcm(sampleRate, bitsPerSample, numberOfChannels);
                    }
                }
                catch (Exception e)
                {
                    Debug.Write(e.GetType());

                    // expected thrown exception
                    // unfortunately no way to tell how many streams other than trying
                    doneEnumerating = true;
                }

                streamIndex += 1;
            }
        }
Exemple #41
0
        /// <summary>
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        /// <remarks>
        ///
        /// See https://www.dmtf.org/sites/default/files/standards/documents/DSP0221_3.0.1.pdf
        ///
        /// 7.6.1.3 String values
        ///
        /// Unless explicitly specified via ABNF rule WS, no whitespace is allowed between the elements of the rules
        /// in this ABNF section.
        ///
        ///     singleStringValue = DOUBLEQUOTE *stringChar DOUBLEQUOTE
        ///     stringValue       = singleStringValue *( *WS singleStringValue )
        ///
        ///     stringChar        = stringUCSchar / stringEscapeSequence
        ///     stringUCSchar     = U+0020...U+0021 / U+0023...U+D7FF /
        ///                         U+E000...U+FFFD / U+10000...U+10FFFF
        ///                         ; Note that these UCS characters can be
        ///                         ; represented in XML without any escaping
        ///                         ; (see W3C XML).
        ///
        ///     stringEscapeSequence = BACKSLASH ( BACKSLASH / DOUBLEQUOTE / SINGLEQUOTE /
        ///                            BACKSPACE_ESC / TAB_ESC / LINEFEED_ESC /
        ///                            FORMFEED_ESC / CARRIAGERETURN_ESC /
        ///                            escapedUCSchar )
        ///
        ///     BACKSPACE_ESC      = "b" ; escape for back space (U+0008)
        ///     TAB_ESC            = "t" ; escape for horizontal tab(U+0009)
        ///     LINEFEED_ESC       = "n" ; escape for line feed(U+000A)
        ///     FORMFEED_ESC       = "f" ; escape for form feed(U+000C)
        ///     CARRIAGERETURN_ESC = "r" ; escape for carriage return (U+000D)
        ///
        ///     escapedUCSchar     = ( "x" / "X" ) 1*6(hexDigit ) ; escaped UCS
        ///                          ; character with a UCS code position that is
        ///                          ; the numeric value of the hex number
        ///
        /// The following special characters are also used in other ABNF rules in this specification:
        ///
        ///     BACKSLASH   = U+005C ; \
        ///     DOUBLEQUOTE = U+0022 ; "
        ///     SINGLEQUOTE = U+0027 ; '
        ///     UPPERALPHA  = U+0041...U+005A ; A ... Z
        ///     LOWERALPHA  = U+0061...U+007A ; a ... z
        ///     UNDERSCORE  = U+005F ; _
        ///
        /// </remarks>
        public static (StringLiteralToken, Lexer) ReadStringLiteralToken(SourceReader reader)
        {
            // BUGBUG - no support for *( *WS DOUBLEQUOTE *stringChar DOUBLEQUOTE )
            // BUGBUG - incomplete escape sequences
            // BUGBUG - no support for UCS characters
            var thisReader  = reader;
            var sourceChar  = default(SourceChar);
            var sourceChars = new List <SourceChar>();
            var stringChars = new StringBuilder();

            // read the first double-quote character
            (sourceChar, thisReader) = thisReader.Read(Constants.DOUBLEQUOTE);
            sourceChars.Add(sourceChar);
            // read the remaining characters
            bool isEscaped    = false;
            bool isTerminated = false;

            while (!thisReader.Eof())
            {
                var peek = thisReader.Peek();
                if (isEscaped)
                {
                    // read the second character in an escape sequence
                    var escapedChar = default(char);
                    switch (peek.Value)
                    {
                    case Constants.BACKSLASH:
                        escapedChar = Constants.BACKSLASH;
                        break;

                    case Constants.DOUBLEQUOTE:
                        escapedChar = Constants.DOUBLEQUOTE;
                        break;

                    case Constants.SINGLEQUOTE:
                        escapedChar = Constants.SINGLEQUOTE;
                        break;

                    case Constants.BACKSPACE_ESC:
                        escapedChar = '\b';
                        break;

                    case Constants.TAB_ESC:
                        escapedChar = '\t';
                        break;

                    case Constants.LINEFEED_ESC:
                        escapedChar = '\n';
                        break;

                    case Constants.FORMFEED_ESC:
                        escapedChar = '\f';
                        break;

                    case Constants.CARRIAGERETURN_ESC:
                        escapedChar = '\r';
                        break;

                    default:
                        throw new UnexpectedCharacterException(peek);
                    }
                    thisReader = thisReader.Next();
                    sourceChars.Add(peek);
                    stringChars.Append(escapedChar);
                    isEscaped = false;
                }
                else if (peek.Value == Constants.BACKSLASH)
                {
                    // read the first character in an escape sequence
                    thisReader = thisReader.Next();
                    sourceChars.Add(peek);
                    isEscaped = true;
                }
                else if (peek.Value == Constants.DOUBLEQUOTE)
                {
                    // read the last double-quote character
                    (_, thisReader) = thisReader.Read(Constants.DOUBLEQUOTE);
                    sourceChars.Add(peek);
                    isTerminated = true;
                    break;
                }
                else
                {
                    // read a literal string character
                    thisReader = thisReader.Next();
                    sourceChars.Add(peek);
                    stringChars.Append(peek.Value);
                }
            }
            // make sure we found the end of the string
            if (!isTerminated)
            {
                throw new InvalidOperationException("Unterminated string found.");
            }
            // return the result
            var extent      = SourceExtent.From(sourceChars);
            var stringValue = stringChars.ToString();

            return(new StringLiteralToken(extent, stringValue), new Lexer(thisReader));
        }
Exemple #42
0
 /// <summary>
 /// 使用要扫描的源文件初始化 <see cref="TokenReader{T}"/> 类的新实例。
 /// </summary>
 /// <param name="reader">要使用的源文件读取器。</param>
 /// <exception cref="ArgumentNullException"><paramref name="reader"/> 为 <c>null</c>。</exception>
 protected TokenReader(SourceReader reader)
 {
     CommonExceptions.CheckArgumentNull(reader, "reader");
     Contract.EndContractBlock();
     this.Source = reader;
 }
            public static void ParsePropetyValueArrayWithAliasIdentifier()
            {
                var tokens = Lexing.Lexer.Lex(
                    SourceReader.From(
                        "instance of myType as $Alias00000070\r\n" +
                        "{\r\n" +
                        "    Reference = {$Alias0000006E};\r\n" +
                        "};"
                        )
                    );
                var actualAst   = Parser.Parse(tokens);
                var expectedAst = new MofSpecificationAst(
                    new ReadOnlyCollection <MofProductionAst>(
                        new List <MofProductionAst>
                {
                    new InstanceValueDeclarationAst(
                        new IdentifierToken(
                            new SourceExtent(
                                new SourcePosition(0, 1, 1),
                                new SourcePosition(7, 1, 8),
                                "instance"
                                ),
                            "instance"
                            ),
                        new IdentifierToken(
                            new SourceExtent(
                                new SourcePosition(9, 1, 10),
                                new SourcePosition(10, 1, 11),
                                "of"
                                ),
                            "of"
                            ),
                        new IdentifierToken(
                            new SourceExtent(
                                new SourcePosition(12, 1, 13),
                                new SourcePosition(17, 1, 18),
                                "myType"
                                ),
                            "myType"
                            ),
                        new IdentifierToken(
                            new SourceExtent(
                                new SourcePosition(19, 1, 20),
                                new SourcePosition(20, 1, 21),
                                "as"
                                ),
                            "as"
                            ),
                        new AliasIdentifierToken(
                            new SourceExtent(
                                new SourcePosition(22, 1, 23),
                                new SourcePosition(35, 1, 36),
                                "$Alias00000070"
                                ),
                            "Alias00000070"
                            ),
                        new PropertyValueListAst(
                            new ReadOnlyDictionary <string, PropertyValueAst>(
                                new Dictionary <string, PropertyValueAst> {
                        { "Reference", new ComplexValueArrayAst(
                              new ReadOnlyCollection <ComplexValueAst>(
                                  new List <ComplexValueAst> {
                                new ComplexValueAst(
                                    new AliasIdentifierToken(
                                        new SourceExtent(
                                            new SourcePosition(58, 3, 18),
                                            new SourcePosition(71, 3, 31),
                                            "$Alias0000006E"
                                            ),
                                        "Alias0000006E"
                                        )
                                    )
                            }
                                  )
                              ) }
                    }
                                )
                            ),
                        new StatementEndToken(
                            new SourceExtent(
                                new SourcePosition(77, 4, 2),
                                new SourcePosition(77, 4, 2),
                                ";"
                                )
                            )
                        )
                }
                        )
                    );
                var actualJson   = TestUtils.ConvertToJson(actualAst);
                var expectedJson = TestUtils.ConvertToJson(expectedAst);

                Assert.AreEqual(expectedJson, actualJson);
            }
Exemple #44
0
        public T Parse(SourceReader source)
        {
            CodeContract.RequiresArgumentNotNull(source, "source");

            if (!m_isInitialized)
            {
                OnInitialize();
            }

            Scanner scanner = m_scanner;

            scanner.SetSource(source);

            ParserEngine engine = new ParserEngine(m_transitionTable, m_errorDefinition);

            Lexeme r = scanner.Read();

            while (true)
            {
                try
                {
                    engine.Input(r);
                }
                catch (PanicRecoverException prex)
                {
                    var follow = prex.PossibleFollow;

                    HashSet <int> validTokens = new HashSet <int>(follow.Select(p =>
                    {
                        Terminal t = p as Terminal;
                        if (t != null)
                        {
                            return(t.Token.Index);
                        }
                        else
                        {
                            return(m_scannerInfo.EndOfStreamTokenIndex);
                        }
                    }));

                    while (!validTokens.Contains(r.TokenIndex) && !r.IsEndOfStream)
                    {
                        r = scanner.Read();
                    }

                    continue;
                }

                if (r.IsEndOfStream)
                {
                    break;
                }

                r = scanner.Read();
            }

            if (engine.AcceptedCount == 0)
            {
                throw new ParsingFailureException("There's no parsing result");
            }

            if (engine.AcceptedCount > 1 && engine.GetResultInfo(0).ErrorCount == 0)
            {
                throw new ParsingFailureException("Multiple parsing results are found. There's ambiguity in your grammar");
            }

            object result = engine.GetResult(0, m_errorManager);

            return((T)result);
        }
 public static void ParsePropetyValueArrayWithNumericLiteralValues()
 {
     var tokens = Lexing.Lexer.Lex(
         SourceReader.From(
             "instance of myType as $Alias00000070\r\n" +
             "{\r\n" +
             "    MyBinaryValue = 0101010b;\r\n" +
             "    MyOctalValue = 0444444;\r\n" +
             "    MyHexValue = 0xABC123;\r\n" +
             "    MyDecimalValue = 12345;\r\n" +
             "    MyRealValue = 123.45;\r\n" +
             "};"
             )
         );
     var actualAst   = Parser.Parse(tokens);
     var expectedAst = new MofSpecificationAst(
         new ReadOnlyCollection <MofProductionAst>(
             new List <MofProductionAst>
     {
         new InstanceValueDeclarationAst(
             new IdentifierToken(
                 new SourceExtent(
                     new SourcePosition(0, 1, 1),
                     new SourcePosition(7, 1, 8),
                     "instance"
                     ),
                 "instance"
                 ),
             new IdentifierToken(
                 new SourceExtent(
                     new SourcePosition(9, 1, 10),
                     new SourcePosition(10, 1, 11),
                     "of"
                     ),
                 "of"
                 ),
             new IdentifierToken(
                 new SourceExtent(
                     new SourcePosition(12, 1, 13),
                     new SourcePosition(17, 1, 18),
                     "myType"
                     ),
                 "myType"
                 ),
             new IdentifierToken(
                 new SourceExtent(
                     new SourcePosition(19, 1, 20),
                     new SourcePosition(20, 1, 21),
                     "as"
                     ),
                 "as"
                 ),
             new AliasIdentifierToken(
                 new SourceExtent(
                     new SourcePosition(22, 1, 23),
                     new SourcePosition(35, 1, 36),
                     "$Alias00000070"
                     ),
                 "Alias00000070"
                 ),
             new PropertyValueListAst(
                 new ReadOnlyDictionary <string, PropertyValueAst>(
                     new Dictionary <string, PropertyValueAst> {
             { "MyBinaryValue", new IntegerValueAst(
                   new IntegerLiteralToken(
                       new SourceExtent(
                           new SourcePosition(61, 3, 21),
                           new SourcePosition(68, 3, 28),
                           "0101010b"
                           ),
                       IntegerKind.BinaryValue, 0b101010
                       )
                   ) },
Exemple #46
0
        public void Setup(object pars)
        {
            logger.Debug("VideoCaptureSource::Setup()");

            if (State != CaptureState.Closed)
            {
                throw new InvalidOperationException("Invalid capture state " + State);
            }

            UvcDevice captureParams = pars as UvcDevice;

            var deviceId = captureParams.DeviceId;

            try
            {
                int adapterIndex = 0;
                using (var dxgiFactory = new SharpDX.DXGI.Factory1())
                {
                    using (var adapter = dxgiFactory.GetAdapter1(adapterIndex))
                    {
                        var deviceCreationFlags = //DeviceCreationFlags.Debug |
                                                  DeviceCreationFlags.VideoSupport |
                                                  DeviceCreationFlags.BgraSupport;

                        device = new Device(adapter, deviceCreationFlags);

                        using (var multiThread = device.QueryInterface <SharpDX.Direct3D11.Multithread>())
                        {
                            multiThread.SetMultithreadProtected(true);
                        }
                    }
                }

                sourceReader = CreateSourceReaderByDeviceId(deviceId);

                if (sourceReader == null)
                {
                    throw new Exception("Unable to create media source reader " + deviceId);
                }

                var mediaType = sourceReader.GetCurrentMediaType(SourceReaderIndex.FirstVideoStream);

                logger.Debug("------------------CurrentMediaType-------------------");
                logger.Debug(MfTool.LogMediaType(mediaType));

                srcSize = MfTool.GetFrameSize(mediaType);

                var destSize = captureParams.Resolution;

                if (destSize.IsEmpty)
                {
                    destSize = srcSize;
                }

                var subtype = mediaType.Get(MediaTypeAttributeKeys.Subtype);


                mediaType?.Dispose();


                SharedTexture = new Texture2D(device,
                                              new Texture2DDescription
                {
                    CpuAccessFlags    = CpuAccessFlags.None,
                    BindFlags         = BindFlags.RenderTarget | BindFlags.ShaderResource,
                    Format            = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                    Width             = destSize.Width,
                    Height            = destSize.Height,
                    MipLevels         = 1,
                    ArraySize         = 1,
                    SampleDescription = { Count = 1, Quality = 0 },
                    Usage             = ResourceUsage.Default,
                    OptionFlags       = ResourceOptionFlags.Shared,
                });

                stagingTexture = new Texture2D(device,
                                               new Texture2DDescription
                {
                    CpuAccessFlags = CpuAccessFlags.Read,
                    //BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource,
                    BindFlags         = BindFlags.None,
                    Format            = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                    Width             = destSize.Width,
                    Height            = destSize.Height,
                    MipLevels         = 1,
                    ArraySize         = 1,
                    SampleDescription = { Count = 1, Quality = 0 },
                    Usage             = ResourceUsage.Staging,
                    OptionFlags       = ResourceOptionFlags.None,
                });


                processor = new MfVideoProcessor(null);
                var intupArgs = new MfVideoArgs
                {
                    Width  = srcSize.Width,
                    Height = srcSize.Height,
                    Format = subtype,//VideoFormatGuids.NV12,
                };


                var outputArgs = new MfVideoArgs
                {
                    Width  = destSize.Width,
                    Height = destSize.Height,
                    Format = VideoFormatGuids.Argb32,
                };

                processor.Setup(intupArgs, outputArgs);
                processor.SetMirror(VideoProcessorMirror.MirrorVertical);

                state = CaptureState.Initialized;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                LastError = ex;
                errorCode = (int)SharedTypes.ErrorCode.NotInitialized;

                CleanUp();

                throw;
            }
        }
Exemple #47
0
		/// <summary>
		/// 根据给定的源文件读取器解析正则表达式。
		/// </summary>
		/// <param name="reader">正则表达式的源文件读取器。</param>
		/// <param name="option">正则表达式的选项。</param>
		/// <returns>解析得到的正则表达式。</returns>
		public static Regex Parse(SourceReader reader, RegexOptions option)
		{
			return RegexParser.ParseRegex(reader, option, null);
		}
 public Scanner CreateStringLexer(string source)
 {
     SourceReader reader = new SourceReader(new System.IO.StringReader(source));
     return new Scanner(reader, errors);
 }
Exemple #49
0
        public BuildDriver(Log log, BuildTarget target, BuildOptions options, Project project, UnoConfig config)
            : base(log)
        {
            _target           = target;
            _options          = options;
            _project          = project;
            _config           = config;
            Log.MaxErrorCount = options.MaxErrorCount;
            Log.WarningLevel  = options.WarningLevel;

            Log.Reset();
            if (target.IsObsolete)
            {
                Log.Warning("The build target " + target.Quote() + " is obsolete and might not work any more.");
            }

            _anim = Log.StartAnimation("Configuring");
            PrintRow("Project file", project.FullPath);

            _compilerOptions = new CompilerOptions {
                BuildTarget     = _target.Identifier,
                Configuration   = _options.Configuration.ToString(),
                OutputDirectory = !string.IsNullOrEmpty(_options.OutputDirectory)
                    ? Path.GetFullPath(_options.OutputDirectory)
                    : project.OutputDirectory,
                MainClass     = _options.MainClass,
                Debug         = _options.Configuration != BuildConfiguration.Release,
                Parallel      = _options.Parallel,
                Strip         = _options.Strip ?? _target.DefaultStrip,
                CanCacheIL    = _options.PackageCache != null,
                OptimizeLevel = _options.OptimizeLevel
            };

            if (_options.Test)
            {
                _options.Defines.Add("UNO_TEST");
                _compilerOptions.TestOptions = new TestOptions {
                    TestServerUrl = _options.TestServerUrl,
                    Filter        = _options.TestFilter
                };
            }

            _cache = _options.PackageCache ?? new PackageCache(Log, _config);
            PrintRow("Search paths", _cache.SearchPaths);

            _compiler = new Compiler(
                Log,
                _target.CreateBackend(config),
                GetPackage(),
                _compilerOptions);
            _env     = _compiler.Environment;
            _backend = _compiler.Backend;
            _input   = _compiler.Input;

            if (_options.Clean)
            {
                _compiler.Disk.DeleteDirectory(_env.OutputDirectory);
            }

            _file = new BuildFile(_env.OutputDirectory);
        }
Exemple #50
0
        public void SourceCodeTest()
        {
            string       code   = @"class ABCDEFG
{
    public int c;
}";
            StringReader sr     = new StringReader(code);
            SourceReader source = new SourceReader(sr);

            Assert.AreEqual('c', (char)source.PeekChar());
            Assert.AreEqual('c', (char)source.ReadChar());
            Assert.AreEqual(0, source.Location.CharIndex);

            //create a revert point
            var rp1 = source.CreateRevertPoint();

            Assert.AreEqual('l', (char)source.PeekChar());
            Assert.AreEqual('l', (char)source.ReadChar());
            Assert.AreEqual('a', (char)source.ReadChar());
            Assert.AreEqual(2, source.Location.CharIndex);

            //revert
            source.Revert(rp1);
            Assert.AreEqual('l', (char)source.PeekChar());
            Assert.AreEqual('l', (char)source.ReadChar());
            Assert.AreEqual('a', (char)source.ReadChar());
            Assert.AreEqual(2, source.Location.CharIndex);
            Assert.AreEqual('s', (char)source.ReadChar());
            Assert.AreEqual(3, source.Location.CharIndex);

            source.Revert(rp1);
            source.RemoveRevertPoint(rp1);
            Assert.AreEqual('l', (char)source.ReadChar());
            Assert.AreEqual('a', (char)source.ReadChar());
            Assert.AreEqual(2, source.Location.CharIndex);
            Assert.AreEqual('s', (char)source.ReadChar());
            Assert.AreEqual(3, source.Location.CharIndex);
            Assert.AreEqual('s', (char)source.ReadChar());

            Assert.Catch <ArgumentException>(() => source.Revert(rp1));

            //peek and then revert
            Assert.AreEqual(' ', (char)source.PeekChar());
            var rp2 = source.CreateRevertPoint();

            Assert.AreEqual(' ', (char)source.PeekChar());
            Assert.AreEqual(' ', (char)source.ReadChar());
            Assert.AreEqual('A', (char)source.ReadChar());

            source.Revert(rp2);
            Assert.AreEqual(' ', (char)source.PeekChar());
            Assert.AreEqual(' ', (char)source.ReadChar());
            Assert.AreEqual('A', (char)source.ReadChar());

            //multiple revert point
            var rp3 = source.CreateRevertPoint();

            Assert.AreEqual('B', (char)source.ReadChar());
            Assert.AreEqual('C', (char)source.ReadChar());
            Assert.AreEqual('D', (char)source.ReadChar());
            Assert.AreEqual('E', (char)source.PeekChar());

            source.Revert(rp2);
            Assert.AreEqual(' ', (char)source.PeekChar());
            Assert.AreEqual(' ', (char)source.ReadChar());
            Assert.AreEqual('A', (char)source.ReadChar());

            source.Revert(rp3);
            Assert.AreEqual('B', (char)source.ReadChar());
            Assert.AreEqual('C', (char)source.ReadChar());
            Assert.AreEqual('D', (char)source.ReadChar());
            Assert.AreEqual('E', (char)source.PeekChar());

            source.Revert(rp2);
            Assert.AreEqual(' ', (char)source.PeekChar());
            Assert.AreEqual(' ', (char)source.ReadChar());
            Assert.AreEqual('A', (char)source.ReadChar());

            source.RemoveRevertPoint(rp2);
            source.RemoveRevertPoint(rp3);

            Assert.AreEqual('B', (char)source.ReadChar());
            Assert.AreEqual('C', (char)source.ReadChar());
            Assert.AreEqual('D', (char)source.ReadChar());
            Assert.AreEqual('E', (char)source.ReadChar());
            Assert.AreEqual('F', (char)source.PeekChar());
        }
        private bool CheckForSegmentSkip(long offsetV, int readerIndex, SourceReader reader)
        {
            var isSkipSegment = reader.Duration + offsetV < startPosition;

            if (isSkipSegment)
            {
                Trace.WriteLine(string.Format("File index: {0}, Skipping entire file. Duration: {1}, offset: {2}, startPosition: {3}",
                    readerIndex,
                    reader.Duration.FromNanoToSeconds(),
                    offsetV.FromNanoToSeconds(),
                    startPosition.FromNanoToSeconds()
                ));

            }

            return isSkipSegment;
        }
		public void CanReadUnQuotedValueFromStream()
		{
			var sourceReader = new SourceReader(_csv, Encoding.UTF8, 7);
			var arraySegment = sourceReader.ReadFromStream().Skip(8).First().Values[0];
			Assert.Equal("95111", new string(arraySegment.Array, arraySegment.Offset, arraySegment.Count));
		}
Exemple #53
0
 public TomlParser(string text)
 {
     this.reader = new SourceReader(text);
 }
		/// <summary>
		/// 使用指定模式字符串和选项初始化 <see cref="RegexParser"/> 类的新实例。
		/// </summary>
		/// <param name="pattern">正则表达式的模式字符串。</param>
		/// <param name="option">正则表达式的选项。</param>
		/// <param name="regexDef">正则表达式的定义。</param>
		private RegexParser(string pattern, RegexOptions option, IDictionary<string, Regex> regexDef)
			: this(option, regexDef)
		{
			this.pattern = pattern;
			this.reader = new SourceReader(new StringReader(pattern));
		}
Exemple #55
0
        public void CompactCharSetTest()
        {
            Lexicon    lexicon  = new Lexicon();
            LexerState global   = lexicon.DefaultLexer;
            LexerState keywords = global.CreateSubState();
            LexerState xml      = keywords.CreateSubState();

            var lettersCategories = new[] { UnicodeCategory.LetterNumber,
                                            UnicodeCategory.LowercaseLetter,
                                            UnicodeCategory.ModifierLetter,
                                            UnicodeCategory.OtherLetter,
                                            UnicodeCategory.TitlecaseLetter,
                                            UnicodeCategory.UppercaseLetter };

            var RE_IDCHAR = RE.CharsOf(c => lettersCategories.Contains(Char.GetUnicodeCategory(c)));


            var ID = global.DefineToken(RE_IDCHAR.Concat(
                                            (RE_IDCHAR | RE.Range('0', '9')).Many()));
            var NUM        = global.DefineToken(RE.Range('0', '9').Many1());
            var WHITESPACE = global.DefineToken(RE.Symbol(' ').Many());

            var IF   = keywords.DefineToken(RE.Literal("if"));
            var ELSE = keywords.DefineToken(RE.Literal("else"));

            var XMLNS = xml.DefineToken(RE.Literal("xmlns"));

            var scannerInfo = lexicon.CreateScannerInfo();

            scannerInfo.LexerStateIndex = xml.Index;

            Scanner s = new Scanner(scannerInfo);

            string source = "xmlns 你好吗1 123 蘏臦囧綗 ABCD if";

            SourceReader sr = new SourceReader(new StringReader(source));

            s.SetSource(sr);
            s.SetTriviaTokens(WHITESPACE.Index);

            var l1 = s.Read();

            Assert.AreEqual(XMLNS.Index, l1.TokenIndex);

            var l2 = s.Read();

            Assert.AreEqual(ID.Index, l2.TokenIndex);

            var l3 = s.Read();

            Assert.AreEqual(NUM.Index, l3.TokenIndex);

            var l4 = s.Read();

            Assert.AreEqual(ID.Index, l4.TokenIndex);

            var l5 = s.Read();

            Assert.AreEqual(ID.Index, l5.TokenIndex);

            var l6 = s.Read();

            Assert.AreEqual(IF.Index, l6.TokenIndex);
        }
        public override void UpdateItems(List <DataCompareItem> items, IDataSynchronizationStatus status)
        {
            if (items != null && items.Count > 0)
            {
                int currentItem = 0;

                foreach (var item in items)
                {
                    if (!status.ContinueProcessing)
                    {
                        break;
                    }

                    try
                    {
                        var itemInvariant = new DataCompareItemInvariant(item);
                        var filename      = itemInvariant.GetTargetIdentifier <string>();

                        Automation?.BeforeUpdateItem(this, itemInvariant, filename);

                        if (itemInvariant.Sync)
                        {
                            #region Update Item

                            //Get the Target Item Data
                            Dictionary <string, object> targetItem = UpdateItemToDictionary(Mapping, itemInvariant);

                            string tmpFile = System.IO.Path.Combine(Utility.GetTempPath(), $"{Guid.NewGuid()}.tmp");

                            try
                            {
                                System.IO.File.WriteAllBytes(tmpFile, SourceReader.GetBlobData(itemInvariant.ToDataCompareItem(), 0));
                                if (targetItem.TryGetValue("DateModified", out object value))
                                {
                                    System.IO.File.SetLastWriteTimeUtc(tmpFile, DataSchemaTypeConverter.ConvertTo <DateTime>(value).ToUniversalTime());
                                }

                                Session.PutFiles(tmpFile, filename, false, new TransferOptions {
                                    OverwriteMode = OverwriteMode.Overwrite, PreserveTimestamp = true, TransferMode = TransferMode.Binary
                                }).Check();
                            }
                            catch (Exception e)
                            {
                                Automation?.ErrorItem(this, itemInvariant, filename, e);
                                throw;
                            }
                            finally
                            {
                                if (System.IO.File.Exists(tmpFile))
                                {
                                    System.IO.File.Delete(tmpFile);
                                }
                            }

                            Automation?.AfterUpdateItem(this, itemInvariant, filename);
                            #endregion
                        }

                        ClearSyncStatus(item); //Clear the Sync Flag on Processed Rows
                    }
                    catch (SystemException e)
                    {
                        HandleError(status, e);
                    }
                    finally
                    {
                        status.Progress(items.Count, ++currentItem); //Update the Sync Progress
                    }
                }
            }
        }