pbase.pas 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. {
  2. $Id$
  3. Copyright (c) 1998 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. interface
  20. uses
  21. cobjects,globals,scanner,symtable,systems,verbose;
  22. const
  23. { forward types should only be possible inside }
  24. { a TYPE statement, this crashed the compiler }
  25. { when trying to dispose local symbols }
  26. typecanbeforward : boolean = false;
  27. { true, if we are after an assignement }
  28. afterassignment : boolean = false;
  29. { sspecial for handling procedure vars }
  30. getprocvar : boolean = false;
  31. getprocvardef : pprocvardef = nil;
  32. type
  33. tblock_type = (bt_general,bt_type,bt_const);
  34. var
  35. { contains the current token to be processes }
  36. token : ttoken;
  37. { size of data segment, set by proc_unit or proc_program }
  38. datasize : longint;
  39. { for operators }
  40. optoken : ttoken;
  41. opsym : pvarsym;
  42. { symtable were unit references are stored }
  43. refsymtable : psymtable;
  44. { true, if only routine headers should be }
  45. { parsed }
  46. parse_only : boolean;
  47. { true, if we are in a except block }
  48. in_except_block : boolean;
  49. { type of currently parsed block }
  50. { isn't full implemented (FK) }
  51. block_type : tblock_type;
  52. { true, if we should ignore an equal in const x : 1..2=2 }
  53. ignore_equal : boolean;
  54. { consumes token i, if the current token is unequal i }
  55. { a syntax error is written }
  56. procedure consume(i : ttoken);
  57. { consumes all tokens til atoken (for error recovering }
  58. procedure consume_all_until(atoken : ttoken);
  59. { consumes tokens while they are semicolons }
  60. procedure emptystats;
  61. { reads a list of identifiers into a string container }
  62. function idlist : pstringcontainer;
  63. { inserts the symbols of sc in st with def as definition }
  64. { sc is disposed }
  65. procedure insert_syms(st : psymtable;sc : pstringcontainer;def : pdef);
  66. implementation
  67. { consumes token i, if the current token is unequal i }
  68. { a syntax error is written }
  69. procedure consume(i : ttoken);
  70. { generates a syntax error message }
  71. procedure syntaxerror(const s : string);
  72. begin
  73. Message2(scan_f_syn_expected,tostr(get_current_col),s);
  74. end;
  75. { This is changed since I changed the order of token
  76. in cobjects.pas for operator overloading !!!! }
  77. { ttoken = (PLUS,MINUS,STAR,SLASH,EQUAL,GT,LT,LTE,GTE,SYMDIF,CARET,ASSIGNMENT,
  78. LECKKLAMMER,RECKKLAMMER,
  79. POINT,COMMA,LKLAMMER,RKLAMMER,COLON,SEMICOLON,
  80. KLAMMERAFFE,UNEQUAL,POINTPOINT,
  81. ID,REALNUMBER,_EOF,INTCONST,CSTRING,CCHAR,DOUBLEADDR,}
  82. const tokens : array[PLUS..DOUBLEADDR] of string[12] = (
  83. '+','-','*','/','=','>','<','>=','<=','is','as','in',
  84. '><','^',':=','<>','[',']','.',',','(',')',':',';',
  85. '@','..',
  86. 'identifier','const real.','end of file',
  87. 'ord const','const string','const char','@@');
  88. var
  89. j : integer;
  90. begin
  91. if token<>i then
  92. begin
  93. if i<_AND then
  94. syntaxerror(tokens[i])
  95. else
  96. begin
  97. { um die ProgrammgrӇe klein zu halten, }
  98. { wird f�r ein Schl�sselwort-Token der }
  99. { "Text" in der Schl�sselworttabelle }
  100. { des Scanners nachgeschaut }
  101. for j:=1 to anz_keywords do
  102. if keyword_token[j]=i then
  103. syntaxerror(keyword[j])
  104. end;
  105. end
  106. else
  107. token:=yylex;
  108. end;
  109. procedure consume_all_until(atoken : ttoken);
  110. begin
  111. while (token<>atoken) and (token<>_EOF) do
  112. consume(token);
  113. { this will create an error if the token is _EOF }
  114. if token<>atoken then
  115. consume(atoken);
  116. { this error is fatal as we have read the whole file }
  117. Message(scan_f_end_of_file);
  118. end;
  119. procedure emptystats;
  120. begin
  121. while token=SEMICOLON do
  122. consume(SEMICOLON);
  123. end;
  124. { reads a list of identifiers into a string container }
  125. function idlist : pstringcontainer;
  126. var
  127. sc : pstringcontainer;
  128. begin
  129. sc:=new(pstringcontainer,init);
  130. repeat
  131. sc^.insert(pattern);
  132. consume(ID);
  133. if token=COMMA then consume(COMMA)
  134. else break
  135. until false;
  136. idlist:=sc;
  137. end;
  138. { inserts the symbols of sc in st with def as definition }
  139. { sc is disposed }
  140. procedure insert_syms(st : psymtable;sc : pstringcontainer;def : pdef);
  141. var
  142. s : string;
  143. begin
  144. s:=sc^.get;
  145. while s<>'' do
  146. begin
  147. st^.insert(new(pvarsym,init(s,def)));
  148. { static data fields are inserted in the globalsymtable }
  149. if (st^.symtabletype=objectsymtable) and
  150. ((current_object_option and sp_static)<>0) then
  151. begin
  152. s:=lowercase(st^.name^)+'_'+s;
  153. st^.defowner^.owner^.insert(new(pvarsym,init(s,def)));
  154. end;
  155. s:=sc^.get;
  156. end;
  157. dispose(sc,done);
  158. end;
  159. end.
  160. {
  161. $Log$
  162. Revision 1.2 1998-04-07 22:45:05 florian
  163. * bug0092, bug0115 and bug0121 fixed
  164. + packed object/class/array
  165. Revision 1.1.1.1 1998/03/25 11:18:14 root
  166. * Restored version
  167. Revision 1.9 1998/03/10 01:17:23 peter
  168. * all files have the same header
  169. * messages are fully implemented, EXTDEBUG uses Comment()
  170. + AG... files for the Assembler generation
  171. Revision 1.8 1998/03/06 00:52:40 peter
  172. * replaced all old messages from errore.msg, only ExtDebug and some
  173. Comment() calls are left
  174. * fixed options.pas
  175. Revision 1.7 1998/03/02 01:48:59 peter
  176. * renamed target_DOS to target_GO32V1
  177. + new verbose system, merged old errors and verbose units into one new
  178. verbose.pas, so errors.pas is obsolete
  179. Revision 1.6 1998/02/16 12:51:38 michael
  180. + Implemented linker object
  181. Revision 1.5 1998/02/13 10:35:22 daniel
  182. * Made Motorola version compilable.
  183. * Fixed optimizer
  184. Revision 1.4 1998/02/12 11:50:24 daniel
  185. Yes! Finally! After three retries, my patch!
  186. Changes:
  187. Complete rewrite of psub.pas.
  188. Added support for DLL's.
  189. Compiler requires less memory.
  190. Platform units for each platform.
  191. Revision 1.3 1998/01/13 17:13:08 michael
  192. * File time handling and file searching is now done in an OS-independent way,
  193. using the new file treating functions in globals.pas.
  194. Revision 1.2 1998/01/09 09:09:58 michael
  195. + Initial implementation, second try
  196. }