summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-12-28 22:44:25 -0500
committerJune McEnroe <june@causal.agency>2020-12-28 22:48:06 -0500
commit8cb2140bbeb2baa4db3de40e2b9b3c34914e4bcf (patch)
treea13b7190bd15772b95f599e2819279b8d3299fa3
parentLex strings inside macros (diff)
downloadsrc-8cb2140bbeb2baa4db3de40e2b9b3c34914e4bcf.tar.gz
src-8cb2140bbeb2baa4db3de40e2b9b3c34914e4bcf.zip
Add mdoc lexer
Diffstat (limited to '')
-rw-r--r--bin/hilex/Makefile1
-rw-r--r--bin/hilex/hilex.c2
-rw-r--r--bin/hilex/hilex.h1
-rw-r--r--bin/hilex/mdoc.l61
4 files changed, 65 insertions, 0 deletions
diff --git a/bin/hilex/Makefile b/bin/hilex/Makefile
index 4a930757..3fd11069 100644
--- a/bin/hilex/Makefile
+++ b/bin/hilex/Makefile
@@ -3,6 +3,7 @@ CFLAGS += -std=c11 -Wall -Wextra -Wpedantic
 OBJS += ansi.o
 OBJS += c.o
 OBJS += hilex.o
+OBJS += mdoc.o
 OBJS += text.o
 
 hilex: ${OBJS}
diff --git a/bin/hilex/hilex.c b/bin/hilex/hilex.c
index e973f0cd..aa8d4e05 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]$" },
+	{ &LexMdoc, "mdoc", "[.][1-9]$" },
 	{ &LexText, "text", "[.]txt$" },
 };
 
@@ -123,6 +124,7 @@ int main(int argc, char *argv[]) {
 	*lexer->in = file;
 	if (formatter->header) formatter->header(NULL);
 	for (enum Class class; None != (class = lexer->lex());) {
+		assert(class < ClassCap);
 		formatter->format(NULL, class, *lexer->text);
 	}
 	if (formatter->footer) formatter->footer(NULL);
diff --git a/bin/hilex/hilex.h b/bin/hilex/hilex.h
index 63e5a43b..ddd09fd5 100644
--- a/bin/hilex/hilex.h
+++ b/bin/hilex/hilex.h
@@ -46,6 +46,7 @@ struct Lexer {
 };
 
 extern const struct Lexer LexC;
+extern const struct Lexer LexMdoc;
 extern const struct Lexer LexText;
 
 typedef void Header(const char *opts[]);
diff --git a/bin/hilex/mdoc.l b/bin/hilex/mdoc.l
new file mode 100644
index 00000000..50e2f94f
--- /dev/null
+++ b/bin/hilex/mdoc.l
@@ -0,0 +1,61 @@
+/* Copyright (C) 2020  C. 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="mdoc"
+%option noyywrap
+
+%{
+#include "hilex.h"
+%}
+
+%s MacroLine
+
+%%
+
+^"." {
+	BEGIN(MacroLine);
+	return Keyword;
+}
+
+^".\\\"".* { return Comment; }
+
+<MacroLine>{
+	"\n" {
+		BEGIN(0);
+		return Normal;
+	}
+
+	%[ABCDIJNOPQRTUV]|A[cdnopqrt]|B[cdfkloqtx]|Br[coq]|Bsx|C[dm]|D[1bcdloqtvx] |
+	E[cdfklmnorsvx]|F[acdlnortx]|Hf|I[cnt]|L[bikp]|M[st]|N[dmosx]|O[copstx] |
+	P[acfopq]|Q[cloq]|R[esv]|S[chmoqstxy]|T[an]|U[dx]|V[at]|X[cor] {
+		return Keyword;
+	}
+
+	"\""([^""]|"\\\"")*"\"" { return String; }
+}
+
+"\\"(.|"("..|"["[^]]*"]") { return String; }
+
+[[:blank:]]+|[^.\n""\\[:space:]]+|.|\n { return Normal; }
+
+%{
+	(void)yyunput;
+	(void)input;
+%}
+
+%%
+
+const struct Lexer LexMdoc = { yylex, &yyin, &yytext };