public override void EnterNamespace_(XSharpParser.Namespace_Context context)
 {
     String newNamespaceName = context.Name.GetText();
     // We already have something in Stack
     // so we are nesting Namespaces, get the previous name prefix
     if (this.NamespaceStack.Count > 0)
     {
         newNamespaceName = this.CurrentNamespace.Name + "." + newNamespaceName;
     }
     CodeNamespace newNamespace = new CodeNamespace(newNamespaceName);
     //
     this.NamespaceStack.Push(this.CurrentNamespace);
     //
     if (String.IsNullOrEmpty(this.CurrentNamespace.Name))
     {
         // We could just have the empty fake Namespace here, but
         // if we have some Usings inside we must copy them
         if ((this.CurrentNamespace.Types.Count == 0) && (this.CurrentNamespace.Imports.Count > 0))
         {
             // No Types means no Classes
             // Ok, copy
             foreach (CodeNamespaceImport import in this.CurrentNamespace.Imports)
                 newNamespace.Imports.Add(import);
         }
     }
     //
     this.CurrentNamespace = newNamespace;
 }
Example #2
0
        XSharpParserRuleContext buildTree(XSharpParser parser)
        {
            XSharpParserRuleContext tree;

            if (_isScript)
            {
                if (_isMacroScript)
                {
                    tree = parser.macroScript();
                }
                else
                {
                    tree = parser.script();
                }
            }
            else if (_options.Dialect == XSharpDialect.FoxPro)
            {
                tree = parser.foxsource();
            }
            else
            {
                tree = parser.source();
            }
            return(tree);
        }
 public XSharpTreeTransformationXPP(XSharpParser parser, CSharpParseOptions options, SyntaxListPool pool,
                                    ContextAwareSyntax syntaxFactory, string fileName) :
     base(parser, options, pool, syntaxFactory, fileName)
 {
     _classes      = new List <XppClassInfo>();
     _classstack   = new Stack <IList <XppClassInfo> >();
     _currentClass = null;
     _entryPoint   = "Main";
 }
Example #4
0
 public XSharpTreeTransformationVO(XSharpParser parser, CSharpParseOptions options, SyntaxListPool pool,
                                   ContextAwareSyntax syntaxFactory, string fileName) :
     base(parser, options, pool, syntaxFactory, fileName)
 {
     if (options.XSharpRuntime)
     {
         _winBoolType = XSharpQualifiedTypeNames.WinBool;
     }
     else
     {
         _winBoolType = VulcanQualifiedTypeNames.WinBool;
     }
 }
Example #5
0
        static int AnalyzeCode(string code, bool showErrors)
        {
            var stream = new AntlrInputStream(code.ToString());
            var lexer  = new XSharpLexer(stream);

            lexer.Options = new CSharpParseOptions();
            var tokens = new CommonTokenStream(lexer);
            var parser = new XSharpParser(tokens);

            parser.Options = lexer.Options;
            var errorListener = new XSharpErrorListener(showErrors);

            parser.AddErrorListener(errorListener);
            var tree = parser.source();

            //Console.WriteLine(tree.ToStringTree());
            return(errorListener.TotalErrors);
        }
Example #6
0
        List <MethodInfo> GetMethodInfos()
        {
            var lexer = XSharpLexer.Create(SourceCodeFile.SourceCode, SourceCodeFile.FileName);

            lexer.RemoveErrorListeners();

            var tokenStream = new CommonTokenStream(lexer, 0);
            var parser      = new XSharpParser(tokenStream);

            parser.Options = new XSharpParseOptions();
            parser.Options.SetXSharpSpecificOptions(XSharpSpecificCompilationOptions.Default);
            parser.RemoveErrorListeners();

            var source   = parser.source();
            var listener = new MethodListener();

            new ParseTreeWalker().Walk(listener, source);

            return(listener.MethodList);
        }
