Example #1
0
        public static bool TestSyntax(string src)
        {
            var grammar   = new PyUsacGrammar();
            var langData  = new LanguageData(grammar);
            var parser    = new Irony.Parsing.Parser(langData);//Para evitar conflicto con el namespace Parser
            var parseTree = parser.Parse(src);
            var root      = parseTree.Root;

            if (parseTree.HasErrors())
            {
                ErrorHelper.ErrorFactory.CreateParsingErrors(parseTree, "_null");
                return(false);
            }
            else
            {
                GetDot(root);


                Debug.WriteLine("");
                Debug.WriteLine("--------------------------------------------------");
                Debug.WriteLine("");

                return(true);
            }
        }
Example #2
0
        public Form1()
        {
            InitializeComponent();

            var grammar = new CustomDslGrammar();
            _parser = new Parser(grammar);
        }
Example #3
0
        public static bool TestAst(string src)
        {
            var grammar   = new PyUsacGrammar();
            var langData  = new LanguageData(grammar);
            var parser    = new Irony.Parsing.Parser(langData);//Para evitar conflicto con el namespace Parser
            var parseTree = parser.Parse(src);
            var root      = parseTree.Root;

            if (parseTree.HasErrors())
            {
                ErrorHelper.ErrorFactory.CreateParsingErrors(parseTree, "_null");
                return(false);
            }
            else
            {
                var dotCode = GetDot(root);

                var astBuilder = new AstBuilder(new AstContext(langData));
                astBuilder.BuildAst(parseTree);
                var astRoot = (AstNode)root.AstNode;

                Debug.WriteLine("");
                Debug.WriteLine("--------------------------------------------------");
                Debug.WriteLine("");

                GetDot(astRoot);

                return(true);
            }
        }
Example #4
0
        public void TestMethod1()
        {
            //LuaGrammar grammar = new LuaGrammar();
            Irony.Parsing.Parser parser = new Irony.Parsing.Parser(new NPLTools.IronyParser.LuaGrammar());

            string code = "local a = [[ ";
            //try
            //{
            //    code = File.ReadAllText("test.lua");
            //}
            //catch (Exception e)
            //{

            //}
            int state = 0;

            parser.Scanner.VsSetSource(code, 0);
            Token token = parser.Scanner.VsReadToken(ref state);

            while (token != null)
            {
                token = parser.Scanner.VsReadToken(ref state);
            }
            //Irony.Parsing.ParseTree tree = parser.Scanner.VsReadToken()
            //Irony.Parsing.ParseTree tree2 = parser.Parse(code + " ");
            //PrintTree(tree);
            //PrintAstTree(tree);
            //tree.
            //LogMessageList m = tree.ParserMessages;
            //if (m[0] != null)
            //    Console.WriteLine(m[0].Message);
        }
Example #5
0
 public void StructTest01()
 {
     GLSLGrammar lang     = new GLSLGrammar();
     var         compiler = new Irony.Parsing.Parser(lang);
     var         tree     = compiler.Parse("struct Camera { float x;}");
     //CheckNodes (tree.Root, 0);
 }
Example #6
0
        // Ensure that the grammar has no conflicts
        public void NoGrammarConflicts()
        {
            /* Conflict either indicate an ambiguity in the grammar or an error in the rules
             * In case of an ambiguity Irony will choose an action itself, which is not always the correct one
             * Rewrite the grammar rules untill there are no conflicts, and the parses are corrent 
             * As a very last resort, you can manually specify shift with a grammar hint like PreferShiftHere() or ReduceHere()
             * However, this should only be done if one of the two is always correct, otherwise the grammar will need to be written differently
             */

            /* An example:
             * (A1) can be parsed both as an union and a bracketed reference
             * This is an ambiguity, and thus must be solved by precedence or a grammar hint
             */
            
            /* An example:
             * Functioncall.Rule =
             *   Prefix + Formula           // Prefix unop
             * | Formula + infix + Formula  // Binop
             * | Formula + Formula          // Intersection
             * 
             * With this 1+1 can be parsed as both 1-1 and 1 (-1)
             * This is obviously erroneous as there is only 1 correct interpertation, so the rules had to be rewritten.
             */

            var parser = new Parser(new ExcelFormulaGrammar());
            Assert.IsTrue(parser.Language.Errors.Count == 0, "Grammar has {0} error(s) or conflict(s)", parser.Language.Errors.Count);
        }
Example #7
0
 /// <summary>
 /// Build a parse tree from trip idl
 /// </summary>
 /// <param name="tripIdl">trip idl text</param>
 /// <returns>a parse tree</returns>
 public static ParseTree ParseTripIdl(string tripIdl)
 {
     Grammar g = new IdlGrammar();
     var parser = new Parser(g);
     var parseTree = parser.Parse(tripIdl);
     return parseTree;
 }
Example #8
0
 public void Setup()
 {
     _grammar = new TestGrammar();
       _language = new LanguageData(_grammar);
       _parser = new Parser(_language);
       _context = _parser.Context;
 }
        public void Setup()
        {
            var grammar = new XamlMarkupExtensionGrammar();
            var language = new LanguageData(grammar);

            _parser = new Parser(language) {Context = {TracingEnabled = true}};
        }
Example #10
0
 public void TestCase()
 {
     GLSLGrammar lang     = new GLSLGrammar();
     var         compiler = new Irony.Parsing.Parser(lang);
     var         tree     = compiler.Parse("void main(void) { float v = vec3(1,1,1); }");
     //CheckNodes (tree.Root, 0);
 }
