private void TestParseParentListArgumentException(String props, int offset)
 {
     try
     {
         HmdParser.ParseParentList(props, ref offset);
         Assert.Fail("Expected an ArgumentException, but didn't get one");
     }
     catch (ArgumentException e)
     {
         Console.WriteLine("Expected Exception: \"{0}\"", e.Message);
     }
 }
        private void TestParseParentList(String props, int offset, params String[] expectedParents)
        {
            int parentLength = expectedParents.Length;

            HmdParentReference[] actualParents = HmdParser.ParseParentList(props, ref offset);

            Assert.AreEqual(actualParents.Length, parentLength);

            Boolean [] actualParentalreadyMatchd = new Boolean[parentLength];
            int        lowestNonMatchedParent    = 0;

            for (int i = 0; i < parentLength; i++)
            {
                Boolean expectedParentFound = false;
                for (int j = lowestNonMatchedParent; j < parentLength; j++)
                {
                    if (actualParents[j].IDLowerCase.Equals(expectedParents[i], StringComparison.CurrentCultureIgnoreCase))
                    {
                        if (actualParentalreadyMatchd[j])
                        {
                            throw new InvalidOperationException("Found the same parent twice...what??");
                        }
                        actualParentalreadyMatchd[j] = true;

                        if (j == lowestNonMatchedParent)
                        {
                            lowestNonMatchedParent++;
                            while (lowestNonMatchedParent < parentLength && actualParentalreadyMatchd[lowestNonMatchedParent])
                            {
                                lowestNonMatchedParent++;
                            }
                        }

                        expectedParentFound = true;
                        break;
                    }
                }
                if (!expectedParentFound)
                {
                    Assert.Fail(String.Format("The expected parent \"{0}\" was not found in the actual parents", expectedParents[i]));
                }
            }
        }
