pbase.pas 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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,globals,
  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 should ignore an equal in const x : 1..2=2 }
  45. ignore_equal : boolean;
  46. { true, if we found a name for a named arg }
  47. found_arg_name : 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):boolean;
  66. function try_consume_hintdirective(var symopt:tsymoptions):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. globtype,htypechk,scanner,systems,verbose;
  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. { Unused:
  87. function tokenstring(i : ttoken):string;
  88. begin
  89. tokenstring:=tokeninfo^[i].str;
  90. end;
  91. }
  92. { consumes token i, write error if token is different }
  93. procedure consume(i : ttoken);
  94. begin
  95. if (token<>i) and (idtoken<>i) then
  96. if token=_id then
  97. Message2(scan_f_syn_expected,tokeninfo^[i].str,'identifier '+pattern)
  98. else
  99. Message2(scan_f_syn_expected,tokeninfo^[i].str,tokeninfo^[token].str)
  100. else
  101. begin
  102. if token=_END then
  103. last_endtoken_filepos:=current_tokenpos;
  104. current_scanner.readtoken(true);
  105. end;
  106. end;
  107. function try_to_consume(i:Ttoken):boolean;
  108. begin
  109. try_to_consume:=false;
  110. if (token=i) or (idtoken=i) then
  111. begin
  112. try_to_consume:=true;
  113. if token=_END then
  114. last_endtoken_filepos:=current_tokenpos;
  115. current_scanner.readtoken(true);
  116. end;
  117. end;
  118. procedure consume_all_until(atoken : ttoken);
  119. begin
  120. while (token<>atoken) and (idtoken<>atoken) do
  121. begin
  122. Consume(token);
  123. if token=_EOF then
  124. begin
  125. Consume(atoken);
  126. Message(scan_f_end_of_file);
  127. exit;
  128. end;
  129. end;
  130. end;
  131. procedure consume_emptystats;
  132. begin
  133. repeat
  134. until not try_to_consume(_SEMICOLON);
  135. end;
  136. { check if a symbol contains the hint directive, and if so gives out a hint
  137. if required.
  138. If this code is changed, it's likly that consume_sym_orgid and factor_read_id
  139. must be changed as well (FK)
  140. }
  141. function consume_sym(var srsym:tsym;var srsymtable:TSymtable):boolean;
  142. begin
  143. { first check for identifier }
  144. if token<>_ID then
  145. begin
  146. consume(_ID);
  147. srsym:=generrorsym;
  148. srsymtable:=nil;
  149. result:=false;
  150. exit;
  151. end;
  152. searchsym(pattern,srsym,srsymtable);
  153. { handle unit specification like System.Writeln }
  154. try_consume_unitsym(srsym,srsymtable);
  155. { if nothing found give error and return errorsym }
  156. if assigned(srsym) then
  157. check_hints(srsym,srsym.symoptions)
  158. else
  159. begin
  160. identifier_not_found(orgpattern);
  161. srsym:=generrorsym;
  162. srsymtable:=nil;
  163. end;
  164. consume(_ID);
  165. result:=assigned(srsym);
  166. end;
  167. { check if a symbol contains the hint directive, and if so gives out a hint
  168. if required and returns the id with it's original casing
  169. }
  170. function consume_sym_orgid(var srsym:tsym;var srsymtable:TSymtable;var s : string):boolean;
  171. begin
  172. { first check for identifier }
  173. if token<>_ID then
  174. begin
  175. consume(_ID);
  176. srsym:=generrorsym;
  177. srsymtable:=nil;
  178. result:=false;
  179. exit;
  180. end;
  181. searchsym(pattern,srsym,srsymtable);
  182. { handle unit specification like System.Writeln }
  183. try_consume_unitsym(srsym,srsymtable);
  184. { if nothing found give error and return errorsym }
  185. if assigned(srsym) then
  186. check_hints(srsym,srsym.symoptions)
  187. else
  188. begin
  189. identifier_not_found(orgpattern);
  190. srsym:=generrorsym;
  191. srsymtable:=nil;
  192. end;
  193. s:=orgpattern;
  194. consume(_ID);
  195. result:=assigned(srsym);
  196. end;
  197. function try_consume_unitsym(var srsym:tsym;var srsymtable:TSymtable):boolean;
  198. begin
  199. result:=false;
  200. if assigned(srsym) and
  201. (srsym.typ=unitsym) then
  202. begin
  203. if not(srsym.owner.symtabletype in [staticsymtable,globalsymtable]) then
  204. internalerror(200501154);
  205. { only allow unit.symbol access if the name was
  206. found in the current module }
  207. if srsym.owner.iscurrentunit then
  208. begin
  209. consume(_ID);
  210. consume(_POINT);
  211. searchsym_in_module(tunitsym(srsym).module,pattern,srsym,srsymtable);
  212. end
  213. else
  214. begin
  215. srsym:=nil;
  216. srsymtable:=nil;
  217. end;
  218. result:=true;
  219. end;
  220. end;
  221. function try_consume_hintdirective(var symopt:tsymoptions):boolean;
  222. begin
  223. try_consume_hintdirective:=false;
  224. if not(m_hintdirective in current_settings.modeswitches) then
  225. exit;
  226. repeat
  227. case idtoken of
  228. _LIBRARY :
  229. begin
  230. include(symopt,sp_hint_library);
  231. try_consume_hintdirective:=true;
  232. end;
  233. _DEPRECATED :
  234. begin
  235. include(symopt,sp_hint_deprecated);
  236. try_consume_hintdirective:=true;
  237. end;
  238. _PLATFORM :
  239. begin
  240. include(symopt,sp_hint_platform);
  241. try_consume_hintdirective:=true;
  242. end;
  243. _UNIMPLEMENTED :
  244. begin
  245. include(symopt,sp_hint_unimplemented);
  246. try_consume_hintdirective:=true;
  247. end;
  248. else
  249. break;
  250. end;
  251. consume(Token);
  252. until false;
  253. end;
  254. end.