wordrecg.nim 7.1 KB

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