Ejemplo n.º 1
0
        protected static void ParseSymbolSet(ref Grammar.Grammar grammar, string line)
        {
            string backup = line;
            LiteralGrammarElement SymbolKeyword = new LiteralGrammarElement("SymbolSet ")
            {
                Name = "SymbolSetKeyword"
            };

            if (SymbolKeyword.Validate(ref line, true).Result)
            {
                line = line.Trim();
                string exception = "";
                if (line.Contains("except"))
                {
                    int idxx = line.IndexOf("except");
                    exception = line.Substring(idxx + 7).Trim();
                    line      = line.Substring(0, idxx);
                }
                string    templine   = line;
                SymbolSet Identifier = SymbolSet.FromType(SymbolSetPredefinedType.AlphaNumeric);
                Identifier.Add(new Symbol('_'));
                Identifier.Add(new Symbol('$'));
                VariableLengthGrammarElement SymbolName = new VariableLengthGrammarElement(VariableLengthGrammarElementType.Plus, Identifier)
                {
                    Name = "Identifier"
                };
                if (SymbolName.Validate(ref line, true).Result)
                {
                    string name = templine.Substring(0, templine.IndexOf(line)).Trim();
                    line = line.Trim();
                    LiteralGrammarElement EqualsSign = new LiteralGrammarElement("=")
                    {
                        Name = "EqualsSign"
                    };
                    if (EqualsSign.Validate(ref line, true).Result)
                    {
                        line = line.Trim();
                        #region Symbol Set %[...]
                        if (line.StartsWith("%"))
                        {
                            line = line.Substring(1);
                            line = line.Trim();
                            if (line.StartsWith("[") && line.EndsWith("]"))
                            {
                                line = line.Substring(1, line.Length - 2);
                                SymbolSet set = new SymbolSet();
                                foreach (char c in line)
                                {
                                    set.Add(new Symbol(c));
                                }
                                foreach (char c in exception)
                                {
                                    set.RemoveSymbol(new Symbol(c));
                                }
                                grammar.AddSymbolSet(name, set);
                                LiteralGrammarElement none = new LiteralGrammarElement();
                                none.AddElement(set);
                                VariableLengthGrammarElement star = new VariableLengthGrammarElement(VariableLengthGrammarElementType.Star, set);
                                VariableLengthGrammarElement plus = new VariableLengthGrammarElement(VariableLengthGrammarElementType.Plus, set);
                                grammar.AddTerminal(name, none);
                                grammar.AddTerminal(name + "*", star);
                                grammar.AddTerminal(name + "+", plus);
                            }
                        }
                        #endregion
                        #region Symbol Set [...]
                        else if (line.StartsWith("[") && line.EndsWith("]"))
                        {
                            line = line.Substring(1, line.Length - 2);
                            SymbolSet set = GetSetValues(line);
                            foreach (char c in exception)
                            {
                                set.RemoveSymbol(new Symbol(c));
                            }
                            grammar.AddSymbolSet(name, set);
                            LiteralGrammarElement none = new LiteralGrammarElement();
                            none.AddElement(set);
                            VariableLengthGrammarElement star = new VariableLengthGrammarElement(VariableLengthGrammarElementType.Star, set);
                            VariableLengthGrammarElement plus = new VariableLengthGrammarElement(VariableLengthGrammarElementType.Plus, set);
                            grammar.AddTerminal(name, none);
                            grammar.AddTerminal(name + "*", star);
                            grammar.AddTerminal(name + "+", plus);
                        }
                        #endregion
                        #region Symbol Set with Predefined/Predeclared Sets
                        else
                        {
                            string[] types = Enum.GetNames(typeof(SymbolSetPredefinedType));
                            LiteralGrammarElement  NewKeyword    = new LiteralGrammarElement("new");
                            LiteralGrammarElement  SetTypes      = new LiteralGrammarElement(types);
                            CompoundGrammarElement PreDefinedSet = new CompoundGrammarElement(NewKeyword, SetTypes)
                            {
                                Delimiters = " \n\r"
                            };
                            string[]  values = line.Split(',');
                            SymbolSet set    = new SymbolSet();
                            foreach (string valueq in values)
                            {
                                string value = valueq.Trim();
                                string iv    = value;
                                string iv2   = value;
                                if (PreDefinedSet.Validate(ref iv2, true).Result)
                                {
                                    NewKeyword.Validate(ref value, true);
                                    SymbolSetPredefinedType type = (SymbolSetPredefinedType)Enum.Parse(typeof(SymbolSetPredefinedType), value);
                                    SymbolSet temp = SymbolSet.FromType(type);
                                    foreach (Symbol s in temp)
                                    {
                                        set.Add(s);
                                    }
                                }
                                else if (SymbolName.Validate(ref iv, true).Result)
                                {
                                    if (grammar.Symbols.Keys.Contains(value))
                                    {
                                        set.Add(grammar.Symbols[value]);
                                    }
                                    else if (grammar.SymbolSets.Keys.Contains(value))
                                    {
                                        foreach (Symbol sym in grammar.SymbolSets[value])
                                        {
                                            set.Add(sym);
                                        }
                                    }
                                    else
                                    {
                                        throw new InfinityGrammarScriptParseError("Symbol not declared or found - " + value, backup);
                                    }
                                }
                                else
                                {
                                    throw new InfinityGrammarScriptParseError("Invalid parameters passed - " + value, backup);
                                }
                            }
                            foreach (char c in exception)
                            {
                                set.RemoveSymbol(new Symbol(c));
                            }
                            grammar.SymbolSets.Add(name, set);
                            LiteralGrammarElement none = new LiteralGrammarElement();
                            none.AddElement(set);
                            VariableLengthGrammarElement star = new VariableLengthGrammarElement(VariableLengthGrammarElementType.Star, set);
                            VariableLengthGrammarElement plus = new VariableLengthGrammarElement(VariableLengthGrammarElementType.Plus, set);
                            grammar.AddTerminal(name, none);
                            grammar.AddTerminal(name + "*", star);
                            grammar.AddTerminal(name + "+", plus);
                        }
                        #endregion
                    }
                    else
                    {
                        throw new InfinityGrammarScriptParseError("Syntax requires a symbol value assignment, which is missing.", backup);
                    }
                }
                else
                {
                    throw new InfinityGrammarScriptParseError("The Symbol name specified was not valid. Names given can have Letters, Numbers and $ and _.", backup);
                }
            }
            else
            {
                throw new InfinityGrammarScriptParseError("Not a SymbolSet definition.", backup);
            }
        }