Example #11
0
 public static Parser CreateParser(Terminal terminal, string terminator = "end")
 {
     var grammar = new TerminalTestGrammar(terminal, terminator);
       var parser = new Parser(grammar);
       CheckGrammarErrors(parser);
       return parser;
 }
        public void ShouldNotParseWithoutCases()
        {
            // Arrange
            var grammar = new CicodeGrammar();
            var parser = new Parser(grammar);
            var sourceCode =
            @"
            FUNCTION A()

            SELECT CASE a
            END SELECT

            END
            ";
            // Act
            var parseTree = parser.Parse(sourceCode);

            // Assert
            Assert.IsNotNull(parseTree);
            Assert.IsTrue(parseTree.HasErrors());
            // A parser error is expected at the end of line 4 because the expected list of cases which is missing
            Assert.AreEqual<int>(1, parseTree.ParserMessages
                .Where(m => m.Location.Line == 4)
                .Where(m => m.ParserState.ExpectedTerminals.First().Name == "CASE")
                .Count());
        }
Example #13
0
        private static void LoadBnfFile(string fileName, Builder mainBuilder)
        {
            Console.WriteLine("Parse BNF file: {0}", fileName);

            var bnf = File.ReadAllText(fileName);

            var metaParser = new MetaParser();
            var meta = metaParser.Parse(bnf);

            var oprimizedBnf = Optimize(bnf);

            var parser = new Parser(new BnfGrammar(meta.Mode));
            var tree = parser.Parse(oprimizedBnf, fileName);
            if (tree.Status == ParseTreeStatus.Error)
            {
                throw new Exception((tree.ParserMessages.Count > 0)
                    ? string.Format("{0}, in {3} file at line {1}, column {2}", tree.ParserMessages[0].Message, tree.ParserMessages[0].Location.Line, tree.ParserMessages[0].Location.Column, fileName)
                    : string.Format(@"Unknow error in BNF file {0}", fileName));
            }

            var builder = new Builder(tree, mainBuilder);
            builder.BuildExpressions();

            foreach (var @using in meta.Usings)
                LoadBnfFile(@using, mainBuilder);
        }
Example #14
0
        public override object Read(string path)
        {
            string txt;
            using (var reader = new StreamReader(path))
            {
                txt = reader.ReadToEnd();
            }

            var grammar = new OpenFoamGrammar();
            var parser = new Parser(grammar);
            var tree = parser.Parse(txt);
            var d = new Vertice();

            foreach (ParseTreeNode rootEntryNode in tree.Root.FindDictEntries(null))
            {
                var identifier = rootEntryNode.GetEntryIdentifier();
                switch (identifier)
                {
                    case "value":
                        d.X = rootEntryNode.GetDictArrayBody().GetArrayOfDecimal()[0];
                        d.Y = rootEntryNode.GetDictArrayBody().GetArrayOfDecimal()[1];
                        d.Z = rootEntryNode.GetDictArrayBody().GetArrayOfDecimal()[2];
                        break;
                }
            }
            return d;
        }
        public void ShouldNotParseCaseElseWhenNotAtTheEnd()
        {
            // Arrange
            var grammar = new CicodeGrammar();
            var parser = new Parser(grammar);
            var sourceCode =
            @"
            FUNCTION A()

            SELECT CASE a
            CASE 1
            A();
            CASE ELSE
            A();
            CASE 1
            A();
            END SELECT

            END
            ";
            // Act
            var parseTree = parser.Parse(sourceCode);

            // Assert
            Assert.IsNotNull(parseTree);
            Assert.IsTrue(parseTree.HasErrors());
            // A parser error is expected at line 7 because of the CASE ELSE clause which is not at then end of the case list
            Assert.AreEqual<int>(1, parseTree.ParserMessages.Where(m => m.Location.Line == 7).Count());
        }
        protected static MtCompiler InternCreateScriptApp(Stream output, Stream debugStream)
        {
            var grammar = new MtGrammar();
            var lang = new LanguageData(grammar);
            var parser = new Parser(grammar);
            var runtime = grammar.CreateRuntime(lang) as MultiTasksRuntime;

            if (output != null)
            {
                runtime.OutputStream = output;
            }
            #if DEBUG && !SILVERLIGHT

            if (debugStream != null)
            {
                // Add as a listener to debug
                var listener = new TextWriterTraceListener(debugStream);
                Debug.Listeners.Add(listener);
            }

            #endif
            var app = new ScriptApp(runtime);

            // Add constants
            app.Globals.Add("TRUE", MtResult.True);
            app.Globals.Add("FALSE", MtResult.False);

            #if DEBUG && !SILVERLIGHT
            MultiTasksRuntime.DebugDisplayInfo();
            #endif

            return new MtCompiler(app);
        }
        public override object Read(string path)
        {
            var rawData = new FvSolutionData();
            string txt;
            using (var reader = new StreamReader(path))
            {
                txt = reader.ReadToEnd();
            }

            var grammar = new OpenFoamGrammar();
            var parser = new Parser(grammar);
            var tree = parser.Parse(txt);

            foreach (ParseTreeNode rootEntryNode in tree.Root.FindDictEntries(null))
            {
                var identifier = rootEntryNode.GetEntryIdentifier();
                switch (identifier)
                {
                    case "options":
                        ParseOptions(rootEntryNode.ChildNodes[2], rawData);
                        break;
                    case "solvers":
                        ParseSolvers(rootEntryNode.ChildNodes[2], rawData);
                        break;
                }
            }
            return rawData;
        }
 public void SetUp()
 {
     var grammar = new CustomDslGrammar();
     var language = new LanguageData(grammar);
     _parser = new Parser(language);
     _visitor = new CSharpExpressionBuilder();
 }
        public void TestConflictGrammarWithHintsOnRules()
        {
            var grammar = new ConflictGrammarWithHintsInRules();
            var parser = new Parser(grammar);
            Assert.True(parser.Language.Errors.Count == 0);
            // Field sample
            var sample = FieldSample;
            var tree = parser.Parse(sample);
            Assert.NotNull(tree);
            Assert.False(tree.HasErrors());

            Assert.NotNull(tree.Root);
            var term = tree.Root.Term as NonTerminal;
            Assert.NotNull(term);
            Assert.Equal("definition", term.Name);

            Assert.Equal(1, tree.Root.ChildNodes.Count);
            var modNode = tree.Root.ChildNodes[0].ChildNodes[0];
            Assert.Equal("fieldModifier", modNode.Term.Name);

            //Property 
            sample = PropertySample;
            tree = parser.Parse(sample);
            Assert.NotNull(tree);
            Assert.False(tree.HasErrors());

            Assert.NotNull(tree.Root);
            term = tree.Root.Term as NonTerminal;
            Assert.NotNull(term);
            Assert.Equal("definition", term.Name);

            Assert.Equal(1, tree.Root.ChildNodes.Count);
            modNode = tree.Root.ChildNodes[0].ChildNodes[0];
            Assert.Equal("propModifier", modNode.Term.Name);
        }
        public override object Read(string path)
        {
            var rawData = new DecomposeParDictData();
            string txt;
            using (var reader = new StreamReader(path))
            {
                txt = reader.ReadToEnd();
            }

            var grammar = new OpenFoamGrammar();
            var parser = new Parser(grammar);
            var tree = parser.Parse(txt);

            foreach (ParseTreeNode rootEntryNode in tree.Root.FindDictEntries(null))
            {
                var identifier = rootEntryNode.GetEntryIdentifier();
                switch (identifier)
                {
                    case "numberOfSubdomains":
                        rawData.numberOfSubdomains = rootEntryNode.GetBasicValInt();
                        break;
                }
            }
            return rawData;
        }
