예제 #1
0
        /// <summary>
        /// Get parsed tree node from text by antlr4 engine.
        /// </summary>
        /// <param name="text">Original text which will be parsed.</param>
        /// <returns>Parsed tree node.</returns>
        private static LGFileParser.FileContext GetFileContentContext(string text, string source)
        {
            if (string.IsNullOrEmpty(text))
            {
                return(null);
            }

            var input  = new AntlrInputStream(text);
            var lexer  = new LGFileLexer(input);
            var tokens = new CommonTokenStream(lexer);
            var parser = new LGFileParser(tokens);

            parser.RemoveErrorListeners();
            var listener = new ErrorListener(source);

            parser.AddErrorListener(listener);
            parser.BuildParseTree = true;

            return(parser.file());
        }
예제 #2
0
        /// <summary>
        /// Parse LG content and achieve the AST.
        /// </summary>
        /// <param name="resource">LG resource.</param>
        /// <returns>The abstract syntax tree of lg file.</returns>
        public static IParseTree AntlrParseTemplates(LGResource resource)
        {
            if (string.IsNullOrEmpty(resource.Content))
            {
                return(null);
            }

            var input = new AntlrInputStream(resource.Content);
            var lexer = new LGFileLexer(input);

            lexer.RemoveErrorListeners();

            var tokens = new CommonTokenStream(lexer);
            var parser = new LGFileParser(tokens);

            parser.RemoveErrorListeners();
            var listener = new ErrorListener(resource.FullName);

            parser.AddErrorListener(listener);
            parser.BuildParseTree = true;

            return(parser.file());
        }
        private static IParseTree AntlrParseTemplates(string text, string id)
        {
            if (string.IsNullOrEmpty(text))
            {
                return(null);
            }

            var input = new AntlrInputStream(text);
            var lexer = new LGFileLexer(input);

            lexer.RemoveErrorListeners();

            var tokens = new CommonTokenStream(lexer);
            var parser = new LGFileParser(tokens);

            parser.RemoveErrorListeners();
            var listener = new ErrorListener(id);

            parser.AddErrorListener(listener);
            parser.BuildParseTree = true;

            return(parser.file());
        }