ILexer GenerateLexerForSnippet(StringReader sr, SnippetType type)
        {
            var lexer = ParserFactory.CreateLexer(SupportedLanguage.VBNet, sr);

            lexer.SetInitialContext(type);
            return(lexer);
        }
Example #2
0
        /// <summary>
        /// Parse the code. The result may be a CompilationUnit, an Expression, a list of statements or a list of class
        /// members.
        /// </summary>
        public INode Parse(string code)
        {
            IParser parser = ParserFactory.CreateParser(language, new StringReader(code));

            parser.Parse();
            this.Errors      = parser.Errors;
            this.Specials    = parser.Lexer.SpecialTracker.RetrieveSpecials();
            this.SnippetType = SnippetType.CompilationUnit;
            INode result = parser.CompilationUnit;

            if (this.Errors.Count > 0)
            {
                if (language == SupportedLanguage.CSharp)
                {
                    // SEMICOLON HACK : without a trailing semicolon, parsing expressions does not work correctly
                    parser = ParserFactory.CreateParser(language, new StringReader(code + ";"));
                }
                else
                {
                    parser = ParserFactory.CreateParser(language, new StringReader(code));
                }
                Expression expression = parser.ParseExpression();
                if (expression != null && parser.Errors.Count < this.Errors.Count)
                {
                    this.Errors      = parser.Errors;
                    this.Specials    = parser.Lexer.SpecialTracker.RetrieveSpecials();
                    this.SnippetType = SnippetType.Expression;
                    result           = expression;
                }
            }
            if (this.Errors.Count > 0)
            {
                parser = ParserFactory.CreateParser(language, new StringReader(code));
                BlockStatement block = parser.ParseBlock();
                if (block != null && parser.Errors.Count < this.Errors.Count)
                {
                    this.Errors      = parser.Errors;
                    this.Specials    = parser.Lexer.SpecialTracker.RetrieveSpecials();
                    this.SnippetType = SnippetType.Statements;
                    result           = block;
                }
            }
            if (this.Errors.Count > 0)
            {
                parser = ParserFactory.CreateParser(language, new StringReader(code));
                List <INode> members = parser.ParseTypeMembers();
                if (members != null && members.Count > 0 && parser.Errors.Count < this.Errors.Count)
                {
                    this.Errors          = parser.Errors;
                    this.Specials        = parser.Lexer.SpecialTracker.RetrieveSpecials();
                    this.SnippetType     = SnippetType.TypeMembers;
                    result               = new NodeListNode(members);
                    result.StartLocation = members[0].StartLocation;
                    result.EndLocation   = members[members.Count - 1].EndLocation;
                }
            }
            Debug.Assert(result is CompilationUnit || !result.StartLocation.IsEmpty);
            Debug.Assert(result is CompilationUnit || !result.EndLocation.IsEmpty);
            return(result);
        }
Example #3
0
        public ContentSnippetModel GetSnippet(SnippetType snippetType)
        {
            var cacheKey = SnippetCachePrefix + snippetType.ToString();

            if (_memoryCache.TryGetValue(cacheKey, out ContentSnippetModel snippet))
            {
                return(new ContentSnippetModel()
                {
                    Content = snippet.Content,
                    SnippetType = snippet.SnippetType
                });
            }
            else
            {
                var dbModel = _contentSnippetRepository.Get(snippetType);

                if (dbModel == null)
                {
                    return(new ContentSnippetModel());
                }

                var model = new ContentSnippetModel()
                {
                    Content     = dbModel.Content,
                    SnippetType = dbModel.SnippetType
                };

                _memoryCache.Set(cacheKey, model);

                return(model);
            }
        }
Example #4
0
        VBLexer GenerateLexerForSnippet(StringReader sr, SnippetType type)
        {
            var lexer = new VBLexer(sr);

            lexer.SetInitialContext(type);
            return(lexer);
        }
Example #5
0
    /// <summary>
    /// Returns a random snippet for given snippet type.
    /// </summary>
    /// <param name="type">Type of the snippet</param>
    /// <returns></returns>
    public Snippet GetRandomSnippet(SnippetType type)
    {
        var snippets = AdventureSnippetDictionary[type];

        var randomIndex = Random.Range(0, snippets.Count);

        return(snippets[randomIndex]);
    }