Example #21
0
        public override object Read(string path)
        {
            var rawData = new VelocityData();
            string txt;
            using (var reader = new StreamReader(path))
            {
                txt = reader.ReadToEnd();
            }

            var grammar = new OpenFoamGrammar();
            var parser = new Parser(grammar);
            var tree = parser.Parse(txt);

            foreach (ParseTreeNode rootEntryNode in tree.Root.FindDictEntries(null))
            {
                var identifier = rootEntryNode.GetEntryIdentifier();
                switch (identifier)
                {
                    //case "turbineArrayOn":
                    //    rawData.TurbineArrayOn = rootEntryNode.GetBasicValBool();
                    //    break;
                }
            }
            return rawData;
        }
Example #22
0
 public ScriptInterpreter(LanguageData language) {
   Language = language;
   Runtime = Language.Grammar.CreateRuntime(Language);
   Parser = new Parser(Language);
   EvaluationContext = new EvaluationContext(Runtime);
   Status = _internalStatus = InterpreterStatus.Ready;
 }
        public override object Read(string path)
        {
            var obj = new AirfoilPropertiesInstance();
            string text = Load(path);

            var grammar = new OpenFoamGrammar();
            var parser = new Parser(grammar);
            var tree = parser.Parse(text);
            obj.row = new List<Vertice>();

            foreach (ParseTreeNode rootEntryNode in tree.Root.FindDictEntries(null))
            {
                var id = rootEntryNode.GetEntryIdentifier();
                if (id == "airfoilData")
                {
                    var dict = rootEntryNode.GetDictContent().ChildNodes[1];
                    foreach (ParseTreeNode t in dict.ChildNodes)
                    {
                        var array_head = t.ChildNodes[0].ChildNodes[1].ChildNodes;
                        var v = new Vertice
                                    {
                                        X = Convert.ToDecimal(array_head[0].ChildNodes[0].Token.Value),
                                        Y = Convert.ToDecimal(array_head[1].ChildNodes[0].Token.Value),
                                        Z = Convert.ToDecimal(array_head[2].ChildNodes[0].Token.Value)
                                    };
                        obj.row.Add( v );
                    }
                }
            }
            obj.airfoilName = FileName;
            return obj;
        }
Example #24
0
 //Default constructor, creates default evaluator 
 public ExpressionEvaluator(ExpressionEvaluatorGrammar grammar) {
   Grammar = grammar;
   Language = new LanguageData(Grammar);
   Parser = new Parser(Language);
   Runtime = Grammar.CreateRuntime(Language);
   App = new ScriptApp(Runtime);
 }
            /// <summary>
            /// Insert the string from the include file inside the include directive tag
            /// </summary>
            /// <remarks></remarks>
            private void ExpandIncludeFiles(ParseTreeNode rootNode, T4Grammar grammar, Parser parser)
            {
                var includes = from n in rootNode.ChildNodes
                                   let segmentChild = n.ChildNodes.First()
                                   where segmentChild.Term == grammar.Directive
                                   let dir = segmentChild
                                   let dirName = grammar.GetDirectiveName(dir)
                                   where T4Grammar.IsEqualText(Convert.ToString(dirName), "include")
                                   select dir;
                var includeFiles = from incDir in includes
                                       let filepath = grammar.GetDirectiveAttributes(incDir)["File"]
                                       select new
                                   { DirectiveNode = incDir, File = GetIncludeFile(Convert.ToString(filepath)) };
                foreach (var incFile in includeFiles)
                {
                    //do not expand the same files, in case there's a multiple nested references to the same include files
                    if (_expandedFiles.Contains(incFile.File.FullName)) continue;
                    var text = File.ReadAllText(Convert.ToString(incFile.File.FullName));
                    var content = grammar.GetContentNode(parser.Parse(text));
                    incFile.DirectiveNode.Tag = new TranslationTag { ExpandedParseTreeNode = content };
                    //remember expanded file
                    _expandedFiles.Add(incFile.File.FullName);

                    //recursively process content from include file
                    ExpandIncludeFiles(content, grammar, parser);
                }
            }
        public override object Read(string path)
        {
            var obj = new AirfoilPropertiesData();
            string text;
            using (var reader = new StreamReader(path))
            {
                text = reader.ReadToEnd();
            }
            var grammar = new OpenFoamGrammar();
            var parser = new Parser(grammar);
            var tree = parser.Parse(text);
            obj.airfoilData = new List<Vertice>();

            foreach (ParseTreeNode rootEntryNode in tree.Root.FindDictEntries(null))
            {
                var id = rootEntryNode.GetEntryIdentifier();
                if (id == "airfoilData")
                {
                    var dict = rootEntryNode.GetDictContent().ChildNodes[1];
                    for (int i = 0; i < dict.ChildNodes.Count; i++)
                    {
                        var array_head = dict.ChildNodes[i].ChildNodes[0].ChildNodes[1].ChildNodes;
                        var v = new Vertice();
                        v.X = Convert.ToDecimal(array_head[0].ChildNodes[0].Token.Value);
                        v.Y = Convert.ToDecimal(array_head[1].ChildNodes[0].Token.Value);
                        v.Z = Convert.ToDecimal(array_head[2].ChildNodes[0].Token.Value);
                        obj.airfoilData.Add( v );
                    }
                }
            }
            return obj;
        }
