scanner.pas 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341
  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. var tokenpos : tfileposinfo;
  144. {public}
  145. procedure syntaxerror(const s : string);
  146. function yylex : ttoken;
  147. function asmgetchar : char;
  148. function get_current_col : longint;
  149. procedure get_cur_file_pos(var fileinfo : tfileposinfo);
  150. procedure set_cur_file_pos(const fileinfo : tfileposinfo);
  151. procedure InitScanner(const fn: string);
  152. procedure DoneScanner(testendif:boolean);
  153. { changes to keywords to be tp compatible }
  154. procedure change_to_tp_keywords;
  155. implementation
  156. uses
  157. dos,verbose,systems,
  158. pbase,symtable,
  159. 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. function get_current_col : longint;
  224. begin
  225. {$ifdef TP}
  226. if lastlinepos<=lasttokenpos then
  227. get_current_col:=longint(lasttokenpos)-longint(lastlinepos)
  228. else
  229. get_current_col:=longint(lastlinepos)-longint(lasttokenpos);
  230. {$else}
  231. get_current_col:=cardinal(lasttokenpos)-cardinal(lastlinepos);
  232. {$endif}
  233. end;
  234. procedure inc_comment_level;
  235. begin
  236. inc(comment_level);
  237. if (comment_level>1) then
  238. Message1(scan_w_comment_level,tostr(comment_level));
  239. end;
  240. procedure dec_comment_level;
  241. begin
  242. if cs_tp_compatible in aktswitches then
  243. comment_level:=0
  244. else
  245. dec(comment_level);
  246. end;
  247. procedure syntaxerror(const s : string);
  248. begin
  249. Message2(scan_f_syn_expected,tostr(get_current_col),s);
  250. end;
  251. {*****************************************************************************
  252. Scanner
  253. *****************************************************************************}
  254. procedure reload;
  255. var
  256. readsize : word;
  257. i,saveline : longint;
  258. begin
  259. if not assigned(current_module^.current_inputfile) then
  260. internalerror(14);
  261. if current_module^.current_inputfile^.filenotatend then
  262. begin
  263. { load the next piece of source }
  264. blockread(current_module^.current_inputfile^.f,inputbuffer^,
  265. current_module^.current_inputfile^.bufsize-1,readsize);
  266. { Scan the buffer for #0 chars, which are not alllowed }
  267. if readsize > 0 then
  268. begin
  269. { force proper line counting }
  270. saveline:=current_module^.current_inputfile^.line_no;
  271. i:=0;
  272. inputpointer:=inputbuffer;
  273. while i<readsize do
  274. begin
  275. c:=inputpointer^;
  276. case c of
  277. #0 : Message(scan_f_illegal_char);
  278. #10,#13 : begin
  279. if (byte(c)+byte(inputpointer[1])=23) then
  280. begin
  281. inc(longint(inputpointer));
  282. inc(i);
  283. end;
  284. inc(current_module^.current_inputfile^.line_no);
  285. end;
  286. end;
  287. inc(i);
  288. inc(longint(inputpointer));
  289. end;
  290. current_module^.current_inputfile^.line_no:=saveline;
  291. end;
  292. inputbuffer[readsize]:=#0;
  293. inputpointer:=inputbuffer;
  294. { Set EOF when main source and at endoffile }
  295. if eof(current_module^.current_inputfile^.f) then
  296. begin
  297. current_module^.current_inputfile^.filenotatend:=false;
  298. if current_module^.current_inputfile^.next=nil then
  299. inputbuffer[readsize]:=#26;
  300. end;
  301. end
  302. else
  303. begin
  304. current_module^.current_inputfile^.close;
  305. { load next module }
  306. current_module^.current_inputfile:=current_module^.current_inputfile^.next;
  307. current_module^.current_index:=current_module^.current_inputfile^.ref_index;
  308. status.currentsource:=current_module^.current_inputfile^.name^+current_module^.current_inputfile^.ext^;
  309. inputbuffer:=current_module^.current_inputfile^.buf;
  310. inputpointer:=inputbuffer+current_module^.current_inputfile^.bufpos;
  311. end;
  312. { load next char }
  313. c:=inputpointer^;
  314. inc(longint(inputpointer));
  315. end;
  316. procedure linebreak;
  317. var
  318. cur : char;
  319. begin
  320. if (byte(inputpointer^)=0) and
  321. current_module^.current_inputfile^.filenotatend then
  322. begin
  323. cur:=c;
  324. reload;
  325. if byte(cur)+byte(c)<>23 then
  326. dec(longint(inputpointer));
  327. end
  328. else
  329. begin
  330. { Fix linebreak to be only newline (=#10) for all types of linebreaks }
  331. if (byte(inputpointer^)+byte(c)=23) then
  332. inc(longint(inputpointer));
  333. end;
  334. c:=newline;
  335. { show status }
  336. Comment(V_Status,'');
  337. { increase line counters }
  338. inc(current_module^.current_inputfile^.line_no);
  339. status.currentline:=current_module^.current_inputfile^.line_no;
  340. inc(status.compiledlines);
  341. lastlinepos:=inputpointer;
  342. end;
  343. procedure readchar;
  344. begin
  345. c:=inputpointer^;
  346. if c=#0 then
  347. reload
  348. else
  349. inc(longint(inputpointer));
  350. if c in [#10,#13] then
  351. linebreak;
  352. end;
  353. function readstring:string;
  354. var
  355. i : longint;
  356. begin
  357. i:=0;
  358. { 'in []' splitted, so it will be CMP's and no SET_IN_BYTE (PFV) }
  359. while (c in ['A'..'Z','a'..'z']) or (c in ['0'..'9','_']) do
  360. begin
  361. if i<255 then
  362. begin
  363. inc(i);
  364. readstring[i]:=c;
  365. end;
  366. { get next char }
  367. c:=inputpointer^;
  368. if c=#0 then
  369. reload
  370. else
  371. inc(longint(inputpointer));
  372. end;
  373. { was the next char a linebreak ? }
  374. if c in [#10,#13] then
  375. linebreak;
  376. readstring[0]:=chr(i);
  377. end;
  378. function readid:string;
  379. begin
  380. readid:=upper(readstring);
  381. end;
  382. function readnumber:string;
  383. var
  384. base,
  385. i : longint;
  386. begin
  387. case c of
  388. '%' : begin
  389. readchar;
  390. base:=2;
  391. readnumber[1]:='%';
  392. i:=1;
  393. end;
  394. '$' : begin
  395. readchar;
  396. base:=16;
  397. readnumber[1]:='$';
  398. i:=1;
  399. end;
  400. else
  401. begin
  402. base:=10;
  403. i:=0;
  404. end;
  405. end;
  406. while ((base>=10) and (c in ['0'..'9'])) or
  407. ((base=16) and (c in ['A'..'F','a'..'f'])) or
  408. ((base=2) and (c in ['0'..'1'])) do
  409. begin
  410. if i<255 then
  411. begin
  412. inc(i);
  413. readnumber[i]:=c;
  414. end;
  415. { get next char }
  416. c:=inputpointer^;
  417. if c=#0 then
  418. reload
  419. else
  420. inc(longint(inputpointer));
  421. end;
  422. { was the next char a linebreak ? }
  423. if c in [#10,#13] then
  424. linebreak;
  425. readnumber[0]:=chr(i);
  426. end;
  427. function readval:longint;
  428. var
  429. l : longint;
  430. w : word;
  431. begin
  432. val(readnumber,l,w);
  433. readval:=l;
  434. end;
  435. function readcomment:string;
  436. var
  437. i : longint;
  438. begin
  439. i:=0;
  440. repeat
  441. case c of
  442. '}' : begin
  443. readchar;
  444. dec_comment_level;
  445. break;
  446. end;
  447. #26 : Message(scan_f_end_of_file);
  448. else
  449. begin
  450. if (i<255) then
  451. begin
  452. inc(i);
  453. readcomment[i]:=c;
  454. end;
  455. end;
  456. end;
  457. { readchar; }
  458. c:=inputpointer^;
  459. if c=#0 then
  460. reload
  461. else
  462. inc(longint(inputpointer));
  463. if c in [#10,#13] then
  464. linebreak;
  465. until false;
  466. readcomment[0]:=chr(i);
  467. end;
  468. procedure skipspace;
  469. begin
  470. while c in [' ',#9..#13] do
  471. begin
  472. { readchar; }
  473. c:=inputpointer^;
  474. if c=#0 then
  475. reload
  476. else
  477. inc(longint(inputpointer));
  478. if c in [#10,#13] then
  479. linebreak;
  480. end;
  481. end;
  482. procedure skipuntildirective;
  483. var
  484. found : longint;
  485. begin
  486. found:=0;
  487. repeat
  488. case c of
  489. #26 : Message(scan_f_end_of_file);
  490. '{' : begin
  491. if comment_level=0 then
  492. found:=1;
  493. inc_comment_level;
  494. end;
  495. '}' : begin
  496. dec_comment_level;
  497. found:=0;
  498. end;
  499. '$' : begin
  500. if found=1 then
  501. found:=2;
  502. end;
  503. else
  504. found:=0;
  505. end;
  506. { readchar;}
  507. c:=inputpointer^;
  508. if c=#0 then
  509. reload
  510. else
  511. inc(longint(inputpointer));
  512. if c in [#10,#13] then
  513. linebreak;
  514. until (found=2);
  515. end;
  516. {$i scandir.inc}
  517. procedure skipcomment;
  518. begin
  519. readchar;
  520. inc_comment_level;
  521. { handle compiler switches }
  522. if (c='$') then
  523. handledirectives;
  524. { handle_switches can dec comment_level, }
  525. while (comment_level>0) do
  526. begin
  527. case c of
  528. '{' : inc_comment_level;
  529. '}' : dec_comment_level;
  530. #26 : Message(scan_f_end_of_file);
  531. end;
  532. { readchar; }
  533. c:=inputpointer^;
  534. if c=#0 then
  535. reload
  536. else
  537. inc(longint(inputpointer));
  538. if c in [#10,#13] then
  539. linebreak;
  540. end;
  541. end;
  542. procedure skipdelphicomment;
  543. begin
  544. inc_comment_level;
  545. readchar;
  546. { this is currently not supported }
  547. if c='$' then
  548. Message(scan_e_wrong_styled_switch);
  549. { skip comment }
  550. while c<>newline do
  551. begin
  552. if c=#26 then
  553. Message(scan_f_end_of_file);
  554. readchar;
  555. end;
  556. dec_comment_level;
  557. end;
  558. procedure skipoldtpcomment;
  559. var
  560. found : longint;
  561. begin
  562. inc_comment_level;
  563. readchar;
  564. { this is currently not supported }
  565. if c='$' then
  566. Message(scan_e_wrong_styled_switch);
  567. { skip comment }
  568. while (comment_level>0) do
  569. begin
  570. found:=0;
  571. repeat
  572. case c of
  573. #26 : Message(scan_f_end_of_file);
  574. '*' : begin
  575. if found=3 then
  576. inc_comment_level
  577. else
  578. found:=1;
  579. end;
  580. ')' : begin
  581. if found=1 then
  582. begin
  583. dec_comment_level;
  584. if comment_level=0 then
  585. found:=2;
  586. end;
  587. end;
  588. '(' : found:=3;
  589. else
  590. found:=0;
  591. end;
  592. { readchar; }
  593. c:=inputpointer^;
  594. if c=#0 then
  595. reload
  596. else
  597. inc(longint(inputpointer));
  598. if c in [#10,#13] then
  599. linebreak;
  600. until (found=2);
  601. end;
  602. end;
  603. function yylex : ttoken;
  604. var
  605. y : ttoken;
  606. code : word;
  607. l : longint;
  608. mac : pmacrosym;
  609. hp : pinputfile;
  610. hp2 : pchar;
  611. label
  612. exit_label;
  613. begin
  614. { was the last character a point ? }
  615. { this code is needed because the scanner if there is a 1. found if }
  616. { this is a floating point number or range like 1..3 }
  617. if s_point then
  618. begin
  619. tokenpos.line:=current_module^.current_inputfile^.line_no;
  620. tokenpos.column:=get_current_col;
  621. tokenpos.fileindex:=current_module^.current_index;
  622. s_point:=false;
  623. if c='.' then
  624. begin
  625. readchar;
  626. yylex:=POINTPOINT;
  627. goto exit_label;
  628. end;
  629. yylex:=POINT;
  630. goto exit_label;
  631. end;
  632. { Skip all spaces and comments }
  633. repeat
  634. case c of
  635. '{' : skipcomment;
  636. ' ',#9..#13 : skipspace;
  637. else
  638. break;
  639. end;
  640. until false;
  641. { Save current token position }
  642. lasttokenpos:=inputpointer;
  643. tokenpos.line:=current_module^.current_inputfile^.line_no;
  644. tokenpos.column:=get_current_col;
  645. tokenpos.fileindex:=current_module^.current_index;
  646. { Check first for a identifier/keyword, this is 20+% faster (PFV) }
  647. if c in ['_','A'..'Z','a'..'z'] then
  648. begin
  649. orgpattern:=readstring;
  650. pattern:=upper(orgpattern);
  651. if (length(pattern) in [2..id_len]) and is_keyword(y) then
  652. yylex:=y
  653. else
  654. begin
  655. { this takes some time ... }
  656. if support_macros then
  657. begin
  658. mac:=pmacrosym(macros^.search(pattern));
  659. if assigned(mac) and (assigned(mac^.buftext)) then
  660. begin
  661. { don't forget the last char }
  662. dec(longint(inputpointer));
  663. current_module^.current_inputfile^.bufpos:=inputpointer-inputbuffer;
  664. { this isn't a proper way, but ... }
  665. hp:=new(pinputfile,init('','Macro '+pattern,''));
  666. hp^.next:=current_module^.current_inputfile;
  667. current_module^.current_inputfile:=hp;
  668. status.currentsource:=current_module^.current_inputfile^.name^;
  669. { I don't think that we should do that
  670. because otherwise the file will be searched !! (PM)
  671. but there is the problem of index !! }
  672. current_module^.sourcefiles.register_file(hp);
  673. current_module^.current_index:=hp^.ref_index;
  674. { set an own buffer }
  675. getmem(hp2,mac^.buflen+1);
  676. current_module^.current_inputfile^.setbuf(hp2,mac^.buflen+1);
  677. inputbuffer:=current_module^.current_inputfile^.buf;
  678. { copy text }
  679. move(mac^.buftext^,inputbuffer^,mac^.buflen);
  680. { put end sign }
  681. inputbuffer[mac^.buflen+1]:=#0;
  682. { load c }
  683. c:=inputbuffer^;
  684. inputpointer:=inputbuffer+1;
  685. { handle empty macros }
  686. if c=#0 then
  687. reload;
  688. { play it again ... }
  689. inc(yylexcount);
  690. if yylexcount>16 then
  691. Message(scan_w_macro_deep_ten);
  692. {$ifdef TP}
  693. yylex:=yylex;
  694. {$else}
  695. yylex:=yylex();
  696. {$endif}
  697. { that's all folks }
  698. dec(yylexcount);
  699. exit;
  700. end;
  701. end;
  702. yylex:=ID;
  703. end;
  704. goto exit_label;
  705. end
  706. else
  707. begin
  708. case c of
  709. '$' : begin
  710. pattern:=readnumber;
  711. yylex:=INTCONST;
  712. goto exit_label;
  713. end;
  714. '%' : begin
  715. pattern:=readnumber;
  716. yylex:=INTCONST;
  717. goto exit_label;
  718. end;
  719. '0'..'9' : begin
  720. pattern:=readnumber;
  721. case c of
  722. '.' : begin
  723. readchar;
  724. if not(c in ['0'..'9']) then
  725. begin
  726. s_point:=true;
  727. yylex:=INTCONST;
  728. goto exit_label;
  729. end;
  730. pattern:=pattern+'.';
  731. while c in ['0'..'9'] do
  732. begin
  733. pattern:=pattern+c;
  734. readchar;
  735. end;
  736. yylex:=REALNUMBER;
  737. goto exit_label;
  738. end;
  739. 'e','E' : begin
  740. pattern:=pattern+'E';
  741. readchar;
  742. if c in ['-','+'] then
  743. begin
  744. pattern:=pattern+c;
  745. readchar;
  746. end;
  747. if not(c in ['0'..'9']) then
  748. Message(scan_f_illegal_char);
  749. while c in ['0'..'9'] do
  750. begin
  751. pattern:=pattern+c;
  752. readchar;
  753. end;
  754. yylex:=REALNUMBER;
  755. goto exit_label;
  756. end;
  757. end;
  758. yylex:=INTCONST;
  759. goto exit_label;
  760. end;
  761. ';' : begin
  762. readchar;
  763. yylex:=SEMICOLON;
  764. goto exit_label;
  765. end;
  766. '[' : begin
  767. readchar;
  768. yylex:=LECKKLAMMER;
  769. goto exit_label;
  770. end;
  771. ']' : begin
  772. readchar;
  773. yylex:=RECKKLAMMER;
  774. goto exit_label;
  775. end;
  776. '(' : begin
  777. readchar;
  778. if c='*' then
  779. begin
  780. skipoldtpcomment;
  781. {$ifndef TP}
  782. yylex:=yylex();
  783. {$else TP}
  784. yylex:=yylex;
  785. {$endif TP}
  786. exit;
  787. end;
  788. yylex:=LKLAMMER;
  789. goto exit_label;
  790. end;
  791. ')' : begin
  792. readchar;
  793. yylex:=RKLAMMER;
  794. goto exit_label;
  795. end;
  796. '+' : begin
  797. readchar;
  798. if (c='=') and support_c_operators then
  799. begin
  800. readchar;
  801. yylex:=_PLUSASN;
  802. goto exit_label;
  803. end;
  804. yylex:=PLUS;
  805. goto exit_label;
  806. end;
  807. '-' : begin
  808. readchar;
  809. if (c='=') and support_c_operators then
  810. begin
  811. readchar;
  812. yylex:=_MINUSASN;
  813. goto exit_label;
  814. end;
  815. yylex:=MINUS;
  816. goto exit_label;
  817. end;
  818. ':' : begin
  819. readchar;
  820. if c='=' then
  821. begin
  822. readchar;
  823. yylex:=ASSIGNMENT;
  824. goto exit_label;
  825. end;
  826. yylex:=COLON;
  827. goto exit_label;
  828. end;
  829. '*' : begin
  830. readchar;
  831. if (c='=') and support_c_operators then
  832. begin
  833. readchar;
  834. yylex:=_STARASN;
  835. end else if c='*' then
  836. begin
  837. readchar;
  838. yylex:=STARSTAR;
  839. end
  840. else
  841. yylex:=STAR;
  842. goto exit_label;
  843. end;
  844. '/' : begin
  845. readchar;
  846. case c of
  847. '=' : begin
  848. if support_c_operators then
  849. begin
  850. readchar;
  851. yylex:=_SLASHASN;
  852. goto exit_label;
  853. end;
  854. end;
  855. '/' : begin
  856. skipdelphicomment;
  857. {$ifndef TP}
  858. yylex:=yylex();
  859. {$else TP}
  860. yylex:=yylex;
  861. {$endif TP}
  862. exit;
  863. end;
  864. end;
  865. yylex:=SLASH;
  866. goto exit_label;
  867. end;
  868. '=' : begin
  869. readchar;
  870. yylex:=EQUAL;
  871. goto exit_label;
  872. end;
  873. '.' : begin
  874. readchar;
  875. if c='.' then
  876. begin
  877. readchar;
  878. yylex:=POINTPOINT;
  879. goto exit_label;
  880. end
  881. else
  882. yylex:=POINT;
  883. goto exit_label;
  884. end;
  885. '@' : begin
  886. readchar;
  887. if c='@' then
  888. begin
  889. readchar;
  890. yylex:=DOUBLEADDR;
  891. end
  892. else
  893. yylex:=KLAMMERAFFE;
  894. goto exit_label;
  895. end;
  896. ',' : begin
  897. readchar;
  898. yylex:=COMMA;
  899. goto exit_label;
  900. end;
  901. '''','#','^' : begin
  902. if c='^' then
  903. begin
  904. readchar;
  905. c:=upcase(c);
  906. if not(block_type=bt_type) and (c in ['A'..'Z']) then
  907. begin
  908. pattern:=chr(ord(c)-64);
  909. readchar;
  910. end
  911. else
  912. begin
  913. yylex:=CARET;
  914. goto exit_label;
  915. end;
  916. end
  917. else
  918. pattern:='';
  919. repeat
  920. case c of
  921. '#' : begin
  922. readchar; { read # }
  923. valint(readnumber,l,code);
  924. if (code<>0) or (l<0) or (l>255) then
  925. Message(scan_e_illegal_char_const);
  926. pattern:=pattern+chr(l);
  927. end;
  928. '''' : begin
  929. repeat
  930. readchar;
  931. case c of
  932. #26 : Message(scan_f_end_of_file);
  933. newline : Message(scan_f_string_exceeds_line);
  934. '''' : begin
  935. readchar;
  936. if c<>'''' then
  937. break;
  938. end;
  939. end;
  940. pattern:=pattern+c;
  941. until false;
  942. end;
  943. '^' : begin
  944. readchar;
  945. if c<#64 then
  946. c:=chr(ord(c)+64)
  947. else
  948. c:=chr(ord(c)-64);
  949. pattern:=pattern+c;
  950. readchar;
  951. end;
  952. else
  953. break;
  954. end;
  955. until false;
  956. { strings with length 1 become const chars }
  957. if length(pattern)=1 then
  958. yylex:=CCHAR
  959. else
  960. yylex:=CSTRING;
  961. goto exit_label;
  962. end;
  963. '>' : begin
  964. readchar;
  965. case c of
  966. '=' : begin
  967. readchar;
  968. yylex:=GTE;
  969. goto exit_label;
  970. end;
  971. '>' : begin
  972. readchar;
  973. yylex:=_SHR;
  974. goto exit_label;
  975. end;
  976. '<' : begin { >< is for a symetric diff for sets }
  977. readchar;
  978. yylex:=SYMDIF;
  979. goto exit_label;
  980. end;
  981. end;
  982. yylex:=GT;
  983. goto exit_label;
  984. end;
  985. '<' : begin
  986. readchar;
  987. case c of
  988. '>' : begin
  989. readchar;
  990. yylex:=UNEQUAL;
  991. goto exit_label;
  992. end;
  993. '=' : begin
  994. readchar;
  995. yylex:=LTE;
  996. goto exit_label;
  997. end;
  998. '<' : begin
  999. readchar;
  1000. yylex:=_SHL;
  1001. goto exit_label;
  1002. end;
  1003. end;
  1004. yylex:=LT;
  1005. goto exit_label;
  1006. end;
  1007. #26 : begin
  1008. yylex:=_EOF;
  1009. goto exit_label;
  1010. end;
  1011. else
  1012. begin
  1013. Message(scan_f_illegal_char);
  1014. end;
  1015. end;
  1016. end;
  1017. exit_label:
  1018. end;
  1019. function asmgetchar : char;
  1020. begin
  1021. if lastasmgetchar<>#0 then
  1022. begin
  1023. c:=lastasmgetchar;
  1024. lastasmgetchar:=#0;
  1025. end
  1026. else
  1027. readchar;
  1028. with tokenpos do
  1029. begin
  1030. line:=current_module^.current_inputfile^.line_no;
  1031. column:=get_current_col;
  1032. fileindex:=current_module^.current_index;
  1033. end;
  1034. case c of
  1035. '{' : begin
  1036. skipcomment;
  1037. lastasmgetchar:=c;
  1038. asmgetchar:=';';
  1039. exit;
  1040. end;
  1041. '/' : begin
  1042. readchar;
  1043. if c='/' then
  1044. begin
  1045. skipdelphicomment;
  1046. asmgetchar:=';';
  1047. end
  1048. else
  1049. asmgetchar:='/';
  1050. lastasmgetchar:=c;
  1051. exit;
  1052. end;
  1053. '(' : begin
  1054. readchar;
  1055. if c='*' then
  1056. begin
  1057. skipoldtpcomment;
  1058. asmgetchar:=';';
  1059. end
  1060. else
  1061. asmgetchar:='(';
  1062. lastasmgetchar:=c;
  1063. exit;
  1064. end;
  1065. else
  1066. begin
  1067. asmgetchar:=c;
  1068. end;
  1069. end;
  1070. end;
  1071. procedure InitScanner(const fn: string);
  1072. var
  1073. d:dirstr;
  1074. n:namestr;
  1075. e:extstr;
  1076. begin
  1077. fsplit(fn,d,n,e);
  1078. current_module^.current_inputfile:=new(pinputfile,init(d,n,e));
  1079. current_module^.current_inputfile^.reset;
  1080. current_module^.sourcefiles.register_file(current_module^.current_inputfile);
  1081. current_module^.current_index:=current_module^.current_inputfile^.ref_index;
  1082. status.currentsource:=current_module^.current_inputfile^.name^+current_module^.current_inputfile^.ext^;
  1083. if ioresult<>0 then
  1084. Message(scan_f_cannot_open_input);
  1085. inputbuffer:=current_module^.current_inputfile^.buf;
  1086. reload;
  1087. preprocstack:=nil;
  1088. comment_level:=0;
  1089. lasttokenpos:=inputpointer;
  1090. lastlinepos:=inputpointer;
  1091. s_point:=false;
  1092. end;
  1093. procedure get_cur_file_pos(var fileinfo : tfileposinfo);
  1094. begin
  1095. with fileinfo do
  1096. begin
  1097. line:=current_module^.current_inputfile^.line_no;
  1098. {fileinfo.fileindex:=current_module^.current_inputfile^.ref_index;}
  1099. { should allways be the same !! }
  1100. fileindex:=current_module^.current_index;
  1101. column:=get_current_col;
  1102. end;
  1103. end;
  1104. procedure set_cur_file_pos(const fileinfo : tfileposinfo);
  1105. begin
  1106. current_module^.current_index:=fileinfo.fileindex;
  1107. current_module^.current_inputfile:=
  1108. pinputfile(current_module^.sourcefiles.get_file(fileinfo.fileindex));
  1109. if assigned(current_module^.current_inputfile) then
  1110. current_module^.current_inputfile^.line_no:=fileinfo.line;
  1111. {fileinfo.fileindex:=current_module^.current_inputfile^.ref_index;}
  1112. { should allways be the same !! }
  1113. { fileinfo.column:=get_current_col; }
  1114. end;
  1115. procedure DoneScanner(testendif:boolean);
  1116. var
  1117. st : string[16];
  1118. begin
  1119. if (not testendif) then
  1120. begin
  1121. while assigned(preprocstack) do
  1122. begin
  1123. if preprocstack^.isifdef then
  1124. st:='$IF(N)(DEF)'
  1125. else
  1126. st:='$ELSE';
  1127. Message3(scan_e_endif_expected,st,preprocstack^.name,tostr(preprocstack^.line_nb));
  1128. popstack;
  1129. end;
  1130. end;
  1131. end;
  1132. procedure change_to_tp_keywords;
  1133. const
  1134. non_tp : array[0..13] of string[id_len] = (
  1135. 'AS','CLASS','EXCEPT','FINALLY','INITIALIZATION','IS',
  1136. 'ON','OPERATOR','OTHERWISE','PROPERTY','RAISE','TRY',
  1137. 'EXPORTS','LIBRARY');
  1138. var
  1139. i : longint;
  1140. begin
  1141. for i:=0 to 13 do
  1142. remove_keyword(non_tp[i]);
  1143. end;
  1144. procedure change_to_delphi_keywords;
  1145. {
  1146. const
  1147. non_tp : array[0..13] of string[id_len] = (
  1148. 'AS','CLASS','EXCEPT','FINALLY','INITIALIZATION','IS',
  1149. 'ON','OPERATOR','OTHERWISE','PROPERTY','RAISE','TRY',
  1150. 'EXPORTS','LIBRARY');
  1151. var
  1152. i : longint;
  1153. }
  1154. begin
  1155. {
  1156. for i:=0 to 13 do
  1157. remove_keyword(non_tp[i]);
  1158. }
  1159. end;
  1160. end.
  1161. {
  1162. $Log$
  1163. Revision 1.22 1998-05-31 14:10:54 peter
  1164. * better get_current_col
  1165. Revision 1.21 1998/05/27 00:20:32 peter
  1166. * some scanner optimizes
  1167. * automaticly aout2exe for go32v1
  1168. * fixed dynamiclinker option which was added at the wrong place
  1169. Revision 1.20 1998/05/23 01:21:30 peter
  1170. + aktasmmode, aktoptprocessor, aktoutputformat
  1171. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  1172. + $LIBNAME to set the library name where the unit will be put in
  1173. * splitted cgi386 a bit (codeseg to large for bp7)
  1174. * nasm, tasm works again. nasm moved to ag386nsm.pas
  1175. Revision 1.19 1998/05/20 09:42:37 pierre
  1176. + UseTokenInfo now default
  1177. * unit in interface uses and implementation uses gives error now
  1178. * only one error for unknown symbol (uses lastsymknown boolean)
  1179. the problem came from the label code !
  1180. + first inlined procedures and function work
  1181. (warning there might be allowed cases were the result is still wrong !!)
  1182. * UseBrower updated gives a global list of all position of all used symbols
  1183. with switch -gb
  1184. Revision 1.18 1998/05/12 10:47:00 peter
  1185. * moved printstatus to verb_def
  1186. + V_Normal which is between V_Error and V_Warning and doesn't have a
  1187. prefix like error: warning: and is included in V_Default
  1188. * fixed some messages
  1189. * first time parameter scan is only for -v and -T
  1190. - removed old style messages
  1191. Revision 1.17 1998/05/06 08:38:47 pierre
  1192. * better position info with UseTokenInfo
  1193. UseTokenInfo greatly simplified
  1194. + added check for changed tree after first time firstpass
  1195. (if we could remove all the cases were it happen
  1196. we could skip all firstpass if firstpasscount > 1)
  1197. Only with ExtDebug
  1198. Revision 1.16 1998/05/04 17:54:28 peter
  1199. + smartlinking works (only case jumptable left todo)
  1200. * redesign of systems.pas to support assemblers and linkers
  1201. + Unitname is now also in the PPU-file, increased version to 14
  1202. Revision 1.15 1998/05/01 16:38:46 florian
  1203. * handling of private and protected fixed
  1204. + change_keywords_to_tp implemented to remove
  1205. keywords which aren't supported by tp
  1206. * break and continue are now symbols of the system unit
  1207. + widestring, longstring and ansistring type released
  1208. Revision 1.14 1998/04/30 15:59:42 pierre
  1209. * GDB works again better :
  1210. correct type info in one pass
  1211. + UseTokenInfo for better source position
  1212. * fixed one remaining bug in scanner for line counts
  1213. * several little fixes
  1214. Revision 1.13 1998/04/29 13:42:27 peter
  1215. + $IOCHECKS and $ALIGN to test already, other will follow soon
  1216. * fixed the wrong linecounting with comments
  1217. Revision 1.12 1998/04/29 10:34:04 pierre
  1218. + added some code for ansistring (not complete nor working yet)
  1219. * corrected operator overloading
  1220. * corrected nasm output
  1221. + started inline procedures
  1222. + added starstarn : use ** for exponentiation (^ gave problems)
  1223. + started UseTokenInfo cond to get accurate positions
  1224. Revision 1.11 1998/04/27 23:10:29 peter
  1225. + new scanner
  1226. * $makelib -> if smartlink
  1227. * small filename fixes pmodule.setfilename
  1228. * moved import from files.pas -> import.pas
  1229. }