scanner.pas 40 KB

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