Exemplo n.º 1
0
 private void ExecutionAcceptKeystroke()
 {
     // Send the key that was just typed (in pause mode) to the parser for execution.
     // TODO: But then the two parsers get desynchronized???
     // TODO: But what if the current message is not a keystroke?
     upParser.Parse(((KeystrokeMessage)messageQueue.Dequeue()).Tag);
 }
Exemplo n.º 2
0
        public void Parse(string text)
        {
            try
            {
                var scanner = new Scanner();
                scanner.SetSource(text, 0);

                var parser = new Parser.Parser(scanner);

                var parseSuccess = parser.Parse();
                if (parseSuccess)
                {
                    ParsingCompleted(this, parser.root);
                }
                else
                {
                    ParsingErrored(this, null);
                }
            }
            catch (LexException ex)
            {
                ParsingLexErrored(this, ex);
            }
            catch (SyntaxException ex)
            {
                ParsingSyntaxErrored(this, ex);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Executes all the statements in the script.
        /// </summary>
        public override PhaseResult Execute(PhaseContext phaseCtx)
        {
            var script = phaseCtx.ScriptText;
            var memory = phaseCtx.Ctx.Memory;

            var runResult = LangHelper.Execute(() =>
            {
                this.Ctx.Limits.CheckScriptLength(script);
                _parser.Parse(script, memory);

                if (phaseCtx.Nodes == null)
                {
                    phaseCtx.Nodes = new List <Expr>();
                }

                if (phaseCtx.NodesStack == null)
                {
                    phaseCtx.NodesStack = new List <List <Expr> >();
                }

                // 1. Keep track of all the statements.
                phaseCtx.Nodes.AddRange(_parser.Statements);

                // 2. Keep track of the each individual set of statements ( from multiple scripts )
                phaseCtx.NodesStack.Add(_parser.Statements);
            });

            return(new PhaseResult(runResult));
        }
Exemplo n.º 4
0
        public override string[] Build()
        {
            var options = new ParserOption();
            var currentAssembly = GetType().Assembly;
            var name = new FileInfo(currentAssembly.Location).Name;
            var namespaces = currentAssembly.GetTypes().Select(t => t.Namespace).ToArray();
            options.Namesapces.AddRange(namespaces);
            options.References.Add("System.dll");
            options.References.Add(@".\" + name);
            options.SourceCode = Parameters.Template;
            options.VariableCollection.Add("Namespace",
                                           string.IsNullOrEmpty(Parameters.Namespace) ? "" : Parameters.Namespace);
            var tmp = new List<string>();
            foreach (Table table in Parameters.DataBase.Tables)
            {
                options.VariableParameter = table;
                options.StatementParameters = new object[] {Parameters.DataBase, table};

                var parser = new Parser.Parser(options);
                string temp = parser.Parse(ParserType.XCODER);
                string fileName = Path.Combine(Parameters.OutputDirectory.FullName, table.Name) + Extension;
                File.WriteAllText(fileName, temp);
                tmp.Add(fileName);
            }
            return tmp.ToArray();
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            var message       = @"MSH|^~\&|EPIC|ANCL||IRIS|20170410102836|HJ123|ORM^O01|254|T|2.4||||||||
PID|1|ITCC20170410^^^^MRN|ITCC20170410||DOE^JOHN||19581012|M||W|100 FOAMY WAY^APT A^BELLEVUE^WA^98104^US^^^KING|KING|(425)444-5555^^PH^^^425^4445555|||M||1234|111-22-3333|||
PV1||O|POC01|||||||||||||||||||||||||||||||||||||||||20170410082742|||||
ORC|NW|2017041006^EPC|||||^^^20170410^^R||20170410150905|HJ123^HOPKINS^JOHNS||OP0001^DOE^JACK^^^^^^SERDOTON^^^^SERDOTON|DFM^^^20403^^^^^TEST CLINIC|(425)123-4567^^^^^425^1234567||||H48033^H48033^^20403001^TEST CLINIC|||||||||||O|||
OBR|1|2017041006^EPC||92250^FUNDUS PHOTOGRAPHY^EAP^^FUNDAL PHOTO||20170410|||||L|||||OP0001^DOE^JACK^^^^^^SERDOTON^^^^SERDOTON|(425)598-6900^^^^^425^5986900|||||||OPHTHALMOLOG|||^^^20170410^^R|||||||||20170410||||||||
DG1|1|ICD-10|E11.41^Type 2 diabetes mellitus with diabetic mononeuropathy^I10|Type 2 diabetes mellitus with diabetic mononeuropathy
DG1|2|ICD-9|250.0|DIABETES MELLITUS WITHOUT MENTION OF COMPLICATION, TYPE II OR UNSPECIFIED TYPE, NOT STATED AS UNCONTROLLED";
            var parser        = new Parser.Parser();
            var parsedMessage = parser.Parse(message);
            var structure     = parsedMessage.GetStructureName();

            System.Console.WriteLine($"Message Type: {parsedMessage.GetStructureName()}");
            if (structure == "ORM_O01")
            {
                var orm = parsedMessage as ORM_O01;
                System.Console.WriteLine(
                    $"Patient Id: {orm.PATIENT.PID.PatientID.ID}.  Patient Id Type: {orm.PATIENT.PID.PatientID.IdentifierTypeCode}");
                var patientName = orm.PATIENT.PID.GetPatientName();
                foreach (var name in patientName)
                {
                    System.Console.WriteLine($"Patient Last Name: {name.FamilyName.Surname}.  Patient First Name: {name.GivenName}");
                }
            }
        }
Exemplo n.º 6
0
        public void TestExpressionFunctionReturnTypeBinding()
        {
            const string src = "namespace MyNamespace" +
                               "{" +
                               "    class MyClass" +
                               "    {" +
                               "        func Add(int a, int b) -> return new MyClass2()" +
                               "    }" +
                               "" +
                               "    class MyClass2" +
                               "    {" +
                               "        " +
                               "    }" +
                               "}";

            var lexer = new Lexer.Lexer();
            var tokens = lexer.Lex(src);
            var parser = new Parser.Parser(tokens);

            var ast = parser.Parse();

            var binder = new Binder();
            var semanticModel = binder.Bind(new List<CompilationUnitSyntax> { ast }).Single();

            var boundNamespace = semanticModel.Namespaces.Single(x => x.Name == "MyNamespace");
            var expectedFunctionReturnType = boundNamespace.Types.Single(x => x.Name == "MyClass2");
            var function = boundNamespace.Types.Single(x => x.Name == "MyClass").Functions.Single(x => x.Name == "Add");
            Assert.AreSame(expectedFunctionReturnType, function.ReturnType);
        }
Exemplo n.º 7
0
 public static Parser.Parser ParseCore(this string expression, ExprBuilder builder)
 {
     Scanner scanner = new Scanner(expression);
     Parser.Parser parser = new Parser.Parser(scanner, builder);
     parser.Parse();
     return parser;
 }
Exemplo n.º 8
0
 public void TestParser()
 {
     var p = new Parser.Parser();
     Assert.AreEqual(p.RemoveComments("lol;//Comment"), "lol;");
     Assert.AreEqual(p.RemoveComments("lol;//Comment\nlol;//Comment 2\nlol/*Alex Sabala*/;"), "lol;\nlol;\nlol;");
     Assert.AreEqual(p.Parse("lol;\nlol;").Count(), 4);
 }
Exemplo n.º 9
0
        public void GraphNumeratorTest0()
        {
            var code    = @"a = 1;
                goto h;
                h: b = 1;
                goto h2;
                h2: c = 1;
                d = 1;";
            var scanner = new Scanner();

            scanner.SetSource(code, 0);
            var parser        = new Parser.Parser(scanner);
            var b             = parser.Parse();
            var astRoot       = parser.root;
            var tacodeVisitor = new TACodeVisitor();

            astRoot.Visit(tacodeVisitor);
            var tacodeInstance = tacodeVisitor.Code;

            tacodeInstance.CreateBasicBlockList();
            var cfg   = new ControlFlowGraph(tacodeInstance);
            var numer = GraphNumerator.BackOrder(cfg);

            Assert.AreEqual(0, numer.GetIndex(cfg.CFGNodes.ElementAt(0)));
            Assert.AreEqual(1, numer.GetIndex(cfg.CFGNodes.ElementAt(1)));
            Assert.AreEqual(2, numer.GetIndex(cfg.CFGNodes.ElementAt(2)));
        }
Exemplo n.º 10
0
        public static ProgramNode Parse(string s)
        {
            var lex = LexerTest.Read(s);
            var p   = new Parser.Parser();

            lex.Parser = p;
            return(p.Parse(lex).Cast <ProgramNode>());
        }
Exemplo n.º 11
0
        private Document Parse(string text, IHost host)
        {
            Parser.Parser parser = new Parser.Parser(host);
            Document      document;

            parser.Parse(text, out document);

            return(document);
        }
Exemplo n.º 12
0
        public void TestBinaryExpression()
        {
            //Test
            var testSrc     = "1+1";
            var compilation = parser.Parse(testSrc);
            var result      = evaluator.Evaluate(compilation);

            Assert.Equal(2, result);
        }
Exemplo n.º 13
0
        private Document Parse(string text)
        {
            Parser.Parser parsr = new Parser.Parser();
            Document document;

            parsr.Parse(text, out document);

            return document;
        }
Exemplo n.º 14
0
        internal static Document LoadDocument(string template)
        {
            Parser.Parser parser = new Parser.Parser();
            Document document;

            if (parser.Parse(template, out document))
            {
                return document;
            }

            throw new Exception("Unable to parse: " + parser.ErrorString);
        }
Exemplo n.º 15
0
        protected string Render(string parrot, object model, IHost host)
        {
            Parser.Parser parser = new Parser.Parser(host);
            Document document;

            parser.Parse(parrot, out document);

            DocumentRenderer renderer = new DocumentRenderer(new MemoryHost());

            StringBuilder sb = new StringBuilder();
            return renderer.Render(document, model);
        }
Exemplo n.º 16
0
        internal Document LoadDocument(Stream template)
        {
            Parser.Parser parser = new Parser.Parser();
            Document document;

            if (parser.Parse(template, out document))
            {
                return document;
            }

            throw new Exception("Unable to parse: ");
        }
Exemplo n.º 17
0
        internal Document LoadDocument(string template)
        {
            Parser.Parser parser = new Parser.Parser(_host);
            Document      document;

            if (parser.Parse(template, out document))
            {
                return(document);
            }

            throw new Exception("Unable to parse: ");
        }
Exemplo n.º 18
0
        /// <summary>
        /// Parse the input and return the root of the AST node structure.
        /// </summary>
        /// <param name="reader">inputstream retrieved by a resource loader</param>
        /// <param name="templateName">name of the template being parsed</param>
        /// <param name="dumpNamespace">flag to dump the Velocimacro namespace for this template</param>
        public SimpleNode Parse(TextReader reader, String templateName, bool dumpNamespace)
        {
            SimpleNode ast = null;

            Parser.Parser parser  = (Parser.Parser)parserPool.get();
            bool          madeNew = false;

            if (parser == null)
            {
                // if we couldn't get a parser from the pool
                // make one and log it.
                Error(
                    "Runtime : ran out of parsers. Creating new.  Please increment the parser.pool.size property. The current value is too small.");

                parser = CreateNewParser();

                if (parser != null)
                {
                    madeNew = true;
                }
            }

            // now, if we have a parser
            if (parser == null)
            {
                Error("Runtime : ran out of parsers and unable to create more.");
            }
            else
            {
                try
                {
                    // dump namespace if we are told to.  Generally, you want to
                    // do this - you don't in special circumstances, such as
                    // when a VM is getting init()-ed & parsed
                    if (dumpNamespace)
                    {
                        DumpVMNamespace(templateName);
                    }

                    ast = parser.Parse(reader, templateName);
                }
                finally
                {
                    // if this came from the pool, then put back
                    if (!madeNew)
                    {
                        parserPool.put(parser);
                    }
                }
            }
            return(ast);
        }
Exemplo n.º 19
0
        protected string Render(string parrot, object model, IHost host)
        {
            Parser.Parser parser = new Parser.Parser(host);
            Document      document;

            parser.Parse(parrot, out document);

            DocumentRenderer renderer = new DocumentRenderer(new MemoryHost());

            StringBuilder sb = new StringBuilder();

            return(renderer.Render(document, model));
        }
Exemplo n.º 20
0
        public ApplyResult Apply(BuildEnvironment env, Command command)
        {
            string id   = command.Arguments[1].GetAsText();
            string path = env.GetPath(id, "mcfunction");

            Logger.Debug($"Importing {id}...");

            var        parser     = new Parser.Parser(env);
            McFunction mcFunction = parser.Parse(id);

            mcFunction.Compile(env, false);

            return(new ApplyResult(true, mcFunction.Commands));
        }
Exemplo n.º 21
0
        public void TestClass()
        {
            var lexer = new Lexer.Lexer();
            var tokens =
                lexer.Lex(
                    string.Format(NamespaceSource, string.Format(ClassSource, string.Empty, string.Empty, string.Empty)));
            var parser = new Parser.Parser(tokens);

            var ast = parser.Parse();
            Assert.IsInstanceOf<CompilationUnitSyntax>(ast);
            Assert.IsNotEmpty(ast.Namespaces);
            Assert.IsNotEmpty(ast.Namespaces[0].Classes);
            Assert.AreEqual("ClassSample", ast.Namespaces[0].Classes[0].Name.Value);
            Assert.AreEqual("DSampleProtocol", ast.Namespaces[0].Classes[0].ProtocolName.Value);
        }
Exemplo n.º 22
0
        public async Task <ICompletion <INative> > ExecuteAsync(string code)
        {
            try
            {
                var result     = _parser.Parse(code);
                var completion = await result.ExecuteAsync(GlobalScope);

                return(completion);
            }
            catch (NativeException e)
            {
                Console.WriteLine($"Ha ocurrido un error: {e.Message}, {e.Position?.Line}-{e.Position?.Column}");
            }
            return(new Completion());
        }
Exemplo n.º 23
0
        public static SyntaxTree Analyse(ParserOptions options = null, params string[] source)
        {
            List <Parser.Program> programs  = new List <Parser.Program>();
            Tokenizer             tokenizer = new Tokenizer();

            Parser.Parser parser = new Parser.Parser();

            int index = 0;

            foreach (var item in source)
            {
                programs.AddIfNotNull(parser.Parse(tokenizer.Tokenize(item), options ?? ParserOptions.Default, index.ToString()));
                index++;
            }

            return(Analyse(options?.EntryPoint ?? 0, programs.ToArray()));
        }
Exemplo n.º 24
0
        public void TestExpressionFunction()
        {
            const string functionSource = "func FunctionSample(int a, int b, int c) -> return a * b * c";

            var lexer = new Lexer.Lexer();
            var tokens =
                lexer.Lex(
                    string.Format(
                        NamespaceSource,
                        string.Format(ClassSource, functionSource, string.Empty, string.Empty)));
            var parser = new Parser.Parser(tokens);

            var ast = parser.Parse();
            Assert.IsNotEmpty(ast.Namespaces[0].Classes[0].Functions);
            Assert.IsInstanceOf<FunctionSyntax>(ast.Namespaces[0].Classes[0].Functions[0]);
            Assert.AreEqual("FunctionSample", ast.Namespaces[0].Classes[0].Functions[0].Name.Value);
        }
Exemplo n.º 25
0
        public static SyntaxTree Analyse(ParserOptions options = null, params FileStream[] source)
        {
            List <Parser.Program> programs  = new List <Parser.Program>();
            Tokenizer             tokenizer = new Tokenizer();

            Parser.Parser parser = new Parser.Parser();

            foreach (var item in source)
            {
                using (StreamReader reader = new StreamReader(item)) {
                    programs.AddIfNotNull(parser.Parse(tokenizer.Tokenize(reader.ReadToEnd()),
                                                       options ?? ParserOptions.Default, item.Name));
                }
            }

            return(Analyse(options?.EntryPoint ?? 0, programs.ToArray()));
        }
Exemplo n.º 26
0
        public static IStatement[] GetParseResultStatements(string expression, MethodInfo[] methodInfos = null)
        {
            var lexer        = new Lexer.Lexer(expression);
            var readOnlyList = lexer.Tokenize();
            var context      = new ParserContext(
                readOnlyList,
                new Dictionary <string, CompilerType>
            {
                { "x", CompilerType.Long }, { "y", CompilerType.Long }, { "z", CompilerType.Long }
            },
                new Dictionary <string, FieldInfo>(),
                methodInfos?.ToDictionary(x => x.Name, x => x) ?? new Dictionary <string, MethodInfo>(),
                true);
            var parser = new Parser.Parser(context);
            var result = parser.Parse();

            return(result.Statements);
        }
Exemplo n.º 27
0
 public int Run()
 {
     var parser = new Parser.Parser(_parserStyle, Grouping);
     foreach (Argument argument in GetArguments())
         parser.Arguments.Add(argument);
     foreach (Option option in GetOptions())
         parser.Options.Add(option);
     foreach (Command command in GetCommands())
         parser.Commands.Add(command);
     try
     {
         ParseResult result = parser.Parse(Environment.GetCommandLineArgs().Skip(1));
         return Handle(result);
     } catch (Exception ex)
     {
         Console.WriteLine(ex);
         return -1;
     }
 }
Exemplo n.º 28
0
        public string Compile(string program)
        {
            Kernel = new List<uint>(KERNEL_SIZE);
            for (int i = 0; i < KERNEL_SIZE; i++) { Kernel.Add(0); }

            Parser.Parser parser = new Parser.Parser();
            string output;

            try
            {
                var tree = parser.Parse(program);
                output = CompileTree(tree);
            }
            catch (Exception e)
            {
                Console.WriteLine($"Error {e.Message} on line {LineNumber}");
                throw;
            }
            return output;
        }
Exemplo n.º 29
0
        public void TestFieldTypeToVariableBinding()
        {
            const string src = "namespace MyNamespace" +
                               "{" +
                               "    class MyClass" +
                               "    {" +
                               "        var field : false" +
                               "        var field2 : new MyClass2()" +
                               "        func MyFunc()" +
                               "        {" +
                               "            var i : field" +
                               "            var i2 : field2" +
                               "        }" +
                               "    }" +
                               "" +
                               "    class MyClass2" +
                               "    {" +
                               "        " +
                               "    }" +
                               "}";

            var lexer = new Lexer.Lexer();
            var tokens = lexer.Lex(src);
            var parser = new Parser.Parser(tokens);

            var ast = parser.Parse();

            var binder = new Binder();
            var semanticModel = binder.Bind(new List<CompilationUnitSyntax> { ast }).Single();

            var boundNamespace = semanticModel.Namespaces.Single(x => x.Name == "MyNamespace");
            var referencedBoundType = boundNamespace.Types.Single(x => x.Name == "MyClass2");
            var boundType = boundNamespace.Types.Single(x => x.Name == "MyClass");
            var boundFunction = (BoundFunction)boundType.Functions.Single(x => x.Name == "MyFunc");
            Assert.IsInstanceOf<BoolCompilerGeneratedType>(
                ((IBoundMember)((BoundScopeStatement)boundFunction.Statements).Statements[0]).Type);
            Assert.AreSame(
                ((IBoundMember)((BoundScopeStatement)boundFunction.Statements).Statements[1]).Type,
                referencedBoundType);
        }
Exemplo n.º 30
0
        public static void Compile(TextReader input, string output, string[] asms)
        {
            var lex = new Lexer(new SourceCodeReader(input));
            var par = new Parser.Parser();

            lex.Parser = par;
            var pgm = par.Parse(lex).Cast <ProgramNode>();

            var root = new RootNamespace();

            root.Assemblies.AddRange(asms.Map(Assembly.Load));
            Lookup.LoadType(root, typeof(string)).Name = "String";
            Lookup.LoadType(root, typeof(int)).Name    = "Int";
            Lookup.LoadType(root, typeof(long)).Name   = "Long";
            Lookup.LoadType(root, typeof(short)).Name  = "Short";
            Lookup.LoadType(root, typeof(byte)).Name   = "Byte";
            Lookup.LoadType(root, typeof(bool)).Name   = "Bool";
            Lookup.LoadType(root, typeof(double)).Name = "Double";
            Lookup.LoadType(root, typeof(float)).Name  = "Float";
            Lookup.LoadType(root, typeof(object)).Name = "Object";
            DefineNumericFunction(root, "Int");
            DefineNumericFunction(root, "Long");
            DefineNumericFunction(root, "Short");
            DefineNumericFunction(root, "Byte");
            DefineNumericFunction(root, "Double", "ldc.r8 0");
            DefineNumericFunction(root, "Float", "ldc.r4 0");
            _ = Lookup.LoadFunction(root, "print", typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }) !);
            _ = Lookup.LoadFunction(root, "print", typeof(Console).GetMethod("WriteLine", new Type[] { typeof(int) }) !);
            _ = Lookup.LoadFunction(root, "print", typeof(Console).GetMethod("WriteLine", new Type[] { typeof(long) }) !);
            _ = Lookup.LoadFunction(root, "print", typeof(Console).GetMethod("WriteLine", new Type[] { typeof(byte) }) !);
            _ = Lookup.LoadFunction(root, "print", typeof(Console).GetMethod("WriteLine", new Type[] { typeof(bool) }) !);
            _ = Lookup.LoadFunction(root, "print", typeof(Console).GetMethod("WriteLine", new Type[] { typeof(double) }) !);
            _ = Lookup.LoadFunction(root, "print", typeof(Console).GetMethod("WriteLine", new Type[] { typeof(float) }) !);
            _ = Lookup.LoadFunction(root, "+", typeof(string).GetMethod("Concat", new Type[] { typeof(string), typeof(string) }) !);

            var src = Definition.LoadProgram(root, pgm);

            Typing.TypeInference(root, src);
            CodeGenerator.Emit(root, src, output);
        }
Exemplo n.º 31
0
        public SimpleNode Parse(TextReader reader, string templateName, bool dumpNamespace)
        {
            SimpleNode result = null;

            Parser.Parser parser = (Parser.Parser) this.parserPool.get();
            bool          flag   = false;

            if (parser == null)
            {
                this.Error("Runtime : ran out of parsers. Creating new.   Please increment the parser.pool.size property. The current value is too small.");
                parser = this.CreateNewParser();
                if (parser != null)
                {
                    flag = true;
                }
            }
            if (parser != null)
            {
                try
                {
                    if (dumpNamespace)
                    {
                        this.DumpVMNamespace(templateName);
                    }
                    result = parser.Parse(reader, templateName);
                }
                finally
                {
                    if (!flag)
                    {
                        this.parserPool.put(parser);
                    }
                }
            }
            else
            {
                this.Error("Runtime : ran out of parsers and unable to create more.");
            }
            return(result);
        }
Exemplo n.º 32
0
        public ActionResult Index(string template, string model)
        {
            if (string.IsNullOrWhiteSpace(template) || string.IsNullOrWhiteSpace(model))
            {
                return(View(Tuple.Create(DefaultHtmlTemplate, DefaultModelTemplate, "")));
            }

            Parser.Parser parser = new Parser.Parser(_host);

            Type T = TypeBuilderFromJson.CreateType(Newtonsoft.Json.JsonConvert.DeserializeObject(model) as JObject);

            var modelObject = Newtonsoft.Json.JsonConvert.DeserializeObject(model, T);

            Document document;
            string   result = null;

            if (parser.Parse(template, out document))
            {
                StringBuilder sb = new StringBuilder();
                foreach (var element in document.Children)
                {
                    if (element != null)
                    {
                        var renderer = _host.DependencyResolver.Resolve <IRendererFactory>().GetRenderer(element.Name);
                        sb.AppendLine(renderer.Render(element, modelObject));
                    }
                }

                result = sb.ToString();
            }
            else
            {
                //TODO: Get this later
                result = "Oops!"; // parser.ErrorString;
            }

            return(View(Tuple.Create(template, model, result)));
        }
Exemplo n.º 33
0
        public void LoadData(string programCode)
        {
            Lexer   lexer  = new Lexer(programCode);
            IParser parser = new Parser.Parser(lexer);
            AST     root   = parser.Parse();

            AbstractSyntaxTree = root;
            IDesignExtractor designExtractor = ImplementationFactory.CreateDesignExtractor();

            designExtractor.ExtractData(root);

            Variables     = designExtractor.Variables;
            Statements    = designExtractor.Statements;
            Procedures    = designExtractor.Procedures;
            Constants     = designExtractor.Constants;
            FollowsTable  = designExtractor.FollowsTable;
            ParentTable   = designExtractor.ParentTable;
            ModifiesTable = designExtractor.ModifiesTable;
            UsesTable     = designExtractor.UsesTable;
            CallsTable    = designExtractor.CallsTable;
            NextTable     = designExtractor.NextTable;
            AffectsTable  = designExtractor.AffectsTable;
        }
Exemplo n.º 34
0
        private static BlockNode AST(string fileName)
        {
            Console.OutputEncoding = Encoding.UTF8;
            try
            {
                string text = File.ReadAllText(fileName);

                var scanner = new Scanner();
                scanner.SetSource(text, 0);

                var parser = new Parser.Parser(scanner);

                var b = parser.Parse();
                if (b)
                {
                    Console.WriteLine("Синтаксическое дерево построено");
                    return(parser.root);
                }
                else
                {
                    Console.WriteLine("Ошибка");
                }
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine($"Файл {fileName} не найден");
            }
            catch (LexException e)
            {
                Console.WriteLine($"Лексическая ошибка. {e.Message}");
            }
            catch (SyntaxException e)
            {
                Console.WriteLine($"Синтаксическая ошибка. {e.Message}");
            }
            return(null);
        }
Exemplo n.º 35
0
        public void ParserParse_Test()
        {
            var parser = new Parser.Parser();

            Assert.That(parser.Parse(""), Is.EqualTo(null));

            Assert.That(parser.Parse("h1 header"), Is.EqualTo(new List <Tuple <string, string> >()
            {
                new Tuple <string, string>(KeyWords.Header1, "header")
            }));

            Assert.That(parser.Parse("h2   header"), Is.EqualTo(new List <Tuple <string, string> >()
            {
                new Tuple <string, string>(KeyWords.Header2, "  header")
            }));

            Assert.That(parser.Parse("h2 header\r\n\r\np text"),
                        Is.EqualTo(new List <Tuple <string, string> >()
            {
                new Tuple <string, string>(KeyWords.Header2, "header"),
                new Tuple <string, string>(KeyWords.Text, "text")
            }));

            Assert.That(parser.Parse("h2 header\r\n\r\np text\r\n\r\nignore text"),
                        Is.EqualTo(new List <Tuple <string, string> >()
            {
                new Tuple <string, string>(KeyWords.Header2, "header"),
                new Tuple <string, string>(KeyWords.Text, "text"),
                new Tuple <string, string>(null, "ignore text")
            }));

            Assert.That(parser.Parse("ordlist sometext\r\none\r\ntwo"),
                        Is.EqualTo(new List <Tuple <string, string> >()
            {
                new Tuple <string, string>(KeyWords.OrderedList, "sometext\r\none\r\ntwo")
            }));
        }
Exemplo n.º 36
0
        public void Test_IsExpression_ThrowsTypeNotDefined()
        {
            const string src = "namespace MyNamespace" +
                               "{" +
                               "    class MyClass" +
                               "    {" +
                               "        func Add(int a, int b) -> return a * b" +
                               "    }" +
                               "" +
                               "    class MyClass2" +
                               "    {" +
                               "        var myClassField : new MyClass()" +
                               "        func Foo() -> bool" +
                               "        {" +
                               "            return myClassField is MyClassS" +
                               "        }" +
                               "    }" +
                               "}";

            var lexer = new Lexer.Lexer();
            var tokens = lexer.Lex(src);
            var parser = new Parser.Parser(tokens);

            var ast = parser.Parse();

            var binder = new Binder();

            Assert.That(
                () => binder.Bind(new List<CompilationUnitSyntax>() { ast }),
                Throws.InstanceOf<KiwiSemanticException>().With.Message.EqualTo("MyClassS undefined Type"));
        }
Exemplo n.º 37
0
        public void Test_MemberAccessExpressionBinding()
        {
            const string src = "namespace MyNamespace" +
                               "{" +
                               "    class MyClass" +
                               "    {" +
                               "        func Add(int a, int b) -> return a * b" +
                               "    }" +
                               "" +
                               "    class MyClass2" +
                               "    {" +
                               "        var addMethod : new MyClass().Add" +
                               "    }" +
                               "}";

            var lexer = new Lexer.Lexer();
            var tokens = lexer.Lex(src);
            var parser = new Parser.Parser(tokens);

            var ast = parser.Parse();

            var binder = new Binder();

            var boundCompilationUnit = binder.Bind(new List<CompilationUnitSyntax>() { ast }).Single();
            var function =
                boundCompilationUnit.Namespaces[0].Types.Single(x => x.Name == "MyClass")
                                                  .Functions.Single(x => x.Name == "Add");
            var field =
                boundCompilationUnit.Namespaces[0].Types.Single(x => x.Name == "MyClass2")
                                                  .Fields.Single(x => x.Name == "addMethod");
            Assert.That(
                () => field.Type,
                Is.InstanceOf<FunctionCompilerGeneratedType>()
                  .And.Property("ReturnType")
                  .InstanceOf<IntCompilerGeneratedType>());
            CollectionAssert.AllItemsAreInstancesOfType(
                ((FunctionCompilerGeneratedType)field.Type).ParameterTypes,
                typeof(IntCompilerGeneratedType));
        }
Exemplo n.º 38
0
        public void TestErrorParser()
        {
            var parser = new Parser.Parser();

            parser.Parse(@"CaffeTests\Error.prototxt");
        }
Exemplo n.º 39
0
        static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.WriteLine("Please provide input file");
                Environment.Exit(-1);
            }

            string filePath      = $@"{Environment.CurrentDirectory}\{args[0]}";
            string fileName      = Path.GetFileNameWithoutExtension(filePath);
            string fileDirectory = Path.GetDirectoryName(filePath);
            string inputText     = File.ReadAllText(filePath);

            Lexer.Lexer lex = new Lexer.Lexer(inputText);

            List <Token> tokensToParse = new List <Token>();

            using (StreamWriter tokenFile = new StreamWriter($@"{fileDirectory}\{fileName}.outlextokens"))
                using (StreamWriter tokenErrorFile = new StreamWriter($@"{fileDirectory}\{fileName}.outlexerrors"))
                {
                    Token t;
                    do
                    {
                        t = lex.GetNextToken();
                        //Console.WriteLine(t.ToString());

                        if (lex.IsErrorToken(t.TokenType))
                        {
                            tokenErrorFile.WriteLine(t.ToString());
                            Console.WriteLine($"LexError: {t.ToString()}");
                        }
                        else
                        {
                            tokenFile.WriteLine(t.ToString());
                            tokensToParse.Add(t);
                        }
                    }while (t.TokenType != TokenType.EOF);

                    tokensToParse.RemoveAll(x => lex.IsCommentToken(x.TokenType));
                    Console.WriteLine("INFO: Lexing completed.");
                }

            using (StreamWriter astStream = new StreamWriter($@"{fileDirectory}\{fileName}.outast"))
                using (StreamWriter derivationsStream = new StreamWriter($@"{fileDirectory}\{fileName}.outderivation"))
                    using (StreamWriter syntaxErrorStream = new StreamWriter($@"{fileDirectory}\{fileName}.outsyntaxerrors"))
                        using (StreamWriter symbolTablesStream = new StreamWriter($@"{fileDirectory}\{fileName}.outsymboltables"))
                            using (StreamWriter semanticErrorStream = new StreamWriter($@"{fileDirectory}\{fileName}.outsemanticerrors"))
                                using (StreamWriter codeGenOutput = new StreamWriter($@"{fileDirectory}\{fileName}.moon"))
                                {
                                    // Do parsing
                                    Parser.Parser parser = new Parser.Parser(tokensToParse, syntaxErrorStream, derivationsStream, astStream);
                                    Console.WriteLine(parser.Parse() ? "Parsing passed" : "Error: Parsing Failed");
                                    var tree = parser.GetASTTree();

                                    var printVisitor = new DOTPrinterVisitor(astStream);
                                    tree.Accept(printVisitor);
                                    astStream.Flush();
                                    Console.WriteLine("INFO: AST Tree dumped to outast");

                                    var symbolTableVisitor = new SymbolTableVisitor(semanticErrorStream);
                                    tree.Accept(symbolTableVisitor);
                                    Console.WriteLine("INFO: SymbolTable Generated");

                                    var semanticCheckerVisitor = new SemanticCheckerVisitor(semanticErrorStream, symbolTableVisitor.GlobalSymbolTable);
                                    tree.Accept(semanticCheckerVisitor);
                                    Console.WriteLine("INFO: Semantic Checking Complete");

                                    syntaxErrorStream.Flush();
                                    semanticErrorStream.Flush();
                                    bool hasErrors = semanticErrorStream.BaseStream.Length != 0 || syntaxErrorStream.BaseStream.Length != 0;
                                    if (hasErrors)
                                    {
                                        Console.WriteLine("Errors generated during parsing/semantic checking, terminating...");
                                        Console.ReadKey();
                                        Environment.Exit(-10);
                                    }

                                    // Codegen
                                    codeGenOutput.NewLine = "\n";
                                    var codeWriter = new CodeWriter(codeGenOutput);
                                    var codeGen    = new CodeGen.CodeGen(tree, symbolTableVisitor.GlobalSymbolTable, codeWriter);
                                    codeGen.GenerateCode();

                                    symbolTablesStream.WriteLine(symbolTableVisitor.GlobalSymbolTable);
                                    Console.WriteLine("INFO: Code Generated");

                                    Console.ReadKey();
                                }
        }