Example #27
0
 public FileAssembler(Grammar grammar, ISegmentFactory segmentBuilder, IExecutableFactory executableBuilder)
 {
     this.executableBuilder = executableBuilder;
     this.segmentBuilder = segmentBuilder;
     this.ironyGrammar = grammar;
     this.ironyParser = new Parser(ironyGrammar);
 }
        public FSharpParser()
            : base()
        {
            grammar = new FSharpGrammar();
            parser = new Parser(new LanguageData(grammar));

        }
Example #29
0
 public void TestCase()
 {
     GLSLGrammar lang = new GLSLGrammar ();
     var compiler = new Irony.Parsing.Parser(lang);
     var tree = compiler.Parse ("void main(void) { float v = vec3(1,1,1); }");
     //CheckNodes (tree.Root, 0);
 }
Example #30
0
 public void StructTest01()
 {
     GLSLGrammar lang = new GLSLGrammar ();
     var compiler = new Irony.Parsing.Parser(lang);
     var tree = compiler.Parse ("struct Camera { float x;}");
     //CheckNodes (tree.Root, 0);
 }
        public override object Read(string path)
        {
            var obj = new DecomposeParDictData();
            string txt = Load(path);

            var grammar = new OpenFoamGrammar();
            var parser = new Parser(grammar);
            var tree = parser.Parse(txt);

            foreach (ParseTreeNode rootEntryNode in tree.Root.FindDictEntries(null))
            {
                var identifier = rootEntryNode.GetEntryIdentifier();
                switch (identifier)
                {
                    case "numberOfSubdomains":
                        obj.numberOfSubdomains = rootEntryNode.GetBasicValInt();
                        break;
                    case "method":
                        obj.method = rootEntryNode.GetBasicValEnum<DecompositionMethod>();
                        break;
                    case "hierarchicalCoeffs":
                        obj.hCoefs = GetHierarchicalCoeffs(rootEntryNode.GetDictContent());
                        break;
                }
            }
            return obj;
        }
Example #32
0
        public void HandleFile(string file)
        {
            if (!System.IO.File.Exists(file))
                return;
			try
			{
				FileStream fileStream = System.IO.File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

				using (StreamReader reader = new StreamReader(fileStream))
				{
					var parser = new Parser(LuaGrammar.Instance);
					var tree = parser.Parse(reader.ReadToEnd());
					var root = tree.Root;
					if (root != null)
					{
						File = new LuaFile(file, tree.Tokens);
						RefreshTree(root);
						FileManager.Instance.AddFile(File);
					}
					else
					{
						System.Diagnostics.Debug.Print("***********error***********" + file);
					}
				}
			}
			catch(Exception e) 
			{
				BabePackage.Setting.LogError("open file failed:" + e.GetType().FullName);
			}
        }
Example #33
0
 /// <summary>
 /// Prevents a default instance of the <see cref="ShaderParser"/> class from being created.
 /// </summary>
 /// <param name="languageData">The language data.</param>
 /// <param name="root">The root of the language.</param>
 private ShaderParser(ShaderLanguageData languageData, NonTerminal root)
 {
     LanguageData = languageData;
     Grammar      = (ShaderGrammar)languageData.Grammar;
     Tokenizer    = new Tokenizer(languageData);
     Parser       = new Irony.Parsing.Parser(languageData, null, root);
 }
Example #34
0
		static int Main(string[] args)
		{
			try
			{
				bool mode2 = (((args.Length >= 3) ? args[2] : "") == "mode2");

				Console.WriteLine("Create grammar");
				var grammar = new XbnfGrammar(mode2 ? XbnfGrammar.Mode.HttpCompatible : XbnfGrammar.Mode.Strict);

				Console.WriteLine("Create parser");
				var parser = new Parser(grammar);

				Console.WriteLine("Read XBNF from {0}", args[0]);
				var xbnf = File.ReadAllText(args[0]);

				Console.WriteLine("Optimize");
				var oprimized = Optimize(xbnf);

				Console.WriteLine("Parse");
				var tree = parser.Parse(oprimized, "<source>");

				Console.WriteLine("Convert to C#");
				var csharp = grammar.RunSample(tree);

				Console.WriteLine("Write C# to {0}", args[1]);
				File.WriteAllText(args[1], AddHeaderFooter(csharp));
			}
			catch (Exception ex)
			{
				Console.Write(ex.ToString());
				return -1;
			}
			return 0;
		}
