private void NotInCore(XSharpParserRuleContext context, string msg)
 {
     if (_options.Dialect == XSharpDialect.Core)
     {
         NotInDialect(context, msg);
     }
 }
        public bool CheckOption(CompilerOption option, bool defaultValue, XSharpParserRuleContext context, IList <PragmaOption> options)
        {
            bool result = defaultValue;

            if (context != null && options != null && options.Count > 0)
            {
                int line = context.Start.Line;
                foreach (var pragmaoption in options)
                {
                    if (pragmaoption.Line > line)
                    {
                        break;
                    }
                    if (pragmaoption.Option == option || pragmaoption.Option == CompilerOption.All)
                    {
                        switch (pragmaoption.State)
                        {
                        case Pragmastate.On:
                            result = true;
                            break;

                        case Pragmastate.Off:
                            result = false;
                            break;

                        case Pragmastate.Default:
                            result = defaultValue;
                            break;
                        }
                    }
                }
            }
            return(result);
        }
示例#3
0
        public static string GetCleanText(this XSharpParserRuleContext context)
        {
            string name = context.GetText();

            //if (name.IndexOf("@@") >= 0)
            //    name = name.Replace("@@", "");
            return(name);
        }
 internal static bool IsVoConvert(this XSharpParserRuleContext node)
 {
     if (node is XP.PrimaryExpressionContext pec)
     {
         return(pec.Expr is XP.VoConversionExpressionContext);
     }
     return(false);
 }
示例#5
0
        /// <summary>
        /// The list of trivia that appears before this node in the source code.
        /// </summary>
        public new SyntaxTriviaList GetLeadingTrivia()
        {
#if XSHARP
            XSharpParserRuleContext node = this.XNode as XSharpParserRuleContext;
            if (node != null && !this.XGenerated)
            {
                var result = node.GetLeadingTrivia(this, this.SyntaxTree.GetRoot() as CompilationUnitSyntax);
                return(result);
            }
#endif
            var firstToken = this.GetFirstToken(includeZeroWidth: true);
            return(firstToken.LeadingTrivia);
        }
示例#6
0
            string GetClassName(XSharpParserRuleContext context)
            {
                var parent = context?.Parent;

                while (parent != null)
                {
                    if (parent is XSharpParser.Class_Context classContent)
                    {
                        return(classContent.Id?.GetText() ?? "");
                    }

                    parent = parent.Parent;
                }

                return("");
            }
        public bool HasOption(CompilerOption option, XSharpParserRuleContext context, IList <PragmaOption> options)
        {
            switch (option)
            {
            case CompilerOption.ArrayZero:     // az
                return(CheckOption(option, ArrayZero, context, options));

            case CompilerOption.InitLocals:     // initlocals
                return(CheckOption(option, InitLocals, context, options));

            case CompilerOption.MemVars:     // memvar
                return(CheckOption(option, MemVars, context, options));

            case CompilerOption.UndeclaredMemVars:     // undeclared
                return(CheckOption(option, UndeclaredMemVars, context, options));

            case CompilerOption.NullStrings:     // vo2
                return(CheckOption(option, VONullStrings, context, options));

            case CompilerOption.ClipperCallingConvention:     // vo5
                return(CheckOption(option, VOClipperCallingConvention, context, options));

            case CompilerOption.AllowMissingReturns:     // vo9
                return(CheckOption(option, VOAllowMissingReturns, context, options));

            case CompilerOption.ClipperIntegerDivisions:     // vo12
                return(CheckOption(option, VOClipperIntegerDivisions, context, options));

            case CompilerOption.FloatConstants:     // vo14
                return(CheckOption(option, VOFloatConstants, context, options));

            case CompilerOption.LateBinding:                       // lb is handled in cde generation
            case CompilerOption.SignedUnsignedConversion:          // vo4
            case CompilerOption.ResolveTypedFunctionPointersToPtr: // vo6
            case CompilerOption.ImplicitCastsAndConversions:       // vo7
            case CompilerOption.CompatibleIIF:                     // vo10
            case CompilerOption.ArithmeticConversions:             // vo11
            case CompilerOption.StringComparisons:                 // vo13
                return(false);                                     // not handled during parsing
            }
            return(false);
        }
