Example #1
0
        public IncludeNode(ITokenReader tokenReader, INodeReader nodeReader, string directiveName = null, Location location = null)
            : base(directiveName, location)
        {
            Filename = ExpressionRewriter.Rewrite(tokenReader);

            this.parentParser = nodeReader as Parser.Parser;
        }
Example #2
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();
        }
 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;
 }
Example #4
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);
        }
        private void goButton_Click(object sender, RoutedEventArgs e)
        {
            Parser.Parser.Variable variableI;
            Parser.Parser parser = new Parser.Parser();

            parser = new Parser.Parser();
            parser.InputString = functionTextBox.Text;
            variableI = parser.GetVariable("x");

            PointCollection p = new PointCollection();

            Random r = new Random();

            for (float i = 0; i <= 10; i += 0.5f)
            {
                points.pointX.Add(i);
                variableI.value = i;
                clearSignal.Add(variableI.value);

                //variableI.value = (i % 3 == 0) ? i * (float)r.NextDouble() : i;
                //noiseSignal.Add(variableI.value);
                points.pointY.Add(parser.Calculate());
                p.Add(new Point(parser.Calculate(), i));
            }

            chart.DataContext = p;
        }
Example #6
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);
 }
Example #7
0
        public static HassiumModule CompileModuleFromSource(string source)
        {
            var tokens = new Lexer().Scan(source);
            var ast = new Parser.Parser().Parse(tokens);
            var table = new SemanticAnalyzer().Analyze(ast);

            return new Compiler().Compile(ast, table);
        }
Example #8
0
        private Document Parse(string text)
        {
            Parser.Parser parsr = new Parser.Parser();
            Document document;

            parsr.Parse(text, out document);

            return document;
        }
Example #9
0
        public Evaluator(Parser.Parser parser, Environment environmentExtensions, TextWriter stdOut, TextReader stdIn)
        {
            this.parser = parser;
            applier = new Applier(this);
            backqouteExpander = new BackquoteExpander(this);

            InitializeGlobalEnvironment(environmentExtensions, stdOut, stdIn);

            ReadCoreLanguageCode();
        }
Example #10
0
        public PackageViewModel(ObservableCollection<string> packages, XElement document)
        {
            var parser = new Parser.Parser(document);

            Packages = new Dictionary<string, ObservableCollection<string>>();

            foreach (var package in packages)
            {
                Packages.Add(package, new ObservableCollection<string>(parser.ExtractPackageForms(package)));
            }
        }
Example #11
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);
        }
Example #12
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);
        }
Example #13
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: ");
        }
Example #14
0
        private void ParseIntoDictionary(Dictionary<string, string> keyVals, string fragmentVal)
        {
            Parser.Parser p = new Parser.Parser();

            while (fragmentVal.Length > 0 && p.Validate(fragmentVal) && p.IsAtomic(fragmentVal))
            {
                string firstKey = p.GetFirstKey(fragmentVal);

                keyVals[firstKey] = p.Extract(firstKey, fragmentVal);

                fragmentVal = p.TrimKeyVal(firstKey, fragmentVal);
            }
        }
Example #15
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);
        }
Example #16
0
        public Fragment(string fragmentString)
        {
            Parser.Parser p = new Parser.Parser();

            if (!p.IsAtomic(fragmentString))
            {
                fragmentString = "item={" + fragmentString + "}";
            }

            if (!p.Validate(fragmentString))
            {
                //if its not valid, its broken
                throw new ArgumentException("invalid fragment: " + fragmentString);
            }

            ParseIntoDictionary(keyVals, fragmentString);
        }
Example #17
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);
        }
        private void goButton_Click(object sender, RoutedEventArgs e)
        {
            Parser.Parser.Variable VariableI;
            Parser.Parser parser = new Parser.Parser();

            parser = new Parser.Parser();
            parser.InputString = FunctionBox.Text;
            VariableI = parser.GetVariable("x");

            PointCollection p = new PointCollection();

            for (float i = 0; i < 10; i += 0.5f)
            {
                VariableI.value = i;
                p.Add(new Point(parser.Calculate(), i));
            }

            chart.DataContext = p;
        }
Example #19
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;
     }
 }
Example #20
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;
        }