Exemplo n.º 40
0
        public void Test_SwitchStatement_CasesTypeMustMatchSwitchConditionType()
        {
            const string src = "namespace MyNamespace" +
                               "{" +
                               "    class MyClass" +
                               "    {" +
                               "        func Add(int a, int b) " +
                               "        { " +
                               "            switch(a)" +
                               "            {" +
                               "                case \"LOL\" -> Add(1, 0)" +
                               "            }" +
                               "        }" +
                               "    }" +
                               "" +
                               "}";

            var lexer = new Lexer.Lexer();
            var tokens = lexer.Lex(src);
            var parser = new Parser.Parser(tokens);

            var ast = parser.Parse();

            var binder = new Binder();

            Assert.That(
                () => binder.Bind(new List<CompilationUnitSyntax>() { ast }),
                Throws.InstanceOf<KiwiSemanticException>()
                      .With.Message.EqualTo("Switch cases condition type must match switch condition type"));
        }
Exemplo n.º 41
0
        public void TestGoogLeNetDeploySuccessParser()
        {
            var parser = new Parser.Parser();

            parser.Parse(@"CaffeTests\GoogLeNet-deploy.prototxt");
        }
Exemplo n.º 42
0
        public void Test_IfElseExpression_IfTrueIfFalseTypeMustBeEqual()
        {
            const string src = "namespace MyNamespace" +
                               "{" +
                               "    class MyClass" +
                               "    {" +
                               "        func Add(int a, float b) -> return if(true) a else b" +
                               "    }" +
                               "" +
                               "}";

            var lexer = new Lexer.Lexer();
            var tokens = lexer.Lex(src);
            var parser = new Parser.Parser(tokens);

            var ast = parser.Parse();

            var binder = new Binder();

            Assert.That(
                () => binder.Bind(new List<CompilationUnitSyntax>() { ast }),
                Throws.InstanceOf<KiwiSemanticException>()
                      .With.Message.EqualTo("IfTrue and IfFalse expression Type must match"));
        }
