[546] | 1 | #include "stdafx.h"
|
---|
[812] | 2 | #include <abdev/ab_common/include/ab_common.h>
|
---|
| 3 | #include "LexicalAnalyzer.h"
|
---|
| 4 | #include "Compiler.h"
|
---|
| 5 | #include "StrOperation.h"
|
---|
[814] | 6 | #include <boost/algorithm/string/predicate.hpp>
|
---|
[546] | 7 |
|
---|
| 8 | using namespace ActiveBasic::Compiler;
|
---|
| 9 |
|
---|
[814] | 10 | void LexicalAnalyzer::AddTypeDef( TypeDefCollection &typeDefs, const NamespaceScopes &namespaceScopes, const boost::iterator_range<char const*> &expression, int nowLine )
|
---|
[546] | 11 | {
|
---|
[814] | 12 | auto name = boost::find<boost::return_begin_found>(expression, '=');
|
---|
[546] | 13 |
|
---|
[814] | 14 | if (boost::end(name) == boost::end(expression))
|
---|
| 15 | {
|
---|
| 16 | compiler.errorMessenger.Output(10, "TypeDef", nowLine);
|
---|
[546] | 17 | return;
|
---|
| 18 | }
|
---|
| 19 |
|
---|
[814] | 20 | const char *pTemp = expression.begin() + boost::size(name) + 1;
|
---|
[546] | 21 |
|
---|
| 22 | //識別文字のエラーチェック(新しい型)
|
---|
[814] | 23 | if (!boost::algorithm::all(name, [](char c) {return IsVariableChar(c, true);}))
|
---|
| 24 | {
|
---|
| 25 | compiler.errorMessenger.Output(10, "TypeDef", nowLine);
|
---|
| 26 | return;
|
---|
[546] | 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 | }
|
---|
[814] | 37 | else
|
---|
| 38 | {
|
---|
| 39 | int i=0;
|
---|
[546] | 40 | while(pTemp[i]=='*') i++;
|
---|
[814] | 41 | if (!boost::algorithm::all(name, [](char c) {return IsVariableChar(c, true);}))
|
---|
| 42 | {
|
---|
| 43 | compiler.errorMessenger.Output(10,"TypeDef",nowLine);
|
---|
| 44 | return;
|
---|
[546] | 45 | }
|
---|
| 46 | }
|
---|
| 47 |
|
---|
[814] | 48 | std::string baseName(pTemp, expression.end());
|
---|
| 49 |
|
---|
[546] | 50 | //識別子が重複している場合はエラーにする
|
---|
[814] | 51 | if (boost::algorithm::equals(name, baseName))
|
---|
| 52 | {
|
---|
[546] | 53 | compiler.errorMessenger.Output(1,NULL,nowLine);
|
---|
| 54 | return;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 |
|
---|
| 58 |
|
---|
| 59 | //////////////////////////
|
---|
| 60 | // TypeDef情報を追加
|
---|
| 61 | //////////////////////////
|
---|
| 62 |
|
---|
| 63 | Type baseType;
|
---|
[814] | 64 | if( !compiler.StringToType( baseName, baseType ) )
|
---|
[546] | 65 | {
|
---|
[814] | 66 | compiler.errorMessenger.Output(3, baseName, nowLine );
|
---|
[546] | 67 | return;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | typeDefs.push_back(
|
---|
| 71 | TypeDef(
|
---|
[814] | 72 | Symbol(namespaceScopes, boost::copy_range<std::string>(name)),
|
---|
| 73 | baseName,
|
---|
[546] | 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 |
|
---|
[668] | 84 | // Imports情報のクリア
|
---|
| 85 | compiler.GetNamespaceSupporter().ClearImportedNamespaces();
|
---|
[546] | 86 |
|
---|
[814] | 87 | for (int i = 0; ; ++i)
|
---|
| 88 | {
|
---|
[546] | 89 | if( source[i] == 1 && source[i+1] == ESC_NAMESPACE ){
|
---|
[814] | 90 | i+=2;
|
---|
| 91 | char const* p = &source[i];
|
---|
| 92 | while (!IsCommandDelimitation(source[i]))
|
---|
| 93 | {
|
---|
| 94 | ++i;
|
---|
[546] | 95 | }
|
---|
[814] | 96 | namespaceScopes.push_back(std::string(p, &source[i]));
|
---|
[546] | 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 ){
|
---|
[814] | 112 | i+=2;
|
---|
| 113 | char const* p = &source[i];
|
---|
| 114 | while (!IsCommandDelimitation(source[i]))
|
---|
| 115 | {
|
---|
| 116 | ++i;
|
---|
[546] | 117 | }
|
---|
[814] | 118 | std::string s(p, &source[i]);
|
---|
| 119 | if (!compiler.GetNamespaceSupporter().ImportsNamespace(s))
|
---|
[546] | 120 | {
|
---|
[814] | 121 | compiler.errorMessenger.Output(64, s.c_str(), i);
|
---|
[546] | 122 | }
|
---|
| 123 |
|
---|
| 124 | continue;
|
---|
| 125 | }
|
---|
[668] | 126 | else if( source[i] == 1 && source[i+1] == ESC_CLEARNAMESPACEIMPORTED )
|
---|
| 127 | {
|
---|
| 128 | // Imports情報のクリア
|
---|
| 129 | compiler.GetNamespaceSupporter().ClearImportedNamespaces();
|
---|
[546] | 130 | continue;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | if( source[i]==1 ){
|
---|
| 134 | char temporary[VN_SIZE];
|
---|
| 135 | if(source[i+1]==ESC_TYPEDEF){
|
---|
[814] | 136 | i+=2;
|
---|
| 137 | char const* p = &source[i];
|
---|
| 138 | while (!IsCommandDelimitation(source[i]))
|
---|
| 139 | {
|
---|
| 140 | ++i;
|
---|
[546] | 141 | }
|
---|
[814] | 142 | AddTypeDef(typeDefs, namespaceScopes, boost::make_iterator_range(p, &source[i]), i);
|
---|
[546] | 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(
|
---|
[637] | 158 | Symbol( namespaceScopes, temporary ),
|
---|
[546] | 159 | "Long",
|
---|
[814] | 160 | Type(DEF_LONG)
|
---|
[546] | 161 | )
|
---|
| 162 | );
|
---|
| 163 | }
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | //次の行
|
---|
| 167 | for(;;i++){
|
---|
| 168 | if(IsCommandDelimitation(source[i])) break;
|
---|
| 169 | }
|
---|
| 170 | if(source[i]=='\0') break;
|
---|
| 171 | }
|
---|
| 172 | }
|
---|