From 646d77d8156ad72da1c24f734a029a525ba4bed9 Mon Sep 17 00:00:00 2001 From: Samuel Mendoza-Jonas Date: Tue, 30 Oct 2018 15:26:04 +1100 Subject: 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 --- discover/native/Makefile.am | 54 +++++++++++ discover/native/native-lexer.l | 65 +++++++++++++ discover/native/native-parser.y | 200 ++++++++++++++++++++++++++++++++++++++++ discover/native/native.c | 55 +++++++++++ discover/native/native.h | 27 ++++++ 5 files changed, 401 insertions(+) create mode 100644 discover/native/Makefile.am create mode 100644 discover/native/native-lexer.l create mode 100644 discover/native/native-parser.y create mode 100644 discover/native/native.c create mode 100644 discover/native/native.h (limited to 'discover/native') diff --git a/discover/native/Makefile.am b/discover/native/Makefile.am new file mode 100644 index 0000000..f120e70 --- /dev/null +++ b/discover/native/Makefile.am @@ -0,0 +1,54 @@ +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; version 2 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# + +noinst_PROGRAMS += discover/native/native-parser.ro + +discover_native_native_parser_ro_SOURCES = \ + discover/native/native.h \ + discover/native/native.c \ + discover/native/native-lexer.l \ + discover/native/native-parser.y + +BUILT_SOURCES += \ + discover/native/native-parser.c \ + discover/native/native-parser.h \ + discover/native/native-lexer.h \ + discover/native/native-lexer.c + +CLEANFILES += \ + discover/native/native-parser.c \ + discover/native/native-parser.h \ + discover/native/native-lexer.c \ + discover/native/native-lexer.h + +discover_native_native_parser_ro_CPPFLAGS = \ + $(AM_CPPFLAGS) \ + -I$(top_srcdir)/discover/native \ + -I$(top_builddir)/discover/native + +discover_native_native_parser_ro_LINK = \ + $(LD) -r -o $@ + +# ylwrap doesn't handle flex header files well; use our own rule here. +discover/native/native-lexer.h discover/native/native-lexer.c: \ + $(top_srcdir)/discover/native/native-lexer.l + $(AM_V_LEX)$(LEXCOMPILE) --header-file=discover/native/native-lexer.h \ + -o discover/native/native-lexer.c $^ + +# We need to loosen our warnings for the generated lexer code. +discover/native/%native-lexer.o discover/native/native-lexer.o: \ + AM_CFLAGS += -Wno-unused-parameter -Wno-missing-prototypes \ + -Wno-missing-declarations -Wno-sign-compare + +$(discover_native_native_parser_ro_OBJECTS): discover/native/native-parser.h 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 + +#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} { ; } +