public static void Postfix(Localization.Locale ___sLocale) { TryWriteTemplate(); if (___sLocale == null || !(LoadLocalization(___sLocale.Code) || LoadLocalization(___sLocale.Code + ".json"))) { if (DefaultLocalization != null) { for (int i = 0; i < DefaultLocalization.Length - 1; i += 2) { Strings.Add(DefaultLocalization[i], DefaultLocalization[i + 1]); } LocalizationCompleteEvent?.Invoke(DefaultLocalizationCode); } else { LocalizationCompleteEvent?.Invoke(null); } } else { LocalizationCompleteEvent?.Invoke(___sLocale.Code); } }
/// <summary> /// Localizes the POptions UI. /// </summary> /// <param name="locale">The locale to use.</param> internal static void LocalizeItself(Localization.Locale locale) { if (locale == null) { throw new ArgumentNullException("locale"); } Localization.RegisterForTranslation(typeof(PUIStrings)); Assembly assembly = Assembly.GetExecutingAssembly(); try { using (var stream = assembly.GetManifestResourceStream(TRANSLATIONS_EMBEDDEDRESOURCE_PREFIX + locale.Code + TRANSLATIONS_EXT)) { if (stream != null) { var lines = new List <string>(); using (var reader = new StreamReader(stream, Encoding.UTF8)) { string line; while ((line = reader.ReadLine()) != null) { lines.Add(line); } } Localization.OverloadStrings(Localization.ExtractTranslatedStrings(lines.ToArray())); #if DEBUG PUtil.LogDebug("Localizing Plib itself to locale {0}".F(locale.Code)); #endif } } } catch (Exception e) { PUtil.LogWarning("Failed to load {0} localization for Plib itself:".F(locale.Code)); PUtil.LogExcWarn(e); } }
public static void SendLocalizedText(PhotonPlayer target, string file, string key, string[] args) { string[] sendArgs = args ?? new string[0]; if (!target.AnarchySync) { Localization.Locale loc; bool needClose = false; if (Localization.Language.SelectedLanguage != "English") { loc = new Localization.Locale("English", file, true, ','); loc.Load(); needClose = true; } else { loc = Localization.Language.Find(file) ?? new Localization.Locale(file, true, ','); if (!loc.IsOpen) { loc.Load(); needClose = true; } } string content = sendArgs.Length <= 0 ? loc[key] : loc.Format(key, sendArgs); FengGameManagerMKII.FGM.BasePV.RPC("Chat", target, new object[] { content, string.Empty }); if (needClose) { loc.Unload(); } } else { FengGameManagerMKII.FGM.BasePV.RPC("ChatLocalized", target, new object[] { file, key, sendArgs }); } }
public static void SendLocalizedTextAll(string file, string key, string[] args) { string[] sendArgs = args ?? new string[0]; if (Localization.Language.SelectedLanguage != Localization.Language.DefaultLanguage) { Localization.Locale loc = new Localization.Locale("English", file, true, ','); loc.Load(); string content = sendArgs.Length <= 0 ? loc[key] : loc.Format(key, sendArgs); FengGameManagerMKII.FGM.BasePV.RPC("Chat", PhotonTargets.NotAnarchy, new object[] { content, string.Empty }); loc.Unload(); } else { Localization.Locale loc = Localization.Language.Find(file); bool needClose = false; if (!loc.IsOpen) { loc.Load(); needClose = true; } string content = sendArgs.Length <= 0 ? loc[key] : loc.Format(key, sendArgs); FengGameManagerMKII.FGM.BasePV.RPC("Chat", PhotonTargets.NotAnarchy, new object[] { content, string.Empty }); if (needClose) { loc.Unload(); } } FengGameManagerMKII.FGM.BasePV.RPC("ChatLocalized", PhotonTargets.AnarchyUsers, new object[] { file, key, sendArgs }); }
/// <summary> /// Localizes the specified mod assembly. /// </summary> /// <param name="modAssembly">The assembly to localize.</param> /// <param name="locale">The locale file name to be used.</param> private static void Localize(Assembly modAssembly, Localization.Locale locale) { string path = PUtil.GetModPath(modAssembly); string locCode = locale.Code; if (string.IsNullOrEmpty(locCode)) { locCode = Localization.GetCurrentLanguageCode(); } var poFile = Path.Combine(Path.Combine(path, TRANSLATIONS_DIR), locCode + PLibLocalization.TRANSLATIONS_EXT); try { Localization.OverloadStrings(Localization.LoadStringsFile(poFile, false)); RewriteStrings(modAssembly); } catch (FileNotFoundException) { // No localization available for this locale #if DEBUG PDatabaseUtils.LogDatabaseDebug("No {0} localization available for mod {1}".F( locCode, modAssembly.GetNameSafe() ?? "?")); #endif } catch (DirectoryNotFoundException) { } catch (IOException e) { PDatabaseUtils.LogDatabaseWarning("Failed to load {0} localization for mod {1}:". F(locCode, modAssembly.GetNameSafe() ?? "?")); PUtil.LogExcWarn(e); } }
private void ConfirmLanguageChoiceDialog(string[] lines, bool is_template, System.Action install_language) { Localization.Locale locale = Localization.GetLocale(lines); Dictionary <string, string> translated_strings = Localization.ExtractTranslatedStrings(lines, is_template); TMP_FontAsset font = Localization.GetFont(locale.FontName); ConfirmDialogScreen screen = GetConfirmDialog(); HashSet <MemberInfo> excluded_members = new HashSet <MemberInfo>(typeof(ConfirmDialogScreen).GetMember("cancelButton", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy)); Localization.SetFont(screen, font, locale.IsRightToLeft, excluded_members); Func <LocString, string> func = delegate(LocString loc_string) { Dictionary <string, string> dictionary = translated_strings; StringKey key = loc_string.key; string value; return((!dictionary.TryGetValue(key.String, out value)) ? ((string)loc_string) : value); }; screen.PopupConfirmDialog(title_text: func(UI.CONFIRMDIALOG.DIALOG_HEADER), text: func(UI.FRONTEND.TRANSLATIONS_SCREEN.PLEASE_REBOOT), cancel_text: UI.FRONTEND.TRANSLATIONS_SCREEN.CANCEL, on_confirm: delegate { install_language(); App.instance.Restart(); }, on_cancel: delegate { Localization.SetFont(screen, Localization.FontAsset, Localization.IsRightToLeft, excluded_members); }, configurable_text: null, on_configurable_clicked: null, confirm_text: func(UI.FRONTEND.TRANSLATIONS_SCREEN.RESTART), image_sprite: null, activateBlackBackground: true); }
/// <summary> /// Loads language files /// </summary> public static void LoadLocale() { English = new Localization.Locale(Localization.Language.DefaultLanguage, "ChatCommands", true, ','); English.Reload(); English.FormatColors(); Lang = new Localization.Locale("ChatCommands"); Lang.Formateable = true; Lang.Reload(); Lang.FormatColors(); }
private IEnumerator Load() { string profile = ""; Info.text = "Loading configuration..."; using (ConfigFile file = new ConfigFile(Application.dataPath + "/Configuration/Settings.ini", ' ', false)) { file.Load(); file.AutoSave = false; string[] res = file.GetString("resolution").Split('x'); int width = Convert.ToInt32(res[0]); int height = Convert.ToInt32(res[1]); Screen.SetResolution(width, height, file.GetBool("fullscreen")); profile = file.GetString("profile"); int fps = file.GetInt("fps"); Application.targetFrameRate = fps >= 30 ? fps : -1; QualitySettings.SetQualityLevel(file.GetInt("graphics"), true); Localization.Language.SetLanguage(file.GetString("language")); Configuration.Settings.Load(); Configuration.VideoSettings.Apply(); } yield return(new WaitForSeconds(0.5f)); Info.text = "Loading RCAssets..."; yield return(StartCoroutine(RC.RCManager.DownloadAssets())); Optimization.Caching.Pool.Create(); yield return(new WaitForSeconds(0.5f)); Info.text = $"Loading profile({profile}).."; User.LoadProfile(profile); Localization.Language.UpdateFormats(); Localization.Locale loc = new Localization.Locale("GUI", true); GUI.LabelEnabled = loc["enabled"]; GUI.LabelDisabled = loc["disabled"]; yield return(new WaitForSeconds(0.5f)); Info.text = "Loading visuals.."; Style.Load(); Style.ResetScreenParameters(); UIManager.UpdateGUIScaling(); Optimization.Labels.Font = Style.Font; yield return(new WaitForSeconds(0.5f)); Info.text = "Enjoy!"; Optimization.Labels.VERSION = string.Format(UIMainReferences.VersionShow, AnarchyManager.AnarchyVersion.ToString()); textUpdate = false; Loading.text = "Loading complete"; yield return(new WaitForSeconds(2f)); Destroy(gameObject); AnarchyManager.Background.Enable(); }
private bool GetTranslatedStatus() { Localization.Locale locale = Localization.GetLocale(); if (locale == null) { return(true); } string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "/translations/"; string fullPath = path + locale.Code + ".po"; return(File.Exists(fullPath)); }
// загружаем строки для локализации public static void InitLocalization(Type locstring_tree_root, string filename_prefix = "", bool writeStringsTemplate = false) { // регистрируемся Localization.RegisterForTranslation(locstring_tree_root); if (writeStringsTemplate) // для записи шаблона !!!! { try { Localization.GenerateStringsTemplate(locstring_tree_root, modInfo.langDirectory); } catch (IOException e) { Debug.LogWarning($"{modInfo.assemblyName} Failed to write localization template."); LogExcWarn(e); } } Localization.Locale locale = Localization.GetLocale(); if (locale != null) { try { string langFile = Path.Combine(modInfo.langDirectory, filename_prefix + locale.Code + ".po"); if (File.Exists(langFile)) { // перезагружаем строки #if DEBUG Debug.Log($"{modInfo.assemblyName} try load LangFile: {langFile}"); #endif Localization.OverloadStrings(Localization.LoadStringsFile(langFile, false)); } } catch (IOException e) { Debug.LogWarning($"{modInfo.assemblyName} Failed to load localization."); LogExcWarn(e); } } // выполняем замену если нужно. тип должен содержать статичный метод "DoReplacement" MethodInfo methodInfo = AccessTools.Method(locstring_tree_root, "DoReplacement", new Type[0]); if (methodInfo != null) { methodInfo.Invoke(null, null); } // дополнительно создаем ключи. обычно и без этого работает, но не всегда. // нужно для опций. LocString.CreateLocStringKeys(locstring_tree_root, locstring_tree_root.Namespace + "."); }
internal static int GetId(StringConstant str, Localization.Locale locale) { int output = GetId(str.Value); if (output == -1) { throw ParserException.ThrowException( locale, Localization.ErrorMessages.UNKNOWN_CORE_FUNCTION_ID, str.FirstToken, str.Value); } return(output); }
private static string GetLocalePathSuffix() { string str = string.Empty; Localization.Locale locale = Localization.GetLocale(); if (locale != null) { Localization.Language lang = locale.Lang; if (lang == Localization.Language.Korean || lang == Localization.Language.Russian || lang == Localization.Language.Chinese) { str = locale.Code + "/"; } } return(str + "motd.json"); }
/// <summary> /// Localizes the specified mod. /// </summary> /// <param name="mod">The mod to localize.</param> /// <param name="path">The path to its data folder.</param> /// <param name="locale">The locale to use.</param> private static void Localize(Assembly mod, string path, Localization.Locale locale) { var poFile = Path.Combine(Path.Combine(path, TRANSLATIONS_DIR), locale.Code + TRANSLATIONS_EXT); if (File.Exists(poFile)) { try { Localization.OverloadStrings(Localization.LoadStringsFile(poFile, false)); } catch (IOException e) { PUtil.LogWarning("Failed to load {0} localization for mod {1}:".F(locale. Code, mod.GetName()?.Name)); PUtil.LogExcWarn(e); } } }
/// <summary> /// Localizes the specified mod. /// </summary> /// <param name="mod">The mod to localize.</param> /// <param name="path">The path to its data folder.</param> /// <param name="locale">The locale to use.</param> private static void Localize(Assembly mod, string path, Localization.Locale locale) { var poFile = Path.Combine(Path.Combine(path, TRANSLATIONS_DIR), locale.Code + TRANSLATIONS_EXT); try { Localization.OverloadStrings(Localization.LoadStringsFile(poFile, false)); } catch (FileNotFoundException) { // No localization available for this locale } catch (DirectoryNotFoundException) { } catch (IOException e) { PUtil.LogWarning("Failed to load {0} localization for mod {1}:".F(locale. Code, mod.GetName()?.Name)); PUtil.LogExcWarn(e); } }
public static void Postfix(ref Localization.Locale __result) { try { if (font != null) { var Language = fc.Code == "zh" ? Localization.Language.Chinese : Localization.Language.Unspecified; var Direction = fc.LeftToRight ? Localization.Direction.LeftToRight : Localization.Direction.RightToLeft; __result = new Localization.Locale(Language, Direction, fc.Code, font.name); } } catch (Exception ex) { DebugUtil.LogWarningArgs(new object[] { ex }); } }
public static string GetInstalledLanguageCode(out PublishedFileId_t installed) { string result = string.Empty; System.DateTime lastModified; string languageFilename = GetLanguageFilename(out installed, out lastModified); if (languageFilename != null && File.Exists(languageFilename)) { string[] lines = File.ReadAllLines(languageFilename, Encoding.UTF8); Localization.Locale locale = Localization.GetLocale(lines); if (locale != null) { result = locale.Code; } } return(result); }
public GUIBase(string name, int layer) { Name = name; Layer = GetLayer(layer, name); Directory = Application.dataPath + "/Resources/" + Name + "/"; if (!System.IO.Directory.Exists(Directory)) { System.IO.Directory.CreateDirectory(Directory); } textureCache = new Dictionary <string, Texture2D>(); locale = new Localization.Locale(name); animator = new NoneAnimation(this); if (AllBases == null) { AllBases = new List <GUIBase>(); } AllBases.Add(this); }
public static void Prefix() { Localization.Locale locale = Localization.GetLocale(); if (locale != null && locale.Code == "zh") { Strings.Add($"STRINGS.BUILDINGS.PREFABS.STAIRS.NAME", $"台阶"); Strings.Add($"STRINGS.BUILDINGS.PREFABS.STAIRS.DESC", $"(在发明了火箭之后我们甚至发明出了台阶)"); Strings.Add($"STRINGS.BUILDINGS.PREFABS.STAIRS.EFFECT", $"使复制人可以步行上下,节省力气。\n\n下台阶的速度会很快,向上则会稍慢。\n如果没有建造在可行走的方格上可能会无法正常工作。"); Strings.Add($"STRINGS.UI.USERMENUACTIONS.STAIRSBLOCK.NAME", $"禁止通过"); Strings.Add($"STRINGS.UI.USERMENUACTIONS.STAIRSBLOCK.NAME_OFF", $"允许通过"); Strings.Add($"STRINGS.UI.USERMENUACTIONS.STAIRSBLOCK.TOOLTIP", $"禁止复制人从此步行通过。"); Strings.Add($"STRINGS.UI.USERMENUACTIONS.STAIRSBLOCK.TOOLTIP_OFF", $"允许复制人从此步行通过。"); Strings.Add($"STRINGS.BUILDINGS.PREFABS.STAIRS_ALT1.NAME", $"豪华台阶"); Strings.Add($"STRINGS.BUILDINGS.PREFABS.STAIRS_ALT1.DESC", $""); Strings.Add($"STRINGS.BUILDINGS.PREFABS.STAIRS_ALT1.EFFECT", $"除了用料比较豪华外,使用上与普通台阶没有什么不同。"); Strings.Add($"STRINGS.BUILDINGS.PREFABS.URFSCAFFOLDING.NAME", $"脚手架"); Strings.Add($"STRINGS.BUILDINGS.PREFABS.URFSCAFFOLDING.DESC", $"(由于不太结实,不建议在上面攀爬)"); Strings.Add($"STRINGS.BUILDINGS.PREFABS.URFSCAFFOLDING.EFFECT", $"一种可以快速建造的薄板,可供复制人在上面行走并能与其他建筑叠加。"); } else { Strings.Add($"STRINGS.BUILDINGS.PREFABS.STAIRS.NAME", $"Stairs"); Strings.Add($"STRINGS.BUILDINGS.PREFABS.STAIRS.DESC", $"(After we made Space Rocket we even invent Stairs)"); Strings.Add($"STRINGS.BUILDINGS.PREFABS.STAIRS.EFFECT", $"Saving your energy by step up and down.\n\nWalking down strair will be easy and fast ,and tiny slower when walking up.\n" + $"It may not working correctly when built on an unwalkble floor."); Strings.Add($"STRINGS.UI.USERMENUACTIONS.STAIRSBLOCK.NAME", $"Block Path"); Strings.Add($"STRINGS.UI.USERMENUACTIONS.STAIRSBLOCK.NAME_OFF", $"Unblock Path"); Strings.Add($"STRINGS.UI.USERMENUACTIONS.STAIRSBLOCK.TOOLTIP", $"Prevent Duplicants walk through here."); Strings.Add($"STRINGS.UI.USERMENUACTIONS.STAIRSBLOCK.TOOLTIP_OFF", $"Allow Duplicants walk through here."); Strings.Add($"STRINGS.BUILDINGS.PREFABS.STAIRS_ALT1.NAME", $"Deluxe Stairs"); Strings.Add($"STRINGS.BUILDINGS.PREFABS.STAIRS_ALT1.DESC", $""); Strings.Add($"STRINGS.BUILDINGS.PREFABS.STAIRS_ALT1.EFFECT", $"In addition to the better material, there is no different from normal stairs."); Strings.Add($"STRINGS.BUILDINGS.PREFABS.URFSCAFFOLDING.NAME", $"Scaffolding"); Strings.Add($"STRINGS.BUILDINGS.PREFABS.URFSCAFFOLDING.DESC", $"(It's not very stable, any climbing is not recommended.)"); Strings.Add($"STRINGS.BUILDINGS.PREFABS.URFSCAFFOLDING.EFFECT", $"Thin plate that fast to build, allow Duplicants walk on it and can stack with other building."); } #if VANILLA AddBuildingToTechnology("Luxury", Stairs.StairsAlt1Config.ID); AddBuildingToTechnology("RefinedObjects", Stairs.ScaffoldingConfig.ID); #endif }
/// <summary> /// Localizes the PLib strings. /// </summary> /// <param name="locale">The locale to use.</param> internal static void LocalizeItself(Localization.Locale locale) { if (locale == null) { throw new ArgumentNullException(nameof(locale)); } Localization.RegisterForTranslation(typeof(PLibStrings)); var assembly = Assembly.GetExecutingAssembly(); string locCode = locale.Code; if (string.IsNullOrEmpty(locCode)) { locCode = Localization.GetCurrentLanguageCode(); } try { using (var stream = assembly.GetManifestResourceStream( TRANSLATIONS_RES_PATH + locCode + TRANSLATIONS_EXT)) { if (stream != null) { // File.ReadAllLines does not work on streams unfortunately var lines = new List <string>(128); using (var reader = new StreamReader(stream, Encoding.UTF8)) { string line; while ((line = reader.ReadLine()) != null) { lines.Add(line); } } Localization.OverloadStrings(Localization.ExtractTranslatedStrings( lines.ToArray())); #if DEBUG PUtil.LogDebug("Localizing PLib Core to locale {0}".F(locCode)); #endif } } } catch (Exception e) { PUtil.LogWarning("Failed to load {0} localization for PLib Core:".F(locCode)); PUtil.LogExcWarn(e); } }
/// <summary> /// Load a .po file and override translations /// Should be called at Localization.Initialize (Postfix) /// </summary> /// <param name="locStringRoot"> locStringRoot is typeof(YourStringsClass) </param> /// <param name="modPath"> modPath is obtained via OnLoad(string) </param> /// <param name="translationsDir"> translationsDir is the directory with your translations. /// Ideally, it should be named something other than "strings" /// because that's where the game will try to look for translations by default and load them on its own ///</param> /// Thanks exnihilo to helping making translations public static void LoadTranslations(Type locStringRoot, string modPath, string translationsDir = "translations") { // you still need to call this Localization.RegisterForTranslation(locStringRoot); Localization.Locale locale = Localization.GetLocale(); if (locale == null) { // english language is selected, so no action is needed return; } if (string.IsNullOrEmpty(modPath)) { Debug.LogError("modPath is empty"); return; } string stringsPath = Path.Combine(modPath, translationsDir ?? ""); string translationsPath = Path.Combine(stringsPath, locale.Code + ".po"); Debug.Log($"Loading translation file for {locale.Lang} ({locale.Code}) language: '{translationsPath}'"); if (!File.Exists(translationsPath)) { Debug.LogWarning($"Translation file not found: '{translationsPath}'"); return; } try { Dictionary <string, string> translations = Localization.LoadStringsFile(translationsPath, false); Localization.OverloadStrings(translations); } catch (Exception ex) { Debug.LogError($"Unexpected error while loading translation file: '{translationsPath}'"); Debug.LogError(ex); } }
private bool LoadTranslations() { string path = FileSystem.Normalize(label.install_path); if (!System.IO.Directory.Exists(path)) { return(false); } DirectoryInfo directoryInfo = new DirectoryInfo(path); HashSetPool <Localization.Locale, Mod> .PooledHashSet pooledHashSet = HashSetPool <Localization.Locale, Mod> .Allocate(); FileInfo[] files = directoryInfo.GetFiles(); foreach (FileInfo fileInfo in files) { if (!(fileInfo.Extension.ToLower() != ".po")) { string[] lines = File.ReadAllLines(fileInfo.FullName, Encoding.UTF8); pooledHashSet.Add(Localization.GetLocale(lines)); Dictionary <string, string> translated_strings = Localization.ExtractTranslatedStrings(lines, false); Localization.OverloadStrings(translated_strings); } } if (pooledHashSet.Count == 0) { return(false); } Localization.Locale new_locale = pooledHashSet.First(); if (!pooledHashSet.All((Localization.Locale locale) => locale == new_locale)) { return(false); } Localization.SetLocale(new_locale); Localization.SwapToLocalizedFont(new_locale.FontName); KPlayerPrefs.SetString(Localization.SELECTED_LANGUAGE_TYPE_KEY, 2.ToString()); KPlayerPrefs.SetString(Localization.SELECTED_LANGUAGE_CODE_KEY, new_locale.Code); return(true); }
/// <summary> /// Localizes all mods which registered for it. /// </summary> /// <param name="locale">The locale to use.</param> internal static void LocalizeAll(Localization.Locale locale) { if (locale == null) { throw new ArgumentNullException("locale"); } lock (PSharedData.GetLock(PRegistry.KEY_LOCALE_LOCK)) { // Get list holding locale information var list = PSharedData.GetData <IList <Assembly> >(PRegistry.KEY_LOCALE_TABLE); if (list != null) { PUtil.LogDebug("Localizing {0:D} mods to locale {1}".F(list.Count, locale.Code)); foreach (var mod in list) { if (mod != null) { Localize(mod, POptions.GetModDir(mod), locale); } } } } }
public abstract void Draw(UI.SmartRect rect, Localization.Locale locale);
public ParserException(Localization.ErrorMessages errorType, Token token, Localization.Locale locale) : this(token, locale.Strings.Get(errorType.ToString())) { this.TokenInfo = token; this.OriginalMessage = locale.Strings.Get(errorType.ToString()); }
public DefaultProjectGenerator(string projectId, string localeId) { this.originalProjectId = projectId.Trim(); this.projectLocale = Localization.Locale.Get(localeId); this.projectId = StringUtil.FilterStringToAlphanumerics(this.originalProjectId); }
public static Token[] Tokenize(FileScope file) { Localization.Locale locale = file.CompilationScope.Locale; string code = file.Content; // Add a newline and a dummy character at the end. // Set the length equal to the code with the newline but without the null terminator. // This makes dereferencing the index + 1 code simpler and all makes the check for the end // of word tokens and single-line comments easy. code += "\n\0"; int length = code.Length - 1; int[] lineByIndex = new int[code.Length]; int[] colByIndex = new int[code.Length]; char c; int line = 0; int col = 0; for (int i = 0; i < code.Length; ++i) { c = code[i]; lineByIndex[i] = line; colByIndex[i] = col; if (c == '\n') { ++line; col = -1; } ++col; } List <Token> tokens = new List <Token>(); TokenMode mode = TokenMode.NORMAL; char modeSubtype = ' '; int tokenStart = 0; string tokenValue; char c2; bool isTokenEnd = false; bool stringIsRaw = false; for (int i = 0; i < length; ++i) { c = code[i]; switch (mode) { case TokenMode.COMMENT: if (modeSubtype == '*') { if (c == '*' && code[i + 1] == '/') { ++i; mode = TokenMode.NORMAL; } } else { if (c == '\n') { mode = TokenMode.NORMAL; } } break; case TokenMode.NORMAL: if (WHITESPACE.Contains(c)) { // do nothing } else if (c == '/' && (code[i + 1] == '/' || code[i + 1] == '*')) { mode = TokenMode.COMMENT; modeSubtype = code[++i]; } else if (IsIdentifierChar(c)) { tokenStart = i; mode = TokenMode.WORD; } else if (c == '"' | c == '\'') { tokenStart = i; mode = TokenMode.STRING; modeSubtype = c; stringIsRaw = tokens.Count > 0 && tokens[tokens.Count - 1].Value == "@"; } else { if (c == '.') { c2 = code[i + 1]; if (c2 >= '0' && c2 <= '9') { mode = TokenMode.WORD; tokenStart = i++; } } if (mode == TokenMode.NORMAL) { tokens.Add(new Token(c.ToString(), TokenType.PUNCTUATION, file, lineByIndex[i], colByIndex[i])); } } break; case TokenMode.STRING: if (c == modeSubtype) { tokenValue = code.Substring(tokenStart, i - tokenStart + 1); tokens.Add(new Token(tokenValue, TokenType.STRING, file, lineByIndex[i], colByIndex[i])); mode = TokenMode.NORMAL; } else if (!stringIsRaw && c == '\\') { ++i; } break; case TokenMode.WORD: isTokenEnd = false; if (IsIdentifierChar(c)) { // do nothing } else if (c == '.') { if (code[tokenStart] >= '0' && code[tokenStart] <= '9') { // do nothing } else { isTokenEnd = true; } } else { isTokenEnd = true; } if (isTokenEnd) { tokenValue = code.Substring(tokenStart, i - tokenStart); c = tokenValue[0]; TokenType type = TokenType.WORD; if ((c >= '0' && c <= '9') || c == '.') { type = TokenType.NUMBER; } else if (!locale.Keywords.IsValidVariable(tokenValue)) { type = TokenType.KEYWORD; } tokens.Add(new Token(tokenValue, type, file, lineByIndex[tokenStart], colByIndex[tokenStart])); mode = TokenMode.NORMAL; --i; } break; } } switch (mode) { case TokenMode.COMMENT: throw new ParserException(file, "There is an unclosed comment in this file."); case TokenMode.STRING: throw new ParserException(file, "There is an unclosed string in this file."); case TokenMode.WORD: throw new System.InvalidOperationException(); default: break; } return(tokens.ToArray()); }
public ParserException(Localization.ErrorMessages errorType, Token token, Localization.Locale locale) : this(token, locale.Strings.Get(errorType.ToString())) { }
// Throws an exception, but also returns an exception so that you can use 'throw' syntax // at the calling site to prevent the compiler from complaining about control flow branches // that aren't actually accessible. public static ParserException ThrowException(Localization.Locale locale, Localization.ErrorMessages errorType, Token token, params string[] args) { throw new ParserException(locale.Strings.Get(errorType.ToString(), args)); }