コード例 #1
0
        private static IParseTree ParseInternal(string code, IParseTreeListener[] listeners, out ITokenStream outStream)
        {
            var stream = new AntlrInputStream(code);
            var lexer  = new VBALexer(stream);
            var tokens = new CommonTokenStream(lexer);
            var parser = new VBAParser(tokens);

            parser.AddErrorListener(new ExceptionErrorListener());
            foreach (var l in listeners)
            {
                parser.AddParseListener(l);
            }

            outStream = tokens;
            return(parser.startRule());
        }
コード例 #2
0
        public string Format(string code)
        {
            var indenter     = new Indenter(null, () => new IndenterSettings(true));
            var indentedCode = indenter.Indent(code.Split('\n').Select(line => line.Replace("\r", string.Empty))).ToArray();

            var builder = new StringBuilder();
            var tokens  = Tokenize(string.Join("\n", indentedCode));

            var parser = new VBAParser(tokens)
            {
                Interpreter = { PredictionMode = PredictionMode.Ll }
            };
            var listeners = new IntervalListener[]
            {
                new CommentIntervalsListener(),
                new AnnotationIntervalsListener(),
                new AttributeIntervalsListener(),
                new AttributeValueIntervalsListener(),
            };

            foreach (var listener in listeners)
            {
                parser.AddParseListener(listener);
            }
            parser.startRule();

            for (var i = 0; i < tokens.Size; i++)
            {
                var token    = tokens.Get(i);
                var listener = listeners
                               .Select(e =>
                                       new
                {
                    IsValidInterval = e.IsValidInterval(token, out var interval),
                    Interval        = interval,
                    Class           = e.Class
                })
コード例 #3
0
        private static IParseTree ParseInternal(string code, IParseTreeListener[] listeners, out ITokenStream outStream)
        {
            var stream = new AntlrInputStream(code);
            var lexer = new VBALexer(stream);
            var tokens = new CommonTokenStream(lexer);
            var parser = new VBAParser(tokens);

            parser.AddErrorListener(new ExceptionErrorListener());
            foreach (var l in listeners)
            {
                parser.AddParseListener(l);
            }

            outStream = tokens;
            return parser.startRule();
        }