Example #35
0
        private SourceUnitTree Parse(SourceUnit sourceUnit, bool allowSingle, out bool isExpression)
        {
            isExpression = false;
            IronyParser parser = new IronyParser(allowSingle ? singleStatement : fullGrammar);
            parser.Context.Mode = ParseMode.CommandLine;

            scopes = new Stack<LexicalScopeBuilder>();
            EnterTopLevelScope();
            try
            {
                var parsedScript = parser.Parse(sourceUnit.GetCode());
                if (parsedScript.HasErrors())
                {
                    sourceUnit.CodeProperties = ScriptCodeParseResult.Invalid;
                    return null;
                }

                if (sourceUnit.Kind == SourceCodeKind.InteractiveCode && parser.Context.Status == ParserStatus.AcceptedPartial)
                {
                    sourceUnit.CodeProperties = ScriptCodeParseResult.IncompleteStatement;
                    return null;
                }

                sourceUnit.CodeProperties = ScriptCodeParseResult.Complete;
                return BuildSourceTree(parsedScript.Root, sourceUnit, allowSingle, out isExpression);
            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                LeaveScope();
            }
        }
Example #36
0
        /// <summary>
        /// retorna null si ocurrio un error al leer el archivo.
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        private StaticEntity CreateStaticEntity(string path)
        {
            var src       = File.ReadAllText(path);
            var grammar   = new PyUsacGrammar();
            var langData  = new LanguageData(grammar);
            var parser    = new Irony.Parsing.Parser(langData);//Para evitar conflicto con el namespace Parser
            var parseTree = parser.Parse(src);

            bool hasErrors = false;

            foreach (var error in parseTree.ParserMessages)
            {
                if (error.Level == Irony.ErrorLevel.Error)
                {
                    hasErrors = true;
                }
                ErrorFactory.CreateParsingError(error, path);
            }
            if (hasErrors)
            {
                return(null);
            }

            var astBuilder = new PyAstBuilder(new AstContext(langData), path);

            astBuilder.BuildAst(parseTree);
            var programNode = (ProgramNode)parseTree.Root.AstNode;

            var entity = new StaticEntity(programNode);

            entity.InitVisitor(true);

            return(entity);
        }
        public override object Read(string path)
        {
            var obj = new RefineMeshDictData();
            string txt;
            using (var reader = new StreamReader(path))
            {
                txt = reader.ReadToEnd();
            }
            var grammar = new OpenFoamGrammar();
            var parser = new Parser(grammar);
            var tree = parser.Parse(txt);

            foreach (ParseTreeNode rootEntryNode in tree.Root.FindDictEntries(null))
            {
                var identifier = rootEntryNode.GetEntryIdentifier();
                string patch;
                switch ( identifier )
                {
                    case "set":
                        obj.setvalue = rootEntryNode.GetBasicValString();
                        break;
                    case "coordinateSystem":
                        obj.coordsys = rootEntryNode.GetBasicValEnum<CoordinateSystem>();
                        break;
                    case "globalCoeffs":
                        {
                            var dict = rootEntryNode.GetDictContent();
                            obj.globalCoeffs = GetCoeffs(ref dict, out patch);
                        }
                        break;
                    case "patchLocalCoeffs":
                        {
                            var dict = rootEntryNode.GetDictContent();
                            obj.patchLocalCoeffs = GetCoeffs(ref dict, out patch);
                            obj.patch = patch;
                        }
                        break;
                    case "directions":
                        {
                            obj.direction = new List<DirectionType>();
                            var s = rootEntryNode.ChildNodes[2].ChildNodes[1].GetArrayOfString();
                            foreach (string t in s)
                            {
                                obj.direction.Add(t.ToEnum<DirectionType>());
                            }
                        }
                        break;
                    case "useHexTopology":
                        obj.useHexTopology = rootEntryNode.GetBasicValBool();
                        break;
                    case "geometricCut":
                        obj.geometricCut = rootEntryNode.GetBasicValBool();
                        break;
                    case "writeMesh":
                        obj.writeMesh = rootEntryNode.GetBasicValBool();
                        break;
                }
            }
            return obj;
        }
Example #38
0
        public SqlDefaultParser(SqlGrammarBase grammar)
        {
            languageData = new LanguageData(grammar);
            parser       = new Irony.Parsing.Parser(languageData);

            if (!languageData.CanParse())
            {
                throw new InvalidOperationException();
            }
        }
        public void Setup()
        {
            var grammar  = new XamlMarkupExtensionGrammar();
            var language = new LanguageData(grammar);

            this.parser = new Irony.Parsing.Parser(language)
            {
                Context = { TracingEnabled = true }
            };
        }
Example #40
0
        internal LuaClassifier(ITextBuffer buffer,
                               IClassificationTypeRegistryService typeService)
        {
            _buffer = buffer;

            _grammar             = LuaGrammar.Instance;
            _parser              = new Irony.Parsing.Parser(_grammar);
            _parser.Context.Mode = Irony.Parsing.ParseMode.VsLineScan;

            InitializeLineStates(_buffer.CurrentSnapshot);
        }
        public MarkupExtensionParser()
        {
            var grammar  = new XamlMarkupExtensionGrammar();
            var language = new LanguageData(grammar);

            this.parser = new Irony.Parsing.Parser(language)
#if DEBUG
            {
                Context = { TracingEnabled = true }
            }
#endif
            ;
        }
