diff trunk/src/Messages.d @ 309:b4d842b0d2c7

- Added new files Settings.d, config.d and lang_en.d - Removed redundant break statements from Lexer.d. - Added function Cast() to SyntaxTree.d. - Relocated compiler version info to Settings.d. - Removed messages variable from module Messages. Compiler messages are loaded dynamically now. - Relocated some functions from module Information to module Messages, and added some format functions to it. - Message tables are located in their own lang_*.d files. - Added getString() method to class StringLiteralsExpression. - Module Settings has a static struct GlobalSettings. It loads global settings from config.d and a language file.
author aziz
date Wed, 15 Aug 2007 16:07:05 +0000
parents 0bde32503976
children 6259fb93e3dd
line wrap: on
line diff
--- a/trunk/src/Messages.d	Tue Aug 14 18:35:02 2007 +0000
+++ b/trunk/src/Messages.d	Wed Aug 15 16:07:05 2007 +0000
@@ -3,10 +3,13 @@
   License: GPL3
 +/
 module Messages;
+import Settings;
+import std.stdarg;
 
-/// Index into table of error messages.
+/// Index into table of compiler messages.
 enum MID
 {
+  // Lexer messages:
   InvalidUnicodeCharacter,
   InvalidUTF8Sequence,
   // ''
@@ -51,57 +54,55 @@
   HexFloatMissingExpDigits,
   FloatExponentDigitExpected,
 
-  // Parser messages
+  // Parser messages:
   ExpectedButFound,
   RedundantStorageClass,
+
+  // Help messages:
+  HelpMain,
+}
+
+string GetMsg(MID mid)
+{
+  assert(mid < GlobalSettings.messages.length);
+  return GlobalSettings.messages[mid];
+}
+
+char[] format(MID mid, ...)
+{
+  auto args = arguments(_arguments, _argptr);
+  return format_args(GetMsg(mid), args);
+}
+
+char[] format(char[] format_str, ...)
+{
+  auto args = arguments(_arguments, _argptr);
+  return format_args(format_str, args);
 }
 
-string[] messages = [
-  "invalid Unicode character.",
-  "invalid UTF-8 sequence.",
-  // ''
-  "unterminated character literal.",
-  "empty character literal.",
-  // #line
-  "expected 'line' after '#'.",
-  `the filespec must be defined in a double quote string literal (e.g. "filespec".)`,
-  "positive integer expected after #line",
-  "newline not allowed inside special token.",
-  "expected a terminating newline after special token.",
-  // ""
-  "unterminated string literal.",
-  // x""
-  "non-hex character '{1}' found in hex string.",
-  "odd number of hex digits in hex string.",
-  "unterminated hex string.",
-  // /* */ /+ +/
-  "unterminated block comment (/* */).",
-  "unterminated nested comment (/+ +/).",
-  // `` r""
-  "unterminated raw string.",
-  "unterminated back quote string.",
-  // \x \u \U
-  "found undefined escape sequence.",
-  "insufficient number of hex digits in escape sequence.",
-  // \&[a-zA-Z][a-zA-Z0-9]+;
-  "undefined HTML entity '{1}'",
-  "unterminated HTML entity.",
-  "html entities must begin with a letter.",
-  // integer overflows
-  "decimal number overflows sign bit.",
-  "overflow in decimal number.",
-  "overflow in hexadecimal number.",
-  "overflow in binary number.",
-  "overflow in octal number.",
-  "overflow in float number.",
-  "digits 8 and 9 are not allowed in octal numbers.",
-  "invalid hex number; at least one hex digit expected.",
-  "invalid binary number; at least one binary digit expected.",
-  "the exponent of a hexadecimal float number is required.",
-  "missing decimal digits in hexadecimal float exponent.",
-  "exponents have to start with a digit.",
+char[] format_args(char[] format_str, char[][] args)
+{
+  char[] result = format_str;
+
+  foreach (i, arg; args)
+    result = std.string.replace(result, std.string.format("{%s}", i+1), arg);
+
+  return result;
+}
 
-  // Parser messages
-  "expected '{1}', but found '{2}'.",
-  "'{1}' is redundant",
-];
+char[][] arguments(TypeInfo[] tinfos, void* argptr)
+{
+  char[][] args;
+  foreach (ti; tinfos)
+  {
+    if (ti == typeid(char[]))
+      args ~= va_arg!(char[])(argptr);
+    else if (ti == typeid(int))
+      args ~= std.string.format(va_arg!(int)(argptr));
+    else if (ti == typeid(dchar))
+      args ~= std.string.format(va_arg!(dchar)(argptr));
+    else
+      assert(0, "argument type not supported yet.");
+  }
+  return args;
+}