示例#1
0
        public static Property ReadProperty(ReadFile input)
        {
            string lName = string.Empty;
            string lValue = string.Empty;

            while (!input.EOF)
            {
                char c = input.Get();

                if (Char.IsLetter(c))
                    lName = lName + c.ToString();

                if (c == '[')
                {
                    lValue = ReadPropertyValue(input);

                    return new Property(lName, lValue);
                }
            }

            return null;
        }
示例#2
0
        public static string ReadPropertyValue(ReadFile input)
        {
            string lValue = string.Empty;
            bool lEscaped = false;

            while (!input.EOF)
            {
                char c = input.Get();

                if ((c == ']') && (!lEscaped))
                    return lValue;

                lValue = lValue + c.ToString();

                if (lEscaped)
                    lEscaped = false;
                else
                    if (c == EscapeCharacter)
                        lEscaped = true;
            }

            return lValue;
        }
示例#3
0
        public static SGFTokens Tokenize(ReadFile input)
        {
            SGFTokens lTokens = new SGFTokens();

            if (!input.IsOk())
            {
                lTokens.SetErrorMessage(input.GetErrorMessage());
                return lTokens;
            }

            while (!input.EOF)
            {
                char c = input.Peek();

                if (Char.IsLetter(c))
                {
                    Property lProperty = ReadProperty(input);

                    if (lProperty == null)
                    {
                        lTokens.SetErrorMessage("ERROR: unable to parse sgf file");
                        return lTokens;
                    }

                    lTokens.Add(new SGFToken(lProperty));
                }
                else
                {
                    input.Get(); // read past the character

                    if (c == '[')
                    {
                        lTokens.RemovePreviousWhitespace();

                        SGFToken lSGFToken = lTokens.LastToken();

                        if ((lSGFToken == null) || (lSGFToken.Type != TokenType.PROPERTY))
                        {
                            lTokens.SetErrorMessage("ERROR: unable to parse sgf file");
                            return lTokens;
                        }

                        string lValue = ReadPropertyValue(input);

                        lSGFToken.Property.AdditionalValues.Add(lValue);
                    }
                    else if (c == '(')
                        lTokens.Add(new SGFToken(TokenType.OPENPARAN));
                    else if (c == ')')
                        lTokens.Add(new SGFToken(TokenType.CLOSEPARAN));
                    else if (c == ';')
                        lTokens.Add(new SGFToken(TokenType.SEMICOMMA));
                    else
                        if (WhitespaceCharacters.IndexOf(c) >= 0)
                            lTokens.AddWhitespace(c.ToString());
                }
            }

            return lTokens;
        }
示例#4
0
        public bool Process()
        {
            BoardSize = DefaultBoardSize;
            ReadFile lInput = new ReadFile();

            if (string.IsNullOrEmpty(InputFileName))
                lInput.OpenConsole();
            else
                lInput.OpenFile(InputFileName);

            if (lInput.IsError())
                return SetErrorMessage(lInput);

            SGFTokens lTokens = SGFScanner.Tokenize(lInput);

            if (lTokens.IsError())
                return SetErrorMessage(lTokens);

            if (RemoveWhiteSpace)
                lTokens.RemoveAllWhiteSpace();

            lTokens.ProcessProperties(ProcessProperty);

            if (Pretty)
                lTokens.Pretty();

            SaveFile lOutput = new SaveFile();

            try
            {
                if (string.IsNullOrEmpty(OutputFileName))
                    lOutput.Create();
                else
                    lOutput.Create(OutputFileName);

                if (lOutput.IsOk())
                    lTokens.ToFile(lOutput);

                if (lOutput.IsError())
                    return SetErrorMessage(lOutput);
            }
            finally
            {
                lOutput.Close();
            }
            return true;
        }