示例#8
0
 private void addFields(XCodeTypeDeclaration newClass, XSharpParserRuleContext context)
 {
     if (FieldList.ContainsKey(context))
     {
         var fields = FieldList[context];
         foreach (var f in fields)
         {
             newClass.Members.Add(f);
             var xtype = findType(f.Type.BaseType);
             if (xtype != null)
             {
                 addClassMember(new XMemberType(f.Name, MemberTypes.Field, false, xtype.FullName));
             }
             else
             {
                 addClassMember(new XMemberType(f.Name, MemberTypes.Field, false, f.Type.BaseType));
             }
         }
     }
 }
        internal XSharpSourceLocation(XSharpParserRuleContext context)
        {
            var start = context.Start as XSharpToken;
            var stop  = context.Stop as XSharpToken;

            if (start.SourceSymbol != null)
            {
                start = start.SourceSymbol;
            }
            if (stop.SourceSymbol != null)
            {
                stop = stop.SourceSymbol;
            }

            if (stop.Type == XSharpLexer.Eof)
            {
                var cu = context.CompilationUnit;
                if (cu != null)
                {
                    var stream = cu.XTokens as BufferedTokenStream;
                    var tokens = stream.GetTokens();
                    var last   = tokens.Count - 1;
                    if (tokens[last].Type == XSharpLexer.Eof && last > 0)
                    {
                        stop = (XSharpToken)tokens[last - 1];
                    }
                }
            }
            var width = stop.StopIndex - start.StartIndex;
            var lp1   = new LinePosition(start.Line - 1, start.Column);
            var lp2   = new LinePosition(stop.Line - 1, stop.Column + stop.Text.Length - 1);

            _context    = context;
            _sourceSpan = new TextSpan(start.StartIndex, width);
            _lineSpan   = new FileLinePositionSpan(context.SourceFileName, new LinePositionSpan(lp1, lp2));
        }
