Ejemplo n.º 1
0
        // 读入文本转换为CSV
        static void ParseRaw(IEnumerable <char> raw)
        {
            // 初始化
            table    = new List <List <CSVNode> >();
            tableRow = new List <CSVNode>();
            currNode = "";
            status   = ParserStatus.RowStart;

            // 迭代字符
            int i = 0;

            foreach (char c in raw)
            {
                if (!ParseChar(c))
                {
                    throw new InvalidCastException(String.Format("字符#{0}: {1}", i, errorMsg));
                }
                i++;
            }

            // 是否读入最后一行
            switch (status)
            {
            case ParserStatus.InQuote:
                throw new InvalidCastException("引号内容未结束");

            case ParserStatus.RowStart:
                break;

            default:
                AddRow();
                break;
            }
        }
Ejemplo n.º 2
0
        /// <inheritdoc/>
        public override ParseError ParseNextChar(char c)
        {
            if (c == '}' && _depth == 0)
            {
                ParserStatus status = _childParser.Finalize();
                if (status.Failed)
                {
                    return(new ParseError(status));
                }

                _node.AddChild(_childParser.Tree.Root);
                Output = _node;
                return(new ParseError());
            }
            else
            {
                if (c == '{')
                {
                    _depth++;
                }
                else if (c == '}')
                {
                    _depth--;
                }

                ParserStatus status = _childParser.ParseNextChar(c);
                return(new ParseError(status));
            }
        }
