wordrecg.nim 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. wUnderscore = "_",
  33. wMagic = "magic", wThread = "thread", wFinal = "final", wProfiler = "profiler",
  34. wMemTracker = "memtracker", wObjChecks = "objchecks",
  35. wIntDefine = "intdefine", wStrDefine = "strdefine", wBoolDefine = "booldefine",
  36. wCursor = "cursor", wNoalias = "noalias", wEffectsOf = "effectsOf",
  37. wUncheckedAssign = "uncheckedAssign", wRunnableExamples = "runnableExamples",
  38. wImmediate = "immediate", wConstructor = "constructor", wDestructor = "destructor",
  39. wDelegator = "delegator", wOverride = "override", wImportCpp = "importcpp",
  40. wCppNonPod = "cppNonPod",
  41. wImportObjC = "importobjc", wImportCompilerProc = "importCompilerProc",
  42. wImportc = "importc", wImportJs = "importjs", wExportc = "exportc", wExportCpp = "exportcpp",
  43. wExportNims = "exportnims",
  44. wIncompleteStruct = "incompleteStruct", # deprecated
  45. wCompleteStruct = "completeStruct", wRequiresInit = "requiresInit", wAlign = "align",
  46. wNodecl = "nodecl", wPure = "pure", wSideEffect = "sideEffect", wHeader = "header",
  47. wNoSideEffect = "noSideEffect", wGcSafe = "gcsafe", wNoreturn = "noreturn",
  48. wNosinks = "nosinks", wLib = "lib", wDynlib = "dynlib",
  49. wCompilerProc = "compilerproc", wCore = "core", wProcVar = "procvar",
  50. wBase = "base", wUsed = "used", wFatal = "fatal", wError = "error", wWarning = "warning",
  51. wHint = "hint",
  52. wWarningAsError = "warningAsError",
  53. wHintAsError = "hintAsError",
  54. wLine = "line", wPush = "push",
  55. wPop = "pop", wDefine = "define", wUndef = "undef", wLineDir = "lineDir",
  56. wStackTrace = "stackTrace", wLineTrace = "lineTrace", wLink = "link", wCompile = "compile",
  57. wLinksys = "linksys", wDeprecated = "deprecated", wVarargs = "varargs", wCallconv = "callconv",
  58. wDebugger = "debugger", wNimcall = "nimcall", wStdcall = "stdcall", wCdecl = "cdecl",
  59. wSafecall = "safecall", wSyscall = "syscall", wInline = "inline", wNoInline = "noinline",
  60. wFastcall = "fastcall", wThiscall = "thiscall", wClosure = "closure", wNoconv = "noconv",
  61. wOn = "on", wOff = "off", wChecks = "checks", wRangeChecks = "rangeChecks",
  62. wBoundChecks = "boundChecks", wOverflowChecks = "overflowChecks", wNilChecks = "nilChecks",
  63. wFloatChecks = "floatChecks", wNanChecks = "nanChecks", wInfChecks = "infChecks",
  64. wStyleChecks = "styleChecks", wStaticBoundchecks = "staticBoundChecks",
  65. wNonReloadable = "nonReloadable", wExecuteOnReload = "executeOnReload",
  66. wAssertions = "assertions", wPatterns = "patterns", wTrMacros = "trmacros",
  67. wSinkInference = "sinkInference", wWarnings = "warnings",
  68. wHints = "hints", wOptimization = "optimization", wRaises = "raises",
  69. wWrites = "writes", wReads = "reads", wSize = "size", wEffects = "effects", wTags = "tags",
  70. wForbids = "forbids", wRequires = "requires", wEnsures = "ensures", wInvariant = "invariant",
  71. wAssume = "assume", wAssert = "assert",
  72. wDeadCodeElimUnused = "deadCodeElim", # deprecated, dead code elim always happens
  73. wSafecode = "safecode", wPackage = "package", wNoForward = "noforward", wReorder = "reorder",
  74. wNoRewrite = "norewrite", wNoDestroy = "nodestroy", wPragma = "pragma",
  75. wCompileTime = "compileTime", wNoInit = "noinit", wPassc = "passc", wPassl = "passl",
  76. wLocalPassc = "localPassC", wBorrow = "borrow", wDiscardable = "discardable",
  77. wFieldChecks = "fieldChecks", wSubsChar = "subschar", wAcyclic = "acyclic",
  78. wShallow = "shallow", wUnroll = "unroll", wLinearScanEnd = "linearScanEnd",
  79. wComputedGoto = "computedGoto", wExperimental = "experimental", wDoctype = "doctype",
  80. wWrite = "write", wGensym = "gensym", wInject = "inject", wDirty = "dirty",
  81. wInheritable = "inheritable", wThreadVar = "threadvar", wEmit = "emit",
  82. wAsmNoStackFrame = "asmNoStackFrame", wAsmSyntax = "asmSyntax", wImplicitStatic = "implicitStatic",
  83. wGlobal = "global", wCodegenDecl = "codegenDecl", wUnchecked = "unchecked",
  84. wGuard = "guard", wLocks = "locks", wPartial = "partial", wExplain = "explain",
  85. wLiftLocals = "liftlocals", wEnforceNoRaises = "enforceNoRaises", wSystemRaisesDefect = "systemRaisesDefect",
  86. wRedefine = "redefine", wCallsite = "callsite",
  87. wQuirky = "quirky",
  88. # codegen keywords, but first the ones that are also pragmas:
  89. wExtern = "extern", wGoto = "goto", wRegister = "register",
  90. wUnion = "union", wPacked = "packed", wVirtual = "virtual",
  91. wVolatile = "volatile", wMember = "member",
  92. wByCopy = "bycopy", wByRef = "byref",
  93. # codegen keywords but not pragmas:
  94. wAuto = "auto", wBool = "bool", wCatch = "catch", wChar = "char",
  95. wClass = "class", wCompl = "compl", wConstCast = "const_cast", wDefault = "default",
  96. wDelete = "delete", wDouble = "double", wDynamicCast = "dynamic_cast",
  97. wExplicit = "explicit", wFalse = "false", wFloat = "float",
  98. wFriend = "friend", wInt = "int", wLong = "long", wMutable = "mutable",
  99. wNamespace = "namespace", wNew = "new", wOperator = "operator", wPrivate = "private",
  100. wProtected = "protected", wPublic = "public",
  101. wReinterpretCast = "reinterpret_cast", wRestrict = "restrict", wShort = "short",
  102. wSigned = "signed", wSizeof = "sizeof", wStaticCast = "static_cast", wStruct = "struct",
  103. wSwitch = "switch", wThis = "this", wThrow = "throw", wTrue = "true", wTypedef = "typedef",
  104. wTypeid = "typeid", wTypeof = "typeof", wTypename = "typename",
  105. wUnsigned = "unsigned", wVoid = "void",
  106. wAlignas = "alignas", wAlignof = "alignof", wConstexpr = "constexpr", wDecltype = "decltype",
  107. wNullptr = "nullptr", wNoexcept = "noexcept",
  108. wThreadLocal = "thread_local", wStaticAssert = "static_assert",
  109. wChar16 = "char16_t", wChar32 = "char32_t", wWchar = "wchar_t",
  110. wStdIn = "stdin", wStdOut = "stdout", wStdErr = "stderr",
  111. wInOut = "inout", wOneWay = "oneway",
  112. # end of codegen keywords
  113. wBitsize = "bitsize", wImportHidden = "all",
  114. wSendable = "sendable"
  115. TSpecialWords* = set[TSpecialWord]
  116. const
  117. oprLow* = ord(wColon)
  118. oprHigh* = ord(wDotDot)
  119. nimKeywordsLow* = ord(wAsm)
  120. nimKeywordsHigh* = ord(wYield)
  121. ccgKeywordsLow* = ord(wExtern)
  122. ccgKeywordsHigh* = ord(wOneWay)
  123. cppNimSharedKeywords* = {
  124. wAsm, wBreak, wCase, wConst, wContinue, wDo, wElse, wEnum, wExport,
  125. wFor, wIf, wReturn, wStatic, wTemplate, wTry, wWhile, wUsing}
  126. nonPragmaWordsLow* = wAuto
  127. nonPragmaWordsHigh* = wOneWay
  128. from std/enumutils import genEnumCaseStmt
  129. from std/strutils import normalize
  130. proc findStr*[T: enum](a, b: static[T], s: string, default: T): T =
  131. genEnumCaseStmt(T, s, default, ord(a), ord(b), normalize)