pbase.pas 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 got an @ to get the address }
  34. got_addrn : boolean = false;
  35. { special for handling procedure vars }
  36. getprocvardef : tprocvardef = nil;
  37. var
  38. { for operators }
  39. optoken : ttoken;
  40. { true, if only routine headers should be parsed }
  41. parse_only : boolean;
  42. { true, if we should ignore an equal in const x : 1..2=2 }
  43. ignore_equal : boolean;
  44. procedure identifier_not_found(const s:string);
  45. { function tokenstring(i : ttoken):string;}
  46. { consumes token i, if the current token is unequal i }
  47. { a syntax error is written }
  48. procedure consume(i : ttoken);
  49. {Tries to consume the token i, and returns true if it was consumed:
  50. if token=i.}
  51. function try_to_consume(i:Ttoken):boolean;
  52. { consumes all tokens til atoken (for error recovering }
  53. procedure consume_all_until(atoken : ttoken);
  54. { consumes tokens while they are semicolons }
  55. procedure consume_emptystats;
  56. { reads a list of identifiers into a string list }
  57. { consume a symbol, if not found give an error and
  58. and return an errorsym }
  59. function consume_sym(var srsym:tsym;var srsymtable:tsymtable):boolean;
  60. function consume_sym_orgid(var srsym:tsym;var srsymtable:tsymtable;var s : string):boolean;
  61. function try_consume_unitsym(var srsym:tsym;var srsymtable:tsymtable):boolean;
  62. function try_consume_hintdirective(var symopt:tsymoptions):boolean;
  63. { just for an accurate position of the end of a procedure (PM) }
  64. var
  65. last_endtoken_filepos: tfileposinfo;
  66. implementation
  67. uses
  68. globtype,htypechk,scanner,systems,verbose;
  69. {****************************************************************************
  70. Token Parsing
  71. ****************************************************************************}
  72. procedure identifier_not_found(const s:string);
  73. begin
  74. Message1(sym_e_id_not_found,s);
  75. { show a fatal that you need -S2 or -Sd, but only
  76. if we just parsed the a token that has m_class }
  77. if not(m_class in current_settings.modeswitches) and
  78. (Upper(s)=pattern) and
  79. (tokeninfo^[idtoken].keyword=m_class) then
  80. Message(parser_f_need_objfpc_or_delphi_mode);
  81. end;
  82. { Unused:
  83. function tokenstring(i : ttoken):string;
  84. begin
  85. tokenstring:=tokeninfo^[i].str;
  86. end;
  87. }
  88. { consumes token i, write error if token is different }
  89. procedure consume(i : ttoken);
  90. begin
  91. if (token<>i) and (idtoken<>i) then
  92. if token=_id then
  93. Message2(scan_f_syn_expected,tokeninfo^[i].str,'identifier '+pattern)
  94. else
  95. Message2(scan_f_syn_expected,tokeninfo^[i].str,tokeninfo^[token].str)
  96. else
  97. begin
  98. if token=_END then
  99. last_endtoken_filepos:=akttokenpos;
  100. current_scanner.readtoken(true);
  101. end;
  102. end;
  103. function try_to_consume(i:Ttoken):boolean;
  104. begin
  105. try_to_consume:=false;
  106. if (token=i) or (idtoken=i) then
  107. begin
  108. try_to_consume:=true;
  109. if token=_END then
  110. last_endtoken_filepos:=akttokenpos;
  111. current_scanner.readtoken(true);
  112. end;
  113. end;
  114. procedure consume_all_until(atoken : ttoken);
  115. begin
  116. while (token<>atoken) and (idtoken<>atoken) do
  117. begin
  118. Consume(token);
  119. if token=_EOF then
  120. begin
  121. Consume(atoken);
  122. Message(scan_f_end_of_file);
  123. exit;
  124. end;
  125. end;
  126. end;
  127. procedure consume_emptystats;
  128. begin
  129. repeat
  130. until not try_to_consume(_SEMICOLON);
  131. end;
  132. { check if a symbol contains the hint directive, and if so gives out a hint
  133. if required.
  134. If this code is changed, it's like that consume_sym_orgid must be changed
  135. as well (FK)
  136. }
  137. function consume_sym(var srsym:tsym;var srsymtable:tsymtable):boolean;
  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);
  151. { if nothing found give error and return errorsym }
  152. if assigned(srsym) then
  153. check_hints(srsym,srsym.symoptions)
  154. else
  155. begin
  156. identifier_not_found(orgpattern);
  157. srsym:=generrorsym;
  158. srsymtable:=nil;
  159. end;
  160. consume(_ID);
  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. begin
  168. { first check for identifier }
  169. if token<>_ID then
  170. begin
  171. consume(_ID);
  172. srsym:=generrorsym;
  173. srsymtable:=nil;
  174. result:=false;
  175. exit;
  176. end;
  177. searchsym(pattern,srsym,srsymtable);
  178. { handle unit specification like System.Writeln }
  179. try_consume_unitsym(srsym,srsymtable);
  180. { if nothing found give error and return errorsym }
  181. if assigned(srsym) then
  182. check_hints(srsym,srsym.symoptions)
  183. else
  184. begin
  185. identifier_not_found(orgpattern);
  186. srsym:=generrorsym;
  187. srsymtable:=nil;
  188. end;
  189. s:=orgpattern;
  190. consume(_ID);
  191. result:=assigned(srsym);
  192. end;
  193. function try_consume_unitsym(var srsym:tsym;var srsymtable:tsymtable):boolean;
  194. begin
  195. result:=false;
  196. if assigned(srsym) and
  197. (srsym.typ=unitsym) then
  198. begin
  199. if not(srsym.owner.symtabletype in [staticsymtable,globalsymtable]) then
  200. internalerror(200501154);
  201. { only allow unit.symbol access if the name was
  202. found in the current module }
  203. if srsym.owner.iscurrentunit then
  204. begin
  205. consume(_ID);
  206. consume(_POINT);
  207. searchsym_in_module(tunitsym(srsym).module,pattern,srsym,srsymtable);
  208. end
  209. else
  210. begin
  211. srsym:=nil;
  212. srsymtable:=nil;
  213. end;
  214. result:=true;
  215. end;
  216. end;
  217. function try_consume_hintdirective(var symopt:tsymoptions):boolean;
  218. begin
  219. try_consume_hintdirective:=false;
  220. if not(m_hintdirective in current_settings.modeswitches) then
  221. exit;
  222. repeat
  223. case idtoken of
  224. _LIBRARY :
  225. begin
  226. include(symopt,sp_hint_library);
  227. try_consume_hintdirective:=true;
  228. end;
  229. _DEPRECATED :
  230. begin
  231. include(symopt,sp_hint_deprecated);
  232. try_consume_hintdirective:=true;
  233. end;
  234. _PLATFORM :
  235. begin
  236. include(symopt,sp_hint_platform);
  237. try_consume_hintdirective:=true;
  238. end;
  239. _UNIMPLEMENTED :
  240. begin
  241. include(symopt,sp_hint_unimplemented);
  242. try_consume_hintdirective:=true;
  243. end;
  244. else
  245. break;
  246. end;
  247. consume(Token);
  248. until false;
  249. end;
  250. end.