summary refs log tree commit diff stats
path: root/compiler/wordrecg.nim
blob: ded02abe8c98b50ae9be2461ed530ba7935e2a7b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#
#
#           The Nim Compiler
#        (c) Copyright 2015 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

# This module contains a word recognizer, i.e. a simple
# procedure which maps special words to an enumeration.
# It is primarily needed because Pascal's case statement
# does not support strings. Without this the code would
# be slow and unreadable.

type
  TSpecialWord* = enum
    wInvalid = "",
    wAddr = "addr", wAnd = "and", wAs = "as", wAsm = "asm",
    wBind = "bind", wBlock = "block", wBreak = "break", wCase = "case", wCast = "cast",
    wConcept = "concept", wConst = "const", wContinue = "continue", wConverter = "converter",
    wDefer = "defer", wDiscard = "discard", wDistinct = "distinct", wDiv = "div", wDo = "do",
    wElif = "elif", wElse = "else", wEnd = "end", wEnum = "enum", wExcept = "except",
    wExport = "export", wFinally = "finally", wFor = "for", wFrom = "from", wFunc = "func",
    wIf = "if", wImport = "import", wIn = "in", wInclude = "include", wInterface = "interface",
    wIs = "is", wIsnot = "isnot",  wIterator = "iterator", wLet = "let", wMacro = "macro",
    wMethod = "method", wMixin = "mixin", wMod = "mod", wNil = "nil", wNot = "not", wNotin = "notin",
    wObject = "object", wOf = "of", wOr = "or", wOut = "out", wProc = "proc", wPtr = "ptr",
    wRaise = "raise", wRef = "ref", wReturn = "return", wShl = "shl", wShr = "shr", wStatic = "static",
    wTemplate = "template", wTry = "try", wTuple = "tuple", wType = "type", wUsing = "using",
    wVar = "var", wWhen = "when", wWhile = "while", wXor = "xor", wYield = "yield",

    wColon = ":", wColonColon = "::", wEquals = "=", wDot = ".", wDotDot = "..",
    wStar = "*", wMinus = "-",
    wMagic = "magic", wThread = "thread", wFinal = "final", wProfiler = "profiler",
    wMemTracker = "memtracker", wObjChecks = "objchecks",
    wIntDefine = "intdefine", wStrDefine = "strdefine", wBoolDefine = "booldefine",
    wCursor = "cursor", wNoalias = "noalias", wEffectsOf = "effectsOf",
    wUncheckedAssign = "uncheckedAssign",

    wImmediate = "immediate", wConstructor = "constructor", wDestructor = "destructor",
    wDelegator = "delegator", wOverride = "override", wImportCpp = "importcpp",
    wCppNonPod = "cppNonPod",
    wImportObjC = "importobjc", wImportCompilerProc = "importCompilerProc",
    wImportc = "importc", wImportJs = "importjs", wExportc = "exportc", wExportCpp = "exportcpp",
    wExportNims = "exportnims",
    wIncompleteStruct = "incompleteStruct", # deprecated
    wCompleteStruct = "completeStruct", wRequiresInit = "requiresInit", wAlign = "align",
    wNodecl = "nodecl", wPure = "pure", wSideEffect = "sideEffect", wHeader = "header",
    wNoSideEffect = "noSideEffect", wGcSafe = "gcsafe", wNoreturn = "noreturn",
    wNosinks = "nosinks", wMerge = "merge", wLib = "lib", wDynlib = "dynlib",
    wCompilerProc = "compilerproc", wCore = "core", wProcVar = "procvar",
    wBase = "base", wUsed = "used", wFatal = "fatal", wError = "error", wWarning = "warning",
    wHint = "hint",
    wWarningAsError = "warningAsError",
    wHintAsError = "hintAsError",
    wLine = "line", wPush = "push",
    wPop = "pop", wDefine = "define", wUndef = "undef", wLineDir = "lineDir",
    wStackTrace = "stackTrace", wLineTrace = "lineTrace", wLink = "link", wCompile = "compile",
    wLinksys = "linksys", wDeprecated = "deprecated", wVarargs = "varargs", wCallconv = "callconv",
    wDebugger = "debugger", wNimcall = "nimcall", wStdcall = "stdcall", wCdecl = "cdecl",
    wSafecall = "safecall", wSyscall = "syscall", wInline = "inline", wNoInline = "noinline",
    wFastcall = "fastcall", wThiscall = "thiscall", wClosure = "closure", wNoconv = "noconv",
    wOn = "on", wOff = "off", wChecks = "checks", wRangeChecks = "rangeChecks",
    wBoundChecks = "boundChecks", wOverflowChecks = "overflowChecks", wNilChecks = "nilChecks",
    wFloatChecks = "floatChecks", wNanChecks = "nanChecks", wInfChecks = "infChecks",
    wStyleChecks = "styleChecks", wStaticBoundchecks = "staticBoundChecks",
    wNonReloadable = "nonReloadable", wExecuteOnReload = "executeOnReload",

    wAssertions = "assertions", wPatterns = "patterns", wTrMacros = "trmacros",
    wSinkInference = "sinkInference", wWarnings = "warnings",
    wHints = "hints", wOptimization = "optimization", wRaises = "raises",
    wWrites = "writes", wReads = "reads", wSize = "size", wEffects = "effects", wTags = "tags",
    wForbids = "forbids", wRequires = "requires", wEnsures = "ensures", wInvariant = "invariant",
    wAssume = "assume", wAssert = "assert",
    wDeadCodeElimUnused = "deadCodeElim",  # deprecated, dead code elim always happens
    wSafecode = "safecode", wPackage = "package", wNoForward = "noforward", wReorder = "reorder",
    wNoRewrite = "norewrite", wNoDestroy = "nodestroy", wPragma = "pragma",
    wCompileTime = "compileTime", wNoInit = "noinit", wPassc = "passc", wPassl = "passl",
    wLocalPassc = "localPassC", wBorrow = "borrow", wDiscardable = "discardable",
    wFieldChecks = "fieldChecks", wSubsChar = "subschar", wAcyclic = "acyclic",
    wShallow = "shallow", wUnroll = "unroll", wLinearScanEnd = "linearScanEnd",
    wComputedGoto = "computedGoto", wExperimental = "experimental",
    wWrite = "write", wGensym = "gensym", wInject = "inject", wDirty = "dirty",
    wInheritable = "inheritable", wThreadVar = "threadvar", wEmit = "emit",
    wAsmNoStackFrame = "asmNoStackFrame", wImplicitStatic = "implicitStatic",
    wGlobal = "global", wCodegenDecl = "codegenDecl", wUnchecked = "unchecked",
    wGuard = "guard", wLocks = "locks", wPartial = "partial", wExplain = "explain",
    wLiftLocals = "liftlocals", wEnforceNoRaises = "enforceNoRaises",

    wAuto = "auto", wBool = "bool", wCatch = "catch", wChar = "char",
    wClass = "class", wCompl = "compl", wConstCast = "const_cast", wDefault = "default",
    wDelete = "delete", wDouble = "double", wDynamicCast = "dynamic_cast",
    wExplicit = "explicit", wExtern = "extern", wFalse = "false", wFloat = "float",
    wFriend = "friend", wGoto = "goto", wInt = "int", wLong = "long", wMutable = "mutable",
    wNamespace = "namespace", wNew = "new", wOperator = "operator", wPrivate = "private",
    wProtected = "protected", wPublic = "public", wRegister = "register",
    wReinterpretCast = "reinterpret_cast", wRestrict = "restrict", wShort = "short",
    wSigned = "signed", wSizeof = "sizeof", wStaticCast = "static_cast", wStruct = "struct",
    wSwitch = "switch", wThis = "this", wThrow = "throw", wTrue = "true", wTypedef = "typedef",
    wTypeid = "typeid", wTypeof = "typeof",  wTypename = "typename",
    wUnion = "union", wPacked = "packed", wUnsigned = "unsigned", wVirtual = "virtual",
    wVoid = "void", wVolatile = "volatile", wWchar = "wchar_t",

    wAlignas = "alignas", wAlignof = "alignof", wConstexpr = "constexpr", wDecltype = "decltype",
    wNullptr = "nullptr", wNoexcept = "noexcept",
    wThreadLocal = "thread_local", wStaticAssert = "static_assert",
    wChar16 = "char16_t", wChar32 = "char32_t",

    wStdIn = "stdin", wStdOut = "stdout", wStdErr = "stderr",

    wInOut = "inout", wByCopy = "bycopy", wByRef = "byref", wOneWay = "oneway",
    wBitsize = "bitsize", wImportHidden = "all",

  TSpecialWords* = set[TSpecialWord]

