about summary refs log tree commit diff stats
path: root/src/chat_state.c
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2015-05-19 22:38:56 +0100
committerJames Booth <boothj5@gmail.com>2015-05-19 22:38:56 +0100
commit22e322e2f117fc6074706c28e5fdb193f951583e (patch)
treef9162d784365e21236d5b99c4ad412f59a0c460f /src/chat_state.c
parent9ce25c33b03b7ff8ca8cd1cc52d8b7d24d125703 (diff)
parent8d8b651cf293e18dd2c1f8fd8aad257e76d3c76a (diff)
downloadprofani-tty-22e322e2f117fc6074706c28e5fdb193f951583e.tar.gz
Merge branch 'master' into stabber-tests
Diffstat (limited to 'src/chat_state.c')
0 files changed, 0 insertions, 0 deletions
ef='#n116'>116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
#
#
#           The Nim Compiler
#        (c) Copyright 2013 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

import
  options, strutils, os, tables, ropes, platform, terminal

type
  TMsgKind* = enum
    errUnknown, errIllFormedAstX, errInternal, errCannotOpenFile, errGenerated,
    errXCompilerDoesNotSupportCpp, errStringLiteralExpected,
    errIntLiteralExpected, errInvalidCharacterConstant,
    errClosingTripleQuoteExpected, errClosingQuoteExpected,
    errTabulatorsAreNotAllowed, errInvalidToken, errLineTooLong,
    errInvalidNumber, errInvalidNumberOctalCode, errNumberOutOfRange,
    errNnotAllowedInCharacter, errClosingBracketExpected, errMissingFinalQuote,
    errIdentifierExpected, errNewlineExpected, errInvalidModuleName,
    errOperatorExpected, errTokenExpected, errStringAfterIncludeExpected,
    errRecursiveDependencyX, errOnOrOffExpected, errNoneSpeedOrSizeExpected,
    errInvalidPragma, errUnknownPragma, errInvalidDirectiveX,
    errAtPopWithoutPush, errEmptyAsm, errInvalidIndentation,
    errExceptionExpected, errExceptionAlreadyHandled,
    errYieldNotAllowedHere, errYieldNotAllowedInTryStmt,
    errInvalidNumberOfYieldExpr, errCannotReturnExpr, errAttemptToRedefine,
    errStmtInvalidAfterReturn, errStmtExpected, errInvalidLabel,
    errInvalidCmdLineOption, errCmdLineArgExpected, errCmdLineNoArgExpected,
    errInvalidVarSubstitution, errUnknownVar, errUnknownCcompiler,
    errOnOrOffExpectedButXFound, errNoneBoehmRefcExpectedButXFound,
    errNoneSpeedOrSizeExpectedButXFound, errGuiConsoleOrLibExpectedButXFound,
    errUnknownOS, errUnknownCPU, errGenOutExpectedButXFound,
    errArgsNeedRunOption, errInvalidMultipleAsgn, errColonOrEqualsExpected,
    errExprExpected, errUndeclaredIdentifier, errUndeclaredField,
    errUndeclaredRoutine, errUseQualifier,
    errTypeExpected,
    errSystemNeeds, errExecutionOfProgramFailed, errNotOverloadable,
    errInvalidArgForX, errStmtHasNoEffect, errXExpectsTypeOrValue,
    errXExpectsArrayType, errIteratorCannotBeInstantiated, errExprXAmbiguous,
    errConstantDivisionByZero, errOrdinalTypeExpected,
    errOrdinalOrFloatTypeExpected, errOverOrUnderflow,
    errCannotEvalXBecauseIncompletelyDefined, errChrExpectsRange0_255,
    errDynlibRequiresExportc, errUndeclaredFieldX, errNilAccess,
    errIndexOutOfBounds, errIndexTypesDoNotMatch, errBracketsInvalidForType,
    errValueOutOfSetBounds, errFieldInitTwice, errFieldNotInit,
    errExprXCannotBeCalled, errExprHasNoType, errExprXHasNoType,
    errCastNotInSafeMode, errExprCannotBeCastedToX, errCommaOrParRiExpected,
    errCurlyLeOrParLeExpected, errSectionExpected, errRangeExpected,
    errMagicOnlyInSystem, errPowerOfTwoExpected,
    errStringMayNotBeEmpty, errCallConvExpected, errProcOnlyOneCallConv,
    errSymbolMustBeImported, errExprMustBeBool, errConstExprExpected,
    errDuplicateCaseLabel, errRangeIsEmpty, errSelectorMustBeOfCertainTypes,
    errSelectorMustBeOrdinal, errOrdXMustNotBeNegative, errLenXinvalid,
    errWrongNumberOfVariables, errExprCannotBeRaised, errBreakOnlyInLoop,
    errTypeXhasUnknownSize, errConstNeedsConstExpr, errConstNeedsValue,
    errResultCannotBeOpenArray, errSizeTooBig, errSetTooBig,
    errBaseTypeMustBeOrdinal, errInheritanceOnlyWithNonFinalObjects,
    errInheritanceOnlyWithEnums, errIllegalRecursionInTypeX,
    errCannotInstantiateX, errExprHasNoAddress, errXStackEscape,
    errVarForOutParamNeeded,
    errPureTypeMismatch, errTypeMismatch, errButExpected, errButExpectedX,
    errAmbiguousCallXYZ, errWrongNumberOfArguments,
    errXCannotBePassedToProcVar,
    errXCannotBeInParamDecl, errPragmaOnlyInHeaderOfProc, errImplOfXNotAllowed,
    errImplOfXexpected, errNoSymbolToBorrowFromFound, errDiscardValueX,
    errInvalidDiscard, errIllegalConvFromXtoY, errCannotBindXTwice,
    errInvalidOrderInArrayConstructor,
    errInvalidOrderInEnumX, errEnumXHasHoles, errExceptExpected, errInvalidTry,
    errOptionExpected, errXisNoLabel, errNotAllCasesCovered,
    errUnknownSubstitionVar, errComplexStmtRequiresInd, errXisNotCallable,
    errNoPragmasAllowedForX, errNoGenericParamsAllowedForX,
    errInvalidParamKindX, errDefaultArgumentInvalid, errNamedParamHasToBeIdent,
    errNoReturnTypeForX, errConvNeedsOneArg, errInvalidPragmaX,
    errXNotAllowedHere, errInvalidControlFlowX,
    errXisNoType, errCircumNeedsPointer, errInvalidExpression,
    errInvalidExpressionX, errEnumHasNoValueX, errNamedExprExpected,
    errNamedExprNotAllowed, errXExpectsOneTypeParam,
    errArrayExpectsTwoTypeParams, errInvalidVisibilityX, errInitHereNotAllowed,
    errXCannotBeAssignedTo, errIteratorNotAllowed, errXNeedsReturnType,
    errNoReturnTypeDeclared,
    errInvalidCommandX, errXOnlyAtModuleScope,
    errXNeedsParamObjectType,
    errTemplateInstantiationTooNested, errInstantiationFrom,
    errInvalidIndexValueForTuple, errCommandExpectsFilename,
    errMainModuleMustBeSpecified,
    errXExpected,
    errTIsNotAConcreteType,
    errInvalidSectionStart, errGridTableNotImplemented, errGeneralParseError,
    errNewSectionExpected, errWhitespaceExpected, errXisNoValidIndexFile,
    errCannotRenderX, errVarVarTypeNotAllowed, errInstantiateXExplicitly,
    errOnlyACallOpCanBeDelegator, errUsingNoSymbol,
    errMacroBodyDependsOnGenericTypes,
    errDestructorNotGenericEnough,
    errInlineIteratorsAsProcParams,
    errXExpectsTwoArguments,
    errXExpectsObjectTypes, errXcanNeverBeOfThisSubtype, errTooManyIterations,
    errCannotInterpretNodeX, errFieldXNotFound, errInvalidConversionFromTypeX,
    errAssertionFailed, errCannotGenerateCodeForX, errXRequiresOneArgument,
    errUnhandledExceptionX, errCyclicTree, errXisNoMacroOrTemplate,
    errXhasSideEffects, errIteratorExpected, errLetNeedsInit,
    errThreadvarCannotInit, errWrongSymbolX, errIllegalCaptureX,
    errXCannotBeClosure, errXMustBeCompileTime,
    errCannotInferTypeOfTheLiteral,
    errCannotInferReturnType,
    errGenericLambdaNotAllowed,
    errCompilerDoesntSupportTarget,
    errUser,
    warnCannotOpenFile,
    warnOctalEscape, warnXIsNeverRead, warnXmightNotBeenInit,
    warnDeprecated, warnConfigDeprecated,
    warnSmallLshouldNotBeUsed, warnUnknownMagic, warnRedefinitionOfLabel,
    warnUnknownSubstitutionX, warnLanguageXNotSupported,
    warnFieldXNotSupported, warnCommentXIgnored,
    warnNilStatement, warnTypelessParam,
    warnDifferentHeaps, warnWriteToForeignHeap, warnUnsafeCode,
    warnEachIdentIsTuple, warnShadowIdent,
    warnProveInit, warnProveField, warnProveIndex, warnGcUnsafe, warnGcUnsafe2,
    warnUninit, warnGcMem, warnDestructor, warnLockLevel, warnResultShadowed,
    warnUser,
    hintSuccess, hintSuccessX,
    hintLineTooLong, hintXDeclaredButNotUsed, hintConvToBaseNotNeeded,
    hintConvFromXtoItselfNotNeeded, hintExprAlwaysX, hintQuitCalled,
    hintProcessing, hintCodeBegin, hintCodeEnd, hintConf, hintPath,
    hintConditionAlwaysTrue, hintName, hintPattern,
    hintUser