示例#10
0
        internal CompilationUnitSyntax ParseCompilationUnitCore()
        {
#if DEBUG && DUMP_TIMES
            DateTime t = DateTime.Now;
#endif
            if (_options.ShowIncludes)
            {
                _options.ConsoleOutput.WriteLine("Compiling {0}", _fileName);
            }
            var                     sourceText  = _text.ToString();
            XSharpLexer             lexer       = null;
            XSharpPreprocessor      pp          = null;
            XSharpParserRuleContext tree        = new XSharpParserRuleContext();
            XSharpParser            parser      = null;
            var                     parseErrors = ParseErrorData.NewBag();
            try
            {
                lexer             = XSharpLexer.Create(sourceText, _fileName, _options);
                lexer.Options     = _options;
                _lexerTokenStream = lexer.GetTokenStream();
            }
            catch (Exception e)
            {
                // Exception during Lexing
                parseErrors.Add(new ParseErrorData(_fileName, ErrorCode.ERR_Internal, e.Message, e.StackTrace));
                // create empty token stream so we can continue the rest of the code
                _lexerTokenStream = new BufferedTokenStream(new XSharpListTokenSource(lexer, new List <IToken>()));
            }
#if DEBUG && DUMP_TIMES
            {
                var ts = DateTime.Now - t;
                t += ts;
                Debug.WriteLine("Lexing completed in {0}", ts);
            }
#endif
            // do not pre-process when there were lexer exceptions
            if (lexer != null && parseErrors.Count == 0)
            {
                foreach (var e in lexer.LexErrors)
                {
                    parseErrors.Add(e);
                }
                BufferedTokenStream ppStream = null;
                try
                {
                    // Check for #pragma in the lexerTokenStream
                    _lexerTokenStream.Fill();

                    if (!_options.MacroScript)
                    {
                        pp = new XSharpPreprocessor(lexer, _lexerTokenStream, _options, _fileName, _text.Encoding, _text.ChecksumAlgorithm, parseErrors);
                    }
                    var mustPreprocess = !_options.MacroScript && (lexer.HasPreprocessorTokens || !_options.NoStdDef);
                    if (mustPreprocess)
                    {
                        var ppTokens = pp.PreProcess();
                        ppStream = new CommonTokenStream(new XSharpListTokenSource(lexer, ppTokens));
                    }
                    else
                    {
                        // No Standard Defs and no preprocessor tokens in the lexer
                        // so we bypass the preprocessor and use the lexer tokenstream
                        // but if a .ppo is required we must use the preprocessor to
                        // write the source text to the .ppo file
                        if (_options.PreprocessorOutput && pp != null)
                        {
                            pp.writeToPPO(sourceText, false);
                        }
                        BufferedTokenStream ts = (BufferedTokenStream)_lexerTokenStream;
                        var tokens             = ts.GetTokens();
                        // commontokenstream filters on tokens on the default channel. All other tokens are ignored
                        ppStream = new CommonTokenStream(new XSharpListTokenSource(lexer, tokens));
                    }
                    ppStream.Fill();
                    _preprocessorTokenStream = ppStream;
                }
                catch (Exception e)
                {
                    // Exception during Preprocessing
                    parseErrors.Add(new ParseErrorData(_fileName, ErrorCode.ERR_Internal, e.Message, e.StackTrace));
                    // create empty token stream so we can continue the rest of the code
                    _preprocessorTokenStream = new BufferedTokenStream(new XSharpListTokenSource(lexer, new List <IToken>()));
                }
            }
#if DEBUG && DUMP_TIMES
            {
                var ts = DateTime.Now - t;
                t += ts;
                Debug.WriteLine("Preprocessing completed in {0}", ts);
            }
#endif
            parser = new XSharpParser(_preprocessorTokenStream)
            {
                Options = _options
            };

            tree = new XSharpParserRuleContext();
            if (_options.ParseLevel != ParseLevel.Lex)
            {
                // When parsing in Sll mode we do not record any parser errors.
                // When this fails, then we try again with LL mode and then we record errors
                parser.RemoveErrorListeners();
                parser.Interpreter.PredictionMode = PredictionMode.Sll;
                // some options to have FAST parsing
                parser.Interpreter.tail_call_preserves_sll           = false;
                parser.Interpreter.treat_sllk1_conflict_as_ambiguity = true;
                parser.ErrorHandler = new BailErrorStrategy();
                try
                {
                    tree = buildTree(parser);
                }
                catch (ParseCanceledException e)
                {
                    if (_options.Verbose)
                    {
                        string msg = _GetInnerExceptionMessage(e);
                        _options.ConsoleOutput.WriteLine("Antlr: SLL parsing failed with failure: " + msg + ". Trying again in LL mode.");
                    }
                    var errorListener = new XSharpErrorListener(_fileName, parseErrors);
                    parser.AddErrorListener(errorListener);
                    parser.ErrorHandler = new XSharpErrorStrategy();
                    // we need to set force_global_context to get proper error messages. This makes parsing slower
                    // but gives better messages
                    parser.Interpreter.treat_sllk1_conflict_as_ambiguity = false;
                    parser.Interpreter.force_global_context      = true;
                    parser.Interpreter.enable_global_context_dfa = true;
                    parser.Interpreter.PredictionMode            = PredictionMode.Ll;
                    _preprocessorTokenStream.Reset();
                    if (_options.Verbose && pp != null)
                    {
                        pp.DumpStats();
                    }
                    if (pp != null)
                    {
                        pp.Close();
                    }
                    parser.Reset();
                    try
                    {
                        tree = buildTree(parser);
                    }
                    catch (Exception e1)
                    {
                        // Cannot parse again. Must be a syntax error.
                        if (_options.Verbose)
                        {
                            string msg = _GetInnerExceptionMessage(e1);
                            _options.ConsoleOutput.WriteLine("Antlr: LL parsing also failed with failure: " + msg);
                        }
                    }
                }
            }// _options.ParseLevel < Complete
#if DEBUG && DUMP_TIMES
            {
                var ts = DateTime.Now - t;
                t += ts;
                Debug.WriteLine("Parsing completed in {0}", ts);
            }
#endif
            if (_options.DumpAST && tree != null)
            {
                string strTree = tree.ToStringTree();
                string file    = System.IO.Path.ChangeExtension(_fileName, "ast");
                strTree = strTree.Replace(@"\r\n)))))", @"\r\n*)))))" + "\r\n");
                strTree = strTree.Replace(@"\r\n))))", @"\r\n*)))" + "\r\n");
                strTree = strTree.Replace(@"\r\n)))", @"\r\n*)))" + "\r\n");
                strTree = strTree.Replace(@"\r\n))", @"\r\n*))" + "\r\n");
                strTree = strTree.Replace(@"\r\n)", @"\r\n*)" + "\r\n");
                strTree = strTree.Replace(@"\r\n*)", @"\r\n)");
                System.IO.File.WriteAllText(file, strTree);
            }
            var walker = new ParseTreeWalker();


            if (_options.ParseLevel == ParseLevel.Complete)
            {
                // check for parser errors, such as missing tokens
                // This adds items to the parseErrors list for missing
                // tokens and missing keywords
                try
                {
                    var errchecker = new XSharpParseErrorAnalysis(parser, parseErrors, _options);
                    walker.Walk(errchecker, tree);
                }
                catch (Exception e)
                {
                    parseErrors.Add(new ParseErrorData(_fileName, ErrorCode.ERR_Internal, e.Message, e.StackTrace));
                }
            }
            var         treeTransform = CreateTransform(parser, _options, _pool, _syntaxFactory, _fileName);
            bool        hasErrors     = false;
            SyntaxToken eof           = null;
            try
            {
                if (_options.ParseLevel < ParseLevel.Complete || parser.NumberOfSyntaxErrors != 0 ||
                    (parseErrors.Count != 0 && parseErrors.Contains(p => !ErrorFacts.IsWarning(p.Code))))
                {
                    eof = SyntaxFactory.Token(SyntaxKind.EndOfFileToken);
                    eof = AddLeadingSkippedSyntax(eof, ParserErrorsAsTrivia(parseErrors, pp.IncludedFiles));
                    if (tree != null)
                    {
                        eof.XNode = new XTerminalNodeImpl(tree.Stop);
                    }
                    else
                    {
                        eof.XNode = new XTerminalNodeImpl(_lexerTokenStream.Get(_lexerTokenStream.Size - 1));
                    }
                    hasErrors = true;
                }

                if (!hasErrors)
                {
                    try
                    {
                        walker.Walk(treeTransform, tree);
                    }
                    catch (Exception e)
                    {
                        parseErrors.Add(new ParseErrorData(_fileName, ErrorCode.ERR_Internal, e.Message, e.StackTrace));
                    }
                    eof = SyntaxFactory.Token(SyntaxKind.EndOfFileToken);
                    if (!parseErrors.IsEmpty())
                    {
                        eof = AddLeadingSkippedSyntax(eof, ParserErrorsAsTrivia(parseErrors, pp.IncludedFiles));
                    }
                }
                var result = _syntaxFactory.CompilationUnit(
                    treeTransform.GlobalEntities.Externs,
                    treeTransform.GlobalEntities.Usings,
                    treeTransform.GlobalEntities.Attributes,
                    treeTransform.GlobalEntities.Members, eof);
                result.XNode          = tree;
                tree.CsNode           = result;
                result.XTokens        = _lexerTokenStream;
                result.XPPTokens      = _preprocessorTokenStream;
                result.HasDocComments = lexer.HasDocComments;
                if (!_options.MacroScript && !hasErrors)
                {
                    result.InitProcedures  = treeTransform.GlobalEntities.InitProcedures;
                    result.Globals         = treeTransform.GlobalEntities.Globals;
                    result.PragmaWarnings  = treeTransform.GlobalEntities.PragmaWarnings;
                    result.PragmaOptions   = treeTransform.GlobalEntities.PragmaOptions;
                    result.IncludedFiles   = pp?.IncludedFiles;
                    result.FileWidePublics = treeTransform.GlobalEntities.FileWidePublics;
                    result.HasPCall        = treeTransform.GlobalEntities.HasPCall;
                    result.NeedsProcessing = treeTransform.GlobalEntities.NeedsProcessing;
                    if (_options.HasRuntime)
                    {
                        result.LiteralSymbols = ((XSharpTreeTransformationRT)treeTransform).LiteralSymbols;
                        result.LiteralPSZs    = ((XSharpTreeTransformationRT)treeTransform).LiteralPSZs;
                    }
                }
                return(result);
            }
            finally
            {
#if DEBUG && DUMP_TIMES
                {
                    var ts = DateTime.Now - t;
                    t += ts;
                    Debug.WriteLine("Tree transform completed in {0}", ts);
                }
#endif
                treeTransform.Free();
                if (pp != null)
                {
                    pp.Close();
                }
            }
        }
 private void NotInDialect(XSharpParserRuleContext context, string msg)
 {
     _parseErrors.Add(new ParseErrorData(context, ErrorCode.ERR_FeatureNotAvailableInDialect, msg, _options.Dialect.ToString()));
 }
 internal static bool IsCastClass(this XSharpParserRuleContext node)
 {
     return(node.Start.Type == XSharpLexer.CASTCLASS);
 }
