libcruft-util/uri.cpp.rl

103 lines
2.8 KiB
Ragel

/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
*/
#include "uri.hpp"
#include <cruft/util/debug/assert.hpp>
#include <algorithm>
#include <iostream>
// We generate some really old style C code via ragel here, so we have to
// disable some noisy warnings (doubly so given -Werror)
#pragma GCC diagnostic ignored "-Wold-style-cast"
using cruft::uri;
///////////////////////////////////////////////////////////////////////////////
%%{
machine impl;
action trace { if (0) std::cerr << *p; }
action success {__success = true; }
action failure {__success = false; }
action scheme_begin { starts[SCHEME] = p; }
action scheme_end { CHECK (starts[SCHEME]); m_views[SCHEME] = { starts[SCHEME], p }; }
action hier_begin { starts[HIERARCHICAL] = p; }
action hier_end { CHECK (starts[HIERARCHICAL]); m_views[HIERARCHICAL] = { starts[HIERARCHICAL], p }; }
action user_begin { starts[USER] = p; }
action user_end { CHECK (starts[USER]); m_views[USER] = { starts[USER], p }; }
action host_begin { starts[HOST] = p; }
action host_end { CHECK (starts[HOST]); m_views[HOST] = { starts[HOST], p }; }
action port_begin { starts[PORT] = p; }
action port_end { CHECK (starts[PORT]); m_views[PORT] = { starts[PORT], p }; }
action authority_begin { starts[AUTHORITY] = p; }
action authority_end { CHECK (starts[AUTHORITY]); m_views[AUTHORITY] = { starts[AUTHORITY], p }; }
action path_begin { starts[PATH] = p; }
action path_end { CHECK (starts[PATH]); m_views[PATH] = { starts[PATH], p }; }
action query_begin { starts[QUERY] = p; }
action query_end { CHECK (starts[QUERY]); m_views[QUERY] = { starts[QUERY], p }; }
action fragment_begin { starts[FRAGMENT] = p; }
action fragment_end { CHECK (starts[FRAGMENT]); m_views[FRAGMENT] = { starts[FRAGMENT], p }; }
action uri_begin {}
action uri_end {}
include rfc3986 'rfc3986.rl';
impl := URI >uri_begin %uri_end
%success
$!failure
$trace;
write data;
}%%
///////////////////////////////////////////////////////////////////////////////
void
cruft::uri::parse (void)
{
char const *starts[NUM_COMPONENTS] = {};
m_views = {
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr
};
const char *p = m_value.data ();
const char *pe = m_value.data () + m_value.size ();
const char *eof = pe;
bool __success = false;
int cs;
%%write init;
%%write exec;
if (!__success)
throw parse_error ("invalid uri");
}