Example #7
0
        internal static XSharpTreeTransformationCore CreateTransform(XSharpParser parser, CSharpParseOptions options, SyntaxListPool pool,
                                                                     ContextAwareSyntax syntaxFactory, string fileName)
        {
            switch (options.Dialect)
            {
            case XSharpDialect.Core:
                return(new XSharpTreeTransformationCore(parser, options, pool, syntaxFactory, fileName));

            case XSharpDialect.VO:
            case XSharpDialect.Vulcan:
                return(new XSharpTreeTransformationVO(parser, options, pool, syntaxFactory, fileName));

            case XSharpDialect.FoxPro:
                return(new XSharpTreeTransformationFox(parser, options, pool, syntaxFactory, fileName));

            case XSharpDialect.XPP:
                return(new XSharpTreeTransformationXPP(parser, options, pool, syntaxFactory, fileName));

            case XSharpDialect.Harbour:
            default:
                return(new XSharpTreeTransformationRT(parser, options, pool, syntaxFactory, fileName));
            }
        }
 private CodeTypeReference BuildName(XSharpParser.NameContext context)
 {
     CodeTypeReference expr = null;
     //
     if (context is XSharpParser.QualifiedNameContext)
     {
         XSharpParser.QualifiedNameContext qual = (XSharpParser.QualifiedNameContext)context;
         expr = BuildName(qual.Left);
         expr = new CodeTypeReference(expr.BaseType + "." + BuildSimpleName(qual.Right).BaseType);
     }
     else if (context is XSharpParser.SimpleOrAliasedNameContext)
     {
         XSharpParser.SimpleOrAliasedNameContext alias = (XSharpParser.SimpleOrAliasedNameContext)context;
         //
         if (alias.Name is XSharpParser.AliasQualifiedNameContext)
         {
             XSharpParser.AliasQualifiedNameContext al = (XSharpParser.AliasQualifiedNameContext)alias.Name;
             expr = BuildSimpleName(al.Right);
             expr = new CodeTypeReference(al.Alias.GetText() + "::" + expr.BaseType);
         }
         else if (alias.Name is XSharpParser.GlobalQualifiedNameContext)
         {
             XSharpParser.GlobalQualifiedNameContext gbl = (XSharpParser.GlobalQualifiedNameContext)alias.Name;
             expr = BuildSimpleName(gbl.Right);
             expr = new CodeTypeReference("GLOBAL::" + expr.BaseType);
         }
         else if (alias.Name is XSharpParser.IdentifierOrGenericNameContext)
         {
             XSharpParser.IdentifierOrGenericNameContext id = (XSharpParser.IdentifierOrGenericNameContext)alias.Name;
             expr = BuildSimpleName(id.Name);
         }
     }
     //
     return expr;
 }
        private CodeVariableDeclarationStatement BuildLocalVar(XSharpParser.LocalvarContext context, CodeTypeReference localType)
        {
            CodeVariableDeclarationStatement local = new CodeVariableDeclarationStatement();
            local.Name = context.Id.GetText();
            local.Type = localType;
            if (context.Expression != null)
            {
                if (context.Expression is XSharpParser.PrimaryExpressionContext)
                {
                    XSharpParser.PrimaryContext ctx = ((XSharpParser.PrimaryExpressionContext)context.Expression).Expr;
                    if (ctx is XSharpParser.LiteralExpressionContext)
                    {
                        XSharpParser.LiteralExpressionContext lit = (XSharpParser.LiteralExpressionContext)ctx;
                        local.InitExpression = BuildLiteralValue(lit.Literal);
                    }
                }
                else
                {
                    local.InitExpression = new CodeSnippetExpression(context.Expression.GetText());
                }
            }

            return local;
        }
 /// <summary>
 /// Get a LiteralValueContext containing a BIN_CONST, INT_CONST, HEX_CONST, or a REAL_CONST
 /// as a String, and convert it to the "real" value, with the corresponding Type.
 /// </summary>
 /// <param name="context"></param>
 /// <returns>An Object of the needed Type, with the value</returns>
 private object GetNumericValue(XSharpParser.LiteralValueContext context)
 {
     Object ret = null;
     String value = context.GetText();
     //
     if (context.BIN_CONST() != null || context.INT_CONST() != null || context.HEX_CONST() != null)
     {
         bool isUnsigned = (value.Substring(0, 1).ToLower().CompareTo("u") == 0);
         // -1 for Unsigned; -2 for 0x or 0b
         int len = value.Length - (isUnsigned ? 1 : 0) - (context.BIN_CONST() != null || context.HEX_CONST() != null ? 2 : 0);
         //
         if (context.BIN_CONST() != null)
         {
             if (len > 64)
             {
                 ret = Double.NaN;
             }
             else
             {
                 // Don't forget to remove the prefix !!!
                 value = value.Substring(2);
                 // BIN are always unsigned (??)
                 UInt64 bin64;
                 try
                 {
                     bin64 = Convert.ToUInt64(value, 2);
                     // Maybe 32 bits is enough ?
                     if (bin64 <= UInt32.MaxValue)
                     {
                         UInt32 bin32 = Convert.ToUInt32(bin64);
                         ret = bin32;
                     }
                     else
                     {
                         ret = bin64;
                     }
                 }
                 catch
                 {
                     ret = Double.NaN;
                 }
             }
         }
         else if (context.HEX_CONST() != null)
         {
             if (len > 16)
             {
                 ret = Double.NaN;
             }
             else
             {
                 // Don't forget to remove the prefix !!!
                 value = value.Substring(2);
                 // HEX are always unsigned (??)
                 UInt64 hex64;
                 try
                 {
                     hex64 = Convert.ToUInt64(value, 16);
                     // Maybe 32 bits is enough ?
                     if (hex64 <= UInt32.MaxValue)
                     {
                         UInt32 hex32 = Convert.ToUInt32(hex64);
                         ret = hex32;
                     }
                     else
                     {
                         ret = hex64;
                     }
                 }
                 catch
                 {
                     ret = Double.NaN;
                 }
             }
         }
         else
         {
             // context.INT_CONST() != null
             if (len > 64)
             {
                 ret = Double.NaN;
             }
             else if (isUnsigned)
             {
                 UInt64 myUInt64;
                 try
                 {
                     myUInt64 = Convert.ToUInt64(value, 10);
                     // Maybe 32 bits is enough ?
                     if (myUInt64 <= UInt32.MaxValue)
                     {
                         UInt32 myUInt32 = Convert.ToUInt32(myUInt64);
                         ret = myUInt32;
                     }
                     else
                     {
                         ret = myUInt64;
                     }
                 }
                 catch
                 {
                     ret = Double.NaN;
                 }
             }
             else
             {
                 Int64 myInt64;
                 try
                 {
                     myInt64 = Convert.ToInt64(value, 10);
                     // Maybe 32 bits is enough ?
                     if ((myInt64 >= UInt32.MinValue) && (myInt64 <= UInt32.MaxValue))
                     {
                         Int32 myInt32 = Convert.ToInt32(myInt64);
                         ret = myInt32;
                     }
                     else
                     {
                         ret = myInt64;
                     }
                 }
                 catch
                 {
                     ret = Double.NaN;
                 }
             }
         }
     }
     else
     {
         double d;
         // Should be REAL_CONST
         if (!double.TryParse(value, out d))
         {
             d = double.NaN;
         }
         ret = d;
     }
     return ret;
 }
 private MemberAttributes ContextToEventModifiers(XSharpParser.EventModifiersContext modifiers)
 {
     return decodeMemberAttributes(modifiers._Tokens);
 }
        private CodeExpression BuildExpression(XSharpParser.PrimaryExpressionContext expression)
        {
            CodeExpression expr = null;
            XSharpParser.PrimaryContext ctx = expression.Expr;
            //
            if (ctx is XSharpParser.SelfExpressionContext) // Self
            {
                expr = new CodeThisReferenceExpression();
            }
            else if (ctx is XSharpParser.SuperExpressionContext) // Super
            {
                expr = new CodeBaseReferenceExpression();
            }
            else if (ctx is XSharpParser.LiteralExpressionContext)
            {
                XSharpParser.LiteralExpressionContext lit = (XSharpParser.LiteralExpressionContext)ctx;
                expr = BuildLiteralValue(lit.Literal);
            }
            else if (ctx is XSharpParser.LiteralArrayExpressionContext) // { expr [, expr] }
            {
                XSharpParser.LiteralArrayContext arr = ((XSharpParser.LiteralArrayExpressionContext)ctx).LiteralArray;
                // Typed Array ?
                if (arr.Type != null)
                {
                    List<CodeExpression> exprlist = new List<CodeExpression>();
                    foreach (var Element in arr._Elements)
                    {
                        exprlist.Add(BuildExpression(Element.Expr,true));
                    }
                    expr = new CodeArrayCreateExpression(BuildDataType(arr.Type), exprlist.ToArray());
                }
                else
                {
                    expr = new CodeSnippetExpression(arr.GetText());
                }
            }
            else if (ctx is XSharpParser.DelegateCtorCallContext)
            {
                XSharpParser.DelegateCtorCallContext delg = (XSharpParser.DelegateCtorCallContext)ctx;
                //
                CodeTypeReference ctr = BuildDataType(delg.Type);
                CodeExpression ce = BuildExpression(delg.Obj,false);
                //
                expr = new CodeDelegateCreateExpression(BuildDataType(delg.Type), BuildExpression(delg.Obj,false), delg.Func.GetText());

            }
            else if (ctx is XSharpParser.CtorCallContext)
            {
                XSharpParser.CtorCallContext ctor = (XSharpParser.CtorCallContext)ctx;
                CodeTypeReference ctr = BuildDataType(ctor.Type);
                List<CodeExpression> exprlist = new List<CodeExpression>();
                if (ctor.ArgList != null)
                {
                    foreach (var arg in ctor.ArgList._Args)
                    {
                        // We should handle arg.Name if arg.ASSIGN_OP is not null...
                        exprlist.Add(BuildExpression(arg.Expr,false));
                    }
                }
                expr = new CodeObjectCreateExpression(ctr.BaseType, exprlist.ToArray());
            }
            else if (ctx is XSharpParser.TypeOfExpressionContext)
            {
                CodeTypeReference ctr = BuildDataType(((XSharpParser.TypeOfExpressionContext)ctx).Type);
                expr = new CodeTypeOfExpression(ctr);
            }
            else if (ctx is XSharpParser.NameExpressionContext)
            {
                String name = ((XSharpParser.NameExpressionContext)ctx).Name.Id.GetText();
                // Sometimes, we will need to do it that way....
                if (name.ToLower() == "self")
                {
                    expr = new CodeThisReferenceExpression();
                }
                else if (name.ToLower() == "super")
                {
                    expr = new CodeBaseReferenceExpression();
                }
                else
                {
                    CodeTypeReference ctr = BuildSimpleName(((XSharpParser.NameExpressionContext)ctx).Name);
                    expr = new CodeVariableReferenceExpression(name);
                }
            }
            else
            {
                expr = new CodeSnippetExpression(ctx.GetText());
            }
            return expr;
        }
 public XSharpParseErrorAnalysis(XSharpParser parser, IList <ParseErrorData> parseErrors, CSharpParseOptions options)
 {
     _parser      = parser;
     _parseErrors = parseErrors;
     _options     = options;
 }
