scanner.pas 42 KB

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