Example #6
0
		public void SetContext(SnippetType type)
		{
			switch (type) {
				case SnippetType.Expression:
					currentState = startOfExpression;
					break;
			}
			
			Advance();
		}
		public void SetContext(SnippetType type)
		{
			switch (type) {
				case SnippetType.Expression:
					currentState = startOfExpression;
					break;
			}
			
			Advance();
		}
Example #8
0
        }                                                                            // TODO: Expand and replace with the IFeature if a new feature type is introduced

        internal DataSetInstance(string codeSnippetId, string link, string projectLink, SnippetType type, Dictionary <CaDETMetric, double> metricFeatures)
        {
            CodeSnippetId = codeSnippetId;
            Link          = link;
            ProjectLink   = projectLink;
            Type          = type;


            Annotations    = new HashSet <DataSetAnnotation>();
            MetricFeatures = metricFeatures;
            Validate();
        }
 public ContentSnippet Get(SnippetType snippetType)
 {
     try
     {
         return(Context.ContentSnippet.FirstOrDefault(x => x.SnippetType == snippetType));
     }
     catch (Exception ex)
     {
         log.Fatal(ex);
         throw new Exception("DB error", ex.InnerException);
     }
 }
Example #10
0
        /// <summary>
        /// Parse the code. The result may be a CompilationUnit, an Expression, a list of statements or a list of class
        /// members.
        /// </summary>
        public INode Parse(string code)
        {
            IParser parser = ParserFactory.CreateParser(new StringReader(code));

            parser.Parse();
            this.Errors      = parser.Errors;
            this.Specials    = parser.Lexer.SpecialTracker.RetrieveSpecials();
            this.SnippetType = SnippetType.CompilationUnit;
            INode result = parser.CompilationUnit;

            if (this.Errors.Count > 0)
            {
                parser = ParserFactory.CreateParser(new StringReader(code));
                Expression expression = parser.ParseExpression();
                if (expression != null && parser.Errors.Count < this.Errors.Count)
                {
                    this.Errors      = parser.Errors;
                    this.Specials    = parser.Lexer.SpecialTracker.RetrieveSpecials();
                    this.SnippetType = SnippetType.Expression;
                    result           = expression;
                }
            }
            if (this.Errors.Count > 0)
            {
                parser = ParserFactory.CreateParser(new StringReader(code));
                BlockStatement block = parser.ParseBlock();
                if (block != null && parser.Errors.Count < this.Errors.Count)
                {
                    this.Errors      = parser.Errors;
                    this.Specials    = parser.Lexer.SpecialTracker.RetrieveSpecials();
                    this.SnippetType = SnippetType.Statements;
                    result           = block;
                }
            }
            if (this.Errors.Count > 0)
            {
                parser = ParserFactory.CreateParser(new StringReader(code));
                List <INode> members = parser.ParseTypeMembers();
                if (members != null && members.Count > 0 && parser.Errors.Count < this.Errors.Count)
                {
                    this.Errors          = parser.Errors;
                    this.Specials        = parser.Lexer.SpecialTracker.RetrieveSpecials();
                    this.SnippetType     = SnippetType.TypeMembers;
                    result               = new NodeListNode(members);
                    result.StartLocation = members[0].StartLocation;
                    result.EndLocation   = members[members.Count - 1].EndLocation;
                }
            }
            Debug.Assert(result is CompilationUnit || !result.StartLocation.IsEmpty);
            Debug.Assert(result is CompilationUnit || !result.EndLocation.IsEmpty);
            return(result);
        }
 public static Snippet Create(SnippetType type, string code, int offset = 0, int startIndex = -1, int endIndex = -1, int internalOffset = 0)
 {
     return(new Snippet
     {
         Type = type,
         Code = code,
         Length = code.Length,
         StartIndex = startIndex,
         EndIndex = endIndex,
         Offset = offset,
         InternalOffset = internalOffset
     });
 }
Example #12
0
 public static Snippet Create(SnippetType type, string code, int offset = 0, int startIndex = -1, int endIndex = -1, int internalOffset = 0)
 {
     return new Snippet
     {
         Type = type,
         Code = code,
         Length = code.Length,
         StartIndex = startIndex,
         EndIndex = endIndex,
         Offset = offset,
         InternalOffset = internalOffset
     };
 }
