Esempio n. 1
0
        public Feature Parse(TextReader featureFileReader)
        {
            var fileContent = featureFileReader.ReadToEnd() + Environment.NewLine;

            CultureInfo language = GetLanguage(fileContent);

            var inputStream = new ANTLRReaderStream(new StringReader(fileContent));
            var lexer = GetLexter(language, inputStream);
            var tokenStream = new CommonTokenStream(lexer);
            var parser = new Grammar.SpecFlowLangParser(tokenStream);

            var featureTree = parser.feature().Tree as CommonTree;

            if (featureTree == null || parser.ParserErrors.Count > 0 || lexer.LexerErrors.Count > 0)
            {
                throw new SpecFlowParserException("Invalid Gherkin file!", lexer.LexerErrors.Concat(parser.ParserErrors).ToArray());
            }

            var walker = new SpecFlowLangWalker(new CommonTreeNodeStream(featureTree));

            Feature feature = walker.feature();

            if (feature == null)
                throw new SpecFlowParserException("Invalid Gherkin file!");

            feature.Language = language.Name;

            return feature;
        }
        Dictionary<char, NamedCharacter> LiteralsCollection; // literals collection

        /// <summary>
        /// Translate ABNF grammar to ANTLR grammar
        /// </summary>
        /// <param name="input">TextReader which reads the ABNF grammar</param>
        /// <param name="output">TextWriter which writes the ANTLR grammar</param>
        public void Translate(TextReader input, TextWriter writer, bool performDirectTranslation)
        {
            // force single threaded operation
            lock (SyncLock)
            {
                this.LiteralsCollection = new Dictionary<char, NamedCharacter>();

                // open input stream
                var stream = new Antlr.Runtime.ANTLRReaderStream(input);

                // create lexer
                var lexer = new AbnfAstLexer(stream);

                // get token stream from input stream
                var tokens = new CommonTokenStream(lexer);

                // create parser
                var parser = new AbnfAstParser(tokens);

                // parse token stream
                var results = parser.start();

                if (parser.RecognitionExceptions.Count > 0 || lexer.RecognitionExceptions.Count > 0)
                {
                    var message =
                        AntlrHelper.GetErrorMessages(parser.RecognitionExceptions)
                        + AntlrHelper.GetErrorMessages(lexer.RecognitionExceptions)
                        ;

                    throw new TranslationException(message, parser.RecognitionExceptions, lexer.RecognitionExceptions);
                }

                // get parse tree
                var tree = results.Tree;

                // Use simplified named characters for indirect translation
                var lookup = new NamedCharacterLookupSimple();

                // enable this line to use Unicode named characters for indirect translation
                // var lookup = new NamedCharacterLookupUnicode();

                var ruleStatistics = new RuleStatistics();
                var statisticsVisitor = new TreeVisitor_GatherRuleStatistics(ruleStatistics);
                statisticsVisitor.Visit(tree);

                // output translated grammar
                if (performDirectTranslation)
                {
                    OutputDirectTranslation(writer, tokens, tree, lookup, ruleStatistics);
                }
                else
                {
                    OutputIndirectTranslation(writer, tokens, tree, lookup, ruleStatistics);
                }
            }
        }
Esempio n. 3
0
        public object Interpret(TextReader reader)
        {
            var charStream = new ANTLRReaderStream(reader);
            var lexer = new ChunkyLexer(charStream);
            var tokenStream = new CommonTokenStream(lexer);
            var parser = new ChunkyParser(tokenStream);
            var ast = parser.program().Tree;

            return Exec(ast);
        }
Esempio n. 4
0
 public object ParseFile(string filename, TextReader reader)
 {
     var input = new ANTLRReaderStream(reader);
     var lexer = new FunctionalLexer(input);
     var tokenStream = new CommonTokenStream(lexer);
     var parser = new FunctionalParser(tokenStream);
     var tree = parser.prog().Tree;
     var nodes = new CommonTreeNodeStream(tree);
     var eval = new FunctionalTreeParser(nodes, parser.FunctionDefinitions);
     eval.prog();
     return default(BigInteger);
 }
