tokens.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 by Florian Klaempfl, Pierre Muller
  4. Tokens used by the compiler
  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 tokens;
  19. interface
  20. uses
  21. globtype;
  22. const
  23. tokenidlen=14;
  24. tokheader=#8'Free Pascal Compiler -- Token data'#13#10#26;
  25. type
  26. ttoken=(NOTOKEN,
  27. { operators, which can also be overloaded }
  28. _PLUS,
  29. _MINUS,
  30. _STAR,
  31. _SLASH,
  32. _EQUAL,
  33. _GT,
  34. _LT,
  35. _GTE,
  36. _LTE,
  37. _SYMDIF,
  38. _STARSTAR,
  39. _OP_IS,
  40. _OP_AS,
  41. _OP_IN,
  42. _ASSIGNMENT,
  43. { special chars }
  44. _CARET,
  45. _UNEQUAL,
  46. _LECKKLAMMER,
  47. _RECKKLAMMER,
  48. _POINT,
  49. _COMMA,
  50. _LKLAMMER,
  51. _RKLAMMER,
  52. _COLON,
  53. _SEMICOLON,
  54. _KLAMMERAFFE,
  55. _POINTPOINT,
  56. _DOUBLEADDR,
  57. _EOF,
  58. _ID,
  59. _NOID,
  60. _REALNUMBER,
  61. _INTCONST,
  62. _CSTRING,
  63. _CCHAR,
  64. { C like operators }
  65. _PLUSASN,
  66. _MINUSASN,
  67. _ANDASN,
  68. _ORASN,
  69. _STARASN,
  70. _SLASHASN,
  71. _MODASN,
  72. _DIVASN,
  73. _NOTASN,
  74. _XORASN,
  75. { Normal words }
  76. _AS,
  77. _AT,
  78. _DO,
  79. _IF,
  80. _IN,
  81. _IS,
  82. _OF,
  83. _ON,
  84. _OR,
  85. _TO,
  86. _AND,
  87. _ASM,
  88. _DIV,
  89. _END,
  90. _FAR,
  91. _FOR,
  92. _MOD,
  93. _NEW,
  94. _NIL,
  95. _NOT,
  96. _SET,
  97. _SHL,
  98. _SHR,
  99. _TRY,
  100. _VAR,
  101. _XOR,
  102. _CASE,
  103. _CVAR,
  104. _ELSE,
  105. _EXIT,
  106. _FAIL,
  107. _FILE,
  108. _GOTO,
  109. _NAME,
  110. _NEAR,
  111. _READ,
  112. _SELF,
  113. _THEN,
  114. _TRUE,
  115. _TYPE,
  116. _UNIT,
  117. _USES,
  118. _WITH,
  119. _ALIAS,
  120. _ARRAY,
  121. _BEGIN,
  122. _BREAK,
  123. _CDECL,
  124. _CLASS,
  125. _CONST,
  126. _FALSE,
  127. _INDEX,
  128. _LABEL,
  129. _RAISE,
  130. _UNTIL,
  131. _WHILE,
  132. _WRITE,
  133. _DOWNTO,
  134. _EXCEPT,
  135. _EXPORT,
  136. _INLINE,
  137. _OBJECT,
  138. _PACKED,
  139. _PASCAL,
  140. _PUBLIC,
  141. _RECORD,
  142. _REPEAT,
  143. _RESULT,
  144. _STATIC,
  145. _STORED,
  146. _STRING,
  147. _SYSTEM,
  148. _ASMNAME,
  149. _DEFAULT,
  150. _DISPOSE,
  151. _DYNAMIC,
  152. _EXPORTS,
  153. _FINALLY,
  154. _FORWARD,
  155. _IOCHECK,
  156. _LIBRARY,
  157. _MESSAGE,
  158. _PRIVATE,
  159. _PROGRAM,
  160. _STDCALL,
  161. _SYSCALL,
  162. _VIRTUAL,
  163. _ABSOLUTE,
  164. _ABSTRACT,
  165. _CONTINUE,
  166. _EXTERNAL,
  167. _FUNCTION,
  168. _OPERATOR,
  169. _OVERRIDE,
  170. _POPSTACK,
  171. _PROPERTY,
  172. _REGISTER,
  173. _RESIDENT,
  174. _SAFECALL,
  175. _ASSEMBLER,
  176. _INHERITED,
  177. _INTERFACE,
  178. _INTERRUPT,
  179. _NODEFAULT,
  180. _OTHERWISE,
  181. _PROCEDURE,
  182. _PROTECTED,
  183. _PUBLISHED,
  184. _THREADVAR,
  185. _DESTRUCTOR,
  186. _INTERNPROC,
  187. _OPENSTRING,
  188. _CONSTRUCTOR,
  189. _INTERNCONST,
  190. _SHORTSTRING,
  191. _FINALIZATION,
  192. _SAVEREGISTERS,
  193. _IMPLEMENTATION,
  194. _INITIALIZATION,
  195. _RESOURCESTRING
  196. );
  197. tokenrec=record
  198. str : string[tokenidlen];
  199. special : boolean;
  200. keyword : tmodeswitch;
  201. encoded : longint;
  202. end;
  203. ttokenarray=array[ttoken] of tokenrec;
  204. ptokenarray=^ttokenarray;
  205. tokenidxrec=record
  206. first,last : ttoken;
  207. end;
  208. ptokenidx=^ttokenidx;
  209. ttokenidx=array[2..tokenidlen,'A'..'Z'] of tokenidxrec;
  210. const
  211. arraytokeninfo : ttokenarray =(
  212. (str:'' ;special:true ;keyword:m_none),
  213. { Operators which can be overloaded }
  214. (str:'+' ;special:true ;keyword:m_none),
  215. (str:'-' ;special:true ;keyword:m_none),
  216. (str:'*' ;special:true ;keyword:m_none),
  217. (str:'/' ;special:true ;keyword:m_none),
  218. (str:'=' ;special:true ;keyword:m_none),
  219. (str:'>' ;special:true ;keyword:m_none),
  220. (str:'<' ;special:true ;keyword:m_none),
  221. (str:'>=' ;special:true ;keyword:m_none),
  222. (str:'<=' ;special:true ;keyword:m_none),
  223. (str:'><' ;special:true ;keyword:m_none),
  224. (str:'**' ;special:true ;keyword:m_none),
  225. (str:'is' ;special:true ;keyword:m_none),
  226. (str:'as' ;special:true ;keyword:m_none),
  227. (str:'in' ;special:true ;keyword:m_none),
  228. (str:':=' ;special:true ;keyword:m_none),
  229. { Special chars }
  230. (str:'^' ;special:true ;keyword:m_none),
  231. (str:'<>' ;special:true ;keyword:m_none),
  232. (str:'[' ;special:true ;keyword:m_none),
  233. (str:']' ;special:true ;keyword:m_none),
  234. (str:'.' ;special:true ;keyword:m_none),
  235. (str:',' ;special:true ;keyword:m_none),
  236. (str:'(' ;special:true ;keyword:m_none),
  237. (str:')' ;special:true ;keyword:m_none),
  238. (str:':' ;special:true ;keyword:m_none),
  239. (str:';' ;special:true ;keyword:m_none),
  240. (str:'@' ;special:true ;keyword:m_none),
  241. (str:'..' ;special:true ;keyword:m_none),
  242. (str:'@@' ;special:true ;keyword:m_none),
  243. (str:'end of file' ;special:true ;keyword:m_none),
  244. (str:'identifier' ;special:true ;keyword:m_none),
  245. (str:'non identifier';special:true ;keyword:m_none),
  246. (str:'const real' ;special:true ;keyword:m_none),
  247. (str:'ordinal const' ;special:true ;keyword:m_none),
  248. (str:'const string' ;special:true ;keyword:m_none),
  249. (str:'const char' ;special:true ;keyword:m_none),
  250. { C like operators }
  251. (str:'+=' ;special:true ;keyword:m_none),
  252. (str:'-=' ;special:true ;keyword:m_none),
  253. (str:'&=' ;special:true ;keyword:m_none),
  254. (str:'|=' ;special:true ;keyword:m_none),
  255. (str:'*=' ;special:true ;keyword:m_none),
  256. (str:'/=' ;special:true ;keyword:m_none),
  257. (str:'' ;special:true ;keyword:m_none),
  258. (str:'' ;special:true ;keyword:m_none),
  259. (str:'' ;special:true ;keyword:m_none),
  260. (str:'' ;special:true ;keyword:m_none),
  261. { Normal words }
  262. (str:'AS' ;special:false;keyword:m_class),
  263. (str:'AT' ;special:false;keyword:m_none),
  264. (str:'DO' ;special:false;keyword:m_all),
  265. (str:'IF' ;special:false;keyword:m_all),
  266. (str:'IN' ;special:false;keyword:m_all),
  267. (str:'IS' ;special:false;keyword:m_class),
  268. (str:'OF' ;special:false;keyword:m_all),
  269. (str:'ON' ;special:false;keyword:m_class),
  270. (str:'OR' ;special:false;keyword:m_all),
  271. (str:'TO' ;special:false;keyword:m_all),
  272. (str:'AND' ;special:false;keyword:m_all),
  273. (str:'ASM' ;special:false;keyword:m_all),
  274. (str:'DIV' ;special:false;keyword:m_all),
  275. (str:'END' ;special:false;keyword:m_all),
  276. (str:'FAR' ;special:false;keyword:m_none),
  277. (str:'FOR' ;special:false;keyword:m_all),
  278. (str:'MOD' ;special:false;keyword:m_all),
  279. (str:'NEW' ;special:false;keyword:m_all),
  280. (str:'NIL' ;special:false;keyword:m_all),
  281. (str:'NOT' ;special:false;keyword:m_all),
  282. (str:'SET' ;special:false;keyword:m_all),
  283. (str:'SHL' ;special:false;keyword:m_all),
  284. (str:'SHR' ;special:false;keyword:m_all),
  285. (str:'TRY' ;special:false;keyword:m_class),
  286. (str:'VAR' ;special:false;keyword:m_all),
  287. (str:'XOR' ;special:false;keyword:m_all),
  288. (str:'CASE' ;special:false;keyword:m_all),
  289. (str:'CVAR' ;special:false;keyword:m_none),
  290. (str:'ELSE' ;special:false;keyword:m_all),
  291. (str:'EXIT' ;special:false;keyword:m_all),
  292. (str:'FAIL' ;special:false;keyword:m_none), { only set within constructors PM }
  293. (str:'FILE' ;special:false;keyword:m_all),
  294. (str:'GOTO' ;special:false;keyword:m_all),
  295. (str:'NAME' ;special:false;keyword:m_none),
  296. (str:'NEAR' ;special:false;keyword:m_none),
  297. (str:'READ' ;special:false;keyword:m_none),
  298. (str:'SELF' ;special:false;keyword:m_none), {set inside methods only PM }
  299. (str:'THEN' ;special:false;keyword:m_all),
  300. (str:'TRUE' ;special:false;keyword:m_all),
  301. (str:'TYPE' ;special:false;keyword:m_all),
  302. (str:'UNIT' ;special:false;keyword:m_all),
  303. (str:'USES' ;special:false;keyword:m_all),
  304. (str:'WITH' ;special:false;keyword:m_all),
  305. (str:'ALIAS' ;special:false;keyword:m_none),
  306. (str:'ARRAY' ;special:false;keyword:m_all),
  307. (str:'BEGIN' ;special:false;keyword:m_all),
  308. (str:'BREAK' ;special:false;keyword:m_none),
  309. (str:'CDECL' ;special:false;keyword:m_none),
  310. (str:'CLASS' ;special:false;keyword:m_class),
  311. (str:'CONST' ;special:false;keyword:m_all),
  312. (str:'FALSE' ;special:false;keyword:m_all),
  313. (str:'INDEX' ;special:false;keyword:m_none),
  314. (str:'LABEL' ;special:false;keyword:m_all),
  315. (str:'RAISE' ;special:false;keyword:m_class),
  316. (str:'UNTIL' ;special:false;keyword:m_all),
  317. (str:'WHILE' ;special:false;keyword:m_all),
  318. (str:'WRITE' ;special:false;keyword:m_none),
  319. (str:'DOWNTO' ;special:false;keyword:m_all),
  320. (str:'EXCEPT' ;special:false;keyword:m_class),
  321. (str:'EXPORT' ;special:false;keyword:m_none),
  322. (str:'INLINE' ;special:false;keyword:m_none),
  323. (str:'OBJECT' ;special:false;keyword:m_all),
  324. (str:'PACKED' ;special:false;keyword:m_all),
  325. (str:'PASCAL' ;special:false;keyword:m_none),
  326. (str:'PUBLIC' ;special:false;keyword:m_none),
  327. (str:'RECORD' ;special:false;keyword:m_all),
  328. (str:'REPEAT' ;special:false;keyword:m_all),
  329. (str:'RESULT' ;special:false;keyword:m_none),
  330. (str:'STATIC' ;special:false;keyword:m_none),
  331. (str:'STORED' ;special:false;keyword:m_none),
  332. (str:'STRING' ;special:false;keyword:m_all),
  333. (str:'SYSTEM' ;special:false;keyword:m_none),
  334. (str:'ASMNAME' ;special:false;keyword:m_none),
  335. (str:'DEFAULT' ;special:false;keyword:m_none),
  336. (str:'DISPOSE' ;special:false;keyword:m_all),
  337. (str:'DYNAMIC' ;special:false;keyword:m_none),
  338. (str:'EXPORTS' ;special:false;keyword:m_all),
  339. (str:'FINALLY' ;special:false;keyword:m_class),
  340. (str:'FORWARD' ;special:false;keyword:m_none),
  341. (str:'IOCHECK' ;special:false;keyword:m_none),
  342. (str:'LIBRARY' ;special:false;keyword:m_all),
  343. (str:'MESSAGE' ;special:false;keyword:m_none),
  344. (str:'PRIVATE' ;special:false;keyword:m_none),
  345. (str:'PROGRAM' ;special:false;keyword:m_all),
  346. (str:'STDCALL' ;special:false;keyword:m_none),
  347. (str:'SYSCALL' ;special:false;keyword:m_none),
  348. (str:'VIRTUAL' ;special:false;keyword:m_none),
  349. (str:'ABSOLUTE' ;special:false;keyword:m_none),
  350. (str:'ABSTRACT' ;special:false;keyword:m_none),
  351. (str:'CONTINUE' ;special:false;keyword:m_none),
  352. (str:'EXTERNAL' ;special:false;keyword:m_none),
  353. (str:'FUNCTION' ;special:false;keyword:m_all),
  354. (str:'OPERATOR' ;special:false;keyword:m_fpc),
  355. (str:'OVERRIDE' ;special:false;keyword:m_none),
  356. (str:'POPSTACK' ;special:false;keyword:m_none),
  357. (str:'PROPERTY' ;special:false;keyword:m_class),
  358. (str:'REGISTER' ;special:false;keyword:m_none),
  359. (str:'RESIDENT' ;special:false;keyword:m_none),
  360. (str:'SAFECALL' ;special:false;keyword:m_none),
  361. (str:'ASSEMBLER' ;special:false;keyword:m_none),
  362. (str:'INHERITED' ;special:false;keyword:m_all),
  363. (str:'INTERFACE' ;special:false;keyword:m_all),
  364. (str:'INTERRUPT' ;special:false;keyword:m_none),
  365. (str:'NODEFAULT' ;special:false;keyword:m_none),
  366. (str:'OTHERWISE' ;special:false;keyword:m_all),
  367. (str:'PROCEDURE' ;special:false;keyword:m_all),
  368. (str:'PROTECTED' ;special:false;keyword:m_none),
  369. (str:'PUBLISHED' ;special:false;keyword:m_none),
  370. (str:'THREADVAR' ;special:false;keyword:m_class),
  371. (str:'DESTRUCTOR' ;special:false;keyword:m_all),
  372. (str:'INTERNPROC' ;special:false;keyword:m_none),
  373. (str:'OPENSTRING' ;special:false;keyword:m_none),
  374. (str:'CONSTRUCTOR' ;special:false;keyword:m_all),
  375. (str:'INTERNCONST' ;special:false;keyword:m_none),
  376. (str:'SHORTSTRING' ;special:false;keyword:m_none),
  377. (str:'FINALIZATION' ;special:false;keyword:m_initfinal),
  378. (str:'SAVEREGISTERS' ;special:false;keyword:m_none),
  379. (str:'IMPLEMENTATION';special:false;keyword:m_all),
  380. (str:'INITIALIZATION';special:false;keyword:m_initfinal),
  381. (str:'RESOURCESTRING';special:false;keyword:m_class)
  382. );
  383. var
  384. tokeninfo:ptokenarray;
  385. tokenidx:ptokenidx;
  386. procedure inittokens;
  387. procedure donetokens;
  388. procedure create_tokenidx;
  389. implementation
  390. uses
  391. globals;
  392. procedure create_tokenidx;
  393. { create an index with the first and last token for every possible token
  394. length, so a search only will be done in that small part }
  395. var
  396. t : ttoken;
  397. begin
  398. fillchar(tokenidx^,sizeof(tokenidx^),0);
  399. for t:=low(ttoken) to high(ttoken) do
  400. begin
  401. if not arraytokeninfo[t].special then
  402. begin
  403. if ord(tokenidx^[length(arraytokeninfo[t].str),arraytokeninfo[t].str[1]].first)=0 then
  404. tokenidx^[length(arraytokeninfo[t].str),arraytokeninfo[t].str[1]].first:=t;
  405. tokenidx^[length(arraytokeninfo[t].str),arraytokeninfo[t].str[1]].last:=t;
  406. end;
  407. end;
  408. end;
  409. procedure inittokens;
  410. {$ifdef TP}
  411. var
  412. f:file;
  413. header:string;
  414. a:longint;
  415. {$endif TP}
  416. begin
  417. {$ifdef TP}
  418. assign(f,exepath+'tokens.dat');
  419. reset(f,1);
  420. {We are not sure that the msg file is loaded!}
  421. if ioresult<>0 then
  422. begin
  423. close(f);
  424. { Very nice indeed !!! PM }
  425. writeln('Fatal: File tokens.dat not found.');
  426. halt(3);
  427. end;
  428. blockread(f,header,1);
  429. blockread(f,header[1],length(header));
  430. blockread(f,a,sizeof(a));
  431. if (header<>tokheader) or (a<>sizeof(ttokenarray)) then
  432. begin
  433. close(f);
  434. writeln('Fatal: File tokens.dat corrupt.');
  435. halt(3);
  436. end;
  437. new(tokeninfo);
  438. blockread(f,tokeninfo^,sizeof(ttokenarray));
  439. new(tokenidx);
  440. blockread(f,tokenidx^,sizeof(tokenidx^));
  441. close(f);
  442. {$else not TP}
  443. tokeninfo:=@arraytokeninfo;
  444. new(tokenidx);
  445. create_tokenidx;
  446. {$endif not TP}
  447. end;
  448. procedure donetokens;
  449. begin
  450. {$ifdef TP}
  451. dispose(tokeninfo);
  452. {$else TP}
  453. tokeninfo:=nil;
  454. {$endif TP}
  455. dispose(tokenidx);
  456. end;
  457. end.
  458. {
  459. $Log$
  460. Revision 1.14 1999-09-08 16:02:04 peter
  461. * tokendat compiles for tp
  462. * tokens.dat supplied by default
  463. Revision 1.13 1999/09/03 08:37:34 pierre
  464. * tokens.dat only used for TP, and also removed from
  465. compiler dependencies
  466. Revision 1.12 1999/09/02 18:47:49 daniel
  467. * Could not compile with TP, some arrays moved to heap
  468. * NOAG386BIN default for TP
  469. * AG386* files were not compatible with TP, fixed.
  470. Revision 1.11 1999/08/04 13:03:17 jonas
  471. * all tokens now start with an underscore
  472. * PowerPC compiles!!
  473. Revision 1.10 1999/08/03 22:03:39 peter
  474. * moved bitmask constants to sets
  475. * some other type/const renamings
  476. Revision 1.9 1999/07/22 09:38:01 florian
  477. + resourcestring implemented
  478. + start of longstring support
  479. Revision 1.8 1999/07/10 10:26:21 peter
  480. * merged
  481. Revision 1.7.2.1 1999/07/10 10:03:18 peter
  482. * fixed initialization/finalization in fpc mode
  483. * allow $TARGET also in search paths
  484. Revision 1.7 1999/05/24 08:55:30 florian
  485. * non working safecall directiv implemented, I don't know if we
  486. need it
  487. Revision 1.6 1999/04/28 06:02:19 florian
  488. * changes of Bruessel:
  489. + message handler can now take an explicit self
  490. * typinfo fixed: sometimes the type names weren't written
  491. * the type checking for pointer comparisations and subtraction
  492. and are now more strict (was also buggy)
  493. * small bug fix to link.pas to support compiling on another
  494. drive
  495. * probable bug in popt386 fixed: call/jmp => push/jmp
  496. transformation didn't count correctly the jmp references
  497. + threadvar support
  498. * warning if ln/sqrt gets an invalid constant argument
  499. Revision 1.5 1999/04/06 11:22:01 peter
  500. * more use of ttoken
  501. Revision 1.4 1999/04/01 22:07:53 peter
  502. * universal string names (ansistr instead of stransi) for val/str
  503. Revision 1.3 1999/02/22 20:13:41 florian
  504. + first implementation of message keyword
  505. Revision 1.2 1999/02/05 12:51:21 florian
  506. + openstring id is now supported
  507. Revision 1.1 1998/12/11 00:04:02 peter
  508. + globtype,tokens,version unit splitted from globals
  509. }