Example #13
0
        public async Task <Snippet> AddSnippetAsync(int tokensCount, SnippetType type, int hash)
        {
            var snippet = new Snippet
            {
                TokensCount = tokensCount,
                SnippetType = type,
                Hash        = hash
            };
            await db.Snippets.AddAsync(snippet);

            await db.SaveChangesAsync();

            return(snippet);
        }
		/// <summary>
		/// Parse the code. The result may be a CompilationUnit, an Expression, a list of statements or a list of class
		/// members.
		/// </summary>
		public INode Parse(string code)
		{
			IParser parser = ParserFactory.CreateParser(language, new StringReader(code));
			parser.Parse();
			this.Errors = parser.Errors;
			this.Specials = parser.Lexer.SpecialTracker.RetrieveSpecials();
			this.SnippetType = SnippetType.CompilationUnit;
			INode result = parser.CompilationUnit;
			
			if (this.Errors.Count > 0) {
				if (language == SupportedLanguage.CSharp) {
					// SEMICOLON HACK : without a trailing semicolon, parsing expressions does not work correctly
					parser = ParserFactory.CreateParser(language, new StringReader(code + ";"));
				} else {
					parser = ParserFactory.CreateParser(language, new StringReader(code));
				}
				Expression expression = parser.ParseExpression();
				if (expression != null && parser.Errors.Count < this.Errors.Count) {
					this.Errors = parser.Errors;
					this.Specials = parser.Lexer.SpecialTracker.RetrieveSpecials();
					this.SnippetType = SnippetType.Expression;
					result = expression;
				}
			}
			if (this.Errors.Count > 0) {
				parser = ParserFactory.CreateParser(language, new StringReader(code));
				BlockStatement block = parser.ParseBlock();
				if (block != null && parser.Errors.Count < this.Errors.Count) {
					this.Errors = parser.Errors;
					this.Specials = parser.Lexer.SpecialTracker.RetrieveSpecials();
					this.SnippetType = SnippetType.Statements;
					result = block;
				}
			}
			if (this.Errors.Count > 0) {
				parser = ParserFactory.CreateParser(language, new StringReader(code));
				List<INode> members = parser.ParseTypeMembers();
				if (members != null && members.Count > 0 && parser.Errors.Count < this.Errors.Count) {
					this.Errors = parser.Errors;
					this.Specials = parser.Lexer.SpecialTracker.RetrieveSpecials();
					this.SnippetType = SnippetType.TypeMembers;
					result = new NodeListNode(members);
					result.StartLocation = members[0].StartLocation;
					result.EndLocation = members[members.Count - 1].EndLocation;
				}
			}
			Debug.Assert(result is CompilationUnit || !result.StartLocation.IsEmpty);
			Debug.Assert(result is CompilationUnit || !result.EndLocation.IsEmpty);
			return result;
		}
Example #15
0
		/// <summary>
		/// Parse the code. The result may be a CompilationUnit, an Expression, a list of statements or a list of class
		/// members.
		/// </summary>
		public INode Parse(string code)
		{
			VBParser parser = ParserFactory.CreateParser(new StringReader(code));
			parser.Parse();
			this.Errors = parser.Errors;
			this.Specials = parser.Lexer.SpecialTracker.RetrieveSpecials();
			this.SnippetType = SnippetType.CompilationUnit;
			INode result = parser.CompilationUnit;
			
			if (this.Errors.Count > 0) {
				parser = ParserFactory.CreateParser(new StringReader(code));
				Expression expression = parser.ParseExpression();
				if (expression != null && parser.Errors.Count < this.Errors.Count) {
					this.Errors = parser.Errors;
					this.Specials = parser.Lexer.SpecialTracker.RetrieveSpecials();
					this.SnippetType = SnippetType.Expression;
					result = expression;
				}
			}
			if (this.Errors.Count > 0) {
				parser = ParserFactory.CreateParser(new StringReader(code));
				BlockStatement block = parser.ParseBlock();
				if (block != null && parser.Errors.Count < this.Errors.Count) {
					this.Errors = parser.Errors;
					this.Specials = parser.Lexer.SpecialTracker.RetrieveSpecials();
					this.SnippetType = SnippetType.Statements;
					result = block;
				}
			}
			if (this.Errors.Count > 0) {
				parser = ParserFactory.CreateParser(new StringReader(code));
				List<INode> members = parser.ParseTypeMembers();
				if (members != null && members.Count > 0 && parser.Errors.Count < this.Errors.Count) {
					this.Errors = parser.Errors;
					this.Specials = parser.Lexer.SpecialTracker.RetrieveSpecials();
					this.SnippetType = SnippetType.TypeMembers;
					result = new NodeListNode(members);
					result.StartLocation = members[0].StartLocation;
					result.EndLocation = members[members.Count - 1].EndLocation;
				}
			}
			Debug.Assert(result is CompilationUnit || !result.StartLocation.IsEmpty);
			Debug.Assert(result is CompilationUnit || !result.EndLocation.IsEmpty);
			return result;
		}
		public void SetInitialContext(SnippetType type)
		{
			ef.SetContext(type);
		}