Example #14
0
 protected override XSharpTreeTransformationCore CreateWalker(XSharpParser parser)
 {
     return(new XSharpTreeTransformationVO(parser, _options, _pool, _syntaxFactory, _fileName));
 }
Example #15
0
 public XSharpTreeTransformationFox(XSharpParser parser, CSharpParseOptions options, SyntaxListPool pool,
                                    ContextAwareSyntax syntaxFactory, string fileName) :
     base(parser, options, pool, syntaxFactory, fileName)
 {
 }
 private TypeAttributes ContextToClassModifiers(XSharpParser.ClassModifiersContext modifiers)
 {
     TypeAttributes retValue = TypeAttributes.Public;
     ITerminalNode[] visibility;
     //
     visibility = modifiers.INTERNAL();
     if (visibility.Length > 0)
         retValue = TypeAttributes.NestedAssembly;
     //
     visibility = modifiers.HIDDEN();
     if (visibility.Length > 0)
         retValue = TypeAttributes.NestedPrivate;
     //
     visibility = modifiers.PRIVATE();
     if (visibility.Length > 0)
         retValue = TypeAttributes.NestedPrivate;
     //
     visibility = modifiers.PROTECTED();
     if (visibility.Length > 0)
     {
         visibility = modifiers.INTERNAL();
         if (visibility.Length > 0)
             retValue = TypeAttributes.NestedFamORAssem;
         else
             retValue = TypeAttributes.NestedFamily;
     }
     //
     visibility = modifiers.EXPORT();
     if (visibility.Length > 0)
         retValue = TypeAttributes.Public;
     //
     return retValue;
 }
 private MemberAttributes ContextToConstructorModifiers(XSharpParser.ConstructorModifiersContext modifiers)
 {
     return decodeMemberAttributes(modifiers._Tokens);
 }
        public override void EnterClass_(XSharpParser.Class_Context context)
        {
            CodeTypeDeclaration newClass = new CodeTypeDeclaration(context.Id.GetText());
            // Set as Current working Class
            CurrentClass = newClass;
            // and push into the Namespace
            CurrentNamespace.Types.Add(newClass);
            // That's a Class
            newClass.IsClass = true;
            // 
            if (context.Modifiers == null)
            {
                newClass.TypeAttributes = System.Reflection.TypeAttributes.Public;
            }
            else
            {
                // PARTIAL ?
                if (context.Modifiers.PARTIAL().Length > 0)
                    newClass.IsPartial = true;
                //
                if (context.Modifiers.SEALED().Length > 0)
                    newClass.Attributes |= MemberAttributes.Final;
                if (context.Modifiers.ABSTRACT().Length > 0)
                    newClass.Attributes |= MemberAttributes.Abstract;
                if (context.Modifiers.INTERNAL().Length > 0)
                    newClass.Attributes |= MemberAttributes.Private;

                // What Visibility ?
                newClass.TypeAttributes = ContextToClassModifiers(context.Modifiers);
            }
            // INHERIT from ?
            if (context.BaseType != null)
            {
                newClass.BaseTypes.Add(new CodeTypeReference(context.BaseType.GetText()));
            }
            // IMPLEMENTS ?
            if ((context._Implements != null) && (context._Implements.Count > 0))
            {
                foreach (var interfaces in context._Implements)
                {
                    newClass.BaseTypes.Add(new CodeTypeReference(interfaces.GetText()));
                }
            }
            //
        }
 private CodeParameterDeclarationExpressionCollection GetParametersList(XSharpParser.ParameterListContext paramList)
 {
     CodeParameterDeclarationExpressionCollection pList = new CodeParameterDeclarationExpressionCollection();
     //
     foreach (var param in paramList._Params)
     {
         CodeParameterDeclarationExpression pm = new CodeParameterDeclarationExpression();
         pm.Name = param.Id.GetText();
         pm.Type = BuildDataType(param.Type); // new CodeTypeReference(param.Type.GetText());
         pm.Direction = FieldDirection.In;
         if (param.Modifiers != null)
         {
             if (param.Modifiers.REF() != null)
             {
                 pm.Direction = FieldDirection.Ref;
             }
             else if (param.Modifiers.OUT() != null)
             {
                 pm.Direction = FieldDirection.Out;
             }
         }
         //
         pList.Add(pm);
     }
     //
     return pList;
 }
 private CodeTypeReference BuildNativeType(XSharpParser.NativeTypeContext nativeType)
 {
     CodeTypeReference expr = null;
     //
     if ((nativeType.BYTE() != null) ||
         (nativeType.CHAR() != null) ||
         (nativeType.DWORD() != null) ||
         (nativeType.DYNAMIC() != null) ||
         (nativeType.INT() != null) ||
         (nativeType.INT64() != null) ||
         (nativeType.LOGIC() != null) ||
         (nativeType.LONGINT() != null) ||
         (nativeType.OBJECT() != null) ||
         (nativeType.PTR() != null) ||
         (nativeType.REAL4() != null) ||
         (nativeType.REAL8() != null) ||
         (nativeType.SHORTINT() != null) ||
         (nativeType.STRING() != null) ||
         (nativeType.UINT64() != null) ||
         (nativeType.VOID() != null) ||
         (nativeType.WORD() != null))
     {
         expr = BuildNativeType(nativeType.GetText());
     }
     //
     return expr;
 }
 private CodeTypeReference BuildXBaseType(XSharpParser.XbaseTypeContext xbaseType)
 {
     CodeTypeReference expr = null;
     //
     if ((xbaseType.ARRAY() != null) ||
         (xbaseType.CODEBLOCK() != null) ||
         (xbaseType.DATE() != null) ||
         (xbaseType.FLOAT() != null) ||
         (xbaseType.PSZ() != null) ||
         (xbaseType.SYMBOL() != null) ||
         (xbaseType.USUAL() != null))
     {
         expr = new CodeTypeReference(xbaseType.GetText());
     }
     //
     return expr;
 }
 private CodeTypeReference BuildSimpleName(XSharpParser.SimpleNameContext simpleName)
 {
     CodeTypeReference expr = null;
     //
     String name = simpleName.Id.GetText();
     String gen = "";
     if (simpleName.GenericArgList != null)
     {
         string argList = "";
         int i = 0;
         foreach (var generic in simpleName.GenericArgList._GenericArgs)
         {
             if (i > 0)
                 argList += ",";
             CodeTypeReference tmp = BuildDataType(generic);
             argList += tmp.BaseType;
             i++;
         }
         //
         gen = "`" + i.ToString() + "[" + argList + "]";
     }
     expr = new CodeTypeReference(name + gen);
     //
     return expr;
 }
 private CodeExpression BuildExpression(XSharpParser.ExpressionContext expression, bool right )
 {
     CodeExpression expr = null;
     //
     if (expression is XSharpParser.PrimaryExpressionContext) // xyz.SimpleName
     {
         expr = BuildExpression((XSharpParser.PrimaryExpressionContext)expression);
     }
     else if (expression is XSharpParser.AccessMemberContext) // xyz.SimpleName
     {
         XSharpParser.AccessMemberContext member = (XSharpParser.AccessMemberContext)expression;
         //what is the left hand side ?
         //    Self  -> check if Right is in the member of CurrentClass --> FieldReference
         // else --> always Property
         bool isMember = false;
         CodeExpression left = BuildExpression(member.Expr,false);
         if (left is CodeThisReferenceExpression)
         {
             string fieldCandidate = member.Name.GetText();
             foreach (CodeTypeMember cm in this.CurrentClass.Members)
             {
                 if (cm is CodeMemberField)
                 {
                     if (String.Compare(fieldCandidate, cm.Name, true) == 0)
                     {
                         isMember = true;
                         break;
                     }
                 }
             }
         }
         // It seems to be a member...
         if (isMember)
         {
             expr = new CodeFieldReferenceExpression(BuildExpression(member.Expr,false), member.Name.GetText());
         }
         else
         {
             // Let's guess that on the Left member, we should have a Property if it is not a Field
             if (!right)
             {
                 expr = new CodePropertyReferenceExpression(BuildExpression(member.Expr, false), member.Name.GetText());
             }
             else
             {
                 // We are processing the Right member of an Assignment...
                 // Most likely Enum Value, which is a typereference expression followed by a DOT and a field
                 if (member.DOT() != null) {
                     var typeexpr = new CodeTypeReferenceExpression(member.Expr.GetText());
                     expr = new CodeFieldReferenceExpression(typeexpr, member.Name.GetText());
                 }
                 else {
                     expr = new CodeSnippetExpression(member.GetText());
                 }
             }
         }
     }
     else if (expression is XSharpParser.MethodCallContext)
     {
         XSharpParser.MethodCallContext meth = (XSharpParser.MethodCallContext)expression;
         CodeExpression target = BuildExpression(meth.Expr,false);
         List<CodeExpression> exprlist = new List<CodeExpression>();
         if (meth.ArgList != null)
         {
             foreach (var arg in meth.ArgList._Args)
             {
                 exprlist.Add(BuildExpression(arg.Expr,false));
             }
         }
         if (target is CodeFieldReferenceExpression)
         {
             //
             expr = new CodeMethodInvokeExpression(((CodeFieldReferenceExpression)target).TargetObject, ((CodeFieldReferenceExpression)target).FieldName, exprlist.ToArray());
         }
         else if (target is CodePropertyReferenceExpression)
         {
             //
             expr = new CodeMethodInvokeExpression(((CodePropertyReferenceExpression)target).TargetObject, ((CodePropertyReferenceExpression)target).PropertyName, exprlist.ToArray());
         }
         else
             expr = new CodeMethodInvokeExpression(null, meth.Expr.GetText(), exprlist.ToArray());
     }
     else
     {
         expr = new CodeSnippetExpression(expression.GetText());
     }
     //
     return expr;
 }
 private CodeExpression BuildLiteralValue(XSharpParser.LiteralValueContext context)
 {
     CodeExpression expr = null;
     ITerminalNode node;
     //
     node = context.BIN_CONST();
     if (node != null)
     {
         expr = new CodePrimitiveExpression(GetNumericValue(context));
     }
     node = context.INT_CONST();
     if (node != null)
     {
         expr = new CodePrimitiveExpression(GetNumericValue(context));
     }
     //
     node = context.HEX_CONST();
     if (node != null)
     {
         expr = new CodePrimitiveExpression(GetNumericValue(context));
     }
     //
     node = context.REAL_CONST();
     if (node != null)
     {
         expr = new CodePrimitiveExpression(GetNumericValue(context));
     }
     //
     node = context.TRUE_CONST();
     if (node != null)
     {
         expr = new CodePrimitiveExpression(true);
     }
     //
     node = context.FALSE_CONST();
     if (node != null)
     {
         expr = new CodePrimitiveExpression(false);
     }
     //
     node = context.STRING_CONST();
     if (node != null)
     {
         // Remove the quotes
         String value = context.GetText();
         value = value.Substring(1, value.Length - 2);
         expr = new CodePrimitiveExpression(value);
     }
     //
     node = context.ESCAPED_STRING_CONST();
     if (node != null)
     {
         // Remove the e in front of quotes, AND the Quotes
         String value = context.GetText();
         value = value.Substring(1);
         value = value.Substring(1, value.Length - 2);
         expr = new CodePrimitiveExpression(BuildUnEscapedString(value));
     }
     //
     node = context.CHAR_CONST();
     if (node != null)
     {
         // Remove the quotes
         String value = context.GetText();
         value = value.Substring(1, value.Length - 2);
         if (value.Length >= 1)
             expr = new CodePrimitiveExpression(value[0]);
     }
     //
     node = context.NIL();
     if (node != null)
     {
         expr = new CodeSnippetExpression("NIL");
     }
     //
     node = context.NULL();
     if (node != null)
     {
         expr = new CodePrimitiveExpression(null);
     }
     //
     node = context.NULL_ARRAY();
     if (node != null)
     {
         expr = new CodeSnippetExpression("NULL_ARRAY");
     }
     //
     node = context.NULL_CODEBLOCK();
     if (node != null)
     {
         expr = new CodeSnippetExpression("NULL_CODEBLOCK");
     }
     //
     node = context.NULL_DATE();
     if (node != null)
     {
         expr = new CodeSnippetExpression("NULL_DATE");
     }
     //
     node = context.NULL_OBJECT();
     if (node != null)
     {
         expr = new CodeSnippetExpression("NULL_OBJECT");
     }
     //            
     node = context.NULL_PSZ();
     if (node != null)
     {
         expr = new CodeSnippetExpression("NULL_PSZ");
     }
     //            
     node = context.NULL_PTR();
     if (node != null)
     {
         expr = new CodeSnippetExpression("NULL_PTR");
     }
     //            
     node = context.NULL_STRING();
     if (node != null)
     {
         expr = new CodeSnippetExpression("NULL_STRING");
     }
     //            
     node = context.NULL_SYMBOL();
     if (node != null)
     {
         expr = new CodeSnippetExpression("NULL_SYMBOL");
     }
     //
     node = context.SYMBOL_CONST();
     if (node != null)
     {
         expr = new CodeSnippetExpression(context.GetText().ToUpper());
     }
     //
     node = context.DATE_CONST();
     if (node != null)
     {
         expr = new CodeSnippetExpression(context.GetText());
     }
     //
     if ( expr == null )
     {
         expr = new CodeSnippetExpression(context.GetText());
     }
     return expr;
 }
