summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-12-29 22:39:16 -0500
committerJune McEnroe <june@causal.agency>2020-12-29 22:39:16 -0500
commitb2de730b6105964949df8bcf3937704108ba12b0 (patch)
treed8a1653d32432b9cf5c4ae1df806c172f3f44cb6
parentClean up C lexer (diff)
downloadsrc-b2de730b6105964949df8bcf3937704108ba12b0.tar.gz
src-b2de730b6105964949df8bcf3937704108ba12b0.zip
Add make lexer
This is kind of a mess and needs to be cleaned up against more careful
reading of the make grammar.
-rw-r--r--bin/hilex/Makefile1
-rw-r--r--bin/hilex/ansi.c1
-rw-r--r--bin/hilex/hilex.120
-rw-r--r--bin/hilex/hilex.c1
-rw-r--r--bin/hilex/hilex.h4
-rw-r--r--bin/hilex/html.c1
-rw-r--r--bin/hilex/irc.c11
-rw-r--r--bin/hilex/make.l102
8 files changed, 130 insertions, 11 deletions
diff --git a/bin/hilex/Makefile b/bin/hilex/Makefile
index 5f6c1d80..c71738d6 100644
--- a/bin/hilex/Makefile
+++ b/bin/hilex/Makefile
@@ -5,6 +5,7 @@ OBJS += c.o
 OBJS += hilex.o
 OBJS += html.o
 OBJS += irc.o
+OBJS += make.o
 OBJS += mdoc.o
 OBJS += text.o
 
