做编译器难免会遇到如何输出错误信息的问题。这个问题不是那么好解决的,因为有可能你需要提供中文、英文还是其他什么乱七八糟语言的编译器。于是就要想一种办法让错误信息的语言可以很方便的切换。
由于同时提供中文和英文的编译器其实没什么用,中文的编译器和英文的编译器其实可以是两个不同的exe,因此我想写两个宏,譬如说中文.bat点击了之后,代码里面的错误信息就被换成中文,英文.bat被点击之后,代码里面的错误信息就被换成英文。选择完之后再编译,就可以产生需要的语言种类的编译器了。
因此我想到了一个办法。这次在
Vczh Library++3.0里面实现了一个小工具StrGen.exe(代码也一并上传了)。StrGen.exe需要有四个参数:
1、Library文件夹的位置
2、字符串资源文件的位置
3、生成的.h文件的位置
4、生成的.cpp文件的位置
首先,StrGen.exe读入下面这个文件:
1 resource:vl.scripting.basiclanguage.BasicErrorMessage
2
3 TypeNameNotExists(type)=Type {type} does not exists.
4 FunctionAlreadyExists(name)=Function {name} already exists.
5 VariableAlreadyExists(name)=Variable {name} already exists.
6 TypeAlreadyExists(name)=Type {name} already exists.
7 StructureMemberAlreadyExists(name)=Member {name} already exists.
8 VariableNotExists(name)=Variable {name} not exists.
9 FailToCast(from,to)=Fail to cast from {from} to {to}.
10 VoidFunctionNotHaveResult=Cannot access function result in a function without return value.
11 GlobalNotHaveResult=Cannot access function result in global definitions.
12 CannotInvokeNonFunctionValue(type)=Cannot invoke a value of {type}.
13 ArgumentNumnerNotMatch=Argument number should not be greater or less than the function required.
14 ArgumentTypeNotMatch(index,from,to)=Cannot implicitly cast argument {index} from {from} to {to}.
15 StructureMemberNotExists(name)=Member {name} not exists.
16 CannotConvertIndexToInt(from,to)=Cannot implicit cast the index from {from} to {to}.
17 CannotSubscribe(type)=Cannot subscribe a value of {type}.
18 UnaryOperandShouldBeLeftValue=Operand should be left value.
19 UnaryTypeNotMatch(op,type)=Unary operator {op} cannot apply to a value of {type}.
20 BinaryLeftOperandShouldBeLeftValue(op)=Left operand of binary operator {op} should be left value.
21 BinaryTypeNotMatch=(op,left,right)=Binary operator {op} cannot apply to values of {left} and {right}.
22 ConditionCannotConvertToBool(from,boolean)=Cannot convert the condition from {from} to {boolean}.
23 BreakShouldBeInLooping(breakStatement)={breakStatement} should be used in a loop.
24 ContinueShouldBeInLooping(continueStatement)={continueStatement} should be used in a loop.
25 InitializerTypeNotMatch(from,to)=Cannot convert the variable initializer from {from} to {to}.
26 ParameterCountNotMatch=Parameter number should not be greater or less than the function required.
27 ParameterAlreadyExists(name)=Parameter (name) already exists.
28 StructureMemberCannotBeUndefinedType(name)=Cannot refer to an undefined structure {name}.
29 LeftOperandShouldBeStructure=Left operand should be a structure.
30 LeftOperandShouldBePointerToStructure=Left operand should be a pointer to a structure.
31 PredeclaredStructureShouldBeDefined(name)=Predeclared structure (name) should be defined.
第一行resource告诉StrGen生成的类的namespace和类名。然后用如下的命令去生成:
1 ..\Tools\StrGen\Debug\StrGen .\ .\Scripting\Languages\BasicErrorMessage_English.txt .\Scripting\Languages\BasicErrorMessage.h .\Scripting\Languages\BasicErrorMessage.cpp
2 pause
这个English.bat放在Library目录下,所以第一个参数自然是“.\”。后面的都好理解,就是三个文件名。那么想替换成中文怎么办呢?只要写一个BasicErrorMessage_Chinese.txt,然后写一个Chinese.bat去读他就好了。bat文件里面可以放很多行,就能批量替换了。实际上生成的两个代码文件如下:
首先是BasicErrorMessage.h
1 /***********************************************************************
2 Vczh Library++ 3.0
3 Developer: 陈梓瀚(vczh)
4 StringResource::BasicErrorMessage
5
6 Classes:
7 BasicErrorMessage :字符串资源类
8
9 本文件使用Vczh String Resource Code Generator工具自动生成
10 ***********************************************************************/
11
12 #ifndef VCZH_STRING_RESOURCE_CODE_FILE_VL_SCRIPTING_BASICLANGUAGE_BASICERRORMESSAGE
13 #define VCZH_STRING_RESOURCE_CODE_FILE_VL_SCRIPTING_BASICLANGUAGE_BASICERRORMESSAGE
14
15 #include "..\..\String.h"
16
17 using namespace vl;
18
19 namespace vl
20 {
21 namespace scripting
22 {
23 namespace basiclanguage
24 {
25 class BasicErrorMessage
26 {
27 public:
28 static WString TypeNameNotExists(const WString& type);
29 static WString FunctionAlreadyExists(const WString& name);
30 static WString VariableAlreadyExists(const WString& name);
31 static WString TypeAlreadyExists(const WString& name);
32 static WString StructureMemberAlreadyExists(const WString& name);
33 static WString VariableNotExists(const WString& name);
34 static WString FailToCast(const WString& from, const WString& to);
35 static WString VoidFunctionNotHaveResult();
36 static WString GlobalNotHaveResult();
37 static WString CannotInvokeNonFunctionValue(const WString& type);
38 static WString ArgumentNumnerNotMatch();
39 static WString ArgumentTypeNotMatch(const WString& index, const WString& from, const WString& to);
40 static WString StructureMemberNotExists(const WString& name);
41 static WString CannotConvertIndexToInt(const WString& from, const WString& to);
42 static WString CannotSubscribe(const WString& type);
43 static WString UnaryOperandShouldBeLeftValue();
44 static WString UnaryTypeNotMatch(const WString& op, const WString& type);
45 static WString BinaryLeftOperandShouldBeLeftValue(const WString& op);
46 static WString BinaryTypeNotMatch();
47 static WString ConditionCannotConvertToBool(const WString& from, const WString& boolean);
48 static WString BreakShouldBeInLooping(const WString& breakStatement);
49 static WString ContinueShouldBeInLooping(const WString& continueStatement);
50 static WString InitializerTypeNotMatch(const WString& from, const WString& to);
51 static WString ParameterCountNotMatch();
52 static WString ParameterAlreadyExists(const WString& name);
53 static WString StructureMemberCannotBeUndefinedType(const WString& name);
54 static WString LeftOperandShouldBeStructure();
55 static WString LeftOperandShouldBePointerToStructure();
56 static WString PredeclaredStructureShouldBeDefined(const WString& name);
57 };
58 }
59 }
60 }
61
62 #endif
看到这里一般都知道是怎么一回事了吧。资源的名字被编译成静态函数。容易想象函数里面自然是这些内容:
1 #include "BasicErrorMessage.h"
2
3 namespace vl
4 {
5 namespace scripting
6 {
7 namespace basiclanguage
8 {
9 WString BasicErrorMessage::TypeNameNotExists(const WString& type)
10 {
11 return L"Type "+type+L" does not exists.";
12 }
13
14 WString BasicErrorMessage::FunctionAlreadyExists(const WString& name)
15 {
16 return L"Function "+name+L" already exists.";
17 }
18
19 WString BasicErrorMessage::VariableAlreadyExists(const WString& name)
20 {
21 return L"Variable "+name+L" already exists.";
22 }
23
24 WString BasicErrorMessage::TypeAlreadyExists(const WString& name)
25 {
26 return L"Type "+name+L" already exists.";
27 }
28
29 WString BasicErrorMessage::StructureMemberAlreadyExists(const WString& name)
30 {
31 return L"Member "+name+L" already exists.";
32 }
33
34 WString BasicErrorMessage::VariableNotExists(const WString& name)
35 {
36 return L"Variable "+name+L" not exists.";
37 }
38
39 WString BasicErrorMessage::FailToCast(const WString& from, const WString& to)
40 {
41 return L"Fail to cast from "+from+L" to "+to+L".";
42 }
43
44 WString BasicErrorMessage::VoidFunctionNotHaveResult()
45 {
46 return L"Cannot access function result in a function without return value.";
47 }
48
49 WString BasicErrorMessage::GlobalNotHaveResult()
50 {
51 return L"Cannot access function result in global definitions.";
52 }
53
54 WString BasicErrorMessage::CannotInvokeNonFunctionValue(const WString& type)
55 {
56 return L"Cannot invoke a value of "+type+L".";
57 }
58
59 WString BasicErrorMessage::ArgumentNumnerNotMatch()
60 {
61 return L"Argument number should not be greater or less than the function required.";
62 }
63
64 WString BasicErrorMessage::ArgumentTypeNotMatch(const WString& index, const WString& from, const WString& to)
65 {
66 return L"Cannot implicitly cast argument "+index+L" from "+from+L" to "+to+L".";
67 }
68
69 WString BasicErrorMessage::StructureMemberNotExists(const WString& name)
70 {
71 return L"Member "+name+L" not exists.";
72 }
73
74 WString BasicErrorMessage::CannotConvertIndexToInt(const WString& from, const WString& to)
75 {
76 return L"Cannot implicit cast the index from "+from+L" to "+to+L".";
77 }
78
79 WString BasicErrorMessage::CannotSubscribe(const WString& type)
80 {
81 return L"Cannot subscribe a value of "+type+L".";
82 }
83
84 WString BasicErrorMessage::UnaryOperandShouldBeLeftValue()
85 {
86 return L"Operand should be left value.";
87 }
88
89 WString BasicErrorMessage::UnaryTypeNotMatch(const WString& op, const WString& type)
90 {
91 return L"Unary operator "+op+L" cannot apply to a value of "+type+L".";
92 }
93
94 WString BasicErrorMessage::BinaryLeftOperandShouldBeLeftValue(const WString& op)
95 {
96 return L"Left operand of binary operator "+op+L" should be left value.";
97 }
98
99 WString BasicErrorMessage::BinaryTypeNotMatch()
100 {
101 return L"(op,left,right)=Binary operator "L"{op}"L" cannot apply to values of "L"{left}"L" and "L"{right}"L".";
102 }
103
104 WString BasicErrorMessage::ConditionCannotConvertToBool(const WString& from, const WString& boolean)
105 {
106 return L"Cannot convert the condition from "+from+L" to "+boolean+L".";
107 }
108
109 WString BasicErrorMessage::BreakShouldBeInLooping(const WString& breakStatement)
110 {
111 return breakStatement+L" should be used in a loop.";
112 }
113
114 WString BasicErrorMessage::ContinueShouldBeInLooping(const WString& continueStatement)
115 {
116 return continueStatement+L" should be used in a loop.";
117 }
118
119 WString BasicErrorMessage::InitializerTypeNotMatch(const WString& from, const WString& to)
120 {
121 return L"Cannot convert the variable initializer from "+from+L" to "+to+L".";
122 }
123
124 WString BasicErrorMessage::ParameterCountNotMatch()
125 {
126 return L"Parameter number should not be greater or less than the function required.";
127 }
128
129 WString BasicErrorMessage::ParameterAlreadyExists(const WString& name)
130 {
131 return L"Parameter (name) already exists.";
132 }
133
134 WString BasicErrorMessage::StructureMemberCannotBeUndefinedType(const WString& name)
135 {
136 return L"Cannot refer to an undefined structure "+name+L".";
137 }
138
139 WString BasicErrorMessage::LeftOperandShouldBeStructure()
140 {
141 return L"Left operand should be a structure.";
142 }
143
144 WString BasicErrorMessage::LeftOperandShouldBePointerToStructure()
145 {
146 return L"Left operand should be a pointer to a structure.";
147 }
148
149 WString BasicErrorMessage::PredeclaredStructureShouldBeDefined(const WString& name)
150 {
151 return L"Predeclared structure (name) should be defined.";
152 }
153
154 }
155 }
156 }
157
使用的时候就十分方便了,譬如说BasicErrorMessage::VariableNotExists(L"a"),就会给你一个相应的错误信息的字符串了。
在
Vczh Library++3.0的目录.\Tools\StrGen\下面有这个工具的源代码。
posted on 2010-03-05 23:08
陈梓瀚(vczh) 阅读(2499)
评论(2) 编辑 收藏 引用 所属分类:
VL++3.0开发纪事