scanner.pas 42 KB

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