Blame src/parser.cpp

Packit Service 7770af
#include "sass.hpp"
Packit Service 7770af
#include "parser.hpp"
Packit Service 7770af
#include "file.hpp"
Packit Service 7770af
#include "inspect.hpp"
Packit Service 7770af
#include "constants.hpp"
Packit Service 7770af
#include "util.hpp"
Packit Service 7770af
#include "prelexer.hpp"
Packit Service 7770af
#include "color_maps.hpp"
Packit Service 7770af
#include "sass/functions.h"
Packit Service 7770af
#include "error_handling.hpp"
Packit Service 7770af
Packit Service 7770af
// Notes about delayed: some ast nodes can have delayed evaluation so
Packit Service 7770af
// they can preserve their original semantics if needed. This is most
Packit Service 7770af
// prominently exhibited by the division operation, since it is not
Packit Service 7770af
// only a valid operation, but also a valid css statement (i.e. for
Packit Service 7770af
// fonts, as in `16px/24px`). When parsing lists and expression we
Packit Service 7770af
// unwrap single items from lists and other operations. A nested list
Packit Service 7770af
// must not be delayed, only the items of the first level sometimes
Packit Service 7770af
// are delayed (as with argument lists). To achieve this we need to
Packit Service 7770af
// pass status to the list parser, so this can be set correctly.
Packit Service 7770af
// Another case with delayed values are colors. In compressed mode
Packit Service 7770af
// only processed values get compressed (other are left as written).
Packit Service 7770af
Packit Service 7770af
#include <cstdlib>
Packit Service 7770af
#include <iostream>
Packit Service 7770af
#include <vector>
Packit Service 7770af
#include <typeinfo>
Packit Service 7770af
Packit Service 7770af
namespace Sass {
Packit Service 7770af
  using namespace Constants;
Packit Service 7770af
  using namespace Prelexer;
Packit Service 7770af
Packit Service 7770af
  Parser Parser::from_c_str(const char* beg, Context& ctx, ParserState pstate, const char* source)
Packit Service 7770af
  {
Packit Service 7770af
    pstate.offset.column = 0;
Packit Service 7770af
    pstate.offset.line = 0;
Packit Service 7770af
    Parser p(ctx, pstate);
Packit Service 7770af
    p.source   = source ? source : beg;
Packit Service 7770af
    p.position = beg ? beg : p.source;
Packit Service 7770af
    p.end      = p.position + strlen(p.position);
Packit Service 7770af
    Block_Obj root = SASS_MEMORY_NEW(Block, pstate);
Packit Service 7770af
    p.block_stack.push_back(root);
Packit Service 7770af
    root->is_root(true);
Packit Service 7770af
    return p;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Parser Parser::from_c_str(const char* beg, const char* end, Context& ctx, ParserState pstate, const char* source)
Packit Service 7770af
  {
Packit Service 7770af
    pstate.offset.column = 0;
Packit Service 7770af
    pstate.offset.line = 0;
Packit Service 7770af
    Parser p(ctx, pstate);
Packit Service 7770af
    p.source   = source ? source : beg;
Packit Service 7770af
    p.position = beg ? beg : p.source;
Packit Service 7770af
    p.end      = end ? end : p.position + strlen(p.position);
Packit Service 7770af
    Block_Obj root = SASS_MEMORY_NEW(Block, pstate);
Packit Service 7770af
    p.block_stack.push_back(root);
Packit Service 7770af
    root->is_root(true);
Packit Service 7770af
    return p;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
   void Parser::advanceToNextToken() {
Packit Service 7770af
      lex < css_comments >(false);
Packit Service 7770af
      // advance to position
Packit Service 7770af
      pstate += pstate.offset;
Packit Service 7770af
      pstate.offset.column = 0;
Packit Service 7770af
      pstate.offset.line = 0;
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
  Selector_List_Obj Parser::parse_selector(const char* beg, Context& ctx, ParserState pstate, const char* source)
Packit Service 7770af
  {
Packit Service 7770af
    Parser p = Parser::from_c_str(beg, ctx, pstate, source);
Packit Service 7770af
    // ToDo: ruby sass errors on parent references
Packit Service 7770af
    // ToDo: remap the source-map entries somehow
Packit Service 7770af
    return p.parse_selector_list(false);
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Parser::peek_newline(const char* start)
Packit Service 7770af
  {
Packit Service 7770af
    return peek_linefeed(start ? start : position)
Packit Service 7770af
           && ! peek_css<exactly<'{'>>(start);
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Parser Parser::from_token(Token t, Context& ctx, ParserState pstate, const char* source)
Packit Service 7770af
  {
Packit Service 7770af
    Parser p(ctx, pstate);
Packit Service 7770af
    p.source   = source ? source : t.begin;
Packit Service 7770af
    p.position = t.begin ? t.begin : p.source;
Packit Service 7770af
    p.end      = t.end ? t.end : p.position + strlen(p.position);
Packit Service 7770af
    Block_Obj root = SASS_MEMORY_NEW(Block, pstate);
Packit Service 7770af
    p.block_stack.push_back(root);
Packit Service 7770af
    root->is_root(true);
Packit Service 7770af
    return p;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  /* main entry point to parse root block */
Packit Service 7770af
  Block_Obj Parser::parse()
Packit Service 7770af
  {
Packit Service 7770af
Packit Service 7770af
    // consume unicode BOM
Packit Service 7770af
    read_bom();
Packit Service 7770af
Packit Service 7770af
    // create a block AST node to hold children
Packit Service 7770af
    Block_Obj root = SASS_MEMORY_NEW(Block, pstate, 0, true);
Packit Service 7770af
Packit Service 7770af
    // check seems a bit esoteric but works
Packit Service 7770af
    if (ctx.resources.size() == 1) {
Packit Service 7770af
      // apply headers only on very first include
Packit Service 7770af
      ctx.apply_custom_headers(root, path, pstate);
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    // parse children nodes
Packit Service 7770af
    block_stack.push_back(root);
Packit Service 7770af
    parse_block_nodes(true);
Packit Service 7770af
    block_stack.pop_back();
Packit Service 7770af
Packit Service 7770af
    // update final position
Packit Service 7770af
    root->update_pstate(pstate);
Packit Service 7770af
Packit Service 7770af
    if (position != end) {
Packit Service 7770af
      css_error("Invalid CSS", " after ", ": expected selector or at-rule, was ");
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    return root;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
Packit Service 7770af
  // convenience function for block parsing
Packit Service 7770af
  // will create a new block ad-hoc for you
Packit Service 7770af
  // this is the base block parsing function
Packit Service 7770af
  Block_Obj Parser::parse_css_block(bool is_root)
Packit Service 7770af
  {
Packit Service 7770af
Packit Service 7770af
    // parse comments before block
Packit Service 7770af
    // lex < optional_css_comments >();
Packit Service 7770af
Packit Service 7770af
    // lex mandatory opener or error out
Packit Service 7770af
    if (!lex_css < exactly<'{'> >()) {
Packit Service 7770af
      css_error("Invalid CSS", " after ", ": expected \"{\", was ");
Packit Service 7770af
    }
Packit Service 7770af
    // create new block and push to the selector stack
Packit Service 7770af
    Block_Obj block = SASS_MEMORY_NEW(Block, pstate, 0, is_root);
Packit Service 7770af
    block_stack.push_back(block);
Packit Service 7770af
Packit Service 7770af
    if (!parse_block_nodes(is_root)) css_error("Invalid CSS", " after ", ": expected \"}\", was ");
Packit Service 7770af
Packit Service 7770af
    if (!lex_css < exactly<'}'> >()) {
Packit Service 7770af
      css_error("Invalid CSS", " after ", ": expected \"}\", was ");
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    // update for end position
Packit Service 7770af
    // this seems to be done somewhere else
Packit Service 7770af
    // but that fixed selector schema issue
Packit Service 7770af
    // block->update_pstate(pstate);
Packit Service 7770af
Packit Service 7770af
    // parse comments after block
Packit Service 7770af
    // lex < optional_css_comments >();
Packit Service 7770af
Packit Service 7770af
    block_stack.pop_back();
Packit Service 7770af
Packit Service 7770af
    return block;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  // convenience function for block parsing
Packit Service 7770af
  // will create a new block ad-hoc for you
Packit Service 7770af
  // also updates the `in_at_root` flag
Packit Service 7770af
  Block_Obj Parser::parse_block(bool is_root)
Packit Service 7770af
  {
Packit Service 7770af
    return parse_css_block(is_root);
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  // the main block parsing function
Packit Service 7770af
  // parses stuff between `{` and `}`
Packit Service 7770af
  bool Parser::parse_block_nodes(bool is_root)
Packit Service 7770af
  {
Packit Service 7770af
Packit Service 7770af
    // loop until end of string
Packit Service 7770af
    while (position < end) {
Packit Service 7770af
Packit Service 7770af
      // we should be able to refactor this
Packit Service 7770af
      parse_block_comments();
Packit Service 7770af
      lex < css_whitespace >();
Packit Service 7770af
Packit Service 7770af
      if (lex < exactly<';'> >()) continue;
Packit Service 7770af
      if (peek < end_of_file >()) return true;
Packit Service 7770af
      if (peek < exactly<'}'> >()) return true;
Packit Service 7770af
Packit Service 7770af
      if (parse_block_node(is_root)) continue;
Packit Service 7770af
Packit Service 7770af
      parse_block_comments();
Packit Service 7770af
Packit Service 7770af
      if (lex_css < exactly<';'> >()) continue;
Packit Service 7770af
      if (peek_css < end_of_file >()) return true;
Packit Service 7770af
      if (peek_css < exactly<'}'> >()) return true;
Packit Service 7770af
Packit Service 7770af
      // illegal sass
Packit Service 7770af
      return false;
Packit Service 7770af
    }
Packit Service 7770af
    // return success
Packit Service 7770af
    return true;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  // parser for a single node in a block
Packit Service 7770af
  // semicolons must be lexed beforehand
Packit Service 7770af
  bool Parser::parse_block_node(bool is_root) {
Packit Service 7770af
Packit Service 7770af
    Block_Obj block = block_stack.back();
Packit Service 7770af
Packit Service 7770af
    parse_block_comments();
Packit Service 7770af
Packit Service 7770af
    // throw away white-space
Packit Service 7770af
    // includes line comments
Packit Service 7770af
    lex < css_whitespace >();
Packit Service 7770af
Packit Service 7770af
    Lookahead lookahead_result;
Packit Service 7770af
Packit Service 7770af
    // also parse block comments
Packit Service 7770af
Packit Service 7770af
    // first parse everything that is allowed in functions
Packit Service 7770af
    if (lex < variable >(true)) { block->append(parse_assignment()); }
Packit Service 7770af
    else if (lex < kwd_err >(true)) { block->append(parse_error()); }
Packit Service 7770af
    else if (lex < kwd_dbg >(true)) { block->append(parse_debug()); }
Packit Service 7770af
    else if (lex < kwd_warn >(true)) { block->append(parse_warning()); }
Packit Service 7770af
    else if (lex < kwd_if_directive >(true)) { block->append(parse_if_directive()); }
Packit Service 7770af
    else if (lex < kwd_for_directive >(true)) { block->append(parse_for_directive()); }
Packit Service 7770af
    else if (lex < kwd_each_directive >(true)) { block->append(parse_each_directive()); }
Packit Service 7770af
    else if (lex < kwd_while_directive >(true)) { block->append(parse_while_directive()); }
Packit Service 7770af
    else if (lex < kwd_return_directive >(true)) { block->append(parse_return_directive()); }
Packit Service 7770af
Packit Service 7770af
    // parse imports to process later
Packit Service 7770af
    else if (lex < kwd_import >(true)) {
Packit Service 7770af
      Scope parent = stack.empty() ? Scope::Rules : stack.back();
Packit Service 7770af
      if (parent != Scope::Function && parent != Scope::Root && parent != Scope::Rules && parent != Scope::Media) {
Packit Service 7770af
        if (! peek_css< uri_prefix >(position)) { // this seems to go in ruby sass 3.4.20
Packit Service 7770af
          error("Import directives may not be used within control directives or mixins.", pstate);
Packit Service 7770af
        }
Packit Service 7770af
      }
Packit Service 7770af
      // this puts the parsed doc into sheets
Packit Service 7770af
      // import stub will fetch this in expand
Packit Service 7770af
      Import_Obj imp = parse_import();
Packit Service 7770af
      // if it is a url, we only add the statement
Packit Service 7770af
      if (!imp->urls().empty()) block->append(imp);
Packit Service 7770af
      // process all resources now (add Import_Stub nodes)
Packit Service 7770af
      for (size_t i = 0, S = imp->incs().size(); i < S; ++i) {
Packit Service 7770af
        block->append(SASS_MEMORY_NEW(Import_Stub, pstate, imp->incs()[i]));
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    else if (lex < kwd_extend >(true)) {
Packit Service 7770af
      Lookahead lookahead = lookahead_for_include(position);
Packit Service 7770af
      if (!lookahead.found) css_error("Invalid CSS", " after ", ": expected selector, was ");
Packit Service 7770af
      Selector_List_Obj target;
Packit Service 7770af
      if (!lookahead.has_interpolants) {
Packit Service 7770af
        target = parse_selector_list(true);
Packit Service 7770af
      }
Packit Service 7770af
      else {
Packit Service 7770af
        target = SASS_MEMORY_NEW(Selector_List, pstate);
Packit Service 7770af
        target->schema(parse_selector_schema(lookahead.found, true));
Packit Service 7770af
      }
Packit Service 7770af
Packit Service 7770af
      block->append(SASS_MEMORY_NEW(Extension, pstate, target));
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    // selector may contain interpolations which need delayed evaluation
Packit Service 7770af
    else if (!(lookahead_result = lookahead_for_selector(position)).error)
Packit Service 7770af
    { block->append(parse_ruleset(lookahead_result)); }
Packit Service 7770af
Packit Service 7770af
    // parse multiple specific keyword directives
Packit Service 7770af
    else if (lex < kwd_media >(true)) { block->append(parse_media_block()); }
Packit Service 7770af
    else if (lex < kwd_at_root >(true)) { block->append(parse_at_root_block()); }
Packit Service 7770af
    else if (lex < kwd_include_directive >(true)) { block->append(parse_include_directive()); }
Packit Service 7770af
    else if (lex < kwd_content_directive >(true)) { block->append(parse_content_directive()); }
Packit Service 7770af
    else if (lex < kwd_supports_directive >(true)) { block->append(parse_supports_directive()); }
Packit Service 7770af
    else if (lex < kwd_mixin >(true)) { block->append(parse_definition(Definition::MIXIN)); }
Packit Service 7770af
    else if (lex < kwd_function >(true)) { block->append(parse_definition(Definition::FUNCTION)); }
Packit Service 7770af
Packit Service 7770af
    // ignore the @charset directive for now
Packit Service 7770af
    else if (lex< kwd_charset_directive >(true)) { parse_charset_directive(); }
Packit Service 7770af
Packit Service 7770af
    // generic at keyword (keep last)
Packit Service 7770af
    else if (lex< re_special_directive >(true)) { block->append(parse_special_directive()); }
Packit Service 7770af
    else if (lex< re_prefixed_directive >(true)) { block->append(parse_prefixed_directive()); }
Packit Service 7770af
    else if (lex< at_keyword >(true)) { block->append(parse_directive()); }
Packit Service 7770af
Packit Service 7770af
    else if (is_root /* && block->is_root() */) {
Packit Service 7770af
      lex< css_whitespace >();
Packit Service 7770af
      if (position >= end) return true;
Packit Service 7770af
      css_error("Invalid CSS", " after ", ": expected 1 selector or at-rule, was ");
Packit Service 7770af
    }
Packit Service 7770af
    // parse a declaration
Packit Service 7770af
    else
Packit Service 7770af
    {
Packit Service 7770af
      // ToDo: how does it handle parse errors?
Packit Service 7770af
      // maybe we are expected to parse something?
Packit Service 7770af
      Declaration_Obj decl = parse_declaration();
Packit Service 7770af
      decl->tabs(indentation);
Packit Service 7770af
      block->append(decl);
Packit Service 7770af
      // maybe we have a "sub-block"
Packit Service 7770af
      if (peek< exactly<'{'> >()) {
Packit Service 7770af
        if (decl->is_indented()) ++ indentation;
Packit Service 7770af
        // parse a propset that rides on the declaration's property
Packit Service 7770af
        stack.push_back(Scope::Properties);
Packit Service 7770af
        decl->block(parse_block());
Packit Service 7770af
        stack.pop_back();
Packit Service 7770af
        if (decl->is_indented()) -- indentation;
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
    // something matched
Packit Service 7770af
    return true;
Packit Service 7770af
  }
Packit Service 7770af
  // EO parse_block_nodes
Packit Service 7770af
Packit Service 7770af
  // parse imports inside the
Packit Service 7770af
  Import_Obj Parser::parse_import()
Packit Service 7770af
  {
Packit Service 7770af
    Import_Obj imp = SASS_MEMORY_NEW(Import, pstate);
Packit Service 7770af
    std::vector<std::pair<std::string,Function_Call_Obj>> to_import;
Packit Service 7770af
    bool first = true;
Packit Service 7770af
    do {
Packit Service 7770af
      while (lex< block_comment >());
Packit Service 7770af
      if (lex< quoted_string >()) {
Packit Service 7770af
        to_import.push_back(std::pair<std::string,Function_Call_Obj>(std::string(lexed), 0));
Packit Service 7770af
      }
Packit Service 7770af
      else if (lex< uri_prefix >()) {
Packit Service 7770af
        Arguments_Obj args = SASS_MEMORY_NEW(Arguments, pstate);
Packit Service 7770af
        Function_Call_Obj result = SASS_MEMORY_NEW(Function_Call, pstate, "url", args);
Packit Service 7770af
Packit Service 7770af
        if (lex< quoted_string >()) {
Packit Service 7770af
          Expression_Obj quoted_url = parse_string();
Packit Service 7770af
          args->append(SASS_MEMORY_NEW(Argument, quoted_url->pstate(), quoted_url));
Packit Service 7770af
        }
Packit Service 7770af
        else if (String_Obj string_url = parse_url_function_argument()) {
Packit Service 7770af
          args->append(SASS_MEMORY_NEW(Argument, string_url->pstate(), string_url));
Packit Service 7770af
        }
Packit Service 7770af
        else if (peek < skip_over_scopes < exactly < '(' >, exactly < ')' > > >(position)) {
Packit Service 7770af
          Expression_Obj braced_url = parse_list(); // parse_interpolated_chunk(lexed);
Packit Service 7770af
          args->append(SASS_MEMORY_NEW(Argument, braced_url->pstate(), braced_url));
Packit Service 7770af
        }
Packit Service 7770af
        else {
Packit Service 7770af
          error("malformed URL", pstate);
Packit Service 7770af
        }
Packit Service 7770af
        if (!lex< exactly<')'> >()) error("URI is missing ')'", pstate);
Packit Service 7770af
        to_import.push_back(std::pair<std::string, Function_Call_Obj>("", result));
Packit Service 7770af
      }
Packit Service 7770af
      else {
Packit Service 7770af
        if (first) error("@import directive requires a url or quoted path", pstate);
Packit Service 7770af
        else error("expecting another url or quoted path in @import list", pstate);
Packit Service 7770af
      }
Packit Service 7770af
      first = false;
Packit Service 7770af
    } while (lex_css< exactly<','> >());
Packit Service 7770af
Packit Service 7770af
    if (!peek_css< alternatives< exactly<';'>, exactly<'}'>, end_of_file > >()) {
Packit Service 7770af
      List_Obj import_queries = parse_media_queries();
Packit Service 7770af
      imp->import_queries(import_queries);
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    for(auto location : to_import) {
Packit Service 7770af
      if (location.second) {
Packit Service 7770af
        imp->urls().push_back(location.second);
Packit Service 7770af
      }
Packit Service 7770af
      // check if custom importers want to take over the handling
Packit Service 7770af
      else if (!ctx.call_importers(unquote(location.first), path, pstate, imp)) {
Packit Service 7770af
        // nobody wants it, so we do our import
Packit Service 7770af
        ctx.import_url(imp, location.first, path);
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    return imp;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Definition_Obj Parser::parse_definition(Definition::Type which_type)
Packit Service 7770af
  {
Packit Service 7770af
    std::string which_str(lexed);
Packit Service 7770af
    if (!lex< identifier >()) error("invalid name in " + which_str + " definition", pstate);
Packit Service 7770af
    std::string name(Util::normalize_underscores(lexed));
Packit Service 7770af
    if (which_type == Definition::FUNCTION && (name == "and" || name == "or" || name == "not"))
Packit Service 7770af
    { error("Invalid function name \"" + name + "\".", pstate); }
Packit Service 7770af
    ParserState source_position_of_def = pstate;
Packit Service 7770af
    Parameters_Obj params = parse_parameters();
Packit Service 7770af
    if (which_type == Definition::MIXIN) stack.push_back(Scope::Mixin);
Packit Service 7770af
    else stack.push_back(Scope::Function);
Packit Service 7770af
    Block_Obj body = parse_block();
Packit Service 7770af
    stack.pop_back();
Packit Service 7770af
    return SASS_MEMORY_NEW(Definition, source_position_of_def, name, params, body, which_type);
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Parameters_Obj Parser::parse_parameters()
Packit Service 7770af
  {
Packit Service 7770af
    Parameters_Obj params = SASS_MEMORY_NEW(Parameters, pstate);
Packit Service 7770af
    if (lex_css< exactly<'('> >()) {
Packit Service 7770af
      // if there's anything there at all
Packit Service 7770af
      if (!peek_css< exactly<')'> >()) {
Packit Service 7770af
        do {
Packit Service 7770af
          if (peek< exactly<')'> >()) break;
Packit Service 7770af
          params->append(parse_parameter());
Packit Service 7770af
        } while (lex_css< exactly<','> >());
Packit Service 7770af
      }
Packit Service 7770af
      if (!lex_css< exactly<')'> >()) {
Packit Service 7770af
        css_error("Invalid CSS", " after ", ": expected \")\", was ");
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
    return params;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Parameter_Obj Parser::parse_parameter()
Packit Service 7770af
  {
Packit Service 7770af
    if (peek< alternatives< exactly<','>, exactly< '{' >, exactly<';'> > >()) {
Packit Service 7770af
      css_error("Invalid CSS", " after ", ": expected variable (e.g. $foo), was ");
Packit Service 7770af
    }
Packit Service 7770af
    while (lex< alternatives < spaces, block_comment > >());
Packit Service 7770af
    lex < variable >();
Packit Service 7770af
    std::string name(Util::normalize_underscores(lexed));
Packit Service 7770af
    ParserState pos = pstate;
Packit Service 7770af
    Expression_Obj val;
Packit Service 7770af
    bool is_rest = false;
Packit Service 7770af
    while (lex< alternatives < spaces, block_comment > >());
Packit Service 7770af
    if (lex< exactly<':'> >()) { // there's a default value
Packit Service 7770af
      while (lex< block_comment >());
Packit Service 7770af
      val = parse_space_list();
Packit Service 7770af
    }
Packit Service 7770af
    else if (lex< exactly< ellipsis > >()) {
Packit Service 7770af
      is_rest = true;
Packit Service 7770af
    }
Packit Service 7770af
    return SASS_MEMORY_NEW(Parameter, pos, name, val, is_rest);
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Arguments_Obj Parser::parse_arguments()
Packit Service 7770af
  {
Packit Service 7770af
    Arguments_Obj args = SASS_MEMORY_NEW(Arguments, pstate);
Packit Service 7770af
    if (lex_css< exactly<'('> >()) {
Packit Service 7770af
      // if there's anything there at all
Packit Service 7770af
      if (!peek_css< exactly<')'> >()) {
Packit Service 7770af
        do {
Packit Service 7770af
          if (peek< exactly<')'> >()) break;
Packit Service 7770af
          args->append(parse_argument());
Packit Service 7770af
        } while (lex_css< exactly<','> >());
Packit Service 7770af
      }
Packit Service 7770af
      if (!lex_css< exactly<')'> >()) {
Packit Service 7770af
        css_error("Invalid CSS", " after ", ": expected expression (e.g. 1px, bold), was ");
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
    return args;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Argument_Obj Parser::parse_argument()
Packit Service 7770af
  {
Packit Service 7770af
    if (peek< alternatives< exactly<','>, exactly< '{' >, exactly<';'> > >()) {
Packit Service 7770af
      css_error("Invalid CSS", " after ", ": expected \")\", was ");
Packit Service 7770af
    }
Packit Service 7770af
    if (peek_css< sequence < exactly< hash_lbrace >, exactly< rbrace > > >()) {
Packit Service 7770af
      position += 2;
Packit Service 7770af
      css_error("Invalid CSS", " after ", ": expected expression (e.g. 1px, bold), was ");
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Argument_Obj arg;
Packit Service 7770af
    if (peek_css< sequence < variable, optional_css_comments, exactly<':'> > >()) {
Packit Service 7770af
      lex_css< variable >();
Packit Service 7770af
      std::string name(Util::normalize_underscores(lexed));
Packit Service 7770af
      ParserState p = pstate;
Packit Service 7770af
      lex_css< exactly<':'> >();
Packit Service 7770af
      Expression_Obj val = parse_space_list();
Packit Service 7770af
      arg = SASS_MEMORY_NEW(Argument, p, val, name);
Packit Service 7770af
    }
Packit Service 7770af
    else {
Packit Service 7770af
      bool is_arglist = false;
Packit Service 7770af
      bool is_keyword = false;
Packit Service 7770af
      Expression_Obj val = parse_space_list();
Packit Service 7770af
      List_Ptr l = Cast<List>(val);
Packit Service 7770af
      if (lex_css< exactly< ellipsis > >()) {
Packit Service 7770af
        if (val->concrete_type() == Expression::MAP || (
Packit Service 7770af
           (l != NULL && l->separator() == SASS_HASH)
Packit Service 7770af
        )) is_keyword = true;
Packit Service 7770af
        else is_arglist = true;
Packit Service 7770af
      }
Packit Service 7770af
      arg = SASS_MEMORY_NEW(Argument, pstate, val, "", is_arglist, is_keyword);
Packit Service 7770af
    }
Packit Service 7770af
    return arg;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Assignment_Obj Parser::parse_assignment()
Packit Service 7770af
  {
Packit Service 7770af
    std::string name(Util::normalize_underscores(lexed));
Packit Service 7770af
    ParserState var_source_position = pstate;
Packit Service 7770af
    if (!lex< exactly<':'> >()) error("expected ':' after " + name + " in assignment statement", pstate);
Packit Service 7770af
    if (peek_css< alternatives < exactly<';'>, end_of_file > >()) {
Packit Service 7770af
      css_error("Invalid CSS", " after ", ": expected expression (e.g. 1px, bold), was ");
Packit Service 7770af
    }
Packit Service 7770af
    Expression_Obj val;
Packit Service 7770af
    Lookahead lookahead = lookahead_for_value(position);
Packit Service 7770af
    if (lookahead.has_interpolants && lookahead.found) {
Packit Service 7770af
      val = parse_value_schema(lookahead.found);
Packit Service 7770af
    } else {
Packit Service 7770af
      val = parse_list();
Packit Service 7770af
    }
Packit Service 7770af
    bool is_default = false;
Packit Service 7770af
    bool is_global = false;
Packit Service 7770af
    while (peek< alternatives < default_flag, global_flag > >()) {
Packit Service 7770af
      if (lex< default_flag >()) is_default = true;
Packit Service 7770af
      else if (lex< global_flag >()) is_global = true;
Packit Service 7770af
    }
Packit Service 7770af
    return SASS_MEMORY_NEW(Assignment, var_source_position, name, val, is_default, is_global);
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  // a ruleset connects a selector and a block
Packit Service 7770af
  Ruleset_Obj Parser::parse_ruleset(Lookahead lookahead)
Packit Service 7770af
  {
Packit Service 7770af
    // inherit is_root from parent block
Packit Service 7770af
    Block_Obj parent = block_stack.back();
Packit Service 7770af
    bool is_root = parent && parent->is_root();
Packit Service 7770af
    // make sure to move up the the last position
Packit Service 7770af
    lex < optional_css_whitespace >(false, true);
Packit Service 7770af
    // create the connector object (add parts later)
Packit Service 7770af
    Ruleset_Obj ruleset = SASS_MEMORY_NEW(Ruleset, pstate);
Packit Service 7770af
    // parse selector static or as schema to be evaluated later
Packit Service 7770af
    if (lookahead.parsable) ruleset->selector(parse_selector_list(false));
Packit Service 7770af
    else {
Packit Service 7770af
      Selector_List_Obj list = SASS_MEMORY_NEW(Selector_List, pstate);
Packit Service 7770af
      list->schema(parse_selector_schema(lookahead.position, false));
Packit Service 7770af
      ruleset->selector(list);
Packit Service 7770af
    }
Packit Service 7770af
    // then parse the inner block
Packit Service 7770af
    stack.push_back(Scope::Rules);
Packit Service 7770af
    ruleset->block(parse_block());
Packit Service 7770af
    stack.pop_back();
Packit Service 7770af
    // update for end position
Packit Service 7770af
    ruleset->update_pstate(pstate);
Packit Service 7770af
    ruleset->block()->update_pstate(pstate);
Packit Service 7770af
    // need this info for sanity checks
Packit Service 7770af
    ruleset->is_root(is_root);
Packit Service 7770af
    // return AST Node
Packit Service 7770af
    return ruleset;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  // parse a selector schema that will be evaluated in the eval stage
Packit Service 7770af
  // uses a string schema internally to do the actual schema handling
Packit Service 7770af
  // in the eval stage we will be re-parse it into an actual selector
Packit Service 7770af
  Selector_Schema_Obj Parser::parse_selector_schema(const char* end_of_selector, bool chroot)
Packit Service 7770af
  {
Packit Service 7770af
    // move up to the start
Packit Service 7770af
    lex< optional_spaces >();
Packit Service 7770af
    const char* i = position;
Packit Service 7770af
    // selector schema re-uses string schema implementation
Packit Service 7770af
    String_Schema_Ptr schema = SASS_MEMORY_NEW(String_Schema, pstate);
Packit Service 7770af
    // the selector schema is pretty much just a wrapper for the string schema
Packit Service 7770af
    Selector_Schema_Obj selector_schema = SASS_MEMORY_NEW(Selector_Schema, pstate, schema);
Packit Service 7770af
    selector_schema->connect_parent(chroot == false);
Packit Service 7770af
    selector_schema->media_block(last_media_block);
Packit Service 7770af
Packit Service 7770af
    // process until end
Packit Service 7770af
    while (i < end_of_selector) {
Packit Service 7770af
      // try to parse mutliple interpolants
Packit Service 7770af
      if (const char* p = find_first_in_interval< exactly<hash_lbrace>, block_comment >(i, end_of_selector)) {
Packit Service 7770af
        // accumulate the preceding segment if the position has advanced
Packit Service 7770af
        if (i < p) {
Packit Service 7770af
          std::string parsed(i, p);
Packit Service 7770af
          String_Constant_Obj str = SASS_MEMORY_NEW(String_Constant, pstate, parsed);
Packit Service 7770af
          pstate += Offset(parsed);
Packit Service 7770af
          str->update_pstate(pstate);
Packit Service 7770af
          schema->append(str);
Packit Service 7770af
        }
Packit Service 7770af
Packit Service 7770af
        // skip over all nested inner interpolations up to our own delimiter
Packit Service 7770af
        const char* j = skip_over_scopes< exactly<hash_lbrace>, exactly<rbrace> >(p + 2, end_of_selector);
Packit Service 7770af
        // check if the interpolation never ends of only contains white-space (error out)
Packit Service 7770af
        if (!j || peek < sequence < optional_spaces, exactly<rbrace> > >(p+2)) {
Packit Service 7770af
          position = p+2;
Packit Service 7770af
          css_error("Invalid CSS", " after ", ": expected expression (e.g. 1px, bold), was ");
Packit Service 7770af
        }
Packit Service 7770af
        // pass inner expression to the parser to resolve nested interpolations
Packit Service 7770af
        pstate.add(p, p+2);
Packit Service 7770af
        Expression_Obj interpolant = Parser::from_c_str(p+2, j, ctx, pstate).parse_list();
Packit Service 7770af
        // set status on the list expression
Packit Service 7770af
        interpolant->is_interpolant(true);
Packit Service 7770af
        // schema->has_interpolants(true);
Packit Service 7770af
        // add to the string schema
Packit Service 7770af
        schema->append(interpolant);
Packit Service 7770af
        // advance parser state
Packit Service 7770af
        pstate.add(p+2, j);
Packit Service 7770af
        // advance position
Packit Service 7770af
        i = j;
Packit Service 7770af
      }
Packit Service 7770af
      // no more interpolants have been found
Packit Service 7770af
      // add the last segment if there is one
Packit Service 7770af
      else {
Packit Service 7770af
        // make sure to add the last bits of the string up to the end (if any)
Packit Service 7770af
        if (i < end_of_selector) {
Packit Service 7770af
          std::string parsed(i, end_of_selector);
Packit Service 7770af
          String_Constant_Obj str = SASS_MEMORY_NEW(String_Constant, pstate, parsed);
Packit Service 7770af
          pstate += Offset(parsed);
Packit Service 7770af
          str->update_pstate(pstate);
Packit Service 7770af
          i = end_of_selector;
Packit Service 7770af
          schema->append(str);
Packit Service 7770af
        }
Packit Service 7770af
        // exit loop
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
    // EO until eos
Packit Service 7770af
Packit Service 7770af
    // update position
Packit Service 7770af
    position = i;
Packit Service 7770af
Packit Service 7770af
    // update for end position
Packit Service 7770af
    selector_schema->update_pstate(pstate);
Packit Service 7770af
    schema->update_pstate(pstate);
Packit Service 7770af
Packit Service 7770af
    after_token = before_token = pstate;
Packit Service 7770af
Packit Service 7770af
    // return parsed result
Packit Service 7770af
    return selector_schema.detach();
Packit Service 7770af
  }
Packit Service 7770af
  // EO parse_selector_schema
Packit Service 7770af
Packit Service 7770af
  void Parser::parse_charset_directive()
Packit Service 7770af
  {
Packit Service 7770af
    lex <
Packit Service 7770af
      sequence <
Packit Service 7770af
        quoted_string,
Packit Service 7770af
        optional_spaces,
Packit Service 7770af
        exactly <';'>
Packit Service 7770af
      >
Packit Service 7770af
    >();
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  // called after parsing `kwd_include_directive`
Packit Service 7770af
  Mixin_Call_Obj Parser::parse_include_directive()
Packit Service 7770af
  {
Packit Service 7770af
    // lex identifier into `lexed` var
Packit Service 7770af
    lex_identifier(); // may error out
Packit Service 7770af
    // normalize underscores to hyphens
Packit Service 7770af
    std::string name(Util::normalize_underscores(lexed));
Packit Service 7770af
    // create the initial mixin call object
Packit Service 7770af
    Mixin_Call_Obj call = SASS_MEMORY_NEW(Mixin_Call, pstate, name, 0, 0);
Packit Service 7770af
    // parse mandatory arguments
Packit Service 7770af
    call->arguments(parse_arguments());
Packit Service 7770af
    // parse optional block
Packit Service 7770af
    if (peek < exactly <'{'> >()) {
Packit Service 7770af
      call->block(parse_block());
Packit Service 7770af
    }
Packit Service 7770af
    // return ast node
Packit Service 7770af
    return call.detach();
Packit Service 7770af
  }
Packit Service 7770af
  // EO parse_include_directive
Packit Service 7770af
Packit Service 7770af
  // parse a list of complex selectors
Packit Service 7770af
  // this is the main entry point for most
Packit Service 7770af
  Selector_List_Obj Parser::parse_selector_list(bool chroot)
Packit Service 7770af
  {
Packit Service 7770af
    bool reloop;
Packit Service 7770af
    bool had_linefeed = false;
Packit Service 7770af
    Complex_Selector_Obj sel;
Packit Service 7770af
    Selector_List_Obj group = SASS_MEMORY_NEW(Selector_List, pstate);
Packit Service 7770af
    group->media_block(last_media_block);
Packit Service 7770af
Packit Service 7770af
    if (peek_css< alternatives < end_of_file, exactly <'{'>, exactly <','> > >()) {
Packit Service 7770af
      css_error("Invalid CSS", " after ", ": expected selector, was ");
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    do {
Packit Service 7770af
      reloop = false;
Packit Service 7770af
Packit Service 7770af
      had_linefeed = had_linefeed || peek_newline();
Packit Service 7770af
Packit Service 7770af
      if (peek_css< alternatives < class_char < selector_list_delims > > >())
Packit Service 7770af
        break; // in case there are superfluous commas at the end
Packit Service 7770af
Packit Service 7770af
      // now parse the complex selector
Packit Service 7770af
      sel = parse_complex_selector(chroot);
Packit Service 7770af
Packit Service 7770af
      if (!sel) return group.detach();
Packit Service 7770af
Packit Service 7770af
      sel->has_line_feed(had_linefeed);
Packit Service 7770af
Packit Service 7770af
      had_linefeed = false;
Packit Service 7770af
Packit Service 7770af
      while (peek_css< exactly<','> >())
Packit Service 7770af
      {
Packit Service 7770af
        lex< css_comments >(false);
Packit Service 7770af
        // consume everything up and including the comma separator
Packit Service 7770af
        reloop = lex< exactly<','> >() != 0;
Packit Service 7770af
        // remember line break (also between some commas)
Packit Service 7770af
        had_linefeed = had_linefeed || peek_newline();
Packit Service 7770af
        // remember line break (also between some commas)
Packit Service 7770af
      }
Packit Service 7770af
      group->append(sel);
Packit Service 7770af
    }
Packit Service 7770af
    while (reloop);
Packit Service 7770af
    while (lex_css< kwd_optional >()) {
Packit Service 7770af
      group->is_optional(true);
Packit Service 7770af
    }
Packit Service 7770af
    // update for end position
Packit Service 7770af
    group->update_pstate(pstate);
Packit Service 7770af
    if (sel) sel->last()->has_line_break(false);
Packit Service 7770af
    return group.detach();
Packit Service 7770af
  }
Packit Service 7770af
  // EO parse_selector_list
Packit Service 7770af
Packit Service 7770af
  // a complex selector combines a compound selector with another
Packit Service 7770af
  // complex selector, with one of four combinator operations.
Packit Service 7770af
  // the compound selector (head) is optional, since the combinator
Packit Service 7770af
  // can come first in the whole selector sequence (like `> DIV').
Packit Service 7770af
  Complex_Selector_Obj Parser::parse_complex_selector(bool chroot)
Packit Service 7770af
  {
Packit Service 7770af
Packit Service 7770af
    String_Obj reference = 0;
Packit Service 7770af
    lex < block_comment >();
Packit Service 7770af
    advanceToNextToken();
Packit Service 7770af
    Complex_Selector_Obj sel = SASS_MEMORY_NEW(Complex_Selector, pstate);
Packit Service 7770af
Packit Service 7770af
    if (peek < end_of_file >()) return 0;
Packit Service 7770af
Packit Service 7770af
    // parse the left hand side
Packit Service 7770af
    Compound_Selector_Obj lhs;
Packit Service 7770af
    // special case if it starts with combinator ([+~>])
Packit Service 7770af
    if (!peek_css< class_char < selector_combinator_ops > >()) {
Packit Service 7770af
      // parse the left hand side
Packit Service 7770af
      lhs = parse_compound_selector();
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
Packit Service 7770af
    // parse combinator between lhs and rhs
Packit Service 7770af
    Complex_Selector::Combinator combinator = Complex_Selector::ANCESTOR_OF;
Packit Service 7770af
    if      (lex< exactly<'+'> >()) combinator = Complex_Selector::ADJACENT_TO;
Packit Service 7770af
    else if (lex< exactly<'~'> >()) combinator = Complex_Selector::PRECEDES;
Packit Service 7770af
    else if (lex< exactly<'>'> >()) combinator = Complex_Selector::PARENT_OF;
Packit Service 7770af
    else if (lex< sequence < exactly<'/'>, negate < exactly < '*' > > > >()) {
Packit Service 7770af
      // comments are allowed, but not spaces?
Packit Service 7770af
      combinator = Complex_Selector::REFERENCE;
Packit Service 7770af
      if (!lex < re_reference_combinator >()) return 0;
Packit Service 7770af
      reference = SASS_MEMORY_NEW(String_Constant, pstate, lexed);
Packit Service 7770af
      if (!lex < exactly < '/' > >()) return 0; // ToDo: error msg?
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    if (!lhs && combinator == Complex_Selector::ANCESTOR_OF) return 0;
Packit Service 7770af
Packit Service 7770af
    // lex < block_comment >();
Packit Service 7770af
    sel->head(lhs);
Packit Service 7770af
    sel->combinator(combinator);
Packit Service 7770af
    sel->media_block(last_media_block);
Packit Service 7770af
Packit Service 7770af
    if (combinator == Complex_Selector::REFERENCE) sel->reference(reference);
Packit Service 7770af
    // has linfeed after combinator?
Packit Service 7770af
    sel->has_line_break(peek_newline());
Packit Service 7770af
    // sel->has_line_feed(has_line_feed);
Packit Service 7770af
Packit Service 7770af
    // check if we got the abort condition (ToDo: optimize)
Packit Service 7770af
    if (!peek_css< class_char < complex_selector_delims > >()) {
Packit Service 7770af
      // parse next selector in sequence
Packit Service 7770af
      sel->tail(parse_complex_selector(true));
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    // add a parent selector if we are not in a root
Packit Service 7770af
    // also skip adding parent ref if we only have refs
Packit Service 7770af
    if (!sel->has_parent_ref() && !chroot) {
Packit Service 7770af
      // create the objects to wrap parent selector reference
Packit Service 7770af
      Compound_Selector_Obj head = SASS_MEMORY_NEW(Compound_Selector, pstate);
Packit Service 7770af
      Parent_Selector_Ptr parent = SASS_MEMORY_NEW(Parent_Selector, pstate, false);
Packit Service 7770af
      parent->media_block(last_media_block);
Packit Service 7770af
      head->media_block(last_media_block);
Packit Service 7770af
      // add simple selector
Packit Service 7770af
      head->append(parent);
Packit Service 7770af
      // selector may not have any head yet
Packit Service 7770af
      if (!sel->head()) { sel->head(head); }
Packit Service 7770af
      // otherwise we need to create a new complex selector and set the old one as its tail
Packit Service 7770af
      else {
Packit Service 7770af
        sel = SASS_MEMORY_NEW(Complex_Selector, pstate, Complex_Selector::ANCESTOR_OF, head, sel);
Packit Service 7770af
        sel->media_block(last_media_block);
Packit Service 7770af
      }
Packit Service 7770af
      // peek for linefeed and remember result on head
Packit Service 7770af
      // if (peek_newline()) head->has_line_break(true);
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    sel->update_pstate(pstate);
Packit Service 7770af
    // complex selector
Packit Service 7770af
    return sel;
Packit Service 7770af
  }
Packit Service 7770af
  // EO parse_complex_selector
Packit Service 7770af
Packit Service 7770af
  // parse one compound selector, which is basically
Packit Service 7770af
  // a list of simple selectors (directly adjacent)
Packit Service 7770af
  // lex them exactly (without skipping white-space)
Packit Service 7770af
  Compound_Selector_Obj Parser::parse_compound_selector()
Packit Service 7770af
  {
Packit Service 7770af
    // init an empty compound selector wrapper
Packit Service 7770af
    Compound_Selector_Obj seq = SASS_MEMORY_NEW(Compound_Selector, pstate);
Packit Service 7770af
    seq->media_block(last_media_block);
Packit Service 7770af
Packit Service 7770af
    // skip initial white-space
Packit Service 7770af
    lex< css_whitespace >();
Packit Service 7770af
Packit Service 7770af
    // parse list
Packit Service 7770af
    while (true)
Packit Service 7770af
    {
Packit Service 7770af
      // remove all block comments (don't skip white-space)
Packit Service 7770af
      lex< delimited_by< slash_star, star_slash, false > >(false);
Packit Service 7770af
      // parse functional
Packit Service 7770af
      if (match < re_pseudo_selector >())
Packit Service 7770af
      {
Packit Service 7770af
        seq->append(parse_simple_selector());
Packit Service 7770af
      }
Packit Service 7770af
      // parse parent selector
Packit Service 7770af
      else if (lex< exactly<'&'> >(false))
Packit Service 7770af
      {
Packit Service 7770af
        // this produces a linefeed!?
Packit Service 7770af
        seq->has_parent_reference(true);
Packit Service 7770af
        seq->append(SASS_MEMORY_NEW(Parent_Selector, pstate));
Packit Service 7770af
        // parent selector only allowed at start
Packit Service 7770af
        // upcoming Sass may allow also trailing
Packit Service 7770af
        if (seq->length() > 1) {
Packit Service 7770af
          ParserState state(pstate);
Packit Service 7770af
          Simple_Selector_Obj cur = (*seq)[seq->length()-1];
Packit Service 7770af
          Simple_Selector_Obj prev = (*seq)[seq->length()-2];
Packit Service 7770af
          std::string sel(prev->to_string({ NESTED, 5 }));
Packit Service 7770af
          std::string found(cur->to_string({ NESTED, 5 }));
Packit Service 7770af
          if (lex < identifier >()) { found += std::string(lexed); }
Packit Service 7770af
          error("Invalid CSS after \"" + sel + "\": expected \"{\", was \"" + found + "\"\n\n"
Packit Service 7770af
            "\"" + found + "\" may only be used at the beginning of a compound selector.", state);
Packit Service 7770af
        }
Packit Service 7770af
      }
Packit Service 7770af
      // parse type selector
Packit Service 7770af
      else if (lex< re_type_selector >(false))
Packit Service 7770af
      {
Packit Service 7770af
        seq->append(SASS_MEMORY_NEW(Element_Selector, pstate, lexed));
Packit Service 7770af
      }
Packit Service 7770af
      // peek for abort conditions
Packit Service 7770af
      else if (peek< spaces >()) break;
Packit Service 7770af
      else if (peek< end_of_file >()) { break; }
Packit Service 7770af
      else if (peek_css < class_char < selector_combinator_ops > >()) break;
Packit Service 7770af
      else if (peek_css < class_char < complex_selector_delims > >()) break;
Packit Service 7770af
      // otherwise parse another simple selector
Packit Service 7770af
      else {
Packit Service 7770af
        Simple_Selector_Obj sel = parse_simple_selector();
Packit Service 7770af
        if (!sel) return 0;
Packit Service 7770af
        seq->append(sel);
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    if (seq && !peek_css<alternatives<end_of_file,exactly<'{'>>>()) {
Packit Service 7770af
      seq->has_line_break(peek_newline());
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    // EO while true
Packit Service 7770af
    return seq;
Packit Service 7770af
Packit Service 7770af
  }
Packit Service 7770af
  // EO parse_compound_selector
Packit Service 7770af
Packit Service 7770af
  Simple_Selector_Obj Parser::parse_simple_selector()
Packit Service 7770af
  {
Packit Service 7770af
    lex < css_comments >(false);
Packit Service 7770af
    if (lex< class_name >()) {
Packit Service 7770af
      return SASS_MEMORY_NEW(Class_Selector, pstate, lexed);
Packit Service 7770af
    }
Packit Service 7770af
    else if (lex< id_name >()) {
Packit Service 7770af
      return SASS_MEMORY_NEW(Id_Selector, pstate, lexed);
Packit Service 7770af
    }
Packit Service 7770af
    else if (lex< quoted_string >()) {
Packit Service 7770af
      return SASS_MEMORY_NEW(Element_Selector, pstate, unquote(lexed));
Packit Service 7770af
    }
Packit Service 7770af
    else if (lex< alternatives < variable, number, static_reference_combinator > >()) {
Packit Service 7770af
      return SASS_MEMORY_NEW(Element_Selector, pstate, lexed);
Packit Service 7770af
    }
Packit Service 7770af
    else if (peek< pseudo_not >()) {
Packit Service 7770af
      return parse_negated_selector();
Packit Service 7770af
    }
Packit Service 7770af
    else if (peek< re_pseudo_selector >()) {
Packit Service 7770af
      return parse_pseudo_selector();
Packit Service 7770af
    }
Packit Service 7770af
    else if (peek< exactly<':'> >()) {
Packit Service 7770af
      return parse_pseudo_selector();
Packit Service 7770af
    }
Packit Service 7770af
    else if (lex < exactly<'['> >()) {
Packit Service 7770af
      return parse_attribute_selector();
Packit Service 7770af
    }
Packit Service 7770af
    else if (lex< placeholder >()) {
Packit Service 7770af
      Placeholder_Selector_Ptr sel = SASS_MEMORY_NEW(Placeholder_Selector, pstate, lexed);
Packit Service 7770af
      sel->media_block(last_media_block);
Packit Service 7770af
      return sel;
Packit Service 7770af
    }
Packit Service 7770af
    // failed
Packit Service 7770af
    return 0;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Wrapped_Selector_Obj Parser::parse_negated_selector()
Packit Service 7770af
  {
Packit Service 7770af
    lex< pseudo_not >();
Packit Service 7770af
    std::string name(lexed);
Packit Service 7770af
    ParserState nsource_position = pstate;
Packit Service 7770af
    Selector_List_Obj negated = parse_selector_list(true);
Packit Service 7770af
    if (!lex< exactly<')'> >()) {
Packit Service 7770af
      error("negated selector is missing ')'", pstate);
Packit Service 7770af
    }
Packit Service 7770af
    name.erase(name.size() - 1);
Packit Service 7770af
    return SASS_MEMORY_NEW(Wrapped_Selector, nsource_position, name, negated);
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  // a pseudo selector often starts with one or two colons
Packit Service 7770af
  // it can contain more selectors inside parentheses
Packit Service 7770af
  Simple_Selector_Obj Parser::parse_pseudo_selector() {
Packit Service 7770af
    if (lex< sequence<
Packit Service 7770af
          optional < pseudo_prefix >,
Packit Service 7770af
          // we keep the space within the name, strange enough
Packit Service 7770af
          // ToDo: refactor output to schedule the space for it
Packit Service 7770af
          // or do we really want to keep the real white-space?
Packit Service 7770af
          sequence< identifier, optional < block_comment >, exactly<'('> >
Packit Service 7770af
        > >())
Packit Service 7770af
    {
Packit Service 7770af
Packit Service 7770af
      std::string name(lexed);
Packit Service 7770af
      name.erase(name.size() - 1);
Packit Service 7770af
      ParserState p = pstate;
Packit Service 7770af
Packit Service 7770af
      // specially parse static stuff
Packit Service 7770af
      // ToDo: really everything static?
Packit Service 7770af
      if (peek_css <
Packit Service 7770af
            sequence <
Packit Service 7770af
              alternatives <
Packit Service 7770af
                static_value,
Packit Service 7770af
                binomial
Packit Service 7770af
              >,
Packit Service 7770af
              optional_css_whitespace,
Packit Service 7770af
              exactly<')'>
Packit Service 7770af
            >
Packit Service 7770af
          >()
Packit Service 7770af
      ) {
Packit Service 7770af
        lex_css< alternatives < static_value, binomial > >();
Packit Service 7770af
        String_Constant_Obj expr = SASS_MEMORY_NEW(String_Constant, pstate, lexed);
Packit Service 7770af
        if (lex_css< exactly<')'> >()) {
Packit Service 7770af
          expr->can_compress_whitespace(true);
Packit Service 7770af
          return SASS_MEMORY_NEW(Pseudo_Selector, p, name, expr);
Packit Service 7770af
        }
Packit Service 7770af
      }
Packit Service 7770af
      else if (Selector_List_Obj wrapped = parse_selector_list(true)) {
Packit Service 7770af
        if (wrapped && lex_css< exactly<')'> >()) {
Packit Service 7770af
          return SASS_MEMORY_NEW(Wrapped_Selector, p, name, wrapped);
Packit Service 7770af
        }
Packit Service 7770af
      }
Packit Service 7770af
Packit Service 7770af
    }
Packit Service 7770af
    // EO if pseudo selector
Packit Service 7770af
Packit Service 7770af
    else if (lex < sequence< optional < pseudo_prefix >, identifier > >()) {
Packit Service 7770af
      return SASS_MEMORY_NEW(Pseudo_Selector, pstate, lexed);
Packit Service 7770af
    }
Packit Service 7770af
    else if(lex < pseudo_prefix >()) {
Packit Service 7770af
      css_error("Invalid CSS", " after ", ": expected pseudoclass or pseudoelement, was ");
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    css_error("Invalid CSS", " after ", ": expected \")\", was ");
Packit Service 7770af
Packit Service 7770af
    // unreachable statement
Packit Service 7770af
    return 0;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Attribute_Selector_Obj Parser::parse_attribute_selector()
Packit Service 7770af
  {
Packit Service 7770af
    ParserState p = pstate;
Packit Service 7770af
    if (!lex_css< attribute_name >()) error("invalid attribute name in attribute selector", pstate);
Packit Service 7770af
    std::string name(lexed);
Packit Service 7770af
    if (lex_css< alternatives < exactly<']'>, exactly<'/'> > >()) return SASS_MEMORY_NEW(Attribute_Selector, p, name, "", 0);
Packit Service 7770af
    if (!lex_css< alternatives< exact_match, class_match, dash_match,
Packit Service 7770af
                                prefix_match, suffix_match, substring_match > >()) {
Packit Service 7770af
      error("invalid operator in attribute selector for " + name, pstate);
Packit Service 7770af
    }
Packit Service 7770af
    std::string matcher(lexed);
Packit Service 7770af
Packit Service 7770af
    String_Obj value = 0;
Packit Service 7770af
    if (lex_css< identifier >()) {
Packit Service 7770af
      value = SASS_MEMORY_NEW(String_Constant, p, lexed);
Packit Service 7770af
    }
Packit Service 7770af
    else if (lex_css< quoted_string >()) {
Packit Service 7770af
      value = parse_interpolated_chunk(lexed, true); // needed!
Packit Service 7770af
    }
Packit Service 7770af
    else {
Packit Service 7770af
      error("expected a string constant or identifier in attribute selector for " + name, pstate);
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    if (!lex_css< alternatives < exactly<']'>, exactly<'/'> > >()) error("unterminated attribute selector for " + name, pstate);
Packit Service 7770af
    return SASS_MEMORY_NEW(Attribute_Selector, p, name, matcher, value);
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  /* parse block comment and add to block */
Packit Service 7770af
  void Parser::parse_block_comments()
Packit Service 7770af
  {
Packit Service 7770af
    Block_Obj block = block_stack.back();
Packit Service 7770af
Packit Service 7770af
    while (lex< block_comment >()) {
Packit Service 7770af
      bool is_important = lexed.begin[2] == '!';
Packit Service 7770af
      // flag on second param is to skip loosely over comments
Packit Service 7770af
      String_Obj contents = parse_interpolated_chunk(lexed, true);
Packit Service 7770af
      block->append(SASS_MEMORY_NEW(Comment, pstate, contents, is_important));
Packit Service 7770af
    }
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Declaration_Obj Parser::parse_declaration() {
Packit Service 7770af
    String_Obj prop;
Packit Service 7770af
    if (lex< sequence< optional< exactly<'*'> >, identifier_schema > >()) {
Packit Service 7770af
      prop = parse_identifier_schema();
Packit Service 7770af
    }
Packit Service 7770af
    else if (lex< sequence< optional< exactly<'*'> >, identifier, zero_plus< block_comment > > >()) {
Packit Service 7770af
      prop = SASS_MEMORY_NEW(String_Constant, pstate, lexed);
Packit Service 7770af
    }
Packit Service 7770af
    else {
Packit Service 7770af
      css_error("Invalid CSS", " after ", ": expected \"}\", was ");
Packit Service 7770af
    }
Packit Service 7770af
    bool is_indented = true;
Packit Service 7770af
    const std::string property(lexed);
Packit Service 7770af
    if (!lex_css< one_plus< exactly<':'> > >()) error("property \"" + property + "\" must be followed by a ':'", pstate);
Packit Service 7770af
    lex < css_comments >(false);
Packit Service 7770af
    if (peek_css< exactly<';'> >()) error("style declaration must contain a value", pstate);
Packit Service 7770af
    if (peek_css< exactly<'{'> >()) is_indented = false; // don't indent if value is empty
Packit Service 7770af
    if (peek_css< static_value >()) {
Packit Service 7770af
      return SASS_MEMORY_NEW(Declaration, prop->pstate(), prop, parse_static_value()/*, lex<kwd_important>()*/);
Packit Service 7770af
    }
Packit Service 7770af
    else {
Packit Service 7770af
      Expression_Obj value;
Packit Service 7770af
      Lookahead lookahead = lookahead_for_value(position);
Packit Service 7770af
      if (lookahead.found) {
Packit Service 7770af
        if (lookahead.has_interpolants) {
Packit Service 7770af
          value = parse_value_schema(lookahead.found);
Packit Service 7770af
        } else {
Packit Service 7770af
          value = parse_list(DELAYED);
Packit Service 7770af
        }
Packit Service 7770af
      }
Packit Service 7770af
      else {
Packit Service 7770af
        value = parse_list(DELAYED);
Packit Service 7770af
        if (List_Ptr list = Cast<List>(value)) {
Packit Service 7770af
          if (list->length() == 0 && !peek< exactly <'{'> >()) {
Packit Service 7770af
            css_error("Invalid CSS", " after ", ": expected expression (e.g. 1px, bold), was ");
Packit Service 7770af
          }
Packit Service 7770af
        }
Packit Service 7770af
      }
Packit Service 7770af
      lex < css_comments >(false);
Packit Service 7770af
      Declaration_Obj decl = SASS_MEMORY_NEW(Declaration, prop->pstate(), prop, value/*, lex<kwd_important>()*/);
Packit Service 7770af
      decl->is_indented(is_indented);
Packit Service 7770af
      decl->update_pstate(pstate);
Packit Service 7770af
      return decl;
Packit Service 7770af
    }
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  // parse +/- and return false if negative
Packit Service 7770af
  // this is never hit via spec tests
Packit Service 7770af
  bool Parser::parse_number_prefix()
Packit Service 7770af
  {
Packit Service 7770af
    bool positive = true;
Packit Service 7770af
    while(true) {
Packit Service 7770af
      if (lex < block_comment >()) continue;
Packit Service 7770af
      if (lex < number_prefix >()) continue;
Packit Service 7770af
      if (lex < exactly < '-' > >()) {
Packit Service 7770af
        positive = !positive;
Packit Service 7770af
        continue;
Packit Service 7770af
      }
Packit Service 7770af
      break;
Packit Service 7770af
    }
Packit Service 7770af
    return positive;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Expression_Obj Parser::parse_map()
Packit Service 7770af
  {
Packit Service 7770af
    Expression_Obj key = parse_list();
Packit Service 7770af
    List_Obj map = SASS_MEMORY_NEW(List, pstate, 0, SASS_HASH);
Packit Service 7770af
Packit Service 7770af
    // it's not a map so return the lexed value as a list value
Packit Service 7770af
    if (!lex_css< exactly<':'> >())
Packit Service 7770af
    { return key; }
Packit Service 7770af
Packit Service 7770af
    List_Obj l = Cast<List>(key);
Packit Service 7770af
    if (l && l->separator() == SASS_COMMA) {
Packit Service 7770af
      css_error("Invalid CSS", " after ", ": expected \")\", was ");
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Expression_Obj value = parse_space_list();
Packit Service 7770af
Packit Service 7770af
    map->append(key);
Packit Service 7770af
    map->append(value);
Packit Service 7770af
Packit Service 7770af
    while (lex_css< exactly<','> >())
Packit Service 7770af
    {
Packit Service 7770af
      // allow trailing commas - #495
Packit Service 7770af
      if (peek_css< exactly<')'> >(position))
Packit Service 7770af
      { break; }
Packit Service 7770af
Packit Service 7770af
      key = parse_space_list();
Packit Service 7770af
Packit Service 7770af
      if (!(lex< exactly<':'> >()))
Packit Service 7770af
      { css_error("Invalid CSS", " after ", ": expected \":\", was "); }
Packit Service 7770af
Packit Service 7770af
      value = parse_space_list();
Packit Service 7770af
Packit Service 7770af
      map->append(key);
Packit Service 7770af
      map->append(value);
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    ParserState ps = map->pstate();
Packit Service 7770af
    ps.offset = pstate - ps + pstate.offset;
Packit Service 7770af
    map->pstate(ps);
Packit Service 7770af
Packit Service 7770af
    return map;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  // parse list returns either a space separated list,
Packit Service 7770af
  // a comma separated list or any bare expression found.
Packit Service 7770af
  // so to speak: we unwrap items from lists if possible here!
Packit Service 7770af
  Expression_Obj Parser::parse_list(bool delayed)
Packit Service 7770af
  {
Packit Service 7770af
    return parse_comma_list(delayed);
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  // will return singletons unwrapped
Packit Service 7770af
  Expression_Obj Parser::parse_comma_list(bool delayed)
Packit Service 7770af
  {
Packit Service 7770af
    // check if we have an empty list
Packit Service 7770af
    // return the empty list as such
Packit Service 7770af
    if (peek_css< alternatives <
Packit Service 7770af
          // exactly<'!'>,
Packit Service 7770af
          exactly<';'>,
Packit Service 7770af
          exactly<'}'>,
Packit Service 7770af
          exactly<'{'>,
Packit Service 7770af
          exactly<')'>,
Packit Service 7770af
          exactly<':'>,
Packit Service 7770af
          end_of_file,
Packit Service 7770af
          exactly<ellipsis>,
Packit Service 7770af
          default_flag,
Packit Service 7770af
          global_flag
Packit Service 7770af
        > >(position))
Packit Service 7770af
    {
Packit Service 7770af
      // return an empty list (nothing to delay)
Packit Service 7770af
      return SASS_MEMORY_NEW(List, pstate, 0);
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    // now try to parse a space list
Packit Service 7770af
    Expression_Obj list = parse_space_list();
Packit Service 7770af
    // if it's a singleton, return it (don't wrap it)
Packit Service 7770af
    if (!peek_css< exactly<','> >(position)) {
Packit Service 7770af
      // set_delay doesn't apply to list children
Packit Service 7770af
      // so this will only undelay single values
Packit Service 7770af
      if (!delayed) list->set_delayed(false);
Packit Service 7770af
      return list;
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    // if we got so far, we actually do have a comma list
Packit Service 7770af
    List_Obj comma_list = SASS_MEMORY_NEW(List, pstate, 2, SASS_COMMA);
Packit Service 7770af
    // wrap the first expression
Packit Service 7770af
    comma_list->append(list);
Packit Service 7770af
Packit Service 7770af
    while (lex_css< exactly<','> >())
Packit Service 7770af
    {
Packit Service 7770af
      // check for abort condition
Packit Service 7770af
      if (peek_css< alternatives <
Packit Service 7770af
            exactly<';'>,
Packit Service 7770af
            exactly<'}'>,
Packit Service 7770af
            exactly<'{'>,
Packit Service 7770af
            exactly<')'>,
Packit Service 7770af
            exactly<':'>,
Packit Service 7770af
            end_of_file,
Packit Service 7770af
            exactly<ellipsis>,
Packit Service 7770af
            default_flag,
Packit Service 7770af
            global_flag
Packit Service 7770af
          > >(position)
Packit Service 7770af
      ) { break; }
Packit Service 7770af
      // otherwise add another expression
Packit Service 7770af
      comma_list->append(parse_space_list());
Packit Service 7770af
    }
Packit Service 7770af
    // return the list
Packit Service 7770af
    return comma_list;
Packit Service 7770af
  }
Packit Service 7770af
  // EO parse_comma_list
Packit Service 7770af
Packit Service 7770af
  // will return singletons unwrapped
Packit Service 7770af
  Expression_Obj Parser::parse_space_list()
Packit Service 7770af
  {
Packit Service 7770af
    Expression_Obj disj1 = parse_disjunction();
Packit Service 7770af
    // if it's a singleton, return it (don't wrap it)
Packit Service 7770af
    if (peek_css< alternatives <
Packit Service 7770af
          // exactly<'!'>,
Packit Service 7770af
          exactly<';'>,
Packit Service 7770af
          exactly<'}'>,
Packit Service 7770af
          exactly<'{'>,
Packit Service 7770af
          exactly<')'>,
Packit Service 7770af
          exactly<','>,
Packit Service 7770af
          exactly<':'>,
Packit Service 7770af
          end_of_file,
Packit Service 7770af
          exactly<ellipsis>,
Packit Service 7770af
          default_flag,
Packit Service 7770af
          global_flag
Packit Service 7770af
        > >(position)
Packit Service 7770af
    ) { return disj1; }
Packit Service 7770af
Packit Service 7770af
    List_Obj space_list = SASS_MEMORY_NEW(List, pstate, 2, SASS_SPACE);
Packit Service 7770af
    space_list->append(disj1);
Packit Service 7770af
Packit Service 7770af
    while (!(peek_css< alternatives <
Packit Service 7770af
               // exactly<'!'>,
Packit Service 7770af
               exactly<';'>,
Packit Service 7770af
               exactly<'}'>,
Packit Service 7770af
               exactly<'{'>,
Packit Service 7770af
               exactly<')'>,
Packit Service 7770af
               exactly<','>,
Packit Service 7770af
               exactly<':'>,
Packit Service 7770af
               end_of_file,
Packit Service 7770af
               exactly<ellipsis>,
Packit Service 7770af
               default_flag,
Packit Service 7770af
               global_flag
Packit Service 7770af
           > >(position)) && peek_css< optional_css_whitespace >() != end
Packit Service 7770af
    ) {
Packit Service 7770af
      // the space is parsed implicitly?
Packit Service 7770af
      space_list->append(parse_disjunction());
Packit Service 7770af
    }
Packit Service 7770af
    // return the list
Packit Service 7770af
    return space_list;
Packit Service 7770af
  }
Packit Service 7770af
  // EO parse_space_list
Packit Service 7770af
Packit Service 7770af
  // parse logical OR operation
Packit Service 7770af
  Expression_Obj Parser::parse_disjunction()
Packit Service 7770af
  {
Packit Service 7770af
    advanceToNextToken();
Packit Service 7770af
    ParserState state(pstate);
Packit Service 7770af
    // parse the left hand side conjunction
Packit Service 7770af
    Expression_Obj conj = parse_conjunction();
Packit Service 7770af
    // parse multiple right hand sides
Packit Service 7770af
    std::vector<Expression_Obj> operands;
Packit Service 7770af
    while (lex_css< kwd_or >())
Packit Service 7770af
      operands.push_back(parse_conjunction());
Packit Service 7770af
    // if it's a singleton, return it directly
Packit Service 7770af
    if (operands.size() == 0) return conj;
Packit Service 7770af
    // fold all operands into one binary expression
Packit Service 7770af
    Expression_Obj ex = fold_operands(conj, operands, { Sass_OP::OR });
Packit Service 7770af
    state.offset = pstate - state + pstate.offset;
Packit Service 7770af
    ex->pstate(state);
Packit Service 7770af
    return ex;
Packit Service 7770af
  }
Packit Service 7770af
  // EO parse_disjunction
Packit Service 7770af
Packit Service 7770af
  // parse logical AND operation
Packit Service 7770af
  Expression_Obj Parser::parse_conjunction()
Packit Service 7770af
  {
Packit Service 7770af
    advanceToNextToken();
Packit Service 7770af
    ParserState state(pstate);
Packit Service 7770af
    // parse the left hand side relation
Packit Service 7770af
    Expression_Obj rel = parse_relation();
Packit Service 7770af
    // parse multiple right hand sides
Packit Service 7770af
    std::vector<Expression_Obj> operands;
Packit Service 7770af
    while (lex_css< kwd_and >()) {
Packit Service 7770af
      operands.push_back(parse_relation());
Packit Service 7770af
    }
Packit Service 7770af
    // if it's a singleton, return it directly
Packit Service 7770af
    if (operands.size() == 0) return rel;
Packit Service 7770af
    // fold all operands into one binary expression
Packit Service 7770af
    Expression_Obj ex = fold_operands(rel, operands, { Sass_OP::AND });
Packit Service 7770af
    state.offset = pstate - state + pstate.offset;
Packit Service 7770af
    ex->pstate(state);
Packit Service 7770af
    return ex;
Packit Service 7770af
  }
Packit Service 7770af
  // EO parse_conjunction
Packit Service 7770af
Packit Service 7770af
  // parse comparison operations
Packit Service 7770af
  Expression_Obj Parser::parse_relation()
Packit Service 7770af
  {
Packit Service 7770af
    advanceToNextToken();
Packit Service 7770af
    ParserState state(pstate);
Packit Service 7770af
    // parse the left hand side expression
Packit Service 7770af
    Expression_Obj lhs = parse_expression();
Packit Service 7770af
    std::vector<Expression_Obj> operands;
Packit Service 7770af
    std::vector<Operand> operators;
Packit Service 7770af
    // if it's a singleton, return it (don't wrap it)
Packit Service 7770af
    while (peek< alternatives <
Packit Service 7770af
            kwd_eq,
Packit Service 7770af
            kwd_neq,
Packit Service 7770af
            kwd_gte,
Packit Service 7770af
            kwd_gt,
Packit Service 7770af
            kwd_lte,
Packit Service 7770af
            kwd_lt
Packit Service 7770af
          > >(position))
Packit Service 7770af
    {
Packit Service 7770af
      // is directly adjancent to expression?
Packit Service 7770af
      bool left_ws = peek < css_comments >() != NULL;
Packit Service 7770af
      // parse the operator
Packit Service 7770af
      enum Sass_OP op
Packit Service 7770af
      = lex<kwd_eq>()  ? Sass_OP::EQ
Packit Service 7770af
      : lex<kwd_neq>() ? Sass_OP::NEQ
Packit Service 7770af
      : lex<kwd_gte>() ? Sass_OP::GTE
Packit Service 7770af
      : lex<kwd_lte>() ? Sass_OP::LTE
Packit Service 7770af
      : lex<kwd_gt>()  ? Sass_OP::GT
Packit Service 7770af
      : lex<kwd_lt>()  ? Sass_OP::LT
Packit Service 7770af
      // we checked the possibilities on top of fn
Packit Service 7770af
      :                  Sass_OP::EQ;
Packit Service 7770af
      // is directly adjacent to expression?
Packit Service 7770af
      bool right_ws = peek < css_comments >() != NULL;
Packit Service 7770af
      operators.push_back({ op, left_ws, right_ws });
Packit Service 7770af
      operands.push_back(parse_expression());
Packit Service 7770af
    }
Packit Service 7770af
    // we are called recursively for list, so we first
Packit Service 7770af
    // fold inner binary expression which has delayed
Packit Service 7770af
    // correctly set to zero. After folding we also unwrap
Packit Service 7770af
    // single nested items. So we cannot set delay on the
Packit Service 7770af
    // returned result here, as we have lost nestings ...
Packit Service 7770af
    Expression_Obj ex = fold_operands(lhs, operands, operators);
Packit Service 7770af
    state.offset = pstate - state + pstate.offset;
Packit Service 7770af
    ex->pstate(state);
Packit Service 7770af
    return ex;
Packit Service 7770af
  }
Packit Service 7770af
  // parse_relation
Packit Service 7770af
Packit Service 7770af
  // parse expression valid for operations
Packit Service 7770af
  // called from parse_relation
Packit Service 7770af
  // called from parse_for_directive
Packit Service 7770af
  // called from parse_media_expression
Packit Service 7770af
  // parse addition and subtraction operations
Packit Service 7770af
  Expression_Obj Parser::parse_expression()
Packit Service 7770af
  {
Packit Service 7770af
    advanceToNextToken();
Packit Service 7770af
    ParserState state(pstate);
Packit Service 7770af
    // parses multiple add and subtract operations
Packit Service 7770af
    // NOTE: make sure that identifiers starting with
Packit Service 7770af
    // NOTE: dashes do NOT count as subtract operation
Packit Service 7770af
    Expression_Obj lhs = parse_operators();
Packit Service 7770af
    // if it's a singleton, return it (don't wrap it)
Packit Service 7770af
    if (!(peek_css< exactly<'+'> >(position) ||
Packit Service 7770af
          // condition is a bit misterious, but some combinations should not be counted as operations
Packit Service 7770af
          (peek< no_spaces >(position) && peek< sequence< negate< unsigned_number >, exactly<'-'>, negate< space > > >(position)) ||
Packit Service 7770af
          (peek< sequence< negate< unsigned_number >, exactly<'-'>, negate< unsigned_number > > >(position))) ||
Packit Service 7770af
          peek< sequence < zero_plus < exactly <'-' > >, identifier > >(position))
Packit Service 7770af
    { return lhs; }
Packit Service 7770af
Packit Service 7770af
    std::vector<Expression_Obj> operands;
Packit Service 7770af
    std::vector<Operand> operators;
Packit Service 7770af
    bool left_ws = peek < css_comments >() != NULL;
Packit Service 7770af
    while (
Packit Service 7770af
      lex_css< exactly<'+'> >() ||
Packit Service 7770af
Packit Service 7770af
      (
Packit Service 7770af
      ! peek_css< sequence < zero_plus < exactly <'-' > >, identifier > >(position)
Packit Service 7770af
      && lex_css< sequence< negate< digit >, exactly<'-'> > >()
Packit Service 7770af
      )
Packit Service 7770af
Packit Service 7770af
    ) {
Packit Service 7770af
Packit Service 7770af
      bool right_ws = peek < css_comments >() != NULL;
Packit Service 7770af
      operators.push_back({ lexed.to_string() == "+" ? Sass_OP::ADD : Sass_OP::SUB, left_ws, right_ws });
Packit Service 7770af
      operands.push_back(parse_operators());
Packit Service 7770af
      left_ws = peek < css_comments >() != NULL;
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    if (operands.size() == 0) return lhs;
Packit Service 7770af
    Expression_Obj ex = fold_operands(lhs, operands, operators);
Packit Service 7770af
    state.offset = pstate - state + pstate.offset;
Packit Service 7770af
    ex->pstate(state);
Packit Service 7770af
    return ex;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  // parse addition and subtraction operations
Packit Service 7770af
  Expression_Obj Parser::parse_operators()
Packit Service 7770af
  {
Packit Service 7770af
    advanceToNextToken();
Packit Service 7770af
    ParserState state(pstate);
Packit Service 7770af
    Expression_Obj factor = parse_factor();
Packit Service 7770af
    // if it's a singleton, return it (don't wrap it)
Packit Service 7770af
    std::vector<Expression_Obj> operands; // factors
Packit Service 7770af
    std::vector<Operand> operators; // ops
Packit Service 7770af
    // lex operations to apply to lhs
Packit Service 7770af
    const char* left_ws = peek < css_comments >();
Packit Service 7770af
    while (lex_css< class_char< static_ops > >()) {
Packit Service 7770af
      const char* right_ws = peek < css_comments >();
Packit Service 7770af
      switch(*lexed.begin) {
Packit Service 7770af
        case '*': operators.push_back({ Sass_OP::MUL, left_ws != 0, right_ws != 0 }); break;
Packit Service 7770af
        case '/': operators.push_back({ Sass_OP::DIV, left_ws != 0, right_ws != 0 }); break;
Packit Service 7770af
        case '%': operators.push_back({ Sass_OP::MOD, left_ws != 0, right_ws != 0 }); break;
Packit Service 7770af
        default: throw std::runtime_error("unknown static op parsed");
Packit Service 7770af
      }
Packit Service 7770af
      operands.push_back(parse_factor());
Packit Service 7770af
      left_ws = peek < css_comments >();
Packit Service 7770af
    }
Packit Service 7770af
    // operands and operators to binary expression
Packit Service 7770af
    Expression_Obj ex = fold_operands(factor, operands, operators);
Packit Service 7770af
    state.offset = pstate - state + pstate.offset;
Packit Service 7770af
    ex->pstate(state);
Packit Service 7770af
    return ex;
Packit Service 7770af
  }
Packit Service 7770af
  // EO parse_operators
Packit Service 7770af
Packit Service 7770af
Packit Service 7770af
  // called from parse_operators
Packit Service 7770af
  // called from parse_value_schema
Packit Service 7770af
  Expression_Obj Parser::parse_factor()
Packit Service 7770af
  {
Packit Service 7770af
    lex < css_comments >(false);
Packit Service 7770af
    if (lex_css< exactly<'('> >()) {
Packit Service 7770af
      // parse_map may return a list
Packit Service 7770af
      Expression_Obj value = parse_map();
Packit Service 7770af
      // lex the expected closing parenthesis
Packit Service 7770af
      if (!lex_css< exactly<')'> >()) error("unclosed parenthesis", pstate);
Packit Service 7770af
      // expression can be evaluated
Packit Service 7770af
      return value;
Packit Service 7770af
    }
Packit Service 7770af
    // string may be interpolated
Packit Service 7770af
    // if (lex< quoted_string >()) {
Packit Service 7770af
    //   return &parse_string();
Packit Service 7770af
    // }
Packit Service 7770af
    else if (peek< ie_property >()) {
Packit Service 7770af
      return parse_ie_property();
Packit Service 7770af
    }
Packit Service 7770af
    else if (peek< ie_keyword_arg >()) {
Packit Service 7770af
      return parse_ie_keyword_arg();
Packit Service 7770af
    }
Packit Service 7770af
    else if (peek< sequence < calc_fn_call, exactly <'('> > >()) {
Packit Service 7770af
      return parse_calc_function();
Packit Service 7770af
    }
Packit Service 7770af
    else if (lex < functional_schema >()) {
Packit Service 7770af
      return parse_function_call_schema();
Packit Service 7770af
    }
Packit Service 7770af
    else if (lex< identifier_schema >()) {
Packit Service 7770af
      String_Obj string = parse_identifier_schema();
Packit Service 7770af
      if (String_Schema_Ptr schema = Cast<String_Schema>(string)) {
Packit Service 7770af
        if (lex < exactly < '(' > >()) {
Packit Service 7770af
          schema->append(parse_list());
Packit Service 7770af
          lex < exactly < ')' > >();
Packit Service 7770af
        }
Packit Service 7770af
      }
Packit Service 7770af
      return string;
Packit Service 7770af
    }
Packit Service 7770af
    else if (peek< sequence< uri_prefix, W, real_uri_value > >()) {
Packit Service 7770af
      return parse_url_function_string();
Packit Service 7770af
    }
Packit Service 7770af
    else if (peek< re_functional >()) {
Packit Service 7770af
      return parse_function_call();
Packit Service 7770af
    }
Packit Service 7770af
    else if (lex< exactly<'+'> >()) {
Packit Service 7770af
      Unary_Expression_Ptr ex = SASS_MEMORY_NEW(Unary_Expression, pstate, Unary_Expression::PLUS, parse_factor());
Packit Service 7770af
      if (ex && ex->operand()) ex->is_delayed(ex->operand()->is_delayed());
Packit Service 7770af
      return ex;
Packit Service 7770af
    }
Packit Service 7770af
    else if (lex< exactly<'-'> >()) {
Packit Service 7770af
      Unary_Expression_Ptr ex = SASS_MEMORY_NEW(Unary_Expression, pstate, Unary_Expression::MINUS, parse_factor());
Packit Service 7770af
      if (ex && ex->operand()) ex->is_delayed(ex->operand()->is_delayed());
Packit Service 7770af
      return ex;
Packit Service 7770af
    }
Packit Service 7770af
    else if (lex< exactly<'/'> >()) {
Packit Service 7770af
      Unary_Expression_Ptr ex = SASS_MEMORY_NEW(Unary_Expression, pstate, Unary_Expression::SLASH, parse_factor());
Packit Service 7770af
      if (ex && ex->operand()) ex->is_delayed(ex->operand()->is_delayed());
Packit Service 7770af
      return ex;
Packit Service 7770af
    }
Packit Service 7770af
    else if (lex< sequence< kwd_not > >()) {
Packit Service 7770af
      Unary_Expression_Ptr ex = SASS_MEMORY_NEW(Unary_Expression, pstate, Unary_Expression::NOT, parse_factor());
Packit Service 7770af
      if (ex && ex->operand()) ex->is_delayed(ex->operand()->is_delayed());
Packit Service 7770af
      return ex;
Packit Service 7770af
    }
Packit Service 7770af
    // this whole branch is never hit via spec tests
Packit Service 7770af
    else if (peek < sequence < one_plus < alternatives < css_whitespace, exactly<'-'>, exactly<'+'> > >, number > >()) {
Packit Service 7770af
      if (parse_number_prefix()) return parse_value(); // prefix is positive
Packit Service 7770af
      Unary_Expression_Ptr ex = SASS_MEMORY_NEW(Unary_Expression, pstate, Unary_Expression::MINUS, parse_value());
Packit Service 7770af
      if (ex->operand()) ex->is_delayed(ex->operand()->is_delayed());
Packit Service 7770af
      return ex;
Packit Service 7770af
    }
Packit Service 7770af
    else {
Packit Service 7770af
      return parse_value();
Packit Service 7770af
    }
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool number_has_zero(const std::string& parsed)
Packit Service 7770af
  {
Packit Service 7770af
    size_t L = parsed.length();
Packit Service 7770af
    return !( (L > 0 && parsed.substr(0, 1) == ".") ||
Packit Service 7770af
              (L > 1 && parsed.substr(0, 2) == "0.") ||
Packit Service 7770af
              (L > 1 && parsed.substr(0, 2) == "-.")  ||
Packit Service 7770af
              (L > 2 && parsed.substr(0, 3) == "-0.") );
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Number_Ptr Parser::lexed_number(const ParserState& pstate, const std::string& parsed)
Packit Service 7770af
  {
Packit Service 7770af
    Number_Ptr nr = SASS_MEMORY_NEW(Number,
Packit Service 7770af
                                    pstate,
Packit Service 7770af
                                    sass_atof(parsed.c_str()),
Packit Service 7770af
                                    "",
Packit Service 7770af
                                    number_has_zero(parsed));
Packit Service 7770af
    nr->is_interpolant(false);
Packit Service 7770af
    nr->is_delayed(true);
Packit Service 7770af
    return nr;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Number_Ptr Parser::lexed_percentage(const ParserState& pstate, const std::string& parsed)
Packit Service 7770af
  {
Packit Service 7770af
    Number_Ptr nr = SASS_MEMORY_NEW(Number,
Packit Service 7770af
                                    pstate,
Packit Service 7770af
                                    sass_atof(parsed.c_str()),
Packit Service 7770af
                                    "%",
Packit Service 7770af
                                    true);
Packit Service 7770af
    nr->is_interpolant(false);
Packit Service 7770af
    nr->is_delayed(true);
Packit Service 7770af
    return nr;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Number_Ptr Parser::lexed_dimension(const ParserState& pstate, const std::string& parsed)
Packit Service 7770af
  {
Packit Service 7770af
    size_t L = parsed.length();
Packit Service 7770af
    size_t num_pos = parsed.find_first_not_of(" \n\r\t");
Packit Service 7770af
    if (num_pos == std::string::npos) num_pos = L;
Packit Service 7770af
    size_t unit_pos = parsed.find_first_not_of("-+0123456789.", num_pos);
Packit Service 7770af
    if (unit_pos == std::string::npos) unit_pos = L;
Packit Service 7770af
    const std::string& num = parsed.substr(num_pos, unit_pos - num_pos);
Packit Service 7770af
    Number_Ptr nr = SASS_MEMORY_NEW(Number,
Packit Service 7770af
                                    pstate,
Packit Service 7770af
                                    sass_atof(num.c_str()),
Packit Service 7770af
                                    Token(number(parsed.c_str())),
Packit Service 7770af
                                    number_has_zero(parsed));
Packit Service 7770af
    nr->is_interpolant(false);
Packit Service 7770af
    nr->is_delayed(true);
Packit Service 7770af
    return nr;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Expression_Ptr Parser::lexed_hex_color(const ParserState& pstate, const std::string& parsed)
Packit Service 7770af
  {
Packit Service 7770af
    Color_Ptr color = NULL;
Packit Service 7770af
    if (parsed[0] != '#') {
Packit Service 7770af
      return SASS_MEMORY_NEW(String_Quoted, pstate, parsed);
Packit Service 7770af
    }
Packit Service 7770af
    // chop off the '#'
Packit Service 7770af
    std::string hext(parsed.substr(1));
Packit Service 7770af
    if (parsed.length() == 4) {
Packit Service 7770af
      std::string r(2, parsed[1]);
Packit Service 7770af
      std::string g(2, parsed[2]);
Packit Service 7770af
      std::string b(2, parsed[3]);
Packit Service 7770af
      color = SASS_MEMORY_NEW(Color,
Packit Service 7770af
                               pstate,
Packit Service 7770af
                               static_cast<double>(strtol(r.c_str(), NULL, 16)),
Packit Service 7770af
                               static_cast<double>(strtol(g.c_str(), NULL, 16)),
Packit Service 7770af
                               static_cast<double>(strtol(b.c_str(), NULL, 16)),
Packit Service 7770af
                               1, // alpha channel
Packit Service 7770af
                               parsed);
Packit Service 7770af
    }
Packit Service 7770af
    else if (parsed.length() == 7) {
Packit Service 7770af
      std::string r(parsed.substr(1,2));
Packit Service 7770af
      std::string g(parsed.substr(3,2));
Packit Service 7770af
      std::string b(parsed.substr(5,2));
Packit Service 7770af
      color = SASS_MEMORY_NEW(Color,
Packit Service 7770af
                               pstate,
Packit Service 7770af
                               static_cast<double>(strtol(r.c_str(), NULL, 16)),
Packit Service 7770af
                               static_cast<double>(strtol(g.c_str(), NULL, 16)),
Packit Service 7770af
                               static_cast<double>(strtol(b.c_str(), NULL, 16)),
Packit Service 7770af
                               1, // alpha channel
Packit Service 7770af
                               parsed);
Packit Service 7770af
    }
Packit Service 7770af
    else if (parsed.length() == 9) {
Packit Service 7770af
      std::string r(parsed.substr(1,2));
Packit Service 7770af
      std::string g(parsed.substr(3,2));
Packit Service 7770af
      std::string b(parsed.substr(5,2));
Packit Service 7770af
      std::string a(parsed.substr(7,2));
Packit Service 7770af
      color = SASS_MEMORY_NEW(Color,
Packit Service 7770af
                               pstate,
Packit Service 7770af
                               static_cast<double>(strtol(r.c_str(), NULL, 16)),
Packit Service 7770af
                               static_cast<double>(strtol(g.c_str(), NULL, 16)),
Packit Service 7770af
                               static_cast<double>(strtol(b.c_str(), NULL, 16)),
Packit Service 7770af
                               static_cast<double>(strtol(a.c_str(), NULL, 16)) / 255,
Packit Service 7770af
                               parsed);
Packit Service 7770af
    }
Packit Service 7770af
    color->is_interpolant(false);
Packit Service 7770af
    color->is_delayed(false);
Packit Service 7770af
    return color;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  // parse one value for a list
Packit Service 7770af
  Expression_Obj Parser::parse_value()
Packit Service 7770af
  {
Packit Service 7770af
    lex< css_comments >(false);
Packit Service 7770af
    if (lex< ampersand >())
Packit Service 7770af
    {
Packit Service 7770af
      return SASS_MEMORY_NEW(Parent_Selector, pstate); }
Packit Service 7770af
Packit Service 7770af
    if (lex< kwd_important >())
Packit Service 7770af
    { return SASS_MEMORY_NEW(String_Constant, pstate, "!important"); }
Packit Service 7770af
Packit Service 7770af
    // parse `10%4px` into separated items and not a schema
Packit Service 7770af
    if (lex< sequence < percentage, lookahead < number > > >())
Packit Service 7770af
    { return lexed_percentage(lexed); }
Packit Service 7770af
Packit Service 7770af
    if (lex< sequence < number, lookahead< sequence < op, number > > > >())
Packit Service 7770af
    { return lexed_number(lexed); }
Packit Service 7770af
Packit Service 7770af
    // string may be interpolated
Packit Service 7770af
    if (lex< sequence < quoted_string, lookahead < exactly <'-'> > > >())
Packit Service 7770af
    { return parse_string(); }
Packit Service 7770af
Packit Service 7770af
    if (const char* stop = peek< value_schema >())
Packit Service 7770af
    { return parse_value_schema(stop); }
Packit Service 7770af
Packit Service 7770af
    // string may be interpolated
Packit Service 7770af
    if (lex< quoted_string >())
Packit Service 7770af
    { return parse_string(); }
Packit Service 7770af
Packit Service 7770af
    if (lex< kwd_true >())
Packit Service 7770af
    { return SASS_MEMORY_NEW(Boolean, pstate, true); }
Packit Service 7770af
Packit Service 7770af
    if (lex< kwd_false >())
Packit Service 7770af
    { return SASS_MEMORY_NEW(Boolean, pstate, false); }
Packit Service 7770af
Packit Service 7770af
    if (lex< kwd_null >())
Packit Service 7770af
    { return SASS_MEMORY_NEW(Null, pstate); }
Packit Service 7770af
Packit Service 7770af
    if (lex< identifier >()) {
Packit Service 7770af
      return SASS_MEMORY_NEW(String_Constant, pstate, lexed);
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    if (lex< percentage >())
Packit Service 7770af
    { return lexed_percentage(lexed); }
Packit Service 7770af
Packit Service 7770af
    // match hex number first because 0x000 looks like a number followed by an identifier
Packit Service 7770af
    if (lex< sequence < alternatives< hex, hex0 >, negate < exactly<'-'> > > >())
Packit Service 7770af
    { return lexed_hex_color(lexed); }
Packit Service 7770af
Packit Service 7770af
    if (lex< sequence < exactly <'#'>, identifier > >())
Packit Service 7770af
    { return SASS_MEMORY_NEW(String_Quoted, pstate, lexed); }
Packit Service 7770af
Packit Service 7770af
    // also handle the 10em- foo special case
Packit Service 7770af
    // alternatives < exactly < '.' >, .. > -- `1.5em-.75em` is split into a list, not a binary expression
Packit Service 7770af
    if (lex< sequence< dimension, optional< sequence< exactly<'-'>, lookahead< alternatives < space > > > > > >())
Packit Service 7770af
    { return lexed_dimension(lexed); }
Packit Service 7770af
Packit Service 7770af
    if (lex< sequence< static_component, one_plus< strict_identifier > > >())
Packit Service 7770af
    { return SASS_MEMORY_NEW(String_Constant, pstate, lexed); }
Packit Service 7770af
Packit Service 7770af
    if (lex< number >())
Packit Service 7770af
    { return lexed_number(lexed); }
Packit Service 7770af
Packit Service 7770af
    if (lex< variable >())
Packit Service 7770af
    { return SASS_MEMORY_NEW(Variable, pstate, Util::normalize_underscores(lexed)); }
Packit Service 7770af
Packit Service 7770af
    // Special case handling for `%` proceeding an interpolant.
Packit Service 7770af
    if (lex< sequence< exactly<'%'>, optional< percentage > > >())
Packit Service 7770af
    { return SASS_MEMORY_NEW(String_Constant, pstate, lexed); }
Packit Service 7770af
Packit Service 7770af
    css_error("Invalid CSS", " after ", ": expected expression (e.g. 1px, bold), was ");
Packit Service 7770af
Packit Service 7770af
    // unreachable statement
Packit Service 7770af
    return 0;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  // this parses interpolation inside other strings
Packit Service 7770af
  // means the result should later be quoted again
Packit Service 7770af
  String_Obj Parser::parse_interpolated_chunk(Token chunk, bool constant)
Packit Service 7770af
  {
Packit Service 7770af
    const char* i = chunk.begin;
Packit Service 7770af
    // see if there any interpolants
Packit Service 7770af
    const char* p = constant ? find_first_in_interval< exactly<hash_lbrace> >(i, chunk.end) :
Packit Service 7770af
                    find_first_in_interval< exactly<hash_lbrace>, block_comment >(i, chunk.end);
Packit Service 7770af
Packit Service 7770af
    if (!p) {
Packit Service 7770af
      String_Quoted_Ptr str_quoted = SASS_MEMORY_NEW(String_Quoted, pstate, std::string(i, chunk.end));
Packit Service 7770af
      if (!constant && str_quoted->quote_mark()) str_quoted->quote_mark('*');
Packit Service 7770af
      return str_quoted;
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    String_Schema_Obj schema = SASS_MEMORY_NEW(String_Schema, pstate);
Packit Service 7770af
    schema->is_interpolant(true);
Packit Service 7770af
    while (i < chunk.end) {
Packit Service 7770af
      p = constant ? find_first_in_interval< exactly<hash_lbrace> >(i, chunk.end) :
Packit Service 7770af
          find_first_in_interval< exactly<hash_lbrace>, block_comment >(i, chunk.end);
Packit Service 7770af
      if (p) {
Packit Service 7770af
        if (i < p) {
Packit Service 7770af
          // accumulate the preceding segment if it's nonempty
Packit Service 7770af
          schema->append(SASS_MEMORY_NEW(String_Constant, pstate, std::string(i, p)));
Packit Service 7770af
        }
Packit Service 7770af
        // we need to skip anything inside strings
Packit Service 7770af
        // create a new target in parser/prelexer
Packit Service 7770af
        if (peek < sequence < optional_spaces, exactly<rbrace> > >(p+2)) { position = p+2;
Packit Service 7770af
          css_error("Invalid CSS", " after ", ": expected expression (e.g. 1px, bold), was ");
Packit Service 7770af
        }
Packit Service 7770af
        const char* j = skip_over_scopes< exactly<hash_lbrace>, exactly<rbrace> >(p + 2, chunk.end); // find the closing brace
Packit Service 7770af
        if (j) { --j;
Packit Service 7770af
          // parse the interpolant and accumulate it
Packit Service 7770af
          Expression_Obj interp_node = Parser::from_token(Token(p+2, j), ctx, pstate, source).parse_list();
Packit Service 7770af
          interp_node->is_interpolant(true);
Packit Service 7770af
          schema->append(interp_node);
Packit Service 7770af
          i = j;
Packit Service 7770af
        }
Packit Service 7770af
        else {
Packit Service 7770af
          // throw an error if the interpolant is unterminated
Packit Service 7770af
          error("unterminated interpolant inside string constant " + chunk.to_string(), pstate);
Packit Service 7770af
        }
Packit Service 7770af
      }
Packit Service 7770af
      else { // no interpolants left; add the last segment if nonempty
Packit Service 7770af
        // check if we need quotes here (was not sure after merge)
Packit Service 7770af
        if (i < chunk.end) schema->append(SASS_MEMORY_NEW(String_Constant, pstate, std::string(i, chunk.end)));
Packit Service 7770af
        break;
Packit Service 7770af
      }
Packit Service 7770af
      ++ i;
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    return schema.detach();
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  String_Constant_Obj Parser::parse_static_value()
Packit Service 7770af
  {
Packit Service 7770af
    lex< static_value >();
Packit Service 7770af
    Token str(lexed);
Packit Service 7770af
    // static values always have trailing white-
Packit Service 7770af
    // space and end delimiter (\s*[;]$) included
Packit Service 7770af
    -- pstate.offset.column;
Packit Service 7770af
    --str.end;
Packit Service 7770af
    --position;
Packit Service 7770af
Packit Service 7770af
    String_Constant_Ptr str_node = SASS_MEMORY_NEW(String_Constant, pstate, str.time_wspace());
Packit Service 7770af
    return str_node;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  String_Obj Parser::parse_string()
Packit Service 7770af
  {
Packit Service 7770af
    return parse_interpolated_chunk(Token(lexed));
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  String_Obj Parser::parse_ie_property()
Packit Service 7770af
  {
Packit Service 7770af
    lex< ie_property >();
Packit Service 7770af
    Token str(lexed);
Packit Service 7770af
    const char* i = str.begin;
Packit Service 7770af
    // see if there any interpolants
Packit Service 7770af
    const char* p = find_first_in_interval< exactly<hash_lbrace>, block_comment >(str.begin, str.end);
Packit Service 7770af
    if (!p) {
Packit Service 7770af
      return SASS_MEMORY_NEW(String_Quoted, pstate, std::string(str.begin, str.end));
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    String_Schema_Ptr schema = SASS_MEMORY_NEW(String_Schema, pstate);
Packit Service 7770af
    while (i < str.end) {
Packit Service 7770af
      p = find_first_in_interval< exactly<hash_lbrace>, block_comment >(i, str.end);
Packit Service 7770af
      if (p) {
Packit Service 7770af
        if (i < p) {
Packit Service 7770af
          schema->append(SASS_MEMORY_NEW(String_Constant, pstate, std::string(i, p))); // accumulate the preceding segment if it's nonempty
Packit Service 7770af
        }
Packit Service 7770af
        if (peek < sequence < optional_spaces, exactly<rbrace> > >(p+2)) { position = p+2;
Packit Service 7770af
          css_error("Invalid CSS", " after ", ": expected expression (e.g. 1px, bold), was ");
Packit Service 7770af
        }
Packit Service 7770af
        const char* j = skip_over_scopes< exactly<hash_lbrace>, exactly<rbrace> >(p+2, str.end); // find the closing brace
Packit Service 7770af
        if (j) {
Packit Service 7770af
          // parse the interpolant and accumulate it
Packit Service 7770af
          Expression_Obj interp_node = Parser::from_token(Token(p+2, j), ctx, pstate, source).parse_list();
Packit Service 7770af
          interp_node->is_interpolant(true);
Packit Service 7770af
          schema->append(interp_node);
Packit Service 7770af
          i = j;
Packit Service 7770af
        }
Packit Service 7770af
        else {
Packit Service 7770af
          // throw an error if the interpolant is unterminated
Packit Service 7770af
          error("unterminated interpolant inside IE function " + str.to_string(), pstate);
Packit Service 7770af
        }
Packit Service 7770af
      }
Packit Service 7770af
      else { // no interpolants left; add the last segment if nonempty
Packit Service 7770af
        if (i < str.end) {
Packit Service 7770af
          schema->append(SASS_MEMORY_NEW(String_Constant, pstate, std::string(i, str.end)));
Packit Service 7770af
        }
Packit Service 7770af
        break;
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
    return schema;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  String_Obj Parser::parse_ie_keyword_arg()
Packit Service 7770af
  {
Packit Service 7770af
    String_Schema_Ptr kwd_arg = SASS_MEMORY_NEW(String_Schema, pstate, 3);
Packit Service 7770af
    if (lex< variable >()) {
Packit Service 7770af
      kwd_arg->append(SASS_MEMORY_NEW(Variable, pstate, Util::normalize_underscores(lexed)));
Packit Service 7770af
    } else {
Packit Service 7770af
      lex< alternatives< identifier_schema, identifier > >();
Packit Service 7770af
      kwd_arg->append(SASS_MEMORY_NEW(String_Constant, pstate, lexed));
Packit Service 7770af
    }
Packit Service 7770af
    lex< exactly<'='> >();
Packit Service 7770af
    kwd_arg->append(SASS_MEMORY_NEW(String_Constant, pstate, lexed));
Packit Service 7770af
    if (peek< variable >()) kwd_arg->append(parse_list());
Packit Service 7770af
    else if (lex< number >()) {
Packit Service 7770af
      std::string parsed(lexed);
Packit Service 7770af
      Util::normalize_decimals(parsed);
Packit Service 7770af
      kwd_arg->append(lexed_number(parsed));
Packit Service 7770af
    }
Packit Service 7770af
    else if (peek < ie_keyword_arg_value >()) { kwd_arg->append(parse_list()); }
Packit Service 7770af
    return kwd_arg;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  String_Schema_Obj Parser::parse_value_schema(const char* stop)
Packit Service 7770af
  {
Packit Service 7770af
    // initialize the string schema object to add tokens
Packit Service 7770af
    String_Schema_Obj schema = SASS_MEMORY_NEW(String_Schema, pstate);
Packit Service 7770af
Packit Service 7770af
    if (peek<exactly<'}'>>()) {
Packit Service 7770af
      css_error("Invalid CSS", " after ", ": expected expression (e.g. 1px, bold), was ");
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    const char* e;
Packit Service 7770af
    const char* ee = end;
Packit Service 7770af
    end = stop;
Packit Service 7770af
    size_t num_items = 0;
Packit Service 7770af
    bool need_space = false;
Packit Service 7770af
    while (position < stop) {
Packit Service 7770af
      // parse space between tokens
Packit Service 7770af
      if (lex< spaces >() && num_items) {
Packit Service 7770af
        need_space = true;
Packit Service 7770af
      }
Packit Service 7770af
      if (need_space) {
Packit Service 7770af
        need_space = false;
Packit Service 7770af
        // schema->append(SASS_MEMORY_NEW(String_Constant, pstate, " "));
Packit Service 7770af
      }
Packit Service 7770af
      if ((e = peek< re_functional >()) && e < stop) {
Packit Service 7770af
        schema->append(parse_function_call());
Packit Service 7770af
      }
Packit Service 7770af
      // lex an interpolant /#{...}/
Packit Service 7770af
      else if (lex< exactly < hash_lbrace > >()) {
Packit Service 7770af
        // Try to lex static expression first
Packit Service 7770af
        if (peek< exactly< rbrace > >()) {
Packit Service 7770af
          css_error("Invalid CSS", " after ", ": expected expression (e.g. 1px, bold), was ");
Packit Service 7770af
        }
Packit Service 7770af
        Expression_Obj ex;
Packit Service 7770af
        if (lex< re_static_expression >()) {
Packit Service 7770af
          ex = SASS_MEMORY_NEW(String_Constant, pstate, lexed);
Packit Service 7770af
        } else {
Packit Service 7770af
          ex = parse_list();
Packit Service 7770af
        }
Packit Service 7770af
        ex->is_interpolant(true);
Packit Service 7770af
        schema->append(ex);
Packit Service 7770af
        if (!lex < exactly < rbrace > >()) {
Packit Service 7770af
          css_error("Invalid CSS", " after ", ": expected \"}\", was ");
Packit Service 7770af
        }
Packit Service 7770af
      }
Packit Service 7770af
      // lex some string constants or other valid token
Packit Service 7770af
      // Note: [-+] chars are left over from i.e. `#{3}+3`
Packit Service 7770af
      else if (lex< alternatives < exactly<'%'>, exactly < '-' >, exactly < '+' > > >()) {
Packit Service 7770af
        schema->append(SASS_MEMORY_NEW(String_Constant, pstate, lexed));
Packit Service 7770af
      }
Packit Service 7770af
      // lex a quoted string
Packit Service 7770af
      else if (lex< quoted_string >()) {
Packit Service 7770af
        // need_space = true;
Packit Service 7770af
        // if (schema->length()) schema->append(SASS_MEMORY_NEW(String_Constant, pstate, " "));
Packit Service 7770af
        // else need_space = true;
Packit Service 7770af
        schema->append(parse_string());
Packit Service 7770af
        if ((*position == '"' || *position == '\'') || peek < alternatives < alpha > >()) {
Packit Service 7770af
          // need_space = true;
Packit Service 7770af
        }
Packit Service 7770af
        if (peek < exactly < '-' > >()) break;
Packit Service 7770af
      }
Packit Service 7770af
      else if (lex< sequence < identifier > >()) {
Packit Service 7770af
        schema->append(SASS_MEMORY_NEW(String_Constant, pstate, lexed));
Packit Service 7770af
        if ((*position == '"' || *position == '\'') || peek < alternatives < alpha > >()) {
Packit Service 7770af
           // need_space = true;
Packit Service 7770af
        }
Packit Service 7770af
      }
Packit Service 7770af
      // lex (normalized) variable
Packit Service 7770af
      else if (lex< variable >()) {
Packit Service 7770af
        std::string name(Util::normalize_underscores(lexed));
Packit Service 7770af
        schema->append(SASS_MEMORY_NEW(Variable, pstate, name));
Packit Service 7770af
      }
Packit Service 7770af
      // lex percentage value
Packit Service 7770af
      else if (lex< percentage >()) {
Packit Service 7770af
        schema->append(lexed_percentage(lexed));
Packit Service 7770af
      }
Packit Service 7770af
      // lex dimension value
Packit Service 7770af
      else if (lex< dimension >()) {
Packit Service 7770af
        schema->append(lexed_dimension(lexed));
Packit Service 7770af
      }
Packit Service 7770af
      // lex number value
Packit Service 7770af
      else if (lex< number >()) {
Packit Service 7770af
        schema->append(lexed_number(lexed));
Packit Service 7770af
      }
Packit Service 7770af
      // lex hex color value
Packit Service 7770af
      else if (lex< sequence < hex, negate < exactly < '-' > > > >()) {
Packit Service 7770af
        schema->append(lexed_hex_color(lexed));
Packit Service 7770af
      }
Packit Service 7770af
      else if (lex< sequence < exactly <'#'>, identifier > >()) {
Packit Service 7770af
        schema->append(SASS_MEMORY_NEW(String_Quoted, pstate, lexed));
Packit Service 7770af
      }
Packit Service 7770af
      // lex a value in parentheses
Packit Service 7770af
      else if (peek< parenthese_scope >()) {
Packit Service 7770af
        schema->append(parse_factor());
Packit Service 7770af
      }
Packit Service 7770af
      else {
Packit Service 7770af
        break;
Packit Service 7770af
      }
Packit Service 7770af
      ++num_items;
Packit Service 7770af
    }
Packit Service 7770af
    if (position != stop) {
Packit Service 7770af
      schema->append(SASS_MEMORY_NEW(String_Constant, pstate, std::string(position, stop)));
Packit Service 7770af
      position = stop;
Packit Service 7770af
    }
Packit Service 7770af
    end = ee;
Packit Service 7770af
    return schema;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  // this parses interpolation outside other strings
Packit Service 7770af
  // means the result must not be quoted again later
Packit Service 7770af
  String_Obj Parser::parse_identifier_schema()
Packit Service 7770af
  {
Packit Service 7770af
    Token id(lexed);
Packit Service 7770af
    const char* i = id.begin;
Packit Service 7770af
    // see if there any interpolants
Packit Service 7770af
    const char* p = find_first_in_interval< exactly<hash_lbrace>, block_comment >(id.begin, id.end);
Packit Service 7770af
    if (!p) {
Packit Service 7770af
      return SASS_MEMORY_NEW(String_Constant, pstate, std::string(id.begin, id.end));
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    String_Schema_Obj schema = SASS_MEMORY_NEW(String_Schema, pstate);
Packit Service 7770af
    while (i < id.end) {
Packit Service 7770af
      p = find_first_in_interval< exactly<hash_lbrace>, block_comment >(i, id.end);
Packit Service 7770af
      if (p) {
Packit Service 7770af
        if (i < p) {
Packit Service 7770af
          // accumulate the preceding segment if it's nonempty
Packit Service 7770af
          const char* o = position; position = i;
Packit Service 7770af
          schema->append(parse_value_schema(p));
Packit Service 7770af
          position = o;
Packit Service 7770af
        }
Packit Service 7770af
        // we need to skip anything inside strings
Packit Service 7770af
        // create a new target in parser/prelexer
Packit Service 7770af
        if (peek < sequence < optional_spaces, exactly<rbrace> > >(p+2)) { position = p;
Packit Service 7770af
          css_error("Invalid CSS", " after ", ": expected expression (e.g. 1px, bold), was ");
Packit Service 7770af
        }
Packit Service 7770af
        const char* j = skip_over_scopes< exactly<hash_lbrace>, exactly<rbrace> >(p+2, id.end); // find the closing brace
Packit Service 7770af
        if (j) {
Packit Service 7770af
          // parse the interpolant and accumulate it
Packit Service 7770af
          Expression_Obj interp_node = Parser::from_token(Token(p+2, j), ctx, pstate, source).parse_list(DELAYED);
Packit Service 7770af
          interp_node->is_interpolant(true);
Packit Service 7770af
          schema->append(interp_node);
Packit Service 7770af
          // schema->has_interpolants(true);
Packit Service 7770af
          i = j;
Packit Service 7770af
        }
Packit Service 7770af
        else {
Packit Service 7770af
          // throw an error if the interpolant is unterminated
Packit Service 7770af
          error("unterminated interpolant inside interpolated identifier " + id.to_string(), pstate);
Packit Service 7770af
        }
Packit Service 7770af
      }
Packit Service 7770af
      else { // no interpolants left; add the last segment if nonempty
Packit Service 7770af
        if (i < end) {
Packit Service 7770af
          const char* o = position; position = i;
Packit Service 7770af
          schema->append(parse_value_schema(id.end));
Packit Service 7770af
          position = o;
Packit Service 7770af
        }
Packit Service 7770af
        break;
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
    return schema ? schema.detach() : 0;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  // calc functions should preserve arguments
Packit Service 7770af
  Function_Call_Obj Parser::parse_calc_function()
Packit Service 7770af
  {
Packit Service 7770af
    lex< identifier >();
Packit Service 7770af
    std::string name(lexed);
Packit Service 7770af
    ParserState call_pos = pstate;
Packit Service 7770af
    lex< exactly<'('> >();
Packit Service 7770af
    ParserState arg_pos = pstate;
Packit Service 7770af
    const char* arg_beg = position;
Packit Service 7770af
    parse_list();
Packit Service 7770af
    const char* arg_end = position;
Packit Service 7770af
    lex< skip_over_scopes <
Packit Service 7770af
          exactly < '(' >,
Packit Service 7770af
          exactly < ')' >
Packit Service 7770af
        > >();
Packit Service 7770af
Packit Service 7770af
    Argument_Obj arg = SASS_MEMORY_NEW(Argument, arg_pos, parse_interpolated_chunk(Token(arg_beg, arg_end)));
Packit Service 7770af
    Arguments_Obj args = SASS_MEMORY_NEW(Arguments, arg_pos);
Packit Service 7770af
    args->append(arg);
Packit Service 7770af
    return SASS_MEMORY_NEW(Function_Call, call_pos, name, args);
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  String_Obj Parser::parse_url_function_string()
Packit Service 7770af
  {
Packit Service 7770af
    std::string prefix("");
Packit Service 7770af
    if (lex< uri_prefix >()) {
Packit Service 7770af
      prefix = std::string(lexed);
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    lex < optional_spaces >();
Packit Service 7770af
    String_Obj url_string = parse_url_function_argument();
Packit Service 7770af
Packit Service 7770af
    std::string suffix("");
Packit Service 7770af
    if (lex< real_uri_suffix >()) {
Packit Service 7770af
      suffix = std::string(lexed);
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    std::string uri("");
Packit Service 7770af
    if (url_string) {
Packit Service 7770af
      uri = url_string->to_string({ NESTED, 5 });
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    if (String_Schema_Ptr schema = Cast<String_Schema>(url_string)) {
Packit Service 7770af
      String_Schema_Obj res = SASS_MEMORY_NEW(String_Schema, pstate);
Packit Service 7770af
      res->append(SASS_MEMORY_NEW(String_Constant, pstate, prefix));
Packit Service 7770af
      res->append(schema);
Packit Service 7770af
      res->append(SASS_MEMORY_NEW(String_Constant, pstate, suffix));
Packit Service 7770af
      return res;
Packit Service 7770af
    } else {
Packit Service 7770af
      std::string res = prefix + uri + suffix;
Packit Service 7770af
      return SASS_MEMORY_NEW(String_Constant, pstate, res);
Packit Service 7770af
    }
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  String_Obj Parser::parse_url_function_argument()
Packit Service 7770af
  {
Packit Service 7770af
    const char* p = position;
Packit Service 7770af
Packit Service 7770af
    std::string uri("");
Packit Service 7770af
    if (lex< real_uri_value >(false)) {
Packit Service 7770af
      uri = lexed.to_string();
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    if (peek< exactly< hash_lbrace > >()) {
Packit Service 7770af
      const char* pp = position;
Packit Service 7770af
      // TODO: error checking for unclosed interpolants
Packit Service 7770af
      while (pp && peek< exactly< hash_lbrace > >(pp)) {
Packit Service 7770af
        pp = sequence< interpolant, real_uri_value >(pp);
Packit Service 7770af
      }
Packit Service 7770af
      position = pp;
Packit Service 7770af
      return parse_interpolated_chunk(Token(p, position));
Packit Service 7770af
    }
Packit Service 7770af
    else if (uri != "") {
Packit Service 7770af
      std::string res = Util::rtrim(uri);
Packit Service 7770af
      return SASS_MEMORY_NEW(String_Constant, pstate, res);
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    return 0;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Function_Call_Obj Parser::parse_function_call()
Packit Service 7770af
  {
Packit Service 7770af
    lex< identifier >();
Packit Service 7770af
    std::string name(lexed);
Packit Service 7770af
Packit Service 7770af
    ParserState call_pos = pstate;
Packit Service 7770af
    Arguments_Obj args = parse_arguments();
Packit Service 7770af
    return SASS_MEMORY_NEW(Function_Call, call_pos, name, args);
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Function_Call_Schema_Obj Parser::parse_function_call_schema()
Packit Service 7770af
  {
Packit Service 7770af
    String_Obj name = parse_identifier_schema();
Packit Service 7770af
    ParserState source_position_of_call = pstate;
Packit Service 7770af
    Arguments_Obj args = parse_arguments();
Packit Service 7770af
Packit Service 7770af
    return SASS_MEMORY_NEW(Function_Call_Schema, source_position_of_call, name, args);
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Content_Obj Parser::parse_content_directive()
Packit Service 7770af
  {
Packit Service 7770af
    return SASS_MEMORY_NEW(Content, pstate);
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  If_Obj Parser::parse_if_directive(bool else_if)
Packit Service 7770af
  {
Packit Service 7770af
    stack.push_back(Scope::Control);
Packit Service 7770af
    ParserState if_source_position = pstate;
Packit Service 7770af
    bool root = block_stack.back()->is_root();
Packit Service 7770af
    Expression_Obj predicate = parse_list();
Packit Service 7770af
    Block_Obj block = parse_block(root);
Packit Service 7770af
    Block_Obj alternative = NULL;
Packit Service 7770af
Packit Service 7770af
    // only throw away comment if we parse a case
Packit Service 7770af
    // we want all other comments to be parsed
Packit Service 7770af
    if (lex_css< elseif_directive >()) {
Packit Service 7770af
      alternative = SASS_MEMORY_NEW(Block, pstate);
Packit Service 7770af
      alternative->append(parse_if_directive(true));
Packit Service 7770af
    }
Packit Service 7770af
    else if (lex_css< kwd_else_directive >()) {
Packit Service 7770af
      alternative = parse_block(root);
Packit Service 7770af
    }
Packit Service 7770af
    stack.pop_back();
Packit Service 7770af
    return SASS_MEMORY_NEW(If, if_source_position, predicate, block, alternative);
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  For_Obj Parser::parse_for_directive()
Packit Service 7770af
  {
Packit Service 7770af
    stack.push_back(Scope::Control);
Packit Service 7770af
    ParserState for_source_position = pstate;
Packit Service 7770af
    bool root = block_stack.back()->is_root();
Packit Service 7770af
    lex_variable();
Packit Service 7770af
    std::string var(Util::normalize_underscores(lexed));
Packit Service 7770af
    if (!lex< kwd_from >()) error("expected 'from' keyword in @for directive", pstate);
Packit Service 7770af
    Expression_Obj lower_bound = parse_expression();
Packit Service 7770af
    bool inclusive = false;
Packit Service 7770af
    if (lex< kwd_through >()) inclusive = true;
Packit Service 7770af
    else if (lex< kwd_to >()) inclusive = false;
Packit Service 7770af
    else                  error("expected 'through' or 'to' keyword in @for directive", pstate);
Packit Service 7770af
    Expression_Obj upper_bound = parse_expression();
Packit Service 7770af
    Block_Obj body = parse_block(root);
Packit Service 7770af
    stack.pop_back();
Packit Service 7770af
    return SASS_MEMORY_NEW(For, for_source_position, var, lower_bound, upper_bound, body, inclusive);
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  // helper to parse a var token
Packit Service 7770af
  Token Parser::lex_variable()
Packit Service 7770af
  {
Packit Service 7770af
    // peek for dollar sign first
Packit Service 7770af
    if (!peek< exactly <'$'> >()) {
Packit Service 7770af
      css_error("Invalid CSS", " after ", ": expected \"$\", was ");
Packit Service 7770af
    }
Packit Service 7770af
    // we expect a simple identifier as the call name
Packit Service 7770af
    if (!lex< sequence < exactly <'$'>, identifier > >()) {
Packit Service 7770af
      lex< exactly <'$'> >(); // move pstate and position up
Packit Service 7770af
      css_error("Invalid CSS", " after ", ": expected identifier, was ");
Packit Service 7770af
    }
Packit Service 7770af
    // return object
Packit Service 7770af
    return token;
Packit Service 7770af
  }
Packit Service 7770af
  // helper to parse identifier
Packit Service 7770af
  Token Parser::lex_identifier()
Packit Service 7770af
  {
Packit Service 7770af
    // we expect a simple identifier as the call name
Packit Service 7770af
    if (!lex< identifier >()) { // ToDo: pstate wrong?
Packit Service 7770af
      css_error("Invalid CSS", " after ", ": expected identifier, was ");
Packit Service 7770af
    }
Packit Service 7770af
    // return object
Packit Service 7770af
    return token;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Each_Obj Parser::parse_each_directive()
Packit Service 7770af
  {
Packit Service 7770af
    stack.push_back(Scope::Control);
Packit Service 7770af
    ParserState each_source_position = pstate;
Packit Service 7770af
    bool root = block_stack.back()->is_root();
Packit Service 7770af
    std::vector<std::string> vars;
Packit Service 7770af
    lex_variable();
Packit Service 7770af
    vars.push_back(Util::normalize_underscores(lexed));
Packit Service 7770af
    while (lex< exactly<','> >()) {
Packit Service 7770af
      if (!lex< variable >()) error("@each directive requires an iteration variable", pstate);
Packit Service 7770af
      vars.push_back(Util::normalize_underscores(lexed));
Packit Service 7770af
    }
Packit Service 7770af
    if (!lex< kwd_in >()) error("expected 'in' keyword in @each directive", pstate);
Packit Service 7770af
    Expression_Obj list = parse_list();
Packit Service 7770af
    Block_Obj body = parse_block(root);
Packit Service 7770af
    stack.pop_back();
Packit Service 7770af
    return SASS_MEMORY_NEW(Each, each_source_position, vars, list, body);
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  // called after parsing `kwd_while_directive`
Packit Service 7770af
  While_Obj Parser::parse_while_directive()
Packit Service 7770af
  {
Packit Service 7770af
    stack.push_back(Scope::Control);
Packit Service 7770af
    bool root = block_stack.back()->is_root();
Packit Service 7770af
    // create the initial while call object
Packit Service 7770af
    While_Obj call = SASS_MEMORY_NEW(While, pstate, 0, 0);
Packit Service 7770af
    // parse mandatory predicate
Packit Service 7770af
    Expression_Obj predicate = parse_list();
Packit Service 7770af
    List_Obj l = Cast<List>(predicate);
Packit Service 7770af
    if (!predicate || (l && !l->length())) {
Packit Service 7770af
      css_error("Invalid CSS", " after ", ": expected expression (e.g. 1px, bold), was ", false);
Packit Service 7770af
    }
Packit Service 7770af
    call->predicate(predicate);
Packit Service 7770af
    // parse mandatory block
Packit Service 7770af
    call->block(parse_block(root));
Packit Service 7770af
    // return ast node
Packit Service 7770af
    stack.pop_back();
Packit Service 7770af
    // return ast node
Packit Service 7770af
    return call.detach();
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  // EO parse_while_directive
Packit Service 7770af
  Media_Block_Obj Parser::parse_media_block()
Packit Service 7770af
  {
Packit Service 7770af
    stack.push_back(Scope::Media);
Packit Service 7770af
    Media_Block_Obj media_block = SASS_MEMORY_NEW(Media_Block, pstate, 0, 0);
Packit Service 7770af
Packit Service 7770af
    media_block->media_queries(parse_media_queries());
Packit Service 7770af
Packit Service 7770af
    Media_Block_Obj prev_media_block = last_media_block;
Packit Service 7770af
    last_media_block = media_block;
Packit Service 7770af
    media_block->block(parse_css_block());
Packit Service 7770af
    last_media_block = prev_media_block;
Packit Service 7770af
    stack.pop_back();
Packit Service 7770af
    return media_block.detach();
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  List_Obj Parser::parse_media_queries()
Packit Service 7770af
  {
Packit Service 7770af
    advanceToNextToken();
Packit Service 7770af
    List_Obj queries = SASS_MEMORY_NEW(List, pstate, 0, SASS_COMMA);
Packit Service 7770af
    if (!peek_css < exactly <'{'> >()) queries->append(parse_media_query());
Packit Service 7770af
    while (lex_css < exactly <','> >()) queries->append(parse_media_query());
Packit Service 7770af
    queries->update_pstate(pstate);
Packit Service 7770af
    return queries.detach();
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  // Expression_Ptr Parser::parse_media_query()
Packit Service 7770af
  Media_Query_Obj Parser::parse_media_query()
Packit Service 7770af
  {
Packit Service 7770af
    advanceToNextToken();
Packit Service 7770af
    Media_Query_Obj media_query = SASS_MEMORY_NEW(Media_Query, pstate);
Packit Service 7770af
    if (lex < kwd_not >()) { media_query->is_negated(true); lex < css_comments >(false); }
Packit Service 7770af
    else if (lex < kwd_only >()) { media_query->is_restricted(true); lex < css_comments >(false); }
Packit Service 7770af
Packit Service 7770af
    if (lex < identifier_schema >())  media_query->media_type(parse_identifier_schema());
Packit Service 7770af
    else if (lex < identifier >())    media_query->media_type(parse_interpolated_chunk(lexed));
Packit Service 7770af
    else                             media_query->append(parse_media_expression());
Packit Service 7770af
Packit Service 7770af
    while (lex_css < kwd_and >()) media_query->append(parse_media_expression());
Packit Service 7770af
    if (lex < identifier_schema >()) {
Packit Service 7770af
      String_Schema_Ptr schema = SASS_MEMORY_NEW(String_Schema, pstate);
Packit Service 7770af
      schema->append(media_query->media_type());
Packit Service 7770af
      schema->append(SASS_MEMORY_NEW(String_Constant, pstate, " "));
Packit Service 7770af
      schema->append(parse_identifier_schema());
Packit Service 7770af
      media_query->media_type(schema);
Packit Service 7770af
    }
Packit Service 7770af
    while (lex_css < kwd_and >()) media_query->append(parse_media_expression());
Packit Service 7770af
Packit Service 7770af
    media_query->update_pstate(pstate);
Packit Service 7770af
Packit Service 7770af
    return media_query;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Media_Query_Expression_Obj Parser::parse_media_expression()
Packit Service 7770af
  {
Packit Service 7770af
    if (lex < identifier_schema >()) {
Packit Service 7770af
      String_Obj ss = parse_identifier_schema();
Packit Service 7770af
      return SASS_MEMORY_NEW(Media_Query_Expression, pstate, ss, 0, true);
Packit Service 7770af
    }
Packit Service 7770af
    if (!lex_css< exactly<'('> >()) {
Packit Service 7770af
      error("media query expression must begin with '('", pstate);
Packit Service 7770af
    }
Packit Service 7770af
    Expression_Obj feature;
Packit Service 7770af
    if (peek_css< exactly<')'> >()) {
Packit Service 7770af
      error("media feature required in media query expression", pstate);
Packit Service 7770af
    }
Packit Service 7770af
    feature = parse_expression();
Packit Service 7770af
    Expression_Obj expression = 0;
Packit Service 7770af
    if (lex_css< exactly<':'> >()) {
Packit Service 7770af
      expression = parse_list(DELAYED);
Packit Service 7770af
    }
Packit Service 7770af
    if (!lex_css< exactly<')'> >()) {
Packit Service 7770af
      error("unclosed parenthesis in media query expression", pstate);
Packit Service 7770af
    }
Packit Service 7770af
    return SASS_MEMORY_NEW(Media_Query_Expression, feature->pstate(), feature, expression);
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  // lexed after `kwd_supports_directive`
Packit Service 7770af
  // these are very similar to media blocks
Packit Service 7770af
  Supports_Block_Obj Parser::parse_supports_directive()
Packit Service 7770af
  {
Packit Service 7770af
    Supports_Condition_Obj cond = parse_supports_condition();
Packit Service 7770af
    if (!cond) {
Packit Service 7770af
      css_error("Invalid CSS", " after ", ": expected @supports condition (e.g. (display: flexbox)), was ", false);
Packit Service 7770af
    }
Packit Service 7770af
    // create the ast node object for the support queries
Packit Service 7770af
    Supports_Block_Obj query = SASS_MEMORY_NEW(Supports_Block, pstate, cond);
Packit Service 7770af
    // additional block is mandatory
Packit Service 7770af
    // parse inner block
Packit Service 7770af
    query->block(parse_block());
Packit Service 7770af
    // return ast node
Packit Service 7770af
    return query;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  // parse one query operation
Packit Service 7770af
  // may encounter nested queries
Packit Service 7770af
  Supports_Condition_Obj Parser::parse_supports_condition()
Packit Service 7770af
  {
Packit Service 7770af
    lex < css_whitespace >();
Packit Service 7770af
    Supports_Condition_Obj cond;
Packit Service 7770af
    if ((cond = parse_supports_negation())) return cond;
Packit Service 7770af
    if ((cond = parse_supports_operator())) return cond;
Packit Service 7770af
    if ((cond = parse_supports_interpolation())) return cond;
Packit Service 7770af
    return cond;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Supports_Condition_Obj Parser::parse_supports_negation()
Packit Service 7770af
  {
Packit Service 7770af
    if (!lex < kwd_not >()) return 0;
Packit Service 7770af
    Supports_Condition_Obj cond = parse_supports_condition_in_parens();
Packit Service 7770af
    return SASS_MEMORY_NEW(Supports_Negation, pstate, cond);
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Supports_Condition_Obj Parser::parse_supports_operator()
Packit Service 7770af
  {
Packit Service 7770af
    Supports_Condition_Obj cond = parse_supports_condition_in_parens();
Packit Service 7770af
    if (cond.isNull()) return 0;
Packit Service 7770af
Packit Service 7770af
    while (true) {
Packit Service 7770af
      Supports_Operator::Operand op = Supports_Operator::OR;
Packit Service 7770af
      if (lex < kwd_and >()) { op = Supports_Operator::AND; }
Packit Service 7770af
      else if(!lex < kwd_or >()) { break; }
Packit Service 7770af
Packit Service 7770af
      lex < css_whitespace >();
Packit Service 7770af
      Supports_Condition_Obj right = parse_supports_condition_in_parens();
Packit Service 7770af
Packit Service 7770af
      // Supports_Condition_Ptr cc = SASS_MEMORY_NEW(Supports_Condition, *static_cast<Supports_Condition_Ptr>(cond));
Packit Service 7770af
      cond = SASS_MEMORY_NEW(Supports_Operator, pstate, cond, right, op);
Packit Service 7770af
    }
Packit Service 7770af
    return cond;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Supports_Condition_Obj Parser::parse_supports_interpolation()
Packit Service 7770af
  {
Packit Service 7770af
    if (!lex < interpolant >()) return 0;
Packit Service 7770af
Packit Service 7770af
    String_Obj interp = parse_interpolated_chunk(lexed);
Packit Service 7770af
    if (!interp) return 0;
Packit Service 7770af
Packit Service 7770af
    return SASS_MEMORY_NEW(Supports_Interpolation, pstate, interp);
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  // TODO: This needs some major work. Although feature conditions
Packit Service 7770af
  // look like declarations their semantics differ significantly
Packit Service 7770af
  Supports_Condition_Obj Parser::parse_supports_declaration()
Packit Service 7770af
  {
Packit Service 7770af
    Supports_Condition_Ptr cond;
Packit Service 7770af
    // parse something declaration like
Packit Service 7770af
    Declaration_Obj declaration = parse_declaration();
Packit Service 7770af
    if (!declaration) error("@supports condition expected declaration", pstate);
Packit Service 7770af
    cond = SASS_MEMORY_NEW(Supports_Declaration,
Packit Service 7770af
                     declaration->pstate(),
Packit Service 7770af
                     declaration->property(),
Packit Service 7770af
                     declaration->value());
Packit Service 7770af
    // ToDo: maybe we need an additional error condition?
Packit Service 7770af
    return cond;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Supports_Condition_Obj Parser::parse_supports_condition_in_parens()
Packit Service 7770af
  {
Packit Service 7770af
    Supports_Condition_Obj interp = parse_supports_interpolation();
Packit Service 7770af
    if (interp != 0) return interp;
Packit Service 7770af
Packit Service 7770af
    if (!lex < exactly <'('> >()) return 0;
Packit Service 7770af
    lex < css_whitespace >();
Packit Service 7770af
Packit Service 7770af
    Supports_Condition_Obj cond = parse_supports_condition();
Packit Service 7770af
    if (cond != 0) {
Packit Service 7770af
      if (!lex < exactly <')'> >()) error("unclosed parenthesis in @supports declaration", pstate);
Packit Service 7770af
    } else {
Packit Service 7770af
      cond = parse_supports_declaration();
Packit Service 7770af
      if (!lex < exactly <')'> >()) error("unclosed parenthesis in @supports declaration", pstate);
Packit Service 7770af
    }
Packit Service 7770af
    lex < css_whitespace >();
Packit Service 7770af
    return cond;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  At_Root_Block_Obj Parser::parse_at_root_block()
Packit Service 7770af
  {
Packit Service 7770af
    ParserState at_source_position = pstate;
Packit Service 7770af
    Block_Obj body = 0;
Packit Service 7770af
    At_Root_Query_Obj expr;
Packit Service 7770af
    Lookahead lookahead_result;
Packit Service 7770af
    if (lex_css< exactly<'('> >()) {
Packit Service 7770af
      expr = parse_at_root_query();
Packit Service 7770af
    }
Packit Service 7770af
    if (peek_css < exactly<'{'> >()) {
Packit Service 7770af
      lex <optional_spaces>();
Packit Service 7770af
      body = parse_block(true);
Packit Service 7770af
    }
Packit Service 7770af
    else if ((lookahead_result = lookahead_for_selector(position)).found) {
Packit Service 7770af
      Ruleset_Obj r = parse_ruleset(lookahead_result);
Packit Service 7770af
      body = SASS_MEMORY_NEW(Block, r->pstate(), 1, true);
Packit Service 7770af
      body->append(r);
Packit Service 7770af
    }
Packit Service 7770af
    At_Root_Block_Obj at_root = SASS_MEMORY_NEW(At_Root_Block, at_source_position, body);
Packit Service 7770af
    if (!expr.isNull()) at_root->expression(expr);
Packit Service 7770af
    return at_root;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  At_Root_Query_Obj Parser::parse_at_root_query()
Packit Service 7770af
  {
Packit Service 7770af
    if (peek< exactly<')'> >()) error("at-root feature required in at-root expression", pstate);
Packit Service 7770af
Packit Service 7770af
    if (!peek< alternatives< kwd_with_directive, kwd_without_directive > >()) {
Packit Service 7770af
      css_error("Invalid CSS", " after ", ": expected \"with\" or \"without\", was ");
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Expression_Obj feature = parse_list();
Packit Service 7770af
    if (!lex_css< exactly<':'> >()) error("style declaration must contain a value", pstate);
Packit Service 7770af
    Expression_Obj expression = parse_list();
Packit Service 7770af
    List_Obj value = SASS_MEMORY_NEW(List, feature->pstate(), 1);
Packit Service 7770af
Packit Service 7770af
    if (expression->concrete_type() == Expression::LIST) {
Packit Service 7770af
        value = Cast<List>(expression);
Packit Service 7770af
    }
Packit Service 7770af
    else value->append(expression);
Packit Service 7770af
Packit Service 7770af
    At_Root_Query_Obj cond = SASS_MEMORY_NEW(At_Root_Query,
Packit Service 7770af
                                          value->pstate(),
Packit Service 7770af
                                          feature,
Packit Service 7770af
                                          value);
Packit Service 7770af
    if (!lex_css< exactly<')'> >()) error("unclosed parenthesis in @at-root expression", pstate);
Packit Service 7770af
    return cond;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Directive_Obj Parser::parse_special_directive()
Packit Service 7770af
  {
Packit Service 7770af
    std::string kwd(lexed);
Packit Service 7770af
Packit Service 7770af
    if (lexed == "@else") error("Invalid CSS: @else must come after @if", pstate);
Packit Service 7770af
Packit Service 7770af
    // this whole branch is never hit via spec tests
Packit Service 7770af
Packit Service 7770af
    Directive_Ptr at_rule = SASS_MEMORY_NEW(Directive, pstate, kwd);
Packit Service 7770af
    Lookahead lookahead = lookahead_for_include(position);
Packit Service 7770af
    if (lookahead.found && !lookahead.has_interpolants) {
Packit Service 7770af
      at_rule->selector(parse_selector_list(false));
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    lex < css_comments >(false);
Packit Service 7770af
Packit Service 7770af
    if (lex < static_property >()) {
Packit Service 7770af
      at_rule->value(parse_interpolated_chunk(Token(lexed)));
Packit Service 7770af
    } else if (!(peek < alternatives < exactly<'{'>, exactly<'}'>, exactly<';'> > >())) {
Packit Service 7770af
      at_rule->value(parse_list());
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    lex < css_comments >(false);
Packit Service 7770af
Packit Service 7770af
    if (peek< exactly<'{'> >()) {
Packit Service 7770af
      at_rule->block(parse_block());
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    return at_rule;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  // this whole branch is never hit via spec tests
Packit Service 7770af
  Directive_Obj Parser::parse_prefixed_directive()
Packit Service 7770af
  {
Packit Service 7770af
    std::string kwd(lexed);
Packit Service 7770af
Packit Service 7770af
    if (lexed == "@else") error("Invalid CSS: @else must come after @if", pstate);
Packit Service 7770af
Packit Service 7770af
    Directive_Obj at_rule = SASS_MEMORY_NEW(Directive, pstate, kwd);
Packit Service 7770af
    Lookahead lookahead = lookahead_for_include(position);
Packit Service 7770af
    if (lookahead.found && !lookahead.has_interpolants) {
Packit Service 7770af
      at_rule->selector(parse_selector_list(false));
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    lex < css_comments >(false);
Packit Service 7770af
Packit Service 7770af
    if (lex < static_property >()) {
Packit Service 7770af
      at_rule->value(parse_interpolated_chunk(Token(lexed)));
Packit Service 7770af
    } else if (!(peek < alternatives < exactly<'{'>, exactly<'}'>, exactly<';'> > >())) {
Packit Service 7770af
      at_rule->value(parse_list());
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    lex < css_comments >(false);
Packit Service 7770af
Packit Service 7770af
    if (peek< exactly<'{'> >()) {
Packit Service 7770af
      at_rule->block(parse_block());
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    return at_rule;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
Packit Service 7770af
  Directive_Obj Parser::parse_directive()
Packit Service 7770af
  {
Packit Service 7770af
    Directive_Obj directive = SASS_MEMORY_NEW(Directive, pstate, lexed);
Packit Service 7770af
    String_Schema_Obj val = parse_almost_any_value();
Packit Service 7770af
    // strip left and right if they are of type string
Packit Service 7770af
    directive->value(val);
Packit Service 7770af
    if (peek< exactly<'{'> >()) {
Packit Service 7770af
      directive->block(parse_block());
Packit Service 7770af
    }
Packit Service 7770af
    return directive;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Expression_Obj Parser::lex_interpolation()
Packit Service 7770af
  {
Packit Service 7770af
    if (lex < interpolant >(true) != NULL) {
Packit Service 7770af
      return parse_interpolated_chunk(lexed, true);
Packit Service 7770af
    }
Packit Service 7770af
    return 0;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Expression_Obj Parser::lex_interp_uri()
Packit Service 7770af
  {
Packit Service 7770af
    // create a string schema by lexing optional interpolations
Packit Service 7770af
    return lex_interp< re_string_uri_open, re_string_uri_close >();
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Expression_Obj Parser::lex_interp_string()
Packit Service 7770af
  {
Packit Service 7770af
    Expression_Obj rv;
Packit Service 7770af
    if ((rv = lex_interp< re_string_double_open, re_string_double_close >())) return rv;
Packit Service 7770af
    if ((rv = lex_interp< re_string_single_open, re_string_single_close >())) return rv;
Packit Service 7770af
    return rv;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Expression_Obj Parser::lex_almost_any_value_chars()
Packit Service 7770af
  {
Packit Service 7770af
    const char* match =
Packit Service 7770af
    lex <
Packit Service 7770af
      one_plus <
Packit Service 7770af
        alternatives <
Packit Service 7770af
          sequence <
Packit Service 7770af
            exactly <'\\'>,
Packit Service 7770af
            any_char
Packit Service 7770af
          >,
Packit Service 7770af
          sequence <
Packit Service 7770af
            negate <
Packit Service 7770af
              sequence <
Packit Service 7770af
                exactly < url_kwd >,
Packit Service 7770af
                exactly <'('>
Packit Service 7770af
              >
Packit Service 7770af
            >,
Packit Service 7770af
            neg_class_char <
Packit Service 7770af
              almost_any_value_class
Packit Service 7770af
            >
Packit Service 7770af
          >,
Packit Service 7770af
          sequence <
Packit Service 7770af
            exactly <'/'>,
Packit Service 7770af
            negate <
Packit Service 7770af
              alternatives <
Packit Service 7770af
                exactly <'/'>,
Packit Service 7770af
                exactly <'*'>
Packit Service 7770af
              >
Packit Service 7770af
            >
Packit Service 7770af
          >,
Packit Service 7770af
          sequence <
Packit Service 7770af
            exactly <'\\'>,
Packit Service 7770af
            exactly <'#'>,
Packit Service 7770af
            negate <
Packit Service 7770af
              exactly <'{'>
Packit Service 7770af
            >
Packit Service 7770af
          >,
Packit Service 7770af
          sequence <
Packit Service 7770af
            exactly <'!'>,
Packit Service 7770af
            negate <
Packit Service 7770af
              alpha
Packit Service 7770af
            >
Packit Service 7770af
          >
Packit Service 7770af
        >
Packit Service 7770af
      >
Packit Service 7770af
    >(false);
Packit Service 7770af
    if (match) {
Packit Service 7770af
      return SASS_MEMORY_NEW(String_Constant, pstate, lexed);
Packit Service 7770af
    }
Packit Service 7770af
    return NULL;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Expression_Obj Parser::lex_almost_any_value_token()
Packit Service 7770af
  {
Packit Service 7770af
    Expression_Obj rv;
Packit Service 7770af
    if (*position == 0) return 0;
Packit Service 7770af
    if ((rv = lex_almost_any_value_chars())) return rv;
Packit Service 7770af
    // if ((rv = lex_block_comment())) return rv;
Packit Service 7770af
    // if ((rv = lex_single_line_comment())) return rv;
Packit Service 7770af
    if ((rv = lex_interp_string())) return rv;
Packit Service 7770af
    if ((rv = lex_interp_uri())) return rv;
Packit Service 7770af
    if ((rv = lex_interpolation())) return rv;
Packit Service 7770af
    return rv;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  String_Schema_Obj Parser::parse_almost_any_value()
Packit Service 7770af
  {
Packit Service 7770af
Packit Service 7770af
    String_Schema_Obj schema = SASS_MEMORY_NEW(String_Schema, pstate);
Packit Service 7770af
    if (*position == 0) return 0;
Packit Service 7770af
    lex < spaces >(false);
Packit Service 7770af
    Expression_Obj token = lex_almost_any_value_token();
Packit Service 7770af
    if (!token) return 0;
Packit Service 7770af
    schema->append(token);
Packit Service 7770af
    if (*position == 0) {
Packit Service 7770af
      schema->rtrim();
Packit Service 7770af
      return schema.detach();
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    while ((token = lex_almost_any_value_token())) {
Packit Service 7770af
      schema->append(token);
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    lex < css_whitespace >();
Packit Service 7770af
Packit Service 7770af
    schema->rtrim();
Packit Service 7770af
Packit Service 7770af
    return schema.detach();
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Warning_Obj Parser::parse_warning()
Packit Service 7770af
  {
Packit Service 7770af
    if (stack.back() != Scope::Root &&
Packit Service 7770af
        stack.back() != Scope::Function &&
Packit Service 7770af
        stack.back() != Scope::Mixin &&
Packit Service 7770af
        stack.back() != Scope::Control &&
Packit Service 7770af
        stack.back() != Scope::Rules) {
Packit Service 7770af
      error("Illegal nesting: Only properties may be nested beneath properties.", pstate);
Packit Service 7770af
    }
Packit Service 7770af
    return SASS_MEMORY_NEW(Warning, pstate, parse_list(DELAYED));
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Error_Obj Parser::parse_error()
Packit Service 7770af
  {
Packit Service 7770af
    if (stack.back() != Scope::Root &&
Packit Service 7770af
        stack.back() != Scope::Function &&
Packit Service 7770af
        stack.back() != Scope::Mixin &&
Packit Service 7770af
        stack.back() != Scope::Control &&
Packit Service 7770af
        stack.back() != Scope::Rules) {
Packit Service 7770af
      error("Illegal nesting: Only properties may be nested beneath properties.", pstate);
Packit Service 7770af
    }
Packit Service 7770af
    return SASS_MEMORY_NEW(Error, pstate, parse_list(DELAYED));
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Debug_Obj Parser::parse_debug()
Packit Service 7770af
  {
Packit Service 7770af
    if (stack.back() != Scope::Root &&
Packit Service 7770af
        stack.back() != Scope::Function &&
Packit Service 7770af
        stack.back() != Scope::Mixin &&
Packit Service 7770af
        stack.back() != Scope::Control &&
Packit Service 7770af
        stack.back() != Scope::Rules) {
Packit Service 7770af
      error("Illegal nesting: Only properties may be nested beneath properties.", pstate);
Packit Service 7770af
    }
Packit Service 7770af
    return SASS_MEMORY_NEW(Debug, pstate, parse_list(DELAYED));
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Return_Obj Parser::parse_return_directive()
Packit Service 7770af
  {
Packit Service 7770af
    // check that we do not have an empty list (ToDo: check if we got all cases)
Packit Service 7770af
    if (peek_css < alternatives < exactly < ';' >, exactly < '}' >, end_of_file > >())
Packit Service 7770af
    { css_error("Invalid CSS", " after ", ": expected expression (e.g. 1px, bold), was "); }
Packit Service 7770af
    return SASS_MEMORY_NEW(Return, pstate, parse_list());
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Lookahead Parser::lookahead_for_selector(const char* start)
Packit Service 7770af
  {
Packit Service 7770af
    // init result struct
Packit Service 7770af
    Lookahead rv = Lookahead();
Packit Service 7770af
    // get start position
Packit Service 7770af
    const char* p = start ? start : position;
Packit Service 7770af
    // match in one big "regex"
Packit Service 7770af
    rv.error = p;
Packit Service 7770af
    if (const char* q =
Packit Service 7770af
      peek <
Packit Service 7770af
        re_selector_list
Packit Service 7770af
      >(p)
Packit Service 7770af
    ) {
Packit Service 7770af
      while (p < q) {
Packit Service 7770af
        // did we have interpolations?
Packit Service 7770af
        if (*p == '#' && *(p+1) == '{') {
Packit Service 7770af
          rv.has_interpolants = true;
Packit Service 7770af
          p = q; break;
Packit Service 7770af
        }
Packit Service 7770af
        ++ p;
Packit Service 7770af
      }
Packit Service 7770af
      // store anyway  }
Packit Service 7770af
Packit Service 7770af
Packit Service 7770af
      // ToDo: remove
Packit Service 7770af
      rv.error = q;
Packit Service 7770af
      rv.position = q;
Packit Service 7770af
      // check expected opening bracket
Packit Service 7770af
      // only after successfull matching
Packit Service 7770af
      if (peek < exactly<'{'> >(q)) rv.found = q;
Packit Service 7770af
      // else if (peek < end_of_file >(q)) rv.found = q;
Packit Service 7770af
      else if (peek < exactly<'('> >(q)) rv.found = q;
Packit Service 7770af
      // else if (peek < exactly<';'> >(q)) rv.found = q;
Packit Service 7770af
      // else if (peek < exactly<'}'> >(q)) rv.found = q;
Packit Service 7770af
      if (rv.found || *p == 0) rv.error = 0;
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    rv.parsable = ! rv.has_interpolants;
Packit Service 7770af
Packit Service 7770af
    // return result
Packit Service 7770af
    return rv;
Packit Service 7770af
Packit Service 7770af
  }
Packit Service 7770af
  // EO lookahead_for_selector
Packit Service 7770af
Packit Service 7770af
  // used in parse_block_nodes and parse_special_directive
Packit Service 7770af
  // ToDo: actual usage is still not really clear to me?
Packit Service 7770af
  Lookahead Parser::lookahead_for_include(const char* start)
Packit Service 7770af
  {
Packit Service 7770af
    // we actually just lookahead for a selector
Packit Service 7770af
    Lookahead rv = lookahead_for_selector(start);
Packit Service 7770af
    // but the "found" rules are different
Packit Service 7770af
    if (const char* p = rv.position) {
Packit Service 7770af
      // check for additional abort condition
Packit Service 7770af
      if (peek < exactly<';'> >(p)) rv.found = p;
Packit Service 7770af
      else if (peek < exactly<'}'> >(p)) rv.found = p;
Packit Service 7770af
    }
Packit Service 7770af
    // return result
Packit Service 7770af
    return rv;
Packit Service 7770af
  }
Packit Service 7770af
  // EO lookahead_for_include
Packit Service 7770af
Packit Service 7770af
  // look ahead for a token with interpolation in it
Packit Service 7770af
  // we mostly use the result if there is an interpolation
Packit Service 7770af
  // everything that passes here gets parsed as one schema
Packit Service 7770af
  // meaning it will not be parsed as a space separated list
Packit Service 7770af
  Lookahead Parser::lookahead_for_value(const char* start)
Packit Service 7770af
  {
Packit Service 7770af
    // init result struct
Packit Service 7770af
    Lookahead rv = Lookahead();
Packit Service 7770af
    // get start position
Packit Service 7770af
    const char* p = start ? start : position;
Packit Service 7770af
    // match in one big "regex"
Packit Service 7770af
    if (const char* q =
Packit Service 7770af
      peek <
Packit Service 7770af
        non_greedy <
Packit Service 7770af
          alternatives <
Packit Service 7770af
            // consume whitespace
Packit Service 7770af
            block_comment, // spaces,
Packit Service 7770af
            // main tokens
Packit Service 7770af
            sequence <
Packit Service 7770af
              interpolant,
Packit Service 7770af
              optional <
Packit Service 7770af
                quoted_string
Packit Service 7770af
              >
Packit Service 7770af
            >,
Packit Service 7770af
            identifier,
Packit Service 7770af
            variable,
Packit Service 7770af
            // issue #442
Packit Service 7770af
            sequence <
Packit Service 7770af
              parenthese_scope,
Packit Service 7770af
              interpolant,
Packit Service 7770af
              optional <
Packit Service 7770af
                quoted_string
Packit Service 7770af
              >
Packit Service 7770af
            >
Packit Service 7770af
          >,
Packit Service 7770af
          sequence <
Packit Service 7770af
            // optional_spaces,
Packit Service 7770af
            alternatives <
Packit Service 7770af
              // end_of_file,
Packit Service 7770af
              exactly<'{'>,
Packit Service 7770af
              exactly<'}'>,
Packit Service 7770af
              exactly<';'>
Packit Service 7770af
            >
Packit Service 7770af
          >
Packit Service 7770af
        >
Packit Service 7770af
      >(p)
Packit Service 7770af
    ) {
Packit Service 7770af
      if (p == q) return rv;
Packit Service 7770af
      while (p < q) {
Packit Service 7770af
        // did we have interpolations?
Packit Service 7770af
        if (*p == '#' && *(p+1) == '{') {
Packit Service 7770af
          rv.has_interpolants = true;
Packit Service 7770af
          p = q; break;
Packit Service 7770af
        }
Packit Service 7770af
        ++ p;
Packit Service 7770af
      }
Packit Service 7770af
      // store anyway
Packit Service 7770af
      // ToDo: remove
Packit Service 7770af
      rv.position = q;
Packit Service 7770af
      // check expected opening bracket
Packit Service 7770af
      // only after successful matching
Packit Service 7770af
      if (peek < exactly<'{'> >(q)) rv.found = q;
Packit Service 7770af
      else if (peek < exactly<';'> >(q)) rv.found = q;
Packit Service 7770af
      else if (peek < exactly<'}'> >(q)) rv.found = q;
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    // return result
Packit Service 7770af
    return rv;
Packit Service 7770af
  }
Packit Service 7770af
  // EO lookahead_for_value
Packit Service 7770af
Packit Service 7770af
  void Parser::read_bom()
Packit Service 7770af
  {
Packit Service 7770af
    size_t skip = 0;
Packit Service 7770af
    std::string encoding;
Packit Service 7770af
    bool utf_8 = false;
Packit Service 7770af
    switch ((unsigned char) source[0]) {
Packit Service 7770af
    case 0xEF:
Packit Service 7770af
      skip = check_bom_chars(source, end, utf_8_bom, 3);
Packit Service 7770af
      encoding = "UTF-8";
Packit Service 7770af
      utf_8 = true;
Packit Service 7770af
      break;
Packit Service 7770af
    case 0xFE:
Packit Service 7770af
      skip = check_bom_chars(source, end, utf_16_bom_be, 2);
Packit Service 7770af
      encoding = "UTF-16 (big endian)";
Packit Service 7770af
      break;
Packit Service 7770af
    case 0xFF:
Packit Service 7770af
      skip = check_bom_chars(source, end, utf_16_bom_le, 2);
Packit Service 7770af
      skip += (skip ? check_bom_chars(source, end, utf_32_bom_le, 4) : 0);
Packit Service 7770af
      encoding = (skip == 2 ? "UTF-16 (little endian)" : "UTF-32 (little endian)");
Packit Service 7770af
      break;
Packit Service 7770af
    case 0x00:
Packit Service 7770af
      skip = check_bom_chars(source, end, utf_32_bom_be, 4);
Packit Service 7770af
      encoding = "UTF-32 (big endian)";
Packit Service 7770af
      break;
Packit Service 7770af
    case 0x2B:
Packit Service 7770af
      skip = check_bom_chars(source, end, utf_7_bom_1, 4)
Packit Service 7770af
           | check_bom_chars(source, end, utf_7_bom_2, 4)
Packit Service 7770af
           | check_bom_chars(source, end, utf_7_bom_3, 4)
Packit Service 7770af
           | check_bom_chars(source, end, utf_7_bom_4, 4)
Packit Service 7770af
           | check_bom_chars(source, end, utf_7_bom_5, 5);
Packit Service 7770af
      encoding = "UTF-7";
Packit Service 7770af
      break;
Packit Service 7770af
    case 0xF7:
Packit Service 7770af
      skip = check_bom_chars(source, end, utf_1_bom, 3);
Packit Service 7770af
      encoding = "UTF-1";
Packit Service 7770af
      break;
Packit Service 7770af
    case 0xDD:
Packit Service 7770af
      skip = check_bom_chars(source, end, utf_ebcdic_bom, 4);
Packit Service 7770af
      encoding = "UTF-EBCDIC";
Packit Service 7770af
      break;
Packit Service 7770af
    case 0x0E:
Packit Service 7770af
      skip = check_bom_chars(source, end, scsu_bom, 3);
Packit Service 7770af
      encoding = "SCSU";
Packit Service 7770af
      break;
Packit Service 7770af
    case 0xFB:
Packit Service 7770af
      skip = check_bom_chars(source, end, bocu_1_bom, 3);
Packit Service 7770af
      encoding = "BOCU-1";
Packit Service 7770af
      break;
Packit Service 7770af
    case 0x84:
Packit Service 7770af
      skip = check_bom_chars(source, end, gb_18030_bom, 4);
Packit Service 7770af
      encoding = "GB-18030";
Packit Service 7770af
      break;
Packit Service 7770af
    default: break;
Packit Service 7770af
    }
Packit Service 7770af
    if (skip > 0 && !utf_8) error("only UTF-8 documents are currently supported; your document appears to be " + encoding, pstate);
Packit Service 7770af
    position += skip;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  size_t check_bom_chars(const char* src, const char *end, const unsigned char* bom, size_t len)
Packit Service 7770af
  {
Packit Service 7770af
    size_t skip = 0;
Packit Service 7770af
    if (src + len > end) return 0;
Packit Service 7770af
    for (size_t i = 0; i < len; ++i, ++skip) {
Packit Service 7770af
      if ((unsigned char) src[i] != bom[i]) return 0;
Packit Service 7770af
    }
Packit Service 7770af
    return skip;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
Packit Service 7770af
  Expression_Obj Parser::fold_operands(Expression_Obj base, std::vector<Expression_Obj>& operands, Operand op)
Packit Service 7770af
  {
Packit Service 7770af
    for (size_t i = 0, S = operands.size(); i < S; ++i) {
Packit Service 7770af
      base = SASS_MEMORY_NEW(Binary_Expression, base->pstate(), op, base, operands[i]);
Packit Service 7770af
    }
Packit Service 7770af
    return base;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Expression_Obj Parser::fold_operands(Expression_Obj base, std::vector<Expression_Obj>& operands, std::vector<Operand>& ops, size_t i)
Packit Service 7770af
  {
Packit Service 7770af
    if (String_Schema_Ptr schema = Cast<String_Schema>(base)) {
Packit Service 7770af
      // return schema;
Packit Service 7770af
      if (schema->has_interpolants()) {
Packit Service 7770af
        if (i + 1 < operands.size() && (
Packit Service 7770af
             (ops[0].operand == Sass_OP::EQ)
Packit Service 7770af
          || (ops[0].operand == Sass_OP::ADD)
Packit Service 7770af
          || (ops[0].operand == Sass_OP::DIV)
Packit Service 7770af
          || (ops[0].operand == Sass_OP::MUL)
Packit Service 7770af
          || (ops[0].operand == Sass_OP::NEQ)
Packit Service 7770af
          || (ops[0].operand == Sass_OP::LT)
Packit Service 7770af
          || (ops[0].operand == Sass_OP::GT)
Packit Service 7770af
          || (ops[0].operand == Sass_OP::LTE)
Packit Service 7770af
          || (ops[0].operand == Sass_OP::GTE)
Packit Service 7770af
        )) {
Packit Service 7770af
          Expression_Obj rhs = fold_operands(operands[i], operands, ops, i + 1);
Packit Service 7770af
          rhs = SASS_MEMORY_NEW(Binary_Expression, base->pstate(), ops[0], schema, rhs);
Packit Service 7770af
          return rhs;
Packit Service 7770af
        }
Packit Service 7770af
        // return schema;
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    for (size_t S = operands.size(); i < S; ++i) {
Packit Service 7770af
      if (String_Schema_Ptr schema = Cast<String_Schema>(operands[i])) {
Packit Service 7770af
        if (schema->has_interpolants()) {
Packit Service 7770af
          if (i + 1 < S) {
Packit Service 7770af
            // this whole branch is never hit via spec tests
Packit Service 7770af
            Expression_Obj rhs = fold_operands(operands[i+1], operands, ops, i + 2);
Packit Service 7770af
            rhs = SASS_MEMORY_NEW(Binary_Expression, base->pstate(), ops[i], schema, rhs);
Packit Service 7770af
            base = SASS_MEMORY_NEW(Binary_Expression, base->pstate(), ops[i], base, rhs);
Packit Service 7770af
            return base;
Packit Service 7770af
          }
Packit Service 7770af
          base = SASS_MEMORY_NEW(Binary_Expression, base->pstate(), ops[i], base, operands[i]);
Packit Service 7770af
          return base;
Packit Service 7770af
        } else {
Packit Service 7770af
          base = SASS_MEMORY_NEW(Binary_Expression, base->pstate(), ops[i], base, operands[i]);
Packit Service 7770af
        }
Packit Service 7770af
      } else {
Packit Service 7770af
        base = SASS_MEMORY_NEW(Binary_Expression, base->pstate(), ops[i], base, operands[i]);
Packit Service 7770af
      }
Packit Service 7770af
      Binary_Expression_Ptr b = Cast<Binary_Expression>(base.ptr());
Packit Service 7770af
      if (b && ops[i].operand == Sass_OP::DIV && b->left()->is_delayed() && b->right()->is_delayed()) {
Packit Service 7770af
        base->is_delayed(true);
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
    // nested binary expression are never to be delayed
Packit Service 7770af
    if (Binary_Expression_Ptr b = Cast<Binary_Expression>(base)) {
Packit Service 7770af
      if (Cast<Binary_Expression>(b->left())) base->set_delayed(false);
Packit Service 7770af
      if (Cast<Binary_Expression>(b->right())) base->set_delayed(false);
Packit Service 7770af
    }
Packit Service 7770af
    return base;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  void Parser::error(std::string msg, Position pos)
Packit Service 7770af
  {
Packit Service 7770af
    throw Exception::InvalidSass(ParserState(path, source, pos.line ? pos : before_token, Offset(0, 0)), msg);
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  // print a css parsing error with actual context information from parsed source
Packit Service 7770af
  void Parser::css_error(const std::string& msg, const std::string& prefix, const std::string& middle, const bool trim)
Packit Service 7770af
  {
Packit Service 7770af
    int max_len = 18;
Packit Service 7770af
    const char* end = this->end;
Packit Service 7770af
    while (*end != 0) ++ end;
Packit Service 7770af
    const char* pos = peek < optional_spaces >();
Packit Service 7770af
    if (!pos) pos = position;
Packit Service 7770af
Packit Service 7770af
    const char* last_pos(pos);
Packit Service 7770af
    if (last_pos > source) {
Packit Service 7770af
      utf8::prior(last_pos, source);
Packit Service 7770af
    }
Packit Service 7770af
    // backup position to last significant char
Packit Service 7770af
    while (trim && last_pos > source && last_pos < end) {
Packit Service 7770af
      if (!Prelexer::is_space(*last_pos)) break;
Packit Service 7770af
      utf8::prior(last_pos, source);
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    bool ellipsis_left = false;
Packit Service 7770af
    const char* pos_left(last_pos);
Packit Service 7770af
    const char* end_left(last_pos);
Packit Service 7770af
Packit Service 7770af
    if (*pos_left) utf8::next(pos_left, end);
Packit Service 7770af
    if (*end_left) utf8::next(end_left, end);
Packit Service 7770af
    while (pos_left > source) {
Packit Service 7770af
      if (utf8::distance(pos_left, end_left) >= max_len) {
Packit Service 7770af
        utf8::prior(pos_left, source);
Packit Service 7770af
        ellipsis_left = *(pos_left) != '\n' &&
Packit Service 7770af
                        *(pos_left) != '\r';
Packit Service 7770af
        utf8::next(pos_left, end);
Packit Service 7770af
        break;
Packit Service 7770af
      }
Packit Service 7770af
Packit Service 7770af
      const char* prev = pos_left;
Packit Service 7770af
      utf8::prior(prev, source);
Packit Service 7770af
      if (*prev == '\r') break;
Packit Service 7770af
      if (*prev == '\n') break;
Packit Service 7770af
      pos_left = prev;
Packit Service 7770af
    }
Packit Service 7770af
    if (pos_left < source) {
Packit Service 7770af
      pos_left = source;
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    bool ellipsis_right = false;
Packit Service 7770af
    const char* end_right(pos);
Packit Service 7770af
    const char* pos_right(pos);
Packit Service 7770af
    while (end_right < end) {
Packit Service 7770af
      if (utf8::distance(pos_right, end_right) > max_len) {
Packit Service 7770af
        ellipsis_left = *(pos_right) != '\n' &&
Packit Service 7770af
                        *(pos_right) != '\r';
Packit Service 7770af
        break;
Packit Service 7770af
      }
Packit Service 7770af
      if (*end_right == '\r') break;
Packit Service 7770af
      if (*end_right == '\n') break;
Packit Service 7770af
      utf8::next(end_right, end);
Packit Service 7770af
    }
Packit Service 7770af
    // if (*end_right == 0) end_right ++;
Packit Service 7770af
Packit Service 7770af
    std::string left(pos_left, end_left);
Packit Service 7770af
    std::string right(pos_right, end_right);
Packit Service 7770af
    size_t left_subpos = left.size() > 15 ? left.size() - 15 : 0;
Packit Service 7770af
    size_t right_subpos = right.size() > 15 ? right.size() - 15 : 0;
Packit Service 7770af
    if (left_subpos && ellipsis_left) left = ellipsis + left.substr(left_subpos);
Packit Service 7770af
    if (right_subpos && ellipsis_right) right = right.substr(right_subpos) + ellipsis;
Packit Service 7770af
    // now pass new message to the more generic error function
Packit Service 7770af
    error(msg + prefix + quote(left) + middle + quote(right), pstate);
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
}