const
  MsgKindToStr*: array[TMsgKind, string] = [
    errUnknown: "unknown error",
    errIllFormedAstX: "illformed AST: $1",
    errInternal: "internal error: $1",
    errCannotOpenFile: "cannot open \'$1\'",
    errGenerated: "$1",
    errXCompilerDoesNotSupportCpp: "\'$1\' compiler does not support C++",
    errStringLiteralExpected: "string literal expected",
    errIntLiteralExpected: "integer literal expected",
    errInvalidCharacterConstant: "invalid character constant",
    errClosingTripleQuoteExpected: "closing \"\"\" expected, but end of file reached",
    errClosingQuoteExpected: "closing \" expected",
    errTabulatorsAreNotAllowed: "tabulators are not allowed",
    errInvalidToken: "invalid token: $1",
    errLineTooLong: "line too long",
    errInvalidNumber: "$1 is not a valid number",
    errInvalidNumberOctalCode: "$1 is not a valid number; did you mean octal? Then use one of '0o', '0c' or '0C'.",
    errNumberOutOfRange: "number $1 out of valid range",
    errNnotAllowedInCharacter: "\\n not allowed in character literal",
    errClosingBracketExpected: "closing ']' expected, but end of file reached",
    errMissingFinalQuote: "missing final \' for character literal",
    errIdentifierExpected: "identifier expected, but found \'$1\'",
    errNewlineExpected: "newline expected, but found \'$1\'",
    errInvalidModuleName: "invalid module name: '$1'",
    errOperatorExpected: "operator expected, but found \'$1\'",
    errTokenExpected: "\'$1\' expected",
    errStringAfterIncludeExpected: "string after \'include\' expected",
    errRecursiveDependencyX: "recursive dependency: \'$1\'",
    errOnOrOffExpected: "\'on\' or \'off\' expected",
    errNoneSpeedOrSizeExpected: "\'none\', \'speed\' or \'size\' expected",
    errInvalidPragma: "invalid pragma",
    errUnknownPragma: "unknown pragma: \'$1\'",
    errInvalidDirectiveX: "invalid directive: \'$1\'",
    errAtPopWithoutPush: "\'pop\' without a \'push\' pragma",
    errEmptyAsm: "empty asm statement",
    errInvalidIndentation: "invalid indentation",
    errExceptionExpected: "exception expected",
    errExceptionAlreadyHandled: "exception already handled",
    errYieldNotAllowedHere: "'yield' only allowed in an iterator",
    errYieldNotAllowedInTryStmt: "'yield' cannot be used within 'try' in a non-inlined iterator",
    errInvalidNumberOfYieldExpr: "invalid number of \'yield\' expressions",
    errCannotReturnExpr: "current routine cannot return an expression",
    errAttemptToRedefine: "redefinition of \'$1\'",
    errStmtInvalidAfterReturn: "statement not allowed after \'return\', \'break\', \'raise\' or \'continue'",
    errStmtExpected: "statement expected",
    errInvalidLabel: "\'$1\' is no label",
    errInvalidCmdLineOption: "invalid command line option: \'$1\'",
    errCmdLineArgExpected: "argument for command line option expected: \'$1\'",
    errCmdLineNoArgExpected: "invalid argument for command line option: \'$1\'",
    errInvalidVarSubstitution: "invalid variable substitution in \'$1\'",
    errUnknownVar: "unknown variable: \'$1\'",
    errUnknownCcompiler: "unknown C compiler: \'$1\'",
    errOnOrOffExpectedButXFound: "\'on\' or \'off\' expected, but \'$1\' found",
    errNoneBoehmRefcExpectedButXFound: "'none', 'boehm' or 'refc' expected, but '$1' found",
    errNoneSpeedOrSizeExpectedButXFound: "'none', 'speed' or 'size' expected, but '$1' found",
    errGuiConsoleOrLibExpectedButXFound: "'gui', 'console' or 'lib' expected, but '$1' found",
    errUnknownOS: "unknown OS: '$1'",
    errUnknownCPU: "unknown CPU: '$1'",
    errGenOutExpectedButXFound: "'c', 'c++' or 'yaml' expected, but '$1' found",
    errArgsNeedRunOption: "arguments can only be given if the '--run' option is selected",
    errInvalidMultipleAsgn: "multiple assignment is not allowed",
    errColonOrEqualsExpected: "\':\' or \'=\' expected, but found \'$1\'",
    errExprExpected: "expression expected, but found \'$1\'",
    errUndeclaredIdentifier: "undeclared identifier: \'$1\'",
    errUndeclaredField: "undeclared field: \'$1\'",
    errUndeclaredRoutine: "attempting to call undeclared routine: \'$1\'",
    errUseQualifier: "ambiguous identifier: \'$1\' -- use a qualifier",
    errTypeExpected: "type expected",
    errSystemNeeds: "system module needs \'$1\'",
    errExecutionOfProgramFailed: "execution of an external program failed",
    errNotOverloadable: "overloaded \'$1\' leads to ambiguous calls",
    errInvalidArgForX: "invalid argument for \'$1\'",
    errStmtHasNoEffect: "statement has no effect",
    errXExpectsTypeOrValue: "\'$1\' expects a type or value",
    errXExpectsArrayType: "\'$1\' expects an array type",
    errIteratorCannotBeInstantiated: "'$1' cannot be instantiated because its body has not been compiled yet",
    errExprXAmbiguous: "expression '$1' ambiguous in this context",
    errConstantDivisionByZero: "division by zero",
    errOrdinalTypeExpected: "ordinal type expected",
    errOrdinalOrFloatTypeExpected: "ordinal or float type expected",
    errOverOrUnderflow: "over- or underflow",
    errCannotEvalXBecauseIncompletelyDefined: "cannot evalutate '$1' because type is not defined completely",
    errChrExpectsRange0_255: "\'chr\' expects an int in the range 0..255",
    errDynlibRequiresExportc: "\'dynlib\' requires \'exportc\'",
    errUndeclaredFieldX: "undeclared field: \'$1\'",
    errNilAccess: "attempt to access a nil address",
    errIndexOutOfBounds: "index out of bounds",
    errIndexTypesDoNotMatch: "index types do not match",
    errBracketsInvalidForType: "\'[]\' operator invalid for this type",
    errValueOutOfSetBounds: "value out of set bounds",
    errFieldInitTwice: "field initialized twice: \'$1\'",
    errFieldNotInit: "field \'$1\' not initialized",
    errExprXCannotBeCalled: "expression \'$1\' cannot be called",
    errExprHasNoType: "expression has no type",
    errExprXHasNoType: "expression \'$1\' has no type (or is ambiguous)",
    errCastNotInSafeMode: "\'cast\' not allowed in safe mode",
    errExprCannotBeCastedToX: "expression cannot be casted to $1",
    errCommaOrParRiExpected: "',' or ')' expected",
    errCurlyLeOrParLeExpected: "\'{\' or \'(\' expected",
    errSectionExpected: "section (\'type\', \'proc\', etc.) expected",
    errRangeExpected: "range expected",
    errMagicOnlyInSystem: "\'magic\' only allowed in system module",
    errPowerOfTwoExpected: "power of two expected",
    errStringMayNotBeEmpty: "string literal may not be empty",
    errCallConvExpected: "calling convention expected",
    errProcOnlyOneCallConv: "a proc can only have one calling convention",
    errSymbolMustBeImported: "symbol must be imported if 'lib' pragma is used",
    errExprMustBeBool: "expression must be of type 'bool'",
    errConstExprExpected: "constant expression expected",
    errDuplicateCaseLabel: "duplicate case label",
    errRangeIsEmpty: "range is empty",
    errSelectorMustBeOfCertainTypes: "selector must be of an ordinal type, float or string",
    errSelectorMustBeOrdinal: "selector must be of an ordinal type",
    errOrdXMustNotBeNegative: "ord($1) must not be negative",
    errLenXinvalid: "len($1) must be less than 32768",
    errWrongNumberOfVariables: "wrong number of variables",
    errExprCannotBeRaised: "only a 'ref object' can be raised",
    errBreakOnlyInLoop: "'break' only allowed in loop construct",
    errTypeXhasUnknownSize: "type \'$1\' has unknown size",
    errConstNeedsConstExpr: "a constant can only be initialized with a constant expression",
    errConstNeedsValue: "a constant needs a value",
    errResultCannotBeOpenArray: "the result type cannot be on open array",
    errSizeTooBig: "computing the type\'s size produced an overflow",
    errSetTooBig: "set is too large",
    errBaseTypeMustBeOrdinal: "base type of a set must be an ordinal",
    errInheritanceOnlyWithNonFinalObjects: "inheritance only works with non-final objects",
    errInheritanceOnlyWithEnums: "inheritance only works with an enum",
    errIllegalRecursionInTypeX: "illegal recursion in type \'$1\'",
    errCannotInstantiateX: "cannot instantiate: \'$1\'",
    errExprHasNoAddress: "expression has no address",
    errXStackEscape: "address of '$1' may not escape its stack frame",
    errVarForOutParamNeeded: "for a \'var\' type a variable needs to be passed",
    errPureTypeMismatch: "type mismatch",
    errTypeMismatch: "type mismatch: got (",
    errButExpected: "but expected one of: ",
    errButExpectedX: "but expected \'$1\'",
    errAmbiguousCallXYZ: "ambiguous call; both $1 and $2 match for: $3",
    errWrongNumberOfArguments: "wrong number of arguments",
    errXCannotBePassedToProcVar: "\'$1\' cannot be passed to a procvar",
    errXCannotBeInParamDecl: "$1 cannot be declared in parameter declaration",
    errPragmaOnlyInHeaderOfProc: "pragmas are only allowed in the header of a proc",
    errImplOfXNotAllowed: "implementation of \'$1\' is not allowed",
    errImplOfXexpected: "implementation of \'$1\' expected",
    errNoSymbolToBorrowFromFound: "no symbol to borrow from found",
    errDiscardValueX: "value of type '$1' has to be discarded",
    errInvalidDiscard: "statement returns no value that can be discarded",
    errIllegalConvFromXtoY: "conversion from $1 to $2 is invalid",
    errCannotBindXTwice: "cannot bind parameter \'$1\' twice",
    errInvalidOrderInArrayConstructor: "invalid order in array constructor",
    errInvalidOrderInEnumX: "invalid order in enum \'$1\'",
    errEnumXHasHoles: "enum \'$1\' has holes",
    errExceptExpected: "\'except\' or \'finally\' expected",
    errInvalidTry: "after catch all \'except\' or \'finally\' no section may follow",
    errOptionExpected: "option expected, but found \'$1\'",
    errXisNoLabel: "\'$1\' is not a label",
    errNotAllCasesCovered: "not all cases are covered",
    errUnknownSubstitionVar: "unknown substitution variable: \'$1\'",
    errComplexStmtRequiresInd: "complex statement requires indentation",
    errXisNotCallable: "\'$1\' is not callable",
    errNoPragmasAllowedForX: "no pragmas allowed for $1",
    errNoGenericParamsAllowedForX: "no generic parameters allowed for $1",
    errInvalidParamKindX: "invalid param kind: \'$1\'",
    errDefaultArgumentInvalid: "default argument invalid",
    errNamedParamHasToBeIdent: "named parameter has to be an identifier",
    errNoReturnTypeForX: "no return type allowed for $1",
    errConvNeedsOneArg: "a type conversion needs exactly one argument",
    errInvalidPragmaX: "invalid pragma: $1",
    errXNotAllowedHere: "$1 not allowed here",
    errInvalidControlFlowX: "invalid control flow: $1",
    errXisNoType: "invalid type: \'$1\'",
    errCircumNeedsPointer: "'[]' needs a pointer or reference type",
    errInvalidExpression: "invalid expression",
    errInvalidExpressionX: "invalid expression: \'$1\'",
    errEnumHasNoValueX: "enum has no value \'$1\'",
    errNamedExprExpected: "named expression expected",
    errNamedExprNotAllowed: "named expression not allowed here",
    errXExpectsOneTypeParam: "\'$1\' expects one type parameter",
    errArrayExpectsTwoTypeParams: "array expects two type parameters",
    errInvalidVisibilityX: "invalid visibility: \'$1\'",
    errInitHereNotAllowed: "initialization not allowed here",
    errXCannotBeAssignedTo: "\'$1\' cannot be assigned to",
    errIteratorNotAllowed: "iterators can only be defined at the module\'s top level",
    errXNeedsReturnType: "$1 needs a return type",
    errNoReturnTypeDeclared: "no return type declared",
    errInvalidCommandX: "invalid command: \'$1\'",
    errXOnlyAtModuleScope: "\'$1\' is only allowed at top level",
    errXNeedsParamObjectType: "'$1' needs a parameter that has an object type",
    errTemplateInstantiationTooNested: "template/macro instantiation too nested",
    errInstantiationFrom: "template/generic instantiation from here",
    errInvalidIndexValueForTuple: "invalid index value for tuple subscript",
    errCommandExpectsFilename: "command expects a filename argument",
    errMainModuleMustBeSpecified: "please, specify a main module in the project configuration file",
    errXExpected: "\'$1\' expected",
    errTIsNotAConcreteType: "\'$1\' is not a concrete type.",
    errInvalidSectionStart: "invalid section start",
    errGridTableNotImplemented: "grid table is not implemented",
    errGeneralParseError: "general parse error",
    errNewSectionExpected: "new section expected",
    errWhitespaceExpected: "whitespace expected, got \'$1\'",
    errXisNoValidIndexFile: "\'$1\' is no valid index file",
    errCannotRenderX: "cannot render reStructuredText element \'$1\'",
    errVarVarTypeNotAllowed: "type \'var var\' is not allowed",
    errInstantiateXExplicitly: "instantiate '$1' explicitly",
    errOnlyACallOpCanBeDelegator: "only a call operator can be a delegator",
    errUsingNoSymbol: "'$1' is not a variable, constant or a proc name",
    errMacroBodyDependsOnGenericTypes: "the macro body cannot be compiled, " &
                                       "because the parameter '$1' has a generic type",
    errDestructorNotGenericEnough: "Destructor signature is too specific. " &
                                   "A destructor must be associated will all instantiations of a generic type",
    errInlineIteratorsAsProcParams: "inline iterators can be used as parameters only for " &
                                    "templates, macros and other inline iterators",
    errXExpectsTwoArguments: "\'$1\' expects two arguments",
    errXExpectsObjectTypes: "\'$1\' expects object types",
    errXcanNeverBeOfThisSubtype: "\'$1\' can never be of this subtype",
    errTooManyIterations: "interpretation requires too many iterations",
    errCannotInterpretNodeX: "cannot evaluate \'$1\'",
    errFieldXNotFound: "field \'$1\' cannot be found",
    errInvalidConversionFromTypeX: "invalid conversion from type \'$1\'",
    errAssertionFailed: "assertion failed",
    errCannotGenerateCodeForX: "cannot generate code for \'$1\'",
    errXRequiresOneArgument: "$1 requires one parameter",
    errUnhandledExceptionX: "unhandled exception: $1",
    errCyclicTree: "macro returned a cyclic abstract syntax tree",
    errXisNoMacroOrTemplate: "\'$1\' is no macro or template",
    errXhasSideEffects: "\'$1\' can have side effects",
    errIteratorExpected: "iterator within for loop context expected",
    errLetNeedsInit: "'let' symbol requires an initialization",
    errThreadvarCannotInit: "a thread var cannot be initialized explicitly",
    errWrongSymbolX: "usage of \'$1\' is a user-defined error",
    errIllegalCaptureX: "illegal capture '$1'",
    errXCannotBeClosure: "'$1' cannot have 'closure' calling convention",
    errXMustBeCompileTime: "'$1' can only be used in compile-time context",
    errCannotInferTypeOfTheLiteral: "cannot infer the type of the $1",
    errCannotInferReturnType: "cannot infer the return type of the proc",
    errGenericLambdaNotAllowed: "A nested proc can have generic parameters only when " &
                                "it is used as an operand to another routine and the types " &
                                "of the generic paramers can be inferred from the expected signature.",
    errCompilerDoesntSupportTarget: "The current compiler \'$1\' doesn't support the requested compilation target",
    errUser: "$1",
    warnCannotOpenFile: "cannot open \'$1\' [CannotOpenFile]",
    warnOctalEscape: "octal escape sequences do not exist; leading zero is ignored [OctalEscape]",
    warnXIsNeverRead: "\'$1\' is never read [XIsNeverRead]",
    warnXmightNotBeenInit: "\'$1\' might not have been initialized [XmightNotBeenInit]",
    warnDeprecated: "$1 is deprecated [Deprecated]",
    warnConfigDeprecated: "config file '$1' is deprecated [ConfigDeprecated]",
    warnSmallLshouldNotBeUsed: "\'l\' should not be used as an identifier; may look like \'1\' (one) [SmallLshouldNotBeUsed]",
    warnUnknownMagic: "unknown magic \'$1\' might crash the compiler [UnknownMagic]",
    warnRedefinitionOfLabel: "redefinition of label \'$1\' [RedefinitionOfLabel]",
    warnUnknownSubstitutionX: "unknown substitution \'$1\' [UnknownSubstitutionX]",
    warnLanguageXNotSupported: "language \'$1\' not supported [LanguageXNotSupported]",
    warnFieldXNotSupported: "field \'$1\' not supported [FieldXNotSupported]",
    warnCommentXIgnored: "comment \'$1\' ignored [CommentXIgnored]",
    warnNilStatement: "'nil' statement is deprecated; use an empty 'discard' statement instead [NilStmt]",
    warnTypelessParam: "'$1' has no type. Typeless parameters are deprecated; only allowed for 'template' [TypelessParam]",
    warnDifferentHeaps: "possible inconsistency of thread local heaps [DifferentHeaps]",
    warnWriteToForeignHeap: "write to foreign heap [WriteToForeignHeap]",
    warnUnsafeCode: "unsafe code: '$1' [UnsafeCode]",
    warnEachIdentIsTuple: "each identifier is a tuple [EachIdentIsTuple]",
    warnShadowIdent: "shadowed identifier: '$1' [ShadowIdent]",
    warnProveInit: "Cannot prove that '$1' is initialized. This will become a compile time error in the future. [ProveInit]",
    warnProveField: "cannot prove that field '$1' is accessible [ProveField]",
    warnProveIndex: "cannot prove index '$1' is valid [ProveIndex]",
    warnGcUnsafe: "not GC-safe: '$1' [GcUnsafe]",
    warnGcUnsafe2: "$1",
    warnUninit: "'$1' might not have been initialized [Uninit]",
    warnGcMem: "'$1' uses GC'ed memory [GcMem]",
    warnDestructor: "usage of a type with a destructor in a non destructible context. This will become a compile time error in the future. [Destructor]",
    warnLockLevel: "$1 [LockLevel]",
    warnResultShadowed: "Special variable 'result' is shadowed. [ResultShadowed]",
    warnUser: "$1 [User]",
    hintSuccess: "operation successful [Success]",
    hintSuccessX: "operation successful ($# lines compiled; $# sec total; $#; $#) [SuccessX]",
    hintLineTooLong: "line too long [LineTooLong]",
    hintXDeclaredButNotUsed: "\'$1\' is declared but not used [XDeclaredButNotUsed]",
    hintConvToBaseNotNeeded: "conversion to base object is not needed [ConvToBaseNotNeeded]",
    hintConvFromXtoItselfNotNeeded: "conversion from $1 to itself is pointless [ConvFromXtoItselfNotNeeded]",
    hintExprAlwaysX: "expression evaluates always to \'$1\' [ExprAlwaysX]",
    hintQuitCalled: "quit() called [QuitCalled]",
    hintProcessing: "$1 [Processing]",
    hintCodeBegin: "generated code listing: [CodeBegin]",
    hintCodeEnd: "end of listing [CodeEnd]",
    hintConf: "used config file \'$1\' [Conf]",
    hintPath: "added path: '$1' [Path]",
    hintConditionAlwaysTrue: "condition is always true: '$1' [CondTrue]",
    hintName: "name should be: '$1' [Name]",
    hintPattern: "$1 [Pattern]",
    hintUser: "$1 [User]"]