Example #42
0
        public ParseTreeNode getRoot(string sourceCode, Grammar grammar)
        {
            LanguageData language = new LanguageData(grammar);

            Irony.Parsing.Parser parser    = new Irony.Parsing.Parser(language);
            ParseTree            parseTree = parser.Parse(sourceCode);
            ParseTreeNode        root      = parseTree.Root;

            if (root == null)
            {
                throw new Exception(parseTree.ParserMessages[0].ToString());
            }
            return(root);
        }
Example #43
0
        public static bool InterpretFromFileName(string filePath)
        {
            string src;

            try
            {
                src = File.ReadAllText(filePath);
            }
            catch (Exception)
            {
                //Reprotar error de preprocesador
                return(false);
            }
            var grammar   = new PyUsacGrammar();
            var langData  = new LanguageData(grammar);
            var parser    = new Irony.Parsing.Parser(langData);//Para evitar conflicto con el namespace Parser
            var parseTree = parser.Parse(src);
            var root      = parseTree.Root;

            bool hasErrors = false;

            foreach (var error in parseTree.ParserMessages)
            {
                if (error.Level == Irony.ErrorLevel.Error)
                {
                    hasErrors = true;
                }
                ErrorHelper.ErrorFactory.CreateParsingError(error, filePath);
            }
            if (hasErrors)
            {
                return(false);
            }
            //var dotCode = GetDot(root);//Descomentar en debug mode! :)

            var astBuilder = new PyAstBuilder(new AstContext(langData), filePath);

            astBuilder.BuildAst(parseTree);
            var programNode = (ProgramNode)parseTree.Root.AstNode;
            //GetDot(programNode);//Descomentar en debug mode! :)

            var entity = new StaticEntity(programNode);

            entity.InitVisitor(true);
            entity.InvokeMain();
            TypeConstants.ClearTypeHashtable();

            return(true);
        }
Example #44
0
        public Hub ParseString(string text)
        {
            var grammar  = new HubGrammar();
            var language = new LanguageData(grammar);

            var parser = new Irony.Parsing.Parser(language);

            var tree = parser.Parse(text);

            var hub = new Hub
            {
                Id      = tree.Root.ChildNodes.Single(x => x.Term.Name == "Name").ChildNodes[0].Token.ValueString,
                Name    = tree.Root.ChildNodes.Single(x => x.Term.Name == "Name").ChildNodes[1].Token.ValueString,
                HubType = tree.Root.ChildNodes.Single(x => x.Term.Name == "HubType").ChildNodes[0].Token.ValueString,
                Lines   = new List <string>(tree.Root.ChildNodes.Where(x => x.Term.Name == "Line").Select(x => x.ChildNodes[0].Token.ValueString)),
                Lores   = new List <string>(tree.Root.ChildNodes.Where(x => x.Term.Name == "Lore").Select(x => x.ChildNodes[0].Token.ValueString)),
            };

            return(hub);
        }
		bool ParseJava (string javaSourceText)
		{
			var parser = new Irony.Parsing.Parser (grammar);
			var result = parser.Parse (javaSourceText);
			foreach (var m in result.ParserMessages)
				Console.WriteLine ($"{m.Level} {m.Location} {m.Message}");
			if (result.HasErrors ())
				return false;
			var parsedPackage = (JavaPackage)result.Root.AstNode;
			FlattenNestedTypes (parsedPackage);
			var pkg = api.Packages.FirstOrDefault (p => p.Name == parsedPackage.Name);
			if (pkg == null) {
				api.Packages.Add (parsedPackage);
				pkg = parsedPackage;
			} else
				foreach (var t in parsedPackage.Types)
					pkg.Types.Add (t);
			pkg.Types = pkg.Types.OrderBy (t => t.Name).ToList ();
			return true;
		}
Example #46
0
 public void GetDeclarations(LuaBlockNode block, LuaModel model)
 {
     // require("sample.lua")
     if (Target != null &&
         (Target.AsString == "require" ||
          Target.AsString == "NPL.load") &&
         Arguments.ChildNodes.Count == 1 &&
         Arguments.ChildNodes[0] is LuaLiteralNode &&
         ((LuaLiteralNode)Arguments.ChildNodes[0]).Type == LuaType.String)
     {
         string fileName = ((LuaLiteralNode)Arguments.ChildNodes[0]).Value;
         fileName = fileName.Substring(1, fileName.Length - 2);
         try
         {
             string filePath = Path.Combine(Path.GetDirectoryName(model.FilePath), fileName);
             // project mode
             if (model.Entry != null && model.Entry.Analyzer != null && model.Entry.Analyzer.ContainsFile(filePath))
             {
                 AnalysisEntry requiredEntry = model.Entry.Analyzer.GetAnalysisEntry(filePath);
                 if (requiredEntry.Model != null)
                 {
                     block.Requires.AddRange(requiredEntry.Model.GetGlobalDeclarations());
                     model.AddIncludedFile(filePath, requiredEntry.Model);
                 }
             }
             // singleton mode
             else
             {
                 string source = File.ReadAllText(filePath);
                 Irony.Parsing.Parser parser        = new Irony.Parsing.Parser(LuaGrammar.Instance);
                 ParseTree            tree          = parser.Parse(source);
                 LuaModel             requiredModel = new LuaModel(tree, filePath);
                 block.Requires.AddRange(requiredModel.GetGlobalDeclarations());
                 model.AddIncludedFile(filePath, requiredModel);
             }
         }
         catch (Exception e)
         {
         }
     }
 }