Ejemplo n.º 3
0
        private static void ParsePropertiesFile(HmdBlockIDProperties propertyBlockRoot, HmdProperties hmdProperties,
                                                HmdTokenizer tokenizer, String importPath, Boolean isPImportFile)
        {
            TextWriter debugOutput = HmdDebug.DebugOutput;

            if (debugOutput == null)
            {
                debugOutput = TextWriter.Null;
            }

            if (propertyBlockRoot == null)
            {
                throw new ArgumentNullException("propertyBlockRoot");
            }

            //
            // TODO: What about pimport files? Should they require a %props block? If not, then can they put it anyways? I'll need to add another argument to this argument list
            //       that will say whether or not it is a PImport so it can gracefully end on EOF instead of '}' (close brace)
            //

            //
            // TODO: MAKE SURE YOU DISALLOW SPECIFYING %PROPS VALUE ID in a BLOCK TWICE!!!
            //
            //
            HmdBlockIDProperties         currentParent = propertyBlockRoot;
            Stack <HmdBlockIDProperties> parentStack   = new Stack <HmdBlockIDProperties>();

            while (true)
            {
                HmdGlobalToken token = tokenizer.NextGlobalToken();

                //
                // Check for EOF
                //
                if (token.type == HmdGlobalTokenType.EOF)
                {
                    if (isPImportFile && parentStack.Count <= 0)
                    {
                        return;
                    }
                    else
                    {
                        throw new FormatException("Reached EOF inside a %props block directive");
                    }
                }

                if (token.type == HmdGlobalTokenType.ID)
                {
                    String idString = token.text;

                    Boolean isBlockID;
                    Boolean isEmptyID = tokenizer.NextIDType(out isBlockID);

                    if (isEmptyID)
                    {
                        // this just means the value ID has all the defaults
                        debugOutput.WriteLine("EmptyProp ID: {0}", idString);

                        HmdValueIDProperties valueIDProperties = new HmdValueIDProperties(idString, hmdProperties.defaultCountProperty,
                                                                                          hmdProperties.defaultHmdType, null, currentParent, null);
                        if (!valueIDProperties.DirectParentIsOverriden)
                        {
                            currentParent.AddDirectChildWithNoParentOverrideList(valueIDProperties);
                        }
                        hmdProperties.AddPropertiesFromDefinition(valueIDProperties);
                    }
                    else if (!isBlockID)
                    {
                        debugOutput.WriteLine("ValProp ID: {0}", idString);

                        String nextValue = tokenizer.NextValue();
                        debugOutput.WriteLine("ValProp   : {0}", nextValue);

                        HmdValueIDProperties valueIDProperties = HmdParser.ParseValueProperties(idString, nextValue, currentParent, hmdProperties);
                        if (!valueIDProperties.DirectParentIsOverriden)
                        {
                            currentParent.AddDirectChildWithNoParentOverrideList(valueIDProperties);
                        }
                        hmdProperties.AddPropertiesFromDefinition(valueIDProperties);
                    }
                    else
                    {
                        debugOutput.WriteLine("BlkProp ID: {0}", idString);

                        HmdBlockIDProperties blockIDProperties = new HmdBlockIDProperties(idString, hmdProperties.defaultCountProperty, currentParent);
                        // wait to add this child to the current parent so we know whether or not it's default parent is overriden
                        hmdProperties.AddPropertiesFromDefinition(blockIDProperties);

                        parentStack.Push(currentParent);
                        currentParent = blockIDProperties;
                    }
                }
                else if (token.type == HmdGlobalTokenType.Directive)
                {
                    String directiveID = token.text;

                    Boolean isBlockID;
                    Boolean isEmptyID = tokenizer.NextIDType(out isBlockID);

                    if (isEmptyID)
                    {
                        throw new NotImplementedException();
                    }
                    else if (!isBlockID)
                    {
                        //
                        // TODO: Props Blocks should not allow %import directives, they should only allow %pimport directives
                        //
                        if (token.text.Equals("import", StringComparison.CurrentCulture))
                        {
                            throw new FormatException("%import directive not allowed inside a %prop block");
                        }
                        else if (token.text.Equals("pimport", StringComparison.CurrentCulture))
                        {
                            String nextValue = tokenizer.NextValue();
                            debugOutput.WriteLine("%PImport  : {0}", nextValue);

                            String importFileAndPathName = Path.Combine(importPath, nextValue);

                            using (FileStream importStream = new FileStream(importFileAndPathName, FileMode.Open))
                            {
                                debugOutput.WriteLine("File Start: {0}", importFileAndPathName);
                                ParsePropertiesFile(currentParent, hmdProperties, new HmdTokenizer(new StreamReader(importStream), 1), importPath, false);
                                debugOutput.WriteLine("File End  : {0}", importFileAndPathName);
                            }
                        }
                        else if (token.text.Equals("enum", StringComparison.CurrentCulture))
                        {
                            String nextValue = tokenizer.NextValue();
                            debugOutput.WriteLine("%Enum     : {0}", nextValue);

                            hmdProperties.AddEnum(new HmdEnum(nextValue));
                        }
                        else if (token.text.Equals("props", StringComparison.CurrentCulture))
                        {
                            if (parentStack.Count <= 0)
                            {
                                throw new FormatException("You can't specify a %props value on the root");
                            }
                            String nextValue = tokenizer.NextValue();
                            debugOutput.WriteLine("%Props    : {0}", nextValue);

                            HmdParser.ParseAndOverrideBlockProperties(currentParent, nextValue);
                        }
                        else
                        {
                            throw new Exception(String.Format("Parser (line {0}): Unrecognized value directive \"{1}\"",
                                                              token.line, token.text));
                        }
                    }
                    else
                    {
                        if (token.text.Equals("props", StringComparison.CurrentCulture))
                        {
                            throw new FormatException("Right now this is just weird, why do you have a %props block inside a props block (Maybe I'll let this slide later)?");
                            debugOutput.WriteLine("Block ID  : %props Directive");

                            if (hmdProperties == null)
                            {
                                debugOutput.WriteLine("Not Parsing %props Directive Block ID...");
                                throw new NotImplementedException("Haven't implemented the feature to parse without doing the props block");
                            }
                            else
                            {
                                ParsePropertiesFile(hmdProperties.root, hmdProperties, tokenizer, importPath, false);
                            }
                        }
                        else if (token.text.Equals("group", StringComparison.CurrentCulture))
                        {
                            throw new NotImplementedException();
                        }
                        else
                        {
                            throw new Exception(String.Format("Parser (line {0}): Unrecognized block directive \"{1}\"",
                                                              token.line, token.text));
                        }
                    }
                }
                else if (token.type == HmdGlobalTokenType.CloseBrace)
                {
                    if (parentStack.Count <= 0)
                    {
                        debugOutput.WriteLine("%Props End:");
                        return;
                    }

                    debugOutput.WriteLine("Block End : {0}", currentParent.idOriginalCase);

                    HmdBlockIDProperties temp = currentParent;
                    currentParent = parentStack.Pop();
                    if (!temp.DirectParentIsOverriden)
                    {
                        currentParent.AddDirectChildWithNoParentOverrideList(temp);
                    }
                }
                else
                {
                    throw new FormatException(String.Format("Parser (line {0}): Unexpected token {1}", token.line, token));
                }
            }
        }
Ejemplo n.º 4
0
 private static HmdValueIDProperties CallParserWithDefaults(String propertyString, HmdProperties hmdProperties)
 {
     return(HmdParser.ParseValueProperties(String.Empty, propertyString, hmdProperties.root, hmdProperties));
 }