Example #17
0
 public override string ToString()
 {
     return($"SnippetWith{SnippetType.ToString()}(Hash={Hash}, TokensCount={TokensCount})");
 }
Example #18
0
        public static string GetSnippet(Type typeClass, SnippetType snipType, bool isMobile)
        {
            //bool isMobile=CrudGenHelper.IsMobile(typeClass);
            string Sname = GetSname(typeClass.Name);

            FieldInfo[] fields  = typeClass.GetFields();         //We can't assume they are in the correct order.
            FieldInfo   priKey  = null;
            FieldInfo   priKey1 = null;
            FieldInfo   priKey2 = null;

            if (isMobile)
            {
                priKey1 = CrudGenHelper.GetPriKeyMobile1(fields, typeClass.Name);
                priKey2 = CrudGenHelper.GetPriKeyMobile2(fields, typeClass.Name);
            }
            else
            {
                priKey = CrudGenHelper.GetPriKey(fields, typeClass.Name);
            }
            string priKeyParam  = null;
            string priKeyParam1 = null;
            string priKeyParam2 = null;

            if (isMobile)
            {
                priKeyParam1 = priKey1.Name.Substring(0, 1).ToLower() + priKey1.Name.Substring(1);           //lowercase initial letter.  Example customerNum
                priKeyParam2 = priKey2.Name.Substring(0, 1).ToLower() + priKey2.Name.Substring(1);           //lowercase initial letter.  Example patNum
            }
            else
            {
                priKeyParam = priKey.Name.Substring(0, 1).ToLower() + priKey.Name.Substring(1);                        //lowercase initial letter.  Example patNum
            }
            string           obj             = typeClass.Name.Substring(0, 1).ToLower() + typeClass.Name.Substring(1); //lowercase initial letter.  Example feeSched
            string           tablename       = CrudGenHelper.GetTableName(typeClass);                                  //in lowercase now.  Example feesched
            List <FieldInfo> fieldsExceptPri = null;

            if (isMobile)
            {
                fieldsExceptPri = CrudGenHelper.GetFieldsExceptPriKey(fields, priKey2);             //for mobile, only excludes PK2
            }
            else
            {
                fieldsExceptPri = CrudGenHelper.GetFieldsExceptPriKey(fields, priKey);
            }
            bool isTableHist        = CrudGenHelper.IsTableHist(typeClass);
            List <DbSchemaCol> cols = CrudQueries.GetListColumns(priKey.Name, null, fieldsExceptPri, false);

            switch (snipType)
            {
            default:
                return("snippet type not found.");

            case SnippetType.Insert:
                if (isMobile)
                {
                    return(GetInsert(typeClass.Name, obj, null, true));
                }
                else
                {
                    return(GetInsert(typeClass.Name, obj, priKey.Name, false));
                }

            case SnippetType.Update:
                return(GetUpdate(typeClass.Name, obj, isMobile, isTableHist));

            case SnippetType.EntireSclass:
                if (isMobile)
                {
                    return(GetEntireSclassMobile(typeClass.Name, obj, priKey1.Name, priKey2.Name, Sname, tablename, priKeyParam1, priKeyParam2, isTableHist));
                }
                else
                {
                    List <Permissions> listAuditTrailPerms = GroupPermissions.GetPermsFromCrudAuditPerm(CrudTableAttribute.GetCrudAuditPermForClass(typeClass));
                    return(GetEntireSclass(typeClass.Name, obj, priKey.Name, Sname, tablename, priKeyParam, listAuditTrailPerms, isTableHist));
                }

            case SnippetType.AddTable:
                return(CrudSchemaRaw.AddTable(tablename, cols, 0, false, false));

            case SnippetType.AddColumnEnd:
                return(string.Join("\r\n\r\n", cols.Select(x => CrudSchemaRaw.AddColumnEnd(tablename, x, 0, false, typeClass).Trim())));

            case SnippetType.AddIndex:
                return(string.Join("\r\n\r\n", cols.Select(x => CrudSchemaRaw.AddIndex(tablename, x.ColumnName, 0).Trim())));

            case SnippetType.DropColumn:
                return(string.Join("\r\n\r\n", cols.Select(x => CrudSchemaRaw.DropColumn(tablename, x.ColumnName, 0).Trim())));
            }
        }
