Esempio n. 1
0
 public static void Main(string[] args)
 {
     List<string> str = new List<string>();
     EnhancedGrammar grammar = new EnhancedGrammar
     {
         (Rule)new RuleGenerator("S'")
         {
             { "Rest", null },
         },
         (Rule)new RuleGenerator("Rest")
         {
             { "Instructions", null },
         },
         (Rule)new RuleGenerator("Instructions")
         {
             { "Instructions Instruction", (x) => { str.Add(x[1].ToString()); return null; } },
             { "Instruction", (x) => { str.Add(x[0].ToString()); return null; } },
         },
         (Rule)new RuleGenerator("Instruction")
         {
             { "Predicate Instruction'", (x) => string.Format(x[1].ToString(), x[0].ToString()) },
             { "Instruction'", (x) => string.Format(x[0].ToString(), "p0") },
         },
         (Rule)new RuleGenerator("Instruction'")
         {
             { "id", (x) => string.Format("({0} {1})", x[0].ToString(), "{0}") },
             { "id id", (x) => string.Format("({0} {1} {2})", x[0].ToString(), "{0}", x[1].ToString()) },
             { "id id , id", (x) => string.Format("({0} {1} {2} {3})", x[0].ToString(), "{0}", x[1].ToString(), x[3].ToString()) },
             { "id id = SourceRegisters", (x) => string.Format("({0} {1} {2} {3})",
                     x[0].ToString(), "{0}", x[1].ToString(), x[3].ToString()) },
             { "id id , id = SourceRegisters", (x) => string.Format("({0} {1} {2} {3} {4})", x[0].ToString(), "{0}", x[1].ToString(), x[3].ToString(), x[5].ToString()) },
         },
         (Rule)new RuleGenerator("SourceRegisters")
         {
             { "id", (x) => x[0].ToString().Replace(";;","") },
             { "id , id", (x) => string.Format("{0} {1}", x[0].ToString(), x[2].ToString()).Replace(";;","") },
             { "id , id , id", (x) => string.Format("{0} {1} {2}", x[0].ToString(), x[2].ToString(), x[4].ToString().Replace(";;","")) },
             { "id , id , id , id", (x) => string.Format("{0} {1} {2} {3}", x[0].ToString(), x[2].ToString(), x[4].ToString(), x[6].ToString().Replace(";;","")) },
             { "id , id , id , id , id", (x) => string.Format("{0} {1} {2} {3} {4}", x[0].ToString(), x[2].ToString(), x[4].ToString(), x[6].ToString(), x[8].ToString().Replace(";;","")) },
         },
         (Rule)new RuleGenerator("Predicate")
         {
             { "( id )", (x) => x[1] },
         },
     };
     EnhancedLR1ParsableLanguage elpl = new EnhancedLR1ParsableLanguage(
             "ItaniumConverter",
             "1.0",
             "\\[?[\\.\\-\\+0-9a-zA-Z_$]([\\.0-9a-zA-Z_$])*\\]?",
             new Comment[]
             {
             },
             new Symbol[]
             {
                 new Symbol('\n', "Newline", "<new-line>"),
                 new Symbol(' ', "Space", "<space>"),
                 new Symbol('\t', "Tab", "<tab>"),
                 new Symbol('\r', "Carriage Return", "<cr>"),
             },
             new RegexSymbol[]
             {
             //	new GenericInteger("Number", "num"),
             },
             new Keyword[]
             {
                 new Keyword("="),
                 new Keyword(","),
                 new Keyword("("),
                 new Keyword(")"),
             },
             LexicalExtensions.DefaultTypedStringSelector,
             grammar,
             "$",
             (x) =>
             {
                 StringBuilder sb = new StringBuilder();
                 foreach(var v in str)
                     sb.AppendLine(v.ToString());
                 return sb.ToString();
             },
             true,
             IsValid);
     string msg = Console.In.ReadToEnd().Replace(";;","").Replace("\r\n","\n");
     var result = elpl.Parse(msg).ToString().Replace("[","{").Replace("]","}");
     Console.WriteLine(result);
 }
		public OmnicronLanguage()
		{
			dataStack = new Stack<object>(); 
			EnhancedGrammar grammar = new EnhancedGrammar
			{
				new Rule("S'")
				{
					new Production { "Block" },
				},
					new Rule("Block")
					{
						new Production { "Rest", "return" },
					},
					new Rule("Rest")
					{
						new Production { "BlockStatements", },
					},
					new Rule("BlockStatements" )
					{
						new Production{ "BlockStatements", "BlockStatement" }, 
						new Production{ "BlockStatement" },
					},
					new Rule("BlockStatement")
					{
						new Production { "Word" },
					},
					new Rule("Word")
					{
						new Production { "Action" },
						new Production { "Type" }, 
					},
					new Rule("Type")
					{
						new Production((x) => { Push(int.Parse(x[0].ToString())); return EmptyArray; }) { "int" },
						new Production((x) => { Push(long.Parse(x[0].ToString())); return EmptyArray; }) { "long" },
						new Production((x) => { Push(float.Parse(x[0].ToString())); return EmptyArray; }) { "float" },
						new Production((x) => { Push(double.Parse(x[0].ToString())); return EmptyArray; }) { "double" },
						new Production((x) => { Push(x[0].ToString().Replace("\"",string.Empty)); return EmptyArray; }) { "string-literal" },
						new Production((x) => { Push(x[0].ToString().Replace("\"",string.Empty)); return EmptyArray; }) { "id" },
						new Production((x) => { Push(true); return EmptyArray; }) { "true" }, 
						new Production((x) => { Push(false); return EmptyArray; }) { "false" }, 
					},
					new Rule("Action")
					{
						new Production(NewItem) { "new" },
						new Production(ImbueItem) { "imbue" },
						new Production(NewPoint) { "point" },
						new Production(NewSize) { "size" },
					},

			};
			language = new EnhancedLR1ParsableLanguage(
					"Dynamic Form Constructor Language",
					"1.0.0.0",
					IdSymbol.DEFAULT_IDENTIFIER,
					new Comment[]
					{
					new Comment("single-line-comment", "--", "\n", "\""),
					},
					new Symbol[]
					{
					new Symbol('\n', "Newline", "<new-line>"),
					new Symbol(' ', "Space", "<space>" ),
					new Symbol('\t', "Tab", "<tab>"),
					new Symbol('\r', "Carriage Return", "<cr>"),
					},
					new RegexSymbol[]
					{
					new StringLiteral(),
					new CharacterSymbol(),
					new GenericFloatingPointNumber("Single Precision Floating Point Number", "[fF]", "single"),
					new GenericFloatingPointNumber("Double Precision Floating Point Number", "double"),
					new GenericInteger("Long", "[Ll]", "long"),
					new GenericInteger("Int", "int"),
					},
					new Keyword[]
					{
						new Keyword("new"),
						new Keyword("imbue"),
						new Keyword("point"),
						new Keyword("size"),
						new Keyword("extract"),
						new Keyword("true"),
						new Keyword("false"),
						new Keyword("return"),
					},
					LexicalExtensions.DefaultTypedStringSelector,
					grammar,
					"$",
					(x) => 
					{
						object result = dataStack.Pop();
						if(result is DynamicForm)
						{
							DynamicForm d = (DynamicForm)result;
							d.ResumeLayout(false);
							return d;
						}
						else
						{
							throw new ArgumentException("A form should always be returned from any of these scripts!");
						}
					},
					true,
					IsValid);
		}
        public FormConstructionLanguage()
        {
            dataStack = new Stack <object>();
            EnhancedGrammar grammar = new EnhancedGrammar
            {
                new Rule("S'")
                {
                    new Production {
                        "Block"
                    },
                },
                new Rule("Block")
                {
                    new Production {
                        "Rest", "return"
                    },
                },
                new Rule("Rest")
                {
                    new Production {
                        "BlockStatements",
                    },
                },
                new Rule("BlockStatements")
                {
                    new Production {
                        "BlockStatements", "BlockStatement"
                    },
                    new Production {
                        "BlockStatement"
                    },
                },
                new Rule("BlockStatement")
                {
                    new Production {
                        "Word"
                    },
                },
                new Rule("Word")
                {
                    new Production {
                        "Action"
                    },
                    new Production {
                        "Type"
                    },
                },
                new Rule("Type")
                {
                    new Production((x) => { Push(int.Parse(x[0].ToString())); return(EmptyArray); })
                    {
                        "int"
                    },
                    new Production((x) => { Push(long.Parse(x[0].ToString())); return(EmptyArray); })
                    {
                        "long"
                    },
                    new Production((x) => { Push(float.Parse(x[0].ToString())); return(EmptyArray); })
                    {
                        "float"
                    },
                    new Production((x) => { Push(double.Parse(x[0].ToString())); return(EmptyArray); })
                    {
                        "double"
                    },
                    new Production((x) => { Push(x[0].ToString().Replace("\"", string.Empty)); return(EmptyArray); })
                    {
                        "string-literal"
                    },
                    new Production((x) => { Push(x[0].ToString().Replace("\"", string.Empty)); return(EmptyArray); })
                    {
                        "id"
                    },
                    new Production((x) => { Push(true); return(EmptyArray); })
                    {
                        "true"
                    },
                    new Production((x) => { Push(false); return(EmptyArray); })
                    {
                        "false"
                    },
                },
                new Rule("Action")
                {
                    new Production(NewItem)
                    {
                        "new"
                    },
                    new Production(ImbueItem)
                    {
                        "imbue"
                    },
                    new Production(NewPoint)
                    {
                        "point"
                    },
                    new Production(NewSize)
                    {
                        "size"
                    },
                },
            };

            language = new EnhancedLR1ParsableLanguage(
                "Dynamic Form Constructor Language",
                "1.0.0.0",
                IdSymbol.DEFAULT_IDENTIFIER,
                new Comment[]
            {
                new Comment("single-line-comment", "--", "\n", "\""),
            },
                new Symbol[]
            {
                new Symbol('\n', "Newline", "<new-line>"),
                new Symbol(' ', "Space", "<space>"),
                new Symbol('\t', "Tab", "<tab>"),
                new Symbol('\r', "Carriage Return", "<cr>"),
            },
                new RegexSymbol[]
            {
                new StringLiteral(),
                new CharacterSymbol(),
                new GenericFloatingPointNumber("Single Precision Floating Point Number", "[fF]", "single"),
                new GenericFloatingPointNumber("Double Precision Floating Point Number", "double"),
                new GenericInteger("Long", "[Ll]", "long"),
                new GenericInteger("Int", "int"),
            },
                new Keyword[]
            {
                new Keyword("new"),
                new Keyword("imbue"),
                new Keyword("point"),
                new Keyword("size"),
                new Keyword("extract"),
                new Keyword("true"),
                new Keyword("false"),
                new Keyword("return"),
            },
                LexicalExtensions.DefaultTypedStringSelector,
                grammar,
                "$",
                (x) =>
            {
                object result = dataStack.Pop();
                if (result is DynamicForm)
                {
                    DynamicForm d = (DynamicForm)result;
                    d.ResumeLayout(false);
                    return(d);
                }
                else
                {
                    throw new ArgumentException("A form should always be returned from any of these scripts!");
                }
            },
                true,
                IsValid);
        }