const
  oprLow* = ord(wColon)
  oprHigh* = ord(wDotDot)

  nimKeywordsLow* = ord(wAsm)
  nimKeywordsHigh* = ord(wYield)

  ccgKeywordsLow* = ord(wAuto)
  ccgKeywordsHigh* = ord(wOneWay)

  cppNimSharedKeywords* = {
    wAsm, wBreak, wCase, wConst, wContinue, wDo, wElse, wEnum, wExport,
    wFor, wIf, wReturn, wStatic, wTemplate, wTry, wWhile, wUsing}


const enumUtilsExist = compiles:
  import std/enumutils

when enumUtilsExist:
  from std/enumutils import genEnumCaseStmt
  from strutils import normalize
  proc findStr*[T: enum](a, b: static[T], s: string, default: T): T =
    genEnumCaseStmt(T, s, default, ord(a), ord(b), normalize)

else:
  from strutils import cmpIgnoreStyle
  proc findStr*[T: enum](a, b: static[T], s: string, default: T): T {.deprecated.} =
    # used for compiler bootstrapping only
    for i in a..b:
      if cmpIgnoreStyle($i, s) == 0:
        return i
    result = default
eta.path.java - include: package - match: '{{classcase_id}}' scope: support.class.java pop: true - include: any_POP after-object-type: - match: (?=<) set: [array-brackets, generic-type-invocation] - match: \.(?!\.) scope: punctuation.accessor.dot.java set: - match: (?=<) set: generic-type-invocation - match: (?:(class)\b|({{uppercase_id}})) captures: 1: variable.language.java 2: constant.other.java pop: true - match: '{{classcase_id}}' scope: support.class.java set: after-object-type - include: any_POP - include: array-brackets # Used in 'throws' and generic bounds object-type-reference: - match: '{{before_fqn}}' set: - meta_scope: meta.path.java - include: package - include: object-type-reference-no-fqn - include: object-type-reference-no-fqn object-type-reference-no-fqn: - match: '{{classcase_id}}' scope: support.class.java set: after-object-type-reference - include: any_POP after-object-type-reference: - match: (?=<) set: generic-type-invocation - match: \. scope: punctuation.accessor.dot.java set: object-type-reference-no-fqn - include: any_POP # Used in method's and generic's parameters object-and-array-types: - match: '{{before_fqn}}' push: - meta_scope: meta.path.java - include: package - include: object-and-array-types-no-fqn - match: \b({{primitives}})(?=\s*\[) scope: storage.type.primitive.java push: array-brackets - match: \b{{classcase_id}} scope: support.class.java push: after-object-and-array-types object-and-array-types-no-fqn: - match: '{{classcase_id}}' scope: support.class.java set: after-object-and-array-types - include: any_POP after-object-and-array-types: - match: (?=<) set: [array-brackets, generic-type-invocation] - match: \.(?!\.) scope: punctuation.accessor.dot.java set: object-and-array-types-no-fqn - include: array-brackets # Used in class-level 'extends' and 'implements' inherited-object-type-reference: - match: '{{before_fqn}}' set: - meta_scope: meta.path.java - match: '{{lowercase_id}}' scope: entity.other.inherited-class.package.java - include: punctuation-accessor-dot - include: inherited-object-type-reference-no-fqn - include: inherited-object-type-reference-no-fqn inherited-object-type-reference-no-fqn: - match: (?!class|extends|implements|interface){{id}} scope: entity.other.inherited-class.java set: after-inherited-object-type-reference - include: any_POP after-inherited-object-type-reference: - match: (?=<) set: generic-type-invocation - match: \. scope: punctuation.accessor.dot.java set: inherited-object-type-reference-no-fqn - include: any_POP generic-type-declaration: - match: < scope: punctuation.definition.generic.begin.java push: generic-type-parameter - include: any_POP generic-type-terminator: - include: illegal-semicolon # These characters can't appear in a generic. If we've matched # them then someone forgot to close it. - match: (?=[{}()]) pop: true - match: '>' scope: punctuation.definition.generic.end.java pop: true generic-type-parameter: - meta_scope: meta.generic.declaration.java - match: \b{{id}}\b scope: variable.parameter.type.java push: generic-type-bounds - include: generic-type-terminator generic-type-bounds: - match: (,)|(?=>) captures: 1: punctuation.separator.comma.java pop: true - match: \bextends\b scope: keyword.declaration.extends.java push: [generic-type-extends-multiple-bounds, object-type-reference] - match: \bsuper\b scope: keyword.declaration.super.java push: object-type-reference generic-type-extends-multiple-bounds: - match: '&' scope: keyword.operator.multiple-bounds.java set: [generic-type-extends-multiple-bounds, object-type-reference] - include: any_POP generic-type-invocation: - match: < scope: punctuation.definition.generic.begin.java set: generic-type-argument - include: any_POP generic-type-argument: - meta_scope: meta.generic.java - match: \? scope: keyword.operator.wildcard.java push: generic-type-bounds - include: generic-type-terminator - include: object-and-array-types - include: punctuation-separator-comma annotation-default: - match: \bdefault\b scope: keyword.declaration.default.java push: - meta_scope: meta.annotation.default.java - match: (?=;) pop: true - include: code parameters: - match: \bfinal\b scope: storage.modifier.java - include: annotations - include: primitive-types - include: object-and-array-types - match: \.\.\. scope: keyword.operator.variadic.java - match: '{{id}}' scope: variable.parameter.java push: array-brackets - include: punctuation-separator-comma lambdas: - match: (?={{lambda_lookahead}}) push: lambda-params lambda-params: - meta_scope: meta.function.anonymous.parameters.java - match: \( scope: punctuation.section.parens.begin.java set: - meta_scope: meta.function.anonymous.parameters.java - match: \) scope: punctuation.section.parens.end.java set: lambda-arrow - include: parameters - match: '{{id}}' scope: variable.parameter.java set: lambda-arrow lambda-arrow: - match: -> scope: storage.type.function.anonymous.java set: - meta_scope: meta.function.anonymous.body.java - match: (?=[)};]) pop: true - include: code parens: - match: \( scope: punctuation.section.parens.begin.java push: - meta_scope: meta.parens.java - match: \) scope: punctuation.section.parens.end.java pop: true - include: illegal-parens-terminators - include: code declaration-statement-parens: - match: \( scope: punctuation.section.parens.begin.java set: - meta_scope: meta.parens.java - match: \) scope: punctuation.section.parens.end.java pop: true - include: illegal-open-block - include: code-block - include: any_POP primitive-types: - match: \b{{primitives}}\b scope: storage.type.primitive.java push: array-brackets var-type: - match: \bvar\b scope: storage.type.var.java array-brackets: - match: \[\s*\] scope: storage.modifier.array.java - include: any_POP static-assignment: - match: \= scope: keyword.operator.assignment.java set: - meta_scope: meta.assignment.rhs.java - match: (?=[,;]) pop: true - include: before-next-field - include: code - include: stray-parens - include: any_POP assignment: - match: ([|&^*/+-]\=|\=(?!=)) scope: keyword.operator.assignment.java push: - meta_scope: meta.assignment.rhs.java - match: (?=;|\)|\}|,) pop: true - include: code static-code-block: - match: \{ scope: punctuation.section.block.begin.java push: - meta_scope: meta.static.body.java - match: \} scope: punctuation.section.block.end.java pop: true - include: code-block storage-modifiers: - match: \b{{storage_modifiers}}\b scope: storage.modifier.java stray-braces: - match: \} scope: invalid.illegal.stray-brace-end stray-parens: - match: \) scope: invalid.illegal.stray-parens-end strings: - match: \" scope: punctuation.definition.string.begin.java push: - meta_include_prototype: false - meta_scope: string.quoted.double.java - match: \" scope: punctuation.definition.string.end.java pop: true - include: strings-common - match: \' scope: punctuation.definition.string.begin.java push: - meta_include_prototype: false - meta_scope: string.quoted.single.java - match: \' scope: punctuation.definition.string.end.java pop: true - include: strings-common strings-common: - match: \n scope: invalid.illegal.newline.java pop: true - match: \\. scope: constant.character.escape.java module: - match: (?=\b(?:open\s+)?module\b) push: - - meta_scope: meta.module.java - include: immediate_POP - - match: \bopen\b scope: storage.modifier.java - match: \bmodule\b scope: storage.type.java set: [module-body, module-identifier-scope, module-identifier] module-identifier-scope: - meta_scope: meta.module.identifier.java - include: immediate_POP module-identifier: - match: '{{id}}' set: - - meta_scope: entity.name.module.java - include: immediate_POP - dot-separated-identifier - include: any_POP module-body: - match: \{ scope: punctuation.section.braces.begin.java set: - meta_scope: meta.module.body.java - include: module-body-content - match: \} scope: punctuation.section.braces.end.java pop: true - include: any_POP module-body-content: - match: \bexports\b scope: keyword.other.module.exports.java push: [exports-statement-scope, exports-or-opens-statement] - match: \bopens\b scope: keyword.other.module.opens.java push: [opens-statement-scope, exports-or-opens-statement] - match: \brequires\b scope: keyword.other.module.requires.java push: requires-statement - match: \buses\b scope: keyword.other.module.uses.java push: [uses-statement-scope, object-type-reference] - match: \bprovides\b scope: keyword.other.module.provides.java push: [provides-statement-scope, provides-with-statement, object-type-reference] - include: punctuation-terminator-semicolon # Should always come before module/package patterns module-statement-terminator: - match: (?=[;\}]) pop: true - match: (?=\b(?:requires|exports|uses|provides|opens)\b) pop: true support-type-module: - match: '{{id}}' push: - - meta_scope: support.type.module.java - include: immediate_POP - dot-separated-identifier exports-statement-scope: - meta_scope: meta.exports.java - include: immediate_POP opens-statement-scope: - meta_scope: meta.opens.java - include: immediate_POP exports-or-opens-statement: - match: \bto\b scope: keyword.other.module.to.java set: - include: module-statement-terminator - include: support-type-module - include: punctuation-separator-comma - include: module-statement-terminator - match: '{{id}}' push: - - meta_scope: support.type.package.java - include: immediate_POP - dot-separated-identifier requires-statement: - meta_scope: meta.requires.java - match: \btransitive\b scope: keyword.other.module.transitive.java - include: module-statement-terminator - include: support-type-module uses-statement-scope: - meta_scope: meta.uses.java - include: immediate_POP provides-statement-scope: - meta_scope: meta.provides.java - include: immediate_POP provides-with-statement: - match: \bwith\b scope: keyword.other.module.with.java set: - - match: \, scope: punctuation.separator.comma.java push: object-type-reference - include: any_POP - object-type-reference - include: any_POP