const
  WarningsToStr*: array[0..30, string] = ["CannotOpenFile", "OctalEscape",
    "XIsNeverRead", "XmightNotBeenInit",
    "Deprecated", "ConfigDeprecated",
    "SmallLshouldNotBeUsed", "UnknownMagic",
    "RedefinitionOfLabel", "UnknownSubstitutionX",
    "LanguageXNotSupported", "FieldXNotSupported",
    "CommentXIgnored", "NilStmt",
    "TypelessParam", "DifferentHeaps", "WriteToForeignHeap",
    "UnsafeCode", "EachIdentIsTuple", "ShadowIdent",
    "ProveInit", "ProveField", "ProveIndex", "GcUnsafe", "GcUnsafe2", "Uninit",
    "GcMem", "Destructor", "LockLevel", "ResultShadowed", "User"]

  HintsToStr*: array[0..16, string] = ["Success", "SuccessX", "LineTooLong",
    "XDeclaredButNotUsed", "ConvToBaseNotNeeded", "ConvFromXtoItselfNotNeeded",
    "ExprAlwaysX", "QuitCalled", "Processing", "CodeBegin", "CodeEnd", "Conf",
    "Path", "CondTrue", "Name", "Pattern",
    "User"]

const
  fatalMin* = errUnknown
  fatalMax* = errInternal
  errMin* = errUnknown
  errMax* = errUser
  warnMin* = warnCannotOpenFile
  warnMax* = pred(hintSuccess)
  hintMin* = hintSuccess
  hintMax* = high(TMsgKind)

