nimconf.nim 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2012 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # This module handles the reading of the config file.
  10. import
  11. llstream, commands, os, strutils, msgs, lexer,
  12. options, idents, wordrecg, strtabs, lineinfos, pathutils, scriptconfig
  13. # ---------------- configuration file parser -----------------------------
  14. # we use Nim's scanner here to save space and work
  15. proc ppGetTok(L: var Lexer, tok: var Token) =
  16. # simple filter
  17. rawGetTok(L, tok)
  18. while tok.tokType in {tkComment}: rawGetTok(L, tok)
  19. proc parseExpr(L: var Lexer, tok: var Token; config: ConfigRef): bool
  20. proc parseAtom(L: var Lexer, tok: var Token; config: ConfigRef): bool =
  21. if tok.tokType == tkParLe:
  22. ppGetTok(L, tok)
  23. result = parseExpr(L, tok, config)
  24. if tok.tokType == tkParRi: ppGetTok(L, tok)
  25. else: lexMessage(L, errGenerated, "expected closing ')'")
  26. elif tok.tokType == tkNot:
  27. ppGetTok(L, tok)
  28. result = not parseAtom(L, tok, config)
  29. else:
  30. result = isDefined(config, tok.ident.s)
  31. ppGetTok(L, tok)
  32. proc parseAndExpr(L: var Lexer, tok: var Token; config: ConfigRef): bool =
  33. result = parseAtom(L, tok, config)
  34. while tok.tokType == tkAnd:
  35. ppGetTok(L, tok) # skip "and"
  36. var b = parseAtom(L, tok, config)
  37. result = result and b
  38. proc parseExpr(L: var Lexer, tok: var Token; config: ConfigRef): bool =
  39. result = parseAndExpr(L, tok, config)
  40. while tok.tokType == tkOr:
  41. ppGetTok(L, tok) # skip "or"
  42. var b = parseAndExpr(L, tok, config)
  43. result = result or b
  44. proc evalppIf(L: var Lexer, tok: var Token; config: ConfigRef): bool =
  45. ppGetTok(L, tok) # skip 'if' or 'elif'
  46. result = parseExpr(L, tok, config)
  47. if tok.tokType == tkColon: ppGetTok(L, tok)
  48. else: lexMessage(L, errGenerated, "expected ':'")
  49. #var condStack: seq[bool] = @[]
  50. proc doEnd(L: var Lexer, tok: var Token; condStack: var seq[bool]) =
  51. if high(condStack) < 0: lexMessage(L, errGenerated, "expected @if")
  52. ppGetTok(L, tok) # skip 'end'
  53. setLen(condStack, high(condStack))
  54. type
  55. TJumpDest = enum
  56. jdEndif, jdElseEndif
  57. proc jumpToDirective(L: var Lexer, tok: var Token, dest: TJumpDest; config: ConfigRef;
  58. condStack: var seq[bool])
  59. proc doElse(L: var Lexer, tok: var Token; config: ConfigRef; condStack: var seq[bool]) =
  60. if high(condStack) < 0: lexMessage(L, errGenerated, "expected @if")
  61. ppGetTok(L, tok)
  62. if tok.tokType == tkColon: ppGetTok(L, tok)
  63. if condStack[high(condStack)]: jumpToDirective(L, tok, jdEndif, config, condStack)
  64. proc doElif(L: var Lexer, tok: var Token; config: ConfigRef; condStack: var seq[bool]) =
  65. if high(condStack) < 0: lexMessage(L, errGenerated, "expected @if")
  66. var res = evalppIf(L, tok, config)
  67. if condStack[high(condStack)] or not res: jumpToDirective(L, tok, jdElseEndif, config, condStack)
  68. else: condStack[high(condStack)] = true
  69. proc jumpToDirective(L: var Lexer, tok: var Token, dest: TJumpDest; config: ConfigRef;
  70. condStack: var seq[bool]) =
  71. var nestedIfs = 0
  72. while true:
  73. if tok.ident != nil and tok.ident.s == "@":
  74. ppGetTok(L, tok)
  75. case whichKeyword(tok.ident)
  76. of wIf:
  77. inc(nestedIfs)
  78. of wElse:
  79. if dest == jdElseEndif and nestedIfs == 0:
  80. doElse(L, tok, config, condStack)
  81. break
  82. of wElif:
  83. if dest == jdElseEndif and nestedIfs == 0:
  84. doElif(L, tok, config, condStack)
  85. break
  86. of wEnd:
  87. if nestedIfs == 0:
  88. doEnd(L, tok, condStack)
  89. break
  90. if nestedIfs > 0: dec(nestedIfs)
  91. else:
  92. discard
  93. ppGetTok(L, tok)
  94. elif tok.tokType == tkEof:
  95. lexMessage(L, errGenerated, "expected @end")
  96. else:
  97. ppGetTok(L, tok)
  98. proc parseDirective(L: var Lexer, tok: var Token; config: ConfigRef; condStack: var seq[bool]) =
  99. ppGetTok(L, tok) # skip @
  100. case whichKeyword(tok.ident)
  101. of wIf:
  102. setLen(condStack, condStack.len + 1)
  103. let res = evalppIf(L, tok, config)
  104. condStack[high(condStack)] = res
  105. if not res: jumpToDirective(L, tok, jdElseEndif, config, condStack)
  106. of wElif: doElif(L, tok, config, condStack)
  107. of wElse: doElse(L, tok, config, condStack)
  108. of wEnd: doEnd(L, tok, condStack)
  109. of wWrite:
  110. ppGetTok(L, tok)
  111. msgs.msgWriteln(config, strtabs.`%`($tok, config.configVars,
  112. {useEnvironment, useKey}))
  113. ppGetTok(L, tok)
  114. else:
  115. case tok.ident.s.normalize
  116. of "putenv":
  117. ppGetTok(L, tok)
  118. var key = $tok
  119. ppGetTok(L, tok)
  120. os.putEnv(key, $tok)
  121. ppGetTok(L, tok)
  122. of "prependenv":
  123. ppGetTok(L, tok)
  124. var key = $tok
  125. ppGetTok(L, tok)
  126. os.putEnv(key, $tok & os.getEnv(key))
  127. ppGetTok(L, tok)
  128. of "appendenv":
  129. ppGetTok(L, tok)
  130. var key = $tok
  131. ppGetTok(L, tok)
  132. os.putEnv(key, os.getEnv(key) & $tok)
  133. ppGetTok(L, tok)
  134. else:
  135. lexMessage(L, errGenerated, "invalid directive: '$1'" % $tok)
  136. proc confTok(L: var Lexer, tok: var Token; config: ConfigRef; condStack: var seq[bool]) =
  137. ppGetTok(L, tok)
  138. while tok.ident != nil and tok.ident.s == "@":
  139. parseDirective(L, tok, config, condStack) # else: give the token to the parser
  140. proc checkSymbol(L: Lexer, tok: Token) =
  141. if tok.tokType notin {tkSymbol..tkInt64Lit, tkStrLit..tkTripleStrLit}:
  142. lexMessage(L, errGenerated, "expected identifier, but got: " & $tok)
  143. proc parseAssignment(L: var Lexer, tok: var Token;
  144. config: ConfigRef; condStack: var seq[bool]) =
  145. if tok.ident != nil:
  146. if tok.ident.s == "-" or tok.ident.s == "--":
  147. confTok(L, tok, config, condStack) # skip unnecessary prefix
  148. var info = getLineInfo(L, tok) # save for later in case of an error
  149. checkSymbol(L, tok)
  150. var s = $tok
  151. confTok(L, tok, config, condStack) # skip symbol
  152. var val = ""
  153. while tok.tokType == tkDot:
  154. s.add('.')
  155. confTok(L, tok, config, condStack)
  156. checkSymbol(L, tok)
  157. s.add($tok)
  158. confTok(L, tok, config, condStack)
  159. if tok.tokType == tkBracketLe:
  160. # BUGFIX: val, not s!
  161. confTok(L, tok, config, condStack)
  162. checkSymbol(L, tok)
  163. val.add('[')
  164. val.add($tok)
  165. confTok(L, tok, config, condStack)
  166. if tok.tokType == tkBracketRi: confTok(L, tok, config, condStack)
  167. else: lexMessage(L, errGenerated, "expected closing ']'")
  168. val.add(']')
  169. let percent = tok.ident != nil and tok.ident.s == "%="
  170. if tok.tokType in {tkColon, tkEquals} or percent:
  171. if val.len > 0: val.add(':')
  172. confTok(L, tok, config, condStack) # skip ':' or '=' or '%'
  173. checkSymbol(L, tok)
  174. val.add($tok)
  175. confTok(L, tok, config, condStack) # skip symbol
  176. if tok.tokType in {tkColon, tkEquals}:
  177. val.add($tok) # add the :
  178. confTok(L, tok, config, condStack) # skip symbol
  179. checkSymbol(L, tok)
  180. val.add($tok) # add the token after it
  181. confTok(L, tok, config, condStack) # skip symbol
  182. while tok.ident != nil and tok.ident.s == "&":
  183. confTok(L, tok, config, condStack)
  184. checkSymbol(L, tok)
  185. val.add($tok)
  186. confTok(L, tok, config, condStack)
  187. if percent:
  188. processSwitch(s, strtabs.`%`(val, config.configVars,
  189. {useEnvironment, useEmpty}), passPP, info, config)
  190. else:
  191. processSwitch(s, val, passPP, info, config)
  192. proc readConfigFile*(filename: AbsoluteFile; cache: IdentCache;
  193. config: ConfigRef): bool =
  194. var
  195. L: Lexer
  196. tok: Token
  197. stream: PLLStream
  198. stream = llStreamOpen(filename, fmRead)
  199. if stream != nil:
  200. initToken(tok)
  201. openLexer(L, filename, stream, cache, config)
  202. tok.tokType = tkEof # to avoid a pointless warning
  203. var condStack: seq[bool] = @[]
  204. confTok(L, tok, config, condStack) # read in the first token
  205. while tok.tokType != tkEof: parseAssignment(L, tok, config, condStack)
  206. if condStack.len > 0: lexMessage(L, errGenerated, "expected @end")
  207. closeLexer(L)
  208. return true
  209. proc getUserConfigPath*(filename: RelativeFile): AbsoluteFile =
  210. result = getConfigDir().AbsoluteDir / RelativeDir"nim" / filename
  211. proc getSystemConfigPath*(conf: ConfigRef; filename: RelativeFile): AbsoluteFile =
  212. # try standard configuration file (installation did not distribute files
  213. # the UNIX way)
  214. let p = getPrefixDir(conf)
  215. result = p / RelativeDir"config" / filename
  216. when defined(unix):
  217. if not fileExists(result): result = p / RelativeDir"etc/nim" / filename
  218. if not fileExists(result): result = AbsoluteDir"/etc/nim" / filename
  219. proc loadConfigs*(cfg: RelativeFile; cache: IdentCache; conf: ConfigRef) =
  220. setDefaultLibpath(conf)
  221. var configFiles = newSeq[AbsoluteFile]()
  222. template readConfigFile(path) =
  223. let configPath = path
  224. if readConfigFile(configPath, cache, conf):
  225. configFiles.add(configPath)
  226. template runNimScriptIfExists(path: AbsoluteFile) =
  227. let p = path # eval once
  228. if fileExists(p):
  229. configFiles.add(p)
  230. runNimScript(cache, p, freshDefines = false, conf)
  231. if optSkipSystemConfigFile notin conf.globalOptions:
  232. readConfigFile(getSystemConfigPath(conf, cfg))
  233. if cfg == DefaultConfig:
  234. runNimScriptIfExists(getSystemConfigPath(conf, DefaultConfigNims))
  235. if optSkipUserConfigFile notin conf.globalOptions:
  236. readConfigFile(getUserConfigPath(cfg))
  237. if cfg == DefaultConfig:
  238. runNimScriptIfExists(getUserConfigPath(DefaultConfigNims))
  239. let pd = if not conf.projectPath.isEmpty: conf.projectPath else: AbsoluteDir(getCurrentDir())
  240. if optSkipParentConfigFiles notin conf.globalOptions:
  241. for dir in parentDirs(pd.string, fromRoot=true, inclusive=false):
  242. readConfigFile(AbsoluteDir(dir) / cfg)
  243. if cfg == DefaultConfig:
  244. runNimScriptIfExists(AbsoluteDir(dir) / DefaultConfigNims)
  245. if optSkipProjConfigFile notin conf.globalOptions:
  246. readConfigFile(pd / cfg)
  247. if conf.projectName.len != 0:
  248. # new project wide config file:
  249. var projectConfig = changeFileExt(conf.projectFull, "nimcfg")
  250. if not fileExists(projectConfig):
  251. projectConfig = changeFileExt(conf.projectFull, "nim.cfg")
  252. readConfigFile(projectConfig)
  253. if cfg == DefaultConfig:
  254. runNimScriptIfExists(pd / DefaultConfigNims)
  255. for filename in configFiles:
  256. # delayed to here so that `hintConf` is honored
  257. rawMessage(conf, hintConf, filename.string)
  258. block:
  259. let scriptFile = conf.projectFull.changeFileExt("nims")
  260. if conf.command != "nimsuggest":
  261. runNimScriptIfExists(scriptFile)
  262. else:
  263. if scriptFile != conf.projectFull:
  264. runNimScriptIfExists(scriptFile)
  265. else:
  266. # 'nimsuggest foo.nims' means to just auto-complete the NimScript file
  267. discard