pbase.pas 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Contains some helper routines for the parser
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit pbase;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cutils,cclasses,
  23. tokens,globals,
  24. symconst,symbase,symtype,symdef,symsym,symtable
  25. ;
  26. const
  27. { true, if we are after an assignement }
  28. afterassignment : boolean = false;
  29. { special for handling procedure vars }
  30. getprocvardef : tprocvardef = nil;
  31. var
  32. { size of data segment, set by proc_unit or proc_program }
  33. datasize : longint;
  34. { for operators }
  35. optoken : ttoken;
  36. { symtable were unit references are stored }
  37. refsymtable : tsymtable;
  38. { true, if only routine headers should be parsed }
  39. parse_only : boolean;
  40. { true, if we should ignore an equal in const x : 1..2=2 }
  41. ignore_equal : boolean;
  42. procedure identifier_not_found(const s:string);
  43. function tokenstring(i : ttoken):string;
  44. { consumes token i, if the current token is unequal i }
  45. { a syntax error is written }
  46. procedure consume(i : ttoken);
  47. {Tries to consume the token i, and returns true if it was consumed:
  48. if token=i.}
  49. function try_to_consume(i:Ttoken):boolean;
  50. { consumes all tokens til atoken (for error recovering }
  51. procedure consume_all_until(atoken : ttoken);
  52. { consumes tokens while they are semicolons }
  53. procedure consume_emptystats;
  54. { reads a list of identifiers into a string list }
  55. { consume a symbol, if not found give an error and
  56. and return an errorsym }
  57. function consume_sym(var srsym:tsym;var srsymtable:tsymtable):boolean;
  58. function try_consume_hintdirective(var symopt:tsymoptions):boolean;
  59. { just for an accurate position of the end of a procedure (PM) }
  60. var
  61. last_endtoken_filepos: tfileposinfo;
  62. implementation
  63. uses
  64. globtype,scanner,systems,verbose;
  65. {****************************************************************************
  66. Token Parsing
  67. ****************************************************************************}
  68. procedure identifier_not_found(const s:string);
  69. begin
  70. Message1(sym_e_id_not_found,s);
  71. { show a fatal that you need -S2 or -Sd, but only
  72. if we just parsed the a token that has m_class }
  73. if not(m_class in aktmodeswitches) and
  74. (Upper(s)=pattern) and
  75. (tokeninfo^[idtoken].keyword=m_class) then
  76. Message(parser_f_need_objfpc_or_delphi_mode);
  77. end;
  78. function tokenstring(i : ttoken):string;
  79. begin
  80. tokenstring:=tokeninfo^[i].str;
  81. end;
  82. { consumes token i, write error if token is different }
  83. procedure consume(i : ttoken);
  84. begin
  85. if (token<>i) and (idtoken<>i) then
  86. if token=_id then
  87. Message2(scan_f_syn_expected,tokeninfo^[i].str,'identifier '+pattern)
  88. else
  89. Message2(scan_f_syn_expected,tokeninfo^[i].str,tokeninfo^[token].str)
  90. else
  91. begin
  92. if token=_END then
  93. last_endtoken_filepos:=akttokenpos;
  94. current_scanner.readtoken;
  95. end;
  96. end;
  97. function try_to_consume(i:Ttoken):boolean;
  98. begin
  99. try_to_consume:=false;
  100. if (token=i) or (idtoken=i) then
  101. begin
  102. try_to_consume:=true;
  103. if token=_END then
  104. last_endtoken_filepos:=akttokenpos;
  105. current_scanner.readtoken;
  106. end;
  107. end;
  108. procedure consume_all_until(atoken : ttoken);
  109. begin
  110. while (token<>atoken) and (idtoken<>atoken) do
  111. begin
  112. Consume(token);
  113. if token=_EOF then
  114. begin
  115. Consume(atoken);
  116. Message(scan_f_end_of_file);
  117. exit;
  118. end;
  119. end;
  120. end;
  121. procedure consume_emptystats;
  122. begin
  123. repeat
  124. until not try_to_consume(_SEMICOLON);
  125. end;
  126. function consume_sym(var srsym:tsym;var srsymtable:tsymtable):boolean;
  127. begin
  128. { first check for identifier }
  129. if token<>_ID then
  130. begin
  131. consume(_ID);
  132. srsym:=generrorsym;
  133. srsymtable:=nil;
  134. consume_sym:=false;
  135. exit;
  136. end;
  137. searchsym(pattern,srsym,srsymtable);
  138. if assigned(srsym) then
  139. begin
  140. if (srsym.typ=unitsym) then
  141. begin
  142. { only allow unit.symbol access if the name was
  143. found in the current module }
  144. if srsym.owner.unitid=0 then
  145. begin
  146. consume(_ID);
  147. consume(_POINT);
  148. srsymtable:=tunitsym(srsym).unitsymtable;
  149. srsym:=searchsymonlyin(srsymtable,pattern);
  150. end
  151. else
  152. srsym:=nil;
  153. end;
  154. end;
  155. { if nothing found give error and return errorsym }
  156. if srsym=nil then
  157. begin
  158. identifier_not_found(orgpattern);
  159. srsym:=generrorsym;
  160. srsymtable:=nil;
  161. end;
  162. consume(_ID);
  163. consume_sym:=assigned(srsym);
  164. end;
  165. function try_consume_hintdirective(var symopt:tsymoptions):boolean;
  166. begin
  167. try_consume_hintdirective:=false;
  168. if not(m_hintdirective in aktmodeswitches) then
  169. exit;
  170. repeat
  171. case idtoken of
  172. _LIBRARY :
  173. begin
  174. include(symopt,sp_hint_library);
  175. try_consume_hintdirective:=true;
  176. end;
  177. _DEPRECATED :
  178. begin
  179. include(symopt,sp_hint_deprecated);
  180. try_consume_hintdirective:=true;
  181. end;
  182. _PLATFORM :
  183. begin
  184. include(symopt,sp_hint_platform);
  185. try_consume_hintdirective:=true;
  186. end;
  187. else
  188. break;
  189. end;
  190. consume(Token);
  191. until false;
  192. end;
  193. end.
  194. {
  195. $Log$
  196. Revision 1.19 2002-09-09 17:34:15 peter
  197. * tdicationary.replace added to replace and item in a dictionary. This
  198. is only allowed for the same name
  199. * varsyms are inserted in symtable before the types are parsed. This
  200. fixes the long standing "var longint : longint" bug
  201. - consume_idlist and idstringlist removed. The loops are inserted
  202. at the callers place and uses the symtable for duplicate id checking
  203. Revision 1.18 2002/08/17 09:23:38 florian
  204. * first part of procinfo rewrite
  205. Revision 1.17 2002/05/18 13:34:11 peter
  206. * readded missing revisions
  207. Revision 1.16 2002/05/16 19:46:42 carl
  208. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  209. + try to fix temp allocation (still in ifdef)
  210. + generic constructor calls
  211. + start of tassembler / tmodulebase class cleanup
  212. Revision 1.14 2002/01/06 21:47:32 peter
  213. * removed getprocvar, use only getprocvardef
  214. }