type
  TNoteKind* = range[warnMin..hintMax] # "notes" are warnings or hints
  TNoteKinds* = set[TNoteKind]

  TFileInfo* = object
    fullPath: string           # This is a canonical full filesystem path
    projPath*: string          # This is relative to the project's root
    shortName*: string         # short name of the module
    quotedName*: Rope         # cached quoted short name for codegen
                               # purposes

    lines*: seq[Rope]         # the source code of the module
                               #   used for better error messages and
                               #   embedding the original source in the
                               #   generated code
    dirtyfile: string          # the file that is actually read into memory
                               # and parsed; usually 'nil' but is used
                               # for 'nimsuggest'

  TLineInfo* = object          # This is designed to be as small as possible,
                               # because it is used
                               # in syntax nodes. We save space here by using
                               # two int16 and an int32.
                               # On 64 bit and on 32 bit systems this is
                               # only 8 bytes.
    line*, col*: int16
    fileIndex*: int32

  TErrorOutput* = enum
    eStdOut
    eStdErr
    eInMemory

  TErrorOutputs* = set[TErrorOutput]

  ERecoverableError* = object of ValueError
  ESuggestDone* = object of Exception

const
  InvalidFileIDX* = int32(-1)

var
  filenameToIndexTbl = initTable[string, int32]()
  fileInfos*: seq[TFileInfo] = @[]
  systemFileIdx*: int32