Exemplo n.º 43
0
        public void Test_VariableWithSameName_Throws()
        {
            const string src = "namespace MyNamespace" +
                               "{" +
                               "    class MyClass" +
                               "    {" +
                               "        func Add(int a, int b)" +
                               "        {" +
                               "            var a : 1" +
                               "            var b : 2" +
                               "            var a : 1" +
                               "        }" +
                               "    }" +
                               "}";

            var lexer = new Lexer.Lexer();
            var tokens = lexer.Lex(src);
            var parser = new Parser.Parser(tokens);

            var ast = parser.Parse();

            var binder = new Binder();
            Assert.That(
                () => binder.Bind(new List<CompilationUnitSyntax> { ast }),
                Throws.TypeOf<KiwiSemanticException>().With.Message.EqualTo("a already defined."));
        }
Exemplo n.º 44
0
        public void Test_IsExpression()
        {
            const string src = "namespace MyNamespace" +
                               "{" +
                               "    class MyClass" +
                               "    {" +
                               "        func Add(int a, int b) -> return a * b" +
                               "    }" +
                               "" +
                               "    class MyClass2" +
                               "    {" +
                               "        var myClassField : new MyClass()" +
                               "        func Foo() -> bool" +
                               "        {" +
                               "            return myClassField is MyClass" +
                               "        }" +
                               "    }" +
                               "}";

            var lexer = new Lexer.Lexer();
            var tokens = lexer.Lex(src);
            var parser = new Parser.Parser(tokens);

            var ast = parser.Parse();

            var binder = new Binder();

            var boundCompilationUnit = binder.Bind(new List<CompilationUnitSyntax>() { ast }).Single();
            var type = boundCompilationUnit.Namespaces[0].Types.Single(x => x.Name == "MyClass");
            var function =
                (BoundFunction)
                boundCompilationUnit.Namespaces[0].Types.Single(x => x.Name == "MyClass2")
                                                  .Functions.Single(x => x.Name == "Foo");
            var returnStatement = (BoundReturnStatement)((BoundScopeStatement)function.Statements).Statements[0];
            var boundBinaryExpression = (BoundBinaryExpression)returnStatement.Expression;
            Assert.AreEqual(BinaryOperators.Is, boundBinaryExpression.Operator);
            Assert.IsInstanceOf<BoundTypeExpression>(boundBinaryExpression.Right);
            Assert.AreSame(((BoundTypeExpression)boundBinaryExpression.Right).ReferencedType, type);
        }
