summaryrefslogtreecommitdiff
path: root/libadhocutil/nvpParse.ll
diff options
context:
space:
mode:
Diffstat (limited to 'libadhocutil/nvpParse.ll')
-rw-r--r--libadhocutil/nvpParse.ll83
1 files changed, 83 insertions, 0 deletions
diff --git a/libadhocutil/nvpParse.ll b/libadhocutil/nvpParse.ll
new file mode 100644
index 0000000..b149b92
--- /dev/null
+++ b/libadhocutil/nvpParse.ll
@@ -0,0 +1,83 @@
+%option batch
+%option c++
+%option noyywrap
+%option 8bit
+%option stack
+%option yyclass="NvpParse"
+%option prefix="nvpBase"
+
+%{
+#include "nvpParse.h"
+#pragma GCC diagnostic ignored "-Wsign-compare"
+%}
+
+element [a-zA-Z][a-zA-Z0-9_-]*
+identifier {element}("."{element})*
+eq "="
+value [^ ][^;]*
+semi ";"
+
+%x VALUE
+%x EQUAL
+%x SEMI
+
+%%
+
+<INITIAL>{identifier} {
+ name = YYText();
+ BEGIN(EQUAL);
+}
+
+<EQUAL>{eq} {
+ BEGIN(VALUE);
+}
+
+<VALUE>{value} {
+ process(YYText());
+ BEGIN(SEMI);
+}
+
+<SEMI>{semi} {
+ BEGIN(INITIAL);
+}
+
+<*>[ \t\r\n\f] {
+}
+
+<*>. {
+ throw std::runtime_error(std::string("Lex error at: ") + YYText());
+}
+
+%%
+#include "safeMapFind.h"
+
+NvpParse::ValueNotFound::ValueNotFound(const std::string & vn) :
+ std::runtime_error("Value not found: " + vn)
+{
+}
+
+NvpParse::NvpParse(std::istream & in, const AssignMap & v) :
+ yyFlexLexer(&in),
+ values(v)
+{
+}
+
+void
+NvpParse::process(const std::string & value) const
+{
+ safeMapLookup<ValueNotFound>(values, name)(value);
+}
+
+void
+NvpParse::LexerError(const char * msg)
+{
+ throw std::runtime_error(msg);
+}
+
+void
+NvpParse::Parse(std::istream & in, const AssignMap & m)
+{
+ NvpParse p(in, m);
+ p.yylex();
+}
+