scanner.pas 104 KB

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