Exemplo n.º 45
0
        public void Test_RecursivExpressionFunction_DoesThrow()
        {
            const string src = "namespace MyNamespace" +
                               "{" +
                               "    class MyClass" +
                               "    {" +
                               "        func Add(int a, int b) -> return Add(1, 2)" +
                               "    }" +
                               "" +
                               "}";

            var lexer = new Lexer.Lexer();
            var tokens = lexer.Lex(src);
            var parser = new Parser.Parser(tokens);

            var ast = parser.Parse();

            var binder = new Binder();

            Assert.That(
                () => binder.Bind(new List<CompilationUnitSyntax>() { ast }),
                Throws.InstanceOf<KiwiSemanticException>().With.Message.EqualTo("Add Type cannot be inferred"));
        }
Exemplo n.º 46
0
        private void ExecutionProcessKeystrokeMessage
            (KeystrokeMessage keystroke,
            Engine engine,
            Program program,
            Stack stack,
            ref bool ignoreNextDown,
            ref bool ignoreNextUp,
            out bool mustUnbusyUI)
        {
            try
            {
                mustUnbusyUI = true;

                // We want to protect this sequence against asynchronous changes to the menus which
                // may happen if the W/PGRM-RUN switch is moved: we wouldn't want the menus to be
                // changed by the main thread between the invocation of notifyUI and the call to
                // the parser, or during the execution of the keystroke.
                lock (IsBusy)
                {
                    bool programWasEmpty = program.IsEmpty;
                    display.Invoke
                        (notifyUI, new object [] { /* threadIsBusy */ true, programWasEmpty });

                    switch (keystroke.Motion)
                    {
                    case KeystrokeMotion.Up:

                        // We may have to ignore two up keystrokes after an error: the one for
                        // the key that caused the error, and the one for the key that clears
                        // the error.
                        if (ignoreNextUp)
                        {
                            ignoreNextUp = ignoreNextDown;
                        }
                        else
                        {
                            upParser.Parse(keystroke.Tag);
                        }
                        mustUnbusyUI = !ignoreNextDown;
                        break;

                    case KeystrokeMotion.Down:

                        // In general we *don't* refresh the display here because a down
                        // keystroke may display the current instruction.  There are three
                        // exceptions to this, though: we refresh if the display is blurred, or
                        // if this is the key that clears an error, or if the state of the
                        // program changed.  The latter is necessary because we are not sure to
                        // get an up keystroke for MERGE, as the mouse is likely to move to the
                        // dialog box.

                        // TODO: What if another down key was queued in-between?
                        downKeystrokeWasEnqueued.Reset();
                        if (ignoreNextDown)
                        {
                            Trace.Assert(ignoreNextUp);
                            ignoreNextDown = false;
                        }
                        else
                        {
                            downParser.Parse(keystroke.Tag);
                        }
                        mustUnbusyUI = ignoreNextUp ||
                                       display.IsBlurred ||
                                       program.IsEmpty != programWasEmpty;
                        break;
                    }
                }
            }
            catch (Interrupt)
            {
                display.Formatter.Value = stack.X; // Refresh the numeric display.
                ignoreNextDown          = true;
                ignoreNextUp            = true;
                mustUnbusyUI            = true;
                ExecutionCompleteKeystrokes();
            }
        }
