pbase.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Contains some helper routines for the parser
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit pbase;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cutils,cclasses,
  22. tokens,globtype,
  23. symconst,symbase,symtype,symdef,symsym,symtable
  24. ;
  25. const
  26. { tokens that end a block or statement. And don't require
  27. a ; on the statement before }
  28. endtokens = [_SEMICOLON,_END,_ELSE,_UNTIL,_EXCEPT,_FINALLY];
  29. { true, if we are after an assignement }
  30. afterassignment : boolean = false;
  31. { true, if we are parsing arguments }
  32. in_args : boolean = false;
  33. { true, if we are parsing arguments allowing named parameters }
  34. named_args_allowed : boolean = false;
  35. { true, if we got an @ to get the address }
  36. got_addrn : boolean = false;
  37. { special for handling procedure vars }
  38. getprocvardef : tprocvardef = nil;
  39. var
  40. { for operators }
  41. optoken : ttoken;
  42. { true, if only routine headers should be parsed }
  43. parse_only : boolean;
  44. { true, if we found a name for a named arg }
  45. found_arg_name : boolean;
  46. { true, if we are parsing generic declaration }
  47. parse_generic : boolean;
  48. procedure identifier_not_found(const s:string);
  49. { function tokenstring(i : ttoken):string;}
  50. { consumes token i, if the current token is unequal i }
  51. { a syntax error is written }
  52. procedure consume(i : ttoken);
  53. {Tries to consume the token i, and returns true if it was consumed:
  54. if token=i.}
  55. function try_to_consume(i:Ttoken):boolean;
  56. { consumes all tokens til atoken (for error recovering }
  57. procedure consume_all_until(atoken : ttoken);
  58. { consumes tokens while they are semicolons }
  59. procedure consume_emptystats;
  60. { reads a list of identifiers into a string list }
  61. { consume a symbol, if not found give an error and
  62. and return an errorsym }
  63. function consume_sym(var srsym:tsym;var srsymtable:TSymtable):boolean;
  64. function consume_sym_orgid(var srsym:tsym;var srsymtable:TSymtable;var s : string):boolean;
  65. function try_consume_unitsym(var srsym:tsym;var srsymtable:TSymtable;var tokentoconsume:ttoken;consume_id:boolean):boolean;
  66. function try_consume_hintdirective(var symopt:tsymoptions; var deprecatedmsg:pshortstring):boolean;
  67. { just for an accurate position of the end of a procedure (PM) }
  68. var
  69. last_endtoken_filepos: tfileposinfo;
  70. implementation
  71. uses
  72. globals,htypechk,scanner,systems,verbose,fmodule;
  73. {****************************************************************************
  74. Token Parsing
  75. ****************************************************************************}
  76. procedure identifier_not_found(const s:string);
  77. begin
  78. Message1(sym_e_id_not_found,s);
  79. { show a fatal that you need -S2 or -Sd, but only
  80. if we just parsed the a token that has m_class }
  81. if not(m_class in current_settings.modeswitches) and
  82. (Upper(s)=pattern) and
  83. (tokeninfo^[idtoken].keyword=m_class) then
  84. Message(parser_f_need_objfpc_or_delphi_mode);
  85. end;
  86. { consumes token i, write error if token is different }
  87. procedure consume(i : ttoken);
  88. begin
  89. if (token<>i) and (idtoken<>i) then
  90. if token=_id then
  91. Message2(scan_f_syn_expected,tokeninfo^[i].str,'identifier '+pattern)
  92. else
  93. Message2(scan_f_syn_expected,tokeninfo^[i].str,tokeninfo^[token].str)
  94. else
  95. begin
  96. if token=_END then
  97. last_endtoken_filepos:=current_tokenpos;
  98. current_scanner.readtoken(true);
  99. end;
  100. end;
  101. function try_to_consume(i:Ttoken):boolean;
  102. begin
  103. try_to_consume:=false;
  104. if (token=i) or (idtoken=i) then
  105. begin
  106. try_to_consume:=true;
  107. if token=_END then
  108. last_endtoken_filepos:=current_tokenpos;
  109. current_scanner.readtoken(true);
  110. end;
  111. end;
  112. procedure consume_all_until(atoken : ttoken);
  113. begin
  114. while (token<>atoken) and (idtoken<>atoken) do
  115. begin
  116. Consume(token);
  117. if token=_EOF then
  118. begin
  119. Consume(atoken);
  120. Message(scan_f_end_of_file);
  121. exit;
  122. end;
  123. end;
  124. end;
  125. procedure consume_emptystats;
  126. begin
  127. repeat
  128. until not try_to_consume(_SEMICOLON);
  129. end;
  130. { check if a symbol contains the hint directive, and if so gives out a hint
  131. if required.
  132. If this code is changed, it's likly that consume_sym_orgid and factor_read_id
  133. must be changed as well (FK)
  134. }
  135. function consume_sym(var srsym:tsym;var srsymtable:TSymtable):boolean;
  136. var
  137. t : ttoken;
  138. begin
  139. { first check for identifier }
  140. if token<>_ID then
  141. begin
  142. consume(_ID);
  143. srsym:=generrorsym;
  144. srsymtable:=nil;
  145. result:=false;
  146. exit;
  147. end;
  148. searchsym(pattern,srsym,srsymtable);
  149. { handle unit specification like System.Writeln }
  150. try_consume_unitsym(srsym,srsymtable,t,true);
  151. { if nothing found give error and return errorsym }
  152. if assigned(srsym) then
  153. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg)
  154. else
  155. begin
  156. identifier_not_found(orgpattern);
  157. srsym:=generrorsym;
  158. srsymtable:=nil;
  159. end;
  160. consume(t);
  161. result:=assigned(srsym);
  162. end;
  163. { check if a symbol contains the hint directive, and if so gives out a hint
  164. if required and returns the id with it's original casing
  165. }
  166. function consume_sym_orgid(var srsym:tsym;var srsymtable:TSymtable;var s : string):boolean;
  167. var
  168. t : ttoken;
  169. begin
  170. { first check for identifier }
  171. if token<>_ID then
  172. begin
  173. consume(_ID);
  174. srsym:=generrorsym;
  175. srsymtable:=nil;
  176. result:=false;
  177. exit;
  178. end;
  179. searchsym(pattern,srsym,srsymtable);
  180. { handle unit specification like System.Writeln }
  181. try_consume_unitsym(srsym,srsymtable,t,true);
  182. { if nothing found give error and return errorsym }
  183. if assigned(srsym) then
  184. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg)
  185. else
  186. begin
  187. identifier_not_found(orgpattern);
  188. srsym:=generrorsym;
  189. srsymtable:=nil;
  190. end;
  191. s:=orgpattern;
  192. consume(t);
  193. result:=assigned(srsym);
  194. end;
  195. function try_consume_unitsym(var srsym:tsym;var srsymtable:TSymtable;var tokentoconsume:ttoken;consume_id:boolean):boolean;
  196. var
  197. hmodule: tmodule;
  198. ns:ansistring;
  199. nssym:tsym;
  200. begin
  201. result:=false;
  202. tokentoconsume:=_ID;
  203. if assigned(srsym) and (srsym.typ in [unitsym,namespacesym]) then
  204. begin
  205. if not(srsym.owner.symtabletype in [staticsymtable,globalsymtable]) then
  206. internalerror(200501154);
  207. { only allow unit.symbol access if the name was
  208. found in the current module
  209. we can use iscurrentunit because generic specializations does not
  210. change current_unit variable }
  211. hmodule:=find_module_from_symtable(srsym.Owner);
  212. if not Assigned(hmodule) then
  213. internalerror(201001120);
  214. if hmodule.unit_index=current_filepos.moduleindex then
  215. begin
  216. if consume_id then
  217. consume(_ID);
  218. consume(_POINT);
  219. if srsym.typ=namespacesym then
  220. begin
  221. ns:=srsym.name;
  222. nssym:=srsym;
  223. while assigned(srsym) and (srsym.typ=namespacesym) do
  224. begin
  225. { we have a namespace. the next identifier should be either a namespace or a unit }
  226. searchsym_in_module(hmodule,ns+'.'+pattern,srsym,srsymtable);
  227. if assigned(srsym) and (srsym.typ in [namespacesym,unitsym]) then
  228. begin
  229. ns:=ns+'.'+pattern;
  230. nssym:=srsym;
  231. consume(_ID);
  232. consume(_POINT);
  233. end;
  234. end;
  235. { check if there is a hidden unit with this pattern in the namespace }
  236. if not assigned(srsym) and
  237. assigned(nssym) and (nssym.typ=namespacesym) and assigned(tnamespacesym(nssym).unitsym) then
  238. srsym:=tnamespacesym(nssym).unitsym;
  239. if assigned(srsym) and (srsym.typ<>unitsym) then
  240. internalerror(201108260);
  241. if not assigned(srsym) then
  242. begin
  243. result:=true;
  244. srsymtable:=nil;
  245. exit;
  246. end;
  247. end;
  248. case token of
  249. _ID:
  250. searchsym_in_module(tunitsym(srsym).module,pattern,srsym,srsymtable);
  251. _STRING:
  252. begin
  253. { system.string? }
  254. if tmodule(tunitsym(srsym).module).globalsymtable=systemunit then
  255. begin
  256. if cs_ansistrings in current_settings.localswitches then
  257. searchsym_in_module(tunitsym(srsym).module,'ANSISTRING',srsym,srsymtable)
  258. else
  259. searchsym_in_module(tunitsym(srsym).module,'SHORTSTRING',srsym,srsymtable);
  260. tokentoconsume:=_STRING;
  261. end;
  262. end
  263. end;
  264. end
  265. else
  266. begin
  267. srsym:=nil;
  268. srsymtable:=nil;
  269. end;
  270. result:=true;
  271. end;
  272. end;
  273. function try_consume_hintdirective(var symopt:tsymoptions; var deprecatedmsg:pshortstring):boolean;
  274. var
  275. last_is_deprecated:boolean;
  276. begin
  277. try_consume_hintdirective:=false;
  278. if not(m_hintdirective in current_settings.modeswitches) then
  279. exit;
  280. repeat
  281. last_is_deprecated:=false;
  282. case idtoken of
  283. _LIBRARY :
  284. begin
  285. include(symopt,sp_hint_library);
  286. try_consume_hintdirective:=true;
  287. end;
  288. _DEPRECATED :
  289. begin
  290. include(symopt,sp_hint_deprecated);
  291. try_consume_hintdirective:=true;
  292. last_is_deprecated:=true;
  293. end;
  294. _EXPERIMENTAL :
  295. begin
  296. include(symopt,sp_hint_experimental);
  297. try_consume_hintdirective:=true;
  298. end;
  299. _PLATFORM :
  300. begin
  301. include(symopt,sp_hint_platform);
  302. try_consume_hintdirective:=true;
  303. end;
  304. _UNIMPLEMENTED :
  305. begin
  306. include(symopt,sp_hint_unimplemented);
  307. try_consume_hintdirective:=true;
  308. end;
  309. else
  310. break;
  311. end;
  312. consume(Token);
  313. { handle deprecated message }
  314. if ((token=_CSTRING) or (token=_CCHAR)) and last_is_deprecated then
  315. begin
  316. if deprecatedmsg<>nil then
  317. internalerror(200910181);
  318. if token=_CSTRING then
  319. deprecatedmsg:=stringdup(cstringpattern)
  320. else
  321. deprecatedmsg:=stringdup(pattern);
  322. consume(token);
  323. include(symopt,sp_has_deprecated_msg);
  324. end;
  325. until false;
  326. end;
  327. end.