| 1 |
#charset "us-ascii" |
| 2 |
|
| 3 |
/* |
| 4 |
* Copyright (c) 2000, 2006 Michael J. Roberts. All Rights Reserved. |
| 5 |
* |
| 6 |
* TADS 3 Library - output formatting |
| 7 |
* |
| 8 |
* This module defines the framework for displaying output text. |
| 9 |
*/ |
| 10 |
|
| 11 |
/* include the library header */ |
| 12 |
#include "adv3.h" |
| 13 |
|
| 14 |
|
| 15 |
/* ------------------------------------------------------------------------ */ |
| 16 |
/* |
| 17 |
* The standard library output function. We set this up as the default |
| 18 |
* display function (for double-quoted strings and for "<< >>" |
| 19 |
* embeddings). Code can also call this directly to display items. |
| 20 |
*/ |
| 21 |
say(val) |
| 22 |
{ |
| 23 |
/* send output to the active output stream */ |
| 24 |
outputManager.curOutputStream.writeToStream(val); |
| 25 |
} |
| 26 |
|
| 27 |
/* ------------------------------------------------------------------------ */ |
| 28 |
/* |
| 29 |
* Generate a string for showing quoted text. We simply enclose the |
| 30 |
* text in a <Q>...</Q> tag sequence and return the result. |
| 31 |
*/ |
| 32 |
withQuotes(txt) |
| 33 |
{ |
| 34 |
return '<q><<txt>></q>'; |
| 35 |
} |
| 36 |
|
| 37 |
/* ------------------------------------------------------------------------ */ |
| 38 |
/* |
| 39 |
* Output Manager. This object contains global code for displaying text |
| 40 |
* on the console. |
| 41 |
* |
| 42 |
* The output manager is transient because we don't want its state to be |
| 43 |
* saved and restored; the output manager state is essentially part of |
| 44 |
* the interpreter user interface, which is not affected by save and |
| 45 |
* restore. |
| 46 |
*/ |
| 47 |
transient outputManager: object |
| 48 |
/* |
| 49 |
* Switch to a new active output stream. Returns the previously |
| 50 |
* active output stream, so that the caller can easily restore the |
| 51 |
* old output stream if the new output stream is to be established |
| 52 |
* only for a specific duration. |
| 53 |
*/ |
| 54 |
setOutputStream(ostr) |
| 55 |
{ |
| 56 |
local oldStr; |
| 57 |
|
| 58 |
/* remember the old stream for a moment */ |
| 59 |
oldStr = curOutputStream; |
| 60 |
|
| 61 |
/* set the new output stream */ |
| 62 |
curOutputStream = ostr; |
| 63 |
|
| 64 |
/* |
| 65 |
* return the old stream, so the caller can restore it later if |
| 66 |
* desired |
| 67 |
*/ |
| 68 |
return oldStr; |
| 69 |
} |
| 70 |
|
| 71 |
/* |
| 72 |
* run the given function, using the given output stream as the |
| 73 |
* active default output stream |
| 74 |
*/ |
| 75 |
withOutputStream(ostr, func) |
| 76 |
{ |
| 77 |
/* establish the new stream */ |
| 78 |
local oldStr = setOutputStream(ostr); |
| 79 |
|
| 80 |
/* make sure we restore the old active stream on the way out */ |
| 81 |
try |
| 82 |
{ |
| 83 |
/* invoke the callback */ |
| 84 |
(func)(); |
| 85 |
} |
| 86 |
finally |
| 87 |
{ |
| 88 |
/* restore the old output stream */ |
| 89 |
setOutputStream(oldStr); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
/* the current output stream - start with the main text stream */ |
| 94 |
curOutputStream = mainOutputStream |
| 95 |
|
| 96 |
/* |
| 97 |
* Is the UI running in HTML mode? This tells us if we have a full |
| 98 |
* HTML UI or a text-only UI. Full HTML mode applies if we're |
| 99 |
* running on a Multimedia TADS interpreter, or we're using the Web |
| 100 |
* UI, which runs in a separate browser and is thus inherently |
| 101 |
* HTML-capable. |
| 102 |
* |
| 103 |
* (The result can't change during a session, since it's a function |
| 104 |
* of the game and interpreter capabilities, so we store the result |
| 105 |
* on the first evaluation to avoid having to recompute it on each |
| 106 |
* query. Since 'self' is a static object, we'll recompute this each |
| 107 |
* time we run the program, which is important because we could save |
| 108 |
* the game on one interpreter and resume the session on a different |
| 109 |
* interpreter with different capabilities.) |
| 110 |
*/ |
| 111 |
htmlMode = (self.htmlMode = checkHtmlMode()) |
| 112 |
; |
| 113 |
|
| 114 |
/* ------------------------------------------------------------------------ */ |
| 115 |
/* |
| 116 |
* Output Stream. This class provides a stream-oriented interface to |
| 117 |
* displaying text on the console. "Stream-oriented" means that we write |
| 118 |
* text as a sequential string of characters. |
| 119 |
* |
| 120 |
* Output streams are always transient, since they track the system user |
| 121 |
* interface in the interpreter. The interpreter does not save its UI |
| 122 |
* state with a saved position, so objects such as output streams that |
| 123 |
* track the UI state should not be saved either. |
| 124 |
*/ |
| 125 |
class OutputStream: PreinitObject |
| 126 |
/* |
| 127 |
* Write a value to the stream. If the value is a string, we'll |
| 128 |
* display the text of the string; if it's nil, we'll ignore it; if |
| 129 |
* it's anything else, we'll try to convert it to a string (with the |
| 130 |
* toString() function) and display the resulting text. |
| 131 |
*/ |
| 132 |
writeToStream(val) |
| 133 |
{ |
| 134 |
/* convert the value to a string */ |
| 135 |
switch(dataType(val)) |
| 136 |
{ |
| 137 |
case TypeSString: |
| 138 |
/* |
| 139 |
* it's a string - no conversion is needed, but if it's |
| 140 |
* empty, it doesn't count as real output (so don't notify |
| 141 |
* anyone, and don't set any output flags) |
| 142 |
*/ |
| 143 |
if (val == '') |
| 144 |
return; |
| 145 |
break; |
| 146 |
|
| 147 |
case TypeNil: |
| 148 |
/* nil - don't display anything for this */ |
| 149 |
return; |
| 150 |
|
| 151 |
case TypeInt: |
| 152 |
case TypeObject: |
| 153 |
/* convert integers and objects to strings */ |
| 154 |
val = toString(val); |
| 155 |
break; |
| 156 |
} |
| 157 |
|
| 158 |
/* run it through our output filters */ |
| 159 |
val = applyFilters(val); |
| 160 |
|
| 161 |
/* |
| 162 |
* if, after filtering, we're not writing anything at all, |
| 163 |
* there's nothing left to do |
| 164 |
*/ |
| 165 |
if (val == nil || val == '') |
| 166 |
return; |
| 167 |
|
| 168 |
/* write the text to our underlying system stream */ |
| 169 |
writeFromStream(val); |
| 170 |
} |
| 171 |
|
| 172 |
/* |
| 173 |
* Watch the stream for output. It's sometimes useful to be able to |
| 174 |
* call out to some code and determine whether or not the code |
| 175 |
* generated any text output. This routine invokes the given |
| 176 |
* callback function, monitoring the stream for output; if any |
| 177 |
* occurs, we'll return true, otherwise we'll return nil. |
| 178 |
*/ |
| 179 |
watchForOutput(func) |
| 180 |
{ |
| 181 |
local mon; |
| 182 |
|
| 183 |
/* set up a monitor filter on the stream */ |
| 184 |
addOutputFilter(mon = new MonitorFilter()); |
| 185 |
|
| 186 |
/* catch any exceptions so we can remove our filter before leaving */ |
| 187 |
try |
| 188 |
{ |
| 189 |
/* invoke the callback */ |
| 190 |
(func)(); |
| 191 |
|
| 192 |
/* return the monitor's status, indicating if output occurred */ |
| 193 |
return mon.outputFlag; |
| 194 |
} |
| 195 |
finally |
| 196 |
{ |
| 197 |
/* remove our monitor filter */ |
| 198 |
removeOutputFilter(mon); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
/* |
| 203 |
* Call the given function, capturing all text output to this stream |
| 204 |
* in the course of the function call. Return a string containing |
| 205 |
* the captured text. |
| 206 |
*/ |
| 207 |
captureOutput(func, [args]) |
| 208 |
{ |
| 209 |
/* install a string capture filter */ |
| 210 |
local filter = new StringCaptureFilter(); |
| 211 |
addOutputFilter(filter); |
| 212 |
|
| 213 |
/* make sure we don't leave without removing our capturer */ |
| 214 |
try |
| 215 |
{ |
| 216 |
/* invoke the function */ |
| 217 |
(func)(args...); |
| 218 |
|
| 219 |
/* return the text that we captured */ |
| 220 |
return filter.txt_; |
| 221 |
} |
| 222 |
finally |
| 223 |
{ |
| 224 |
/* we're done with our filter, so remove it */ |
| 225 |
removeOutputFilter(filter); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
/* my associated input manager, if I have one */ |
| 230 |
myInputManager = nil |
| 231 |
|
| 232 |
/* dynamic construction */ |
| 233 |
construct() |
| 234 |
{ |
| 235 |
/* |
| 236 |
* Set up filter list. Output streams are always transient, so |
| 237 |
* make our filter list transient as well. |
| 238 |
*/ |
| 239 |
filterList_ = new transient Vector(10); |
| 240 |
} |
| 241 |
|
| 242 |
/* execute pre-initialization */ |
| 243 |
execute() |
| 244 |
{ |
| 245 |
/* do the same set-up we would do for dynamic construction */ |
| 246 |
construct(); |
| 247 |
} |
| 248 |
|
| 249 |
/* |
| 250 |
* Write text out from this stream; this writes to the lower-level |
| 251 |
* stream underlying this stream. This routine is intended to be |
| 252 |
* called only from within this class. |
| 253 |
* |
| 254 |
* Each output stream is conceptually "stacked" on top of another, |
| 255 |
* lower-level stream. At the bottom of the stack is usually some |
| 256 |
* kind of physical device, such as the display, or a file on disk. |
| 257 |
* |
| 258 |
* This method must be defined in each subclass to write to the |
| 259 |
* appropriate underlying stream. Most subclasses are specifically |
| 260 |
* designed to sit atop a system-level stream, such as the display |
| 261 |
* output stream, so most implementations of this method will call |
| 262 |
* directly to a system-level output function. |
| 263 |
*/ |
| 264 |
writeFromStream(txt) { } |
| 265 |
|
| 266 |
/* |
| 267 |
* The list of active filters on this stream, in the order in which |
| 268 |
* they are to be called. This should normally be initialized to a |
| 269 |
* Vector in each instance. |
| 270 |
*/ |
| 271 |
filterList_ = [] |
| 272 |
|
| 273 |
/* |
| 274 |
* Add an output filter. The argument is an object of class |
| 275 |
* OutputFilter, or any object implementing the filterText() method. |
| 276 |
* |
| 277 |
* Filters are always arranged in a "stack": the last output filter |
| 278 |
* added is the first one called during output. This method thus |
| 279 |
* adds the new filter at the "top" of the stack. |
| 280 |
*/ |
| 281 |
addOutputFilter(filter) |
| 282 |
{ |
| 283 |
/* add the filter to the end of our list */ |
| 284 |
filterList_.append(filter); |
| 285 |
} |
| 286 |
|
| 287 |
/* |
| 288 |
* Add an output filter at a given point in the filter stack: add |
| 289 |
* the filter so that it is "below" the given existing filter in the |
| 290 |
* stack. This means that the new filter will be called just after |
| 291 |
* the existing filter during output. |
| 292 |
* |
| 293 |
* If 'existingFilter' isn't in the stack of existing filters, we'll |
| 294 |
* add the new filter at the "top" of the stack. |
| 295 |
*/ |
| 296 |
addOutputFilterBelow(newFilter, existingFilter) |
| 297 |
{ |
| 298 |
/* find the existing filter in our list */ |
| 299 |
local idx = filterList_.indexOf(existingFilter); |
| 300 |
|
| 301 |
/* |
| 302 |
* If we found the old filter, add the new filter below the |
| 303 |
* existing filter in the stack, which is to say just before the |
| 304 |
* old filter in our vector of filters (since we call the |
| 305 |
* filters in reverse order of the list). |
| 306 |
* |
| 307 |
* If we didn't find the existing filter, simply add the new |
| 308 |
* filter at the top of the stack, by appending the new filter |
| 309 |
* at the end of the list. |
| 310 |
*/ |
| 311 |
if (idx != nil) |
| 312 |
filterList_.insertAt(idx, newFilter); |
| 313 |
else |
| 314 |
filterList_.append(newFilter); |
| 315 |
} |
| 316 |
|
| 317 |
/* |
| 318 |
* Remove an output filter. Since filters are arranged in a stack, |
| 319 |
* only the LAST output filter added may be removed. It's an error |
| 320 |
* to remove a filter other than the last one. |
| 321 |
*/ |
| 322 |
removeOutputFilter(filter) |
| 323 |
{ |
| 324 |
/* get the filter count */ |
| 325 |
local len = filterList_.length(); |
| 326 |
|
| 327 |
/* make sure it's the last filter */ |
| 328 |
if (len == 0 || filterList_[len] != filter) |
| 329 |
t3DebugTrace(T3DebugBreak); |
| 330 |
|
| 331 |
/* remove the filter from my list */ |
| 332 |
filterList_.removeElementAt(len); |
| 333 |
} |
| 334 |
|
| 335 |
/* call the filters */ |
| 336 |
applyFilters(val) |
| 337 |
{ |
| 338 |
/* |
| 339 |
* Run through the list, applying each filter in turn. We work |
| 340 |
* backwards through the list from the last element, because the |
| 341 |
* filter list is a stack: the last element added is the topmost |
| 342 |
* element of the stack, so it must be called first. |
| 343 |
*/ |
| 344 |
for (local i in filterList_.length()..1 step -1 ; val != nil ; ) |
| 345 |
val = filterList_[i].filterText(self, val); |
| 346 |
|
| 347 |
/* return the result of all of the filters */ |
| 348 |
return val; |
| 349 |
} |
| 350 |
|
| 351 |
/* |
| 352 |
* Apply the current set of text transformation filters to a string. |
| 353 |
* This applies only the non-capturing filters; we skip any capture |
| 354 |
* filters. |
| 355 |
*/ |
| 356 |
applyTextFilters(val) |
| 357 |
{ |
| 358 |
/* run through the filter stack from top to bottom */ |
| 359 |
for (local i in filterList_.length()..1 step -1 ; val != nil ; ) |
| 360 |
{ |
| 361 |
/* skip capturing filters */ |
| 362 |
local f = filterList_[i]; |
| 363 |
if (f.ofKind(CaptureFilter)) |
| 364 |
continue; |
| 365 |
|
| 366 |
/* apply the filter */ |
| 367 |
val = f.filterText(self, val); |
| 368 |
} |
| 369 |
|
| 370 |
/* return the result */ |
| 371 |
return val; |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
/* |
| 376 |
* Receive notification from the input manager that we have just |
| 377 |
* ended reading a line of input from the keyboard. |
| 378 |
*/ |
| 379 |
inputLineEnd() |
| 380 |
{ |
| 381 |
/* an input line ending doesn't look like a paragraph */ |
| 382 |
justDidPara = nil; |
| 383 |
} |
| 384 |
|
| 385 |
/* |
| 386 |
* Internal state: we just wrote a paragraph break, and there has |
| 387 |
* not yet been any intervening text. By default, we set this to |
| 388 |
* true initially, so that we suppress any paragraph breaks at the |
| 389 |
* very start of the text. |
| 390 |
*/ |
| 391 |
justDidPara = true |
| 392 |
|
| 393 |
/* |
| 394 |
* Internal state: we just wrote a character that suppresses |
| 395 |
* paragraph breaks that immediately follow. In this state, we'll |
| 396 |
* suppress any paragraph marker that immediately follows, but we |
| 397 |
* won't suppress any other characters. |
| 398 |
*/ |
| 399 |
justDidParaSuppressor = nil |
| 400 |
; |
| 401 |
|
| 402 |
/* |
| 403 |
* The OutputStream for the main text area. |
| 404 |
* |
| 405 |
* This object is transient because the output stream state is |
| 406 |
* effectively part of the interpreter user interface, which is not |
| 407 |
* affected by save and restore. |
| 408 |
*/ |
| 409 |
transient mainOutputStream: OutputStream |
| 410 |
/* |
| 411 |
* The main text area is the same place where we normally read |
| 412 |
* command lines from the keyboard, so associate this output stream |
| 413 |
* with the primary input manager. |
| 414 |
*/ |
| 415 |
myInputManager = inputManager |
| 416 |
|
| 417 |
/* the current command transcript */ |
| 418 |
curTranscript = nil |
| 419 |
|
| 420 |
/* we sit atop the system-level main console output stream */ |
| 421 |
writeFromStream(txt) |
| 422 |
{ |
| 423 |
/* if an input event was interrupted, cancel the event */ |
| 424 |
inputManager.inputEventEnd(); |
| 425 |
|
| 426 |
/* write the text to the console */ |
| 427 |
aioSay(txt); |
| 428 |
} |
| 429 |
; |
| 430 |
|
| 431 |
/* ------------------------------------------------------------------------ */ |
| 432 |
/* |
| 433 |
* Paragraph manager. We filter strings as they're about to be sent to |
| 434 |
* the console to convert paragraph markers (represented in the source |
| 435 |
* text using the "style tag" format, <.P>) into a configurable display |
| 436 |
* rendering. |
| 437 |
* |
| 438 |
* We also process the zero-spacing paragraph, <.P0>. This doesn't |
| 439 |
* generate any output, but otherwise acts like a paragraph break in that |
| 440 |
* it suppresses any paragraph breaks that immediately follow. |
| 441 |
* |
| 442 |
* The special marker <./P0> cancels the effect of a <.P0>. This can be |
| 443 |
* used if you want to ensure that a newline or paragraph break is |
| 444 |
* displayed, even if a <.P0> was just displayed. |
| 445 |
* |
| 446 |
* Our special processing ensures that paragraph tags interact with one |
| 447 |
* another and with other display elements specially: |
| 448 |
* |
| 449 |
* - A run of multiple consecutive paragraph tags is treated as a single |
| 450 |
* paragraph tag. This property is particularly important because it |
| 451 |
* allows code to write out a paragraph marker without having to worry |
| 452 |
* about whether preceding code or following code add paragraph markers |
| 453 |
* of their own; if redundant markers are found, we'll filter them out |
| 454 |
* automatically. |
| 455 |
* |
| 456 |
* - We can suppress paragraph markers following other specific |
| 457 |
* sequences. For example, if the paragraph break is rendered as a blank |
| 458 |
* line, we might want to suppress an extra blank line for a paragraph |
| 459 |
* break after an explicit blank line. |
| 460 |
* |
| 461 |
* - We can suppress other specific sequences following a paragraph |
| 462 |
* marker. For example, if the paragraph break is rendered as a newline |
| 463 |
* plus a tab, we could suppress whitespace following the paragraph |
| 464 |
* break. |
| 465 |
* |
| 466 |
* The paragraph manager should always be instantiated with transient |
| 467 |
* instances, because this object's state is effectively part of the |
| 468 |
* interpreter user interface, which doesn't participate in save and |
| 469 |
* restore. |
| 470 |
*/ |
| 471 |
class ParagraphManager: OutputFilter |
| 472 |
/* |
| 473 |
* Rendering - this is what we display on the console to represent a |
| 474 |
* paragraph break. By default, we'll display a blank line. |
| 475 |
*/ |
| 476 |
renderText = '\b' |
| 477 |
|
| 478 |
/* |
| 479 |
* Flag: show or hide paragraph breaks immediately after input. By |
| 480 |
* default, we do not show paragraph breaks after an input line. |
| 481 |
*/ |
| 482 |
renderAfterInput = nil |
| 483 |
|
| 484 |
/* |
| 485 |
* Preceding suppression. This is a regular expression that we |
| 486 |
* match to individual characters. If the character immediately |
| 487 |
* preceding a paragraph marker matches this expression, we'll |
| 488 |
* suppress the paragraph marker in the output. By default, we'll |
| 489 |
* suppress a paragraph break following a blank line, because the |
| 490 |
* default rendering would add a redundant blank line. |
| 491 |
*/ |
| 492 |
suppressBefore = static new RexPattern('\b') |
| 493 |
|
| 494 |
/* |
| 495 |
* Following suppression. This is a regular expression that we |
| 496 |
* match to individual characters. If the character immediately |
| 497 |
* following a paragraph marker matches this expression, we'll |
| 498 |
* suppress the character. We'll apply this to each character |
| 499 |
* following a paragraph marker in turn until we find one that does |
| 500 |
* not match; we'll suppress all of the characters that do match. |
| 501 |
* By default, we suppress additional blank lines after a paragraph |
| 502 |
* break. |
| 503 |
*/ |
| 504 |
suppressAfter = static new RexPattern('[\b\n]') |
| 505 |
|
| 506 |
/* pre-compile some regular expression patterns we use a lot */ |
| 507 |
leadingMultiPat = static new RexPattern('(<langle><dot>[pP]0?<rangle>)+') |
| 508 |
leadingSinglePat = static new RexPattern( |
| 509 |
'<langle><dot>([pP]0?|/[pP]0)<rangle>') |
| 510 |
|
| 511 |
/* process a string that's about to be written to the console */ |
| 512 |
filterText(ostr, txt) |
| 513 |
{ |
| 514 |
local ret; |
| 515 |
|
| 516 |
/* we don't have anything in our translated string yet */ |
| 517 |
ret = ''; |
| 518 |
|
| 519 |
/* keep going until we run out of string to process */ |
| 520 |
while (txt != '') |
| 521 |
{ |
| 522 |
local len; |
| 523 |
local match; |
| 524 |
local p0; |
| 525 |
local unp0; |
| 526 |
|
| 527 |
/* |
| 528 |
* if we just wrote a paragraph break, suppress any |
| 529 |
* character that matches 'suppressAfter', and suppress any |
| 530 |
* paragraph markers that immediately follow |
| 531 |
*/ |
| 532 |
if (ostr.justDidPara) |
| 533 |
{ |
| 534 |
/* check for any consecutive paragraph markers */ |
| 535 |
if ((len = rexMatch(leadingMultiPat, txt)) != nil) |
| 536 |
{ |
| 537 |
/* discard the consecutive <.P>'s, and keep going */ |
| 538 |
txt = txt.substr(len + 1); |
| 539 |
continue; |
| 540 |
} |
| 541 |
|
| 542 |
/* check for a match to the suppressAfter pattern */ |
| 543 |
if (rexMatch(suppressAfter, txt) != nil) |
| 544 |
{ |
| 545 |
/* discard the suppressed character and keep going */ |
| 546 |
txt = txt.substr(2); |
| 547 |
continue; |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
/* |
| 552 |
* we have a character other than a paragraph marker, so we |
| 553 |
* didn't just scan a paragraph marker |
| 554 |
*/ |
| 555 |
ostr.justDidPara = nil; |
| 556 |
|
| 557 |
/* |
| 558 |
* if we just wrote a suppressBefore character, discard any |
| 559 |
* leading paragraph markers |
| 560 |
*/ |
| 561 |
if (ostr.justDidParaSuppressor |
| 562 |
&& (len = rexMatch(leadingMultiPat, txt)) != nil) |
| 563 |
{ |
| 564 |
/* remove the paragraph markers */ |
| 565 |
txt = txt.substr(len + 1); |
| 566 |
|
| 567 |
/* |
| 568 |
* even though we're not rendering the paragraph, note |
| 569 |
* that a logical paragraph just started |
| 570 |
*/ |
| 571 |
ostr.justDidPara = true; |
| 572 |
|
| 573 |
/* keep going */ |
| 574 |
continue; |
| 575 |
} |
| 576 |
|
| 577 |
/* presume we won't find a <.p0> or <./p0> */ |
| 578 |
p0 = unp0 = nil; |
| 579 |
|
| 580 |
/* find the next paragraph marker */ |
| 581 |
match = rexSearch(leadingSinglePat, txt); |
| 582 |
if (match == nil) |
| 583 |
{ |
| 584 |
/* |
| 585 |
* there are no more paragraph markers - copy the |
| 586 |
* remainder of the input string to the output |
| 587 |
*/ |
| 588 |
ret += txt; |
| 589 |
txt = ''; |
| 590 |
|
| 591 |
/* we just did something other than a paragraph */ |
| 592 |
ostr.justDidPara = nil; |
| 593 |
} |
| 594 |
else |
| 595 |
{ |
| 596 |
/* add everything up to the paragraph break to the output */ |
| 597 |
ret += txt.substr(1, match[1] - 1); |
| 598 |
|
| 599 |
/* get the rest of the string following the paragraph mark */ |
| 600 |
txt = txt.substr(match[1] + match[2]); |
| 601 |
|
| 602 |
/* note if we found a <.p0> or <./p0> */ |
| 603 |
p0 = (match[3] is in ('<.p0>', '<.P0>')); |
| 604 |
unp0 = (match[3] is in ('<./p0>', '<./P0>')); |
| 605 |
|
| 606 |
/* |
| 607 |
* note that we just found a paragraph marker, unless |
| 608 |
* this is a <./p0> |
| 609 |
*/ |
| 610 |
ostr.justDidPara = !unp0; |
| 611 |
} |
| 612 |
|
| 613 |
/* |
| 614 |
* If the last character we copied out is a suppressBefore |
| 615 |
* character, note for next time that we have a suppressor |
| 616 |
* pending. Likewise, if we found a <.p0> rather than a |
| 617 |
* <.p>, this counts as a suppressor. |
| 618 |
*/ |
| 619 |
ostr.justDidParaSuppressor = |
| 620 |
(p0 || rexMatch(suppressBefore, |
| 621 |
ret.substr(ret.length(), 1)) != nil); |
| 622 |
|
| 623 |
/* |
| 624 |
* if we found a paragraph marker, and we didn't find a |
| 625 |
* leading suppressor character just before it, add the |
| 626 |
* paragraph rendering |
| 627 |
*/ |
| 628 |
if (ostr.justDidPara && !ostr.justDidParaSuppressor) |
| 629 |
ret += renderText; |
| 630 |
} |
| 631 |
|
| 632 |
/* return the translated string */ |
| 633 |
return ret; |
| 634 |
} |
| 635 |
; |
| 636 |
|
| 637 |
/* the paragraph manager for the main output stream */ |
| 638 |
transient mainParagraphManager: ParagraphManager |
| 639 |
; |
| 640 |
|
| 641 |
/* ------------------------------------------------------------------------ */ |
| 642 |
/* |
| 643 |
* Output Filter |
| 644 |
*/ |
| 645 |
class OutputFilter: object |
| 646 |
/* |
| 647 |
* Apply the filter - this should be overridden in each filter. The |
| 648 |
* return value is the result of filtering the string. |
| 649 |
* |
| 650 |
* 'ostr' is the OutputStream to which the text is being written, |
| 651 |
* and 'txt' is the original text to be displayed. |
| 652 |
*/ |
| 653 |
filterText(ostr, txt) { return txt; } |
| 654 |
; |
| 655 |
|
| 656 |
|
| 657 |
/* ------------------------------------------------------------------------ */ |
| 658 |
/* |
| 659 |
* Output monitor filter. This is a filter that leaves the filtered |
| 660 |
* text unchanged, but keeps track of whether any text was seen at all. |
| 661 |
* Our 'outputFlag' is true if we've seen any output, nil if not. |
| 662 |
*/ |
| 663 |
class MonitorFilter: OutputFilter |
| 664 |
/* filter text */ |
| 665 |
filterText(ostr, val) |
| 666 |
{ |
| 667 |
/* if the value is non-empty, note the output */ |
| 668 |
if (val != nil && val != '') |
| 669 |
outputFlag = true; |
| 670 |
|
| 671 |
/* return the input value unchanged */ |
| 672 |
return val; |
| 673 |
} |
| 674 |
|
| 675 |
/* flag: has any output occurred for this monitor yet? */ |
| 676 |
outputFlag = nil |
| 677 |
; |
| 678 |
|
| 679 |
|
| 680 |
/* ------------------------------------------------------------------------ */ |
| 681 |
/* |
| 682 |
* Capture Filter. This is an output filter that simply captures all of |
| 683 |
* the text sent through the filter, sending nothing out to the |
| 684 |
* underlying stream. |
| 685 |
* |
| 686 |
* The default implementation simply discards the incoming text. |
| 687 |
* Subclasses can keep track of the text in memory, in a file, or |
| 688 |
* wherever desired. |
| 689 |
*/ |
| 690 |
class CaptureFilter: OutputFilter |
| 691 |
/* |
| 692 |
* Filter the text. We simply discard the text, passing nothing |
| 693 |
* through to the underlying stream. |
| 694 |
*/ |
| 695 |
filterText(ostr, txt) |
| 696 |
{ |
| 697 |
/* leave nothing for the underlying stream */ |
| 698 |
return nil; |
| 699 |
} |
| 700 |
; |
| 701 |
|
| 702 |
/* |
| 703 |
* "Switchable" capture filter. This filter can have its blocking |
| 704 |
* enabled or disabled. When blocking is enabled, we capture |
| 705 |
* everything, leaving nothing to the underlying stream; when disabled, |
| 706 |
* we pass everything through to the underyling stream unchanged. |
| 707 |
*/ |
| 708 |
class SwitchableCaptureFilter: CaptureFilter |
| 709 |
/* filter the text */ |
| 710 |
filterText(ostr, txt) |
| 711 |
{ |
| 712 |
/* |
| 713 |
* if we're blocking output, return nothing to the underlying |
| 714 |
* stream; if we're disabled, return the input unchanged |
| 715 |
*/ |
| 716 |
return (isBlocking ? nil : txt); |
| 717 |
} |
| 718 |
|
| 719 |
/* |
| 720 |
* Blocking enabled: if this is true, we'll capture all text passed |
| 721 |
* through us, leaving nothing to the underyling stream. Blocking |
| 722 |
* is enabled by default. |
| 723 |
*/ |
| 724 |
isBlocking = true |
| 725 |
; |
| 726 |
|
| 727 |
/* |
| 728 |
* String capturer. This is an implementation of CaptureFilter that |
| 729 |
* saves the captured text to a string. |
| 730 |
*/ |
| 731 |
class StringCaptureFilter: CaptureFilter |
| 732 |
/* filter text */ |
| 733 |
filterText(ostr, txt) |
| 734 |
{ |
| 735 |
/* add the text to my captured text so far */ |
| 736 |
addText(txt); |
| 737 |
} |
| 738 |
|
| 739 |
/* add to my captured text */ |
| 740 |
addText(txt) |
| 741 |
{ |
| 742 |
/* append the text to my string of captured text */ |
| 743 |
txt_ += txt; |
| 744 |
} |
| 745 |
|
| 746 |
/* my captured text so far */ |
| 747 |
txt_ = '' |
| 748 |
; |
| 749 |
|
| 750 |
/* ------------------------------------------------------------------------ */ |
| 751 |
/* |
| 752 |
* Style tag. This defines an HTML-like tag that can be used in output |
| 753 |
* text to display an author-customizable substitution string. |
| 754 |
* |
| 755 |
* Each StyleTag object defines the name of the tag, which can be |
| 756 |
* invoked in output text using the syntax "<.name>" - we require the |
| 757 |
* period after the opening angle-bracket to plainly distinguish the |
| 758 |
* sequence as a style tag, not a regular HTML tag. |
| 759 |
* |
| 760 |
* Each StyleTag also defines the text string that should be substituted |
| 761 |
* for each occurrence of the "<.name>" sequence in output text, and, |
| 762 |
* optionally, another string that is substituted for occurrences of the |
| 763 |
* "closing" version of the tag, invoked with the syntax "<./name>". |
| 764 |
*/ |
| 765 |
class StyleTag: object |
| 766 |
/* name of the tag - the tag appears in source text in <.xxx> notation */ |
| 767 |
tagName = '' |
| 768 |
|
| 769 |
/* |
| 770 |
* opening text - this is substituted for each instance of the tag |
| 771 |
* without a '/' prefix |
| 772 |
*/ |
| 773 |
openText = '' |
| 774 |
|
| 775 |
/* |
| 776 |
* Closing text - this is substituted for each instance of the tag |
| 777 |
* with a '/' prefix (<./xxx>). Note that non-container tags don't |
| 778 |
* have closing text at all. |
| 779 |
*/ |
| 780 |
closeText = '' |
| 781 |
; |
| 782 |
|
| 783 |
/* |
| 784 |
* HtmlStyleTag - this is a subclass of StyleTag that provides different |
| 785 |
* rendering depending on whether the interpreter is in HTML mode or not. |
| 786 |
* In HTML mode, we display our htmlOpenText and htmlCloseText; when not |
| 787 |
* in HTML mode, we display our plainOpenText and plainCloseText. |
| 788 |
*/ |
| 789 |
class HtmlStyleTag: StyleTag |
| 790 |
openText = (outputManager.htmlMode ? htmlOpenText : plainOpenText) |
| 791 |
|
| 792 |
closeText = (outputManager.htmlMode ? htmlCloseText : plainCloseText) |
| 793 |
|
| 794 |
/* our HTML-mode opening and closing text */ |
| 795 |
htmlOpenText = '' |
| 796 |
htmlCloseText = '' |
| 797 |
|
| 798 |
/* our plain (non-HTML) opening and closing text */ |
| 799 |
plainOpenText = '' |
| 800 |
plainCloseText = '' |
| 801 |
; |
| 802 |
|
| 803 |
/* |
| 804 |
* Define our default style tags. We name all of these StyleTag objects |
| 805 |
* so that authors can easily change the expansion text strings at |
| 806 |
* compile-time with the 'modify' syntax, or dynamically at run-time by |
| 807 |
* assigning new strings to the appropriate properties of these objects. |
| 808 |
*/ |
| 809 |
|
| 810 |
/* |
| 811 |
* <.roomname> - we use this to display the room's name in the |
| 812 |
* description of a room (such as in a LOOK AROUND command, or when |
| 813 |
* entering a new location). By default, we display the room name in |
| 814 |
* boldface on a line by itself. |
| 815 |
*/ |
| 816 |
roomnameStyleTag: StyleTag 'roomname' '\n<b>' '</b><br>\n'; |
| 817 |
|
| 818 |
/* <.roomdesc> - we use this to display a room's long description */ |
| 819 |
roomdescStyleTag: StyleTag 'roomdesc' '' ''; |
| 820 |
|
| 821 |
/* |
| 822 |
* <.roompara> - we use this to separate paragraphs within a room's long |
| 823 |
* description |
| 824 |
*/ |
| 825 |
roomparaStyleTag: StyleTag 'roompara' '<.p>\n'; |
| 826 |
|
| 827 |
/* |
| 828 |
* <.inputline> - we use this to display the text actually entered by the |
| 829 |
* user on a command line. Note that this isn't used for the prompt text |
| 830 |
* - it's used only for the command-line text itself. |
| 831 |
*/ |
| 832 |
inputlineStyleTag: HtmlStyleTag 'inputline' |
| 833 |
/* in HTML mode, switch in and out of TADS-Input font */ |
| 834 |
htmlOpenText = '<font face="tads-input">' |
| 835 |
htmlCloseText = '</font>' |
| 836 |
|
| 837 |
/* in plain mode, do nothing */ |
| 838 |
plainOpenText = '' |
| 839 |
plainCloseText = '' |
| 840 |
; |
| 841 |
|
| 842 |
/* |
| 843 |
* <.a> (named in analogy to the HTML <a> tag) - we use this to display |
| 844 |
* hyperlinked text. Note that this goes *inside* an HTML <a> tag - this |
| 845 |
* doesn't do the actual linking (the true <a> tag does that), but rather |
| 846 |
* allows customized text formatting for hyperlinked text. |
| 847 |
*/ |
| 848 |
hyperlinkStyleTag: HtmlStyleTag 'a' |
| 849 |
; |
| 850 |
|
| 851 |
/* <.statusroom> - style for the room name in a status line */ |
| 852 |
statusroomStyleTag: HtmlStyleTag 'statusroom' |
| 853 |
htmlOpenText = '<b>' |
| 854 |
htmlCloseText = '</b>' |
| 855 |
; |
| 856 |
|
| 857 |
/* <.statusscore> - style for the score in a status line */ |
| 858 |
statusscoreStyleTag: HtmlStyleTag 'statusscore' |
| 859 |
htmlOpenText = '<i>' |
| 860 |
htmlCloseText = '</i>' |
| 861 |
; |
| 862 |
|
| 863 |
/* |
| 864 |
* <.parser> - style for messages explicitly from the parser. |
| 865 |
* |
| 866 |
* By default, we do nothing special with these messages. Many games |
| 867 |
* like to use a distinctive notation for parser messages, to make it |
| 868 |
* clear that the messages are "meta" text that's not part of the story |
| 869 |
* but rather specific to the game mechanics; one common convention is |
| 870 |
* to put parser messages in [square brackets]. |
| 871 |
* |
| 872 |
* If the game defines a special appearance for parser messages, for |
| 873 |
* consistency it might want to use the same appearance for notification |
| 874 |
* messages displayed with the <.notification> tag (see |
| 875 |
* notificationStyleTag). |
| 876 |
*/ |
| 877 |
parserStyleTag: StyleTag 'parser' |
| 878 |
openText = '' |
| 879 |
closeText = '' |
| 880 |
; |
| 881 |
|
| 882 |
/* |
| 883 |
* <.notification> - style for "notification" messages, such as score |
| 884 |
* changes and messages explaining how facilities (footnotes, exit |
| 885 |
* lists) work the first time they come up. |
| 886 |
* |
| 887 |
* By default, we'll put notifications in parentheses. Games that use |
| 888 |
* [square brackets] for parser messages (i.e., for the <.parser> tag) |
| 889 |
* might want to use the same notation here for consistency. |
| 890 |
*/ |
| 891 |
notificationStyleTag: StyleTag 'notification' |
| 892 |
openText = '(' |
| 893 |
closeText = ')' |
| 894 |
; |
| 895 |
|
| 896 |
/* |
| 897 |
* <.assume> - style for "assumption" messages, showing an assumption |
| 898 |
* the parser is making. This style is used for showing objects used by |
| 899 |
* default when not specified in a command, objects that the parser |
| 900 |
* chose despite some ambiguity, and implied commands. |
| 901 |
*/ |
| 902 |
assumeStyleTag: StyleTag 'assume' |
| 903 |
openText = '(' |
| 904 |
closeText = ')' |
| 905 |
; |
| 906 |
|
| 907 |
/* |
| 908 |
* <.announceObj> - style for object announcement messages. The parser |
| 909 |
* shows an object announcement for each object when a command is applied |
| 910 |
* to multiple objects (TAKE ALL, DROP KEYS AND WALLET). The |
| 911 |
* announcement simply shows the object's name and a colon, to let the |
| 912 |
* player know that the response text that follows applies to the |
| 913 |
* announced object. |
| 914 |
*/ |
| 915 |
announceObjStyleTag: StyleTag 'announceObj' |
| 916 |
openText = '<b>' |
| 917 |
closeText = '</b>' |
| 918 |
; |
| 919 |
|
| 920 |
/* ------------------------------------------------------------------------ */ |
| 921 |
/* |
| 922 |
* "Style tag" filter. This is an output filter that expands our |
| 923 |
* special style tags in output text. |
| 924 |
*/ |
| 925 |
styleTagFilter: OutputFilter, PreinitObject |
| 926 |
/* pre-compile our frequently-used tag search pattern */ |
| 927 |
tagPattern = static new RexPattern( |
| 928 |
'<nocase><langle>%.(/?[a-z][a-z0-9]*)<rangle>') |
| 929 |
|
| 930 |
/* filter for a style tag */ |
| 931 |
filterText(ostr, val) |
| 932 |
{ |
| 933 |
local idx; |
| 934 |
|
| 935 |
/* search for our special '<.xxx>' tags, and expand any we find */ |
| 936 |
idx = rexSearch(tagPattern, val); |
| 937 |
while (idx != nil) |
| 938 |
{ |
| 939 |
local xlat; |
| 940 |
local afterOfs; |
| 941 |
local afterStr; |
| 942 |
|
| 943 |
/* ask the formatter to translate it */ |
| 944 |
xlat = translateTag(rexGroup(1)[3]); |
| 945 |
|
| 946 |
/* get the part of the string that follows the tag */ |
| 947 |
afterOfs = idx[1] + idx[2]; |
| 948 |
afterStr = val.substr(idx[1] + idx[2]); |
| 949 |
|
| 950 |
/* |
| 951 |
* if we got a translation, replace it; otherwise, leave the |
| 952 |
* original text intact |
| 953 |
*/ |
| 954 |
if (xlat != nil) |
| 955 |
{ |
| 956 |
/* replace the tag with its translation */ |
| 957 |
val = val.substr(1, idx[1] - 1) + xlat + afterStr; |
| 958 |
|
| 959 |
/* |
| 960 |
* figure the offset of the remainder of the string in |
| 961 |
* the replaced version of the string - this is the |
| 962 |
* length of the original part up to the replacement |
| 963 |
* text plus the length of the replacement text |
| 964 |
*/ |
| 965 |
afterOfs = idx[1] + xlat.length(); |
| 966 |
} |
| 967 |
|
| 968 |
/* |
| 969 |
* search for the next tag, considering only the part of |
| 970 |
* the string following the replacement text - we do not |
| 971 |
* want to re-scan the replacement text for tags |
| 972 |
*/ |
| 973 |
idx = rexSearch(tagPattern, afterStr); |
| 974 |
|
| 975 |
/* |
| 976 |
* If we found it, adjust the starting index of the match to |
| 977 |
* its position in the actual string. Note that we do this |
| 978 |
* by adding the OFFSET of the remainder of the string, |
| 979 |
* which is 1 less than its INDEX, because idx[1] is already |
| 980 |
* a string index. (An offset is one less than an index |
| 981 |
* because the index of the first character is 1.) |
| 982 |
*/ |
| 983 |
if (idx != nil) |
| 984 |
idx[1] += afterOfs - 1; |
| 985 |
} |
| 986 |
|
| 987 |
/* return the filtered value */ |
| 988 |
return val; |
| 989 |
} |
| 990 |
|
| 991 |
/* |
| 992 |
* Translate a tag |
| 993 |
*/ |
| 994 |
translateTag(tag) |
| 995 |
{ |
| 996 |
local isClose; |
| 997 |
local styleTag; |
| 998 |
|
| 999 |
/* if it's a close tag, so note and remove the leading slash */ |
| 1000 |
isClose = tag.startsWith('/'); |
| 1001 |
if (isClose) |
| 1002 |
tag = tag.substr(2); |
| 1003 |
|
| 1004 |
/* look up the tag object in our table */ |
| 1005 |
styleTag = tagTable[tag]; |
| 1006 |
|
| 1007 |
/* |
| 1008 |
* if we found it, return the open or close text, as |
| 1009 |
* appropriate; otherwise return nil |
| 1010 |
*/ |
| 1011 |
return (styleTag != nil |
| 1012 |
? (isClose ? styleTag.closeText : styleTag.openText) |
| 1013 |
: nil); |
| 1014 |
} |
| 1015 |
|
| 1016 |
/* preinitialization */ |
| 1017 |
execute() |
| 1018 |
{ |
| 1019 |
/* create a lookup table for our style table */ |
| 1020 |
tagTable = new LookupTable(); |
| 1021 |
|
| 1022 |
/* |
| 1023 |
* Populate the table with all of the StyleTag instances. Key |
| 1024 |
* by tag name, storing the tag object as the value for each |
| 1025 |
* key. This will let us efficiently look up the StyleTag |
| 1026 |
* object given a tag name string. |
| 1027 |
*/ |
| 1028 |
forEachInstance(StyleTag, { tag: tagTable[tag.tagName] = tag }); |
| 1029 |
} |
| 1030 |
|
| 1031 |
/* |
| 1032 |
* Our tag translation table. We'll initialize this during preinit |
| 1033 |
* to a lookup table with all of the defined StyleTag objects. |
| 1034 |
*/ |
| 1035 |
tagTable = nil |
| 1036 |
; |
| 1037 |
|
| 1038 |
|
| 1039 |
/* ------------------------------------------------------------------------ */ |
| 1040 |
/* |
| 1041 |
* MessageBuilder - this object provides a general text substitution |
| 1042 |
* mechanism. Text to be substituted is enclosed in {curly braces}. |
| 1043 |
* Within the braces, we have the substitution parameter name, which can |
| 1044 |
* be in the following formats: |
| 1045 |
* |
| 1046 |
* id |
| 1047 |
*. id obj |
| 1048 |
*. id1/id2 obj |
| 1049 |
*. id1 obj/id2 |
| 1050 |
* |
| 1051 |
* The ID string gives the type of substitution to perform. The ID's |
| 1052 |
* all come from a table, which is specified by the language-specific |
| 1053 |
* subclass, so the ID's can vary by language (to allow for natural |
| 1054 |
* template-style parameter names for each language). If the ID is in |
| 1055 |
* two pieces (id1 and id2), we concatenate the two pieces together with |
| 1056 |
* a slash between to form the name we seek in the table - so {the/he |
| 1057 |
* dobj} and {the dobj/he} are equivalent, and both look up the |
| 1058 |
* identifier 'the/he'. If a two-part identifier is given, and the |
| 1059 |
* identifier isn't found in the table, we'll try looking it up with the |
| 1060 |
* parts reversed: if we see {he/the dobj}, we'll first try finding |
| 1061 |
* 'he/the', and if that fails we'll look for 'the/he'. |
| 1062 |
* |
| 1063 |
* If 'obj' is present, it specificies the target object providing the |
| 1064 |
* text to be substitutued; this is a string passed to the current |
| 1065 |
* Action, and is usually something like 'actor', 'dobj', or 'iobj'. |
| 1066 |
* |
| 1067 |
* One instance of this class, called langMessageBuilder, should be |
| 1068 |
* created by the language-specific library. |
| 1069 |
*/ |
| 1070 |
class MessageBuilder: OutputFilter, PreinitObject |
| 1071 |
/* pre-compile some regular expressions we use a lot */ |
| 1072 |
patUpper = static new RexPattern('<upper>') |
| 1073 |
patAllCaps = static new RexPattern('<upper><upper>') |
| 1074 |
patIdObjSlashId = static new RexPattern( |
| 1075 |
'(<^space|/>+)<space>+(<^space|/>+)(/<^space|/>+)') |
| 1076 |
patIdObj = static new RexPattern('(<^space>+)<space>+(<^space>+)') |
| 1077 |
patIdSlash = static new RexPattern('([^/]+)/([^/]+)') |
| 1078 |
|
| 1079 |
/* |
| 1080 |
* Given a source string with substitution parameters, generate the |
| 1081 |
* expanded message with the appropriate text in place of the |
| 1082 |
* parameters. |
| 1083 |
*/ |
| 1084 |
generateMessage(orig) |
| 1085 |
{ |
| 1086 |
local result; |
| 1087 |
|
| 1088 |
/* we have nothing in the result string so far */ |
| 1089 |
result = ''; |
| 1090 |
|
| 1091 |
/* keep going until we run out of substitution parameters */ |
| 1092 |
for (;;) |
| 1093 |
{ |
| 1094 |
local idx; |
| 1095 |
local paramStr; |
| 1096 |
local paramName; |
| 1097 |
local paramObj; |
| 1098 |
local info; |
| 1099 |
local initCap, allCaps; |
| 1100 |
local targetObj; |
| 1101 |
local newText; |
| 1102 |
local prop; |
| 1103 |
|
| 1104 |
/* get the position of the next brace */ |
| 1105 |
idx = orig.find('{'); |
| 1106 |
|
| 1107 |
/* |
| 1108 |
* if there are no braces, the rest of the string is simply |
| 1109 |
* literal text; add the entire remainder of the string to |
| 1110 |
* the result, and we're done |
| 1111 |
*/ |
| 1112 |
if (idx == nil) |
| 1113 |
{ |
| 1114 |
result += processResult(genLiteral(orig)); |
| 1115 |
break; |
| 1116 |
} |
| 1117 |
|
| 1118 |
/* add everything up to the brace to the result string */ |
| 1119 |
result += processResult(genLiteral(orig.substr(1, idx - 1))); |
| 1120 |
|
| 1121 |
/* |
| 1122 |
* lop off everything up to and including the brace from the |
| 1123 |
* source string, since we're done with the part up to the |
| 1124 |
* brace now |
| 1125 |
*/ |
| 1126 |
orig = orig.substr(idx + 1); |
| 1127 |
|
| 1128 |
/* |
| 1129 |
* if the brace was the last thing in the source string, or |
| 1130 |
* it's a stuttered brace, add it literally to the result |
| 1131 |
*/ |
| 1132 |
if (orig.length() == 0) |
| 1133 |
{ |
| 1134 |
/* |
| 1135 |
* nothing follows - add a literal brace to the result, |
| 1136 |
* and we're done |
| 1137 |
*/ |
| 1138 |
result += processResult('{'); |
| 1139 |
break; |
| 1140 |
} |
| 1141 |
else if (orig.substr(1, 1) == '{') |
| 1142 |
{ |
| 1143 |
/* |
| 1144 |
* it's a stuttered brace - add a literal brace to the |
| 1145 |
* result |
| 1146 |
*/ |
| 1147 |
result += processResult('{'); |
| 1148 |
|
| 1149 |
/* remove the second brace from the source */ |
| 1150 |
orig = orig.substr(2); |
| 1151 |
|
| 1152 |
/* we're finished processing this brace - go back for more */ |
| 1153 |
continue; |
| 1154 |
} |
| 1155 |
|
| 1156 |
/* find the closing brace */ |
| 1157 |
idx = orig.find('}'); |
| 1158 |
|
| 1159 |
/* |
| 1160 |
* if there is no closing brace, include the brace and |
| 1161 |
* whatever follows as literal text |
| 1162 |
*/ |
| 1163 |
if (idx == nil) |
| 1164 |
{ |
| 1165 |
/* add the literal brace to the result */ |
| 1166 |
result += processResult('{'); |
| 1167 |
|
| 1168 |
/* we're done with the brace - go back for more */ |
| 1169 |
continue; |
| 1170 |
} |
| 1171 |
|
| 1172 |
/* |
| 1173 |
* Pull out everything up to the brace as the parameter |
| 1174 |
* text. |
| 1175 |
*/ |
| 1176 |
paramStr = orig.substr(1, idx - 1); |
| 1177 |
|
| 1178 |
/* assume for now that we will have no parameter object */ |
| 1179 |
paramObj = nil; |
| 1180 |
|
| 1181 |
/* |
| 1182 |
* drop everything up to and including the closing brace, |
| 1183 |
* since we've pulled it out for processing now |
| 1184 |
*/ |
| 1185 |
orig = orig.substr(idx + 1); |
| 1186 |
|
| 1187 |
/* |
| 1188 |
* Note the capitalization of the first two letters. If |
| 1189 |
* they're both lower-case, we won't adjust the case of the |
| 1190 |
* substitution text at all. If the first is a capital and |
| 1191 |
* the second isn't, we'll capitalize the first letter of |
| 1192 |
* the replacement text. If they're both capitals, we'll |
| 1193 |
* capitalize the entire replacement text. |
| 1194 |
*/ |
| 1195 |
initCap = (rexMatch(patUpper, paramStr) != nil); |
| 1196 |
allCaps = (rexMatch(patAllCaps, paramStr) != nil); |
| 1197 |
|
| 1198 |
/* lower-case the entire parameter string for matching */ |
| 1199 |
local origParamStr = paramStr; |
| 1200 |
paramStr = paramStr.toLower(); |
| 1201 |
|
| 1202 |
/* perform any language-specific rewriting on the string */ |
| 1203 |
paramStr = langRewriteParam(paramStr); |
| 1204 |
|
| 1205 |
/* |
| 1206 |
* Figure out which format we have. The allowable formats |
| 1207 |
* are: |
| 1208 |
* |
| 1209 |
* id |
| 1210 |
* obj |
| 1211 |
*. id obj |
| 1212 |
*. id1/id2 |
| 1213 |
*. id1/id2 obj |
| 1214 |
*. id1 obj/id2 |
| 1215 |
*/ |
| 1216 |
if (rexMatch(patIdObjSlashId, paramStr) != nil) |
| 1217 |
{ |
| 1218 |
/* we have the id1 obj/id2 format */ |
| 1219 |
paramName = rexGroup(1)[3] + rexGroup(3)[3]; |
| 1220 |
paramObj = rexGroup(2)[3]; |
| 1221 |
} |
| 1222 |
else if (rexMatch(patIdObj, paramStr) != nil) |
| 1223 |
{ |
| 1224 |
/* we have 'id obj' or 'id1/id2 obj' */ |
| 1225 |
paramName = rexGroup(1)[3]; |
| 1226 |
paramObj = rexGroup(2)[3]; |
| 1227 |
} |
| 1228 |
else |
| 1229 |
{ |
| 1230 |
/* we have no spaces, so we have no target object */ |
| 1231 |
paramName = paramStr; |
| 1232 |
paramObj = nil; |
| 1233 |
} |
| 1234 |
|
| 1235 |
/* look up our parameter name */ |
| 1236 |
info = paramTable_[paramName]; |
| 1237 |
|
| 1238 |
/* |
| 1239 |
* If we didn't find it, and the parameter name contains a |
| 1240 |
* slash ('/'), try reversing the order of the parts before |
| 1241 |
* and after the slash. |
| 1242 |
*/ |
| 1243 |
if (info == nil && rexMatch(patIdSlash, paramName) != nil) |
| 1244 |
{ |
| 1245 |
/* |
| 1246 |
* rebuild the name with the order of the parts |
| 1247 |
* reversed, and look up the result |
| 1248 |
*/ |
| 1249 |
info = paramTable_[rexGroup(2)[3] + '/' + rexGroup(1)[3]]; |
| 1250 |
} |
| 1251 |
|
| 1252 |
/* |
| 1253 |
* If we didn't find a match, simply put the entire thing in |
| 1254 |
* the result stream literally, including the braces. |
| 1255 |
*/ |
| 1256 |
if (info == nil) |
| 1257 |
{ |
| 1258 |
/* |
| 1259 |
* We didn't find it, so try treating it as a string |
| 1260 |
* parameter object. Try getting the string from the |
| 1261 |
* action. |
| 1262 |
*/ |
| 1263 |
newText = (gAction != nil |
| 1264 |
? gAction.getMessageParam(paramName) : nil); |
| 1265 |
|
| 1266 |
/* check what we found */ |
| 1267 |
if (dataType(newText) == TypeSString) |
| 1268 |
{ |
| 1269 |
/* |
| 1270 |
* It's a valid string parameter. Simply add the |
| 1271 |
* literal text to the result. If we're in html |
| 1272 |
* mode, translate the string to ensure that any |
| 1273 |
* markup-significant characters are properly quoted |
| 1274 |
* so that they aren't taken as html themselves. |
| 1275 |
*/ |
| 1276 |
result += processResult(newText.htmlify()); |
| 1277 |
} |
| 1278 |
else |
| 1279 |
{ |
| 1280 |
/* |
| 1281 |
* the parameter is completely undefined; simply add |
| 1282 |
* the original text, including the braces |
| 1283 |
*/ |
| 1284 |
result += processResult('{<<origParamStr>>}'); |
| 1285 |
} |
| 1286 |
|
| 1287 |
/* |
| 1288 |
* we're done with this substitution string - go back |
| 1289 |
* for more |
| 1290 |
*/ |
| 1291 |
continue; |
| 1292 |
} |
| 1293 |
|
| 1294 |
/* |
| 1295 |
* If we have no target object specified in the substitution |
| 1296 |
* string, and the parameter name has an associated implicit |
| 1297 |
* target object, use the implied object. |
| 1298 |
*/ |
| 1299 |
if (paramObj == nil && info[3] != nil) |
| 1300 |
paramObj = info[3]; |
| 1301 |
|
| 1302 |
/* |
| 1303 |
* If we have a target object name, ask the current action |
| 1304 |
* for the target object value. Otherwise, use the same |
| 1305 |
* target object as the previous expansion. |
| 1306 |
*/ |
| 1307 |
if (paramObj != nil) |
| 1308 |
{ |
| 1309 |
/* check for a current action */ |
| 1310 |
if (gAction != nil) |
| 1311 |
{ |
| 1312 |
/* get the target object by name through the action */ |
| 1313 |
targetObj = gAction.getMessageParam(paramObj); |
| 1314 |
} |
| 1315 |
else |
| 1316 |
{ |
| 1317 |
/* there's no action, so we don't have a value yet */ |
| 1318 |
targetObj = nil; |
| 1319 |
} |
| 1320 |
|
| 1321 |
/* |
| 1322 |
* if we didn't find a value, look up the name in our |
| 1323 |
* global name table |
| 1324 |
*/ |
| 1325 |
if (targetObj == nil) |
| 1326 |
{ |
| 1327 |
/* look up the name */ |
| 1328 |
targetObj = nameTable_[paramObj]; |
| 1329 |
|
| 1330 |
/* |
| 1331 |
* if we found it, and the result is a function |
| 1332 |
* pointer or an anonymous function, invoke the |
| 1333 |
* function to get the result |
| 1334 |
*/ |
| 1335 |
if (dataTypeXlat(targetObj) == TypeFuncPtr) |
| 1336 |
{ |
| 1337 |
/* evaluate the function */ |
| 1338 |
targetObj = (targetObj)(); |
| 1339 |
} |
| 1340 |
} |
| 1341 |
|
| 1342 |
/* |
| 1343 |
* remember this for next time, in case the next |
| 1344 |
* substitution string doesn't include a target object |
| 1345 |
*/ |
| 1346 |
lastTargetObj_ = targetObj; |
| 1347 |
lastParamObj_ = paramObj; |
| 1348 |
} |
| 1349 |
else |
| 1350 |
{ |
| 1351 |
/* |
| 1352 |
* there's no implied or explicit target - use the same |
| 1353 |
* one as last time |
| 1354 |
*/ |
| 1355 |
targetObj = lastTargetObj_; |
| 1356 |
paramObj = lastParamObj_; |
| 1357 |
} |
| 1358 |
|
| 1359 |
/* |
| 1360 |
* if the target object wasn't found, treat the whole thing |
| 1361 |
* as a failure - put the entire parameter string back in |
| 1362 |
* the result stream literally |
| 1363 |
*/ |
| 1364 |
if (targetObj == nil) |
| 1365 |
{ |
| 1366 |
/* add it to the output literally, and go back for more */ |
| 1367 |
result += processResult('{' + paramStr + '}'); |
| 1368 |
continue; |
| 1369 |
} |
| 1370 |
|
| 1371 |
/* get the property to call on the target */ |
| 1372 |
prop = getTargetProp(targetObj, paramObj, info); |
| 1373 |
|
| 1374 |
/* evaluate the parameter's associated property on the target */ |
| 1375 |
newText = targetObj.(prop); |
| 1376 |
|
| 1377 |
/* apply the appropriate capitalization to the result */ |
| 1378 |
if (allCaps) |
| 1379 |
newText = newText.toUpper(); |
| 1380 |
else if (initCap) |
| 1381 |
newText = newText.substr(1, 1).toUpper() + newText.substr(2); |
| 1382 |
|
| 1383 |
/* |
| 1384 |
* append the new text to the output result so far, and |
| 1385 |
* we're finished with this round |
| 1386 |
*/ |
| 1387 |
result += processResult(newText); |
| 1388 |
} |
| 1389 |
|
| 1390 |
/* return the result string */ |
| 1391 |
return result; |
| 1392 |
} |
| 1393 |
|
| 1394 |
/* |
| 1395 |
* Get the property to invoke on the target object for the given |
| 1396 |
* parameter information entry. By default, we simply return |
| 1397 |
* info[2], which is the standard property to call on the target. |
| 1398 |
* This can be overridden by the language-specific subclass to |
| 1399 |
* provide a different property if appropriate. |
| 1400 |
* |
| 1401 |
* 'targetObj' is the target object, and 'paramObj' is the parameter |
| 1402 |
* name of the target object. For example, 'paramObj' might be the |
| 1403 |
* string 'dobj' to represent the direct object, in which case |
| 1404 |
* 'targetObj' will be the gDobj object. |
| 1405 |
* |
| 1406 |
* The English version, for example, uses this routine to supply a |
| 1407 |
* reflexive instead of the default entry when the target object |
| 1408 |
* matches the subject of the sentence. |
| 1409 |
*/ |
| 1410 |
getTargetProp(targetObj, paramObj, info) |
| 1411 |
{ |
| 1412 |
/* return the standard property mapping from the parameter info */ |
| 1413 |
return info[2]; |
| 1414 |
} |
| 1415 |
|
| 1416 |
/* |
| 1417 |
* Process result text. This takes some result text that we're |
| 1418 |
* about to add and returns a processed version. This is called for |
| 1419 |
* all text as we add it to the result string. |
| 1420 |
* |
| 1421 |
* The text we pass to this method has already had all parameter |
| 1422 |
* text fully expanded, so this routine does not need to worry about |
| 1423 |
* { } sequences - all { } sequences will have been removed and |
| 1424 |
* replaced with the corresponding expansion text before this is |
| 1425 |
* called. |
| 1426 |
* |
| 1427 |
* This routine is called piecewise: the routine will be called once |
| 1428 |
* for each parameter replacement text and once for each run of text |
| 1429 |
* between parameters, and is called in the order in which the text |
| 1430 |
* appears in the original string. |
| 1431 |
* |
| 1432 |
* By default we do nothing with the result text; we simply return |
| 1433 |
* the original text unchanged. The language-specific subclass can |
| 1434 |
* override this as desired to further modify the text for special |
| 1435 |
* language-specific parameterization outside of the { } mechanism. |
| 1436 |
* The subclass can also use this routine to maintain internal state |
| 1437 |
* that depends on sentence structure. For example, the English |
| 1438 |
* version looks for sentence-ending punctuation so that it can |
| 1439 |
* reset its internal notion of the subject of the sentence when a |
| 1440 |
* sentence appears to be ending. |
| 1441 |
*/ |
| 1442 |
processResult(txt) { return txt; } |
| 1443 |
|
| 1444 |
/* |
| 1445 |
* "Quote" a message - double each open or close brace, so that braces in |
| 1446 |
* the message will be taken literally when run through the substitution |
| 1447 |
* replacer. This can be used when a message is expanded prior to being |
| 1448 |
* displayed to ensure that braces in the result won't be mistaken as |
| 1449 |
* substitution parameters requiring further expansion. |
| 1450 |
* |
| 1451 |
* Note that only open braces need to be quoted, since lone close braces |
| 1452 |
* are ignored in the substitution process. |
| 1453 |
*/ |
| 1454 |
quoteMessage(str) |
| 1455 |
{ |
| 1456 |
return str.findReplace(['{', '}'], ['{{', '}}'], ReplaceAll); |
| 1457 |
} |
| 1458 |
|
| 1459 |
/* |
| 1460 |
* Internal routine - generate the literal text for the given source |
| 1461 |
* string. We'll remove any stuttered close braces. |
| 1462 |
*/ |
| 1463 |
genLiteral(str) |
| 1464 |
{ |
| 1465 |
/* replace all '}}' sequences with '}' sequences */ |
| 1466 |
return str.findReplace('}}', '}', ReplaceAll); |
| 1467 |
} |
| 1468 |
|
| 1469 |
/* |
| 1470 |
* execute pre-initialization |
| 1471 |
*/ |
| 1472 |
execute() |
| 1473 |
{ |
| 1474 |
/* create a lookup table for our parameter names */ |
| 1475 |
paramTable_ = new LookupTable(); |
| 1476 |
|
| 1477 |
/* add each element of our list to the table */ |
| 1478 |
foreach (local cur in paramList_) |
| 1479 |
paramTable_[cur[1]] = cur; |
| 1480 |
|
| 1481 |
/* create a lookup table for our global names */ |
| 1482 |
nameTable_ = new LookupTable(); |
| 1483 |
|
| 1484 |
/* |
| 1485 |
* Add an entry for 'actor', which resolves to gActor if there is |
| 1486 |
* a gActor when evaluated, or the current player character if |
| 1487 |
* not. Note that using a function ensures that we evaluate the |
| 1488 |
* current gActor or gPlayerChar each time we need the 'actor' |
| 1489 |
* value. |
| 1490 |
*/ |
| 1491 |
nameTable_['actor'] = {: gActor != nil ? gActor : gPlayerChar }; |
| 1492 |
} |
| 1493 |
|
| 1494 |
/* |
| 1495 |
* Our output filter method. We'll run each string written to the |
| 1496 |
* display through our parameter substitution method. |
| 1497 |
*/ |
| 1498 |
filterText(ostr, txt) |
| 1499 |
{ |
| 1500 |
/* substitute any parameters in the string and return the result */ |
| 1501 |
return generateMessage(txt); |
| 1502 |
} |
| 1503 |
|
| 1504 |
/* |
| 1505 |
* The most recent target object. Each time we parse a substitution |
| 1506 |
* string, we'll remember the target object here; when a |
| 1507 |
* substitution string doesn't imply or specify a target object, |
| 1508 |
* we'll use the previous one by default. |
| 1509 |
*/ |
| 1510 |
lastTargetObj_ = nil |
| 1511 |
|
| 1512 |
/* the parameter name of the last target object ('dobj', 'actor', etc) */ |
| 1513 |
lastParamObj_ = nil |
| 1514 |
|
| 1515 |
/* our parameter table - a LookupTable that we set up during preinit */ |
| 1516 |
paramTable_ = nil |
| 1517 |
|
| 1518 |
/* our global name table - a LookupTable we set up during preinit */ |
| 1519 |
nameTable_ = nil |
| 1520 |
|
| 1521 |
/* |
| 1522 |
* Rewrite the parameter string for any language-specific rules. By |
| 1523 |
* default, we'll return the original parameter string unchanged; |
| 1524 |
* the language-specific instance can override this to provide any |
| 1525 |
* special syntax extensions to the parameter string syntax desired |
| 1526 |
* by the language-specific library. The returned string must be in |
| 1527 |
* one of the formats recognized by the generic handler. |
| 1528 |
*/ |
| 1529 |
langRewriteParam(paramStr) |
| 1530 |
{ |
| 1531 |
/* by default, return the original unchanged */ |
| 1532 |
return paramStr; |
| 1533 |
} |
| 1534 |
|
| 1535 |
/* |
| 1536 |
* our parameter list - this should be initialized in the |
| 1537 |
* language-specific subclass to a list like this: |
| 1538 |
* |
| 1539 |
* [entry1, entry2, entry3, ...] |
| 1540 |
* |
| 1541 |
* Each entry is a list like this: |
| 1542 |
* |
| 1543 |
* [paramName, &prop, impliedTargetName, <extra>] |
| 1544 |
* |
| 1545 |
* paramName is a string giving the substitution parameter name; |
| 1546 |
* this can be one word or two ('the' or 'the obj', for example). |
| 1547 |
* |
| 1548 |
* prop is a property identifier. This is the property invoked on |
| 1549 |
* the target object to obtain the substitution text. |
| 1550 |
* |
| 1551 |
* impliedTargetName is a string giving the target object name to |
| 1552 |
* use. When this is supplied, the paramName is normally used in |
| 1553 |
* message text with no object name. This should be nil for |
| 1554 |
* parameters that do not imply a particular target. |
| 1555 |
* |
| 1556 |
* <extra> is any number of additional parameters for the |
| 1557 |
* language-specific subclass. The generic code ignores these extra |
| 1558 |
* parameters, but the langague-specific subclass can use them if it |
| 1559 |
* requires additional information. |
| 1560 |
* |
| 1561 |
* Here's an example: |
| 1562 |
* |
| 1563 |
* paramList_ = [ |
| 1564 |
*. ['you', &theDesc, nil, 'actor'], |
| 1565 |
*. ['the obj' &theObjDesc, &itReflexive, nil] |
| 1566 |
*. ] |
| 1567 |
* |
| 1568 |
* The first item specifies a substitution name of 'you', which is |
| 1569 |
* expanded by evaluating the property theDesc on the target object, |
| 1570 |
* and specifies an implied target object of 'actor'. When this is |
| 1571 |
* expanded, we'll call the current action to get the meaning of |
| 1572 |
* 'actor', then evaulate property theDesc on the result. |
| 1573 |
* |
| 1574 |
* The second item specifies a substitution name of 'the obj', |
| 1575 |
* expanded by evaluating property theObjDesc on the target object. |
| 1576 |
* This one doesn't have an implied object, so the target object is |
| 1577 |
* the one explicitly given in the message source text or is the |
| 1578 |
* previous target object if one isn't specified in the message |
| 1579 |
* text. |
| 1580 |
*/ |
| 1581 |
paramList_ = [] |
| 1582 |
; |
| 1583 |
|
| 1584 |
|
| 1585 |
/* ------------------------------------------------------------------------ */ |
| 1586 |
/* |
| 1587 |
* Command Sequencer Filter. This is an output filter that handles the |
| 1588 |
* special <.commandsep> tag for visual command separation. This tag has |
| 1589 |
* the form of a style tag, but must be processed specially. |
| 1590 |
* |
| 1591 |
* <.commandsep> shows an appropriate separator between commands. Before |
| 1592 |
* the first command output or after the last command output, this has no |
| 1593 |
* effect. A run of multiple consecutive <.commandsep> tags is treated |
| 1594 |
* as a single tag. |
| 1595 |
* |
| 1596 |
* Between commands, we show gLibMessages.commandResultsSeparator. After |
| 1597 |
* an input line and before the first command result text, we show |
| 1598 |
* gLibMessages.commandResultsPrefix. After the last command result text |
| 1599 |
* before a new input line, we show gLibMessages.commandResultsSuffix. |
| 1600 |
* If we read two input lines, and there is no intervening text output at |
| 1601 |
* all, we show gLibMessages.commandResultsEmpty. |
| 1602 |
* |
| 1603 |
* The input manager should write a <.commandbefore> tag whenever it |
| 1604 |
* starts reading a command line, and a <.commandafter> tag whenever it |
| 1605 |
* finishes reading a command line. |
| 1606 |
*/ |
| 1607 |
enum stateReadingCommand, stateBeforeCommand, stateBeforeInterruption, |
| 1608 |
stateInCommand, stateBetweenCommands, stateWriteThrough, |
| 1609 |
stateNoCommand; |
| 1610 |
|
| 1611 |
transient commandSequencer: OutputFilter |
| 1612 |
/* |
| 1613 |
* Force the sequencer into mid-command mode. This can be used to |
| 1614 |
* defeat the resequencing into before-results mode that occurs if |
| 1615 |
* any interactive command-line input must be read in the course of |
| 1616 |
* a command's execution. |
| 1617 |
*/ |
| 1618 |
setCommandMode() { state_ = stateInCommand; } |
| 1619 |
|
| 1620 |
/* |
| 1621 |
* Internal routine: write the given text directly through us, |
| 1622 |
* skipping any filtering we'd otherwise apply. |
| 1623 |
*/ |
| 1624 |
writeThrough(txt) |
| 1625 |
{ |
| 1626 |
local oldState; |
| 1627 |
|
| 1628 |
/* remember our old state */ |
| 1629 |
oldState = state_; |
| 1630 |
|
| 1631 |
/* set our state to write-through */ |
| 1632 |
state_ = stateWriteThrough; |
| 1633 |
|
| 1634 |
/* make sure we reset things on the way out */ |
| 1635 |
try |
| 1636 |
{ |
| 1637 |
/* write the text */ |
| 1638 |
say(txt); |
| 1639 |
} |
| 1640 |
finally |
| 1641 |
{ |
| 1642 |
/* restore our old state */ |
| 1643 |
state_ = oldState; |
| 1644 |
} |
| 1645 |
} |
| 1646 |
|
| 1647 |
/* pre-compile our tag sequence pattern */ |
| 1648 |
patNextTag = static new RexPattern( |
| 1649 |
'<nocase><langle><dot>' |
| 1650 |
+ 'command(sep|int|before|after|none|mid)' |
| 1651 |
+ '<rangle>') |
| 1652 |
|
| 1653 |
/* |
| 1654 |
* Apply our filter |
| 1655 |
*/ |
| 1656 |
filterText(ostr, txt) |
| 1657 |
{ |
| 1658 |
local ret; |
| 1659 |
|
| 1660 |
/* |
| 1661 |
* if we're in write-through mode, simply pass the text through |
| 1662 |
* unchanged |
| 1663 |
*/ |
| 1664 |
if (state_ == stateWriteThrough) |
| 1665 |
return txt; |
| 1666 |
|
| 1667 |
/* scan for tags */ |
| 1668 |
for (ret = '' ; txt != '' ; ) |
| 1669 |
{ |
| 1670 |
local match; |
| 1671 |
local cur; |
| 1672 |
local tag; |
| 1673 |
|
| 1674 |
/* search for our next special tag sequence */ |
| 1675 |
match = rexSearch(patNextTag, txt); |
| 1676 |
|
| 1677 |
/* check to see if we found a tag */ |
| 1678 |
if (match == nil) |
| 1679 |
{ |
| 1680 |
/* no more tags - the rest of the text is plain text */ |
| 1681 |
cur = txt; |
| 1682 |
txt = ''; |
| 1683 |
tag = nil; |
| 1684 |
} |
| 1685 |
else |
| 1686 |
{ |
| 1687 |
/* found a tag - get the plain text up to the tag */ |
| 1688 |
cur = txt.substr(1, match[1] - 1); |
| 1689 |
txt = txt.substr(match[1] + match[2]); |
| 1690 |
|
| 1691 |
/* get the tag name */ |
| 1692 |
tag = rexGroup(1)[3]; |
| 1693 |
} |
| 1694 |
|
| 1695 |
/* process the plain text up to the tag, if any */ |
| 1696 |
if (cur != '') |
| 1697 |
{ |
| 1698 |
/* check our state */ |
| 1699 |
switch(state_) |
| 1700 |
{ |
| 1701 |
case stateReadingCommand: |
| 1702 |
case stateWriteThrough: |
| 1703 |
case stateInCommand: |
| 1704 |
case stateNoCommand: |
| 1705 |
/* we don't need to add anything in these states */ |
| 1706 |
break; |
| 1707 |
|
| 1708 |
case stateBeforeCommand: |
| 1709 |
/* |
| 1710 |
* We're waiting for the first command output, and |
| 1711 |
* we've now found it. Write the command results |
| 1712 |
* prefix separator. |
| 1713 |
*/ |
| 1714 |
ret += gLibMessages.commandResultsPrefix; |
| 1715 |
|
| 1716 |
/* we're now inside some command result text */ |
| 1717 |
state_ = stateInCommand; |
| 1718 |
break; |
| 1719 |
|
| 1720 |
case stateBeforeInterruption: |
| 1721 |
/* |
| 1722 |
* An editing session has been interrupted, and we're |
| 1723 |
* showing new output. First, switch to normal |
| 1724 |
* in-command mode - do this before doing anything |
| 1725 |
* else, since we might recursively show some more |
| 1726 |
* text in the course of canceling the input line. |
| 1727 |
*/ |
| 1728 |
state_ = stateInCommand; |
| 1729 |
|
| 1730 |
/* |
| 1731 |
* Now tell the input manager that we're canceling |
| 1732 |
* the input line that was under construction. Don't |
| 1733 |
* reset the input editor state, though, since we |
| 1734 |
* might be able to resume editing the same line |
| 1735 |
* later. |
| 1736 |
*/ |
| 1737 |
inputManager.cancelInputInProgress(nil); |
| 1738 |
|
| 1739 |
/* insert the command interruption prefix */ |
| 1740 |
ret += gLibMessages.commandInterruptionPrefix; |
| 1741 |
break; |
| 1742 |
|
| 1743 |
case stateBetweenCommands: |
| 1744 |
/* |
| 1745 |
* We've been waiting for a new command to start |
| 1746 |
* after seeing a <.commandsep> tag. We now have |
| 1747 |
* some text for the new command, so show a command |
| 1748 |
* separator. |
| 1749 |
*/ |
| 1750 |
ret += gLibMessages.commandResultsSeparator; |
| 1751 |
|
| 1752 |
/* we're now inside some command result text */ |
| 1753 |
state_ = stateInCommand; |
| 1754 |
break; |
| 1755 |
} |
| 1756 |
|
| 1757 |
/* add the plain text */ |
| 1758 |
ret += cur; |
| 1759 |
} |
| 1760 |
|
| 1761 |
/* if we found the tag, process it */ |
| 1762 |
switch(tag) |
| 1763 |
{ |
| 1764 |
case 'none': |
| 1765 |
/* switching to no-command mode */ |
| 1766 |
state_ = stateNoCommand; |
| 1767 |
break; |
| 1768 |
|
| 1769 |
case 'mid': |
| 1770 |
/* switching back to mid-command mode */ |
| 1771 |
state_ = stateInCommand; |
| 1772 |
break; |
| 1773 |
|
| 1774 |
case 'sep': |
| 1775 |
/* command separation - check our state */ |
| 1776 |
switch(state_) |
| 1777 |
{ |
| 1778 |
case stateReadingCommand: |
| 1779 |
case stateBeforeCommand: |
| 1780 |
case stateBetweenCommands: |
| 1781 |
case stateWriteThrough: |
| 1782 |
/* in these states, <.commandsep> has no effect */ |
| 1783 |
break; |
| 1784 |
|
| 1785 |
case stateInCommand: |
| 1786 |
/* |
| 1787 |
* We're inside some command text. <.commandsep> |
| 1788 |
* tells us that we've reached the end of one |
| 1789 |
* command's output, so any subsequent output text |
| 1790 |
* belongs to a new command and thus must be visually |
| 1791 |
* separated from the preceding text. Don't add any |
| 1792 |
* separation text yet, because we don't know for |
| 1793 |
* sure that there will ever be any more output text; |
| 1794 |
* instead, switch our state to between-commands, so |
| 1795 |
* that any subsequent text will trigger addition of |
| 1796 |
* a separator. |
| 1797 |
*/ |
| 1798 |
state_ = stateBetweenCommands; |
| 1799 |
break; |
| 1800 |
} |
| 1801 |
break; |
| 1802 |
|
| 1803 |
case 'int': |
| 1804 |
/* |
| 1805 |
* we've just interrupted reading a command line, due to |
| 1806 |
* an expired timeout event - switch to the |
| 1807 |
* before-interruption state |
| 1808 |
*/ |
| 1809 |
state_ = stateBeforeInterruption; |
| 1810 |
break; |
| 1811 |
|
| 1812 |
case 'before': |
| 1813 |
/* we're about to start reading a command */ |
| 1814 |
switch (state_) |
| 1815 |
{ |
| 1816 |
case stateBeforeCommand: |
| 1817 |
/* |
| 1818 |
* we've shown nothing since the last command; show |
| 1819 |
* the empty command separator |
| 1820 |
*/ |
| 1821 |
writeThrough(gLibMessages.commandResultsEmpty()); |
| 1822 |
break; |
| 1823 |
|
| 1824 |
case stateBetweenCommands: |
| 1825 |
case stateInCommand: |
| 1826 |
/* |
| 1827 |
* we've written at least one command result, so |
| 1828 |
* show the after-command separator |
| 1829 |
*/ |
| 1830 |
writeThrough(gLibMessages.commandResultsSuffix()); |
| 1831 |
break; |
| 1832 |
|
| 1833 |
default: |
| 1834 |
/* do nothing in other modes */ |
| 1835 |
break; |
| 1836 |
} |
| 1837 |
|
| 1838 |
/* switch to reading-command mode */ |
| 1839 |
state_ = stateReadingCommand; |
| 1840 |
break; |
| 1841 |
|
| 1842 |
case 'after': |
| 1843 |
/* |
| 1844 |
* We've just finished reading a command. If we're |
| 1845 |
* still in reading-command mode, switch to |
| 1846 |
* before-command-results mode. Don't switch if we're |
| 1847 |
* in another state, since we must have switched to |
| 1848 |
* another state already by a different route, in which |
| 1849 |
* case we can ignore this notification. |
| 1850 |
*/ |
| 1851 |
if (state_ == stateReadingCommand) |
| 1852 |
state_ = stateBeforeCommand; |
| 1853 |
break; |
| 1854 |
} |
| 1855 |
} |
| 1856 |
|
| 1857 |
/* return the results */ |
| 1858 |
return ret; |
| 1859 |
} |
| 1860 |
|
| 1861 |
/* our current state - start out in before-command mode */ |
| 1862 |
state_ = stateBeforeCommand |
| 1863 |
; |
| 1864 |
|
| 1865 |
/* ------------------------------------------------------------------------ */ |
| 1866 |
/* |
| 1867 |
* Log Console output stream. This is a simple wrapper for the system |
| 1868 |
* log console, which allows console-style output to be captured to a |
| 1869 |
* file, with full processing (HTML expansion, word wrapping, etc) but |
| 1870 |
* without displaying anything to the game window. |
| 1871 |
* |
| 1872 |
* This class should always be instantiated with transient instances, |
| 1873 |
* since the underlying system object doesn't participate in save/restore |
| 1874 |
* operations. |
| 1875 |
*/ |
| 1876 |
class LogConsole: OutputStream |
| 1877 |
/* |
| 1878 |
* Utility method: create a log file, set up to capture all console |
| 1879 |
* output to the log file, run the given callback function, and then |
| 1880 |
* close the log file and restore the console output. This can be |
| 1881 |
* used as a simple means of creating a file that captures the output |
| 1882 |
* of a command. |
| 1883 |
*/ |
| 1884 |
captureToFile(filename, charset, width, func) |
| 1885 |
{ |
| 1886 |
local con; |
| 1887 |
|
| 1888 |
/* set up a log console to do the capturing */ |
| 1889 |
con = new LogConsole(filename, charset, width); |
| 1890 |
|
| 1891 |
/* capture to the console and run our command */ |
| 1892 |
outputManager.withOutputStream(con, func); |
| 1893 |
|
| 1894 |
/* done with the console */ |
| 1895 |
con.closeConsole(); |
| 1896 |
} |
| 1897 |
|
| 1898 |
/* create a log console */ |
| 1899 |
construct(filename, charset, width) |
| 1900 |
{ |
| 1901 |
/* inherit base class handling */ |
| 1902 |
inherited(); |
| 1903 |
|
| 1904 |
/* create the system log console object */ |
| 1905 |
handle_ = logConsoleCreate(filename, charset, width); |
| 1906 |
|
| 1907 |
/* install the standard output filters */ |
| 1908 |
addOutputFilter(typographicalOutputFilter); |
| 1909 |
addOutputFilter(new transient ParagraphManager()); |
| 1910 |
addOutputFilter(styleTagFilter); |
| 1911 |
addOutputFilter(langMessageBuilder); |
| 1912 |
} |
| 1913 |
|
| 1914 |
/* |
| 1915 |
* Close the console. This closes the underlying system log console, |
| 1916 |
* which closes the operating system file. No further text can be |
| 1917 |
* written to the console after it's closed. |
| 1918 |
*/ |
| 1919 |
closeConsole() |
| 1920 |
{ |
| 1921 |
/* close our underlying system console */ |
| 1922 |
logConsoleClose(handle_); |
| 1923 |
|
| 1924 |
/* |
| 1925 |
* forget our handle, since it's no longer valid; setting the |
| 1926 |
* handle to nil will make it more obvious what's going on if |
| 1927 |
* someone tries to write more text after we've been closed |
| 1928 |
*/ |
| 1929 |
handle_ = nil; |
| 1930 |
} |
| 1931 |
|
| 1932 |
/* low-level stream writer - write to our system log console */ |
| 1933 |
writeFromStream(txt) { logConsoleSay(handle_, txt); } |
| 1934 |
|
| 1935 |
/* our system log console handle */ |
| 1936 |
handle_ = nil |
| 1937 |
; |
| 1938 |
|
| 1939 |
/* ------------------------------------------------------------------------ */ |
| 1940 |
/* |
| 1941 |
* Output stream window. |
| 1942 |
* |
| 1943 |
* This is an abstract base class for UI widgets that have output |
| 1944 |
* streams, such as Banner Windows and Web UI windows. This base class |
| 1945 |
* essentially handles the interior of the window, and leaves the details |
| 1946 |
* of the window's layout in the broader UI to subclasses. |
| 1947 |
*/ |
| 1948 |
class OutputStreamWindow: object |
| 1949 |
/* |
| 1950 |
* Invoke the given callback function, setting the default output |
| 1951 |
* stream to the window's output stream for the duration of the call. |
| 1952 |
* This allows invoking any code that writes to the current default |
| 1953 |
* output stream and displaying the result in the window. |
| 1954 |
*/ |
| 1955 |
captureOutput(func) |
| 1956 |
{ |
| 1957 |
/* make my output stream the global default */ |
| 1958 |
local oldStr = outputManager.setOutputStream(outputStream_); |
| 1959 |
|
| 1960 |
/* make sure we restore the default output stream on the way out */ |
| 1961 |
try |
| 1962 |
{ |
| 1963 |
/* invoke the callback function */ |
| 1964 |
(func)(); |
| 1965 |
} |
| 1966 |
finally |
| 1967 |
{ |
| 1968 |
/* restore the original default output stream */ |
| 1969 |
outputManager.setOutputStream(oldStr); |
| 1970 |
} |
| 1971 |
} |
| 1972 |
|
| 1973 |
/* |
| 1974 |
* Make my output stream the default in the output manager. Returns |
| 1975 |
* the previous default output stream; the caller can note the return |
| 1976 |
* value and use it later to restore the original output stream via a |
| 1977 |
* call to outputManager.setOutputStream(), if desired. |
| 1978 |
*/ |
| 1979 |
setOutputStream() |
| 1980 |
{ |
| 1981 |
/* set my stream as the default */ |
| 1982 |
return outputManager.setOutputStream(outputStream_); |
| 1983 |
} |
| 1984 |
|
| 1985 |
/* |
| 1986 |
* Create our output stream. We'll create the appropriate output |
| 1987 |
* stream subclass and set it up with our default output filters. |
| 1988 |
* Subclasses can override this as needed to customize the filters. |
| 1989 |
*/ |
| 1990 |
createOutputStream() |
| 1991 |
{ |
| 1992 |
/* create a banner output stream */ |
| 1993 |
outputStream_ = createOutputStreamObj(); |
| 1994 |
|
| 1995 |
/* set up the default filters */ |
| 1996 |
outputStream_.addOutputFilter(typographicalOutputFilter); |
| 1997 |
outputStream_.addOutputFilter(new transient ParagraphManager()); |
| 1998 |
outputStream_.addOutputFilter(styleTagFilter); |
| 1999 |
outputStream_.addOutputFilter(langMessageBuilder); |
| 2000 |
} |
| 2001 |
|
| 2002 |
/* |
| 2003 |
* Create the output stream object. Subclasses can override this to |
| 2004 |
* create the appropriate stream subclass. Note that the stream |
| 2005 |
* should always be created as a transient object. |
| 2006 |
*/ |
| 2007 |
createOutputStreamObj() { return new transient OutputStream(); } |
| 2008 |
|
| 2009 |
/* |
| 2010 |
* My output stream - this is a transient OutputStream instance. |
| 2011 |
* Subclasses must create this explicitly by calling |
| 2012 |
* createOutputStream() when the underlying UI window is first |
| 2013 |
* created. |
| 2014 |
*/ |
| 2015 |
outputStream_ = nil |
| 2016 |
; |
| 2017 |
|