Example #47
0
        static void Main(string[] args)
        {
            LanguageData LangData = new LanguageData(new UIGrammar());

            Irony.Parsing.Parser Pars = new Irony.Parsing.Parser(LangData);

            StringBuilder SBuilder = new StringBuilder();

            foreach (string Statement in File.ReadLines("C:\\Program Files\\Maxis\\The Sims Online\\TSOClient\\gamedata\\uiscripts\\personselection.uis"))
            {
                SBuilder.Append(Statement + "\r\n");
            }

            Debug.WriteLine("Attempting to parse: " + SBuilder.ToString());
            ParseTree Tree = Pars.Parse(SBuilder.ToString());

            //DisplayTree((AstNode)Tree.Root.AstNode, 0);
            WalkTree(Tree.Root);

            Console.ReadLine();
        }
Example #48
0
        public DeclaredObjectContainer Parse(string programFile)
        {
            string program = System.IO.File.ReadAllText(programFile);

            var grammar = new BDVHDLGrammar();
            var parser  = new Irony.Parsing.Parser(grammar);
            var ast     = parser.Parse(program);

            if (ast.Status != ParseTreeStatus.Parsed)
            {
                throw new ParserException(programFile, ast.Status, ast.ParserMessages);
            }

            (new TreeConverter()).Convert(ast, grammar);

            var evaluator = new NodeEvaluator();

            evaluator.EvaluateGeneral(ast.Root);

            return(evaluator.declaredObjects);
        }
Example #49
0
        public static bool TryParse(string sql, ISqlDialect dialect, string tablePrefix, IDictionary <string, object> parameters, out string query, out IEnumerable <string> messages)
        {
            try
            {
                var tree = new Irony.Parsing.Parser(language).Parse(sql);

                if (tree.HasErrors())
                {
                    query = null;

                    messages = tree
                               .ParserMessages
                               .Select(x => $"{x.Message} at line:{x.Location.Line}, col:{x.Location.Column}")
                               .ToArray();

                    return(false);
                }

                var sqlParser = new SqlParser(tree, dialect, tablePrefix, parameters);
                query = sqlParser.Evaluate();

                messages = Array.Empty <string>();

                return(true);
            }
            catch (SqlParserException se)
            {
                query    = null;
                messages = new string[] { se.Message };
            }
            catch (Exception e)
            {
                query    = null;
                messages = new string[] { "Unexpected error: " + e.Message };
            }

            return(false);
        }
Example #50
0
 private void Dispose(bool disposing)
 {
     parser       = null;
     languageData = null;
 }
Example #51
0
 public Parser(NonTerminal startTerm)
 {
     grammar        = new Grammar();
     languageData   = new LanguageData(grammar);
     internalParser = new Irony.Parsing.Parser(languageData, startTerm);
 }
Example #52
0
        /// <summary>
        /// Format selection block
        /// </summary>
        /// <param name="entry"></param>
        /// <param name="textView"></param>
        internal void FormatBlock(ITextView textView)
        {
            int startLine = textView.Selection.Start.Position.GetContainingLine().LineNumber;
            int endLine   = textView.Selection.End.Position.GetContainingLine().LineNumber;

            ITextSnapshot snapshot = textView.TextSnapshot;

            int[]  indentations;
            bool[] fixedLines;
            this.Model.RetrieveIndentationsFromSyntaxTree(out indentations, out fixedLines);

            //Get long-string and block-comment spans, which are not to be added or removed indentations


            //Need a new parser and scanner here
            Irony.Parsing.Parser  parser  = new Irony.Parsing.Parser(IronyParser.LuaGrammar.Instance);
            Irony.Parsing.Scanner scanner = parser.Scanner;

            //rule 1: insert space before and after binary operator if there not any
            //rule 2: insert space after comma, semicolon if there not any
            //rule 3: indentation increase inside block
            //rule 4: multiple spaces replaced by a single space
            using (var edit = textView.TextBuffer.CreateEdit())
            {
                //IEnumerable<ITextSnapshotLine> lines = view.TextSnapshot.Lines;
                for (int lineNumber = startLine; lineNumber <= endLine; lineNumber++)
                {
                    if (fixedLines[lineNumber])
                    {
                        continue;
                    }

                    ITextSnapshotLine line = snapshot.GetLineFromLineNumber(lineNumber);
                    int    lineOffset      = line.Start.Position;
                    string lineText        = line.GetText();

                    scanner.VsSetSource(lineText, 0);

                    int state = 0;
                    Irony.Parsing.Token currentToken = scanner.VsReadToken(ref state);
                    Irony.Parsing.Token lastToken    = null;
                    // add space before the first token
                    if (currentToken != null)
                    {
                        Span   editSpan    = new Span(lineOffset, currentToken.Location.Position);
                        string indentation = "";
                        for (int i = 0; i < indentations[lineNumber]; ++i)
                        {
                            indentation += "\t";
                        }
                        edit.Replace(editSpan, indentation);
                    }

                    while (currentToken != null && currentToken.Terminal.Name != "SYNTAX_ERROR")
                    {
                        Irony.Parsing.Token nextToken = scanner.VsReadToken(ref state);
                        if (currentToken.Text == "+" ||
                            currentToken.Text == "=" ||
                            currentToken.Text == "*" ||
                            currentToken.Text == "\\" ||
                            currentToken.Text == "-")
                        {
                            if (lastToken != null)
                            {
                                int spaceStart  = lastToken.Location.Position + lastToken.Length;
                                int spaceLength = currentToken.Location.Position - spaceStart;
                                if (spaceLength != 1)
                                {
                                    Span span = new Span(lineOffset + spaceStart, spaceLength);
                                    edit.Replace(span, " ");
                                }
                            }

                            if (nextToken != null)
                            {
                                int spaceStart  = currentToken.Location.Position + currentToken.Length;
                                int spaceLength = nextToken.Location.Position - spaceStart;
                                if (spaceLength != 1)
                                {
                                    Span span = new Span(lineOffset + spaceStart, spaceLength);
                                    edit.Replace(span, " ");
                                }
                            }
                        }
                        else if (currentToken.Text == "," ||
                                 currentToken.Text == ";")
                        {
                            if (nextToken != null)
                            {
                                int spaceStart  = currentToken.Location.Position + currentToken.Length;
                                int spaceLength = nextToken.Location.Position - spaceStart;
                                if (spaceLength != 1)
                                {
                                    Span span = new Span(lineOffset + spaceStart, spaceLength);
                                    edit.Replace(span, " ");
                                }
                            }
                        }

                        lastToken    = currentToken;
                        currentToken = nextToken;
                    }
                }
                edit.Apply();
            }
        }