Example #19
0
 public virtual void SetInitialContext(SnippetType context)
 {
     throw new NotSupportedException();
 }
Example #20
0
		ILexer GenerateLexerForSnippet(StringReader sr, SnippetType type)
		{
			var lexer = ParserFactory.CreateLexer(SupportedLanguage.VBNet, sr);
			lexer.SetInitialContext(type);
			return lexer;
		}
Example #21
0
 public List <DataSetInstance> GetInstancesOfType(SnippetType type)
 {
     return(_instances.Where(i => i.Type.Equals(type)).ToList());
 }
Example #22
0
		public virtual void SetInitialContext(SnippetType context)
		{
			throw new NotSupportedException();
		}
Example #23
0
        public static string GetSnippet(Type typeClass, SnippetType snipType, bool isMobile)
        {
            //bool isMobile=CrudGenHelper.IsMobile(typeClass);
            string Sname = GetSname(typeClass.Name);

            FieldInfo[] fields  = typeClass.GetFields();         //We can't assume they are in the correct order.
            FieldInfo   priKey  = null;
            FieldInfo   priKey1 = null;
            FieldInfo   priKey2 = null;

            if (isMobile)
            {
                priKey1 = CrudGenHelper.GetPriKeyMobile1(fields, typeClass.Name);
                priKey2 = CrudGenHelper.GetPriKeyMobile2(fields, typeClass.Name);
            }
            else
            {
                priKey = CrudGenHelper.GetPriKey(fields, typeClass.Name);
            }
            string priKeyParam  = null;
            string priKeyParam1 = null;
            string priKeyParam2 = null;

            if (isMobile)
            {
                priKeyParam1 = priKey1.Name.Substring(0, 1).ToLower() + priKey1.Name.Substring(1);           //lowercase initial letter.  Example customerNum
                priKeyParam2 = priKey2.Name.Substring(0, 1).ToLower() + priKey2.Name.Substring(1);           //lowercase initial letter.  Example patNum
            }
            else
            {
                priKeyParam = priKey.Name.Substring(0, 1).ToLower() + priKey.Name.Substring(1);                        //lowercase initial letter.  Example patNum
            }
            string           obj             = typeClass.Name.Substring(0, 1).ToLower() + typeClass.Name.Substring(1); //lowercase initial letter.  Example feeSched
            string           tablename       = CrudGenHelper.GetTableName(typeClass);                                  //in lowercase now.  Example feesched
            List <FieldInfo> fieldsExceptPri = null;

            if (isMobile)
            {
                fieldsExceptPri = CrudGenHelper.GetFieldsExceptPriKey(fields, priKey2);             //for mobile, only excludes PK2
            }
            else
            {
                fieldsExceptPri = CrudGenHelper.GetFieldsExceptPriKey(fields, priKey);
            }
            switch (snipType)
            {
            default:
                return("snippet type not found.");

            case SnippetType.Insert:
                if (isMobile)
                {
                    return(GetInsert(typeClass.Name, obj, null, true));
                }
                else
                {
                    return(GetInsert(typeClass.Name, obj, priKey.Name, false));
                }

            case SnippetType.Update:
                return(GetUpdate(typeClass.Name, obj, isMobile));

            case SnippetType.EntireSclass:
                if (isMobile)
                {
                    return(GetEntireSclassMobile(typeClass.Name, obj, priKey1.Name, priKey2.Name, Sname, tablename, priKeyParam1, priKeyParam2));
                }
                else
                {
                    return(GetEntireSclass(typeClass.Name, obj, priKey.Name, Sname, tablename, priKeyParam));
                }

            case SnippetType.CreateTable:
                return("Currently unavailable.");

                /*
                 * if(isMobile) {
                 *      return GetCreateTable(tablename,priKey1.Name,priKey2.Name,fieldsExceptPri);
                 * }
                 * else {
                 *      return GetCreateTable(tablename,priKey.Name,null,fieldsExceptPri);
                 * }*/
            }
        }
