Exemplo n.º 1
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;
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
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);
        }