wordrecg.nim 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2015 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # This module contains a word recognizer, i.e. a simple
  10. # procedure which maps special words to an enumeration.
  11. # It is primarily needed because Pascal's case statement
  12. # does not support strings. Without this the code would
  13. # be slow and unreadable.
  14. type
  15. TSpecialWord* = enum
  16. wInvalid = "",
  17. wAddr = "addr", wAnd = "and", wAs = "as", wAsm = "asm",
  18. wBind = "bind", wBlock = "block", wBreak = "break", wCase = "case", wCast = "cast",
  19. wConcept = "concept", wConst = "const", wContinue = "continue", wConverter = "converter",
  20. wDefer = "defer", wDiscard = "discard", wDistinct = "distinct", wDiv = "div", wDo = "do",
  21. wElif = "elif", wElse = "else", wEnd = "end", wEnum = "enum", wExcept = "except",
  22. wExport = "export", wFinally = "finally", wFor = "for", wFrom = "from", wFunc = "func",
  23. wIf = "if", wImport = "import", wIn = "in", wInclude = "include", wInterface = "interface",
  24. wIs = "is", wIsnot = "isnot", wIterator = "iterator", wLet = "let", wMacro = "macro",
  25. wMethod = "method", wMixin = "mixin", wMod = "mod", wNil = "nil", wNot = "not", wNotin = "notin",
  26. wObject = "object", wOf = "of", wOr = "or", wOut = "out", wProc = "proc", wPtr = "ptr",
  27. wRaise = "raise", wRef = "ref", wReturn = "return", wShl = "shl", wShr = "shr", wStatic = "static",
  28. wTemplate = "template", wTry = "try", wTuple = "tuple", wType = "type", wUsing = "using",
  29. wVar = "var", wWhen = "when", wWhile = "while", wXor = "xor", wYield = "yield",
  30. wColon = ":", wColonColon = "::", wEquals = "=", wDot = ".", wDotDot = "..",
  31. wStar = "*", wMinus = "-",
  32. wMagic = "magic", wThread = "thread", wFinal = "final", wProfiler = "profiler",
  33. wMemTracker = "memtracker", wObjChecks = "objchecks",
  34. wIntDefine = "intdefine", wStrDefine = "strdefine", wBoolDefine = "booldefine",
  35. wCursor = "cursor", wNoalias = "noalias", wEffectsOf = "effectsOf",
  36. wUncheckedAssign = "uncheckedAssign",
  37. wImmediate = "immediate", wConstructor = "constructor", wDestructor = "destructor",
  38. wDelegator = "delegator", wOverride = "override", wImportCpp = "importcpp",
  39. wCppNonPod = "cppNonPod",
  40. wImportObjC = "importobjc", wImportCompilerProc = "importCompilerProc",
  41. wImportc = "importc", wImportJs = "importjs", wExportc = "exportc", wExportCpp = "exportcpp",
  42. wExportNims = "exportnims",
  43. wIncompleteStruct = "incompleteStruct", # deprecated
  44. wCompleteStruct = "completeStruct", wRequiresInit = "requiresInit", wAlign = "align",
  45. wNodecl = "nodecl", wPure = "pure", wSideEffect = "sideEffect", wHeader = "header",
  46. wNoSideEffect = "noSideEffect", wGcSafe = "gcsafe", wNoreturn = "noreturn",
  47. wNosinks = "nosinks", wMerge = "merge", wLib = "lib", wDynlib = "dynlib",
  48. wCompilerProc = "compilerproc", wCore = "core", wProcVar = "procvar",
  49. wBase = "base", wUsed = "used", wFatal = "fatal", wError = "error", wWarning = "warning",
  50. wHint = "hint",
  51. wWarningAsError = "warningAsError",
  52. wHintAsError = "hintAsError",
  53. wLine = "line", wPush = "push",
  54. wPop = "pop", wDefine = "define", wUndef = "undef", wLineDir = "lineDir",
  55. wStackTrace = "stackTrace", wLineTrace = "lineTrace", wLink = "link", wCompile = "compile",
  56. wLinksys = "linksys", wDeprecated = "deprecated", wVarargs = "varargs", wCallconv = "callconv",
  57. wDebugger = "debugger", wNimcall = "nimcall", wStdcall = "stdcall", wCdecl = "cdecl",
  58. wSafecall = "safecall", wSyscall = "syscall", wInline = "inline", wNoInline = "noinline",
  59. wFastcall = "fastcall", wThiscall = "thiscall", wClosure = "closure", wNoconv = "noconv",
  60. wOn = "on", wOff = "off", wChecks = "checks", wRangeChecks = "rangeChecks",
  61. wBoundChecks = "boundChecks", wOverflowChecks = "overflowChecks", wNilChecks = "nilChecks",
  62. wFloatChecks = "floatChecks", wNanChecks = "nanChecks", wInfChecks = "infChecks",
  63. wStyleChecks = "styleChecks", wStaticBoundchecks = "staticBoundChecks",
  64. wNonReloadable = "nonReloadable", wExecuteOnReload = "executeOnReload",
  65. wAssertions = "assertions", wPatterns = "patterns", wTrMacros = "trmacros",
  66. wSinkInference = "sinkInference", wWarnings = "warnings",
  67. wHints = "hints", wOptimization = "optimization", wRaises = "raises",
  68. wWrites = "writes", wReads = "reads", wSize = "size", wEffects = "effects", wTags = "tags",
  69. wForbids = "forbids", wRequires = "requires", wEnsures = "ensures", wInvariant = "invariant",
  70. wAssume = "assume", wAssert = "assert",
  71. wDeadCodeElimUnused = "deadCodeElim", # deprecated, dead code elim always happens
  72. wSafecode = "safecode", wPackage = "package", wNoForward = "noforward", wReorder = "reorder",
  73. wNoRewrite = "norewrite", wNoDestroy = "nodestroy", wPragma = "pragma",
  74. wCompileTime = "compileTime", wNoInit = "noinit", wPassc = "passc", wPassl = "passl",
  75. wLocalPassc = "localPassC", wBorrow = "borrow", wDiscardable = "discardable",
  76. wFieldChecks = "fieldChecks", wSubsChar = "subschar", wAcyclic = "acyclic",
  77. wShallow = "shallow", wUnroll = "unroll", wLinearScanEnd = "linearScanEnd",
  78. wComputedGoto = "computedGoto", wExperimental = "experimental", wDoctype = "doctype",
  79. wWrite = "write", wGensym = "gensym", wInject = "inject", wDirty = "dirty",
  80. wInheritable = "inheritable", wThreadVar = "threadvar", wEmit = "emit",
  81. wAsmNoStackFrame = "asmNoStackFrame", wImplicitStatic = "implicitStatic",
  82. wGlobal = "global", wCodegenDecl = "codegenDecl", wUnchecked = "unchecked",
  83. wGuard = "guard", wLocks = "locks", wPartial = "partial", wExplain = "explain",
  84. wLiftLocals = "liftlocals", wEnforceNoRaises = "enforceNoRaises", wSystemRaisesDefect = "systemRaisesDefect",
  85. wRedefine = "redefine", wCallsite = "callsite",
  86. wAuto = "auto", wBool = "bool", wCatch = "catch", wChar = "char",
  87. wClass = "class", wCompl = "compl", wConstCast = "const_cast", wDefault = "default",
  88. wDelete = "delete", wDouble = "double", wDynamicCast = "dynamic_cast",
  89. wExplicit = "explicit", wExtern = "extern", wFalse = "false", wFloat = "float",
  90. wFriend = "friend", wGoto = "goto", wInt = "int", wLong = "long", wMutable = "mutable",
  91. wNamespace = "namespace", wNew = "new", wOperator = "operator", wPrivate = "private",
  92. wProtected = "protected", wPublic = "public", wRegister = "register",
  93. wReinterpretCast = "reinterpret_cast", wRestrict = "restrict", wShort = "short",
  94. wSigned = "signed", wSizeof = "sizeof", wStaticCast = "static_cast", wStruct = "struct",
  95. wSwitch = "switch", wThis = "this", wThrow = "throw", wTrue = "true", wTypedef = "typedef",
  96. wTypeid = "typeid", wTypeof = "typeof", wTypename = "typename",
  97. wUnion = "union", wPacked = "packed", wUnsigned = "unsigned", wVirtual = "virtual",
  98. wVoid = "void", wVolatile = "volatile", wWchar = "wchar_t",
  99. wAlignas = "alignas", wAlignof = "alignof", wConstexpr = "constexpr", wDecltype = "decltype",
  100. wNullptr = "nullptr", wNoexcept = "noexcept",
  101. wThreadLocal = "thread_local", wStaticAssert = "static_assert",
  102. wChar16 = "char16_t", wChar32 = "char32_t",
  103. wStdIn = "stdin", wStdOut = "stdout", wStdErr = "stderr",
  104. wInOut = "inout", wByCopy = "bycopy", wByRef = "byref", wOneWay = "oneway",
  105. wBitsize = "bitsize", wImportHidden = "all",
  106. TSpecialWords* = set[TSpecialWord]
  107. const
  108. oprLow* = ord(wColon)
  109. oprHigh* = ord(wDotDot)
  110. nimKeywordsLow* = ord(wAsm)
  111. nimKeywordsHigh* = ord(wYield)
  112. ccgKeywordsLow* = ord(wAuto)
  113. ccgKeywordsHigh* = ord(wOneWay)
  114. cppNimSharedKeywords* = {
  115. wAsm, wBreak, wCase, wConst, wContinue, wDo, wElse, wEnum, wExport,
  116. wFor, wIf, wReturn, wStatic, wTemplate, wTry, wWhile, wUsing}
  117. const enumUtilsExist = compiles:
  118. import std/enumutils
  119. when enumUtilsExist:
  120. from std/enumutils import genEnumCaseStmt
  121. from strutils import normalize
  122. proc findStr*[T: enum](a, b: static[T], s: string, default: T): T =
  123. genEnumCaseStmt(T, s, default, ord(a), ord(b), normalize)
  124. else:
  125. from strutils import cmpIgnoreStyle
  126. proc findStr*[T: enum](a, b: static[T], s: string, default: T): T {.deprecated.} =
  127. # used for compiler bootstrapping only
  128. for i in a..b:
  129. if cmpIgnoreStyle($i, s) == 0:
  130. return i
  131. result = default