Exemplo n.º 47
0
        public void Test_Operator_FunctionMapping()
        {
            const string src = "namespace MyNamespace" +
                               "{" +
                               "    class MyClass2" +
                               "    {" +
                               "        operator Add(MyClass2 opA, MyClass2 opB) -> return new MyClass2()" +
                               "        operator Sub(MyClass2 opA, MyClass2 opB) -> return new MyClass2()" +
                               "        operator Mult(MyClass2 opA, MyClass2 opB) -> return new MyClass2()" +
                               "        operator Div(MyClass2 opA, MyClass2 opB) -> return new MyClass2()" +
                               "        operator Pow(MyClass2 opA, MyClass2 opB) -> return new MyClass2()" +
                               "        operator Range(MyClass2 opA, MyClass2 opB) -> return new MyClass2[1]" +
                               "        operator In(MyClass2 opA, MyClass2 opB) -> return true" +
                               "    }" +
                               "    " +
                               "    class TestClass" +
                               "    {" +
                               "        func TestBinaryFunc()" +
                               "        {" +
                               "            var instance : new MyClass2()" +
                               "            var testAdd : instance + new MyClass2()" +
                               "            var testSub : instance - new MyClass2()" +
                               "            var testMult : instance * new MyClass2()" +
                               "            var testDiv : instance / new MyClass2()" +
                               "            var testPow : instance ^ new MyClass2()" +
                               "            var testRange : instance..newMyClass2()" +
                               "            var testIn : instance in new myClass2[10]" +
                               "        }" +
                               "" +
                               "" +
                               "        func TestAssignFunc()" +
                               "        {" +
                               "            var instance : None MyClass2" +
                               "            instance +: new MyClass2()" +
                               "            instance -: new MyClass2()" +
                               "            instance *: new MyClass2()" +
                               "            instance ^: new MyClass2()" +
                               "            instance /: new MyClass2()" +
                               "        }" +
                               "    }" +
                               "}";

            var lexer = new Lexer.Lexer();
            var tokens = lexer.Lex(src);
            var parser = new Parser.Parser(tokens);

            var ast = parser.Parse();
            var binder = new Binder();
            var boundCompilationUnit = binder.Bind(new List<CompilationUnitSyntax>() { ast }).Single();

            var boundNamespace = boundCompilationUnit.Namespaces.Single(x=>x.Name== "MyNamespace");
            var operatorClass = boundNamespace.Types.Single(x=>x.Name== "MyClass2");
            var testClass = boundNamespace.Types.Single(x=>x.Name == "TestClass");
            var testBinaryFunc = testClass.Functions.Single(x=>x.Name== "TestBinaryFunc");
            var testAssignFunc = testClass.Functions.Single(x=>x.Name== "TestAssignFunc");
        }
