parser.cpp 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <vector>
  4. #include "parser.hpp"
  5. #include "file.hpp"
  6. #include "inspect.hpp"
  7. #include "to_string.hpp"
  8. #include "constants.hpp"
  9. #include "util.hpp"
  10. #ifndef SASS_PRELEXER
  11. #include "prelexer.hpp"
  12. #endif
  13. #include "sass_functions.h"
  14. #include <typeinfo>
  15. namespace Sass {
  16. using namespace std;
  17. using namespace Constants;
  18. Parser Parser::from_c_str(const char* str, Context& ctx, string path, Position source_position)
  19. {
  20. Parser p(ctx, path, source_position);
  21. p.source = str;
  22. p.position = p.source;
  23. p.end = str + strlen(str);
  24. return p;
  25. }
  26. Parser Parser::from_token(Token t, Context& ctx, string path, Position source_position)
  27. {
  28. Parser p(ctx, path, source_position);
  29. p.source = t.begin;
  30. p.position = p.source;
  31. p.end = t.end;
  32. p.dequote = true;
  33. return p;
  34. }
  35. Block* Parser::parse()
  36. {
  37. Block* root = new (ctx.mem) Block(path, source_position);
  38. root->is_root(true);
  39. read_bom();
  40. lex< optional_spaces >();
  41. Selector_Lookahead lookahead_result;
  42. while (position < end) {
  43. if (lex< block_comment >()) {
  44. String* contents = parse_interpolated_chunk(lexed);
  45. Comment* comment = new (ctx.mem) Comment(path, source_position, contents);
  46. (*root) << comment;
  47. }
  48. else if (peek< import >()) {
  49. Import* imp = parse_import();
  50. if (!imp->urls().empty()) (*root) << imp;
  51. if (!imp->files().empty()) {
  52. for (size_t i = 0, S = imp->files().size(); i < S; ++i) {
  53. (*root) << new (ctx.mem) Import_Stub(path, source_position, imp->files()[i]);
  54. }
  55. }
  56. if (!lex< one_plus< exactly<';'> > >()) error("top-level @import directive must be terminated by ';'");
  57. }
  58. else if (peek< mixin >() || peek< function >()) {
  59. (*root) << parse_definition();
  60. }
  61. else if (peek< variable >()) {
  62. (*root) << parse_assignment();
  63. if (!lex< one_plus< exactly<';'> > >()) error("top-level variable binding must be terminated by ';'");
  64. }
  65. else if (peek< sequence< optional< exactly<'*'> >, alternatives< identifier_schema, identifier >, optional_spaces, exactly<':'>, optional_spaces, exactly<'{'> > >(position)) {
  66. (*root) << parse_propset();
  67. }
  68. else if (peek< include >() /* || peek< exactly<'+'> >() */) {
  69. Mixin_Call* mixin_call = parse_mixin_call();
  70. (*root) << mixin_call;
  71. if (!mixin_call->block() && !lex< one_plus< exactly<';'> > >()) error("top-level @include directive must be terminated by ';'");
  72. }
  73. else if (peek< if_directive >()) {
  74. (*root) << parse_if_directive();
  75. }
  76. else if (peek< for_directive >()) {
  77. (*root) << parse_for_directive();
  78. }
  79. else if (peek< each_directive >()) {
  80. (*root) << parse_each_directive();
  81. }
  82. else if (peek< while_directive >()) {
  83. (*root) << parse_while_directive();
  84. }
  85. else if (peek< media >()) {
  86. (*root) << parse_media_block();
  87. }
  88. else if (peek< supports >()) {
  89. (*root) << parse_feature_block();
  90. }
  91. else if (peek< warn >()) {
  92. (*root) << parse_warning();
  93. if (!lex< one_plus< exactly<';'> > >()) error("top-level @warn directive must be terminated by ';'");
  94. }
  95. else if (peek< err >()) {
  96. (*root) << parse_error();
  97. if (!lex< one_plus< exactly<';'> > >()) error("top-level @error directive must be terminated by ';'");
  98. }
  99. else if (peek< dbg >()) {
  100. (*root) << parse_debug();
  101. if (!lex< one_plus< exactly<';'> > >()) error("top-level @debug directive must be terminated by ';'");
  102. }
  103. // ignore the @charset directive for now
  104. else if (lex< exactly< charset_kwd > >()) {
  105. lex< string_constant >();
  106. lex< one_plus< exactly<';'> > >();
  107. }
  108. else if (peek< at_keyword >()) {
  109. At_Rule* at_rule = parse_at_rule();
  110. (*root) << at_rule;
  111. if (!at_rule->block() && !lex< one_plus< exactly<';'> > >()) error("top-level directive must be terminated by ';'");
  112. }
  113. else if ((lookahead_result = lookahead_for_selector(position)).found) {
  114. (*root) << parse_ruleset(lookahead_result);
  115. }
  116. else if (peek< exactly<';'> >()) {
  117. lex< one_plus< exactly<';'> > >();
  118. }
  119. else {
  120. lex< spaces_and_comments >();
  121. if (position >= end) break;
  122. error("invalid top-level expression");
  123. }
  124. lex< optional_spaces >();
  125. }
  126. return root;
  127. }
  128. void Parser::add_single_file (Import* imp, string import_path) {
  129. string extension;
  130. string unquoted(unquote(import_path));
  131. if (unquoted.length() > 4) { // 2 quote marks + the 4 chars in .css
  132. // a string constant is guaranteed to end with a quote mark, so make sure to skip it when indexing from the end
  133. extension = unquoted.substr(unquoted.length() - 4, 4);
  134. }
  135. if (extension == ".css") {
  136. String_Constant* loc = new (ctx.mem) String_Constant(path, source_position, import_path, true);
  137. Argument* loc_arg = new (ctx.mem) Argument(path, source_position, loc);
  138. Arguments* loc_args = new (ctx.mem) Arguments(path, source_position);
  139. (*loc_args) << loc_arg;
  140. Function_Call* new_url = new (ctx.mem) Function_Call(path, source_position, "url", loc_args);
  141. imp->urls().push_back(new_url);
  142. }
  143. else {
  144. string current_dir = File::dir_name(path);
  145. string resolved(ctx.add_file(current_dir, unquoted));
  146. if (resolved.empty()) error("file to import not found or unreadable: " + unquoted + "\nCurrent dir: " + current_dir);
  147. imp->files().push_back(resolved);
  148. }
  149. }
  150. Import* Parser::parse_import()
  151. {
  152. lex< import >();
  153. Import* imp = new (ctx.mem) Import(path, source_position);
  154. bool first = true;
  155. do {
  156. if (lex< string_constant >()) {
  157. string import_path(lexed);
  158. // struct Sass_Options opt = sass_context_get_options(ctx)
  159. Sass_C_Import_Callback importer = ctx.importer;
  160. // custom importer
  161. if (importer) {
  162. Sass_Import* current = ctx.import_stack.back();
  163. Sass_C_Import_Fn fn = sass_import_get_function(importer);
  164. void* cookie = sass_import_get_cookie(importer);
  165. // create a new import entry
  166. string inc_path = unquote(import_path);
  167. struct Sass_Import** includes = fn(
  168. inc_path.c_str(),
  169. sass_import_get_path(current),
  170. cookie);
  171. if (includes) {
  172. struct Sass_Import** list = includes;
  173. while (*includes) {
  174. struct Sass_Import* include = *includes;
  175. const char *file = sass_import_get_path(include);
  176. char *source = sass_import_take_source(include);
  177. // char *srcmap = sass_import_take_srcmap(include);
  178. if (source) {
  179. if (file) {
  180. ctx.add_source(file, inc_path, source);
  181. imp->files().push_back(file);
  182. } else {
  183. ctx.add_source(inc_path, inc_path, source);
  184. imp->files().push_back(inc_path);
  185. }
  186. } else if(file) {
  187. add_single_file(imp, file);
  188. }
  189. ++includes;
  190. }
  191. // deallocate returned memory
  192. sass_delete_import_list(list);
  193. // parse next import
  194. continue;
  195. }
  196. }
  197. add_single_file(imp, import_path);
  198. }
  199. else if (peek< uri_prefix >()) {
  200. imp->urls().push_back(parse_value());
  201. }
  202. else {
  203. if (first) error("@import directive requires a url or quoted path");
  204. else error("expecting another url or quoted path in @import list");
  205. }
  206. first = false;
  207. } while (lex< exactly<','> >());
  208. return imp;
  209. }
  210. Definition* Parser::parse_definition()
  211. {
  212. Definition::Type which_type = Definition::MIXIN;
  213. if (lex< mixin >()) which_type = Definition::MIXIN;
  214. else if (lex< function >()) which_type = Definition::FUNCTION;
  215. string which_str(lexed);
  216. if (!lex< identifier >()) error("invalid name in " + which_str + " definition");
  217. string name(Util::normalize_underscores(lexed));
  218. if (which_type == Definition::FUNCTION && (name == "and" || name == "or" || name == "not"))
  219. { error("Invalid function name \"" + name + "\"."); }
  220. Position source_position_of_def = source_position;
  221. Parameters* params = parse_parameters();
  222. if (!peek< exactly<'{'> >()) error("body for " + which_str + " " + name + " must begin with a '{'");
  223. if (which_type == Definition::MIXIN) stack.push_back(mixin_def);
  224. else stack.push_back(function_def);
  225. Block* body = parse_block();
  226. stack.pop_back();
  227. Definition* def = new (ctx.mem) Definition(path, source_position_of_def, name, params, body, which_type);
  228. return def;
  229. }
  230. Parameters* Parser::parse_parameters()
  231. {
  232. string name(lexed); // for the error message
  233. Parameters* params = new (ctx.mem) Parameters(path, source_position);
  234. if (lex< exactly<'('> >()) {
  235. // if there's anything there at all
  236. if (!peek< exactly<')'> >()) {
  237. do (*params) << parse_parameter();
  238. while (lex< exactly<','> >());
  239. }
  240. if (!lex< exactly<')'> >()) error("expected a variable name (e.g. $x) or ')' for the parameter list for " + name);
  241. }
  242. return params;
  243. }
  244. Parameter* Parser::parse_parameter()
  245. {
  246. lex< variable >();
  247. string name(Util::normalize_underscores(lexed));
  248. Position pos = source_position;
  249. Expression* val = 0;
  250. bool is_rest = false;
  251. if (lex< exactly<':'> >()) { // there's a default value
  252. val = parse_space_list();
  253. val->is_delayed(false);
  254. }
  255. else if (lex< exactly< ellipsis > >()) {
  256. is_rest = true;
  257. }
  258. Parameter* p = new (ctx.mem) Parameter(path, pos, name, val, is_rest);
  259. return p;
  260. }
  261. Mixin_Call* Parser::parse_mixin_call()
  262. {
  263. lex< include >() /* || lex< exactly<'+'> >() */;
  264. if (!lex< identifier >()) error("invalid name in @include directive");
  265. Position source_position_of_call = source_position;
  266. string name(Util::normalize_underscores(lexed));
  267. Arguments* args = parse_arguments();
  268. Block* content = 0;
  269. if (peek< exactly<'{'> >()) {
  270. content = parse_block();
  271. }
  272. Mixin_Call* the_call = new (ctx.mem) Mixin_Call(path, source_position_of_call, name, args, content);
  273. return the_call;
  274. }
  275. Arguments* Parser::parse_arguments()
  276. {
  277. string name(lexed);
  278. Arguments* args = new (ctx.mem) Arguments(path, source_position);
  279. if (lex< exactly<'('> >()) {
  280. // if there's anything there at all
  281. if (!peek< exactly<')'> >()) {
  282. do (*args) << parse_argument();
  283. while (lex< exactly<','> >());
  284. }
  285. if (!lex< exactly<')'> >()) error("expected a variable name (e.g. $x) or ')' for the parameter list for " + name);
  286. }
  287. return args;
  288. }
  289. Argument* Parser::parse_argument()
  290. {
  291. Argument* arg;
  292. if (peek< sequence < variable, spaces_and_comments, exactly<':'> > >()) {
  293. lex< variable >();
  294. string name(Util::normalize_underscores(lexed));
  295. Position p = source_position;
  296. lex< exactly<':'> >();
  297. Expression* val = parse_space_list();
  298. val->is_delayed(false);
  299. arg = new (ctx.mem) Argument(path, p, val, name);
  300. }
  301. else {
  302. bool is_arglist = false;
  303. bool is_keyword = false;
  304. Expression* val = parse_space_list();
  305. val->is_delayed(false);
  306. if (lex< exactly< ellipsis > >()) {
  307. if (val->concrete_type() == Expression::MAP) is_keyword = true;
  308. else is_arglist = true;
  309. }
  310. arg = new (ctx.mem) Argument(path, source_position, val, "", is_arglist, is_keyword);
  311. }
  312. return arg;
  313. }
  314. Assignment* Parser::parse_assignment()
  315. {
  316. lex< variable >();
  317. string name(Util::normalize_underscores(lexed));
  318. Position var_source_position = source_position;
  319. if (!lex< exactly<':'> >()) error("expected ':' after " + name + " in assignment statement");
  320. Expression* val = parse_list();
  321. val->is_delayed(false);
  322. bool is_guarded = false;
  323. bool is_global = false;
  324. while (peek< default_flag >() || peek< global_flag >()) {
  325. is_guarded = lex< default_flag >() || is_guarded;
  326. is_global = lex< global_flag >() || is_global;
  327. }
  328. Assignment* var = new (ctx.mem) Assignment(path, var_source_position, name, val, is_guarded, is_global);
  329. return var;
  330. }
  331. Propset* Parser::parse_propset()
  332. {
  333. String* property_segment;
  334. if (peek< sequence< optional< exactly<'*'> >, identifier_schema > >()) {
  335. property_segment = parse_identifier_schema();
  336. }
  337. else {
  338. lex< sequence< optional< exactly<'*'> >, identifier > >();
  339. property_segment = new (ctx.mem) String_Constant(path, source_position, lexed);
  340. }
  341. Propset* propset = new (ctx.mem) Propset(path, source_position, property_segment);
  342. lex< exactly<':'> >();
  343. if (!peek< exactly<'{'> >()) error("expected a '{' after namespaced property");
  344. propset->block(parse_block());
  345. return propset;
  346. }
  347. Ruleset* Parser::parse_ruleset(Selector_Lookahead lookahead)
  348. {
  349. Selector* sel;
  350. if (lookahead.has_interpolants) {
  351. sel = parse_selector_schema(lookahead.found);
  352. }
  353. else {
  354. sel = parse_selector_group();
  355. }
  356. Position r_source_position = source_position;
  357. if (!peek< exactly<'{'> >()) error("expected a '{' after the selector");
  358. Block* block = parse_block();
  359. Ruleset* ruleset = new (ctx.mem) Ruleset(path, r_source_position, sel, block);
  360. return ruleset;
  361. }
  362. Selector_Schema* Parser::parse_selector_schema(const char* end_of_selector)
  363. {
  364. lex< optional_spaces >();
  365. const char* i = position;
  366. const char* p;
  367. String_Schema* schema = new (ctx.mem) String_Schema(path, source_position);
  368. while (i < end_of_selector) {
  369. p = find_first_in_interval< exactly<hash_lbrace> >(i, end_of_selector);
  370. if (p) {
  371. // accumulate the preceding segment if there is one
  372. if (i < p) (*schema) << new (ctx.mem) String_Constant(path, source_position, Token(i, p));
  373. // find the end of the interpolant and parse it
  374. const char* j = find_first_in_interval< exactly<rbrace> >(p, end_of_selector);
  375. Expression* interp_node = Parser::from_token(Token(p+2, j), ctx, path, source_position).parse_list();
  376. interp_node->is_interpolant(true);
  377. (*schema) << interp_node;
  378. i = j + 1;
  379. }
  380. else { // no interpolants left; add the last segment if there is one
  381. if (i < end_of_selector) (*schema) << new (ctx.mem) String_Constant(path, source_position, Token(i, end_of_selector));
  382. break;
  383. }
  384. }
  385. position = end_of_selector;
  386. return new (ctx.mem) Selector_Schema(path, source_position, schema);
  387. }
  388. Selector_List* Parser::parse_selector_group()
  389. {
  390. To_String to_string;
  391. lex< spaces_and_comments >();
  392. Selector_List* group = new (ctx.mem) Selector_List(path, source_position);
  393. do {
  394. if (peek< exactly<'{'> >() ||
  395. peek< exactly<'}'> >() ||
  396. peek< exactly<')'> >() ||
  397. peek< exactly<';'> >())
  398. break; // in case there are superfluous commas at the end
  399. Complex_Selector* comb = parse_selector_combination();
  400. if (!comb->has_reference()) {
  401. Position sel_source_position = source_position;
  402. Selector_Reference* ref = new (ctx.mem) Selector_Reference(path, sel_source_position);
  403. Compound_Selector* ref_wrap = new (ctx.mem) Compound_Selector(path, sel_source_position);
  404. (*ref_wrap) << ref;
  405. if (!comb->head()) {
  406. comb->head(ref_wrap);
  407. comb->has_reference(true);
  408. }
  409. else {
  410. comb = new (ctx.mem) Complex_Selector(path, sel_source_position, Complex_Selector::ANCESTOR_OF, ref_wrap, comb);
  411. comb->has_reference(true);
  412. }
  413. }
  414. (*group) << comb;
  415. }
  416. while (lex< one_plus< sequence< spaces_and_comments, exactly<','> > > >());
  417. while (lex< optional >()); // JMA - ignore optional flag if it follows the selector group
  418. return group;
  419. }
  420. Complex_Selector* Parser::parse_selector_combination()
  421. {
  422. Position sel_source_position = Position();
  423. Compound_Selector* lhs;
  424. if (peek< exactly<'+'> >() ||
  425. peek< exactly<'~'> >() ||
  426. peek< exactly<'>'> >()) {
  427. // no selector before the combinator
  428. lhs = 0;
  429. }
  430. else {
  431. lhs = parse_simple_selector_sequence();
  432. sel_source_position = source_position;
  433. }
  434. Complex_Selector::Combinator cmb;
  435. if (lex< exactly<'+'> >()) cmb = Complex_Selector::ADJACENT_TO;
  436. else if (lex< exactly<'~'> >()) cmb = Complex_Selector::PRECEDES;
  437. else if (lex< exactly<'>'> >()) cmb = Complex_Selector::PARENT_OF;
  438. else cmb = Complex_Selector::ANCESTOR_OF;
  439. Complex_Selector* rhs;
  440. if (peek< exactly<','> >() ||
  441. peek< exactly<')'> >() ||
  442. peek< exactly<'{'> >() ||
  443. peek< exactly<'}'> >() ||
  444. peek< exactly<';'> >() ||
  445. peek< optional >()) {
  446. // no selector after the combinator
  447. rhs = 0;
  448. }
  449. else {
  450. rhs = parse_selector_combination();
  451. sel_source_position = source_position;
  452. }
  453. if (!sel_source_position.line) sel_source_position = source_position;
  454. return new (ctx.mem) Complex_Selector(path, sel_source_position, cmb, lhs, rhs);
  455. }
  456. Compound_Selector* Parser::parse_simple_selector_sequence()
  457. {
  458. Compound_Selector* seq = new (ctx.mem) Compound_Selector(path, source_position);
  459. bool sawsomething = false;
  460. if (lex< exactly<'&'> >()) {
  461. // if you see a &
  462. (*seq) << new (ctx.mem) Selector_Reference(path, source_position);
  463. sawsomething = true;
  464. // if you see a space after a &, then you're done
  465. if(lex< spaces >()) {
  466. return seq;
  467. }
  468. }
  469. if (sawsomething && lex< sequence< negate< functional >, alternatives< identifier_fragment, universal, string_constant, dimension, percentage, number > > >()) {
  470. // saw an ampersand, then allow type selectors with arbitrary number of hyphens at the beginning
  471. (*seq) << new (ctx.mem) Type_Selector(path, source_position, lexed);
  472. } else if (lex< sequence< negate< functional >, alternatives< type_selector, universal, string_constant, dimension, percentage, number > > >()) {
  473. // if you see a type selector
  474. (*seq) << new (ctx.mem) Type_Selector(path, source_position, lexed);
  475. sawsomething = true;
  476. }
  477. if (!sawsomething) {
  478. // don't blindly do this if you saw a & or selector
  479. (*seq) << parse_simple_selector();
  480. }
  481. while (!peek< spaces >(position) &&
  482. !(peek < exactly<'+'> >(position) ||
  483. peek < exactly<'~'> >(position) ||
  484. peek < exactly<'>'> >(position) ||
  485. peek < exactly<','> >(position) ||
  486. peek < exactly<')'> >(position) ||
  487. peek < exactly<'{'> >(position) ||
  488. peek < exactly<'}'> >(position) ||
  489. peek < exactly<';'> >(position))) {
  490. (*seq) << parse_simple_selector();
  491. }
  492. return seq;
  493. }
  494. Simple_Selector* Parser::parse_simple_selector()
  495. {
  496. if (lex< id_name >() || lex< class_name >()) {
  497. return new (ctx.mem) Selector_Qualifier(path, source_position, lexed);
  498. }
  499. else if (lex< string_constant >() || lex< number >()) {
  500. return new (ctx.mem) Type_Selector(path, source_position, lexed);
  501. }
  502. else if (peek< pseudo_not >()) {
  503. return parse_negated_selector();
  504. }
  505. else if (peek< exactly<':'> >(position) || peek< functional >()) {
  506. return parse_pseudo_selector();
  507. }
  508. else if (peek< exactly<'['> >(position)) {
  509. return parse_attribute_selector();
  510. }
  511. else if (lex< placeholder >()) {
  512. return new (ctx.mem) Selector_Placeholder(path, source_position, lexed);
  513. }
  514. else {
  515. error("invalid selector after " + lexed.to_string());
  516. }
  517. // unreachable statement
  518. return 0;
  519. }
  520. Wrapped_Selector* Parser::parse_negated_selector()
  521. {
  522. lex< pseudo_not >();
  523. string name(lexed);
  524. Position nsource_position = source_position;
  525. Selector* negated = parse_selector_group();
  526. if (!lex< exactly<')'> >()) {
  527. error("negated selector is missing ')'");
  528. }
  529. return new (ctx.mem) Wrapped_Selector(path, nsource_position, name, negated);
  530. }
  531. Simple_Selector* Parser::parse_pseudo_selector() {
  532. if (lex< sequence< pseudo_prefix, functional > >() || lex< functional >()) {
  533. string name(lexed);
  534. String* expr = 0;
  535. Position p = source_position;
  536. Selector* wrapped = 0;
  537. if (lex< alternatives< even, odd > >()) {
  538. expr = new (ctx.mem) String_Constant(path, p, lexed);
  539. }
  540. else if (peek< binomial >(position)) {
  541. lex< sequence< optional< coefficient >, exactly<'n'> > >();
  542. String_Constant* var_coef = new (ctx.mem) String_Constant(path, p, lexed);
  543. lex< sign >();
  544. String_Constant* op = new (ctx.mem) String_Constant(path, p, lexed);
  545. // Binary_Expression::Type op = (lexed == "+" ? Binary_Expression::ADD : Binary_Expression::SUB);
  546. lex< digits >();
  547. String_Constant* constant = new (ctx.mem) String_Constant(path, p, lexed);
  548. // expr = new (ctx.mem) Binary_Expression(path, p, op, var_coef, constant);
  549. String_Schema* schema = new (ctx.mem) String_Schema(path, p, 3);
  550. *schema << var_coef << op << constant;
  551. expr = schema;
  552. }
  553. else if (peek< sequence< optional<sign>,
  554. optional<digits>,
  555. exactly<'n'>,
  556. spaces_and_comments,
  557. exactly<')'> > >()) {
  558. lex< sequence< optional<sign>,
  559. optional<digits>,
  560. exactly<'n'> > >();
  561. expr = new (ctx.mem) String_Constant(path, p, lexed);
  562. }
  563. else if (lex< sequence< optional<sign>, digits > >()) {
  564. expr = new (ctx.mem) String_Constant(path, p, lexed);
  565. }
  566. else if (peek< sequence< identifier, spaces_and_comments, exactly<')'> > >()) {
  567. lex< identifier >();
  568. expr = new (ctx.mem) String_Constant(path, p, lexed);
  569. }
  570. else if (lex< string_constant >()) {
  571. expr = new (ctx.mem) String_Constant(path, p, lexed);
  572. }
  573. else if (peek< exactly<')'> >()) {
  574. expr = new (ctx.mem) String_Constant(path, p, "");
  575. }
  576. else {
  577. wrapped = parse_selector_group();
  578. }
  579. if (!lex< exactly<')'> >()) error("unterminated argument to " + name + "...)");
  580. if (wrapped) {
  581. return new (ctx.mem) Wrapped_Selector(path, p, name, wrapped);
  582. }
  583. return new (ctx.mem) Pseudo_Selector(path, p, name, expr);
  584. }
  585. else if (lex < sequence< pseudo_prefix, identifier > >()) {
  586. return new (ctx.mem) Pseudo_Selector(path, source_position, lexed);
  587. }
  588. else {
  589. error("unrecognized pseudo-class or pseudo-element");
  590. }
  591. // unreachable statement
  592. return 0;
  593. }
  594. Attribute_Selector* Parser::parse_attribute_selector()
  595. {
  596. lex< exactly<'['> >();
  597. Position p = source_position;
  598. if (!lex< attribute_name >()) error("invalid attribute name in attribute selector");
  599. string name(lexed);
  600. if (lex< exactly<']'> >()) return new (ctx.mem) Attribute_Selector(path, p, name, "", 0);
  601. if (!lex< alternatives< exact_match, class_match, dash_match,
  602. prefix_match, suffix_match, substring_match > >()) {
  603. error("invalid operator in attribute selector for " + name);
  604. }
  605. string matcher(lexed);
  606. String* value = 0;
  607. if (lex< identifier >()) {
  608. value = new (ctx.mem) String_Constant(path, p, lexed, true);
  609. }
  610. else if (lex< string_constant >()) {
  611. value = parse_interpolated_chunk(lexed);
  612. }
  613. else {
  614. error("expected a string constant or identifier in attribute selector for " + name);
  615. }
  616. if (!lex< exactly<']'> >()) error("unterminated attribute selector for " + name);
  617. return new (ctx.mem) Attribute_Selector(path, p, name, matcher, value);
  618. }
  619. Block* Parser::parse_block()
  620. {
  621. lex< exactly<'{'> >();
  622. bool semicolon = false;
  623. Selector_Lookahead lookahead_result;
  624. Block* block = new (ctx.mem) Block(path, source_position);
  625. // JMA - ensure that a block containing only block_comments is parsed
  626. while (lex< block_comment >()) {
  627. String* contents = parse_interpolated_chunk(lexed);
  628. Comment* comment = new (ctx.mem) Comment(path, source_position, contents);
  629. (*block) << comment;
  630. }
  631. while (!lex< exactly<'}'> >()) {
  632. if (semicolon) {
  633. if (!lex< one_plus< exactly<';'> > >()) {
  634. error("non-terminal statement or declaration must end with ';'");
  635. }
  636. semicolon = false;
  637. while (lex< block_comment >()) {
  638. String* contents = parse_interpolated_chunk(lexed);
  639. Comment* comment = new (ctx.mem) Comment(path, source_position, contents);
  640. (*block) << comment;
  641. }
  642. if (lex< sequence< exactly<'}'>, zero_plus< exactly<';'> > > >()) break;
  643. }
  644. if (lex< block_comment >()) {
  645. String* contents = parse_interpolated_chunk(lexed);
  646. Comment* comment = new (ctx.mem) Comment(path, source_position, contents);
  647. (*block) << comment;
  648. }
  649. else if (peek< import >(position)) {
  650. if (stack.back() == mixin_def || stack.back() == function_def) {
  651. lex< import >(); // to adjust the source_position number
  652. error("@import directives are not allowed inside mixins and functions");
  653. }
  654. Import* imp = parse_import();
  655. if (!imp->urls().empty()) (*block) << imp;
  656. if (!imp->files().empty()) {
  657. for (size_t i = 0, S = imp->files().size(); i < S; ++i) {
  658. (*block) << new (ctx.mem) Import_Stub(path, source_position, imp->files()[i]);
  659. }
  660. }
  661. semicolon = true;
  662. }
  663. else if (lex< variable >()) {
  664. (*block) << parse_assignment();
  665. semicolon = true;
  666. }
  667. else if (peek< if_directive >()) {
  668. (*block) << parse_if_directive();
  669. }
  670. else if (peek< for_directive >()) {
  671. (*block) << parse_for_directive();
  672. }
  673. else if (peek< each_directive >()) {
  674. (*block) << parse_each_directive();
  675. }
  676. else if (peek < while_directive >()) {
  677. (*block) << parse_while_directive();
  678. }
  679. else if (lex < return_directive >()) {
  680. (*block) << new (ctx.mem) Return(path, source_position, parse_list());
  681. semicolon = true;
  682. }
  683. else if (peek< warn >()) {
  684. (*block) << parse_warning();
  685. semicolon = true;
  686. }
  687. else if (peek< err >()) {
  688. (*block) << parse_error();
  689. semicolon = true;
  690. }
  691. else if (peek< dbg >()) {
  692. (*block) << parse_debug();
  693. semicolon = true;
  694. }
  695. else if (stack.back() == function_def) {
  696. error("only variable declarations and control directives are allowed inside functions");
  697. }
  698. else if (peek< mixin >() || peek< function >()) {
  699. (*block) << parse_definition();
  700. }
  701. else if (peek< include >(position)) {
  702. Mixin_Call* the_call = parse_mixin_call();
  703. (*block) << the_call;
  704. // don't need a semicolon after a content block
  705. semicolon = (the_call->block()) ? false : true;
  706. }
  707. else if (lex< content >()) {
  708. if (stack.back() != mixin_def) {
  709. error("@content may only be used within a mixin");
  710. }
  711. (*block) << new (ctx.mem) Content(path, source_position);
  712. semicolon = true;
  713. }
  714. /*
  715. else if (peek< exactly<'+'> >()) {
  716. (*block) << parse_mixin_call();
  717. semicolon = true;
  718. }
  719. */
  720. else if (lex< extend >()) {
  721. Selector_Lookahead lookahead = lookahead_for_extension_target(position);
  722. if (!lookahead.found) error("invalid selector for @extend");
  723. Selector* target;
  724. if (lookahead.has_interpolants) target = parse_selector_schema(lookahead.found);
  725. else target = parse_selector_group();
  726. (*block) << new (ctx.mem) Extension(path, source_position, target);
  727. semicolon = true;
  728. }
  729. else if (peek< media >()) {
  730. (*block) << parse_media_block();
  731. }
  732. else if (peek< supports >()) {
  733. (*block) << parse_feature_block();
  734. }
  735. // ignore the @charset directive for now
  736. else if (lex< exactly< charset_kwd > >()) {
  737. lex< string_constant >();
  738. lex< one_plus< exactly<';'> > >();
  739. }
  740. else if (peek< at_keyword >()) {
  741. At_Rule* at_rule = parse_at_rule();
  742. (*block) << at_rule;
  743. if (!at_rule->block()) semicolon = true;
  744. }
  745. else if ((lookahead_result = lookahead_for_selector(position)).found) {
  746. (*block) << parse_ruleset(lookahead_result);
  747. }
  748. else if (peek< sequence< optional< exactly<'*'> >, alternatives< identifier_schema, identifier >, optional_spaces, exactly<':'>, optional_spaces, exactly<'{'> > >(position)) {
  749. (*block) << parse_propset();
  750. }
  751. else if (!peek< exactly<';'> >()) {
  752. if (peek< sequence< optional< exactly<'*'> >, identifier_schema, exactly<':'>, exactly<'{'> > >()) {
  753. (*block) << parse_propset();
  754. }
  755. else if (peek< sequence< optional< exactly<'*'> >, identifier, exactly<':'>, exactly<'{'> > >()) {
  756. (*block) << parse_propset();
  757. }
  758. else {
  759. Declaration* decl = parse_declaration();
  760. (*block) << decl;
  761. if (peek< exactly<'{'> >()) {
  762. // parse a propset that rides on the declaration's property
  763. Propset* ps = new (ctx.mem) Propset(path, source_position, decl->property(), parse_block());
  764. (*block) << ps;
  765. }
  766. else {
  767. // finish and let the semicolon get munched
  768. semicolon = true;
  769. }
  770. }
  771. }
  772. else lex< one_plus< exactly<';'> > >();
  773. while (lex< block_comment >()) {
  774. String* contents = parse_interpolated_chunk(lexed);
  775. Comment* comment = new (ctx.mem) Comment(path, source_position, contents);
  776. (*block) << comment;
  777. }
  778. }
  779. return block;
  780. }
  781. Declaration* Parser::parse_declaration() {
  782. String* prop = 0;
  783. if (peek< sequence< optional< exactly<'*'> >, identifier_schema > >()) {
  784. prop = parse_identifier_schema();
  785. }
  786. else if (lex< sequence< optional< exactly<'*'> >, identifier > >()) {
  787. prop = new (ctx.mem) String_Constant(path, source_position, lexed);
  788. }
  789. else if (lex< custom_property_name >()) {
  790. prop = new (ctx.mem) String_Constant(path, source_position, lexed);
  791. }
  792. else {
  793. error("invalid property name");
  794. }
  795. if (!lex< one_plus< exactly<':'> > >()) error("property \"" + string(lexed) + "\" must be followed by a ':'");
  796. if (peek< exactly<';'> >()) error("style declaration must contain a value");
  797. if (peek< static_value >()) {
  798. return new (ctx.mem) Declaration(path, prop->position(), prop, parse_static_value()/*, lex<important>()*/);
  799. }
  800. else {
  801. return new (ctx.mem) Declaration(path, prop->position(), prop, parse_list()/*, lex<important>()*/);
  802. }
  803. }
  804. Expression* Parser::parse_map()
  805. {
  806. To_String to_string;
  807. Expression* key = parse_list();
  808. // it's not a map so return the lexed value as a list value
  809. if (!peek< exactly<':'> >())
  810. { return key; }
  811. lex< exactly<':'> >();
  812. Expression* value = parse_space_list();
  813. Map* map = new (ctx.mem) Map(path, source_position, 1);
  814. (*map) << make_pair(key, value);
  815. while (lex< exactly<','> >())
  816. {
  817. // allow trailing commas - #495
  818. if (peek< exactly<')'> >(position))
  819. { break; }
  820. Expression* key = parse_list();
  821. if (!(lex< exactly<':'> >()))
  822. { error("invalid syntax"); }
  823. Expression* value = parse_space_list();
  824. (*map) << make_pair(key, value);
  825. }
  826. if (map->has_duplicate_key())
  827. { error("Duplicate key \"" + map->get_duplicate_key()->perform(&to_string) + "\" in map " + map->perform(&to_string) + "."); }
  828. return map;
  829. }
  830. Expression* Parser::parse_list()
  831. {
  832. return parse_comma_list();
  833. }
  834. Expression* Parser::parse_comma_list()
  835. {
  836. if (//peek< exactly<'!'> >(position) ||
  837. peek< exactly<';'> >(position) ||
  838. peek< exactly<'}'> >(position) ||
  839. peek< exactly<'{'> >(position) ||
  840. peek< exactly<')'> >(position) ||
  841. //peek< exactly<':'> >(position) ||
  842. peek< exactly<ellipsis> >(position))
  843. { return new (ctx.mem) List(path, source_position, 0); }
  844. Expression* list1 = parse_space_list();
  845. // if it's a singleton, return it directly; don't wrap it
  846. if (!peek< exactly<','> >(position)) return list1;
  847. List* comma_list = new (ctx.mem) List(path, source_position, 2, List::COMMA);
  848. (*comma_list) << list1;
  849. while (lex< exactly<','> >())
  850. {
  851. if (//peek< exactly<'!'> >(position) ||
  852. peek< exactly<';'> >(position) ||
  853. peek< exactly<'}'> >(position) ||
  854. peek< exactly<'{'> >(position) ||
  855. peek< exactly<')'> >(position) ||
  856. peek< exactly<':'> >(position) ||
  857. peek< exactly<ellipsis> >(position)) {
  858. break;
  859. }
  860. Expression* list = parse_space_list();
  861. (*comma_list) << list;
  862. }
  863. return comma_list;
  864. }
  865. Expression* Parser::parse_space_list()
  866. {
  867. Expression* disj1 = parse_disjunction();
  868. // if it's a singleton, return it directly; don't wrap it
  869. if (//peek< exactly<'!'> >(position) ||
  870. peek< exactly<';'> >(position) ||
  871. peek< exactly<'}'> >(position) ||
  872. peek< exactly<'{'> >(position) ||
  873. peek< exactly<')'> >(position) ||
  874. peek< exactly<','> >(position) ||
  875. peek< exactly<':'> >(position) ||
  876. peek< exactly<ellipsis> >(position) ||
  877. peek< default_flag >(position) ||
  878. peek< global_flag >(position))
  879. { return disj1; }
  880. List* space_list = new (ctx.mem) List(path, source_position, 2, List::SPACE);
  881. (*space_list) << disj1;
  882. while (!(//peek< exactly<'!'> >(position) ||
  883. peek< exactly<';'> >(position) ||
  884. peek< exactly<'}'> >(position) ||
  885. peek< exactly<'{'> >(position) ||
  886. peek< exactly<')'> >(position) ||
  887. peek< exactly<','> >(position) ||
  888. peek< exactly<':'> >(position) ||
  889. peek< exactly<ellipsis> >(position) ||
  890. peek< default_flag >(position) ||
  891. peek< global_flag >(position)))
  892. {
  893. (*space_list) << parse_disjunction();
  894. }
  895. return space_list;
  896. }
  897. Expression* Parser::parse_disjunction()
  898. {
  899. Expression* conj1 = parse_conjunction();
  900. // if it's a singleton, return it directly; don't wrap it
  901. if (!peek< sequence< or_op, negate< identifier > > >()) return conj1;
  902. vector<Expression*> operands;
  903. while (lex< sequence< or_op, negate< identifier > > >())
  904. operands.push_back(parse_conjunction());
  905. return fold_operands(conj1, operands, Binary_Expression::OR);
  906. }
  907. Expression* Parser::parse_conjunction()
  908. {
  909. Expression* rel1 = parse_relation();
  910. // if it's a singleton, return it directly; don't wrap it
  911. if (!peek< sequence< and_op, negate< identifier > > >()) return rel1;
  912. vector<Expression*> operands;
  913. while (lex< sequence< and_op, negate< identifier > > >())
  914. operands.push_back(parse_relation());
  915. return fold_operands(rel1, operands, Binary_Expression::AND);
  916. }
  917. Expression* Parser::parse_relation()
  918. {
  919. Expression* expr1 = parse_expression();
  920. // if it's a singleton, return it directly; don't wrap it
  921. if (!(peek< eq_op >(position) ||
  922. peek< neq_op >(position) ||
  923. peek< gte_op >(position) ||
  924. peek< gt_op >(position) ||
  925. peek< lte_op >(position) ||
  926. peek< lt_op >(position)))
  927. { return expr1; }
  928. Binary_Expression::Type op
  929. = lex<eq_op>() ? Binary_Expression::EQ
  930. : lex<neq_op>() ? Binary_Expression::NEQ
  931. : lex<gte_op>() ? Binary_Expression::GTE
  932. : lex<lte_op>() ? Binary_Expression::LTE
  933. : lex<gt_op>() ? Binary_Expression::GT
  934. : lex<lt_op>() ? Binary_Expression::LT
  935. : Binary_Expression::LT; // whatever
  936. Expression* expr2 = parse_expression();
  937. return new (ctx.mem) Binary_Expression(path, expr1->position(), op, expr1, expr2);
  938. }
  939. Expression* Parser::parse_expression()
  940. {
  941. Expression* term1 = parse_term();
  942. // if it's a singleton, return it directly; don't wrap it
  943. if (!(peek< exactly<'+'> >(position) ||
  944. peek< sequence< negate< number >, exactly<'-'> > >(position)) ||
  945. peek< identifier >(position))
  946. { return term1; }
  947. vector<Expression*> operands;
  948. vector<Binary_Expression::Type> operators;
  949. while (lex< exactly<'+'> >() || lex< sequence< negate< number >, exactly<'-'> > >()) {
  950. operators.push_back(lexed == "+" ? Binary_Expression::ADD : Binary_Expression::SUB);
  951. operands.push_back(parse_term());
  952. }
  953. return fold_operands(term1, operands, operators);
  954. }
  955. Expression* Parser::parse_term()
  956. {
  957. Expression* fact1 = parse_factor();
  958. // Special case: Ruby sass never tries to modulo if the lhs contains an interpolant
  959. if (peek< exactly<'%'> >(position) && fact1->concrete_type() == Expression::STRING) {
  960. try {
  961. String_Schema* ss = dynamic_cast<String_Schema*>(fact1);
  962. if (ss->has_interpolants()) return fact1;
  963. }
  964. catch (bad_cast&) {}
  965. catch (...) { throw; }
  966. }
  967. // if it's a singleton, return it directly; don't wrap it
  968. if (!(peek< exactly<'*'> >(position) ||
  969. peek< exactly<'/'> >(position) ||
  970. peek< exactly<'%'> >(position)))
  971. { return fact1; }
  972. vector<Expression*> operands;
  973. vector<Binary_Expression::Type> operators;
  974. while (lex< exactly<'*'> >() || lex< exactly<'/'> >() || lex< exactly<'%'> >()) {
  975. if (lexed == "*") operators.push_back(Binary_Expression::MUL);
  976. else if (lexed == "/") operators.push_back(Binary_Expression::DIV);
  977. else operators.push_back(Binary_Expression::MOD);
  978. operands.push_back(parse_factor());
  979. }
  980. return fold_operands(fact1, operands, operators);
  981. }
  982. Expression* Parser::parse_factor()
  983. {
  984. if (lex< exactly<'('> >()) {
  985. Expression* value = parse_map();
  986. if (!lex< exactly<')'> >()) error("unclosed parenthesis");
  987. value->is_delayed(false);
  988. // make sure wrapped lists and division expressions are non-delayed within parentheses
  989. if (value->concrete_type() == Expression::LIST) {
  990. List* l = static_cast<List*>(value);
  991. if (!l->empty()) (*l)[0]->is_delayed(false);
  992. } else if (typeid(*value) == typeid(Binary_Expression)) {
  993. Binary_Expression* b = static_cast<Binary_Expression*>(value);
  994. Binary_Expression* lhs = static_cast<Binary_Expression*>(b->left());
  995. if (lhs && lhs->type() == Binary_Expression::DIV) lhs->is_delayed(false);
  996. }
  997. return value;
  998. }
  999. else if (peek< ie_property >()) {
  1000. return parse_ie_property();
  1001. }
  1002. else if (peek< ie_keyword_arg >()) {
  1003. return parse_ie_keyword_arg();
  1004. }
  1005. else if (peek< exactly< calc_kwd > >() ||
  1006. peek< exactly< moz_calc_kwd > >() ||
  1007. peek< exactly< webkit_calc_kwd > >()) {
  1008. return parse_calc_function();
  1009. }
  1010. else if (peek< functional_schema >()) {
  1011. return parse_function_call_schema();
  1012. }
  1013. else if (peek< sequence< identifier_schema, negate< exactly<'%'> > > >()) {
  1014. return parse_identifier_schema();
  1015. }
  1016. else if (peek< functional >() && !peek< uri_prefix >()) {
  1017. return parse_function_call();
  1018. }
  1019. else if (lex< sequence< exactly<'+'>, spaces_and_comments, negate< number > > >()) {
  1020. return new (ctx.mem) Unary_Expression(path, source_position, Unary_Expression::PLUS, parse_factor());
  1021. }
  1022. else if (lex< sequence< exactly<'-'>, spaces_and_comments, negate< number> > >()) {
  1023. return new (ctx.mem) Unary_Expression(path, source_position, Unary_Expression::MINUS, parse_factor());
  1024. }
  1025. else if (lex< sequence< not_op, spaces_and_comments > >()) {
  1026. return new (ctx.mem) Unary_Expression(path, source_position, Unary_Expression::NOT, parse_factor());
  1027. }
  1028. else {
  1029. return parse_value();
  1030. }
  1031. }
  1032. Expression* Parser::parse_value()
  1033. {
  1034. if (lex< uri_prefix >()) {
  1035. Arguments* args = new (ctx.mem) Arguments(path, source_position);
  1036. Function_Call* result = new (ctx.mem) Function_Call(path, source_position, "url", args);
  1037. const char* here = position;
  1038. Position here_p = source_position;
  1039. // Try to parse a SassScript expression. If it succeeds and we can munch
  1040. // a matching rparen, then that's our url. If we can't munch a matching
  1041. // rparen, or if the attempt to parse an expression fails, then try to
  1042. // munch a regular CSS url.
  1043. try {
  1044. // special case -- if there's a comment, treat it as part of a URL
  1045. lex<spaces>();
  1046. if (peek<line_comment_prefix>() || peek<block_comment_prefix>()) error("comment in URL"); // doesn't really matter what we throw
  1047. Expression* expr = parse_list();
  1048. if (!lex< exactly<')'> >()) error("dangling expression in URL"); // doesn't really matter what we throw
  1049. Argument* arg = new (ctx.mem) Argument(path, expr->position(), expr);
  1050. *args << arg;
  1051. return result;
  1052. }
  1053. catch (Sass_Error&) {
  1054. // back up so we can try again
  1055. position = here;
  1056. source_position = here_p;
  1057. }
  1058. catch (...) { throw; }
  1059. lex< spaces >();
  1060. if (lex< url >()) {
  1061. String* the_url = parse_interpolated_chunk(lexed);
  1062. Argument* arg = new (ctx.mem) Argument(path, the_url->position(), the_url);
  1063. *args << arg;
  1064. }
  1065. else {
  1066. error("malformed URL");
  1067. }
  1068. if (!lex< exactly<')'> >()) error("URI is missing ')'");
  1069. return result;
  1070. }
  1071. if (lex< important >())
  1072. { return new (ctx.mem) String_Constant(path, source_position, "!important"); }
  1073. if (lex< value_schema >())
  1074. { return Parser::from_token(lexed, ctx, path, source_position).parse_value_schema(); }
  1075. if (lex< sequence< true_val, negate< identifier > > >())
  1076. { return new (ctx.mem) Boolean(path, source_position, true); }
  1077. if (lex< sequence< false_val, negate< identifier > > >())
  1078. { return new (ctx.mem) Boolean(path, source_position, false); }
  1079. if (lex< sequence< null, negate< identifier > > >())
  1080. { return new (ctx.mem) Null(path, source_position); }
  1081. if (lex< identifier >()) {
  1082. String_Constant* str = new (ctx.mem) String_Constant(path, source_position, lexed);
  1083. // Dont' delay this string if it is a name color. Fixes #652.
  1084. str->is_delayed(ctx.names_to_colors.count(lexed) == 0);
  1085. return str;
  1086. }
  1087. if (lex< percentage >())
  1088. { return new (ctx.mem) Textual(path, source_position, Textual::PERCENTAGE, lexed); }
  1089. if (lex< dimension >())
  1090. { return new (ctx.mem) Textual(path, source_position, Textual::DIMENSION, lexed); }
  1091. if (lex< number >())
  1092. { return new (ctx.mem) Textual(path, source_position, Textual::NUMBER, lexed); }
  1093. if (lex< hex >())
  1094. { return new (ctx.mem) Textual(path, source_position, Textual::HEX, lexed); }
  1095. if (peek< string_constant >())
  1096. { return parse_string(); }
  1097. if (lex< variable >())
  1098. { return new (ctx.mem) Variable(path, source_position, Util::normalize_underscores(lexed)); }
  1099. // Special case handling for `%` proceeding an interpolant.
  1100. if (lex< sequence< exactly<'%'>, optional< percentage > > >())
  1101. { return new (ctx.mem) String_Constant(path, source_position, lexed); }
  1102. error("error reading values after " + lexed.to_string());
  1103. // unreachable statement
  1104. return 0;
  1105. }
  1106. String* Parser::parse_interpolated_chunk(Token chunk)
  1107. {
  1108. const char* i = chunk.begin;
  1109. // see if there any interpolants
  1110. const char* p = find_first_in_interval< sequence< negate< exactly<'\\'> >, exactly<hash_lbrace> > >(chunk.begin, chunk.end);
  1111. if (!p) {
  1112. String_Constant* str_node = new (ctx.mem) String_Constant(path, source_position, chunk, dequote);
  1113. str_node->is_delayed(true);
  1114. return str_node;
  1115. }
  1116. String_Schema* schema = new (ctx.mem) String_Schema(path, source_position);
  1117. schema->quote_mark(*chunk.begin);
  1118. while (i < chunk.end) {
  1119. p = find_first_in_interval< sequence< negate< exactly<'\\'> >, exactly<hash_lbrace> > >(i, chunk.end);
  1120. if (p) {
  1121. if (i < p) {
  1122. (*schema) << new (ctx.mem) String_Constant(path, source_position, Token(i, p)); // accumulate the preceding segment if it's nonempty
  1123. }
  1124. const char* j = find_first_in_interval< exactly<rbrace> >(p, chunk.end); // find the closing brace
  1125. if (j) {
  1126. // parse the interpolant and accumulate it
  1127. Expression* interp_node = Parser::from_token(Token(p+2, j), ctx, path, source_position).parse_list();
  1128. interp_node->is_interpolant(true);
  1129. (*schema) << interp_node;
  1130. i = j+1;
  1131. }
  1132. else {
  1133. // throw an error if the interpolant is unterminated
  1134. error("unterminated interpolant inside string constant " + chunk.to_string());
  1135. }
  1136. }
  1137. else { // no interpolants left; add the last segment if nonempty
  1138. if (i < chunk.end) (*schema) << new (ctx.mem) String_Constant(path, source_position, Token(i, chunk.end));
  1139. break;
  1140. }
  1141. }
  1142. return schema;
  1143. }
  1144. String_Constant* Parser::parse_static_value()
  1145. {
  1146. lex< static_value >();
  1147. Token str(lexed);
  1148. --str.end;
  1149. --position;
  1150. String_Constant* str_node = new (ctx.mem) String_Constant(path, source_position, str);
  1151. str_node->is_delayed(true);
  1152. return str_node;
  1153. }
  1154. String* Parser::parse_string()
  1155. {
  1156. lex< string_constant >();
  1157. Token str(lexed);
  1158. return parse_interpolated_chunk(str);
  1159. // const char* i = str.begin;
  1160. // // see if there any interpolants
  1161. // const char* p = find_first_in_interval< sequence< negate< exactly<'\\'> >, exactly<hash_lbrace> > >(str.begin, str.end);
  1162. // if (!p) {
  1163. // String_Constant* str_node = new (ctx.mem) String_Constant(path, source_position, str);
  1164. // str_node->is_delayed(true);
  1165. // return str_node;
  1166. // }
  1167. // String_Schema* schema = new (ctx.mem) String_Schema(path, source_position);
  1168. // schema->quote_mark(*str.begin);
  1169. // while (i < str.end) {
  1170. // p = find_first_in_interval< sequence< negate< exactly<'\\'> >, exactly<hash_lbrace> > >(i, str.end);
  1171. // if (p) {
  1172. // if (i < p) {
  1173. // (*schema) << new (ctx.mem) String_Constant(path, source_position, Token(i, p)); // accumulate the preceding segment if it's nonempty
  1174. // }
  1175. // const char* j = find_first_in_interval< exactly<rbrace> >(p, str.end); // find the closing brace
  1176. // if (j) {
  1177. // // parse the interpolant and accumulate it
  1178. // Expression* interp_node = Parser::from_token(Token(p+2, j), ctx, path, source_position).parse_list();
  1179. // interp_node->is_interpolant(true);
  1180. // (*schema) << interp_node;
  1181. // i = j+1;
  1182. // }
  1183. // else {
  1184. // // throw an error if the interpolant is unterminated
  1185. // error("unterminated interpolant inside string constant " + str.to_string());
  1186. // }
  1187. // }
  1188. // else { // no interpolants left; add the last segment if nonempty
  1189. // if (i < str.end) (*schema) << new (ctx.mem) String_Constant(path, source_position, Token(i, str.end));
  1190. // break;
  1191. // }
  1192. // }
  1193. // return schema;
  1194. }
  1195. String* Parser::parse_ie_property()
  1196. {
  1197. lex< ie_property >();
  1198. Token str(lexed);
  1199. const char* i = str.begin;
  1200. // see if there any interpolants
  1201. const char* p = find_first_in_interval< sequence< negate< exactly<'\\'> >, exactly<hash_lbrace> > >(str.begin, str.end);
  1202. if (!p) {
  1203. String_Constant* str_node = new (ctx.mem) String_Constant(path, source_position, str);
  1204. str_node->is_delayed(true);
  1205. return str_node;
  1206. }
  1207. String_Schema* schema = new (ctx.mem) String_Schema(path, source_position);
  1208. while (i < str.end) {
  1209. p = find_first_in_interval< sequence< negate< exactly<'\\'> >, exactly<hash_lbrace> > >(i, str.end);
  1210. if (p) {
  1211. if (i < p) {
  1212. (*schema) << new (ctx.mem) String_Constant(path, source_position, Token(i, p)); // accumulate the preceding segment if it's nonempty
  1213. }
  1214. const char* j = find_first_in_interval< exactly<rbrace> >(p, str.end); // find the closing brace
  1215. if (j) {
  1216. // parse the interpolant and accumulate it
  1217. Expression* interp_node = Parser::from_token(Token(p+2, j), ctx, path, source_position).parse_list();
  1218. interp_node->is_interpolant(true);
  1219. (*schema) << interp_node;
  1220. i = j+1;
  1221. }
  1222. else {
  1223. // throw an error if the interpolant is unterminated
  1224. error("unterminated interpolant inside IE function " + str.to_string());
  1225. }
  1226. }
  1227. else { // no interpolants left; add the last segment if nonempty
  1228. if (i < str.end) (*schema) << new (ctx.mem) String_Constant(path, source_position, Token(i, str.end));
  1229. break;
  1230. }
  1231. }
  1232. return schema;
  1233. }
  1234. String* Parser::parse_ie_keyword_arg()
  1235. {
  1236. String_Schema* kwd_arg = new (ctx.mem) String_Schema(path, source_position, 3);
  1237. if (lex< variable >()) *kwd_arg << new (ctx.mem) Variable(path, source_position, Util::normalize_underscores(lexed));
  1238. else {
  1239. lex< alternatives< identifier_schema, identifier > >();
  1240. *kwd_arg << new (ctx.mem) String_Constant(path, source_position, lexed);
  1241. }
  1242. lex< exactly<'='> >();
  1243. *kwd_arg << new (ctx.mem) String_Constant(path, source_position, lexed);
  1244. if (peek< variable >()) *kwd_arg << parse_list();
  1245. else if (lex< number >()) *kwd_arg << new (ctx.mem) Textual(path, source_position, Textual::NUMBER, Util::normalize_decimals(lexed));
  1246. else {
  1247. lex< alternatives< identifier_schema, identifier, number, hex > >();
  1248. *kwd_arg << new (ctx.mem) String_Constant(path, source_position, lexed);
  1249. }
  1250. return kwd_arg;
  1251. }
  1252. String_Schema* Parser::parse_value_schema()
  1253. {
  1254. String_Schema* schema = new (ctx.mem) String_Schema(path, source_position);
  1255. size_t num_items = 0;
  1256. while (position < end) {
  1257. if (lex< interpolant >()) {
  1258. Token insides(Token(lexed.begin + 2, lexed.end - 1));
  1259. Expression* interp_node = Parser::from_token(insides, ctx, path, source_position).parse_list();
  1260. interp_node->is_interpolant(true);
  1261. (*schema) << interp_node;
  1262. }
  1263. else if (lex< exactly<'%'> >()) {
  1264. (*schema) << new (ctx.mem) String_Constant(path, source_position, lexed);
  1265. }
  1266. else if (lex< identifier >()) {
  1267. (*schema) << new (ctx.mem) String_Constant(path, source_position, lexed);
  1268. }
  1269. else if (lex< percentage >()) {
  1270. (*schema) << new (ctx.mem) Textual(path, source_position, Textual::PERCENTAGE, lexed);
  1271. }
  1272. else if (lex< dimension >()) {
  1273. (*schema) << new (ctx.mem) Textual(path, source_position, Textual::DIMENSION, lexed);
  1274. }
  1275. else if (lex< number >()) {
  1276. (*schema) << new (ctx.mem) Textual(path, source_position, Textual::NUMBER, lexed);
  1277. }
  1278. else if (lex< hex >()) {
  1279. (*schema) << new (ctx.mem) Textual(path, source_position, Textual::HEX, lexed);
  1280. }
  1281. else if (lex< string_constant >()) {
  1282. (*schema) << new (ctx.mem) String_Constant(path, source_position, lexed);
  1283. if (!num_items) schema->quote_mark(*lexed.begin);
  1284. }
  1285. else if (lex< variable >()) {
  1286. (*schema) << new (ctx.mem) Variable(path, source_position, Util::normalize_underscores(lexed));
  1287. }
  1288. else {
  1289. error("error parsing interpolated value");
  1290. }
  1291. ++num_items;
  1292. }
  1293. return schema;
  1294. }
  1295. String_Schema* Parser::parse_url_schema()
  1296. {
  1297. String_Schema* schema = new (ctx.mem) String_Schema(path, source_position);
  1298. while (position < end) {
  1299. if (position[0] == '/') {
  1300. lexed = Token(position, position+1);
  1301. (*schema) << new (ctx.mem) String_Constant(path, source_position, lexed);
  1302. ++position;
  1303. }
  1304. else if (lex< interpolant >()) {
  1305. Token insides(Token(lexed.begin + 2, lexed.end - 1));
  1306. Expression* interp_node = Parser::from_token(insides, ctx, path, source_position).parse_list();
  1307. interp_node->is_interpolant(true);
  1308. (*schema) << interp_node;
  1309. }
  1310. else if (lex< sequence< identifier, exactly<':'> > >()) {
  1311. (*schema) << new (ctx.mem) String_Constant(path, source_position, lexed);
  1312. }
  1313. else if (lex< filename >()) {
  1314. (*schema) << new (ctx.mem) String_Constant(path, source_position, lexed);
  1315. }
  1316. else {
  1317. error("error parsing interpolated url");
  1318. }
  1319. }
  1320. return schema;
  1321. }
  1322. String* Parser::parse_identifier_schema()
  1323. {
  1324. lex< sequence< optional< exactly<'*'> >, identifier_schema > >();
  1325. Token id(lexed);
  1326. const char* i = id.begin;
  1327. // see if there any interpolants
  1328. const char* p = find_first_in_interval< sequence< negate< exactly<'\\'> >, exactly<hash_lbrace> > >(id.begin, id.end);
  1329. if (!p) {
  1330. return new (ctx.mem) String_Constant(path, source_position, id);
  1331. }
  1332. String_Schema* schema = new (ctx.mem) String_Schema(path, source_position);
  1333. while (i < id.end) {
  1334. p = find_first_in_interval< sequence< negate< exactly<'\\'> >, exactly<hash_lbrace> > >(i, id.end);
  1335. if (p) {
  1336. if (i < p) {
  1337. (*schema) << new (ctx.mem) String_Constant(path, source_position, Token(i, p)); // accumulate the preceding segment if it's nonempty
  1338. }
  1339. const char* j = find_first_in_interval< exactly<rbrace> >(p, id.end); // find the closing brace
  1340. if (j) {
  1341. // parse the interpolant and accumulate it
  1342. Expression* interp_node = Parser::from_token(Token(p+2, j), ctx, path, source_position).parse_list();
  1343. interp_node->is_interpolant(true);
  1344. (*schema) << interp_node;
  1345. schema->has_interpolants(true);
  1346. i = j+1;
  1347. }
  1348. else {
  1349. // throw an error if the interpolant is unterminated
  1350. error("unterminated interpolant inside interpolated identifier " + id.to_string());
  1351. }
  1352. }
  1353. else { // no interpolants left; add the last segment if nonempty
  1354. if (i < id.end) (*schema) << new (ctx.mem) String_Constant(path, source_position, Token(i, id.end));
  1355. break;
  1356. }
  1357. }
  1358. return schema;
  1359. }
  1360. Function_Call* Parser::parse_calc_function()
  1361. {
  1362. lex< identifier >();
  1363. string name(lexed);
  1364. Position call_pos = source_position;
  1365. lex< exactly<'('> >();
  1366. Position arg_pos = source_position;
  1367. const char* arg_beg = position;
  1368. parse_list();
  1369. const char* arg_end = position;
  1370. lex< exactly<')'> >();
  1371. Argument* arg = new (ctx.mem) Argument(path, arg_pos, parse_interpolated_chunk(Token(arg_beg, arg_end)));
  1372. Arguments* args = new (ctx.mem) Arguments(path, arg_pos);
  1373. *args << arg;
  1374. return new (ctx.mem) Function_Call(path, call_pos, name, args);
  1375. }
  1376. Function_Call* Parser::parse_function_call()
  1377. {
  1378. lex< identifier >();
  1379. string name(Util::normalize_underscores(lexed));
  1380. Position source_position_of_call = source_position;
  1381. Function_Call* the_call = new (ctx.mem) Function_Call(path, source_position_of_call, name, parse_arguments());
  1382. return the_call;
  1383. }
  1384. Function_Call_Schema* Parser::parse_function_call_schema()
  1385. {
  1386. String* name = parse_identifier_schema();
  1387. Position source_position_of_call = source_position;
  1388. Function_Call_Schema* the_call = new (ctx.mem) Function_Call_Schema(path, source_position_of_call, name, parse_arguments());
  1389. return the_call;
  1390. }
  1391. If* Parser::parse_if_directive(bool else_if)
  1392. {
  1393. lex< if_directive >() || (else_if && lex< exactly<if_after_else_kwd> >());
  1394. Position if_source_position = source_position;
  1395. Expression* predicate = parse_list();
  1396. predicate->is_delayed(false);
  1397. if (!peek< exactly<'{'> >()) error("expected '{' after the predicate for @if");
  1398. Block* consequent = parse_block();
  1399. Block* alternative = 0;
  1400. if (lex< else_directive >()) {
  1401. if (peek< exactly<if_after_else_kwd> >()) {
  1402. alternative = new (ctx.mem) Block(path, source_position);
  1403. (*alternative) << parse_if_directive(true);
  1404. }
  1405. else if (!peek< exactly<'{'> >()) {
  1406. error("expected '{' after @else");
  1407. }
  1408. else {
  1409. alternative = parse_block();
  1410. }
  1411. }
  1412. return new (ctx.mem) If(path, if_source_position, predicate, consequent, alternative);
  1413. }
  1414. For* Parser::parse_for_directive()
  1415. {
  1416. lex< for_directive >();
  1417. Position for_source_position = source_position;
  1418. if (!lex< variable >()) error("@for directive requires an iteration variable");
  1419. string var(Util::normalize_underscores(lexed));
  1420. if (!lex< from >()) error("expected 'from' keyword in @for directive");
  1421. Expression* lower_bound = parse_expression();
  1422. lower_bound->is_delayed(false);
  1423. bool inclusive = false;
  1424. if (lex< through >()) inclusive = true;
  1425. else if (lex< to >()) inclusive = false;
  1426. else error("expected 'through' or 'to' keyword in @for directive");
  1427. Expression* upper_bound = parse_expression();
  1428. upper_bound->is_delayed(false);
  1429. if (!peek< exactly<'{'> >()) error("expected '{' after the upper bound in @for directive");
  1430. Block* body = parse_block();
  1431. return new (ctx.mem) For(path, for_source_position, var, lower_bound, upper_bound, body, inclusive);
  1432. }
  1433. Each* Parser::parse_each_directive()
  1434. {
  1435. lex < each_directive >();
  1436. Position each_source_position = source_position;
  1437. if (!lex< variable >()) error("@each directive requires an iteration variable");
  1438. vector<string> vars;
  1439. vars.push_back(Util::normalize_underscores(lexed));
  1440. while (peek< exactly<','> >() && lex< exactly<','> >()) {
  1441. if (!lex< variable >()) error("@each directive requires an iteration variable");
  1442. vars.push_back(Util::normalize_underscores(lexed));
  1443. }
  1444. if (!lex< in >()) error("expected 'in' keyword in @each directive");
  1445. Expression* list = parse_list();
  1446. list->is_delayed(false);
  1447. if (list->concrete_type() == Expression::LIST) {
  1448. List* l = static_cast<List*>(list);
  1449. for (size_t i = 0, L = l->length(); i < L; ++i) {
  1450. (*l)[i]->is_delayed(false);
  1451. }
  1452. }
  1453. if (!peek< exactly<'{'> >()) error("expected '{' after the upper bound in @each directive");
  1454. Block* body = parse_block();
  1455. return new (ctx.mem) Each(path, each_source_position, vars, list, body);
  1456. }
  1457. While* Parser::parse_while_directive()
  1458. {
  1459. lex< while_directive >();
  1460. Position while_source_position = source_position;
  1461. Expression* predicate = parse_list();
  1462. predicate->is_delayed(false);
  1463. Block* body = parse_block();
  1464. return new (ctx.mem) While(path, while_source_position, predicate, body);
  1465. }
  1466. Media_Block* Parser::parse_media_block()
  1467. {
  1468. lex< media >();
  1469. Position media_source_position = source_position;
  1470. List* media_queries = parse_media_queries();
  1471. if (!peek< exactly<'{'> >()) {
  1472. error("expected '{' in media query");
  1473. }
  1474. Block* block = parse_block();
  1475. return new (ctx.mem) Media_Block(path, media_source_position, media_queries, block);
  1476. }
  1477. List* Parser::parse_media_queries()
  1478. {
  1479. List* media_queries = new (ctx.mem) List(path, source_position, 0, List::COMMA);
  1480. if (!peek< exactly<'{'> >()) (*media_queries) << parse_media_query();
  1481. while (lex< exactly<','> >()) (*media_queries) << parse_media_query();
  1482. return media_queries;
  1483. }
  1484. // Expression* Parser::parse_media_query()
  1485. Media_Query* Parser::parse_media_query()
  1486. {
  1487. Media_Query* media_query = new (ctx.mem) Media_Query(path, source_position);
  1488. if (lex< exactly< not_kwd > >()) media_query->is_negated(true);
  1489. else if (lex< exactly< only_kwd > >()) media_query->is_restricted(true);
  1490. if (peek< identifier_schema >()) media_query->media_type(parse_identifier_schema());
  1491. else if (lex< identifier >()) media_query->media_type(new (ctx.mem) String_Constant(path, source_position, lexed));
  1492. else (*media_query) << parse_media_expression();
  1493. while (lex< exactly< and_kwd > >()) (*media_query) << parse_media_expression();
  1494. return media_query;
  1495. }
  1496. Media_Query_Expression* Parser::parse_media_expression()
  1497. {
  1498. if (peek< identifier_schema >()) {
  1499. String* ss = parse_identifier_schema();
  1500. return new (ctx.mem) Media_Query_Expression(path, source_position, ss, 0, true);
  1501. }
  1502. if (!lex< exactly<'('> >()) {
  1503. error("media query expression must begin with '('");
  1504. }
  1505. Expression* feature = 0;
  1506. if (peek< exactly<')'> >()) {
  1507. error("media feature required in media query expression");
  1508. }
  1509. feature = parse_expression();
  1510. Expression* expression = 0;
  1511. if (lex< exactly<':'> >()) {
  1512. expression = parse_list();
  1513. }
  1514. if (!lex< exactly<')'> >()) {
  1515. error("unclosed parenthesis in media query expression");
  1516. }
  1517. return new (ctx.mem) Media_Query_Expression(path, feature->position(), feature, expression);
  1518. }
  1519. Feature_Block* Parser::parse_feature_block()
  1520. {
  1521. lex< supports >();
  1522. Position supports_source_position = source_position;
  1523. Feature_Query* feature_queries = parse_feature_queries();
  1524. if (!peek< exactly<'{'> >()) {
  1525. error("expected '{' in feature query");
  1526. }
  1527. Block* block = parse_block();
  1528. return new (ctx.mem) Feature_Block(path, supports_source_position, feature_queries, block);
  1529. }
  1530. Feature_Query* Parser::parse_feature_queries()
  1531. {
  1532. Feature_Query* fq = new (ctx.mem) Feature_Query(path, source_position);
  1533. Feature_Query_Condition* cond = new (ctx.mem) Feature_Query_Condition(path, source_position);
  1534. cond->is_root(true);
  1535. while (!peek< exactly<')'> >(position) && !peek< exactly<'{'> >(position))
  1536. (*cond) << parse_feature_query();
  1537. (*fq) << cond;
  1538. if (fq->empty()) error("expected @supports condition (e.g. (display: flexbox))");
  1539. return fq;
  1540. }
  1541. Feature_Query_Condition* Parser::parse_feature_query()
  1542. {
  1543. if (peek< not_op >(position)) return parse_supports_negation();
  1544. else if (peek< and_op >(position)) return parse_supports_conjunction();
  1545. else if (peek< or_op >(position)) return parse_supports_disjunction();
  1546. else if (peek< exactly<'('> >(position)) return parse_feature_query_in_parens();
  1547. else return parse_supports_declaration();
  1548. }
  1549. Feature_Query_Condition* Parser::parse_feature_query_in_parens()
  1550. {
  1551. Feature_Query_Condition* cond = new (ctx.mem) Feature_Query_Condition(path, source_position);
  1552. if (!lex< exactly<'('> >()) error("@supports declaration expected '('");
  1553. while (!peek< exactly<')'> >(position) && !peek< exactly<'{'> >(position))
  1554. (*cond) << parse_feature_query();
  1555. if (!lex< exactly<')'> >()) error("unclosed parenthesis in @supports declaration");
  1556. return (cond->length() == 1) ? (*cond)[0] : cond;
  1557. }
  1558. Feature_Query_Condition* Parser::parse_supports_negation()
  1559. {
  1560. lex< not_op >();
  1561. Feature_Query_Condition* cond = parse_feature_query();
  1562. cond->operand(Feature_Query_Condition::NOT);
  1563. return cond;
  1564. }
  1565. Feature_Query_Condition* Parser::parse_supports_conjunction()
  1566. {
  1567. lex< and_op >();
  1568. Feature_Query_Condition* cond = parse_feature_query();
  1569. cond->operand(Feature_Query_Condition::AND);
  1570. return cond;
  1571. }
  1572. Feature_Query_Condition* Parser::parse_supports_disjunction()
  1573. {
  1574. lex< or_op >();
  1575. Feature_Query_Condition* cond = parse_feature_query();
  1576. cond->operand(Feature_Query_Condition::OR);
  1577. return cond;
  1578. }
  1579. Feature_Query_Condition* Parser::parse_supports_declaration()
  1580. {
  1581. Declaration* declaration = parse_declaration();
  1582. Feature_Query_Condition* cond = new (ctx.mem) Feature_Query_Condition(declaration->path(),
  1583. declaration->position(),
  1584. 1,
  1585. declaration->property(),
  1586. declaration->value());
  1587. return cond;
  1588. }
  1589. At_Rule* Parser::parse_at_rule()
  1590. {
  1591. lex<at_keyword>();
  1592. string kwd(lexed);
  1593. Position at_source_position = source_position;
  1594. Selector* sel = 0;
  1595. Expression* val = 0;
  1596. Selector_Lookahead lookahead = lookahead_for_extension_target(position);
  1597. if (lookahead.found) {
  1598. if (lookahead.has_interpolants) {
  1599. sel = parse_selector_schema(lookahead.found);
  1600. }
  1601. else {
  1602. sel = parse_selector_group();
  1603. }
  1604. }
  1605. else if (!(peek<exactly<'{'> >() || peek<exactly<'}'> >() || peek<exactly<';'> >())) {
  1606. val = parse_list();
  1607. }
  1608. Block* body = 0;
  1609. if (peek< exactly<'{'> >()) body = parse_block();
  1610. At_Rule* rule = new (ctx.mem) At_Rule(path, at_source_position, kwd, sel, body);
  1611. if (!sel) rule->value(val);
  1612. return rule;
  1613. }
  1614. Warning* Parser::parse_warning()
  1615. {
  1616. lex< warn >();
  1617. return new (ctx.mem) Warning(path, source_position, parse_list());
  1618. }
  1619. Error* Parser::parse_error()
  1620. {
  1621. lex< err >();
  1622. return new (ctx.mem) Error(path, source_position, parse_list());
  1623. }
  1624. Debug* Parser::parse_debug()
  1625. {
  1626. lex< dbg >();
  1627. return new (ctx.mem) Debug(path, source_position, parse_list());
  1628. }
  1629. Selector_Lookahead Parser::lookahead_for_selector(const char* start)
  1630. {
  1631. const char* p = start ? start : position;
  1632. const char* q;
  1633. bool saw_stuff = false;
  1634. bool saw_interpolant = false;
  1635. while ((q = peek< identifier >(p)) ||
  1636. (q = peek< hyphens_and_identifier >(p)) ||
  1637. (q = peek< hyphens_and_name >(p)) ||
  1638. (q = peek< type_selector >(p)) ||
  1639. (q = peek< id_name >(p)) ||
  1640. (q = peek< class_name >(p)) ||
  1641. (q = peek< sequence< pseudo_prefix, identifier > >(p)) ||
  1642. (q = peek< percentage >(p)) ||
  1643. (q = peek< dimension >(p)) ||
  1644. (q = peek< string_constant >(p)) ||
  1645. (q = peek< exactly<'*'> >(p)) ||
  1646. (q = peek< exactly<'('> >(p)) ||
  1647. (q = peek< exactly<')'> >(p)) ||
  1648. (q = peek< exactly<'['> >(p)) ||
  1649. (q = peek< exactly<']'> >(p)) ||
  1650. (q = peek< exactly<'+'> >(p)) ||
  1651. (q = peek< exactly<'~'> >(p)) ||
  1652. (q = peek< exactly<'>'> >(p)) ||
  1653. (q = peek< exactly<','> >(p)) ||
  1654. (q = peek< binomial >(p)) ||
  1655. (q = peek< sequence< optional<sign>,
  1656. optional<digits>,
  1657. exactly<'n'> > >(p)) ||
  1658. (q = peek< sequence< optional<sign>,
  1659. digits > >(p)) ||
  1660. (q = peek< number >(p)) ||
  1661. (q = peek< sequence< exactly<'&'>,
  1662. identifier_fragment > >(p)) ||
  1663. (q = peek< exactly<'&'> >(p)) ||
  1664. (q = peek< exactly<'%'> >(p)) ||
  1665. (q = peek< alternatives<exact_match,
  1666. class_match,
  1667. dash_match,
  1668. prefix_match,
  1669. suffix_match,
  1670. substring_match> >(p)) ||
  1671. (q = peek< sequence< exactly<'.'>, interpolant > >(p)) ||
  1672. (q = peek< sequence< exactly<'#'>, interpolant > >(p)) ||
  1673. (q = peek< sequence< one_plus< exactly<'-'> >, interpolant > >(p)) ||
  1674. (q = peek< sequence< pseudo_prefix, interpolant > >(p)) ||
  1675. (q = peek< interpolant >(p))) {
  1676. saw_stuff = true;
  1677. p = q;
  1678. if (*(p - 1) == '}') saw_interpolant = true;
  1679. }
  1680. Selector_Lookahead result;
  1681. result.found = saw_stuff && peek< exactly<'{'> >(p) ? p : 0;
  1682. result.has_interpolants = saw_interpolant;
  1683. return result;
  1684. }
  1685. Selector_Lookahead Parser::lookahead_for_extension_target(const char* start)
  1686. {
  1687. const char* p = start ? start : position;
  1688. const char* q;
  1689. bool saw_interpolant = false;
  1690. bool saw_stuff = false;
  1691. while ((q = peek< identifier >(p)) ||
  1692. (q = peek< type_selector >(p)) ||
  1693. (q = peek< id_name >(p)) ||
  1694. (q = peek< class_name >(p)) ||
  1695. (q = peek< sequence< pseudo_prefix, identifier > >(p)) ||
  1696. (q = peek< percentage >(p)) ||
  1697. (q = peek< dimension >(p)) ||
  1698. (q = peek< string_constant >(p)) ||
  1699. (q = peek< exactly<'*'> >(p)) ||
  1700. (q = peek< exactly<'('> >(p)) ||
  1701. (q = peek< exactly<')'> >(p)) ||
  1702. (q = peek< exactly<'['> >(p)) ||
  1703. (q = peek< exactly<']'> >(p)) ||
  1704. (q = peek< exactly<'+'> >(p)) ||
  1705. (q = peek< exactly<'~'> >(p)) ||
  1706. (q = peek< exactly<'>'> >(p)) ||
  1707. (q = peek< exactly<','> >(p)) ||
  1708. (q = peek< binomial >(p)) ||
  1709. (q = peek< sequence< optional<sign>,
  1710. optional<digits>,
  1711. exactly<'n'> > >(p)) ||
  1712. (q = peek< sequence< optional<sign>,
  1713. digits > >(p)) ||
  1714. (q = peek< number >(p)) ||
  1715. (q = peek< sequence< exactly<'&'>,
  1716. identifier_fragment > >(p)) ||
  1717. (q = peek< exactly<'&'> >(p)) ||
  1718. (q = peek< exactly<'%'> >(p)) ||
  1719. (q = peek< alternatives<exact_match,
  1720. class_match,
  1721. dash_match,
  1722. prefix_match,
  1723. suffix_match,
  1724. substring_match> >(p)) ||
  1725. (q = peek< sequence< exactly<'.'>, interpolant > >(p)) ||
  1726. (q = peek< sequence< exactly<'#'>, interpolant > >(p)) ||
  1727. (q = peek< sequence< one_plus< exactly<'-'> >, interpolant > >(p)) ||
  1728. (q = peek< sequence< pseudo_prefix, interpolant > >(p)) ||
  1729. (q = peek< interpolant >(p)) ||
  1730. (q = peek< optional >(p))) {
  1731. p = q;
  1732. if (*(p - 1) == '}') saw_interpolant = true;
  1733. saw_stuff = true;
  1734. }
  1735. Selector_Lookahead result;
  1736. result.found = peek< alternatives< exactly<';'>, exactly<'}'>, exactly<'{'> > >(p) && saw_stuff ? p : 0;
  1737. result.has_interpolants = saw_interpolant;
  1738. return result;
  1739. }
  1740. void Parser::read_bom()
  1741. {
  1742. size_t skip = 0;
  1743. string encoding;
  1744. bool utf_8 = false;
  1745. switch ((unsigned char) source[0]) {
  1746. case 0xEF:
  1747. skip = check_bom_chars(source, end, utf_8_bom, 3);
  1748. encoding = "UTF-8";
  1749. utf_8 = true;
  1750. break;
  1751. case 0xFE:
  1752. skip = check_bom_chars(source, end, utf_16_bom_be, 2);
  1753. encoding = "UTF-16 (big endian)";
  1754. break;
  1755. case 0xFF:
  1756. skip = check_bom_chars(source, end, utf_16_bom_le, 2);
  1757. skip += (skip ? check_bom_chars(source, end, utf_32_bom_le, 4) : 0);
  1758. encoding = (skip == 2 ? "UTF-16 (little endian)" : "UTF-32 (little endian)");
  1759. break;
  1760. case 0x00:
  1761. skip = check_bom_chars(source, end, utf_32_bom_be, 4);
  1762. encoding = "UTF-32 (big endian)";
  1763. break;
  1764. case 0x2B:
  1765. skip = check_bom_chars(source, end, utf_7_bom_1, 4)
  1766. | check_bom_chars(source, end, utf_7_bom_2, 4)
  1767. | check_bom_chars(source, end, utf_7_bom_3, 4)
  1768. | check_bom_chars(source, end, utf_7_bom_4, 4)
  1769. | check_bom_chars(source, end, utf_7_bom_5, 5);
  1770. encoding = "UTF-7";
  1771. break;
  1772. case 0xF7:
  1773. skip = check_bom_chars(source, end, utf_1_bom, 3);
  1774. encoding = "UTF-1";
  1775. break;
  1776. case 0xDD:
  1777. skip = check_bom_chars(source, end, utf_ebcdic_bom, 4);
  1778. encoding = "UTF-EBCDIC";
  1779. break;
  1780. case 0x0E:
  1781. skip = check_bom_chars(source, end, scsu_bom, 3);
  1782. encoding = "SCSU";
  1783. break;
  1784. case 0xFB:
  1785. skip = check_bom_chars(source, end, bocu_1_bom, 3);
  1786. encoding = "BOCU-1";
  1787. break;
  1788. case 0x84:
  1789. skip = check_bom_chars(source, end, gb_18030_bom, 4);
  1790. encoding = "GB-18030";
  1791. break;
  1792. }
  1793. if (skip > 0 && !utf_8) error("only UTF-8 documents are currently supported; your document appears to be " + encoding);
  1794. position += skip;
  1795. }
  1796. size_t check_bom_chars(const char* src, const char *end, const unsigned char* bom, size_t len)
  1797. {
  1798. size_t skip = 0;
  1799. if (src + len > end) return 0;
  1800. for (size_t i = 0; i < len; ++i, ++skip) {
  1801. if ((unsigned char) src[i] != bom[i]) return 0;
  1802. }
  1803. return skip;
  1804. }
  1805. Expression* Parser::fold_operands(Expression* base, vector<Expression*>& operands, Binary_Expression::Type op)
  1806. {
  1807. for (size_t i = 0, S = operands.size(); i < S; ++i) {
  1808. base = new (ctx.mem) Binary_Expression(path, source_position, op, base, operands[i]);
  1809. Binary_Expression* b = static_cast<Binary_Expression*>(base);
  1810. if (op == Binary_Expression::DIV && b->left()->is_delayed() && b->right()->is_delayed()) {
  1811. base->is_delayed(true);
  1812. }
  1813. else {
  1814. b->left()->is_delayed(false);
  1815. b->right()->is_delayed(false);
  1816. }
  1817. }
  1818. return base;
  1819. }
  1820. Expression* Parser::fold_operands(Expression* base, vector<Expression*>& operands, vector<Binary_Expression::Type>& ops)
  1821. {
  1822. for (size_t i = 0, S = operands.size(); i < S; ++i) {
  1823. base = new (ctx.mem) Binary_Expression(path, base->position(), ops[i], base, operands[i]);
  1824. Binary_Expression* b = static_cast<Binary_Expression*>(base);
  1825. if (ops[i] == Binary_Expression::DIV && b->left()->is_delayed() && b->right()->is_delayed()) {
  1826. base->is_delayed(true);
  1827. }
  1828. else {
  1829. b->left()->is_delayed(false);
  1830. b->right()->is_delayed(false);
  1831. }
  1832. }
  1833. return base;
  1834. }
  1835. void Parser::error(string msg, Position pos)
  1836. {
  1837. throw Sass_Error(Sass_Error::syntax, path, pos.line ? pos : source_position, msg);
  1838. }
  1839. }