Esempio n. 1
0
        private void AssignValueToCurrentPair(CharLocation begin, CharLocation end, bool missingQuote = false)
        {
            var pair = _lineState.CurrentPair;

            pair.ValueInterval     = begin == CharLocation.Empty ? Interval.Empty : new Interval(begin, end);
            pair.MissingValueQuote = missingQuote;
        }
Esempio n. 2
0
        public static CharLocation NextLocation(Direction direction, CharLocation currLocation, char[][] charsArray)
        {
            switch (direction)
            {
            case Direction.Right:
                currLocation.X += 1;
                break;

            case Direction.Left:
                currLocation.X -= 1;
                break;

            case Direction.Down:
                currLocation.Y += 1;
                break;

            case Direction.Up:
                currLocation.Y -= 1;
                break;
            }

            currLocation.Character = charsArray[currLocation.Y][currLocation.X];

            return(currLocation);
        }
Esempio n. 3
0
        public void DetermineInitialPosition()
        {
            CharLocation expectedLocation = new CharLocation()
            {
                Character = '@', Direction = Direction.Left, X = 2, Y = 0
            };

            char[][] testArray = new char[9][]
            {
                new char[] { ' ', ' ', '@', '-', '-', '-', '+' },
                new char[] { ' ', ' ', ' ', ' ', ' ', ' ', 'B' },
                new char[] { 'K', '-', '-', '-', '-', '-', '|', '-', '-', 'A' },
                new char[] { '|', ' ', ' ', ' ', ' ', ' ', '|', ' ', ' ', '|' },
                new char[] { '|', ' ', ' ', '+', '-', '-', 'E', ' ', ' ', '|' },
                new char[] { '|', ' ', ' ', '|', ' ', ' ', ' ', ' ', ' ', '|' },
                new char[] { '+', '-', '-', 'E', '-', '-', 'E', 'x', ' ', 'C' },
                new char[] { ' ', ' ', ' ', '|', ' ', ' ', ' ', ' ', ' ', '|' },
                new char[] { ' ', ' ', ' ', '+', '-', '-', 'F', '-', '-', '+' },
            };

            var result = Helpers.Utils.DetermineInitialPosition(testArray);

            Assert.AreEqual(expectedLocation.Character, result.Character);
            Assert.AreEqual(expectedLocation.Direction, result.Direction);
            Assert.AreEqual(expectedLocation.X, result.X);
            Assert.AreEqual(expectedLocation.Y, result.Y);
        }
Esempio n. 4
0
 public static CharLocation CopyLocation(CharLocation currLocation)
 {
     return(new CharLocation()
     {
         X = currLocation.X, Y = currLocation.Y, Character = currLocation.Character, Direction = currLocation.Direction
     });
 }
Esempio n. 5
0
        private void ParseOpenString()
        {
            var          c = _input.Next;
            CharLocation begin = CharLocation.Empty;
            int          endLine = 0, endColumn = 0, endIndex = -1;

            while (true)
            {
                if (c.IsEndOfOpenString(_processJsonBrackets) || c == -1)
                {
                    AssignValueToCurrentPair(begin, new CharLocation(endLine, endColumn, endIndex));
                    return;
                }

                if (c.IsNewLineCharacter())
                {
                    if (begin.Index == -1)
                    {
                        begin     = new CharLocation(_input.Line, _input.Column + 1, _input.Index + 1);
                        endLine   = 0;
                        endColumn = 0;
                        endIndex  = -1;
                    }
                    if (_wsaStack.Count < 1 && !_lineState.Inline) //ML String is not allowed for inline pairs and in WSA
                    {
                        _lineState.State = ParserStateEnum.IndentMLS;
                        AssignValueToCurrentPair(begin, new CharLocation(endLine, endColumn, endIndex));
                    }
                    else
                    {
                        AssignValueToCurrentPair(begin, new CharLocation(endLine, endColumn, endIndex));
                        return;
                    }
                    break;
                }
                _input.Consume();

                if (!c.IsSpaceCharacter())
                {
                    if (begin.Index == -1)
                    {
                        begin = new CharLocation(_input);
                    }
                    endLine   = _input.Line;
                    endColumn = _input.Column;
                    endIndex  = _input.Index;
                }
                c = _input.Next;
            }
        }