Exemplo n.º 48
0
        public void Test_ObjectCreationExpression_ThrowsNoConstructorWithoutArguments()
        {
            const string src = "namespace MyNamespace" +
                               "{" +
                               "    class MyClass" +
                               "    {" +
                               "        Constructor(int a){}" +
                               "    }" +
                               "    class MyClass2" +
                               "    {" +
                               "        func Foo()" +
                               "        {" +
                               "            var a : new MyClass()" +
                               "        }" +
                               "    }" +
                               "}";

            var lexer = new Lexer.Lexer();
            var tokens = lexer.Lex(src);
            var parser = new Parser.Parser(tokens);

            var ast = parser.Parse();

            var binder = new Binder();

            Assert.That(
                () => binder.Bind(new List<CompilationUnitSyntax>() { ast }),
                Throws.InstanceOf<KiwiSemanticException>()
                      .With.Message.EqualTo("MyNamespace.MyClass has no constructor without arguments."));
        }
Exemplo n.º 49
0
        static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.WriteLine("Please provide input file");
                Environment.Exit(-1);
            }

            string filePath      = $@"{Environment.CurrentDirectory}\{args[0]}";
            string fileName      = Path.GetFileNameWithoutExtension(filePath);
            string fileDirectory = Path.GetDirectoryName(filePath);
            string inputText     = File.ReadAllText(filePath);

            Lexer.Lexer lex = new Lexer.Lexer(inputText);

            List <Token> tokensToParse = new List <Token>();

            using (StreamWriter tokenFile = new StreamWriter($@"{fileDirectory}\{fileName}.outlextokens"))
                using (StreamWriter tokenErrorFile = new StreamWriter($@"{fileDirectory}\{fileName}.outlexerrors"))
                {
                    Token t;
                    do
                    {
                        t = lex.GetNextToken();
                        Console.WriteLine(t.ToString());

                        if (lex.IsErrorToken(t.TokenType))
                        {
                            tokenErrorFile.WriteLine(t.ToString());
                        }
                        else
                        {
                            tokenFile.WriteLine(t.ToString());
                            tokensToParse.Add(t);
                        }
                    }while (t.TokenType != TokenType.EOF);

                    tokensToParse.RemoveAll(x => lex.IsCommentToken(x.TokenType));
                }

            using (StreamWriter astStream = new StreamWriter($@"{fileDirectory}\{fileName}.outast"))
                using (StreamWriter derivationsStream = new StreamWriter($@"{fileDirectory}\{fileName}.outderivation"))
                    using (StreamWriter syntaxErrorStream = new StreamWriter($@"{fileDirectory}\{fileName}.outsyntaxerrors"))
                        using (StreamWriter symbolTablesStream = new StreamWriter($@"{fileDirectory}\{fileName}.outsymboltables"))
                            using (StreamWriter semanticErrorStream = new StreamWriter($@"{fileDirectory}\{fileName}.outsemanticerrors"))
                            {
                                // Do parsing
                                Parser.Parser parser = new Parser.Parser(tokensToParse, syntaxErrorStream, derivationsStream, astStream);
                                Console.WriteLine(parser.Parse());
                                var tree = parser.GetASTTree();
                                derivationsStream.Flush();
                                syntaxErrorStream.Flush();

                                var printVisitor = new DOTPrinterVisitor(astStream);
                                tree.Accept(printVisitor);
                                astStream.Flush();

                                var symbolTableVisitor = new SymbolTableVisitor(semanticErrorStream);
                                tree.Accept(symbolTableVisitor);

                                symbolTablesStream.WriteLine(symbolTableVisitor.GlobalSymbolTable);
                                Console.WriteLine(symbolTableVisitor.GlobalSymbolTable);

                                var semanticCheckerVisitor = new SemanticCheckerVisitor(semanticErrorStream, symbolTableVisitor.GlobalSymbolTable);
                                tree.Accept(semanticCheckerVisitor);
                            }
        }