Example #25
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 CodeTypeReference BuildDataType(XSharpParser.DatatypeContext context)
        {
            CodeTypeReference expr = null;
            //
            if (context is XSharpParser.PtrDatatypeContext)
            {
                XSharpParser.PtrDatatypeContext ptrData = (XSharpParser.PtrDatatypeContext)context;
                if (ptrData.TypeName.NativeType != null)
                    expr = BuildNativeType(ptrData.TypeName.NativeType);
                else if (ptrData.TypeName.XType != null)
                    expr = BuildXBaseType(ptrData.TypeName.XType);
                else if (ptrData.TypeName.Name != null)
                    expr = BuildName(ptrData.TypeName.Name);
            }
            else if (context is XSharpParser.ArrayDatatypeContext)
            {

            }
            else if (context is XSharpParser.SimpleDatatypeContext)
            {
                XSharpParser.SimpleDatatypeContext sdt = (XSharpParser.SimpleDatatypeContext)context;
                if (sdt.TypeName.Name != null)
                    expr = BuildName(sdt.TypeName.Name);
                else
                    expr = BuildNativeType(sdt.TypeName.GetText());
            }
            else if (context is XSharpParser.NullableDatatypeContext)
            {

            }
            //
            return expr;
        }
Example #27
0
        private static void Parse(string fileName)
        {
            ITokenStream           stream;
            IList <ParseErrorData> parseErrors = ParseErrorData.NewBag();
            var filestream = new AntlrFileStream(fileName);
            var lexer      = new XSharpLexer(filestream);

            lexer.TokenFactory = XSharpTokenFactory.Default;
            stream             = new CommonTokenStream(lexer, Lexer.DefaultTokenChannel);
            var parser = new XSharpParser(stream);

            parser.IsScript = false;
            parser.AllowFunctionInsideClass = false;
            parser.AllowNamedArgs           = false;
            parser.AllowXBaseVariables      = false;
            parser.RemoveErrorListeners();

            parser.Interpreter.PredictionMode    = PredictionMode.Sll;
            parser.Interpreter.reportAmbiguities = true;

            parser.Interpreter.enable_global_context_dfa = true; // default false
            parser.Interpreter.optimize_tail_calls       = true;
            parser.Interpreter.tail_call_preserves_sll   = true;
            //parser.Interpreter.userWantsCtxSensitive = true; // default true

            parser.ErrorHandler = new XSharpErrorStrategy();
            parser.AddErrorListener(new XSharpErrorListener(fileName, parseErrors, true));
            XSharpParserRuleContext tree;

            try
            {
                tree = parser.source();
            }
            catch (ParseCanceledException)
            {
                Console.WriteLine("Parse error, Errors from SLL mode");
                showErrors(parseErrors);
                parseErrors.Clear();
                parser.ErrorHandler = new XSharpErrorStrategy();
                parser.AddErrorListener(new XSharpErrorListener(fileName, parseErrors, true));
                parser.Interpreter.PredictionMode       = PredictionMode.Ll;
                parser.Interpreter.force_global_context = true;
                parser.Interpreter.optimize_ll1         = false;
                parser.Interpreter.reportAmbiguities    = true;
                parser.Reset();
                try
                {
                    tree = parser.source();
                }
                catch (Exception e)
                {
                    tree = null;
                    Console.WriteLine(e.Message);
                }
            }
            // find parser errors (missing tokens etc)

            foreach (var e in lexer.LexErrors)
            {
                parseErrors.Add(e);
            }
            var walker     = new ParseTreeWalker();
            var errchecker = new XSharpParseErrorAnalysis(parser, parseErrors);

            if (tree != null)
            {
                walker.Walk(errchecker, tree);
            }
            Console.WriteLine("Parse error, Errors:");
            showErrors(parseErrors);
        }