proc toCChar*(c: char): string =
  case c
  of '\0'..'\x1F', '\x80'..'\xFF': result = '\\' & toOctal(c)
  of '\'', '\"', '\\': result = '\\' & c
  else: result = $(c)

proc makeCString*(s: string): Rope =
  const
    MaxLineLength = 64
  result = nil
  var res = newStringOfCap(int(s.len.toFloat * 1.1) + 1)
  add(res, "\"")
  for i in countup(0, len(s) - 1):
    if (i + 1) mod MaxLineLength == 0:
      add(res, '\"')
      add(res, tnl)
      add(res, '\"')
    add(res, toCChar(s[i]))
  add(res, '\"')
  add(result, rope(res))


proc newFileInfo(fullPath, projPath: string): TFileInfo =
  result.fullPath = fullPath
  #shallow(result.fullPath)
  result.projPath = projPath
  #shallow(result.projPath)
  let fileName = projPath.extractFilename
  result.shortName = fileName.changeFileExt("")
  result.quotedName = fileName.makeCString
  if optEmbedOrigSrc in gGlobalOptions or true:
    result.lines = @[]

proc fileInfoIdx*(filename: string; isKnownFile: var bool): int32 =
  var
    canon: string
    pseudoPath = false

  try:
    canon = canonicalizePath(filename)
    shallow(canon)
  except:
    canon = filename
    # The compiler uses "filenames" such as `command line` or `stdin`
    # This flag indicates that we are working with such a path here
    pseudoPath = true

  if filenameToIndexTbl.hasKey(canon):
    result = filenameToIndexTbl[canon]
  else:
    isKnownFile = false
    result = fileInfos.len.int32
    fileInfos.add(newFileInfo(canon, if pseudoPath: filename
                                     else: canon.shortenDir))
    filenameToIndexTbl[canon] = result