示例#13
0
        private MemberDeclarationSyntax createProperty(string fldName, TypeSyntax type, XSharpParserRuleContext context, XP.ClassvarModifiersContext modifiers)
        {
            var         accessors = _pool.Allocate <AccessorDeclarationSyntax>();
            BlockSyntax body      = null;

            if (_options.fox1)
            {
                var call = GenerateMethodCall(XSharpSpecialNames.GetProperty, MakeArgumentList(MakeArgument(GenerateLiteral(fldName))), true);
                body            = MakeBlock(GenerateReturn(call, true));
                body.XGenerated = true;
            }
            var accessor = _syntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration,
                                                              EmptyList <AttributeListSyntax>(), EmptyList(),
                                                              SyntaxFactory.MakeToken(SyntaxKind.GetKeyword),
                                                              body,
                                                              null,
                                                              SyntaxFactory.MakeToken(SyntaxKind.SemicolonToken));

            accessor.XNode      = context;
            accessor.XGenerated = true;
            accessors.Add(accessor);
            if (_options.fox1)
            {
                var call = GenerateMethodCall(XSharpSpecialNames.SetProperty, MakeArgumentList(MakeArgument(GenerateLiteral(fldName)), MakeArgument(GenerateSimpleName("value"))), true);
                body            = MakeBlock(GenerateExpressionStatement(call, true));
                body.XGenerated = true;
            }
            accessor = _syntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration,
                                                          EmptyList <AttributeListSyntax>(), EmptyList(),
                                                          SyntaxFactory.MakeToken(SyntaxKind.SetKeyword),
                                                          body,
                                                          null,
                                                          SyntaxFactory.MakeToken(SyntaxKind.SemicolonToken));
            accessor.XNode      = context;
            accessor.XGenerated = true;
            accessors.Add(accessor);
            var id           = SyntaxFactory.MakeIdentifier(fldName);
            var accessorList = _syntaxFactory.AccessorList(SyntaxFactory.MakeToken(SyntaxKind.OpenBraceToken),
                                                           accessors, SyntaxFactory.MakeToken(SyntaxKind.CloseBraceToken));

            _pool.Free(accessors);
            var mods = modifiers?.GetList <SyntaxToken>() ?? DefaultMethodModifiers(false, false, false);
            var prop = _syntaxFactory.PropertyDeclaration(
                attributeLists: EmptyList <AttributeListSyntax>(),
                modifiers: mods,
                type: type,
                explicitInterfaceSpecifier: null,
                identifier: id,
                accessorList: accessorList,
                expressionBody: null,
                initializer: null,
                semicolonToken: SyntaxFactory.MakeToken(SyntaxKind.SemicolonToken));

            return(prop);
        }