wordrecg.nim 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. from strutils import cmpIgnoreStyle
  15. # Keywords must be kept sorted and within a range
  16. type
  17. TSpecialWord* = enum
  18. wInvalid,
  19. wAddr, wAnd, wAs, wAsm,
  20. wBind, wBlock, wBreak, wCase, wCast, wConcept, wConst,
  21. wContinue, wConverter, wDefer, wDiscard, wDistinct, wDiv, wDo,
  22. wElif, wElse, wEnd, wEnum, wExcept, wExport,
  23. wFinally, wFor, wFrom, wFunc, wIf, wImport, wIn,
  24. wInclude, wInterface, wIs, wIsnot, wIterator, wLet,
  25. wMacro, wMethod, wMixin, wMod, wNil,
  26. wNot, wNotin, wObject, wOf, wOr, wOut, wProc, wPtr, wRaise, wRef, wReturn,
  27. wShl, wShr, wStatic, wTemplate, wTry, wTuple, wType, wUsing, wVar,
  28. wWhen, wWhile, wXor, wYield,
  29. wColon, wColonColon, wEquals, wDot, wDotDot,
  30. wStar, wMinus,
  31. wMagic, wThread, wFinal, wProfiler, wMemTracker, wObjChecks,
  32. wIntDefine, wStrDefine, wBoolDefine
  33. wImmediate, wConstructor, wDestructor, wDelegator, wOverride,
  34. wImportCpp, wImportObjC,
  35. wImportCompilerProc,
  36. wImportc, wImportJs, wExportc, wExportCpp, wExportNims, wIncompleteStruct, wRequiresInit,
  37. wAlign, wNodecl, wPure, wSideEffect, wHeader,
  38. wNoSideEffect, wGcSafe, wNoreturn, wMerge, wLib, wDynlib,
  39. wCompilerProc, wCore, wProcVar, wBase, wUsed,
  40. wFatal, wError, wWarning, wHint, wLine, wPush, wPop, wDefine, wUndef,
  41. wLineDir, wStackTrace, wLineTrace, wLink, wCompile,
  42. wLinksys, wDeprecated, wVarargs, wCallconv, wDebugger,
  43. wNimcall, wStdcall, wCdecl, wSafecall, wSyscall, wInline, wNoInline,
  44. wFastcall, wClosure, wNoconv, wOn, wOff, wChecks, wRangeChecks,
  45. wBoundChecks, wOverflowChecks, wNilChecks,
  46. wFloatChecks, wNanChecks, wInfChecks, wStyleChecks,
  47. wNonReloadable, wExecuteOnReload,
  48. wAssertions, wPatterns, wTrMacros, wWarnings,
  49. wHints, wOptimization, wRaises, wWrites, wReads, wSize, wEffects, wTags,
  50. wDeadCodeElimUnused, # deprecated, dead code elim always happens
  51. wSafecode, wPackage, wNoForward, wReorder, wNoRewrite, wNoDestroy,
  52. wPragma,
  53. wCompileTime, wNoInit,
  54. wPassc, wPassl, wBorrow, wDiscardable,
  55. wFieldChecks,
  56. wSubsChar, wAcyclic, wShallow, wUnroll, wLinearScanEnd, wComputedGoto,
  57. wInjectStmt, wExperimental,
  58. wWrite, wGensym, wInject, wDirty, wInheritable, wThreadVar, wEmit,
  59. wAsmNoStackFrame,
  60. wImplicitStatic, wGlobal, wCodegenDecl, wUnchecked, wGuard, wLocks,
  61. wPartial, wExplain, wLiftLocals,
  62. wAuto, wBool, wCatch, wChar, wClass, wCompl
  63. wConst_cast, wDefault, wDelete, wDouble, wDynamic_cast,
  64. wExplicit, wExtern, wFalse, wFloat, wFriend,
  65. wGoto, wInt, wLong, wMutable, wNamespace, wNew, wOperator,
  66. wPrivate, wProtected, wPublic, wRegister, wReinterpret_cast, wRestrict,
  67. wShort, wSigned, wSizeof, wStatic_cast, wStruct, wSwitch,
  68. wThis, wThrow, wTrue, wTypedef, wTypeid, wTypeof, wTypename,
  69. wUnion, wPacked, wUnsigned, wVirtual, wVoid, wVolatile, wWchar_t,
  70. wAlignas, wAlignof, wConstexpr, wDecltype, wNullptr, wNoexcept,
  71. wThread_local, wStatic_assert, wChar16_t, wChar32_t,
  72. wStdIn, wStdOut, wStdErr,
  73. wInOut, wByCopy, wByRef, wOneWay,
  74. wBitsize
  75. TSpecialWords* = set[TSpecialWord]
  76. const
  77. oprLow* = ord(wColon)
  78. oprHigh* = ord(wDotDot)
  79. nimKeywordsLow* = ord(wAsm)
  80. nimKeywordsHigh* = ord(wYield)
  81. ccgKeywordsLow* = ord(wAuto)
  82. ccgKeywordsHigh* = ord(wOneWay)
  83. cppNimSharedKeywords* = {
  84. wAsm, wBreak, wCase, wConst, wContinue, wDo, wElse, wEnum, wExport,
  85. wFor, wIf, wReturn, wStatic, wTemplate, wTry, wWhile, wUsing}
  86. specialWords*: array[low(TSpecialWord)..high(TSpecialWord), string] = ["",
  87. "addr", "and", "as", "asm",
  88. "bind", "block", "break", "case", "cast",
  89. "concept", "const", "continue", "converter",
  90. "defer", "discard", "distinct", "div", "do",
  91. "elif", "else", "end", "enum", "except", "export",
  92. "finally", "for", "from", "func", "if",
  93. "import", "in", "include", "interface", "is", "isnot", "iterator",
  94. "let",
  95. "macro", "method", "mixin", "mod", "nil", "not", "notin",
  96. "object", "of", "or",
  97. "out", "proc", "ptr", "raise", "ref", "return",
  98. "shl", "shr", "static",
  99. "template", "try", "tuple", "type", "using", "var",
  100. "when", "while", "xor",
  101. "yield",
  102. ":", "::", "=", ".", "..",
  103. "*", "-",
  104. "magic", "thread", "final", "profiler", "memtracker", "objchecks",
  105. "intdefine", "strdefine", "booldefine",
  106. "immediate", "constructor", "destructor", "delegator", "override",
  107. "importcpp", "importobjc",
  108. "importcompilerproc", "importc", "importjs", "exportc", "exportcpp", "exportnims",
  109. "incompletestruct",
  110. "requiresinit", "align", "nodecl", "pure", "sideeffect",
  111. "header", "nosideeffect", "gcsafe", "noreturn", "merge", "lib", "dynlib",
  112. "compilerproc", "core", "procvar", "base", "used",
  113. "fatal", "error", "warning", "hint", "line",
  114. "push", "pop", "define", "undef", "linedir", "stacktrace", "linetrace",
  115. "link", "compile", "linksys", "deprecated", "varargs",
  116. "callconv", "debugger", "nimcall", "stdcall",
  117. "cdecl", "safecall", "syscall", "inline", "noinline", "fastcall", "closure",
  118. "noconv", "on", "off", "checks", "rangechecks", "boundchecks",
  119. "overflowchecks", "nilchecks",
  120. "floatchecks", "nanchecks", "infchecks", "stylechecks",
  121. "nonreloadable", "executeonreload",
  122. "assertions", "patterns", "trmacros", "warnings", "hints",
  123. "optimization", "raises", "writes", "reads", "size", "effects", "tags",
  124. "deadcodeelim", # deprecated, dead code elim always happens
  125. "safecode", "package", "noforward", "reorder", "norewrite", "nodestroy",
  126. "pragma",
  127. "compiletime", "noinit",
  128. "passc", "passl", "borrow", "discardable", "fieldchecks",
  129. "subschar", "acyclic", "shallow", "unroll", "linearscanend",
  130. "computedgoto", "injectstmt", "experimental",
  131. "write", "gensym", "inject", "dirty", "inheritable", "threadvar", "emit",
  132. "asmnostackframe", "implicitstatic", "global", "codegendecl", "unchecked",
  133. "guard", "locks", "partial", "explain", "liftlocals",
  134. "auto", "bool", "catch", "char", "class", "compl",
  135. "const_cast", "default", "delete", "double",
  136. "dynamic_cast", "explicit", "extern", "false",
  137. "float", "friend", "goto", "int", "long", "mutable",
  138. "namespace", "new", "operator",
  139. "private", "protected", "public", "register", "reinterpret_cast", "restrict",
  140. "short", "signed", "sizeof", "static_cast", "struct", "switch",
  141. "this", "throw", "true", "typedef", "typeid", "typeof",
  142. "typename", "union", "packed", "unsigned", "virtual", "void", "volatile",
  143. "wchar_t",
  144. "alignas", "alignof", "constexpr", "decltype", "nullptr", "noexcept",
  145. "thread_local", "static_assert", "char16_t", "char32_t",
  146. "stdin", "stdout", "stderr",
  147. "inout", "bycopy", "byref", "oneway",
  148. "bitsize"
  149. ]
  150. proc findStr*(a: openArray[string], s: string): int =
  151. for i in low(a) .. high(a):
  152. if cmpIgnoreStyle(a[i], s) == 0:
  153. return i
  154. result = - 1
  155. proc canonPragmaSpelling*(w: TSpecialWord): string =
  156. case w
  157. of wNoSideEffect: "noSideEffect"
  158. of wImportCompilerProc: "importCompilerProc"
  159. of wIncompleteStruct: "incompleteStruct"
  160. of wRequiresInit: "requiresInit"
  161. of wSideEffect: "sideEffect"
  162. of wLineDir: "lineDir"
  163. of wStackTrace: "stackTrace"
  164. of wLineTrace: "lineTrace"
  165. of wRangeChecks: "rangeChecks"
  166. of wBoundChecks: "boundChecks"
  167. of wOverflowChecks: "overflowChecks"
  168. of wNilChecks: "nilChecks"
  169. of wFloatChecks: "floatChecks"
  170. of wNanChecks: "nanChecks"
  171. of wInfChecks: "infChecks"
  172. of wStyleChecks: "styleChecks"
  173. of wNonReloadable: "nonReloadable"
  174. of wExecuteOnReload: "executeOnReload"
  175. of wDeadCodeElimUnused: "deadCodeElim"
  176. of wCompileTime: "compileTime"
  177. of wFieldChecks: "fieldChecks"
  178. of wLinearScanEnd: "linearScanEnd"
  179. of wComputedGoto: "computedGoto"
  180. of wInjectStmt: "injectStmt"
  181. of wAsmNoStackFrame: "asmNoStackframe"
  182. of wImplicitStatic: "implicitStatic"
  183. of wCodegenDecl: "codegenDecl"
  184. of wLiftLocals: "liftLocals"
  185. else: specialWords[w]