Example #24
0
 public Task <bool> IsSnippetExistsAsync(int tokensCount, SnippetType type, int hash)
 {
     return(db.Snippets.AnyAsync(s => s.TokensCount == tokensCount && s.SnippetType == type && s.Hash == hash));
 }
Example #25
0
		public static string GetSnippet(Type typeClass,SnippetType snipType,bool isMobile){
			//bool isMobile=CrudGenHelper.IsMobile(typeClass);
			string Sname=GetSname(typeClass.Name);
			FieldInfo[] fields=typeClass.GetFields();//We can't assume they are in the correct order.
			FieldInfo priKey=null;
			FieldInfo priKey1=null;
			FieldInfo priKey2=null;
			if(isMobile) {
				priKey1=CrudGenHelper.GetPriKeyMobile1(fields,typeClass.Name);
				priKey2=CrudGenHelper.GetPriKeyMobile2(fields,typeClass.Name);
			}
			else {
				priKey=CrudGenHelper.GetPriKey(fields,typeClass.Name);
			}
			string priKeyParam=null;
			string priKeyParam1=null;
			string priKeyParam2=null;
			if(isMobile) {
				priKeyParam1=priKey1.Name.Substring(0,1).ToLower()+priKey1.Name.Substring(1);//lowercase initial letter.  Example customerNum
				priKeyParam2=priKey2.Name.Substring(0,1).ToLower()+priKey2.Name.Substring(1);//lowercase initial letter.  Example patNum
			}
			else {
				priKeyParam=priKey.Name.Substring(0,1).ToLower()+priKey.Name.Substring(1);//lowercase initial letter.  Example patNum
			}
			string obj=typeClass.Name.Substring(0,1).ToLower()+typeClass.Name.Substring(1);//lowercase initial letter.  Example feeSched
			string tablename=CrudGenHelper.GetTableName(typeClass);//in lowercase now.  Example feesched
			List<FieldInfo> fieldsExceptPri=null;
			if(isMobile) {
				fieldsExceptPri=CrudGenHelper.GetFieldsExceptPriKey(fields,priKey2);//for mobile, only excludes PK2
			}
			else {
				fieldsExceptPri=CrudGenHelper.GetFieldsExceptPriKey(fields,priKey);
			}
			switch(snipType){
				default:
					return "snippet type not found.";
				case SnippetType.Insert:
					if(isMobile) {
						return GetInsert(typeClass.Name,obj,null,true);
					}
					else {
						return GetInsert(typeClass.Name,obj,priKey.Name,false);
					}
				case SnippetType.Update:
					return GetUpdate(typeClass.Name,obj,isMobile);
				case SnippetType.EntireSclass:
					if(isMobile) {
						return GetEntireSclassMobile(typeClass.Name,obj,priKey1.Name,priKey2.Name,Sname,tablename,priKeyParam1,priKeyParam2);
					}
					else {
						return GetEntireSclass(typeClass.Name,obj,priKey.Name,Sname,tablename,priKeyParam);
					}
				case SnippetType.CreateTable:
					return "Currently unavailable.";
					/*
					if(isMobile) {
						return GetCreateTable(tablename,priKey1.Name,priKey2.Name,fieldsExceptPri);
					}
					else {
						return GetCreateTable(tablename,priKey.Name,null,fieldsExceptPri);
					}*/
			}
		}
Example #26
0
		VBLexer GenerateLexerForSnippet(StringReader sr, SnippetType type)
		{
			var lexer = new VBLexer(sr);
			lexer.SetInitialContext(type);
			return lexer;
		}
Example #27
0
        public void ClearSnippetCache(SnippetType snippetType)
        {
            var cacheKey = SnippetCachePrefix + snippetType.ToString();

            _memoryCache.Remove(cacheKey);
        }