wordrecg.nim 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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, wAtomic,
  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, wGeneric, 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, 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,
  48. wAssertions, wPatterns, wWarnings,
  49. wHints, wOptimization, wRaises, wWrites, wReads, wSize, wEffects, wTags,
  50. wDeadCodeElim, wSafecode, wNoForward, wReorder, wNoRewrite,
  51. wPragma,
  52. wCompileTime, wNoInit,
  53. wPassc, wPassl, wBorrow, wDiscardable,
  54. wFieldChecks,
  55. wWatchPoint, wSubsChar,
  56. 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,
  62. wAuto, wBool, wCatch, wChar, wClass,
  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,
  67. wShort, wSigned, wSizeof, wStatic_cast, wStruct, wSwitch,
  68. wThis, wThrow, wTrue, wTypedef, wTypeid, 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", "atomic",
  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", "generic", "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", "intdefine", "strdefine",
  105. "destroy",
  106. "immediate", "constructor", "destructor", "delegator", "override",
  107. "importcpp", "importobjc",
  108. "importcompilerproc", "importc", "exportc", "exportnims",
  109. "incompletestruct",
  110. "requiresinit", "align", "nodecl", "pure", "sideeffect",
  111. "header", "nosideeffect", "gcsafe", "noreturn", "merge", "lib", "dynlib",
  112. "compilerproc", "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", "breakpoint", "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",
  121. "assertions", "patterns", "warnings", "hints",
  122. "optimization", "raises", "writes", "reads", "size", "effects", "tags",
  123. "deadcodeelim", "safecode", "noforward", "reorder", "norewrite",
  124. "pragma",
  125. "compiletime", "noinit",
  126. "passc", "passl", "borrow", "discardable", "fieldchecks",
  127. "watchpoint",
  128. "subschar", "acyclic", "shallow", "unroll", "linearscanend",
  129. "computedgoto", "injectstmt", "experimental",
  130. "write", "gensym", "inject", "dirty", "inheritable", "threadvar", "emit",
  131. "asmnostackframe", "implicitstatic", "global", "codegendecl", "unchecked",
  132. "guard", "locks", "partial", "explain",
  133. "auto", "bool", "catch", "char", "class",
  134. "const_cast", "default", "delete", "double",
  135. "dynamic_cast", "explicit", "extern", "false",
  136. "float", "friend", "goto", "int", "long", "mutable",
  137. "namespace", "new", "operator",
  138. "private", "protected", "public", "register", "reinterpret_cast",
  139. "short", "signed", "sizeof", "static_cast", "struct", "switch",
  140. "this", "throw", "true", "typedef", "typeid",
  141. "typename", "union", "packed", "unsigned", "virtual", "void", "volatile",
  142. "wchar_t",
  143. "alignas", "alignof", "constexpr", "decltype", "nullptr", "noexcept",
  144. "thread_local", "static_assert", "char16_t", "char32_t",
  145. "stdin", "stdout", "stderr",
  146. "inout", "bycopy", "byref", "oneway",
  147. "bitsize",
  148. ]
  149. proc findStr*(a: openArray[string], s: string): int =
  150. for i in countup(low(a), high(a)):
  151. if cmpIgnoreStyle(a[i], s) == 0:
  152. return i
  153. result = - 1