wordrecg.nim 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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, wCursor, wNoalias,
  33. wImmediate, wConstructor, wDestructor, wDelegator, wOverride,
  34. wImportCpp, wImportObjC,
  35. wImportCompilerProc,
  36. wImportc, wImportJs, wExportc, wExportCpp, wExportNims,
  37. wIncompleteStruct, # deprecated
  38. wCompleteStruct,
  39. wRequiresInit,
  40. wAlign, wNodecl, wPure, wSideEffect, wHeader,
  41. wNoSideEffect, wGcSafe, wNoreturn, wNosinks, wMerge, wLib, wDynlib,
  42. wCompilerProc, wCore, wProcVar, wBase, wUsed,
  43. wFatal, wError, wWarning, wHint, wWarningAsError, wLine, wPush, wPop, wDefine, wUndef,
  44. wLineDir, wStackTrace, wLineTrace, wLink, wCompile,
  45. wLinksys, wDeprecated, wVarargs, wCallconv, wDebugger,
  46. wNimcall, wStdcall, wCdecl, wSafecall, wSyscall, wInline, wNoInline,
  47. wFastcall, wThiscall, wClosure, wNoconv, wOn, wOff, wChecks, wRangeChecks,
  48. wBoundChecks, wOverflowChecks, wNilChecks,
  49. wFloatChecks, wNanChecks, wInfChecks, wStyleChecks, wStaticBoundchecks,
  50. wNonReloadable, wExecuteOnReload,
  51. wAssertions, wPatterns, wTrMacros, wSinkInference, wWarnings,
  52. wHints, wOptimization, wRaises, wWrites, wReads, wSize, wEffects, wTags,
  53. wRequires, wEnsures, wInvariant, wAssume, wAssert,
  54. wDeadCodeElimUnused, # deprecated, dead code elim always happens
  55. wSafecode, wPackage, wNoForward, wReorder, wNoRewrite, wNoDestroy,
  56. wPragma,
  57. wCompileTime, wNoInit,
  58. wPassc, wPassl, wLocalPassc, wBorrow, wDiscardable,
  59. wFieldChecks,
  60. wSubsChar, wAcyclic, wShallow, wUnroll, wLinearScanEnd, wComputedGoto,
  61. wInjectStmt, wExperimental,
  62. wWrite, wGensym, wInject, wDirty, wInheritable, wThreadVar, wEmit,
  63. wAsmNoStackFrame,
  64. wImplicitStatic, wGlobal, wCodegenDecl, wUnchecked, wGuard, wLocks,
  65. wPartial, wExplain, wLiftLocals,
  66. wAuto, wBool, wCatch, wChar, wClass, wCompl
  67. wConst_cast, wDefault, wDelete, wDouble, wDynamic_cast,
  68. wExplicit, wExtern, wFalse, wFloat, wFriend,
  69. wGoto, wInt, wLong, wMutable, wNamespace, wNew, wOperator,
  70. wPrivate, wProtected, wPublic, wRegister, wReinterpret_cast, wRestrict,
  71. wShort, wSigned, wSizeof, wStatic_cast, wStruct, wSwitch,
  72. wThis, wThrow, wTrue, wTypedef, wTypeid, wTypeof, wTypename,
  73. wUnion, wPacked, wUnsigned, wVirtual, wVoid, wVolatile, wWchar_t,
  74. wAlignas, wAlignof, wConstexpr, wDecltype, wNullptr, wNoexcept,
  75. wThread_local, wStatic_assert, wChar16_t, wChar32_t,
  76. wStdIn, wStdOut, wStdErr,
  77. wInOut, wByCopy, wByRef, wOneWay,
  78. wBitsize
  79. TSpecialWords* = set[TSpecialWord]
  80. const
  81. oprLow* = ord(wColon)
  82. oprHigh* = ord(wDotDot)
  83. nimKeywordsLow* = ord(wAsm)
  84. nimKeywordsHigh* = ord(wYield)
  85. ccgKeywordsLow* = ord(wAuto)
  86. ccgKeywordsHigh* = ord(wOneWay)
  87. cppNimSharedKeywords* = {
  88. wAsm, wBreak, wCase, wConst, wContinue, wDo, wElse, wEnum, wExport,
  89. wFor, wIf, wReturn, wStatic, wTemplate, wTry, wWhile, wUsing}
  90. specialWords*: array[TSpecialWord, string] = ["",
  91. "addr", "and", "as", "asm",
  92. "bind", "block", "break", "case", "cast",
  93. "concept", "const", "continue", "converter",
  94. "defer", "discard", "distinct", "div", "do",
  95. "elif", "else", "end", "enum", "except", "export",
  96. "finally", "for", "from", "func", "if",
  97. "import", "in", "include", "interface", "is", "isnot", "iterator",
  98. "let",
  99. "macro", "method", "mixin", "mod", "nil", "not", "notin",
  100. "object", "of", "or",
  101. "out", "proc", "ptr", "raise", "ref", "return",
  102. "shl", "shr", "static",
  103. "template", "try", "tuple", "type", "using", "var",
  104. "when", "while", "xor",
  105. "yield",
  106. ":", "::", "=", ".", "..",
  107. "*", "-",
  108. "magic", "thread", "final", "profiler", "memtracker", "objchecks",
  109. "intdefine", "strdefine", "booldefine", "cursor", "noalias",
  110. "immediate", "constructor", "destructor", "delegator", "override",
  111. "importcpp", "importobjc",
  112. "importCompilerProc", "importc", "importjs", "exportc", "exportcpp", "exportnims",
  113. "incompleteStruct",
  114. "completeStruct",
  115. "requiresInit", "align", "nodecl", "pure", "sideEffect",
  116. "header", "noSideEffect", "gcsafe", "noreturn", "nosinks", "merge", "lib", "dynlib",
  117. "compilerproc", "core", "procvar", "base", "used",
  118. "fatal", "error", "warning", "hint", "warningAsError", "line",
  119. "push", "pop", "define", "undef", "lineDir", "stackTrace", "lineTrace",
  120. "link", "compile", "linksys", "deprecated", "varargs",
  121. "callconv", "debugger", "nimcall", "stdcall",
  122. "cdecl", "safecall", "syscall", "inline", "noinline", "fastcall", "thiscall", "closure",
  123. "noconv", "on", "off", "checks", "rangeChecks", "boundChecks",
  124. "overflowChecks", "nilChecks",
  125. "floatChecks", "nanChecks", "infChecks", "styleChecks", "staticBoundChecks",
  126. "nonReloadable", "executeOnReload",
  127. "assertions", "patterns", "trmacros", "sinkinference", "warnings", "hints",
  128. "optimization", "raises", "writes", "reads", "size", "effects", "tags",
  129. "requires", "ensures", "invariant", "assume", "assert",
  130. "deadCodeElim", # deprecated, dead code elim always happens
  131. "safecode", "package", "noforward", "reorder", "norewrite", "nodestroy",
  132. "pragma",
  133. "compileTime", "noinit",
  134. "passc", "passl", "localPassC", "borrow", "discardable", "fieldChecks",
  135. "subschar", "acyclic", "shallow", "unroll", "linearScanEnd",
  136. "computedGoto", "injectStmt", "experimental",
  137. "write", "gensym", "inject", "dirty", "inheritable", "threadvar", "emit",
  138. "asmNoStackFrame", "implicitStatic", "global", "codegenDecl", "unchecked",
  139. "guard", "locks", "partial", "explain", "liftLocals",
  140. "auto", "bool", "catch", "char", "class", "compl",
  141. "const_cast", "default", "delete", "double",
  142. "dynamic_cast", "explicit", "extern", "false",
  143. "float", "friend", "goto", "int", "long", "mutable",
  144. "namespace", "new", "operator",
  145. "private", "protected", "public", "register", "reinterpret_cast", "restrict",
  146. "short", "signed", "sizeof", "static_cast", "struct", "switch",
  147. "this", "throw", "true", "typedef", "typeid", "typeof",
  148. "typename", "union", "packed", "unsigned", "virtual", "void", "volatile",
  149. "wchar_t",
  150. "alignas", "alignof", "constexpr", "decltype", "nullptr", "noexcept",
  151. "thread_local", "static_assert", "char16_t", "char32_t",
  152. "stdin", "stdout", "stderr",
  153. "inout", "bycopy", "byref", "oneway",
  154. "bitsize"
  155. ]
  156. proc findStr*(a: openArray[string], s: string): int =
  157. for i in low(a)..high(a):
  158. if cmpIgnoreStyle(a[i], s) == 0:
  159. return i
  160. result = - 1