Ejemplo n.º 3
0
        protected string Run(string input)
        {
            ExpTree      tree;
            ParserStatus result = DefaultParser.Parse(input, out tree);

            Assert.IsFalse(result.Failed);
            return(tree.Root.Execute(_simplifier).Print(_printer));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Parses a complete string and returns the resulting tree and status.
        /// </summary>
        /// <param name="equation">The equation to parse.</param>
        /// <param name="tree">The resulting tree.</param>
        /// <returns>The resulting parsing status.</returns>
        public static ParserStatus Parse(string equation, out ExpTree tree)
        {
            DefaultParser parser = new DefaultParser();
            ParserStatus  result = parser.ParseString(equation);

            // Errors are automatically handled like this
            tree = parser.Tree;
            return(result);
        }
Ejemplo n.º 5
0
        /// <summary>Parses a complete .map file</summary>
        /// <param name="filename">Filename of the map to parse</param>
        /// <returns>The minimum version of EntityPlus that's required to run this map</returns>
        private void ParseMap(string filename)
        {
            Debug("Reading map file... ", false);
            StreamReader sr = File.OpenText(filename);

            ParserStatus parserStatus = ParserStatus.Normal;
            string       line         = "";
            Entity       ent          = null;

            while (sr.Peek() > 0)
            {
                line = sr.ReadLine();
                if (line.IndexOf("// entity") >= 0)
                {
                    parserStatus  = ParserStatus.EntityTokenFound;
                    ent           = new Entity();
                    ent.EntityNum = int.Parse(line.Replace("// entity", ""));
                }
                else if (line.IndexOf("// brush") >= 0)
                {
                    parserStatus = ParserStatus.ReadingBrush;
                }
                else if (line.IndexOf("patchDef2") >= 0)
                {
                    parserStatus = ParserStatus.ReadingPatch;
                }
                else if (line == "{" && parserStatus == ParserStatus.EntityTokenFound)
                {
                    parserStatus = ParserStatus.EntityStartTokenFound;
                }
                else if (line != "}" && parserStatus == ParserStatus.EntityStartTokenFound)
                {
                    ParseKeyValue(line, ent);
                }
                else if (line == "}" && parserStatus == ParserStatus.EntityStartTokenFound)
                {
                    parserStatus = ParserStatus.Normal;
                    entities.Add(ent);
                }
                else if (line == "}" && parserStatus == ParserStatus.ReadingBrush)
                {
                    parserStatus = ParserStatus.EntityStartTokenFound;
                }
                else if (line == "}" && parserStatus == ParserStatus.ReadingPatch)
                {
                    parserStatus = ParserStatus.ReadingBrush;
                }
            }

            sr.Close();

            DateTime end = DateTime.Now;

            Debug("Done.");
        }
Ejemplo n.º 6
0
        private ParseError FinalizeChild()
        {
            ParserStatus status = _childParser.Finalize();
            if (status.Failed)
            {
                return new ParseError(status);
            }

            _node.AddChild(_childParser.Tree.Root);
            return new ParseError();
        }
Ejemplo n.º 7
0
        public override void ReceiveChunk(byte[] buffer, int offset, int length)
        {
            if (lastState == ParserStatus.EOF)
            {
                throw new EndOfStreamException();
            }
            if (offset + length >= buffer.Length)
            {
                throw new IndexOutOfRangeException();
            }
            if (lastState == ParserStatus.MOVE_NEXT_SAME_BUFFER)
            {
                // We have unprocess data in previous buffer.
                parser.BufferOffset = StartPtr;
                lastState           = parser.ProcessBuffer(connection.ConnectionBuffer, HttpConnection.BUFFER_SIZE, length);
                StartPtr            = parser.BufferOffset;

                if (lastState == ParserStatus.MOVE_NEXT)
                {
                    byte[] result = parser.Content;
                    Array.Copy(result, 0, buffer, offset, result.Length);
                    parser.ResetParser();
                }
            }
            else
            {
                lastState = ParserStatus.REQUIRE_MORE;
                int size = 0;
                while (lastState == ParserStatus.REQUIRE_MORE)
                {
                    size      = connection.ActiveSocket.Receive(connection.ConnectionBuffer);
                    lastState = parser.ProcessBuffer(connection.ConnectionBuffer, size, length);
                }
                if (lastState.HasFlag(ParserStatus.MOVE_NEXT) || lastState == ParserStatus.EOF)
                {
                    Array.Copy(parser.Content, 0, buffer, offset, length);
                    if (lastState == ParserStatus.EOF)
                    {
                        // Put connection back to normal listening mode
                        connection.ActiveSocket.BeginReceive(
                            connection.ConnectionBuffer, 0, HttpConnection.BUFFER_SIZE, 0,
                            new AsyncCallback(connection.ConnectionHandler.ReadCallback),
                            connection);
                    }
                }
                else if (lastState == ParserStatus.ERROR)
                {
                    connection.Response.SendError(System.Net.HttpStatusCode.NotImplemented);
                    throw new InvalidDataException();
                }

                parser.ResetParser();
            }
        }
Ejemplo n.º 8
0
        public void ITC23HardCodeParser()
        {
            // Test hard coded grammar
            // Status is for collection of errors
            ParserStatus status         = new ParserStatus(null);
            Parser       hardcodeParser = MetaParser.GetHardCodeParser(status);
            string       actual         = hardcodeParser.GetGrammar();
            string       expect         = GetExpectHardCodeGrammar();
            string       msg            = Util.CompareTextLines(actual, expect);

            Assert.AreEqual(string.Empty, msg, "Hardcode grammar diff error");
        }
Ejemplo n.º 9
0
        private ParseError ParseAsChild(char c)
        {
            if (c == '{' || c == '<')
            {
                _depth++;
            }
            else if (c == '}' || c == '>')
            {
                _depth--;
            }

            ParserStatus status = _childParser.ParseNextChar(c);
            return new ParseError(status);
        }
Ejemplo n.º 10
0
        // 状态转移
        static bool TransStatus(ParserStatus newStatus)
        {
            switch (newStatus)
            {
            case ParserStatus.CellStart:
                AddCell();
                break;

            // 换行
            case ParserStatus.RowStart:
                AddRow();
                break;
            }
            status = newStatus;
            return(true);
        }
Ejemplo n.º 11
0
        public override void ReceiveChunkAsync(byte[] buffer, int offset, int length, Action <ReadResult> callback, object stateObject)
        {
            if (lastState == ParserStatus.EOF)
            {
                throw new EndOfStreamException();
            }
            if (offset + length >= buffer.Length)
            {
                throw new IndexOutOfRangeException();
            }
            if (lastState == ParserStatus.MOVE_NEXT_SAME_BUFFER)
            {
                // We have unprocess data in previous buffer.
                parser.BufferOffset = StartPtr;
                lastState           = parser.ProcessBuffer(connection.ConnectionBuffer, HttpConnection.BUFFER_SIZE, length);
                StartPtr            = parser.BufferOffset;

                if (lastState == ParserStatus.MOVE_NEXT || lastState == ParserStatus.EOF)
                {
                    byte[] result = parser.Content;
                    Array.Copy(result, 0, buffer, offset, result.Length);
                    callback.Invoke(new ReadResult(buffer, result.Length, stateObject));
                    parser.ResetParser();
                }
                if (lastState == ParserStatus.EOF)
                {
                    // Put connection back to normal listening mode
                    connection.ActiveSocket.BeginReceive(
                        connection.ConnectionBuffer, 0, HttpConnection.BUFFER_SIZE, 0,
                        new AsyncCallback(connection.ConnectionHandler.ReadCallback),
                        connection);
                }
            }
            else if (lastState == ParserStatus.ERROR)
            {
                connection.Response.SendError(System.Net.HttpStatusCode.NotImplemented);
                callback.Invoke(new ReadResult(buffer, -1, stateObject));
            }
            else
            {
                connection.ActiveSocket.BeginReceive(
                    connection.ConnectionBuffer, 0, HttpConnection.BUFFER_SIZE, 0,
                    new AsyncCallback(AsyncCallback),
                    new AsyncState(connection, stateObject, buffer, length, offset, callback)
                    );
            }
        }
Ejemplo n.º 12
0
        public static string ToAppStateName(this ParserStatus status)
        {
            switch (status)
            {
            case ParserStatus.Ready:
                return("Не запущен");

            case ParserStatus.Parsing:
                return("Парсинг");

            case ParserStatus.Done:
                return("Завершил работу");

            default:
                return("Не определен");
            }
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Parses a complete string and returns the resulting status.
        /// </summary>
        /// <param name="expression">The expression to parse.</param>
        /// <returns>The resulting parsing status.</returns>
        public ParserStatus ParseString(string expression)
        {
            _input = expression;

            // Parse each character one at a time
            foreach (char c in expression)
            {
                ParserStatus status = ParseNextChar(c, true);
                if (status.Failed)
                {
                    return(status);
                }
            }

            // Finalize after all characters are parsed.
            Finalize();

            return(new ParserStatus(_input, _position));
        }
Ejemplo n.º 14
0
        /// <inheritdoc/>
        public override ParseError ParseNextChar(char c)
        {
            if ((c == ',' || c == '>') && _depth == 0)
            {
                ParserStatus error = _childParser.Finalize();
                if (error.Failed)
                {
                    return(new ParseError(error));
                }

                ExpTree tree = _childParser.Tree;
                _childParser = new DefaultParser();
                if (tree == null)
                {
                    return(new ParseError(ErrorType.UNKNOWN));
                }

                _children.Add(tree.Root);
                if (c == '>')
                {
                    Output = new TensorNode(new int[] { _children.Count }, _children);
                }

                return(new ParseError());
            }
            else
            {
                if (c == '<')
                {
                    _depth++;
                }
                else if (c == '>')
                {
                    _depth--;
                }

                ParserStatus result = _childParser.ParseNextChar(c);
                return(new ParseError(result));
            }
        }
Ejemplo n.º 15
0
        private void AsyncCallback(IAsyncResult result)
        {
            AsyncState state = (AsyncState)result.AsyncState;
            int        size  = state.Connection.ActiveSocket.EndReceive(result);

            lastState = parser.ProcessBuffer(
                state.BufferPtr,
                size,
                state.DesireSize);
            if (lastState == ParserStatus.REQUIRE_MORE)
            {
                connection.ActiveSocket.BeginReceive(
                    connection.ConnectionBuffer, 0, HttpConnection.BUFFER_SIZE, 0,
                    new AsyncCallback(AsyncCallback),
                    state
                    );
            }
            else if (lastState == ParserStatus.ERROR)
            {
                connection.Response.SendError(System.Net.HttpStatusCode.NotImplemented);
                state.Callback.Invoke(
                    new ReadResult(state.BufferPtr, -1, state.Appendix));
            }
            else if (lastState == ParserStatus.EOF)
            {
                // Put connection back to normal listening mode
                connection.ActiveSocket.BeginReceive(
                    connection.ConnectionBuffer, 0, HttpConnection.BUFFER_SIZE, 0,
                    new AsyncCallback(connection.ConnectionHandler.ReadCallback),
                    connection);
            }
            else
            {
                byte[] res = parser.Content;
                Array.Copy(res, 0, state.BufferPtr, state.Offset, res.Length);
                state.Callback.Invoke(new ReadResult(state.BufferPtr, res.Length, state.Appendix));
                parser.ResetParser();
            }
        }
Ejemplo n.º 16
0
        void Header()
        {
            string region_1 =
                "GET / HTTP/1.1\r\n" +
                "Host: blog.lunaixsky.com\r\n" +
                "Connection: keep-alive\r\n" +
                "Cache-Control: max-age=0\r\n" +
                "Upgrade-Insecure-Requests: 1\r\n" +
                "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36";

            string region_2 = "\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\r\n" +
                              "Sec-Fetch-Site: none\r\n" +
                              "Sec-Fetch-Mode: navigate\r\nSec-Fetch-User: ?1\r\nSec-Fetch-Dest: document\r\n" +
                              "Accept-Encoding: gzip, deflate, br\r\n" +
                              "Accept-Language: en-GB,en;q=0.9,zh-CN;q=0.8,zh;q=0.7,ru-RU;q=0.6,ru;q=0.5,en-US;q=0.4\r\n" +
                              "Cookie: _ga=GA1.2.1727535589.1597313386; _gid=GA1.2.1552299765.1598851724; _gat_gtag_UA_175290221_1=1\r\n\r\n" +
                              "<html>some html tags</html>";

            byte[] r1 = Encoding.ASCII.GetBytes(region_1);
            byte[] r2 = Encoding.ASCII.GetBytes(region_2);

            HeaderParser headerParser = new HeaderParser();
            ParserStatus status1      = headerParser.ProcessBuffer(r1, r1.Length);

            Console.WriteLine("Region 1 : {0}", status1);
            if (status1 == ParserStatus.REQUIRE_MORE)
            {
                status1 = headerParser.ProcessBuffer(r2, r2.Length);
                Console.WriteLine("Region 2 : {0}", status1);
            }

            if (status1 == ParserStatus.MOVE_NEXT_NEW_BUFFER || status1 == ParserStatus.MOVE_NEXT_SAME_BUFFER)
            {
                Console.WriteLine("Next State start pointer={0}", headerParser.BufferOffset);
                Console.WriteLine("Hint: {0}", region_2[headerParser.BufferOffset]);
                Console.WriteLine(headerParser.Content);
            }
        }
Ejemplo n.º 17
0
        /// <summary>Override this to set a property from grammar.</summary>
        /// <param name="property">CodeElement with property name.</param>
        /// <param name="value">Value string.</param>
        /// <param name="status">If error add to this.</param>
        /// <returns>True: property set. False: not set.</returns>
        public override bool SetProperty(CodeElement property, string value, ParserStatus status)
        {
            if (property.Value == nameof(Precedence))
            {
                int val;
                if (int.TryParse(value, out val) && (val > 0))
                {
                    Precedence = val;
                    return(true);
                }
                else
                {
                    status.AddBuildError(() => MessageRes.itc33, property, property.Value);
                }
            }
            if (property.Value == nameof(RightAssociative))
            {
                RightAssociative = value != "false";
                return(true);
            }

            return(false);
        }
Ejemplo n.º 18
0
        static void Main(string[] args)
        {
            while (true)
            {
                SysCon.Write("Enter an Equation (q to quit): ");
                string str = SysCon.ReadLine();

                if (str == "q")
                {
                    SysCon.Write("Done!");
                    return;
                }

                DefaultParser  parser  = new DefaultParser();
                DefaultPrinter printer = new DefaultPrinter();
                ParserStatus   status  = parser.ParseString(str);
                if (status.Failed)
                {
                    SysCon.WriteLine($"\nError: {status.ErrorType}\n");
                }
                else
                {
                    ExpNode    root       = parser.Tree.Root;
                    Simplifier simplifier = new Simplifier();
                    root = root.Execute(simplifier);

                    if (root == null)
                    {
                        SysCon.WriteLine($"\n{simplifier.Error.Message}\n");
                    }
                    else
                    {
                        SysCon.WriteLine($"\n{root.Print(printer)}\n");
                    }
                }
            }
        }
Ejemplo n.º 19
0
 internal ChunkedRequestStream(HttpConnection connection, ParserStatus previousStatus, int ptr)
 {
     lastState       = previousStatus;
     StartPtr        = ptr;
     this.connection = connection ?? throw new ArgumentNullException(nameof(connection));
 }
Ejemplo n.º 20
0
 public override bool InitializeLoop(List <Rule> rules, List <ParserElementBase> path, ParserStatus status)
 {
     base.InitializeLoop(rules, path, status);
     return(true);
 }
Ejemplo n.º 21
0
        public async Task Run()
        {
            try
            {
                List <IdPrice> prices = Repository.Parser.GetAllPricesPrev().ToList();
                prices.Sort((p1, p2) => p1.ObjectID.CompareTo(p2.ObjectID));


                if (status != ParserStatus.Ready)
                {
                    return;
                }

                status = ParserStatus.Parsing;

                List <int> objects        = Repository.EuroMade.GetAvailableObjects();
                List <int> downloaded     = Repository.Parser.GetDownloadedObjects();
                List <int> objectsToCheck = new List <int>();

                foreach (var item in objects)
                {
                    if (!downloaded.Contains(item))
                    {
                        objectsToCheck.Add(item);
                    }
                }

                TotalObjectCount = objects.Count;
                int processedObject = downloaded.Count();

                foreach (var id in objectsToCheck)
                {
                    processedObject++;
                    CurrentObject = processedObject;
                    EuroItem euroItem = null;
                    try
                    {
                        euroItem = Repository.EuroMade.GetEuroItem(id);
                    }
                    catch (Exception e)
                    {
                    }

                    if (euroItem != null)
                    {
                        ParserItem parserItem = new ParserItem();

                        parserItem.EuroMadePrice        = euroItem.EuroMadePrice;
                        parserItem.Uri                  = euroItem.Uri;
                        parserItem.UploadToMarket       = euroItem.UploadToMarket;
                        parserItem.Name                 = euroItem.Name;
                        parserItem.ObjectID             = euroItem.ObjectID;
                        parserItem.AvailabilityEuroMade = euroItem.Available;
                        parserItem.VendorCode           = euroItem.VendorCode;

                        decimal prevPrice = 0;

                        try
                        {
                            prevPrice = prices.Where(e => e.ObjectID == parserItem.ObjectID).First().Price;
                        }
                        catch (InvalidOperationException)
                        {
                        }

                        parserItem.OriginalPrice = prevPrice;

                        parserItem.AvailabilityVerkk  = false;
                        parserItem.AvailabilityCount1 = 0;
                        parserItem.AvailabilityCount2 = 0;
                        parserItem.Discount           = false;
                        parserItem.DiscountValue      = 0;
                        parserItem.Height             = 0;
                        parserItem.Weight             = 0;
                        parserItem.Length             = 0;
                        parserItem.Width             = 0;
                        parserItem.EAN               = "0";
                        parserItem.VerkkPrice        = 0;
                        parserItem.DiscountUntilDate = null;
                        parserItem.DiscountUntilTime = null;

                        var       config   = Configuration.Default.WithDefaultLoader();
                        IDocument document = null;

                        try
                        {
                            document = await BrowsingContext.New(config).OpenAsync(euroItem.Uri);
                        }
                        catch (Exception)
                        {
                        }

                        var cellSelector = ".product-basic-details .add-to-cart .vk-button";
                        var button       = document.QuerySelector(cellSelector);

                        if (button == null)
                        {
                            continue;
                        }

                        if (document.QuerySelector("#avail-0-content") == null)
                        {
                            try
                            {
                                Repository.Parser.Insert(parserItem);
                            }
                            catch (Exception e)
                            {
                            }
                            continue;
                        }
                        else
                        {
                            parserItem.AvailabilityVerkk = true;
                        }

                        IElement parameters = null;

                        try
                        {
                            parameters = document.QuerySelectorAll(".product-details__category")?[1];
                        }
                        catch (ArgumentOutOfRangeException)
                        {
                            parameters = null;
                        }

                        string size = null;
                        if (parameters != null)
                        {
                            try
                            {
                                size = parameters?.QuerySelectorAll(".product-details-row__value")?[0]?.TextContent;
                            }
                            catch (ArgumentOutOfRangeException)
                            {
                                size = null;
                            }
                        }

                        string weight = null;
                        if (parameters != null)
                        {
                            try
                            {
                                weight = parameters?.QuerySelectorAll(".product-details-row__value")?[1]?.TextContent;
                            }
                            catch (ArgumentOutOfRangeException)
                            {
                                weight = null;
                            }
                        }

                        string ean = null;

                        try
                        {
                            ean = document.QuerySelectorAll(".product-details__category")?[0]
                                  ?.QuerySelectorAll(".product-details-row__value")?[2]
                                  ?.TextContent;
                        }
                        catch (ArgumentOutOfRangeException)
                        {
                            ean = null;
                        }

                        if (ean == null)
                        {
                            parserItem.EAN = "0";
                        }
                        else
                        {
                            parserItem.EAN = ean;
                        }

                        if (weight == null)
                        {
                            parserItem.Weight = 0;
                        }
                        else
                        {
                            if (double.TryParse(weight.Replace("kg", string.Empty).Trim(), out double weightValue))
                            {
                                parserItem.Weight = weightValue;
                            }
                            else
                            {
                                parserItem.Weight = 0;
                            }
                        }

                        if (size == null)
                        {
                            parserItem.Height = 0;
                            parserItem.Width  = 0;
                            parserItem.Length = 0;
                        }
                        else
                        {
                            List <string> sizes = size
                                                  .Replace("cm", string.Empty)
                                                  .Split('x')
                                                  .ToList();

                            if (sizes.Count == 3)
                            {
                                if (int.TryParse(sizes[0].Trim(), out int width))
                                {
                                    parserItem.Width = width;
                                }
                                else
                                {
                                    parserItem.Width = 0;
                                }

                                if (int.TryParse(sizes[1].Trim(), out int height))
                                {
                                    parserItem.Height = height;
                                }
                                else
                                {
                                    parserItem.Height = 0;
                                }

                                if (int.TryParse(sizes[2].Trim(), out int length))
                                {
                                    parserItem.Length = Convert.ToInt32(sizes[2].Trim());
                                }
                                else
                                {
                                    parserItem.Length = 0;
                                }
                            }
                            else
                            {
                                parserItem.Height = 0;
                                parserItem.Width  = 0;
                                parserItem.Length = 0;
                            }
                        }

                        var discountValue = document.QuerySelector(".price-tag-discount__amount");

                        if (discountValue != null)
                        {
                            parserItem.Discount = true;
                            string discountString = discountValue.TextContent
                                                    .Replace("€", string.Empty)
                                                    .Replace(",", ".");

                            if (decimal.TryParse(discountString, NumberStyles.AllowParentheses | NumberStyles.Float, CultureInfo.InvariantCulture, out decimal discountOutValue))
                            {
                                parserItem.DiscountValue = Math.Abs(discountOutValue);
                            }
                            else
                            {
                                parserItem.DiscountValue = 0;
                            }
                        }
                        else
                        {
                            parserItem.Discount      = false;
                            parserItem.DiscountValue = 0;
                        }

                        var price = document.QuerySelector(".price-tag-content__price-tag-price--current .price-tag-price__euros")
                                    ?.GetAttribute("content");

                        if (price != null)
                        {
                            if (decimal.TryParse(price, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out decimal priceVerkk))
                            {
                                parserItem.VerkkPrice = priceVerkk;
                            }
                            else
                            {
                                parserItem.VerkkPrice = 0;
                            }
                        }
                        else
                        {
                            parserItem.VerkkPrice = 0;
                        }

                        var available1 = document.QuerySelector(".stock-indicator__link")
                                         ?.TextContent
                                         ?.Replace("yli", string.Empty)
                                         ?.Trim();

                        var available2 = document.QuerySelector(".product-info-row__location")
                                         ?.TextContent
                                         ?.Replace("yli", string.Empty)
                                         ?.Trim();

                        IHtmlCollection <IElement> available3row = null;
                        string available3 = null;

                        try
                        {
                            available3row = document.QuerySelectorAll(".product-info-row");

                            foreach (var item in available3row)
                            {
                                var name = item.QuerySelector(".product-info-row__name")?.TextContent ?? string.Empty;
                                if (name == "Vantaan varasto")
                                {
                                    available3 = document.QuerySelector(".product-info-row__location")
                                                 ?.TextContent
                                                 ?.Replace("yli", string.Empty)
                                                 ?.Trim();
                                }
                            }
                        }
                        catch (ArgumentOutOfRangeException)
                        {
                            available3row = null;
                        }

                        if (available1 == null)
                        {
                            parserItem.AvailabilityCount1 = 0;
                        }
                        else
                        {
                            if (int.TryParse(available1, out int available1value))
                            {
                                parserItem.AvailabilityCount1 = available1value;
                            }
                            else
                            {
                                parserItem.AvailabilityCount1 = 0;
                            }
                        }

                        if (available2 == null)
                        {
                            parserItem.AvailabilityCount2 = 0;
                        }
                        else
                        {
                            if (int.TryParse(available2, out int available2value))
                            {
                                parserItem.AvailabilityCount2 = available2value;
                            }
                            else
                            {
                                parserItem.AvailabilityCount2 = 0;
                            }
                        }

                        if (available3 == null)
                        {
                            parserItem.AvailabilityCount3 = 0;
                        }
                        else
                        {
                            if (int.TryParse(available3, out int available3value))
                            {
                                parserItem.AvailabilityCount3 = available3value;
                            }
                            else
                            {
                                parserItem.AvailabilityCount3 = 0;
                            }
                        }

                        var discountDate = document.QuerySelector(".discount-info-details em")?.TextContent;

                        if (discountDate != null)
                        {
                            Regex dateReg = new Regex(@"(\d+\.\d+\.\d+)");
                            Regex timeReg = new Regex(@"( \d+\.\d+ )");

                            Match dateMatch = dateReg.Match(discountDate);
                            if (dateMatch.Success)
                            {
                                try
                                {
                                    parserItem.DiscountUntilDate = DateTime.ParseExact(dateMatch.Value, @"d.M.yyyy", CultureInfo.InvariantCulture);
                                }
                                catch (FormatException e)
                                {
                                }
                            }

                            Match timeMatch = timeReg.Match(discountDate);
                            if (timeMatch.Success)
                            {
                                try
                                {
                                    DateTime dt = new DateTime();
                                    dt = DateTime.ParseExact(timeMatch.Value.Trim(), @"H.m", CultureInfo.InvariantCulture);
                                    parserItem.DiscountUntilTime = new TimeSpan(0, dt.Hour, dt.Minute, dt.Second, 0);
                                }
                                catch (FormatException e)
                                {
                                }
                            }
                        }

                        try
                        {
                            Repository.Parser.Insert(parserItem);
                        }
                        catch (Exception e)
                        {
                        }
                    }
                }

                List <IdPrice> pricesParser = Repository.Parser.GetAllPricesParser().ToList();
                prices.Sort((p1, p2) => p1.ObjectID.CompareTo(p2.ObjectID));

                Repository.Parser.TruncatePrices();
                Repository.Parser.SavePrices(pricesParser);

                status = ParserStatus.Done;
            }
            catch (Exception e)
            {
            }
        }
Ejemplo n.º 22
0
        public override bool InitializeLoop(List <Rule> rules, List <ParserElementBase> path, ParserStatus status)
        {
            bool LoopHasEnd  = false;
            bool LoopHasWord = false;

            path.Add(this);
            int ruleNo = path.IndexOf(RuleElement);

            if (ruleNo > -1)
            {
                // The link is recursive
                for (int i = ruleNo; i < path.Count; i++)
                {
                    if (path[i] is RuleLink)
                    {
                        ((RuleLink)path[i]).Recursive = true;
                    }
                    if (path[i] is Rule)
                    {
                        var rule = path[i] as Rule;
                        LoopHasEnd = LoopHasEnd || rule.LoopHasEnd;
                        var  subPath   = new List <RuleLink>();
                        bool linkFound = false;
                        LoopHasWord = LoopHasWord || rule.InitializeLoopHasWord(((RuleLink)path[i + 1]), subPath, ref linkFound);
                    }
                }

                if (!LoopHasWord)
                {
                    ((Rule)path[ruleNo]).LoopLeftRecursive = true;
                }
            }
            else
            {
                LoopHasEnd = RuleElement.InitializeLoop(rules, path, status);
            }

            path.Remove(this);
            return(LoopHasEnd);
        }
Ejemplo n.º 23
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ParseError"/> class as a partial clone of <see cref="ParserStatus"/>.
 /// </summary>
 /// <param name="status">The full <see cref="ParserStatus"/> to create a <see cref="ParseError"/> from.</param>
 public ParseError(ParserStatus status)
 {
     ErrorType         = status.ErrorType;
     ExpectedCharacter = status.ExpectedCharacter;
 }
Ejemplo n.º 24
0
 public TextBuffer()
 {
     PointerNextChar = 0;
     Status          = new ParserStatus(this);
     //Comments = new List<CodeElement>();
 }
Ejemplo n.º 25
0
        /// <inheritdoc/>
        public override ParseError ParseNextChar(char c)
        {
            switch (_state)
            {
            case State.ARGUMENT_X:
                return(ParseUintArg(c));

            case State.OPEN_Y:
            case State.ARGUMENT_Y:
                return(ParseUintArg(c));

            case State.OPEN_EXPRESSION:
                if (c == '{')
                {
                    _state  = State.EXPRESSION;
                    _matrix = new TensorNode(_sizes);
                    return(new ParseError());
                }
                else
                {
                    return(new ParseError(ErrorType.CANNOT_PROCEED));
                }

            case State.EXPRESSION:
            {
                if ((c == '}' || c == ',') && _depth == 0)
                {
                    ParserStatus status = _childParser.Finalize();
                    if (status.Failed)
                    {
                        return(new ParseError(status));
                    }

                    ExpTree tree = _childParser.Tree;
                    _childParser = new DefaultParser();
                    if (tree == null)
                    {
                        return(new ParseError(ErrorType.UNKNOWN));
                    }

                    _matrix.AddChild(tree.Root);

                    if (c == '}')
                    {
                        _state = State.DONE;
                        Output = _matrix;
                    }

                    return(new ParseError());
                }
                else
                {
                    if (c == '{')
                    {
                        _depth++;
                    }
                    else if (c == '}')
                    {
                        _depth--;
                    }
                    ParserStatus result = _childParser.ParseNextChar(c);
                    return(new ParseError(result));
                }
            }

            default:
                return(new ParseError(ErrorType.UNKNOWN));
            }
        }
Ejemplo n.º 26
0
        /// <inheritdoc/>
        public override ParseError ParseNextChar(char c)
        {
            switch (_state)
            {
            case State.VAR:
            {
                if (char.IsLetter(c))
                {
                    _node.Variable = new VarValueNode(c);
                    _state         = State.CLOSING_VAR;
                    return(new ParseError());
                }

                return(new ParseError(ErrorType.DERIVATIVE_MUST_BE_VARIABLE));
            }

            case State.CLOSING_VAR:
            {
                if (c == ']')
                {
                    _state = State.OPEN_EXPRESSION;
                    return(new ParseError());
                }
                if (c == ',')
                {
                    _state = State.LOWER;
                    _node.IsDeterminate = true;
                    return(new ParseError());
                }
                return(new ParseError(ErrorType.MUST_BE, ']'));        // TODO: Multiple MUST_BE characters.
            }

            case State.LOWER:
            {
                if (c == ',' && _depth == 0)
                {
                    ParserStatus status = _childParser.Finalize();
                    if (status.Failed)
                    {
                        return(new ParseError(status));
                    }

                    ExpTree tree = _childParser.Tree;
                    if (tree == null)
                    {
                        return(new ParseError(ErrorType.UNKNOWN));
                    }

                    _node.LowerBound = tree.Root;
                    _state           = State.UPPER;
                    _childParser     = new DefaultParser();
                    return(new ParseError());
                }
                else
                {
                    if (c == '{')
                    {
                        _depth++;
                    }
                    else if (c == '}')
                    {
                        _depth--;
                    }
                    ParserStatus result = _childParser.ParseNextChar(c);
                    return(new ParseError(result));
                }
            }

            case State.UPPER:
            {
                if (c == ']' && _depth == 0)
                {
                    ParserStatus status = _childParser.Finalize();
                    if (status.Failed)
                    {
                        return(new ParseError(status));
                    }

                    ExpTree tree = _childParser.Tree;
                    if (tree == null)
                    {
                        return(new ParseError(ErrorType.UNKNOWN));
                    }

                    _node.UpperBound = tree.Root;
                    _state           = State.OPEN_EXPRESSION;
                    _childParser     = new DefaultParser();
                    return(new ParseError());
                }
                else
                {
                    if (c == '{')
                    {
                        _depth++;
                    }
                    else if (c == '}')
                    {
                        _depth--;
                    }
                    ParserStatus result = _childParser.ParseNextChar(c);
                    return(new ParseError(result));
                }
            }

            case State.OPEN_EXPRESSION:
            {
                if (c != '{')
                {
                    return(new ParseError(ErrorType.MUST_BE, '{'));
                }
                _state = State.EXPRESSION;
                return(new ParseError());
            }

            case State.EXPRESSION:
            {
                if (c == '}' && _depth == 0)
                {
                    ParserStatus status = _childParser.Finalize();
                    if (status.Failed)
                    {
                        return(new ParseError(status));
                    }

                    ExpTree tree = _childParser.Tree;
                    if (tree == null)
                    {
                        return(new ParseError(ErrorType.UNKNOWN));
                    }

                    _node.AddChild(tree.Root);
                    _state = State.DONE;
                    Output = _node;
                    return(new ParseError());
                }
                else
                {
                    if (c == '{')
                    {
                        _depth++;
                    }
                    else if (c == '}')
                    {
                        _depth--;
                    }
                    ParserStatus result = _childParser.ParseNextChar(c);
                    return(new ParseError(result));
                }
            }

            default:
                return(new ParseError(ErrorType.UNKNOWN));
            }
        }
Ejemplo n.º 27
0
        public void ReadCallback(IAsyncResult result)
        {
            HttpConnection conn = (HttpConnection)result.AsyncState;

            conn.MarkActiveOnce();
            try {
                int byteReceived = conn.ActiveSocket.EndReceive(result);

                // Remote Client Close Connection
                if (byteReceived == 0)
                {
                    conn.NaturalClose();
                    return;
                }

                ParserStatus status = ParserStatus.MOVE_NEXT_SAME_BUFFER;

                do
                {
                    switch (currentState)
                    {
                    case HTTP_HEADER:
                        status = headerParser.ProcessBuffer(conn.ConnectionBuffer, byteReceived);
                        if (status == ParserStatus.ERROR)
                        {
                            conn.Response.SendError(HttpStatusCode.BadRequest);
                            return;
                        }
                        else if (status.HasFlag(ParserStatus.MOVE_NEXT))
                        {
                            HeaderStatus header = CreateAndCheckHeader(headerParser.Content, conn, out maxContentLength);
                            if (header == HeaderStatus.ERROR)
                            {
                                conn.Response.SendError(HttpStatusCode.BadRequest);
                            }
                            else if (header == HeaderStatus.NO_CONTENT)
                            {
                                currentState = ACCEPT;
                            }
                            else if (header == HeaderStatus.CONTINUE)
                            {
                                //TODO Add 100/417 response
                            }
                            else
                            {
                                currentState = CONTENT;
                            }

                            contentParser.BufferOffset = headerParser.BufferOffset;
                        }
                        conn.ActiveSocket.BeginReceive(
                            conn.ConnectionBuffer, 0,
                            HttpConnection.BUFFER_SIZE, 0,
                            new AsyncCallback(ReadCallback), conn);
                        break;

                    case CONTENT:
                        if (maxContentLength > conn.ServerContext.ServerConfig.MaxContentSizeByte)
                        {
                            conn.Response.SendError(HttpStatusCode.RequestEntityTooLarge);
                        }

                        // IMPORTANT Identify chunked data and try to process it.
                        // =================================================
                        // The illustration of such process.
                        // We will handle it to a IChunkedRequestStream (yet to implement)
                        // This will have a built in chunk parse state machine, invoking each time
                        // when user calls method IChunkedRequestStream::ReadData.
                        //
                        // This connection will be blocked until all chunked data to be read
                        // The control will be moved to the IChunkedRequestStream, this current thread
                        // will be blocked according to "chunk parse state machine" return value:
                        //      1. ParserStatus.REQUIRE_MORE:
                        //          Thread resume, BeginReceive will be invoke to get more data.
                        //      2. ParserStatus.MOVE_NEXT:
                        //          Thread resume, Goto ACCEPT state, wait for next request.
                        //      3. ParserStatus.ERROR:
                        //          Thread resume, Send BAD_REQUEST, close the current socket.

                        status = contentParser.ProcessBuffer(conn.ConnectionBuffer, byteReceived, maxContentLength);
                        if (status.HasFlag(ParserStatus.MOVE_NEXT))
                        {
                            currentState = ACCEPT;
                        }

                        // Put the socket on recieving mode for further incoming requests
                        conn.ActiveSocket.BeginReceive(
                            conn.ConnectionBuffer, 0,
                            HttpConnection.BUFFER_SIZE, 0,
                            new AsyncCallback(ReadCallback), conn);
                        break;

                    case ACCEPT:
                        currentState     = HTTP_HEADER;
                        maxContentLength = 0;
                        headerParser.ResetParser();
                        contentParser.ResetParser();

                        CurrentRequest.RequestContentStream = new RequestStream();
                        CurrentRequest.RequestContentStream.CopyFrom(contentParser.Content);

                        conn.ServerContext.RequestDispatcher.Dispatch(
                            CurrentRequest,
                            new Response.HttpResponse(conn));
                        break;
                    }
                }while (status.HasFlag(ParserStatus.SAME_BUFFER) || currentState == ACCEPT);
            }
            catch (SocketException se) {
                //TODO May need logger here
                Console.WriteLine("{0}\r\n{1}", se.Message, se.StackTrace);
                conn.NaturalClose();
            }
            catch (Exception ex) {
                Console.WriteLine("{0}\r\n{1}", ex.Message, ex.StackTrace);
                conn.Response.SendError(HttpStatusCode.InternalServerError);
            }
        }
Ejemplo n.º 28
0
 public override bool InitializeLoop(List <Rule> rules, List <ParserElementBase> path, ParserStatus status)
 {
     return(_otherForms.Aggregate(false, (ok, item) => ok | item.InitializeLoop(rules, path, status)));
 }
Ejemplo n.º 29
0
        /// <summary>
        /// Process token according to late bind scheme
        /// </summary>
        /// <param name="TokenToAdd">Lexer generated token</param>
        void ProcessTokenLB(Token TokenToAdd)
        {
            try
            {
                switch (TokenToAdd.TokenTypeValue)
                {
                    case TokenType.ISO1030321:
                        Lexer.Tokenizer(DataStream);
                        currStatus = ParserStatus.ISO;
                        break;
                    case TokenType.ISO1030321END:
                        Lexer.Tokenizer(DataStream);
                        currStatus = ParserStatus.READY;
                        break;
                    case TokenType.HEADER:
                        Lexer.Tokenizer(DataStream);
                        currStatus = ParserStatus.HEADER;
                        break;
                    case TokenType.ENDSEC:
                        Lexer.Tokenizer(DataStream);
                        currStatus = ParserStatus.ISO;
                        break;
                    case TokenType.ENTITYINSTANCENAME:
                        switch (currStatus)
                        {
                            case ParserStatus.DATA:
                                Token oneToken = Lexer.Tokenizer(DataStream);
                                if (oneToken.TokenTypeValue != TokenType.EQUAL)
                                {
                                    Console.WriteLine("Syntax Error in Entity Instance : #" + TokenToAdd.IntegerValue.ToString());
                                    return;
                                }
                                oneToken = Lexer.Tokenizer(DataStream);
                                if (oneToken.TokenTypeValue == TokenType.KEYWORD)
                                {
                                    SortedDictionary<uint, Instance> toAdd = DataList;
                                    if (toAdd.ContainsKey((uint)TokenToAdd.IntegerValue))
                                    {
                                        // when instance is already created by other caller
                                        //P21Instance a =   toAdd[(uint)TokenToAdd.IntegerValue];
                                        InstanceSimple aSimpleInstance = (InstanceSimple)toAdd[(uint)TokenToAdd.IntegerValue];
                                        ModifySimpleInstanceRecord(aSimpleInstance, oneToken.StringValue);
                                    }
                                    else
                                    {
                                        // when it is the first time of parsing entity instance name #???
                                        InstanceSimple aSimpleInstance = AddSimpleInstance((uint)TokenToAdd.IntegerValue, oneToken.StringValue);
                                        toAdd.Add((uint)TokenToAdd.IntegerValue, aSimpleInstance);
                                    }

                                    // adding instances to instanceByType according to instance type
                                    if (InstanceByType.ContainsKey(oneToken.StringValue))
                                    {
                                        List<uint> existingList = InstanceByType[oneToken.StringValue];
                                        existingList.Add((uint)TokenToAdd.IntegerValue);
                                    }
                                    else
                                    {
                                        List<uint> newList = new List<uint>();
                                        newList.Add((uint)TokenToAdd.IntegerValue);
                                        InstanceByType.Add(oneToken.StringValue, newList);
                                    }

                                }
                                else if (oneToken.TokenTypeValue == TokenType.LEFTPARENTHESIS)
                                {
                                    // need update for complex type
                                    SortedDictionary<uint, Instance> toAdd = DataList;
                                    InstanceComplex aComplexInstance = AddComplexInstance((uint)TokenToAdd.IntegerValue);
                                    if (toAdd.ContainsKey((uint)TokenToAdd.IntegerValue))
                                    { toAdd[(uint)TokenToAdd.IntegerValue] = aComplexInstance; }
                                    else
                                    { toAdd.Add((uint)TokenToAdd.IntegerValue, aComplexInstance); }

                                }
                                else
                                {
                                    Console.WriteLine("Syntax Error at value defintion in Entity Instance : #" + TokenToAdd.IntegerValue.ToString());
                                    return;
                                }
                                break;
                            default:
                                Console.WriteLine("Parser Error : Not defined parsing condition in Entity Instance Name : " + currStatus);
                                break;
                        }
                        break;
                    case TokenType.KEYWORD:
                        switch (currStatus)
                        {
                            case ParserStatus.HEADER:
                                SimpleRecord aSimpleRecord = AddSimpleRecord(null, TokenToAdd.StringValue);
                                HeaderList.Add(aSimpleRecord);

                                Lexer.Tokenizer(DataStream); // cleans semicolon at the end of header instance
                                break;
                            case ParserStatus.DATA:
                                Console.WriteLine("Syntax Error : Instance without Entity Instance Name");
                                break;
                            default:
                                Console.WriteLine("Parser Error : Not defined parsing condition in Keyword");
                                break;
                        }
                        break;

                    case TokenType.DATA:
                        Lexer.Tokenizer(DataStream);
                        currStatus = ParserStatus.DATA;

                        //theDataSet.aDataSectionList.Add(new DataSection());

                        break;

                    default:
                        Console.WriteLine("not defined condition : " + TokenToAdd.TokenTypeValue.ToString());
                        break;
                }
            }
            catch (Exception)
            {

                throw new FormatException("Parse Error!");
            }
        }
Ejemplo n.º 30
0
 public override bool InitializeLoop(List <Rule> rules, List <ParserElementBase> path, ParserStatus status)
 {
     // If a word contains recursiveness, this must be overriden
     return(true);
 }
Ejemplo n.º 31
0
 public override bool InitializeLoop(List <Rule> rules, List <ParserElementBase> path, ParserStatus status)
 {
     return(ChildNodes[0].InitializeLoop(rules, path, status) |
            ChildNodes[1].InitializeLoop(rules, path, status));
 }