scanner.pas 130 KB

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