You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Parser.jj 72KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972
  1. /*
  2. * Copyright 2000-2013 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. /* -*-java-extended-*-
  17. * Copyright (c) 1999 World Wide Web Consortium
  18. * (Massachusetts Institute of Technology, Institut National de Recherche
  19. * en Informatique et en Automatique, Keio University).
  20. * All Rights Reserved. http://www.w3.org/Consortium/Legal/
  21. *
  22. * $Id: Parser.jj,v 1.15 2000/10/27 21:09:37 plehegar Exp $
  23. */
  24. options {
  25. IGNORE_CASE = true;
  26. STATIC = false;
  27. USER_CHAR_STREAM = true;
  28. /* DEBUG_TOKEN_MANAGER = true;
  29. DEBUG_PARSER = true; */
  30. }
  31. PARSER_BEGIN(Parser)
  32. package com.vaadin.sass.internal.parser;
  33. import java.io.*;
  34. import java.net.*;
  35. import java.util.ArrayList;
  36. import java.util.Locale;
  37. import java.util.Map;
  38. import java.util.UUID;
  39. import org.w3c.css.sac.ConditionFactory;
  40. import org.w3c.css.sac.Condition;
  41. import org.w3c.css.sac.SelectorFactory;
  42. import org.w3c.css.sac.SelectorList;
  43. import org.w3c.css.sac.Selector;
  44. import org.w3c.css.sac.SimpleSelector;
  45. import org.w3c.css.sac.DocumentHandler;
  46. import org.w3c.css.sac.InputSource;
  47. import org.w3c.css.sac.ErrorHandler;
  48. import org.w3c.css.sac.CSSException;
  49. import org.w3c.css.sac.CSSParseException;
  50. import org.w3c.css.sac.Locator;
  51. import org.w3c.css.sac.LexicalUnit;
  52. import org.w3c.flute.parser.selectors.SelectorFactoryImpl;
  53. import org.w3c.flute.parser.selectors.ConditionFactoryImpl;
  54. import org.w3c.flute.util.Encoding;
  55. import com.vaadin.sass.internal.handler.*;
  56. import com.vaadin.sass.internal.tree.*;
  57. /**
  58. * A CSS2 parser
  59. *
  60. * @author Philippe Le H�garet
  61. * @version $Revision: 1.15 $
  62. */
  63. public class Parser implements org.w3c.css.sac.Parser {
  64. // replaces all \t, \n, etc with this StringBuffer.
  65. static final StringBuilder SPACE = new StringBuilder(" ");
  66. // the document handler for the parser
  67. protected SCSSDocumentHandlerImpl documentHandler;
  68. // the error handler for the parser
  69. protected ErrorHandler errorHandler;
  70. // the input source for the parser
  71. protected InputSource source;
  72. protected ConditionFactory conditionFactory;
  73. protected SelectorFactory selectorFactory;
  74. // temporary place holder for pseudo-element ...
  75. private String pseudoElt;
  76. /**
  77. * Creates a new Parser
  78. */
  79. public Parser() {
  80. this((CharStream) null);
  81. }
  82. /**
  83. * @@TODO
  84. * @exception CSSException Not yet implemented
  85. */
  86. public void setLocale(Locale locale) throws CSSException {
  87. throw new CSSException(CSSException.SAC_NOT_SUPPORTED_ERR);
  88. }
  89. public InputSource getInputSource(){
  90. return source;
  91. }
  92. /**
  93. * Set the document handler for this parser
  94. */
  95. public void setDocumentHandler(DocumentHandler handler) {
  96. this.documentHandler = (SCSSDocumentHandlerImpl) handler;
  97. }
  98. public void setSelectorFactory(SelectorFactory selectorFactory) {
  99. this.selectorFactory = selectorFactory;
  100. }
  101. public void setConditionFactory(ConditionFactory conditionFactory) {
  102. this.conditionFactory = conditionFactory;
  103. }
  104. /**
  105. * Set the error handler for this parser
  106. */
  107. public void setErrorHandler(ErrorHandler error) {
  108. this.errorHandler = error;
  109. }
  110. /**
  111. * Main parse methods
  112. *
  113. * @param source the source of the style sheet.
  114. * @exception IOException the source can't be parsed.
  115. * @exception CSSException the source is not CSS valid.
  116. */
  117. public void parseStyleSheet(InputSource source)
  118. throws CSSException, IOException {
  119. this.source = source;
  120. ReInit(getCharStreamWithLurk(source));
  121. if (selectorFactory == null) {
  122. selectorFactory = new SelectorFactoryImpl();
  123. }
  124. if (conditionFactory == null) {
  125. conditionFactory = new ConditionFactoryImpl();
  126. }
  127. parserUnit();
  128. }
  129. /**
  130. * Convenient method for URIs.
  131. *
  132. * @param systemId the fully resolved URI of the style sheet.
  133. * @exception IOException the source can't be parsed.
  134. * @exception CSSException the source is not CSS valid.
  135. */
  136. public void parseStyleSheet(String systemId)
  137. throws CSSException, IOException {
  138. parseStyleSheet(new InputSource(systemId));
  139. }
  140. /**
  141. * This method parses only one rule (style rule or at-rule, except @charset).
  142. *
  143. * @param source the source of the rule.
  144. * @exception IOException the source can't be parsed.
  145. * @exception CSSException the source is not CSS valid.
  146. */
  147. public void parseRule(InputSource source)
  148. throws CSSException, IOException {
  149. this.source = source;
  150. ReInit(getCharStreamWithLurk(source));
  151. if (selectorFactory == null) {
  152. selectorFactory = new SelectorFactoryImpl();
  153. }
  154. if (conditionFactory == null) {
  155. conditionFactory = new ConditionFactoryImpl();
  156. }
  157. _parseRule();
  158. }
  159. /**
  160. * This method parses a style declaration (including the surrounding curly
  161. * braces).
  162. *
  163. * @param source the source of the style declaration.
  164. * @exception IOException the source can't be parsed.
  165. * @exception CSSException the source is not CSS valid.
  166. */
  167. public void parseStyleDeclaration(InputSource source)
  168. throws CSSException, IOException {
  169. this.source = source;
  170. ReInit(getCharStreamWithLurk(source));
  171. if (selectorFactory == null) {
  172. selectorFactory = new SelectorFactoryImpl();
  173. }
  174. if (conditionFactory == null) {
  175. conditionFactory = new ConditionFactoryImpl();
  176. }
  177. _parseDeclarationBlock();
  178. }
  179. /**
  180. * This methods returns "http://www.w3.org/TR/REC-CSS2".
  181. * @return the string "http://www.w3.org/TR/REC-CSS2".
  182. */
  183. public String getParserVersion() {
  184. return "http://www.w3.org/TR/REC-CSS2";
  185. }
  186. /**
  187. * Parse methods used by DOM Level 2 implementation.
  188. */
  189. public void parseImportRule(InputSource source)
  190. throws CSSException, IOException {
  191. this.source = source;
  192. ReInit(getCharStreamWithLurk(source));
  193. if (selectorFactory == null) {
  194. selectorFactory = new SelectorFactoryImpl();
  195. }
  196. if (conditionFactory == null) {
  197. conditionFactory = new ConditionFactoryImpl();
  198. }
  199. _parseImportRule();
  200. }
  201. public void parseMediaRule(InputSource source)
  202. throws CSSException, IOException {
  203. this.source = source;
  204. ReInit(getCharStreamWithLurk(source));
  205. if (selectorFactory == null) {
  206. selectorFactory = new SelectorFactoryImpl();
  207. }
  208. if (conditionFactory == null) {
  209. conditionFactory = new ConditionFactoryImpl();
  210. }
  211. _parseMediaRule();
  212. }
  213. public SelectorList parseSelectors(InputSource source)
  214. throws CSSException, IOException {
  215. this.source = source;
  216. ReInit(getCharStreamWithLurk(source));
  217. return null;
  218. }
  219. public LexicalUnit parsePropertyValue(InputSource source)
  220. throws CSSException, IOException {
  221. this.source = source;
  222. ReInit(getCharStreamWithLurk(source));
  223. return expr();
  224. }
  225. public boolean parsePriority(InputSource source)
  226. throws CSSException, IOException {
  227. this.source = source;
  228. ReInit(getCharStreamWithLurk(source));
  229. return prio();
  230. }
  231. /**
  232. * Convert the source into a Reader. Used only by DOM Level 2 parser methods.
  233. */
  234. private Reader getReader(InputSource source) throws IOException {
  235. if (source.getCharacterStream() != null) {
  236. return source.getCharacterStream();
  237. } else if (source.getByteStream() != null) {
  238. // My DOM level 2 implementation doesn't use this case.
  239. if (source.getEncoding() == null) {
  240. // unknown encoding, use ASCII as default.
  241. return new InputStreamReader(source.getByteStream(), "ASCII");
  242. } else {
  243. return new InputStreamReader(source.getByteStream(),
  244. source.getEncoding());
  245. }
  246. } else {
  247. // systemId
  248. // @@TODO
  249. throw new CSSException("not yet implemented");
  250. }
  251. }
  252. /**
  253. * Convert the source into a CharStream with encoding informations.
  254. * The encoding can be found in the InputSource or in the CSS document.
  255. * Since this method marks the reader and make a reset after looking for
  256. * the charset declaration, you'll find the charset declaration into the
  257. * stream.
  258. */
  259. private CharStream getCharStreamWithLurk(InputSource source)
  260. throws CSSException, IOException {
  261. if (source.getCharacterStream() != null) {
  262. // all encoding are supposed to be resolved by the user
  263. // return the reader
  264. return new Generic_CharStream(source.getCharacterStream(), 1, 1);
  265. } else if (source.getByteStream() == null) {
  266. // @@CONTINUE ME. see also getReader() with systemId
  267. try {
  268. source.setByteStream(new URL(source.getURI()).openStream());
  269. } catch (Exception e) {
  270. try {
  271. source.setByteStream(new FileInputStream(source.getURI()));
  272. } catch (IOException ex) {
  273. throw new CSSException("invalid url ?");
  274. }
  275. }
  276. }
  277. //use UTF-8 as the default encoding.
  278. String encoding = source.getEncoding();
  279. InputStream input = source.getByteStream();
  280. if (!input.markSupported()) {
  281. // If mark is not supported, wrap it in a stream which supports mark
  282. input = new BufferedInputStream(input);
  283. source.setByteStream(input);
  284. }
  285. // Mark either the original stream or the wrapped stream
  286. input.mark(100);
  287. if(encoding == null){
  288. encoding = "ASCII";
  289. char c = ' ';
  290. c = (char) input.read();
  291. if (c == '@') {
  292. // hum, is it a charset ?
  293. int size = 100;
  294. byte[] buf = new byte[size];
  295. input.read(buf, 0, 7);
  296. String keyword = new String(buf, 0, 7);
  297. if (keyword.equals("charset")) {
  298. // Yes, this is the charset declaration !
  299. // here I don't use the right declaration : white space are ' '.
  300. while ((c = (char) input.read()) == ' ') {
  301. // find the first quote
  302. }
  303. char endChar = c;
  304. int i = 0;
  305. if ((endChar != '"') && (endChar != '\'')) {
  306. // hum this is not a quote.
  307. throw new CSSException("invalid charset declaration");
  308. }
  309. while ((c = (char) input.read()) != endChar) {
  310. buf[i++] = (byte) c;
  311. if (i == size) {
  312. byte[] old = buf;
  313. buf = new byte[size + 100];
  314. System.arraycopy(old, 0, buf, 0, size);
  315. size += 100;
  316. }
  317. }
  318. while ((c = (char) input.read()) == ' ') {
  319. // find the next relevant character
  320. }
  321. if (c != ';') {
  322. // no semi colon at the end ?
  323. throw new CSSException("invalid charset declaration: "
  324. + "missing semi colon");
  325. }
  326. encoding = new String(buf, 0, i);
  327. if (source.getEncoding() != null) {
  328. // compare the two encoding informations.
  329. // For example, I don't accept to have ASCII and after UTF-8.
  330. // Is it really good ? That is the question.
  331. if (!encoding.equals(source.getEncoding())) {
  332. throw new CSSException("invalid encoding information.");
  333. }
  334. }
  335. } // else no charset declaration available
  336. }
  337. }
  338. // ok set the real encoding of this source.
  339. source.setEncoding(encoding);
  340. // set the real reader of this source.
  341. source.setCharacterStream(new InputStreamReader(source.getByteStream(),
  342. Encoding.getJavaEncoding(encoding)));
  343. // reset the stream (leave the charset declaration in the stream).
  344. input.reset();
  345. return new Generic_CharStream(source.getCharacterStream(), 1, 1);
  346. }
  347. private LocatorImpl currentLocator;
  348. private Locator getLocator() {
  349. if (currentLocator == null) {
  350. currentLocator = new LocatorImpl(this);
  351. return currentLocator;
  352. }
  353. return currentLocator.reInit(this);
  354. }
  355. private LocatorImpl getLocator(Token save) {
  356. if (currentLocator == null) {
  357. currentLocator = new LocatorImpl(this, save);
  358. return currentLocator;
  359. }
  360. return currentLocator.reInit(this, save);
  361. }
  362. private void reportError(Locator l, Exception e) {
  363. if (errorHandler != null) {
  364. if (e instanceof ParseException) {
  365. // construct a clean error message.
  366. ParseException pe = (ParseException) e;
  367. if (pe.specialConstructor) {
  368. StringBuffer errorM = new StringBuffer();
  369. if (pe.currentToken != null) {
  370. errorM.append("encountered \"")
  371. .append(pe.currentToken.next);
  372. }
  373. errorM.append('"');
  374. if (pe.expectedTokenSequences.length != 0) {
  375. errorM.append(". Was expecting one of: ");
  376. for (int i = 0; i < pe.expectedTokenSequences.length; i++) {
  377. for (int j = 0; j < pe.expectedTokenSequences[i].length; j++) {
  378. int kind = pe.expectedTokenSequences[i][j];
  379. if (kind != S) {
  380. errorM.append(pe.tokenImage[kind]);
  381. errorM.append(' ');
  382. }
  383. }
  384. }
  385. }
  386. errorHandler.error(new CSSParseException(errorM.toString(),
  387. l, e));
  388. } else {
  389. errorHandler.error(new CSSParseException(e.getMessage(),
  390. l, e));
  391. }
  392. } else if (e == null) {
  393. errorHandler.error(new CSSParseException("error", l, null));
  394. } else {
  395. errorHandler.error(new CSSParseException(e.getMessage(), l, e));
  396. }
  397. }
  398. }
  399. private void reportWarningSkipText(Locator l, String text) {
  400. if (errorHandler != null && text != null) {
  401. errorHandler.warning(new CSSParseException("Skipping: " + text, l));
  402. }
  403. }
  404. }
  405. PARSER_END(Parser)
  406. /*
  407. * The tokenizer
  408. */
  409. <DEFAULT>
  410. TOKEN :
  411. {
  412. < S : ( [ " ", "\t" , "\n" , "\r", "\f" ] )+ >
  413. { image = Parser.SPACE; }
  414. }
  415. <DEFAULT>
  416. MORE :
  417. {
  418. "//" : IN_SINGLE_LINE_COMMENT
  419. }
  420. <IN_SINGLE_LINE_COMMENT>
  421. MORE :
  422. {
  423. < ~["\n","\r"] > : IN_SINGLE_LINE_COMMENT
  424. }
  425. <IN_SINGLE_LINE_COMMENT>
  426. SKIP :
  427. {
  428. < "\n"|"\r"|"\r\n" > : DEFAULT
  429. }
  430. <DEFAULT>
  431. MORE :
  432. {
  433. <"/**" ~["/"]> { input_stream.backup(1); } : IN_FORMAL_COMMENT
  434. |
  435. "/*" : IN_MULTI_LINE_COMMENT
  436. }
  437. <IN_FORMAL_COMMENT>
  438. SPECIAL_TOKEN :
  439. {
  440. <FORMAL_COMMENT: "*/" > : DEFAULT
  441. }
  442. <IN_MULTI_LINE_COMMENT>
  443. SKIP :
  444. {
  445. <MULTI_LINE_COMMENT: "*/" > : DEFAULT
  446. }
  447. <IN_FORMAL_COMMENT,IN_MULTI_LINE_COMMENT>
  448. MORE :
  449. {
  450. < ~[] >
  451. }
  452. <DEFAULT>
  453. TOKEN :
  454. {
  455. < CDO : "<!--" >
  456. | < CDC : "-->" >
  457. | < LBRACE : "{" >
  458. | < RBRACE : "}">
  459. | < DASHMATCH : "|=" >
  460. | < CARETMATCH : "^=" >
  461. | < DOLLARMATCH : "$=" >
  462. | < STARMATCH : "*=" >
  463. | < INCLUDES : "~=" >
  464. | < EQ : "=" >
  465. | < PLUS : "+" >
  466. | < MINUS : "-" >
  467. | < COMMA : "," >
  468. | < SEMICOLON : ";" >
  469. | < PRECEDES : ">" >
  470. | < SIBLING : "~" >
  471. | < SUCCEEDS : "<" >
  472. | < DIV : "/" >
  473. | < LBRACKET : "[" >
  474. | < RBRACKET : "]" >
  475. | < ANY : "*" >
  476. | < MOD : "%" >
  477. | < PARENT : "&" >
  478. | < DOT : "." >
  479. | < LPARAN : "(" >
  480. | < RPARAN : ")">
  481. | < COMPARE : "==" >
  482. | < OR : "||" >
  483. | < AND : "&&" >
  484. | < NOT_EQ : "!=" >
  485. }
  486. <DEFAULT>
  487. TOKEN :
  488. {
  489. < COLON : ":" >
  490. }
  491. < DEFAULT >
  492. TOKEN :
  493. {
  494. < INTERPOLATION : "#{"< VARIABLE > "}">
  495. }
  496. <DEFAULT>
  497. TOKEN : /* basic tokens */
  498. {
  499. < NONASCII : ["\200"-"\377"] >
  500. | < #H : ["0"-"9", "a"-"f"] >
  501. | < #UNICODE : "\\" <H> ( <H> )? /* I can't say {1,6} */
  502. ( <H> )? ( <H> )?
  503. ( <H> )? ( <H> )?
  504. ( [ " ", "\t" , "\n" , "\r", "\f" ] )? >
  505. | < #ESCAPE : <UNICODE> | ( "\\" [ " "-"~","\200"-"\377" ] ) >
  506. | < #NMSTART : ("-")?[ "a"-"z"] | <NONASCII> | <ESCAPE> >
  507. | < #NMCHAR : ["a"-"z", "0"-"9", "-", "_"] | <NONASCII> | <ESCAPE> >
  508. | < #STRINGCHAR : [ "\t"," ","!","#","$","%","&","("-"~" ]
  509. | "\\\n" | "\\\r\n" | "\\\r" | "\\\f"
  510. | <NONASCII> | <ESCAPE> >
  511. | < #D : ["0"-"9"] >
  512. | < #NAME : ( <NMCHAR> )+ >
  513. }
  514. <DEFAULT>
  515. TOKEN :
  516. {
  517. <TO : "to">
  518. |<THROUGH : "through">
  519. |<EACH_IN : "in">
  520. |<FROM : "from">
  521. }
  522. /* DERECTIVES */
  523. <DEFAULT>
  524. TOKEN :
  525. {
  526. <MIXIN_SYM : "@mixin">
  527. | <INCLUDE_SYM : "@include">
  528. | <FUNCTION_SYM : "@function">
  529. | <RETURN_SYM : "@return">
  530. | <DEBUG_SYM : "@debug">
  531. | <WARN_SYM : "@warn">
  532. | <FOR_SYM : "@for">
  533. | <EACH_SYM : "@each">
  534. | <WHILE_SYM : "@while">
  535. | <IF_SYM : "@if">
  536. | <ELSE_SYM : "@else">
  537. | <EXTEND_SYM : "@extend">
  538. | <MOZ_DOCUMENT_SYM : "@-moz-document">
  539. | <SUPPORTS_SYM : "@supports">
  540. }
  541. < DEFAULT >
  542. TOKEN:
  543. {
  544. < MICROSOFT_RULE : "filter"|"-ms-filter" >
  545. }
  546. < DEFAULT >
  547. TOKEN:
  548. {
  549. < IF : "if" >
  550. }
  551. <DEFAULT>
  552. TOKEN:
  553. {
  554. < GUARDED_SYM : "!" ( <S> )? "default">
  555. }
  556. <DEFAULT>
  557. TOKEN :
  558. {
  559. < STRING : ( "\"" ( <STRINGCHAR> | "'" )* "\"" ) |
  560. ( "'" ( <STRINGCHAR> | "\"" )* "'" ) >
  561. | < IDENT : <NMSTART> ( <NMCHAR> )* >
  562. | < NUMBER : ( <D> )+ | ( <D> )* "." ( <D> )+ >
  563. | < #_URL : [ "!","#","$","%","&","*"-"~" ] | <NONASCII> | <ESCAPE> >
  564. | < URL : "url(" ( <S> )*
  565. ( <STRING> | ( <_URL> )* ) ( <S> )* ")" >
  566. }
  567. <DEFAULT>
  568. TOKEN:
  569. {
  570. < VARIABLE : "$" <IDENT>>
  571. }
  572. <DEFAULT>
  573. TOKEN :
  574. {
  575. < PERCENTAGE : <NUMBER> "%" >
  576. | < PT : <NUMBER> "pt" >
  577. | < MM : <NUMBER> "mm" >
  578. | < CM : <NUMBER> "cm" >
  579. | < PC : <NUMBER> "pc" >
  580. | < IN : <NUMBER> "in" >
  581. | < PX : <NUMBER> "px" >
  582. | < EMS : <NUMBER> "em" >
  583. | < LEM : <NUMBER> "lem" >
  584. | < REM : <NUMBER> "rem" >
  585. | < EXS : <NUMBER> "ex" >
  586. | < DEG : <NUMBER> "deg" >
  587. | < RAD : <NUMBER> "rad" >
  588. | < GRAD : <NUMBER> "grad" >
  589. | < MS : <NUMBER> "ms" >
  590. | < SECOND : <NUMBER> "s" >
  591. | < HZ : <NUMBER> "Hz" >
  592. | < KHZ : <NUMBER> "kHz" >
  593. | < DIMEN : <NUMBER> <IDENT> >
  594. }
  595. <DEFAULT>
  596. TOKEN :
  597. {
  598. < HASH : "#" <NAME> >
  599. }
  600. /* RESERVED ATRULE WORDS */
  601. <DEFAULT>
  602. TOKEN :
  603. {
  604. < IMPORT_SYM : "@import">
  605. | < MEDIA_SYM : "@media" >
  606. | < CHARSET_SYM : "@charset" >
  607. | < PAGE_SYM : "@page" >
  608. | < FONT_FACE_SYM: "@font-face" >
  609. | < KEY_FRAME_SYM: "@keyframes" | "@-moz-keyframes" | "@-o-keyframes" | "@-webkit-keyframes" | "@-ms-keyframes">
  610. | < ATKEYWORD : "@" <IDENT> >
  611. }
  612. <DEFAULT>
  613. TOKEN :
  614. {
  615. < IMPORTANT_SYM : "!" ( <S> )? "important" >
  616. }
  617. <DEFAULT>
  618. TOKEN :
  619. {
  620. < #RANGE0 : <H> <H> <H> <H> <H> >
  621. | < #RANGE1 : <H> <H> <H> <H> <H> ( "?" )? >
  622. | < #RANGE2 : <H> <H> <H> <H> ( "?" )? ( "?" )? >
  623. | < #RANGE3 : <H> <H> <H> ( "?" )? ( "?" )? ( "?" )? >
  624. | < #RANGE4 : <H> <H> ( "?" )? ( "?" )? ( "?" )? ( "?" )? >
  625. | < #RANGE5 : <H> ( "?" )? ( "?" )? ( "?" )? ( "?" )? ( "?" )? >
  626. | < #RANGE6 : "?" ( "?" )? ( "?" )? ( "?" )? ( "?" )? ( "?" )? >
  627. | < #RANGE : <RANGE0> | <RANGE1> | <RANGE2>
  628. | <RANGE3> | <RANGE4> | <RANGE5> | <RANGE6> >
  629. | < #UNI : <H> ( <H> )? ( <H> )? ( <H> )? ( <H> )? ( <H> )? >
  630. | < UNICODERANGE : "U+" <RANGE>
  631. | "U+" <UNI> "-" <UNI> >
  632. }
  633. < DEFAULT >
  634. TOKEN :
  635. {
  636. < REMOVE : "remove" (< S >)? "(" >
  637. | < APPEND : "append" (< S >)? "(" >
  638. | < CONTAINS : "contains" (< S >)? "(" >
  639. }
  640. <DEFAULT>
  641. TOKEN :
  642. {
  643. < FUNCTION : <IDENT>(< S >)* "(" >
  644. }
  645. <DEFAULT, IN_MULTI_LINE_COMMENT>
  646. TOKEN :
  647. { /* avoid token manager error */
  648. < UNKNOWN : ~[] >
  649. }
  650. /*
  651. * The grammar of CSS2
  652. */
  653. /**
  654. * The main entry for the parser.
  655. *
  656. * @exception ParseException exception during the parse
  657. */
  658. void parserUnit() :
  659. {}
  660. {
  661. try {
  662. { documentHandler.startDocument(source); }
  663. ( charset() )?
  664. ( <S> comments()
  665. | ignoreStatement() )*
  666. ( importDeclaration() ( ignoreStatement() ( <S> )* )* )*
  667. afterImportDeclaration()
  668. <EOF>
  669. } finally {
  670. documentHandler.endDocument(source);
  671. }
  672. }
  673. void charset() :
  674. { Token n; }
  675. {
  676. try {
  677. <CHARSET_SYM> ( <S> )* n=<STRING> ( <S> )* ";"
  678. } catch (ParseException e) {
  679. reportError(getLocator(e.currentToken.next), e);
  680. skipStatement();
  681. // reportWarningSkipText(getLocator(), skipStatement());
  682. } catch (Exception e) {
  683. reportError(getLocator(), e);
  684. skipStatement();
  685. // reportWarningSkipText(getLocator(), skipStatement());
  686. }
  687. }
  688. void afterImportDeclaration() :
  689. {String ret;
  690. Locator l;
  691. }
  692. {
  693. (
  694. ( debuggingDirective() | mixinDirective() | controlDirective() | includeDirective() | styleRule() | media()
  695. | page() | fontFace() | keyframes() | LOOKAHEAD(variable()) variable() | listModifyDirective()
  696. | { l = getLocator(); } ret=skipStatement()
  697. {
  698. if ((ret == null) || (ret.length() == 0)) {
  699. return;
  700. }
  701. if (ret.charAt(0) == '@') {
  702. documentHandler.unrecognizedRule(ret);
  703. } else {
  704. reportWarningSkipText(l, ret);
  705. }
  706. }
  707. )
  708. ( ignoreStatement() ( <S> )* )* )*
  709. }
  710. void ignoreStatement() :
  711. {}
  712. {
  713. <CDO> | <CDC> | atRuleDeclaration()
  714. }
  715. /**
  716. * The import statement
  717. *
  718. * @exception ParseException exception during the parse
  719. */
  720. void importDeclaration() :
  721. {Token n;
  722. String uri;
  723. MediaListImpl ml = new MediaListImpl();
  724. boolean isURL = false;
  725. }
  726. {
  727. try {
  728. <IMPORT_SYM>
  729. ( <S> )* ( n=<STRING> { uri = convertStringIndex(n.image, 1,
  730. n.image.length() -1); }
  731. | n=<URL>
  732. {
  733. isURL=true;
  734. uri = n.image.substring(4, n.image.length()-1).trim();
  735. if ((uri.charAt(0) == '"')
  736. || (uri.charAt(0) == '\'')) {
  737. uri = uri.substring(1, uri.length()-1);
  738. }
  739. }
  740. )
  741. ( <S> )* ( mediaStatement(ml) )? ";"
  742. ( <S> )*
  743. {
  744. if (ml.getLength() == 0) {
  745. // see section 6.3 of the CSS2 recommandation.
  746. ml.addItem("all");
  747. }
  748. documentHandler.importStyle(uri, ml, isURL);
  749. }
  750. } catch (ParseException e) {
  751. reportError(getLocator(), e);
  752. skipStatement();
  753. // reportWarningSkipText(getLocator(), skipStatement());
  754. }
  755. }
  756. /**
  757. * @exception ParseException exception during the parse
  758. */
  759. void keyframes() :
  760. {
  761. Token n;
  762. boolean start = false;
  763. String keyframeName = null;
  764. String animationname = "";
  765. }
  766. {
  767. try {
  768. n=<KEY_FRAME_SYM> ( <S> )* {keyframeName = n.image;}
  769. (n = <IDENT>{animationname += n.image; }|n = < INTERPOLATION >{ animationname += n.image; })+(<S>)*
  770. {start = true; documentHandler.startKeyFrames(keyframeName, animationname); }
  771. <LBRACE> ( <S> )* ( keyframeSelector() )* <RBRACE> ( <S> )*
  772. } catch (ParseException e) {
  773. reportError(getLocator(), e);
  774. skipStatement();
  775. } finally {
  776. if (start) {
  777. documentHandler.endKeyFrames();
  778. }
  779. }
  780. }
  781. void keyframeSelector():
  782. {
  783. Token n;
  784. boolean start = false;
  785. }
  786. {
  787. try{
  788. (n = <FROM> | n = <TO> | n = <PERCENTAGE>) (<S>)* <LBRACE> (<S>)*
  789. {
  790. start = true;
  791. documentHandler.startKeyframeSelector(n.image);
  792. }
  793. (ifContentStatement() | controlDirective() | microsoftExtension() )*
  794. <RBRACE> (<S>)*
  795. }
  796. catch (ThrowedParseException e) {
  797. if (errorHandler != null) {
  798. LocatorImpl li = new LocatorImpl(this,
  799. e.e.currentToken.next.beginLine,
  800. e.e.currentToken.next.beginColumn-1);
  801. reportError(li, e.e);
  802. }
  803. } catch (ParseException e) {
  804. reportError(getLocator(), e);
  805. skipStatement();
  806. // reportWarningSkipText(getLocator(), skipStatement());
  807. } catch (TokenMgrError e) {
  808. reportWarningSkipText(getLocator(), skipStatement());
  809. } finally {
  810. if (start) {
  811. documentHandler.endKeyframeSelector();
  812. }
  813. }
  814. }
  815. /**
  816. * @exception ParseException exception during the parse
  817. */
  818. void media() :
  819. {
  820. boolean start = false;
  821. String ret;
  822. MediaListImpl ml = new MediaListImpl();
  823. }
  824. {
  825. try {
  826. <MEDIA_SYM> ( <S> )*
  827. mediaStatement(ml)
  828. { start = true; documentHandler.startMedia(ml); }
  829. <LBRACE> ( <S> )* ( debuggingDirective() | styleRule() | skipUnknownRule() )* <RBRACE> ( <S> )*
  830. } catch (ParseException e) {
  831. reportError(getLocator(), e);
  832. skipStatement();
  833. // reportWarningSkipText(getLocator(), skipStatement());
  834. } finally {
  835. if (start) {
  836. documentHandler.endMedia(ml);
  837. }
  838. }
  839. }
  840. void mediaStatement(MediaListImpl ml) :
  841. {
  842. String m;
  843. }
  844. {
  845. m=medium() ( <COMMA> ( <S> )* { ml.addItem(m); } m=medium() )*
  846. { ml.addItem(m); }
  847. }
  848. /**
  849. * @exception ParseException exception during the parse
  850. */
  851. String medium() : /* tv, projection, screen, ... */
  852. {Token n;}
  853. {
  854. n=<IDENT> ( <S> )* { return convertIdent(n.image); }
  855. }
  856. /**
  857. * @exception ParseException exception during the parse
  858. */
  859. void page() :
  860. {
  861. boolean start = false;
  862. Token n = null;
  863. String page = null;
  864. String pseudo = null;
  865. }
  866. {
  867. try {
  868. <PAGE_SYM> ( <S> )* ( n=<IDENT> ( <S> )* )?
  869. ( pseudo=pseudo_page() )?
  870. {
  871. if (n != null) {
  872. page = convertIdent(n.image);
  873. }
  874. }
  875. <LBRACE> (<S>)*
  876. {
  877. start = true;
  878. documentHandler.startPage(page, pseudo);
  879. }
  880. ( declaration() )? ( ";" ( <S> )* ( declaration() )? )*
  881. <RBRACE> (<S>)*
  882. } catch (ParseException e) {
  883. if (errorHandler != null) {
  884. LocatorImpl li = new LocatorImpl(this,
  885. e.currentToken.next.beginLine,
  886. e.currentToken.next.beginColumn-1);
  887. reportError(li, e);
  888. skipStatement();
  889. // reportWarningSkipText(li, skipStatement());
  890. } else {
  891. skipStatement();
  892. }
  893. } finally {
  894. if (start) {
  895. documentHandler.endPage(page, pseudo);
  896. }
  897. }
  898. }
  899. String pseudo_page() :
  900. { Token n; }
  901. {
  902. ":" n=<IDENT> ( <S> )* { return convertIdent(n.image); }
  903. }
  904. void fontFace() :
  905. {
  906. boolean start = false;
  907. }
  908. {
  909. try {
  910. <FONT_FACE_SYM> ( <S> )*
  911. <LBRACE> (<S>)*
  912. { start = true; documentHandler.startFontFace(); }
  913. ( declaration() )? ( ";" ( <S> )* ( declaration() )? )*
  914. <RBRACE> (<S>)*
  915. } catch (ParseException e) {
  916. reportError(getLocator(), e);
  917. skipStatement();
  918. // reportWarningSkipText(getLocator(), skipStatement());
  919. } finally {
  920. if (start) {
  921. documentHandler.endFontFace();
  922. }
  923. }
  924. }
  925. /**
  926. * @exception ParseException exception during the parse
  927. */
  928. void atRuleDeclaration() :
  929. {Token n;
  930. String ret;
  931. }
  932. {
  933. n=<ATKEYWORD>
  934. {
  935. ret=skipStatement();
  936. if ((ret != null) && (ret.charAt(0) == '@')) {
  937. documentHandler.unrecognizedRule(ret);
  938. } else {
  939. reportWarningSkipText(getLocator(), ret);
  940. }
  941. }
  942. }
  943. void skipUnknownRule() :
  944. { Token n;}
  945. {
  946. ( n=<ATKEYWORD>
  947. | n=<CDO>
  948. | n=<CHARSET_SYM>
  949. | n=<COMMA>
  950. | n=<DASHMATCH>
  951. | n=<FONT_FACE_SYM>
  952. | n=<FUNCTION>
  953. | n=<IMPORTANT_SYM>
  954. | n=<IMPORT_SYM>
  955. | n=<INCLUDES>
  956. | n=<LBRACE>
  957. | n=<MEDIA_SYM>
  958. | n=<NONASCII>
  959. | n=<NUMBER>
  960. | n=<PAGE_SYM>
  961. | n=<PERCENTAGE>
  962. | n=<STRING>
  963. | n=<UNICODERANGE>
  964. | n=<URL>
  965. | n=";"
  966. | n="-"
  967. | n=<UNKNOWN>
  968. ) {
  969. String ret;
  970. Locator loc = getLocator();
  971. ret=skipStatement();
  972. if ((ret != null) && (n.image.charAt(0) == '@')) {
  973. documentHandler.unrecognizedRule(ret);
  974. } else {
  975. reportWarningSkipText(loc, ret);
  976. }
  977. }
  978. }
  979. /**
  980. * @exception ParseException exception during the parse
  981. */
  982. char combinator() :
  983. {
  984. char connector = ' ';
  985. }
  986. {
  987. (connector = combinatorChar()
  988. | <S> (connector = combinatorChar())?) { return connector; }
  989. }
  990. /**to refactor combinator and reuse in selector().*/
  991. char combinatorChar() :
  992. {Token t;}
  993. {
  994. (t = <PLUS> | t = <PRECEDES> | t = <SIBLING>) (<S>)*
  995. {
  996. return t.image.charAt(0);
  997. }
  998. }
  999. void microsoftExtension() :
  1000. {
  1001. Token n;
  1002. String name = "";
  1003. String value = "";
  1004. }
  1005. {
  1006. // This is not really taking the syntax of filter rules into account
  1007. n = < MICROSOFT_RULE > (< S >)* { name = n.image; }
  1008. < COLON >
  1009. ((n = < IDENT > { value += n.image; })
  1010. | (n = < NUMBER > { value += n.image; })
  1011. | (n = < STRING > { value += n.image; })
  1012. | (n = < COMMA > { value += n.image; })
  1013. | (n = < INTERPOLATION > { value += n.image; })
  1014. | (n = < COLON > { value += n.image; })
  1015. | (n = < FUNCTION > { value += n.image; })
  1016. | (n = < RPARAN > { value += n.image; })
  1017. | (n = < EQ > { value += n.image; })
  1018. | (n = < DOT > { value += n.image; })
  1019. | (n = < S > { if(value.lastIndexOf(' ') != value.length()-1)
  1020. { value += n.image; } }
  1021. ) )+
  1022. < SEMICOLON >
  1023. (< S >)*
  1024. { documentHandler.microsoftDirective(name, value); }
  1025. }
  1026. /**
  1027. * @exception ParseException exception during the parse
  1028. */
  1029. String property() :
  1030. {Token t;String s = "";
  1031. }
  1032. {
  1033. (t = <IDENT>{s += t.image; }|t = < INTERPOLATION >{ s += t.image; })+(< S >)*
  1034. {
  1035. return s;
  1036. }
  1037. }
  1038. String variableName() :
  1039. {Token n;}
  1040. {
  1041. n=<VARIABLE> (<S>)* {return convertIdent(n.image.substring(1));}
  1042. }
  1043. String functionName() :
  1044. {Token n;}
  1045. {
  1046. n=<FUNCTION> ( <S> )* {return convertIdent(n.image.substring(0, n.image.length()-1));}
  1047. }
  1048. /**
  1049. * @exception ParseException exception during the parse
  1050. */
  1051. void styleRule() :
  1052. {
  1053. boolean start = false;
  1054. ArrayList<String> l = null;
  1055. Token save;
  1056. Locator loc;
  1057. }
  1058. {
  1059. try {
  1060. l=selectorList() { save = token; } <LBRACE> (<S>)*
  1061. {
  1062. start = true;
  1063. documentHandler.startSelector(l);
  1064. }
  1065. ( ifContentStatement() | controlDirective() | microsoftExtension() )*
  1066. <RBRACE> (<S>)*
  1067. } catch (ThrowedParseException e) {
  1068. if (errorHandler != null) {
  1069. LocatorImpl li = new LocatorImpl(this,
  1070. e.e.currentToken.next.beginLine,
  1071. e.e.currentToken.next.beginColumn-1);
  1072. reportError(li, e.e);
  1073. }
  1074. } catch (ParseException e) {
  1075. reportError(getLocator(), e);
  1076. skipStatement();
  1077. // reportWarningSkipText(getLocator(), skipStatement());
  1078. } catch (TokenMgrError e) {
  1079. reportWarningSkipText(getLocator(), skipStatement());
  1080. } finally {
  1081. if (start) {
  1082. documentHandler.endSelector();
  1083. }
  1084. }
  1085. }
  1086. ArrayList<String> selectorList() :
  1087. {
  1088. ArrayList<String> selectors = new ArrayList<String>();
  1089. String selector;
  1090. }
  1091. {
  1092. selector=selector() ( <COMMA> (<S>)* { selectors.add(selector); }
  1093. selector=selector() )*
  1094. { selectors.add(selector);
  1095. return selectors;
  1096. }
  1097. }
  1098. /**
  1099. * @exception ParseException exception during the parse
  1100. */
  1101. String selector() :
  1102. {
  1103. String selector = null;
  1104. char comb;
  1105. }
  1106. {
  1107. try {
  1108. // the selector can begin either a simple_selector, or a combinatorChar(+, >, ~).
  1109. // when beginning with combinatorChar, the next one should be a simple_selector().
  1110. (selector=simple_selector(null, ' ') | (comb=combinatorChar() selector=simple_selector(selector, comb)))
  1111. ( LOOKAHEAD(2) comb=combinator()
  1112. selector=simple_selector(selector, comb) )* (<S>)*
  1113. {
  1114. return selector;
  1115. }
  1116. } catch (ParseException e) {
  1117. /*
  1118. Token t = getToken(1);
  1119. StringBuffer s = new StringBuffer();
  1120. s.append(getToken(0).image);
  1121. while ((t.kind != COMMA) && (t.kind != SEMICOLON)
  1122. && (t.kind != LBRACE) && (t.kind != EOF)) {
  1123. s.append(t.image);
  1124. getNextToken();
  1125. t = getToken(1);
  1126. }
  1127. reportWarningSkipText(getLocator(), s.toString());
  1128. */
  1129. Token t = getToken(1);
  1130. while ((t.kind != COMMA) && (t.kind != SEMICOLON)
  1131. && (t.kind != LBRACE) && (t.kind != EOF)) {
  1132. getNextToken();
  1133. t = getToken(1);
  1134. }
  1135. throw new ThrowedParseException(e);
  1136. }
  1137. }
  1138. /**
  1139. * @exception ParseException exception during the parse
  1140. */
  1141. String simple_selector(String selector, char comb) :
  1142. {
  1143. String simple_current = null;
  1144. String cond = null;
  1145. pseudoElt = null;
  1146. }
  1147. {
  1148. ( simple_current=element_name()
  1149. ( cond=hash(cond) | cond=_class(cond)
  1150. | cond=attrib(cond) | cond=pseudo(cond) )*
  1151. | cond=hash(cond) ( cond=_class(cond)
  1152. | cond=attrib(cond) | cond=pseudo(cond) )*
  1153. | cond=_class(cond) ( cond=hash(cond) | cond=_class(cond)
  1154. | cond=attrib(cond) | cond=pseudo(cond) )*
  1155. | cond=pseudo(cond) ( cond=hash(cond) | cond=_class(cond)
  1156. | cond=attrib(cond) | cond=pseudo(cond) )*
  1157. | cond=attrib(cond) ( cond=hash(cond) | cond=_class(cond)
  1158. | cond=attrib(cond) | cond=pseudo(cond) )*
  1159. )
  1160. {
  1161. if (simple_current == null) {
  1162. simple_current = "";
  1163. }
  1164. if (cond != null) {
  1165. simple_current = simple_current + cond;
  1166. }
  1167. StringBuilder builder = new StringBuilder();
  1168. switch (comb) {
  1169. case ' ':
  1170. if(selector!=null){
  1171. builder.append(selector).append(" ");
  1172. }
  1173. break;
  1174. case '+':
  1175. case '>':
  1176. case '~':
  1177. if(selector!=null){
  1178. builder.append(selector).append(" ");
  1179. }
  1180. builder.append(comb).append(" ");
  1181. break;
  1182. default:
  1183. throw new ParseException("invalid state. send a bug report");
  1184. }
  1185. builder.append(simple_current);
  1186. selector = builder.toString();
  1187. if (pseudoElt != null) {
  1188. selector = selector + pseudoElt;
  1189. }
  1190. return selector;
  1191. }
  1192. }
  1193. /**
  1194. * @exception ParseException exception during the parse
  1195. */
  1196. String _class(String pred) :
  1197. {Token t;
  1198. String s = ".";
  1199. }
  1200. {
  1201. "." (t = <IDENT>{s += t.image; }|t = < INTERPOLATION >{ s += t.image; })+
  1202. {
  1203. if (pred == null) {
  1204. return s;
  1205. } else {
  1206. return pred + s;
  1207. }
  1208. }
  1209. }
  1210. /**
  1211. * @exception ParseException exception during the parse
  1212. */
  1213. String element_name() :
  1214. {Token t; String s = "";}
  1215. {
  1216. (t = <IDENT>{s += t.image; }|t = < INTERPOLATION >{ s += t.image; })+
  1217. {
  1218. return s;
  1219. }
  1220. | "*"
  1221. { return "*"; }
  1222. | "&"
  1223. { return "&"; }
  1224. }
  1225. /**
  1226. * @exception ParseException exception during the parse
  1227. */
  1228. String attrib(String pred) :
  1229. {
  1230. int cases = 0;
  1231. Token att = null;
  1232. Token val = null;
  1233. String attValue = null;
  1234. }
  1235. {
  1236. "[" ( <S> )* att=<IDENT> ( <S> )*
  1237. ( ( "=" { cases = 1; }
  1238. | <INCLUDES> { cases = 2; }
  1239. | <DASHMATCH> { cases = 3; }
  1240. | <CARETMATCH> { cases = 4; }
  1241. | <DOLLARMATCH> { cases = 5; }
  1242. | <STARMATCH> { cases = 6; } ) ( <S> )*
  1243. ( val=<IDENT> { attValue = val.image; }
  1244. | val=<STRING> { attValue = val.image; }
  1245. )
  1246. ( <S> )* )?
  1247. "]"
  1248. {
  1249. String name = convertIdent(att.image);
  1250. String c;
  1251. switch (cases) {
  1252. case 0:
  1253. c = name;
  1254. break;
  1255. case 1:
  1256. c = name + "=" + attValue;
  1257. break;
  1258. case 2:
  1259. c = name + "~=" + attValue;
  1260. break;
  1261. case 3:
  1262. c = name + "|=" +attValue;
  1263. break;
  1264. case 4:
  1265. c = name + "^=" +attValue;
  1266. break;
  1267. case 5:
  1268. c = name + "$=" +attValue;
  1269. break;
  1270. case 6:
  1271. c = name + "*=" +attValue;
  1272. break;
  1273. default:
  1274. // never reached.
  1275. c = null;
  1276. }
  1277. c = "[" + c + "]";
  1278. if (pred == null) {
  1279. return c;
  1280. } else {
  1281. return pred + c;
  1282. }
  1283. }
  1284. }
  1285. /**
  1286. * @exception ParseException exception during the parse
  1287. */
  1288. String pseudo(String pred) :
  1289. {Token n;
  1290. Token param;
  1291. String d;
  1292. boolean isPseudoElement = false;
  1293. }
  1294. {
  1295. ":" (":"{isPseudoElement=true;})?( n=<IDENT>
  1296. {
  1297. String s = ":" + convertIdent(n.image);
  1298. if (isPseudoElement) {
  1299. if (pseudoElt != null) {
  1300. throw new CSSParseException("duplicate pseudo element definition "
  1301. + s, getLocator());
  1302. } else {
  1303. pseudoElt = ":"+s;
  1304. return pred;
  1305. }
  1306. } else {
  1307. String c = s;
  1308. if (pred == null) {
  1309. return c;
  1310. } else {
  1311. return pred + c;
  1312. }
  1313. }
  1314. }
  1315. | ( n=<FUNCTION> ( <S> )* d=skipStatementUntilRightParan() <RPARAN>
  1316. {
  1317. // accept anything between function and a right parenthesis
  1318. String f = convertIdent(n.image);
  1319. String colons = isPseudoElement ? "::" : ":";
  1320. String pseudofn = colons + f + d + ")";
  1321. if (pred == null) {
  1322. return pseudofn;
  1323. } else {
  1324. return pred + pseudofn;
  1325. }
  1326. }
  1327. )
  1328. )
  1329. }
  1330. /**
  1331. * @exception ParseException exception during the parse
  1332. */
  1333. String hash(String pred) :
  1334. {Token n; }
  1335. {
  1336. n=<HASH>
  1337. {
  1338. String d = n.image;
  1339. if (pred == null) {
  1340. return d;
  1341. } else {
  1342. return pred + d;
  1343. }
  1344. }
  1345. }
  1346. void variable() :
  1347. {
  1348. String name;
  1349. LexicalUnitImpl exp = null;
  1350. boolean guarded = false;
  1351. String raw;
  1352. }
  1353. {
  1354. try{
  1355. name = variableName()
  1356. ":" ( <S> )* exp=expr() ( guarded=guarded() )?(";"(<S>)*)+
  1357. //raw=skipStatementUntilSemiColon()
  1358. {
  1359. documentHandler.variable(name, exp, guarded);
  1360. }
  1361. }catch (JumpException e) {
  1362. skipAfterExpression();
  1363. } catch (NumberFormatException e) {
  1364. if (errorHandler != null) {
  1365. errorHandler.error(new CSSParseException("Invalid number "
  1366. + e.getMessage(),
  1367. getLocator(),
  1368. e));
  1369. }
  1370. reportWarningSkipText(getLocator(), skipAfterExpression());
  1371. } catch (ParseException e) {
  1372. if (errorHandler != null) {
  1373. if (e.currentToken != null) {
  1374. LocatorImpl li = new LocatorImpl(this,
  1375. e.currentToken.next.beginLine,
  1376. e.currentToken.next.beginColumn-1);
  1377. reportError(li, e);
  1378. } else {
  1379. reportError(getLocator(), e);
  1380. }
  1381. skipAfterExpression();
  1382. } else {
  1383. skipAfterExpression();
  1384. }
  1385. }
  1386. }
  1387. void controlDirective() :
  1388. {}
  1389. {
  1390. ifDirective() | eachDirective()
  1391. }
  1392. void ifContentStatement() :
  1393. {}
  1394. {
  1395. includeDirective() | media() | extendDirective() | styleRuleOrDeclarationOrNestedProperties()
  1396. | keyframes() | LOOKAHEAD(variable()) variable() | listModifyDirective()
  1397. }
  1398. void ifDirective() :
  1399. {
  1400. Token n = null;
  1401. String s = null;
  1402. String evaluator = "";
  1403. }
  1404. {
  1405. < IF_SYM >
  1406. ( s = booleanExpressionToken() { evaluator += s;} )+
  1407. < LBRACE >(< S >)*
  1408. { documentHandler.startIfElseDirective();
  1409. documentHandler.ifDirective(evaluator);
  1410. }
  1411. ( ifContentStatement() )*
  1412. < RBRACE >(< S >)*
  1413. (elseDirective())*
  1414. { documentHandler.endIfElseDirective(); }
  1415. }
  1416. void elseDirective() :
  1417. {
  1418. String evaluator = "";
  1419. Token n = null;
  1420. String s = null;
  1421. }
  1422. {
  1423. < ELSE_SYM >(< S >)*
  1424. ( < IF > ( s = booleanExpressionToken() { evaluator += s; } )+ )?
  1425. < LBRACE >(< S >)*
  1426. { if(!evaluator.trim().equals("")){ documentHandler.ifDirective(evaluator); }
  1427. else{ documentHandler.elseDirective(); }
  1428. }
  1429. ( ifContentStatement() )*
  1430. < RBRACE >(< S >)*
  1431. }
  1432. String booleanExpressionToken() :
  1433. {
  1434. Token n = null;
  1435. String s = null;
  1436. }
  1437. {
  1438. (
  1439. LOOKAHEAD(containsDirective())
  1440. s = containsDirective()
  1441. |n = < VARIABLE >
  1442. |n = < IDENT >
  1443. |n = < NUMBER >
  1444. |n = < LPARAN >
  1445. |n = < RPARAN >
  1446. |n = < PLUS >
  1447. |n = < MINUS >
  1448. |n = < DIV >
  1449. |n = < ANY >
  1450. |n = < COMPARE >
  1451. |n = < EQ >
  1452. |n = < PRECEDES >
  1453. |n = < SUCCEEDS >
  1454. |n = < OR >
  1455. |n = < AND >
  1456. |n = < S >
  1457. |n = < NOT_EQ >
  1458. ){
  1459. if(n!=null){return n.image;}
  1460. else{return s;}
  1461. }
  1462. }
  1463. void eachDirective() :
  1464. {
  1465. Token var;
  1466. ArrayList<String> list = null;
  1467. String listVariable = null;
  1468. }
  1469. {
  1470. < EACH_SYM >
  1471. (< S >)*
  1472. var = < VARIABLE > (< S >)* < EACH_IN > (< S >)*
  1473. (list = stringList()
  1474. {documentHandler.startEachDirective(var.image, list);}
  1475. |listVariable = variableName()
  1476. {documentHandler.startEachDirective(var.image, listVariable);}
  1477. )
  1478. < LBRACE >(< S >)*
  1479. ( ifContentStatement() )*
  1480. < RBRACE >(< S >)*
  1481. { documentHandler.endEachDirective();}
  1482. }
  1483. ArrayList<String > stringList():
  1484. {
  1485. ArrayList<String > strings = new ArrayList<String >();
  1486. Token input;
  1487. }
  1488. {
  1489. (input = < IDENT > (< S >)*)
  1490. { strings.add(input.image); }
  1491. (< COMMA >(< S >)* input = < IDENT > { strings.add(input.image); } (< S >)*)*
  1492. { return strings; }
  1493. }
  1494. void mixinDirective() :
  1495. {
  1496. String name;
  1497. ArrayList<VariableNode> args = null;
  1498. String body;
  1499. }
  1500. {
  1501. <MIXIN_SYM>
  1502. (<S>)*
  1503. (name = property()
  1504. |(name = functionName()
  1505. args = arglist()) <RPARAN> (<S>)*) <LBRACE> (<S>)*
  1506. {documentHandler.startMixinDirective(name, args);}
  1507. ( ifContentStatement() | controlDirective() | fontFace() | page())*
  1508. <RBRACE>(<S>)*
  1509. {documentHandler.endMixinDirective(name, args);}
  1510. }
  1511. ArrayList<VariableNode> arglist() :
  1512. {
  1513. ArrayList<VariableNode> args = new ArrayList<VariableNode>();
  1514. VariableNode arg;
  1515. boolean hasNonOptionalArgument = false;
  1516. }
  1517. {
  1518. arg=mixinArg() ( <COMMA> (<S>)* { hasNonOptionalArgument = checkMixinForNonOptionalArguments(arg, hasNonOptionalArgument); args.add(arg); }
  1519. arg=mixinArg() )*
  1520. { hasNonOptionalArgument = checkMixinForNonOptionalArguments(arg, hasNonOptionalArgument); args.add(arg);
  1521. return args;
  1522. }
  1523. }
  1524. JAVACODE
  1525. boolean checkMixinForNonOptionalArguments(VariableNode arg, boolean hasNonOptionalArguments) {
  1526. boolean currentArgHasArguments = arg.getExpr() != null && arg.getExpr().getLexicalUnitType() == LexicalUnitImpl.SCSS_VARIABLE && arg.getExpr().getNextLexicalUnit() != null;
  1527. if(currentArgHasArguments) {
  1528. if(hasNonOptionalArguments) { throw new ParseException("Sass Error: Required argument $"+ arg.getName() +" must come before any optional arguments.");
  1529. }
  1530. return hasNonOptionalArguments;
  1531. }else { return true;
  1532. }
  1533. }
  1534. VariableNode mixinArg() :
  1535. {
  1536. String name;
  1537. Token variable = null;
  1538. LexicalUnitImpl first = null;
  1539. LexicalUnitImpl prev = null;
  1540. LexicalUnitImpl next = null;
  1541. }
  1542. {
  1543. name=variableName() (< COLON > (< S >)*
  1544. ( first = nonVariableTerm(null) {
  1545. prev = first; }
  1546. (LOOKAHEAD(3)(< COMMA >(< S >)*)? prev = nonVariableTerm(prev))* )
  1547. | (variable = < VARIABLE >{ first = LexicalUnitImpl.createVariable(token.beginLine, token.beginColumn,
  1548. prev, variable.image);}
  1549. )
  1550. )?
  1551. {
  1552. VariableNode arg = new VariableNode(name, first, false);
  1553. return arg;
  1554. }
  1555. }
  1556. ArrayList<LexicalUnitImpl> argValuelist() :
  1557. {
  1558. ArrayList<LexicalUnitImpl> args = new ArrayList<LexicalUnitImpl>();
  1559. LexicalUnitImpl first = null;
  1560. LexicalUnitImpl next = null;
  1561. LexicalUnitImpl prev = null;
  1562. }
  1563. {
  1564. first = term(null) { args.add(first); prev = first;}((< COLON > (< S >)*)?next=term(prev){prev.setNextLexicalUnit(next); prev = next;})*
  1565. ( <COMMA> (<S>)*
  1566. first = term(null) { args.add(first); prev = first;}((< COLON > (< S >)*)?next=term(prev){prev.setNextLexicalUnit(next); prev = next;})*
  1567. )*
  1568. {return args;}
  1569. }
  1570. void includeDirective() :
  1571. {
  1572. String name;
  1573. ArrayList<LexicalUnitImpl> args=null;
  1574. }
  1575. {
  1576. <INCLUDE_SYM>
  1577. (<S>)*
  1578. (name = property()|name = variableName(){ name = "$"+name;}
  1579. |(name = functionName()
  1580. args = argValuelist()) <RPARAN>)(";"(<S>)*)+
  1581. {documentHandler.includeDirective(name, args);}
  1582. }
  1583. String interpolation() :
  1584. {
  1585. Token n;
  1586. }
  1587. {
  1588. n = < INTERPOLATION >
  1589. {
  1590. return n.image;
  1591. }
  1592. }
  1593. void listModifyDirective() :
  1594. {
  1595. String list = null;
  1596. String remove = null;
  1597. String separator = null;
  1598. String variable = null;
  1599. Token n = null;
  1600. Token type = null;
  1601. }
  1602. {
  1603. //refactor, remove those 3 LOOKAHEAD(5).
  1604. n = < VARIABLE >{ variable = n.image; }(< S >)* ":" (< S >)*
  1605. (type = < APPEND> | type = <REMOVE> | type=<CONTAINS> )(< S >)*
  1606. (list = listModifyDirectiveArgs(0))
  1607. (< RPARAN >)? < COMMA >(< S >)*
  1608. (remove = listModifyDirectiveArgs(1))
  1609. ( < COMMA >(< S >)* n = < IDENT >{ separator = n.image; } (< S >)*)?
  1610. < RPARAN >
  1611. {
  1612. switch (type.kind) {
  1613. case APPEND:
  1614. documentHandler.appendDirective(variable,list,remove,separator);
  1615. break;
  1616. case REMOVE:
  1617. documentHandler.removeDirective(variable,list,remove,separator);
  1618. break;
  1619. case CONTAINS:
  1620. if(variable == null){
  1621. variable = "$var_"+UUID.randomUUID();
  1622. }
  1623. documentHandler.containsDirective(variable,list,remove,separator);
  1624. break;
  1625. default:
  1626. break;
  1627. }
  1628. }
  1629. (< S >)*< SEMICOLON >(<S>)*
  1630. }
  1631. /**
  1632. * @exception ParseException exception during the parse
  1633. */
  1634. void appendDirective() :
  1635. {
  1636. String list = null;
  1637. String remove = null;
  1638. String separator = null;
  1639. String variable = null;
  1640. Token n = null;
  1641. }
  1642. {
  1643. n = < VARIABLE >{ variable = n.image; }(< S >)* ":" (< S >)*
  1644. < APPEND >(< S >)*
  1645. (list = listModifyDirectiveArgs(0))
  1646. (< RPARAN >)? < COMMA >(< S >)*
  1647. (remove = listModifyDirectiveArgs(1))
  1648. ( < COMMA >(< S >)* n = < IDENT >{ separator = n.image; } (< S >)*)?
  1649. < RPARAN >
  1650. { documentHandler.appendDirective(variable,list,remove,separator); }
  1651. }
  1652. /**
  1653. * @exception ParseException exception during the parse
  1654. */
  1655. void removeDirective() :
  1656. {
  1657. String list = null;
  1658. String remove = null;
  1659. String separator = null;
  1660. String variable = null;
  1661. Token n = null;
  1662. }
  1663. {
  1664. n = < VARIABLE >{ variable = n.image; }(< S >)* ":" (< S >)*
  1665. < REMOVE >(< S >)*
  1666. (list = listModifyDirectiveArgs(0))
  1667. (< RPARAN >)? < COMMA >(< S >)*
  1668. (remove = listModifyDirectiveArgs(1))
  1669. ( < COMMA >(< S >)* n = < IDENT >{ separator = n.image; } (< S >)*)?
  1670. < RPARAN >
  1671. { documentHandler.removeDirective(variable,list,remove,separator); }
  1672. }
  1673. /**
  1674. * @exception ParseException exception during the parse
  1675. */
  1676. String containsDirective() :
  1677. {
  1678. String list = null;
  1679. String remove = null;
  1680. String separator = null;
  1681. String variable = null;
  1682. Token n = null;
  1683. }
  1684. {
  1685. (n = < VARIABLE >{ variable = n.image; }(< S >)* ":" (< S >)*)?
  1686. < CONTAINS >(< S >)*
  1687. (list = listModifyDirectiveArgs(0))
  1688. (< RPARAN >)? < COMMA >(< S >)*
  1689. (remove = listModifyDirectiveArgs(1))
  1690. ( < COMMA >(< S >)* n = < IDENT >{ separator = n.image; } (< S >)*)?
  1691. < RPARAN >
  1692. { /*
  1693. *if it is not in the form like "$contains : contains($items, .v-button);"
  1694. *for example in @if, like "@if (contains(a b c, b))", then create a temp
  1695. *variable for contains(a b c, b);
  1696. */
  1697. if(variable == null){
  1698. variable = "$var_"+UUID.randomUUID();
  1699. }
  1700. documentHandler.containsDirective(variable,list,remove,separator);
  1701. return variable;
  1702. }
  1703. }
  1704. JAVACODE
  1705. String listModifyDirectiveArgs(int nest)
  1706. {
  1707. String list = "";
  1708. int nesting = nest;
  1709. Token t = null;
  1710. while(true)
  1711. {
  1712. t = getToken(1);
  1713. String s = t.image;
  1714. if(t.kind == VARIABLE||t.kind == IDENT) {
  1715. list += s;
  1716. }else if(s.toLowerCase().equals("auto")||s.toLowerCase().equals("space")||s.toLowerCase().equals("comma")) {
  1717. int i = 2;
  1718. Token temp = getToken(i);
  1719. boolean isLast = true;
  1720. while(temp.kind != SEMICOLON)
  1721. { if(temp.kind != RPARAN || temp.kind != S)
  1722. { isLast = false; }
  1723. i++;
  1724. temp = getToken(i);
  1725. }
  1726. if(isLast) { return list;
  1727. }
  1728. } else if(t.kind == STRING) { list += s.substring(1,s.length()).substring(0,s.length()-2);
  1729. }else if(t.kind == LPARAN) { nesting++;
  1730. if(nesting > nest+1) { throw new CSSParseException("Only one ( ) pair per parameter allowed", getLocator());
  1731. }
  1732. }else if(t.kind == RPARAN) { nesting--;
  1733. if(nesting == 0) {
  1734. return list;
  1735. }
  1736. } else if(t.kind == COMMA) {
  1737. if(nesting == nest) {
  1738. return list; }else {
  1739. list += ","; }
  1740. }else if(t.kind == S) {
  1741. list += " "; } else if(t.kind == LBRACE) {
  1742. throw new CSSParseException("Invalid token,'{' found", getLocator()); }
  1743. getNextToken();
  1744. }
  1745. }
  1746. Node returnDirective() :
  1747. {
  1748. String raw;
  1749. }
  1750. {
  1751. raw = skipStatement()
  1752. {return null;}
  1753. }
  1754. void debuggingDirective() :
  1755. {}
  1756. {
  1757. debugDirective() | warnDirective()
  1758. }
  1759. void debugDirective() :
  1760. {}
  1761. {
  1762. <DEBUG_SYM>
  1763. {
  1764. String content = skipStatementUntilSemiColon();
  1765. // TODO should evaluate the content expression, call documentHandler.debugDirective() etc.
  1766. System.out.println(content);
  1767. }
  1768. (<S>)*
  1769. }
  1770. void warnDirective() :
  1771. {}
  1772. {
  1773. <WARN_SYM>
  1774. {
  1775. String content = skipStatementUntilSemiColon();
  1776. // TODO should evaluate the content expression, call documentHandler.warnDirective() etc.
  1777. System.err.println(content);
  1778. }
  1779. (<S>)*
  1780. }
  1781. Node forDirective() :
  1782. {
  1783. String var;
  1784. String from;
  1785. String to;
  1786. boolean exclusive;
  1787. String body;
  1788. Token tok;
  1789. }
  1790. {
  1791. var = variableName()
  1792. {
  1793. int[] toThrough = {TO, THROUGH};
  1794. from = skipStatementUntil(toThrough);
  1795. }
  1796. (tok = <TO> {exclusive = true;}
  1797. | tok = <THROUGH> {exclusive = false;})
  1798. to = skipStatementUntilLeftBrace()
  1799. (<S>)*
  1800. body = skipStatement()
  1801. {return documentHandler.forDirective(var, from, to, exclusive, body);}
  1802. }
  1803. Node whileDirective() :
  1804. {
  1805. String condition;
  1806. String body;
  1807. }
  1808. {
  1809. condition = skipStatementUntilLeftBrace()
  1810. body = skipStatement()
  1811. { return documentHandler.whileDirective(condition, body);}
  1812. }
  1813. void extendDirective() :
  1814. {ArrayList<String> list;}
  1815. {
  1816. <EXTEND_SYM>
  1817. (<S>)*
  1818. list = selectorList()
  1819. (";"(<S>)*)+
  1820. {documentHandler.extendDirective(list);}
  1821. }
  1822. JAVACODE
  1823. Node importDirective(){
  1824. return null;
  1825. }
  1826. JAVACODE
  1827. Node charsetDirective(){
  1828. return null;
  1829. }
  1830. JAVACODE
  1831. Node mozDocumentDirective(){
  1832. return null;
  1833. }
  1834. JAVACODE
  1835. Node supportsDirective(){
  1836. return null;
  1837. }
  1838. void nestedProperties():
  1839. {String name;
  1840. LexicalUnit exp;}
  1841. {
  1842. name=property()
  1843. ":" ( <S> )*
  1844. <LBRACE> (<S>)*
  1845. {
  1846. documentHandler.startNestedProperties(name);
  1847. }
  1848. ( declaration() )? ( ";" ( <S> )* ( declaration() )? )*
  1849. <RBRACE>
  1850. {
  1851. documentHandler.endNestedProperties(name);
  1852. }
  1853. (<S>)*
  1854. }
  1855. /**
  1856. * @exception ParseException exception during the parse
  1857. */
  1858. void styleRuleOrDeclarationOrNestedProperties() :
  1859. {
  1860. }
  1861. {
  1862. try {
  1863. // differentiate between the colon of a pseudo and the colon of nested properties and the colon of a simple property
  1864. // first check if it is a normal styleRule, if not check if it is declarationOrNestedProperties(), if still fails, most likely, it is
  1865. // is styleRule with pseudo selector with contains functions. have to do it in this way, because both the styleRule and declarationOrNestedProperties()
  1866. // have 'skipStatementUntilXXX', which cannot be LOOKAHEAD properly.
  1867. ( debuggingDirective() | LOOKAHEAD(selectorList()<LBRACE>) styleRule() | LOOKAHEAD(3)declarationOrNestedProperties() | styleRule())
  1868. } catch (JumpException e) {
  1869. skipAfterExpression();
  1870. // reportWarningSkipText(getLocator(), skipAfterExpression());
  1871. } catch (ParseException e) {
  1872. if (errorHandler != null) {
  1873. if (e.currentToken != null) {
  1874. LocatorImpl li = new LocatorImpl(this,
  1875. e.currentToken.next.beginLine,
  1876. e.currentToken.next.beginColumn-1);
  1877. reportError(li, e);
  1878. } else {
  1879. reportError(getLocator(), e);
  1880. }
  1881. skipAfterExpression();
  1882. /*
  1883. LocatorImpl loc = (LocatorImpl) getLocator();
  1884. loc.column--;
  1885. reportWarningSkipText(loc, skipAfterExpression());
  1886. */
  1887. } else {
  1888. skipAfterExpression();
  1889. }
  1890. }
  1891. }
  1892. /**
  1893. * @exception ParseException exception during the parse
  1894. */
  1895. void declarationOrNestedProperties() :
  1896. { boolean important = false;
  1897. String name;
  1898. LexicalUnitImpl exp;
  1899. Token save;
  1900. String comment = null;
  1901. }
  1902. {
  1903. try {
  1904. name=property()
  1905. { save = token; }
  1906. ":" ( <S> )*
  1907. (exp=expr() ( important=prio() )?
  1908. {
  1909. Token next = getToken(1);
  1910. if(next.kind == SEMICOLON || next.kind == RBRACE){
  1911. while(next.kind == SEMICOLON){
  1912. skipStatement();
  1913. next = getToken(1);
  1914. }
  1915. if(token.specialToken!=null){
  1916. documentHandler.property(name, exp, important, token.specialToken.image);
  1917. }else{
  1918. documentHandler.property(name, exp, important, null);
  1919. }
  1920. }
  1921. }
  1922. |<LBRACE> (<S>)*
  1923. {
  1924. documentHandler.startNestedProperties(name);
  1925. }
  1926. ( declaration() )? ( ";" ( <S> )* ( declaration() )? )*
  1927. <RBRACE>(<S>)*
  1928. {
  1929. documentHandler.endNestedProperties(name);
  1930. }
  1931. )
  1932. } catch (JumpException e) {
  1933. skipAfterExpression();
  1934. // reportWarningSkipText(getLocator(), skipAfterExpression());
  1935. } catch (NumberFormatException e) {
  1936. if (errorHandler != null) {
  1937. errorHandler.error(new CSSParseException("Invalid number "
  1938. + e.getMessage(),
  1939. getLocator(),
  1940. e));
  1941. }
  1942. reportWarningSkipText(getLocator(), skipAfterExpression());
  1943. } catch (ParseException e) {
  1944. if (errorHandler != null) {
  1945. if (e.currentToken != null) {
  1946. LocatorImpl li = new LocatorImpl(this,
  1947. e.currentToken.next.beginLine,
  1948. e.currentToken.next.beginColumn-1);
  1949. reportError(li, e);
  1950. } else {
  1951. reportError(getLocator(), e);
  1952. }
  1953. skipAfterExpression();
  1954. /*
  1955. LocatorImpl loc = (LocatorImpl) getLocator();
  1956. loc.column--;
  1957. reportWarningSkipText(loc, skipAfterExpression());
  1958. */
  1959. } else {
  1960. skipAfterExpression();
  1961. }
  1962. }
  1963. }
  1964. /**
  1965. * @exception ParseException exception during the parse
  1966. */
  1967. void declaration() :
  1968. { boolean important = false;
  1969. String name;
  1970. LexicalUnit exp;
  1971. Token save;
  1972. }
  1973. {
  1974. try {
  1975. name=property()
  1976. { save = token; }
  1977. ":" ( <S> )* exp=expr() ( important=prio() )?
  1978. {
  1979. documentHandler.property(name, exp, important);
  1980. }
  1981. } catch (JumpException e) {
  1982. skipAfterExpression();
  1983. // reportWarningSkipText(getLocator(), skipAfterExpression());
  1984. } catch (NumberFormatException e) {
  1985. if (errorHandler != null) {
  1986. errorHandler.error(new CSSParseException("Invalid number "
  1987. + e.getMessage(),
  1988. getLocator(),
  1989. e));
  1990. }
  1991. reportWarningSkipText(getLocator(), skipAfterExpression());
  1992. } catch (ParseException e) {
  1993. if (errorHandler != null) {
  1994. if (e.currentToken != null) {
  1995. LocatorImpl li = new LocatorImpl(this,
  1996. e.currentToken.next.beginLine,
  1997. e.currentToken.next.beginColumn-1);
  1998. reportError(li, e);
  1999. } else {
  2000. reportError(getLocator(), e);
  2001. }
  2002. skipAfterExpression();
  2003. /*
  2004. LocatorImpl loc = (LocatorImpl) getLocator();
  2005. loc.column--;
  2006. reportWarningSkipText(loc, skipAfterExpression());
  2007. */
  2008. } else {
  2009. skipAfterExpression();
  2010. }
  2011. }
  2012. }
  2013. /**
  2014. * @exception ParseException exception during the parse
  2015. */
  2016. boolean prio() :
  2017. {}
  2018. {
  2019. <IMPORTANT_SYM> ( <S> )* { return true; }
  2020. }
  2021. boolean guarded() :
  2022. {}
  2023. {
  2024. <GUARDED_SYM> (<S>)* {return true;}
  2025. }
  2026. /**
  2027. * @exception ParseException exception during the parse
  2028. */
  2029. LexicalUnitImpl operator(LexicalUnitImpl prev) :
  2030. {Token n;}
  2031. {
  2032. /* (comments copied from basic_arithmetics.scss)
  2033. *supports:
  2034. * 1. standard arithmetic operations (+, -, *, /, %)
  2035. * 2. / is treated as css operator, unless one of its operands is variable or there is another binary arithmetic operator
  2036. *limits:
  2037. * 1. cannot mix arithmetic and css operations, e.g. "margin: 1px + 3px 2px" will fail
  2038. * 2. space between add and minus operator and their following operand is mandatory. e.g. "1 + 2" is valid, "1+2" is not
  2039. * 3. parenthesis is not supported now.
  2040. */
  2041. n="," ( <S> )* { return LexicalUnitImpl.createComma(n.beginLine,
  2042. n.beginColumn,
  2043. prev); }
  2044. |n="/" ( <S> )* { return LexicalUnitImpl.createSlash(n.beginLine,
  2045. n.beginColumn,
  2046. prev); }
  2047. | n="*" ( <S> )* { return LexicalUnitImpl.createMultiply(n.beginLine,
  2048. n.beginColumn,
  2049. prev); }
  2050. | n="%" ( <S> )* { return LexicalUnitImpl.createModulo(n.beginLine,
  2051. n.beginColumn,
  2052. prev); }
  2053. /*
  2054. * for '+', since it can be either a binary operator or an unary operator,
  2055. * which is ambiguous. To avoid this, the binary operator '+' always has
  2056. * a space before the following term. so '2+3' is not a valid binary expression,
  2057. * but '2 + 3' is. The same for '-' operator.
  2058. */
  2059. | n="+" ( <S> )+{ return LexicalUnitImpl.createAdd(n.beginLine,
  2060. n.beginColumn,
  2061. prev); }
  2062. | n="-" ( <S> )+{ return LexicalUnitImpl.createMinus(n.beginLine,
  2063. n.beginColumn,
  2064. prev); }
  2065. }
  2066. /**
  2067. * @exception ParseException exception during the parse
  2068. */
  2069. LexicalUnitImpl expr() :
  2070. {
  2071. LexicalUnitImpl first, res;
  2072. char op;
  2073. }
  2074. {
  2075. first=term(null){ res = first; }
  2076. ( LOOKAHEAD(2) ( LOOKAHEAD(2) res=operator(res) )? res=term(res))*
  2077. { return first; }
  2078. }
  2079. /**
  2080. * @exception ParseException exception during the parse
  2081. */
  2082. char unaryOperator() :
  2083. {}
  2084. {
  2085. "-" { return '-'; }
  2086. | "+" { return '+'; }
  2087. }
  2088. /**
  2089. * @exception ParseException exception during the parse
  2090. */
  2091. LexicalUnitImpl term(LexicalUnitImpl prev) :
  2092. { LexicalUnitImpl result = null;
  2093. Token n = null;
  2094. char op = ' ';
  2095. }
  2096. {
  2097. (result = nonVariableTerm(prev)| result = variableTerm(prev))
  2098. {
  2099. return result;
  2100. }
  2101. }
  2102. LexicalUnitImpl variableTerm(LexicalUnitImpl prev) : {
  2103. LexicalUnitImpl result = null;
  2104. String varName = ""; } {
  2105. varName = variableName()
  2106. {result = LexicalUnitImpl.createVariable(token.beginLine, token.beginColumn,
  2107. prev, varName); return result;} }
  2108. LexicalUnitImpl nonVariableTerm(LexicalUnitImpl prev) : { LexicalUnitImpl result = null;
  2109. Token n = null;
  2110. char op = ' ';
  2111. String varName;
  2112. String s = "";
  2113. }
  2114. {
  2115. ( ( (
  2116. op=unaryOperator() )?
  2117. (n=<NUMBER>
  2118. { result = LexicalUnitImpl.createNumber(n.beginLine, n.beginColumn,
  2119. prev, number(op, n, 0)); }
  2120. | n=<PERCENTAGE>
  2121. { result = LexicalUnitImpl.createPercentage(n.beginLine, n.beginColumn,
  2122. prev, number(op, n, 1)); }
  2123. | n=<PT>
  2124. { result = LexicalUnitImpl.createPT(n.beginLine, n.beginColumn,
  2125. prev, number(op, n, 2)); }
  2126. | n=<CM>
  2127. { result = LexicalUnitImpl.createCM(n.beginLine, n.beginColumn,
  2128. prev, number(op, n, 2)); }
  2129. | n=<MM>
  2130. { result = LexicalUnitImpl.createMM(n.beginLine, n.beginColumn,
  2131. prev, number(op, n, 2)); }
  2132. | n=<PC>
  2133. { result = LexicalUnitImpl.createPC(n.beginLine, n.beginColumn,
  2134. prev, number(op, n, 2)); }
  2135. | n=<IN>
  2136. { result = LexicalUnitImpl.createIN(n.beginLine, n.beginColumn,
  2137. prev, number(op, n, 2)); }
  2138. | n=<PX>
  2139. { result = LexicalUnitImpl.createPX(n.beginLine, n.beginColumn,
  2140. prev, number(op, n, 2)); }
  2141. | n=<EMS>
  2142. { result = LexicalUnitImpl.createEMS(n.beginLine, n.beginColumn,
  2143. prev, number(op, n, 2)); }
  2144. | n=<LEM>
  2145. { result = LexicalUnitImpl.createLEM(n.beginLine, n.beginColumn,
  2146. prev, number(op, n, 3)); }
  2147. | n=<REM>
  2148. { result = LexicalUnitImpl.createREM(n.beginLine, n.beginColumn,
  2149. prev, number(op, n, 3)); }
  2150. | n=<EXS>
  2151. { result = LexicalUnitImpl.createEXS(n.beginLine, n.beginColumn,
  2152. prev, number(op, n, 2)); }
  2153. | n=<DEG>
  2154. { result = LexicalUnitImpl.createDEG(n.beginLine, n.beginColumn,
  2155. prev, number(op, n, 3)); }
  2156. | n=<RAD>
  2157. { result = LexicalUnitImpl.createRAD(n.beginLine, n.beginColumn,
  2158. prev, number(op, n, 3)); }
  2159. | n=<GRAD>
  2160. { result = LexicalUnitImpl.createGRAD(n.beginLine, n.beginColumn,
  2161. prev, number(op, n, 3)); }
  2162. | n=<SECOND>
  2163. { result = LexicalUnitImpl.createS(n.beginLine, n.beginColumn,
  2164. prev, number(op, n, 1)); }
  2165. | n=<MS>
  2166. { result = LexicalUnitImpl.createMS(n.beginLine, n.beginColumn,
  2167. prev, number(op, n, 2)); }
  2168. | n=<HZ>
  2169. { result = LexicalUnitImpl.createHZ(n.beginLine, n.beginColumn,
  2170. prev, number(op, n, 2)); }
  2171. | n=<KHZ>
  2172. { result = LexicalUnitImpl.createKHZ(n.beginLine, n.beginColumn,
  2173. prev, number(op, n, 3)); }
  2174. | n=<DIMEN>
  2175. {
  2176. s = n.image;
  2177. int i = 0;
  2178. while (i < s.length()
  2179. && (Character.isDigit(s.charAt(i)) || (s.charAt(i) == '.'))) {
  2180. i++;
  2181. }
  2182. result = LexicalUnitImpl.createDimen(n.beginLine, n.beginColumn, prev,
  2183. Float.valueOf(s.substring(0, i)).floatValue(),
  2184. s.substring(i));
  2185. }
  2186. | result=function(op, prev) ) )
  2187. | ( n=<STRING>
  2188. { result =
  2189. LexicalUnitImpl.createString(n.beginLine, n.beginColumn, prev,
  2190. convertStringIndex(n.image, 1,
  2191. n.image.length() -1));}
  2192. | (< DOT >{ s+="."; })?n=<IDENT>
  2193. { s += convertIdent(n.image);
  2194. if ("inherit".equals(s)) {
  2195. result = LexicalUnitImpl.createInherit(n.beginLine, n.beginColumn,
  2196. prev);
  2197. } else {
  2198. result = LexicalUnitImpl.createIdent(n.beginLine, n.beginColumn,
  2199. prev, convertIdent(n.image));
  2200. }
  2201. /* /
  2202. Auto correction code used in the CSS Validator but must not
  2203. be used by a conformant CSS2 parser.
  2204. * Common error :
  2205. * H1 {
  2206. * color : black
  2207. * background : white
  2208. * }
  2209. *
  2210. Token t = getToken(1);
  2211. Token semicolon = new Token();
  2212. semicolon.kind = SEMICOLON;
  2213. semicolon.image = ";";
  2214. if (t.kind == COLON) {
  2215. // @@SEEME. (generate a warning?)
  2216. // @@SEEME if expression is a single ident,
  2217. generate an error ?
  2218. rejectToken(semicolon);
  2219. result = prev;
  2220. }
  2221. / */
  2222. }
  2223. | result=hexcolor(prev)
  2224. | result=url(prev)
  2225. | result=unicode(prev)
  2226. ) ) ( <S> )*
  2227. {
  2228. return result; } }
  2229. /**
  2230. * Handle all CSS2 functions.
  2231. * @exception ParseException exception during the parse
  2232. */
  2233. LexicalUnitImpl function(char operator, LexicalUnitImpl prev) :
  2234. {Token n;
  2235. LexicalUnit params = null;
  2236. }
  2237. {
  2238. n=<FUNCTION> ( <S> )*
  2239. {
  2240. String fname = convertIdent(n.image);
  2241. if("alpha(".equals(fname)){
  2242. String body = skipStatementUntilSemiColon();
  2243. return LexicalUnitImpl.createIdent(n.beginLine, n.beginColumn,
  2244. null, "alpha("+body);
  2245. }else if("expression(".equals(fname)){
  2246. String body = skipStatementUntilSemiColon();
  2247. return LexicalUnitImpl.createIdent(n.beginLine, n.beginColumn,
  2248. null, "expression("+body);
  2249. }
  2250. }
  2251. ( params=expr() )? ")"
  2252. {
  2253. if (operator != ' ') {
  2254. throw new CSSParseException("invalid operator before a function.",
  2255. getLocator());
  2256. }
  2257. String f = convertIdent(n.image);
  2258. LexicalUnitImpl l = (LexicalUnitImpl) params;
  2259. boolean loop = true;
  2260. if ("rgb(".equals(f)) {
  2261. // this is a RGB declaration (e.g. rgb(255, 50%, 0) )
  2262. int i = 0;
  2263. while (loop && l != null && i < 5) {
  2264. switch (i) {
  2265. case 0:
  2266. case 2:
  2267. case 4:
  2268. if ((l.getLexicalUnitType() != LexicalUnit.SAC_INTEGER)
  2269. && (l.getLexicalUnitType() != LexicalUnit.SAC_PERCENTAGE)) {
  2270. loop = false;
  2271. }
  2272. break;
  2273. case 1:
  2274. case 3:
  2275. if (l.getLexicalUnitType() != LexicalUnit.SAC_OPERATOR_COMMA) {
  2276. loop = false;
  2277. }
  2278. break;
  2279. default:
  2280. throw new ParseException("implementation error");
  2281. }
  2282. if (loop) {
  2283. l = (LexicalUnitImpl) l.getNextLexicalUnit();
  2284. i ++;
  2285. }
  2286. }
  2287. if ((i == 5) && loop && (l == null)) {
  2288. return LexicalUnitImpl.createRGBColor(n.beginLine,
  2289. n.beginColumn,
  2290. prev, params);
  2291. } else {
  2292. if (errorHandler != null) {
  2293. String errorText;
  2294. Locator loc;
  2295. if (i < 5) {
  2296. if (params == null) {
  2297. loc = new LocatorImpl(this, n.beginLine,
  2298. n.beginColumn-1);
  2299. errorText = "not enough parameters.";
  2300. } else if (l == null) {
  2301. loc = new LocatorImpl(this, n.beginLine,
  2302. n.beginColumn-1);
  2303. errorText = "not enough parameters: "
  2304. + params.toString();
  2305. } else {
  2306. loc = new LocatorImpl(this, l.getLineNumber(),
  2307. l.getColumnNumber());
  2308. errorText = "invalid parameter: "
  2309. + l.toString();
  2310. }
  2311. } else {
  2312. loc = new LocatorImpl(this, l.getLineNumber(),
  2313. l.getColumnNumber());
  2314. errorText = "too many parameters: "
  2315. + l.toString();
  2316. }
  2317. errorHandler.error(new CSSParseException(errorText, loc));
  2318. }
  2319. throw new JumpException();
  2320. }
  2321. } else if ("counter".equals(f)) {
  2322. int i = 0;
  2323. while (loop && l != null && i < 3) {
  2324. switch (i) {
  2325. case 0:
  2326. case 2:
  2327. if (l.getLexicalUnitType() != LexicalUnit.SAC_IDENT) {
  2328. loop = false;
  2329. }
  2330. break;
  2331. case 1:
  2332. if (l.getLexicalUnitType() != LexicalUnit.SAC_OPERATOR_COMMA) {
  2333. loop = false;
  2334. }
  2335. break;
  2336. default:
  2337. throw new ParseException("implementation error");
  2338. }
  2339. l = (LexicalUnitImpl) l.getNextLexicalUnit();
  2340. i ++;
  2341. }
  2342. if (((i == 1) || (i == 3)) && loop && (l == null)) {
  2343. return LexicalUnitImpl.createCounter(n.beginLine, n.beginColumn,
  2344. prev, params);
  2345. }
  2346. } else if ("counters(".equals(f)) {
  2347. int i = 0;
  2348. while (loop && l != null && i < 5) {
  2349. switch (i) {
  2350. case 0:
  2351. case 4:
  2352. if (l.getLexicalUnitType() != LexicalUnit.SAC_IDENT) {
  2353. loop = false;
  2354. }
  2355. break;
  2356. case 2:
  2357. if (l.getLexicalUnitType() != LexicalUnit.SAC_STRING_VALUE) {
  2358. loop = false;
  2359. }
  2360. break;
  2361. case 1:
  2362. case 3:
  2363. if (l.getLexicalUnitType() != LexicalUnit.SAC_OPERATOR_COMMA) {
  2364. loop = false;
  2365. }
  2366. break;
  2367. default:
  2368. throw new ParseException("implementation error");
  2369. }
  2370. l = (LexicalUnitImpl) l.getNextLexicalUnit();
  2371. i ++;
  2372. }
  2373. if (((i == 3) || (i == 5)) && loop && (l == null)) {
  2374. return LexicalUnitImpl.createCounters(n.beginLine, n.beginColumn,
  2375. prev, params);
  2376. }
  2377. } else if ("attr(".equals(f)) {
  2378. if ((l != null)
  2379. && (l.getNextLexicalUnit() == null)
  2380. && (l.getLexicalUnitType() == LexicalUnit.SAC_IDENT)) {
  2381. return LexicalUnitImpl.createAttr(l.getLineNumber(),
  2382. l.getColumnNumber(),
  2383. prev, l.getStringValue());
  2384. }
  2385. } else if ("rect(".equals(f)) {
  2386. int i = 0;
  2387. while (loop && l != null && i < 7) {
  2388. switch (i) {
  2389. case 0:
  2390. case 2:
  2391. case 4:
  2392. case 6:
  2393. switch (l.getLexicalUnitType()) {
  2394. case LexicalUnit.SAC_INTEGER:
  2395. if (l.getIntegerValue() != 0) {
  2396. loop = false;
  2397. }
  2398. break;
  2399. case LexicalUnit.SAC_IDENT:
  2400. if (!l.getStringValue().equals("auto")) {
  2401. loop = false;
  2402. }
  2403. break;
  2404. case LexicalUnit.SAC_EM:
  2405. case LexicalUnit.SAC_EX:
  2406. case LexicalUnit.SAC_PIXEL:
  2407. case LexicalUnit.SAC_CENTIMETER:
  2408. case LexicalUnit.SAC_MILLIMETER:
  2409. case LexicalUnit.SAC_INCH:
  2410. case LexicalUnit.SAC_POINT:
  2411. case LexicalUnit.SAC_PICA:
  2412. // nothing
  2413. break;
  2414. default:
  2415. loop = false;
  2416. }
  2417. break;
  2418. case 1:
  2419. case 3:
  2420. case 5:
  2421. if (l.getLexicalUnitType() != LexicalUnit.SAC_OPERATOR_COMMA) {
  2422. loop = false;
  2423. }
  2424. break;
  2425. default:
  2426. throw new ParseException("implementation error");
  2427. }
  2428. l = (LexicalUnitImpl) l.getNextLexicalUnit();
  2429. i ++;
  2430. }
  2431. if ((i == 7) && loop && (l == null)) {
  2432. return LexicalUnitImpl.createRect(n.beginLine, n.beginColumn,
  2433. prev, params);
  2434. }
  2435. }
  2436. return LexicalUnitImpl.createFunction(n.beginLine, n.beginColumn, prev,
  2437. f.substring(0,
  2438. f.length() -1),
  2439. params);
  2440. }
  2441. }
  2442. LexicalUnitImpl unicode(LexicalUnitImpl prev) :
  2443. { Token n;
  2444. }
  2445. {
  2446. n=<UNICODERANGE>
  2447. {
  2448. LexicalUnitImpl params = null;
  2449. String s = n.image.substring(2);
  2450. int index = s.indexOf('-');
  2451. if (index == -1) {
  2452. params = LexicalUnitImpl.createInteger(n.beginLine, n.beginColumn,
  2453. params, Integer.parseInt(s, 16));
  2454. } else {
  2455. String s1 = s.substring(0, index);
  2456. String s2 = s.substring(index);
  2457. params = LexicalUnitImpl.createInteger(n.beginLine, n.beginColumn,
  2458. params, Integer.parseInt(s1, 16));
  2459. params = LexicalUnitImpl.createInteger(n.beginLine, n.beginColumn,
  2460. params, Integer.parseInt(s2, 16));
  2461. }
  2462. return LexicalUnitImpl.createUnicodeRange(n.beginLine, n.beginColumn,
  2463. prev, params);
  2464. }
  2465. }
  2466. LexicalUnitImpl url(LexicalUnitImpl prev) :
  2467. { Token n;
  2468. }
  2469. {
  2470. n=<URL>
  2471. {
  2472. String urlname = n.image.substring(4, n.image.length()-1).trim();
  2473. return LexicalUnitImpl.createURL(n.beginLine, n.beginColumn, prev, urlname);
  2474. }
  2475. }
  2476. /**
  2477. * @exception ParseException exception during the parse
  2478. */
  2479. LexicalUnitImpl hexcolor(LexicalUnitImpl prev) :
  2480. {Token n;
  2481. }
  2482. {
  2483. n=<HASH>
  2484. {
  2485. int r;
  2486. LexicalUnitImpl first, params = null;
  2487. String s = n.image.substring(1);
  2488. if(s.length()!=3 && s.length()!=6) {
  2489. first = null;
  2490. throw new CSSParseException("invalid hexadecimal notation for RGB: " + s,
  2491. getLocator());
  2492. }
  2493. return LexicalUnitImpl.createIdent(n.beginLine, n.beginColumn,
  2494. prev, n.image);
  2495. }
  2496. }
  2497. JAVACODE
  2498. float number(char operator, Token n, int lengthUnit) {
  2499. String image = n.image;
  2500. float f = 0;
  2501. if (lengthUnit != 0) {
  2502. image = image.substring(0, image.length() - lengthUnit);
  2503. }
  2504. f = Float.valueOf(image).floatValue();
  2505. return (operator == '-')? -f: f;
  2506. }
  2507. JAVACODE
  2508. String skipStatementUntilSemiColon(){
  2509. int[] semicolon = {SEMICOLON};
  2510. return skipStatementUntil(semicolon);
  2511. }
  2512. JAVACODE
  2513. String skipStatementUntilLeftBrace(){
  2514. int[] lBrace = {LBRACE};
  2515. return skipStatementUntil(lBrace);
  2516. }
  2517. JAVACODE
  2518. String skipStatementUntilRightParan(){
  2519. int[] rParan = {RPARAN};
  2520. return skipStatementUntil(rParan);
  2521. }
  2522. JAVACODE
  2523. String skipStatementUntil(int[] symbols){
  2524. StringBuffer s = new StringBuffer();
  2525. boolean stop = false;
  2526. Token tok;
  2527. while(!stop){
  2528. tok = getToken(1);
  2529. if(tok.kind == EOF) {
  2530. return null;
  2531. }
  2532. for(int sym : symbols){
  2533. if(tok.kind == sym){
  2534. stop = true;
  2535. break;
  2536. }
  2537. }
  2538. if(!stop){
  2539. if (tok.image != null) {
  2540. s.append(tok.image);
  2541. }
  2542. getNextToken();
  2543. }
  2544. }
  2545. return s.toString().trim();
  2546. }
  2547. JAVACODE
  2548. String skipStatement() {
  2549. StringBuffer s = new StringBuffer();
  2550. Token tok = getToken(0);
  2551. if (tok.image != null) {
  2552. s.append(tok.image);
  2553. }
  2554. while (true) {
  2555. tok = getToken(1);
  2556. if (tok.kind == EOF) {
  2557. return null;
  2558. }
  2559. s.append(tok.image);
  2560. if (tok.kind == LBRACE) {
  2561. getNextToken();
  2562. s.append(skip_to_matching_brace());
  2563. getNextToken();
  2564. tok = getToken(1);
  2565. break;
  2566. } else if (tok.kind == RBRACE) {
  2567. getNextToken();
  2568. tok = getToken(1);
  2569. break;
  2570. } else if (tok.kind == SEMICOLON) {
  2571. getNextToken();
  2572. tok = getToken(1);
  2573. break;
  2574. }
  2575. getNextToken();
  2576. }
  2577. // skip white space
  2578. while (true) {
  2579. if (tok.kind != S) {
  2580. break;
  2581. }
  2582. tok = getNextToken();
  2583. tok = getToken(1);
  2584. }
  2585. return s.toString().trim();
  2586. }
  2587. JAVACODE
  2588. String skip_to_matching_brace() {
  2589. StringBuffer s = new StringBuffer();
  2590. Token tok;
  2591. int nesting = 1;
  2592. while (true) {
  2593. tok = getToken(1);
  2594. if (tok.kind == EOF) {
  2595. break;
  2596. }
  2597. s.append(tok.image);
  2598. if (tok.kind == LBRACE) {
  2599. nesting++;
  2600. } else if (tok.kind == RBRACE) {
  2601. nesting--;
  2602. if (nesting == 0) {
  2603. break;
  2604. }
  2605. }
  2606. getNextToken();
  2607. }
  2608. return s.toString();
  2609. }
  2610. /*
  2611. * Here I handle all CSS2 unicode character stuffs.
  2612. * I convert all \XXXXXX character into a single character.
  2613. * Don't forget that the parser has recognize the token before.
  2614. * (So IDENT won't contain newline and stuffs like this).
  2615. */
  2616. JAVACODE
  2617. String convertStringIndex(String s, int start, int len) {
  2618. StringBuffer buf = new StringBuffer(len);
  2619. int index = start;
  2620. while (index < len) {
  2621. char c = s.charAt(index);
  2622. if (c == '\\') {
  2623. if (++index < len) {
  2624. c = s.charAt(index);
  2625. switch (c) {
  2626. case '0': case '1': case '2': case '3': case '4':
  2627. case '5': case '6': case '7': case '8': case '9':
  2628. case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
  2629. case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
  2630. buf.append('\\');
  2631. while (index < len) {
  2632. buf.append(s.charAt(index++));
  2633. }
  2634. break;
  2635. case '\n':
  2636. case '\f':
  2637. break;
  2638. case '\r':
  2639. if (index + 1 < len) {
  2640. if (s.charAt(index + 1) == '\n') {
  2641. index ++;
  2642. }
  2643. }
  2644. break;
  2645. default:
  2646. buf.append(c);
  2647. }
  2648. } else {
  2649. throw new CSSParseException("invalid string " + s, getLocator());
  2650. }
  2651. } else {
  2652. buf.append(c);
  2653. }
  2654. index++;
  2655. }
  2656. return buf.toString();
  2657. }
  2658. JAVACODE
  2659. String convertIdent(String s) {
  2660. return convertStringIndex(s, 0, s.length());
  2661. }
  2662. JAVACODE
  2663. String convertString(String s) {
  2664. return convertStringIndex(s, 0, s.length());
  2665. }
  2666. JAVACODE
  2667. void comments(){
  2668. if (token.specialToken != null){
  2669. Token tmp_t = token.specialToken;
  2670. while (tmp_t.specialToken != null) tmp_t = tmp_t.specialToken;
  2671. while (tmp_t != null) {
  2672. documentHandler.comment(tmp_t.image);
  2673. tmp_t = tmp_t.next;
  2674. }
  2675. }
  2676. }
  2677. /*
  2678. * @@HACK
  2679. * I can't insert a token into the tokens flow.
  2680. * It's jj_consume_token implementation dependant! :-(
  2681. */
  2682. JAVACODE
  2683. void rejectToken(Token t) {
  2684. Token fakeToken = new Token();
  2685. t.next = token;
  2686. fakeToken.next = t;
  2687. token = fakeToken;
  2688. }
  2689. /**
  2690. * skip after an expression
  2691. */
  2692. JAVACODE
  2693. String skipAfterExpression() {
  2694. Token t = getToken(1);
  2695. StringBuffer s = new StringBuffer();
  2696. s.append(getToken(0).image);
  2697. while ((t.kind != RBRACE) && (t.kind != SEMICOLON) && (t.kind != EOF)) {
  2698. s.append(t.image);
  2699. getNextToken();
  2700. t = getToken(1);
  2701. }
  2702. return s.toString();
  2703. }
  2704. /**
  2705. * The following functions are useful for a DOM CSS implementation only and are
  2706. * not part of the general CSS2 parser.
  2707. */
  2708. void _parseRule() :
  2709. {String ret = null;
  2710. }
  2711. {
  2712. ( <S> )*
  2713. ( importDeclaration() | debuggingDirective() | styleRule() | media() | page() | fontFace() | ret=skipStatement()
  2714. {
  2715. if ((ret == null) || (ret.length() == 0)) {
  2716. return;
  2717. }
  2718. if (ret.charAt(0) == '@') {
  2719. documentHandler.unrecognizedRule(ret);
  2720. } else {
  2721. throw new CSSParseException("unrecognize rule: " + ret,
  2722. getLocator());
  2723. }
  2724. }
  2725. )
  2726. }
  2727. void _parseImportRule() :
  2728. {
  2729. }
  2730. {
  2731. ( <S> )* importDeclaration()
  2732. }
  2733. void _parseMediaRule() :
  2734. {
  2735. }
  2736. {
  2737. ( <S> )* media()
  2738. }
  2739. void _parseDeclarationBlock() :
  2740. {
  2741. }
  2742. {
  2743. ( <S> )*
  2744. ( declaration() )? ( ";" ( <S> )* ( declaration() )? )*
  2745. }
  2746. ArrayList<String> _parseSelectors() :
  2747. { ArrayList<String> p = null;
  2748. }
  2749. {
  2750. try {
  2751. ( <S> )* p = selectorList()
  2752. { return p; }
  2753. } catch (ThrowedParseException e) {
  2754. throw (ParseException) e.e.fillInStackTrace();
  2755. }
  2756. }
  2757. /*
  2758. * Local Variables:
  2759. * compile-command: javacc Parser.jj & javac Parser.java
  2760. * End:
  2761. */