| 1 |
// Copyright (c) 1996 James Clark |
| 2 |
// See the file copying.txt for copying permission. |
| 3 |
|
| 4 |
#include "stylelib.h" |
| 5 |
#include "SchemeParser.h" |
| 6 |
#include "InterpreterMessages.h" |
| 7 |
#include "Pattern.h" |
| 8 |
#include "MacroFlowObj.h" |
| 9 |
#include <OpenSP/macros.h> |
| 10 |
#include <stdlib.h> |
| 11 |
#include "LangObj.h" |
| 12 |
#include "VM.h" |
| 13 |
#include "ELObjMessageArg.h" |
| 14 |
#include "DssslSpecEventHandler.h" |
| 15 |
#include <stdio.h> |
| 16 |
|
| 17 |
#ifdef DSSSL_NAMESPACE |
| 18 |
namespace DSSSL_NAMESPACE { |
| 19 |
#endif |
| 20 |
|
| 21 |
const Char defaultChar = 0xfffd; |
| 22 |
|
| 23 |
SchemeParser::SchemeParser(Interpreter &interp, |
| 24 |
Owner<InputSource> &in) |
| 25 |
: interp_(&interp), |
| 26 |
defMode_(interp.initialProcessingMode()), |
| 27 |
dsssl2_(interp.dsssl2()), |
| 28 |
lang_(0) |
| 29 |
{ |
| 30 |
in_.swap(in); |
| 31 |
{ |
| 32 |
StringC tem(Interpreter::makeStringC("ISO/IEC 10036/RA//Glyphs")); |
| 33 |
afiiPublicId_ = interp_->storePublicId(tem.data(), tem.size(), Location()); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
void SchemeParser::parseStandardChars() |
| 38 |
{ |
| 39 |
Location loc(in_->currentLocation()); |
| 40 |
interp_->requireFeature(Interpreter::charset, loc); |
| 41 |
for (;;) { |
| 42 |
Token tok; |
| 43 |
if (!getToken(allowIdentifier|allowEndOfEntity, tok) |
| 44 |
|| tok == tokenEndOfEntity) |
| 45 |
break; |
| 46 |
|
| 47 |
StringC name(currentToken_); |
| 48 |
|
| 49 |
if (!getToken(allowOtherExpr, tok) || tok != tokenNumber) { |
| 50 |
message(InterpreterMessages::badDeclaration); |
| 51 |
break; |
| 52 |
} |
| 53 |
|
| 54 |
int i; |
| 55 |
for (i = 0; i < name.size(); i++) |
| 56 |
if (interp_->lexCategory(name[i]) != Interpreter::lexLetter |
| 57 |
&& ((i == 0) || |
| 58 |
(interp_->lexCategory(name[i]) != Interpreter::lexDigit |
| 59 |
&& name[i] != '-' && name[i] != '.'))) |
| 60 |
break; |
| 61 |
if (i < name.size() || name.size() == 1) { |
| 62 |
message(InterpreterMessages::invalidCharName, |
| 63 |
StringMessageArg(name)); |
| 64 |
continue; |
| 65 |
} |
| 66 |
|
| 67 |
for (i = 0; i < currentToken_.size(); i++) |
| 68 |
if (interp_->lexCategory(currentToken_[i]) != Interpreter::lexDigit) |
| 69 |
break; |
| 70 |
|
| 71 |
if (i < currentToken_.size()) { |
| 72 |
message(InterpreterMessages::invalidCharNumber, |
| 73 |
StringMessageArg(currentToken_)); |
| 74 |
continue; |
| 75 |
} |
| 76 |
|
| 77 |
interp_->addStandardChar(name, currentToken_); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
void SchemeParser::parseNameChars() |
| 82 |
{ |
| 83 |
Location loc(in_->currentLocation()); |
| 84 |
interp_->requireFeature(Interpreter::charset, loc); |
| 85 |
for (;;) { |
| 86 |
Token tok; |
| 87 |
if (!getToken(allowIdentifier|allowEndOfEntity, tok) |
| 88 |
|| tok == tokenEndOfEntity) |
| 89 |
break; |
| 90 |
interp_->addNameChar(currentToken_); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
void SchemeParser::parseSeparatorChars() |
| 95 |
{ |
| 96 |
Location loc(in_->currentLocation()); |
| 97 |
interp_->requireFeature(Interpreter::charset, loc); |
| 98 |
for (;;) { |
| 99 |
Token tok; |
| 100 |
if (!getToken(allowIdentifier|allowEndOfEntity, tok) |
| 101 |
|| tok == tokenEndOfEntity) |
| 102 |
break; |
| 103 |
interp_->addSeparatorChar(currentToken_); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
void SchemeParser::parseFeatures() |
| 108 |
{ |
| 109 |
interp_->explicitFeatures(); |
| 110 |
for (;;) { |
| 111 |
Token tok; |
| 112 |
if (!getToken(allowIdentifier|allowEndOfEntity, tok) |
| 113 |
|| tok == tokenEndOfEntity) |
| 114 |
break; |
| 115 |
interp_->declareFeature(currentToken_); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
void SchemeParser::parseGrovePlan() |
| 120 |
{ |
| 121 |
interp_->explicitModules(); |
| 122 |
for (;;) { |
| 123 |
Token tok; |
| 124 |
if (!getToken(allowIdentifier|allowEndOfEntity, tok) |
| 125 |
|| tok == tokenEndOfEntity) |
| 126 |
break; |
| 127 |
interp_->addModule(currentToken_); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
void SchemeParser::parseMapSdataEntity(const StringC &ename, const StringC &etext) |
| 132 |
{ |
| 133 |
Location loc(in_->currentLocation()); |
| 134 |
interp_->requireFeature(Interpreter::charset, loc); |
| 135 |
Token tok; |
| 136 |
if (!getToken(allowIdentifier|allowEndOfEntity, tok) |
| 137 |
|| tok == tokenEndOfEntity) { |
| 138 |
message(InterpreterMessages::badDeclaration); |
| 139 |
return; |
| 140 |
} |
| 141 |
|
| 142 |
interp_->addSdataEntity(ename, etext, currentToken_); |
| 143 |
} |
| 144 |
|
| 145 |
void SchemeParser::parse() |
| 146 |
{ |
| 147 |
bool recovering = 0; |
| 148 |
for (;;) { |
| 149 |
Token tok; |
| 150 |
if (!getToken(recovering ? ~0 : allowOpenParen|allowEndOfEntity, |
| 151 |
tok)) |
| 152 |
recovering = 1; |
| 153 |
else { |
| 154 |
if (tok == tokenEndOfEntity) |
| 155 |
break; |
| 156 |
if (tok != tokenOpenParen |
| 157 |
|| !getToken(recovering ? ~0 : unsigned(allowIdentifier), tok) |
| 158 |
|| tok != tokenIdentifier) |
| 159 |
recovering = 1; |
| 160 |
else { |
| 161 |
const Identifier *ident = lookup(currentToken_); |
| 162 |
Identifier::SyntacticKey key; |
| 163 |
if (!ident->syntacticKey(key)) { |
| 164 |
if (!recovering) |
| 165 |
message(InterpreterMessages::unknownTopLevelForm, |
| 166 |
StringMessageArg(currentToken_)); |
| 167 |
recovering = 1; |
| 168 |
} |
| 169 |
else { |
| 170 |
switch (key) { |
| 171 |
case Identifier::keyDefine: |
| 172 |
recovering = !doDefine(); |
| 173 |
break; |
| 174 |
case Identifier::keyDefineUnit: |
| 175 |
recovering = !doDefineUnit(); |
| 176 |
break; |
| 177 |
case Identifier::keyDefault: |
| 178 |
recovering = !doDefault(); |
| 179 |
break; |
| 180 |
case Identifier::keyQuery: |
| 181 |
recovering = !doQuery(); |
| 182 |
break; |
| 183 |
case Identifier::keyElement: |
| 184 |
recovering = !doElement(); |
| 185 |
break; |
| 186 |
case Identifier::keyOrElement: |
| 187 |
recovering = !doOrElement(); |
| 188 |
break; |
| 189 |
case Identifier::keyRoot: |
| 190 |
recovering = !doRoot(); |
| 191 |
break; |
| 192 |
case Identifier::keyId: |
| 193 |
recovering = !doId(); |
| 194 |
break; |
| 195 |
case Identifier::keyMode: |
| 196 |
recovering = !doMode(); |
| 197 |
break; |
| 198 |
case Identifier::keyDeclareInitialValue: |
| 199 |
recovering = !doDeclareInitialValue(); |
| 200 |
break; |
| 201 |
case Identifier::keyDeclareCharacteristic: |
| 202 |
recovering = !doDeclareCharacteristic(); |
| 203 |
break; |
| 204 |
case Identifier::keyDeclareFlowObjectClass: |
| 205 |
recovering = !doDeclareFlowObjectClass(); |
| 206 |
break; |
| 207 |
case Identifier::keyDeclareClassAttribute: |
| 208 |
recovering = !doDeclareClassAttribute(); |
| 209 |
break; |
| 210 |
case Identifier::keyDeclareIdAttribute: |
| 211 |
recovering = !doDeclareIdAttribute(); |
| 212 |
break; |
| 213 |
case Identifier::keyDeclareFlowObjectMacro: |
| 214 |
recovering = !doDeclareFlowObjectMacro(); |
| 215 |
break; |
| 216 |
case Identifier::keyDeclareDefaultLanguage: |
| 217 |
recovering = !doDeclareDefaultLanguage(); |
| 218 |
break; |
| 219 |
case Identifier::keyDefineLanguage: |
| 220 |
recovering = !doDefineLanguage(); |
| 221 |
break; |
| 222 |
case Identifier::keyDeclareCharProperty: |
| 223 |
recovering = !doDeclareCharProperty(); |
| 224 |
break; |
| 225 |
case Identifier::keyAddCharProperties: |
| 226 |
recovering = !doAddCharProperties(); |
| 227 |
break; |
| 228 |
case Identifier::keyDeclareCharCharacteristicAndProperty: |
| 229 |
recovering = !doDeclareCharCharacteristicAndProperty(); |
| 230 |
break; |
| 231 |
case Identifier::keyDeclareReferenceValueType: |
| 232 |
case Identifier::keyDefinePageModel: |
| 233 |
case Identifier::keyDefineColumnSetModel: |
| 234 |
case Identifier::keyDefineTransliterationMap: |
| 235 |
recovering = !skipForm(); |
| 236 |
break; |
| 237 |
case Identifier::keyArrow: |
| 238 |
recovering = !doAssociation(); |
| 239 |
break; |
| 240 |
default: |
| 241 |
if (!recovering) |
| 242 |
message(InterpreterMessages::unknownTopLevelForm, |
| 243 |
StringMessageArg(currentToken_)); |
| 244 |
recovering = 1; |
| 245 |
break; |
| 246 |
} |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
#if 0 |
| 252 |
NamedTableIter<Identifier> iter(identTable_); |
| 253 |
for (;;) { |
| 254 |
Identifier *ident = iter.next(); |
| 255 |
if (!ident) |
| 256 |
break; |
| 257 |
Location loc; |
| 258 |
unsigned part; |
| 259 |
if (ident->defined(part, loc)) { |
| 260 |
ELObj *obj = ident->computeValue(1, *this); |
| 261 |
if (!isError(obj)) { |
| 262 |
*os_ << ident->name() << "="; |
| 263 |
obj->print(*this, *os_); |
| 264 |
*os_ << OutputCharStream::newline; |
| 265 |
os_->flush(); |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
#endif |
| 270 |
} |
| 271 |
|
| 272 |
bool SchemeParser::parseExpression(Owner<Expression> &expr) |
| 273 |
{ |
| 274 |
Identifier::SyntacticKey key; |
| 275 |
Token tok; |
| 276 |
if (!parseExpression(0, expr, key, tok)) |
| 277 |
return 0; |
| 278 |
getToken(allowEndOfEntity, tok); |
| 279 |
return 1; |
| 280 |
} |
| 281 |
|
| 282 |
bool SchemeParser::doMode() |
| 283 |
{ |
| 284 |
Location loc(in_->currentLocation()); |
| 285 |
if (!interp_->style()) { |
| 286 |
setNextLocation(loc); |
| 287 |
message(InterpreterMessages::styleLanguage); |
| 288 |
return 0; |
| 289 |
} |
| 290 |
Token tok; |
| 291 |
if (!getToken(allowIdentifier, tok)) |
| 292 |
return 0; |
| 293 |
defMode_ = lookupProcessingMode(currentToken_); |
| 294 |
defMode_->setDefined(); |
| 295 |
for (;;) { |
| 296 |
if (!getToken(allowOpenParen|allowCloseParen, tok)) |
| 297 |
return 0; |
| 298 |
if (tok == tokenCloseParen) |
| 299 |
break; |
| 300 |
if (!getToken(allowIdentifier, tok)) |
| 301 |
return 0; |
| 302 |
const Identifier *ident = lookup(currentToken_); |
| 303 |
Identifier::SyntacticKey key; |
| 304 |
if (!ident->syntacticKey(key)) { |
| 305 |
message(InterpreterMessages::badModeForm, |
| 306 |
StringMessageArg(currentToken_)); |
| 307 |
return 0; |
| 308 |
} |
| 309 |
else { |
| 310 |
switch (key) { |
| 311 |
case Identifier::keyDefault: |
| 312 |
if (!doDefault()) |
| 313 |
return 0; |
| 314 |
break; |
| 315 |
case Identifier::keyQuery: |
| 316 |
if (!doQuery()) |
| 317 |
return 0; |
| 318 |
break; |
| 319 |
case Identifier::keyElement: |
| 320 |
if (!doElement()) |
| 321 |
return 0; |
| 322 |
break; |
| 323 |
case Identifier::keyOrElement: |
| 324 |
if (!doOrElement()) |
| 325 |
return 0; |
| 326 |
break; |
| 327 |
case Identifier::keyRoot: |
| 328 |
if (!doRoot()) |
| 329 |
return 0; |
| 330 |
break; |
| 331 |
case Identifier::keyId: |
| 332 |
if (!doId()) |
| 333 |
return 0; |
| 334 |
break; |
| 335 |
default: |
| 336 |
message(InterpreterMessages::badModeForm, |
| 337 |
StringMessageArg(currentToken_)); |
| 338 |
return 0; |
| 339 |
} |
| 340 |
} |
| 341 |
} |
| 342 |
defMode_ = interp_->initialProcessingMode(); |
| 343 |
return 1; |
| 344 |
} |
| 345 |
|
| 346 |
bool SchemeParser::doQuery() |
| 347 |
{ |
| 348 |
Location loc(in_->currentLocation()); |
| 349 |
Owner<Expression> query, expr, priority; |
| 350 |
Token tok; |
| 351 |
Identifier::SyntacticKey key; |
| 352 |
if (!parseExpression(0, query, key, tok)) |
| 353 |
return 0; |
| 354 |
|
| 355 |
ProcessingMode::RuleType ruleType; |
| 356 |
if (!parseRuleBody(expr, priority, ruleType, 1)) |
| 357 |
return 0; |
| 358 |
|
| 359 |
defMode_->addQueryRule(query, expr, priority, ruleType, loc, *interp_); |
| 360 |
return 1; |
| 361 |
} |
| 362 |
|
| 363 |
bool SchemeParser::doElement() |
| 364 |
{ |
| 365 |
Location loc(in_->currentLocation()); |
| 366 |
if (!interp_->style()) { |
| 367 |
setNextLocation(loc); |
| 368 |
message(InterpreterMessages::styleLanguage); |
| 369 |
return 0; |
| 370 |
} |
| 371 |
Token tok; |
| 372 |
ELObj *obj; |
| 373 |
if (!parseDatum(0, obj, loc, tok)) |
| 374 |
return 0; |
| 375 |
NCVector<Pattern> patterns(1); |
| 376 |
Owner<Expression> expr; |
| 377 |
ProcessingMode::RuleType ruleType; |
| 378 |
if (interp_->convertToPattern(obj, loc, patterns[0])) { |
| 379 |
if (!parseRuleBody(expr, ruleType)) |
| 380 |
return 0; |
| 381 |
defMode_->addElementRule(patterns, expr, ruleType, loc, *interp_); |
| 382 |
} |
| 383 |
else if (!parseRuleBody(expr, ruleType)) |
| 384 |
return 0; |
| 385 |
return 1; |
| 386 |
} |
| 387 |
|
| 388 |
bool SchemeParser::doOrElement() |
| 389 |
{ |
| 390 |
Location loc(in_->currentLocation()); |
| 391 |
if (!interp_->style()) { |
| 392 |
setNextLocation(loc); |
| 393 |
message(InterpreterMessages::styleLanguage); |
| 394 |
return 0; |
| 395 |
} |
| 396 |
Token tok; |
| 397 |
if (!getToken(allowOpenParen, tok)) |
| 398 |
return 0; |
| 399 |
NCVector<Pattern> patterns; |
| 400 |
unsigned allowed = 0; |
| 401 |
bool ok = 1; |
| 402 |
for (;;) { |
| 403 |
ELObj *obj; |
| 404 |
if (!parseDatum(allowed, obj, loc, tok)) |
| 405 |
return 0; |
| 406 |
if (!obj) |
| 407 |
break; |
| 408 |
allowed = allowCloseParen; |
| 409 |
if (ok) { |
| 410 |
patterns.resize(patterns.size() + 1); |
| 411 |
if (!interp_->convertToPattern(obj, loc, patterns.back())) |
| 412 |
ok = 0; |
| 413 |
} |
| 414 |
} |
| 415 |
ProcessingMode::RuleType ruleType; |
| 416 |
Owner<Expression> expr; |
| 417 |
if (!parseRuleBody(expr, ruleType)) |
| 418 |
return 0; |
| 419 |
if (ok) |
| 420 |
defMode_->addElementRule(patterns, expr, ruleType, loc, *interp_); |
| 421 |
return 1; |
| 422 |
} |
| 423 |
|
| 424 |
bool SchemeParser::doId() |
| 425 |
{ |
| 426 |
Location loc(in_->currentLocation()); |
| 427 |
if (!interp_->style()) { |
| 428 |
setNextLocation(loc); |
| 429 |
message(InterpreterMessages::styleLanguage); |
| 430 |
return 0; |
| 431 |
} |
| 432 |
Token tok; |
| 433 |
if (!getToken(allowString|allowIdentifier, tok)) |
| 434 |
return 0; |
| 435 |
StringC id(currentToken_); |
| 436 |
Owner<Expression> expr; |
| 437 |
ProcessingMode::RuleType ruleType; |
| 438 |
if (!parseRuleBody(expr, ruleType)) |
| 439 |
return 0; |
| 440 |
IList<Pattern::Element> list; |
| 441 |
Pattern::Element *elem = new Pattern::Element(StringC()); |
| 442 |
list.insert(elem); |
| 443 |
elem->addQualifier(new Pattern::IdQualifier(id)); |
| 444 |
Pattern pattern(list); |
| 445 |
NCVector<Pattern> patterns(1); |
| 446 |
patterns[0].swap(pattern); |
| 447 |
defMode_->addElementRule(patterns, expr, ruleType, loc, *interp_); |
| 448 |
return 1; |
| 449 |
} |
| 450 |
|
| 451 |
bool SchemeParser::doDefault() |
| 452 |
{ |
| 453 |
Location loc(in_->currentLocation()); |
| 454 |
if (!interp_->style()) { |
| 455 |
setNextLocation(loc); |
| 456 |
message(InterpreterMessages::styleLanguage); |
| 457 |
return 0; |
| 458 |
} |
| 459 |
Owner<Expression> expr; |
| 460 |
ProcessingMode::RuleType ruleType; |
| 461 |
if (!parseRuleBody(expr, ruleType)) |
| 462 |
return 0; |
| 463 |
IList<Pattern::Element> list; |
| 464 |
list.insert(new Pattern::Element(StringC())); |
| 465 |
Pattern pattern(list); |
| 466 |
NCVector<Pattern> patterns(1); |
| 467 |
pattern.swap(patterns[0]); |
| 468 |
defMode_->addElementRule(patterns, expr, ruleType, loc, *interp_); |
| 469 |
return 1; |
| 470 |
} |
| 471 |
|
| 472 |
bool SchemeParser::doRoot() |
| 473 |
{ |
| 474 |
Location loc(in_->currentLocation()); |
| 475 |
if (!interp_->style()) { |
| 476 |
setNextLocation(loc); |
| 477 |
message(InterpreterMessages::styleLanguage); |
| 478 |
return 0; |
| 479 |
} |
| 480 |
Owner<Expression> expr; |
| 481 |
ProcessingMode::RuleType ruleType; |
| 482 |
if (!parseRuleBody(expr, ruleType)) |
| 483 |
return 0; |
| 484 |
defMode_->addRootRule(expr, ruleType, loc, *interp_); |
| 485 |
return 1; |
| 486 |
} |
| 487 |
|
| 488 |
bool SchemeParser::parseRuleBody(Owner<Expression> &expr, ProcessingMode::RuleType &ruleType) |
| 489 |
{ |
| 490 |
Owner<Expression> tem; |
| 491 |
return parseRuleBody(expr, tem, ruleType, 0); |
| 492 |
} |
| 493 |
|
| 494 |
bool SchemeParser::parseRuleBody(Owner<Expression> &expr, Owner<Expression> &priority, ProcessingMode::RuleType &ruleType, bool query) |
| 495 |
{ |
| 496 |
Token tok; |
| 497 |
Identifier::SyntacticKey key; |
| 498 |
if (!parseExpression(0, expr, key, tok)) |
| 499 |
return 0; |
| 500 |
const Identifier *k = dsssl2() ? expr->keyword() : 0; |
| 501 |
if (k) { |
| 502 |
// style rule |
| 503 |
Vector<const Identifier *> keys; |
| 504 |
NCVector<Owner<Expression> > exprs; |
| 505 |
for (;;) { |
| 506 |
keys.push_back(k); |
| 507 |
exprs.resize(exprs.size() + 1); |
| 508 |
if (!parseExpression(0, exprs.back(), key, tok)) |
| 509 |
return 0; |
| 510 |
if (!getToken(allowKeyword|allowCloseParen, tok)) |
| 511 |
return 0; |
| 512 |
if (tok == tokenCloseParen) |
| 513 |
break; |
| 514 |
k = lookup(currentToken_); |
| 515 |
} |
| 516 |
expr = new StyleExpression(keys, exprs, expr->location()); |
| 517 |
ruleType = ProcessingMode::styleRule; |
| 518 |
} |
| 519 |
else { |
| 520 |
ruleType = ProcessingMode::constructionRule; |
| 521 |
if (query) { |
| 522 |
if (!parseExpression(allowCloseParen, priority, key, tok)) |
| 523 |
return 0; |
| 524 |
if (tok == tokenCloseParen) |
| 525 |
return 1; |
| 526 |
} |
| 527 |
if (!getToken(allowCloseParen, tok)) |
| 528 |
return 0; |
| 529 |
} |
| 530 |
return 1; |
| 531 |
} |
| 532 |
|
| 533 |
bool SchemeParser::doDeclareInitialValue() |
| 534 |
{ |
| 535 |
Location loc(in_->currentLocation()); |
| 536 |
if (!interp_->style()) { |
| 537 |
setNextLocation(loc); |
| 538 |
message(InterpreterMessages::styleLanguage); |
| 539 |
return 0; |
| 540 |
} |
| 541 |
Token tok; |
| 542 |
if (!getToken(allowIdentifier, tok)) |
| 543 |
return 0; |
| 544 |
Identifier *ident = lookup(currentToken_); |
| 545 |
if (ident->inheritedC().isNull()) |
| 546 |
message(InterpreterMessages::notABuiltinInheritedC, |
| 547 |
StringMessageArg(ident->name())); |
| 548 |
Owner<Expression> expr; |
| 549 |
Identifier::SyntacticKey key; |
| 550 |
if (!parseExpression(0, expr, key, tok)) |
| 551 |
return 0; |
| 552 |
if (!getToken(allowCloseParen, tok)) |
| 553 |
return 0; |
| 554 |
if (ident->inheritedC().isNull()) |
| 555 |
return 1; |
| 556 |
interp_->installInitialValue(ident, expr); |
| 557 |
return 1; |
| 558 |
} |
| 559 |
|
| 560 |
bool SchemeParser::doDeclareCharCharacteristicAndProperty() |
| 561 |
{ |
| 562 |
Location loc(in_->currentLocation()); |
| 563 |
if (!interp_->style()) { |
| 564 |
setNextLocation(loc); |
| 565 |
message(InterpreterMessages::styleLanguage); |
| 566 |
return 0; |
| 567 |
} |
| 568 |
Token tok; |
| 569 |
if (!getToken(allowIdentifier, tok)) |
| 570 |
return 0; |
| 571 |
Identifier *ident = lookup(currentToken_); |
| 572 |
CharProp *cp = interp_->lookupCharProperty(currentToken_); |
| 573 |
if (!getToken(allowString|(dsssl2() ? unsigned(allowFalse) : 0), tok)) |
| 574 |
return 0; |
| 575 |
StringC pubid; |
| 576 |
if (tok == tokenString) |
| 577 |
pubid = currentToken_; |
| 578 |
Owner<Expression> expr; |
| 579 |
Identifier::SyntacticKey key; |
| 580 |
if (!parseExpression(0, expr, key, tok)) |
| 581 |
return 0; |
| 582 |
if (!getToken(allowCloseParen, tok)) |
| 583 |
return 0; |
| 584 |
Location defLoc; |
| 585 |
unsigned defPart; |
| 586 |
if (ident->inheritedCDefined(defPart, defLoc)) { |
| 587 |
setNextLocation(loc); |
| 588 |
message(InterpreterMessages::duplicateCharacteristic, |
| 589 |
StringMessageArg(ident->name()), defLoc); |
| 590 |
} |
| 591 |
else if (ident->charNICDefined(defPart, defLoc) |
| 592 |
&& defPart <= interp_->currentPartIndex()) { |
| 593 |
if (defPart == interp_->currentPartIndex()) { |
| 594 |
setNextLocation(loc); |
| 595 |
message(InterpreterMessages::duplicateCharacteristic, |
| 596 |
StringMessageArg(ident->name()), defLoc); |
| 597 |
} |
| 598 |
} |
| 599 |
else if (cp->declared (defPart, defLoc) |
| 600 |
&& defPart <= interp_->currentPartIndex()) { |
| 601 |
setNextLocation(loc); |
| 602 |
message(InterpreterMessages::duplicateCharPropertyDecl, |
| 603 |
StringMessageArg(cp->name()), defLoc); |
| 604 |
} |
| 605 |
else { |
| 606 |
cp->declare(expr, interp_->currentPartIndex(), loc, |
| 607 |
interp_->installExtensionCharNIC(ident, pubid, loc)); |
| 608 |
} |
| 609 |
return 1; |
| 610 |
} |
| 611 |
|
| 612 |
bool SchemeParser::doDeclareCharacteristic() |
| 613 |
{ |
| 614 |
Location loc(in_->currentLocation()); |
| 615 |
if (!interp_->style()) { |
| 616 |
setNextLocation(loc); |
| 617 |
message(InterpreterMessages::styleLanguage); |
| 618 |
return 0; |
| 619 |
} |
| 620 |
Token tok; |
| 621 |
if (!getToken(allowIdentifier, tok)) |
| 622 |
return 0; |
| 623 |
Identifier *ident = lookup(currentToken_); |
| 624 |
if (!getToken(allowString|(dsssl2() ? unsigned(allowFalse) : 0), tok)) |
| 625 |
return 0; |
| 626 |
StringC pubid; |
| 627 |
if (tok == tokenString) |
| 628 |
pubid = currentToken_; |
| 629 |
Owner<Expression> expr; |
| 630 |
Identifier::SyntacticKey key; |
| 631 |
if (!parseExpression(0, expr, key, tok)) |
| 632 |
return 0; |
| 633 |
if (!getToken(allowCloseParen, tok)) |
| 634 |
return 0; |
| 635 |
Location defLoc; |
| 636 |
unsigned defPart; |
| 637 |
if (ident->charNICDefined(defPart, defLoc)) { |
| 638 |
setNextLocation(loc); |
| 639 |
message(InterpreterMessages::duplicateCharacteristic, |
| 640 |
StringMessageArg(ident->name()), defLoc); |
| 641 |
} |
| 642 |
else if (ident->inheritedCDefined(defPart, defLoc) |
| 643 |
&& defPart <= interp_->currentPartIndex()) { |
| 644 |
if (defPart == interp_->currentPartIndex()) { |
| 645 |
setNextLocation(loc); |
| 646 |
message(InterpreterMessages::duplicateCharacteristic, |
| 647 |
StringMessageArg(ident->name()), defLoc); |
| 648 |
} |
| 649 |
} |
| 650 |
else { |
| 651 |
interp_->installExtensionInheritedC(ident, pubid, loc); |
| 652 |
interp_->installInitialValue(ident, expr); |
| 653 |
} |
| 654 |
return 1; |
| 655 |
} |
| 656 |
|
| 657 |
bool SchemeParser::doDeclareFlowObjectClass() |
| 658 |
{ |
| 659 |
Location loc(in_->currentLocation()); |
| 660 |
if (!interp_->style()) { |
| 661 |
setNextLocation(loc); |
| 662 |
message(InterpreterMessages::styleLanguage); |
| 663 |
return 0; |
| 664 |
} |
| 665 |
Token tok; |
| 666 |
if (!getToken(allowIdentifier, tok)) |
| 667 |
return 0; |
| 668 |
Identifier *ident = lookup(currentToken_); |
| 669 |
if (!getToken(allowString, tok)) |
| 670 |
return 0; |
| 671 |
Location defLoc; |
| 672 |
unsigned defPart; |
| 673 |
if (ident->inheritedCDefined(defPart, defLoc) |
| 674 |
&& defPart <= interp_->currentPartIndex()) { |
| 675 |
if (defPart == interp_->currentPartIndex()) { |
| 676 |
setNextLocation(loc); |
| 677 |
message(InterpreterMessages::duplicateFlowObjectClass, |
| 678 |
StringMessageArg(ident->name()), defLoc); |
| 679 |
} |
| 680 |
} |
| 681 |
else |
| 682 |
interp_->installExtensionFlowObjectClass(ident, currentToken_, loc); |
| 683 |
if (!getToken(allowCloseParen, tok)) |
| 684 |
return 0; |
| 685 |
return 1; |
| 686 |
} |
| 687 |
|
| 688 |
bool SchemeParser::doDeclareFlowObjectMacro() |
| 689 |
{ |
| 690 |
Location loc(in_->currentLocation()); |
| 691 |
if (!interp_->style()) { |
| 692 |
setNextLocation(loc); |
| 693 |
message(InterpreterMessages::styleLanguage); |
| 694 |
return 0; |
| 695 |
} |
| 696 |
Token tok; |
| 697 |
if (!getToken(allowIdentifier, tok)) |
| 698 |
return 0; |
| 699 |
Identifier *ident = lookup(currentToken_); |
| 700 |
if (ident->flowObj()) |
| 701 |
// FIXME report an error if same part |
| 702 |
; |
| 703 |
if (!getToken(allowOpenParen, tok)) |
| 704 |
return 0; |
| 705 |
Vector<const Identifier *> nics; |
| 706 |
NCVector<Owner<Expression> > inits; |
| 707 |
const Identifier *contentsId = 0; |
| 708 |
unsigned allowed = (allowOpenParen|allowCloseParen|allowIdentifier|allowHashContents); |
| 709 |
for (;;) { |
| 710 |
if (!getToken(allowed, tok)) |
| 711 |
return 0; |
| 712 |
if (tok == tokenCloseParen) |
| 713 |
break; |
| 714 |
switch (tok) { |
| 715 |
case tokenHashContents: |
| 716 |
if (!getToken(allowIdentifier, tok)) |
| 717 |
return 0; |
| 718 |
contentsId = lookup(currentToken_); |
| 719 |
allowed = allowCloseParen; |
| 720 |
break; |
| 721 |
case tokenIdentifier: |
| 722 |
nics.push_back(lookup(currentToken_)); |
| 723 |
break; |
| 724 |
case tokenOpenParen: |
| 725 |
{ |
| 726 |
if (!getToken(allowIdentifier, tok)) |
| 727 |
return 0; |
| 728 |
nics.push_back(lookup(currentToken_)); |
| 729 |
inits.resize(nics.size()); |
| 730 |
Identifier::SyntacticKey key; |
| 731 |
if (!parseExpression(0, inits.back(), key, tok)) |
| 732 |
return 0; |
| 733 |
if (!getToken(allowCloseParen, tok)) |
| 734 |
return 0; |
| 735 |
} |
| 736 |
break; |
| 737 |
default: |
| 738 |
CANNOT_HAPPEN(); |
| 739 |
} |
| 740 |
} |
| 741 |
// We could allow sequence which is appended together here. |
| 742 |
Owner<Expression> body; |
| 743 |
Identifier::SyntacticKey key; |
| 744 |
if (!parseExpression(0, body, key, tok)) |
| 745 |
return 0; |
| 746 |
if (!getToken(allowCloseParen, tok)) |
| 747 |
return 0; |
| 748 |
Location defLoc; |
| 749 |
unsigned defPart; |
| 750 |
if (ident->inheritedCDefined(defPart, defLoc) |
| 751 |
&& defPart <= interp_->currentPartIndex()) { |
| 752 |
if (defPart == interp_->currentPartIndex()) { |
| 753 |
setNextLocation(loc); |
| 754 |
message(InterpreterMessages::duplicateFlowObjectClass, |
| 755 |
StringMessageArg(ident->name()), defLoc); |
| 756 |
} |
| 757 |
} |
| 758 |
else { |
| 759 |
MacroFlowObj *flowObj |
| 760 |
= new (*interp_) MacroFlowObj(nics, inits, contentsId, body); |
| 761 |
interp_->makePermanent(flowObj); |
| 762 |
ident->setFlowObj(flowObj); |
| 763 |
} |
| 764 |
return 1; |
| 765 |
} |
| 766 |
|
| 767 |
bool SchemeParser::doDeclareClassAttribute() |
| 768 |
{ |
| 769 |
Token tok; |
| 770 |
if (!getToken(allowString|allowIdentifier, tok)) |
| 771 |
return 0; |
| 772 |
interp_->addClassAttributeName(currentToken_); |
| 773 |
if (!getToken(allowCloseParen, tok)) |
| 774 |
return 0; |
| 775 |
return 1; |
| 776 |
} |
| 777 |
|
| 778 |
bool SchemeParser::doDeclareIdAttribute() |
| 779 |
{ |
| 780 |
Token tok; |
| 781 |
if (!getToken(allowString|allowIdentifier, tok)) |
| 782 |
return 0; |
| 783 |
interp_->addIdAttributeName(currentToken_); |
| 784 |
if (!getToken(allowCloseParen, tok)) |
| 785 |
return 0; |
| 786 |
return 1; |
| 787 |
} |
| 788 |
|
| 789 |
bool SchemeParser::doDefine() |
| 790 |
{ |
| 791 |
Location loc(in_->currentLocation()); |
| 792 |
bool internal = 0; |
| 793 |
Interpreter::Feature feature = Interpreter::noFeature; |
| 794 |
if (interp_->currentPartIndex() == unsigned(-1)) { |
| 795 |
Token tok; |
| 796 |
if (!getToken(allowOpenParen|allowIdentifier|allowKeyword, tok)) |
| 797 |
return 0; |
| 798 |
if (tok == tokenKeyword) { |
| 799 |
if (currentToken_ == interp_->makeStringC("internal")) |
| 800 |
internal = 1; |
| 801 |
else if (currentToken_ == interp_->makeStringC("feature")) { |
| 802 |
if (!getToken(allowIdentifier, tok) |
| 803 |
|| !interp_->convertFeature(currentToken_, feature)) |
| 804 |
return 0; |
| 805 |
} |
| 806 |
else |
| 807 |
return 0; |
| 808 |
} |
| 809 |
else |
| 810 |
in_->ungetToken(); |
| 811 |
} |
| 812 |
Identifier *ident = 0; |
| 813 |
Owner<Expression> expr; |
| 814 |
if (!parseDefine(ident, expr)) |
| 815 |
return 0; |
| 816 |
Location defLoc; |
| 817 |
unsigned defPart; |
| 818 |
if (ident->defined(defPart, defLoc) |
| 819 |
&& defPart <= interp_->currentPartIndex()) { |
| 820 |
if (defPart == interp_->currentPartIndex()) |
| 821 |
message(InterpreterMessages::duplicateDefinition, |
| 822 |
StringMessageArg(ident->name()), |
| 823 |
defLoc); |
| 824 |
} |
| 825 |
else if (internal) |
| 826 |
ident->setBuiltinDefinition(expr, interp_->currentPartIndex(), loc); |
| 827 |
else |
| 828 |
ident->setDefinition(expr, interp_->currentPartIndex(), loc); |
| 829 |
ident->setFeature(feature); |
| 830 |
return 1; |
| 831 |
} |
| 832 |
|
| 833 |
bool SchemeParser::parseDefine(Identifier *&ident, Owner<Expression> &expr) |
| 834 |
{ |
| 835 |
Location loc(in_->currentLocation()); |
| 836 |
Token tok; |
| 837 |
Vector<const Identifier *> formals; |
| 838 |
bool isProcedure; |
| 839 |
if (!getToken(allowOpenParen|allowIdentifier, tok)) |
| 840 |
return 0; |
| 841 |
if (tok == tokenOpenParen) { |
| 842 |
interp_->requireFeature(Interpreter::expression, loc); |
| 843 |
if (!getToken(allowIdentifier, tok)) |
| 844 |
return 0; |
| 845 |
isProcedure = 1; |
| 846 |
} |
| 847 |
else |
| 848 |
isProcedure = 0; |
| 849 |
ident = lookup(currentToken_); |
| 850 |
Identifier::SyntacticKey key; |
| 851 |
if (ident->syntacticKey(key) && key <= int(Identifier::lastSyntacticKey)) |
| 852 |
message(InterpreterMessages::syntacticKeywordAsVariable, |
| 853 |
StringMessageArg(currentToken_)); |
| 854 |
NCVector<Owner<Expression> > inits; |
| 855 |
int nOptional; |
| 856 |
int nKey; |
| 857 |
bool hasRest; |
| 858 |
if (isProcedure && !parseFormals(formals, inits, nOptional, hasRest, nKey)) |
| 859 |
return 0; |
| 860 |
if (isProcedure) { |
| 861 |
if (!parseBody(expr)) |
| 862 |
return 0; |
| 863 |
} |
| 864 |
else { |
| 865 |
if (!parseExpression(0, expr, key, tok)) |
| 866 |
return 0; |
| 867 |
if (!getToken(allowCloseParen, tok)) |
| 868 |
return 0; |
| 869 |
} |
| 870 |
if (isProcedure) |
| 871 |
expr = new LambdaExpression(formals, inits, nOptional, hasRest, nKey, |
| 872 |
expr, loc); |
| 873 |
return 1; |
| 874 |
} |
| 875 |
|
| 876 |
bool SchemeParser::doDefineUnit() |
| 877 |
{ |
| 878 |
Location loc(in_->currentLocation()); |
| 879 |
Token tok; |
| 880 |
if (!getToken(allowIdentifier, tok)) |
| 881 |
return 0; |
| 882 |
int i; |
| 883 |
for (i = 0; i < currentToken_.size(); i++) |
| 884 |
if (interp_->lexCategory(currentToken_[i]) != Interpreter::lexLetter) |
| 885 |
break; |
| 886 |
if ((i < currentToken_.size()) |
| 887 |
|| ((currentToken_.size() == 1) && (currentToken_[0] =='e'))) { |
| 888 |
message(InterpreterMessages::invalidUnitName, |
| 889 |
StringMessageArg(currentToken_)); |
| 890 |
return 0; |
| 891 |
} |
| 892 |
|
| 893 |
Unit *unit = interp_->lookupUnit(currentToken_); |
| 894 |
Owner<Expression> expr; |
| 895 |
Identifier::SyntacticKey key; |
| 896 |
if (!parseExpression(0, expr, key, tok)) |
| 897 |
return 0; |
| 898 |
if (!getToken(allowCloseParen, tok)) |
| 899 |
return 0; |
| 900 |
Location defLoc; |
| 901 |
unsigned defPart; |
| 902 |
if (unit->defined(defPart, defLoc) |
| 903 |
&& defPart <= interp_->currentPartIndex()) { |
| 904 |
if (defPart == interp_->currentPartIndex()) |
| 905 |
message(InterpreterMessages::duplicateUnitDefinition, |
| 906 |
StringMessageArg(unit->name()), |
| 907 |
defLoc); |
| 908 |
} |
| 909 |
else |
| 910 |
unit->setDefinition(expr, interp_->currentPartIndex(), loc); |
| 911 |
return 1; |
| 912 |
} |
| 913 |
|
| 914 |
bool SchemeParser::doAssociation() |
| 915 |
{ |
| 916 |
Location loc(in_->currentLocation()); |
| 917 |
if (interp_->style()) { |
| 918 |
interp_->setNextLocation(loc); |
| 919 |
interp_->message(InterpreterMessages::transformationLanguage); |
| 920 |
return 0; |
| 921 |
} |
| 922 |
Owner<Expression> qexpr, texpr, pexpr; |
| 923 |
Identifier::SyntacticKey key; |
| 924 |
Token tok; |
| 925 |
if (!parseExpression(0, qexpr, key, tok) |
| 926 |
|| !parseExpression(0, texpr, key, tok) |
| 927 |
|| !parseExpression(allowCloseParen, pexpr, key, tok)) |
| 928 |
return 0; |
| 929 |
if (!pexpr) |
| 930 |
pexpr = new ConstantExpression(interp_->makeInteger(0), in_->currentLocation()); |
| 931 |
else if (!getToken(allowCloseParen, tok)) |
| 932 |
return 0; |
| 933 |
interp_->transformationMode()->addAssociation(qexpr, texpr, pexpr, |
| 934 |
loc, *interp_); |
| 935 |
return 1; |
| 936 |
} |
| 937 |
|
| 938 |
bool SchemeParser::skipForm() |
| 939 |
{ |
| 940 |
static const unsigned allow = (~0 & ~allowEndOfEntity); |
| 941 |
unsigned level = 0; |
| 942 |
for (;;) { |
| 943 |
Token tok; |
| 944 |
if (!getToken(allow, tok)) |
| 945 |
break; |
| 946 |
switch (tok) { |
| 947 |
case tokenOpenParen: |
| 948 |
level++; |
| 949 |
break; |
| 950 |
case tokenCloseParen: |
| 951 |
if (level == 0) |
| 952 |
return 1; |
| 953 |
level--; |
| 954 |
break; |
| 955 |
default: |
| 956 |
break; |
| 957 |
} |
| 958 |
} |
| 959 |
return 0; |
| 960 |
} |
| 961 |
|
| 962 |
bool SchemeParser::parseExpression(unsigned allowed, |
| 963 |
Owner<Expression> &expr, |
| 964 |
Identifier::SyntacticKey &key, |
| 965 |
Token &tok) |
| 966 |
{ |
| 967 |
expr.clear(); |
| 968 |
key = Identifier::notKey; |
| 969 |
ELObj *obj; |
| 970 |
if (!parseSelfEvaluating(allowed, obj, tok)) |
| 971 |
return 0; |
| 972 |
if (obj) { |
| 973 |
interp_->makePermanent(obj); |
| 974 |
expr = new ConstantExpression(obj, in_->currentLocation()); |
| 975 |
return 1; |
| 976 |
} |
| 977 |
switch (tok) { |
| 978 |
case tokenQuote: |
| 979 |
{ |
| 980 |
Location loc; |
| 981 |
if (!parseDatum(0, obj, loc, tok)) |
| 982 |
return 0; |
| 983 |
interp_->makePermanent(obj); |
| 984 |
expr = new ConstantExpression(obj, loc); |
| 985 |
break; |
| 986 |
} |
| 987 |
case tokenQuasiquote: |
| 988 |
{ |
| 989 |
bool spliced; |
| 990 |
return parseQuasiquoteTemplate(0, 0, expr, key, tok, spliced); |
| 991 |
} |
| 992 |
case tokenOpenParen: |
| 993 |
{ |
| 994 |
Location loc(in_->currentLocation()); |
| 995 |
if (!parseExpression(allowExpressionKey, expr, key, tok)) |
| 996 |
return 0; |
| 997 |
if (expr) { |
| 998 |
NCVector<Owner<Expression> > args; |
| 999 |
for (;;) { |
| 1000 |
args.resize(args.size() + 1); |
| 1001 |
if (!parseExpression(allowCloseParen, args.back(), key, tok)) |
| 1002 |
return 0; |
| 1003 |
if (!args.back()) { |
| 1004 |
args.resize(args.size() - 1); |
| 1005 |
break; |
| 1006 |
} |
| 1007 |
} |
| 1008 |
expr = new CallExpression(expr, args, loc); |
| 1009 |
} |
| 1010 |
else { |
| 1011 |
switch (key) { |
| 1012 |
case Identifier::keyQuote: |
| 1013 |
return parseQuote(expr); |
| 1014 |
case Identifier::keyLambda: |
| 1015 |
return parseLambda(expr); |
| 1016 |
case Identifier::keyIf: |
| 1017 |
return parseIf(expr); |
| 1018 |
case Identifier::keyCond: |
| 1019 |
return parseCond(expr); |
| 1020 |
case Identifier::keyAnd: |
| 1021 |
return parseAnd(expr); |
| 1022 |
case Identifier::keyOr: |
| 1023 |
return parseOr(expr); |
| 1024 |
case Identifier::keyCase: |
| 1025 |
return parseCase(expr); |
| 1026 |
case Identifier::keyLet: |
| 1027 |
return parseLet(expr); |
| 1028 |
case Identifier::keyLetStar: |
| 1029 |
return parseLetStar(expr); |
| 1030 |
case Identifier::keyLetrec: |
| 1031 |
return parseLetrec(expr); |
| 1032 |
case Identifier::keyThereExists: |
| 1033 |
return parseSpecialQuery(expr, "node-list-some?"); |
| 1034 |
case Identifier::keyForAll: |
| 1035 |
return parseSpecialQuery(expr, "node-list-every?"); |
| 1036 |
case Identifier::keySelectEach: |
| 1037 |
return parseSpecialQuery(expr, "node-list-filter"); |
| 1038 |
case Identifier::keyUnionForEach: |
| 1039 |
return parseSpecialQuery(expr, "node-list-union-map"); |
| 1040 |
case Identifier::keyMake: |
| 1041 |
return parseMake(expr); |
| 1042 |
case Identifier::keyStyle: |
| 1043 |
return parseStyle(expr); |
| 1044 |
case Identifier::keyWithMode: |
| 1045 |
return parseWithMode(expr); |
| 1046 |
case Identifier::keyQuasiquote: |
| 1047 |
return parseQuasiquote(expr); |
| 1048 |
case Identifier::keySet: |
| 1049 |
return parseSet(expr); |
| 1050 |
case Identifier::keyBegin: |
| 1051 |
return parseBegin(expr); |
| 1052 |
default: |
| 1053 |
CANNOT_HAPPEN(); |
| 1054 |
} |
| 1055 |
} |
| 1056 |
break; |
| 1057 |
} |
| 1058 |
case tokenIdentifier: |
| 1059 |
{ |
| 1060 |
const Identifier *ident = lookup(currentToken_); |
| 1061 |
if (ident->syntacticKey(key) && key <= int(Identifier::lastSyntacticKey)) { |
| 1062 |
switch (key) { |
| 1063 |
case Identifier::keyDefine: |
| 1064 |
if (allowed & allowKeyDefine) |
| 1065 |
return 1; |
| 1066 |
break; |
| 1067 |
case Identifier::keyArrow: |
| 1068 |
if (allowed & allowKeyArrow) |
| 1069 |
return 1; |
| 1070 |
break; |
| 1071 |
case Identifier::keyElse: |
| 1072 |
if (allowed & allowKeyElse) |
| 1073 |
return 1; |
| 1074 |
break; |
| 1075 |
case Identifier::keyUnquote: |
| 1076 |
case Identifier::keyUnquoteSplicing: |
| 1077 |
break; |
| 1078 |
default: |
| 1079 |
if (allowed & allowExpressionKey) |
| 1080 |
return 1; |
| 1081 |
break; |
| 1082 |
} |
| 1083 |
message(InterpreterMessages::syntacticKeywordAsVariable, |
| 1084 |
StringMessageArg(currentToken_)); |
| 1085 |
} |
| 1086 |
expr = new VariableExpression(ident, in_->currentLocation()); |
| 1087 |
} |
| 1088 |
break; |
| 1089 |
default: |
| 1090 |
break; |
| 1091 |
} |
| 1092 |
return 1; |
| 1093 |
} |
| 1094 |
|
| 1095 |
|
| 1096 |
bool SchemeParser::parseQuote(Owner<Expression> &expr) |
| 1097 |
{ |
| 1098 |
Token tok; |
| 1099 |
Location loc; |
| 1100 |
ELObj *obj; |
| 1101 |
if (!parseDatum(0, obj, loc, tok)) |
| 1102 |
return 0; |
| 1103 |
if (!getToken(allowCloseParen, tok)) |
| 1104 |
return 0; |
| 1105 |
interp_->makePermanent(obj); |
| 1106 |
expr = new ConstantExpression(obj, loc); |
| 1107 |
return 1; |
| 1108 |
} |
| 1109 |
|
| 1110 |
bool SchemeParser::parseQuasiquote(Owner<Expression> &expr) |
| 1111 |
{ |
| 1112 |
bool spliced; |
| 1113 |
Token tok; |
| 1114 |
Identifier::SyntacticKey key; |
| 1115 |
if (!parseQuasiquoteTemplate(0, 0, expr, key, tok, spliced)) |
| 1116 |
return 0; |
| 1117 |
return getToken(allowCloseParen, tok); |
| 1118 |
} |
| 1119 |
|
| 1120 |
bool SchemeParser::parseQuasiquoteTemplate(unsigned level, |
| 1121 |
unsigned allowed, |
| 1122 |
Owner<Expression> &expr, |
| 1123 |
Identifier::SyntacticKey &key, |
| 1124 |
Token &tok, |
| 1125 |
bool &spliced) |
| 1126 |
{ |
| 1127 |
Location loc(in_->currentLocation()); |
| 1128 |
interp_->requireFeature(Interpreter::expression, loc); |
| 1129 |
key = Identifier::notKey; |
| 1130 |
spliced = 0; |
| 1131 |
ELObj *obj; |
| 1132 |
if (!parseSelfEvaluating(allowed|allowUnquote|allowVector, obj, tok)) |
| 1133 |
return 0; |
| 1134 |
switch (tok) { |
| 1135 |
case tokenQuasiquote: |
| 1136 |
if (!parseQuasiquoteTemplate(level + 1, 0, expr, key, tok, spliced)) |
| 1137 |
return 0; |
| 1138 |
createQuasiquoteAbbreviation("quasiquote", expr); |
| 1139 |
break; |
| 1140 |
case tokenQuote: |
| 1141 |
if (!parseQuasiquoteTemplate(level, 0, expr, key, tok, spliced)) |
| 1142 |
break; |
| 1143 |
createQuasiquoteAbbreviation("quote", expr); |
| 1144 |
break; |
| 1145 |
case tokenUnquote: |
| 1146 |
case tokenUnquoteSplicing: |
| 1147 |
if (level == 0) { |
| 1148 |
spliced = (tok == tokenUnquoteSplicing); |
| 1149 |
if (!parseExpression(0, expr, key, tok)) |
| 1150 |
return 0; |
| 1151 |
} |
| 1152 |
else { |
| 1153 |
Token tem; |
| 1154 |
if (!parseQuasiquoteTemplate(level - 1, 0, expr, key, tem, spliced)) |
| 1155 |
break; |
| 1156 |
createQuasiquoteAbbreviation(tok == tokenUnquote ? "unquote" : "unquote-splicing", expr); |
| 1157 |
} |
| 1158 |
break; |
| 1159 |
case tokenOpenParen: |
| 1160 |
case tokenVector: |
| 1161 |
{ |
| 1162 |
QuasiquoteExpression::Type type |
| 1163 |
= (tok == tokenVector |
| 1164 |
? QuasiquoteExpression::vectorType |
| 1165 |
: QuasiquoteExpression::listType); |
| 1166 |
Location loc(in_->currentLocation()); |
| 1167 |
NCVector<Owner<Expression> > exprs(1); |
| 1168 |
Vector<PackedBoolean> exprsSpliced; |
| 1169 |
bool temSpliced; |
| 1170 |
if (!parseQuasiquoteTemplate(level, |
| 1171 |
allowCloseParen|allowQuasiquoteKey|allowUnquoteSplicing, |
| 1172 |
exprs[0], key, tok, temSpliced)) |
| 1173 |
return 0; |
| 1174 |
if (!exprs[0]) { |
| 1175 |
switch (key) { |
| 1176 |
case Identifier::keyQuasiquote: |
| 1177 |
if (!parseQuasiquoteTemplate(level + 1, 0, expr, key, tok, spliced)) |
| 1178 |
return 0; |
| 1179 |
createQuasiquoteAbbreviation("quasiquotation", expr); |
| 1180 |
break; |
| 1181 |
case Identifier::keyUnquoteSplicing: |
| 1182 |
spliced = 1; |
| 1183 |
// fall through |
| 1184 |
case Identifier::keyUnquote: |
| 1185 |
if (level == 0) { |
| 1186 |
if (!parseExpression(0, expr, key, tok)) |
| 1187 |
return 0; |
| 1188 |
} |
| 1189 |
else { |
| 1190 |
if (!parseQuasiquoteTemplate(level - 1, 0, expr, key, tok, temSpliced)) |
| 1191 |
return 0; |
| 1192 |
createQuasiquoteAbbreviation(spliced ? "unquote-splicing" : "unquote", expr); |
| 1193 |
spliced = 0; |
| 1194 |
} |
| 1195 |
break; |
| 1196 |
default: |
| 1197 |
expr = new ConstantExpression(interp_->makeNil(), loc); |
| 1198 |
return 1; |
| 1199 |
} |
| 1200 |
return getToken(allowCloseParen, tok); |
| 1201 |
} |
| 1202 |
exprsSpliced.push_back(PackedBoolean(temSpliced)); |
| 1203 |
for (;;) { |
| 1204 |
Owner<Expression> tem; |
| 1205 |
if (!parseQuasiquoteTemplate(level, |
| 1206 |
allowCloseParen|allowUnquoteSplicing |
| 1207 |
|(type == QuasiquoteExpression::vectorType |
| 1208 |
? 0 |
| 1209 |
: allowPeriod), |
| 1210 |
tem, key, tok, temSpliced)) |
| 1211 |
return 0; |
| 1212 |
if (!tem) { |
| 1213 |
if (tok == tokenCloseParen) |
| 1214 |
break; |
| 1215 |
exprs.resize(exprs.size() + 1); |
| 1216 |
type = QuasiquoteExpression::improperType; |
| 1217 |
if (!parseQuasiquoteTemplate(level, 0, exprs.back(), key, tok, temSpliced)) |
| 1218 |
return 0; |
| 1219 |
if (!getToken(allowCloseParen, tok)) |
| 1220 |
return 0; |
| 1221 |
exprsSpliced.push_back(0); |
| 1222 |
break; |
| 1223 |
} |
| 1224 |
exprs.resize(exprs.size() + 1); |
| 1225 |
exprs.back().swap(tem); |
| 1226 |
exprsSpliced.push_back(PackedBoolean(temSpliced)); |
| 1227 |
} |
| 1228 |
expr = new QuasiquoteExpression(exprs, exprsSpliced, type, loc); |
| 1229 |
} |
| 1230 |
break; |
| 1231 |
case tokenIdentifier: |
| 1232 |
if (allowed & allowQuasiquoteKey) { |
| 1233 |
const Identifier *ident = lookup(currentToken_); |
| 1234 |
if (ident->syntacticKey(key)) { |
| 1235 |
switch (key) { |
| 1236 |
case Identifier::keyUnquoteSplicing: |
| 1237 |
case Identifier::keyUnquote: |
| 1238 |
case Identifier::keyQuasiquote: |
| 1239 |
return 1; |
| 1240 |
default: |
| 1241 |
break; |
| 1242 |
} |
| 1243 |
} |
| 1244 |
} |
| 1245 |
obj = interp_->makeSymbol(currentToken_); |
| 1246 |
// fall through |
| 1247 |
default: |
| 1248 |
if (obj) { |
| 1249 |
interp_->makePermanent(obj); |
| 1250 |
expr = new ConstantExpression(obj, in_->currentLocation()); |
| 1251 |
} |
| 1252 |
break; |
| 1253 |
} |
| 1254 |
return 1; |
| 1255 |
} |
| 1256 |
|
| 1257 |
void SchemeParser::createQuasiquoteAbbreviation(const char *sym, Owner<Expression> &expr) |
| 1258 |
{ |
| 1259 |
Location loc(expr->location()); |
| 1260 |
NCVector<Owner<Expression> > v(2); |
| 1261 |
v[1].swap(expr); |
| 1262 |
v[0] = new ConstantExpression(interp_->makeSymbol(Interpreter::makeStringC(sym)), loc); |
| 1263 |
Vector<PackedBoolean> spliced; |
| 1264 |
spliced.push_back(0); |
| 1265 |
spliced.push_back(0); |
| 1266 |
expr = new QuasiquoteExpression(v, spliced, QuasiquoteExpression::listType, loc); |
| 1267 |
} |
| 1268 |
|
| 1269 |
bool SchemeParser::parseIf(Owner<Expression> &expr) |
| 1270 |
{ |
| 1271 |
Location loc(in_->currentLocation()); |
| 1272 |
Owner<Expression> expr0, expr1, expr2; |
| 1273 |
Token tok; |
| 1274 |
Identifier::SyntacticKey key; |
| 1275 |
if (!parseExpression(0, expr0, key, tok) |
| 1276 |
|| !parseExpression(0, expr1, key, tok) |
| 1277 |
|| !parseExpression(dsssl2() ? allowCloseParen : 0, expr2, key, tok)) |
| 1278 |
return 0; |
| 1279 |
if (!expr2) |
| 1280 |
expr2 = new ConstantExpression(interp_->makeUnspecified(), in_->currentLocation()); |
| 1281 |
else if (!getToken(allowCloseParen, tok)) |
| 1282 |
return 0; |
| 1283 |
expr = new IfExpression(expr0, expr1, expr2, loc); |
| 1284 |
return 1; |
| 1285 |
} |
| 1286 |
|
| 1287 |
bool SchemeParser::parseCond(Owner<Expression> &expr, bool opt) |
| 1288 |
{ |
| 1289 |
Location loc(in_->currentLocation()); |
| 1290 |
Token tok; |
| 1291 |
if (!getToken(allowOpenParen|(opt ? unsigned(allowCloseParen) : 0), tok)) |
| 1292 |
return 0; |
| 1293 |
if (tok == tokenCloseParen) { |
| 1294 |
if (dsssl2()) |
| 1295 |
expr = new ConstantExpression(interp_->makeUnspecified(), loc); |
| 1296 |
else |
| 1297 |
expr = new CondFailExpression(loc); |
| 1298 |
return 1; |
| 1299 |
} |
| 1300 |
Identifier::SyntacticKey key; |
| 1301 |
Owner<Expression> testExpr; |
| 1302 |
if (!parseExpression(allowKeyElse, testExpr, key, tok)) |
| 1303 |
return 0; |
| 1304 |
if (!testExpr) { |
| 1305 |
if (!parseBegin(expr)) |
| 1306 |
return 0; |
| 1307 |
return getToken(allowCloseParen, tok); |
| 1308 |
} |
| 1309 |
NCVector<Owner<Expression> > valExprs; |
| 1310 |
Owner<Expression> tem; |
| 1311 |
bool arrow = 0; |
| 1312 |
if (!parseExpression(allowCloseParen|allowKeyArrow, tem, key, tok)) |
| 1313 |
return 0; |
| 1314 |
if (key == Identifier::keyArrow) { |
| 1315 |
arrow = 1; |
| 1316 |
interp_->requireFeature(Interpreter::expression, testExpr->location()); |
| 1317 |
if (!parseExpression(0, tem, key, tok)) |
| 1318 |
return 0; |
| 1319 |
} |
| 1320 |
if (!tem) |
| 1321 |
interp_->requireFeature(Interpreter::expression, testExpr->location()); |
| 1322 |
else { |
| 1323 |
valExprs.resize(valExprs.size() + 1); |
| 1324 |
tem.swap(valExprs.back()); |
| 1325 |
if (arrow || !dsssl2()) { |
| 1326 |
if (!getToken(allowCloseParen, tok)) |
| 1327 |
return 0; |
| 1328 |
} |
| 1329 |
else |
| 1330 |
for (;;) { |
| 1331 |
if (!parseExpression(allowCloseParen, tem, key, tok)) |
| 1332 |
return 0; |
| 1333 |
if (!tem) |
| 1334 |
break; |
| 1335 |
valExprs.resize(valExprs.size() + 1); |
| 1336 |
tem.swap(valExprs.back()); |
| 1337 |
} |
| 1338 |
} |
| 1339 |
Owner<Expression> valExpr; |
| 1340 |
if (valExprs.size() == 1) |
| 1341 |
valExprs[0].swap(valExpr); |
| 1342 |
else if (valExprs.size()) |
| 1343 |
valExpr = new SequenceExpression(valExprs, valExprs[0]->location()); |
| 1344 |
Owner<Expression> elseExpr; |
| 1345 |
if (!parseCond(elseExpr, 1)) |
| 1346 |
return 0; |
| 1347 |
if (arrow) { |
| 1348 |
const Identifier *ident = interp_->lookup(interp_->makeStringC("cond")); |
| 1349 |
Vector<const Identifier *> vars; |
| 1350 |
NCVector<Owner<Expression> > inits(1), args(1); |
| 1351 |
Owner<Expression> thenExpr, body; |
| 1352 |
vars.push_back(ident); |
| 1353 |
testExpr.swap(inits.back()); |
| 1354 |
testExpr = new VariableExpression(ident, loc); |
| 1355 |
args.back() = new VariableExpression(ident, loc); |
| 1356 |
thenExpr = new CallExpression(valExpr, args, loc); |
| 1357 |
body = new IfExpression(testExpr, thenExpr, elseExpr, loc); |
| 1358 |
expr = new LetExpression(vars, inits, body, loc); |
| 1359 |
} |
| 1360 |
else if (valExpr) |
| 1361 |
expr = new IfExpression(testExpr, valExpr, elseExpr, loc); |
| 1362 |
else |
| 1363 |
expr = new OrExpression(testExpr, elseExpr, loc); |
| 1364 |
return 1; |
| 1365 |
} |
| 1366 |
|
| 1367 |
bool SchemeParser::parseCase(Owner<Expression> &expr) |
| 1368 |
{ |
| 1369 |
Owner<Expression> keyExpr; |
| 1370 |
Owner<Expression> elseClause; |
| 1371 |
NCVector<CaseExpression::Case> cases; |
| 1372 |
Location loc(in_->currentLocation()); |
| 1373 |
Token tok; |
| 1374 |
Identifier::SyntacticKey key; |
| 1375 |
if (!parseExpression(0, keyExpr, key, tok)) |
| 1376 |
return 0; |
| 1377 |
for (;;) { |
| 1378 |
if (!getToken(allowOpenParen |
| 1379 |
|(cases.size() ? unsigned(allowCloseParen) : 0), tok)) |
| 1380 |
return 0; |
| 1381 |
if (tok == tokenCloseParen) |
| 1382 |
break; |
| 1383 |
if (!getToken(allowOpenParen|allowIdentifier, tok)) |
| 1384 |
return 0; |
| 1385 |
if (tok == tokenOpenParen) { |
| 1386 |
cases.resize(cases.size() + 1); |
| 1387 |
Location loc; |
| 1388 |
for (;;) { |
| 1389 |
ELObj *obj; |
| 1390 |
if (!parseDatum(allowCloseParen, obj, loc, tok)) |
| 1391 |
return 0; |
| 1392 |
if (tok == tokenCloseParen) |
| 1393 |
break; |
| 1394 |
interp_->makePermanent(obj); |
| 1395 |
cases.back().datums.push_back(obj); |
| 1396 |
} |
| 1397 |
if (!parseBegin(cases.back().expr)) |
| 1398 |
return 0; |
| 1399 |
} |
| 1400 |
else { |
| 1401 |
const Identifier *ident = lookup(currentToken_); |
| 1402 |
if (ident->syntacticKey(key) && key == Identifier::keyElse) { |
| 1403 |
if (!parseBegin(elseClause)) |
| 1404 |
return 0; |
| 1405 |
if (!getToken(allowCloseParen, tok)) |
| 1406 |
return 0; |
| 1407 |
break; |
| 1408 |
} |
| 1409 |
else { |
| 1410 |
message(InterpreterMessages::caseElse, |
| 1411 |
StringMessageArg(currentToken_)); |
| 1412 |
return 0; |
| 1413 |
} |
| 1414 |
} |
| 1415 |
} |
| 1416 |
if (dsssl2() && !elseClause) |
| 1417 |
elseClause = new ConstantExpression(interp_->makeUnspecified(), loc); |
| 1418 |
expr = new CaseExpression(keyExpr, cases, elseClause, loc); |
| 1419 |
return 1; |
| 1420 |
} |
| 1421 |
|
| 1422 |
bool SchemeParser::parseOr(Owner<Expression> &expr) |
| 1423 |
{ |
| 1424 |
Location loc(in_->currentLocation()); |
| 1425 |
Token tok; |
| 1426 |
Identifier::SyntacticKey key; |
| 1427 |
Owner<Expression> test1Expr; |
| 1428 |
if (!parseExpression(allowCloseParen, test1Expr, key, tok)) |
| 1429 |
return 0; |
| 1430 |
if (!test1Expr) { |
| 1431 |
expr = new ConstantExpression(interp_->makeFalse(), loc); |
| 1432 |
return 1; |
| 1433 |
} |
| 1434 |
Owner<Expression> test2Expr; |
| 1435 |
if (!parseOr(test2Expr)) |
| 1436 |
return 0; |
| 1437 |
expr = new OrExpression(test1Expr, test2Expr, loc); |
| 1438 |
return 1; |
| 1439 |
} |
| 1440 |
|
| 1441 |
bool SchemeParser::parseAnd(Owner<Expression> &expr, bool opt) |
| 1442 |
{ |
| 1443 |
Location loc(in_->currentLocation()); |
| 1444 |
Token tok; |
| 1445 |
Identifier::SyntacticKey key; |
| 1446 |
Owner<Expression> testExpr; |
| 1447 |
if (!parseExpression(allowCloseParen, testExpr, key, tok)) |
| 1448 |
return 0; |
| 1449 |
if (!testExpr) { |
| 1450 |
if (!opt) |
| 1451 |
expr = new ConstantExpression(interp_->makeTrue(), loc); |
| 1452 |
return 1; |
| 1453 |
} |
| 1454 |
Owner<Expression> restExpr; |
| 1455 |
if (!parseAnd(restExpr, 1)) |
| 1456 |
return 0; |
| 1457 |
if (!restExpr) |
| 1458 |
testExpr.swap(expr); |
| 1459 |
else { |
| 1460 |
// This relies on the fact that #f is the only false value. |
| 1461 |
Owner<Expression> falseExpr(new ConstantExpression(interp_->makeFalse(), loc)); |
| 1462 |
expr = new IfExpression(testExpr, restExpr, falseExpr, loc); |
| 1463 |
} |
| 1464 |
return 1; |
| 1465 |
} |
| 1466 |
|
| 1467 |
void SchemeParser::skipIntertokenSpace() |
| 1468 |
{ |
| 1469 |
for (;;) { |
| 1470 |
Xchar c = *in_->currentTokenEnd(); |
| 1471 |
switch (c) { |
| 1472 |
case InputSource::eE: |
| 1473 |
return; |
| 1474 |
case ' ': |
| 1475 |
case '\r': |
| 1476 |
case '\n': |
| 1477 |
case '\t': |
| 1478 |
case '\f': |
| 1479 |
break; |
| 1480 |
case ';': |
| 1481 |
for (;;) { |
| 1482 |
Xchar c1 = in_->tokenChar(*this); |
| 1483 |
if (c1 == InputSource::eE || c1 == '\r') |
| 1484 |
break; |
| 1485 |
} |
| 1486 |
break; |
| 1487 |
default: |
| 1488 |
if (interp_->lexCategory(c) != Interpreter::lexAddWhiteSpace) |
| 1489 |
return; |
| 1490 |
} |
| 1491 |
in_->tokenChar(*this); |
| 1492 |
} |
| 1493 |
} |
| 1494 |
|
| 1495 |
bool SchemeParser::peekDefine() |
| 1496 |
{ |
| 1497 |
// return true if we see (define, possibly with intertoken space |
| 1498 |
// around the paren. Do not change the current pos of in_. |
| 1499 |
size_t i = 0; |
| 1500 |
StringC seen; |
| 1501 |
in_->startToken(); |
| 1502 |
skipIntertokenSpace(); |
| 1503 |
if (in_->tokenChar(*this) == '(') { |
| 1504 |
static char txt[] = "define"; |
| 1505 |
skipIntertokenSpace(); |
| 1506 |
for (i = 0; i < 6; i++) |
| 1507 |
if (in_->tokenChar(*this) != txt[i]) |
| 1508 |
break; |
| 1509 |
} |
| 1510 |
in_->ungetToken(); |
| 1511 |
return (i == 6); |
| 1512 |
} |
| 1513 |
|
| 1514 |
bool SchemeParser::parseBody(Owner<Expression> &expr) |
| 1515 |
{ |
| 1516 |
Location loc(in_->currentLocation()); |
| 1517 |
Vector<const Identifier *> vars; |
| 1518 |
NCVector<Owner<Expression> > inits; |
| 1519 |
// FIXME: This is an ugly workaround to unget two tokens |
| 1520 |
// having something like setMark() and rewindToMark() in InputSource |
| 1521 |
// would be much nicer |
| 1522 |
while (peekDefine()) { |
| 1523 |
Token tok; |
| 1524 |
if (!getToken(allowOpenParen, tok) || !getToken(allowIdentifier, tok)) |
| 1525 |
return 0; // FIXME: Should never happen since we have peeked |
| 1526 |
Identifier *var; |
| 1527 |
inits.resize(inits.size() + 1); |
| 1528 |
if (!parseDefine(var, inits.back())) |
| 1529 |
return 0; |
| 1530 |
for (size_t i = 0; i < vars.size(); i++) |
| 1531 |
if (vars[i]->name() == var->name()) { |
| 1532 |
setNextLocation(in_->currentLocation()); |
| 1533 |
message(InterpreterMessages::duplicateVariableBinding, |
| 1534 |
StringMessageArg(var->name())); |
| 1535 |
return 0; |
| 1536 |
} |
| 1537 |
vars.push_back(var); |
| 1538 |
} |
| 1539 |
if (!parseBegin(expr)) |
| 1540 |
return 0; |
| 1541 |
if (vars.size() > 0) |
| 1542 |
expr = new LetrecExpression(vars, inits, expr, loc); |
| 1543 |
return 1; |
| 1544 |
} |
| 1545 |
|
| 1546 |
bool SchemeParser::parseBegin(Owner<Expression> &expr) |
| 1547 |
{ |
| 1548 |
Location loc(in_->currentLocation()); |
| 1549 |
Token tok; |
| 1550 |
Identifier::SyntacticKey key; |
| 1551 |
if (!parseExpression(0, expr, key, tok)) |
| 1552 |
return 0; |
| 1553 |
if (dsssl2()) { |
| 1554 |
NCVector<Owner<Expression> > exprs; |
| 1555 |
for (size_t i = 1;; i++) { |
| 1556 |
Owner<Expression> tem; |
| 1557 |
if (!parseExpression(allowCloseParen, tem, key, tok)) |
| 1558 |
return 0; |
| 1559 |
if (!tem) |
| 1560 |
break; |
| 1561 |
exprs.resize(i + 1); |
| 1562 |
tem.swap(exprs[i]); |
| 1563 |
} |
| 1564 |
if (exprs.size()) { |
| 1565 |
expr.swap(exprs[0]); |
| 1566 |
expr = new SequenceExpression(exprs, loc); |
| 1567 |
} |
| 1568 |
return 1; |
| 1569 |
} |
| 1570 |
else |
| 1571 |
return getToken(allowCloseParen, tok); |
| 1572 |
} |
| 1573 |
|
| 1574 |
bool SchemeParser::parseSet(Owner<Expression> &expr) |
| 1575 |
{ |
| 1576 |
Location loc(in_->currentLocation()); |
| 1577 |
Token tok; |
| 1578 |
if (!getToken(allowIdentifier, tok)) |
| 1579 |
return 0; |
| 1580 |
const Identifier *var = lookup(currentToken_); |
| 1581 |
Identifier::SyntacticKey key; |
| 1582 |
Owner<Expression> value; |
| 1583 |
if (!parseExpression(0, value, key, tok)) |
| 1584 |
return 0; |
| 1585 |
if (!getToken(allowCloseParen, tok)) |
| 1586 |
return 0; |
| 1587 |
expr = new AssignmentExpression(var, value, loc); |
| 1588 |
return 1; |
| 1589 |
} |
| 1590 |
|
| 1591 |
bool SchemeParser::parseWithMode(Owner<Expression> &expr) |
| 1592 |
{ |
| 1593 |
Location loc(in_->currentLocation()); |
| 1594 |
if (!interp_->style()) { |
| 1595 |
setNextLocation(loc); |
| 1596 |
message(InterpreterMessages::styleLanguage); |
| 1597 |
return 0; |
| 1598 |
} |
| 1599 |
Token tok; |
| 1600 |
if (!getToken(allowIdentifier|allowFalse, tok)) |
| 1601 |
return 0; |
| 1602 |
const ProcessingMode *mode; |
| 1603 |
if (tok == tokenFalse) |
| 1604 |
mode = interp_->initialProcessingMode(); |
| 1605 |
else |
| 1606 |
mode = interp_->lookupProcessingMode(currentToken_); |
| 1607 |
Owner<Expression> content; |
| 1608 |
Identifier::SyntacticKey key; |
| 1609 |
if (!parseExpression(0, content, key, tok)) |
| 1610 |
return 0; |
| 1611 |
if (!getToken(allowCloseParen, tok)) |
| 1612 |
return 0; |
| 1613 |
expr = new WithModeExpression(mode, content, loc); |
| 1614 |
return 1; |
| 1615 |
} |
| 1616 |
|
| 1617 |
bool SchemeParser::parseMake(Owner<Expression> &expr) |
| 1618 |
{ |
| 1619 |
Location loc(in_->currentLocation()); |
| 1620 |
if (!interp_->style()) { |
| 1621 |
setNextLocation(loc); |
| 1622 |
message(InterpreterMessages::styleLanguage); |
| 1623 |
return 0; |
| 1624 |
} |
| 1625 |
Token tok; |
| 1626 |
if (!getToken(allowIdentifier, tok)) |
| 1627 |
return 0; |
| 1628 |
const Identifier *foc = lookup(currentToken_); |
| 1629 |
NCVector<Owner<Expression> > exprs; |
| 1630 |
Vector<const Identifier *> keys; |
| 1631 |
for (;;) { |
| 1632 |
Owner<Expression> tem; |
| 1633 |
Identifier::SyntacticKey key; |
| 1634 |
if (!parseExpression(allowCloseParen, tem, key, tok)) |
| 1635 |
return 0; |
| 1636 |
if (!tem) |
| 1637 |
break; |
| 1638 |
if (keys.size() == exprs.size()) { |
| 1639 |
const Identifier *k = tem->keyword(); |
| 1640 |
if (k) { |
| 1641 |
tem.clear(); |
| 1642 |
if (!parseExpression(0, tem, key, tok)) |
| 1643 |
return 0; |
| 1644 |
size_t i; |
| 1645 |
for (i = 0; i < keys.size(); i++) |
| 1646 |
if (keys[i]->name() == k->name()) |
| 1647 |
break; |
| 1648 |
if (i < keys.size()) |
| 1649 |
continue; |
| 1650 |
keys.push_back(k); |
| 1651 |
} |
| 1652 |
} |
| 1653 |
exprs.resize(exprs.size() + 1); |
| 1654 |
tem.swap(exprs.back()); |
| 1655 |
} |
| 1656 |
expr = new MakeExpression(foc, keys, exprs, loc); |
| 1657 |
return 1; |
| 1658 |
} |
| 1659 |
|
| 1660 |
bool SchemeParser::parseStyle(Owner<Expression> &expr) |
| 1661 |
{ |
| 1662 |
Location loc(in_->currentLocation()); |
| 1663 |
if (!interp_->style()) { |
| 1664 |
setNextLocation(loc); |
| 1665 |
message(InterpreterMessages::styleLanguage); |
| 1666 |
return 0; |
| 1667 |
} |
| 1668 |
NCVector<Owner<Expression> > exprs; |
| 1669 |
Vector<const Identifier *> keys; |
| 1670 |
for (;;) { |
| 1671 |
Token tok; |
| 1672 |
if (!getToken(allowKeyword|allowCloseParen, tok)) |
| 1673 |
return 0; |
| 1674 |
if (tok == tokenCloseParen) |
| 1675 |
break; |
| 1676 |
keys.resize(keys.size() + 1); |
| 1677 |
keys.back() = lookup(currentToken_); |
| 1678 |
exprs.resize(exprs.size() + 1); |
| 1679 |
Identifier::SyntacticKey key; |
| 1680 |
if (!parseExpression(0, exprs.back(), key, tok)) |
| 1681 |
return 0; |
| 1682 |
} |
| 1683 |
expr = new StyleExpression(keys, exprs, loc); |
| 1684 |
return 1; |
| 1685 |
} |
| 1686 |
|
| 1687 |
bool SchemeParser::parseLambda(Owner<Expression> &expr) |
| 1688 |
{ |
| 1689 |
Location loc(in_->currentLocation()); |
| 1690 |
interp_->requireFeature(Interpreter::expression, loc); |
| 1691 |
Token tok; |
| 1692 |
if (!getToken(allowOpenParen, tok)) |
| 1693 |
return 0; |
| 1694 |
Vector<const Identifier *> formals; |
| 1695 |
NCVector<Owner<Expression> > inits; |
| 1696 |
int nOptional; |
| 1697 |
int nKey; |
| 1698 |
bool hasRest; |
| 1699 |
if (!parseFormals(formals, inits, nOptional, hasRest, nKey)) |
| 1700 |
return 0; |
| 1701 |
Owner<Expression> body; |
| 1702 |
if (!parseBody(body)) |
| 1703 |
return 0; |
| 1704 |
expr = new LambdaExpression(formals, inits, nOptional, hasRest, nKey, |
| 1705 |
body, loc); |
| 1706 |
return 1; |
| 1707 |
} |
| 1708 |
|
| 1709 |
// The rest arg is put last. |
| 1710 |
|
| 1711 |
bool SchemeParser::parseFormals(Vector<const Identifier *> &formals, |
| 1712 |
NCVector<Owner<Expression> > &inits, |
| 1713 |
int &nOptional, |
| 1714 |
bool &hasRest, |
| 1715 |
int &nKey) |
| 1716 |
{ |
| 1717 |
Location loc(in_->currentLocation()); |
| 1718 |
Token tok; |
| 1719 |
enum FormalType { required, optional, rest, key } type = required; |
| 1720 |
unsigned allowed = (allowCloseParen|allowIdentifier |
| 1721 |
|allowHashOptional|allowHashRest|allowHashKey); |
| 1722 |
int argCount[4]; |
| 1723 |
for (int i = 0; i < 4; i++) |
| 1724 |
argCount[i] = 0; |
| 1725 |
for (;;) { |
| 1726 |
if (!getToken(allowed, tok)) |
| 1727 |
return 0; |
| 1728 |
switch (tok) { |
| 1729 |
case tokenHashOptional: |
| 1730 |
allowed |= allowOpenParen; |
| 1731 |
allowed &= ~allowHashOptional; |
| 1732 |
type = optional; |
| 1733 |
break; |
| 1734 |
case tokenHashRest: |
| 1735 |
allowed = allowIdentifier; |
| 1736 |
type = rest; |
| 1737 |
break; |
| 1738 |
case tokenHashKey: |
| 1739 |
interp_->requireFeature(Interpreter::keyword, loc); |
| 1740 |
allowed = (allowOpenParen|allowCloseParen|allowIdentifier); |
| 1741 |
type = key; |
| 1742 |
break; |
| 1743 |
case tokenOpenParen: |
| 1744 |
{ |
| 1745 |
if (!getToken(allowIdentifier, tok)) |
| 1746 |
return 0; |
| 1747 |
argCount[type]++; |
| 1748 |
formals.push_back(lookup(currentToken_)); |
| 1749 |
inits.resize(argCount[optional] + argCount[key]); |
| 1750 |
Identifier::SyntacticKey key; |
| 1751 |
if (!parseExpression(0, inits.back(), key, tok)) |
| 1752 |
return 0; |
| 1753 |
if (!getToken(allowCloseParen, tok)) |
| 1754 |
return 0; |
| 1755 |
} |
| 1756 |
break; |
| 1757 |
case tokenIdentifier: |
| 1758 |
{ |
| 1759 |
formals.push_back(lookup(currentToken_)); |
| 1760 |
argCount[type]++; |
| 1761 |
if (type == rest) |
| 1762 |
allowed = (allowHashKey|allowCloseParen); |
| 1763 |
} |
| 1764 |
break; |
| 1765 |
case tokenCloseParen: |
| 1766 |
goto done; |
| 1767 |
default: |
| 1768 |
CANNOT_HAPPEN(); |
| 1769 |
} |
| 1770 |
} |
| 1771 |
done: |
| 1772 |
nOptional = argCount[optional]; |
| 1773 |
nKey = argCount[key]; |
| 1774 |
inits.resize(nOptional + nKey); |
| 1775 |
hasRest = argCount[rest]; |
| 1776 |
return 1; |
| 1777 |
} |
| 1778 |
|
| 1779 |
bool SchemeParser::parseLet(Owner<Expression> &expr) |
| 1780 |
{ |
| 1781 |
Location loc(in_->currentLocation()); |
| 1782 |
interp_->requireFeature(Interpreter::expression, loc); |
| 1783 |
Token tok; |
| 1784 |
if (!getToken(allowOpenParen|allowIdentifier, tok)) |
| 1785 |
return 0; |
| 1786 |
Vector<const Identifier *> vars; |
| 1787 |
NCVector<Owner<Expression> > inits; |
| 1788 |
Owner<Expression> body; |
| 1789 |
const Identifier *name; |
| 1790 |
if (tok == tokenOpenParen) { |
| 1791 |
name = 0; |
| 1792 |
if (!parseBindingsAndBody1(vars, inits, body)) |
| 1793 |
return 0; |
| 1794 |
} |
| 1795 |
else { |
| 1796 |
name = lookup(currentToken_); |
| 1797 |
if (!parseBindingsAndBody(vars, inits, body)) |
| 1798 |
return 0; |
| 1799 |
} |
| 1800 |
if (name) { |
| 1801 |
// Named let |
| 1802 |
NCVector<Owner<Expression> > loopInit(1); |
| 1803 |
NCVector<Owner<Expression> > argsInit; |
| 1804 |
loopInit[0] = new LambdaExpression(vars, argsInit, 0, 0, 0, body, loc); |
| 1805 |
Vector<const Identifier *> loopFormal(1); |
| 1806 |
loopFormal[0] = name; |
| 1807 |
expr = new VariableExpression(name, loc); |
| 1808 |
expr = new LetrecExpression(loopFormal, loopInit, expr, loc); |
| 1809 |
expr = new CallExpression(expr, inits, loc); |
| 1810 |
} |
| 1811 |
else |
| 1812 |
expr = new LetExpression(vars, inits, body, loc); |
| 1813 |
return 1; |
| 1814 |
} |
| 1815 |
|
| 1816 |
bool SchemeParser::parseLetStar(Owner<Expression> &expr) |
| 1817 |
{ |
| 1818 |
Location loc(in_->currentLocation()); |
| 1819 |
interp_->requireFeature(Interpreter::expression, loc); |
| 1820 |
Vector<const Identifier *> vars; |
| 1821 |
NCVector<Owner<Expression> > inits; |
| 1822 |
Owner<Expression> body; |
| 1823 |
if (!parseBindingsAndBody(vars, inits, body)) |
| 1824 |
return 0; |
| 1825 |
expr = new LetStarExpression(vars, inits, body, loc); |
| 1826 |
return 1; |
| 1827 |
} |
| 1828 |
|
| 1829 |
bool SchemeParser::parseLetrec(Owner<Expression> &expr) |
| 1830 |
{ |
| 1831 |
Location loc(in_->currentLocation()); |
| 1832 |
interp_->requireFeature(Interpreter::expression, loc); |
| 1833 |
Vector<const Identifier *> vars; |
| 1834 |
NCVector<Owner<Expression> > inits; |
| 1835 |
Owner<Expression> body; |
| 1836 |
if (!parseBindingsAndBody(vars, inits, body)) |
| 1837 |
return 0; |
| 1838 |
expr = new LetrecExpression(vars, inits, body, loc); |
| 1839 |
return 1; |
| 1840 |
} |
| 1841 |
|
| 1842 |
bool SchemeParser::parseBindingsAndBody(Vector<const Identifier *> &vars, |
| 1843 |
NCVector<Owner<Expression> > &inits, |
| 1844 |
Owner<Expression> &body) |
| 1845 |
{ |
| 1846 |
Token tok; |
| 1847 |
if (!getToken(allowOpenParen, tok)) |
| 1848 |
return 0; |
| 1849 |
return parseBindingsAndBody1(vars, inits, body); |
| 1850 |
} |
| 1851 |
|
| 1852 |
bool SchemeParser::parseBindingsAndBody1(Vector<const Identifier *> &vars, |
| 1853 |
NCVector<Owner<Expression> > &inits, |
| 1854 |
Owner<Expression> &body) |
| 1855 |
{ |
| 1856 |
Token tok; |
| 1857 |
Identifier::SyntacticKey key; |
| 1858 |
for (;;) { |
| 1859 |
if (!getToken(allowCloseParen|allowOpenParen, tok)) |
| 1860 |
return 0; |
| 1861 |
if (tok == tokenCloseParen) |
| 1862 |
break; |
| 1863 |
if (!getToken(allowIdentifier, tok)) |
| 1864 |
return 0; |
| 1865 |
Identifier *var = lookup(currentToken_); |
| 1866 |
for (size_t i = 0; i < vars.size(); i++) |
| 1867 |
if (vars[i]->name() == var->name()) { |
| 1868 |
setNextLocation(in_->currentLocation()); |
| 1869 |
message(InterpreterMessages::duplicateVariableBinding, |
| 1870 |
StringMessageArg(var->name())); |
| 1871 |
return 0; |
| 1872 |
} |
| 1873 |
vars.push_back(var); |
| 1874 |
inits.resize(inits.size() + 1); |
| 1875 |
if (!parseExpression(0, inits.back(), key, tok)) |
| 1876 |
return 0; |
| 1877 |
if (!getToken(allowCloseParen, tok)) |
| 1878 |
return 0; |
| 1879 |
} |
| 1880 |
return parseBody(body); |
| 1881 |
} |
| 1882 |
|
| 1883 |
bool SchemeParser::parseDatum(unsigned otherAllowed, |
| 1884 |
ELObj *&result, |
| 1885 |
Location &loc, |
| 1886 |
Token &tok) |
| 1887 |
{ |
| 1888 |
if (!parseSelfEvaluating(otherAllowed|allowVector|allowUnquote|allowUnquoteSplicing, result, tok)) |
| 1889 |
return 0; |
| 1890 |
loc = in_->currentLocation(); |
| 1891 |
if (result) |
| 1892 |
return 1; |
| 1893 |
switch (tok) { |
| 1894 |
case tokenIdentifier: |
| 1895 |
result = interp_->makeSymbol(currentToken_); |
| 1896 |
break; |
| 1897 |
case tokenQuote: |
| 1898 |
return parseAbbreviation("quote", result); |
| 1899 |
case tokenQuasiquote: |
| 1900 |
return parseAbbreviation("quasiquote", result); |
| 1901 |
case tokenUnquote: |
| 1902 |
return parseAbbreviation("unquote", result); |
| 1903 |
case tokenUnquoteSplicing: |
| 1904 |
return parseAbbreviation("unquote-splicing", result); |
| 1905 |
case tokenOpenParen: |
| 1906 |
{ |
| 1907 |
ELObj *tem; |
| 1908 |
Location ignore; |
| 1909 |
if (!parseDatum(allowCloseParen, tem, ignore, tok)) |
| 1910 |
return 0; |
| 1911 |
if (!tem) { |
| 1912 |
result = interp_->makeNil(); |
| 1913 |
break; |
| 1914 |
} |
| 1915 |
ELObjDynamicRoot list(*interp_, tem); |
| 1916 |
PairObj *last = new (*interp_) PairObj(tem, 0); |
| 1917 |
list = last; |
| 1918 |
for (;;) { |
| 1919 |
if (!parseDatum(allowCloseParen|allowPeriod, tem, ignore, tok)) |
| 1920 |
return 0; |
| 1921 |
if (!tem) { |
| 1922 |
if (tok == tokenCloseParen) { |
| 1923 |
last->setCdr(interp_->makeNil()); |
| 1924 |
break; |
| 1925 |
} |
| 1926 |
if (!parseDatum(0, tem, ignore, tok)) |
| 1927 |
return 0; |
| 1928 |
last->setCdr(tem); |
| 1929 |
if (!getToken(allowCloseParen, tok)) |
| 1930 |
return 0; |
| 1931 |
break; |
| 1932 |
} |
| 1933 |
last->setCdr(tem); // to protect it |
| 1934 |
PairObj *p = new (*interp_) PairObj(tem, 0); |
| 1935 |
last->setCdr(p); |
| 1936 |
last = p; |
| 1937 |
} |
| 1938 |
result = list; |
| 1939 |
} |
| 1940 |
break; |
| 1941 |
case tokenVector: |
| 1942 |
{ |
| 1943 |
VectorObj *v = new (*interp_) VectorObj; |
| 1944 |
ELObjDynamicRoot protect(*interp_, v); |
| 1945 |
Vector<ELObj *> &vec = *v; |
| 1946 |
Location ignore; |
| 1947 |
for (;;) { |
| 1948 |
ELObj *tem; |
| 1949 |
if (!parseDatum(allowCloseParen, tem, ignore, tok)) |
| 1950 |
return 0; |
| 1951 |
if (!tem) |
| 1952 |
break; |
| 1953 |
vec.push_back(tem); |
| 1954 |
} |
| 1955 |
result = v; |
| 1956 |
} |
| 1957 |
break; |
| 1958 |
default: |
| 1959 |
break; |
| 1960 |
} |
| 1961 |
return 1; |
| 1962 |
} |
| 1963 |
|
| 1964 |
bool SchemeParser::parseSelfEvaluating(unsigned otherAllowed, |
| 1965 |
ELObj *&result, |
| 1966 |
Token &tok) |
| 1967 |
{ |
| 1968 |
Location loc(in_->currentLocation()); |
| 1969 |
if (!getToken(allowExpr|otherAllowed, tok)) |
| 1970 |
return 0; |
| 1971 |
switch (tok) { |
| 1972 |
case tokenTrue: |
| 1973 |
result = interp_->makeTrue(); |
| 1974 |
break; |
| 1975 |
case tokenFalse: |
| 1976 |
result = interp_->makeFalse(); |
| 1977 |
break; |
| 1978 |
case tokenVoid: |
| 1979 |
result = interp_->makeUnspecified(); |
| 1980 |
break; |
| 1981 |
case tokenString: |
| 1982 |
result = new (*interp_) StringObj(currentToken_); |
| 1983 |
break; |
| 1984 |
case tokenKeyword: |
| 1985 |
result = interp_->makeKeyword(currentToken_); |
| 1986 |
break; |
| 1987 |
case tokenChar: |
| 1988 |
result = interp_->makeChar(currentToken_[0]); |
| 1989 |
break; |
| 1990 |
case tokenNumber: |
| 1991 |
result = interp_->convertNumber(currentToken_); |
| 1992 |
if (!result) { |
| 1993 |
message(InterpreterMessages::invalidNumber, |
| 1994 |
StringMessageArg(currentToken_)); |
| 1995 |
result = interp_->makeError(); |
| 1996 |
} |
| 1997 |
break; |
| 1998 |
case tokenGlyphId: |
| 1999 |
if (!interp_->style()) { |
| 2000 |
setNextLocation(loc); |
| 2001 |
message(InterpreterMessages::styleLanguage); |
| 2002 |
return 0; |
| 2003 |
} |
| 2004 |
result = convertAfiiGlyphId(currentToken_); |
| 2005 |
break; |
| 2006 |
default: |
| 2007 |
result = 0; |
| 2008 |
break; |
| 2009 |
} |
| 2010 |
return 1; |
| 2011 |
} |
| 2012 |
|
| 2013 |
bool SchemeParser::parseAbbreviation(const char *sym, ELObj *&result) |
| 2014 |
{ |
| 2015 |
SymbolObj *quoteSym = interp_->makeSymbol(Interpreter::makeStringC(sym)); |
| 2016 |
ELObj *obj; |
| 2017 |
Location ignore; |
| 2018 |
Token tok; |
| 2019 |
if (!parseDatum(0, obj, ignore, tok)) |
| 2020 |
return 0; |
| 2021 |
ELObjDynamicRoot protect(*interp_, obj); |
| 2022 |
protect = new (*interp_) PairObj(protect, interp_->makeNil()); |
| 2023 |
result = interp_->makePair(quoteSym, protect); |
| 2024 |
return 1; |
| 2025 |
} |
| 2026 |
|
| 2027 |
bool SchemeParser::getToken(unsigned allowed, Token &tok) |
| 2028 |
{ |
| 2029 |
Location loc(in_->currentLocation()); |
| 2030 |
InputSource *in = in_.pointer(); |
| 2031 |
for (;;) { |
| 2032 |
in->startToken(); |
| 2033 |
Xchar c = in->tokenChar(*this); |
| 2034 |
switch (c) { |
| 2035 |
case InputSource::eE: |
| 2036 |
if (!(allowed & allowEndOfEntity)) |
| 2037 |
return tokenRecover(allowed, tok); |
| 2038 |
tok = tokenEndOfEntity; |
| 2039 |
return 1; |
| 2040 |
case '(': |
| 2041 |
if (!(allowed & allowOpenParen)) |
| 2042 |
return tokenRecover(allowed, tok); |
| 2043 |
tok = tokenOpenParen; |
| 2044 |
return 1; |
| 2045 |
case ')': |
| 2046 |
if (!(allowed & allowCloseParen)) |
| 2047 |
return tokenRecover(allowed, tok); |
| 2048 |
tok = tokenCloseParen; |
| 2049 |
return 1; |
| 2050 |
case '\'': |
| 2051 |
if (!(allowed & allowOtherExpr)) |
| 2052 |
return tokenRecover(allowed, tok); |
| 2053 |
tok = tokenQuote; |
| 2054 |
return 1; |
| 2055 |
case '`': |
| 2056 |
if (!(allowed & allowOtherExpr)) |
| 2057 |
return tokenRecover(allowed, tok); |
| 2058 |
tok = tokenQuasiquote; |
| 2059 |
return 1; |
| 2060 |
case ',': |
| 2061 |
c = in->tokenChar(*this); |
| 2062 |
if (c == '@') { |
| 2063 |
if (!(allowed & allowUnquoteSplicing)) |
| 2064 |
return tokenRecover(allowed, tok); |
| 2065 |
tok = tokenUnquoteSplicing; |
| 2066 |
} |
| 2067 |
else { |
| 2068 |
if (!(allowed & allowUnquote)) |
| 2069 |
return tokenRecover(allowed, tok); |
| 2070 |
tok = tokenUnquote; |
| 2071 |
in->endToken(1); |
| 2072 |
} |
| 2073 |
return 1; |
| 2074 |
case ' ': |
| 2075 |
case '\r': |
| 2076 |
case '\n': |
| 2077 |
case '\t': |
| 2078 |
case '\f': |
| 2079 |
// whitespace |
| 2080 |
break; |
| 2081 |
case '#': |
| 2082 |
c = in->tokenChar(*this); |
| 2083 |
switch (c) { |
| 2084 |
case 't': |
| 2085 |
if (!(allowed & allowOtherExpr)) |
| 2086 |
return tokenRecover(allowed, tok); |
| 2087 |
tok = tokenTrue; |
| 2088 |
return 1; |
| 2089 |
case 'f': |
| 2090 |
if (!(allowed & allowFalse)) |
| 2091 |
return tokenRecover(allowed, tok); |
| 2092 |
tok = tokenFalse; |
| 2093 |
return 1; |
| 2094 |
case '\\': |
| 2095 |
{ |
| 2096 |
c = in->tokenChar(*this); |
| 2097 |
if (c == InputSource::eE) { |
| 2098 |
message(InterpreterMessages::unexpectedEof); |
| 2099 |
if (allowed & allowEndOfEntity) { |
| 2100 |
tok = tokenEndOfEntity; |
| 2101 |
return 1; |
| 2102 |
} |
| 2103 |
return 0; |
| 2104 |
} |
| 2105 |
if (!(allowed & allowOtherExpr)) { |
| 2106 |
extendToken(); |
| 2107 |
return tokenRecover(allowed, tok); |
| 2108 |
} |
| 2109 |
in->discardInitial(); |
| 2110 |
extendToken(); |
| 2111 |
tok = tokenChar; |
| 2112 |
if (in->currentTokenLength() == 1) { |
| 2113 |
currentToken_.assign(in->currentTokenStart(), 1); |
| 2114 |
} |
| 2115 |
else { |
| 2116 |
StringC tem(in->currentTokenStart(), in->currentTokenLength()); |
| 2117 |
currentToken_.resize(1); |
| 2118 |
if (!interp_->convertCharName(tem, currentToken_[0])) { |
| 2119 |
message(InterpreterMessages::unknownCharName, |
| 2120 |
StringMessageArg(tem)); |
| 2121 |
currentToken_[0] = defaultChar; |
| 2122 |
} |
| 2123 |
} |
| 2124 |
return 1; |
| 2125 |
} |
| 2126 |
case '!': |
| 2127 |
{ |
| 2128 |
extendToken(); |
| 2129 |
StringC tem(in->currentTokenStart() + 2, |
| 2130 |
in->currentTokenLength() - 2); |
| 2131 |
if (tem == Interpreter::makeStringC("optional")) { |
| 2132 |
if (!(allowed & allowHashOptional)) |
| 2133 |
return tokenRecover(allowed, tok); |
| 2134 |
tok = tokenHashOptional; |
| 2135 |
return 1; |
| 2136 |
} |
| 2137 |
if (tem == Interpreter::makeStringC("key")) { |
| 2138 |
if (!(allowed & allowHashKey)) |
| 2139 |
return tokenRecover(allowed, tok); |
| 2140 |
tok = tokenHashKey; |
| 2141 |
return 1; |
| 2142 |
} |
| 2143 |
if (tem == Interpreter::makeStringC("rest")) { |
| 2144 |
if (!(allowed & allowHashRest)) |
| 2145 |
return tokenRecover(allowed, tok); |
| 2146 |
tok = tokenHashRest; |
| 2147 |
return 1; |
| 2148 |
} |
| 2149 |
if (tem == Interpreter::makeStringC("contents")) { |
| 2150 |
if (!(allowed & allowHashContents)) |
| 2151 |
return tokenRecover(allowed, tok); |
| 2152 |
tok = tokenHashContents; |
| 2153 |
return 1; |
| 2154 |
} |
| 2155 |
message(InterpreterMessages::unknownNamedConstant, |
| 2156 |
StringMessageArg(tem)); |
| 2157 |
break; |
| 2158 |
} |
| 2159 |
case 'b': |
| 2160 |
case 'o': |
| 2161 |
case 'x': |
| 2162 |
case 'd': |
| 2163 |
extendToken(); |
| 2164 |
if (!(allowed & allowOtherExpr)) |
| 2165 |
return tokenRecover(allowed, tok); |
| 2166 |
tok = tokenNumber; |
| 2167 |
currentToken_.assign(in->currentTokenStart(), |
| 2168 |
in->currentTokenLength()); |
| 2169 |
return 1; |
| 2170 |
case 'A': |
| 2171 |
extendToken(); |
| 2172 |
if (!(allowed & allowOtherExpr)) |
| 2173 |
return tokenRecover(allowed, tok); |
| 2174 |
tok = tokenGlyphId; |
| 2175 |
currentToken_.assign(in->currentTokenStart() + 2, |
| 2176 |
in->currentTokenLength() - 2); |
| 2177 |
if (!interp_->style()) { |
| 2178 |
setNextLocation(loc); |
| 2179 |
message(InterpreterMessages::styleLanguage); |
| 2180 |
} |
| 2181 |
return 1; |
| 2182 |
case InputSource::eE: |
| 2183 |
message(InterpreterMessages::unexpectedEof); |
| 2184 |
if (allowed & allowEndOfEntity) { |
| 2185 |
tok = tokenEndOfEntity; |
| 2186 |
return 1; |
| 2187 |
} |
| 2188 |
return 0; |
| 2189 |
case 'v': |
| 2190 |
if (dsssl2()) { |
| 2191 |
if (!(allowed & allowOtherExpr)) |
| 2192 |
return tokenRecover(allowed, tok); |
| 2193 |
tok = tokenVoid; |
| 2194 |
return 1; |
| 2195 |
} |
| 2196 |
// fall through |
| 2197 |
case '(': |
| 2198 |
if (dsssl2()) { |
| 2199 |
if (!(allowed & allowVector)) |
| 2200 |
return tokenRecover(allowed, tok); |
| 2201 |
tok = tokenVector; |
| 2202 |
return 1; |
| 2203 |
} |
| 2204 |
//fall through |
| 2205 |
default: |
| 2206 |
message(InterpreterMessages::unknownHash); |
| 2207 |
break; |
| 2208 |
} |
| 2209 |
break; |
| 2210 |
case '"': |
| 2211 |
if (!(allowed & allowString)) |
| 2212 |
return tokenRecover(allowed, tok); |
| 2213 |
return scanString(allowed, tok); |
| 2214 |
case ';': |
| 2215 |
skipComment(); |
| 2216 |
break; |
| 2217 |
case '.': |
| 2218 |
extendToken(); |
| 2219 |
switch (in->currentTokenLength()) { |
| 2220 |
case 1: |
| 2221 |
if (!(allowed & allowPeriod)) |
| 2222 |
return tokenRecover(allowed, tok); |
| 2223 |
tok = tokenPeriod; |
| 2224 |
return 1; |
| 2225 |
case 3: |
| 2226 |
if (in_->currentTokenStart()[1] == '.' |
| 2227 |
&& in_->currentTokenStart()[2] == '.') |
| 2228 |
return handleIdentifier(allowed, tok); |
| 2229 |
break; |
| 2230 |
} |
| 2231 |
return handleNumber(allowed, tok); |
| 2232 |
default: |
| 2233 |
switch (interp_->lexCategory(c)) { |
| 2234 |
case Interpreter::lexAddWhiteSpace: |
| 2235 |
break; |
| 2236 |
case Interpreter::lexOtherNumberStart: |
| 2237 |
extendToken(); |
| 2238 |
// handle + and - as identifiers |
| 2239 |
if (in->currentTokenLength() == 1) |
| 2240 |
return handleIdentifier(allowed, tok); |
| 2241 |
return handleNumber(allowed, tok); |
| 2242 |
case Interpreter::lexDigit: |
| 2243 |
extendToken(); |
| 2244 |
return handleNumber(allowed, tok); |
| 2245 |
case Interpreter::lexOther: |
| 2246 |
if (c < ' ') { |
| 2247 |
// ignore control characters |
| 2248 |
message(InterpreterMessages::invalidChar); |
| 2249 |
break; |
| 2250 |
} |
| 2251 |
in->ungetToken(); |
| 2252 |
// fall through |
| 2253 |
default: |
| 2254 |
{ |
| 2255 |
bool invalid = 0; |
| 2256 |
size_t length = in->currentTokenLength(); |
| 2257 |
for (;;) { |
| 2258 |
Interpreter::LexCategory lc = interp_->lexCategory(in->tokenChar(*this)); |
| 2259 |
if (lc > Interpreter::lexOther) |
| 2260 |
break; |
| 2261 |
if (lc == Interpreter::lexOther) |
| 2262 |
invalid = 1; |
| 2263 |
length++; |
| 2264 |
} |
| 2265 |
in->endToken(length); |
| 2266 |
|
| 2267 |
if (in->currentTokenEnd()[-1] == ':' |
| 2268 |
&& in->currentTokenLength() > 1) { |
| 2269 |
if (!(allowed & allowKeyword)) |
| 2270 |
return tokenRecover(allowed, tok); |
| 2271 |
currentToken_.assign(in->currentTokenStart(), |
| 2272 |
in->currentTokenLength() - 1); |
| 2273 |
tok = tokenKeyword; |
| 2274 |
if (invalid || (currentToken_.size() > 1 |
| 2275 |
&& currentToken_[currentToken_.size() - 1] == ':')) |
| 2276 |
message(InterpreterMessages::invalidIdentifier, |
| 2277 |
StringMessageArg(currentToken_)); |
| 2278 |
return 1; |
| 2279 |
} |
| 2280 |
if (invalid) |
| 2281 |
message(InterpreterMessages::invalidIdentifier, |
| 2282 |
StringMessageArg(StringC(in->currentTokenStart(), |
| 2283 |
in->currentTokenLength()))); |
| 2284 |
return handleIdentifier(allowed, tok); |
| 2285 |
} |
| 2286 |
} |
| 2287 |
} |
| 2288 |
} |
| 2289 |
} |
| 2290 |
|
| 2291 |
bool SchemeParser::handleNumber(unsigned allowed, Token &tok) |
| 2292 |
{ |
| 2293 |
if (!(allowed & allowOtherExpr)) |
| 2294 |
return tokenRecover(allowed, tok); |
| 2295 |
tok = tokenNumber; |
| 2296 |
currentToken_.assign(in_->currentTokenStart(), |
| 2297 |
in_->currentTokenLength()); |
| 2298 |
return 1; |
| 2299 |
} |
| 2300 |
|
| 2301 |
bool SchemeParser::handleIdentifier(unsigned allowed, Token &tok) |
| 2302 |
{ |
| 2303 |
if (!(allowed & allowIdentifier)) |
| 2304 |
return tokenRecover(allowed, tok); |
| 2305 |
currentToken_.assign(in_->currentTokenStart(), |
| 2306 |
in_->currentTokenLength()); |
| 2307 |
tok = tokenIdentifier; |
| 2308 |
return 1; |
| 2309 |
} |
| 2310 |
|
| 2311 |
bool SchemeParser::tokenRecover(unsigned allowed, Token &tok) |
| 2312 |
{ |
| 2313 |
if (allowed == allowCloseParen) { |
| 2314 |
in_->ungetToken(); |
| 2315 |
tok = tokenCloseParen; |
| 2316 |
message(InterpreterMessages::missingCloseParen); |
| 2317 |
return 1; |
| 2318 |
} |
| 2319 |
if (in_->currentTokenLength() == 0) |
| 2320 |
message(InterpreterMessages::unexpectedEof); |
| 2321 |
else |
| 2322 |
message(InterpreterMessages::unexpectedToken, |
| 2323 |
StringMessageArg(StringC(in_->currentTokenStart(), |
| 2324 |
in_->currentTokenLength()))); |
| 2325 |
return 0; |
| 2326 |
} |
| 2327 |
|
| 2328 |
void SchemeParser::extendToken() |
| 2329 |
{ |
| 2330 |
// extend to a delimiter |
| 2331 |
InputSource *in = in_.pointer(); |
| 2332 |
size_t length = in->currentTokenLength(); |
| 2333 |
while (interp_->lexCategory(in->tokenChar(*this)) |
| 2334 |
<= Interpreter::lexOther) |
| 2335 |
length++; |
| 2336 |
in->endToken(length); |
| 2337 |
} |
| 2338 |
|
| 2339 |
bool SchemeParser::scanString(unsigned allowed, Token &tok) |
| 2340 |
{ |
| 2341 |
InputSource *in = in_.pointer(); |
| 2342 |
currentToken_.resize(0); |
| 2343 |
for (;;) { |
| 2344 |
Xchar c = in->tokenChar(*this); |
| 2345 |
switch (c) { |
| 2346 |
case InputSource::eE: |
| 2347 |
message(InterpreterMessages::unterminatedString); |
| 2348 |
in->endToken(1); |
| 2349 |
return 0; |
| 2350 |
case '"': |
| 2351 |
tok = tokenString; |
| 2352 |
return 1; |
| 2353 |
case '\\': |
| 2354 |
c = in->tokenChar(*this); |
| 2355 |
if (c == '\\' || c == '"') |
| 2356 |
currentToken_ += c; |
| 2357 |
else if (c == InputSource::eE) |
| 2358 |
break; |
| 2359 |
else { |
| 2360 |
StringC name; |
| 2361 |
name += c; |
| 2362 |
while (interp_->lexCategory(c = in->tokenChar(*this)) < Interpreter::lexDelimiter) |
| 2363 |
name += c; |
| 2364 |
if (c != ';') |
| 2365 |
in->endToken(in->currentTokenLength() - 1); |
| 2366 |
Char ch; |
| 2367 |
if (interp_->convertCharName(name, ch)) |
| 2368 |
currentToken_ += ch; |
| 2369 |
else |
| 2370 |
message(InterpreterMessages::unknownCharName, StringMessageArg(name)); |
| 2371 |
} |
| 2372 |
break; |
| 2373 |
default: |
| 2374 |
currentToken_ += c; |
| 2375 |
break; |
| 2376 |
} |
| 2377 |
} |
| 2378 |
return 0; // not reached |
| 2379 |
} |
| 2380 |
|
| 2381 |
void SchemeParser::skipComment() |
| 2382 |
{ |
| 2383 |
for (;;) { |
| 2384 |
Xchar c = in_->get(*this); |
| 2385 |
if (c == InputSource::eE || c == '\r') |
| 2386 |
break; |
| 2387 |
} |
| 2388 |
} |
| 2389 |
|
| 2390 |
ELObj *SchemeParser::convertAfiiGlyphId(const StringC &str) |
| 2391 |
{ |
| 2392 |
unsigned long n = 0; |
| 2393 |
for (size_t i = 0; i < str.size(); i++) { |
| 2394 |
if (str[i] < '0' || str[i] > '9') { |
| 2395 |
n = 0; |
| 2396 |
break; |
| 2397 |
} |
| 2398 |
// FIXME check for overflow |
| 2399 |
n = n*10 + (str[i] - '0'); |
| 2400 |
} |
| 2401 |
if (n == 0) { |
| 2402 |
message(InterpreterMessages::invalidAfiiGlyphId, StringMessageArg(str)); |
| 2403 |
return 0; |
| 2404 |
} |
| 2405 |
return new (*interp_) GlyphIdObj(FOTBuilder::GlyphId(afiiPublicId_, n)); |
| 2406 |
} |
| 2407 |
|
| 2408 |
void SchemeParser::dispatchMessage(Message &msg) |
| 2409 |
{ |
| 2410 |
interp_->dispatchMessage(msg); |
| 2411 |
} |
| 2412 |
|
| 2413 |
void SchemeParser::dispatchMessage(const Message &msg) |
| 2414 |
{ |
| 2415 |
interp_->dispatchMessage(msg); |
| 2416 |
} |
| 2417 |
|
| 2418 |
void SchemeParser::initMessage(Message &msg) |
| 2419 |
{ |
| 2420 |
if (in_) |
| 2421 |
msg.loc = in_->currentLocation(); |
| 2422 |
} |
| 2423 |
|
| 2424 |
bool SchemeParser::doDeclareDefaultLanguage() |
| 2425 |
{ |
| 2426 |
Location loc(in_->currentLocation()); |
| 2427 |
Owner<Expression> expr; |
| 2428 |
Token tok; |
| 2429 |
Identifier::SyntacticKey key; |
| 2430 |
if (!parseExpression(0, expr, key, tok)) |
| 2431 |
return 0; |
| 2432 |
if (!getToken(allowCloseParen, tok)) |
| 2433 |
return 0; |
| 2434 |
Location defLoc; |
| 2435 |
unsigned defPart; |
| 2436 |
if(interp_->defaultLanguageSet(defPart, defLoc) |
| 2437 |
&& defPart <= interp_->currentPartIndex()) { |
| 2438 |
if(defPart == interp_->currentPartIndex()) { |
| 2439 |
interp_->setNextLocation(loc); |
| 2440 |
message(InterpreterMessages::duplicateDefLangDecl, defLoc); |
| 2441 |
} |
| 2442 |
} |
| 2443 |
else |
| 2444 |
interp_->setDefaultLanguage(expr, interp_->currentPartIndex(), loc); |
| 2445 |
return 1; |
| 2446 |
} |
| 2447 |
|
| 2448 |
bool SchemeParser::doDefineLanguage() |
| 2449 |
{ |
| 2450 |
Location loc(in_->currentLocation()); |
| 2451 |
Token tok; |
| 2452 |
if (!getToken(allowIdentifier, tok)) |
| 2453 |
return 0; |
| 2454 |
Identifier *ident = lookup(currentToken_); |
| 2455 |
Identifier::SyntacticKey key; |
| 2456 |
if (ident->syntacticKey(key) && (key <= int(Identifier::lastSyntacticKey))) |
| 2457 |
message(InterpreterMessages::syntacticKeywordAsVariable, |
| 2458 |
StringMessageArg(currentToken_)); |
| 2459 |
Location defLoc; |
| 2460 |
unsigned defPart; |
| 2461 |
if (ident->defined(defPart, defLoc) |
| 2462 |
&& defPart <= interp_->currentPartIndex()) { |
| 2463 |
if (defPart == interp_->currentPartIndex()) { |
| 2464 |
message(InterpreterMessages::duplicateDefinition, |
| 2465 |
StringMessageArg(ident->name()), |
| 2466 |
defLoc); |
| 2467 |
return 0; |
| 2468 |
} |
| 2469 |
} |
| 2470 |
lang_ = new (*interp_) LangObj; |
| 2471 |
bool hadCollate = 0; |
| 2472 |
bool hadToupper = 0; |
| 2473 |
bool hadTolower = 0; |
| 2474 |
for (;;) { |
| 2475 |
if (!getToken(allowOpenParen|allowCloseParen, tok)) |
| 2476 |
return 0; |
| 2477 |
if (tok == tokenCloseParen) |
| 2478 |
break; |
| 2479 |
if (!getToken(allowIdentifier, tok)) |
| 2480 |
return 0; |
| 2481 |
const Identifier *ident = lookup(currentToken_); |
| 2482 |
Identifier::SyntacticKey key; |
| 2483 |
if (!ident->syntacticKey(key)) |
| 2484 |
return 0; |
| 2485 |
else { |
| 2486 |
switch (key) { |
| 2487 |
case Identifier::keyCollate: |
| 2488 |
if (hadCollate || !doCollate()) |
| 2489 |
return 0; |
| 2490 |
hadCollate = 1; |
| 2491 |
break; |
| 2492 |
case Identifier::keyToupper: |
| 2493 |
if (hadToupper || !doToupper()) |
| 2494 |
return 0; |
| 2495 |
hadToupper = 1; |
| 2496 |
break; |
| 2497 |
case Identifier::keyTolower: |
| 2498 |
if (hadTolower || !doTolower()) |
| 2499 |
return 0; |
| 2500 |
hadTolower = 1; |
| 2501 |
break; |
| 2502 |
default: |
| 2503 |
return 0; |
| 2504 |
} |
| 2505 |
} |
| 2506 |
} |
| 2507 |
if (!lang_->compile()) |
| 2508 |
return 0; |
| 2509 |
interp_->makePermanent(lang_); |
| 2510 |
Owner<Expression> expr; |
| 2511 |
expr = new ConstantExpression(lang_, in_->currentLocation()); |
| 2512 |
lang_ = 0; |
| 2513 |
ident->setDefinition(expr, interp_->currentPartIndex(), loc); |
| 2514 |
return 1; |
| 2515 |
} |
| 2516 |
|
| 2517 |
bool SchemeParser::doCollate() |
| 2518 |
{ |
| 2519 |
Token tok; |
| 2520 |
for (;;) { |
| 2521 |
if (!getToken(allowOpenParen|allowCloseParen, tok)) |
| 2522 |
return 0; |
| 2523 |
if (tok == tokenCloseParen) |
| 2524 |
break; |
| 2525 |
if (!getToken(allowIdentifier, tok)) |
| 2526 |
return 0; |
| 2527 |
const Identifier *ident = lookup(currentToken_); |
| 2528 |
Identifier::SyntacticKey key; |
| 2529 |
if (!ident->syntacticKey(key)) { |
| 2530 |
return 0; |
| 2531 |
} else { |
| 2532 |
switch (key) { |
| 2533 |
case Identifier::keyElement: |
| 2534 |
if (!doMultiCollatingElement()) |
| 2535 |
return 0; |
| 2536 |
break; |
| 2537 |
case Identifier::keySymbol: |
| 2538 |
if (!doCollatingSymbol()) |
| 2539 |
return 0; |
| 2540 |
break; |
| 2541 |
case Identifier::keyOrder: |
| 2542 |
if (!doCollatingOrder()) |
| 2543 |
return 0; |
| 2544 |
break; |
| 2545 |
default: |
| 2546 |
return 0; |
| 2547 |
} |
| 2548 |
} |
| 2549 |
} |
| 2550 |
return 1; |
| 2551 |
} |
| 2552 |
|
| 2553 |
bool SchemeParser::doMultiCollatingElement() |
| 2554 |
{ |
| 2555 |
Token tok; |
| 2556 |
if (!getToken(allowIdentifier, tok)) |
| 2557 |
return 0; |
| 2558 |
StringC sym(currentToken_); |
| 2559 |
if (!getToken(allowString, tok)) |
| 2560 |
return 0; |
| 2561 |
StringC str(currentToken_); |
| 2562 |
if (!getToken(allowCloseParen, tok)) |
| 2563 |
return 0; |
| 2564 |
lang_->addMultiCollatingElement(sym, str); |
| 2565 |
return 1; |
| 2566 |
} |
| 2567 |
|
| 2568 |
bool SchemeParser::doCollatingSymbol() |
| 2569 |
{ |
| 2570 |
Token tok; |
| 2571 |
if (!getToken(allowIdentifier, tok)) |
| 2572 |
return 0; |
| 2573 |
StringC sym(currentToken_); |
| 2574 |
if (!getToken(allowCloseParen, tok)) |
| 2575 |
return 0; |
| 2576 |
lang_->addCollatingSymbol(sym); |
| 2577 |
return 1; |
| 2578 |
} |
| 2579 |
|
| 2580 |
bool SchemeParser::doCollatingOrder() |
| 2581 |
{ |
| 2582 |
Token tok; |
| 2583 |
if (!getToken(allowOpenParen, tok)) |
| 2584 |
return 0; |
| 2585 |
int nested = 0; |
| 2586 |
LangObj::LevelSort sort = { 0, 0, 0}; |
| 2587 |
for (;;) { |
| 2588 |
if (!getToken(((nested == 0) ? allowOpenParen : 0)| |
| 2589 |
allowCloseParen|allowIdentifier, tok)) |
| 2590 |
return 0; |
| 2591 |
if (tok == tokenOpenParen) |
| 2592 |
nested++; |
| 2593 |
else if (tok == tokenCloseParen) |
| 2594 |
nested--; |
| 2595 |
else { |
| 2596 |
const Identifier *ident = lookup(currentToken_); |
| 2597 |
Identifier::SyntacticKey key; |
| 2598 |
if (!ident->syntacticKey(key)) |
| 2599 |
return 0; |
| 2600 |
switch (key) { |
| 2601 |
case Identifier::keyForward: |
| 2602 |
if (sort.backward) |
| 2603 |
return 0; |
| 2604 |
sort.forward = 1; |
| 2605 |
break; |
| 2606 |
case Identifier::keyBackward: |
| 2607 |
if (sort.forward) |
| 2608 |
return 0; |
| 2609 |
sort.backward = 1; |
| 2610 |
break; |
| 2611 |
case Identifier::keyPosition: |
| 2612 |
sort.position = 1; |
| 2613 |
break; |
| 2614 |
default: |
| 2615 |
return 0; |
| 2616 |
} |
| 2617 |
} |
| 2618 |
if (nested < 0) |
| 2619 |
break; |
| 2620 |
if (nested == 0) { |
| 2621 |
if (!sort.backward) |
| 2622 |
sort.forward = 1; |
| 2623 |
lang_->addLevel(sort); |
| 2624 |
} |
| 2625 |
} |
| 2626 |
for (;;) { |
| 2627 |
if (!getToken(allowOpenParen| |
| 2628 |
allowCloseParen| |
| 2629 |
allowIdentifier| |
| 2630 |
allowOtherExpr, tok)) |
| 2631 |
return 0; |
| 2632 |
if (tok == tokenCloseParen) |
| 2633 |
break; |
| 2634 |
StringC empty; |
| 2635 |
switch (tok) { |
| 2636 |
case tokenTrue: |
| 2637 |
lang_->addDefaultPos(); |
| 2638 |
for(Char i = 0; i < lang_->levels(); i++) |
| 2639 |
lang_->addLevelWeight(i, empty); |
| 2640 |
break; |
| 2641 |
case tokenIdentifier: |
| 2642 |
case tokenChar: |
| 2643 |
if (!lang_->addCollatingPos(currentToken_)) |
| 2644 |
return 0; |
| 2645 |
for (unsigned i = 0; i < lang_->levels(); i++) |
| 2646 |
lang_->addLevelWeight(i, currentToken_); |
| 2647 |
break; |
| 2648 |
case tokenOpenParen: |
| 2649 |
if (!doWeights()) |
| 2650 |
return 0; |
| 2651 |
break; |
| 2652 |
default: |
| 2653 |
return 0; |
| 2654 |
} |
| 2655 |
} |
| 2656 |
return 1; |
| 2657 |
} |
| 2658 |
|
| 2659 |
bool SchemeParser::doWeights() |
| 2660 |
{ |
| 2661 |
Token tok; |
| 2662 |
if (!getToken(allowIdentifier|allowOtherExpr, tok)) |
| 2663 |
return 0; |
| 2664 |
StringC sym(currentToken_); |
| 2665 |
if (!lang_->addCollatingPos(sym)) |
| 2666 |
return 0; |
| 2667 |
int nested = 0; |
| 2668 |
unsigned l = 0; |
| 2669 |
for (;;) { |
| 2670 |
if (!getToken((nested ? 0 : allowOpenParen)| |
| 2671 |
allowCloseParen| |
| 2672 |
allowIdentifier| |
| 2673 |
allowOtherExpr| |
| 2674 |
allowString, tok)) |
| 2675 |
return 0; |
| 2676 |
if (tok == tokenOpenParen) |
| 2677 |
nested++; |
| 2678 |
else if (tok == tokenCloseParen) |
| 2679 |
nested--; |
| 2680 |
else { |
| 2681 |
switch (tok) { |
| 2682 |
case tokenString: |
| 2683 |
for (size_t i = 0; i < currentToken_.size(); i++) { |
| 2684 |
StringC ctok(&(currentToken_[i]), 1); |
| 2685 |
if (!lang_->addLevelWeight(l, ctok)) |
| 2686 |
return 0; |
| 2687 |
} |
| 2688 |
break; |
| 2689 |
case tokenIdentifier: |
| 2690 |
case tokenChar: |
| 2691 |
if (!lang_->addLevelWeight(l, currentToken_)) |
| 2692 |
return 0; |
| 2693 |
break; |
| 2694 |
default: |
| 2695 |
return 0; |
| 2696 |
} |
| 2697 |
} |
| 2698 |
if (nested < 0) |
| 2699 |
break; |
| 2700 |
if (nested == 0) |
| 2701 |
l++; |
| 2702 |
} |
| 2703 |
return 1; |
| 2704 |
} |
| 2705 |
|
| 2706 |
bool SchemeParser::doToupper() |
| 2707 |
{ |
| 2708 |
Token tok; |
| 2709 |
for (;;) { |
| 2710 |
if (!getToken(allowOpenParen|allowCloseParen, tok)) |
| 2711 |
return 0; |
| 2712 |
if (tok == tokenCloseParen) break; |
| 2713 |
if (!getToken(allowOtherExpr, tok) || (tok != tokenChar)) |
| 2714 |
return 0; |
| 2715 |
Char lc = currentToken_[0]; |
| 2716 |
if (!getToken(allowOtherExpr, tok) || (tok != tokenChar)) |
| 2717 |
return 0; |
| 2718 |
Char uc = currentToken_[0]; |
| 2719 |
if (!getToken(allowCloseParen, tok)) |
| 2720 |
return 0; |
| 2721 |
lang_->addToupper(lc, uc); |
| 2722 |
} |
| 2723 |
return 1; |
| 2724 |
} |
| 2725 |
|
| 2726 |
bool SchemeParser::doTolower() |
| 2727 |
{ |
| 2728 |
Token tok; |
| 2729 |
for (;;) { |
| 2730 |
if (!getToken(allowOpenParen|allowCloseParen, tok)) |
| 2731 |
return 0; |
| 2732 |
if (tok == tokenCloseParen) break; |
| 2733 |
if (!getToken(allowOtherExpr, tok) || (tok != tokenChar)) |
| 2734 |
return 0; |
| 2735 |
Char uc = currentToken_[0]; |
| 2736 |
if (!getToken(allowOtherExpr, tok) || (tok != tokenChar)) |
| 2737 |
return 0; |
| 2738 |
Char lc = currentToken_[0]; |
| 2739 |
if (!getToken(allowCloseParen, tok)) |
| 2740 |
return 0; |
| 2741 |
lang_->addTolower(uc, lc); |
| 2742 |
} |
| 2743 |
return 1; |
| 2744 |
} |
| 2745 |
|
| 2746 |
bool SchemeParser::parseSpecialQuery(Owner<Expression> &rexp, const char *query) |
| 2747 |
{ |
| 2748 |
Location loc(in_->currentLocation()); |
| 2749 |
interp_->requireFeature(Interpreter::query, loc); |
| 2750 |
Token tok; |
| 2751 |
if (!getToken(allowIdentifier, tok)) |
| 2752 |
return 0; |
| 2753 |
Vector<const Identifier *> vars; |
| 2754 |
vars.push_back(lookup(currentToken_)); |
| 2755 |
Identifier::SyntacticKey key; |
| 2756 |
if (vars.back()->syntacticKey(key) && key <= int(Identifier::lastSyntacticKey)) |
| 2757 |
message(InterpreterMessages::syntacticKeywordAsVariable, |
| 2758 |
StringMessageArg(currentToken_)); |
| 2759 |
|
| 2760 |
Owner<Expression> op(new ConstantExpression( |
| 2761 |
interp_->lookup(interp_->makeStringC(query))->computeBuiltinValue(1, *interp_), |
| 2762 |
loc)); |
| 2763 |
NCVector<Owner<Expression> > inits, args(2); |
| 2764 |
Owner<Expression> expr; |
| 2765 |
if (!parseExpression(0, args[1], key, tok) |
| 2766 |
|| !parseExpression(0, expr, key, tok) |
| 2767 |
|| !getToken(allowCloseParen, tok)) |
| 2768 |
return 0; |
| 2769 |
args[0] = new LambdaExpression(vars, inits, 0, 0, 0, expr, loc); |
| 2770 |
rexp = new CallExpression(op, args, loc); |
| 2771 |
return 1; |
| 2772 |
} |
| 2773 |
|
| 2774 |
bool SchemeParser::doDeclareCharProperty() |
| 2775 |
{ |
| 2776 |
Location loc(in_->currentLocation()); |
| 2777 |
Token tok; |
| 2778 |
if (!getToken(allowIdentifier, tok)) |
| 2779 |
return 0; |
| 2780 |
CharProp *cp = interp_->lookupCharProperty (currentToken_); |
| 2781 |
Owner<Expression> expr; |
| 2782 |
Identifier::SyntacticKey key; |
| 2783 |
if (!parseExpression(0, expr, key, tok)) |
| 2784 |
return 0; |
| 2785 |
if (!getToken(allowCloseParen, tok)) |
| 2786 |
return 0; |
| 2787 |
Location defLoc; |
| 2788 |
unsigned defPart; |
| 2789 |
if (cp->declared(defPart, defLoc) |
| 2790 |
&& defPart <= interp_->currentPartIndex()) { |
| 2791 |
if (defPart == interp_->currentPartIndex()) |
| 2792 |
message(InterpreterMessages::duplicateCharPropertyDecl, |
| 2793 |
StringMessageArg(cp->name()), defLoc); |
| 2794 |
} |
| 2795 |
cp->declare(expr, interp_->currentPartIndex(), loc); |
| 2796 |
return 1; |
| 2797 |
} |
| 2798 |
|
| 2799 |
bool SchemeParser::doAddCharProperties() |
| 2800 |
{ |
| 2801 |
Location loc (in_->currentLocation()); |
| 2802 |
NCVector<Owner<Expression> > exprs; |
| 2803 |
Vector<CharProp *> cps; |
| 2804 |
Token tok; |
| 2805 |
for (;;) { |
| 2806 |
if (!getToken(allowKeyword|allowOtherExpr, tok)) |
| 2807 |
return 0; |
| 2808 |
if (tok != tokenKeyword) |
| 2809 |
break; |
| 2810 |
cps.push_back(interp_->lookupCharProperty(currentToken_)); |
| 2811 |
exprs.resize(exprs.size() + 1); |
| 2812 |
Identifier::SyntacticKey key; |
| 2813 |
if (!parseExpression(0, exprs.back(), key, tok)) |
| 2814 |
return 0; |
| 2815 |
} |
| 2816 |
|
| 2817 |
StringC chars; |
| 2818 |
for (;;) { |
| 2819 |
if (tok != tokenChar) { |
| 2820 |
message(InterpreterMessages::badAddCharProperty); |
| 2821 |
return 0; |
| 2822 |
} |
| 2823 |
// FIXME: Check duplicates in same decl. Silently remove them or signal |
| 2824 |
// errors? |
| 2825 |
chars += currentToken_[0]; |
| 2826 |
if (!getToken(allowOtherExpr|allowCloseParen, tok)) |
| 2827 |
return 0; |
| 2828 |
if (tok==tokenCloseParen) |
| 2829 |
break; |
| 2830 |
} |
| 2831 |
|
| 2832 |
unsigned defPart; |
| 2833 |
Location defLoc; |
| 2834 |
for (size_t i = 0; i < cps.size(); i++) { |
| 2835 |
StringC chars2; |
| 2836 |
for (size_t j = 0; j < chars.size(); j++) { |
| 2837 |
if (cps[i]->hasAddedValue (chars[j], defPart, defLoc) |
| 2838 |
&& defPart <= interp_->currentPartIndex()) { |
| 2839 |
if (defPart == interp_->currentPartIndex()) { |
| 2840 |
message(InterpreterMessages::duplicateAddCharProperty, |
| 2841 |
StringMessageArg(cps[i]->name()), defLoc); |
| 2842 |
} |
| 2843 |
} |
| 2844 |
else |
| 2845 |
chars2 += chars[j]; |
| 2846 |
} |
| 2847 |
if (chars2.size() > 0) |
| 2848 |
cps[i]->setValue(chars2, exprs[i], interp_->currentPartIndex(), loc); |
| 2849 |
} |
| 2850 |
return 1; |
| 2851 |
} |
| 2852 |
|
| 2853 |
#ifdef DSSSL_NAMESPACE |
| 2854 |
} |
| 2855 |
#endif |