Esempio n. 6
0
        private void buttonLocation_Click(object sender, EventArgs e)
        {
            if (_curCharacter == null)
            {
                return;
            }
            var locDlg = new CharLocation(_curCharacter);

            if (locDlg.ShowDialog(this) != DialogResult.OK)
            {
                return;
            }

            FillPlayerData(locDlg.Char);
        }
Esempio n. 7
0
        /// <summary>
        /// Open name ends with : = or end of line
        /// </summary>
        /// <returns>true if eof is not reached</returns>
        private void ParseOpenName()
        {
            var c = _input.Next;
            var begin = CharLocation.Empty;
            int endLine = 0, endColumn = 0, endIndex = -1;

            while (c != -1)
            {
                if (c.IsEndOfOpenName(_processJsonBrackets))
                {
                    break;
                }
                _input.Consume();

                if (!c.IsSpaceCharacter())
                {
                    if (begin.Index == -1)
                    {
                        begin = new CharLocation(_input);
                    }
                    endLine   = _input.Line;
                    endColumn = _input.Column;
                    endIndex  = _input.Index;
                }

                c = _input.Next;
            }
            if (!_lineState.ChainingStarted)
            {
                _lineState.CurrentPair = new MappedPair
                {
                    NameInterval   = begin == CharLocation.Empty ? Interval.Empty : new Interval(begin, new CharLocation(endLine, endColumn, endIndex)),
                    Assignment     = AssignmentEnum.None,
                    NameQuotesType = 0
                };
            }
            else
            {
                _lineState.CurrentPair = new MappedPair
                {
                    NameInterval   = new Interval(begin, new CharLocation(endLine, endColumn, endIndex)),
                    Assignment     = AssignmentEnum.None,
                    NameQuotesType = 0
                };
            }
        }
Esempio n. 8
0
        /// <summary>
        /// Parses single line or multiline quoted value.
        /// The function can be called only if the next symbol is a quote.
        /// </summary>
        private void ParseQuotedValue(int quote)
        {
            _lineState.CurrentPair.ValueQuotesType = quote;
            _input.Consume(); // Consume starting '
            var begin = new CharLocation(_input);
            var c     = _input.Next;

            while (true)
            {
                if (c == quote)
                {
                    _input.Consume();
                    AssignValueToCurrentPair(begin, new CharLocation(_input));
                    break;
                }
                if (quote == '"' && c == '\\') //Escape character in DQ value
                {
                    _input.Consume();
                    c = _input.Next;
                }

                if (c.IsNewLineCharacter())
                {
                    if (_wsaStack.Count < 1 && !_lineState.Inline)
                    {
                        AssignValueToCurrentPair(begin, new CharLocation(_input));
                        _lineState.State = ParserStateEnum.IndentMLS;
                        break;
                    }

                    ReportSyntaxError(1, new Interval(_input), quote == '"' ? "Double quote" : "Single quote");
                    AssignValueToCurrentPair(begin, new CharLocation(_input), true);
                    break;
                }

                if (c == -1)
                {
                    ReportSyntaxError(1, new Interval(_input), quote == '"' ? "Double quote" : "Single quote");
                    AssignValueToCurrentPair(begin, new CharLocation(_input), true);
                    break;
                }
                _input.Consume();
                c = _input.Next;
            }
        }
Esempio n. 9
0
        public static CharLocation DetermineInitialPosition(char[][] chars)
        {
            CharLocation startPosition = new CharLocation();

            for (int i = 0; i < chars.Length; i++)
            {
                for (int j = 0; j < chars[i].Length; j++)
                {
                    if (chars[i][j] == START_CHAR)
                    {
                        startPosition.X         = j;
                        startPosition.Y         = i;
                        startPosition.Character = START_CHAR;

                        return(startPosition);
                    }
                }
            }
            return(null);
        }