proc fileInfoIdx*(filename: string): int32 =
  var dummy: bool
  result = fileInfoIdx(filename, dummy)

proc newLineInfo*(fileInfoIdx: int32, line, col: int): TLineInfo =
  result.fileIndex = fileInfoIdx
  result.line = int16(line)
  result.col = int16(col)

proc newLineInfo*(filename: string, line, col: int): TLineInfo {.inline.} =
  result = newLineInfo(filename.fileInfoIdx, line, col)

fileInfos.add(newFileInfo("", "command line"))
var gCmdLineInfo* = newLineInfo(int32(0), 1, 1)

fileInfos.add(newFileInfo("", "compilation artifact"))
var gCodegenLineInfo* = newLineInfo(int32(1), 1, 1)

proc raiseRecoverableError*(msg: string) {.noinline, noreturn.} =
  raise newException(ERecoverableError, msg)

proc sourceLine*(i: TLineInfo): Rope

var
  gNotes*: TNoteKinds = {low(TNoteKind)..high(TNoteKind)} -
                        {warnShadowIdent, warnUninit,
                         warnProveField, warnProveIndex, warnGcUnsafe}
  gErrorCounter*: int = 0     # counts the number of errors
  gHintCounter*: int = 0
  gWarnCounter*: int = 0
  gErrorMax*: int = 1         # stop after gErrorMax errors

proc unknownLineInfo*(): TLineInfo =
  result.line = int16(-1)
  result.col = int16(-1)
  result.fileIndex = -1

var
  msgContext: seq[TLineInfo] = @[]
  lastError = unknownLineInfo()

  errorOutputs* = {eStdOut, eStdErr}
  writelnHook*: proc (output: string) {.closure.}

proc suggestWriteln*(s: string) =
  if eStdOut in errorOutputs:
    if isNil(writelnHook): writeln(stdout, s)
    else: writelnHook(s)

proc msgQuit*(x: int8) = quit x
proc msgQuit*(x: string) = quit x

proc suggestQuit*() =
  raise newException(ESuggestDone, "suggest done")

# this format is understood by many text editors: it is the same that
# Borland and Freepascal use
const
  PosErrorFormat* = "$1($2, $3) Error: "
  PosWarningFormat* = "$1($2, $3) Warning: "
  PosHintFormat* = "$1($2, $3) Hint: "
  PosContextFormat = "$1($2, $3) Info: "
  RawError* = "Error: "
  RawWarning* = "Warning: "
  RawHint* = "Hint: "

proc getInfoContextLen*(): int = return msgContext.len
proc setInfoContextLen*(L: int) = setLen(msgContext, L)

proc pushInfoContext*(info: TLineInfo) =
  msgContext.add(info)

proc popInfoContext*() =
  setLen(msgContext, len(msgContext) - 1)

proc getInfoContext*(index: int): TLineInfo =
  let L = msgContext.len
  let i = if index < 0: L + index else: index
  if i >=% L: result = unknownLineInfo()
  else: result = msgContext[i]

proc toFilename*(fileIdx: int32): string =
  if fileIdx < 0: result = "???"
  else: result = fileInfos[fileIdx].projPath

proc toFullPath*(fileIdx: int32): string =
  if fileIdx < 0: result = "???"
  else: result = fileInfos[fileIdx].fullPath

proc setDirtyFile*(fileIdx: int32; filename: string) =
  assert fileIdx >= 0
  fileInfos[fileIdx].dirtyFile = filename

proc toFullPathConsiderDirty*(fileIdx: int32): string =
  if fileIdx < 0:
    result = "???"
  elif not fileInfos[fileIdx].dirtyFile.isNil:
    result = fileInfos[fileIdx].dirtyFile
  else:
    result = fileInfos[fileIdx].fullPath

template toFilename*(info: TLineInfo): string =
  info.fileIndex.toFilename

template toFullPath*(info: TLineInfo): string =
  info.fileIndex.toFullPath

proc toMsgFilename*(info: TLineInfo): string =
  if info.fileIndex < 0:
    result = "???"
  elif gListFullPaths:
    result = fileInfos[info.fileIndex].fullPath
  else:
    result = fileInfos[info.fileIndex].projPath

proc toLinenumber*(info: TLineInfo): int {.inline.} =
  result = info.line

proc toColumn*(info: TLineInfo): int {.inline.} =
  result = info.col

proc toFileLine*(info: TLineInfo): string {.inline.} =
  result = info.toFilename & ":" & $info.line

proc toFileLineCol*(info: TLineInfo): string {.inline.} =
  result = info.toFilename & "(" & $info.line & "," & $info.col & ")"

proc `$`*(info: TLineInfo): string = toFileLineCol(info)

proc `??`* (info: TLineInfo, filename: string): bool =
  # only for debugging purposes
  result = filename in info.toFilename

var gTrackPos*: TLineInfo

proc outWriteln*(s: string) =
  ## Writes to stdout. Always.
  if eStdOut in errorOutputs: writeln(stdout, s)

proc msgWriteln*(s: string, color: ForegroundColor = fgWhite, coloredText: string = "") =
  ## Writes to stdout. If --stderr option is given, writes to stderr instead.

  #if gCmd == cmdIdeTools and optCDebug notin gGlobalOptions: return

  var hasColor = optUseColors in gGlobalOptions
  if not isNil(writelnHook):
    writelnHook(coloredText & s)
  else:
    if optStdout in gGlobalOptions:
      if eStdErr in errorOutputs:
        if hasColor: setForegroundColor(color)
        write(stderr, coloredText)
        if hasColor: resetAttributes()
        writeln(stderr, s)
    else:
      if eStdOut in errorOutputs:
        if hasColor: setForegroundColor(color)
        write(stdout, coloredText)
        if hasColor: resetAttributes()
        writeln(stdout, s)

proc coordToStr(coord: int): string =
  if coord == -1: result = "???"
  else: result = $coord

proc msgKindToString*(kind: TMsgKind): string =
  # later versions may provide translated error messages
  result = MsgKindToStr[kind]

proc getMessageStr(msg: TMsgKind, arg: string): string =
  result = msgKindToString(msg) % [arg]

type
  TErrorHandling = enum doNothing, doAbort, doRaise

proc handleError(msg: TMsgKind, eh: TErrorHandling, s: string) =
  template quit =
    if defined(debug) or gVerbosity >= 3 or msg == errInternal:
      if stackTraceAvailable() and isNil(writelnHook):
        writeStackTrace()
      else:
        msgWriteln("", fgRed, "No stack traceback available\nTo create a stacktrace, rerun compilation with ./koch temp " & options.command & " <file>")
    quit 1

  if msg >= fatalMin and msg <= fatalMax:
    quit()
  if msg >= errMin and msg <= errMax:
    inc(gErrorCounter)
    options.gExitcode = 1'i8
    if gErrorCounter >= gErrorMax:
      quit()
    elif eh == doAbort and gCmd != cmdIdeTools:
      quit()
    elif eh == doRaise:
      raiseRecoverableError(s)

proc `==`*(a, b: TLineInfo): bool =
  result = a.line == b.line and a.fileIndex == b.fileIndex

proc writeContext(lastinfo: TLineInfo) =
  var info = lastinfo
  for i in countup(0, len(msgContext) - 1):
    if msgContext[i] != lastinfo and msgContext[i] != info:
      msgWriteln(PosContextFormat % [toMsgFilename(msgContext[i]),
                                   coordToStr(msgContext[i].line),
                                   coordToStr(msgContext[i].col+1),
                                   getMessageStr(errInstantiationFrom, "")])
    info = msgContext[i]

proc ignoreMsgBecauseOfIdeTools(msg: TMsgKind): bool =
  msg >= errGenerated and gCmd == cmdIdeTools and optIdeDebug notin gGlobalOptions

proc rawMessage*(msg: TMsgKind, args: openArray[string]) =
  var
    frmt: string
    color: ForegroundColor
  case msg
  of errMin..errMax:
    writeContext(unknownLineInfo())
    frmt = RawError
    color = fgRed
  of warnMin..warnMax:
    if optWarns notin gOptions: return
    if msg notin gNotes: return
    writeContext(unknownLineInfo())
    frmt = RawWarning
    inc(gWarnCounter)
    color = fgYellow
  of hintMin..hintMax:
    if optHints notin gOptions: return
    if msg notin gNotes: return
    frmt = RawHint
    inc(gHintCounter)
    color = fgGreen
  let s = `%`(msgKindToString(msg), args)
  if not ignoreMsgBecauseOfIdeTools(msg):
    msgWriteln(s, color, frmt)
  handleError(msg, doAbort, s)

proc rawMessage*(msg: TMsgKind, arg: string) =
  rawMessage(msg, [arg])

proc writeSurroundingSrc(info: TLineInfo) =
  const indent = "  "
  msgWriteln(indent & $info.sourceLine)
  msgWriteln(indent & spaces(info.col) & '^')

proc formatMsg*(info: TLineInfo, msg: TMsgKind, arg: string): string =
  let frmt = case msg
             of warnMin..warnMax: PosWarningFormat
             of hintMin..hintMax: PosHintFormat
             else: PosErrorFormat
  result = frmt % [toMsgFilename(info), coordToStr(info.line),
                   coordToStr(info.col+1), getMessageStr(msg, arg)]

proc liMessage(info: TLineInfo, msg: TMsgKind, arg: string,
               eh: TErrorHandling) =
  var
    frmt: string
    ignoreMsg = false
    color: ForegroundColor
  case msg
  of errMin..errMax:
    writeContext(info)
    frmt = PosErrorFormat
    # we try to filter error messages so that not two error message
    # in the same file and line are produced:
    #ignoreMsg = lastError == info and eh != doAbort
    lastError = info
    color = fgRed
  of warnMin..warnMax:
    ignoreMsg = optWarns notin gOptions or msg notin gNotes
    if not ignoreMsg: writeContext(info)
    frmt = PosWarningFormat
    inc(gWarnCounter)
    color = fgYellow
  of hintMin..hintMax:
    ignoreMsg = optHints notin gOptions or msg notin gNotes
    frmt = PosHintFormat
    inc(gHintCounter)
    color = fgGreen
  # NOTE: currently line info line numbers start with 1,
  # but column numbers start with 0, however most editors expect
  # first column to be 1, so we need to +1 here
  let x = frmt % [toMsgFilename(info), coordToStr(info.line),
                  coordToStr(info.col+1)]
  let s = getMessageStr(msg, arg)
  if not ignoreMsg and not ignoreMsgBecauseOfIdeTools(msg):
    msgWriteln(s, color, x)
    if optPrintSurroundingSrc and msg in errMin..errMax:
      info.writeSurroundingSrc
  handleError(msg, eh, s)

proc fatal*(info: TLineInfo, msg: TMsgKind, arg = "") =
  liMessage(info, msg, arg, doAbort)

proc globalError*(info: TLineInfo, msg: TMsgKind, arg = "") =
  liMessage(info, msg, arg, doRaise)

proc globalError*(info: TLineInfo, arg: string) =
  liMessage(info, errGenerated, arg, doRaise)

proc localError*(info: TLineInfo, msg: TMsgKind, arg = "") =
  liMessage(info, msg, arg, doNothing)

proc localError*(info: TLineInfo, arg: string) =
  liMessage(info, errGenerated, arg, doNothing)

proc localError*(info: TLineInfo, format: string, params: openarray[string]) =
  localError(info, format % params)

proc message*(info: TLineInfo, msg: TMsgKind, arg = "") =
  liMessage(info, msg, arg, doNothing)

proc internalError*(info: TLineInfo, errMsg: string) =
  if gCmd == cmdIdeTools: return
  writeContext(info)
  liMessage(info, errInternal, errMsg, doAbort)

proc internalError*(errMsg: string) =
  if gCmd == cmdIdeTools: return
  writeContext(unknownLineInfo())
  rawMessage(errInternal, errMsg)

template assertNotNil*(e: expr): expr =
  if e == nil: internalError($instantiationInfo())
  e

template internalAssert*(e: bool): stmt =
  if not e: internalError($instantiationInfo())

proc addSourceLine*(fileIdx: int32, line: string) =
  fileInfos[fileIdx].lines.add line.rope

proc sourceLine*(i: TLineInfo): Rope =
  if i.fileIndex < 0: return nil

  if not optPreserveOrigSource and fileInfos[i.fileIndex].lines.len == 0:
    try:
      for line in lines(i.toFullPath):
        addSourceLine i.fileIndex, line.string
    except IOError:
      discard
  internalAssert i.fileIndex < fileInfos.len
  # can happen if the error points to EOF:
  if i.line > fileInfos[i.fileIndex].lines.len: return nil

  result = fileInfos[i.fileIndex].lines[i.line-1]

proc quotedFilename*(i: TLineInfo): Rope =
  internalAssert i.fileIndex >= 0
  result = fileInfos[i.fileIndex].quotedName

ropes.errorHandler = proc (err: RopesError, msg: string, useWarning: bool) =
  case err
  of rInvalidFormatStr:
    internalError("ropes: invalid format string: " & msg)
  of rCannotOpenFile:
    rawMessage(if useWarning: warnCannotOpenFile else: errCannotOpenFile, msg)