summaryrefslogtreecommitdiffstats
path: root/discover/native/native-lexer.l
diff options
context:
space:
mode:
authorSamuel Mendoza-Jonas <sam@mendozajonas.com>2018-10-30 15:26:04 +1100
committerSamuel Mendoza-Jonas <sam@mendozajonas.com>2018-11-16 13:55:13 +1100
commit646d77d8156ad72da1c24f734a029a525ba4bed9 (patch)
tree8eaa5db080f5c2b733b506fd82eec773ac2afdb3 /discover/native/native-lexer.l
parent638f16c7683db165154bbe53772c4b864f9dc90d (diff)
downloadtalos-petitboot-646d77d8156ad72da1c24f734a029a525ba4bed9.tar.gz
talos-petitboot-646d77d8156ad72da1c24f734a029a525ba4bed9.zip
discover: Reimplement native-parser as a Bison parser
Occasionally you look at some code and realise that a) this never gets built, and b) even if it did it would never compile. Today's example is native-parser.c which we must have just assumed worked for quite a while. The native parser has bitrotted entirely and needs to be brought up to date. While we're here, lets take the chance to implement a proper grammar for it. This helps us reason more effectively about the parser, lets us extend it easily in the future, and.. I wanted to write a Bison parser too. This implements most of the old functionality, but drops off some smaller details like settings icons which needs some separate attention to bring up to date. Signed-off-by: Samuel Mendoza-Jonas <sam@mendozajonas.com>
Diffstat (limited to 'discover/native/native-lexer.l')
-rw-r--r--discover/native/native-lexer.l65
1 files changed, 65 insertions, 0 deletions
diff --git a/discover/native/native-lexer.l b/discover/native/native-lexer.l
new file mode 100644
index 0000000..bf1408a
--- /dev/null
+++ b/discover/native/native-lexer.l
@@ -0,0 +1,65 @@
+%{
+#include "native.h"
+#include "native-parser.h"
+#include <talloc/talloc.h>
+
+#define YYSTYPE NSTYPE
+
+void yyerror(struct native_parser *parser, const char *fmt, ...);
+%}
+
+%option nounput noinput
+%option batch never-interactive
+%option warn
+%option noyywrap
+%option reentrant
+%option bison-bridge
+%option yylineno
+%option noyyalloc noyyfree noyyrealloc
+%option extra-type="struct native_parser *"
+%option prefix="n"
+
+%x label
+%x args
+
+DELIM [ \t]+
+NUMBER 0|[1-9][0-9]*
+WORDS [^\n]+
+NEWLINE [\n]+
+
+%%
+
+name { BEGIN(label); return TOKEN_NAME; }
+image { BEGIN(label); return TOKEN_IMAGE; }
+initrd { BEGIN(label); return TOKEN_INITRD; }
+args { BEGIN(label); return TOKEN_ARGS; }
+dtb { BEGIN(label); return TOKEN_DTB; }
+description { BEGIN(label); return TOKEN_DESCRIPTION; }
+default { BEGIN(label); return TOKEN_DEFAULT; }
+dev_description { BEGIN(label); return TOKEN_DEV_DESCRIPTION; }
+{DELIM} { ; }
+{NEWLINE} { ; }
+<label>{DELIM} { BEGIN(args); return TOKEN_DELIM; }
+<args>{WORDS} { yylval->word = strdup(yytext); return TOKEN_WORD; }
+<args>{NEWLINE} { BEGIN(INITIAL); ; }
+
+%%
+
+struct native_parser;
+
+void *yyalloc(size_t bytes, void *yyscanner)
+{
+ struct native_parser *parser = yyget_extra(yyscanner);
+ return talloc_size(parser, bytes);
+}
+
+void *yyrealloc(void *ptr, size_t bytes, void *yyscanner)
+{
+ struct native_parser *parser = yyget_extra(yyscanner);
+ return talloc_realloc_size(parser, ptr, bytes);
+}
+
+void yyfree(void *ptr, void *yyscanner __attribute__((unused)))
+{
+ talloc_free(ptr);
+}
OpenPOWER on IntegriCloud