Example #28
0
        public static bool Parse(string sourceText, string fileName, CSharpParseOptions options, IErrorListener listener,
                                 out ITokenStream tokens, out XSharpParser.SourceContext tree)
        {
            tree   = null;
            tokens = null;
            var parseErrors = ParseErrorData.NewBag();

            try
            {
                var lexer = XSharpLexer.Create(sourceText, fileName, options);
                lexer.Options = options;
                BufferedTokenStream tokenStream = lexer.GetTokenStream();
                tokenStream.Fill();
                tokens = (ITokenStream)tokenStream;

                GetLexerErrors(lexer, tokenStream, parseErrors);

                // do we need to preprocess
                #region Determine if we really need the preprocessor
                bool mustPreprocess = true;
                if (lexer.HasPreprocessorTokens || !options.NoStdDef)
                {
                    // no need to pre process in partial compilation
                    // if lexer does not contain UDCs, Messages or Includes
                    mustPreprocess = lexer.MustBeProcessed;
                }
                else
                {
                    mustPreprocess = false;
                }
                #endregion
                XSharpPreprocessor  pp       = null;
                BufferedTokenStream ppStream = null;
                pp = new XSharpPreprocessor(lexer, tokenStream, options, fileName, Encoding.Unicode, SourceHashAlgorithm.None, parseErrors);

                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 token stream
                    ppStream = new CommonTokenStream(new XSharpListTokenSource(lexer, tokenStream.GetTokens()));
                }
                ppStream.Fill();
                var parser = new XSharpParser(ppStream);
                parser.Interpreter.tail_call_preserves_sll = false;     // default = true   Setting to FALSE will reduce memory used by parser
                parser.Options = options;
                tree           = null;
                parser.RemoveErrorListeners();
                parser.Interpreter.PredictionMode = PredictionMode.Sll;
                parser.ErrorHandler = new BailErrorStrategy();
                try
                {
                    tree = parser.source();
                }
                catch (Exception)
                {
                    var errorListener = new XSharpErrorListener(fileName, parseErrors);
                    parser.AddErrorListener(errorListener);
                    parser.ErrorHandler = new XSharpErrorStrategy();
                    parser.Interpreter.PredictionMode = PredictionMode.Ll;
                    ppStream.Reset();
                    parser.Reset();
                    try
                    {
                        tree = parser.source();
                    }
                    catch (Exception)
                    {
                        tree = null;
                    }
                }
            }
            catch (Exception)
            {
                tree = null;
            }
            ReportErrors(parseErrors, listener);
            return(tree != null);
        }