scanner.pas 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368
  1. {
  2. $Id$
  3. Copyright (c) 1993,97 by Florian Klaempfl
  4. This unit implements the scanner part and handling of the switches
  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. {$ifdef tp}
  19. {$F+,N+,E+,R-}
  20. {$endif}
  21. unit scanner;
  22. interface
  23. uses
  24. cobjects,globals,files;
  25. const
  26. {$ifdef TP}
  27. maxmacrolen = 1024;
  28. {$else}
  29. maxmacrolen = 16*1024;
  30. {$endif}
  31. id_len = 14;
  32. Newline = #10;
  33. type
  34. ident = string[id_len];
  35. const
  36. max_keywords = 69;
  37. anz_keywords : longint = max_keywords;
  38. { the following keywords are no keywords in TP, they
  39. are internal procedures
  40. CONTINUE, DISPOSE, EXIT, FAIL, FALSE, NEW, SELF
  41. TRUE
  42. }
  43. { INLINE is a keyword in TP, but only an modifier in FPC }
  44. keyword : array[1..max_keywords] of ident = (
  45. { 'ABSOLUTE',}
  46. 'AND',
  47. 'ARRAY','AS','ASM',
  48. { 'ASSEMBLER',}
  49. 'BEGIN',
  50. 'CASE','CLASS',
  51. 'CONST','CONSTRUCTOR',
  52. 'DESTRUCTOR','DISPOSE','DIV','DO','DOWNTO','ELSE','END',
  53. 'EXCEPT',
  54. 'EXIT',
  55. { 'EXPORT',}
  56. 'EXPORTS',
  57. { 'EXTERNAL',}
  58. 'FAIL','FALSE',
  59. { 'FAR',}
  60. 'FILE','FINALLY','FOR',
  61. { 'FORWARD',}
  62. 'FUNCTION','GOTO','IF','IMPLEMENTATION','IN',
  63. 'INHERITED','INITIALIZATION',
  64. { 'INLINE',} {INLINE is a reserved word in TP. Why?}
  65. 'INTERFACE',
  66. { 'INTERRUPT',}
  67. 'IS',
  68. 'LABEL','LIBRARY','MOD',
  69. { 'NEAR',}
  70. 'NEW','NIL','NOT','OBJECT',
  71. 'OF','ON','OPERATOR','OR','OTHERWISE','PACKED',
  72. 'PROCEDURE','PROGRAM','PROPERTY',
  73. 'RAISE','RECORD','REPEAT','SELF',
  74. 'SET','SHL','SHR','STRING','THEN','TO',
  75. 'TRUE','TRY','TYPE','UNIT','UNTIL',
  76. 'USES','VAR',
  77. { 'VIRTUAL',}
  78. 'WHILE','WITH','XOR');
  79. keyword_token : array[1..max_keywords] of ttoken = (
  80. { _ABSOLUTE,}
  81. _AND,
  82. _ARRAY,_AS,_ASM,
  83. { _ASSEMBLER,}
  84. _BEGIN,
  85. _CASE,_CLASS,
  86. _CONST,_CONSTRUCTOR,
  87. _DESTRUCTOR,_DISPOSE,_DIV,_DO,_DOWNTO,
  88. _ELSE,_END,_EXCEPT,
  89. _EXIT,
  90. { _EXPORT,}
  91. _EXPORTS,
  92. { _EXTERNAL,}
  93. _FAIL,_FALSE,
  94. { _FAR,}
  95. _FILE,_FINALLY,_FOR,
  96. { _FORWARD,}
  97. _FUNCTION,_GOTO,_IF,_IMPLEMENTATION,_IN,
  98. _INHERITED,_INITIALIZATION,
  99. { _INLINE,}
  100. _INTERFACE,
  101. { _INTERRUPT,}
  102. _IS,
  103. _LABEL,_LIBRARY,_MOD,
  104. { _NEAR,}
  105. _NEW,_NIL,_NOT,_OBJECT,
  106. _OF,_ON,_OPERATOR,_OR,_OTHERWISE,_PACKED,
  107. _PROCEDURE,_PROGRAM,_PROPERTY,
  108. _RAISE,_RECORD,_REPEAT,_SELF,
  109. _SET,_SHL,_SHR,_STRING,_THEN,_TO,
  110. _TRUE,_TRY,_TYPE,_UNIT,_UNTIL,
  111. _USES,_VAR,
  112. { _VIRTUAL,}
  113. _WHILE,_WITH,_XOR);
  114. type
  115. pmacrobuffer = ^tmacrobuffer;
  116. tmacrobuffer = array[0..maxmacrolen-1] of char;
  117. ppreprocstack = ^tpreprocstack;
  118. tpreprocstack = object
  119. isifdef,
  120. accept : boolean;
  121. next : ppreprocstack;
  122. name : stringid;
  123. line_nb : longint;
  124. constructor init(ifdef,a:boolean;n:ppreprocstack);
  125. destructor done;
  126. end;
  127. var
  128. c : char;
  129. orgpattern,
  130. pattern : string;
  131. macrobuffer : ^tmacrobuffer;
  132. lastlinepos,
  133. lasttokenpos,
  134. inputbuffer,
  135. inputpointer : pchar;
  136. { parse_types, } { true, if type declarations are parsed }
  137. s_point : boolean;
  138. comment_level,
  139. yylexcount,
  140. macropos : longint;
  141. lastasmgetchar : char;
  142. preprocstack : ppreprocstack;
  143. {public}
  144. procedure syntaxerror(const s : string);
  145. function yylex : ttoken;
  146. function asmgetchar : char;
  147. { column position of last token }
  148. function get_current_col : longint;
  149. { column position of file }
  150. function get_file_col : longint;
  151. procedure get_cur_file_pos(var fileinfo : tfileposinfo);
  152. procedure set_cur_file_pos(const fileinfo : tfileposinfo);
  153. procedure InitScanner(const fn: string);
  154. procedure DoneScanner(testendif:boolean);
  155. { changes to keywords to be tp compatible }
  156. procedure change_to_tp_keywords;
  157. implementation
  158. uses
  159. dos,verbose,systems,symtable,switches;
  160. {*****************************************************************************
  161. TPreProcStack
  162. *****************************************************************************}
  163. constructor tpreprocstack.init(ifdef,a:boolean;n:ppreprocstack);
  164. begin
  165. isifdef:=ifdef;
  166. accept:=a;
  167. next:=n;
  168. end;
  169. destructor tpreprocstack.done;
  170. begin
  171. end;
  172. procedure popstack;
  173. var
  174. hp : ppreprocstack;
  175. begin
  176. hp:=preprocstack^.next;
  177. dispose(preprocstack,done);
  178. preprocstack:=hp;
  179. end;
  180. {*****************************************************************************
  181. Helper routines
  182. *****************************************************************************}
  183. function is_keyword(var token : ttoken) : boolean;
  184. var
  185. high,low,mid : longint;
  186. begin
  187. low:=1;
  188. high:=anz_keywords;
  189. while low<high do
  190. begin
  191. mid:=(high+low+1) shr 1;
  192. if pattern<keyword[mid] then
  193. high:=mid-1
  194. else
  195. low:=mid;
  196. end;
  197. if pattern=keyword[high] then
  198. begin
  199. token:=keyword_token[high];
  200. is_keyword:=true;
  201. end
  202. else
  203. is_keyword:=false;
  204. end;
  205. procedure remove_keyword(const s : string);
  206. var
  207. i,j : longint;
  208. begin
  209. for i:=1 to anz_keywords do
  210. begin
  211. if keyword[i]=s then
  212. begin
  213. for j:=i to anz_keywords-1 do
  214. begin
  215. keyword[j]:=keyword[j+1];
  216. keyword_token[j]:=keyword_token[j+1];
  217. end;
  218. dec(anz_keywords);
  219. break;
  220. end;
  221. end;
  222. end;
  223. const
  224. current_column : longint = 1;
  225. function get_current_col : longint;
  226. begin
  227. get_current_col:=current_column;
  228. end;
  229. function get_file_col : longint;
  230. begin
  231. (* how was expecting files larger than 2Go ???
  232. {$ifdef TP}
  233. if lastlinepos<=lasttokenpos then
  234. get_file_col:=longint(lasttokenpos)-longint(lastlinepos)
  235. else
  236. get_file_col:=longint(lastlinepos)-longint(lasttokenpos);
  237. {$else}
  238. get_file_col:=cardinal(lasttokenpos)-cardinal(lastlinepos);
  239. {$endif} *)
  240. get_file_col:=longint(lasttokenpos)-longint(lastlinepos);
  241. end;
  242. procedure inc_comment_level;
  243. begin
  244. inc(comment_level);
  245. if (comment_level>1) then
  246. Message1(scan_w_comment_level,tostr(comment_level));
  247. end;
  248. procedure dec_comment_level;
  249. begin
  250. if cs_tp_compatible in aktswitches then
  251. comment_level:=0
  252. else
  253. dec(comment_level);
  254. end;
  255. procedure syntaxerror(const s : string);
  256. begin
  257. Message2(scan_f_syn_expected,tostr(get_current_col),s);
  258. end;
  259. {*****************************************************************************
  260. Scanner
  261. *****************************************************************************}
  262. procedure reload;
  263. var
  264. readsize : word;
  265. i,saveline : longint;
  266. begin
  267. if not assigned(current_module^.current_inputfile) then
  268. internalerror(14);
  269. if current_module^.current_inputfile^.filenotatend then
  270. begin
  271. { load the next piece of source }
  272. blockread(current_module^.current_inputfile^.f,inputbuffer^,
  273. current_module^.current_inputfile^.bufsize-1,readsize);
  274. { Scan the buffer for #0 chars, which are not alllowed }
  275. if readsize > 0 then
  276. begin
  277. { force proper line counting }
  278. saveline:=current_module^.current_inputfile^.true_line;
  279. i:=0;
  280. inputpointer:=inputbuffer;
  281. while i<readsize do
  282. begin
  283. c:=inputpointer^;
  284. case c of
  285. #0 : Message(scan_f_illegal_char);
  286. #10,#13 : begin
  287. if (byte(c)+byte(inputpointer[1])=23) then
  288. begin
  289. inc(longint(inputpointer));
  290. inc(i);
  291. end;
  292. inc(current_module^.current_inputfile^.true_line);
  293. end;
  294. end;
  295. inc(i);
  296. inc(longint(inputpointer));
  297. end;
  298. current_module^.current_inputfile^.true_line:=saveline;
  299. end;
  300. inputbuffer[readsize]:=#0;
  301. inputpointer:=inputbuffer;
  302. { Set EOF when main source and at endoffile }
  303. if eof(current_module^.current_inputfile^.f) then
  304. begin
  305. current_module^.current_inputfile^.filenotatend:=false;
  306. if current_module^.current_inputfile^.next=nil then
  307. inputbuffer[readsize]:=#26;
  308. end;
  309. end
  310. else
  311. begin
  312. current_module^.current_inputfile^.close;
  313. { load next module }
  314. current_module^.current_inputfile:=current_module^.current_inputfile^.next;
  315. current_module^.current_index:=current_module^.current_inputfile^.ref_index;
  316. status.currentsource:=current_module^.current_inputfile^.name^+current_module^.current_inputfile^.ext^;
  317. inputbuffer:=current_module^.current_inputfile^.buf;
  318. inputpointer:=inputbuffer+current_module^.current_inputfile^.bufpos;
  319. end;
  320. { load next char }
  321. c:=inputpointer^;
  322. inc(longint(inputpointer));
  323. end;
  324. procedure linebreak;
  325. var
  326. cur : char;
  327. begin
  328. if (byte(inputpointer^)=0) and
  329. current_module^.current_inputfile^.filenotatend then
  330. begin
  331. cur:=c;
  332. reload;
  333. if byte(cur)+byte(c)<>23 then
  334. dec(longint(inputpointer));
  335. end
  336. else
  337. begin
  338. { Fix linebreak to be only newline (=#10) for all types of linebreaks }
  339. if (byte(inputpointer^)+byte(c)=23) then
  340. inc(longint(inputpointer));
  341. end;
  342. c:=newline;
  343. { show status }
  344. Comment(V_Status,'');
  345. { increase line counters }
  346. inc(current_module^.current_inputfile^.true_line);
  347. status.currentline:=current_module^.current_inputfile^.true_line;
  348. inc(status.compiledlines);
  349. lastlinepos:=inputpointer;
  350. end;
  351. procedure readchar;
  352. begin
  353. c:=inputpointer^;
  354. if c=#0 then
  355. reload
  356. else
  357. inc(longint(inputpointer));
  358. if c in [#10,#13] then
  359. linebreak;
  360. end;
  361. function readstring:string;
  362. var
  363. i : longint;
  364. begin
  365. i:=0;
  366. { 'in []' splitted, so it will be CMP's and no SET_IN_BYTE (PFV) }
  367. while (c in ['A'..'Z','a'..'z']) or (c in ['0'..'9','_']) do
  368. begin
  369. if i<255 then
  370. begin
  371. inc(i);
  372. readstring[i]:=c;
  373. end;
  374. { get next char }
  375. c:=inputpointer^;
  376. if c=#0 then
  377. reload
  378. else
  379. inc(longint(inputpointer));
  380. end;
  381. { was the next char a linebreak ? }
  382. if c in [#10,#13] then
  383. linebreak;
  384. readstring[0]:=chr(i);
  385. end;
  386. function readid:string;
  387. begin
  388. readid:=upper(readstring);
  389. end;
  390. function readnumber:string;
  391. var
  392. base,
  393. i : longint;
  394. begin
  395. case c of
  396. '%' : begin
  397. readchar;
  398. base:=2;
  399. readnumber[1]:='%';
  400. i:=1;
  401. end;
  402. '$' : begin
  403. readchar;
  404. base:=16;
  405. readnumber[1]:='$';
  406. i:=1;
  407. end;
  408. else
  409. begin
  410. base:=10;
  411. i:=0;
  412. end;
  413. end;
  414. while ((base>=10) and (c in ['0'..'9'])) or
  415. ((base=16) and (c in ['A'..'F','a'..'f'])) or
  416. ((base=2) and (c in ['0'..'1'])) do
  417. begin
  418. if i<255 then
  419. begin
  420. inc(i);
  421. readnumber[i]:=c;
  422. end;
  423. { get next char }
  424. c:=inputpointer^;
  425. if c=#0 then
  426. reload
  427. else
  428. inc(longint(inputpointer));
  429. end;
  430. { was the next char a linebreak ? }
  431. if c in [#10,#13] then
  432. linebreak;
  433. readnumber[0]:=chr(i);
  434. end;
  435. function readval:longint;
  436. var
  437. l : longint;
  438. w : word;
  439. begin
  440. val(readnumber,l,w);
  441. readval:=l;
  442. end;
  443. function readcomment:string;
  444. var
  445. i : longint;
  446. begin
  447. i:=0;
  448. repeat
  449. case c of
  450. '}' : begin
  451. readchar;
  452. dec_comment_level;
  453. break;
  454. end;
  455. #26 : Message(scan_f_end_of_file);
  456. else
  457. begin
  458. if (i<255) then
  459. begin
  460. inc(i);
  461. readcomment[i]:=c;
  462. end;
  463. end;
  464. end;
  465. { readchar; }
  466. c:=inputpointer^;
  467. if c=#0 then
  468. reload
  469. else
  470. inc(longint(inputpointer));
  471. if c in [#10,#13] then
  472. linebreak;
  473. until false;
  474. readcomment[0]:=chr(i);
  475. end;
  476. procedure skipspace;
  477. begin
  478. while c in [' ',#9..#13] do
  479. begin
  480. { readchar; }
  481. c:=inputpointer^;
  482. if c=#0 then
  483. reload
  484. else
  485. inc(longint(inputpointer));
  486. if c in [#10,#13] then
  487. linebreak;
  488. end;
  489. end;
  490. procedure skipuntildirective;
  491. var
  492. found : longint;
  493. begin
  494. found:=0;
  495. repeat
  496. case c of
  497. #26 : Message(scan_f_end_of_file);
  498. '{' : begin
  499. if comment_level=0 then
  500. found:=1;
  501. inc_comment_level;
  502. end;
  503. '}' : begin
  504. dec_comment_level;
  505. found:=0;
  506. end;
  507. '$' : begin
  508. if found=1 then
  509. found:=2;
  510. end;
  511. else
  512. found:=0;
  513. end;
  514. { readchar;}
  515. c:=inputpointer^;
  516. if c=#0 then
  517. reload
  518. else
  519. inc(longint(inputpointer));
  520. if c in [#10,#13] then
  521. linebreak;
  522. until (found=2);
  523. end;
  524. {$i scandir.inc}
  525. procedure skipcomment;
  526. begin
  527. readchar;
  528. inc_comment_level;
  529. { handle compiler switches }
  530. if (c='$') then
  531. handledirectives;
  532. { handle_switches can dec comment_level, }
  533. while (comment_level>0) do
  534. begin
  535. case c of
  536. '{' : inc_comment_level;
  537. '}' : dec_comment_level;
  538. #26 : Message(scan_f_end_of_file);
  539. end;
  540. { readchar; }
  541. c:=inputpointer^;
  542. if c=#0 then
  543. reload
  544. else
  545. inc(longint(inputpointer));
  546. if c in [#10,#13] then
  547. linebreak;
  548. end;
  549. end;
  550. procedure skipdelphicomment;
  551. begin
  552. inc_comment_level;
  553. readchar;
  554. { this is currently not supported }
  555. if c='$' then
  556. Message(scan_e_wrong_styled_switch);
  557. { skip comment }
  558. while c<>newline do
  559. begin
  560. if c=#26 then
  561. Message(scan_f_end_of_file);
  562. readchar;
  563. end;
  564. dec_comment_level;
  565. end;
  566. procedure skipoldtpcomment;
  567. var
  568. found : longint;
  569. begin
  570. inc_comment_level;
  571. readchar;
  572. { this is currently not supported }
  573. if c='$' then
  574. Message(scan_e_wrong_styled_switch);
  575. { skip comment }
  576. while (comment_level>0) do
  577. begin
  578. found:=0;
  579. repeat
  580. case c of
  581. #26 : Message(scan_f_end_of_file);
  582. '*' : begin
  583. if found=3 then
  584. inc_comment_level
  585. else
  586. found:=1;
  587. end;
  588. ')' : begin
  589. if found=1 then
  590. begin
  591. dec_comment_level;
  592. if comment_level=0 then
  593. found:=2;
  594. end;
  595. end;
  596. '(' : found:=3;
  597. else
  598. found:=0;
  599. end;
  600. { readchar; }
  601. c:=inputpointer^;
  602. if c=#0 then
  603. reload
  604. else
  605. inc(longint(inputpointer));
  606. if c in [#10,#13] then
  607. linebreak;
  608. until (found=2);
  609. end;
  610. end;
  611. function yylex : ttoken;
  612. var
  613. y : ttoken;
  614. code : word;
  615. l : longint;
  616. mac : pmacrosym;
  617. hp : pinputfile;
  618. hp2 : pchar;
  619. label
  620. exit_label;
  621. begin
  622. { was the last character a point ? }
  623. { this code is needed because the scanner if there is a 1. found if }
  624. { this is a floating point number or range like 1..3 }
  625. if s_point then
  626. begin
  627. tokenpos.line:=current_module^.current_inputfile^.true_line;
  628. tokenpos.column:=get_file_col;
  629. tokenpos.fileindex:=current_module^.current_index;
  630. s_point:=false;
  631. if c='.' then
  632. begin
  633. readchar;
  634. yylex:=POINTPOINT;
  635. goto exit_label;
  636. end;
  637. yylex:=POINT;
  638. goto exit_label;
  639. end;
  640. { Skip all spaces and comments }
  641. repeat
  642. case c of
  643. '{' : skipcomment;
  644. ' ',#9..#13 : skipspace;
  645. else
  646. break;
  647. end;
  648. until false;
  649. { Save current token position }
  650. lasttokenpos:=inputpointer;
  651. tokenpos.line:=current_module^.current_inputfile^.true_line;
  652. tokenpos.column:=get_file_col;
  653. tokenpos.fileindex:=current_module^.current_index;
  654. { Check first for a identifier/keyword, this is 20+% faster (PFV) }
  655. if c in ['_','A'..'Z','a'..'z'] then
  656. begin
  657. orgpattern:=readstring;
  658. pattern:=upper(orgpattern);
  659. if (length(pattern) in [2..id_len]) and is_keyword(y) then
  660. yylex:=y
  661. else
  662. begin
  663. { this takes some time ... }
  664. if support_macros then
  665. begin
  666. mac:=pmacrosym(macros^.search(pattern));
  667. if assigned(mac) and (assigned(mac^.buftext)) then
  668. begin
  669. { don't forget the last char }
  670. dec(longint(inputpointer));
  671. current_module^.current_inputfile^.bufpos:=inputpointer-inputbuffer;
  672. { this isn't a proper way, but ... }
  673. hp:=new(pinputfile,init('','Macro '+pattern,''));
  674. hp^.next:=current_module^.current_inputfile;
  675. current_module^.current_inputfile:=hp;
  676. status.currentsource:=current_module^.current_inputfile^.name^;
  677. { I don't think that we should do that
  678. because otherwise the file will be searched !! (PM)
  679. but there is the problem of index !! }
  680. current_module^.sourcefiles.register_file(hp);
  681. current_module^.current_index:=hp^.ref_index;
  682. { set an own buffer }
  683. getmem(hp2,mac^.buflen+1);
  684. current_module^.current_inputfile^.setbuf(hp2,mac^.buflen+1);
  685. inputbuffer:=current_module^.current_inputfile^.buf;
  686. { copy text }
  687. move(mac^.buftext^,inputbuffer^,mac^.buflen);
  688. { put end sign }
  689. inputbuffer[mac^.buflen+1]:=#0;
  690. { load c }
  691. c:=inputbuffer^;
  692. inputpointer:=inputbuffer+1;
  693. { handle empty macros }
  694. if c=#0 then
  695. reload;
  696. { play it again ... }
  697. inc(yylexcount);
  698. if yylexcount>16 then
  699. Message(scan_w_macro_deep_ten);
  700. {$ifdef TP}
  701. yylex:=yylex;
  702. {$else}
  703. yylex:=yylex();
  704. {$endif}
  705. { that's all folks }
  706. dec(yylexcount);
  707. exit;
  708. end;
  709. end;
  710. yylex:=ID;
  711. end;
  712. goto exit_label;
  713. end
  714. else
  715. begin
  716. case c of
  717. '$' : begin
  718. pattern:=readnumber;
  719. yylex:=INTCONST;
  720. goto exit_label;
  721. end;
  722. '%' : begin
  723. pattern:=readnumber;
  724. yylex:=INTCONST;
  725. goto exit_label;
  726. end;
  727. '0'..'9' : begin
  728. pattern:=readnumber;
  729. case c of
  730. '.' : begin
  731. readchar;
  732. if not(c in ['0'..'9']) then
  733. begin
  734. s_point:=true;
  735. yylex:=INTCONST;
  736. goto exit_label;
  737. end;
  738. pattern:=pattern+'.';
  739. while c in ['0'..'9'] do
  740. begin
  741. pattern:=pattern+c;
  742. readchar;
  743. end;
  744. yylex:=REALNUMBER;
  745. goto exit_label;
  746. end;
  747. 'e','E' : begin
  748. pattern:=pattern+'E';
  749. readchar;
  750. if c in ['-','+'] then
  751. begin
  752. pattern:=pattern+c;
  753. readchar;
  754. end;
  755. if not(c in ['0'..'9']) then
  756. Message(scan_f_illegal_char);
  757. while c in ['0'..'9'] do
  758. begin
  759. pattern:=pattern+c;
  760. readchar;
  761. end;
  762. yylex:=REALNUMBER;
  763. goto exit_label;
  764. end;
  765. end;
  766. yylex:=INTCONST;
  767. goto exit_label;
  768. end;
  769. ';' : begin
  770. readchar;
  771. yylex:=SEMICOLON;
  772. goto exit_label;
  773. end;
  774. '[' : begin
  775. readchar;
  776. yylex:=LECKKLAMMER;
  777. goto exit_label;
  778. end;
  779. ']' : begin
  780. readchar;
  781. yylex:=RECKKLAMMER;
  782. goto exit_label;
  783. end;
  784. '(' : begin
  785. readchar;
  786. if c='*' then
  787. begin
  788. skipoldtpcomment;
  789. {$ifndef TP}
  790. yylex:=yylex();
  791. {$else TP}
  792. yylex:=yylex;
  793. {$endif TP}
  794. exit;
  795. end;
  796. yylex:=LKLAMMER;
  797. goto exit_label;
  798. end;
  799. ')' : begin
  800. readchar;
  801. yylex:=RKLAMMER;
  802. goto exit_label;
  803. end;
  804. '+' : begin
  805. readchar;
  806. if (c='=') and support_c_operators then
  807. begin
  808. readchar;
  809. yylex:=_PLUSASN;
  810. goto exit_label;
  811. end;
  812. yylex:=PLUS;
  813. goto exit_label;
  814. end;
  815. '-' : begin
  816. readchar;
  817. if (c='=') and support_c_operators then
  818. begin
  819. readchar;
  820. yylex:=_MINUSASN;
  821. goto exit_label;
  822. end;
  823. yylex:=MINUS;
  824. goto exit_label;
  825. end;
  826. ':' : begin
  827. readchar;
  828. if c='=' then
  829. begin
  830. readchar;
  831. yylex:=ASSIGNMENT;
  832. goto exit_label;
  833. end;
  834. yylex:=COLON;
  835. goto exit_label;
  836. end;
  837. '*' : begin
  838. readchar;
  839. if (c='=') and support_c_operators then
  840. begin
  841. readchar;
  842. yylex:=_STARASN;
  843. end else if c='*' then
  844. begin
  845. readchar;
  846. yylex:=STARSTAR;
  847. end
  848. else
  849. yylex:=STAR;
  850. goto exit_label;
  851. end;
  852. '/' : begin
  853. readchar;
  854. case c of
  855. '=' : begin
  856. if support_c_operators then
  857. begin
  858. readchar;
  859. yylex:=_SLASHASN;
  860. goto exit_label;
  861. end;
  862. end;
  863. '/' : begin
  864. skipdelphicomment;
  865. {$ifndef TP}
  866. yylex:=yylex();
  867. {$else TP}
  868. yylex:=yylex;
  869. {$endif TP}
  870. exit;
  871. end;
  872. end;
  873. yylex:=SLASH;
  874. goto exit_label;
  875. end;
  876. '=' : begin
  877. readchar;
  878. yylex:=EQUAL;
  879. goto exit_label;
  880. end;
  881. '.' : begin
  882. readchar;
  883. if c='.' then
  884. begin
  885. readchar;
  886. yylex:=POINTPOINT;
  887. goto exit_label;
  888. end
  889. else
  890. yylex:=POINT;
  891. goto exit_label;
  892. end;
  893. '@' : begin
  894. readchar;
  895. if c='@' then
  896. begin
  897. readchar;
  898. yylex:=DOUBLEADDR;
  899. end
  900. else
  901. yylex:=KLAMMERAFFE;
  902. goto exit_label;
  903. end;
  904. ',' : begin
  905. readchar;
  906. yylex:=COMMA;
  907. goto exit_label;
  908. end;
  909. '''','#','^' : begin
  910. if c='^' then
  911. begin
  912. readchar;
  913. c:=upcase(c);
  914. if not(block_type=bt_type) and (c in ['A'..'Z']) then
  915. begin
  916. pattern:=chr(ord(c)-64);
  917. readchar;
  918. end
  919. else
  920. begin
  921. yylex:=CARET;
  922. goto exit_label;
  923. end;
  924. end
  925. else
  926. pattern:='';
  927. repeat
  928. case c of
  929. '#' : begin
  930. readchar; { read # }
  931. valint(readnumber,l,code);
  932. if (code<>0) or (l<0) or (l>255) then
  933. Message(scan_e_illegal_char_const);
  934. pattern:=pattern+chr(l);
  935. end;
  936. '''' : begin
  937. repeat
  938. readchar;
  939. case c of
  940. #26 : Message(scan_f_end_of_file);
  941. newline : Message(scan_f_string_exceeds_line);
  942. '''' : begin
  943. readchar;
  944. if c<>'''' then
  945. break;
  946. end;
  947. end;
  948. pattern:=pattern+c;
  949. until false;
  950. end;
  951. '^' : begin
  952. readchar;
  953. if c<#64 then
  954. c:=chr(ord(c)+64)
  955. else
  956. c:=chr(ord(c)-64);
  957. pattern:=pattern+c;
  958. readchar;
  959. end;
  960. else
  961. break;
  962. end;
  963. until false;
  964. { strings with length 1 become const chars }
  965. if length(pattern)=1 then
  966. yylex:=CCHAR
  967. else
  968. yylex:=CSTRING;
  969. goto exit_label;
  970. end;
  971. '>' : begin
  972. readchar;
  973. case c of
  974. '=' : begin
  975. readchar;
  976. yylex:=GTE;
  977. goto exit_label;
  978. end;
  979. '>' : begin
  980. readchar;
  981. yylex:=_SHR;
  982. goto exit_label;
  983. end;
  984. '<' : begin { >< is for a symetric diff for sets }
  985. readchar;
  986. yylex:=SYMDIF;
  987. goto exit_label;
  988. end;
  989. end;
  990. yylex:=GT;
  991. goto exit_label;
  992. end;
  993. '<' : begin
  994. readchar;
  995. case c of
  996. '>' : begin
  997. readchar;
  998. yylex:=UNEQUAL;
  999. goto exit_label;
  1000. end;
  1001. '=' : begin
  1002. readchar;
  1003. yylex:=LTE;
  1004. goto exit_label;
  1005. end;
  1006. '<' : begin
  1007. readchar;
  1008. yylex:=_SHL;
  1009. goto exit_label;
  1010. end;
  1011. end;
  1012. yylex:=LT;
  1013. goto exit_label;
  1014. end;
  1015. #26 : begin
  1016. yylex:=_EOF;
  1017. goto exit_label;
  1018. end;
  1019. else
  1020. begin
  1021. Message(scan_f_illegal_char);
  1022. end;
  1023. end;
  1024. end;
  1025. exit_label:
  1026. { don't change the file : too risky !! }
  1027. if current_module^.current_index=tokenpos.fileindex then
  1028. begin
  1029. current_module^.current_inputfile^.line_no:=tokenpos.line;
  1030. current_module^.current_inputfile^.column:=tokenpos.column;
  1031. current_column:=tokenpos.column;
  1032. end;
  1033. end;
  1034. function asmgetchar : char;
  1035. begin
  1036. if lastasmgetchar<>#0 then
  1037. begin
  1038. c:=lastasmgetchar;
  1039. lastasmgetchar:=#0;
  1040. end
  1041. else
  1042. readchar;
  1043. { must be put in the assembler readers }
  1044. (* with tokenpos do
  1045. begin
  1046. line:=current_module^.current_inputfile^.true_line;
  1047. column:=get_file_col;
  1048. fileindex:=current_module^.current_index;
  1049. end; *)
  1050. case c of
  1051. '{' : begin
  1052. skipcomment;
  1053. lastasmgetchar:=c;
  1054. asmgetchar:=';';
  1055. exit;
  1056. end;
  1057. '/' : begin
  1058. readchar;
  1059. if c='/' then
  1060. begin
  1061. skipdelphicomment;
  1062. asmgetchar:=';';
  1063. end
  1064. else
  1065. asmgetchar:='/';
  1066. lastasmgetchar:=c;
  1067. exit;
  1068. end;
  1069. '(' : begin
  1070. readchar;
  1071. if c='*' then
  1072. begin
  1073. skipoldtpcomment;
  1074. asmgetchar:=';';
  1075. end
  1076. else
  1077. asmgetchar:='(';
  1078. lastasmgetchar:=c;
  1079. exit;
  1080. end;
  1081. else
  1082. begin
  1083. asmgetchar:=c;
  1084. end;
  1085. end;
  1086. end;
  1087. procedure InitScanner(const fn: string);
  1088. var
  1089. d:dirstr;
  1090. n:namestr;
  1091. e:extstr;
  1092. begin
  1093. fsplit(fn,d,n,e);
  1094. current_module^.current_inputfile:=new(pinputfile,init(d,n,e));
  1095. current_module^.current_inputfile^.reset;
  1096. current_module^.sourcefiles.register_file(current_module^.current_inputfile);
  1097. current_module^.current_index:=current_module^.current_inputfile^.ref_index;
  1098. status.currentsource:=current_module^.current_inputfile^.name^+current_module^.current_inputfile^.ext^;
  1099. if ioresult<>0 then
  1100. Message(scan_f_cannot_open_input);
  1101. inputbuffer:=current_module^.current_inputfile^.buf;
  1102. reload;
  1103. preprocstack:=nil;
  1104. comment_level:=0;
  1105. lasttokenpos:=inputpointer;
  1106. lastlinepos:=inputpointer;
  1107. s_point:=false;
  1108. block_type:=bt_general;
  1109. end;
  1110. procedure get_cur_file_pos(var fileinfo : tfileposinfo);
  1111. begin
  1112. with fileinfo do
  1113. begin
  1114. line:=current_module^.current_inputfile^.line_no;
  1115. {fileinfo.fileindex:=current_module^.current_inputfile^.ref_index;}
  1116. { should allways be the same !! }
  1117. fileindex:=current_module^.current_index;
  1118. column:=get_current_col;
  1119. end;
  1120. end;
  1121. procedure set_cur_file_pos(const fileinfo : tfileposinfo);
  1122. begin
  1123. if current_module^.current_index<>fileinfo.fileindex then
  1124. begin
  1125. current_module^.current_index:=fileinfo.fileindex;
  1126. current_module^.current_inputfile:=
  1127. pinputfile(current_module^.sourcefiles.get_file(fileinfo.fileindex));
  1128. end;
  1129. if assigned(current_module^.current_inputfile) then
  1130. begin
  1131. current_module^.current_inputfile^.line_no:=fileinfo.line;
  1132. current_module^.current_inputfile^.column:=fileinfo.column;
  1133. current_column:=fileinfo.column;
  1134. end;
  1135. end;
  1136. procedure DoneScanner(testendif:boolean);
  1137. var
  1138. st : string[16];
  1139. begin
  1140. if (not testendif) then
  1141. begin
  1142. while assigned(preprocstack) do
  1143. begin
  1144. if preprocstack^.isifdef then
  1145. st:='$IF(N)(DEF)'
  1146. else
  1147. st:='$ELSE';
  1148. Message3(scan_e_endif_expected,st,preprocstack^.name,tostr(preprocstack^.line_nb));
  1149. popstack;
  1150. end;
  1151. end;
  1152. end;
  1153. procedure change_to_tp_keywords;
  1154. const
  1155. non_tp : array[0..13] of string[id_len] = (
  1156. 'AS','CLASS','EXCEPT','FINALLY','INITIALIZATION','IS',
  1157. 'ON','OPERATOR','OTHERWISE','PROPERTY','RAISE','TRY',
  1158. 'EXPORTS','LIBRARY');
  1159. var
  1160. i : longint;
  1161. begin
  1162. for i:=0 to 13 do
  1163. remove_keyword(non_tp[i]);
  1164. end;
  1165. procedure change_to_delphi_keywords;
  1166. {
  1167. const
  1168. non_tp : array[0..13] of string[id_len] = (
  1169. 'AS','CLASS','EXCEPT','FINALLY','INITIALIZATION','IS',
  1170. 'ON','OPERATOR','OTHERWISE','PROPERTY','RAISE','TRY',
  1171. 'EXPORTS','LIBRARY');
  1172. var
  1173. i : longint;
  1174. }
  1175. begin
  1176. {
  1177. for i:=0 to 13 do
  1178. remove_keyword(non_tp[i]);
  1179. }
  1180. end;
  1181. end.
  1182. {
  1183. $Log$
  1184. Revision 1.24 1998-06-12 10:32:36 pierre
  1185. * column problem hopefully solved
  1186. + C vars declaration changed
  1187. Revision 1.23 1998/06/03 22:49:02 peter
  1188. + wordbool,longbool
  1189. * rename bis,von -> high,low
  1190. * moved some systemunit loading/creating to psystem.pas
  1191. Revision 1.21 1998/05/27 00:20:32 peter
  1192. * some scanner optimizes
  1193. * automaticly aout2exe for go32v1
  1194. * fixed dynamiclinker option which was added at the wrong place
  1195. Revision 1.20 1998/05/23 01:21:30 peter
  1196. + aktasmmode, aktoptprocessor, aktoutputformat
  1197. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  1198. + $LIBNAME to set the library name where the unit will be put in
  1199. * splitted cgi386 a bit (codeseg to large for bp7)
  1200. * nasm, tasm works again. nasm moved to ag386nsm.pas
  1201. Revision 1.19 1998/05/20 09:42:37 pierre
  1202. + UseTokenInfo now default
  1203. * unit in interface uses and implementation uses gives error now
  1204. * only one error for unknown symbol (uses lastsymknown boolean)
  1205. the problem came from the label code !
  1206. + first inlined procedures and function work
  1207. (warning there might be allowed cases were the result is still wrong !!)
  1208. * UseBrower updated gives a global list of all position of all used symbols
  1209. with switch -gb
  1210. Revision 1.18 1998/05/12 10:47:00 peter
  1211. * moved printstatus to verb_def
  1212. + V_Normal which is between V_Error and V_Warning and doesn't have a
  1213. prefix like error: warning: and is included in V_Default
  1214. * fixed some messages
  1215. * first time parameter scan is only for -v and -T
  1216. - removed old style messages
  1217. Revision 1.17 1998/05/06 08:38:47 pierre
  1218. * better position info with UseTokenInfo
  1219. UseTokenInfo greatly simplified
  1220. + added check for changed tree after first time firstpass
  1221. (if we could remove all the cases were it happen
  1222. we could skip all firstpass if firstpasscount > 1)
  1223. Only with ExtDebug
  1224. Revision 1.16 1998/05/04 17:54:28 peter
  1225. + smartlinking works (only case jumptable left todo)
  1226. * redesign of systems.pas to support assemblers and linkers
  1227. + Unitname is now also in the PPU-file, increased version to 14
  1228. Revision 1.15 1998/05/01 16:38:46 florian
  1229. * handling of private and protected fixed
  1230. + change_keywords_to_tp implemented to remove
  1231. keywords which aren't supported by tp
  1232. * break and continue are now symbols of the system unit
  1233. + widestring, longstring and ansistring type released
  1234. Revision 1.14 1998/04/30 15:59:42 pierre
  1235. * GDB works again better :
  1236. correct type info in one pass
  1237. + UseTokenInfo for better source position
  1238. * fixed one remaining bug in scanner for line counts
  1239. * several little fixes
  1240. Revision 1.13 1998/04/29 13:42:27 peter
  1241. + $IOCHECKS and $ALIGN to test already, other will follow soon
  1242. * fixed the wrong linecounting with comments
  1243. Revision 1.12 1998/04/29 10:34:04 pierre
  1244. + added some code for ansistring (not complete nor working yet)
  1245. * corrected operator overloading
  1246. * corrected nasm output
  1247. + started inline procedures
  1248. + added starstarn : use ** for exponentiation (^ gave problems)
  1249. + started UseTokenInfo cond to get accurate positions
  1250. Revision 1.11 1998/04/27 23:10:29 peter
  1251. + new scanner
  1252. * $makelib -> if smartlink
  1253. * small filename fixes pmodule.setfilename
  1254. * moved import from files.pas -> import.pas
  1255. }