scanner.pas 134 KB

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