private static void LoadStandardKeywords(XmlElement container) { CKeywords.Clear(); CPPKeywords.Clear(); ArduinoKeywords.Clear(); preprocKeywords.Clear(); foreach (XmlElement lib in container.GetElementsByTagName("C")) { foreach (XmlElement group in lib.GetElementsByTagName("Group")) { string[] wordList = group.InnerText.Split(new char[] { ' ', '\t', '\r', '\n', }); foreach (string word in wordList) { if (string.IsNullOrEmpty(word) == false) { CodeKeyword kw = new CodeKeyword(word, KeywordSource.C, KeywordTypeFromString(group.GetAttribute("type"))); if (kw.Type == KeywordType.Preprocessor) { if (preprocKeywords.Contains(kw.ListEntry) == false) { preprocKeywords.Add(kw.ListEntry); } } else if (CKeywords.ContainsKey(word) == false) { CKeywords.Add(word, kw); } } } } break; } foreach (XmlElement lib in container.GetElementsByTagName("CPP")) { foreach (XmlElement group in lib.GetElementsByTagName("Group")) { string[] wordList = group.InnerText.Split(new char[] { ' ', '\t', '\r', '\n', }); foreach (string word in wordList) { if (string.IsNullOrEmpty(word) == false) { CodeKeyword kw = new CodeKeyword(word, KeywordSource.CPP, KeywordTypeFromString(group.GetAttribute("type"))); if (kw.Type == KeywordType.Preprocessor) { if (preprocKeywords.Contains(kw.ListEntry) == false) { preprocKeywords.Add(kw.ListEntry); } } else if (CPPKeywords.ContainsKey(word) == false) { CPPKeywords.Add(word, kw); } } } } break; } foreach (XmlElement lib in container.GetElementsByTagName("Arduino")) { foreach (XmlElement group in lib.GetElementsByTagName("Group")) { string[] wordList = group.InnerText.Split(new char[] { ' ', '\t', '\r', '\n', }); foreach (string word in wordList) { if (string.IsNullOrEmpty(word) == false) { CodeKeyword kw = new CodeKeyword(word, KeywordSource.Arduino, KeywordTypeFromString(group.GetAttribute("type"))); if (ArduinoKeywords.ContainsKey(word) == false) { ArduinoKeywords.Add(word, kw); } } } } break; } LoadAVRLibcKeywords(container); preprocKeywords.Sort((x, y) => string.Compare(x, y)); }
private static void LoadStandardKeywords(XmlElement container) { CKeywords.Clear(); CPPKeywords.Clear(); ArduinoKeywords.Clear(); preprocKeywords.Clear(); foreach (XmlElement lib in container.GetElementsByTagName("C")) { foreach (XmlElement group in lib.GetElementsByTagName("Group")) { string[] wordList = group.InnerText.Split(new char[] { ' ', '\t', '\r', '\n', }); foreach (string word in wordList) { if (string.IsNullOrEmpty(word) == false) { CodeKeyword kw = new CodeKeyword(word, KeywordSource.C, KeywordTypeFromString(group.GetAttribute("type"))); if (kw.Type == KeywordType.Preprocessor) { if (preprocKeywords.Contains(kw.ListEntry) == false) preprocKeywords.Add(kw.ListEntry); } else if (CKeywords.ContainsKey(word) == false) CKeywords.Add(word, kw); } } } break; } foreach (XmlElement lib in container.GetElementsByTagName("CPP")) { foreach (XmlElement group in lib.GetElementsByTagName("Group")) { string[] wordList = group.InnerText.Split(new char[] { ' ', '\t', '\r', '\n', }); foreach (string word in wordList) { if (string.IsNullOrEmpty(word) == false) { CodeKeyword kw = new CodeKeyword(word, KeywordSource.CPP, KeywordTypeFromString(group.GetAttribute("type"))); if (kw.Type == KeywordType.Preprocessor) { if (preprocKeywords.Contains(kw.ListEntry) == false) preprocKeywords.Add(kw.ListEntry); } else if (CPPKeywords.ContainsKey(word) == false) CPPKeywords.Add(word, kw); } } } break; } foreach (XmlElement lib in container.GetElementsByTagName("Arduino")) { foreach (XmlElement group in lib.GetElementsByTagName("Group")) { string[] wordList = group.InnerText.Split(new char[] { ' ', '\t', '\r', '\n', }); foreach (string word in wordList) { if (string.IsNullOrEmpty(word) == false) { CodeKeyword kw = new CodeKeyword(word, KeywordSource.Arduino, KeywordTypeFromString(group.GetAttribute("type"))); if (ArduinoKeywords.ContainsKey(word) == false) ArduinoKeywords.Add(word, kw); } } } break; } LoadAVRLibcKeywords(container); preprocKeywords.Sort((x, y) => string.Compare(x, y)); }
public static List <string> GetKeywordsUpTo(ProjectFile file, string content) { if (allResReady.WaitOne(1000) == false) { return(new List <string>()); } List <string> finalRes = new List <string>(); if (allResults.ContainsKey(file)) { finalRes.AddRange(allResults[file]); } int contentLen = content.Length; content = Environment.NewLine + content + Environment.NewLine; string noStringContent = ""; bool inStreamComment = false; bool inLineComment = false; bool inString = false; bool inChar = false; content = Environment.NewLine + content; int originalLength = content.Length; content += Environment.NewLine; for (int i = 1; i < originalLength; i++) { char c = content[i]; if ((inString || inChar) && c == '\\' && content[i + 1] == '\\') { content = content.Remove(i, 2); content = content.Insert(i, " "); } else if (c == '"' && content[i - 1] != '\\' && inStreamComment == false && inLineComment == false && inChar == false) { inString = !inString; } else if (c == '\'' && content[i - 1] != '\\' && inStreamComment == false && inLineComment == false && inString == false) { inChar = !inChar; } else if (c == '/' && content[i + 1] == '/' && inString == false && inChar == false) { inLineComment = true; i += 2; } else if (c == '/' && content[i + 1] == '*' && inLineComment == false && inString == false && inChar == false) { inStreamComment = true; } else if (c == '*' && content[i + 1] == '/' && inString == false && inChar == false) { inStreamComment = false; if (inLineComment == false) { i += 2; } } else if (c == '\n') { inLineComment = false; inString = false; inChar = false; } if (inStreamComment == false && inLineComment == false) { noStringContent += c; } } contentLen = noStringContent.Length; content = Environment.NewLine + noStringContent + Environment.NewLine; string res = ""; int braceNest = 0; int maxNest = 0; int bracketNest = 0; for (int i = contentLen - 1; i >= 0; i--) { char c = content[i]; if (c == '}') { braceNest++; } else if (c == '{') { braceNest--; } else if (c == ')') { bracketNest++; } else if (c == '(') { bracketNest--; } maxNest = Convert.ToInt32(Math.Min(maxNest, braceNest)); if (braceNest <= maxNest) { res = c + res; } } Regex r = new Regex("([^a-zA-Z0-9_])([a-zA-Z_][a-zA-Z0-9_]*)(\\s*)([^a-zA-Z0-9_])", RegexOptions.Multiline); Match m = r.Match("`" + res + "`"); while (m.Success) { string text = m.Value; KeywordType type = KeywordType.Other; if (text.EndsWith("(")) { type = KeywordType.Function; } while (char.IsLetterOrDigit(text, 0) == false && text[0] != '_') { text = text.Substring(1); } while (char.IsLetterOrDigit(text, text.Length - 1) == false && text[text.Length - 1] != '_') { text = text.Substring(0, text.Length - 1); } CodeKeyword kw = new CodeKeyword(text, KeywordSource.User, type); CodeKeyword ppt1 = new CodeKeyword(text, KeywordSource.C, KeywordType.Preprocessor); CodeKeyword ppt2 = new CodeKeyword(text, KeywordSource.CPP, KeywordType.Preprocessor); if (preprocKeywords.Contains(ppt1.ListEntry) == false && preprocKeywords.Contains(ppt2.ListEntry) == false) { if (finalRes.Contains(kw.ListEntry) == false) { finalRes.Add(kw.ListEntry); } } m = m.NextMatch(); } finalRes.Sort((x, y) => string.Compare(x, y)); return(finalRes); }
public static List<string> GetKeywordsUpTo(ProjectFile file, string content) { if (allResReady.WaitOne(1000) == false) return new List<string>(); List<string> finalRes = new List<string>(); if (allResults.ContainsKey(file)) finalRes.AddRange(allResults[file]); int contentLen = content.Length; content = Environment.NewLine + content + Environment.NewLine; string noStringContent = ""; bool inStreamComment = false; bool inLineComment = false; bool inString = false; bool inChar = false; content = Environment.NewLine + content; int originalLength = content.Length; content += Environment.NewLine; for (int i = 1; i < originalLength; i++) { char c = content[i]; if ((inString || inChar) && c == '\\' && content[i + 1] == '\\') { content = content.Remove(i, 2); content = content.Insert(i, " "); } else if (c == '"' && content[i - 1] != '\\' && inStreamComment == false && inLineComment == false && inChar == false) { inString = !inString; } else if (c == '\'' && content[i - 1] != '\\' && inStreamComment == false && inLineComment == false && inString == false) { inChar = !inChar; } else if (c == '/' && content[i + 1] == '/' && inString == false && inChar == false) { inLineComment = true; i += 2; } else if (c == '/' && content[i + 1] == '*' && inLineComment == false && inString == false && inChar == false) { inStreamComment = true; } else if (c == '*' && content[i + 1] == '/' && inString == false && inChar == false) { inStreamComment = false; if (inLineComment == false) { i += 2; } } else if (c == '\n') { inLineComment = false; inString = false; inChar = false; } if (inStreamComment == false && inLineComment == false) { noStringContent += c; } } contentLen = noStringContent.Length; content = Environment.NewLine + noStringContent + Environment.NewLine; string res = ""; int braceNest = 0; int maxNest = 0; int bracketNest = 0; for (int i = contentLen - 1; i >= 0; i--) { char c = content[i]; if (c == '}') braceNest++; else if (c == '{') braceNest--; else if (c == ')') bracketNest++; else if (c == '(') bracketNest--; maxNest = Convert.ToInt32(Math.Min(maxNest, braceNest)); if (braceNest <= maxNest) res = c + res; } Regex r = new Regex("([^a-zA-Z0-9_])([a-zA-Z_][a-zA-Z0-9_]*)(\\s*)([^a-zA-Z0-9_])", RegexOptions.Multiline); Match m = r.Match("`" + res + "`"); while (m.Success) { string text = m.Value; KeywordType type = KeywordType.Other; if (text.EndsWith("(")) type = KeywordType.Function; while (char.IsLetterOrDigit(text, 0) == false && text[0] != '_') text = text.Substring(1); while (char.IsLetterOrDigit(text, text.Length - 1) == false && text[text.Length - 1] != '_') text = text.Substring(0, text.Length - 1); CodeKeyword kw = new CodeKeyword(text, KeywordSource.User, type); CodeKeyword ppt1 = new CodeKeyword(text, KeywordSource.C, KeywordType.Preprocessor); CodeKeyword ppt2 = new CodeKeyword(text, KeywordSource.CPP, KeywordType.Preprocessor); if (preprocKeywords.Contains(ppt1.ListEntry) == false && preprocKeywords.Contains(ppt2.ListEntry) == false) if (finalRes.Contains(kw.ListEntry) == false) finalRes.Add(kw.ListEntry); m = m.NextMatch(); } finalRes.Sort((x, y) => string.Compare(x, y)); return finalRes; }