Esempio n. 5
0
 public static void Main(string[] args)
 {
     try
     {
         ICharStream input = new ANTLRReaderStream(System.Console.In);
         ExprLexer lexer = new ExprLexer(input);
         CommonTokenStream tokens = new CommonTokenStream(lexer);
         ExprParser parser = new ExprParser(tokens);
         parser.prog();
     }
     catch (System.Exception e)
     {
         Console.Error.WriteLine("exception: " + e);
         Console.Error.WriteLine(e.StackTrace);
     }
 }
        Dictionary<char, NamedCharacter> LiteralsCollection; // literals collection

        /// <summary>
        /// Translate ABNF grammar to ANTLR grammar
        /// </summary>
        /// <param name="input">TextReader which reads the ABNF grammar</param>
        /// <param name="output">TextWriter which writes the ANTLR grammar</param>
        public void Translate(TextReader input, TextWriter writer, bool performDirectTranslation)
        {
            // force single threaded operation
            lock (SyncLock)
            {
                this.LiteralsCollection = new Dictionary<char, NamedCharacter>();

                // open input stream
                var stream = new Antlr.Runtime.ANTLRReaderStream(input);

                // create lexer
                var lexer = new AbnfAstLexer(stream);

                // get token stream from input stream
                var tokens = new CommonTokenStream(lexer);

                // create parser
                var parser = new AbnfAstParser(tokens);

                // parse token stream
                var results = parser.start();

                // get parse tree
                var tree = results.Tree;

                // give lexer rules unicode standard names
                INamedCharacterLookup lookup = new NamedCharacterLookupUnicode();

                // enable the next line to give lexer rules simple names
                lookup = new NamedCharacterLookupSimple();

                // output translated grammar
                if (performDirectTranslation)
                {
                    OutputDirectTranslation(writer, tokens, tree, lookup);
                }
                else
                {
                    OutputIndirectTranslation(writer, tokens, tree, lookup);
                }
            }
        }
        public virtual void Instrument(StreamReader inputStream, string inputFilename, StreamWriter outputStream)
        {
            var testTemplate = LoadStringTemplateGroup();

            var inputPath = Path.GetFullPath(inputFilename);
            var inputFile = Path.GetFileName(inputFilename);

            //get headerTemplate for file header
            var headerTemplate = testTemplate.GetInstanceOf("file_header");
            headerTemplate.SetAttribute("src", inputFile);
            headerTemplate.SetAttribute("path", inputPath.Replace("\\", "\\\\"));

            //read lines for later usage
            var codeLines = new StringBuilder();
            var code = new StringBuilder();

            codeLines.Append("_yuitest_coverage[\"");
            codeLines.Append(inputFile);
            codeLines.Append("\"].code=[");

            string line;

            while ((line = inputStream.ReadLine()) != null)
            {
                //build up array of lines
                codeLines.Append("\"");
                codeLines.Append(line.Replace(@"\", @"\\").Replace("\"", "\\\""));
                codeLines.Append("\",");

                //build up source code
                code.AppendLine(line);
            }

            var index = codeLines.Length - 1;
            switch (codeLines[index])
            {
                case ',': //if there's a dangling comma, replace it
                    codeLines.Remove(index, 1);
                    codeLines.Insert(index, ']');
                    break;
                case '[': //empty file
                    codeLines.Append("]");
                    break;
                    //no default
            }
            codeLines.Append(";");

            //output full path

            //setup parser
            var antlrStream = new ANTLRReaderStream(new StringReader(code.ToString()));
            var lexer = new ES3YUITestLexer(antlrStream);
            var tokens = new TokenRewriteStream(lexer);
            var parser = new ES3YUITestParser(tokens)
                             {
                                 TemplateGroup = testTemplate,
                                 SourceFileName = inputFile
                             };

            var result = string.Empty;

            //an empty string will cause the parser to explode
            if (code.ToString().Trim().Length > 0)
            {
                parser.program();
                result = tokens.ToString();
            }

            //output the resulting file
            outputStream.Write(headerTemplate.ToString());
            outputStream.WriteLine();
            outputStream.Write(codeLines.ToString());
            outputStream.WriteLine();
            outputStream.Write(result);
            outputStream.Flush();
        }
        /** Load full path name .st file relative to root by prefix */
        public virtual CompiledTemplate LoadTemplateFile(string prefix, string unqualifiedFileName)
        {
            if (Path.IsPathRooted(unqualifiedFileName))
                throw new ArgumentException();

            if (Verbose)
                Console.WriteLine("loadTemplateFile({0}) in groupdir from {1} prefix={2}", unqualifiedFileName, root, prefix);

            string templateName = Path.ChangeExtension(unqualifiedFileName, null);
            Uri f = null;
            try
            {
                f = new Uri(root.LocalPath + prefix + unqualifiedFileName);
            }
            catch (UriFormatException me)
            {
                ErrorManager.RuntimeError(null, ErrorType.INVALID_TEMPLATE_NAME, me, Path.Combine(root.LocalPath, unqualifiedFileName));
                return null;
            }

            ANTLRReaderStream fs = null;
            try
            {
                fs = new ANTLRReaderStream(new StreamReader(f.LocalPath, Encoding));
                fs.name = unqualifiedFileName;
            }
            catch (IOException)
            {
                if (Verbose)
                    Console.WriteLine("{0}/{1} doesn't exist", root, unqualifiedFileName);

                //errMgr.IOError(null, ErrorType.NO_SUCH_TEMPLATE, ioe, unqualifiedFileName);
                return null;
            }

            return LoadTemplateFile(prefix, unqualifiedFileName, fs);
        }