Example #21
0
        public void Evaluation()
        {
            string input = "x${test - 42}.2";
            string expected = "x42.2";

            var context = new ExecutionContext();
            context.SetVariable("test", 84);

            var output = new StringBuilder();

            using(var reader = new LocatedTextReaderWrapper(input)) {
                var parser = new Parser.Parser(reader);

                foreach(var node in parser.ReadNodes()) {
                    output.Append(node.Execute(context));
                }

                Assert.AreEqual(expected, output.ToString());
            }
        }
Example #22
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);
        }
Example #23
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;
        }
Example #24
0
        public void UpdateStatus(string item, bool completed, bool archived)
        {
            Parser.Parser p = new Parser.Parser();
            string itemValue = p.Extract("item", item);
            if (string.IsNullOrWhiteSpace(itemValue))
            {
                itemValue = item;
            }

            ToDoItem tdi = new ToDoItem()
            {
                Description = itemValue,
                Completed = completed,
                Archived = archived
            };

            //tdi.Add(new Fragment(item));

            AddWithMerge(tdi);
        }
Example #25
0
        private static string GetTagsFromKeyValFile(SyncProfile sp, string sha1Hash)
        {
            //load by using profile to get sync root
            //which gives us NWD/config
            string fileHashIndexPath = Configuration.SyncRootConfigFile(sp.Name, "FileHashIndex");
            string tagIndexPath = Configuration.SyncRootConfigFile(sp.Name, "TagIndex");

            string tags = "";

            if (File.Exists(fileHashIndexPath) && File.Exists(tagIndexPath))
            {
                Parser.Parser p = new Parser.Parser();
                List<string> paths = new List<string>();

                //get all paths matching hash (may be multiple files, if copied in multiple places)
                foreach (string lineItem in File.ReadLines(fileHashIndexPath))
                {
                    string path = p.Extract("path", lineItem);
                    string hash = p.Extract("sha1Hash", lineItem);

                    if (!string.IsNullOrWhiteSpace(path) &&
                        !string.IsNullOrWhiteSpace(hash))
                    {
                        if (hash.Equals(sha1Hash, StringComparison.CurrentCultureIgnoreCase))
                        {
                            paths.Add(path);
                        }
                    }
                }

                List<string> tagStrings = new List<string>();

                //since we may have multiple files for a given hash, need to get all tags for those paths
                foreach (string lineItem in File.ReadLines(tagIndexPath))
                {
                    string path = p.Extract("path", lineItem);
                    string tagString = p.Extract("tags", lineItem);

                    if (paths.Contains(path))
                    {
                        tagStrings.Add(tagString);
                    }
                }

                //remove any duplicates
                HashSet<string> uniqueTags = new HashSet<string>();

                foreach (string tagString in tagStrings)
                {
                    var theseTags = StringToList(tagString);

                    foreach (string tag in theseTags)
                    {
                        if (!uniqueTags.Contains(tag))
                        {
                            uniqueTags.Add(tag);
                        }
                    }
                }

                tags = string.Join(", ", uniqueTags);
            }

            return tags;
        }
Example #26
0
        public static Dictionary<string, string> LoadFromHashToTagsIndex(SyncProfile sp)
        {
            Dictionary<string, string> hashToTagString = new Dictionary<string, string>();
            Parser.Parser p = new Parser.Parser();

            //use syncprofile to load an existing one if it is there
            string exportedTagsIndexPath = HashToTagsIndexPath(sp, false);

            if (File.Exists(exportedTagsIndexPath))
            {
                foreach (string lineItem in File.ReadLines(exportedTagsIndexPath))
                {
                    string hash = p.Extract("sha1Hash", lineItem);
                    string tagString = p.Extract("tags", lineItem);

                    hashToTagString[hash] = tagString;
                }
            }

            return hashToTagString;
        }
Example #27
0
        private Document Parse(string text, IHost host)
        {
            Parser.Parser parser = new Parser.Parser(host);
            Document document;

            parser.Parse(text, out document);

            return document;
        }
Example #28
0
 /// <summary>
 /// initializes this phase.
 /// </summary>
 public ParsePhase(Parser.Parser parser)
 {
     _parser = parser;
     this.Name = "ast-parsing";
 }
Example #29
0
File: Main.cs Project: 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;
        }
Example #30
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();
        }