diff --git a/bin/hilex/ansi.c b/bin/hilex/ansi.c
index 0d337523..cdfcc845 100644
--- a/bin/hilex/ansi.c
+++ b/bin/hilex/ansi.c
@@ -27,6 +27,7 @@ static const char *SGR[ClassCap] = {
 	[Comment]       = "34",
 	[String]        = "36",
 	[StringFormat]  = "36;1;96",
+	[Interpolation] = "33",
 };
 
 static void ansiFormat(const char *opts[], enum Class class, const char *text) {
diff --git a/bin/hilex/hilex.1 b/bin/hilex/hilex.1
index eb429fb0..2b789f65 100644
--- a/bin/hilex/hilex.1
+++ b/bin/hilex/hilex.1
@@ -1,4 +1,4 @@
-.Dd December 28, 2020
+.Dd December 29, 2020
 .Dt HILEX 1
 .Os
 .
@@ -84,7 +84,8 @@ and one of the following:
 .Sy Comment ,
 .Sy String ,
 .Sy StringEscape ,
-.Sy StringFormat .
+.Sy StringFormat ,
+.Sy Interpolation .
 .
 .Pp
 The options are as follows:
@@ -151,21 +152,30 @@ with minimal support for
 .Xr lex 1 ,
 .Xr yacc 1
 and Objective-C input.
-Automatically inferred for
+Inferred for
 .Pa *.[chlmy]
 files.
 .
+.It Cm make
+The portable subset of
+.Xr make 1 .
+Inferred for
+.Pa *.mk
+and
+.Pa Makefile
+files.
+.
 .It Cm mdoc
 The
 .Xr mdoc 7
 language.
-Automatically inferred for
+Inferred for
 .Pa *.[1-9]
 files.
 .
 .It Cm text
 Plain text.
-Automatically inferred for
+Inferred for
 .Pa *.txt
 files.
 .El
diff --git a/bin/hilex/hilex.c b/bin/hilex/hilex.c
index b2bebd5f..1735b10a 100644
--- a/bin/hilex/hilex.c
+++ b/bin/hilex/hilex.c
@@ -32,6 +32,7 @@ static const struct {
 	const char *pattern;
 } Lexers[] = {
 	{ &LexC, "c", "[.][chlmy]$" },
+	{ &LexMake, "make", "[.]mk$|^Makefile$" },
 	{ &LexMdoc, "mdoc", "[.][1-9]$" },
 	{ &LexText, "text", "[.]txt$" },
 };
diff --git a/bin/hilex/hilex.h b/bin/hilex/hilex.h
index f9dcea24..fa16f1b6 100644
--- a/bin/hilex/hilex.h
+++ b/bin/hilex/hilex.h
@@ -30,7 +30,8 @@
 	X(Comment) \
 	X(String) \
 	X(StringEscape) \
-	X(StringFormat)
+	X(StringFormat) \
+	X(Interpolation)
 
 enum Class {
 #define X(class) class,
@@ -47,6 +48,7 @@ struct Lexer {
 };
 
 extern const struct Lexer LexC;
+extern const struct Lexer LexMake;
 extern const struct Lexer LexMdoc;
 extern const struct Lexer LexText;
 
diff --git a/bin/hilex/html.c b/bin/hilex/html.c
index c26d66ad..bec1d6a8 100644
--- a/bin/hilex/html.c
+++ b/bin/hilex/html.c
@@ -46,6 +46,7 @@ static const char *Style[ClassCap] = {
 	[Comment]       = "color: navy;",
 	[String]        = "color: teal;",
 	[StringFormat]  = "color: teal; font-weight: bold;",
+	[Interpolation] = "color: olive;",
 };
 
 static void styleTabSize(const char *tab) {
diff --git a/bin/hilex/irc.c b/bin/hilex/irc.c
index a83b55e6..deb35829 100644
--- a/bin/hilex/irc.c
+++ b/bin/hilex/irc.c
@@ -22,11 +22,12 @@
 #include "hilex.h"
 
 static const char *IRC[ClassCap] = {
-	[Keyword]      = "\00315",
-	[Macro]        = "\0033",
-	[Comment]      = "\0032",
-	[String]       = "\00310",
-	[StringFormat] = "\00311",
+	[Keyword]       = "\00315",
+	[Macro]         = "\0033",
+	[Comment]       = "\0032",
+	[String]        = "\00310",
+	[StringFormat]  = "\00311",
+	[Interpolation] = "\0037",
 };
 
 static void ircHeader(const char *opts[]) {
diff --git a/bin/hilex/make.l b/bin/hilex/make.l
new file mode 100644
index 00000000..00c1acca
--- /dev/null
+++ b/bin/hilex/make.l
@@ -0,0 +1,102 @@
+/* Copyright (C) 2020  June McEnroe <june@causal.agency>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * 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 Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+%option prefix="make"
+%option noyywrap
+
+%{
+#include "hilex.h"
+%}
+
+%s Assign
+%x Interp Shell
+
+ident [._[:alnum:]]+
+target [-/._[:alnum:]]+
+assign [!+:?]?=
+
+%%
+	static int pop = INITIAL;
+	static int depth = 0;
+
+^"\t" {
+	BEGIN(pop = Shell);
+	return Normal;
+}
+<Shell>{
+	"\n" {
+		BEGIN(pop = INITIAL);
+		return Normal;
+	}
+	[^\n$]+|. { return Normal; }
+}
+
+[[:blank:]] { return Normal; }
+
+"."(PHONY|PRECIOUS|SUFFIXES)/":"? {
+	return Keyword;
+}
+
+^{ident}/[[:blank:]]*{assign} {
+	return Identifier;
+}
+
+{assign} {
+	BEGIN(pop = Assign);
+	return Operator;
+}
+<Assign>{
+	"\n" {
+		BEGIN(pop = INITIAL);
+		return Normal;
+	}
+	[^$[:space:]]+ { return String; }
+}
+
+{target}([[:blank:]]+{target})*/":" {
+	return IdentifierTag;
+}
+
+^"-"?include { return Macro; }
+
+"#".* { return Comment; }
+
+<*>{
+	"$"("{"|"(") {
+		depth++;
+		BEGIN(Interp);
+		return Interpolation;
+	}
+	"$". { return Interpolation; }
+}
+<Interp>{
+	"}"|")" {
+		if (!--depth) BEGIN(pop);
+		return Interpolation;
+	}
+	[^${}()]+ { return Interpolation; }
+}
+
+.|\n { return Normal; }
+
+%{
+	(void)yyunput;
+	(void)input;
+%}
+
+%%
+
+const struct Lexer LexMake = { yylex, &yyin, &yytext };