Esempio n. 10
0
        private void ParseQuotedName(int quote)
        {
            _input.Consume(); // Consume starting "
            var begin = new CharLocation(_input);
            var c     = _input.Next;

            while (true)
            {
                if (c == quote)
                {
                    _input.Consume();
                    break;
                }
                if (c.IsNewLineCharacter() || c == -1)
                {
                    ReportSyntaxError(1, new Interval(_input), quote == '"'?"Double quote": "Single quote");
                    break;
                }
                _input.Consume();
                c = _input.Next;
            }
            if (!_lineState.ChainingStarted)
            {
                _lineState.CurrentPair = new MappedPair
                {
                    NameInterval   = new Interval(begin, new CharLocation(_input)),
                    Assignment     = AssignmentEnum.None,
                    NameQuotesType = quote
                };
            }
            else
            {
                _lineState.CurrentPair = new MappedPair
                {
                    NameInterval   = new Interval(begin, new CharLocation(_input)),
                    Assignment     = AssignmentEnum.None,
                    NameQuotesType = quote
                };
            }
        }
Esempio n. 11
0
        private void ParseFreeOpenString()
        {
            _input.ConsumeSpaces();
            CharLocation begin = CharLocation.Empty;
            int          endLine = 0, endColumn = 0, endIndex = -1;

            while (_input.Next != -1)
            {
                if (_input.Next.IsNewLineCharacter())
                {
                    if (begin.Index == -1)
                    {
                        begin = new CharLocation(_input.Line, _input.Column + 1, _input.Index + 1);
                    }
                    if (_wsaStack.Count < 1 && !_lineState.Inline) //ML String is not allowed for inline pairs and in WSA
                    {
                        _lineState.State = ParserStateEnum.IndentMLS;
                        AssignValueToCurrentPair(begin, new CharLocation(endLine, endColumn, endIndex));
                        return;
                    }
                    break;
                }
                if (!_input.Next.IsSpaceCharacter()) //Ignoring leading and trailing spaces
                {
                    _input.Consume();
                    if (begin.Index == -1)
                    {
                        begin = new CharLocation(_input);
                    }
                    endLine   = _input.Line;
                    endColumn = _input.Column;
                    endIndex  = _input.Index;
                }
                else
                {
                    _input.Consume();
                }
            }
            AssignValueToCurrentPair(begin, new CharLocation(endLine, endColumn, endIndex));
        }
Esempio n. 12
0
        public static bool ConsumeMlComment(this ICharStream stream, IPairFactory pairFactory, Pair parent)
        {
            if (stream.Next != '\"')
            {
                return(false);
            }
            if (stream.La(2) != '\"')
            {
                return(false);
            }
            if (stream.La(3) != '\"')
            {
                return(false);
            }

            stream.Consume();
            var begin = new CharLocation(stream);

            stream.Consume();
            stream.Consume();
            while (!(stream.Next == '\"' && stream.La(2) == '\"' && stream.La(3) == '\"') && stream.Next != -1)
            {
                stream.Consume();
            }

            if (stream.Next == '\"')
            {
                stream.Consume();
                stream.Consume();
                stream.Consume();
            }
            var comment = pairFactory.ProcessComment((ITextSource)stream, 2, new Interval(begin, new CharLocation(stream)));

            if (comment != null)
            {
                pairFactory.AppendChild(parent, comment);
            }
            return(true);
        }
Esempio n. 13
0
        public static bool ConsumeSlComment(this ICharStream stream, IPairFactory pairFactory, Pair parent)
        {
            if (stream.Next != '\'')
            {
                return(false);
            }
            if (stream.La(2) != '\'')
            {
                return(false);
            }
            if (stream.La(3) != '\'')
            {
                return(false);
            }

            stream.Consume();
            var begin = new CharLocation(stream);

            stream.Consume();
            stream.Consume();
            var c = stream.Next;

            while (!c.IsNewLineCharacter() && c != -1)
            {
                stream.Consume();
                c = stream.Next;
            }

            var comment = pairFactory.ProcessComment((ITextSource)stream, 1, new Interval(begin, new CharLocation(stream)));

            if (comment != null)
            {
                pairFactory.AppendChild(parent, comment);
            }
            return(true);
        }
