scanner.pas 40 KB

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