tokenlist* parse(tokenlist* tl, sexp** ps) { sexp* s; token* tok = tl->tok; switch (tok->type) { case OPENPAR_TOKEN: if (tl->next == NULL) { error("Unexpected end of input"); } if (tl->next->tok->type == CLOSEPAR_TOKEN) { *ps = &nil_sexp; return tl->next->next; } tl = tl->next; s = new_sexp(PAIR_SEXP); *ps = s; while (1) { tl = parse(tl, &car(s)); if (tl == NULL) { error("Unexpected end of input"); } if (tl->tok->type == CLOSEPAR_TOKEN) { cdr(s) = &nil_sexp; return tl->next; } cdr(s) = new_sexp(PAIR_SEXP); s = cdr(s); } case CLOSEPAR_TOKEN: error("Unexpected ')'"); case SYMBOL_TOKEN: *ps = symbol_sexp(tok->symbol); return tl->next; case NUMBER_TOKEN: *ps = number_sexp(tok->number); return tl->next; case BOOLEAN_TOKEN: *ps = boolean_sexp(tok->number); return tl->next; case QUOTE_TOKEN: tl = parse(tl->next, &s); *ps = quoted(s); return tl; case QQ_TOKEN: tl = parse(tl->next, &s); *ps = quasiquoted(s); return tl; case UNQUOTE_TOKEN: tl = parse(tl->next, &s); *ps = unquoted(s); return tl; case SPLICE_TOKEN: tl = parse(tl->next, &s); *ps = unquote_spliced(s); return tl; case STRING_TOKEN: *ps = string_sexp(tok->symbol); return tl->next; } }
/** * Return the attribute as an unquoted string if it is a string. */ std::string IFunction::Attribute::asUnquotedString() const { std::string attr; try { attr = boost::get<std::string>(m_data); } catch (...) { throw std::runtime_error("Trying to access a " + type() + " attribute " "as string"); } std::string unquoted(attr); if (attr.empty()) return ""; if (*(attr.begin()) == '\"') unquoted = std::string(attr.begin() + 1, attr.end() - 1); if (*(unquoted.end() - 1) == '\"') unquoted = std::string(unquoted.begin(), unquoted.end() - 1); return unquoted; }
bool option_parser::run(std::istream &is, bool ignore_errors) { // quoted value. leftmost and rightmost quotation marks are the delimiters. boost::regex quoted("(\\w+)\\h*=\\h*\"(.*)\"[^\"]*"); // unquoted value. whitespaces get trimmed. boost::regex unquoted("(\\w+)\\h*=\\h*(.*?)\\h*"); boost::smatch match; std::string line; while (std::getline(is, line)) { if (boost::regex_match(line, match, quoted) || boost::regex_match(line, match, unquoted)) { std::string option = match[1]; auto it = m_parsers.find(option); if (it != m_parsers.end()) { try { it->second.parse(match[2]); } catch (std::exception &e) { std::cerr << "Error while processing option \"" << option << "\": " << e.what() << "\n"; if (!ignore_errors) return false; } } else { std::cerr << "Unknown option: " << option << "\n"; if (!ignore_errors) return false; } } } return true; }