Esempio n. 14
0
        /// <summary>
        /// This function is called only if next character is : or =
        /// </summary>
        /// <returns></returns>
        private AssignmentEnum GetAssignment()
        {
            AssignmentEnum assignment;

            if (_input.Next == ':')
            {
                _input.Consume();
                var begin = new CharLocation(_input);

                if (_input.Next == ':')
                {
                    _input.Consume();
                    if (_input.Next == ':')
                    {
                        _input.Consume();
                        assignment = AssignmentEnum.CCC;
                    }
                    else
                    {
                        assignment = AssignmentEnum.CC;
                    }
                }
                else
                {
                    if (_input.Next == '=')
                    {
                        _input.Consume();
                        assignment = AssignmentEnum.CE;
                    }
                    else
                    {
                        assignment = AssignmentEnum.C;
                    }
                }
                _lineState.CurrentPair.AssignmentInterval = new Interval(begin, new CharLocation(_input));
            }
            else // =
            {
                _input.Consume();
                var begin = new CharLocation(_input);
                _lineState.CurrentPair.AssignmentInterval = new Interval(_input);
                if (_input.Next == '=')
                {
                    _input.Consume();
                    assignment = AssignmentEnum.EE;
                }
                else if (_input.Next == ':') // =:
                {
                    _input.Consume();
                    if (_input.Next == ':') // =::
                    {
                        _input.Consume();
                        assignment = AssignmentEnum.ECC;
                    }
                    else
                    {
                        assignment = AssignmentEnum.EC;
                    }
                }
                else
                {
                    assignment = AssignmentEnum.E;
                }
                _lineState.CurrentPair.AssignmentInterval = new Interval(begin, new CharLocation(_input));
            }
            _lineState.CurrentPair.Assignment = assignment;
            EnsureAssignmentEnds();
            return(assignment);
        }
Esempio n. 15
0
        public static Direction DetermineDirection(char[][] charsArray, CharLocation currLoc, CharLocation prevLoc = null)
        {
            var xAdd1 = new CharLocation()
            {
                X = currLoc.X + 1, Y = currLoc.Y
            };
            var xSub1 = new CharLocation()
            {
                X = currLoc.X - 1, Y = currLoc.Y
            };
            var yAdd1 = new CharLocation()
            {
                X = currLoc.X, Y = currLoc.Y + 1
            };
            var ySub1 = new CharLocation()
            {
                X = currLoc.X, Y = currLoc.Y - 1
            };
            var newDirection    = Direction.None;
            int directionsCount = 0;

            if (prevLoc == null)
            {
                prevLoc = currLoc;
            }

            //Up
            if (LocationValid(charsArray, ySub1.Y, ySub1.X) && charsArray[ySub1.Y][ySub1.X] != ' ' && !AreSameCoordinates(prevLoc, ySub1))
            {
                newDirection = Direction.Up;
                directionsCount++;
            }

            //Down
            if (LocationValid(charsArray, yAdd1.Y, yAdd1.X) && charsArray[yAdd1.Y][yAdd1.X] != ' ' && !AreSameCoordinates(prevLoc, yAdd1))
            {
                newDirection = Direction.Down;
                directionsCount++;
            }

            //Right
            if (LocationValid(charsArray, xAdd1.Y, xAdd1.X) && charsArray[xAdd1.Y][xAdd1.X] != ' ' && !AreSameCoordinates(prevLoc, xAdd1))
            {
                newDirection = Direction.Right;
                directionsCount++;
            }

            //Left
            if (LocationValid(charsArray, xSub1.Y, xSub1.X) && charsArray[xSub1.Y][xSub1.X] != ' ' && !AreSameCoordinates(prevLoc, xSub1))
            {
                newDirection = Direction.Left;
                directionsCount++;
            }

            //Last
            if (directionsCount > 1)
            {
                return(currLoc.Direction);
            }

            currLoc.Direction = newDirection;

            return(newDirection);
        }
Esempio n. 16
0
 public static bool AreSameCoordinates(CharLocation a, CharLocation b)
 {
     return(a.Y == b.Y && a.X == b.X);
 }