Пример #1
0
        public Parser(string[] fileAsStringArray)
        {
            Lexer lexedFile = new Lexer(fileAsStringArray);

            this.IniFileDictionary = new Dictionary <string, Dictionary <string, string> >();
            ParseStatePatternHandle statePatternHandle = new ParseStatePatternHandle(this.IniFileDictionary);

            statePatternHandle.CurrentState = new ParseStateFileStart();

            foreach (LexerTokenNode currentToken in lexedFile.Tokens)
            {
                try
                {
                    statePatternHandle.StateRequest(currentToken);
                }
                catch (Exception exception)
                {
                    Console.WriteLine("Exception in file - " + fileName);
                    Console.WriteLine("INI Parser Exception - " + exception.Message);
                    throw exception;
                }
            }

            IniFileDictionary = statePatternHandle.IniFileDictionary;
        }
Пример #2
0
        public void EvaluateState(ParseStatePatternHandle statePatternHandle)
        {
            try
            {
                switch (statePatternHandle.CurrentToken.TokenType)
                {
                case LexerTokenNode.Token.OpenSquareBrace:
                    if (statePatternHandle.CurrentToken.CharacterNumber == 1)
                    {
                        statePatternHandle.CurrentState = new ParseStateReadPrimeKey();
                    }
                    else
                    {
                        throw new Exception("First non-white space or comment in INI file must be an open square brace.");
                    }
                    break;

                case LexerTokenNode.Token.WhiteSpace:     //Ignore white space at the top - keep reading
                    statePatternHandle.CurrentState = new ParseStateFileStart();
                    break;

                default:
                    break;
                }
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }
Пример #3
0
        public void EvaluateState(ParseStatePatternHandle statePatternHandle)
        {
            try
            {
                switch (statePatternHandle.CurrentToken.TokenType)
                {
                case LexerTokenNode.Token.EndOfLine:
                    statePatternHandle.CurrentState = new ParseStateLineStart();
                    break;

                case LexerTokenNode.Token.CloseSquareBrace:
                    if (statePatternHandle.StringBuffer != "")
                    {
                        statePatternHandle.CurrentPrimaryKey = statePatternHandle.StringBuffer;
                        statePatternHandle.IniFileDictionary.Add(statePatternHandle.CurrentPrimaryKey, new Dictionary <string, string>());
                        statePatternHandle.StringBuffer = "";
                        statePatternHandle.CurrentState = new ParseStateReadPrimeKey();
                    }
                    else
                    {
                        throw new Exception("Cannot have an empty primary key.");
                    }
                    break;

                case LexerTokenNode.Token.String:
                    statePatternHandle.StringBuffer = statePatternHandle.CurrentToken.TokenValue;
                    statePatternHandle.CurrentState = new ParseStateReadPrimeKey();
                    break;

                case LexerTokenNode.Token.WhiteSpace:
                    if (statePatternHandle.StringBuffer == "")
                    {
                        statePatternHandle.CurrentState = new ParseStateReadPrimeKey();
                    }
                    else
                    {
                        statePatternHandle.StringBuffer = statePatternHandle.CurrentToken.TokenValue;
                        statePatternHandle.CurrentState = new ParseStateReadPrimeKey();
                    }
                    break;

                default:
                    throw new Exception("Unexpected character in primary key of INI file.");
                }
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }
 public void EvaluateState(ParseStatePatternHandle statePatternHandle)
 {
     try
     {
         if (statePatternHandle.CurrentToken.TokenType == LexerTokenNode.Token.EndOfLine ||
             statePatternHandle.CurrentToken.TokenType == LexerTokenNode.Token.EndOfFile)
         {
             if (statePatternHandle.IniFileDictionary.ContainsKey(statePatternHandle.CurrentPrimaryKey))
             {
                 if (statePatternHandle.IniFileDictionary[statePatternHandle.CurrentPrimaryKey].ContainsKey(statePatternHandle.CurrentSecondaryKey))
                 {
                     statePatternHandle.IniFileDictionary[statePatternHandle.CurrentPrimaryKey][statePatternHandle.CurrentSecondaryKey] = statePatternHandle.StringBuffer;
                     statePatternHandle.StringBuffer = "";
                     statePatternHandle.CurrentState = new ParseStateLineStart();
                 }
                 else
                 {
                     throw new Exception("Cannot find secondary key " + statePatternHandle.CurrentSecondaryKey + ".");
                 }
             }
             else
             {
                 throw new Exception("Cannot find primary key " + statePatternHandle.CurrentPrimaryKey + ".");
             }
         }
         else
         {
             statePatternHandle.StringBuffer = statePatternHandle.StringBuffer + statePatternHandle.CurrentToken.TokenValue;
             statePatternHandle.CurrentState = new ParseStateReadSubkeyValue();
         }
     }
     catch (Exception exception)
     {
         throw exception;
     }
 }
Пример #5
0
        public void EvaluateState(ParseStatePatternHandle statePatternHandle)
        {
            try
            {
                switch (statePatternHandle.CurrentToken.TokenType)
                {
                case LexerTokenNode.Token.EndOfFile:
                    statePatternHandle.CurrentState = null;
                    break;

                case LexerTokenNode.Token.EndOfLine:
                    statePatternHandle.CurrentState = new ParseStateLineStart();
                    break;

                case LexerTokenNode.Token.WhiteSpace:
                    if (statePatternHandle.StringBuffer == "")
                    {
                        statePatternHandle.CurrentState = new ParseStateLineStart();
                    }
                    else
                    {
                        statePatternHandle.StringBuffer = statePatternHandle.StringBuffer + statePatternHandle.CurrentToken.TokenValue;
                        statePatternHandle.CurrentState = new ParseStateLineStart();
                    }
                    break;

                case LexerTokenNode.Token.OpenSquareBrace:
                    statePatternHandle.CurrentState = new ParseStateReadPrimeKey();
                    break;

                case LexerTokenNode.Token.String:
                    statePatternHandle.StringBuffer = statePatternHandle.CurrentToken.TokenValue;
                    statePatternHandle.CurrentState = new ParseStateLineStart();
                    break;

                case LexerTokenNode.Token.EqualSign:
                    if (statePatternHandle.StringBuffer == "")
                    {
                        throw new Exception("Subkey must contain at least one non-whitespace character.");
                    }
                    else
                    {
                        if (statePatternHandle.IniFileDictionary.ContainsKey(statePatternHandle.CurrentPrimaryKey))
                        {
                            statePatternHandle.CurrentSecondaryKey = statePatternHandle.StringBuffer;
                            statePatternHandle.IniFileDictionary[statePatternHandle.CurrentPrimaryKey].Add(statePatternHandle.StringBuffer, "");
                            statePatternHandle.StringBuffer = "";
                            statePatternHandle.CurrentState = new ParseStateReadSubkeyValue();
                        }
                        else
                        {
                            throw new Exception("Cannot find primary key " + statePatternHandle.CurrentPrimaryKey + " in dictionary.");
                        }
                    }
                    break;

                default:
                    throw new Exception("Unexpected character at the start of a line.");
                }
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }