tsets.nim 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. discard """
  2. output: '''
  3. Ha ein F ist in s!
  4. false
  5. '''
  6. """
  7. # Test the handling of sets
  8. import
  9. strutils
  10. proc testSets(s: var set[char]) =
  11. s = {'A', 'B', 'C', 'E'..'G'} + {'Z'} + s
  12. # test sets if the first element is different from 0:
  13. type
  14. TAZ = range['a'..'z']
  15. TAZset = set[TAZ]
  16. TTokType* = enum
  17. tkInvalid, tkEof,
  18. tkSymbol,
  19. tkAddr, tkAnd, tkAs, tkAsm, tkBlock, tkBreak, tkCase, tkCast, tkConst,
  20. tkContinue, tkConverter, tkDiscard, tkDiv, tkElif, tkElse, tkEnd, tkEnum,
  21. tkExcept, tkException, tkFinally, tkFor, tkFrom, tkGeneric, tkIf, tkImplies,
  22. tkImport, tkIn, tkInclude, tkIs, tkIsnot, tkIterator, tkLambda, tkMacro,
  23. tkMethod, tkMod, tkNil, tkNot, tkNotin, tkObject, tkOf, tkOr, tkOut, tkProc,
  24. tkPtr, tkRaise, tkRecord, tkRef, tkReturn, tkShl, tkShr, tkTemplate, tkTry,
  25. tkType, tkVar, tkWhen, tkWhere, tkWhile, tkWith, tkWithout, tkXor, tkYield,
  26. tkIntLit, tkInt8Lit, tkInt16Lit, tkInt32Lit, tkInt64Lit, tkFloatLit,
  27. tkFloat32Lit, tkFloat64Lit, tkStrLit, tkRStrLit, tkTripleStrLit, tkCharLit,
  28. tkRCharLit, tkParLe, tkParRi, tkBracketLe, tkBracketRi, tkCurlyLe,
  29. tkCurlyRi, tkBracketDotLe, tkBracketDotRi,
  30. tkCurlyDotLe, tkCurlyDotRi,
  31. tkParDotLe, tkParDotRi,
  32. tkComma, tkSemiColon, tkColon, tkEquals, tkDot, tkDotDot, tkHat, tkOpr,
  33. tkComment, tkAccent, tkInd, tkSad, tkDed,
  34. tkSpaces, tkInfixOpr, tkPrefixOpr, tkPostfixOpr
  35. TTokTypeRange = range[tkSymbol..tkDed]
  36. TTokTypes* = set[TTokTypeRange]
  37. const
  38. toktypes: TTokTypes = {TTokTypeRange(tkSymbol)..pred(tkIntLit),
  39. tkStrLit..tkTripleStrLit}
  40. var
  41. s: set[char]
  42. a: TAZset
  43. s = {'0'..'9'}
  44. testSets(s)
  45. if 'F' in s: write(stdout, "Ha ein F ist in s!\n")
  46. else: write(stdout, "BUG: F ist nicht in s!\n")
  47. a = {} #{'a'..'z'}
  48. for x in low(TAZ) .. high(TAZ):
  49. incl(a, x)
  50. if x in a: discard
  51. else: write(stdout, "BUG: something not in a!\n")
  52. for x in low(TTokTypeRange) .. high(TTokTypeRange):
  53. if x in tokTypes:
  54. discard
  55. #writeLine(stdout, "the token '$1' is in the set" % repr(x))
  56. #OUT Ha ein F ist in s!
  57. type
  58. TMsgKind* = enum
  59. errUnknown, errIllFormedAstX, errInternal, errCannotOpenFile, errGenerated,
  60. errXCompilerDoesNotSupportCpp, errStringLiteralExpected,
  61. errIntLiteralExpected, errInvalidCharacterConstant,
  62. errClosingTripleQuoteExpected, errClosingQuoteExpected,
  63. errTabulatorsAreNotAllowed, errInvalidToken, errLineTooLong,
  64. errInvalidNumber, errNumberOutOfRange, errNnotAllowedInCharacter,
  65. errClosingBracketExpected, errMissingFinalQuote, errIdentifierExpected,
  66. errNewlineExpected,
  67. errInvalidModuleName,
  68. errOperatorExpected, errTokenExpected, errStringAfterIncludeExpected,
  69. errRecursiveDependencyX, errOnOrOffExpected, errNoneSpeedOrSizeExpected,
  70. errInvalidPragma, errUnknownPragma, errInvalidDirectiveX,
  71. errAtPopWithoutPush, errEmptyAsm, errInvalidIndentation,
  72. errExceptionExpected, errExceptionAlreadyHandled,
  73. errYieldNotAllowedHere, errYieldNotAllowedInTryStmt,
  74. errInvalidNumberOfYieldExpr, errCannotReturnExpr, errAttemptToRedefine,
  75. errStmtInvalidAfterReturn, errStmtExpected, errInvalidLabel,
  76. errInvalidCmdLineOption, errCmdLineArgExpected, errCmdLineNoArgExpected,
  77. errInvalidVarSubstitution, errUnknownVar, errUnknownCcompiler,
  78. errOnOrOffExpectedButXFound, errNoneBoehmRefcExpectedButXFound,
  79. errNoneSpeedOrSizeExpectedButXFound, errGuiConsoleOrLibExpectedButXFound,
  80. errUnknownOS, errUnknownCPU, errGenOutExpectedButXFound,
  81. errArgsNeedRunOption, errInvalidMultipleAsgn, errColonOrEqualsExpected,
  82. errExprExpected, errUndeclaredIdentifier, errUseQualifier, errTypeExpected,
  83. errSystemNeeds, errExecutionOfProgramFailed, errNotOverloadable,
  84. errInvalidArgForX, errStmtHasNoEffect, errXExpectsTypeOrValue,
  85. errXExpectsArrayType, errIteratorCannotBeInstantiated, errExprXAmbiguous,
  86. errConstantDivisionByZero, errOrdinalTypeExpected,
  87. errOrdinalOrFloatTypeExpected, errOverOrUnderflow,
  88. errCannotEvalXBecauseIncompletelyDefined, errChrExpectsRange0_255,
  89. errDynlibRequiresExportc, errUndeclaredFieldX, errNilAccess,
  90. errIndexOutOfBounds, errIndexTypesDoNotMatch, errBracketsInvalidForType,
  91. errValueOutOfSetBounds, errFieldInitTwice, errFieldNotInit,
  92. errExprXCannotBeCalled, errExprHasNoType, errExprXHasNoType,
  93. errCastNotInSafeMode, errExprCannotBeCastedToX, errCommaOrParRiExpected,
  94. errCurlyLeOrParLeExpected, errSectionExpected, errRangeExpected,
  95. errMagicOnlyInSystem, errPowerOfTwoExpected,
  96. errStringMayNotBeEmpty, errCallConvExpected, errProcOnlyOneCallConv,
  97. errSymbolMustBeImported, errExprMustBeBool, errConstExprExpected,
  98. errDuplicateCaseLabel, errRangeIsEmpty, errSelectorMustBeOfCertainTypes,
  99. errSelectorMustBeOrdinal, errOrdXMustNotBeNegative, errLenXinvalid,
  100. errWrongNumberOfVariables, errExprCannotBeRaised, errBreakOnlyInLoop,
  101. errTypeXhasUnknownSize, errConstNeedsConstExpr, errConstNeedsValue,
  102. errResultCannotBeOpenArray, errSizeTooBig, errSetTooBig,
  103. errBaseTypeMustBeOrdinal, errInheritanceOnlyWithNonFinalObjects,
  104. errInheritanceOnlyWithEnums, errIllegalRecursionInTypeX,
  105. errCannotInstantiateX, errExprHasNoAddress, errXStackEscape,
  106. errVarForOutParamNeeded,
  107. errPureTypeMismatch, errTypeMismatch, errButExpected, errButExpectedX,
  108. errAmbiguousCallXYZ, errWrongNumberOfArguments,
  109. errXCannotBePassedToProcVar,
  110. errXCannotBeInParamDecl, errPragmaOnlyInHeaderOfProc, errImplOfXNotAllowed,
  111. errImplOfXexpected, errNoSymbolToBorrowFromFound, errDiscardValueX,
  112. errInvalidDiscard, errIllegalConvFromXtoY, errCannotBindXTwice,
  113. errInvalidOrderInArrayConstructor,
  114. errInvalidOrderInEnumX, errEnumXHasHoles, errExceptExpected, errInvalidTry,
  115. errOptionExpected, errXisNoLabel, errNotAllCasesCovered,
  116. errUnknownSubstitionVar, errComplexStmtRequiresInd, errXisNotCallable,
  117. errNoPragmasAllowedForX, errNoGenericParamsAllowedForX,
  118. errInvalidParamKindX, errDefaultArgumentInvalid, errNamedParamHasToBeIdent,
  119. errNoReturnTypeForX, errConvNeedsOneArg, errInvalidPragmaX,
  120. errXNotAllowedHere, errInvalidControlFlowX,
  121. errXisNoType, errCircumNeedsPointer, errInvalidExpression,
  122. errInvalidExpressionX, errEnumHasNoValueX, errNamedExprExpected,
  123. errNamedExprNotAllowed, errXExpectsOneTypeParam,
  124. errArrayExpectsTwoTypeParams, errInvalidVisibilityX, errInitHereNotAllowed,
  125. errXCannotBeAssignedTo, errIteratorNotAllowed, errXNeedsReturnType,
  126. errNoReturnTypeDeclared,
  127. errInvalidCommandX, errXOnlyAtModuleScope,
  128. errXNeedsParamObjectType,
  129. errTemplateInstantiationTooNested, errInstantiationFrom,
  130. errInvalidIndexValueForTuple, errCommandExpectsFilename,
  131. errMainModuleMustBeSpecified,
  132. errXExpected,
  133. errTIsNotAConcreteType,
  134. errInvalidSectionStart, errGridTableNotImplemented, errGeneralParseError,
  135. errNewSectionExpected, errWhitespaceExpected, errXisNoValidIndexFile,
  136. errCannotRenderX, errVarVarTypeNotAllowed, errInstantiateXExplicitly,
  137. errOnlyACallOpCanBeDelegator, errUsingNoSymbol,
  138. errMacroBodyDependsOnGenericTypes,
  139. errDestructorNotGenericEnough,
  140. errInlineIteratorsAsProcParams,
  141. errXExpectsTwoArguments,
  142. errXExpectsObjectTypes, errXcanNeverBeOfThisSubtype, errTooManyIterations,
  143. errCannotInterpretNodeX, errFieldXNotFound, errInvalidConversionFromTypeX,
  144. errAssertionFailed, errCannotGenerateCodeForX, errXRequiresOneArgument,
  145. errUnhandledExceptionX, errCyclicTree, errXisNoMacroOrTemplate,
  146. errXhasSideEffects, errIteratorExpected, errLetNeedsInit,
  147. errThreadvarCannotInit, errWrongSymbolX, errIllegalCaptureX,
  148. errXCannotBeClosure, errXMustBeCompileTime,
  149. errCannotInferTypeOfTheLiteral,
  150. errCannotInferReturnType,
  151. errGenericLambdaNotAllowed,
  152. errCompilerDoesntSupportTarget,
  153. errUser,
  154. warnCannotOpenFile,
  155. warnOctalEscape, warnXIsNeverRead, warnXmightNotBeenInit,
  156. warnDeprecated, warnConfigDeprecated,
  157. warnSmallLshouldNotBeUsed, warnUnknownMagic, warnRedefinitionOfLabel,
  158. warnUnknownSubstitutionX, warnLanguageXNotSupported,
  159. warnFieldXNotSupported, warnCommentXIgnored,
  160. warnNilStatement, warnTypelessParam,
  161. warnDifferentHeaps, warnWriteToForeignHeap, warnUnsafeCode,
  162. warnEachIdentIsTuple,
  163. warnProveInit, warnProveField, warnProveIndex, warnGcUnsafe, warnGcUnsafe2,
  164. warnUninit, warnGcMem, warnDestructor, warnLockLevel, warnResultShadowed,
  165. warnUser,
  166. hintSuccess, hintSuccessX,
  167. hintLineTooLong, hintXDeclaredButNotUsed, hintConvToBaseNotNeeded,
  168. hintConvFromXtoItselfNotNeeded, hintExprAlwaysX, hintQuitCalled,
  169. hintProcessing, hintCodeBegin, hintCodeEnd, hintConf, hintPath,
  170. hintConditionAlwaysTrue, hintName, hintPattern,
  171. hintUser
  172. const
  173. fatalMin* = errUnknown
  174. fatalMax* = errInternal
  175. errMin* = errUnknown
  176. errMax* = errUser
  177. warnMin* = warnCannotOpenFile
  178. warnMax* = pred(hintSuccess)
  179. hintMin* = hintSuccess
  180. hintMax* = high(TMsgKind)
  181. type
  182. TNoteKind* = range[warnMin..hintMax] # "notes" are warnings or hints
  183. TNoteKinds* = set[TNoteKind]
  184. var
  185. gNotes*: TNoteKinds = {low(TNoteKind)..high(TNoteKind)} -
  186. {warnUninit, warnProveField, warnProveIndex, warnGcUnsafe}
  187. #import compiler.msgs
  188. echo warnUninit in gNotes
  189. # 7555
  190. doAssert {-1.int8, -2, -2}.card == 2
  191. doAssert {1, 2, 2, 3..5, 4..6}.card == 6
  192. # merely test the alias
  193. doAssert {-1.int8, -2, -2}.len == 2
  194. doAssert {1, 2, 2, 3..5, 4..6}.len == 6
  195. type Foo = enum
  196. Foo1 = 0
  197. Foo2 = 1
  198. Foo3 = 3
  199. let x = { Foo1, Foo2 }
  200. # bug #8425