Example #1
0
        public override void Load()
        {
            if (alreadyLoaded)
            {
                return;
            }

            alreadyLoaded = true;
            GroupParser parser = null;

            try
            {
                ANTLRStringStream fs = new ANTLRStringStream(text);
                fs.name = sourceName;
                GroupLexer        lexer  = new GroupLexer(fs);
                CommonTokenStream tokens = new CommonTokenStream(lexer);
                parser = new GroupParser(tokens);
                // no prefix since this group file is the entire group, nothing lives
                // beneath it.
                parser.group(this, "/");
            }
            catch (Exception e)
            {
                ErrorManager.IOError(null, ErrorType.CANT_LOAD_GROUP_FILE, e, "<string>");
            }
        }
Example #2
0
        /** Load full path name .st file relative to root by prefix */
        public virtual CompiledTemplate LoadTemplateFile(string prefix, string fileName)
        {
            if (Path.IsPathRooted(fileName))
            {
                throw new ArgumentException();
            }

            //System.out.println("load "+fileName+" from "+root+" prefix="+prefix);
            string templateName = Path.ChangeExtension(fileName, null);
            Uri    f            = null;

            try
            {
                f = new Uri(Path.Combine(root.LocalPath, fileName));
            }
            catch (UriFormatException me)
            {
                ErrorManager.RuntimeError(null, 0, ErrorType.INVALID_TEMPLATE_NAME, me, Path.Combine(root.LocalPath, fileName));
                return(null);
            }

            ANTLRReaderStream fs = null;

            try
            {
                fs = new ANTLRReaderStream(new StreamReader(f.LocalPath, Encoding ?? Encoding.UTF8));
            }
            catch (IOException)
            {
                // doesn't exist; just return null to say not found
                return(null);
            }

            GroupLexer lexer = new GroupLexer(fs);

            fs.name = fileName;
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            GroupParser       parser = new GroupParser(tokens);

            parser.Group = this;
            lexer.group  = this;
            try
            {
                parser.templateDef(prefix);
            }
            catch (RecognitionException re)
            {
                ErrorManager.GroupSyntaxError(ErrorType.SYNTAX_ERROR, Path.GetFileName(f.LocalPath), re, re.Message);
            }

            return(RawGetTemplate(templateName));
        }
 public override void LoadGroupFile(string prefix, string fileName)
 {
     try
     {
         ANTLRReaderStream antlrReaderStream = new ANTLRReaderStream((TextReader) new StreamReader(new Uri(fileName).LocalPath, this.Encoding));
         GroupLexer        groupLexer        = new GroupLexer((ICharStream)antlrReaderStream);
         antlrReaderStream.name = fileName;
         new GroupParser((ITokenStream) new CommonTokenStream((ITokenSource)groupLexer)).group(this, prefix);
     }
     catch (Exception ex)
     {
         ExceptionExtensions.PreserveStackTrace(ex);
         if (!ExceptionExtensions.IsCritical(ex))
         {
             this.ErrorManager.IOError((Template)null, ErrorType.CANT_LOAD_GROUP_FILE, ex, (object)fileName);
         }
         throw;
     }
     base.LoadGroupFile(prefix, fileName);
 }
Example #4
0
        /** Load a group file with full path fileName; it's relative to root by prefix. */
        public virtual void LoadGroupFile(string prefix, string fileName)
        {
            //System.out.println("load group file prefix="+prefix+", fileName="+fileName);
            GroupParser parser = null;

            try
            {
                Uri f = new Uri(fileName);
                ANTLRReaderStream fs    = new ANTLRReaderStream(new System.IO.StreamReader(f.LocalPath, Encoding ?? Encoding.UTF8));
                GroupLexer        lexer = new GroupLexer(fs);
                fs.name = fileName;
                CommonTokenStream tokens = new CommonTokenStream(lexer);
                parser = new GroupParser(tokens);
                parser.group(this, prefix);
            }
            catch (Exception e)
            {
                ErrorManager.IOError(null, ErrorType.CANT_LOAD_GROUP_FILE, e, fileName);
            }
        }
Example #5
0
        protected override void ReParseImpl()
        {
            var outputWindow = OutputWindowService.TryGetPane(PredefinedOutputWindowPanes.TvlIntellisense);

            try
            {
                Stopwatch stopwatch = Stopwatch.StartNew();

                var snapshot = TextBuffer.CurrentSnapshot;
                SnapshotCharStream         input  = new SnapshotCharStream(snapshot, new Span(0, snapshot.Length));
                GroupLexer                 lexer  = new GroupLexer(input);
                CommonTokenStream          tokens = new CommonTokenStream(lexer);
                GroupParserWrapper         parser = new GroupParserWrapper(tokens);
                List <ParseErrorEventArgs> errors = new List <ParseErrorEventArgs>();
                parser.ParseError += (sender, e) =>
                {
                    errors.Add(e);

                    string message = e.Message;

                    ITextDocument document;
                    if (TextBuffer.Properties.TryGetProperty(typeof(ITextDocument), out document) && document != null)
                    {
                        string fileName = document.FilePath;
                        var    line     = snapshot.GetLineFromPosition(e.Span.Start);
                        message = string.Format("{0}({1},{2}): {3}", fileName, line.LineNumber + 1, e.Span.Start - line.Start.Position + 1, message);
                    }

                    if (message.Length > 100)
                    {
                        message = message.Substring(0, 100) + " ...";
                    }

                    if (outputWindow != null)
                    {
                        outputWindow.WriteLine(message);
                    }

                    if (errors.Count > 100)
                    {
                        throw new OperationCanceledException();
                    }
                };

                TemplateGroupWrapper group = new TemplateGroupWrapper('<', '>');
                parser.group(group, "/");
                TemplateGroupRuleReturnScope returnScope = BuiltAstForGroupTemplates(group);

                // Also parse the input using the V4 lexer/parser for downstream operations that make use of it
                IList <Antlr4.Runtime.IToken>   v4tokens;
                TemplateParser.GroupFileContext v4result = ParseWithAntlr4(snapshot, out v4tokens);

                OnParseComplete(new StringTemplateParseResultEventArgs(snapshot, errors, stopwatch.Elapsed, tokens.GetTokens(), returnScope, v4tokens, v4result));
            }
            catch (Exception e) when(!ErrorHandler.IsCriticalException(e))
            {
                try
                {
                    if (outputWindow != null)
                    {
                        outputWindow.WriteLine(e.Message);
                    }
                }
                catch (Exception ex2) when(!ErrorHandler.IsCriticalException(ex2))
                {
                }
            }
        }
