1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include "sal/config.h"
#include <cerrno>
#include <map>
#include <new>
#include "osl/file.h"
#include "osl/thread.h"
#include "rtl/character.hxx"
#include "rtl/ref.hxx"
#include "rtl/ustrbuf.hxx"
#include "rtl/ustring.hxx"
#include "unoidl/sourceprovider.hxx"
#include "unoidl/unoidl.hxx"
#include "sourceprovider-parser-requires.hxx"
#include "sourceprovider-parser.hxx"
#include "sourceprovider-scanner.hxx"
namespace unoidl {
namespace {
rtl::Reference<Entity> parse(
rtl::Reference<Manager> const & manager, OUString const & name,
OUString const & uri, void const * address, sal_uInt64 size)
{
detail::SourceProviderScannerData data(manager, address, size);
yyscan_t yyscanner;
if (yylex_init_extra(&data, &yyscanner) != 0) {
// Checking errno for the specific EINVAL, ENOMEM documented for
// yylex_init_extra would not work as those values are not defined by
// the C++ Standard:
int e = errno;
throw FileFormatException(
uri, "yylex_init_extra failed with errno " + OUString::number(e));
}
int e = yyparse(yyscanner);
yylex_destroy(yyscanner);
switch (e) {
case 0:
{
std::map<OUString, detail::SourceProviderEntity>::const_iterator i(
data.entities.find(name));
return i == data.entities.end()
? rtl::Reference<Entity>() : i->second.entity;
}
default:
assert(false);
// fall through
case 1:
throw FileFormatException(
uri,
("cannot parse"
+ (data.errorLine == 0
? OUString() : " line " + OUString::number(data.errorLine))
+ (data.parserError.isEmpty()
? OUString()
: (", "
+ OStringToOUString(
data.parserError, osl_getThreadTextEncoding())))
+ (data.errorMessage.isEmpty()
? OUString() : ": \"" + data.errorMessage + "\"")));
case 2:
throw std::bad_alloc();
}
}
class Cursor: public MapCursor {
public:
Cursor() {}
private:
virtual ~Cursor() throw () {}
virtual rtl::Reference<Entity> getNext(OUString *)
{ return rtl::Reference<Entity>(); } //TODO
};
}
SourceProvider::SourceProvider(
rtl::Reference<Manager> const & manager, OUString const & uri):
manager_(manager), uri_(uri.endsWith("/") ? uri : uri + "/")
{}
rtl::Reference<MapCursor> SourceProvider::createRootCursor() const {
return new Cursor;
}
rtl::Reference<Entity> SourceProvider::findEntity(OUString const & name) const {
std::map< OUString, rtl::Reference<Entity> >::iterator ci(
cache_.find(name));
if (ci != cache_.end()) {
return ci->second;
}
// Match name against
// name ::= identifier ("." identifier)*
// identifier ::= upper-blocks | lower-block
// upper-blocks ::= upper ("_"? alnum)*
// lower-block :== lower alnum*
// alnum ::= digit | upper | lower
// digit ::= "0"--"9"
// upper ::= "A"--"Z"
// lower ::= "a"--"z"
OUStringBuffer buf(name);
sal_Int32 start = 0;
sal_Int32 i = 0;
for (; i != name.getLength(); ++i) {
sal_Unicode c = name[i];
if (c == '.') {
assert(i == start || i != 0);
if (i == start || name[i - 1] == '_') {
throw FileFormatException( //TODO
"", "Illegal UNOIDL identifier \"" + name + "\"");
}
buf[i] = '/';
start = i + 1;
} else if (c == '_') {
assert(i == start || i != 0);
if (i == start || name[i - 1] == '_'
|| !rtl::isAsciiUpperCase(name[start]))
{
throw FileFormatException( //TODO
"", "Illegal UNOIDL identifier \"" + name + "\"");
}
} else if (rtl::isAsciiDigit(c)) {
if (i == start) {
throw FileFormatException( //TODO
"", "Illegal UNOIDL identifier \"" + name + "\"");
}
} else if (!rtl::isAsciiAlpha(c)) {
throw FileFormatException( //TODO
"", "Illegal UNOIDL identifier \"" + name + "\"");
}
}
if (i == start) {
throw FileFormatException( //TODO
"", "Illegal UNOIDL identifier \"" + name + "\"");
}
OUString uri(uri_ + buf.makeStringAndClear() + ".idl");
oslFileHandle handle;
oslFileError e = osl_openFile(uri.pData, &handle, osl_File_OpenFlag_Read);
switch (e) {
case osl_File_E_None:
break;
case osl_File_E_NOENT:
cache_.insert(
std::map< OUString, rtl::Reference<Entity> >::value_type(
name, rtl::Reference<Entity>()));
return rtl::Reference<Entity>();
default:
throw FileFormatException(uri, "cannot open: " + OUString::number(e));
}
sal_uInt64 size;
e = osl_getFileSize(handle, &size);
if (e != osl_File_E_None) {
oslFileError e2 = osl_closeFile(handle);
SAL_WARN_IF(
e2 != osl_File_E_None, "unoidl",
"cannot close " << uri << ": " << +e2);
throw FileFormatException(
uri, "cannot get size: " + OUString::number(e));
}
void * address;
e = osl_mapFile(handle, &address, size, 0, osl_File_MapFlag_RandomAccess);
if (e != osl_File_E_None) {
oslFileError e2 = osl_closeFile(handle);
SAL_WARN_IF(
e2 != osl_File_E_None, "unoidl",
"cannot close " << uri << ": " << +e2);
throw FileFormatException(uri, "cannot mmap: " + OUString::number(e));
}
rtl::Reference<Entity> ent;
try {
ent = parse(manager_, name, uri, address, size);
} catch (...) {
e = osl_unmapMappedFile(handle, address, size);
SAL_WARN_IF(e != osl_File_E_None, "unoidl", "cannot unmap: " << +e);
e = osl_closeFile(handle);
SAL_WARN_IF(e != osl_File_E_None, "unoidl", "cannot close: " << +e);
throw;
}
e = osl_unmapMappedFile(handle, address, size);
SAL_WARN_IF(e != osl_File_E_None, "unoidl", "cannot unmap: " << +e);
e = osl_closeFile(handle);
SAL_WARN_IF(e != osl_File_E_None, "unoidl", "cannot close: " << +e);
cache_.insert(
std::map< OUString, rtl::Reference<Entity> >::value_type(name, ent));
return ent;
}
SourceProvider::~SourceProvider() throw () {}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|