scanner.pas 41 KB

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