scanner.pas 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  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. function get_current_col : longint;
  148. procedure get_cur_file_pos(var fileinfo : tfileposinfo);
  149. procedure set_cur_file_pos(const fileinfo : tfileposinfo);
  150. procedure InitScanner(const fn: string);
  151. procedure DoneScanner(testendif:boolean);
  152. { changes to keywords to be tp compatible }
  153. procedure change_to_tp_keywords;
  154. implementation
  155. uses
  156. dos,verbose,systems,symtable,switches;
  157. {*****************************************************************************
  158. TPreProcStack
  159. *****************************************************************************}
  160. constructor tpreprocstack.init(ifdef,a:boolean;n:ppreprocstack);
  161. begin
  162. isifdef:=ifdef;
  163. accept:=a;
  164. next:=n;
  165. end;
  166. destructor tpreprocstack.done;
  167. begin
  168. end;
  169. procedure popstack;
  170. var
  171. hp : ppreprocstack;
  172. begin
  173. hp:=preprocstack^.next;
  174. dispose(preprocstack,done);
  175. preprocstack:=hp;
  176. end;
  177. {*****************************************************************************
  178. Helper routines
  179. *****************************************************************************}
  180. function is_keyword(var token : ttoken) : boolean;
  181. var
  182. high,low,mid : longint;
  183. begin
  184. low:=1;
  185. high:=anz_keywords;
  186. while low<high do
  187. begin
  188. mid:=(high+low+1) shr 1;
  189. if pattern<keyword[mid] then
  190. high:=mid-1
  191. else
  192. low:=mid;
  193. end;
  194. if pattern=keyword[high] then
  195. begin
  196. token:=keyword_token[high];
  197. is_keyword:=true;
  198. end
  199. else
  200. is_keyword:=false;
  201. end;
  202. procedure remove_keyword(const s : string);
  203. var
  204. i,j : longint;
  205. begin
  206. for i:=1 to anz_keywords do
  207. begin
  208. if keyword[i]=s then
  209. begin
  210. for j:=i to anz_keywords-1 do
  211. begin
  212. keyword[j]:=keyword[j+1];
  213. keyword_token[j]:=keyword_token[j+1];
  214. end;
  215. dec(anz_keywords);
  216. break;
  217. end;
  218. end;
  219. end;
  220. function get_current_col : longint;
  221. begin
  222. {$ifdef TP}
  223. if lastlinepos<=lasttokenpos then
  224. get_current_col:=longint(lasttokenpos)-longint(lastlinepos)
  225. else
  226. get_current_col:=longint(lastlinepos)-longint(lasttokenpos);
  227. {$else}
  228. get_current_col:=cardinal(lasttokenpos)-cardinal(lastlinepos);
  229. {$endif}
  230. end;
  231. procedure inc_comment_level;
  232. begin
  233. inc(comment_level);
  234. if (comment_level>1) then
  235. Message1(scan_w_comment_level,tostr(comment_level));
  236. end;
  237. procedure dec_comment_level;
  238. begin
  239. if cs_tp_compatible in aktswitches then
  240. comment_level:=0
  241. else
  242. dec(comment_level);
  243. end;
  244. procedure syntaxerror(const s : string);
  245. begin
  246. Message2(scan_f_syn_expected,tostr(get_current_col),s);
  247. end;
  248. {*****************************************************************************
  249. Scanner
  250. *****************************************************************************}
  251. procedure reload;
  252. var
  253. readsize : word;
  254. i,saveline : longint;
  255. begin
  256. if not assigned(current_module^.current_inputfile) then
  257. internalerror(14);
  258. if current_module^.current_inputfile^.filenotatend then
  259. begin
  260. { load the next piece of source }
  261. blockread(current_module^.current_inputfile^.f,inputbuffer^,
  262. current_module^.current_inputfile^.bufsize-1,readsize);
  263. { Scan the buffer for #0 chars, which are not alllowed }
  264. if readsize > 0 then
  265. begin
  266. { force proper line counting }
  267. saveline:=current_module^.current_inputfile^.line_no;
  268. i:=0;
  269. inputpointer:=inputbuffer;
  270. while i<readsize do
  271. begin
  272. c:=inputpointer^;
  273. case c of
  274. #0 : Message(scan_f_illegal_char);
  275. #10,#13 : begin
  276. if (byte(c)+byte(inputpointer[1])=23) then
  277. begin
  278. inc(longint(inputpointer));
  279. inc(i);
  280. end;
  281. inc(current_module^.current_inputfile^.line_no);
  282. end;
  283. end;
  284. inc(i);
  285. inc(longint(inputpointer));
  286. end;
  287. current_module^.current_inputfile^.line_no:=saveline;
  288. end;
  289. inputbuffer[readsize]:=#0;
  290. inputpointer:=inputbuffer;
  291. { Set EOF when main source and at endoffile }
  292. if eof(current_module^.current_inputfile^.f) then
  293. begin
  294. current_module^.current_inputfile^.filenotatend:=false;
  295. if current_module^.current_inputfile^.next=nil then
  296. inputbuffer[readsize]:=#26;
  297. end;
  298. end
  299. else
  300. begin
  301. current_module^.current_inputfile^.close;
  302. { load next module }
  303. current_module^.current_inputfile:=current_module^.current_inputfile^.next;
  304. current_module^.current_index:=current_module^.current_inputfile^.ref_index;
  305. status.currentsource:=current_module^.current_inputfile^.name^+current_module^.current_inputfile^.ext^;
  306. inputbuffer:=current_module^.current_inputfile^.buf;
  307. inputpointer:=inputbuffer+current_module^.current_inputfile^.bufpos;
  308. end;
  309. { load next char }
  310. c:=inputpointer^;
  311. inc(longint(inputpointer));
  312. end;
  313. procedure linebreak;
  314. var
  315. cur : char;
  316. begin
  317. if (byte(inputpointer^)=0) and
  318. current_module^.current_inputfile^.filenotatend then
  319. begin
  320. cur:=c;
  321. reload;
  322. if byte(cur)+byte(c)<>23 then
  323. dec(longint(inputpointer));
  324. end
  325. else
  326. begin
  327. { Fix linebreak to be only newline (=#10) for all types of linebreaks }
  328. if (byte(inputpointer^)+byte(c)=23) then
  329. inc(longint(inputpointer));
  330. end;
  331. c:=newline;
  332. { show status }
  333. Comment(V_Status,'');
  334. { increase line counters }
  335. inc(current_module^.current_inputfile^.line_no);
  336. status.currentline:=current_module^.current_inputfile^.line_no;
  337. inc(status.compiledlines);
  338. lastlinepos:=inputpointer;
  339. end;
  340. procedure readchar;
  341. begin
  342. c:=inputpointer^;
  343. if c=#0 then
  344. reload
  345. else
  346. inc(longint(inputpointer));
  347. if c in [#10,#13] then
  348. linebreak;
  349. end;
  350. function readstring:string;
  351. var
  352. i : longint;
  353. begin
  354. i:=0;
  355. { 'in []' splitted, so it will be CMP's and no SET_IN_BYTE (PFV) }
  356. while (c in ['A'..'Z','a'..'z']) or (c in ['0'..'9','_']) do
  357. begin
  358. if i<255 then
  359. begin
  360. inc(i);
  361. readstring[i]:=c;
  362. end;
  363. { get next char }
  364. c:=inputpointer^;
  365. if c=#0 then
  366. reload
  367. else
  368. inc(longint(inputpointer));
  369. end;
  370. { was the next char a linebreak ? }
  371. if c in [#10,#13] then
  372. linebreak;
  373. readstring[0]:=chr(i);
  374. end;
  375. function readid:string;
  376. begin
  377. readid:=upper(readstring);
  378. end;
  379. function readnumber:string;
  380. var
  381. base,
  382. i : longint;
  383. begin
  384. case c of
  385. '%' : begin
  386. readchar;
  387. base:=2;
  388. readnumber[1]:='%';
  389. i:=1;
  390. end;
  391. '$' : begin
  392. readchar;
  393. base:=16;
  394. readnumber[1]:='$';
  395. i:=1;
  396. end;
  397. else
  398. begin
  399. base:=10;
  400. i:=0;
  401. end;
  402. end;
  403. while ((base>=10) and (c in ['0'..'9'])) or
  404. ((base=16) and (c in ['A'..'F','a'..'f'])) or
  405. ((base=2) and (c in ['0'..'1'])) do
  406. begin
  407. if i<255 then
  408. begin
  409. inc(i);
  410. readnumber[i]:=c;
  411. end;
  412. { get next char }
  413. c:=inputpointer^;
  414. if c=#0 then
  415. reload
  416. else
  417. inc(longint(inputpointer));
  418. end;
  419. { was the next char a linebreak ? }
  420. if c in [#10,#13] then
  421. linebreak;
  422. readnumber[0]:=chr(i);
  423. end;
  424. function readval:longint;
  425. var
  426. l : longint;
  427. w : word;
  428. begin
  429. val(readnumber,l,w);
  430. readval:=l;
  431. end;
  432. function readcomment:string;
  433. var
  434. i : longint;
  435. begin
  436. i:=0;
  437. repeat
  438. case c of
  439. '}' : begin
  440. readchar;
  441. dec_comment_level;
  442. break;
  443. end;
  444. #26 : Message(scan_f_end_of_file);
  445. else
  446. begin
  447. if (i<255) then
  448. begin
  449. inc(i);
  450. readcomment[i]:=c;
  451. end;
  452. end;
  453. end;
  454. { readchar; }
  455. c:=inputpointer^;
  456. if c=#0 then
  457. reload
  458. else
  459. inc(longint(inputpointer));
  460. if c in [#10,#13] then
  461. linebreak;
  462. until false;
  463. readcomment[0]:=chr(i);
  464. end;
  465. procedure skipspace;
  466. begin
  467. while c in [' ',#9..#13] do
  468. begin
  469. { readchar; }
  470. c:=inputpointer^;
  471. if c=#0 then
  472. reload
  473. else
  474. inc(longint(inputpointer));
  475. if c in [#10,#13] then
  476. linebreak;
  477. end;
  478. end;
  479. procedure skipuntildirective;
  480. var
  481. found : longint;
  482. begin
  483. found:=0;
  484. repeat
  485. case c of
  486. #26 : Message(scan_f_end_of_file);
  487. '{' : begin
  488. if comment_level=0 then
  489. found:=1;
  490. inc_comment_level;
  491. end;
  492. '}' : begin
  493. dec_comment_level;
  494. found:=0;
  495. end;
  496. '$' : begin
  497. if found=1 then
  498. found:=2;
  499. end;
  500. else
  501. found:=0;
  502. end;
  503. { readchar;}
  504. c:=inputpointer^;
  505. if c=#0 then
  506. reload
  507. else
  508. inc(longint(inputpointer));
  509. if c in [#10,#13] then
  510. linebreak;
  511. until (found=2);
  512. end;
  513. {$i scandir.inc}
  514. procedure skipcomment;
  515. begin
  516. readchar;
  517. inc_comment_level;
  518. { handle compiler switches }
  519. if (c='$') then
  520. handledirectives;
  521. { handle_switches can dec comment_level, }
  522. while (comment_level>0) do
  523. begin
  524. case c of
  525. '{' : inc_comment_level;
  526. '}' : dec_comment_level;
  527. #26 : Message(scan_f_end_of_file);
  528. end;
  529. { readchar; }
  530. c:=inputpointer^;
  531. if c=#0 then
  532. reload
  533. else
  534. inc(longint(inputpointer));
  535. if c in [#10,#13] then
  536. linebreak;
  537. end;
  538. end;
  539. procedure skipdelphicomment;
  540. begin
  541. inc_comment_level;
  542. readchar;
  543. { this is currently not supported }
  544. if c='$' then
  545. Message(scan_e_wrong_styled_switch);
  546. { skip comment }
  547. while c<>newline do
  548. begin
  549. if c=#26 then
  550. Message(scan_f_end_of_file);
  551. readchar;
  552. end;
  553. dec_comment_level;
  554. end;
  555. procedure skipoldtpcomment;
  556. var
  557. found : longint;
  558. begin
  559. inc_comment_level;
  560. readchar;
  561. { this is currently not supported }
  562. if c='$' then
  563. Message(scan_e_wrong_styled_switch);
  564. { skip comment }
  565. while (comment_level>0) do
  566. begin
  567. found:=0;
  568. repeat
  569. case c of
  570. #26 : Message(scan_f_end_of_file);
  571. '*' : begin
  572. if found=3 then
  573. inc_comment_level
  574. else
  575. found:=1;
  576. end;
  577. ')' : begin
  578. if found=1 then
  579. begin
  580. dec_comment_level;
  581. if comment_level=0 then
  582. found:=2;
  583. end;
  584. end;
  585. '(' : found:=3;
  586. else
  587. found:=0;
  588. end;
  589. { readchar; }
  590. c:=inputpointer^;
  591. if c=#0 then
  592. reload
  593. else
  594. inc(longint(inputpointer));
  595. if c in [#10,#13] then
  596. linebreak;
  597. until (found=2);
  598. end;
  599. end;
  600. function yylex : ttoken;
  601. var
  602. y : ttoken;
  603. code : word;
  604. l : longint;
  605. mac : pmacrosym;
  606. hp : pinputfile;
  607. hp2 : pchar;
  608. label
  609. exit_label;
  610. begin
  611. { was the last character a point ? }
  612. { this code is needed because the scanner if there is a 1. found if }
  613. { this is a floating point number or range like 1..3 }
  614. if s_point then
  615. begin
  616. tokenpos.line:=current_module^.current_inputfile^.line_no;
  617. tokenpos.column:=get_current_col;
  618. tokenpos.fileindex:=current_module^.current_index;
  619. s_point:=false;
  620. if c='.' then
  621. begin
  622. readchar;
  623. yylex:=POINTPOINT;
  624. goto exit_label;
  625. end;
  626. yylex:=POINT;
  627. goto exit_label;
  628. end;
  629. { Skip all spaces and comments }
  630. repeat
  631. case c of
  632. '{' : skipcomment;
  633. ' ',#9..#13 : skipspace;
  634. else
  635. break;
  636. end;
  637. until false;
  638. { Save current token position }
  639. lasttokenpos:=inputpointer;
  640. tokenpos.line:=current_module^.current_inputfile^.line_no;
  641. tokenpos.column:=get_current_col;
  642. tokenpos.fileindex:=current_module^.current_index;
  643. { Check first for a identifier/keyword, this is 20+% faster (PFV) }
  644. if c in ['_','A'..'Z','a'..'z'] then
  645. begin
  646. orgpattern:=readstring;
  647. pattern:=upper(orgpattern);
  648. if (length(pattern) in [2..id_len]) and is_keyword(y) then
  649. yylex:=y
  650. else
  651. begin
  652. { this takes some time ... }
  653. if support_macros then
  654. begin
  655. mac:=pmacrosym(macros^.search(pattern));
  656. if assigned(mac) and (assigned(mac^.buftext)) then
  657. begin
  658. { don't forget the last char }
  659. dec(longint(inputpointer));
  660. current_module^.current_inputfile^.bufpos:=inputpointer-inputbuffer;
  661. { this isn't a proper way, but ... }
  662. hp:=new(pinputfile,init('','Macro '+pattern,''));
  663. hp^.next:=current_module^.current_inputfile;
  664. current_module^.current_inputfile:=hp;
  665. status.currentsource:=current_module^.current_inputfile^.name^;
  666. { I don't think that we should do that
  667. because otherwise the file will be searched !! (PM)
  668. but there is the problem of index !! }
  669. current_module^.sourcefiles.register_file(hp);
  670. current_module^.current_index:=hp^.ref_index;
  671. { set an own buffer }
  672. getmem(hp2,mac^.buflen+1);
  673. current_module^.current_inputfile^.setbuf(hp2,mac^.buflen+1);
  674. inputbuffer:=current_module^.current_inputfile^.buf;
  675. { copy text }
  676. move(mac^.buftext^,inputbuffer^,mac^.buflen);
  677. { put end sign }
  678. inputbuffer[mac^.buflen+1]:=#0;
  679. { load c }
  680. c:=inputbuffer^;
  681. inputpointer:=inputbuffer+1;
  682. { handle empty macros }
  683. if c=#0 then
  684. reload;
  685. { play it again ... }
  686. inc(yylexcount);
  687. if yylexcount>16 then
  688. Message(scan_w_macro_deep_ten);
  689. {$ifdef TP}
  690. yylex:=yylex;
  691. {$else}
  692. yylex:=yylex();
  693. {$endif}
  694. { that's all folks }
  695. dec(yylexcount);
  696. exit;
  697. end;
  698. end;
  699. yylex:=ID;
  700. end;
  701. goto exit_label;
  702. end
  703. else
  704. begin
  705. case c of
  706. '$' : begin
  707. pattern:=readnumber;
  708. yylex:=INTCONST;
  709. goto exit_label;
  710. end;
  711. '%' : begin
  712. pattern:=readnumber;
  713. yylex:=INTCONST;
  714. goto exit_label;
  715. end;
  716. '0'..'9' : begin
  717. pattern:=readnumber;
  718. case c of
  719. '.' : begin
  720. readchar;
  721. if not(c in ['0'..'9']) then
  722. begin
  723. s_point:=true;
  724. yylex:=INTCONST;
  725. goto exit_label;
  726. end;
  727. pattern:=pattern+'.';
  728. while c in ['0'..'9'] do
  729. begin
  730. pattern:=pattern+c;
  731. readchar;
  732. end;
  733. yylex:=REALNUMBER;
  734. goto exit_label;
  735. end;
  736. 'e','E' : begin
  737. pattern:=pattern+'E';
  738. readchar;
  739. if c in ['-','+'] then
  740. begin
  741. pattern:=pattern+c;
  742. readchar;
  743. end;
  744. if not(c in ['0'..'9']) then
  745. Message(scan_f_illegal_char);
  746. while c in ['0'..'9'] do
  747. begin
  748. pattern:=pattern+c;
  749. readchar;
  750. end;
  751. yylex:=REALNUMBER;
  752. goto exit_label;
  753. end;
  754. end;
  755. yylex:=INTCONST;
  756. goto exit_label;
  757. end;
  758. ';' : begin
  759. readchar;
  760. yylex:=SEMICOLON;
  761. goto exit_label;
  762. end;
  763. '[' : begin
  764. readchar;
  765. yylex:=LECKKLAMMER;
  766. goto exit_label;
  767. end;
  768. ']' : begin
  769. readchar;
  770. yylex:=RECKKLAMMER;
  771. goto exit_label;
  772. end;
  773. '(' : begin
  774. readchar;
  775. if c='*' then
  776. begin
  777. skipoldtpcomment;
  778. {$ifndef TP}
  779. yylex:=yylex();
  780. {$else TP}
  781. yylex:=yylex;
  782. {$endif TP}
  783. exit;
  784. end;
  785. yylex:=LKLAMMER;
  786. goto exit_label;
  787. end;
  788. ')' : begin
  789. readchar;
  790. yylex:=RKLAMMER;
  791. goto exit_label;
  792. end;
  793. '+' : begin
  794. readchar;
  795. if (c='=') and support_c_operators then
  796. begin
  797. readchar;
  798. yylex:=_PLUSASN;
  799. goto exit_label;
  800. end;
  801. yylex:=PLUS;
  802. goto exit_label;
  803. end;
  804. '-' : begin
  805. readchar;
  806. if (c='=') and support_c_operators then
  807. begin
  808. readchar;
  809. yylex:=_MINUSASN;
  810. goto exit_label;
  811. end;
  812. yylex:=MINUS;
  813. goto exit_label;
  814. end;
  815. ':' : begin
  816. readchar;
  817. if c='=' then
  818. begin
  819. readchar;
  820. yylex:=ASSIGNMENT;
  821. goto exit_label;
  822. end;
  823. yylex:=COLON;
  824. goto exit_label;
  825. end;
  826. '*' : begin
  827. readchar;
  828. if (c='=') and support_c_operators then
  829. begin
  830. readchar;
  831. yylex:=_STARASN;
  832. end else if c='*' then
  833. begin
  834. readchar;
  835. yylex:=STARSTAR;
  836. end
  837. else
  838. yylex:=STAR;
  839. goto exit_label;
  840. end;
  841. '/' : begin
  842. readchar;
  843. case c of
  844. '=' : begin
  845. if support_c_operators then
  846. begin
  847. readchar;
  848. yylex:=_SLASHASN;
  849. goto exit_label;
  850. end;
  851. end;
  852. '/' : begin
  853. skipdelphicomment;
  854. {$ifndef TP}
  855. yylex:=yylex();
  856. {$else TP}
  857. yylex:=yylex;
  858. {$endif TP}
  859. exit;
  860. end;
  861. end;
  862. yylex:=SLASH;
  863. goto exit_label;
  864. end;
  865. '=' : begin
  866. readchar;
  867. yylex:=EQUAL;
  868. goto exit_label;
  869. end;
  870. '.' : begin
  871. readchar;
  872. if c='.' then
  873. begin
  874. readchar;
  875. yylex:=POINTPOINT;
  876. goto exit_label;
  877. end
  878. else
  879. yylex:=POINT;
  880. goto exit_label;
  881. end;
  882. '@' : begin
  883. readchar;
  884. if c='@' then
  885. begin
  886. readchar;
  887. yylex:=DOUBLEADDR;
  888. end
  889. else
  890. yylex:=KLAMMERAFFE;
  891. goto exit_label;
  892. end;
  893. ',' : begin
  894. readchar;
  895. yylex:=COMMA;
  896. goto exit_label;
  897. end;
  898. '''','#','^' : begin
  899. if c='^' then
  900. begin
  901. readchar;
  902. c:=upcase(c);
  903. if not(block_type=bt_type) and (c in ['A'..'Z']) then
  904. begin
  905. pattern:=chr(ord(c)-64);
  906. readchar;
  907. end
  908. else
  909. begin
  910. yylex:=CARET;
  911. goto exit_label;
  912. end;
  913. end
  914. else
  915. pattern:='';
  916. repeat
  917. case c of
  918. '#' : begin
  919. readchar; { read # }
  920. valint(readnumber,l,code);
  921. if (code<>0) or (l<0) or (l>255) then
  922. Message(scan_e_illegal_char_const);
  923. pattern:=pattern+chr(l);
  924. end;
  925. '''' : begin
  926. repeat
  927. readchar;
  928. case c of
  929. #26 : Message(scan_f_end_of_file);
  930. newline : Message(scan_f_string_exceeds_line);
  931. '''' : begin
  932. readchar;
  933. if c<>'''' then
  934. break;
  935. end;
  936. end;
  937. pattern:=pattern+c;
  938. until false;
  939. end;
  940. '^' : begin
  941. readchar;
  942. if c<#64 then
  943. c:=chr(ord(c)+64)
  944. else
  945. c:=chr(ord(c)-64);
  946. pattern:=pattern+c;
  947. readchar;
  948. end;
  949. else
  950. break;
  951. end;
  952. until false;
  953. { strings with length 1 become const chars }
  954. if length(pattern)=1 then
  955. yylex:=CCHAR
  956. else
  957. yylex:=CSTRING;
  958. goto exit_label;
  959. end;
  960. '>' : begin
  961. readchar;
  962. case c of
  963. '=' : begin
  964. readchar;
  965. yylex:=GTE;
  966. goto exit_label;
  967. end;
  968. '>' : begin
  969. readchar;
  970. yylex:=_SHR;
  971. goto exit_label;
  972. end;
  973. '<' : begin { >< is for a symetric diff for sets }
  974. readchar;
  975. yylex:=SYMDIF;
  976. goto exit_label;
  977. end;
  978. end;
  979. yylex:=GT;
  980. goto exit_label;
  981. end;
  982. '<' : begin
  983. readchar;
  984. case c of
  985. '>' : begin
  986. readchar;
  987. yylex:=UNEQUAL;
  988. goto exit_label;
  989. end;
  990. '=' : begin
  991. readchar;
  992. yylex:=LTE;
  993. goto exit_label;
  994. end;
  995. '<' : begin
  996. readchar;
  997. yylex:=_SHL;
  998. goto exit_label;
  999. end;
  1000. end;
  1001. yylex:=LT;
  1002. goto exit_label;
  1003. end;
  1004. #26 : begin
  1005. yylex:=_EOF;
  1006. goto exit_label;
  1007. end;
  1008. else
  1009. begin
  1010. Message(scan_f_illegal_char);
  1011. end;
  1012. end;
  1013. end;
  1014. exit_label:
  1015. end;
  1016. function asmgetchar : char;
  1017. begin
  1018. if lastasmgetchar<>#0 then
  1019. begin
  1020. c:=lastasmgetchar;
  1021. lastasmgetchar:=#0;
  1022. end
  1023. else
  1024. readchar;
  1025. with tokenpos do
  1026. begin
  1027. line:=current_module^.current_inputfile^.line_no;
  1028. column:=get_current_col;
  1029. fileindex:=current_module^.current_index;
  1030. end;
  1031. case c of
  1032. '{' : begin
  1033. skipcomment;
  1034. lastasmgetchar:=c;
  1035. asmgetchar:=';';
  1036. exit;
  1037. end;
  1038. '/' : begin
  1039. readchar;
  1040. if c='/' then
  1041. begin
  1042. skipdelphicomment;
  1043. asmgetchar:=';';
  1044. end
  1045. else
  1046. asmgetchar:='/';
  1047. lastasmgetchar:=c;
  1048. exit;
  1049. end;
  1050. '(' : begin
  1051. readchar;
  1052. if c='*' then
  1053. begin
  1054. skipoldtpcomment;
  1055. asmgetchar:=';';
  1056. end
  1057. else
  1058. asmgetchar:='(';
  1059. lastasmgetchar:=c;
  1060. exit;
  1061. end;
  1062. else
  1063. begin
  1064. asmgetchar:=c;
  1065. end;
  1066. end;
  1067. end;
  1068. procedure InitScanner(const fn: string);
  1069. var
  1070. d:dirstr;
  1071. n:namestr;
  1072. e:extstr;
  1073. begin
  1074. fsplit(fn,d,n,e);
  1075. current_module^.current_inputfile:=new(pinputfile,init(d,n,e));
  1076. current_module^.current_inputfile^.reset;
  1077. current_module^.sourcefiles.register_file(current_module^.current_inputfile);
  1078. current_module^.current_index:=current_module^.current_inputfile^.ref_index;
  1079. status.currentsource:=current_module^.current_inputfile^.name^+current_module^.current_inputfile^.ext^;
  1080. if ioresult<>0 then
  1081. Message(scan_f_cannot_open_input);
  1082. inputbuffer:=current_module^.current_inputfile^.buf;
  1083. reload;
  1084. preprocstack:=nil;
  1085. comment_level:=0;
  1086. lasttokenpos:=inputpointer;
  1087. lastlinepos:=inputpointer;
  1088. s_point:=false;
  1089. block_type:=bt_general;
  1090. end;
  1091. procedure get_cur_file_pos(var fileinfo : tfileposinfo);
  1092. begin
  1093. with fileinfo do
  1094. begin
  1095. line:=current_module^.current_inputfile^.line_no;
  1096. {fileinfo.fileindex:=current_module^.current_inputfile^.ref_index;}
  1097. { should allways be the same !! }
  1098. fileindex:=current_module^.current_index;
  1099. column:=get_current_col;
  1100. end;
  1101. end;
  1102. procedure set_cur_file_pos(const fileinfo : tfileposinfo);
  1103. begin
  1104. current_module^.current_index:=fileinfo.fileindex;
  1105. current_module^.current_inputfile:=
  1106. pinputfile(current_module^.sourcefiles.get_file(fileinfo.fileindex));
  1107. if assigned(current_module^.current_inputfile) then
  1108. current_module^.current_inputfile^.line_no:=fileinfo.line;
  1109. {fileinfo.fileindex:=current_module^.current_inputfile^.ref_index;}
  1110. { should allways be the same !! }
  1111. { fileinfo.column:=get_current_col; }
  1112. end;
  1113. procedure DoneScanner(testendif:boolean);
  1114. var
  1115. st : string[16];
  1116. begin
  1117. if (not testendif) then
  1118. begin
  1119. while assigned(preprocstack) do
  1120. begin
  1121. if preprocstack^.isifdef then
  1122. st:='$IF(N)(DEF)'
  1123. else
  1124. st:='$ELSE';
  1125. Message3(scan_e_endif_expected,st,preprocstack^.name,tostr(preprocstack^.line_nb));
  1126. popstack;
  1127. end;
  1128. end;
  1129. end;
  1130. procedure change_to_tp_keywords;
  1131. const
  1132. non_tp : array[0..13] of string[id_len] = (
  1133. 'AS','CLASS','EXCEPT','FINALLY','INITIALIZATION','IS',
  1134. 'ON','OPERATOR','OTHERWISE','PROPERTY','RAISE','TRY',
  1135. 'EXPORTS','LIBRARY');
  1136. var
  1137. i : longint;
  1138. begin
  1139. for i:=0 to 13 do
  1140. remove_keyword(non_tp[i]);
  1141. end;
  1142. procedure change_to_delphi_keywords;
  1143. {
  1144. const
  1145. non_tp : array[0..13] of string[id_len] = (
  1146. 'AS','CLASS','EXCEPT','FINALLY','INITIALIZATION','IS',
  1147. 'ON','OPERATOR','OTHERWISE','PROPERTY','RAISE','TRY',
  1148. 'EXPORTS','LIBRARY');
  1149. var
  1150. i : longint;
  1151. }
  1152. begin
  1153. {
  1154. for i:=0 to 13 do
  1155. remove_keyword(non_tp[i]);
  1156. }
  1157. end;
  1158. end.
  1159. {
  1160. $Log$
  1161. Revision 1.23 1998-06-03 22:49:02 peter
  1162. + wordbool,longbool
  1163. * rename bis,von -> high,low
  1164. * moved some systemunit loading/creating to psystem.pas
  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. }