Example #6
0
    public static void Main()
    {
        System.Console.WriteLine("Testing IntLexer:");
        List <string> test_int = new List <string> {
            "1", "123", "+123", "-123", "+a", "+", "+1233f", " "
        };

        foreach (var str in test_int)
        {
            IntLexer L = new IntLexer(str);
            System.Console.WriteLine(System.String.Format("{0} : {1}", str, L.Parse()));
        }

        System.Console.WriteLine("Testing IDLexer:");
        List <string> test_id = new List <string> {
            "a", "a1ad", "a___", "1", "", " "
        };

        foreach (var str in test_id)
        {
            IdLexer L = new IdLexer(str);
            System.Console.WriteLine(System.String.Format("{0} : {1}", str, L.Parse()));
        }

        System.Console.WriteLine("Testing NoZeroIntLexer:");
        List <string> test_nozeroint = new List <string> {
            "1", "123", "+123", "-123", "+0", "0", "-0", " "
        };

        foreach (var str in test_nozeroint)
        {
            NoZeroIntLexer L = new NoZeroIntLexer(str);
            System.Console.WriteLine(System.String.Format("{0} : {1}", str, L.Parse()));
        }


        System.Console.WriteLine("Testing LetterDigitLexer:");
        List <string> test_LetterDigitLexer = new List <string> {
            "a", "a1", "a1a", "1", "a11", "a1aa", "", " "
        };

        foreach (var str in test_LetterDigitLexer)
        {
            LetterDigitLexer L = new LetterDigitLexer(str);
            System.Console.WriteLine(System.String.Format("{0} : {1}", str, L.Parse()));
        }

        System.Console.WriteLine("Testing ListLexer:");
        List <string> test_ListLexer = new List <string> {
            "a", "a,", "a;b", "a,b;", "ab", "", ",", " "
        };

        foreach (var str in test_ListLexer)
        {
            ListLexer L = new ListLexer(str);
            System.Console.WriteLine(System.String.Format("{0} : {1} : {2}", str, L.Parse(), string.Join(",", L.chrs)));
        }


        System.Console.WriteLine("Testing DigitListLexer:");
        List <string> test_DigitListLexer = new List <string> {
            "1", "  1 2    3", "    ", "", "1 2 3 b"
        };

        foreach (var str in test_DigitListLexer)
        {
            DigitListLexer L = new DigitListLexer(str);
            System.Console.WriteLine(System.String.Format("{0} : {1} : {2}", str, L.Parse(), string.Join(",", L.ints)));
        }

        System.Console.WriteLine("Testing GroupLexer:");
        List <string> test_GroupLexer = new List <string> {
            "aa12c23dd1", "aaa12c23dd1", "aa12c232dd1", " ", ""
        };

        foreach (var str in test_GroupLexer)
        {
            GroupLexer L = new GroupLexer(str);
            System.Console.WriteLine(System.String.Format("{0} : {1} : {2}", str, L.Parse(), L.result));
        }


        System.Console.WriteLine("Testing RealLexer:");
        List <string> test_RealLexer = new List <string> {
            "+12", "12", "+0.1", "123.b", " ", ""
        };

        foreach (var str in test_RealLexer)
        {
            RealLexer L = new RealLexer(str);
            System.Console.WriteLine(System.String.Format("{0} : {1}", str, L.Parse()));
        }

        System.Console.WriteLine("Testing StringLexer:");
        List <string> test_StringLexer = new List <string> {
            "'asd'", "''", "a", "'aaa'a", "'aaa", " ", ""
        };

        foreach (var str in test_StringLexer)
        {
            StringLexer L = new StringLexer(str);
            System.Console.WriteLine(System.String.Format("{0} : {1}", str, L.Parse()));
        }


        System.Console.WriteLine("Testing CommentLexer:");
        List <string> test_CommentLexer = new List <string> {
            "/*ddd*/", "/**/", "/*fff", " ", ""
        };

        foreach (var str in test_CommentLexer)
        {
            CommentLexer L = new CommentLexer(str);
            System.Console.WriteLine(System.String.Format("{0} : {1}", str, L.Parse()));
        }
    }