scanner.pas 130 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082
  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,constexp,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 : TIDString;
  39. line_nb : longint;
  40. owner : tscannerfile;
  41. constructor Create(atyp:preproctyp;a:boolean;n:tpreprocstack);
  42. end;
  43. tdirectiveproc=procedure;
  44. tdirectiveitem = class(TFPHashObject)
  45. public
  46. is_conditional : boolean;
  47. proc : tdirectiveproc;
  48. constructor Create(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  49. constructor CreateCond(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  50. end;
  51. tcompile_time_predicate = function(var valuedescr: String) : Boolean;
  52. tspecialgenerictoken = (ST_LOADSETTINGS,ST_LINE,ST_COLUMN,ST_FILEINDEX);
  53. tscannerfile = class
  54. public
  55. inputfile : tinputfile; { current inputfile list }
  56. inputfilecount : longint;
  57. inputbuffer, { input buffer }
  58. inputpointer : pchar;
  59. inputstart : longint;
  60. line_no, { line }
  61. lastlinepos : longint;
  62. lasttokenpos : longint; { token }
  63. lasttoken,
  64. nexttoken : ttoken;
  65. replaysavetoken : ttoken;
  66. replaytokenbuf,
  67. recordtokenbuf : tdynamicarray;
  68. { old settings, i.e. settings specialization was started }
  69. old_settings,
  70. { last settings we stored }
  71. last_settings : tsettings;
  72. { last filepos we stored }
  73. last_filepos : tfileposinfo;
  74. comment_level,
  75. yylexcount : longint;
  76. lastasmgetchar : char;
  77. ignoredirectives : TFPHashList; { ignore directives, used to give warnings only once }
  78. preprocstack : tpreprocstack;
  79. in_asm_string : boolean;
  80. preproc_pattern : string;
  81. preproc_token : ttoken;
  82. constructor Create(const fn:string);
  83. destructor Destroy;override;
  84. { File buffer things }
  85. function openinputfile:boolean;
  86. procedure closeinputfile;
  87. function tempopeninputfile:boolean;
  88. procedure tempcloseinputfile;
  89. procedure saveinputfile;
  90. procedure restoreinputfile;
  91. procedure firstfile;
  92. procedure nextfile;
  93. procedure addfile(hp:tinputfile);
  94. procedure reload;
  95. procedure insertmacro(const macname:string;p:pchar;len,line,fileindex:longint);
  96. { Scanner things }
  97. procedure gettokenpos;
  98. procedure inc_comment_level;
  99. procedure dec_comment_level;
  100. procedure illegal_char(c:char);
  101. procedure end_of_file;
  102. procedure checkpreprocstack;
  103. procedure poppreprocstack;
  104. procedure ifpreprocstack(atyp : preproctyp;compile_time_predicate:tcompile_time_predicate;messid:longint);
  105. procedure elseifpreprocstack(compile_time_predicate:tcompile_time_predicate);
  106. procedure elsepreprocstack;
  107. procedure handleconditional(p:tdirectiveitem);
  108. procedure handledirectives;
  109. procedure linebreak;
  110. procedure recordtoken;
  111. procedure startrecordtokens(buf:tdynamicarray);
  112. procedure stoprecordtokens;
  113. procedure replaytoken;
  114. procedure startreplaytokens(buf:tdynamicarray);
  115. procedure readchar;
  116. procedure readstring;
  117. procedure readnumber;
  118. function readid:string;
  119. function readval:longint;
  120. function readval_asstring:string;
  121. function readcomment:string;
  122. function readquotedstring:string;
  123. function readstate:char;
  124. function readstatedefault:char;
  125. procedure skipspace;
  126. procedure skipuntildirective;
  127. procedure skipcomment;
  128. procedure skipdelphicomment;
  129. procedure skipoldtpcomment;
  130. procedure readtoken(allowrecordtoken:boolean);
  131. function readpreproc:ttoken;
  132. function asmgetcharstart : char;
  133. function asmgetchar:char;
  134. end;
  135. {$ifdef PREPROCWRITE}
  136. tpreprocfile=class
  137. f : text;
  138. buf : pointer;
  139. spacefound,
  140. eolfound : boolean;
  141. constructor create(const fn:string);
  142. destructor destroy;
  143. procedure Add(const s:string);
  144. procedure AddSpace;
  145. end;
  146. {$endif PREPROCWRITE}
  147. var
  148. { read strings }
  149. c : char;
  150. orgpattern,
  151. pattern : string;
  152. patternw : pcompilerwidestring;
  153. { token }
  154. token, { current token being parsed }
  155. idtoken : ttoken; { holds the token if the pattern is a known word }
  156. current_scanner : tscannerfile; { current scanner in use }
  157. aktcommentstyle : tcommentstyle; { needed to use read_comment from directives }
  158. {$ifdef PREPROCWRITE}
  159. preprocfile : tpreprocfile; { used with only preprocessing }
  160. {$endif PREPROCWRITE}
  161. type
  162. tdirectivemode = (directive_all, directive_turbo, directive_mac);
  163. procedure AddDirective(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  164. procedure AddConditional(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  165. procedure InitScanner;
  166. procedure DoneScanner;
  167. {To be called when the language mode is finally determined}
  168. Function SetCompileMode(const s:string; changeInit: boolean):boolean;
  169. implementation
  170. uses
  171. SysUtils,
  172. cutils,cfileutl,
  173. systems,
  174. switches,
  175. symbase,symtable,symtype,symsym,symconst,symdef,defutil,
  176. fmodule;
  177. var
  178. { dictionaries with the supported directives }
  179. turbo_scannerdirectives : TFPHashObjectList; { for other modes }
  180. mac_scannerdirectives : TFPHashObjectList; { for mode mac }
  181. {*****************************************************************************
  182. Helper routines
  183. *****************************************************************************}
  184. const
  185. { use any special name that is an invalid file name to avoid problems }
  186. preprocstring : array [preproctyp] of string[7]
  187. = ('$IFDEF','$IFNDEF','$IF','$IFOPT','$ELSE','$ELSEIF');
  188. function is_keyword(const s:string):boolean;
  189. var
  190. low,high,mid : longint;
  191. begin
  192. if not (length(s) in [tokenlenmin..tokenlenmax]) or
  193. not (s[1] in ['a'..'z','A'..'Z']) then
  194. begin
  195. is_keyword:=false;
  196. exit;
  197. end;
  198. low:=ord(tokenidx^[length(s),s[1]].first);
  199. high:=ord(tokenidx^[length(s),s[1]].last);
  200. while low<high do
  201. begin
  202. mid:=(high+low+1) shr 1;
  203. if pattern<tokeninfo^[ttoken(mid)].str then
  204. high:=mid-1
  205. else
  206. low:=mid;
  207. end;
  208. is_keyword:=(pattern=tokeninfo^[ttoken(high)].str) and
  209. (tokeninfo^[ttoken(high)].keyword in current_settings.modeswitches);
  210. end;
  211. Function SetCompileMode(const s:string; changeInit: boolean):boolean;
  212. var
  213. b : boolean;
  214. oldmodeswitches : tmodeswitches;
  215. begin
  216. oldmodeswitches:=current_settings.modeswitches;
  217. b:=true;
  218. if s='DEFAULT' then
  219. current_settings.modeswitches:=fpcmodeswitches
  220. else
  221. if s='DELPHI' then
  222. current_settings.modeswitches:=delphimodeswitches
  223. else
  224. if s='TP' then
  225. current_settings.modeswitches:=tpmodeswitches
  226. else
  227. if s='FPC' then begin
  228. current_settings.modeswitches:=fpcmodeswitches;
  229. { TODO: enable this for 2.3/2.9 }
  230. // include(current_settings.localswitches, cs_typed_addresses);
  231. end else
  232. if s='OBJFPC' then begin
  233. current_settings.modeswitches:=objfpcmodeswitches;
  234. { TODO: enable this for 2.3/2.9 }
  235. // include(current_settings.localswitches, cs_typed_addresses);
  236. end
  237. {$ifdef gpc_mode}
  238. else if s='GPC' then
  239. current_settings.modeswitches:=gpcmodeswitches
  240. {$endif}
  241. else
  242. if s='MACPAS' then
  243. current_settings.modeswitches:=macmodeswitches
  244. else
  245. b:=false;
  246. if b and changeInit then
  247. init_settings.modeswitches := current_settings.modeswitches;
  248. if b then
  249. begin
  250. { resolve all postponed switch changes }
  251. if localswitcheschanged then
  252. begin
  253. current_settings.localswitches:=nextlocalswitches;
  254. localswitcheschanged:=false;
  255. end;
  256. { turn ansistrings on by default ? }
  257. if (m_default_ansistring in current_settings.modeswitches) then
  258. begin
  259. include(current_settings.localswitches,cs_ansistrings);
  260. if changeinit then
  261. include(init_settings.localswitches,cs_ansistrings);
  262. end
  263. else
  264. begin
  265. exclude(current_settings.localswitches,cs_ansistrings);
  266. if changeinit then
  267. exclude(init_settings.localswitches,cs_ansistrings);
  268. end;
  269. { turn inline on by default ? }
  270. if (m_default_inline in current_settings.modeswitches) then
  271. begin
  272. include(current_settings.localswitches,cs_do_inline);
  273. if changeinit then
  274. include(init_settings.localswitches,cs_do_inline);
  275. end
  276. else
  277. begin
  278. exclude(current_settings.localswitches,cs_ansistrings);
  279. if changeinit then
  280. exclude(init_settings.localswitches,cs_ansistrings);
  281. end;
  282. { turn on bitpacking for mode macpas }
  283. if (m_mac in current_settings.modeswitches) then
  284. begin
  285. include(current_settings.localswitches,cs_bitpacking);
  286. if changeinit then
  287. include(init_settings.localswitches,cs_bitpacking);
  288. end;
  289. { support goto/label by default in delphi/tp7/mac modes }
  290. if ([m_delphi,m_tp7,m_mac] * current_settings.modeswitches <> []) then
  291. begin
  292. include(current_settings.moduleswitches,cs_support_goto);
  293. if changeinit then
  294. include(init_settings.moduleswitches,cs_support_goto);
  295. end;
  296. { Default enum packing for delphi/tp7 }
  297. if (m_tp7 in current_settings.modeswitches) or
  298. (m_delphi in current_settings.modeswitches) then
  299. current_settings.packenum:=1
  300. else if (m_mac in current_settings.modeswitches) then
  301. { compatible with Metrowerks Pascal }
  302. current_settings.packenum:=2
  303. else
  304. current_settings.packenum:=4;
  305. if changeinit then
  306. init_settings.packenum:=current_settings.packenum;
  307. {$ifdef i386}
  308. { Default to intel assembler for delphi/tp7 on i386 }
  309. if (m_delphi in current_settings.modeswitches) or
  310. (m_tp7 in current_settings.modeswitches) then
  311. current_settings.asmmode:=asmmode_i386_intel;
  312. if changeinit then
  313. init_settings.asmmode:=current_settings.asmmode;
  314. {$endif i386}
  315. { Exception support explicitly turned on (mainly for macpas, to }
  316. { compensate for lack of interprocedural goto support) }
  317. if (cs_support_exceptions in current_settings.globalswitches) then
  318. include(current_settings.modeswitches,m_except);
  319. { Undefine old symbol }
  320. if (m_delphi in oldmodeswitches) then
  321. undef_system_macro('FPC_DELPHI')
  322. else if (m_tp7 in oldmodeswitches) then
  323. undef_system_macro('FPC_TP')
  324. else if (m_objfpc in oldmodeswitches) then
  325. undef_system_macro('FPC_OBJFPC')
  326. {$ifdef gpc_mode}
  327. else if (m_gpc in oldmodeswitches) then
  328. undef_system_macro('FPC_GPC')
  329. {$endif}
  330. else if (m_mac in oldmodeswitches) then
  331. undef_system_macro('FPC_MACPAS');
  332. { define new symbol in delphi,objfpc,tp,gpc,macpas mode }
  333. if (m_delphi in current_settings.modeswitches) then
  334. def_system_macro('FPC_DELPHI')
  335. else if (m_tp7 in current_settings.modeswitches) then
  336. def_system_macro('FPC_TP')
  337. else if (m_objfpc in current_settings.modeswitches) then
  338. def_system_macro('FPC_OBJFPC')
  339. {$ifdef gpc_mode}
  340. else if (m_gpc in current_settings.modeswitches) then
  341. def_system_macro('FPC_GPC')
  342. {$endif}
  343. else if (m_mac in current_settings.modeswitches) then
  344. def_system_macro('FPC_MACPAS');
  345. end;
  346. SetCompileMode:=b;
  347. end;
  348. {*****************************************************************************
  349. Conditional Directives
  350. *****************************************************************************}
  351. procedure dir_else;
  352. begin
  353. current_scanner.elsepreprocstack;
  354. end;
  355. procedure dir_endif;
  356. begin
  357. current_scanner.poppreprocstack;
  358. end;
  359. function isdef(var valuedescr: String): Boolean;
  360. var
  361. hs : string;
  362. mac : tmacro;
  363. begin
  364. current_scanner.skipspace;
  365. hs:=current_scanner.readid;
  366. valuedescr:= hs;
  367. if hs='' then
  368. Message(scan_e_error_in_preproc_expr);
  369. mac:=tmacro(search_macro(hs));
  370. if assigned(mac) then
  371. mac.is_used:=true;
  372. isdef:= assigned(mac) and mac.defined;
  373. end;
  374. procedure dir_ifdef;
  375. begin
  376. current_scanner.ifpreprocstack(pp_ifdef,@isdef,scan_c_ifdef_found);
  377. end;
  378. function isnotdef(var valuedescr: String): Boolean;
  379. var
  380. hs : string;
  381. mac : tmacro;
  382. begin
  383. current_scanner.skipspace;
  384. hs:=current_scanner.readid;
  385. valuedescr:= hs;
  386. if hs='' then
  387. Message(scan_e_error_in_preproc_expr);
  388. mac:=tmacro(search_macro(hs));
  389. if assigned(mac) then
  390. mac.is_used:=true;
  391. isnotdef:= not (assigned(mac) and mac.defined);
  392. end;
  393. procedure dir_ifndef;
  394. begin
  395. current_scanner.ifpreprocstack(pp_ifndef,@isnotdef,scan_c_ifndef_found);
  396. end;
  397. function opt_check(var valuedescr: String): Boolean;
  398. var
  399. hs : string;
  400. state : char;
  401. begin
  402. opt_check:= false;
  403. current_scanner.skipspace;
  404. hs:=current_scanner.readid;
  405. valuedescr:= hs;
  406. if (length(hs)>1) then
  407. Message1(scan_w_illegal_switch,hs)
  408. else
  409. begin
  410. state:=current_scanner.ReadState;
  411. if state in ['-','+'] then
  412. opt_check:=CheckSwitch(hs[1],state)
  413. else
  414. Message(scan_e_error_in_preproc_expr);
  415. end;
  416. end;
  417. procedure dir_ifopt;
  418. begin
  419. current_scanner.ifpreprocstack(pp_ifopt,@opt_check,scan_c_ifopt_found);
  420. end;
  421. procedure dir_libprefix;
  422. var
  423. s : string;
  424. begin
  425. current_scanner.skipspace;
  426. if c <> '''' then
  427. Message2(scan_f_syn_expected, '''', c);
  428. s := current_scanner.readquotedstring;
  429. stringdispose(outputprefix);
  430. outputprefix := stringdup(s);
  431. with current_module do
  432. setfilename(paramfn^, paramallowoutput);
  433. end;
  434. procedure dir_libsuffix;
  435. var
  436. s : string;
  437. begin
  438. current_scanner.skipspace;
  439. if c <> '''' then
  440. Message2(scan_f_syn_expected, '''', c);
  441. s := current_scanner.readquotedstring;
  442. stringdispose(outputsuffix);
  443. outputsuffix := stringdup(s);
  444. with current_module do
  445. setfilename(paramfn^, paramallowoutput);
  446. end;
  447. procedure dir_extension;
  448. var
  449. s : string;
  450. begin
  451. current_scanner.skipspace;
  452. if c <> '''' then
  453. Message2(scan_f_syn_expected, '''', c);
  454. s := current_scanner.readquotedstring;
  455. if OutputFileName='' then
  456. OutputFileName:=InputFileName;
  457. OutputFileName:=ChangeFileExt(OutputFileName,'.'+s);
  458. with current_module do
  459. setfilename(paramfn^, paramallowoutput);
  460. end;
  461. {
  462. Compile time expression type check
  463. ----------------------------------
  464. Each subexpression returns its type to the caller, which then can
  465. do type check. Since data types of compile time expressions is
  466. not well defined, the type system does a best effort. The drawback is
  467. that some errors might not be detected.
  468. Instead of returning a particular data type, a set of possible data types
  469. are returned. This way ambigouos types can be handled. For instance a
  470. value of 1 can be both a boolean and and integer.
  471. Booleans
  472. --------
  473. The following forms of boolean values are supported:
  474. * C coded, that is 0 is false, non-zero is true.
  475. * TRUE/FALSE for mac style compile time variables
  476. Thus boolean mac compile time variables are always stored as TRUE/FALSE.
  477. When a compile time expression is evaluated, they are then translated
  478. to C coded booleans (0/1), to simplify for the expression evaluator.
  479. Note that this scheme then also of support mac compile time variables which
  480. are 0/1 but with a boolean meaning.
  481. The TRUE/FALSE format is new from 22 august 2005, but the above scheme
  482. means that units which is not recompiled, and thus stores
  483. compile time variables as the old format (0/1), continue to work.
  484. Short circuit evaluation
  485. ------------------------
  486. For this to work, the part of a compile time expression which is short
  487. circuited, should not be evaluated, while it still should be parsed.
  488. Therefor there is a parameter eval, telling whether evaluation is needed.
  489. In case not, the value returned can be arbitrary.
  490. }
  491. type
  492. {Compile time expression types}
  493. TCTEType = (ctetBoolean, ctetInteger, ctetString, ctetSet);
  494. TCTETypeSet = set of TCTEType;
  495. const
  496. cteTypeNames : array[TCTEType] of string[10] = (
  497. 'BOOLEAN','INTEGER','STRING','SET');
  498. {Subset of types which can be elements in sets.}
  499. setelementdefs = [ctetBoolean, ctetInteger, ctetString];
  500. function GetCTETypeName(t: TCTETypeSet): String;
  501. var
  502. i: TCTEType;
  503. begin
  504. result:= '';
  505. for i:= Low(TCTEType) to High(TCTEType) do
  506. if i in t then
  507. if result = '' then
  508. result:= cteTypeNames[i]
  509. else
  510. result:= result + ' or ' + cteTypeNames[i];
  511. end;
  512. procedure CTEError(actType, desiredExprType: TCTETypeSet; place: String);
  513. begin
  514. Message3(scan_e_compile_time_typeerror,
  515. GetCTETypeName(desiredExprType),
  516. GetCTETypeName(actType),
  517. place
  518. );
  519. end;
  520. function parse_compiler_expr(var compileExprType: TCTETypeSet):string;
  521. function read_expr(var exprType: TCTETypeSet; eval : Boolean) : string; forward;
  522. procedure preproc_consume(t : ttoken);
  523. begin
  524. if t<>current_scanner.preproc_token then
  525. Message(scan_e_preproc_syntax_error);
  526. current_scanner.preproc_token:=current_scanner.readpreproc;
  527. end;
  528. function preproc_substitutedtoken(var macroType: TCTETypeSet; eval : Boolean): string;
  529. { Currently this parses identifiers as well as numbers.
  530. The result from this procedure can either be that the token
  531. itself is a value, or that it is a compile time variable/macro,
  532. which then is substituted for another value (for macros
  533. recursivelly substituted).}
  534. var
  535. hs: string;
  536. mac : tmacro;
  537. macrocount,
  538. len : integer;
  539. numres : longint;
  540. w: word;
  541. begin
  542. result := current_scanner.preproc_pattern;
  543. if not eval then
  544. exit;
  545. mac:= nil;
  546. { Substitue macros and compiler variables with their content/value.
  547. For real macros also do recursive substitution. }
  548. macrocount:=0;
  549. repeat
  550. mac:=tmacro(search_macro(result));
  551. inc(macrocount);
  552. if macrocount>max_macro_nesting then
  553. begin
  554. Message(scan_w_macro_too_deep);
  555. break;
  556. end;
  557. if assigned(mac) and mac.defined then
  558. if assigned(mac.buftext) then
  559. begin
  560. if mac.buflen>255 then
  561. begin
  562. len:=255;
  563. Message(scan_w_macro_cut_after_255_chars);
  564. end
  565. else
  566. len:=mac.buflen;
  567. hs[0]:=char(len);
  568. move(mac.buftext^,hs[1],len);
  569. result:=upcase(hs);
  570. mac.is_used:=true;
  571. end
  572. else
  573. begin
  574. Message1(scan_e_error_macro_lacks_value, result);
  575. break;
  576. end
  577. else
  578. begin
  579. break;
  580. end;
  581. if mac.is_compiler_var then
  582. break;
  583. until false;
  584. { At this point, result do contain the value. Do some decoding and
  585. determine the type.}
  586. val(result,numres,w);
  587. if (w=0) then {It is an integer}
  588. begin
  589. if (numres = 0) or (numres = 1) then
  590. macroType := [ctetInteger, ctetBoolean]
  591. else
  592. macroType := [ctetInteger];
  593. end
  594. else if assigned(mac) and (m_mac in current_settings.modeswitches) and (result='FALSE') then
  595. begin
  596. result:= '0';
  597. macroType:= [ctetBoolean];
  598. end
  599. else if assigned(mac) and (m_mac in current_settings.modeswitches) and (result='TRUE') then
  600. begin
  601. result:= '1';
  602. macroType:= [ctetBoolean];
  603. end
  604. else if (m_mac in current_settings.modeswitches) and
  605. (not assigned(mac) or not mac.defined) and
  606. (macrocount = 1) then
  607. begin
  608. {Errors in mode mac is issued here. For non macpas modes there is
  609. more liberty, but the error will eventually be caught at a later stage.}
  610. Message1(scan_e_error_macro_undefined, result);
  611. macroType:= [ctetString]; {Just to have something}
  612. end
  613. else
  614. macroType:= [ctetString];
  615. end;
  616. function read_factor(var factorType: TCTETypeSet; eval : Boolean) : string;
  617. var
  618. hs : string;
  619. mac: tmacro;
  620. srsym : tsym;
  621. srsymtable : TSymtable;
  622. l : longint;
  623. w : integer;
  624. hasKlammer: Boolean;
  625. setElemType : TCTETypeSet;
  626. begin
  627. if current_scanner.preproc_token=_ID then
  628. begin
  629. if current_scanner.preproc_pattern='DEFINED' then
  630. begin
  631. factorType:= [ctetBoolean];
  632. preproc_consume(_ID);
  633. current_scanner.skipspace;
  634. if current_scanner.preproc_token =_LKLAMMER then
  635. begin
  636. preproc_consume(_LKLAMMER);
  637. current_scanner.skipspace;
  638. hasKlammer:= true;
  639. end
  640. else if (m_mac in current_settings.modeswitches) then
  641. hasKlammer:= false
  642. else
  643. Message(scan_e_error_in_preproc_expr);
  644. if current_scanner.preproc_token =_ID then
  645. begin
  646. hs := current_scanner.preproc_pattern;
  647. mac := tmacro(search_macro(hs));
  648. if assigned(mac) and mac.defined then
  649. begin
  650. hs := '1';
  651. mac.is_used:=true;
  652. end
  653. else
  654. hs := '0';
  655. read_factor := hs;
  656. preproc_consume(_ID);
  657. current_scanner.skipspace;
  658. end
  659. else
  660. Message(scan_e_error_in_preproc_expr);
  661. if hasKlammer then
  662. if current_scanner.preproc_token =_RKLAMMER then
  663. preproc_consume(_RKLAMMER)
  664. else
  665. Message(scan_e_error_in_preproc_expr);
  666. end
  667. else
  668. if (m_mac in current_settings.modeswitches) and (current_scanner.preproc_pattern='UNDEFINED') then
  669. begin
  670. factorType:= [ctetBoolean];
  671. preproc_consume(_ID);
  672. current_scanner.skipspace;
  673. if current_scanner.preproc_token =_ID then
  674. begin
  675. hs := current_scanner.preproc_pattern;
  676. mac := tmacro(search_macro(hs));
  677. if assigned(mac) then
  678. begin
  679. hs := '0';
  680. mac.is_used:=true;
  681. end
  682. else
  683. hs := '1';
  684. read_factor := hs;
  685. preproc_consume(_ID);
  686. current_scanner.skipspace;
  687. end
  688. else
  689. Message(scan_e_error_in_preproc_expr);
  690. end
  691. else
  692. if (m_mac in current_settings.modeswitches) and (current_scanner.preproc_pattern='OPTION') then
  693. begin
  694. factorType:= [ctetBoolean];
  695. preproc_consume(_ID);
  696. current_scanner.skipspace;
  697. if current_scanner.preproc_token =_LKLAMMER then
  698. begin
  699. preproc_consume(_LKLAMMER);
  700. current_scanner.skipspace;
  701. end
  702. else
  703. Message(scan_e_error_in_preproc_expr);
  704. if not (current_scanner.preproc_token = _ID) then
  705. Message(scan_e_error_in_preproc_expr);
  706. hs:=current_scanner.preproc_pattern;
  707. if (length(hs) > 1) then
  708. {This is allowed in Metrowerks Pascal}
  709. Message(scan_e_error_in_preproc_expr)
  710. else
  711. begin
  712. if CheckSwitch(hs[1],'+') then
  713. read_factor := '1'
  714. else
  715. read_factor := '0';
  716. end;
  717. preproc_consume(_ID);
  718. current_scanner.skipspace;
  719. if current_scanner.preproc_token =_RKLAMMER then
  720. preproc_consume(_RKLAMMER)
  721. else
  722. Message(scan_e_error_in_preproc_expr);
  723. end
  724. else
  725. if current_scanner.preproc_pattern='SIZEOF' then
  726. begin
  727. factorType:= [ctetInteger];
  728. preproc_consume(_ID);
  729. current_scanner.skipspace;
  730. if current_scanner.preproc_token =_LKLAMMER then
  731. begin
  732. preproc_consume(_LKLAMMER);
  733. current_scanner.skipspace;
  734. end
  735. else
  736. Message(scan_e_preproc_syntax_error);
  737. if eval then
  738. if searchsym(current_scanner.preproc_pattern,srsym,srsymtable) then
  739. begin
  740. l:=0;
  741. case srsym.typ of
  742. staticvarsym,
  743. localvarsym,
  744. paravarsym :
  745. l:=tabstractvarsym(srsym).getsize;
  746. typesym:
  747. l:=ttypesym(srsym).typedef.size;
  748. else
  749. Message(scan_e_error_in_preproc_expr);
  750. end;
  751. str(l,read_factor);
  752. end
  753. else
  754. Message1(sym_e_id_not_found,current_scanner.preproc_pattern);
  755. preproc_consume(_ID);
  756. current_scanner.skipspace;
  757. if current_scanner.preproc_token =_RKLAMMER then
  758. preproc_consume(_RKLAMMER)
  759. else
  760. Message(scan_e_preproc_syntax_error);
  761. end
  762. else
  763. if current_scanner.preproc_pattern='DECLARED' then
  764. begin
  765. factorType:= [ctetBoolean];
  766. preproc_consume(_ID);
  767. current_scanner.skipspace;
  768. if current_scanner.preproc_token =_LKLAMMER then
  769. begin
  770. preproc_consume(_LKLAMMER);
  771. current_scanner.skipspace;
  772. end
  773. else
  774. Message(scan_e_error_in_preproc_expr);
  775. if current_scanner.preproc_token =_ID then
  776. begin
  777. hs := upper(current_scanner.preproc_pattern);
  778. if searchsym(hs,srsym,srsymtable) then
  779. hs := '1'
  780. else
  781. hs := '0';
  782. read_factor := hs;
  783. preproc_consume(_ID);
  784. current_scanner.skipspace;
  785. end
  786. else
  787. Message(scan_e_error_in_preproc_expr);
  788. if current_scanner.preproc_token =_RKLAMMER then
  789. preproc_consume(_RKLAMMER)
  790. else
  791. Message(scan_e_error_in_preproc_expr);
  792. end
  793. else
  794. if current_scanner.preproc_pattern='NOT' then
  795. begin
  796. factorType:= [ctetBoolean];
  797. preproc_consume(_ID);
  798. hs:=read_factor(factorType, eval);
  799. if eval then
  800. begin
  801. if not (ctetBoolean in factorType) then
  802. CTEError(factorType, [ctetBoolean], 'NOT');
  803. val(hs,l,w);
  804. if l<>0 then
  805. read_factor:='0'
  806. else
  807. read_factor:='1';
  808. end
  809. else
  810. read_factor:='0'; {Just to have something}
  811. end
  812. else
  813. if (m_mac in current_settings.modeswitches) and (current_scanner.preproc_pattern='TRUE') then
  814. begin
  815. factorType:= [ctetBoolean];
  816. preproc_consume(_ID);
  817. read_factor:='1';
  818. end
  819. else
  820. if (m_mac in current_settings.modeswitches) and (current_scanner.preproc_pattern='FALSE') then
  821. begin
  822. factorType:= [ctetBoolean];
  823. preproc_consume(_ID);
  824. read_factor:='0';
  825. end
  826. else
  827. begin
  828. hs:=preproc_substitutedtoken(factorType, eval);
  829. { Default is to return the original symbol }
  830. read_factor:=hs;
  831. if eval and (m_delphi in current_settings.modeswitches) and (ctetString in factorType) then
  832. if searchsym(current_scanner.preproc_pattern,srsym,srsymtable) then
  833. begin
  834. case srsym.typ of
  835. constsym :
  836. begin
  837. with tconstsym(srsym) do
  838. begin
  839. case consttyp of
  840. constord :
  841. begin
  842. case constdef.typ of
  843. orddef:
  844. begin
  845. if is_integer(constdef) then
  846. begin
  847. read_factor:=tostr(value.valueord);
  848. factorType:= [ctetInteger];
  849. end
  850. else if is_boolean(constdef) then
  851. begin
  852. read_factor:=tostr(value.valueord);
  853. factorType:= [ctetBoolean];
  854. end
  855. else if is_char(constdef) then
  856. begin
  857. read_factor:=char(qword(value.valueord));
  858. factorType:= [ctetString];
  859. end
  860. end;
  861. enumdef:
  862. begin
  863. read_factor:=tostr(value.valueord);
  864. factorType:= [ctetInteger];
  865. end;
  866. end;
  867. end;
  868. conststring :
  869. begin
  870. read_factor := upper(pchar(value.valueptr));
  871. factorType:= [ctetString];
  872. end;
  873. constset :
  874. begin
  875. hs:=',';
  876. for l:=0 to 255 do
  877. if l in pconstset(tconstsym(srsym).value.valueptr)^ then
  878. hs:=hs+tostr(l)+',';
  879. read_factor := hs;
  880. factorType:= [ctetSet];
  881. end;
  882. end;
  883. end;
  884. end;
  885. enumsym :
  886. begin
  887. read_factor:=tostr(tenumsym(srsym).value);
  888. factorType:= [ctetInteger];
  889. end;
  890. end;
  891. end;
  892. preproc_consume(_ID);
  893. current_scanner.skipspace;
  894. end
  895. end
  896. else if current_scanner.preproc_token =_LKLAMMER then
  897. begin
  898. preproc_consume(_LKLAMMER);
  899. read_factor:=read_expr(factorType, eval);
  900. preproc_consume(_RKLAMMER);
  901. end
  902. else if current_scanner.preproc_token = _LECKKLAMMER then
  903. begin
  904. preproc_consume(_LECKKLAMMER);
  905. read_factor := ',';
  906. while current_scanner.preproc_token = _ID do
  907. begin
  908. read_factor := read_factor+read_factor(setElemType, eval)+',';
  909. if current_scanner.preproc_token = _COMMA then
  910. preproc_consume(_COMMA);
  911. end;
  912. // TODO Add check of setElemType
  913. preproc_consume(_RECKKLAMMER);
  914. factorType:= [ctetSet];
  915. end
  916. else
  917. Message(scan_e_error_in_preproc_expr);
  918. end;
  919. function read_term(var termType: TCTETypeSet; eval : Boolean) : string;
  920. var
  921. hs1,hs2 : string;
  922. l1,l2 : longint;
  923. w : integer;
  924. termType2: TCTETypeSet;
  925. begin
  926. hs1:=read_factor(termType, eval);
  927. repeat
  928. if (current_scanner.preproc_token<>_ID) then
  929. break;
  930. if current_scanner.preproc_pattern<>'AND' then
  931. break;
  932. val(hs1,l1,w);
  933. if l1=0 then
  934. eval:= false; {Short circuit evaluation of OR}
  935. if eval then
  936. begin
  937. {Check if first expr is boolean. Must be done here, after we know
  938. it is an AND expression.}
  939. if not (ctetBoolean in termType) then
  940. CTEError(termType, [ctetBoolean], 'AND');
  941. termType:= [ctetBoolean];
  942. end;
  943. preproc_consume(_ID);
  944. hs2:=read_factor(termType2, eval);
  945. if eval then
  946. begin
  947. if not (ctetBoolean in termType2) then
  948. CTEError(termType2, [ctetBoolean], 'AND');
  949. val(hs2,l2,w);
  950. if (l1<>0) and (l2<>0) then
  951. hs1:='1'
  952. else
  953. hs1:='0';
  954. end;
  955. until false;
  956. read_term:=hs1;
  957. end;
  958. function read_simple_expr(var simpleExprType: TCTETypeSet; eval : Boolean) : string;
  959. var
  960. hs1,hs2 : string;
  961. l1,l2 : longint;
  962. w : integer;
  963. simpleExprType2: TCTETypeSet;
  964. begin
  965. hs1:=read_term(simpleExprType, eval);
  966. repeat
  967. if (current_scanner.preproc_token<>_ID) then
  968. break;
  969. if current_scanner.preproc_pattern<>'OR' then
  970. break;
  971. val(hs1,l1,w);
  972. if l1<>0 then
  973. eval:= false; {Short circuit evaluation of OR}
  974. if eval then
  975. begin
  976. {Check if first expr is boolean. Must be done here, after we know
  977. it is an OR expression.}
  978. if not (ctetBoolean in simpleExprType) then
  979. CTEError(simpleExprType, [ctetBoolean], 'OR');
  980. simpleExprType:= [ctetBoolean];
  981. end;
  982. preproc_consume(_ID);
  983. hs2:=read_term(simpleExprType2, eval);
  984. if eval then
  985. begin
  986. if not (ctetBoolean in simpleExprType2) then
  987. CTEError(simpleExprType2, [ctetBoolean], 'OR');
  988. val(hs2,l2,w);
  989. if (l1<>0) or (l2<>0) then
  990. hs1:='1'
  991. else
  992. hs1:='0';
  993. end;
  994. until false;
  995. read_simple_expr:=hs1;
  996. end;
  997. function read_expr(var exprType: TCTETypeSet; eval : Boolean) : string;
  998. var
  999. hs1,hs2 : string;
  1000. b : boolean;
  1001. op : ttoken;
  1002. w : integer;
  1003. l1,l2 : longint;
  1004. exprType2: TCTETypeSet;
  1005. begin
  1006. hs1:=read_simple_expr(exprType, eval);
  1007. op:=current_scanner.preproc_token;
  1008. if (op = _ID) and (current_scanner.preproc_pattern = 'IN') then
  1009. op := _IN;
  1010. if not (op in [_IN,_EQUAL,_UNEQUAL,_LT,_GT,_LTE,_GTE]) then
  1011. begin
  1012. read_expr:=hs1;
  1013. exit;
  1014. end;
  1015. if (op = _IN) then
  1016. preproc_consume(_ID)
  1017. else
  1018. preproc_consume(op);
  1019. hs2:=read_simple_expr(exprType2, eval);
  1020. if eval then
  1021. begin
  1022. if op = _IN then
  1023. begin
  1024. if exprType2 <> [ctetSet] then
  1025. CTEError(exprType2, [ctetSet], 'IN');
  1026. if exprType = [ctetSet] then
  1027. CTEError(exprType, setelementdefs, 'IN');
  1028. if is_number(hs1) and is_number(hs2) then
  1029. Message(scan_e_preproc_syntax_error)
  1030. else if hs2[1] = ',' then
  1031. b:=pos(','+hs1+',', hs2) > 0 { TODO For integer sets, perhaps check for numeric equivalence so that 0 = 00 }
  1032. else
  1033. Message(scan_e_preproc_syntax_error);
  1034. end
  1035. else
  1036. begin
  1037. if (exprType * exprType2) = [] then
  1038. CTEError(exprType2, exprType, tokeninfo^[op].str);
  1039. if is_number(hs1) and is_number(hs2) then
  1040. begin
  1041. val(hs1,l1,w);
  1042. val(hs2,l2,w);
  1043. case op of
  1044. _EQUAL :
  1045. b:=l1=l2;
  1046. _UNEQUAL :
  1047. b:=l1<>l2;
  1048. _LT :
  1049. b:=l1<l2;
  1050. _GT :
  1051. b:=l1>l2;
  1052. _GTE :
  1053. b:=l1>=l2;
  1054. _LTE :
  1055. b:=l1<=l2;
  1056. end;
  1057. end
  1058. else
  1059. begin
  1060. case op of
  1061. _EQUAL :
  1062. b:=hs1=hs2;
  1063. _UNEQUAL :
  1064. b:=hs1<>hs2;
  1065. _LT :
  1066. b:=hs1<hs2;
  1067. _GT :
  1068. b:=hs1>hs2;
  1069. _GTE :
  1070. b:=hs1>=hs2;
  1071. _LTE :
  1072. b:=hs1<=hs2;
  1073. end;
  1074. end;
  1075. end;
  1076. end
  1077. else
  1078. b:= false; {Just to have something}
  1079. if b then
  1080. read_expr:='1'
  1081. else
  1082. read_expr:='0';
  1083. exprType:= [ctetBoolean];
  1084. end;
  1085. begin
  1086. current_scanner.skipspace;
  1087. { start preproc expression scanner }
  1088. current_scanner.preproc_token:=current_scanner.readpreproc;
  1089. parse_compiler_expr:=read_expr(compileExprType, true);
  1090. end;
  1091. function boolean_compile_time_expr(var valuedescr: String): Boolean;
  1092. var
  1093. hs : string;
  1094. exprType: TCTETypeSet;
  1095. begin
  1096. hs:=parse_compiler_expr(exprType);
  1097. if (exprType * [ctetBoolean]) = [] then
  1098. CTEError(exprType, [ctetBoolean], 'IF or ELSEIF');
  1099. boolean_compile_time_expr:= hs <> '0';
  1100. valuedescr:= hs;
  1101. end;
  1102. procedure dir_if;
  1103. begin
  1104. current_scanner.ifpreprocstack(pp_if,@boolean_compile_time_expr, scan_c_if_found);
  1105. end;
  1106. procedure dir_elseif;
  1107. begin
  1108. current_scanner.elseifpreprocstack(@boolean_compile_time_expr);
  1109. end;
  1110. procedure dir_define_impl(macstyle: boolean);
  1111. var
  1112. hs : string;
  1113. bracketcount : longint;
  1114. mac : tmacro;
  1115. macropos : longint;
  1116. macrobuffer : pmacrobuffer;
  1117. begin
  1118. current_scanner.skipspace;
  1119. hs:=current_scanner.readid;
  1120. mac:=tmacro(search_macro(hs));
  1121. if not assigned(mac) or (mac.owner <> current_module.localmacrosymtable) then
  1122. begin
  1123. mac:=tmacro.create(hs);
  1124. mac.defined:=true;
  1125. current_module.localmacrosymtable.insert(mac);
  1126. end
  1127. else
  1128. begin
  1129. mac.defined:=true;
  1130. mac.is_compiler_var:=false;
  1131. { delete old definition }
  1132. if assigned(mac.buftext) then
  1133. begin
  1134. freemem(mac.buftext,mac.buflen);
  1135. mac.buftext:=nil;
  1136. end;
  1137. end;
  1138. Message1(parser_c_macro_defined,mac.name);
  1139. mac.is_used:=true;
  1140. if (cs_support_macro in current_settings.moduleswitches) then
  1141. begin
  1142. current_scanner.skipspace;
  1143. if not macstyle then
  1144. begin
  1145. { may be a macro? }
  1146. if c <> ':' then
  1147. exit;
  1148. current_scanner.readchar;
  1149. if c <> '=' then
  1150. exit;
  1151. current_scanner.readchar;
  1152. current_scanner.skipspace;
  1153. end;
  1154. { key words are never substituted }
  1155. if is_keyword(hs) then
  1156. Message(scan_e_keyword_cant_be_a_macro);
  1157. new(macrobuffer);
  1158. macropos:=0;
  1159. { parse macro, brackets are counted so it's possible
  1160. to have a $ifdef etc. in the macro }
  1161. bracketcount:=0;
  1162. repeat
  1163. case c of
  1164. '}' :
  1165. if (bracketcount=0) then
  1166. break
  1167. else
  1168. dec(bracketcount);
  1169. '{' :
  1170. inc(bracketcount);
  1171. #10,#13 :
  1172. current_scanner.linebreak;
  1173. #26 :
  1174. current_scanner.end_of_file;
  1175. end;
  1176. macrobuffer^[macropos]:=c;
  1177. inc(macropos);
  1178. if macropos>=maxmacrolen then
  1179. Message(scan_f_macro_buffer_overflow);
  1180. current_scanner.readchar;
  1181. until false;
  1182. { free buffer of macro ?}
  1183. if assigned(mac.buftext) then
  1184. freemem(mac.buftext,mac.buflen);
  1185. { get new mem }
  1186. getmem(mac.buftext,macropos);
  1187. mac.buflen:=macropos;
  1188. { copy the text }
  1189. move(macrobuffer^,mac.buftext^,macropos);
  1190. dispose(macrobuffer);
  1191. end
  1192. else
  1193. begin
  1194. { check if there is an assignment, then we need to give a
  1195. warning }
  1196. current_scanner.skipspace;
  1197. if c=':' then
  1198. begin
  1199. current_scanner.readchar;
  1200. if c='=' then
  1201. Message(scan_w_macro_support_turned_off);
  1202. end;
  1203. end;
  1204. end;
  1205. procedure dir_define;
  1206. begin
  1207. dir_define_impl(false);
  1208. end;
  1209. procedure dir_definec;
  1210. begin
  1211. dir_define_impl(true);
  1212. end;
  1213. procedure dir_setc;
  1214. var
  1215. hs : string;
  1216. mac : tmacro;
  1217. exprType: TCTETypeSet;
  1218. l : longint;
  1219. w : integer;
  1220. begin
  1221. current_scanner.skipspace;
  1222. hs:=current_scanner.readid;
  1223. mac:=tmacro(search_macro(hs));
  1224. if not assigned(mac) or
  1225. (mac.owner <> current_module.localmacrosymtable) then
  1226. begin
  1227. mac:=tmacro.create(hs);
  1228. mac.defined:=true;
  1229. mac.is_compiler_var:=true;
  1230. current_module.localmacrosymtable.insert(mac);
  1231. end
  1232. else
  1233. begin
  1234. mac.defined:=true;
  1235. mac.is_compiler_var:=true;
  1236. { delete old definition }
  1237. if assigned(mac.buftext) then
  1238. begin
  1239. freemem(mac.buftext,mac.buflen);
  1240. mac.buftext:=nil;
  1241. end;
  1242. end;
  1243. Message1(parser_c_macro_defined,mac.name);
  1244. mac.is_used:=true;
  1245. { key words are never substituted }
  1246. if is_keyword(hs) then
  1247. Message(scan_e_keyword_cant_be_a_macro);
  1248. { macro assignment can be both := and = }
  1249. current_scanner.skipspace;
  1250. if c=':' then
  1251. current_scanner.readchar;
  1252. if c='=' then
  1253. begin
  1254. current_scanner.readchar;
  1255. hs:= parse_compiler_expr(exprType);
  1256. if (exprType * [ctetBoolean, ctetInteger]) = [] then
  1257. CTEError(exprType, [ctetBoolean, ctetInteger], 'SETC');
  1258. if length(hs) <> 0 then
  1259. begin
  1260. {If we are absolutely shure it is boolean, translate
  1261. to TRUE/FALSE to increase possibility to do future type check}
  1262. if exprType = [ctetBoolean] then
  1263. begin
  1264. val(hs,l,w);
  1265. if l<>0 then
  1266. hs:='TRUE'
  1267. else
  1268. hs:='FALSE';
  1269. end;
  1270. Message2(parser_c_macro_set_to,mac.name,hs);
  1271. { free buffer of macro ?}
  1272. if assigned(mac.buftext) then
  1273. freemem(mac.buftext,mac.buflen);
  1274. { get new mem }
  1275. getmem(mac.buftext,length(hs));
  1276. mac.buflen:=length(hs);
  1277. { copy the text }
  1278. move(hs[1],mac.buftext^,mac.buflen);
  1279. end
  1280. else
  1281. Message(scan_e_preproc_syntax_error);
  1282. end
  1283. else
  1284. Message(scan_e_preproc_syntax_error);
  1285. end;
  1286. procedure dir_undef;
  1287. var
  1288. hs : string;
  1289. mac : tmacro;
  1290. begin
  1291. current_scanner.skipspace;
  1292. hs:=current_scanner.readid;
  1293. mac:=tmacro(search_macro(hs));
  1294. if not assigned(mac) or
  1295. (mac.owner <> current_module.localmacrosymtable) then
  1296. begin
  1297. mac:=tmacro.create(hs);
  1298. mac.defined:=false;
  1299. current_module.localmacrosymtable.insert(mac);
  1300. end
  1301. else
  1302. begin
  1303. mac.defined:=false;
  1304. mac.is_compiler_var:=false;
  1305. { delete old definition }
  1306. if assigned(mac.buftext) then
  1307. begin
  1308. freemem(mac.buftext,mac.buflen);
  1309. mac.buftext:=nil;
  1310. end;
  1311. end;
  1312. Message1(parser_c_macro_undefined,mac.name);
  1313. mac.is_used:=true;
  1314. end;
  1315. procedure dir_include;
  1316. function findincludefile(const path,name:TCmdStr;var foundfile:TCmdStr):boolean;
  1317. var
  1318. found : boolean;
  1319. hpath : TCmdStr;
  1320. begin
  1321. (* look for the include file
  1322. If path was specified as part of {$I } then
  1323. 1. specified path (expanded with path of inputfile if relative)
  1324. else
  1325. 1. path of current inputfile,current dir
  1326. 2. local includepath
  1327. 3. global includepath *)
  1328. found:=false;
  1329. foundfile:='';
  1330. hpath:='';
  1331. if path<>'' then
  1332. begin
  1333. if not path_absolute(path) then
  1334. hpath:=current_scanner.inputfile.path^+path
  1335. else
  1336. hpath:=path;
  1337. found:=FindFile(name, hpath,true,foundfile);
  1338. end
  1339. else
  1340. begin
  1341. hpath:=current_scanner.inputfile.path^+';'+CurDirRelPath(source_info);
  1342. found:=FindFile(name, hpath,true,foundfile);
  1343. if not found then
  1344. found:=current_module.localincludesearchpath.FindFile(name,true,foundfile);
  1345. if not found then
  1346. found:=includesearchpath.FindFile(name,true,foundfile);
  1347. end;
  1348. result:=found;
  1349. end;
  1350. var
  1351. foundfile : TCmdStr;
  1352. path,
  1353. name,
  1354. args,
  1355. hs : tpathstr;
  1356. hp : tinputfile;
  1357. found : boolean;
  1358. begin
  1359. current_scanner.skipspace;
  1360. args:=current_scanner.readcomment;
  1361. hs:=GetToken(args,' ');
  1362. if hs='' then
  1363. exit;
  1364. if (hs[1]='%') then
  1365. begin
  1366. { case insensitive }
  1367. hs:=upper(hs);
  1368. { remove %'s }
  1369. Delete(hs,1,1);
  1370. if hs[length(hs)]='%' then
  1371. Delete(hs,length(hs),1);
  1372. { save old }
  1373. path:=hs;
  1374. { first check for internal macros }
  1375. if hs='TIME' then
  1376. hs:=gettimestr
  1377. else
  1378. if hs='DATE' then
  1379. hs:=getdatestr
  1380. else
  1381. if hs='FILE' then
  1382. hs:=current_module.sourcefiles.get_file_name(current_filepos.fileindex)
  1383. else
  1384. if hs='LINE' then
  1385. hs:=tostr(current_filepos.line)
  1386. else
  1387. if hs='FPCVERSION' then
  1388. hs:=version_string
  1389. else
  1390. if hs='FPCDATE' then
  1391. hs:=date_string
  1392. else
  1393. if hs='FPCTARGET' then
  1394. hs:=target_cpu_string
  1395. else
  1396. if hs='FPCTARGETCPU' then
  1397. hs:=target_cpu_string
  1398. else
  1399. if hs='FPCTARGETOS' then
  1400. hs:=target_info.shortname
  1401. else
  1402. hs:=GetEnvironmentVariable(hs);
  1403. if hs='' then
  1404. Message1(scan_w_include_env_not_found,path);
  1405. { make it a stringconst }
  1406. hs:=''''+hs+'''';
  1407. current_scanner.insertmacro(path,@hs[1],length(hs),
  1408. current_scanner.line_no,current_scanner.inputfile.ref_index);
  1409. end
  1410. else
  1411. begin
  1412. hs:=FixFileName(hs);
  1413. path:=ExtractFilePath(hs);
  1414. name:=ExtractFileName(hs);
  1415. { try to find the file }
  1416. found:=findincludefile(path,name,foundfile);
  1417. if (ExtractFileExt(name)='') then
  1418. begin
  1419. { try default extensions .inc , .pp and .pas }
  1420. if (not found) then
  1421. found:=findincludefile(path,ChangeFileExt(name,'.inc'),foundfile);
  1422. if (not found) then
  1423. found:=findincludefile(path,ChangeFileExt(name,sourceext),foundfile);
  1424. if (not found) then
  1425. found:=findincludefile(path,ChangeFileExt(name,pasext),foundfile);
  1426. end;
  1427. if current_scanner.inputfilecount<max_include_nesting then
  1428. begin
  1429. inc(current_scanner.inputfilecount);
  1430. { we need to reread the current char }
  1431. dec(current_scanner.inputpointer);
  1432. { shutdown current file }
  1433. current_scanner.tempcloseinputfile;
  1434. { load new file }
  1435. hp:=do_openinputfile(foundfile);
  1436. current_scanner.addfile(hp);
  1437. current_module.sourcefiles.register_file(hp);
  1438. if (not found) then
  1439. Message1(scan_f_cannot_open_includefile,hs);
  1440. if (not current_scanner.openinputfile) then
  1441. Message1(scan_f_cannot_open_includefile,hs);
  1442. Message1(scan_t_start_include_file,current_scanner.inputfile.path^+current_scanner.inputfile.name^);
  1443. current_scanner.reload;
  1444. end
  1445. else
  1446. Message(scan_f_include_deep_ten);
  1447. end;
  1448. end;
  1449. {*****************************************************************************
  1450. Preprocessor writting
  1451. *****************************************************************************}
  1452. {$ifdef PREPROCWRITE}
  1453. constructor tpreprocfile.create(const fn:string);
  1454. begin
  1455. { open outputfile }
  1456. assign(f,fn);
  1457. {$I-}
  1458. rewrite(f);
  1459. {$I+}
  1460. if ioresult<>0 then
  1461. Comment(V_Fatal,'can''t create file '+fn);
  1462. getmem(buf,preprocbufsize);
  1463. settextbuf(f,buf^,preprocbufsize);
  1464. { reset }
  1465. eolfound:=false;
  1466. spacefound:=false;
  1467. end;
  1468. destructor tpreprocfile.destroy;
  1469. begin
  1470. close(f);
  1471. freemem(buf,preprocbufsize);
  1472. end;
  1473. procedure tpreprocfile.add(const s:string);
  1474. begin
  1475. write(f,s);
  1476. end;
  1477. procedure tpreprocfile.addspace;
  1478. begin
  1479. if eolfound then
  1480. begin
  1481. writeln(f,'');
  1482. eolfound:=false;
  1483. spacefound:=false;
  1484. end
  1485. else
  1486. if spacefound then
  1487. begin
  1488. write(f,' ');
  1489. spacefound:=false;
  1490. end;
  1491. end;
  1492. {$endif PREPROCWRITE}
  1493. {*****************************************************************************
  1494. TPreProcStack
  1495. *****************************************************************************}
  1496. constructor tpreprocstack.create(atyp : preproctyp;a:boolean;n:tpreprocstack);
  1497. begin
  1498. accept:=a;
  1499. typ:=atyp;
  1500. next:=n;
  1501. end;
  1502. {*****************************************************************************
  1503. TDirectiveItem
  1504. *****************************************************************************}
  1505. constructor TDirectiveItem.Create(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  1506. begin
  1507. inherited Create(AList,n);
  1508. is_conditional:=false;
  1509. proc:=p;
  1510. end;
  1511. constructor TDirectiveItem.CreateCond(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  1512. begin
  1513. inherited Create(AList,n);
  1514. is_conditional:=true;
  1515. proc:=p;
  1516. end;
  1517. {****************************************************************************
  1518. TSCANNERFILE
  1519. ****************************************************************************}
  1520. constructor tscannerfile.create(const fn:string);
  1521. begin
  1522. inputfile:=do_openinputfile(fn);
  1523. if assigned(current_module) then
  1524. current_module.sourcefiles.register_file(inputfile);
  1525. { reset localinput }
  1526. inputbuffer:=nil;
  1527. inputpointer:=nil;
  1528. inputstart:=0;
  1529. { reset scanner }
  1530. preprocstack:=nil;
  1531. comment_level:=0;
  1532. yylexcount:=0;
  1533. block_type:=bt_general;
  1534. line_no:=0;
  1535. lastlinepos:=0;
  1536. lasttokenpos:=0;
  1537. lasttoken:=NOTOKEN;
  1538. nexttoken:=NOTOKEN;
  1539. lastasmgetchar:=#0;
  1540. ignoredirectives:=TFPHashList.Create;
  1541. in_asm_string:=false;
  1542. end;
  1543. procedure tscannerfile.firstfile;
  1544. begin
  1545. { load block }
  1546. if not openinputfile then
  1547. Message1(scan_f_cannot_open_input,inputfile.name^);
  1548. reload;
  1549. end;
  1550. destructor tscannerfile.destroy;
  1551. begin
  1552. if assigned(current_module) and
  1553. (current_module.state=ms_compiled) and
  1554. (status.errorcount=0) then
  1555. checkpreprocstack
  1556. else
  1557. begin
  1558. while assigned(preprocstack) do
  1559. poppreprocstack;
  1560. end;
  1561. if not inputfile.closed then
  1562. closeinputfile;
  1563. ignoredirectives.free;
  1564. end;
  1565. function tscannerfile.openinputfile:boolean;
  1566. begin
  1567. openinputfile:=inputfile.open;
  1568. { load buffer }
  1569. inputbuffer:=inputfile.buf;
  1570. inputpointer:=inputfile.buf;
  1571. inputstart:=inputfile.bufstart;
  1572. { line }
  1573. line_no:=0;
  1574. lastlinepos:=0;
  1575. lasttokenpos:=0;
  1576. end;
  1577. procedure tscannerfile.closeinputfile;
  1578. begin
  1579. inputfile.close;
  1580. { reset buffer }
  1581. inputbuffer:=nil;
  1582. inputpointer:=nil;
  1583. inputstart:=0;
  1584. { reset line }
  1585. line_no:=0;
  1586. lastlinepos:=0;
  1587. lasttokenpos:=0;
  1588. end;
  1589. function tscannerfile.tempopeninputfile:boolean;
  1590. begin
  1591. if inputfile.is_macro then
  1592. exit;
  1593. tempopeninputfile:=inputfile.tempopen;
  1594. { reload buffer }
  1595. inputbuffer:=inputfile.buf;
  1596. inputpointer:=inputfile.buf;
  1597. inputstart:=inputfile.bufstart;
  1598. end;
  1599. procedure tscannerfile.tempcloseinputfile;
  1600. begin
  1601. if inputfile.closed or inputfile.is_macro then
  1602. exit;
  1603. inputfile.setpos(inputstart+(inputpointer-inputbuffer));
  1604. inputfile.tempclose;
  1605. { reset buffer }
  1606. inputbuffer:=nil;
  1607. inputpointer:=nil;
  1608. inputstart:=0;
  1609. end;
  1610. procedure tscannerfile.saveinputfile;
  1611. begin
  1612. inputfile.saveinputpointer:=inputpointer;
  1613. inputfile.savelastlinepos:=lastlinepos;
  1614. inputfile.saveline_no:=line_no;
  1615. end;
  1616. procedure tscannerfile.restoreinputfile;
  1617. begin
  1618. inputbuffer:=inputfile.buf;
  1619. inputpointer:=inputfile.saveinputpointer;
  1620. lastlinepos:=inputfile.savelastlinepos;
  1621. line_no:=inputfile.saveline_no;
  1622. if not inputfile.is_macro then
  1623. parser_current_file:=inputfile.name^;
  1624. end;
  1625. procedure tscannerfile.nextfile;
  1626. var
  1627. to_dispose : tinputfile;
  1628. begin
  1629. if assigned(inputfile.next) then
  1630. begin
  1631. if inputfile.is_macro then
  1632. to_dispose:=inputfile
  1633. else
  1634. begin
  1635. to_dispose:=nil;
  1636. dec(inputfilecount);
  1637. end;
  1638. { we can allways close the file, no ? }
  1639. inputfile.close;
  1640. inputfile:=inputfile.next;
  1641. if assigned(to_dispose) then
  1642. to_dispose.free;
  1643. restoreinputfile;
  1644. end;
  1645. end;
  1646. procedure tscannerfile.startrecordtokens(buf:tdynamicarray);
  1647. begin
  1648. if not assigned(buf) then
  1649. internalerror(200511172);
  1650. if assigned(recordtokenbuf) then
  1651. internalerror(200511173);
  1652. recordtokenbuf:=buf;
  1653. fillchar(last_settings,sizeof(last_settings),0);
  1654. fillchar(last_filepos,sizeof(last_filepos),0);
  1655. end;
  1656. procedure tscannerfile.stoprecordtokens;
  1657. begin
  1658. if not assigned(recordtokenbuf) then
  1659. internalerror(200511174);
  1660. recordtokenbuf:=nil;
  1661. end;
  1662. procedure tscannerfile.recordtoken;
  1663. begin
  1664. if not assigned(recordtokenbuf) then
  1665. internalerror(200511176);
  1666. { settings changed? }
  1667. if CompareByte(current_settings,last_settings,sizeof(current_settings))<>0 then
  1668. begin
  1669. { use a special token to record it }
  1670. recordtokenbuf.write(_GENERICSPECIALTOKEN,1);
  1671. recordtokenbuf.write(ST_LOADSETTINGS,1);
  1672. recordtokenbuf.write(current_settings,sizeof(current_settings));
  1673. last_settings:=current_settings;
  1674. end;
  1675. { file pos changes? }
  1676. if current_tokenpos.line<>last_filepos.line then
  1677. begin
  1678. recordtokenbuf.write(_GENERICSPECIALTOKEN,1);
  1679. recordtokenbuf.write(ST_LINE,1);
  1680. recordtokenbuf.write(current_tokenpos.line,sizeof(current_tokenpos.line));
  1681. last_filepos.line:=current_tokenpos.line;
  1682. end;
  1683. if current_tokenpos.column<>last_filepos.column then
  1684. begin
  1685. recordtokenbuf.write(_GENERICSPECIALTOKEN,1);
  1686. recordtokenbuf.write(ST_COLUMN,1);
  1687. recordtokenbuf.write(current_tokenpos.column,sizeof(current_tokenpos.column));
  1688. last_filepos.column:=current_tokenpos.column;
  1689. end;
  1690. if current_tokenpos.fileindex<>last_filepos.fileindex then
  1691. begin
  1692. recordtokenbuf.write(_GENERICSPECIALTOKEN,1);
  1693. recordtokenbuf.write(ST_FILEINDEX,1);
  1694. recordtokenbuf.write(current_tokenpos.fileindex,sizeof(current_tokenpos.fileindex));
  1695. last_filepos.fileindex:=current_tokenpos.fileindex;
  1696. end;
  1697. recordtokenbuf.write(token,1);
  1698. if token=_ID then
  1699. recordtokenbuf.write(idtoken,1);
  1700. case token of
  1701. _CWCHAR,
  1702. _CWSTRING :
  1703. begin
  1704. recordtokenbuf.write(patternw^.len,sizeof(sizeint));
  1705. recordtokenbuf.write(patternw^.data^,patternw^.len*sizeof(tcompilerwidechar));
  1706. end;
  1707. _CCHAR,
  1708. _CSTRING,
  1709. _INTCONST,
  1710. _REALNUMBER :
  1711. begin
  1712. recordtokenbuf.write(pattern[0],1);
  1713. recordtokenbuf.write(pattern[1],length(pattern));
  1714. end;
  1715. _ID :
  1716. begin
  1717. recordtokenbuf.write(orgpattern[0],1);
  1718. recordtokenbuf.write(orgpattern[1],length(orgpattern));
  1719. end;
  1720. end;
  1721. end;
  1722. procedure tscannerfile.startreplaytokens(buf:tdynamicarray);
  1723. begin
  1724. if not assigned(buf) then
  1725. internalerror(200511175);
  1726. { save current token }
  1727. if token in [_CWCHAR,_CWSTRING,_CCHAR,_CSTRING,_INTCONST,_REALNUMBER,_ID] then
  1728. internalerror(200511178);
  1729. replaysavetoken:=token;
  1730. old_settings:=current_settings;
  1731. if assigned(inputpointer) then
  1732. dec(inputpointer);
  1733. { install buffer }
  1734. replaytokenbuf:=buf;
  1735. { reload next token }
  1736. replaytokenbuf.seek(0);
  1737. replaytoken;
  1738. end;
  1739. procedure tscannerfile.replaytoken;
  1740. var
  1741. wlen : sizeint;
  1742. specialtoken : tspecialgenerictoken;
  1743. begin
  1744. if not assigned(replaytokenbuf) then
  1745. internalerror(200511177);
  1746. { End of replay buffer? Then load the next char from the file again }
  1747. if replaytokenbuf.pos>=replaytokenbuf.size then
  1748. begin
  1749. replaytokenbuf:=nil;
  1750. if assigned(inputpointer) then
  1751. begin
  1752. c:=inputpointer^;
  1753. inc(inputpointer);
  1754. end;
  1755. token:=replaysavetoken;
  1756. { restore compiler settings }
  1757. current_settings:=old_settings;
  1758. exit;
  1759. end;
  1760. repeat
  1761. { load token from the buffer }
  1762. replaytokenbuf.read(token,1);
  1763. if token=_ID then
  1764. replaytokenbuf.read(idtoken,1)
  1765. else
  1766. idtoken:=_NOID;
  1767. case token of
  1768. _CWCHAR,
  1769. _CWSTRING :
  1770. begin
  1771. replaytokenbuf.read(wlen,sizeof(SizeInt));
  1772. setlengthwidestring(patternw,wlen);
  1773. replaytokenbuf.read(patternw^.data^,patternw^.len*sizeof(tcompilerwidechar));
  1774. pattern:='';
  1775. end;
  1776. _CCHAR,
  1777. _CSTRING,
  1778. _INTCONST,
  1779. _REALNUMBER :
  1780. begin
  1781. replaytokenbuf.read(pattern[0],1);
  1782. replaytokenbuf.read(pattern[1],length(pattern));
  1783. orgpattern:='';
  1784. end;
  1785. _ID :
  1786. begin
  1787. replaytokenbuf.read(orgpattern[0],1);
  1788. replaytokenbuf.read(orgpattern[1],length(orgpattern));
  1789. pattern:=upper(orgpattern);
  1790. end;
  1791. _GENERICSPECIALTOKEN:
  1792. begin
  1793. replaytokenbuf.read(specialtoken,1);
  1794. case specialtoken of
  1795. ST_LOADSETTINGS:
  1796. replaytokenbuf.read(current_settings,sizeof(current_settings));
  1797. ST_LINE:
  1798. begin
  1799. replaytokenbuf.read(current_tokenpos.line,sizeof(current_tokenpos.line));
  1800. current_filepos:=current_tokenpos;
  1801. end;
  1802. ST_COLUMN:
  1803. begin
  1804. replaytokenbuf.read(current_tokenpos.column,sizeof(current_tokenpos.column));
  1805. current_filepos:=current_tokenpos;
  1806. end;
  1807. ST_FILEINDEX:
  1808. begin
  1809. replaytokenbuf.read(current_tokenpos.fileindex,sizeof(current_tokenpos.fileindex));
  1810. current_filepos:=current_tokenpos;
  1811. end;
  1812. else
  1813. internalerror(2006103010);
  1814. end;
  1815. continue;
  1816. end;
  1817. end;
  1818. break;
  1819. until false;
  1820. end;
  1821. procedure tscannerfile.addfile(hp:tinputfile);
  1822. begin
  1823. saveinputfile;
  1824. { add to list }
  1825. hp.next:=inputfile;
  1826. inputfile:=hp;
  1827. { load new inputfile }
  1828. restoreinputfile;
  1829. end;
  1830. procedure tscannerfile.reload;
  1831. begin
  1832. with inputfile do
  1833. begin
  1834. { when nothing more to read then leave immediatly, so we
  1835. don't change the current_filepos and leave it point to the last
  1836. char }
  1837. if (c=#26) and (not assigned(next)) then
  1838. exit;
  1839. repeat
  1840. { still more to read?, then change the #0 to a space so its seen
  1841. as a seperator, this can't be used for macro's which can change
  1842. the place of the #0 in the buffer with tempopen }
  1843. if (c=#0) and (bufsize>0) and
  1844. not(inputfile.is_macro) and
  1845. (inputpointer-inputbuffer<bufsize) then
  1846. begin
  1847. c:=' ';
  1848. inc(inputpointer);
  1849. exit;
  1850. end;
  1851. { can we read more from this file ? }
  1852. if (c<>#26) and (not endoffile) then
  1853. begin
  1854. readbuf;
  1855. inputpointer:=buf;
  1856. inputbuffer:=buf;
  1857. inputstart:=bufstart;
  1858. { first line? }
  1859. if line_no=0 then
  1860. begin
  1861. c:=inputpointer^;
  1862. { eat utf-8 signature? }
  1863. if (ord(inputpointer^)=$ef) and
  1864. (ord((inputpointer+1)^)=$bb) and
  1865. (ord((inputpointer+2)^)=$bf) then
  1866. begin
  1867. inc(inputpointer,3);
  1868. message(scan_c_switching_to_utf8);
  1869. current_settings.sourcecodepage:='utf8';
  1870. end;
  1871. line_no:=1;
  1872. if cs_asm_source in current_settings.globalswitches then
  1873. inputfile.setline(line_no,bufstart);
  1874. end;
  1875. end
  1876. else
  1877. begin
  1878. { load eof position in tokenpos/current_filepos }
  1879. gettokenpos;
  1880. { close file }
  1881. closeinputfile;
  1882. { no next module, than EOF }
  1883. if not assigned(inputfile.next) then
  1884. begin
  1885. c:=#26;
  1886. exit;
  1887. end;
  1888. { load next file and reopen it }
  1889. nextfile;
  1890. tempopeninputfile;
  1891. { status }
  1892. Message1(scan_t_back_in,inputfile.name^);
  1893. end;
  1894. { load next char }
  1895. c:=inputpointer^;
  1896. inc(inputpointer);
  1897. until c<>#0; { if also end, then reload again }
  1898. end;
  1899. end;
  1900. procedure tscannerfile.insertmacro(const macname:string;p:pchar;len,line,fileindex:longint);
  1901. var
  1902. hp : tinputfile;
  1903. begin
  1904. { save old postion }
  1905. dec(inputpointer);
  1906. tempcloseinputfile;
  1907. { create macro 'file' }
  1908. { use special name to dispose after !! }
  1909. hp:=do_openinputfile('_Macro_.'+macname);
  1910. addfile(hp);
  1911. with inputfile do
  1912. begin
  1913. setmacro(p,len);
  1914. { local buffer }
  1915. inputbuffer:=buf;
  1916. inputpointer:=buf;
  1917. inputstart:=bufstart;
  1918. ref_index:=fileindex;
  1919. end;
  1920. { reset line }
  1921. line_no:=line;
  1922. lastlinepos:=0;
  1923. lasttokenpos:=0;
  1924. { load new c }
  1925. c:=inputpointer^;
  1926. inc(inputpointer);
  1927. end;
  1928. procedure tscannerfile.gettokenpos;
  1929. { load the values of tokenpos and lasttokenpos }
  1930. begin
  1931. lasttokenpos:=inputstart+(inputpointer-inputbuffer);
  1932. current_tokenpos.line:=line_no;
  1933. current_tokenpos.column:=lasttokenpos-lastlinepos;
  1934. current_tokenpos.fileindex:=inputfile.ref_index;
  1935. current_tokenpos.moduleindex:=current_module.unit_index;
  1936. current_filepos:=current_tokenpos;
  1937. end;
  1938. procedure tscannerfile.inc_comment_level;
  1939. var
  1940. oldcurrent_filepos : tfileposinfo;
  1941. begin
  1942. if (m_nested_comment in current_settings.modeswitches) then
  1943. inc(comment_level)
  1944. else
  1945. comment_level:=1;
  1946. if (comment_level>1) then
  1947. begin
  1948. oldcurrent_filepos:=current_filepos;
  1949. gettokenpos; { update for warning }
  1950. Message1(scan_w_comment_level,tostr(comment_level));
  1951. current_filepos:=oldcurrent_filepos;
  1952. end;
  1953. end;
  1954. procedure tscannerfile.dec_comment_level;
  1955. begin
  1956. if (m_nested_comment in current_settings.modeswitches) then
  1957. dec(comment_level)
  1958. else
  1959. comment_level:=0;
  1960. end;
  1961. procedure tscannerfile.linebreak;
  1962. var
  1963. cur : char;
  1964. oldtokenpos,
  1965. oldcurrent_filepos : tfileposinfo;
  1966. begin
  1967. with inputfile do
  1968. begin
  1969. if (byte(inputpointer^)=0) and not(endoffile) then
  1970. begin
  1971. cur:=c;
  1972. reload;
  1973. if byte(cur)+byte(c)<>23 then
  1974. dec(inputpointer);
  1975. end
  1976. else
  1977. begin
  1978. { Support all combination of #10 and #13 as line break }
  1979. if (byte(inputpointer^)+byte(c)=23) then
  1980. inc(inputpointer);
  1981. end;
  1982. { Always return #10 as line break }
  1983. c:=#10;
  1984. { increase line counters }
  1985. lastlinepos:=bufstart+(inputpointer-inputbuffer);
  1986. inc(line_no);
  1987. { update linebuffer }
  1988. if cs_asm_source in current_settings.globalswitches then
  1989. inputfile.setline(line_no,lastlinepos);
  1990. { update for status and call the show status routine,
  1991. but don't touch current_filepos ! }
  1992. oldcurrent_filepos:=current_filepos;
  1993. oldtokenpos:=current_tokenpos;
  1994. gettokenpos; { update for v_status }
  1995. inc(status.compiledlines);
  1996. ShowStatus;
  1997. current_filepos:=oldcurrent_filepos;
  1998. current_tokenpos:=oldtokenpos;
  1999. end;
  2000. end;
  2001. procedure tscannerfile.illegal_char(c:char);
  2002. var
  2003. s : string;
  2004. begin
  2005. if c in [#32..#255] then
  2006. s:=''''+c+''''
  2007. else
  2008. s:='#'+tostr(ord(c));
  2009. Message2(scan_f_illegal_char,s,'$'+hexstr(ord(c),2));
  2010. end;
  2011. procedure tscannerfile.end_of_file;
  2012. begin
  2013. checkpreprocstack;
  2014. Message(scan_f_end_of_file);
  2015. end;
  2016. {-------------------------------------------
  2017. IF Conditional Handling
  2018. -------------------------------------------}
  2019. procedure tscannerfile.checkpreprocstack;
  2020. begin
  2021. { check for missing ifdefs }
  2022. while assigned(preprocstack) do
  2023. begin
  2024. Message4(scan_e_endif_expected,preprocstring[preprocstack.typ],preprocstack.name,
  2025. preprocstack.owner.inputfile.name^,tostr(preprocstack.line_nb));
  2026. poppreprocstack;
  2027. end;
  2028. end;
  2029. procedure tscannerfile.poppreprocstack;
  2030. var
  2031. hp : tpreprocstack;
  2032. begin
  2033. if assigned(preprocstack) then
  2034. begin
  2035. Message1(scan_c_endif_found,preprocstack.name);
  2036. hp:=preprocstack.next;
  2037. preprocstack.free;
  2038. preprocstack:=hp;
  2039. end
  2040. else
  2041. Message(scan_e_endif_without_if);
  2042. end;
  2043. procedure tscannerfile.ifpreprocstack(atyp : preproctyp;compile_time_predicate:tcompile_time_predicate;messid:longint);
  2044. var
  2045. condition: Boolean;
  2046. valuedescr: String;
  2047. begin
  2048. if (preprocstack=nil) or preprocstack.accept then
  2049. condition:= compile_time_predicate(valuedescr)
  2050. else
  2051. begin
  2052. condition:= false;
  2053. valuedescr:= '';
  2054. end;
  2055. preprocstack:=tpreprocstack.create(atyp, condition, preprocstack);
  2056. preprocstack.name:=valuedescr;
  2057. preprocstack.line_nb:=line_no;
  2058. preprocstack.owner:=self;
  2059. if preprocstack.accept then
  2060. Message2(messid,preprocstack.name,'accepted')
  2061. else
  2062. Message2(messid,preprocstack.name,'rejected');
  2063. end;
  2064. procedure tscannerfile.elsepreprocstack;
  2065. begin
  2066. if assigned(preprocstack) and
  2067. (preprocstack.typ<>pp_else) then
  2068. begin
  2069. if (preprocstack.typ=pp_elseif) then
  2070. preprocstack.accept:=false
  2071. else
  2072. if (not(assigned(preprocstack.next)) or (preprocstack.next.accept)) then
  2073. preprocstack.accept:=not preprocstack.accept;
  2074. preprocstack.typ:=pp_else;
  2075. preprocstack.line_nb:=line_no;
  2076. if preprocstack.accept then
  2077. Message2(scan_c_else_found,preprocstack.name,'accepted')
  2078. else
  2079. Message2(scan_c_else_found,preprocstack.name,'rejected');
  2080. end
  2081. else
  2082. Message(scan_e_endif_without_if);
  2083. end;
  2084. procedure tscannerfile.elseifpreprocstack(compile_time_predicate:tcompile_time_predicate);
  2085. var
  2086. valuedescr: String;
  2087. begin
  2088. if assigned(preprocstack) and
  2089. (preprocstack.typ in [pp_if,pp_elseif]) then
  2090. begin
  2091. { when the branch is accepted we use pp_elseif so we know that
  2092. all the next branches need to be rejected. when this branch is still
  2093. not accepted then leave it at pp_if }
  2094. if (preprocstack.typ=pp_elseif) then
  2095. preprocstack.accept:=false
  2096. else if (preprocstack.typ=pp_if) and preprocstack.accept then
  2097. begin
  2098. preprocstack.accept:=false;
  2099. preprocstack.typ:=pp_elseif;
  2100. end
  2101. else if (not(assigned(preprocstack.next)) or (preprocstack.next.accept))
  2102. and compile_time_predicate(valuedescr) then
  2103. begin
  2104. preprocstack.name:=valuedescr;
  2105. preprocstack.accept:=true;
  2106. preprocstack.typ:=pp_elseif;
  2107. end;
  2108. preprocstack.line_nb:=line_no;
  2109. if preprocstack.accept then
  2110. Message2(scan_c_else_found,preprocstack.name,'accepted')
  2111. else
  2112. Message2(scan_c_else_found,preprocstack.name,'rejected');
  2113. end
  2114. else
  2115. Message(scan_e_endif_without_if);
  2116. end;
  2117. procedure tscannerfile.handleconditional(p:tdirectiveitem);
  2118. var
  2119. oldcurrent_filepos : tfileposinfo;
  2120. begin
  2121. oldcurrent_filepos:=current_filepos;
  2122. repeat
  2123. current_scanner.gettokenpos;
  2124. p.proc();
  2125. { accept the text ? }
  2126. if (current_scanner.preprocstack=nil) or current_scanner.preprocstack.accept then
  2127. break
  2128. else
  2129. begin
  2130. current_scanner.gettokenpos;
  2131. Message(scan_c_skipping_until);
  2132. repeat
  2133. current_scanner.skipuntildirective;
  2134. if not (m_mac in current_settings.modeswitches) then
  2135. p:=tdirectiveitem(turbo_scannerdirectives.Find(current_scanner.readid))
  2136. else
  2137. p:=tdirectiveitem(mac_scannerdirectives.Find(current_scanner.readid));
  2138. until assigned(p) and (p.is_conditional);
  2139. current_scanner.gettokenpos;
  2140. Message1(scan_d_handling_switch,'$'+p.name);
  2141. end;
  2142. until false;
  2143. current_filepos:=oldcurrent_filepos;
  2144. end;
  2145. procedure tscannerfile.handledirectives;
  2146. var
  2147. t : tdirectiveitem;
  2148. hs : string;
  2149. begin
  2150. gettokenpos;
  2151. readchar; {Remove the $}
  2152. hs:=readid;
  2153. { handle empty directive }
  2154. if hs='' then
  2155. begin
  2156. Message1(scan_w_illegal_switch,'$');
  2157. exit;
  2158. end;
  2159. {$ifdef PREPROCWRITE}
  2160. if parapreprocess then
  2161. begin
  2162. t:=Get_Directive(hs);
  2163. if not(is_conditional(t) or (t=_DIR_DEFINE) or (t=_DIR_UNDEF)) then
  2164. begin
  2165. preprocfile^.AddSpace;
  2166. preprocfile^.Add('{$'+hs+current_scanner.readcomment+'}');
  2167. exit;
  2168. end;
  2169. end;
  2170. {$endif PREPROCWRITE}
  2171. { skip this directive? }
  2172. if (ignoredirectives.find(hs)<>nil) then
  2173. begin
  2174. if (comment_level>0) then
  2175. readcomment;
  2176. { we've read the whole comment }
  2177. aktcommentstyle:=comment_none;
  2178. exit;
  2179. end;
  2180. { Check for compiler switches }
  2181. while (length(hs)=1) and (c in ['-','+']) do
  2182. begin
  2183. HandleSwitch(hs[1],c);
  2184. current_scanner.readchar; {Remove + or -}
  2185. if c=',' then
  2186. begin
  2187. current_scanner.readchar; {Remove , }
  2188. { read next switch, support $v+,$+}
  2189. hs:=current_scanner.readid;
  2190. if (hs='') then
  2191. begin
  2192. if (c='$') and (m_fpc in current_settings.modeswitches) then
  2193. begin
  2194. current_scanner.readchar; { skip $ }
  2195. hs:=current_scanner.readid;
  2196. end;
  2197. if (hs='') then
  2198. Message1(scan_w_illegal_directive,'$'+c);
  2199. end
  2200. else
  2201. Message1(scan_d_handling_switch,'$'+hs);
  2202. end
  2203. else
  2204. hs:='';
  2205. end;
  2206. { directives may follow switches after a , }
  2207. if hs<>'' then
  2208. begin
  2209. if not (m_mac in current_settings.modeswitches) then
  2210. t:=tdirectiveitem(turbo_scannerdirectives.Find(hs))
  2211. else
  2212. t:=tdirectiveitem(mac_scannerdirectives.Find(hs));
  2213. if assigned(t) then
  2214. begin
  2215. if t.is_conditional then
  2216. handleconditional(t)
  2217. else
  2218. begin
  2219. Message1(scan_d_handling_switch,'$'+hs);
  2220. t.proc();
  2221. end;
  2222. end
  2223. else
  2224. begin
  2225. current_scanner.ignoredirectives.Add(hs,nil);
  2226. Message1(scan_w_illegal_directive,'$'+hs);
  2227. end;
  2228. { conditionals already read the comment }
  2229. if (current_scanner.comment_level>0) then
  2230. current_scanner.readcomment;
  2231. { we've read the whole comment }
  2232. aktcommentstyle:=comment_none;
  2233. end;
  2234. end;
  2235. procedure tscannerfile.readchar;
  2236. begin
  2237. c:=inputpointer^;
  2238. if c=#0 then
  2239. reload
  2240. else
  2241. inc(inputpointer);
  2242. end;
  2243. procedure tscannerfile.readstring;
  2244. var
  2245. i : longint;
  2246. err : boolean;
  2247. begin
  2248. err:=false;
  2249. i:=0;
  2250. repeat
  2251. case c of
  2252. '_',
  2253. '0'..'9',
  2254. 'A'..'Z' :
  2255. begin
  2256. if i<255 then
  2257. begin
  2258. inc(i);
  2259. orgpattern[i]:=c;
  2260. pattern[i]:=c;
  2261. end
  2262. else
  2263. begin
  2264. if not err then
  2265. begin
  2266. Message(scan_e_string_exceeds_255_chars);
  2267. err:=true;
  2268. end;
  2269. end;
  2270. c:=inputpointer^;
  2271. inc(inputpointer);
  2272. end;
  2273. 'a'..'z' :
  2274. begin
  2275. if i<255 then
  2276. begin
  2277. inc(i);
  2278. orgpattern[i]:=c;
  2279. pattern[i]:=chr(ord(c)-32)
  2280. end
  2281. else
  2282. begin
  2283. if not err then
  2284. begin
  2285. Message(scan_e_string_exceeds_255_chars);
  2286. err:=true;
  2287. end;
  2288. end;
  2289. c:=inputpointer^;
  2290. inc(inputpointer);
  2291. end;
  2292. #0 :
  2293. reload;
  2294. else
  2295. break;
  2296. end;
  2297. until false;
  2298. orgpattern[0]:=chr(i);
  2299. pattern[0]:=chr(i);
  2300. end;
  2301. procedure tscannerfile.readnumber;
  2302. var
  2303. base,
  2304. i : longint;
  2305. begin
  2306. case c of
  2307. '%' :
  2308. begin
  2309. readchar;
  2310. base:=2;
  2311. pattern[1]:='%';
  2312. i:=1;
  2313. end;
  2314. '&' :
  2315. begin
  2316. readchar;
  2317. base:=8;
  2318. pattern[1]:='&';
  2319. i:=1;
  2320. end;
  2321. '$' :
  2322. begin
  2323. readchar;
  2324. base:=16;
  2325. pattern[1]:='$';
  2326. i:=1;
  2327. end;
  2328. else
  2329. begin
  2330. base:=10;
  2331. i:=0;
  2332. end;
  2333. end;
  2334. while ((base>=10) and (c in ['0'..'9'])) or
  2335. ((base=16) and (c in ['A'..'F','a'..'f'])) or
  2336. ((base=8) and (c in ['0'..'7'])) or
  2337. ((base=2) and (c in ['0'..'1'])) do
  2338. begin
  2339. if i<255 then
  2340. begin
  2341. inc(i);
  2342. pattern[i]:=c;
  2343. end;
  2344. readchar;
  2345. end;
  2346. pattern[0]:=chr(i);
  2347. end;
  2348. function tscannerfile.readid:string;
  2349. begin
  2350. readstring;
  2351. readid:=pattern;
  2352. end;
  2353. function tscannerfile.readval:longint;
  2354. var
  2355. l : longint;
  2356. w : integer;
  2357. begin
  2358. readnumber;
  2359. val(pattern,l,w);
  2360. readval:=l;
  2361. end;
  2362. function tscannerfile.readval_asstring:string;
  2363. begin
  2364. readnumber;
  2365. readval_asstring:=pattern;
  2366. end;
  2367. function tscannerfile.readcomment:string;
  2368. var
  2369. i : longint;
  2370. begin
  2371. i:=0;
  2372. repeat
  2373. case c of
  2374. '{' :
  2375. begin
  2376. if aktcommentstyle=comment_tp then
  2377. inc_comment_level;
  2378. end;
  2379. '}' :
  2380. begin
  2381. if aktcommentstyle=comment_tp then
  2382. begin
  2383. readchar;
  2384. dec_comment_level;
  2385. if comment_level=0 then
  2386. break
  2387. else
  2388. continue;
  2389. end;
  2390. end;
  2391. '*' :
  2392. begin
  2393. if aktcommentstyle=comment_oldtp then
  2394. begin
  2395. readchar;
  2396. if c=')' then
  2397. begin
  2398. readchar;
  2399. dec_comment_level;
  2400. break;
  2401. end
  2402. else
  2403. { Add both characters !!}
  2404. if (i<255) then
  2405. begin
  2406. inc(i);
  2407. readcomment[i]:='*';
  2408. if (i<255) then
  2409. begin
  2410. inc(i);
  2411. readcomment[i]:=c;
  2412. end;
  2413. end;
  2414. end
  2415. else
  2416. { Not old TP comment, so add...}
  2417. begin
  2418. if (i<255) then
  2419. begin
  2420. inc(i);
  2421. readcomment[i]:='*';
  2422. end;
  2423. end;
  2424. end;
  2425. #10,#13 :
  2426. linebreak;
  2427. #26 :
  2428. end_of_file;
  2429. else
  2430. begin
  2431. if (i<255) then
  2432. begin
  2433. inc(i);
  2434. readcomment[i]:=c;
  2435. end;
  2436. end;
  2437. end;
  2438. readchar;
  2439. until false;
  2440. readcomment[0]:=chr(i);
  2441. end;
  2442. function tscannerfile.readquotedstring:string;
  2443. var
  2444. i : longint;
  2445. msgwritten : boolean;
  2446. begin
  2447. i:=0;
  2448. msgwritten:=false;
  2449. if (c='''') then
  2450. begin
  2451. repeat
  2452. readchar;
  2453. case c of
  2454. #26 :
  2455. end_of_file;
  2456. #10,#13 :
  2457. Message(scan_f_string_exceeds_line);
  2458. '''' :
  2459. begin
  2460. readchar;
  2461. if c<>'''' then
  2462. break;
  2463. end;
  2464. end;
  2465. if i<255 then
  2466. begin
  2467. inc(i);
  2468. result[i]:=c;
  2469. end
  2470. else
  2471. begin
  2472. if not msgwritten then
  2473. begin
  2474. Message(scan_e_string_exceeds_255_chars);
  2475. msgwritten:=true;
  2476. end;
  2477. end;
  2478. until false;
  2479. end;
  2480. result[0]:=chr(i);
  2481. end;
  2482. function tscannerfile.readstate:char;
  2483. var
  2484. state : char;
  2485. begin
  2486. state:=' ';
  2487. if c=' ' then
  2488. begin
  2489. current_scanner.skipspace;
  2490. current_scanner.readid;
  2491. if pattern='ON' then
  2492. state:='+'
  2493. else
  2494. if pattern='OFF' then
  2495. state:='-';
  2496. end
  2497. else
  2498. state:=c;
  2499. if not (state in ['+','-']) then
  2500. Message(scan_e_wrong_switch_toggle);
  2501. readstate:=state;
  2502. end;
  2503. function tscannerfile.readstatedefault:char;
  2504. var
  2505. state : char;
  2506. begin
  2507. state:=' ';
  2508. if c=' ' then
  2509. begin
  2510. current_scanner.skipspace;
  2511. current_scanner.readid;
  2512. if pattern='ON' then
  2513. state:='+'
  2514. else
  2515. if pattern='OFF' then
  2516. state:='-'
  2517. else
  2518. if pattern='DEFAULT' then
  2519. state:='*';
  2520. end
  2521. else
  2522. state:=c;
  2523. if not (state in ['+','-','*']) then
  2524. Message(scan_e_wrong_switch_toggle_default);
  2525. readstatedefault:=state;
  2526. end;
  2527. procedure tscannerfile.skipspace;
  2528. begin
  2529. repeat
  2530. case c of
  2531. #26 :
  2532. begin
  2533. reload;
  2534. if (c=#26) and not assigned(inputfile.next) then
  2535. break;
  2536. continue;
  2537. end;
  2538. #10,
  2539. #13 :
  2540. linebreak;
  2541. #9,#11,#12,' ' :
  2542. ;
  2543. else
  2544. break;
  2545. end;
  2546. readchar;
  2547. until false;
  2548. end;
  2549. procedure tscannerfile.skipuntildirective;
  2550. var
  2551. found : longint;
  2552. next_char_loaded : boolean;
  2553. begin
  2554. found:=0;
  2555. next_char_loaded:=false;
  2556. repeat
  2557. case c of
  2558. #10,
  2559. #13 :
  2560. linebreak;
  2561. #26 :
  2562. begin
  2563. reload;
  2564. if (c=#26) and not assigned(inputfile.next) then
  2565. end_of_file;
  2566. continue;
  2567. end;
  2568. '{' :
  2569. begin
  2570. if (aktcommentstyle in [comment_tp,comment_none]) then
  2571. begin
  2572. aktcommentstyle:=comment_tp;
  2573. if (comment_level=0) then
  2574. found:=1;
  2575. inc_comment_level;
  2576. end;
  2577. end;
  2578. '*' :
  2579. begin
  2580. if (aktcommentstyle=comment_oldtp) then
  2581. begin
  2582. readchar;
  2583. if c=')' then
  2584. begin
  2585. dec_comment_level;
  2586. found:=0;
  2587. aktcommentstyle:=comment_none;
  2588. end
  2589. else
  2590. next_char_loaded:=true;
  2591. end
  2592. else
  2593. found := 0;
  2594. end;
  2595. '}' :
  2596. begin
  2597. if (aktcommentstyle=comment_tp) then
  2598. begin
  2599. dec_comment_level;
  2600. if (comment_level=0) then
  2601. aktcommentstyle:=comment_none;
  2602. found:=0;
  2603. end;
  2604. end;
  2605. '$' :
  2606. begin
  2607. if found=1 then
  2608. found:=2;
  2609. end;
  2610. '''' :
  2611. if (aktcommentstyle=comment_none) then
  2612. begin
  2613. repeat
  2614. readchar;
  2615. case c of
  2616. #26 :
  2617. end_of_file;
  2618. #10,#13 :
  2619. break;
  2620. '''' :
  2621. begin
  2622. readchar;
  2623. if c<>'''' then
  2624. begin
  2625. next_char_loaded:=true;
  2626. break;
  2627. end;
  2628. end;
  2629. end;
  2630. until false;
  2631. end;
  2632. '(' :
  2633. begin
  2634. if (aktcommentstyle=comment_none) then
  2635. begin
  2636. readchar;
  2637. if c='*' then
  2638. begin
  2639. readchar;
  2640. if c='$' then
  2641. begin
  2642. found:=2;
  2643. inc_comment_level;
  2644. aktcommentstyle:=comment_oldtp;
  2645. end
  2646. else
  2647. begin
  2648. skipoldtpcomment;
  2649. next_char_loaded:=true;
  2650. end;
  2651. end
  2652. else
  2653. next_char_loaded:=true;
  2654. end
  2655. else
  2656. found:=0;
  2657. end;
  2658. '/' :
  2659. begin
  2660. if (aktcommentstyle=comment_none) then
  2661. begin
  2662. readchar;
  2663. if c='/' then
  2664. skipdelphicomment;
  2665. next_char_loaded:=true;
  2666. end
  2667. else
  2668. found:=0;
  2669. end;
  2670. else
  2671. found:=0;
  2672. end;
  2673. if next_char_loaded then
  2674. next_char_loaded:=false
  2675. else
  2676. readchar;
  2677. until (found=2);
  2678. end;
  2679. {****************************************************************************
  2680. Comment Handling
  2681. ****************************************************************************}
  2682. procedure tscannerfile.skipcomment;
  2683. begin
  2684. aktcommentstyle:=comment_tp;
  2685. readchar;
  2686. inc_comment_level;
  2687. { handle compiler switches }
  2688. if (c='$') then
  2689. handledirectives;
  2690. { handle_switches can dec comment_level, }
  2691. while (comment_level>0) do
  2692. begin
  2693. case c of
  2694. '{' :
  2695. inc_comment_level;
  2696. '}' :
  2697. dec_comment_level;
  2698. #10,#13 :
  2699. linebreak;
  2700. #26 :
  2701. begin
  2702. reload;
  2703. if (c=#26) and not assigned(inputfile.next) then
  2704. end_of_file;
  2705. continue;
  2706. end;
  2707. end;
  2708. readchar;
  2709. end;
  2710. aktcommentstyle:=comment_none;
  2711. end;
  2712. procedure tscannerfile.skipdelphicomment;
  2713. begin
  2714. aktcommentstyle:=comment_delphi;
  2715. inc_comment_level;
  2716. readchar;
  2717. { this is not supported }
  2718. if c='$' then
  2719. Message(scan_w_wrong_styled_switch);
  2720. { skip comment }
  2721. while not (c in [#10,#13,#26]) do
  2722. readchar;
  2723. dec_comment_level;
  2724. aktcommentstyle:=comment_none;
  2725. end;
  2726. procedure tscannerfile.skipoldtpcomment;
  2727. var
  2728. found : longint;
  2729. begin
  2730. aktcommentstyle:=comment_oldtp;
  2731. inc_comment_level;
  2732. { only load a char if last already processed,
  2733. was cause of bug1634 PM }
  2734. if c=#0 then
  2735. readchar;
  2736. { this is now supported }
  2737. if (c='$') then
  2738. handledirectives;
  2739. { skip comment }
  2740. while (comment_level>0) do
  2741. begin
  2742. found:=0;
  2743. repeat
  2744. case c of
  2745. #26 :
  2746. begin
  2747. reload;
  2748. if (c=#26) and not assigned(inputfile.next) then
  2749. end_of_file;
  2750. continue;
  2751. end;
  2752. #10,#13 :
  2753. linebreak;
  2754. '*' :
  2755. begin
  2756. if found=3 then
  2757. found:=4
  2758. else
  2759. found:=1;
  2760. end;
  2761. ')' :
  2762. begin
  2763. if found in [1,4] then
  2764. begin
  2765. dec_comment_level;
  2766. if comment_level=0 then
  2767. found:=2
  2768. else
  2769. found:=0;
  2770. end;
  2771. end;
  2772. '(' :
  2773. begin
  2774. if found=4 then
  2775. inc_comment_level;
  2776. found:=3;
  2777. end;
  2778. else
  2779. begin
  2780. if found=4 then
  2781. inc_comment_level;
  2782. found:=0;
  2783. end;
  2784. end;
  2785. readchar;
  2786. until (found=2);
  2787. end;
  2788. aktcommentstyle:=comment_none;
  2789. end;
  2790. {****************************************************************************
  2791. Token Scanner
  2792. ****************************************************************************}
  2793. procedure tscannerfile.readtoken(allowrecordtoken:boolean);
  2794. var
  2795. code : integer;
  2796. len,
  2797. low,high,mid : longint;
  2798. w : word;
  2799. m : longint;
  2800. mac : tmacro;
  2801. asciinr : string[6];
  2802. msgwritten,
  2803. iswidestring : boolean;
  2804. label
  2805. exit_label;
  2806. begin
  2807. if localswitcheschanged then
  2808. begin
  2809. current_settings.localswitches:=nextlocalswitches;
  2810. localswitcheschanged:=false;
  2811. end;
  2812. { record tokens? }
  2813. if allowrecordtoken and
  2814. assigned(recordtokenbuf) then
  2815. recordtoken;
  2816. { replay tokens? }
  2817. if assigned(replaytokenbuf) then
  2818. begin
  2819. replaytoken;
  2820. goto exit_label;
  2821. end;
  2822. { was there already a token read, then return that token }
  2823. if nexttoken<>NOTOKEN then
  2824. begin
  2825. token:=nexttoken;
  2826. nexttoken:=NOTOKEN;
  2827. goto exit_label;
  2828. end;
  2829. { Skip all spaces and comments }
  2830. repeat
  2831. case c of
  2832. '{' :
  2833. skipcomment;
  2834. #26 :
  2835. begin
  2836. reload;
  2837. if (c=#26) and not assigned(inputfile.next) then
  2838. break;
  2839. end;
  2840. ' ',#9..#13 :
  2841. begin
  2842. {$ifdef PREPROCWRITE}
  2843. if parapreprocess then
  2844. begin
  2845. if c=#10 then
  2846. preprocfile.eolfound:=true
  2847. else
  2848. preprocfile.spacefound:=true;
  2849. end;
  2850. {$endif PREPROCWRITE}
  2851. skipspace;
  2852. end
  2853. else
  2854. break;
  2855. end;
  2856. until false;
  2857. { Save current token position, for EOF its already loaded }
  2858. if c<>#26 then
  2859. gettokenpos;
  2860. { Check first for a identifier/keyword, this is 20+% faster (PFV) }
  2861. if c in ['A'..'Z','a'..'z','_'] then
  2862. begin
  2863. readstring;
  2864. token:=_ID;
  2865. idtoken:=_ID;
  2866. { keyword or any other known token,
  2867. pattern is always uppercased }
  2868. if (pattern[1]<>'_') and (length(pattern) in [tokenlenmin..tokenlenmax]) then
  2869. begin
  2870. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  2871. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  2872. while low<high do
  2873. begin
  2874. mid:=(high+low+1) shr 1;
  2875. if pattern<tokeninfo^[ttoken(mid)].str then
  2876. high:=mid-1
  2877. else
  2878. low:=mid;
  2879. end;
  2880. with tokeninfo^[ttoken(high)] do
  2881. if pattern=str then
  2882. begin
  2883. if keyword in current_settings.modeswitches then
  2884. if op=NOTOKEN then
  2885. token:=ttoken(high)
  2886. else
  2887. token:=op;
  2888. idtoken:=ttoken(high);
  2889. end;
  2890. end;
  2891. { Only process identifiers and not keywords }
  2892. if token=_ID then
  2893. begin
  2894. { this takes some time ... }
  2895. if (cs_support_macro in current_settings.moduleswitches) then
  2896. begin
  2897. mac:=tmacro(search_macro(pattern));
  2898. if assigned(mac) and (not mac.is_compiler_var) and (assigned(mac.buftext)) then
  2899. begin
  2900. if yylexcount<max_macro_nesting then
  2901. begin
  2902. mac.is_used:=true;
  2903. inc(yylexcount);
  2904. insertmacro(pattern,mac.buftext,mac.buflen,
  2905. mac.fileinfo.line,mac.fileinfo.fileindex);
  2906. { handle empty macros }
  2907. if c=#0 then
  2908. reload;
  2909. readtoken(false);
  2910. { that's all folks }
  2911. dec(yylexcount);
  2912. exit;
  2913. end
  2914. else
  2915. Message(scan_w_macro_too_deep);
  2916. end;
  2917. end;
  2918. end;
  2919. { return token }
  2920. goto exit_label;
  2921. end
  2922. else
  2923. begin
  2924. idtoken:=_NOID;
  2925. case c of
  2926. '$' :
  2927. begin
  2928. readnumber;
  2929. token:=_INTCONST;
  2930. goto exit_label;
  2931. end;
  2932. '%' :
  2933. begin
  2934. if not(m_fpc in current_settings.modeswitches) then
  2935. Illegal_Char(c)
  2936. else
  2937. begin
  2938. readnumber;
  2939. token:=_INTCONST;
  2940. goto exit_label;
  2941. end;
  2942. end;
  2943. '&' :
  2944. begin
  2945. if m_fpc in current_settings.modeswitches then
  2946. begin
  2947. readnumber;
  2948. token:=_INTCONST;
  2949. goto exit_label;
  2950. end
  2951. else if m_mac in current_settings.modeswitches then
  2952. begin
  2953. readchar;
  2954. token:=_AMPERSAND;
  2955. goto exit_label;
  2956. end
  2957. else
  2958. Illegal_Char(c);
  2959. end;
  2960. '0'..'9' :
  2961. begin
  2962. readnumber;
  2963. if (c in ['.','e','E']) then
  2964. begin
  2965. { first check for a . }
  2966. if c='.' then
  2967. begin
  2968. readchar;
  2969. { is it a .. from a range? }
  2970. case c of
  2971. '.' :
  2972. begin
  2973. readchar;
  2974. token:=_INTCONST;
  2975. nexttoken:=_POINTPOINT;
  2976. goto exit_label;
  2977. end;
  2978. ')' :
  2979. begin
  2980. readchar;
  2981. token:=_INTCONST;
  2982. nexttoken:=_RECKKLAMMER;
  2983. goto exit_label;
  2984. end;
  2985. end;
  2986. { insert the number after the . }
  2987. pattern:=pattern+'.';
  2988. while c in ['0'..'9'] do
  2989. begin
  2990. pattern:=pattern+c;
  2991. readchar;
  2992. end;
  2993. end;
  2994. { E can also follow after a point is scanned }
  2995. if c in ['e','E'] then
  2996. begin
  2997. pattern:=pattern+'E';
  2998. readchar;
  2999. if c in ['-','+'] then
  3000. begin
  3001. pattern:=pattern+c;
  3002. readchar;
  3003. end;
  3004. if not(c in ['0'..'9']) then
  3005. Illegal_Char(c);
  3006. while c in ['0'..'9'] do
  3007. begin
  3008. pattern:=pattern+c;
  3009. readchar;
  3010. end;
  3011. end;
  3012. token:=_REALNUMBER;
  3013. goto exit_label;
  3014. end;
  3015. token:=_INTCONST;
  3016. goto exit_label;
  3017. end;
  3018. ';' :
  3019. begin
  3020. readchar;
  3021. token:=_SEMICOLON;
  3022. goto exit_label;
  3023. end;
  3024. '[' :
  3025. begin
  3026. readchar;
  3027. token:=_LECKKLAMMER;
  3028. goto exit_label;
  3029. end;
  3030. ']' :
  3031. begin
  3032. readchar;
  3033. token:=_RECKKLAMMER;
  3034. goto exit_label;
  3035. end;
  3036. '(' :
  3037. begin
  3038. readchar;
  3039. case c of
  3040. '*' :
  3041. begin
  3042. c:=#0;{Signal skipoldtpcomment to reload a char }
  3043. skipoldtpcomment;
  3044. readtoken(false);
  3045. exit;
  3046. end;
  3047. '.' :
  3048. begin
  3049. readchar;
  3050. token:=_LECKKLAMMER;
  3051. goto exit_label;
  3052. end;
  3053. end;
  3054. token:=_LKLAMMER;
  3055. goto exit_label;
  3056. end;
  3057. ')' :
  3058. begin
  3059. readchar;
  3060. token:=_RKLAMMER;
  3061. goto exit_label;
  3062. end;
  3063. '+' :
  3064. begin
  3065. readchar;
  3066. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  3067. begin
  3068. readchar;
  3069. token:=_PLUSASN;
  3070. goto exit_label;
  3071. end;
  3072. token:=_PLUS;
  3073. goto exit_label;
  3074. end;
  3075. '-' :
  3076. begin
  3077. readchar;
  3078. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  3079. begin
  3080. readchar;
  3081. token:=_MINUSASN;
  3082. goto exit_label;
  3083. end;
  3084. token:=_MINUS;
  3085. goto exit_label;
  3086. end;
  3087. ':' :
  3088. begin
  3089. readchar;
  3090. if c='=' then
  3091. begin
  3092. readchar;
  3093. token:=_ASSIGNMENT;
  3094. goto exit_label;
  3095. end;
  3096. token:=_COLON;
  3097. goto exit_label;
  3098. end;
  3099. '*' :
  3100. begin
  3101. readchar;
  3102. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  3103. begin
  3104. readchar;
  3105. token:=_STARASN;
  3106. end
  3107. else
  3108. if c='*' then
  3109. begin
  3110. readchar;
  3111. token:=_STARSTAR;
  3112. end
  3113. else
  3114. token:=_STAR;
  3115. goto exit_label;
  3116. end;
  3117. '/' :
  3118. begin
  3119. readchar;
  3120. case c of
  3121. '=' :
  3122. begin
  3123. if (cs_support_c_operators in current_settings.moduleswitches) then
  3124. begin
  3125. readchar;
  3126. token:=_SLASHASN;
  3127. goto exit_label;
  3128. end;
  3129. end;
  3130. '/' :
  3131. begin
  3132. skipdelphicomment;
  3133. readtoken(false);
  3134. exit;
  3135. end;
  3136. end;
  3137. token:=_SLASH;
  3138. goto exit_label;
  3139. end;
  3140. '|' :
  3141. if m_mac in current_settings.modeswitches then
  3142. begin
  3143. readchar;
  3144. token:=_PIPE;
  3145. goto exit_label;
  3146. end
  3147. else
  3148. Illegal_Char(c);
  3149. '=' :
  3150. begin
  3151. readchar;
  3152. token:=_EQUAL;
  3153. goto exit_label;
  3154. end;
  3155. '.' :
  3156. begin
  3157. readchar;
  3158. case c of
  3159. '.' :
  3160. begin
  3161. readchar;
  3162. case c of
  3163. '.' :
  3164. begin
  3165. readchar;
  3166. token:=_POINTPOINTPOINT;
  3167. goto exit_label;
  3168. end;
  3169. else
  3170. begin
  3171. token:=_POINTPOINT;
  3172. goto exit_label;
  3173. end;
  3174. end;
  3175. end;
  3176. ')' :
  3177. begin
  3178. readchar;
  3179. token:=_RECKKLAMMER;
  3180. goto exit_label;
  3181. end;
  3182. end;
  3183. token:=_POINT;
  3184. goto exit_label;
  3185. end;
  3186. '@' :
  3187. begin
  3188. readchar;
  3189. token:=_KLAMMERAFFE;
  3190. goto exit_label;
  3191. end;
  3192. ',' :
  3193. begin
  3194. readchar;
  3195. token:=_COMMA;
  3196. goto exit_label;
  3197. end;
  3198. '''','#','^' :
  3199. begin
  3200. len:=0;
  3201. msgwritten:=false;
  3202. pattern:='';
  3203. iswidestring:=false;
  3204. if c='^' then
  3205. begin
  3206. readchar;
  3207. c:=upcase(c);
  3208. if (block_type in [bt_type,bt_specialize]) or
  3209. (lasttoken=_ID) or (lasttoken=_NIL) or
  3210. (lasttoken=_RKLAMMER) or (lasttoken=_RECKKLAMMER) or (lasttoken=_CARET) then
  3211. begin
  3212. token:=_CARET;
  3213. goto exit_label;
  3214. end
  3215. else
  3216. begin
  3217. inc(len);
  3218. if c<#64 then
  3219. pattern[len]:=chr(ord(c)+64)
  3220. else
  3221. pattern[len]:=chr(ord(c)-64);
  3222. readchar;
  3223. end;
  3224. end;
  3225. repeat
  3226. case c of
  3227. '#' :
  3228. begin
  3229. readchar; { read # }
  3230. if c='$' then
  3231. begin
  3232. readchar; { read leading $ }
  3233. asciinr:='$';
  3234. while (upcase(c) in ['A'..'F','0'..'9']) and (length(asciinr)<6) do
  3235. begin
  3236. asciinr:=asciinr+c;
  3237. readchar;
  3238. end;
  3239. end
  3240. else
  3241. begin
  3242. asciinr:='';
  3243. while (c in ['0'..'9']) and (length(asciinr)<6) do
  3244. begin
  3245. asciinr:=asciinr+c;
  3246. readchar;
  3247. end;
  3248. end;
  3249. val(asciinr,m,code);
  3250. if (asciinr='') or (code<>0) then
  3251. Message(scan_e_illegal_char_const)
  3252. else if (m<0) or (m>255) or (length(asciinr)>3) then
  3253. begin
  3254. if (m>=0) and (m<=65535) then
  3255. begin
  3256. if not iswidestring then
  3257. begin
  3258. ascii2unicode(@pattern[1],len,patternw);
  3259. iswidestring:=true;
  3260. len:=0;
  3261. end;
  3262. concatwidestringchar(patternw,tcompilerwidechar(m));
  3263. end
  3264. else
  3265. Message(scan_e_illegal_char_const)
  3266. end
  3267. else if iswidestring then
  3268. concatwidestringchar(patternw,asciichar2unicode(char(m)))
  3269. else
  3270. begin
  3271. if len<255 then
  3272. begin
  3273. inc(len);
  3274. pattern[len]:=chr(m);
  3275. end
  3276. else
  3277. begin
  3278. if not msgwritten then
  3279. begin
  3280. Message(scan_e_string_exceeds_255_chars);
  3281. msgwritten:=true;
  3282. end;
  3283. end;
  3284. end;
  3285. end;
  3286. '''' :
  3287. begin
  3288. repeat
  3289. readchar;
  3290. case c of
  3291. #26 :
  3292. end_of_file;
  3293. #10,#13 :
  3294. Message(scan_f_string_exceeds_line);
  3295. '''' :
  3296. begin
  3297. readchar;
  3298. if c<>'''' then
  3299. break;
  3300. end;
  3301. end;
  3302. { interpret as utf-8 string? }
  3303. if (ord(c)>=$80) and (current_settings.sourcecodepage='utf8') then
  3304. begin
  3305. { convert existing string to an utf-8 string }
  3306. if not iswidestring then
  3307. begin
  3308. ascii2unicode(@pattern[1],len,patternw);
  3309. iswidestring:=true;
  3310. len:=0;
  3311. end;
  3312. { four or more chars aren't handled }
  3313. if (ord(c) and $f0)=$f0 then
  3314. message(scan_e_utf8_bigger_than_65535)
  3315. { three chars }
  3316. else if (ord(c) and $e0)=$e0 then
  3317. begin
  3318. w:=ord(c) and $f;
  3319. readchar;
  3320. if (ord(c) and $c0)<>$80 then
  3321. message(scan_e_utf8_malformed);
  3322. w:=(w shl 6) or (ord(c) and $3f);
  3323. readchar;
  3324. if (ord(c) and $c0)<>$80 then
  3325. message(scan_e_utf8_malformed);
  3326. w:=(w shl 6) or (ord(c) and $3f);
  3327. concatwidestringchar(patternw,w);
  3328. end
  3329. { two chars }
  3330. else if (ord(c) and $c0)<>0 then
  3331. begin
  3332. w:=ord(c) and $1f;
  3333. readchar;
  3334. if (ord(c) and $c0)<>$80 then
  3335. message(scan_e_utf8_malformed);
  3336. w:=(w shl 6) or (ord(c) and $3f);
  3337. concatwidestringchar(patternw,w);
  3338. end
  3339. { illegal }
  3340. else if (ord(c) and $80)<>0 then
  3341. message(scan_e_utf8_malformed)
  3342. else
  3343. concatwidestringchar(patternw,tcompilerwidechar(c))
  3344. end
  3345. else if iswidestring then
  3346. begin
  3347. if current_settings.sourcecodepage='utf8' then
  3348. concatwidestringchar(patternw,ord(c))
  3349. else
  3350. concatwidestringchar(patternw,asciichar2unicode(c))
  3351. end
  3352. else
  3353. begin
  3354. if len<255 then
  3355. begin
  3356. inc(len);
  3357. pattern[len]:=c;
  3358. end
  3359. else
  3360. begin
  3361. if not msgwritten then
  3362. begin
  3363. Message(scan_e_string_exceeds_255_chars);
  3364. msgwritten:=true;
  3365. end;
  3366. end;
  3367. end;
  3368. until false;
  3369. end;
  3370. '^' :
  3371. begin
  3372. readchar;
  3373. c:=upcase(c);
  3374. if c<#64 then
  3375. c:=chr(ord(c)+64)
  3376. else
  3377. c:=chr(ord(c)-64);
  3378. if iswidestring then
  3379. concatwidestringchar(patternw,asciichar2unicode(c))
  3380. else
  3381. begin
  3382. if len<255 then
  3383. begin
  3384. inc(len);
  3385. pattern[len]:=c;
  3386. end
  3387. else
  3388. begin
  3389. if not msgwritten then
  3390. begin
  3391. Message(scan_e_string_exceeds_255_chars);
  3392. msgwritten:=true;
  3393. end;
  3394. end;
  3395. end;
  3396. readchar;
  3397. end;
  3398. else
  3399. break;
  3400. end;
  3401. until false;
  3402. { strings with length 1 become const chars }
  3403. if iswidestring then
  3404. begin
  3405. if patternw^.len=1 then
  3406. token:=_CWCHAR
  3407. else
  3408. token:=_CWSTRING;
  3409. end
  3410. else
  3411. begin
  3412. pattern[0]:=chr(len);
  3413. if len=1 then
  3414. token:=_CCHAR
  3415. else
  3416. token:=_CSTRING;
  3417. end;
  3418. goto exit_label;
  3419. end;
  3420. '>' :
  3421. begin
  3422. readchar;
  3423. if (block_type in [bt_type,bt_specialize]) then
  3424. token:=_RSHARPBRACKET
  3425. else
  3426. begin
  3427. case c of
  3428. '=' :
  3429. begin
  3430. readchar;
  3431. token:=_GTE;
  3432. goto exit_label;
  3433. end;
  3434. '>' :
  3435. begin
  3436. readchar;
  3437. token:=_OP_SHR;
  3438. goto exit_label;
  3439. end;
  3440. '<' :
  3441. begin { >< is for a symetric diff for sets }
  3442. readchar;
  3443. token:=_SYMDIF;
  3444. goto exit_label;
  3445. end;
  3446. end;
  3447. token:=_GT;
  3448. end;
  3449. goto exit_label;
  3450. end;
  3451. '<' :
  3452. begin
  3453. readchar;
  3454. if (block_type in [bt_type,bt_specialize]) then
  3455. token:=_LSHARPBRACKET
  3456. else
  3457. begin
  3458. case c of
  3459. '>' :
  3460. begin
  3461. readchar;
  3462. token:=_UNEQUAL;
  3463. goto exit_label;
  3464. end;
  3465. '=' :
  3466. begin
  3467. readchar;
  3468. token:=_LTE;
  3469. goto exit_label;
  3470. end;
  3471. '<' :
  3472. begin
  3473. readchar;
  3474. token:=_OP_SHL;
  3475. goto exit_label;
  3476. end;
  3477. end;
  3478. token:=_LT;
  3479. end;
  3480. goto exit_label;
  3481. end;
  3482. #26 :
  3483. begin
  3484. token:=_EOF;
  3485. checkpreprocstack;
  3486. goto exit_label;
  3487. end;
  3488. else
  3489. Illegal_Char(c);
  3490. end;
  3491. end;
  3492. exit_label:
  3493. lasttoken:=token;
  3494. end;
  3495. function tscannerfile.readpreproc:ttoken;
  3496. begin
  3497. skipspace;
  3498. case c of
  3499. '_',
  3500. 'A'..'Z',
  3501. 'a'..'z' :
  3502. begin
  3503. current_scanner.preproc_pattern:=readid;
  3504. readpreproc:=_ID;
  3505. end;
  3506. '0'..'9' :
  3507. begin
  3508. current_scanner.preproc_pattern:=readval_asstring;
  3509. { realnumber? }
  3510. if c='.' then
  3511. begin
  3512. readchar;
  3513. while c in ['0'..'9'] do
  3514. begin
  3515. current_scanner.preproc_pattern:=current_scanner.preproc_pattern+c;
  3516. readchar;
  3517. end;
  3518. end;
  3519. readpreproc:=_ID;
  3520. end;
  3521. '$','%','&' :
  3522. begin
  3523. current_scanner.preproc_pattern:=readval_asstring;
  3524. readpreproc:=_ID;
  3525. end;
  3526. ',' :
  3527. begin
  3528. readchar;
  3529. readpreproc:=_COMMA;
  3530. end;
  3531. '}' :
  3532. begin
  3533. readpreproc:=_END;
  3534. end;
  3535. '(' :
  3536. begin
  3537. readchar;
  3538. readpreproc:=_LKLAMMER;
  3539. end;
  3540. ')' :
  3541. begin
  3542. readchar;
  3543. readpreproc:=_RKLAMMER;
  3544. end;
  3545. '[' :
  3546. begin
  3547. readchar;
  3548. readpreproc:=_LECKKLAMMER;
  3549. end;
  3550. ']' :
  3551. begin
  3552. readchar;
  3553. readpreproc:=_RECKKLAMMER;
  3554. end;
  3555. '+' :
  3556. begin
  3557. readchar;
  3558. readpreproc:=_PLUS;
  3559. end;
  3560. '-' :
  3561. begin
  3562. readchar;
  3563. readpreproc:=_MINUS;
  3564. end;
  3565. '*' :
  3566. begin
  3567. readchar;
  3568. readpreproc:=_STAR;
  3569. end;
  3570. '/' :
  3571. begin
  3572. readchar;
  3573. readpreproc:=_SLASH;
  3574. end;
  3575. '=' :
  3576. begin
  3577. readchar;
  3578. readpreproc:=_EQUAL;
  3579. end;
  3580. '>' :
  3581. begin
  3582. readchar;
  3583. if c='=' then
  3584. begin
  3585. readchar;
  3586. readpreproc:=_GTE;
  3587. end
  3588. else
  3589. readpreproc:=_GT;
  3590. end;
  3591. '<' :
  3592. begin
  3593. readchar;
  3594. case c of
  3595. '>' :
  3596. begin
  3597. readchar;
  3598. readpreproc:=_UNEQUAL;
  3599. end;
  3600. '=' :
  3601. begin
  3602. readchar;
  3603. readpreproc:=_LTE;
  3604. end;
  3605. else
  3606. readpreproc:=_LT;
  3607. end;
  3608. end;
  3609. #26 :
  3610. begin
  3611. readpreproc:=_EOF;
  3612. checkpreprocstack;
  3613. end;
  3614. else
  3615. Illegal_Char(c);
  3616. end;
  3617. end;
  3618. function tscannerfile.asmgetcharstart : char;
  3619. begin
  3620. { return first the character already
  3621. available in c }
  3622. lastasmgetchar:=c;
  3623. result:=asmgetchar;
  3624. end;
  3625. function tscannerfile.asmgetchar : char;
  3626. begin
  3627. if lastasmgetchar<>#0 then
  3628. begin
  3629. c:=lastasmgetchar;
  3630. lastasmgetchar:=#0;
  3631. end
  3632. else
  3633. readchar;
  3634. if in_asm_string then
  3635. begin
  3636. asmgetchar:=c;
  3637. exit;
  3638. end;
  3639. repeat
  3640. case c of
  3641. // the { ... } is used in ARM assembler to define register sets, so we can't used
  3642. // it as comment, either (* ... *), /* ... */ or // ... should be used instead.
  3643. // But compiler directives {$...} are allowed in ARM assembler.
  3644. '{' :
  3645. begin
  3646. {$ifdef arm}
  3647. readchar;
  3648. dec(inputpointer);
  3649. if c<>'$' then
  3650. begin
  3651. asmgetchar:='{';
  3652. exit;
  3653. end
  3654. else
  3655. {$endif arm}
  3656. skipcomment;
  3657. end;
  3658. #10,#13 :
  3659. begin
  3660. linebreak;
  3661. asmgetchar:=c;
  3662. exit;
  3663. end;
  3664. #26 :
  3665. begin
  3666. reload;
  3667. if (c=#26) and not assigned(inputfile.next) then
  3668. end_of_file;
  3669. continue;
  3670. end;
  3671. '/' :
  3672. begin
  3673. readchar;
  3674. if c='/' then
  3675. skipdelphicomment
  3676. else
  3677. begin
  3678. asmgetchar:='/';
  3679. lastasmgetchar:=c;
  3680. exit;
  3681. end;
  3682. end;
  3683. '(' :
  3684. begin
  3685. readchar;
  3686. if c='*' then
  3687. begin
  3688. c:=#0;{Signal skipoldtpcomment to reload a char }
  3689. skipoldtpcomment;
  3690. end
  3691. else
  3692. begin
  3693. asmgetchar:='(';
  3694. lastasmgetchar:=c;
  3695. exit;
  3696. end;
  3697. end;
  3698. else
  3699. begin
  3700. asmgetchar:=c;
  3701. exit;
  3702. end;
  3703. end;
  3704. until false;
  3705. end;
  3706. {*****************************************************************************
  3707. Helpers
  3708. *****************************************************************************}
  3709. procedure AddDirective(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  3710. begin
  3711. if dm in [directive_all, directive_turbo] then
  3712. tdirectiveitem.create(turbo_scannerdirectives,s,p);
  3713. if dm in [directive_all, directive_mac] then
  3714. tdirectiveitem.create(mac_scannerdirectives,s,p);
  3715. end;
  3716. procedure AddConditional(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  3717. begin
  3718. if dm in [directive_all, directive_turbo] then
  3719. tdirectiveitem.createcond(turbo_scannerdirectives,s,p);
  3720. if dm in [directive_all, directive_mac] then
  3721. tdirectiveitem.createcond(mac_scannerdirectives,s,p);
  3722. end;
  3723. {*****************************************************************************
  3724. Initialization
  3725. *****************************************************************************}
  3726. procedure InitScanner;
  3727. begin
  3728. InitWideString(patternw);
  3729. turbo_scannerdirectives:=TFPHashObjectList.Create;
  3730. mac_scannerdirectives:=TFPHashObjectList.Create;
  3731. { Common directives and conditionals }
  3732. AddDirective('I',directive_all, @dir_include);
  3733. AddDirective('DEFINE',directive_all, @dir_define);
  3734. AddDirective('UNDEF',directive_all, @dir_undef);
  3735. AddConditional('IF',directive_all, @dir_if);
  3736. AddConditional('IFDEF',directive_all, @dir_ifdef);
  3737. AddConditional('IFNDEF',directive_all, @dir_ifndef);
  3738. AddConditional('ELSE',directive_all, @dir_else);
  3739. AddConditional('ELSEIF',directive_all, @dir_elseif);
  3740. AddConditional('ENDIF',directive_all, @dir_endif);
  3741. { Directives and conditionals for all modes except mode macpas}
  3742. AddDirective('INCLUDE',directive_turbo, @dir_include);
  3743. AddDirective('LIBPREFIX',directive_turbo, @dir_libprefix);
  3744. AddDirective('LIBSUFFIX',directive_turbo, @dir_libsuffix);
  3745. AddDirective('EXTENSION',directive_turbo, @dir_extension);
  3746. AddConditional('IFEND',directive_turbo, @dir_endif);
  3747. AddConditional('IFOPT',directive_turbo, @dir_ifopt);
  3748. { Directives and conditionals for mode macpas: }
  3749. AddDirective('SETC',directive_mac, @dir_setc);
  3750. AddDirective('DEFINEC',directive_mac, @dir_definec);
  3751. AddDirective('UNDEFC',directive_mac, @dir_undef);
  3752. AddConditional('IFC',directive_mac, @dir_if);
  3753. AddConditional('ELSEC',directive_mac, @dir_else);
  3754. AddConditional('ELIFC',directive_mac, @dir_elseif);
  3755. AddConditional('ENDC',directive_mac, @dir_endif);
  3756. end;
  3757. procedure DoneScanner;
  3758. begin
  3759. turbo_scannerdirectives.Free;
  3760. mac_scannerdirectives.Free;
  3761. DoneWideString(patternw);
  3762. end;
  3763. end.