scanner.pas 105 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements the scanner part and handling of the switches
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit scanner;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,
  22. globtype,globals,version,tokens,
  23. verbose,comphook,
  24. finput,
  25. widestr;
  26. const
  27. max_include_nesting=32;
  28. max_macro_nesting=16;
  29. preprocbufsize=32*1024;
  30. type
  31. tcommentstyle = (comment_none,comment_tp,comment_oldtp,comment_delphi,comment_c);
  32. tscannerfile = class;
  33. preproctyp = (pp_ifdef,pp_ifndef,pp_if,pp_ifopt,pp_else,pp_elseif);
  34. tpreprocstack = class
  35. typ : preproctyp;
  36. accept : boolean;
  37. next : tpreprocstack;
  38. name : stringid;
  39. line_nb : longint;
  40. owner : tscannerfile;
  41. constructor Create(atyp:preproctyp;a:boolean;n:tpreprocstack);
  42. end;
  43. tdirectiveproc=procedure;
  44. tdirectiveitem = class(TNamedIndexItem)
  45. public
  46. is_conditional : boolean;
  47. proc : tdirectiveproc;
  48. constructor Create(const n:string;p:tdirectiveproc);
  49. constructor CreateCond(const n:string;p:tdirectiveproc);
  50. end;
  51. tscannerfile = class
  52. public
  53. inputfile : tinputfile; { current inputfile list }
  54. inputfilecount : longint;
  55. inputbuffer, { input buffer }
  56. inputpointer : pchar;
  57. inputstart : longint;
  58. line_no, { line }
  59. lastlinepos : longint;
  60. lasttokenpos : longint; { token }
  61. lasttoken,
  62. nexttoken : ttoken;
  63. comment_level,
  64. yylexcount : longint;
  65. lastasmgetchar : char;
  66. ignoredirectives : tstringlist; { ignore directives, used to give warnings only once }
  67. preprocstack : tpreprocstack;
  68. in_asm_string : boolean;
  69. preproc_pattern : string;
  70. preproc_token : ttoken;
  71. constructor Create(const fn:string);
  72. destructor Destroy;override;
  73. { File buffer things }
  74. function openinputfile:boolean;
  75. procedure closeinputfile;
  76. function tempopeninputfile:boolean;
  77. procedure tempcloseinputfile;
  78. procedure saveinputfile;
  79. procedure restoreinputfile;
  80. procedure firstfile;
  81. procedure nextfile;
  82. procedure addfile(hp:tinputfile);
  83. procedure reload;
  84. procedure insertmacro(const macname:string;p:pchar;len,line,fileindex:longint);
  85. { Scanner things }
  86. procedure gettokenpos;
  87. procedure inc_comment_level;
  88. procedure dec_comment_level;
  89. procedure illegal_char(c:char);
  90. procedure end_of_file;
  91. procedure checkpreprocstack;
  92. procedure poppreprocstack;
  93. procedure addpreprocstack(atyp : preproctyp;a:boolean;const s:string;w:longint);
  94. procedure elsepreprocstack;
  95. procedure elseifpreprocstack(accept:boolean);
  96. procedure handleconditional(p:tdirectiveitem);
  97. procedure handledirectives;
  98. procedure linebreak;
  99. procedure readchar;
  100. procedure readstring;
  101. procedure readnumber;
  102. function readid:string;
  103. function readval:longint;
  104. function readval_asstring:string;
  105. function readcomment:string;
  106. function readquotedstring:string;
  107. function readstate:char;
  108. function readstatedefault:char;
  109. procedure skipspace;
  110. procedure skipuntildirective;
  111. procedure skipcomment;
  112. procedure skipdelphicomment;
  113. procedure skipoldtpcomment;
  114. procedure readtoken;
  115. function readpreproc:ttoken;
  116. function asmgetcharstart : char;
  117. function asmgetchar:char;
  118. end;
  119. {$ifdef PREPROCWRITE}
  120. tpreprocfile=class
  121. f : text;
  122. buf : pointer;
  123. spacefound,
  124. eolfound : boolean;
  125. constructor create(const fn:string);
  126. destructor destroy;
  127. procedure Add(const s:string);
  128. procedure AddSpace;
  129. end;
  130. {$endif PREPROCWRITE}
  131. var
  132. { read strings }
  133. c : char;
  134. orgpattern,
  135. pattern : string;
  136. patternw : pcompilerwidestring;
  137. { token }
  138. token, { current token being parsed }
  139. idtoken : ttoken; { holds the token if the pattern is a known word }
  140. current_scanner : tscannerfile; { current scanner in use }
  141. aktcommentstyle : tcommentstyle; { needed to use read_comment from directives }
  142. {$ifdef PREPROCWRITE}
  143. preprocfile : tpreprocfile; { used with only preprocessing }
  144. {$endif PREPROCWRITE}
  145. type
  146. tdirectivemode = (directive_all, directive_turbo, directive_mac);
  147. procedure AddDirective(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  148. procedure AddConditional(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  149. procedure InitScanner;
  150. procedure DoneScanner;
  151. {To be called when the language mode is finally determined}
  152. procedure ConsolidateMode;
  153. implementation
  154. uses
  155. dos,
  156. cutils,
  157. systems,
  158. switches,
  159. symbase,symtable,symtype,symsym,symconst,symdef,defutil,
  160. fmodule;
  161. var
  162. { dictionaries with the supported directives }
  163. turbo_scannerdirectives : tdictionary; { for other modes }
  164. mac_scannerdirectives : tdictionary; { for mode mac }
  165. {*****************************************************************************
  166. Helper routines
  167. *****************************************************************************}
  168. const
  169. { use any special name that is an invalid file name to avoid problems }
  170. preprocstring : array [preproctyp] of string[7]
  171. = ('$IFDEF','$IFNDEF','$IF','$IFOPT','$ELSE','$ELSEIF');
  172. function is_keyword(const s:string):boolean;
  173. var
  174. low,high,mid : longint;
  175. begin
  176. if not (length(s) in [tokenlenmin..tokenlenmax]) or
  177. not (s[1] in ['a'..'z','A'..'Z']) then
  178. begin
  179. is_keyword:=false;
  180. exit;
  181. end;
  182. low:=ord(tokenidx^[length(s),s[1]].first);
  183. high:=ord(tokenidx^[length(s),s[1]].last);
  184. while low<high do
  185. begin
  186. mid:=(high+low+1) shr 1;
  187. if pattern<tokeninfo^[ttoken(mid)].str then
  188. high:=mid-1
  189. else
  190. low:=mid;
  191. end;
  192. is_keyword:=(pattern=tokeninfo^[ttoken(high)].str) and
  193. (tokeninfo^[ttoken(high)].keyword in aktmodeswitches);
  194. end;
  195. {To be called when the language mode is finally determined}
  196. procedure ConsolidateMode;
  197. begin
  198. if m_mac in aktmodeswitches then
  199. if current_module.is_unit and not assigned(current_module.globalmacrosymtable) then
  200. begin
  201. current_module.globalmacrosymtable:= tmacrosymtable.create(true);
  202. current_module.globalmacrosymtable.next:= current_module.localmacrosymtable;
  203. macrosymtablestack:=current_module.globalmacrosymtable;
  204. end;
  205. { define a symbol in delphi,objfpc,tp,gpc,macpas mode }
  206. if (m_delphi in aktmodeswitches) then
  207. def_system_macro('FPC_DELPHI')
  208. else if (m_tp7 in aktmodeswitches) then
  209. def_system_macro('FPC_TP')
  210. else if (m_objfpc in aktmodeswitches) then
  211. def_system_macro('FPC_OBJFPC')
  212. else if (m_gpc in aktmodeswitches) then
  213. def_system_macro('FPC_GPC')
  214. else if (m_mac in aktmodeswitches) then
  215. def_system_macro('FPC_MACPAS');
  216. end;
  217. {*****************************************************************************
  218. Conditional Directives
  219. *****************************************************************************}
  220. procedure dir_else;
  221. begin
  222. current_scanner.elsepreprocstack;
  223. end;
  224. procedure dir_endif;
  225. begin
  226. current_scanner.poppreprocstack;
  227. end;
  228. procedure dir_ifdef;
  229. var
  230. hs : string;
  231. mac : tmacro;
  232. begin
  233. current_scanner.skipspace;
  234. hs:=current_scanner.readid;
  235. if hs='' then
  236. Message(scan_e_error_in_preproc_expr);
  237. mac:=tmacro(search_macro(hs));
  238. if assigned(mac) then
  239. mac.is_used:=true;
  240. current_scanner.addpreprocstack(pp_ifdef,assigned(mac) and mac.defined,hs,scan_c_ifdef_found);
  241. end;
  242. procedure dir_ifndef;
  243. var
  244. hs : string;
  245. mac : tmacro;
  246. begin
  247. current_scanner.skipspace;
  248. hs:=current_scanner.readid;
  249. if hs='' then
  250. Message(scan_e_error_in_preproc_expr);
  251. mac:=tmacro(search_macro(hs));
  252. if assigned(mac) then
  253. mac.is_used:=true;
  254. current_scanner.addpreprocstack(pp_ifndef,not(assigned(mac) and mac.defined),hs,scan_c_ifndef_found);
  255. end;
  256. procedure dir_ifopt;
  257. var
  258. hs : string;
  259. found : boolean;
  260. state : char;
  261. begin
  262. found:= false;
  263. current_scanner.skipspace;
  264. hs:=current_scanner.readid;
  265. if (length(hs)>1) then
  266. Message1(scan_w_illegal_switch,hs)
  267. else
  268. begin
  269. state:=current_scanner.ReadState;
  270. if state in ['-','+'] then
  271. found:=CheckSwitch(hs[1],state);
  272. end;
  273. current_scanner.addpreprocstack(pp_ifopt,found,hs,scan_c_ifopt_found);
  274. end;
  275. procedure dir_libprefix;
  276. var
  277. s : string;
  278. begin
  279. current_scanner.skipspace;
  280. if c <> '''' then
  281. Message2(scan_f_syn_expected, '''', c);
  282. s := current_scanner.readquotedstring;
  283. stringdispose(outputprefix);
  284. outputprefix := stringdup(s);
  285. with current_module do
  286. setfilename(paramfn^, paramallowoutput);
  287. end;
  288. procedure dir_libsuffix;
  289. var
  290. s : string;
  291. begin
  292. current_scanner.skipspace;
  293. if c <> '''' then
  294. Message2(scan_f_syn_expected, '''', c);
  295. s := current_scanner.readquotedstring;
  296. stringdispose(outputsuffix);
  297. outputsuffix := stringdup(s);
  298. with current_module do
  299. setfilename(paramfn^, paramallowoutput);
  300. end;
  301. procedure dir_extension;
  302. var
  303. s : string;
  304. begin
  305. current_scanner.skipspace;
  306. if c <> '''' then
  307. Message2(scan_f_syn_expected, '''', c);
  308. s := current_scanner.readquotedstring;
  309. outputextension := '.'+s;
  310. with current_module do
  311. setfilename(paramfn^, paramallowoutput);
  312. end;
  313. function parse_compiler_expr:string;
  314. function read_expr : string; forward;
  315. procedure preproc_consume(t : ttoken);
  316. begin
  317. if t<>current_scanner.preproc_token then
  318. Message(scan_e_preproc_syntax_error);
  319. current_scanner.preproc_token:=current_scanner.readpreproc;
  320. end;
  321. function preproc_substitutedtoken: string;
  322. var
  323. hs: string;
  324. mac : tmacro;
  325. macrocount,
  326. len : integer;
  327. begin
  328. result := current_scanner.preproc_pattern;
  329. { Substitue macros and compiler variables with their content/value.
  330. For real macros also do recursive substitution. }
  331. macrocount:=0;
  332. repeat
  333. mac:=tmacro(search_macro(result));
  334. inc(macrocount);
  335. if macrocount>max_macro_nesting then
  336. begin
  337. Message(scan_w_macro_too_deep);
  338. break;
  339. end;
  340. if assigned(mac) and mac.defined then
  341. if assigned(mac.buftext) then
  342. begin
  343. if mac.buflen>255 then
  344. begin
  345. len:=255;
  346. Message(scan_w_macro_cut_after_255_chars);
  347. end
  348. else
  349. len:=mac.buflen;
  350. hs[0]:=char(len);
  351. move(mac.buftext^,hs[1],len);
  352. result:=upcase(hs);
  353. mac.is_used:=true;
  354. end
  355. else
  356. begin
  357. Message1(scan_e_error_macro_lacks_value, result);
  358. break;
  359. end
  360. else
  361. begin
  362. (*
  363. // To make this work, there must be some kind of type checking here...
  364. if m_mac in aktmodeswitches then
  365. Message1(scan_e_error_macro_undefined, result)
  366. else
  367. *)
  368. break;
  369. end;
  370. if mac.is_compiler_var then
  371. break;
  372. until false;
  373. end;
  374. function read_factor : string;
  375. var
  376. hs : string;
  377. mac: tmacro;
  378. srsym : tsym;
  379. srsymtable : tsymtable;
  380. l : longint;
  381. w : integer;
  382. hasKlammer: Boolean;
  383. begin
  384. if current_scanner.preproc_token=_ID then
  385. begin
  386. if current_scanner.preproc_pattern='DEFINED' then
  387. begin
  388. preproc_consume(_ID);
  389. current_scanner.skipspace;
  390. if current_scanner.preproc_token =_LKLAMMER then
  391. begin
  392. preproc_consume(_LKLAMMER);
  393. current_scanner.skipspace;
  394. hasKlammer:= true;
  395. end
  396. else if (m_mac in aktmodeswitches) then
  397. hasKlammer:= false
  398. else
  399. Message(scan_e_error_in_preproc_expr);
  400. if current_scanner.preproc_token =_ID then
  401. begin
  402. hs := current_scanner.preproc_pattern;
  403. mac := tmacro(search_macro(hs));
  404. if assigned(mac) and mac.defined then
  405. begin
  406. hs := '1';
  407. mac.is_used:=true;
  408. end
  409. else
  410. hs := '0';
  411. read_factor := hs;
  412. preproc_consume(_ID);
  413. current_scanner.skipspace;
  414. end
  415. else
  416. Message(scan_e_error_in_preproc_expr);
  417. if hasKlammer then
  418. if current_scanner.preproc_token =_RKLAMMER then
  419. preproc_consume(_RKLAMMER)
  420. else
  421. Message(scan_e_error_in_preproc_expr);
  422. end
  423. else
  424. if (m_mac in aktmodeswitches) and (current_scanner.preproc_pattern='UNDEFINED') then
  425. begin
  426. preproc_consume(_ID);
  427. current_scanner.skipspace;
  428. if current_scanner.preproc_token =_ID then
  429. begin
  430. hs := current_scanner.preproc_pattern;
  431. mac := tmacro(search_macro(hs));
  432. if assigned(mac) then
  433. begin
  434. hs := '0';
  435. mac.is_used:=true;
  436. end
  437. else
  438. hs := '1';
  439. read_factor := hs;
  440. preproc_consume(_ID);
  441. current_scanner.skipspace;
  442. end
  443. else
  444. Message(scan_e_error_in_preproc_expr);
  445. end
  446. else
  447. if (m_mac in aktmodeswitches) and (current_scanner.preproc_pattern='OPTION') then
  448. begin
  449. preproc_consume(_ID);
  450. current_scanner.skipspace;
  451. if current_scanner.preproc_token =_LKLAMMER then
  452. begin
  453. preproc_consume(_LKLAMMER);
  454. current_scanner.skipspace;
  455. end
  456. else
  457. Message(scan_e_error_in_preproc_expr);
  458. if not (current_scanner.preproc_token = _ID) then
  459. Message(scan_e_error_in_preproc_expr);
  460. hs:=current_scanner.preproc_pattern;
  461. if (length(hs) > 1) then
  462. {This is allowed in Metrowerks Pascal}
  463. Message(scan_e_error_in_preproc_expr)
  464. else
  465. begin
  466. if CheckSwitch(hs[1],'+') then
  467. read_factor := '1'
  468. else
  469. read_factor := '0';
  470. end;
  471. preproc_consume(_ID);
  472. current_scanner.skipspace;
  473. if current_scanner.preproc_token =_RKLAMMER then
  474. preproc_consume(_RKLAMMER)
  475. else
  476. Message(scan_e_error_in_preproc_expr);
  477. end
  478. else
  479. if current_scanner.preproc_pattern='SIZEOF' then
  480. begin
  481. preproc_consume(_ID);
  482. current_scanner.skipspace;
  483. if current_scanner.preproc_token =_LKLAMMER then
  484. begin
  485. preproc_consume(_LKLAMMER);
  486. current_scanner.skipspace;
  487. end
  488. else
  489. Message(scan_e_error_in_preproc_expr);
  490. if searchsym(current_scanner.preproc_pattern,srsym,srsymtable) then
  491. begin
  492. l:=0;
  493. case srsym.typ of
  494. globalvarsym,
  495. localvarsym,
  496. paravarsym :
  497. l:=tabstractvarsym(srsym).getsize;
  498. typedconstsym :
  499. l:=ttypedconstsym(srsym).getsize;
  500. typesym:
  501. l:=ttypesym(srsym).restype.def.size;
  502. else
  503. Message(scan_e_error_in_preproc_expr);
  504. end;
  505. str(l,read_factor);
  506. preproc_consume(_ID);
  507. current_scanner.skipspace;
  508. end
  509. else
  510. Message1(sym_e_id_not_found,current_scanner.preproc_pattern);
  511. if current_scanner.preproc_token =_RKLAMMER then
  512. preproc_consume(_RKLAMMER)
  513. else
  514. Message(scan_e_error_in_preproc_expr);
  515. end
  516. else
  517. if current_scanner.preproc_pattern='DECLARED' then
  518. begin
  519. preproc_consume(_ID);
  520. current_scanner.skipspace;
  521. if current_scanner.preproc_token =_LKLAMMER then
  522. begin
  523. preproc_consume(_LKLAMMER);
  524. current_scanner.skipspace;
  525. end
  526. else
  527. Message(scan_e_error_in_preproc_expr);
  528. if current_scanner.preproc_token =_ID then
  529. begin
  530. hs := upper(current_scanner.preproc_pattern);
  531. if searchsym(hs,srsym,srsymtable) then
  532. hs := '1'
  533. else
  534. hs := '0';
  535. read_factor := hs;
  536. preproc_consume(_ID);
  537. current_scanner.skipspace;
  538. end
  539. else
  540. Message(scan_e_error_in_preproc_expr);
  541. if current_scanner.preproc_token =_RKLAMMER then
  542. preproc_consume(_RKLAMMER)
  543. else
  544. Message(scan_e_error_in_preproc_expr);
  545. end
  546. else
  547. if current_scanner.preproc_pattern='NOT' then
  548. begin
  549. preproc_consume(_ID);
  550. hs:=read_factor();
  551. val(hs,l,w);
  552. if l<>0 then
  553. read_factor:='0'
  554. else
  555. read_factor:='1';
  556. end
  557. else
  558. if (m_mac in aktmodeswitches) and (current_scanner.preproc_pattern='TRUE') then
  559. begin
  560. preproc_consume(_ID);
  561. read_factor:='1';
  562. end
  563. else
  564. if (m_mac in aktmodeswitches) and (current_scanner.preproc_pattern='FALSE') then
  565. begin
  566. preproc_consume(_ID);
  567. read_factor:='0';
  568. end
  569. else
  570. begin
  571. hs:=preproc_substitutedtoken;
  572. { Default is to return the original symbol }
  573. read_factor:=hs;
  574. if (m_delphi in aktmodeswitches) then
  575. if searchsym(current_scanner.preproc_pattern,srsym,srsymtable) then
  576. begin
  577. case srsym.typ of
  578. constsym :
  579. begin
  580. with tconstsym(srsym) do
  581. begin
  582. case consttyp of
  583. constord :
  584. begin
  585. case consttype.def.deftype of
  586. orddef:
  587. begin
  588. if is_integer(consttype.def) or is_boolean(consttype.def) then
  589. read_factor:=tostr(value.valueord)
  590. else
  591. if is_char(consttype.def) then
  592. read_factor:=chr(value.valueord);
  593. end;
  594. enumdef:
  595. read_factor:=tostr(value.valueord)
  596. end;
  597. end;
  598. conststring :
  599. read_factor := upper(pchar(value.valueptr));
  600. constset :
  601. begin
  602. hs:='';
  603. for l:=0 to 255 do
  604. if l in pconstset(tconstsym(srsym).value.valueptr)^ then
  605. hs:=hs+','+tostr(l);
  606. if hs<>'' then
  607. hs:=hs+',';
  608. read_factor := hs;
  609. end;
  610. end;
  611. end;
  612. end;
  613. enumsym :
  614. read_factor:=tostr(tenumsym(srsym).value);
  615. end;
  616. end;
  617. preproc_consume(_ID);
  618. current_scanner.skipspace;
  619. end
  620. end
  621. else if current_scanner.preproc_token =_LKLAMMER then
  622. begin
  623. preproc_consume(_LKLAMMER);
  624. read_factor:=read_expr;
  625. preproc_consume(_RKLAMMER);
  626. end
  627. else if current_scanner.preproc_token = _LECKKLAMMER then
  628. begin
  629. preproc_consume(_LECKKLAMMER);
  630. read_factor := ',';
  631. while current_scanner.preproc_token = _ID do
  632. begin
  633. read_factor := read_factor+read_factor()+',';
  634. if current_scanner.preproc_token = _COMMA then
  635. preproc_consume(_COMMA);
  636. end;
  637. preproc_consume(_RECKKLAMMER);
  638. end
  639. else
  640. Message(scan_e_error_in_preproc_expr);
  641. end;
  642. function read_term : string;
  643. var
  644. hs1,hs2 : string;
  645. l1,l2 : longint;
  646. w : integer;
  647. begin
  648. hs1:=read_factor;
  649. repeat
  650. if (current_scanner.preproc_token<>_ID) then
  651. break;
  652. if current_scanner.preproc_pattern<>'AND' then
  653. break;
  654. preproc_consume(_ID);
  655. hs2:=read_factor;
  656. val(hs1,l1,w);
  657. val(hs2,l2,w);
  658. if (l1<>0) and (l2<>0) then
  659. hs1:='1'
  660. else
  661. hs1:='0';
  662. until false;
  663. read_term:=hs1;
  664. end;
  665. function read_simple_expr : string;
  666. var
  667. hs1,hs2 : string;
  668. l1,l2 : longint;
  669. w : integer;
  670. begin
  671. hs1:=read_term;
  672. repeat
  673. if (current_scanner.preproc_token<>_ID) then
  674. break;
  675. if current_scanner.preproc_pattern<>'OR' then
  676. break;
  677. preproc_consume(_ID);
  678. hs2:=read_term;
  679. val(hs1,l1,w);
  680. val(hs2,l2,w);
  681. if (l1<>0) or (l2<>0) then
  682. hs1:='1'
  683. else
  684. hs1:='0';
  685. until false;
  686. read_simple_expr:=hs1;
  687. end;
  688. function read_expr : string;
  689. var
  690. hs1,hs2 : string;
  691. b : boolean;
  692. t : ttoken;
  693. w : integer;
  694. l1,l2 : longint;
  695. begin
  696. hs1:=read_simple_expr;
  697. t:=current_scanner.preproc_token;
  698. if (t = _ID) and (current_scanner.preproc_pattern = 'IN') then
  699. t := _IN;
  700. if not (t in [_IN,_EQUAL,_UNEQUAL,_LT,_GT,_LTE,_GTE]) then
  701. begin
  702. read_expr:=hs1;
  703. exit;
  704. end;
  705. if (t = _IN) then
  706. preproc_consume(_ID)
  707. else
  708. preproc_consume(t);
  709. hs2:=read_simple_expr;
  710. if is_number(hs1) and is_number(hs2) then
  711. begin
  712. val(hs1,l1,w);
  713. val(hs2,l2,w);
  714. case t of
  715. _IN : Message(scan_e_preproc_syntax_error);
  716. _EQUAL : b:=l1=l2;
  717. _UNEQUAL : b:=l1<>l2;
  718. _LT : b:=l1<l2;
  719. _GT : b:=l1>l2;
  720. _GTE : b:=l1>=l2;
  721. _LTE : b:=l1<=l2;
  722. end;
  723. end
  724. else
  725. begin
  726. case t of
  727. _IN : if hs2[1] = ',' then
  728. b:=pos(','+hs1+',', hs2) > 0
  729. else
  730. Message(scan_e_preproc_syntax_error);
  731. _EQUAL : b:=hs1=hs2;
  732. _UNEQUAL : b:=hs1<>hs2;
  733. _LT : b:=hs1<hs2;
  734. _GT : b:=hs1>hs2;
  735. _GTE : b:=hs1>=hs2;
  736. _LTE : b:=hs1<=hs2;
  737. end;
  738. end;
  739. if b then
  740. read_expr:='1'
  741. else
  742. read_expr:='0';
  743. end;
  744. begin
  745. current_scanner.skipspace;
  746. { start preproc expression scanner }
  747. current_scanner.preproc_token:=current_scanner.readpreproc;
  748. parse_compiler_expr:=read_expr;
  749. end;
  750. procedure dir_if;
  751. var
  752. hs : string;
  753. begin
  754. hs:=parse_compiler_expr;
  755. current_scanner.addpreprocstack(pp_if,hs<>'0',hs,scan_c_if_found);
  756. end;
  757. procedure dir_elseif;
  758. var
  759. hs : string;
  760. begin
  761. hs:=parse_compiler_expr;
  762. current_scanner.elseifpreprocstack(hs<>'0');
  763. end;
  764. procedure dir_define;
  765. var
  766. hs : string;
  767. bracketcount : longint;
  768. mac : tmacro;
  769. macropos : longint;
  770. macrobuffer : pmacrobuffer;
  771. begin
  772. current_scanner.skipspace;
  773. hs:=current_scanner.readid;
  774. mac:=tmacro(search_macro(hs));
  775. if not assigned(mac) or (mac.owner <> macrosymtablestack) then
  776. begin
  777. mac:=tmacro.create(hs);
  778. mac.defined:=true;
  779. Message1(parser_c_macro_defined,mac.name);
  780. macrosymtablestack.insert(mac);
  781. end
  782. else
  783. begin
  784. Message1(parser_c_macro_defined,mac.name);
  785. mac.defined:=true;
  786. mac.is_compiler_var:=false;
  787. { delete old definition }
  788. if assigned(mac.buftext) then
  789. begin
  790. freemem(mac.buftext,mac.buflen);
  791. mac.buftext:=nil;
  792. end;
  793. end;
  794. mac.is_used:=true;
  795. if (cs_support_macro in aktmoduleswitches) then
  796. begin
  797. { !!!!!! handle macro params, need we this? }
  798. current_scanner.skipspace;
  799. if not (m_mac in aktmodeswitches) then
  800. begin
  801. { may be a macro? }
  802. if c <> ':' then
  803. exit;
  804. current_scanner.readchar;
  805. if c <> '=' then
  806. exit;
  807. current_scanner.readchar;
  808. current_scanner.skipspace;
  809. end;
  810. { key words are never substituted }
  811. if is_keyword(hs) then
  812. Message(scan_e_keyword_cant_be_a_macro);
  813. new(macrobuffer);
  814. macropos:=0;
  815. { parse macro, brackets are counted so it's possible
  816. to have a $ifdef etc. in the macro }
  817. bracketcount:=0;
  818. repeat
  819. case c of
  820. '}' :
  821. if (bracketcount=0) then
  822. break
  823. else
  824. dec(bracketcount);
  825. '{' :
  826. inc(bracketcount);
  827. #10,#13 :
  828. current_scanner.linebreak;
  829. #26 :
  830. current_scanner.end_of_file;
  831. end;
  832. macrobuffer^[macropos]:=c;
  833. inc(macropos);
  834. if macropos>maxmacrolen then
  835. Message(scan_f_macro_buffer_overflow);
  836. current_scanner.readchar;
  837. until false;
  838. { free buffer of macro ?}
  839. if assigned(mac.buftext) then
  840. freemem(mac.buftext,mac.buflen);
  841. { get new mem }
  842. getmem(mac.buftext,macropos);
  843. mac.buflen:=macropos;
  844. { copy the text }
  845. move(macrobuffer^,mac.buftext^,macropos);
  846. dispose(macrobuffer);
  847. end
  848. else
  849. begin
  850. { check if there is an assignment, then we need to give a
  851. warning }
  852. current_scanner.skipspace;
  853. if c=':' then
  854. begin
  855. current_scanner.readchar;
  856. if c='=' then
  857. Message(scan_w_macro_support_turned_off);
  858. end;
  859. end;
  860. end;
  861. procedure dir_setc;
  862. var
  863. hs : string;
  864. mac : tmacro;
  865. begin
  866. current_scanner.skipspace;
  867. hs:=current_scanner.readid;
  868. mac:=tmacro(search_macro(hs));
  869. if not assigned(mac) or (mac.owner <> macrosymtablestack) then
  870. begin
  871. mac:=tmacro.create(hs);
  872. mac.defined:=true;
  873. mac.is_compiler_var:=true;
  874. Message1(parser_c_macro_defined,mac.name);
  875. macrosymtablestack.insert(mac);
  876. end
  877. else
  878. begin
  879. mac.defined:=true;
  880. mac.is_compiler_var:=true;
  881. { delete old definition }
  882. if assigned(mac.buftext) then
  883. begin
  884. freemem(mac.buftext,mac.buflen);
  885. mac.buftext:=nil;
  886. end;
  887. end;
  888. mac.is_used:=true;
  889. { key words are never substituted }
  890. if is_keyword(hs) then
  891. Message(scan_e_keyword_cant_be_a_macro);
  892. { !!!!!! handle macro params, need we this? }
  893. current_scanner.skipspace;
  894. { may be a macro? }
  895. { assignment can be both := and = }
  896. if c=':' then
  897. current_scanner.readchar;
  898. if c='=' then
  899. begin
  900. current_scanner.readchar;
  901. hs:= parse_compiler_expr;
  902. if length(hs) <> 0 then
  903. begin
  904. Message2(parser_c_macro_set_to,mac.name,hs);
  905. { free buffer of macro ?}
  906. if assigned(mac.buftext) then
  907. freemem(mac.buftext,mac.buflen);
  908. { get new mem }
  909. getmem(mac.buftext,length(hs));
  910. mac.buflen:=length(hs);
  911. { copy the text }
  912. move(hs[1],mac.buftext^,mac.buflen);
  913. end
  914. else
  915. Message(scan_e_preproc_syntax_error);
  916. end
  917. else
  918. Message(scan_e_preproc_syntax_error);
  919. end;
  920. procedure dir_undef;
  921. var
  922. hs : string;
  923. mac : tmacro;
  924. begin
  925. current_scanner.skipspace;
  926. hs:=current_scanner.readid;
  927. mac:=tmacro(search_macro(hs));
  928. if not assigned(mac) or (mac.owner <> macrosymtablestack) then
  929. begin
  930. mac:=tmacro.create(hs);
  931. Message1(parser_c_macro_undefined,mac.name);
  932. mac.defined:=false;
  933. macrosymtablestack.insert(mac);
  934. end
  935. else
  936. begin
  937. Message1(parser_c_macro_undefined,mac.name);
  938. mac.defined:=false;
  939. mac.is_compiler_var:=false;
  940. { delete old definition }
  941. if assigned(mac.buftext) then
  942. begin
  943. freemem(mac.buftext,mac.buflen);
  944. mac.buftext:=nil;
  945. end;
  946. end;
  947. mac.is_used:=true;
  948. end;
  949. procedure dir_include;
  950. function findincludefile(const path,name,ext:string;var foundfile:string):boolean;
  951. var
  952. found : boolean;
  953. hpath : string;
  954. begin
  955. { look for the include file
  956. 1. specified path,path of current inputfile,current dir
  957. 2. local includepath
  958. 3. global includepath }
  959. found:=false;
  960. foundfile:='';
  961. hpath:='';
  962. if path<>'' then
  963. begin
  964. if not path_absolute(path) then
  965. hpath:=current_scanner.inputfile.path^+path
  966. else
  967. hpath:=path+';'+current_scanner.inputfile.path^;
  968. end
  969. else
  970. hpath:=current_scanner.inputfile.path^;
  971. found:=FindFile(name+ext, hpath+';'+CurDirRelPath(source_info),foundfile);
  972. if (not found) then
  973. found:=current_module.localincludesearchpath.FindFile(name+ext,foundfile);
  974. if (not found) then
  975. found:=includesearchpath.FindFile(name+ext,foundfile);
  976. findincludefile:=found;
  977. end;
  978. var
  979. args,
  980. foundfile,
  981. hs : string;
  982. path : dirstr;
  983. name : namestr;
  984. ext : extstr;
  985. hp : tinputfile;
  986. found : boolean;
  987. begin
  988. current_scanner.skipspace;
  989. args:=current_scanner.readcomment;
  990. hs:=GetToken(args,' ');
  991. if hs='' then
  992. exit;
  993. if (hs[1]='%') then
  994. begin
  995. { case insensitive }
  996. hs:=upper(hs);
  997. { remove %'s }
  998. Delete(hs,1,1);
  999. if hs[length(hs)]='%' then
  1000. Delete(hs,length(hs),1);
  1001. { save old }
  1002. path:=hs;
  1003. { first check for internal macros }
  1004. if hs='TIME' then
  1005. hs:=gettimestr
  1006. else
  1007. if hs='DATE' then
  1008. hs:=getdatestr
  1009. else
  1010. if hs='FILE' then
  1011. hs:=current_module.sourcefiles.get_file_name(aktfilepos.fileindex)
  1012. else
  1013. if hs='LINE' then
  1014. hs:=tostr(aktfilepos.line)
  1015. else
  1016. if hs='FPCVERSION' then
  1017. hs:=version_string
  1018. else
  1019. if hs='FPCTARGET' then
  1020. hs:=target_cpu_string
  1021. else
  1022. if hs='FPCTARGETCPU' then
  1023. hs:=target_cpu_string
  1024. else
  1025. if hs='FPCTARGETOS' then
  1026. hs:=target_info.shortname
  1027. else
  1028. hs:=getenv(hs);
  1029. if hs='' then
  1030. Message1(scan_w_include_env_not_found,path);
  1031. { make it a stringconst }
  1032. hs:=''''+hs+'''';
  1033. current_scanner.insertmacro(path,@hs[1],length(hs),
  1034. current_scanner.line_no,current_scanner.inputfile.ref_index);
  1035. end
  1036. else
  1037. begin
  1038. hs:=FixFileName(hs);
  1039. fsplit(hs,path,name,ext);
  1040. { try to find the file }
  1041. found:=findincludefile(path,name,ext,foundfile);
  1042. if (ext='') then
  1043. begin
  1044. { try default extensions .inc , .pp and .pas }
  1045. if (not found) then
  1046. found:=findincludefile(path,name,'.inc',foundfile);
  1047. if (not found) then
  1048. found:=findincludefile(path,name,sourceext,foundfile);
  1049. if (not found) then
  1050. found:=findincludefile(path,name,pasext,foundfile);
  1051. end;
  1052. if current_scanner.inputfilecount<max_include_nesting then
  1053. begin
  1054. inc(current_scanner.inputfilecount);
  1055. { we need to reread the current char }
  1056. dec(current_scanner.inputpointer);
  1057. { shutdown current file }
  1058. current_scanner.tempcloseinputfile;
  1059. { load new file }
  1060. hp:=do_openinputfile(foundfile);
  1061. current_scanner.addfile(hp);
  1062. current_module.sourcefiles.register_file(hp);
  1063. if not current_scanner.openinputfile then
  1064. Message1(scan_f_cannot_open_includefile,hs);
  1065. Message1(scan_t_start_include_file,current_scanner.inputfile.path^+current_scanner.inputfile.name^);
  1066. current_scanner.reload;
  1067. end
  1068. else
  1069. Message(scan_f_include_deep_ten);
  1070. end;
  1071. end;
  1072. {*****************************************************************************
  1073. Preprocessor writting
  1074. *****************************************************************************}
  1075. {$ifdef PREPROCWRITE}
  1076. constructor tpreprocfile.create(const fn:string);
  1077. begin
  1078. { open outputfile }
  1079. assign(f,fn);
  1080. {$I-}
  1081. rewrite(f);
  1082. {$I+}
  1083. if ioresult<>0 then
  1084. Comment(V_Fatal,'can''t create file '+fn);
  1085. getmem(buf,preprocbufsize);
  1086. settextbuf(f,buf^,preprocbufsize);
  1087. { reset }
  1088. eolfound:=false;
  1089. spacefound:=false;
  1090. end;
  1091. destructor tpreprocfile.destroy;
  1092. begin
  1093. close(f);
  1094. freemem(buf,preprocbufsize);
  1095. end;
  1096. procedure tpreprocfile.add(const s:string);
  1097. begin
  1098. write(f,s);
  1099. end;
  1100. procedure tpreprocfile.addspace;
  1101. begin
  1102. if eolfound then
  1103. begin
  1104. writeln(f,'');
  1105. eolfound:=false;
  1106. spacefound:=false;
  1107. end
  1108. else
  1109. if spacefound then
  1110. begin
  1111. write(f,' ');
  1112. spacefound:=false;
  1113. end;
  1114. end;
  1115. {$endif PREPROCWRITE}
  1116. {*****************************************************************************
  1117. TPreProcStack
  1118. *****************************************************************************}
  1119. constructor tpreprocstack.create(atyp : preproctyp;a:boolean;n:tpreprocstack);
  1120. begin
  1121. accept:=a;
  1122. typ:=atyp;
  1123. next:=n;
  1124. end;
  1125. {*****************************************************************************
  1126. TDirectiveItem
  1127. *****************************************************************************}
  1128. constructor TDirectiveItem.Create(const n:string;p:tdirectiveproc);
  1129. begin
  1130. inherited CreateName(n);
  1131. is_conditional:=false;
  1132. proc:=p;
  1133. end;
  1134. constructor TDirectiveItem.CreateCond(const n:string;p:tdirectiveproc);
  1135. begin
  1136. inherited CreateName(n);
  1137. is_conditional:=true;
  1138. proc:=p;
  1139. end;
  1140. {****************************************************************************
  1141. TSCANNERFILE
  1142. ****************************************************************************}
  1143. constructor tscannerfile.create(const fn:string);
  1144. begin
  1145. inputfile:=do_openinputfile(fn);
  1146. if assigned(current_module) then
  1147. current_module.sourcefiles.register_file(inputfile);
  1148. { reset localinput }
  1149. inputbuffer:=nil;
  1150. inputpointer:=nil;
  1151. inputstart:=0;
  1152. { reset scanner }
  1153. preprocstack:=nil;
  1154. comment_level:=0;
  1155. yylexcount:=0;
  1156. block_type:=bt_general;
  1157. line_no:=0;
  1158. lastlinepos:=0;
  1159. lasttokenpos:=0;
  1160. lasttoken:=NOTOKEN;
  1161. nexttoken:=NOTOKEN;
  1162. lastasmgetchar:=#0;
  1163. ignoredirectives:=TStringList.Create;
  1164. in_asm_string:=false;
  1165. end;
  1166. procedure tscannerfile.firstfile;
  1167. begin
  1168. { load block }
  1169. if not openinputfile then
  1170. Message1(scan_f_cannot_open_input,inputfile.name^);
  1171. reload;
  1172. end;
  1173. destructor tscannerfile.destroy;
  1174. begin
  1175. if assigned(current_module) and
  1176. (current_module.state=ms_compiled) and
  1177. (status.errorcount=0) then
  1178. checkpreprocstack
  1179. else
  1180. begin
  1181. while assigned(preprocstack) do
  1182. poppreprocstack;
  1183. end;
  1184. if not inputfile.closed then
  1185. closeinputfile;
  1186. ignoredirectives.free;
  1187. end;
  1188. function tscannerfile.openinputfile:boolean;
  1189. begin
  1190. openinputfile:=inputfile.open;
  1191. { load buffer }
  1192. inputbuffer:=inputfile.buf;
  1193. inputpointer:=inputfile.buf;
  1194. inputstart:=inputfile.bufstart;
  1195. { line }
  1196. line_no:=0;
  1197. lastlinepos:=0;
  1198. lasttokenpos:=0;
  1199. end;
  1200. procedure tscannerfile.closeinputfile;
  1201. begin
  1202. inputfile.close;
  1203. { reset buffer }
  1204. inputbuffer:=nil;
  1205. inputpointer:=nil;
  1206. inputstart:=0;
  1207. { reset line }
  1208. line_no:=0;
  1209. lastlinepos:=0;
  1210. lasttokenpos:=0;
  1211. end;
  1212. function tscannerfile.tempopeninputfile:boolean;
  1213. begin
  1214. if inputfile.is_macro then
  1215. exit;
  1216. tempopeninputfile:=inputfile.tempopen;
  1217. { reload buffer }
  1218. inputbuffer:=inputfile.buf;
  1219. inputpointer:=inputfile.buf;
  1220. inputstart:=inputfile.bufstart;
  1221. end;
  1222. procedure tscannerfile.tempcloseinputfile;
  1223. begin
  1224. if inputfile.closed or inputfile.is_macro then
  1225. exit;
  1226. inputfile.setpos(inputstart+(inputpointer-inputbuffer));
  1227. inputfile.tempclose;
  1228. { reset buffer }
  1229. inputbuffer:=nil;
  1230. inputpointer:=nil;
  1231. inputstart:=0;
  1232. end;
  1233. procedure tscannerfile.saveinputfile;
  1234. begin
  1235. inputfile.saveinputpointer:=inputpointer;
  1236. inputfile.savelastlinepos:=lastlinepos;
  1237. inputfile.saveline_no:=line_no;
  1238. end;
  1239. procedure tscannerfile.restoreinputfile;
  1240. begin
  1241. inputpointer:=inputfile.saveinputpointer;
  1242. lastlinepos:=inputfile.savelastlinepos;
  1243. line_no:=inputfile.saveline_no;
  1244. if not inputfile.is_macro then
  1245. parser_current_file:=inputfile.name^;
  1246. end;
  1247. procedure tscannerfile.nextfile;
  1248. var
  1249. to_dispose : tinputfile;
  1250. begin
  1251. if assigned(inputfile.next) then
  1252. begin
  1253. if inputfile.is_macro then
  1254. to_dispose:=inputfile
  1255. else
  1256. begin
  1257. to_dispose:=nil;
  1258. dec(inputfilecount);
  1259. end;
  1260. { we can allways close the file, no ? }
  1261. inputfile.close;
  1262. inputfile:=inputfile.next;
  1263. if assigned(to_dispose) then
  1264. to_dispose.free;
  1265. restoreinputfile;
  1266. end;
  1267. end;
  1268. procedure tscannerfile.addfile(hp:tinputfile);
  1269. begin
  1270. saveinputfile;
  1271. { add to list }
  1272. hp.next:=inputfile;
  1273. inputfile:=hp;
  1274. { load new inputfile }
  1275. restoreinputfile;
  1276. end;
  1277. procedure tscannerfile.reload;
  1278. begin
  1279. with inputfile do
  1280. begin
  1281. { when nothing more to read then leave immediatly, so we
  1282. don't change the aktfilepos and leave it point to the last
  1283. char }
  1284. if (c=#26) and (not assigned(next)) then
  1285. exit;
  1286. repeat
  1287. { still more to read?, then change the #0 to a space so its seen
  1288. as a seperator, this can't be used for macro's which can change
  1289. the place of the #0 in the buffer with tempopen }
  1290. if (c=#0) and (bufsize>0) and
  1291. not(inputfile.is_macro) and
  1292. (inputpointer-inputbuffer<bufsize) then
  1293. begin
  1294. c:=' ';
  1295. inc(inputpointer);
  1296. exit;
  1297. end;
  1298. { can we read more from this file ? }
  1299. if (c<>#26) and (not endoffile) then
  1300. begin
  1301. readbuf;
  1302. inputpointer:=buf;
  1303. inputbuffer:=buf;
  1304. inputstart:=bufstart;
  1305. { first line? }
  1306. if line_no=0 then
  1307. begin
  1308. c:=inputpointer^;
  1309. { eat utf-8 signature? }
  1310. if (ord(inputpointer^)=$ef) and
  1311. (ord((inputpointer+1)^)=$bb) and
  1312. (ord((inputpointer+2)^)=$bf) then
  1313. begin
  1314. inc(inputpointer,3);
  1315. message(scan_c_switching_to_utf8);
  1316. aktsourcecodepage:='utf8';
  1317. end;
  1318. line_no:=1;
  1319. if cs_asm_source in aktglobalswitches then
  1320. inputfile.setline(line_no,bufstart);
  1321. end;
  1322. end
  1323. else
  1324. begin
  1325. { load eof position in tokenpos/aktfilepos }
  1326. gettokenpos;
  1327. { close file }
  1328. closeinputfile;
  1329. { no next module, than EOF }
  1330. if not assigned(inputfile.next) then
  1331. begin
  1332. c:=#26;
  1333. exit;
  1334. end;
  1335. { load next file and reopen it }
  1336. nextfile;
  1337. tempopeninputfile;
  1338. { status }
  1339. Message1(scan_t_back_in,inputfile.name^);
  1340. end;
  1341. { load next char }
  1342. c:=inputpointer^;
  1343. inc(inputpointer);
  1344. until c<>#0; { if also end, then reload again }
  1345. end;
  1346. end;
  1347. procedure tscannerfile.insertmacro(const macname:string;p:pchar;len,line,fileindex:longint);
  1348. var
  1349. hp : tinputfile;
  1350. begin
  1351. { save old postion }
  1352. dec(inputpointer);
  1353. tempcloseinputfile;
  1354. { create macro 'file' }
  1355. { use special name to dispose after !! }
  1356. hp:=do_openinputfile('_Macro_.'+macname);
  1357. addfile(hp);
  1358. with inputfile do
  1359. begin
  1360. setmacro(p,len);
  1361. { local buffer }
  1362. inputbuffer:=buf;
  1363. inputpointer:=buf;
  1364. inputstart:=bufstart;
  1365. ref_index:=fileindex;
  1366. end;
  1367. { reset line }
  1368. line_no:=line;
  1369. lastlinepos:=0;
  1370. lasttokenpos:=0;
  1371. { load new c }
  1372. c:=inputpointer^;
  1373. inc(inputpointer);
  1374. end;
  1375. procedure tscannerfile.gettokenpos;
  1376. { load the values of tokenpos and lasttokenpos }
  1377. begin
  1378. lasttokenpos:=inputstart+(inputpointer-inputbuffer);
  1379. akttokenpos.line:=line_no;
  1380. akttokenpos.column:=lasttokenpos-lastlinepos;
  1381. akttokenpos.fileindex:=inputfile.ref_index;
  1382. aktfilepos:=akttokenpos;
  1383. end;
  1384. procedure tscannerfile.inc_comment_level;
  1385. var
  1386. oldaktfilepos : tfileposinfo;
  1387. begin
  1388. if (m_nested_comment in aktmodeswitches) then
  1389. inc(comment_level)
  1390. else
  1391. comment_level:=1;
  1392. if (comment_level>1) then
  1393. begin
  1394. oldaktfilepos:=aktfilepos;
  1395. gettokenpos; { update for warning }
  1396. Message1(scan_w_comment_level,tostr(comment_level));
  1397. aktfilepos:=oldaktfilepos;
  1398. end;
  1399. end;
  1400. procedure tscannerfile.dec_comment_level;
  1401. begin
  1402. if (m_nested_comment in aktmodeswitches) then
  1403. dec(comment_level)
  1404. else
  1405. comment_level:=0;
  1406. end;
  1407. procedure tscannerfile.linebreak;
  1408. var
  1409. cur : char;
  1410. oldtokenpos,
  1411. oldaktfilepos : tfileposinfo;
  1412. begin
  1413. with inputfile do
  1414. begin
  1415. if (byte(inputpointer^)=0) and not(endoffile) then
  1416. begin
  1417. cur:=c;
  1418. reload;
  1419. if byte(cur)+byte(c)<>23 then
  1420. dec(inputpointer);
  1421. end
  1422. else
  1423. begin
  1424. { Support all combination of #10 and #13 as line break }
  1425. if (byte(inputpointer^)+byte(c)=23) then
  1426. inc(inputpointer);
  1427. end;
  1428. { Always return #10 as line break }
  1429. c:=#10;
  1430. { increase line counters }
  1431. lastlinepos:=bufstart+(inputpointer-inputbuffer);
  1432. inc(line_no);
  1433. { update linebuffer }
  1434. if cs_asm_source in aktglobalswitches then
  1435. inputfile.setline(line_no,lastlinepos);
  1436. { update for status and call the show status routine,
  1437. but don't touch aktfilepos ! }
  1438. oldaktfilepos:=aktfilepos;
  1439. oldtokenpos:=akttokenpos;
  1440. gettokenpos; { update for v_status }
  1441. inc(status.compiledlines);
  1442. ShowStatus;
  1443. aktfilepos:=oldaktfilepos;
  1444. akttokenpos:=oldtokenpos;
  1445. end;
  1446. end;
  1447. procedure tscannerfile.illegal_char(c:char);
  1448. var
  1449. s : string;
  1450. begin
  1451. if c in [#32..#255] then
  1452. s:=''''+c+''''
  1453. else
  1454. s:='#'+tostr(ord(c));
  1455. Message2(scan_f_illegal_char,s,'$'+hexstr(ord(c),2));
  1456. end;
  1457. procedure tscannerfile.end_of_file;
  1458. begin
  1459. checkpreprocstack;
  1460. Message(scan_f_end_of_file);
  1461. end;
  1462. {-------------------------------------------
  1463. IF Conditional Handling
  1464. -------------------------------------------}
  1465. procedure tscannerfile.checkpreprocstack;
  1466. begin
  1467. { check for missing ifdefs }
  1468. while assigned(preprocstack) do
  1469. begin
  1470. Message4(scan_e_endif_expected,preprocstring[preprocstack.typ],preprocstack.name,
  1471. preprocstack.owner.inputfile.name^,tostr(preprocstack.line_nb));
  1472. poppreprocstack;
  1473. end;
  1474. end;
  1475. procedure tscannerfile.poppreprocstack;
  1476. var
  1477. hp : tpreprocstack;
  1478. begin
  1479. if assigned(preprocstack) then
  1480. begin
  1481. Message1(scan_c_endif_found,preprocstack.name);
  1482. hp:=preprocstack.next;
  1483. preprocstack.free;
  1484. preprocstack:=hp;
  1485. end
  1486. else
  1487. Message(scan_e_endif_without_if);
  1488. end;
  1489. procedure tscannerfile.addpreprocstack(atyp : preproctyp;a:boolean;const s:string;w:longint);
  1490. begin
  1491. preprocstack:=tpreprocstack.create(atyp,((preprocstack=nil) or preprocstack.accept) and a,preprocstack);
  1492. preprocstack.name:=s;
  1493. preprocstack.line_nb:=line_no;
  1494. preprocstack.owner:=self;
  1495. if preprocstack.accept then
  1496. Message2(w,preprocstack.name,'accepted')
  1497. else
  1498. Message2(w,preprocstack.name,'rejected');
  1499. end;
  1500. procedure tscannerfile.elsepreprocstack;
  1501. begin
  1502. if assigned(preprocstack) and
  1503. (preprocstack.typ<>pp_else) then
  1504. begin
  1505. if (preprocstack.typ=pp_elseif) then
  1506. preprocstack.accept:=false
  1507. else
  1508. if (not(assigned(preprocstack.next)) or (preprocstack.next.accept)) then
  1509. preprocstack.accept:=not preprocstack.accept;
  1510. preprocstack.typ:=pp_else;
  1511. preprocstack.line_nb:=line_no;
  1512. if preprocstack.accept then
  1513. Message2(scan_c_else_found,preprocstack.name,'accepted')
  1514. else
  1515. Message2(scan_c_else_found,preprocstack.name,'rejected');
  1516. end
  1517. else
  1518. Message(scan_e_endif_without_if);
  1519. end;
  1520. procedure tscannerfile.elseifpreprocstack(accept:boolean);
  1521. begin
  1522. if assigned(preprocstack) and
  1523. (preprocstack.typ in [pp_if,pp_elseif]) then
  1524. begin
  1525. { when the branch is accepted we use pp_elseif so we know that
  1526. all the next branches need to be rejected. when this branch is still
  1527. not accepted then leave it at pp_if }
  1528. if (preprocstack.typ=pp_elseif) then
  1529. preprocstack.accept:=false
  1530. else
  1531. if (preprocstack.typ=pp_if) and preprocstack.accept then
  1532. begin
  1533. preprocstack.accept:=false;
  1534. preprocstack.typ:=pp_elseif;
  1535. end
  1536. else
  1537. if accept and
  1538. (not(assigned(preprocstack.next)) or (preprocstack.next.accept)) then
  1539. begin
  1540. preprocstack.accept:=true;
  1541. preprocstack.typ:=pp_elseif;
  1542. end;
  1543. preprocstack.line_nb:=line_no;
  1544. if preprocstack.accept then
  1545. Message2(scan_c_else_found,preprocstack.name,'accepted')
  1546. else
  1547. Message2(scan_c_else_found,preprocstack.name,'rejected');
  1548. end
  1549. else
  1550. Message(scan_e_endif_without_if);
  1551. end;
  1552. procedure tscannerfile.handleconditional(p:tdirectiveitem);
  1553. var
  1554. oldaktfilepos : tfileposinfo;
  1555. begin
  1556. oldaktfilepos:=aktfilepos;
  1557. repeat
  1558. current_scanner.gettokenpos;
  1559. p.proc();
  1560. { accept the text ? }
  1561. if (current_scanner.preprocstack=nil) or current_scanner.preprocstack.accept then
  1562. break
  1563. else
  1564. begin
  1565. current_scanner.gettokenpos;
  1566. Message(scan_c_skipping_until);
  1567. repeat
  1568. current_scanner.skipuntildirective;
  1569. if not (m_mac in aktmodeswitches) then
  1570. p:=tdirectiveitem(turbo_scannerdirectives.search(current_scanner.readid))
  1571. else
  1572. p:=tdirectiveitem(mac_scannerdirectives.search(current_scanner.readid));
  1573. until assigned(p) and (p.is_conditional);
  1574. current_scanner.gettokenpos;
  1575. Message1(scan_d_handling_switch,'$'+p.name);
  1576. end;
  1577. until false;
  1578. aktfilepos:=oldaktfilepos;
  1579. end;
  1580. procedure tscannerfile.handledirectives;
  1581. var
  1582. t : tdirectiveitem;
  1583. hs : string;
  1584. begin
  1585. gettokenpos;
  1586. readchar; {Remove the $}
  1587. hs:=readid;
  1588. {$ifdef PREPROCWRITE}
  1589. if parapreprocess then
  1590. begin
  1591. t:=Get_Directive(hs);
  1592. if not(is_conditional(t) or (t=_DIR_DEFINE) or (t=_DIR_UNDEF)) then
  1593. begin
  1594. preprocfile^.AddSpace;
  1595. preprocfile^.Add('{$'+hs+current_scanner.readcomment+'}');
  1596. exit;
  1597. end;
  1598. end;
  1599. {$endif PREPROCWRITE}
  1600. { skip this directive? }
  1601. if (ignoredirectives.find(hs)<>nil) then
  1602. begin
  1603. if (comment_level>0) then
  1604. readcomment;
  1605. { we've read the whole comment }
  1606. aktcommentstyle:=comment_none;
  1607. exit;
  1608. end;
  1609. if hs='' then
  1610. begin
  1611. Message1(scan_w_illegal_switch,'$'+hs);
  1612. end;
  1613. { Check for compiler switches }
  1614. while (length(hs)=1) and (c in ['-','+']) do
  1615. begin
  1616. HandleSwitch(hs[1],c);
  1617. current_scanner.readchar; {Remove + or -}
  1618. if c=',' then
  1619. begin
  1620. current_scanner.readchar; {Remove , }
  1621. { read next switch, support $v+,$+}
  1622. hs:=current_scanner.readid;
  1623. if (hs='') then
  1624. begin
  1625. if (c='$') and (m_fpc in aktmodeswitches) then
  1626. begin
  1627. current_scanner.readchar; { skip $ }
  1628. hs:=current_scanner.readid;
  1629. end;
  1630. if (hs='') then
  1631. Message1(scan_w_illegal_directive,'$'+c);
  1632. end
  1633. else
  1634. Message1(scan_d_handling_switch,'$'+hs);
  1635. end
  1636. else
  1637. hs:='';
  1638. end;
  1639. { directives may follow switches after a , }
  1640. if hs<>'' then
  1641. begin
  1642. if not (m_mac in aktmodeswitches) then
  1643. t:=tdirectiveitem(turbo_scannerdirectives.search(hs))
  1644. else
  1645. t:=tdirectiveitem(mac_scannerdirectives.search(hs));
  1646. if assigned(t) then
  1647. begin
  1648. if t.is_conditional then
  1649. handleconditional(t)
  1650. else
  1651. begin
  1652. Message1(scan_d_handling_switch,'$'+hs);
  1653. t.proc();
  1654. end;
  1655. end
  1656. else
  1657. begin
  1658. current_scanner.ignoredirectives.insert(hs);
  1659. Message1(scan_w_illegal_directive,'$'+hs);
  1660. end;
  1661. { conditionals already read the comment }
  1662. if (current_scanner.comment_level>0) then
  1663. current_scanner.readcomment;
  1664. { we've read the whole comment }
  1665. aktcommentstyle:=comment_none;
  1666. end;
  1667. end;
  1668. procedure tscannerfile.readchar;
  1669. begin
  1670. c:=inputpointer^;
  1671. if c=#0 then
  1672. reload
  1673. else
  1674. inc(inputpointer);
  1675. end;
  1676. procedure tscannerfile.readstring;
  1677. var
  1678. i : longint;
  1679. err : boolean;
  1680. begin
  1681. err:=false;
  1682. i:=0;
  1683. repeat
  1684. case c of
  1685. '_',
  1686. '0'..'9',
  1687. 'A'..'Z' :
  1688. begin
  1689. if i<255 then
  1690. begin
  1691. inc(i);
  1692. orgpattern[i]:=c;
  1693. pattern[i]:=c;
  1694. end
  1695. else
  1696. begin
  1697. if not err then
  1698. begin
  1699. Message(scan_e_string_exceeds_255_chars);
  1700. err:=true;
  1701. end;
  1702. end;
  1703. c:=inputpointer^;
  1704. inc(inputpointer);
  1705. end;
  1706. 'a'..'z' :
  1707. begin
  1708. if i<255 then
  1709. begin
  1710. inc(i);
  1711. orgpattern[i]:=c;
  1712. pattern[i]:=chr(ord(c)-32)
  1713. end
  1714. else
  1715. begin
  1716. if not err then
  1717. begin
  1718. Message(scan_e_string_exceeds_255_chars);
  1719. err:=true;
  1720. end;
  1721. end;
  1722. c:=inputpointer^;
  1723. inc(inputpointer);
  1724. end;
  1725. #0 :
  1726. reload;
  1727. else
  1728. break;
  1729. end;
  1730. until false;
  1731. orgpattern[0]:=chr(i);
  1732. pattern[0]:=chr(i);
  1733. end;
  1734. procedure tscannerfile.readnumber;
  1735. var
  1736. base,
  1737. i : longint;
  1738. begin
  1739. case c of
  1740. '%' :
  1741. begin
  1742. readchar;
  1743. base:=2;
  1744. pattern[1]:='%';
  1745. i:=1;
  1746. end;
  1747. '&' :
  1748. begin
  1749. readchar;
  1750. base:=8;
  1751. pattern[1]:='&';
  1752. i:=1;
  1753. end;
  1754. '$' :
  1755. begin
  1756. readchar;
  1757. base:=16;
  1758. pattern[1]:='$';
  1759. i:=1;
  1760. end;
  1761. else
  1762. begin
  1763. base:=10;
  1764. i:=0;
  1765. end;
  1766. end;
  1767. while ((base>=10) and (c in ['0'..'9'])) or
  1768. ((base=16) and (c in ['A'..'F','a'..'f'])) or
  1769. ((base=8) and (c in ['0'..'7'])) or
  1770. ((base=2) and (c in ['0'..'1'])) do
  1771. begin
  1772. if i<255 then
  1773. begin
  1774. inc(i);
  1775. pattern[i]:=c;
  1776. end;
  1777. readchar;
  1778. end;
  1779. pattern[0]:=chr(i);
  1780. end;
  1781. function tscannerfile.readid:string;
  1782. begin
  1783. readstring;
  1784. readid:=pattern;
  1785. end;
  1786. function tscannerfile.readval:longint;
  1787. var
  1788. l : longint;
  1789. w : integer;
  1790. begin
  1791. readnumber;
  1792. val(pattern,l,w);
  1793. readval:=l;
  1794. end;
  1795. function tscannerfile.readval_asstring:string;
  1796. begin
  1797. readnumber;
  1798. readval_asstring:=pattern;
  1799. end;
  1800. function tscannerfile.readcomment:string;
  1801. var
  1802. i : longint;
  1803. begin
  1804. i:=0;
  1805. repeat
  1806. case c of
  1807. '{' :
  1808. begin
  1809. if aktcommentstyle=comment_tp then
  1810. inc_comment_level;
  1811. end;
  1812. '}' :
  1813. begin
  1814. if aktcommentstyle=comment_tp then
  1815. begin
  1816. readchar;
  1817. dec_comment_level;
  1818. if comment_level=0 then
  1819. break
  1820. else
  1821. continue;
  1822. end;
  1823. end;
  1824. '*' :
  1825. begin
  1826. if aktcommentstyle=comment_oldtp then
  1827. begin
  1828. readchar;
  1829. if c=')' then
  1830. begin
  1831. readchar;
  1832. dec_comment_level;
  1833. break;
  1834. end
  1835. else
  1836. { Add both characters !!}
  1837. if (i<255) then
  1838. begin
  1839. inc(i);
  1840. readcomment[i]:='*';
  1841. if (i<255) then
  1842. begin
  1843. inc(i);
  1844. readcomment[i]:='*';
  1845. end;
  1846. end;
  1847. end
  1848. else
  1849. { Not old TP comment, so add...}
  1850. begin
  1851. if (i<255) then
  1852. begin
  1853. inc(i);
  1854. readcomment[i]:='*';
  1855. end;
  1856. end;
  1857. end;
  1858. #10,#13 :
  1859. linebreak;
  1860. #26 :
  1861. end_of_file;
  1862. else
  1863. begin
  1864. if (i<255) then
  1865. begin
  1866. inc(i);
  1867. readcomment[i]:=c;
  1868. end;
  1869. end;
  1870. end;
  1871. readchar;
  1872. until false;
  1873. readcomment[0]:=chr(i);
  1874. end;
  1875. function tscannerfile.readquotedstring:string;
  1876. var
  1877. i : longint;
  1878. msgwritten : boolean;
  1879. begin
  1880. i:=0;
  1881. msgwritten:=false;
  1882. if (c='''') then
  1883. begin
  1884. repeat
  1885. readchar;
  1886. case c of
  1887. #26 :
  1888. end_of_file;
  1889. #10,#13 :
  1890. Message(scan_f_string_exceeds_line);
  1891. '''' :
  1892. begin
  1893. readchar;
  1894. if c<>'''' then
  1895. break;
  1896. end;
  1897. end;
  1898. if i<255 then
  1899. begin
  1900. inc(i);
  1901. result[i]:=c;
  1902. end
  1903. else
  1904. begin
  1905. if not msgwritten then
  1906. begin
  1907. Message(scan_e_string_exceeds_255_chars);
  1908. msgwritten:=true;
  1909. end;
  1910. end;
  1911. until false;
  1912. end;
  1913. result[0]:=chr(i);
  1914. end;
  1915. function tscannerfile.readstate:char;
  1916. var
  1917. state : char;
  1918. begin
  1919. state:=' ';
  1920. if c=' ' then
  1921. begin
  1922. current_scanner.skipspace;
  1923. current_scanner.readid;
  1924. if pattern='ON' then
  1925. state:='+'
  1926. else
  1927. if pattern='OFF' then
  1928. state:='-';
  1929. end
  1930. else
  1931. state:=c;
  1932. if not (state in ['+','-']) then
  1933. Message(scan_e_wrong_switch_toggle);
  1934. readstate:=state;
  1935. end;
  1936. function tscannerfile.readstatedefault:char;
  1937. var
  1938. state : char;
  1939. begin
  1940. state:=' ';
  1941. if c=' ' then
  1942. begin
  1943. current_scanner.skipspace;
  1944. current_scanner.readid;
  1945. if pattern='ON' then
  1946. state:='+'
  1947. else
  1948. if pattern='OFF' then
  1949. state:='-'
  1950. else
  1951. if pattern='DEFAULT' then
  1952. state:='*';
  1953. end
  1954. else
  1955. state:=c;
  1956. if not (state in ['+','-','*']) then
  1957. Message(scan_e_wrong_switch_toggle_default);
  1958. readstatedefault:=state;
  1959. end;
  1960. procedure tscannerfile.skipspace;
  1961. begin
  1962. repeat
  1963. case c of
  1964. #26 :
  1965. begin
  1966. reload;
  1967. if (c=#26) and not assigned(inputfile.next) then
  1968. break;
  1969. continue;
  1970. end;
  1971. #10,
  1972. #13 :
  1973. linebreak;
  1974. #9,#11,#12,' ' :
  1975. ;
  1976. else
  1977. break;
  1978. end;
  1979. readchar;
  1980. until false;
  1981. end;
  1982. procedure tscannerfile.skipuntildirective;
  1983. var
  1984. found : longint;
  1985. next_char_loaded : boolean;
  1986. begin
  1987. found:=0;
  1988. next_char_loaded:=false;
  1989. repeat
  1990. case c of
  1991. #10,
  1992. #13 :
  1993. linebreak;
  1994. #26 :
  1995. begin
  1996. reload;
  1997. if (c=#26) and not assigned(inputfile.next) then
  1998. end_of_file;
  1999. continue;
  2000. end;
  2001. '{' :
  2002. begin
  2003. if (aktcommentstyle in [comment_tp,comment_none]) then
  2004. begin
  2005. aktcommentstyle:=comment_tp;
  2006. if (comment_level=0) then
  2007. found:=1;
  2008. inc_comment_level;
  2009. end;
  2010. end;
  2011. '*' :
  2012. begin
  2013. if (aktcommentstyle=comment_oldtp) then
  2014. begin
  2015. readchar;
  2016. if c=')' then
  2017. begin
  2018. dec_comment_level;
  2019. found:=0;
  2020. aktcommentstyle:=comment_none;
  2021. end
  2022. else
  2023. next_char_loaded:=true;
  2024. end
  2025. else
  2026. found := 0;
  2027. end;
  2028. '}' :
  2029. begin
  2030. if (aktcommentstyle=comment_tp) then
  2031. begin
  2032. dec_comment_level;
  2033. if (comment_level=0) then
  2034. aktcommentstyle:=comment_none;
  2035. found:=0;
  2036. end;
  2037. end;
  2038. '$' :
  2039. begin
  2040. if found=1 then
  2041. found:=2;
  2042. end;
  2043. '''' :
  2044. if (aktcommentstyle=comment_none) then
  2045. begin
  2046. repeat
  2047. readchar;
  2048. case c of
  2049. #26 :
  2050. end_of_file;
  2051. #10,#13 :
  2052. break;
  2053. '''' :
  2054. begin
  2055. readchar;
  2056. if c<>'''' then
  2057. begin
  2058. next_char_loaded:=true;
  2059. break;
  2060. end;
  2061. end;
  2062. end;
  2063. until false;
  2064. end;
  2065. '(' :
  2066. begin
  2067. if (aktcommentstyle=comment_none) then
  2068. begin
  2069. readchar;
  2070. if c='*' then
  2071. begin
  2072. readchar;
  2073. if c='$' then
  2074. begin
  2075. found:=2;
  2076. inc_comment_level;
  2077. aktcommentstyle:=comment_oldtp;
  2078. end
  2079. else
  2080. begin
  2081. skipoldtpcomment;
  2082. next_char_loaded:=true;
  2083. end;
  2084. end
  2085. else
  2086. next_char_loaded:=true;
  2087. end
  2088. else
  2089. found:=0;
  2090. end;
  2091. '/' :
  2092. begin
  2093. if (aktcommentstyle=comment_none) then
  2094. begin
  2095. readchar;
  2096. if c='/' then
  2097. skipdelphicomment;
  2098. next_char_loaded:=true;
  2099. end
  2100. else
  2101. found:=0;
  2102. end;
  2103. else
  2104. found:=0;
  2105. end;
  2106. if next_char_loaded then
  2107. next_char_loaded:=false
  2108. else
  2109. readchar;
  2110. until (found=2);
  2111. end;
  2112. {****************************************************************************
  2113. Comment Handling
  2114. ****************************************************************************}
  2115. procedure tscannerfile.skipcomment;
  2116. begin
  2117. aktcommentstyle:=comment_tp;
  2118. readchar;
  2119. inc_comment_level;
  2120. { handle compiler switches }
  2121. if (c='$') then
  2122. handledirectives;
  2123. { handle_switches can dec comment_level, }
  2124. while (comment_level>0) do
  2125. begin
  2126. case c of
  2127. '{' :
  2128. inc_comment_level;
  2129. '}' :
  2130. dec_comment_level;
  2131. #10,#13 :
  2132. linebreak;
  2133. #26 :
  2134. begin
  2135. reload;
  2136. if (c=#26) and not assigned(inputfile.next) then
  2137. end_of_file;
  2138. continue;
  2139. end;
  2140. end;
  2141. readchar;
  2142. end;
  2143. aktcommentstyle:=comment_none;
  2144. end;
  2145. procedure tscannerfile.skipdelphicomment;
  2146. begin
  2147. aktcommentstyle:=comment_delphi;
  2148. inc_comment_level;
  2149. readchar;
  2150. { this is not supported }
  2151. if c='$' then
  2152. Message(scan_w_wrong_styled_switch);
  2153. { skip comment }
  2154. while not (c in [#10,#13,#26]) do
  2155. readchar;
  2156. dec_comment_level;
  2157. aktcommentstyle:=comment_none;
  2158. end;
  2159. procedure tscannerfile.skipoldtpcomment;
  2160. var
  2161. found : longint;
  2162. begin
  2163. aktcommentstyle:=comment_oldtp;
  2164. inc_comment_level;
  2165. { only load a char if last already processed,
  2166. was cause of bug1634 PM }
  2167. if c=#0 then
  2168. readchar;
  2169. { this is now supported }
  2170. if (c='$') then
  2171. handledirectives;
  2172. { skip comment }
  2173. while (comment_level>0) do
  2174. begin
  2175. found:=0;
  2176. repeat
  2177. case c of
  2178. #26 :
  2179. begin
  2180. reload;
  2181. if (c=#26) and not assigned(inputfile.next) then
  2182. end_of_file;
  2183. continue;
  2184. end;
  2185. #10,#13 :
  2186. linebreak;
  2187. '*' :
  2188. begin
  2189. if found=3 then
  2190. found:=4
  2191. else
  2192. found:=1;
  2193. end;
  2194. ')' :
  2195. begin
  2196. if found in [1,4] then
  2197. begin
  2198. dec_comment_level;
  2199. if comment_level=0 then
  2200. found:=2
  2201. else
  2202. found:=0;
  2203. end;
  2204. end;
  2205. '(' :
  2206. begin
  2207. if found=4 then
  2208. inc_comment_level;
  2209. found:=3;
  2210. end;
  2211. else
  2212. begin
  2213. if found=4 then
  2214. inc_comment_level;
  2215. found:=0;
  2216. end;
  2217. end;
  2218. readchar;
  2219. until (found=2);
  2220. end;
  2221. aktcommentstyle:=comment_none;
  2222. end;
  2223. {****************************************************************************
  2224. Token Scanner
  2225. ****************************************************************************}
  2226. procedure tscannerfile.readtoken;
  2227. var
  2228. code : integer;
  2229. len,
  2230. low,high,mid : longint;
  2231. w : word;
  2232. m : longint;
  2233. mac : tmacro;
  2234. asciinr : string[6];
  2235. msgwritten,
  2236. iswidestring : boolean;
  2237. label
  2238. exit_label;
  2239. begin
  2240. if localswitcheschanged then
  2241. begin
  2242. aktlocalswitches:=nextaktlocalswitches;
  2243. localswitcheschanged:=false;
  2244. end;
  2245. { was there already a token read, then return that token }
  2246. if nexttoken<>NOTOKEN then
  2247. begin
  2248. token:=nexttoken;
  2249. nexttoken:=NOTOKEN;
  2250. goto exit_label;
  2251. end;
  2252. { Skip all spaces and comments }
  2253. repeat
  2254. case c of
  2255. '{' :
  2256. skipcomment;
  2257. #26 :
  2258. begin
  2259. reload;
  2260. if (c=#26) and not assigned(inputfile.next) then
  2261. break;
  2262. end;
  2263. ' ',#9..#13 :
  2264. begin
  2265. {$ifdef PREPROCWRITE}
  2266. if parapreprocess then
  2267. begin
  2268. if c=#10 then
  2269. preprocfile.eolfound:=true
  2270. else
  2271. preprocfile.spacefound:=true;
  2272. end;
  2273. {$endif PREPROCWRITE}
  2274. skipspace;
  2275. end
  2276. else
  2277. break;
  2278. end;
  2279. until false;
  2280. { Save current token position, for EOF its already loaded }
  2281. if c<>#26 then
  2282. gettokenpos;
  2283. { Check first for a identifier/keyword, this is 20+% faster (PFV) }
  2284. if c in ['A'..'Z','a'..'z','_'] then
  2285. begin
  2286. readstring;
  2287. token:=_ID;
  2288. idtoken:=_ID;
  2289. { keyword or any other known token,
  2290. pattern is always uppercased }
  2291. if (pattern[1]<>'_') and (length(pattern) in [tokenlenmin..tokenlenmax]) then
  2292. begin
  2293. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  2294. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  2295. while low<high do
  2296. begin
  2297. mid:=(high+low+1) shr 1;
  2298. if pattern<tokeninfo^[ttoken(mid)].str then
  2299. high:=mid-1
  2300. else
  2301. low:=mid;
  2302. end;
  2303. with tokeninfo^[ttoken(high)] do
  2304. if pattern=str then
  2305. begin
  2306. if keyword in aktmodeswitches then
  2307. if op=NOTOKEN then
  2308. token:=ttoken(high)
  2309. else
  2310. token:=op;
  2311. idtoken:=ttoken(high);
  2312. end;
  2313. end;
  2314. { Only process identifiers and not keywords }
  2315. if token=_ID then
  2316. begin
  2317. { this takes some time ... }
  2318. if (cs_support_macro in aktmoduleswitches) then
  2319. begin
  2320. mac:=tmacro(search_macro(pattern));
  2321. if assigned(mac) and (not mac.is_compiler_var) and (assigned(mac.buftext)) then
  2322. begin
  2323. if yylexcount<max_macro_nesting then
  2324. begin
  2325. mac.is_used:=true;
  2326. inc(yylexcount);
  2327. insertmacro(pattern,mac.buftext,mac.buflen,
  2328. mac.fileinfo.line,mac.fileinfo.fileindex);
  2329. { handle empty macros }
  2330. if c=#0 then
  2331. reload;
  2332. readtoken;
  2333. { that's all folks }
  2334. dec(yylexcount);
  2335. exit;
  2336. end
  2337. else
  2338. Message(scan_w_macro_too_deep);
  2339. end;
  2340. end;
  2341. end;
  2342. { return token }
  2343. goto exit_label;
  2344. end
  2345. else
  2346. begin
  2347. idtoken:=_NOID;
  2348. case c of
  2349. '$' :
  2350. begin
  2351. readnumber;
  2352. token:=_INTCONST;
  2353. goto exit_label;
  2354. end;
  2355. '%' :
  2356. begin
  2357. if not(m_fpc in aktmodeswitches) then
  2358. Illegal_Char(c)
  2359. else
  2360. begin
  2361. readnumber;
  2362. token:=_INTCONST;
  2363. goto exit_label;
  2364. end;
  2365. end;
  2366. '&' :
  2367. begin
  2368. if m_fpc in aktmodeswitches then
  2369. begin
  2370. readnumber;
  2371. token:=_INTCONST;
  2372. goto exit_label;
  2373. end
  2374. else if m_mac in aktmodeswitches then
  2375. begin
  2376. readchar;
  2377. token:=_AMPERSAND;
  2378. goto exit_label;
  2379. end
  2380. else
  2381. Illegal_Char(c);
  2382. end;
  2383. '0'..'9' :
  2384. begin
  2385. readnumber;
  2386. if (c in ['.','e','E']) then
  2387. begin
  2388. { first check for a . }
  2389. if c='.' then
  2390. begin
  2391. readchar;
  2392. { is it a .. from a range? }
  2393. case c of
  2394. '.' :
  2395. begin
  2396. readchar;
  2397. token:=_INTCONST;
  2398. nexttoken:=_POINTPOINT;
  2399. goto exit_label;
  2400. end;
  2401. ')' :
  2402. begin
  2403. readchar;
  2404. token:=_INTCONST;
  2405. nexttoken:=_RECKKLAMMER;
  2406. goto exit_label;
  2407. end;
  2408. end;
  2409. { insert the number after the . }
  2410. pattern:=pattern+'.';
  2411. while c in ['0'..'9'] do
  2412. begin
  2413. pattern:=pattern+c;
  2414. readchar;
  2415. end;
  2416. end;
  2417. { E can also follow after a point is scanned }
  2418. if c in ['e','E'] then
  2419. begin
  2420. pattern:=pattern+'E';
  2421. readchar;
  2422. if c in ['-','+'] then
  2423. begin
  2424. pattern:=pattern+c;
  2425. readchar;
  2426. end;
  2427. if not(c in ['0'..'9']) then
  2428. Illegal_Char(c);
  2429. while c in ['0'..'9'] do
  2430. begin
  2431. pattern:=pattern+c;
  2432. readchar;
  2433. end;
  2434. end;
  2435. token:=_REALNUMBER;
  2436. goto exit_label;
  2437. end;
  2438. token:=_INTCONST;
  2439. goto exit_label;
  2440. end;
  2441. ';' :
  2442. begin
  2443. readchar;
  2444. token:=_SEMICOLON;
  2445. goto exit_label;
  2446. end;
  2447. '[' :
  2448. begin
  2449. readchar;
  2450. token:=_LECKKLAMMER;
  2451. goto exit_label;
  2452. end;
  2453. ']' :
  2454. begin
  2455. readchar;
  2456. token:=_RECKKLAMMER;
  2457. goto exit_label;
  2458. end;
  2459. '(' :
  2460. begin
  2461. readchar;
  2462. case c of
  2463. '*' :
  2464. begin
  2465. c:=#0;{Signal skipoldtpcomment to reload a char }
  2466. skipoldtpcomment;
  2467. readtoken;
  2468. exit;
  2469. end;
  2470. '.' :
  2471. begin
  2472. readchar;
  2473. token:=_LECKKLAMMER;
  2474. goto exit_label;
  2475. end;
  2476. end;
  2477. token:=_LKLAMMER;
  2478. goto exit_label;
  2479. end;
  2480. ')' :
  2481. begin
  2482. readchar;
  2483. token:=_RKLAMMER;
  2484. goto exit_label;
  2485. end;
  2486. '+' :
  2487. begin
  2488. readchar;
  2489. if (c='=') and (cs_support_c_operators in aktmoduleswitches) then
  2490. begin
  2491. readchar;
  2492. token:=_PLUSASN;
  2493. goto exit_label;
  2494. end;
  2495. token:=_PLUS;
  2496. goto exit_label;
  2497. end;
  2498. '-' :
  2499. begin
  2500. readchar;
  2501. if (c='=') and (cs_support_c_operators in aktmoduleswitches) then
  2502. begin
  2503. readchar;
  2504. token:=_MINUSASN;
  2505. goto exit_label;
  2506. end;
  2507. token:=_MINUS;
  2508. goto exit_label;
  2509. end;
  2510. ':' :
  2511. begin
  2512. readchar;
  2513. if c='=' then
  2514. begin
  2515. readchar;
  2516. token:=_ASSIGNMENT;
  2517. goto exit_label;
  2518. end;
  2519. token:=_COLON;
  2520. goto exit_label;
  2521. end;
  2522. '*' :
  2523. begin
  2524. readchar;
  2525. if (c='=') and (cs_support_c_operators in aktmoduleswitches) then
  2526. begin
  2527. readchar;
  2528. token:=_STARASN;
  2529. end
  2530. else
  2531. if c='*' then
  2532. begin
  2533. readchar;
  2534. token:=_STARSTAR;
  2535. end
  2536. else
  2537. token:=_STAR;
  2538. goto exit_label;
  2539. end;
  2540. '/' :
  2541. begin
  2542. readchar;
  2543. case c of
  2544. '=' :
  2545. begin
  2546. if (cs_support_c_operators in aktmoduleswitches) then
  2547. begin
  2548. readchar;
  2549. token:=_SLASHASN;
  2550. goto exit_label;
  2551. end;
  2552. end;
  2553. '/' :
  2554. begin
  2555. skipdelphicomment;
  2556. readtoken;
  2557. exit;
  2558. end;
  2559. end;
  2560. token:=_SLASH;
  2561. goto exit_label;
  2562. end;
  2563. '|' :
  2564. if m_mac in aktmodeswitches then
  2565. begin
  2566. readchar;
  2567. token:=_PIPE;
  2568. goto exit_label;
  2569. end
  2570. else
  2571. Illegal_Char(c);
  2572. '=' :
  2573. begin
  2574. readchar;
  2575. token:=_EQUAL;
  2576. goto exit_label;
  2577. end;
  2578. '.' :
  2579. begin
  2580. readchar;
  2581. case c of
  2582. '.' :
  2583. begin
  2584. readchar;
  2585. case c of
  2586. '.' :
  2587. begin
  2588. readchar;
  2589. token:=_POINTPOINTPOINT;
  2590. goto exit_label;
  2591. end;
  2592. else
  2593. begin
  2594. token:=_POINTPOINT;
  2595. goto exit_label;
  2596. end;
  2597. end;
  2598. end;
  2599. ')' :
  2600. begin
  2601. readchar;
  2602. token:=_RECKKLAMMER;
  2603. goto exit_label;
  2604. end;
  2605. end;
  2606. token:=_POINT;
  2607. goto exit_label;
  2608. end;
  2609. '@' :
  2610. begin
  2611. readchar;
  2612. token:=_KLAMMERAFFE;
  2613. goto exit_label;
  2614. end;
  2615. ',' :
  2616. begin
  2617. readchar;
  2618. token:=_COMMA;
  2619. goto exit_label;
  2620. end;
  2621. '''','#','^' :
  2622. begin
  2623. len:=0;
  2624. msgwritten:=false;
  2625. pattern:='';
  2626. iswidestring:=false;
  2627. if c='^' then
  2628. begin
  2629. readchar;
  2630. c:=upcase(c);
  2631. if (block_type=bt_type) or
  2632. (lasttoken=_ID) or (lasttoken=_NIL) or
  2633. (lasttoken=_RKLAMMER) or (lasttoken=_RECKKLAMMER) or (lasttoken=_CARET) then
  2634. begin
  2635. token:=_CARET;
  2636. goto exit_label;
  2637. end
  2638. else
  2639. begin
  2640. inc(len);
  2641. if c<#64 then
  2642. pattern[len]:=chr(ord(c)+64)
  2643. else
  2644. pattern[len]:=chr(ord(c)-64);
  2645. readchar;
  2646. end;
  2647. end;
  2648. repeat
  2649. case c of
  2650. '#' :
  2651. begin
  2652. readchar; { read # }
  2653. if c='$' then
  2654. begin
  2655. readchar; { read leading $ }
  2656. asciinr:='$';
  2657. while (upcase(c) in ['A'..'F','0'..'9']) and (length(asciinr)<6) do
  2658. begin
  2659. asciinr:=asciinr+c;
  2660. readchar;
  2661. end;
  2662. end
  2663. else
  2664. begin
  2665. asciinr:='';
  2666. while (c in ['0'..'9']) and (length(asciinr)<6) do
  2667. begin
  2668. asciinr:=asciinr+c;
  2669. readchar;
  2670. end;
  2671. end;
  2672. val(asciinr,m,code);
  2673. if (asciinr='') or (code<>0) then
  2674. Message(scan_e_illegal_char_const)
  2675. else if (m<0) or (m>255) or (length(asciinr)>3) then
  2676. begin
  2677. if (m>=0) and (m<=65535) then
  2678. begin
  2679. if not iswidestring then
  2680. begin
  2681. ascii2unicode(@pattern[1],len,patternw);
  2682. iswidestring:=true;
  2683. len:=0;
  2684. end;
  2685. concatwidestringchar(patternw,tcompilerwidechar(m));
  2686. end
  2687. else
  2688. Message(scan_e_illegal_char_const)
  2689. end
  2690. else if iswidestring then
  2691. concatwidestringchar(patternw,asciichar2unicode(char(m)))
  2692. else
  2693. begin
  2694. if len<255 then
  2695. begin
  2696. inc(len);
  2697. pattern[len]:=chr(m);
  2698. end
  2699. else
  2700. begin
  2701. if not msgwritten then
  2702. begin
  2703. Message(scan_e_string_exceeds_255_chars);
  2704. msgwritten:=true;
  2705. end;
  2706. end;
  2707. end;
  2708. end;
  2709. '''' :
  2710. begin
  2711. repeat
  2712. readchar;
  2713. case c of
  2714. #26 :
  2715. end_of_file;
  2716. #10,#13 :
  2717. Message(scan_f_string_exceeds_line);
  2718. '''' :
  2719. begin
  2720. readchar;
  2721. if c<>'''' then
  2722. break;
  2723. end;
  2724. end;
  2725. { interpret as utf-8 string? }
  2726. if (ord(c)>=$80) and (aktsourcecodepage='utf8') then
  2727. begin
  2728. { convert existing string to an utf-8 string }
  2729. if not iswidestring then
  2730. begin
  2731. ascii2unicode(@pattern[1],len,patternw);
  2732. iswidestring:=true;
  2733. len:=0;
  2734. end;
  2735. { four or more chars aren't handled }
  2736. if (ord(c) and $f0)=$f0 then
  2737. message(scan_e_utf8_bigger_than_65535)
  2738. { three chars }
  2739. else if (ord(c) and $e0)=$e0 then
  2740. begin
  2741. w:=ord(c) and $f;
  2742. readchar;
  2743. if (ord(c) and $c0)<>$80 then
  2744. message(scan_e_utf8_malformed);
  2745. w:=(w shl 6) or (ord(c) and $3f);
  2746. readchar;
  2747. if (ord(c) and $c0)<>$80 then
  2748. message(scan_e_utf8_malformed);
  2749. w:=(w shl 6) or (ord(c) and $3f);
  2750. concatwidestringchar(patternw,w);
  2751. end
  2752. { two chars }
  2753. else if (ord(c) and $c0)<>0 then
  2754. begin
  2755. w:=ord(c) and $1f;
  2756. readchar;
  2757. if (ord(c) and $c0)<>$80 then
  2758. message(scan_e_utf8_malformed);
  2759. w:=(w shl 6) or (ord(c) and $3f);
  2760. concatwidestringchar(patternw,w);
  2761. end
  2762. { illegal }
  2763. else if (ord(c) and $80)<>0 then
  2764. message(scan_e_utf8_malformed)
  2765. else
  2766. concatwidestringchar(patternw,tcompilerwidechar(c))
  2767. end
  2768. else if iswidestring then
  2769. begin
  2770. if aktsourcecodepage='utf8' then
  2771. concatwidestringchar(patternw,ord(c))
  2772. else
  2773. concatwidestringchar(patternw,asciichar2unicode(c))
  2774. end
  2775. else
  2776. begin
  2777. if len<255 then
  2778. begin
  2779. inc(len);
  2780. pattern[len]:=c;
  2781. end
  2782. else
  2783. begin
  2784. if not msgwritten then
  2785. begin
  2786. Message(scan_e_string_exceeds_255_chars);
  2787. msgwritten:=true;
  2788. end;
  2789. end;
  2790. end;
  2791. until false;
  2792. end;
  2793. '^' :
  2794. begin
  2795. readchar;
  2796. c:=upcase(c);
  2797. if c<#64 then
  2798. c:=chr(ord(c)+64)
  2799. else
  2800. c:=chr(ord(c)-64);
  2801. if iswidestring then
  2802. concatwidestringchar(patternw,asciichar2unicode(c))
  2803. else
  2804. begin
  2805. if len<255 then
  2806. begin
  2807. inc(len);
  2808. pattern[len]:=c;
  2809. end
  2810. else
  2811. begin
  2812. if not msgwritten then
  2813. begin
  2814. Message(scan_e_string_exceeds_255_chars);
  2815. msgwritten:=true;
  2816. end;
  2817. end;
  2818. end;
  2819. readchar;
  2820. end;
  2821. else
  2822. break;
  2823. end;
  2824. until false;
  2825. { strings with length 1 become const chars }
  2826. if iswidestring then
  2827. begin
  2828. if patternw^.len=1 then
  2829. token:=_CWCHAR
  2830. else
  2831. token:=_CWSTRING;
  2832. end
  2833. else
  2834. begin
  2835. pattern[0]:=chr(len);
  2836. if len=1 then
  2837. token:=_CCHAR
  2838. else
  2839. token:=_CSTRING;
  2840. end;
  2841. goto exit_label;
  2842. end;
  2843. '>' :
  2844. begin
  2845. readchar;
  2846. case c of
  2847. '=' :
  2848. begin
  2849. readchar;
  2850. token:=_GTE;
  2851. goto exit_label;
  2852. end;
  2853. '>' :
  2854. begin
  2855. readchar;
  2856. token:=_OP_SHR;
  2857. goto exit_label;
  2858. end;
  2859. '<' :
  2860. begin { >< is for a symetric diff for sets }
  2861. readchar;
  2862. token:=_SYMDIF;
  2863. goto exit_label;
  2864. end;
  2865. end;
  2866. token:=_GT;
  2867. goto exit_label;
  2868. end;
  2869. '<' :
  2870. begin
  2871. readchar;
  2872. case c of
  2873. '>' :
  2874. begin
  2875. readchar;
  2876. token:=_UNEQUAL;
  2877. goto exit_label;
  2878. end;
  2879. '=' :
  2880. begin
  2881. readchar;
  2882. token:=_LTE;
  2883. goto exit_label;
  2884. end;
  2885. '<' :
  2886. begin
  2887. readchar;
  2888. token:=_OP_SHL;
  2889. goto exit_label;
  2890. end;
  2891. end;
  2892. token:=_LT;
  2893. goto exit_label;
  2894. end;
  2895. #26 :
  2896. begin
  2897. token:=_EOF;
  2898. checkpreprocstack;
  2899. goto exit_label;
  2900. end;
  2901. else
  2902. Illegal_Char(c);
  2903. end;
  2904. end;
  2905. exit_label:
  2906. lasttoken:=token;
  2907. end;
  2908. function tscannerfile.readpreproc:ttoken;
  2909. begin
  2910. skipspace;
  2911. case c of
  2912. '_',
  2913. 'A'..'Z',
  2914. 'a'..'z' :
  2915. begin
  2916. current_scanner.preproc_pattern:=readid;
  2917. readpreproc:=_ID;
  2918. end;
  2919. '0'..'9' :
  2920. begin
  2921. current_scanner.preproc_pattern:=readval_asstring;
  2922. { realnumber? }
  2923. if c='.' then
  2924. begin
  2925. readchar;
  2926. while c in ['0'..'9'] do
  2927. begin
  2928. current_scanner.preproc_pattern:=current_scanner.preproc_pattern+c;
  2929. readchar;
  2930. end;
  2931. end;
  2932. readpreproc:=_ID;
  2933. end;
  2934. '$','%','&' :
  2935. begin
  2936. current_scanner.preproc_pattern:=readval_asstring;
  2937. readpreproc:=_ID;
  2938. end;
  2939. ',' :
  2940. begin
  2941. readchar;
  2942. readpreproc:=_COMMA;
  2943. end;
  2944. '}' :
  2945. begin
  2946. readpreproc:=_END;
  2947. end;
  2948. '(' :
  2949. begin
  2950. readchar;
  2951. readpreproc:=_LKLAMMER;
  2952. end;
  2953. ')' :
  2954. begin
  2955. readchar;
  2956. readpreproc:=_RKLAMMER;
  2957. end;
  2958. '[' :
  2959. begin
  2960. readchar;
  2961. readpreproc:=_LECKKLAMMER;
  2962. end;
  2963. ']' :
  2964. begin
  2965. readchar;
  2966. readpreproc:=_RECKKLAMMER;
  2967. end;
  2968. '+' :
  2969. begin
  2970. readchar;
  2971. readpreproc:=_PLUS;
  2972. end;
  2973. '-' :
  2974. begin
  2975. readchar;
  2976. readpreproc:=_MINUS;
  2977. end;
  2978. '*' :
  2979. begin
  2980. readchar;
  2981. readpreproc:=_STAR;
  2982. end;
  2983. '/' :
  2984. begin
  2985. readchar;
  2986. readpreproc:=_SLASH;
  2987. end;
  2988. '=' :
  2989. begin
  2990. readchar;
  2991. readpreproc:=_EQUAL;
  2992. end;
  2993. '>' :
  2994. begin
  2995. readchar;
  2996. if c='=' then
  2997. begin
  2998. readchar;
  2999. readpreproc:=_GTE;
  3000. end
  3001. else
  3002. readpreproc:=_GT;
  3003. end;
  3004. '<' :
  3005. begin
  3006. readchar;
  3007. case c of
  3008. '>' :
  3009. begin
  3010. readchar;
  3011. readpreproc:=_UNEQUAL;
  3012. end;
  3013. '=' :
  3014. begin
  3015. readchar;
  3016. readpreproc:=_LTE;
  3017. end;
  3018. else
  3019. readpreproc:=_LT;
  3020. end;
  3021. end;
  3022. #26 :
  3023. begin
  3024. readpreproc:=_EOF;
  3025. checkpreprocstack;
  3026. end;
  3027. else
  3028. Illegal_Char(c);
  3029. end;
  3030. end;
  3031. function tscannerfile.asmgetcharstart : char;
  3032. begin
  3033. { return first the character already
  3034. available in c }
  3035. lastasmgetchar:=c;
  3036. result:=asmgetchar;
  3037. end;
  3038. function tscannerfile.asmgetchar : char;
  3039. begin
  3040. if lastasmgetchar<>#0 then
  3041. begin
  3042. c:=lastasmgetchar;
  3043. lastasmgetchar:=#0;
  3044. end
  3045. else
  3046. readchar;
  3047. if in_asm_string then
  3048. begin
  3049. asmgetchar:=c;
  3050. exit;
  3051. end;
  3052. repeat
  3053. case c of
  3054. {$ifndef arm}
  3055. // the { ... } is used in ARM assembler to define register sets, so we can't used
  3056. // it as comment, either (* ... *), /* ... */ or // ... should be used instead
  3057. '{' :
  3058. skipcomment;
  3059. {$endif arm}
  3060. #10,#13 :
  3061. begin
  3062. linebreak;
  3063. asmgetchar:=c;
  3064. exit;
  3065. end;
  3066. #26 :
  3067. begin
  3068. reload;
  3069. if (c=#26) and not assigned(inputfile.next) then
  3070. end_of_file;
  3071. continue;
  3072. end;
  3073. '/' :
  3074. begin
  3075. readchar;
  3076. if c='/' then
  3077. skipdelphicomment
  3078. else
  3079. begin
  3080. asmgetchar:='/';
  3081. lastasmgetchar:=c;
  3082. exit;
  3083. end;
  3084. end;
  3085. '(' :
  3086. begin
  3087. readchar;
  3088. if c='*' then
  3089. begin
  3090. c:=#0;{Signal skipoldtpcomment to reload a char }
  3091. skipoldtpcomment;
  3092. end
  3093. else
  3094. begin
  3095. asmgetchar:='(';
  3096. lastasmgetchar:=c;
  3097. exit;
  3098. end;
  3099. end;
  3100. else
  3101. begin
  3102. asmgetchar:=c;
  3103. exit;
  3104. end;
  3105. end;
  3106. until false;
  3107. end;
  3108. {*****************************************************************************
  3109. Helpers
  3110. *****************************************************************************}
  3111. procedure AddDirective(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  3112. begin
  3113. if dm in [directive_all, directive_turbo] then
  3114. turbo_scannerdirectives.insert(tdirectiveitem.create(s,p));
  3115. if dm in [directive_all, directive_mac] then
  3116. mac_scannerdirectives.insert(tdirectiveitem.create(s,p));
  3117. end;
  3118. procedure AddConditional(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  3119. begin
  3120. if dm in [directive_all, directive_turbo] then
  3121. turbo_scannerdirectives.insert(tdirectiveitem.createcond(s,p));
  3122. if dm in [directive_all, directive_mac] then
  3123. mac_scannerdirectives.insert(tdirectiveitem.createcond(s,p));
  3124. end;
  3125. {*****************************************************************************
  3126. Initialization
  3127. *****************************************************************************}
  3128. procedure InitScanner;
  3129. begin
  3130. InitWideString(patternw);
  3131. turbo_scannerdirectives:=TDictionary.Create;
  3132. mac_scannerdirectives:=TDictionary.Create;
  3133. { Common directives and conditionals }
  3134. AddDirective('I',directive_all, @dir_include);
  3135. AddDirective('DEFINE',directive_all, @dir_define);
  3136. AddDirective('UNDEF',directive_all, @dir_undef);
  3137. AddConditional('IF',directive_all, @dir_if);
  3138. AddConditional('IFDEF',directive_all, @dir_ifdef);
  3139. AddConditional('IFNDEF',directive_all, @dir_ifndef);
  3140. AddConditional('ELSE',directive_all, @dir_else);
  3141. AddConditional('ELSEIF',directive_all, @dir_elseif);
  3142. AddConditional('ENDIF',directive_all, @dir_endif);
  3143. { Directives and conditionals for all modes except mode macpas}
  3144. AddDirective('INCLUDE',directive_turbo, @dir_include);
  3145. AddDirective('LIBPREFIX',directive_turbo, @dir_libprefix);
  3146. AddDirective('LIBSUFFIX',directive_turbo, @dir_libsuffix);
  3147. AddDirective('EXTENSION',directive_turbo, @dir_extension);
  3148. AddConditional('IFEND',directive_turbo, @dir_endif);
  3149. AddConditional('IFOPT',directive_turbo, @dir_ifopt);
  3150. { Directives and conditionals for mode macpas: }
  3151. AddDirective('SETC',directive_mac, @dir_setc);
  3152. AddDirective('DEFINEC',directive_mac, @dir_define);
  3153. AddDirective('UNDEFC',directive_mac, @dir_undef);
  3154. AddConditional('IFC',directive_mac, @dir_if);
  3155. AddConditional('ELSEC',directive_mac, @dir_else);
  3156. AddConditional('ELIFC',directive_mac, @dir_elseif);
  3157. AddConditional('ENDC',directive_mac, @dir_endif);
  3158. end;
  3159. procedure DoneScanner;
  3160. begin
  3161. turbo_scannerdirectives.Free;
  3162. mac_scannerdirectives.Free;
  3163. DoneWideString(patternw);
  3164. end;
  3165. end.