1 | #include "stdafx.h"
|
---|
2 | #include <abdev/ab_common/include/ab_common.h>
|
---|
3 | #include "LexicalAnalyzer.h"
|
---|
4 | #include "Compiler.h"
|
---|
5 | #include "StrOperation.h"
|
---|
6 | #include <boost/algorithm/string/predicate.hpp>
|
---|
7 |
|
---|
8 | using namespace ActiveBasic::Compiler;
|
---|
9 |
|
---|
10 | void LexicalAnalyzer::AddTypeDef( TypeDefCollection &typeDefs, const NamespaceScopes &namespaceScopes, const boost::iterator_range<char const*> &expression, int nowLine )
|
---|
11 | {
|
---|
12 | auto name = boost::find<boost::return_begin_found>(expression, '=');
|
---|
13 |
|
---|
14 | if (boost::end(name) == boost::end(expression))
|
---|
15 | {
|
---|
16 | compiler.errorMessenger.Output(10, "TypeDef", nowLine);
|
---|
17 | return;
|
---|
18 | }
|
---|
19 |
|
---|
20 | const char *pTemp = expression.begin() + boost::size(name) + 1;
|
---|
21 |
|
---|
22 | //識別文字のエラーチェック(新しい型)
|
---|
23 | if (!boost::algorithm::all(name, [](char c) {return IsVariableChar(c, true);}))
|
---|
24 | {
|
---|
25 | compiler.errorMessenger.Output(10, "TypeDef", nowLine);
|
---|
26 | return;
|
---|
27 | }
|
---|
28 |
|
---|
29 | //識別文字のエラーチェック(コピー元の型)
|
---|
30 | if(pTemp[0]=='*'&&pTemp[1]==1&&(pTemp[2]==ESC_FUNCTION||pTemp[2]==ESC_SUB)){
|
---|
31 | //関数ポインタ
|
---|
32 | if(pTemp[3]!='('){
|
---|
33 | compiler.errorMessenger.Output(10,"TypeDef",nowLine);
|
---|
34 | return;
|
---|
35 | }
|
---|
36 | }
|
---|
37 | else
|
---|
38 | {
|
---|
39 | int i=0;
|
---|
40 | while(pTemp[i]=='*') i++;
|
---|
41 | if (!boost::algorithm::all(name, [](char c) {return IsVariableChar(c, true);}))
|
---|
42 | {
|
---|
43 | compiler.errorMessenger.Output(10,"TypeDef",nowLine);
|
---|
44 | return;
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | std::string baseName(pTemp, expression.end());
|
---|
49 |
|
---|
50 | //識別子が重複している場合はエラーにする
|
---|
51 | if (boost::algorithm::equals(name, baseName))
|
---|
52 | {
|
---|
53 | compiler.errorMessenger.Output(1,NULL,nowLine);
|
---|
54 | return;
|
---|
55 | }
|
---|
56 |
|
---|
57 |
|
---|
58 |
|
---|
59 | //////////////////////////
|
---|
60 | // TypeDef情報を追加
|
---|
61 | //////////////////////////
|
---|
62 |
|
---|
63 | Type baseType;
|
---|
64 | if( !compiler.StringToType( baseName, baseType ) )
|
---|
65 | {
|
---|
66 | compiler.errorMessenger.Output(3, baseName, nowLine );
|
---|
67 | return;
|
---|
68 | }
|
---|
69 |
|
---|
70 | typeDefs.push_back(
|
---|
71 | TypeDef(
|
---|
72 | Symbol(namespaceScopes, boost::copy_range<std::string>(name)),
|
---|
73 | baseName,
|
---|
74 | baseType
|
---|
75 | )
|
---|
76 | );
|
---|
77 | }
|
---|
78 | void LexicalAnalyzer::CollectTypeDefs( const char *source, TypeDefCollection &typeDefs )
|
---|
79 | {
|
---|
80 | // 名前空間管理
|
---|
81 | NamespaceScopes &namespaceScopes = compiler.GetNamespaceSupporter().GetLivingNamespaceScopes();
|
---|
82 | namespaceScopes.clear();
|
---|
83 |
|
---|
84 | // Imports情報のクリア
|
---|
85 | compiler.GetNamespaceSupporter().ClearImportedNamespaces();
|
---|
86 |
|
---|
87 | for (int i = 0; ; ++i)
|
---|
88 | {
|
---|
89 | if( source[i] == 1 && source[i+1] == ESC_NAMESPACE ){
|
---|
90 | i+=2;
|
---|
91 | char const* p = &source[i];
|
---|
92 | while (!IsCommandDelimitation(source[i]))
|
---|
93 | {
|
---|
94 | ++i;
|
---|
95 | }
|
---|
96 | namespaceScopes.push_back(std::string(p, &source[i]));
|
---|
97 |
|
---|
98 | continue;
|
---|
99 | }
|
---|
100 | else if( source[i] == 1 && source[i+1] == ESC_ENDNAMESPACE ){
|
---|
101 | if( namespaceScopes.size() <= 0 ){
|
---|
102 | compiler.errorMessenger.Output(12, "End Namespace", i );
|
---|
103 | }
|
---|
104 | else{
|
---|
105 | namespaceScopes.pop_back();
|
---|
106 | }
|
---|
107 |
|
---|
108 | i += 2;
|
---|
109 | continue;
|
---|
110 | }
|
---|
111 | else if( source[i] == 1 && source[i+1] == ESC_IMPORTS ){
|
---|
112 | i+=2;
|
---|
113 | char const* p = &source[i];
|
---|
114 | while (!IsCommandDelimitation(source[i]))
|
---|
115 | {
|
---|
116 | ++i;
|
---|
117 | }
|
---|
118 | std::string s(p, &source[i]);
|
---|
119 | if (!compiler.GetNamespaceSupporter().ImportsNamespace(s))
|
---|
120 | {
|
---|
121 | compiler.errorMessenger.Output(64, s.c_str(), i);
|
---|
122 | }
|
---|
123 |
|
---|
124 | continue;
|
---|
125 | }
|
---|
126 | else if( source[i] == 1 && source[i+1] == ESC_CLEARNAMESPACEIMPORTED )
|
---|
127 | {
|
---|
128 | // Imports情報のクリア
|
---|
129 | compiler.GetNamespaceSupporter().ClearImportedNamespaces();
|
---|
130 | continue;
|
---|
131 | }
|
---|
132 |
|
---|
133 | if( source[i]==1 ){
|
---|
134 | char temporary[VN_SIZE];
|
---|
135 | if(source[i+1]==ESC_TYPEDEF){
|
---|
136 | i+=2;
|
---|
137 | char const* p = &source[i];
|
---|
138 | while (!IsCommandDelimitation(source[i]))
|
---|
139 | {
|
---|
140 | ++i;
|
---|
141 | }
|
---|
142 | AddTypeDef(typeDefs, namespaceScopes, boost::make_iterator_range(p, &source[i]), i);
|
---|
143 | continue;
|
---|
144 | }
|
---|
145 | else if( source[i+1] == ESC_CONST && source[i+2] == 1 && source[i+3] == ESC_ENUM ){
|
---|
146 | int i2 = 0;
|
---|
147 | for(i+=4;;i2++,i++){
|
---|
148 | if(!IsVariableChar(source[i])){
|
---|
149 | temporary[i2]=0;
|
---|
150 | break;
|
---|
151 | }
|
---|
152 | temporary[i2]=source[i];
|
---|
153 | if(source[i]=='\0') break;
|
---|
154 | }
|
---|
155 |
|
---|
156 | typeDefs.push_back(
|
---|
157 | TypeDef(
|
---|
158 | Symbol( namespaceScopes, temporary ),
|
---|
159 | "Long",
|
---|
160 | Type(DEF_LONG)
|
---|
161 | )
|
---|
162 | );
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | //次の行
|
---|
167 | for(;;i++){
|
---|
168 | if(IsCommandDelimitation(source[i])) break;
|
---|
169 | }
|
---|
170 | if(source[i]=='\0') break;
|
---|
171 | }
|
---|
172 | }
|
---|