public static GeneroParser CreateParser(Type t, TextReader reader, ParserOptions parserOptions, IProjectEntry projEntry = null, string filename = null)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            if (t == null)
            {
                // Try to figure out the type from the projEntry or filename
                if (projEntry != null && projEntry.FilePath != null)
                {
                    t = EditorExtensions.Extensions.GetParserType(projEntry.FilePath);
                }

                if (t == null &&
                    filename != null)
                {
                    t = EditorExtensions.Extensions.GetParserType(filename);
                }

                if (t == null)
                {
                    throw new ArgumentNullException("t");
                }
            }

            if (t != typeof(Genero4glParser) &&
                t != typeof(GeneroPerParser))
            {
                throw new ArgumentException("Invalid type received", "t");
            }

            var options = parserOptions ?? ParserOptions.Default;

            GeneroParser result    = null;
            Tokenizer    tokenizer = new Tokenizer(options.ErrorSink,
                                                   (options.Verbatim ? TokenizerOptions.Verbatim : TokenizerOptions.None) | TokenizerOptions.GroupingRecovery,
                                                   (span, text) => options.RaiseProcessComment(result, new CommentEventArgs(span, text)));

            tokenizer.Initialize(null, reader, SourceLocation.MinValue);
            tokenizer.IndentationInconsistencySeverity = options.IndentationInconsistencySeverity;

            if (t == typeof(Genero4glParser))
            {
                result = new Genero4glParser(tokenizer,
                                             options.ErrorSink ?? ErrorSink.Null,
                                             options.Verbatim,
                                             options.BindReferences,
                                             options
                                             );
            }
            else if (t == typeof(GeneroPerParser))
            {
                result = new GeneroPerParser(tokenizer,
                                             options.ErrorSink ?? ErrorSink.Null,
                                             options.Verbatim,
                                             options.BindReferences,
                                             options
                                             );
            }

            if (result != null)
            {
                result._projectEntry = projEntry;
                result._filename     = filename;

                result._sourceReader = reader;
            }
            return(result);
        }
Exemple #2
0
 public TokenizerErrorSink(GeneroParser parser)
 {
     _parser = parser;
 }
Exemple #3
0
        public static bool TryParseNode(GeneroParser parser, out PreprocessorNode node)
        {
            node = null;
            bool result = false;

            if (parser.PeekToken(TokenKind.Ampersand))
            {
                var options    = parser.Tokenizer.CurrentOptions;                           // backup the current parser options
                var newOptions = options | TokenizerOptions.VerbatimCommentsAndLineJoins;   // allow us to continue until the newline
                parser.Tokenizer.AdjustOptions(newOptions);
                parser.NextToken();
                result          = true;
                node            = new PreprocessorNode();
                node.StartIndex = parser.Token.Span.Start;
                StringBuilder sb = new StringBuilder();

                switch (parser.PeekToken().Kind)
                {
                case TokenKind.IncludeKeyword:
                    parser.NextToken();
                    node.Type = PreprocessorType.Include;
                    StringExpressionNode strExpr;
                    if (StringExpressionNode.TryGetExpressionNode(parser, out strExpr))
                    {
                        node.IncludeFile = strExpr.LiteralValue;
                    }
                    else
                    {
                        parser.ReportSyntaxError("Invalid include file preprocessor directive found.");
                    }
                    break;

                case TokenKind.DefineKeyword:
                    parser.NextToken();
                    node.Type = PreprocessorType.Define;
                    break;

                case TokenKind.UndefKeyword:
                    parser.NextToken();
                    node.Type = PreprocessorType.Undef;
                    break;

                case TokenKind.IfdefKeyword:
                    parser.NextToken();
                    node.Type = PreprocessorType.Ifdef;
                    break;

                case TokenKind.ElseKeyword:
                    parser.NextToken();
                    node.Type = PreprocessorType.Else;
                    break;

                case TokenKind.EndifKeyword:
                    parser.NextToken();
                    node.Type = PreprocessorType.Endif;
                    break;

                default:
                    break;
                }

                while (!parser.PeekToken(TokenKind.NewLine) && !parser.PeekToken(TokenKind.EndOfFile))
                {
                    node.PreprocessorTokens.Add(parser.NextToken());
                }
                parser.Tokenizer.AdjustOptions(options);                                    // restore the backed up parser options
                while (parser.PeekToken(TokenKind.NewLine))
                {
                    parser.NextToken();
                }
                node.IsComplete = true;
            }

            return(result);
        }