Example #53
0
 public Parser()
 {
     grammar        = new Grammar();
     languageData   = new LanguageData(grammar);
     internalParser = new Irony.Parsing.Parser(languageData);
 }
Example #54
0
        public IronyParser()
        {
            parser = new Irony.Parsing.Parser(new Pie.IronyParsing.Rules.PieGrammar());

            buildDelegates = new Dictionary <string, BuildExpressionDelegate>()
            {
                { "namespace", NamespaceBuilder.BuildNamespace },
                { "class_declaration", ClassBuilder.BuildClass },
                { "explicit_variable_declaration", BuildExplicitVariableDeclaration },
                { "method_declaration", MethodDeclarationBuilder.BuildMethodDeclaration },
                { "typed_method_declaration", MethodDeclarationBuilder.BuildMethodDeclaration },
                { "return", BuildReturn },
                { "return_value", BuildReturnValue },
                { "identifier", BuildVariableReference },
                { "number_literal", LiteralBuilder.BuildLiteral },
                { "string_literal", LiteralBuilder.BuildLiteral },
                { "char_literal", LiteralBuilder.BuildLiteral },
                { "null_literal", LiteralBuilder.BuildNullLiteral },
                { "bool_literal", LiteralBuilder.BuildBoolLiteral },
                { "assignment", OperatorBuilder.BuildAssignment },
                { "method_invocation", MethodInvocationBuilder.BuildMethodInvocation },
                { "generic_method_invocation", MethodInvocationBuilder.BuildGenericMethodInvocation },
                { "binary_operator", OperatorBuilder.BuildBinaryOperator },
                { "unary_operator", OperatorBuilder.BuildUnaryOperator },
                { "import", NamespaceBuilder.BuildImport },
                { "simple_if_block", IfBuilder.BuildIfBlock },
                { "bodied_if_block", IfBuilder.BuildIfBlock },
                { "enum_declaration", EnumBuilder.BuildEnum },
                { "simple_for_loop", ForLoopBuilder.BuildForLoop },
                { "bodied_for_loop", ForLoopBuilder.BuildForLoop },
                { "simple_while_loop", WhileLoopBuilder.BuildWhileLoop },
                { "bodied_while_loop", WhileLoopBuilder.BuildWhileLoop },
                { "out_argument", BuildDirectionedArgument },
                { "class_variable", ClassBuilder.BuildClassVariable },
                { "constructor_declaration", ConstructorBuilder.BuildConstructor },
                { "instantiation", InstantiationBuilder.BuildInstantiation },
                { "property_declaration", PropertyBuilder.BuildProperty },
                { "value", PropertyBuilder.BuildValue },
                { "exception_handling_block", ExceptionHandlerBuilder.BuildExceptionHandler },
                { "throw_statement", ExceptionHandlerBuilder.BuildThrow },
                { "switch_block", SwitchBlockBuilder.BuildSwitchBlock },
                { "case_block", SwitchBlockBuilder.BuildCaseBlock },
                { "delegate_declaration", DelegateBuilder.BuildDelegate },
                { "event_member", DelegateBuilder.BuildEvent },
                { "indexed_identifier", IndexedIdentifierBuilder.BuildIndexedIdentifier },
                { "interface_declaration", InterfaceBuilder.BuildInterface },
                { "method_interface", InterfaceMethodBuilder.BuildInterfaceMethod },
                { "property_interface", InterfacePropertyBuilder.BuildInterfaceProperty }
            };

            aliases = new Dictionary <string, string>();
            aliases.Add("int", "System.Int32");
            aliases.Add("long", "System.Int64");
            aliases.Add("float", "System.Single");
            aliases.Add("double", "System.Double");
            aliases.Add("decimal", "System.Decimal");
            aliases.Add("short", "System.Int16");
            aliases.Add("byte", "System.Byte");
            aliases.Add("char", "System.Char");
            aliases.Add("uint", "System.UInt32");
            aliases.Add("ushort", "System.UInt16");
            aliases.Add("ulong", "System.UInt64");
            aliases.Add("bool", "System.Boolean");
            aliases.Add("object", "System.Object");
        }
 public GrammarFixture()
 {
     _parser = new Irony.Parsing.Parser(new CoffeescriptGrammar());
 }
Example #56
0
 public SolLewittParser()
 {
     Grammar = new SolLewittGrammar();
     Parser  = new Irony.Parsing.Parser(Grammar);
 }
Example #57
0
 public Parser()
 {
     language = new LanguageData(grammar);
     parser   = new Irony.Parsing.Parser(language);
 }
Example #58
0
 public void StructTest02()
 {
     GLSLGrammar lang     = new GLSLGrammar();
     var         compiler = new Irony.Parsing.Parser(lang);
     var         tree     = compiler.Parse("struct Camera { float x; int num; }; ");
 }
Example #59
0
 public void StructTest04()
 {
     GLSLGrammar lang     = new GLSLGrammar();
     var         compiler = new Irony.Parsing.Parser(lang);
     var         tree     = compiler.Parse("struct Camera { float x; int num; vec3 output; vec2 data[10]; vec4 grid[3][4]; bool samples[]; };");
 }