Exemplo n.º 50
0
        public static GettextCatalog ParseFromStream(Stream poStream)
        {
            if (poStream == null) throw new ArgumentNullException("poStream");

            GettextCatalog catalog = null;
            try
            {

                var lexer = new Scanner();
                lexer.SetSource(poStream);

                var parser = new Parser.Parser(lexer);
                parser.Parse();

                catalog = parser.Catalog;

                if (catalog == null) goto ret;

                // another parsing step
                catalog.ParseHeaders();

                // transform all strings into internal UTF-8 representation
                catalog.ConvertStringsToUtf8();

                // parse comments
                catalog.ParseComments();

                // build lookup structures
                catalog.Finalize();

            } catch (Exception e)
            {
                throw new GettextException("Parsing exception!", e);
            }

            ret:

            if (catalog == null) throw new GettextException("Couldn't parse the catalog. Check the syntax.");
            return catalog;
        }
Exemplo n.º 51
0
        internal void Process(string fileArg)
        {
            GetNames(fileArg);
            // check for file exists
            OpenSource();
            // parse source file
            if (inputFile != null)
            {
                DateTime start = DateTime.Now;
                try
                {
                    handler        = new ErrorHandler();
                    scanner        = new Scanner(inputFile);
                    parser         = new Parser.Parser(scanner);
                    scanner.yyhdlr = handler;
                    parser.Initialize(this, scanner, handler, new OptionParser2(ParseOption));
                    aast = parser.Aast;
                    parser.Parse();
                    // aast.DiagnosticDump();
                    if (verbose)
                    {
                        Status(start);
                    }
                    CheckOptions();
                    if (!Errors && !ParseOnly)
                    {   // build NFSA
                        if (ChrClasses)
                        {
                            DateTime t0 = DateTime.Now;
                            partition = new Partition(TargetSymCardinality, this);
                            partition.FindClasses(aast);
                            partition.FixMap();
                            if (verbose)
                            {
                                ClassStatus(t0, partition.Length);
                            }
                        }
                        else
                        {
                            CharRange.Init(TargetSymCardinality);
                        }
                        nfsa = new NFSA(this);
                        nfsa.Build(aast);
                        if (!Errors)
                        {       // convert to DFSA
                            dfsa = new DFSA(this);
                            dfsa.Convert(nfsa);
                            if (!Errors)
                            {   // minimize automaton
                                if (minimize)
                                {
                                    dfsa.Minimize();
                                }
                                if (!Errors && !checkOnly)
                                {   // emit the scanner to output file
                                    TextReader frameRdr   = FrameReader();
                                    TextWriter outputWrtr = OutputWriter();
                                    dfsa.EmitScanner(frameRdr, outputWrtr);

                                    if (!embedBuffers)
                                    {
                                        CopyBufferCode();
                                    }
                                    // Clean up!
                                    if (frameRdr != null)
                                    {
                                        frameRdr.Close();
                                    }
                                    if (outputWrtr != null)
                                    {
                                        outputWrtr.Close();
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    string str = ex.Message;
                    handler.AddError(str, aast.AtStart);
                    throw;
                }
            }
        }
Exemplo n.º 52
0
        public void Test_InfixCall_Types()
        {
            const string src = "namespace MyNamespace" +
                               "{" +
                               "    class MyClass2" +
                               "    {" +
                               "        infix func Add(int a, int b) -> return a + b" +
                               "        func Foo()" +
                               "        {" +
                               "            var a : 1 Add 2" +
                               "        }" +
                               "    }" +
                               "}";

            var lexer = new Lexer.Lexer();
            var tokens = lexer.Lex(src);
            var parser = new Parser.Parser(tokens);

            var ast = parser.Parse();
            var binder = new Binder();
            var boundCompilationUnits = binder.Bind(new List<CompilationUnitSyntax>() { ast });

            var myClass2Type = boundCompilationUnits[0].Namespaces[0].Types.Single();
            var fooFunction = myClass2Type.Functions.Single(x => x.Name == "Foo");
            var addFunction = myClass2Type.Functions.Single(x => x.Name == "Add");

            Assert.IsInstanceOf<BoundFunction>(addFunction);
            Assert.IsTrue(((BoundFunction)addFunction).IsInfixFunction);
            Assert.IsInstanceOf<BoundFunction>(fooFunction);
            var boundFunction = (BoundFunction)fooFunction;
            Assert.IsInstanceOf<BoundScopeStatement>(boundFunction.Statements);
            var boundScopeStatement = (BoundScopeStatement)boundFunction.Statements;
            Assert.IsInstanceOf<BoundVariableDeclarationStatement>(boundScopeStatement.Statements[0]);
            var boundVariableDeclarationStatement = (BoundVariableDeclarationStatement)boundScopeStatement.Statements[0];
            Assert.IsInstanceOf<BoundInvocationExpression>(boundVariableDeclarationStatement.BoundExpression);
            var boundInvocationExpression = (BoundInvocationExpression)boundVariableDeclarationStatement.BoundExpression;
            Assert.IsInstanceOf<BoundMemberExpression>(boundInvocationExpression.ToInvoke);
            var boundMemberExpression = (BoundMemberExpression)boundInvocationExpression.ToInvoke;
            Assert.AreSame(addFunction, boundMemberExpression.BoundMember);
        }
Exemplo n.º 53
0
Arquivo: Main.cs Projeto: deAtog/gppg
        private static int Main( string[] args )
        {
            Stream inputFile = null;

            Grammar grammar = null;
            ErrorHandler handler = new ErrorHandler();
            string inputFileInfo = null;  // Filename plus revision time.
            Lexers.Scanner scanner = null;
            Parser.Parser parser = null;
            Assembly assm = Assembly.GetExecutingAssembly();
            object info = Attribute.GetCustomAttribute( assm, typeof( AssemblyFileVersionAttribute ) );
            versionInfo = ((AssemblyFileVersionAttribute)info).Version;

            try {
                string filename = ProcessOptions( args );

                if (filename == null)
                    return MC_OK;

                try {
                    inputFile = new FileStream( filename, FileMode.Open, FileAccess.Read, FileShare.Read );
                    inputFileInfo = filename + " - " + File.GetLastWriteTime( filename ).ToString();
                }
                catch (IOException x) {
                    string message;
                    inputFile = null;
                    if (x is FileNotFoundException)
                        message = String.Format( CultureInfo.InvariantCulture,
                            "Source file <{0}> not found{1}",
                            filename, Environment.NewLine );
                    else
                        message = String.Format( CultureInfo.InvariantCulture,
                            "Source file <{0}> could not be opened{1}",
                            filename, Environment.NewLine );
                    handler.AddError( 4, message, null ); // aast.AtStart;
                    return MC_FILEERROR;
                }

                scanner = new Lexers.Scanner( inputFile );
                scanner.SetHandler( handler );

                parser = new Parser.Parser( filename, inputFileInfo, scanner, handler );
                //
                // If the parse is successful, then process the grammar.
                // Otherwise just report the errors that have been listed.
                //
                if (parser.Parse()) {
                    grammar = parser.Grammar;

                    if (Terminal.Max > 255)
                        handler.ListError( null, 103, CharacterUtilities.Map( Terminal.Max ), '\'' );

                    LALRGenerator generator = new LALRGenerator( grammar );
                    List<AutomatonState> states = generator.BuildStates();
                    generator.ComputeLookAhead();
                    generator.BuildParseTable();
                    if (!grammar.CheckGrammar( handler ))
                        throw new ArgumentException( "Non-terminating grammar" );
                    //
                    // If the grammar has non-terminating non-terms we cannot
                    // create a diagnostic report as the grammar is incomplete.
                    //
                    if (!handler.Errors) {
                        CodeGenerator code = new CodeGenerator();
                        code.Generate( states, grammar );
                    }

                    bool DoDiagnose = Diagnose && !grammar.HasNonTerminatingNonTerms;
                    if (Report || DoDiagnose) {
                        string htmlName = System.IO.Path.ChangeExtension( filename, ".report.html" );
                        try {
                            System.IO.FileStream htmlFile = new System.IO.FileStream( htmlName, System.IO.FileMode.Create );
                            System.IO.StreamWriter htmlWriter = new System.IO.StreamWriter( htmlFile );
                            Grammar.HtmlHeader( htmlWriter, filename );

                            if (Report && DoDiagnose)
                                grammar.GenerateCompoundReport( htmlWriter, inputFileInfo, states );
                            else if (Report)
                                grammar.GenerateReport( htmlWriter, inputFileInfo, states );

                            Grammar.HtmlTrailer( htmlWriter );

                            if (htmlFile != null) {
                                htmlWriter.Flush();
                                htmlFile.Close();
                            }
                        }
                        catch (System.IO.IOException) {
                            Console.Error.WriteLine( "Cannot create html output file {0}", htmlName );
                        }
                    }
                }
            }
            catch (System.Exception e) {
                if (e is TooManyErrorsException)
                    return MC_TOOMANYERRORS;
                Console.Error.WriteLine( "Unexpected Error {0}", e.Message );

                if (NoThrowOnError) {
                    // report the error, do not let it go into the void
                    Console.Error.WriteLine( e );
                    return MC_EXCEPTION;
                }
            }
            finally {
                if (handler.Errors || handler.Warnings)
                    handler.DumpAll( (scanner == null ? null : scanner.Buffer), Console.Error );
                if ((Listing || handler.Errors || handler.Warnings) && parser != null) {
                    string listName = parser.ListfileName;
                    StreamWriter listStream = ListingFile( listName );
                    if (listStream != null)
                        handler.MakeListing( scanner.Buffer, listStream, parser.SourceFileInfo, versionInfo );
                }
            }
            return MC_OK;
        }
Exemplo n.º 54
0
        public override string[] Build()
        {
            var options = BuildOption();
            options.SourceCode = Parameters.Template;
            var tmp = new List<string>();
            var parser = new Parser.Parser(options);
            string fileName;
            string temp;
            string fName;
            DirectoryInfo dir;
            switch (Parameters.FileDependency)
            {
                case FileDepend.DATABASE:
                    parser.Options.VariableParameter = Parameters.DataBase;
                    parser.Options.StatementParameters = new object[] { Parameters.DataBase };
                    temp = parser.Parse(ParserType.XCODER);
                    fName = string.IsNullOrEmpty(Parameters.FileNameFormat)
                                ? Parameters.DataBase.Connection.Name
                                : string.Format(Parameters.FileNameFormat, Parameters.DataBase.Connection.Name);
                    fileName = Path.Combine(Parameters.OutputDirectory.FullName, fName + Extension);
                    dir = new FileInfo(fileName).Directory;
                    if (dir != null && !dir.Exists)
                    {
                        dir.Create();
                    }
                    if (Parameters.Override)
                    {
                        File.WriteAllText(fileName, temp);
                        tmp.Add(fileName);
                    }
                    else
                    {
                        if (!File.Exists(fileName))
                        {
                            File.WriteAllText(fileName, temp);
                        }
                        tmp.Add(fileName);
                    }

                    break;
                case FileDepend.TABLES:
                    foreach (Table table in Parameters.DataBase.Tables)
                    {
                        parser.Options.VariableParameter = table;
                        parser.Options.StatementParameters = new object[] { Parameters.DataBase, table };
                        temp = parser.Parse(ParserType.XCODER);
                        fName = string.IsNullOrEmpty(Parameters.FileNameFormat)
                                    ? table.Name
                                    : string.Format(Parameters.FileNameFormat, table.Name);
                        fileName = Path.Combine(Parameters.OutputDirectory.FullName, fName + Extension);
                        dir = new FileInfo(fileName).Directory;
                        if (dir != null && !dir.Exists)
                        {
                            dir.Create();
                        }
                        if (Parameters.Override)
                        {
                            File.WriteAllText(fileName, temp);
                            tmp.Add(fileName);
                        }
                        else
                        {
                            if (!File.Exists(fileName))
                            {
                                File.WriteAllText(fileName, temp);
                            }
                            tmp.Add(fileName);
                        }
                    }
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
            parser.Release();
            return tmp.ToArray();
        }
Exemplo n.º 55
0
        public void TestResNet50DeploySuccessParser()
        {
            var parser = new Parser.Parser();

            parser.Parse(@"CaffeTests\ResNet-50-deploy.prototxt");
        }
Exemplo n.º 56
0
        public void TestAlexNetSuccessParser()
        {
            var parser = new Parser.Parser();

            parser.Parse(@"CaffeTests\AlexNet.prototxt");
        }
Exemplo n.º 57
0
        private Document Parse(string text, IHost host)
        {
            Parser.Parser parser = new Parser.Parser(host);
            Document document;

            parser.Parse(text, out document);

            return document;
        }
Exemplo n.º 58
0
        public void Test_Scope_VariableNotDefined()
        {
            const string src = "namespace MyNamespace" +
                               "{" +
                               "    class MyClass" +
                               "    {" +
                               "        func Add(int a, int b) " +
                               "        { " +
                               "            {" +
                               "                var i : 1337 + b" +
                               "            }" +
                               "            var c : a * b * i" +
                               "        }" +
                               "    }" +
                               "" +
                               "}";

            var lexer = new Lexer.Lexer();
            var tokens = lexer.Lex(src);
            var parser = new Parser.Parser(tokens);

            var ast = parser.Parse();

            var binder = new Binder();

            Assert.That(
                () => binder.Bind(new List<CompilationUnitSyntax>() { ast }),
                Throws.InstanceOf<KiwiSemanticException>().With.Message.EqualTo("i not defined"));
        }
Exemplo n.º 59
0
        public void Test_ArrayAccessInvalidParameter_Throws()
        {
            const string src = "namespace MyNamespace" +
                               "{" +
                               "    class MyClass" +
                               "    {" +
                               "        func Add(int a, int b)" +
                               "        {   " +
                               "            var c : new int[1][1]" +
                               "            var d : c[1]" +
                               "        }" +
                               "    }" +
                               "}";

            var lexer = new Lexer.Lexer();
            var tokens = lexer.Lex(src);
            var parser = new Parser.Parser(tokens);

            var ast = parser.Parse();

            var binder = new Binder();

            Assert.That(
                () => binder.Bind(new List<CompilationUnitSyntax>() { ast }),
                Throws.InstanceOf<KiwiSemanticException>()
                      .With.Message.EqualTo("Parameter count (1) must match array dimension count (2)"));
        }
Exemplo n.º 60
0
        public void Test_IfStatement_IfConditionMustBeBool()
        {
            const string src = "namespace MyNamespace" +
                               "{" +
                               "    class MyClass" +
                               "    {" +
                               "        func Add(int a, int b) " +
                               "        { " +
                               "            if(a)" +
                               "            {" +
                               "                " +
                               "            }" +
                               "        }" +
                               "    }" +
                               "" +
                               "}";

            var lexer = new Lexer.Lexer();
            var tokens = lexer.Lex(src);
            var parser = new Parser.Parser(tokens);

            var ast = parser.Parse();

            var binder = new Binder();

            Assert.That(
                () => binder.Bind(new List<CompilationUnitSyntax>() { ast }),
                Throws.InstanceOf<KiwiSemanticException>().With.Message.EqualTo("If condition must be of Type Bool"));
        }