Esempio n. 9
0
        private SpecFlowLangLexer GetLexter(CultureInfo language, ANTLRReaderStream inputStream)
        {
            Type lexterType;
            if (!lexters.TryGetValue(language, out lexterType))
            {
                CultureInfo calculatedLanguage = language;

                while (calculatedLanguage.Parent != calculatedLanguage)
                {
                    calculatedLanguage = calculatedLanguage.Parent;
                    if (lexters.TryGetValue(calculatedLanguage, out lexterType))
                        break;
                }

                if (lexterType == null)
                    throw new SpecFlowParserException(string.Format("The specified feature file language ('{0}') is not supported.", language));
            }

            return (SpecFlowLangLexer)Activator.CreateInstance(lexterType, inputStream);
        }
Esempio n. 10
0
 ANTLRReaderStream OpenFileForAntlr(NadirContext context, string fileName)
     {
     System.IO.StreamReader reader = OpenFileAsReader(context, fileName);
     ANTLRReaderStream result = new ANTLRReaderStream(reader);
     result.SourceName = fileName;
     return result;
     }
Esempio n. 11
0
        /** Load a group file with full path fileName; it's relative to root by prefix. */
        public virtual void LoadGroupFile(string prefix, string fileName)
        {
            if (Verbose)
            {
                Console.Out.WriteLine("{0}.LoadGroupFile(prefix={1}, fileName={2})",
                    GetType().FullName, prefix, fileName);
            }

            GroupParser parser = null;
            try
            {
                Uri f = new Uri(fileName);
                ANTLRReaderStream fs = new ANTLRReaderStream(new System.IO.StreamReader(f.LocalPath, Encoding));

                var timer = System.Diagnostics.Stopwatch.StartNew();

                string cachePath = Path.Combine(Path.GetTempPath(), "ST4TemplateCache");
                if (EnableCache && TryLoadGroupFromCache(cachePath, prefix, fileName))
                {
                    System.Diagnostics.Debug.WriteLine(string.Format("Successfully loaded the group from cache {0} in {1}ms.", Name, timer.ElapsedMilliseconds));
                }
                else
                {
                    GroupLexer lexer = new GroupLexer(fs);
                    fs.name = fileName;
                    CommonTokenStream tokens = new CommonTokenStream(lexer);
                    parser = new GroupParser(tokens);
                    parser.group(this, prefix);

                    System.Diagnostics.Debug.WriteLine(string.Format("Successfully loaded the group {0} in {1}ms.", Name, timer.ElapsedMilliseconds));

                    if (EnableCache)
                        CacheCompiledGroup(cachePath, prefix, fileName, File.GetLastWriteTimeUtc(f.LocalPath));
                }

            }
            catch (Exception e)
            {
                e.PreserveStackTrace();
                if (e.IsCritical())
                    throw;

                ErrorManager.IOError(null, ErrorType.CANT_LOAD_GROUP_FILE, e, fileName);
            }
        }
Esempio n. 12
0
        /** Load template file into this group using absolute filename */
        public virtual CompiledTemplate LoadAbsoluteTemplateFile(string fileName)
        {
            ANTLRReaderStream fs;
            try
            {
                fs = new ANTLRReaderStream(new StreamReader(File.OpenRead(fileName), Encoding));
                fs.name = fileName;
            }
            catch (IOException)
            {
                // doesn't exist
                //errMgr.IOError(null, ErrorType.NO_SUCH_TEMPLATE, ioe, fileName);
                return null;
            }

            return LoadTemplateFile("", fileName, fs);
        }