pbase.pas 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 try_consume_unitsym(var srsym:tsym;var srsymtable:tsymtable):boolean;
  61. function try_consume_hintdirective(var symopt:tsymoptions):boolean;
  62. { just for an accurate position of the end of a procedure (PM) }
  63. var
  64. last_endtoken_filepos: tfileposinfo;
  65. implementation
  66. uses
  67. globtype,htypechk,scanner,systems,verbose;
  68. {****************************************************************************
  69. Token Parsing
  70. ****************************************************************************}
  71. procedure identifier_not_found(const s:string);
  72. begin
  73. Message1(sym_e_id_not_found,s);
  74. { show a fatal that you need -S2 or -Sd, but only
  75. if we just parsed the a token that has m_class }
  76. if not(m_class in aktmodeswitches) and
  77. (Upper(s)=pattern) and
  78. (tokeninfo^[idtoken].keyword=m_class) then
  79. Message(parser_f_need_objfpc_or_delphi_mode);
  80. end;
  81. { Unused:
  82. function tokenstring(i : ttoken):string;
  83. begin
  84. tokenstring:=tokeninfo^[i].str;
  85. end;
  86. }
  87. { consumes token i, write error if token is different }
  88. procedure consume(i : ttoken);
  89. begin
  90. if (token<>i) and (idtoken<>i) then
  91. if token=_id then
  92. Message2(scan_f_syn_expected,tokeninfo^[i].str,'identifier '+pattern)
  93. else
  94. Message2(scan_f_syn_expected,tokeninfo^[i].str,tokeninfo^[token].str)
  95. else
  96. begin
  97. if token=_END then
  98. last_endtoken_filepos:=akttokenpos;
  99. current_scanner.readtoken(true);
  100. end;
  101. end;
  102. function try_to_consume(i:Ttoken):boolean;
  103. begin
  104. try_to_consume:=false;
  105. if (token=i) or (idtoken=i) then
  106. begin
  107. try_to_consume:=true;
  108. if token=_END then
  109. last_endtoken_filepos:=akttokenpos;
  110. current_scanner.readtoken(true);
  111. end;
  112. end;
  113. procedure consume_all_until(atoken : ttoken);
  114. begin
  115. while (token<>atoken) and (idtoken<>atoken) do
  116. begin
  117. Consume(token);
  118. if token=_EOF then
  119. begin
  120. Consume(atoken);
  121. Message(scan_f_end_of_file);
  122. exit;
  123. end;
  124. end;
  125. end;
  126. procedure consume_emptystats;
  127. begin
  128. repeat
  129. until not try_to_consume(_SEMICOLON);
  130. end;
  131. { check if a symbol contains the hint directive, and if so gives out a hint
  132. if required.
  133. }
  134. function consume_sym(var srsym:tsym;var srsymtable:tsymtable):boolean;
  135. begin
  136. { first check for identifier }
  137. if token<>_ID then
  138. begin
  139. consume(_ID);
  140. srsym:=generrorsym;
  141. srsymtable:=nil;
  142. result:=false;
  143. exit;
  144. end;
  145. searchsym(pattern,srsym,srsymtable);
  146. { handle unit specification like System.Writeln }
  147. try_consume_unitsym(srsym,srsymtable);
  148. { if nothing found give error and return errorsym }
  149. if assigned(srsym) then
  150. check_hints(srsym,srsym.symoptions)
  151. else
  152. begin
  153. identifier_not_found(orgpattern);
  154. srsym:=generrorsym;
  155. srsymtable:=nil;
  156. end;
  157. consume(_ID);
  158. result:=assigned(srsym);
  159. end;
  160. function try_consume_unitsym(var srsym:tsym;var srsymtable:tsymtable):boolean;
  161. begin
  162. result:=false;
  163. if assigned(srsym) and
  164. (srsym.typ=unitsym) then
  165. begin
  166. if not(srsym.owner.symtabletype in [staticsymtable,globalsymtable]) then
  167. internalerror(200501154);
  168. { only allow unit.symbol access if the name was
  169. found in the current module }
  170. if srsym.owner.iscurrentunit then
  171. begin
  172. consume(_ID);
  173. consume(_POINT);
  174. searchsym_in_module(tunitsym(srsym).module,pattern,srsym,srsymtable);
  175. end
  176. else
  177. begin
  178. srsym:=nil;
  179. srsymtable:=nil;
  180. end;
  181. result:=true;
  182. end;
  183. end;
  184. function try_consume_hintdirective(var symopt:tsymoptions):boolean;
  185. begin
  186. try_consume_hintdirective:=false;
  187. if not(m_hintdirective in aktmodeswitches) then
  188. exit;
  189. repeat
  190. case idtoken of
  191. _LIBRARY :
  192. begin
  193. include(symopt,sp_hint_library);
  194. try_consume_hintdirective:=true;
  195. end;
  196. _DEPRECATED :
  197. begin
  198. include(symopt,sp_hint_deprecated);
  199. try_consume_hintdirective:=true;
  200. end;
  201. _PLATFORM :
  202. begin
  203. include(symopt,sp_hint_platform);
  204. try_consume_hintdirective:=true;
  205. end;
  206. _UNIMPLEMENTED :
  207. begin
  208. include(symopt,sp_hint_unimplemented);
  209. try_consume_hintdirective:=true;
  210. end;
  211. else
  212. break;
  213. end;
  214. consume(Token);
  215. until false;
  216. end;
  217. end.